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, cp_id_kind *);
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, cp_id_kind *);
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, cp_id_kind *);
1618 static tree cp_parser_binary_expression
1619 (cp_parser *, bool, enum cp_parser_prec, cp_id_kind *);
1620 static tree cp_parser_question_colon_clause
1621 (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623 (cp_parser *, bool, cp_id_kind *);
1624 static enum tree_code cp_parser_assignment_operator_opt
1626 static tree cp_parser_expression
1627 (cp_parser *, bool, cp_id_kind *);
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 if (TREE_CODE (token->u.value) == FIXED_CST)
3149 error ("%Hfixed-point types not supported in C++",
3151 return error_mark_node;
3153 /* Floating-point literals are only allowed in an integral
3154 constant expression if they are cast to an integral or
3155 enumeration type. */
3156 if (TREE_CODE (token->u.value) == REAL_CST
3157 && parser->integral_constant_expression_p
3160 /* CAST_P will be set even in invalid code like "int(2.7 +
3161 ...)". Therefore, we have to check that the next token
3162 is sure to end the cast. */
3165 cp_token *next_token;
3167 next_token = cp_lexer_peek_token (parser->lexer);
3168 if (/* The comma at the end of an
3169 enumerator-definition. */
3170 next_token->type != CPP_COMMA
3171 /* The curly brace at the end of an enum-specifier. */
3172 && next_token->type != CPP_CLOSE_BRACE
3173 /* The end of a statement. */
3174 && next_token->type != CPP_SEMICOLON
3175 /* The end of the cast-expression. */
3176 && next_token->type != CPP_CLOSE_PAREN
3177 /* The end of an array bound. */
3178 && next_token->type != CPP_CLOSE_SQUARE
3179 /* The closing ">" in a template-argument-list. */
3180 && (next_token->type != CPP_GREATER
3181 || parser->greater_than_is_operator_p)
3182 /* C++0x only: A ">>" treated like two ">" tokens,
3183 in a template-argument-list. */
3184 && (next_token->type != CPP_RSHIFT
3185 || (cxx_dialect == cxx98)
3186 || parser->greater_than_is_operator_p))
3190 /* If we are within a cast, then the constraint that the
3191 cast is to an integral or enumeration type will be
3192 checked at that point. If we are not within a cast, then
3193 this code is invalid. */
3195 cp_parser_non_integral_constant_expression
3196 (parser, "floating-point literal");
3198 return token->u.value;
3204 /* ??? Should wide strings be allowed when parser->translate_strings_p
3205 is false (i.e. in attributes)? If not, we can kill the third
3206 argument to cp_parser_string_literal. */
3207 return cp_parser_string_literal (parser,
3208 parser->translate_strings_p,
3211 case CPP_OPEN_PAREN:
3214 bool saved_greater_than_is_operator_p;
3216 /* Consume the `('. */
3217 cp_lexer_consume_token (parser->lexer);
3218 /* Within a parenthesized expression, a `>' token is always
3219 the greater-than operator. */
3220 saved_greater_than_is_operator_p
3221 = parser->greater_than_is_operator_p;
3222 parser->greater_than_is_operator_p = true;
3223 /* If we see `( { ' then we are looking at the beginning of
3224 a GNU statement-expression. */
3225 if (cp_parser_allow_gnu_extensions_p (parser)
3226 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3228 /* Statement-expressions are not allowed by the standard. */
3229 pedwarn (token->location, OPT_pedantic,
3230 "ISO C++ forbids braced-groups within expressions");
3232 /* And they're not allowed outside of a function-body; you
3233 cannot, for example, write:
3235 int i = ({ int j = 3; j + 1; });
3237 at class or namespace scope. */
3238 if (!parser->in_function_body
3239 || parser->in_template_argument_list_p)
3241 error ("%Hstatement-expressions are not allowed outside "
3242 "functions nor in template-argument lists",
3244 cp_parser_skip_to_end_of_block_or_statement (parser);
3245 expr = error_mark_node;
3249 /* Start the statement-expression. */
3250 expr = begin_stmt_expr ();
3251 /* Parse the compound-statement. */
3252 cp_parser_compound_statement (parser, expr, false);
3254 expr = finish_stmt_expr (expr, false);
3259 /* Parse the parenthesized expression. */
3260 expr = cp_parser_expression (parser, cast_p, idk);
3261 /* Let the front end know that this expression was
3262 enclosed in parentheses. This matters in case, for
3263 example, the expression is of the form `A::B', since
3264 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3266 finish_parenthesized_expr (expr);
3268 /* The `>' token might be the end of a template-id or
3269 template-parameter-list now. */
3270 parser->greater_than_is_operator_p
3271 = saved_greater_than_is_operator_p;
3272 /* Consume the `)'. */
3273 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3274 cp_parser_skip_to_end_of_statement (parser);
3280 switch (token->keyword)
3282 /* These two are the boolean literals. */
3284 cp_lexer_consume_token (parser->lexer);
3285 return boolean_true_node;
3287 cp_lexer_consume_token (parser->lexer);
3288 return boolean_false_node;
3290 /* The `__null' literal. */
3292 cp_lexer_consume_token (parser->lexer);
3295 /* Recognize the `this' keyword. */
3297 cp_lexer_consume_token (parser->lexer);
3298 if (parser->local_variables_forbidden_p)
3300 error ("%H%<this%> may not be used in this context",
3302 return error_mark_node;
3304 /* Pointers cannot appear in constant-expressions. */
3305 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3306 return error_mark_node;
3307 return finish_this_expr ();
3309 /* The `operator' keyword can be the beginning of an
3314 case RID_FUNCTION_NAME:
3315 case RID_PRETTY_FUNCTION_NAME:
3316 case RID_C99_FUNCTION_NAME:
3320 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3321 __func__ are the names of variables -- but they are
3322 treated specially. Therefore, they are handled here,
3323 rather than relying on the generic id-expression logic
3324 below. Grammatically, these names are id-expressions.
3326 Consume the token. */
3327 token = cp_lexer_consume_token (parser->lexer);
3329 switch (token->keyword)
3331 case RID_FUNCTION_NAME:
3332 name = "%<__FUNCTION__%>";
3334 case RID_PRETTY_FUNCTION_NAME:
3335 name = "%<__PRETTY_FUNCTION__%>";
3337 case RID_C99_FUNCTION_NAME:
3338 name = "%<__func__%>";
3344 if (cp_parser_non_integral_constant_expression (parser, name))
3345 return error_mark_node;
3347 /* Look up the name. */
3348 return finish_fname (token->u.value);
3356 /* The `__builtin_va_arg' construct is used to handle
3357 `va_arg'. Consume the `__builtin_va_arg' token. */
3358 cp_lexer_consume_token (parser->lexer);
3359 /* Look for the opening `('. */
3360 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3361 /* Now, parse the assignment-expression. */
3362 expression = cp_parser_assignment_expression (parser,
3363 /*cast_p=*/false, NULL);
3364 /* Look for the `,'. */
3365 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3366 /* Parse the type-id. */
3367 type = cp_parser_type_id (parser);
3368 /* Look for the closing `)'. */
3369 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3370 /* Using `va_arg' in a constant-expression is not
3372 if (cp_parser_non_integral_constant_expression (parser,
3374 return error_mark_node;
3375 return build_x_va_arg (expression, type);
3379 return cp_parser_builtin_offsetof (parser);
3381 case RID_HAS_NOTHROW_ASSIGN:
3382 case RID_HAS_NOTHROW_CONSTRUCTOR:
3383 case RID_HAS_NOTHROW_COPY:
3384 case RID_HAS_TRIVIAL_ASSIGN:
3385 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3386 case RID_HAS_TRIVIAL_COPY:
3387 case RID_HAS_TRIVIAL_DESTRUCTOR:
3388 case RID_HAS_VIRTUAL_DESTRUCTOR:
3389 case RID_IS_ABSTRACT:
3390 case RID_IS_BASE_OF:
3392 case RID_IS_CONVERTIBLE_TO:
3396 case RID_IS_POLYMORPHIC:
3398 return cp_parser_trait_expr (parser, token->keyword);
3400 /* Objective-C++ expressions. */
3402 case RID_AT_PROTOCOL:
3403 case RID_AT_SELECTOR:
3404 return cp_parser_objc_expression (parser);
3407 cp_parser_error (parser, "expected primary-expression");
3408 return error_mark_node;
3411 /* An id-expression can start with either an identifier, a
3412 `::' as the beginning of a qualified-id, or the "operator"
3416 case CPP_TEMPLATE_ID:
3417 case CPP_NESTED_NAME_SPECIFIER:
3421 const char *error_msg;
3424 cp_token *id_expr_token;
3427 /* Parse the id-expression. */
3429 = cp_parser_id_expression (parser,
3430 /*template_keyword_p=*/false,
3431 /*check_dependency_p=*/true,
3433 /*declarator_p=*/false,
3434 /*optional_p=*/false);
3435 if (id_expression == error_mark_node)
3436 return error_mark_node;
3437 id_expr_token = token;
3438 token = cp_lexer_peek_token (parser->lexer);
3439 done = (token->type != CPP_OPEN_SQUARE
3440 && token->type != CPP_OPEN_PAREN
3441 && token->type != CPP_DOT
3442 && token->type != CPP_DEREF
3443 && token->type != CPP_PLUS_PLUS
3444 && token->type != CPP_MINUS_MINUS);
3445 /* If we have a template-id, then no further lookup is
3446 required. If the template-id was for a template-class, we
3447 will sometimes have a TYPE_DECL at this point. */
3448 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3449 || TREE_CODE (id_expression) == TYPE_DECL)
3450 decl = id_expression;
3451 /* Look up the name. */
3454 tree ambiguous_decls;
3456 decl = cp_parser_lookup_name (parser, id_expression,
3459 /*is_namespace=*/false,
3460 /*check_dependency=*/true,
3462 id_expr_token->location);
3463 /* If the lookup was ambiguous, an error will already have
3465 if (ambiguous_decls)
3466 return error_mark_node;
3468 /* In Objective-C++, an instance variable (ivar) may be preferred
3469 to whatever cp_parser_lookup_name() found. */
3470 decl = objc_lookup_ivar (decl, id_expression);
3472 /* If name lookup gives us a SCOPE_REF, then the
3473 qualifying scope was dependent. */
3474 if (TREE_CODE (decl) == SCOPE_REF)
3476 /* At this point, we do not know if DECL is a valid
3477 integral constant expression. We assume that it is
3478 in fact such an expression, so that code like:
3480 template <int N> struct A {
3484 is accepted. At template-instantiation time, we
3485 will check that B<N>::i is actually a constant. */
3488 /* Check to see if DECL is a local variable in a context
3489 where that is forbidden. */
3490 if (parser->local_variables_forbidden_p
3491 && local_variable_p (decl))
3493 /* It might be that we only found DECL because we are
3494 trying to be generous with pre-ISO scoping rules.
3495 For example, consider:
3499 for (int i = 0; i < 10; ++i) {}
3500 extern void f(int j = i);
3503 Here, name look up will originally find the out
3504 of scope `i'. We need to issue a warning message,
3505 but then use the global `i'. */
3506 decl = check_for_out_of_scope_variable (decl);
3507 if (local_variable_p (decl))
3509 error ("%Hlocal variable %qD may not appear in this context",
3510 &id_expr_token->location, decl);
3511 return error_mark_node;
3516 decl = (finish_id_expression
3517 (id_expression, decl, parser->scope,
3519 parser->integral_constant_expression_p,
3520 parser->allow_non_integral_constant_expression_p,
3521 &parser->non_integral_constant_expression_p,
3522 template_p, done, address_p,
3525 id_expr_token->location));
3527 cp_parser_error (parser, error_msg);
3531 /* Anything else is an error. */
3533 /* ...unless we have an Objective-C++ message or string literal,
3535 if (c_dialect_objc ()
3536 && (token->type == CPP_OPEN_SQUARE
3537 || token->type == CPP_OBJC_STRING))
3538 return cp_parser_objc_expression (parser);
3540 cp_parser_error (parser, "expected primary-expression");
3541 return error_mark_node;
3545 /* Parse an id-expression.
3552 :: [opt] nested-name-specifier template [opt] unqualified-id
3554 :: operator-function-id
3557 Return a representation of the unqualified portion of the
3558 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3559 a `::' or nested-name-specifier.
3561 Often, if the id-expression was a qualified-id, the caller will
3562 want to make a SCOPE_REF to represent the qualified-id. This
3563 function does not do this in order to avoid wastefully creating
3564 SCOPE_REFs when they are not required.
3566 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3569 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3570 uninstantiated templates.
3572 If *TEMPLATE_P is non-NULL, it is set to true iff the
3573 `template' keyword is used to explicitly indicate that the entity
3574 named is a template.
3576 If DECLARATOR_P is true, the id-expression is appearing as part of
3577 a declarator, rather than as part of an expression. */
3580 cp_parser_id_expression (cp_parser *parser,
3581 bool template_keyword_p,
3582 bool check_dependency_p,
3587 bool global_scope_p;
3588 bool nested_name_specifier_p;
3590 /* Assume the `template' keyword was not used. */
3592 *template_p = template_keyword_p;
3594 /* Look for the optional `::' operator. */
3596 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3598 /* Look for the optional nested-name-specifier. */
3599 nested_name_specifier_p
3600 = (cp_parser_nested_name_specifier_opt (parser,
3601 /*typename_keyword_p=*/false,
3606 /* If there is a nested-name-specifier, then we are looking at
3607 the first qualified-id production. */
3608 if (nested_name_specifier_p)
3611 tree saved_object_scope;
3612 tree saved_qualifying_scope;
3613 tree unqualified_id;
3616 /* See if the next token is the `template' keyword. */
3618 template_p = &is_template;
3619 *template_p = cp_parser_optional_template_keyword (parser);
3620 /* Name lookup we do during the processing of the
3621 unqualified-id might obliterate SCOPE. */
3622 saved_scope = parser->scope;
3623 saved_object_scope = parser->object_scope;
3624 saved_qualifying_scope = parser->qualifying_scope;
3625 /* Process the final unqualified-id. */
3626 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3629 /*optional_p=*/false);
3630 /* Restore the SAVED_SCOPE for our caller. */
3631 parser->scope = saved_scope;
3632 parser->object_scope = saved_object_scope;
3633 parser->qualifying_scope = saved_qualifying_scope;
3635 return unqualified_id;
3637 /* Otherwise, if we are in global scope, then we are looking at one
3638 of the other qualified-id productions. */
3639 else if (global_scope_p)
3644 /* Peek at the next token. */
3645 token = cp_lexer_peek_token (parser->lexer);
3647 /* If it's an identifier, and the next token is not a "<", then
3648 we can avoid the template-id case. This is an optimization
3649 for this common case. */
3650 if (token->type == CPP_NAME
3651 && !cp_parser_nth_token_starts_template_argument_list_p
3653 return cp_parser_identifier (parser);
3655 cp_parser_parse_tentatively (parser);
3656 /* Try a template-id. */
3657 id = cp_parser_template_id (parser,
3658 /*template_keyword_p=*/false,
3659 /*check_dependency_p=*/true,
3661 /* If that worked, we're done. */
3662 if (cp_parser_parse_definitely (parser))
3665 /* Peek at the next token. (Changes in the token buffer may
3666 have invalidated the pointer obtained above.) */
3667 token = cp_lexer_peek_token (parser->lexer);
3669 switch (token->type)
3672 return cp_parser_identifier (parser);
3675 if (token->keyword == RID_OPERATOR)
3676 return cp_parser_operator_function_id (parser);
3680 cp_parser_error (parser, "expected id-expression");
3681 return error_mark_node;
3685 return cp_parser_unqualified_id (parser, template_keyword_p,
3686 /*check_dependency_p=*/true,
3691 /* Parse an unqualified-id.
3695 operator-function-id
3696 conversion-function-id
3700 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3701 keyword, in a construct like `A::template ...'.
3703 Returns a representation of unqualified-id. For the `identifier'
3704 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3705 production a BIT_NOT_EXPR is returned; the operand of the
3706 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3707 other productions, see the documentation accompanying the
3708 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3709 names are looked up in uninstantiated templates. If DECLARATOR_P
3710 is true, the unqualified-id is appearing as part of a declarator,
3711 rather than as part of an expression. */
3714 cp_parser_unqualified_id (cp_parser* parser,
3715 bool template_keyword_p,
3716 bool check_dependency_p,
3722 /* Peek at the next token. */
3723 token = cp_lexer_peek_token (parser->lexer);
3725 switch (token->type)
3731 /* We don't know yet whether or not this will be a
3733 cp_parser_parse_tentatively (parser);
3734 /* Try a template-id. */
3735 id = cp_parser_template_id (parser, template_keyword_p,
3738 /* If it worked, we're done. */
3739 if (cp_parser_parse_definitely (parser))
3741 /* Otherwise, it's an ordinary identifier. */
3742 return cp_parser_identifier (parser);
3745 case CPP_TEMPLATE_ID:
3746 return cp_parser_template_id (parser, template_keyword_p,
3753 tree qualifying_scope;
3758 /* Consume the `~' token. */
3759 cp_lexer_consume_token (parser->lexer);
3760 /* Parse the class-name. The standard, as written, seems to
3763 template <typename T> struct S { ~S (); };
3764 template <typename T> S<T>::~S() {}
3766 is invalid, since `~' must be followed by a class-name, but
3767 `S<T>' is dependent, and so not known to be a class.
3768 That's not right; we need to look in uninstantiated
3769 templates. A further complication arises from:
3771 template <typename T> void f(T t) {
3775 Here, it is not possible to look up `T' in the scope of `T'
3776 itself. We must look in both the current scope, and the
3777 scope of the containing complete expression.
3779 Yet another issue is:
3788 The standard does not seem to say that the `S' in `~S'
3789 should refer to the type `S' and not the data member
3792 /* DR 244 says that we look up the name after the "~" in the
3793 same scope as we looked up the qualifying name. That idea
3794 isn't fully worked out; it's more complicated than that. */
3795 scope = parser->scope;
3796 object_scope = parser->object_scope;
3797 qualifying_scope = parser->qualifying_scope;
3799 /* Check for invalid scopes. */
3800 if (scope == error_mark_node)
3802 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3803 cp_lexer_consume_token (parser->lexer);
3804 return error_mark_node;
3806 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3808 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3809 error ("%Hscope %qT before %<~%> is not a class-name",
3810 &token->location, scope);
3811 cp_parser_simulate_error (parser);
3812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3813 cp_lexer_consume_token (parser->lexer);
3814 return error_mark_node;
3816 gcc_assert (!scope || TYPE_P (scope));
3818 /* If the name is of the form "X::~X" it's OK. */
3819 token = cp_lexer_peek_token (parser->lexer);
3821 && token->type == CPP_NAME
3822 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3824 && constructor_name_p (token->u.value, scope))
3826 cp_lexer_consume_token (parser->lexer);
3827 return build_nt (BIT_NOT_EXPR, scope);
3830 /* If there was an explicit qualification (S::~T), first look
3831 in the scope given by the qualification (i.e., S). */
3833 type_decl = NULL_TREE;
3836 cp_parser_parse_tentatively (parser);
3837 type_decl = cp_parser_class_name (parser,
3838 /*typename_keyword_p=*/false,
3839 /*template_keyword_p=*/false,
3841 /*check_dependency=*/false,
3842 /*class_head_p=*/false,
3844 if (cp_parser_parse_definitely (parser))
3847 /* In "N::S::~S", look in "N" as well. */
3848 if (!done && scope && qualifying_scope)
3850 cp_parser_parse_tentatively (parser);
3851 parser->scope = qualifying_scope;
3852 parser->object_scope = NULL_TREE;
3853 parser->qualifying_scope = NULL_TREE;
3855 = cp_parser_class_name (parser,
3856 /*typename_keyword_p=*/false,
3857 /*template_keyword_p=*/false,
3859 /*check_dependency=*/false,
3860 /*class_head_p=*/false,
3862 if (cp_parser_parse_definitely (parser))
3865 /* In "p->S::~T", look in the scope given by "*p" as well. */
3866 else if (!done && object_scope)
3868 cp_parser_parse_tentatively (parser);
3869 parser->scope = object_scope;
3870 parser->object_scope = NULL_TREE;
3871 parser->qualifying_scope = NULL_TREE;
3873 = cp_parser_class_name (parser,
3874 /*typename_keyword_p=*/false,
3875 /*template_keyword_p=*/false,
3877 /*check_dependency=*/false,
3878 /*class_head_p=*/false,
3880 if (cp_parser_parse_definitely (parser))
3883 /* Look in the surrounding context. */
3886 parser->scope = NULL_TREE;
3887 parser->object_scope = NULL_TREE;
3888 parser->qualifying_scope = NULL_TREE;
3889 if (processing_template_decl)
3890 cp_parser_parse_tentatively (parser);
3892 = cp_parser_class_name (parser,
3893 /*typename_keyword_p=*/false,
3894 /*template_keyword_p=*/false,
3896 /*check_dependency=*/false,
3897 /*class_head_p=*/false,
3899 if (processing_template_decl
3900 && ! cp_parser_parse_definitely (parser))
3902 /* We couldn't find a type with this name, so just accept
3903 it and check for a match at instantiation time. */
3904 type_decl = cp_parser_identifier (parser);
3905 if (type_decl != error_mark_node)
3906 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3910 /* If an error occurred, assume that the name of the
3911 destructor is the same as the name of the qualifying
3912 class. That allows us to keep parsing after running
3913 into ill-formed destructor names. */
3914 if (type_decl == error_mark_node && scope)
3915 return build_nt (BIT_NOT_EXPR, scope);
3916 else if (type_decl == error_mark_node)
3917 return error_mark_node;
3919 /* Check that destructor name and scope match. */
3920 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3922 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3923 error ("%Hdeclaration of %<~%T%> as member of %qT",
3924 &token->location, type_decl, scope);
3925 cp_parser_simulate_error (parser);
3926 return error_mark_node;
3931 A typedef-name that names a class shall not be used as the
3932 identifier in the declarator for a destructor declaration. */
3934 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3935 && !DECL_SELF_REFERENCE_P (type_decl)
3936 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3937 error ("%Htypedef-name %qD used as destructor declarator",
3938 &token->location, type_decl);
3940 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3944 if (token->keyword == RID_OPERATOR)
3948 /* This could be a template-id, so we try that first. */
3949 cp_parser_parse_tentatively (parser);
3950 /* Try a template-id. */
3951 id = cp_parser_template_id (parser, template_keyword_p,
3952 /*check_dependency_p=*/true,
3954 /* If that worked, we're done. */
3955 if (cp_parser_parse_definitely (parser))
3957 /* We still don't know whether we're looking at an
3958 operator-function-id or a conversion-function-id. */
3959 cp_parser_parse_tentatively (parser);
3960 /* Try an operator-function-id. */
3961 id = cp_parser_operator_function_id (parser);
3962 /* If that didn't work, try a conversion-function-id. */
3963 if (!cp_parser_parse_definitely (parser))
3964 id = cp_parser_conversion_function_id (parser);
3973 cp_parser_error (parser, "expected unqualified-id");
3974 return error_mark_node;
3978 /* Parse an (optional) nested-name-specifier.
3980 nested-name-specifier: [C++98]
3981 class-or-namespace-name :: nested-name-specifier [opt]
3982 class-or-namespace-name :: template nested-name-specifier [opt]
3984 nested-name-specifier: [C++0x]
3987 nested-name-specifier identifier ::
3988 nested-name-specifier template [opt] simple-template-id ::
3990 PARSER->SCOPE should be set appropriately before this function is
3991 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3992 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3995 Sets PARSER->SCOPE to the class (TYPE) or namespace
3996 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3997 it unchanged if there is no nested-name-specifier. Returns the new
3998 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4000 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4001 part of a declaration and/or decl-specifier. */
4004 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4005 bool typename_keyword_p,
4006 bool check_dependency_p,
4008 bool is_declaration)
4010 bool success = false;
4011 cp_token_position start = 0;
4014 /* Remember where the nested-name-specifier starts. */
4015 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4017 start = cp_lexer_token_position (parser->lexer, false);
4018 push_deferring_access_checks (dk_deferred);
4025 tree saved_qualifying_scope;
4026 bool template_keyword_p;
4028 /* Spot cases that cannot be the beginning of a
4029 nested-name-specifier. */
4030 token = cp_lexer_peek_token (parser->lexer);
4032 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4033 the already parsed nested-name-specifier. */
4034 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4036 /* Grab the nested-name-specifier and continue the loop. */
4037 cp_parser_pre_parsed_nested_name_specifier (parser);
4038 /* If we originally encountered this nested-name-specifier
4039 with IS_DECLARATION set to false, we will not have
4040 resolved TYPENAME_TYPEs, so we must do so here. */
4042 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4044 new_scope = resolve_typename_type (parser->scope,
4045 /*only_current_p=*/false);
4046 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4047 parser->scope = new_scope;
4053 /* Spot cases that cannot be the beginning of a
4054 nested-name-specifier. On the second and subsequent times
4055 through the loop, we look for the `template' keyword. */
4056 if (success && token->keyword == RID_TEMPLATE)
4058 /* A template-id can start a nested-name-specifier. */
4059 else if (token->type == CPP_TEMPLATE_ID)
4063 /* If the next token is not an identifier, then it is
4064 definitely not a type-name or namespace-name. */
4065 if (token->type != CPP_NAME)
4067 /* If the following token is neither a `<' (to begin a
4068 template-id), nor a `::', then we are not looking at a
4069 nested-name-specifier. */
4070 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4071 if (token->type != CPP_SCOPE
4072 && !cp_parser_nth_token_starts_template_argument_list_p
4077 /* The nested-name-specifier is optional, so we parse
4079 cp_parser_parse_tentatively (parser);
4081 /* Look for the optional `template' keyword, if this isn't the
4082 first time through the loop. */
4084 template_keyword_p = cp_parser_optional_template_keyword (parser);
4086 template_keyword_p = false;
4088 /* Save the old scope since the name lookup we are about to do
4089 might destroy it. */
4090 old_scope = parser->scope;
4091 saved_qualifying_scope = parser->qualifying_scope;
4092 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4093 look up names in "X<T>::I" in order to determine that "Y" is
4094 a template. So, if we have a typename at this point, we make
4095 an effort to look through it. */
4097 && !typename_keyword_p
4099 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4100 parser->scope = resolve_typename_type (parser->scope,
4101 /*only_current_p=*/false);
4102 /* Parse the qualifying entity. */
4104 = cp_parser_qualifying_entity (parser,
4110 /* Look for the `::' token. */
4111 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4113 /* If we found what we wanted, we keep going; otherwise, we're
4115 if (!cp_parser_parse_definitely (parser))
4117 bool error_p = false;
4119 /* Restore the OLD_SCOPE since it was valid before the
4120 failed attempt at finding the last
4121 class-or-namespace-name. */
4122 parser->scope = old_scope;
4123 parser->qualifying_scope = saved_qualifying_scope;
4124 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4126 /* If the next token is an identifier, and the one after
4127 that is a `::', then any valid interpretation would have
4128 found a class-or-namespace-name. */
4129 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4130 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4132 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4135 token = cp_lexer_consume_token (parser->lexer);
4138 if (!token->ambiguous_p)
4141 tree ambiguous_decls;
4143 decl = cp_parser_lookup_name (parser, token->u.value,
4145 /*is_template=*/false,
4146 /*is_namespace=*/false,
4147 /*check_dependency=*/true,
4150 if (TREE_CODE (decl) == TEMPLATE_DECL)
4151 error ("%H%qD used without template parameters",
4152 &token->location, decl);
4153 else if (ambiguous_decls)
4155 error ("%Hreference to %qD is ambiguous",
4156 &token->location, token->u.value);
4157 print_candidates (ambiguous_decls);
4158 decl = error_mark_node;
4162 const char* msg = "is not a class or namespace";
4163 if (cxx_dialect != cxx98)
4164 msg = "is not a class, namespace, or enumeration";
4165 cp_parser_name_lookup_error
4166 (parser, token->u.value, decl, msg,
4170 parser->scope = error_mark_node;
4172 /* Treat this as a successful nested-name-specifier
4177 If the name found is not a class-name (clause
4178 _class_) or namespace-name (_namespace.def_), the
4179 program is ill-formed. */
4182 cp_lexer_consume_token (parser->lexer);
4186 /* We've found one valid nested-name-specifier. */
4188 /* Name lookup always gives us a DECL. */
4189 if (TREE_CODE (new_scope) == TYPE_DECL)
4190 new_scope = TREE_TYPE (new_scope);
4191 /* Uses of "template" must be followed by actual templates. */
4192 if (template_keyword_p
4193 && !(CLASS_TYPE_P (new_scope)
4194 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4195 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4196 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4197 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4198 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4199 == TEMPLATE_ID_EXPR)))
4200 permerror (input_location, TYPE_P (new_scope)
4201 ? "%qT is not a template"
4202 : "%qD is not a template",
4204 /* If it is a class scope, try to complete it; we are about to
4205 be looking up names inside the class. */
4206 if (TYPE_P (new_scope)
4207 /* Since checking types for dependency can be expensive,
4208 avoid doing it if the type is already complete. */
4209 && !COMPLETE_TYPE_P (new_scope)
4210 /* Do not try to complete dependent types. */
4211 && !dependent_type_p (new_scope))
4213 new_scope = complete_type (new_scope);
4214 /* If it is a typedef to current class, use the current
4215 class instead, as the typedef won't have any names inside
4217 if (!COMPLETE_TYPE_P (new_scope)
4218 && currently_open_class (new_scope))
4219 new_scope = TYPE_MAIN_VARIANT (new_scope);
4221 /* Make sure we look in the right scope the next time through
4223 parser->scope = new_scope;
4226 /* If parsing tentatively, replace the sequence of tokens that makes
4227 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4228 token. That way, should we re-parse the token stream, we will
4229 not have to repeat the effort required to do the parse, nor will
4230 we issue duplicate error messages. */
4231 if (success && start)
4235 token = cp_lexer_token_at (parser->lexer, start);
4236 /* Reset the contents of the START token. */
4237 token->type = CPP_NESTED_NAME_SPECIFIER;
4238 /* Retrieve any deferred checks. Do not pop this access checks yet
4239 so the memory will not be reclaimed during token replacing below. */
4240 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4241 token->u.tree_check_value->value = parser->scope;
4242 token->u.tree_check_value->checks = get_deferred_access_checks ();
4243 token->u.tree_check_value->qualifying_scope =
4244 parser->qualifying_scope;
4245 token->keyword = RID_MAX;
4247 /* Purge all subsequent tokens. */
4248 cp_lexer_purge_tokens_after (parser->lexer, start);
4252 pop_to_parent_deferring_access_checks ();
4254 return success ? parser->scope : NULL_TREE;
4257 /* Parse a nested-name-specifier. See
4258 cp_parser_nested_name_specifier_opt for details. This function
4259 behaves identically, except that it will an issue an error if no
4260 nested-name-specifier is present. */
4263 cp_parser_nested_name_specifier (cp_parser *parser,
4264 bool typename_keyword_p,
4265 bool check_dependency_p,
4267 bool is_declaration)
4271 /* Look for the nested-name-specifier. */
4272 scope = cp_parser_nested_name_specifier_opt (parser,
4277 /* If it was not present, issue an error message. */
4280 cp_parser_error (parser, "expected nested-name-specifier");
4281 parser->scope = NULL_TREE;
4287 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4288 this is either a class-name or a namespace-name (which corresponds
4289 to the class-or-namespace-name production in the grammar). For
4290 C++0x, it can also be a type-name that refers to an enumeration
4293 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4294 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4295 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4296 TYPE_P is TRUE iff the next name should be taken as a class-name,
4297 even the same name is declared to be another entity in the same
4300 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4301 specified by the class-or-namespace-name. If neither is found the
4302 ERROR_MARK_NODE is returned. */
4305 cp_parser_qualifying_entity (cp_parser *parser,
4306 bool typename_keyword_p,
4307 bool template_keyword_p,
4308 bool check_dependency_p,
4310 bool is_declaration)
4313 tree saved_qualifying_scope;
4314 tree saved_object_scope;
4317 bool successful_parse_p;
4319 /* Before we try to parse the class-name, we must save away the
4320 current PARSER->SCOPE since cp_parser_class_name will destroy
4322 saved_scope = parser->scope;
4323 saved_qualifying_scope = parser->qualifying_scope;
4324 saved_object_scope = parser->object_scope;
4325 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4326 there is no need to look for a namespace-name. */
4327 only_class_p = template_keyword_p
4328 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4330 cp_parser_parse_tentatively (parser);
4331 scope = cp_parser_class_name (parser,
4334 type_p ? class_type : none_type,
4336 /*class_head_p=*/false,
4338 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4339 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4341 && cxx_dialect != cxx98
4342 && !successful_parse_p)
4344 /* Restore the saved scope. */
4345 parser->scope = saved_scope;
4346 parser->qualifying_scope = saved_qualifying_scope;
4347 parser->object_scope = saved_object_scope;
4349 /* Parse tentatively. */
4350 cp_parser_parse_tentatively (parser);
4352 /* Parse a typedef-name or enum-name. */
4353 scope = cp_parser_nonclass_name (parser);
4354 successful_parse_p = cp_parser_parse_definitely (parser);
4356 /* If that didn't work, try for a namespace-name. */
4357 if (!only_class_p && !successful_parse_p)
4359 /* Restore the saved scope. */
4360 parser->scope = saved_scope;
4361 parser->qualifying_scope = saved_qualifying_scope;
4362 parser->object_scope = saved_object_scope;
4363 /* If we are not looking at an identifier followed by the scope
4364 resolution operator, then this is not part of a
4365 nested-name-specifier. (Note that this function is only used
4366 to parse the components of a nested-name-specifier.) */
4367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4368 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4369 return error_mark_node;
4370 scope = cp_parser_namespace_name (parser);
4376 /* Parse a postfix-expression.
4380 postfix-expression [ expression ]
4381 postfix-expression ( expression-list [opt] )
4382 simple-type-specifier ( expression-list [opt] )
4383 typename :: [opt] nested-name-specifier identifier
4384 ( expression-list [opt] )
4385 typename :: [opt] nested-name-specifier template [opt] template-id
4386 ( expression-list [opt] )
4387 postfix-expression . template [opt] id-expression
4388 postfix-expression -> template [opt] id-expression
4389 postfix-expression . pseudo-destructor-name
4390 postfix-expression -> pseudo-destructor-name
4391 postfix-expression ++
4392 postfix-expression --
4393 dynamic_cast < type-id > ( expression )
4394 static_cast < type-id > ( expression )
4395 reinterpret_cast < type-id > ( expression )
4396 const_cast < type-id > ( expression )
4397 typeid ( expression )
4403 ( type-id ) { initializer-list , [opt] }
4405 This extension is a GNU version of the C99 compound-literal
4406 construct. (The C99 grammar uses `type-name' instead of `type-id',
4407 but they are essentially the same concept.)
4409 If ADDRESS_P is true, the postfix expression is the operand of the
4410 `&' operator. CAST_P is true if this expression is the target of a
4413 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4414 class member access expressions [expr.ref].
4416 Returns a representation of the expression. */
4419 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4420 bool member_access_only_p,
4421 cp_id_kind * pidk_return)
4425 cp_id_kind idk = CP_ID_KIND_NONE;
4426 tree postfix_expression = NULL_TREE;
4427 bool is_member_access = false;
4429 /* Peek at the next token. */
4430 token = cp_lexer_peek_token (parser->lexer);
4431 /* Some of the productions are determined by keywords. */
4432 keyword = token->keyword;
4442 const char *saved_message;
4444 /* All of these can be handled in the same way from the point
4445 of view of parsing. Begin by consuming the token
4446 identifying the cast. */
4447 cp_lexer_consume_token (parser->lexer);
4449 /* New types cannot be defined in the cast. */
4450 saved_message = parser->type_definition_forbidden_message;
4451 parser->type_definition_forbidden_message
4452 = "types may not be defined in casts";
4454 /* Look for the opening `<'. */
4455 cp_parser_require (parser, CPP_LESS, "%<<%>");
4456 /* Parse the type to which we are casting. */
4457 type = cp_parser_type_id (parser);
4458 /* Look for the closing `>'. */
4459 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4460 /* Restore the old message. */
4461 parser->type_definition_forbidden_message = saved_message;
4463 /* And the expression which is being cast. */
4464 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4465 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4466 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4468 /* Only type conversions to integral or enumeration types
4469 can be used in constant-expressions. */
4470 if (!cast_valid_in_integral_constant_expression_p (type)
4471 && (cp_parser_non_integral_constant_expression
4473 "a cast to a type other than an integral or "
4474 "enumeration type")))
4475 return error_mark_node;
4481 = build_dynamic_cast (type, expression, tf_warning_or_error);
4485 = build_static_cast (type, expression, tf_warning_or_error);
4489 = build_reinterpret_cast (type, expression,
4490 tf_warning_or_error);
4494 = build_const_cast (type, expression, tf_warning_or_error);
4505 const char *saved_message;
4506 bool saved_in_type_id_in_expr_p;
4508 /* Consume the `typeid' token. */
4509 cp_lexer_consume_token (parser->lexer);
4510 /* Look for the `(' token. */
4511 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4512 /* Types cannot be defined in a `typeid' expression. */
4513 saved_message = parser->type_definition_forbidden_message;
4514 parser->type_definition_forbidden_message
4515 = "types may not be defined in a %<typeid%> expression";
4516 /* We can't be sure yet whether we're looking at a type-id or an
4518 cp_parser_parse_tentatively (parser);
4519 /* Try a type-id first. */
4520 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4521 parser->in_type_id_in_expr_p = true;
4522 type = cp_parser_type_id (parser);
4523 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4524 /* Look for the `)' token. Otherwise, we can't be sure that
4525 we're not looking at an expression: consider `typeid (int
4526 (3))', for example. */
4527 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4528 /* If all went well, simply lookup the type-id. */
4529 if (cp_parser_parse_definitely (parser))
4530 postfix_expression = get_typeid (type);
4531 /* Otherwise, fall back to the expression variant. */
4536 /* Look for an expression. */
4537 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4538 /* Compute its typeid. */
4539 postfix_expression = build_typeid (expression);
4540 /* Look for the `)' token. */
4541 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4543 /* Restore the saved message. */
4544 parser->type_definition_forbidden_message = saved_message;
4545 /* `typeid' may not appear in an integral constant expression. */
4546 if (cp_parser_non_integral_constant_expression(parser,
4547 "%<typeid%> operator"))
4548 return error_mark_node;
4555 /* The syntax permitted here is the same permitted for an
4556 elaborated-type-specifier. */
4557 type = cp_parser_elaborated_type_specifier (parser,
4558 /*is_friend=*/false,
4559 /*is_declaration=*/false);
4560 postfix_expression = cp_parser_functional_cast (parser, type);
4568 /* If the next thing is a simple-type-specifier, we may be
4569 looking at a functional cast. We could also be looking at
4570 an id-expression. So, we try the functional cast, and if
4571 that doesn't work we fall back to the primary-expression. */
4572 cp_parser_parse_tentatively (parser);
4573 /* Look for the simple-type-specifier. */
4574 type = cp_parser_simple_type_specifier (parser,
4575 /*decl_specs=*/NULL,
4576 CP_PARSER_FLAGS_NONE);
4577 /* Parse the cast itself. */
4578 if (!cp_parser_error_occurred (parser))
4580 = cp_parser_functional_cast (parser, type);
4581 /* If that worked, we're done. */
4582 if (cp_parser_parse_definitely (parser))
4585 /* If the functional-cast didn't work out, try a
4586 compound-literal. */
4587 if (cp_parser_allow_gnu_extensions_p (parser)
4588 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4590 VEC(constructor_elt,gc) *initializer_list = NULL;
4591 bool saved_in_type_id_in_expr_p;
4593 cp_parser_parse_tentatively (parser);
4594 /* Consume the `('. */
4595 cp_lexer_consume_token (parser->lexer);
4596 /* Parse the type. */
4597 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4598 parser->in_type_id_in_expr_p = true;
4599 type = cp_parser_type_id (parser);
4600 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4601 /* Look for the `)'. */
4602 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4603 /* Look for the `{'. */
4604 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4605 /* If things aren't going well, there's no need to
4607 if (!cp_parser_error_occurred (parser))
4609 bool non_constant_p;
4610 /* Parse the initializer-list. */
4612 = cp_parser_initializer_list (parser, &non_constant_p);
4613 /* Allow a trailing `,'. */
4614 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4615 cp_lexer_consume_token (parser->lexer);
4616 /* Look for the final `}'. */
4617 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4619 /* If that worked, we're definitely looking at a
4620 compound-literal expression. */
4621 if (cp_parser_parse_definitely (parser))
4623 /* Warn the user that a compound literal is not
4624 allowed in standard C++. */
4625 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4626 /* For simplicity, we disallow compound literals in
4627 constant-expressions. We could
4628 allow compound literals of integer type, whose
4629 initializer was a constant, in constant
4630 expressions. Permitting that usage, as a further
4631 extension, would not change the meaning of any
4632 currently accepted programs. (Of course, as
4633 compound literals are not part of ISO C++, the
4634 standard has nothing to say.) */
4635 if (cp_parser_non_integral_constant_expression
4636 (parser, "non-constant compound literals"))
4638 postfix_expression = error_mark_node;
4641 /* Form the representation of the compound-literal. */
4643 = (finish_compound_literal
4644 (type, build_constructor (init_list_type_node,
4645 initializer_list)));
4650 /* It must be a primary-expression. */
4652 = cp_parser_primary_expression (parser, address_p, cast_p,
4653 /*template_arg_p=*/false,
4659 /* Keep looping until the postfix-expression is complete. */
4662 if (idk == CP_ID_KIND_UNQUALIFIED
4663 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4664 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4665 /* It is not a Koenig lookup function call. */
4667 = unqualified_name_lookup_error (postfix_expression);
4669 /* Peek at the next token. */
4670 token = cp_lexer_peek_token (parser->lexer);
4672 switch (token->type)
4674 case CPP_OPEN_SQUARE:
4676 = cp_parser_postfix_open_square_expression (parser,
4679 idk = CP_ID_KIND_NONE;
4680 is_member_access = false;
4683 case CPP_OPEN_PAREN:
4684 /* postfix-expression ( expression-list [opt] ) */
4687 bool is_builtin_constant_p;
4688 bool saved_integral_constant_expression_p = false;
4689 bool saved_non_integral_constant_expression_p = false;
4692 is_member_access = false;
4694 is_builtin_constant_p
4695 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4696 if (is_builtin_constant_p)
4698 /* The whole point of __builtin_constant_p is to allow
4699 non-constant expressions to appear as arguments. */
4700 saved_integral_constant_expression_p
4701 = parser->integral_constant_expression_p;
4702 saved_non_integral_constant_expression_p
4703 = parser->non_integral_constant_expression_p;
4704 parser->integral_constant_expression_p = false;
4706 args = (cp_parser_parenthesized_expression_list
4707 (parser, /*is_attribute_list=*/false,
4708 /*cast_p=*/false, /*allow_expansion_p=*/true,
4709 /*non_constant_p=*/NULL));
4710 if (is_builtin_constant_p)
4712 parser->integral_constant_expression_p
4713 = saved_integral_constant_expression_p;
4714 parser->non_integral_constant_expression_p
4715 = saved_non_integral_constant_expression_p;
4718 if (args == error_mark_node)
4720 postfix_expression = error_mark_node;
4724 /* Function calls are not permitted in
4725 constant-expressions. */
4726 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4727 && cp_parser_non_integral_constant_expression (parser,
4730 postfix_expression = error_mark_node;
4735 if (idk == CP_ID_KIND_UNQUALIFIED
4736 || idk == CP_ID_KIND_TEMPLATE_ID)
4738 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4744 = perform_koenig_lookup (postfix_expression, args);
4748 = unqualified_fn_lookup_error (postfix_expression);
4750 /* We do not perform argument-dependent lookup if
4751 normal lookup finds a non-function, in accordance
4752 with the expected resolution of DR 218. */
4753 else if (args && is_overloaded_fn (postfix_expression))
4755 tree fn = get_first_fn (postfix_expression);
4757 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4758 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4760 /* Only do argument dependent lookup if regular
4761 lookup does not find a set of member functions.
4762 [basic.lookup.koenig]/2a */
4763 if (!DECL_FUNCTION_MEMBER_P (fn))
4767 = perform_koenig_lookup (postfix_expression, args);
4772 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4774 tree instance = TREE_OPERAND (postfix_expression, 0);
4775 tree fn = TREE_OPERAND (postfix_expression, 1);
4777 if (processing_template_decl
4778 && (type_dependent_expression_p (instance)
4779 || (!BASELINK_P (fn)
4780 && TREE_CODE (fn) != FIELD_DECL)
4781 || type_dependent_expression_p (fn)
4782 || any_type_dependent_arguments_p (args)))
4785 = build_nt_call_list (postfix_expression, args);
4789 if (BASELINK_P (fn))
4792 = (build_new_method_call
4793 (instance, fn, args, NULL_TREE,
4794 (idk == CP_ID_KIND_QUALIFIED
4795 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4797 tf_warning_or_error));
4801 = finish_call_expr (postfix_expression, args,
4802 /*disallow_virtual=*/false,
4804 tf_warning_or_error);
4806 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4807 || TREE_CODE (postfix_expression) == MEMBER_REF
4808 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4809 postfix_expression = (build_offset_ref_call_from_tree
4810 (postfix_expression, args));
4811 else if (idk == CP_ID_KIND_QUALIFIED)
4812 /* A call to a static class member, or a namespace-scope
4815 = finish_call_expr (postfix_expression, args,
4816 /*disallow_virtual=*/true,
4818 tf_warning_or_error);
4820 /* All other function calls. */
4822 = finish_call_expr (postfix_expression, args,
4823 /*disallow_virtual=*/false,
4825 tf_warning_or_error);
4827 if (warn_disallowed_functions)
4828 warn_if_disallowed_function_p (postfix_expression);
4830 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4831 idk = CP_ID_KIND_NONE;
4837 /* postfix-expression . template [opt] id-expression
4838 postfix-expression . pseudo-destructor-name
4839 postfix-expression -> template [opt] id-expression
4840 postfix-expression -> pseudo-destructor-name */
4842 /* Consume the `.' or `->' operator. */
4843 cp_lexer_consume_token (parser->lexer);
4846 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4851 is_member_access = true;
4855 /* postfix-expression ++ */
4856 /* Consume the `++' token. */
4857 cp_lexer_consume_token (parser->lexer);
4858 /* Generate a representation for the complete expression. */
4860 = finish_increment_expr (postfix_expression,
4861 POSTINCREMENT_EXPR);
4862 /* Increments may not appear in constant-expressions. */
4863 if (cp_parser_non_integral_constant_expression (parser,
4865 postfix_expression = error_mark_node;
4866 idk = CP_ID_KIND_NONE;
4867 is_member_access = false;
4870 case CPP_MINUS_MINUS:
4871 /* postfix-expression -- */
4872 /* Consume the `--' token. */
4873 cp_lexer_consume_token (parser->lexer);
4874 /* Generate a representation for the complete expression. */
4876 = finish_increment_expr (postfix_expression,
4877 POSTDECREMENT_EXPR);
4878 /* Decrements may not appear in constant-expressions. */
4879 if (cp_parser_non_integral_constant_expression (parser,
4881 postfix_expression = error_mark_node;
4882 idk = CP_ID_KIND_NONE;
4883 is_member_access = false;
4887 if (pidk_return != NULL)
4888 * pidk_return = idk;
4889 if (member_access_only_p)
4890 return is_member_access? postfix_expression : error_mark_node;
4892 return postfix_expression;
4896 /* We should never get here. */
4898 return error_mark_node;
4901 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4902 by cp_parser_builtin_offsetof. We're looking for
4904 postfix-expression [ expression ]
4906 FOR_OFFSETOF is set if we're being called in that context, which
4907 changes how we deal with integer constant expressions. */
4910 cp_parser_postfix_open_square_expression (cp_parser *parser,
4911 tree postfix_expression,
4916 /* Consume the `[' token. */
4917 cp_lexer_consume_token (parser->lexer);
4919 /* Parse the index expression. */
4920 /* ??? For offsetof, there is a question of what to allow here. If
4921 offsetof is not being used in an integral constant expression context,
4922 then we *could* get the right answer by computing the value at runtime.
4923 If we are in an integral constant expression context, then we might
4924 could accept any constant expression; hard to say without analysis.
4925 Rather than open the barn door too wide right away, allow only integer
4926 constant expressions here. */
4928 index = cp_parser_constant_expression (parser, false, NULL);
4930 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4932 /* Look for the closing `]'. */
4933 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4935 /* Build the ARRAY_REF. */
4936 postfix_expression = grok_array_decl (postfix_expression, index);
4938 /* When not doing offsetof, array references are not permitted in
4939 constant-expressions. */
4941 && (cp_parser_non_integral_constant_expression
4942 (parser, "an array reference")))
4943 postfix_expression = error_mark_node;
4945 return postfix_expression;
4948 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4949 by cp_parser_builtin_offsetof. We're looking for
4951 postfix-expression . template [opt] id-expression
4952 postfix-expression . pseudo-destructor-name
4953 postfix-expression -> template [opt] id-expression
4954 postfix-expression -> pseudo-destructor-name
4956 FOR_OFFSETOF is set if we're being called in that context. That sorta
4957 limits what of the above we'll actually accept, but nevermind.
4958 TOKEN_TYPE is the "." or "->" token, which will already have been
4959 removed from the stream. */
4962 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4963 enum cpp_ttype token_type,
4964 tree postfix_expression,
4965 bool for_offsetof, cp_id_kind *idk,
4966 location_t location)
4970 bool pseudo_destructor_p;
4971 tree scope = NULL_TREE;
4973 /* If this is a `->' operator, dereference the pointer. */
4974 if (token_type == CPP_DEREF)
4975 postfix_expression = build_x_arrow (postfix_expression);
4976 /* Check to see whether or not the expression is type-dependent. */
4977 dependent_p = type_dependent_expression_p (postfix_expression);
4978 /* The identifier following the `->' or `.' is not qualified. */
4979 parser->scope = NULL_TREE;
4980 parser->qualifying_scope = NULL_TREE;
4981 parser->object_scope = NULL_TREE;
4982 *idk = CP_ID_KIND_NONE;
4984 /* Enter the scope corresponding to the type of the object
4985 given by the POSTFIX_EXPRESSION. */
4986 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4988 scope = TREE_TYPE (postfix_expression);
4989 /* According to the standard, no expression should ever have
4990 reference type. Unfortunately, we do not currently match
4991 the standard in this respect in that our internal representation
4992 of an expression may have reference type even when the standard
4993 says it does not. Therefore, we have to manually obtain the
4994 underlying type here. */
4995 scope = non_reference (scope);
4996 /* The type of the POSTFIX_EXPRESSION must be complete. */
4997 if (scope == unknown_type_node)
4999 error ("%H%qE does not have class type", &location, postfix_expression);
5003 scope = complete_type_or_else (scope, NULL_TREE);
5004 /* Let the name lookup machinery know that we are processing a
5005 class member access expression. */
5006 parser->context->object_type = scope;
5007 /* If something went wrong, we want to be able to discern that case,
5008 as opposed to the case where there was no SCOPE due to the type
5009 of expression being dependent. */
5011 scope = error_mark_node;
5012 /* If the SCOPE was erroneous, make the various semantic analysis
5013 functions exit quickly -- and without issuing additional error
5015 if (scope == error_mark_node)
5016 postfix_expression = error_mark_node;
5019 /* Assume this expression is not a pseudo-destructor access. */
5020 pseudo_destructor_p = false;
5022 /* If the SCOPE is a scalar type, then, if this is a valid program,
5023 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5024 is type dependent, it can be pseudo-destructor-name or something else.
5025 Try to parse it as pseudo-destructor-name first. */
5026 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5031 cp_parser_parse_tentatively (parser);
5032 /* Parse the pseudo-destructor-name. */
5034 cp_parser_pseudo_destructor_name (parser, &s, &type);
5036 && (cp_parser_error_occurred (parser)
5037 || TREE_CODE (type) != TYPE_DECL
5038 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5039 cp_parser_abort_tentative_parse (parser);
5040 else if (cp_parser_parse_definitely (parser))
5042 pseudo_destructor_p = true;
5044 = finish_pseudo_destructor_expr (postfix_expression,
5045 s, TREE_TYPE (type));
5049 if (!pseudo_destructor_p)
5051 /* If the SCOPE is not a scalar type, we are looking at an
5052 ordinary class member access expression, rather than a
5053 pseudo-destructor-name. */
5055 cp_token *token = cp_lexer_peek_token (parser->lexer);
5056 /* Parse the id-expression. */
5057 name = (cp_parser_id_expression
5059 cp_parser_optional_template_keyword (parser),
5060 /*check_dependency_p=*/true,
5062 /*declarator_p=*/false,
5063 /*optional_p=*/false));
5064 /* In general, build a SCOPE_REF if the member name is qualified.
5065 However, if the name was not dependent and has already been
5066 resolved; there is no need to build the SCOPE_REF. For example;
5068 struct X { void f(); };
5069 template <typename T> void f(T* t) { t->X::f(); }
5071 Even though "t" is dependent, "X::f" is not and has been resolved
5072 to a BASELINK; there is no need to include scope information. */
5074 /* But we do need to remember that there was an explicit scope for
5075 virtual function calls. */
5077 *idk = CP_ID_KIND_QUALIFIED;
5079 /* If the name is a template-id that names a type, we will get a
5080 TYPE_DECL here. That is invalid code. */
5081 if (TREE_CODE (name) == TYPE_DECL)
5083 error ("%Hinvalid use of %qD", &token->location, name);
5084 postfix_expression = error_mark_node;
5088 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5090 name = build_qualified_name (/*type=*/NULL_TREE,
5094 parser->scope = NULL_TREE;
5095 parser->qualifying_scope = NULL_TREE;
5096 parser->object_scope = NULL_TREE;
5098 if (scope && name && BASELINK_P (name))
5099 adjust_result_of_qualified_name_lookup
5100 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5102 = finish_class_member_access_expr (postfix_expression, name,
5104 tf_warning_or_error);
5108 /* We no longer need to look up names in the scope of the object on
5109 the left-hand side of the `.' or `->' operator. */
5110 parser->context->object_type = NULL_TREE;
5112 /* Outside of offsetof, these operators may not appear in
5113 constant-expressions. */
5115 && (cp_parser_non_integral_constant_expression
5116 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5117 postfix_expression = error_mark_node;
5119 return postfix_expression;
5122 /* Parse a parenthesized expression-list.
5125 assignment-expression
5126 expression-list, assignment-expression
5131 identifier, expression-list
5133 CAST_P is true if this expression is the target of a cast.
5135 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5138 Returns a TREE_LIST. The TREE_VALUE of each node is a
5139 representation of an assignment-expression. Note that a TREE_LIST
5140 is returned even if there is only a single expression in the list.
5141 error_mark_node is returned if the ( and or ) are
5142 missing. NULL_TREE is returned on no expressions. The parentheses
5143 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5144 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5145 indicates whether or not all of the expressions in the list were
5149 cp_parser_parenthesized_expression_list (cp_parser* parser,
5150 bool is_attribute_list,
5152 bool allow_expansion_p,
5153 bool *non_constant_p)
5155 tree expression_list = NULL_TREE;
5156 bool fold_expr_p = is_attribute_list;
5157 tree identifier = NULL_TREE;
5158 bool saved_greater_than_is_operator_p;
5160 /* Assume all the expressions will be constant. */
5162 *non_constant_p = false;
5164 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5165 return error_mark_node;
5167 /* Within a parenthesized expression, a `>' token is always
5168 the greater-than operator. */
5169 saved_greater_than_is_operator_p
5170 = parser->greater_than_is_operator_p;
5171 parser->greater_than_is_operator_p = true;
5173 /* Consume expressions until there are no more. */
5174 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5179 /* At the beginning of attribute lists, check to see if the
5180 next token is an identifier. */
5181 if (is_attribute_list
5182 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5186 /* Consume the identifier. */
5187 token = cp_lexer_consume_token (parser->lexer);
5188 /* Save the identifier. */
5189 identifier = token->u.value;
5193 bool expr_non_constant_p;
5195 /* Parse the next assignment-expression. */
5196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5198 /* A braced-init-list. */
5199 maybe_warn_cpp0x ("extended initializer lists");
5200 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5201 if (non_constant_p && expr_non_constant_p)
5202 *non_constant_p = true;
5204 else if (non_constant_p)
5206 expr = (cp_parser_constant_expression
5207 (parser, /*allow_non_constant_p=*/true,
5208 &expr_non_constant_p));
5209 if (expr_non_constant_p)
5210 *non_constant_p = true;
5213 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5216 expr = fold_non_dependent_expr (expr);
5218 /* If we have an ellipsis, then this is an expression
5220 if (allow_expansion_p
5221 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5223 /* Consume the `...'. */
5224 cp_lexer_consume_token (parser->lexer);
5226 /* Build the argument pack. */
5227 expr = make_pack_expansion (expr);
5230 /* Add it to the list. We add error_mark_node
5231 expressions to the list, so that we can still tell if
5232 the correct form for a parenthesized expression-list
5233 is found. That gives better errors. */
5234 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5236 if (expr == error_mark_node)
5240 /* After the first item, attribute lists look the same as
5241 expression lists. */
5242 is_attribute_list = false;
5245 /* If the next token isn't a `,', then we are done. */
5246 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5249 /* Otherwise, consume the `,' and keep going. */
5250 cp_lexer_consume_token (parser->lexer);
5253 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5258 /* We try and resync to an unnested comma, as that will give the
5259 user better diagnostics. */
5260 ending = cp_parser_skip_to_closing_parenthesis (parser,
5261 /*recovering=*/true,
5263 /*consume_paren=*/true);
5268 parser->greater_than_is_operator_p
5269 = saved_greater_than_is_operator_p;
5270 return error_mark_node;
5274 parser->greater_than_is_operator_p
5275 = saved_greater_than_is_operator_p;
5277 /* We built up the list in reverse order so we must reverse it now. */
5278 expression_list = nreverse (expression_list);
5280 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5282 return expression_list;
5285 /* Parse a pseudo-destructor-name.
5287 pseudo-destructor-name:
5288 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5289 :: [opt] nested-name-specifier template template-id :: ~ type-name
5290 :: [opt] nested-name-specifier [opt] ~ type-name
5292 If either of the first two productions is used, sets *SCOPE to the
5293 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5294 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5295 or ERROR_MARK_NODE if the parse fails. */
5298 cp_parser_pseudo_destructor_name (cp_parser* parser,
5302 bool nested_name_specifier_p;
5304 /* Assume that things will not work out. */
5305 *type = error_mark_node;
5307 /* Look for the optional `::' operator. */
5308 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5309 /* Look for the optional nested-name-specifier. */
5310 nested_name_specifier_p
5311 = (cp_parser_nested_name_specifier_opt (parser,
5312 /*typename_keyword_p=*/false,
5313 /*check_dependency_p=*/true,
5315 /*is_declaration=*/false)
5317 /* Now, if we saw a nested-name-specifier, we might be doing the
5318 second production. */
5319 if (nested_name_specifier_p
5320 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5322 /* Consume the `template' keyword. */
5323 cp_lexer_consume_token (parser->lexer);
5324 /* Parse the template-id. */
5325 cp_parser_template_id (parser,
5326 /*template_keyword_p=*/true,
5327 /*check_dependency_p=*/false,
5328 /*is_declaration=*/true);
5329 /* Look for the `::' token. */
5330 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5332 /* If the next token is not a `~', then there might be some
5333 additional qualification. */
5334 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5336 /* At this point, we're looking for "type-name :: ~". The type-name
5337 must not be a class-name, since this is a pseudo-destructor. So,
5338 it must be either an enum-name, or a typedef-name -- both of which
5339 are just identifiers. So, we peek ahead to check that the "::"
5340 and "~" tokens are present; if they are not, then we can avoid
5341 calling type_name. */
5342 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5343 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5344 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5346 cp_parser_error (parser, "non-scalar type");
5350 /* Look for the type-name. */
5351 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5352 if (*scope == error_mark_node)
5355 /* Look for the `::' token. */
5356 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5361 /* Look for the `~'. */
5362 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5363 /* Look for the type-name again. We are not responsible for
5364 checking that it matches the first type-name. */
5365 *type = cp_parser_nonclass_name (parser);
5368 /* Parse a unary-expression.
5374 unary-operator cast-expression
5375 sizeof unary-expression
5383 __extension__ cast-expression
5384 __alignof__ unary-expression
5385 __alignof__ ( type-id )
5386 __real__ cast-expression
5387 __imag__ cast-expression
5390 ADDRESS_P is true iff the unary-expression is appearing as the
5391 operand of the `&' operator. CAST_P is true if this expression is
5392 the target of a cast.
5394 Returns a representation of the expression. */
5397 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5401 enum tree_code unary_operator;
5403 /* Peek at the next token. */
5404 token = cp_lexer_peek_token (parser->lexer);
5405 /* Some keywords give away the kind of expression. */
5406 if (token->type == CPP_KEYWORD)
5408 enum rid keyword = token->keyword;
5418 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5419 /* Consume the token. */
5420 cp_lexer_consume_token (parser->lexer);
5421 /* Parse the operand. */
5422 operand = cp_parser_sizeof_operand (parser, keyword);
5424 if (TYPE_P (operand))
5425 return cxx_sizeof_or_alignof_type (operand, op, true);
5427 return cxx_sizeof_or_alignof_expr (operand, op, true);
5431 return cp_parser_new_expression (parser);
5434 return cp_parser_delete_expression (parser);
5438 /* The saved value of the PEDANTIC flag. */
5442 /* Save away the PEDANTIC flag. */
5443 cp_parser_extension_opt (parser, &saved_pedantic);
5444 /* Parse the cast-expression. */
5445 expr = cp_parser_simple_cast_expression (parser);
5446 /* Restore the PEDANTIC flag. */
5447 pedantic = saved_pedantic;
5457 /* Consume the `__real__' or `__imag__' token. */
5458 cp_lexer_consume_token (parser->lexer);
5459 /* Parse the cast-expression. */
5460 expression = cp_parser_simple_cast_expression (parser);
5461 /* Create the complete representation. */
5462 return build_x_unary_op ((keyword == RID_REALPART
5463 ? REALPART_EXPR : IMAGPART_EXPR),
5465 tf_warning_or_error);
5474 /* Look for the `:: new' and `:: delete', which also signal the
5475 beginning of a new-expression, or delete-expression,
5476 respectively. If the next token is `::', then it might be one of
5478 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5482 /* See if the token after the `::' is one of the keywords in
5483 which we're interested. */
5484 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5485 /* If it's `new', we have a new-expression. */
5486 if (keyword == RID_NEW)
5487 return cp_parser_new_expression (parser);
5488 /* Similarly, for `delete'. */
5489 else if (keyword == RID_DELETE)
5490 return cp_parser_delete_expression (parser);
5493 /* Look for a unary operator. */
5494 unary_operator = cp_parser_unary_operator (token);
5495 /* The `++' and `--' operators can be handled similarly, even though
5496 they are not technically unary-operators in the grammar. */
5497 if (unary_operator == ERROR_MARK)
5499 if (token->type == CPP_PLUS_PLUS)
5500 unary_operator = PREINCREMENT_EXPR;
5501 else if (token->type == CPP_MINUS_MINUS)
5502 unary_operator = PREDECREMENT_EXPR;
5503 /* Handle the GNU address-of-label extension. */
5504 else if (cp_parser_allow_gnu_extensions_p (parser)
5505 && token->type == CPP_AND_AND)
5509 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5511 /* Consume the '&&' token. */
5512 cp_lexer_consume_token (parser->lexer);
5513 /* Look for the identifier. */
5514 identifier = cp_parser_identifier (parser);
5515 /* Create an expression representing the address. */
5516 expression = finish_label_address_expr (identifier, loc);
5517 if (cp_parser_non_integral_constant_expression (parser,
5518 "the address of a label"))
5519 expression = error_mark_node;
5523 if (unary_operator != ERROR_MARK)
5525 tree cast_expression;
5526 tree expression = error_mark_node;
5527 const char *non_constant_p = NULL;
5529 /* Consume the operator token. */
5530 token = cp_lexer_consume_token (parser->lexer);
5531 /* Parse the cast-expression. */
5533 = cp_parser_cast_expression (parser,
5534 unary_operator == ADDR_EXPR,
5535 /*cast_p=*/false, pidk);
5536 /* Now, build an appropriate representation. */
5537 switch (unary_operator)
5540 non_constant_p = "%<*%>";
5541 expression = build_x_indirect_ref (cast_expression, "unary *",
5542 tf_warning_or_error);
5546 non_constant_p = "%<&%>";
5549 expression = build_x_unary_op (unary_operator, cast_expression,
5550 tf_warning_or_error);
5553 case PREINCREMENT_EXPR:
5554 case PREDECREMENT_EXPR:
5555 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5556 ? "%<++%>" : "%<--%>");
5558 case UNARY_PLUS_EXPR:
5560 case TRUTH_NOT_EXPR:
5561 expression = finish_unary_op_expr (unary_operator, cast_expression);
5569 && cp_parser_non_integral_constant_expression (parser,
5571 expression = error_mark_node;
5576 return cp_parser_postfix_expression (parser, address_p, cast_p,
5577 /*member_access_only_p=*/false,
5581 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5582 unary-operator, the corresponding tree code is returned. */
5584 static enum tree_code
5585 cp_parser_unary_operator (cp_token* token)
5587 switch (token->type)
5590 return INDIRECT_REF;
5596 return UNARY_PLUS_EXPR;
5602 return TRUTH_NOT_EXPR;
5605 return BIT_NOT_EXPR;
5612 /* Parse a new-expression.
5615 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5616 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5618 Returns a representation of the expression. */
5621 cp_parser_new_expression (cp_parser* parser)
5623 bool global_scope_p;
5629 /* Look for the optional `::' operator. */
5631 = (cp_parser_global_scope_opt (parser,
5632 /*current_scope_valid_p=*/false)
5634 /* Look for the `new' operator. */
5635 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5636 /* There's no easy way to tell a new-placement from the
5637 `( type-id )' construct. */
5638 cp_parser_parse_tentatively (parser);
5639 /* Look for a new-placement. */
5640 placement = cp_parser_new_placement (parser);
5641 /* If that didn't work out, there's no new-placement. */
5642 if (!cp_parser_parse_definitely (parser))
5643 placement = NULL_TREE;
5645 /* If the next token is a `(', then we have a parenthesized
5647 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5650 /* Consume the `('. */
5651 cp_lexer_consume_token (parser->lexer);
5652 /* Parse the type-id. */
5653 type = cp_parser_type_id (parser);
5654 /* Look for the closing `)'. */
5655 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5656 token = cp_lexer_peek_token (parser->lexer);
5657 /* There should not be a direct-new-declarator in this production,
5658 but GCC used to allowed this, so we check and emit a sensible error
5659 message for this case. */
5660 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5662 error ("%Harray bound forbidden after parenthesized type-id",
5664 inform (token->location,
5665 "try removing the parentheses around the type-id");
5666 cp_parser_direct_new_declarator (parser);
5670 /* Otherwise, there must be a new-type-id. */
5672 type = cp_parser_new_type_id (parser, &nelts);
5674 /* If the next token is a `(' or '{', then we have a new-initializer. */
5675 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5676 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5677 initializer = cp_parser_new_initializer (parser);
5679 initializer = NULL_TREE;
5681 /* A new-expression may not appear in an integral constant
5683 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5684 return error_mark_node;
5686 /* Create a representation of the new-expression. */
5687 return build_new (placement, type, nelts, initializer, global_scope_p,
5688 tf_warning_or_error);
5691 /* Parse a new-placement.
5696 Returns the same representation as for an expression-list. */
5699 cp_parser_new_placement (cp_parser* parser)
5701 tree expression_list;
5703 /* Parse the expression-list. */
5704 expression_list = (cp_parser_parenthesized_expression_list
5705 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5706 /*non_constant_p=*/NULL));
5708 return expression_list;
5711 /* Parse a new-type-id.
5714 type-specifier-seq new-declarator [opt]
5716 Returns the TYPE allocated. If the new-type-id indicates an array
5717 type, *NELTS is set to the number of elements in the last array
5718 bound; the TYPE will not include the last array bound. */
5721 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5723 cp_decl_specifier_seq type_specifier_seq;
5724 cp_declarator *new_declarator;
5725 cp_declarator *declarator;
5726 cp_declarator *outer_declarator;
5727 const char *saved_message;
5730 /* The type-specifier sequence must not contain type definitions.
5731 (It cannot contain declarations of new types either, but if they
5732 are not definitions we will catch that because they are not
5734 saved_message = parser->type_definition_forbidden_message;
5735 parser->type_definition_forbidden_message
5736 = "types may not be defined in a new-type-id";
5737 /* Parse the type-specifier-seq. */
5738 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5739 &type_specifier_seq);
5740 /* Restore the old message. */
5741 parser->type_definition_forbidden_message = saved_message;
5742 /* Parse the new-declarator. */
5743 new_declarator = cp_parser_new_declarator_opt (parser);
5745 /* Determine the number of elements in the last array dimension, if
5748 /* Skip down to the last array dimension. */
5749 declarator = new_declarator;
5750 outer_declarator = NULL;
5751 while (declarator && (declarator->kind == cdk_pointer
5752 || declarator->kind == cdk_ptrmem))
5754 outer_declarator = declarator;
5755 declarator = declarator->declarator;
5758 && declarator->kind == cdk_array
5759 && declarator->declarator
5760 && declarator->declarator->kind == cdk_array)
5762 outer_declarator = declarator;
5763 declarator = declarator->declarator;
5766 if (declarator && declarator->kind == cdk_array)
5768 *nelts = declarator->u.array.bounds;
5769 if (*nelts == error_mark_node)
5770 *nelts = integer_one_node;
5772 if (outer_declarator)
5773 outer_declarator->declarator = declarator->declarator;
5775 new_declarator = NULL;
5778 type = groktypename (&type_specifier_seq, new_declarator);
5782 /* Parse an (optional) new-declarator.
5785 ptr-operator new-declarator [opt]
5786 direct-new-declarator
5788 Returns the declarator. */
5790 static cp_declarator *
5791 cp_parser_new_declarator_opt (cp_parser* parser)
5793 enum tree_code code;
5795 cp_cv_quals cv_quals;
5797 /* We don't know if there's a ptr-operator next, or not. */
5798 cp_parser_parse_tentatively (parser);
5799 /* Look for a ptr-operator. */
5800 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5801 /* If that worked, look for more new-declarators. */
5802 if (cp_parser_parse_definitely (parser))
5804 cp_declarator *declarator;
5806 /* Parse another optional declarator. */
5807 declarator = cp_parser_new_declarator_opt (parser);
5809 return cp_parser_make_indirect_declarator
5810 (code, type, cv_quals, declarator);
5813 /* If the next token is a `[', there is a direct-new-declarator. */
5814 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5815 return cp_parser_direct_new_declarator (parser);
5820 /* Parse a direct-new-declarator.
5822 direct-new-declarator:
5824 direct-new-declarator [constant-expression]
5828 static cp_declarator *
5829 cp_parser_direct_new_declarator (cp_parser* parser)
5831 cp_declarator *declarator = NULL;
5837 /* Look for the opening `['. */
5838 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5839 /* The first expression is not required to be constant. */
5842 cp_token *token = cp_lexer_peek_token (parser->lexer);
5843 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5844 /* The standard requires that the expression have integral
5845 type. DR 74 adds enumeration types. We believe that the
5846 real intent is that these expressions be handled like the
5847 expression in a `switch' condition, which also allows
5848 classes with a single conversion to integral or
5849 enumeration type. */
5850 if (!processing_template_decl)
5853 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5858 error ("%Hexpression in new-declarator must have integral "
5859 "or enumeration type", &token->location);
5860 expression = error_mark_node;
5864 /* But all the other expressions must be. */
5867 = cp_parser_constant_expression (parser,
5868 /*allow_non_constant=*/false,
5870 /* Look for the closing `]'. */
5871 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5873 /* Add this bound to the declarator. */
5874 declarator = make_array_declarator (declarator, expression);
5876 /* If the next token is not a `[', then there are no more
5878 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5885 /* Parse a new-initializer.
5888 ( expression-list [opt] )
5891 Returns a representation of the expression-list. If there is no
5892 expression-list, VOID_ZERO_NODE is returned. */
5895 cp_parser_new_initializer (cp_parser* parser)
5897 tree expression_list;
5899 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5901 bool expr_non_constant_p;
5902 maybe_warn_cpp0x ("extended initializer lists");
5903 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5904 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5905 expression_list = build_tree_list (NULL_TREE, expression_list);
5908 expression_list = (cp_parser_parenthesized_expression_list
5909 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5910 /*non_constant_p=*/NULL));
5911 if (!expression_list)
5912 expression_list = void_zero_node;
5914 return expression_list;
5917 /* Parse a delete-expression.
5920 :: [opt] delete cast-expression
5921 :: [opt] delete [ ] cast-expression
5923 Returns a representation of the expression. */
5926 cp_parser_delete_expression (cp_parser* parser)
5928 bool global_scope_p;
5932 /* Look for the optional `::' operator. */
5934 = (cp_parser_global_scope_opt (parser,
5935 /*current_scope_valid_p=*/false)
5937 /* Look for the `delete' keyword. */
5938 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5939 /* See if the array syntax is in use. */
5940 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5942 /* Consume the `[' token. */
5943 cp_lexer_consume_token (parser->lexer);
5944 /* Look for the `]' token. */
5945 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5946 /* Remember that this is the `[]' construct. */
5952 /* Parse the cast-expression. */
5953 expression = cp_parser_simple_cast_expression (parser);
5955 /* A delete-expression may not appear in an integral constant
5957 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5958 return error_mark_node;
5960 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5963 /* Returns true if TOKEN may start a cast-expression and false
5967 cp_parser_token_starts_cast_expression (cp_token *token)
5969 switch (token->type)
5975 case CPP_CLOSE_SQUARE:
5976 case CPP_CLOSE_PAREN:
5977 case CPP_CLOSE_BRACE:
5981 case CPP_DEREF_STAR:
5989 case CPP_GREATER_EQ:
6009 /* '[' may start a primary-expression in obj-c++. */
6010 case CPP_OPEN_SQUARE:
6011 return c_dialect_objc ();
6018 /* Parse a cast-expression.
6022 ( type-id ) cast-expression
6024 ADDRESS_P is true iff the unary-expression is appearing as the
6025 operand of the `&' operator. CAST_P is true if this expression is
6026 the target of a cast.
6028 Returns a representation of the expression. */
6031 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6034 /* If it's a `(', then we might be looking at a cast. */
6035 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6037 tree type = NULL_TREE;
6038 tree expr = NULL_TREE;
6039 bool compound_literal_p;
6040 const char *saved_message;
6042 /* There's no way to know yet whether or not this is a cast.
6043 For example, `(int (3))' is a unary-expression, while `(int)
6044 3' is a cast. So, we resort to parsing tentatively. */
6045 cp_parser_parse_tentatively (parser);
6046 /* Types may not be defined in a cast. */
6047 saved_message = parser->type_definition_forbidden_message;
6048 parser->type_definition_forbidden_message
6049 = "types may not be defined in casts";
6050 /* Consume the `('. */
6051 cp_lexer_consume_token (parser->lexer);
6052 /* A very tricky bit is that `(struct S) { 3 }' is a
6053 compound-literal (which we permit in C++ as an extension).
6054 But, that construct is not a cast-expression -- it is a
6055 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6056 is legal; if the compound-literal were a cast-expression,
6057 you'd need an extra set of parentheses.) But, if we parse
6058 the type-id, and it happens to be a class-specifier, then we
6059 will commit to the parse at that point, because we cannot
6060 undo the action that is done when creating a new class. So,
6061 then we cannot back up and do a postfix-expression.
6063 Therefore, we scan ahead to the closing `)', and check to see
6064 if the token after the `)' is a `{'. If so, we are not
6065 looking at a cast-expression.
6067 Save tokens so that we can put them back. */
6068 cp_lexer_save_tokens (parser->lexer);
6069 /* Skip tokens until the next token is a closing parenthesis.
6070 If we find the closing `)', and the next token is a `{', then
6071 we are looking at a compound-literal. */
6073 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6074 /*consume_paren=*/true)
6075 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6076 /* Roll back the tokens we skipped. */
6077 cp_lexer_rollback_tokens (parser->lexer);
6078 /* If we were looking at a compound-literal, simulate an error
6079 so that the call to cp_parser_parse_definitely below will
6081 if (compound_literal_p)
6082 cp_parser_simulate_error (parser);
6085 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6086 parser->in_type_id_in_expr_p = true;
6087 /* Look for the type-id. */
6088 type = cp_parser_type_id (parser);
6089 /* Look for the closing `)'. */
6090 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6091 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6094 /* Restore the saved message. */
6095 parser->type_definition_forbidden_message = saved_message;
6097 /* At this point this can only be either a cast or a
6098 parenthesized ctor such as `(T ())' that looks like a cast to
6099 function returning T. */
6100 if (!cp_parser_error_occurred (parser)
6101 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6104 cp_parser_parse_definitely (parser);
6105 expr = cp_parser_cast_expression (parser,
6106 /*address_p=*/false,
6107 /*cast_p=*/true, pidk);
6109 /* Warn about old-style casts, if so requested. */
6110 if (warn_old_style_cast
6111 && !in_system_header
6112 && !VOID_TYPE_P (type)
6113 && current_lang_name != lang_name_c)
6114 warning (OPT_Wold_style_cast, "use of old-style cast");
6116 /* Only type conversions to integral or enumeration types
6117 can be used in constant-expressions. */
6118 if (!cast_valid_in_integral_constant_expression_p (type)
6119 && (cp_parser_non_integral_constant_expression
6121 "a cast to a type other than an integral or "
6122 "enumeration type")))
6123 return error_mark_node;
6125 /* Perform the cast. */
6126 expr = build_c_cast (type, expr);
6130 cp_parser_abort_tentative_parse (parser);
6133 /* If we get here, then it's not a cast, so it must be a
6134 unary-expression. */
6135 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6138 /* Parse a binary expression of the general form:
6142 pm-expression .* cast-expression
6143 pm-expression ->* cast-expression
6145 multiplicative-expression:
6147 multiplicative-expression * pm-expression
6148 multiplicative-expression / pm-expression
6149 multiplicative-expression % pm-expression
6151 additive-expression:
6152 multiplicative-expression
6153 additive-expression + multiplicative-expression
6154 additive-expression - multiplicative-expression
6158 shift-expression << additive-expression
6159 shift-expression >> additive-expression
6161 relational-expression:
6163 relational-expression < shift-expression
6164 relational-expression > shift-expression
6165 relational-expression <= shift-expression
6166 relational-expression >= shift-expression
6170 relational-expression:
6171 relational-expression <? shift-expression
6172 relational-expression >? shift-expression
6174 equality-expression:
6175 relational-expression
6176 equality-expression == relational-expression
6177 equality-expression != relational-expression
6181 and-expression & equality-expression
6183 exclusive-or-expression:
6185 exclusive-or-expression ^ and-expression
6187 inclusive-or-expression:
6188 exclusive-or-expression
6189 inclusive-or-expression | exclusive-or-expression
6191 logical-and-expression:
6192 inclusive-or-expression
6193 logical-and-expression && inclusive-or-expression
6195 logical-or-expression:
6196 logical-and-expression
6197 logical-or-expression || logical-and-expression
6199 All these are implemented with a single function like:
6202 simple-cast-expression
6203 binary-expression <token> binary-expression
6205 CAST_P is true if this expression is the target of a cast.
6207 The binops_by_token map is used to get the tree codes for each <token> type.
6208 binary-expressions are associated according to a precedence table. */
6210 #define TOKEN_PRECEDENCE(token) \
6211 (((token->type == CPP_GREATER \
6212 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6213 && !parser->greater_than_is_operator_p) \
6214 ? PREC_NOT_OPERATOR \
6215 : binops_by_token[token->type].prec)
6218 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6219 enum cp_parser_prec prec,
6222 cp_parser_expression_stack stack;
6223 cp_parser_expression_stack_entry *sp = &stack[0];
6226 enum tree_code tree_type, lhs_type, rhs_type;
6227 enum cp_parser_prec new_prec, lookahead_prec;
6230 /* Parse the first expression. */
6231 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6232 lhs_type = ERROR_MARK;
6236 /* Get an operator token. */
6237 token = cp_lexer_peek_token (parser->lexer);
6239 if (warn_cxx0x_compat
6240 && token->type == CPP_RSHIFT
6241 && !parser->greater_than_is_operator_p)
6243 warning (OPT_Wc__0x_compat,
6244 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6246 warning (OPT_Wc__0x_compat,
6247 "suggest parentheses around %<>>%> expression");
6250 new_prec = TOKEN_PRECEDENCE (token);
6252 /* Popping an entry off the stack means we completed a subexpression:
6253 - either we found a token which is not an operator (`>' where it is not
6254 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6255 will happen repeatedly;
6256 - or, we found an operator which has lower priority. This is the case
6257 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6259 if (new_prec <= prec)
6268 tree_type = binops_by_token[token->type].tree_type;
6270 /* We used the operator token. */
6271 cp_lexer_consume_token (parser->lexer);
6273 /* Extract another operand. It may be the RHS of this expression
6274 or the LHS of a new, higher priority expression. */
6275 rhs = cp_parser_simple_cast_expression (parser);
6276 rhs_type = ERROR_MARK;
6278 /* Get another operator token. Look up its precedence to avoid
6279 building a useless (immediately popped) stack entry for common
6280 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6281 token = cp_lexer_peek_token (parser->lexer);
6282 lookahead_prec = TOKEN_PRECEDENCE (token);
6283 if (lookahead_prec > new_prec)
6285 /* ... and prepare to parse the RHS of the new, higher priority
6286 expression. Since precedence levels on the stack are
6287 monotonically increasing, we do not have to care about
6290 sp->tree_type = tree_type;
6292 sp->lhs_type = lhs_type;
6295 lhs_type = rhs_type;
6297 new_prec = lookahead_prec;
6301 /* If the stack is not empty, we have parsed into LHS the right side
6302 (`4' in the example above) of an expression we had suspended.
6303 We can use the information on the stack to recover the LHS (`3')
6304 from the stack together with the tree code (`MULT_EXPR'), and
6305 the precedence of the higher level subexpression
6306 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6307 which will be used to actually build the additive expression. */
6310 tree_type = sp->tree_type;
6312 rhs_type = lhs_type;
6314 lhs_type = sp->lhs_type;
6317 overloaded_p = false;
6318 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6319 ERROR_MARK for everything that is not a binary expression.
6320 This makes warn_about_parentheses miss some warnings that
6321 involve unary operators. For unary expressions we should
6322 pass the correct tree_code unless the unary expression was
6323 surrounded by parentheses.
6325 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6326 &overloaded_p, tf_warning_or_error);
6327 lhs_type = tree_type;
6329 /* If the binary operator required the use of an overloaded operator,
6330 then this expression cannot be an integral constant-expression.
6331 An overloaded operator can be used even if both operands are
6332 otherwise permissible in an integral constant-expression if at
6333 least one of the operands is of enumeration type. */
6336 && (cp_parser_non_integral_constant_expression
6337 (parser, "calls to overloaded operators")))
6338 return error_mark_node;
6345 /* Parse the `? expression : assignment-expression' part of a
6346 conditional-expression. The LOGICAL_OR_EXPR is the
6347 logical-or-expression that started the conditional-expression.
6348 Returns a representation of the entire conditional-expression.
6350 This routine is used by cp_parser_assignment_expression.
6352 ? expression : assignment-expression
6356 ? : assignment-expression */
6359 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6362 tree assignment_expr;
6364 /* Consume the `?' token. */
6365 cp_lexer_consume_token (parser->lexer);
6366 if (cp_parser_allow_gnu_extensions_p (parser)
6367 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6368 /* Implicit true clause. */
6371 /* Parse the expression. */
6372 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6374 /* The next token should be a `:'. */
6375 cp_parser_require (parser, CPP_COLON, "%<:%>");
6376 /* Parse the assignment-expression. */
6377 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6379 /* Build the conditional-expression. */
6380 return build_x_conditional_expr (logical_or_expr,
6383 tf_warning_or_error);
6386 /* Parse an assignment-expression.
6388 assignment-expression:
6389 conditional-expression
6390 logical-or-expression assignment-operator assignment_expression
6393 CAST_P is true if this expression is the target of a cast.
6395 Returns a representation for the expression. */
6398 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6403 /* If the next token is the `throw' keyword, then we're looking at
6404 a throw-expression. */
6405 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6406 expr = cp_parser_throw_expression (parser);
6407 /* Otherwise, it must be that we are looking at a
6408 logical-or-expression. */
6411 /* Parse the binary expressions (logical-or-expression). */
6412 expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR, pidk);
6413 /* If the next token is a `?' then we're actually looking at a
6414 conditional-expression. */
6415 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6416 return cp_parser_question_colon_clause (parser, expr);
6419 enum tree_code assignment_operator;
6421 /* If it's an assignment-operator, we're using the second
6424 = cp_parser_assignment_operator_opt (parser);
6425 if (assignment_operator != ERROR_MARK)
6427 bool non_constant_p;
6429 /* Parse the right-hand side of the assignment. */
6430 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6432 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6433 maybe_warn_cpp0x ("extended initializer lists");
6435 /* An assignment may not appear in a
6436 constant-expression. */
6437 if (cp_parser_non_integral_constant_expression (parser,
6439 return error_mark_node;
6440 /* Build the assignment expression. */
6441 expr = build_x_modify_expr (expr,
6442 assignment_operator,
6444 tf_warning_or_error);
6452 /* Parse an (optional) assignment-operator.
6454 assignment-operator: one of
6455 = *= /= %= += -= >>= <<= &= ^= |=
6459 assignment-operator: one of
6462 If the next token is an assignment operator, the corresponding tree
6463 code is returned, and the token is consumed. For example, for
6464 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6465 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6466 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6467 operator, ERROR_MARK is returned. */
6469 static enum tree_code
6470 cp_parser_assignment_operator_opt (cp_parser* parser)
6475 /* Peek at the next token. */
6476 token = cp_lexer_peek_token (parser->lexer);
6478 switch (token->type)
6489 op = TRUNC_DIV_EXPR;
6493 op = TRUNC_MOD_EXPR;
6525 /* Nothing else is an assignment operator. */
6529 /* If it was an assignment operator, consume it. */
6530 if (op != ERROR_MARK)
6531 cp_lexer_consume_token (parser->lexer);
6536 /* Parse an expression.
6539 assignment-expression
6540 expression , assignment-expression
6542 CAST_P is true if this expression is the target of a cast.
6544 Returns a representation of the expression. */
6547 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6549 tree expression = NULL_TREE;
6553 tree assignment_expression;
6555 /* Parse the next assignment-expression. */
6556 assignment_expression
6557 = cp_parser_assignment_expression (parser, cast_p, pidk);
6558 /* If this is the first assignment-expression, we can just
6561 expression = assignment_expression;
6563 expression = build_x_compound_expr (expression,
6564 assignment_expression,
6565 tf_warning_or_error);
6566 /* If the next token is not a comma, then we are done with the
6568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6570 /* Consume the `,'. */
6571 cp_lexer_consume_token (parser->lexer);
6572 /* A comma operator cannot appear in a constant-expression. */
6573 if (cp_parser_non_integral_constant_expression (parser,
6574 "a comma operator"))
6575 expression = error_mark_node;
6581 /* Parse a constant-expression.
6583 constant-expression:
6584 conditional-expression
6586 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6587 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6588 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6589 is false, NON_CONSTANT_P should be NULL. */
6592 cp_parser_constant_expression (cp_parser* parser,
6593 bool allow_non_constant_p,
6594 bool *non_constant_p)
6596 bool saved_integral_constant_expression_p;
6597 bool saved_allow_non_integral_constant_expression_p;
6598 bool saved_non_integral_constant_expression_p;
6601 /* It might seem that we could simply parse the
6602 conditional-expression, and then check to see if it were
6603 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6604 one that the compiler can figure out is constant, possibly after
6605 doing some simplifications or optimizations. The standard has a
6606 precise definition of constant-expression, and we must honor
6607 that, even though it is somewhat more restrictive.
6613 is not a legal declaration, because `(2, 3)' is not a
6614 constant-expression. The `,' operator is forbidden in a
6615 constant-expression. However, GCC's constant-folding machinery
6616 will fold this operation to an INTEGER_CST for `3'. */
6618 /* Save the old settings. */
6619 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6620 saved_allow_non_integral_constant_expression_p
6621 = parser->allow_non_integral_constant_expression_p;
6622 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6623 /* We are now parsing a constant-expression. */
6624 parser->integral_constant_expression_p = true;
6625 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6626 parser->non_integral_constant_expression_p = false;
6627 /* Although the grammar says "conditional-expression", we parse an
6628 "assignment-expression", which also permits "throw-expression"
6629 and the use of assignment operators. In the case that
6630 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6631 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6632 actually essential that we look for an assignment-expression.
6633 For example, cp_parser_initializer_clauses uses this function to
6634 determine whether a particular assignment-expression is in fact
6636 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6637 /* Restore the old settings. */
6638 parser->integral_constant_expression_p
6639 = saved_integral_constant_expression_p;
6640 parser->allow_non_integral_constant_expression_p
6641 = saved_allow_non_integral_constant_expression_p;
6642 if (allow_non_constant_p)
6643 *non_constant_p = parser->non_integral_constant_expression_p;
6644 else if (parser->non_integral_constant_expression_p)
6645 expression = error_mark_node;
6646 parser->non_integral_constant_expression_p
6647 = saved_non_integral_constant_expression_p;
6652 /* Parse __builtin_offsetof.
6654 offsetof-expression:
6655 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6657 offsetof-member-designator:
6659 | offsetof-member-designator "." id-expression
6660 | offsetof-member-designator "[" expression "]"
6661 | offsetof-member-designator "->" id-expression */
6664 cp_parser_builtin_offsetof (cp_parser *parser)
6666 int save_ice_p, save_non_ice_p;
6671 /* We're about to accept non-integral-constant things, but will
6672 definitely yield an integral constant expression. Save and
6673 restore these values around our local parsing. */
6674 save_ice_p = parser->integral_constant_expression_p;
6675 save_non_ice_p = parser->non_integral_constant_expression_p;
6677 /* Consume the "__builtin_offsetof" token. */
6678 cp_lexer_consume_token (parser->lexer);
6679 /* Consume the opening `('. */
6680 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6681 /* Parse the type-id. */
6682 type = cp_parser_type_id (parser);
6683 /* Look for the `,'. */
6684 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6685 token = cp_lexer_peek_token (parser->lexer);
6687 /* Build the (type *)null that begins the traditional offsetof macro. */
6688 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6689 tf_warning_or_error);
6691 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6692 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6693 true, &dummy, token->location);
6696 token = cp_lexer_peek_token (parser->lexer);
6697 switch (token->type)
6699 case CPP_OPEN_SQUARE:
6700 /* offsetof-member-designator "[" expression "]" */
6701 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6705 /* offsetof-member-designator "->" identifier */
6706 expr = grok_array_decl (expr, integer_zero_node);
6710 /* offsetof-member-designator "." identifier */
6711 cp_lexer_consume_token (parser->lexer);
6712 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6717 case CPP_CLOSE_PAREN:
6718 /* Consume the ")" token. */
6719 cp_lexer_consume_token (parser->lexer);
6723 /* Error. We know the following require will fail, but
6724 that gives the proper error message. */
6725 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6726 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6727 expr = error_mark_node;
6733 /* If we're processing a template, we can't finish the semantics yet.
6734 Otherwise we can fold the entire expression now. */
6735 if (processing_template_decl)
6736 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6738 expr = finish_offsetof (expr);
6741 parser->integral_constant_expression_p = save_ice_p;
6742 parser->non_integral_constant_expression_p = save_non_ice_p;
6747 /* Parse a trait expression. */
6750 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6753 tree type1, type2 = NULL_TREE;
6754 bool binary = false;
6755 cp_decl_specifier_seq decl_specs;
6759 case RID_HAS_NOTHROW_ASSIGN:
6760 kind = CPTK_HAS_NOTHROW_ASSIGN;
6762 case RID_HAS_NOTHROW_CONSTRUCTOR:
6763 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6765 case RID_HAS_NOTHROW_COPY:
6766 kind = CPTK_HAS_NOTHROW_COPY;
6768 case RID_HAS_TRIVIAL_ASSIGN:
6769 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6771 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6772 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6774 case RID_HAS_TRIVIAL_COPY:
6775 kind = CPTK_HAS_TRIVIAL_COPY;
6777 case RID_HAS_TRIVIAL_DESTRUCTOR:
6778 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6780 case RID_HAS_VIRTUAL_DESTRUCTOR:
6781 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6783 case RID_IS_ABSTRACT:
6784 kind = CPTK_IS_ABSTRACT;
6786 case RID_IS_BASE_OF:
6787 kind = CPTK_IS_BASE_OF;
6791 kind = CPTK_IS_CLASS;
6793 case RID_IS_CONVERTIBLE_TO:
6794 kind = CPTK_IS_CONVERTIBLE_TO;
6798 kind = CPTK_IS_EMPTY;
6801 kind = CPTK_IS_ENUM;
6806 case RID_IS_POLYMORPHIC:
6807 kind = CPTK_IS_POLYMORPHIC;
6810 kind = CPTK_IS_UNION;
6816 /* Consume the token. */
6817 cp_lexer_consume_token (parser->lexer);
6819 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6821 type1 = cp_parser_type_id (parser);
6823 if (type1 == error_mark_node)
6824 return error_mark_node;
6826 /* Build a trivial decl-specifier-seq. */
6827 clear_decl_specs (&decl_specs);
6828 decl_specs.type = type1;
6830 /* Call grokdeclarator to figure out what type this is. */
6831 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6832 /*initialized=*/0, /*attrlist=*/NULL);
6836 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6838 type2 = cp_parser_type_id (parser);
6840 if (type2 == error_mark_node)
6841 return error_mark_node;
6843 /* Build a trivial decl-specifier-seq. */
6844 clear_decl_specs (&decl_specs);
6845 decl_specs.type = type2;
6847 /* Call grokdeclarator to figure out what type this is. */
6848 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6849 /*initialized=*/0, /*attrlist=*/NULL);
6852 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6854 /* Complete the trait expression, which may mean either processing
6855 the trait expr now or saving it for template instantiation. */
6856 return finish_trait_expr (kind, type1, type2);
6859 /* Statements [gram.stmt.stmt] */
6861 /* Parse a statement.
6865 expression-statement
6870 declaration-statement
6873 IN_COMPOUND is true when the statement is nested inside a
6874 cp_parser_compound_statement; this matters for certain pragmas.
6876 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6877 is a (possibly labeled) if statement which is not enclosed in braces
6878 and has an else clause. This is used to implement -Wparentheses. */
6881 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6882 bool in_compound, bool *if_p)
6886 location_t statement_location;
6891 /* There is no statement yet. */
6892 statement = NULL_TREE;
6893 /* Peek at the next token. */
6894 token = cp_lexer_peek_token (parser->lexer);
6895 /* Remember the location of the first token in the statement. */
6896 statement_location = token->location;
6897 /* If this is a keyword, then that will often determine what kind of
6898 statement we have. */
6899 if (token->type == CPP_KEYWORD)
6901 enum rid keyword = token->keyword;
6907 /* Looks like a labeled-statement with a case label.
6908 Parse the label, and then use tail recursion to parse
6910 cp_parser_label_for_labeled_statement (parser);
6915 statement = cp_parser_selection_statement (parser, if_p);
6921 statement = cp_parser_iteration_statement (parser);
6928 statement = cp_parser_jump_statement (parser);
6931 /* Objective-C++ exception-handling constructs. */
6934 case RID_AT_FINALLY:
6935 case RID_AT_SYNCHRONIZED:
6937 statement = cp_parser_objc_statement (parser);
6941 statement = cp_parser_try_block (parser);
6945 /* This must be a namespace alias definition. */
6946 cp_parser_declaration_statement (parser);
6950 /* It might be a keyword like `int' that can start a
6951 declaration-statement. */
6955 else if (token->type == CPP_NAME)
6957 /* If the next token is a `:', then we are looking at a
6958 labeled-statement. */
6959 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6960 if (token->type == CPP_COLON)
6962 /* Looks like a labeled-statement with an ordinary label.
6963 Parse the label, and then use tail recursion to parse
6965 cp_parser_label_for_labeled_statement (parser);
6969 /* Anything that starts with a `{' must be a compound-statement. */
6970 else if (token->type == CPP_OPEN_BRACE)
6971 statement = cp_parser_compound_statement (parser, NULL, false);
6972 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6973 a statement all its own. */
6974 else if (token->type == CPP_PRAGMA)
6976 /* Only certain OpenMP pragmas are attached to statements, and thus
6977 are considered statements themselves. All others are not. In
6978 the context of a compound, accept the pragma as a "statement" and
6979 return so that we can check for a close brace. Otherwise we
6980 require a real statement and must go back and read one. */
6982 cp_parser_pragma (parser, pragma_compound);
6983 else if (!cp_parser_pragma (parser, pragma_stmt))
6987 else if (token->type == CPP_EOF)
6989 cp_parser_error (parser, "expected statement");
6993 /* Everything else must be a declaration-statement or an
6994 expression-statement. Try for the declaration-statement
6995 first, unless we are looking at a `;', in which case we know that
6996 we have an expression-statement. */
6999 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7001 cp_parser_parse_tentatively (parser);
7002 /* Try to parse the declaration-statement. */
7003 cp_parser_declaration_statement (parser);
7004 /* If that worked, we're done. */
7005 if (cp_parser_parse_definitely (parser))
7008 /* Look for an expression-statement instead. */
7009 statement = cp_parser_expression_statement (parser, in_statement_expr);
7012 /* Set the line number for the statement. */
7013 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7014 SET_EXPR_LOCATION (statement, statement_location);
7017 /* Parse the label for a labeled-statement, i.e.
7020 case constant-expression :
7024 case constant-expression ... constant-expression : statement
7026 When a label is parsed without errors, the label is added to the
7027 parse tree by the finish_* functions, so this function doesn't
7028 have to return the label. */
7031 cp_parser_label_for_labeled_statement (cp_parser* parser)
7035 /* The next token should be an identifier. */
7036 token = cp_lexer_peek_token (parser->lexer);
7037 if (token->type != CPP_NAME
7038 && token->type != CPP_KEYWORD)
7040 cp_parser_error (parser, "expected labeled-statement");
7044 switch (token->keyword)
7051 /* Consume the `case' token. */
7052 cp_lexer_consume_token (parser->lexer);
7053 /* Parse the constant-expression. */
7054 expr = cp_parser_constant_expression (parser,
7055 /*allow_non_constant_p=*/false,
7058 ellipsis = cp_lexer_peek_token (parser->lexer);
7059 if (ellipsis->type == CPP_ELLIPSIS)
7061 /* Consume the `...' token. */
7062 cp_lexer_consume_token (parser->lexer);
7064 cp_parser_constant_expression (parser,
7065 /*allow_non_constant_p=*/false,
7067 /* We don't need to emit warnings here, as the common code
7068 will do this for us. */
7071 expr_hi = NULL_TREE;
7073 if (parser->in_switch_statement_p)
7074 finish_case_label (expr, expr_hi);
7076 error ("%Hcase label %qE not within a switch statement",
7077 &token->location, expr);
7082 /* Consume the `default' token. */
7083 cp_lexer_consume_token (parser->lexer);
7085 if (parser->in_switch_statement_p)
7086 finish_case_label (NULL_TREE, NULL_TREE);
7088 error ("%Hcase label not within a switch statement", &token->location);
7092 /* Anything else must be an ordinary label. */
7093 finish_label_stmt (cp_parser_identifier (parser));
7097 /* Require the `:' token. */
7098 cp_parser_require (parser, CPP_COLON, "%<:%>");
7101 /* Parse an expression-statement.
7103 expression-statement:
7106 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7107 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7108 indicates whether this expression-statement is part of an
7109 expression statement. */
7112 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7114 tree statement = NULL_TREE;
7116 /* If the next token is a ';', then there is no expression
7118 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7119 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7121 /* Consume the final `;'. */
7122 cp_parser_consume_semicolon_at_end_of_statement (parser);
7124 if (in_statement_expr
7125 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7126 /* This is the final expression statement of a statement
7128 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7130 statement = finish_expr_stmt (statement);
7137 /* Parse a compound-statement.
7140 { statement-seq [opt] }
7145 { label-declaration-seq [opt] statement-seq [opt] }
7147 label-declaration-seq:
7149 label-declaration-seq label-declaration
7151 Returns a tree representing the statement. */
7154 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7159 /* Consume the `{'. */
7160 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7161 return error_mark_node;
7162 /* Begin the compound-statement. */
7163 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7164 /* If the next keyword is `__label__' we have a label declaration. */
7165 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7166 cp_parser_label_declaration (parser);
7167 /* Parse an (optional) statement-seq. */
7168 cp_parser_statement_seq_opt (parser, in_statement_expr);
7169 /* Finish the compound-statement. */
7170 finish_compound_stmt (compound_stmt);
7171 /* Consume the `}'. */
7172 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7174 return compound_stmt;
7177 /* Parse an (optional) statement-seq.
7181 statement-seq [opt] statement */
7184 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7186 /* Scan statements until there aren't any more. */
7189 cp_token *token = cp_lexer_peek_token (parser->lexer);
7191 /* If we're looking at a `}', then we've run out of statements. */
7192 if (token->type == CPP_CLOSE_BRACE
7193 || token->type == CPP_EOF
7194 || token->type == CPP_PRAGMA_EOL)
7197 /* If we are in a compound statement and find 'else' then
7198 something went wrong. */
7199 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7201 if (parser->in_statement & IN_IF_STMT)
7205 token = cp_lexer_consume_token (parser->lexer);
7206 error ("%H%<else%> without a previous %<if%>", &token->location);
7210 /* Parse the statement. */
7211 cp_parser_statement (parser, in_statement_expr, true, NULL);
7215 /* Parse a selection-statement.
7217 selection-statement:
7218 if ( condition ) statement
7219 if ( condition ) statement else statement
7220 switch ( condition ) statement
7222 Returns the new IF_STMT or SWITCH_STMT.
7224 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7225 is a (possibly labeled) if statement which is not enclosed in
7226 braces and has an else clause. This is used to implement
7230 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7238 /* Peek at the next token. */
7239 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7241 /* See what kind of keyword it is. */
7242 keyword = token->keyword;
7251 /* Look for the `('. */
7252 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7254 cp_parser_skip_to_end_of_statement (parser);
7255 return error_mark_node;
7258 /* Begin the selection-statement. */
7259 if (keyword == RID_IF)
7260 statement = begin_if_stmt ();
7262 statement = begin_switch_stmt ();
7264 /* Parse the condition. */
7265 condition = cp_parser_condition (parser);
7266 /* Look for the `)'. */
7267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7268 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7269 /*consume_paren=*/true);
7271 if (keyword == RID_IF)
7274 unsigned char in_statement;
7276 /* Add the condition. */
7277 finish_if_stmt_cond (condition, statement);
7279 /* Parse the then-clause. */
7280 in_statement = parser->in_statement;
7281 parser->in_statement |= IN_IF_STMT;
7282 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7284 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7285 add_stmt (build_empty_stmt ());
7286 cp_lexer_consume_token (parser->lexer);
7287 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7288 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7289 "empty body in an %<if%> statement");
7293 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7294 parser->in_statement = in_statement;
7296 finish_then_clause (statement);
7298 /* If the next token is `else', parse the else-clause. */
7299 if (cp_lexer_next_token_is_keyword (parser->lexer,
7302 /* Consume the `else' keyword. */
7303 cp_lexer_consume_token (parser->lexer);
7304 begin_else_clause (statement);
7305 /* Parse the else-clause. */
7306 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7308 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7309 OPT_Wempty_body, "suggest braces around "
7310 "empty body in an %<else%> statement");
7311 add_stmt (build_empty_stmt ());
7312 cp_lexer_consume_token (parser->lexer);
7315 cp_parser_implicitly_scoped_statement (parser, NULL);
7317 finish_else_clause (statement);
7319 /* If we are currently parsing a then-clause, then
7320 IF_P will not be NULL. We set it to true to
7321 indicate that this if statement has an else clause.
7322 This may trigger the Wparentheses warning below
7323 when we get back up to the parent if statement. */
7329 /* This if statement does not have an else clause. If
7330 NESTED_IF is true, then the then-clause is an if
7331 statement which does have an else clause. We warn
7332 about the potential ambiguity. */
7334 warning (OPT_Wparentheses,
7335 ("%Hsuggest explicit braces "
7336 "to avoid ambiguous %<else%>"),
7337 EXPR_LOCUS (statement));
7340 /* Now we're all done with the if-statement. */
7341 finish_if_stmt (statement);
7345 bool in_switch_statement_p;
7346 unsigned char in_statement;
7348 /* Add the condition. */
7349 finish_switch_cond (condition, statement);
7351 /* Parse the body of the switch-statement. */
7352 in_switch_statement_p = parser->in_switch_statement_p;
7353 in_statement = parser->in_statement;
7354 parser->in_switch_statement_p = true;
7355 parser->in_statement |= IN_SWITCH_STMT;
7356 cp_parser_implicitly_scoped_statement (parser, NULL);
7357 parser->in_switch_statement_p = in_switch_statement_p;
7358 parser->in_statement = in_statement;
7360 /* Now we're all done with the switch-statement. */
7361 finish_switch_stmt (statement);
7369 cp_parser_error (parser, "expected selection-statement");
7370 return error_mark_node;
7374 /* Parse a condition.
7378 type-specifier-seq declarator = initializer-clause
7379 type-specifier-seq declarator braced-init-list
7384 type-specifier-seq declarator asm-specification [opt]
7385 attributes [opt] = assignment-expression
7387 Returns the expression that should be tested. */
7390 cp_parser_condition (cp_parser* parser)
7392 cp_decl_specifier_seq type_specifiers;
7393 const char *saved_message;
7395 /* Try the declaration first. */
7396 cp_parser_parse_tentatively (parser);
7397 /* New types are not allowed in the type-specifier-seq for a
7399 saved_message = parser->type_definition_forbidden_message;
7400 parser->type_definition_forbidden_message
7401 = "types may not be defined in conditions";
7402 /* Parse the type-specifier-seq. */
7403 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7405 /* Restore the saved message. */
7406 parser->type_definition_forbidden_message = saved_message;
7407 /* If all is well, we might be looking at a declaration. */
7408 if (!cp_parser_error_occurred (parser))
7411 tree asm_specification;
7413 cp_declarator *declarator;
7414 tree initializer = NULL_TREE;
7416 /* Parse the declarator. */
7417 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7418 /*ctor_dtor_or_conv_p=*/NULL,
7419 /*parenthesized_p=*/NULL,
7420 /*member_p=*/false);
7421 /* Parse the attributes. */
7422 attributes = cp_parser_attributes_opt (parser);
7423 /* Parse the asm-specification. */
7424 asm_specification = cp_parser_asm_specification_opt (parser);
7425 /* If the next token is not an `=' or '{', then we might still be
7426 looking at an expression. For example:
7430 looks like a decl-specifier-seq and a declarator -- but then
7431 there is no `=', so this is an expression. */
7432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7433 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7434 cp_parser_simulate_error (parser);
7436 /* If we did see an `=' or '{', then we are looking at a declaration
7438 if (cp_parser_parse_definitely (parser))
7441 bool non_constant_p;
7442 bool flags = LOOKUP_ONLYCONVERTING;
7444 /* Create the declaration. */
7445 decl = start_decl (declarator, &type_specifiers,
7446 /*initialized_p=*/true,
7447 attributes, /*prefix_attributes=*/NULL_TREE,
7450 /* Parse the initializer. */
7451 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7453 initializer = cp_parser_braced_list (parser, &non_constant_p);
7454 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7459 /* Consume the `='. */
7460 cp_parser_require (parser, CPP_EQ, "%<=%>");
7461 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7463 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7464 maybe_warn_cpp0x ("extended initializer lists");
7466 if (!non_constant_p)
7467 initializer = fold_non_dependent_expr (initializer);
7469 /* Process the initializer. */
7470 cp_finish_decl (decl,
7471 initializer, !non_constant_p,
7476 pop_scope (pushed_scope);
7478 return convert_from_reference (decl);
7481 /* If we didn't even get past the declarator successfully, we are
7482 definitely not looking at a declaration. */
7484 cp_parser_abort_tentative_parse (parser);
7486 /* Otherwise, we are looking at an expression. */
7487 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7490 /* Parse an iteration-statement.
7492 iteration-statement:
7493 while ( condition ) statement
7494 do statement while ( expression ) ;
7495 for ( for-init-statement condition [opt] ; expression [opt] )
7498 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7501 cp_parser_iteration_statement (cp_parser* parser)
7506 unsigned char in_statement;
7508 /* Peek at the next token. */
7509 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7511 return error_mark_node;
7513 /* Remember whether or not we are already within an iteration
7515 in_statement = parser->in_statement;
7517 /* See what kind of keyword it is. */
7518 keyword = token->keyword;
7525 /* Begin the while-statement. */
7526 statement = begin_while_stmt ();
7527 /* Look for the `('. */
7528 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7529 /* Parse the condition. */
7530 condition = cp_parser_condition (parser);
7531 finish_while_stmt_cond (condition, statement);
7532 /* Look for the `)'. */
7533 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7534 /* Parse the dependent statement. */
7535 parser->in_statement = IN_ITERATION_STMT;
7536 cp_parser_already_scoped_statement (parser);
7537 parser->in_statement = in_statement;
7538 /* We're done with the while-statement. */
7539 finish_while_stmt (statement);
7547 /* Begin the do-statement. */
7548 statement = begin_do_stmt ();
7549 /* Parse the body of the do-statement. */
7550 parser->in_statement = IN_ITERATION_STMT;
7551 cp_parser_implicitly_scoped_statement (parser, NULL);
7552 parser->in_statement = in_statement;
7553 finish_do_body (statement);
7554 /* Look for the `while' keyword. */
7555 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7556 /* Look for the `('. */
7557 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7558 /* Parse the expression. */
7559 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7560 /* We're done with the do-statement. */
7561 finish_do_stmt (expression, statement);
7562 /* Look for the `)'. */
7563 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7564 /* Look for the `;'. */
7565 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7571 tree condition = NULL_TREE;
7572 tree expression = NULL_TREE;
7574 /* Begin the for-statement. */
7575 statement = begin_for_stmt ();
7576 /* Look for the `('. */
7577 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7578 /* Parse the initialization. */
7579 cp_parser_for_init_statement (parser);
7580 finish_for_init_stmt (statement);
7582 /* If there's a condition, process it. */
7583 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7584 condition = cp_parser_condition (parser);
7585 finish_for_cond (condition, statement);
7586 /* Look for the `;'. */
7587 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7589 /* If there's an expression, process it. */
7590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7591 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7592 finish_for_expr (expression, statement);
7593 /* Look for the `)'. */
7594 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7596 /* Parse the body of the for-statement. */
7597 parser->in_statement = IN_ITERATION_STMT;
7598 cp_parser_already_scoped_statement (parser);
7599 parser->in_statement = in_statement;
7601 /* We're done with the for-statement. */
7602 finish_for_stmt (statement);
7607 cp_parser_error (parser, "expected iteration-statement");
7608 statement = error_mark_node;
7615 /* Parse a for-init-statement.
7618 expression-statement
7619 simple-declaration */
7622 cp_parser_for_init_statement (cp_parser* parser)
7624 /* If the next token is a `;', then we have an empty
7625 expression-statement. Grammatically, this is also a
7626 simple-declaration, but an invalid one, because it does not
7627 declare anything. Therefore, if we did not handle this case
7628 specially, we would issue an error message about an invalid
7630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7632 /* We're going to speculatively look for a declaration, falling back
7633 to an expression, if necessary. */
7634 cp_parser_parse_tentatively (parser);
7635 /* Parse the declaration. */
7636 cp_parser_simple_declaration (parser,
7637 /*function_definition_allowed_p=*/false);
7638 /* If the tentative parse failed, then we shall need to look for an
7639 expression-statement. */
7640 if (cp_parser_parse_definitely (parser))
7644 cp_parser_expression_statement (parser, false);
7647 /* Parse a jump-statement.
7652 return expression [opt] ;
7653 return braced-init-list ;
7661 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7664 cp_parser_jump_statement (cp_parser* parser)
7666 tree statement = error_mark_node;
7669 unsigned char in_statement;
7671 /* Peek at the next token. */
7672 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7674 return error_mark_node;
7676 /* See what kind of keyword it is. */
7677 keyword = token->keyword;
7681 in_statement = parser->in_statement & ~IN_IF_STMT;
7682 switch (in_statement)
7685 error ("%Hbreak statement not within loop or switch", &token->location);
7688 gcc_assert ((in_statement & IN_SWITCH_STMT)
7689 || in_statement == IN_ITERATION_STMT);
7690 statement = finish_break_stmt ();
7693 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7696 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7699 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7703 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7706 error ("%Hcontinue statement not within a loop", &token->location);
7708 case IN_ITERATION_STMT:
7710 statement = finish_continue_stmt ();
7713 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7718 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7724 bool expr_non_constant_p;
7726 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7728 maybe_warn_cpp0x ("extended initializer lists");
7729 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7731 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7732 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7734 /* If the next token is a `;', then there is no
7737 /* Build the return-statement. */
7738 statement = finish_return_stmt (expr);
7739 /* Look for the final `;'. */
7740 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7745 /* Create the goto-statement. */
7746 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7748 /* Issue a warning about this use of a GNU extension. */
7749 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7750 /* Consume the '*' token. */
7751 cp_lexer_consume_token (parser->lexer);
7752 /* Parse the dependent expression. */
7753 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7756 finish_goto_stmt (cp_parser_identifier (parser));
7757 /* Look for the final `;'. */
7758 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7762 cp_parser_error (parser, "expected jump-statement");
7769 /* Parse a declaration-statement.
7771 declaration-statement:
7772 block-declaration */
7775 cp_parser_declaration_statement (cp_parser* parser)
7779 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7780 p = obstack_alloc (&declarator_obstack, 0);
7782 /* Parse the block-declaration. */
7783 cp_parser_block_declaration (parser, /*statement_p=*/true);
7785 /* Free any declarators allocated. */
7786 obstack_free (&declarator_obstack, p);
7788 /* Finish off the statement. */
7792 /* Some dependent statements (like `if (cond) statement'), are
7793 implicitly in their own scope. In other words, if the statement is
7794 a single statement (as opposed to a compound-statement), it is
7795 none-the-less treated as if it were enclosed in braces. Any
7796 declarations appearing in the dependent statement are out of scope
7797 after control passes that point. This function parses a statement,
7798 but ensures that is in its own scope, even if it is not a
7801 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7802 is a (possibly labeled) if statement which is not enclosed in
7803 braces and has an else clause. This is used to implement
7806 Returns the new statement. */
7809 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7816 /* Mark if () ; with a special NOP_EXPR. */
7817 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7819 cp_lexer_consume_token (parser->lexer);
7820 statement = add_stmt (build_empty_stmt ());
7822 /* if a compound is opened, we simply parse the statement directly. */
7823 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7824 statement = cp_parser_compound_statement (parser, NULL, false);
7825 /* If the token is not a `{', then we must take special action. */
7828 /* Create a compound-statement. */
7829 statement = begin_compound_stmt (0);
7830 /* Parse the dependent-statement. */
7831 cp_parser_statement (parser, NULL_TREE, false, if_p);
7832 /* Finish the dummy compound-statement. */
7833 finish_compound_stmt (statement);
7836 /* Return the statement. */
7840 /* For some dependent statements (like `while (cond) statement'), we
7841 have already created a scope. Therefore, even if the dependent
7842 statement is a compound-statement, we do not want to create another
7846 cp_parser_already_scoped_statement (cp_parser* parser)
7848 /* If the token is a `{', then we must take special action. */
7849 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7850 cp_parser_statement (parser, NULL_TREE, false, NULL);
7853 /* Avoid calling cp_parser_compound_statement, so that we
7854 don't create a new scope. Do everything else by hand. */
7855 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7856 /* If the next keyword is `__label__' we have a label declaration. */
7857 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7858 cp_parser_label_declaration (parser);
7859 /* Parse an (optional) statement-seq. */
7860 cp_parser_statement_seq_opt (parser, NULL_TREE);
7861 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7865 /* Declarations [gram.dcl.dcl] */
7867 /* Parse an optional declaration-sequence.
7871 declaration-seq declaration */
7874 cp_parser_declaration_seq_opt (cp_parser* parser)
7880 token = cp_lexer_peek_token (parser->lexer);
7882 if (token->type == CPP_CLOSE_BRACE
7883 || token->type == CPP_EOF
7884 || token->type == CPP_PRAGMA_EOL)
7887 if (token->type == CPP_SEMICOLON)
7889 /* A declaration consisting of a single semicolon is
7890 invalid. Allow it unless we're being pedantic. */
7891 cp_lexer_consume_token (parser->lexer);
7892 if (!in_system_header)
7893 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7897 /* If we're entering or exiting a region that's implicitly
7898 extern "C", modify the lang context appropriately. */
7899 if (!parser->implicit_extern_c && token->implicit_extern_c)
7901 push_lang_context (lang_name_c);
7902 parser->implicit_extern_c = true;
7904 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7906 pop_lang_context ();
7907 parser->implicit_extern_c = false;
7910 if (token->type == CPP_PRAGMA)
7912 /* A top-level declaration can consist solely of a #pragma.
7913 A nested declaration cannot, so this is done here and not
7914 in cp_parser_declaration. (A #pragma at block scope is
7915 handled in cp_parser_statement.) */
7916 cp_parser_pragma (parser, pragma_external);
7920 /* Parse the declaration itself. */
7921 cp_parser_declaration (parser);
7925 /* Parse a declaration.
7930 template-declaration
7931 explicit-instantiation
7932 explicit-specialization
7933 linkage-specification
7934 namespace-definition
7939 __extension__ declaration */
7942 cp_parser_declaration (cp_parser* parser)
7949 /* Check for the `__extension__' keyword. */
7950 if (cp_parser_extension_opt (parser, &saved_pedantic))
7952 /* Parse the qualified declaration. */
7953 cp_parser_declaration (parser);
7954 /* Restore the PEDANTIC flag. */
7955 pedantic = saved_pedantic;
7960 /* Try to figure out what kind of declaration is present. */
7961 token1 = *cp_lexer_peek_token (parser->lexer);
7963 if (token1.type != CPP_EOF)
7964 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7967 token2.type = CPP_EOF;
7968 token2.keyword = RID_MAX;
7971 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7972 p = obstack_alloc (&declarator_obstack, 0);
7974 /* If the next token is `extern' and the following token is a string
7975 literal, then we have a linkage specification. */
7976 if (token1.keyword == RID_EXTERN
7977 && cp_parser_is_string_literal (&token2))
7978 cp_parser_linkage_specification (parser);
7979 /* If the next token is `template', then we have either a template
7980 declaration, an explicit instantiation, or an explicit
7982 else if (token1.keyword == RID_TEMPLATE)
7984 /* `template <>' indicates a template specialization. */
7985 if (token2.type == CPP_LESS
7986 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7987 cp_parser_explicit_specialization (parser);
7988 /* `template <' indicates a template declaration. */
7989 else if (token2.type == CPP_LESS)
7990 cp_parser_template_declaration (parser, /*member_p=*/false);
7991 /* Anything else must be an explicit instantiation. */
7993 cp_parser_explicit_instantiation (parser);
7995 /* If the next token is `export', then we have a template
7997 else if (token1.keyword == RID_EXPORT)
7998 cp_parser_template_declaration (parser, /*member_p=*/false);
7999 /* If the next token is `extern', 'static' or 'inline' and the one
8000 after that is `template', we have a GNU extended explicit
8001 instantiation directive. */
8002 else if (cp_parser_allow_gnu_extensions_p (parser)
8003 && (token1.keyword == RID_EXTERN
8004 || token1.keyword == RID_STATIC
8005 || token1.keyword == RID_INLINE)
8006 && token2.keyword == RID_TEMPLATE)
8007 cp_parser_explicit_instantiation (parser);
8008 /* If the next token is `namespace', check for a named or unnamed
8009 namespace definition. */
8010 else if (token1.keyword == RID_NAMESPACE
8011 && (/* A named namespace definition. */
8012 (token2.type == CPP_NAME
8013 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8015 /* An unnamed namespace definition. */
8016 || token2.type == CPP_OPEN_BRACE
8017 || token2.keyword == RID_ATTRIBUTE))
8018 cp_parser_namespace_definition (parser);
8019 /* An inline (associated) namespace definition. */
8020 else if (token1.keyword == RID_INLINE
8021 && token2.keyword == RID_NAMESPACE)
8022 cp_parser_namespace_definition (parser);
8023 /* Objective-C++ declaration/definition. */
8024 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8025 cp_parser_objc_declaration (parser);
8026 /* We must have either a block declaration or a function
8029 /* Try to parse a block-declaration, or a function-definition. */
8030 cp_parser_block_declaration (parser, /*statement_p=*/false);
8032 /* Free any declarators allocated. */
8033 obstack_free (&declarator_obstack, p);
8036 /* Parse a block-declaration.
8041 namespace-alias-definition
8048 __extension__ block-declaration
8053 static_assert-declaration
8055 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8056 part of a declaration-statement. */
8059 cp_parser_block_declaration (cp_parser *parser,
8065 /* Check for the `__extension__' keyword. */
8066 if (cp_parser_extension_opt (parser, &saved_pedantic))
8068 /* Parse the qualified declaration. */
8069 cp_parser_block_declaration (parser, statement_p);
8070 /* Restore the PEDANTIC flag. */
8071 pedantic = saved_pedantic;
8076 /* Peek at the next token to figure out which kind of declaration is
8078 token1 = cp_lexer_peek_token (parser->lexer);
8080 /* If the next keyword is `asm', we have an asm-definition. */
8081 if (token1->keyword == RID_ASM)
8084 cp_parser_commit_to_tentative_parse (parser);
8085 cp_parser_asm_definition (parser);
8087 /* If the next keyword is `namespace', we have a
8088 namespace-alias-definition. */
8089 else if (token1->keyword == RID_NAMESPACE)
8090 cp_parser_namespace_alias_definition (parser);
8091 /* If the next keyword is `using', we have either a
8092 using-declaration or a using-directive. */
8093 else if (token1->keyword == RID_USING)
8098 cp_parser_commit_to_tentative_parse (parser);
8099 /* If the token after `using' is `namespace', then we have a
8101 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8102 if (token2->keyword == RID_NAMESPACE)
8103 cp_parser_using_directive (parser);
8104 /* Otherwise, it's a using-declaration. */
8106 cp_parser_using_declaration (parser,
8107 /*access_declaration_p=*/false);
8109 /* If the next keyword is `__label__' we have a misplaced label
8111 else if (token1->keyword == RID_LABEL)
8113 cp_lexer_consume_token (parser->lexer);
8114 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8115 cp_parser_skip_to_end_of_statement (parser);
8116 /* If the next token is now a `;', consume it. */
8117 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8118 cp_lexer_consume_token (parser->lexer);
8120 /* If the next token is `static_assert' we have a static assertion. */
8121 else if (token1->keyword == RID_STATIC_ASSERT)
8122 cp_parser_static_assert (parser, /*member_p=*/false);
8123 /* Anything else must be a simple-declaration. */
8125 cp_parser_simple_declaration (parser, !statement_p);
8128 /* Parse a simple-declaration.
8131 decl-specifier-seq [opt] init-declarator-list [opt] ;
8133 init-declarator-list:
8135 init-declarator-list , init-declarator
8137 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8138 function-definition as a simple-declaration. */
8141 cp_parser_simple_declaration (cp_parser* parser,
8142 bool function_definition_allowed_p)
8144 cp_decl_specifier_seq decl_specifiers;
8145 int declares_class_or_enum;
8146 bool saw_declarator;
8148 /* Defer access checks until we know what is being declared; the
8149 checks for names appearing in the decl-specifier-seq should be
8150 done as if we were in the scope of the thing being declared. */
8151 push_deferring_access_checks (dk_deferred);
8153 /* Parse the decl-specifier-seq. We have to keep track of whether
8154 or not the decl-specifier-seq declares a named class or
8155 enumeration type, since that is the only case in which the
8156 init-declarator-list is allowed to be empty.
8160 In a simple-declaration, the optional init-declarator-list can be
8161 omitted only when declaring a class or enumeration, that is when
8162 the decl-specifier-seq contains either a class-specifier, an
8163 elaborated-type-specifier, or an enum-specifier. */
8164 cp_parser_decl_specifier_seq (parser,
8165 CP_PARSER_FLAGS_OPTIONAL,
8167 &declares_class_or_enum);
8168 /* We no longer need to defer access checks. */
8169 stop_deferring_access_checks ();
8171 /* In a block scope, a valid declaration must always have a
8172 decl-specifier-seq. By not trying to parse declarators, we can
8173 resolve the declaration/expression ambiguity more quickly. */
8174 if (!function_definition_allowed_p
8175 && !decl_specifiers.any_specifiers_p)
8177 cp_parser_error (parser, "expected declaration");
8181 /* If the next two tokens are both identifiers, the code is
8182 erroneous. The usual cause of this situation is code like:
8186 where "T" should name a type -- but does not. */
8187 if (!decl_specifiers.type
8188 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8190 /* If parsing tentatively, we should commit; we really are
8191 looking at a declaration. */
8192 cp_parser_commit_to_tentative_parse (parser);
8197 /* If we have seen at least one decl-specifier, and the next token
8198 is not a parenthesis, then we must be looking at a declaration.
8199 (After "int (" we might be looking at a functional cast.) */
8200 if (decl_specifiers.any_specifiers_p
8201 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8202 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8203 && !cp_parser_error_occurred (parser))
8204 cp_parser_commit_to_tentative_parse (parser);
8206 /* Keep going until we hit the `;' at the end of the simple
8208 saw_declarator = false;
8209 while (cp_lexer_next_token_is_not (parser->lexer,
8213 bool function_definition_p;
8218 /* If we are processing next declarator, coma is expected */
8219 token = cp_lexer_peek_token (parser->lexer);
8220 gcc_assert (token->type == CPP_COMMA);
8221 cp_lexer_consume_token (parser->lexer);
8224 saw_declarator = true;
8226 /* Parse the init-declarator. */
8227 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8229 function_definition_allowed_p,
8231 declares_class_or_enum,
8232 &function_definition_p);
8233 /* If an error occurred while parsing tentatively, exit quickly.
8234 (That usually happens when in the body of a function; each
8235 statement is treated as a declaration-statement until proven
8237 if (cp_parser_error_occurred (parser))
8239 /* Handle function definitions specially. */
8240 if (function_definition_p)
8242 /* If the next token is a `,', then we are probably
8243 processing something like:
8247 which is erroneous. */
8248 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8250 cp_token *token = cp_lexer_peek_token (parser->lexer);
8251 error ("%Hmixing declarations and function-definitions is forbidden",
8254 /* Otherwise, we're done with the list of declarators. */
8257 pop_deferring_access_checks ();
8261 /* The next token should be either a `,' or a `;'. */
8262 token = cp_lexer_peek_token (parser->lexer);
8263 /* If it's a `,', there are more declarators to come. */
8264 if (token->type == CPP_COMMA)
8265 /* will be consumed next time around */;
8266 /* If it's a `;', we are done. */
8267 else if (token->type == CPP_SEMICOLON)
8269 /* Anything else is an error. */
8272 /* If we have already issued an error message we don't need
8273 to issue another one. */
8274 if (decl != error_mark_node
8275 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8276 cp_parser_error (parser, "expected %<,%> or %<;%>");
8277 /* Skip tokens until we reach the end of the statement. */
8278 cp_parser_skip_to_end_of_statement (parser);
8279 /* If the next token is now a `;', consume it. */
8280 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8281 cp_lexer_consume_token (parser->lexer);
8284 /* After the first time around, a function-definition is not
8285 allowed -- even if it was OK at first. For example:
8290 function_definition_allowed_p = false;
8293 /* Issue an error message if no declarators are present, and the
8294 decl-specifier-seq does not itself declare a class or
8296 if (!saw_declarator)
8298 if (cp_parser_declares_only_class_p (parser))
8299 shadow_tag (&decl_specifiers);
8300 /* Perform any deferred access checks. */
8301 perform_deferred_access_checks ();
8304 /* Consume the `;'. */
8305 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8308 pop_deferring_access_checks ();
8311 /* Parse a decl-specifier-seq.
8314 decl-specifier-seq [opt] decl-specifier
8317 storage-class-specifier
8328 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8330 The parser flags FLAGS is used to control type-specifier parsing.
8332 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8335 1: one of the decl-specifiers is an elaborated-type-specifier
8336 (i.e., a type declaration)
8337 2: one of the decl-specifiers is an enum-specifier or a
8338 class-specifier (i.e., a type definition)
8343 cp_parser_decl_specifier_seq (cp_parser* parser,
8344 cp_parser_flags flags,
8345 cp_decl_specifier_seq *decl_specs,
8346 int* declares_class_or_enum)
8348 bool constructor_possible_p = !parser->in_declarator_p;
8349 cp_token *start_token = NULL;
8351 /* Clear DECL_SPECS. */
8352 clear_decl_specs (decl_specs);
8354 /* Assume no class or enumeration type is declared. */
8355 *declares_class_or_enum = 0;
8357 /* Keep reading specifiers until there are no more to read. */
8361 bool found_decl_spec;
8364 /* Peek at the next token. */
8365 token = cp_lexer_peek_token (parser->lexer);
8367 /* Save the first token of the decl spec list for error
8370 start_token = token;
8371 /* Handle attributes. */
8372 if (token->keyword == RID_ATTRIBUTE)
8374 /* Parse the attributes. */
8375 decl_specs->attributes
8376 = chainon (decl_specs->attributes,
8377 cp_parser_attributes_opt (parser));
8380 /* Assume we will find a decl-specifier keyword. */
8381 found_decl_spec = true;
8382 /* If the next token is an appropriate keyword, we can simply
8383 add it to the list. */
8384 switch (token->keyword)
8389 if (!at_class_scope_p ())
8391 error ("%H%<friend%> used outside of class", &token->location);
8392 cp_lexer_purge_token (parser->lexer);
8396 ++decl_specs->specs[(int) ds_friend];
8397 /* Consume the token. */
8398 cp_lexer_consume_token (parser->lexer);
8402 /* function-specifier:
8409 cp_parser_function_specifier_opt (parser, decl_specs);
8415 ++decl_specs->specs[(int) ds_typedef];
8416 /* Consume the token. */
8417 cp_lexer_consume_token (parser->lexer);
8418 /* A constructor declarator cannot appear in a typedef. */
8419 constructor_possible_p = false;
8420 /* The "typedef" keyword can only occur in a declaration; we
8421 may as well commit at this point. */
8422 cp_parser_commit_to_tentative_parse (parser);
8424 if (decl_specs->storage_class != sc_none)
8425 decl_specs->conflicting_specifiers_p = true;
8428 /* storage-class-specifier:
8438 if (cxx_dialect == cxx98)
8440 /* Consume the token. */
8441 cp_lexer_consume_token (parser->lexer);
8443 /* Complain about `auto' as a storage specifier, if
8444 we're complaining about C++0x compatibility. */
8447 "%H%<auto%> will change meaning in C++0x; please remove it",
8450 /* Set the storage class anyway. */
8451 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8455 /* C++0x auto type-specifier. */
8456 found_decl_spec = false;
8463 /* Consume the token. */
8464 cp_lexer_consume_token (parser->lexer);
8465 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8469 /* Consume the token. */
8470 cp_lexer_consume_token (parser->lexer);
8471 ++decl_specs->specs[(int) ds_thread];
8475 /* We did not yet find a decl-specifier yet. */
8476 found_decl_spec = false;
8480 /* Constructors are a special case. The `S' in `S()' is not a
8481 decl-specifier; it is the beginning of the declarator. */
8484 && constructor_possible_p
8485 && (cp_parser_constructor_declarator_p
8486 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8488 /* If we don't have a DECL_SPEC yet, then we must be looking at
8489 a type-specifier. */
8490 if (!found_decl_spec && !constructor_p)
8492 int decl_spec_declares_class_or_enum;
8493 bool is_cv_qualifier;
8497 = cp_parser_type_specifier (parser, flags,
8499 /*is_declaration=*/true,
8500 &decl_spec_declares_class_or_enum,
8502 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8504 /* If this type-specifier referenced a user-defined type
8505 (a typedef, class-name, etc.), then we can't allow any
8506 more such type-specifiers henceforth.
8510 The longest sequence of decl-specifiers that could
8511 possibly be a type name is taken as the
8512 decl-specifier-seq of a declaration. The sequence shall
8513 be self-consistent as described below.
8517 As a general rule, at most one type-specifier is allowed
8518 in the complete decl-specifier-seq of a declaration. The
8519 only exceptions are the following:
8521 -- const or volatile can be combined with any other
8524 -- signed or unsigned can be combined with char, long,
8532 void g (const int Pc);
8534 Here, Pc is *not* part of the decl-specifier seq; it's
8535 the declarator. Therefore, once we see a type-specifier
8536 (other than a cv-qualifier), we forbid any additional
8537 user-defined types. We *do* still allow things like `int
8538 int' to be considered a decl-specifier-seq, and issue the
8539 error message later. */
8540 if (type_spec && !is_cv_qualifier)
8541 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8542 /* A constructor declarator cannot follow a type-specifier. */
8545 constructor_possible_p = false;
8546 found_decl_spec = true;
8550 /* If we still do not have a DECL_SPEC, then there are no more
8552 if (!found_decl_spec)
8555 decl_specs->any_specifiers_p = true;
8556 /* After we see one decl-specifier, further decl-specifiers are
8558 flags |= CP_PARSER_FLAGS_OPTIONAL;
8561 cp_parser_check_decl_spec (decl_specs, start_token->location);
8563 /* Don't allow a friend specifier with a class definition. */
8564 if (decl_specs->specs[(int) ds_friend] != 0
8565 && (*declares_class_or_enum & 2))
8566 error ("%Hclass definition may not be declared a friend",
8567 &start_token->location);
8570 /* Parse an (optional) storage-class-specifier.
8572 storage-class-specifier:
8581 storage-class-specifier:
8584 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8587 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8589 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8592 if (cxx_dialect != cxx98)
8594 /* Fall through for C++98. */
8601 /* Consume the token. */
8602 return cp_lexer_consume_token (parser->lexer)->u.value;
8609 /* Parse an (optional) function-specifier.
8616 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8617 Updates DECL_SPECS, if it is non-NULL. */
8620 cp_parser_function_specifier_opt (cp_parser* parser,
8621 cp_decl_specifier_seq *decl_specs)
8623 cp_token *token = cp_lexer_peek_token (parser->lexer);
8624 switch (token->keyword)
8628 ++decl_specs->specs[(int) ds_inline];
8632 /* 14.5.2.3 [temp.mem]
8634 A member function template shall not be virtual. */
8635 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8636 error ("%Htemplates may not be %<virtual%>", &token->location);
8637 else if (decl_specs)
8638 ++decl_specs->specs[(int) ds_virtual];
8643 ++decl_specs->specs[(int) ds_explicit];
8650 /* Consume the token. */
8651 return cp_lexer_consume_token (parser->lexer)->u.value;
8654 /* Parse a linkage-specification.
8656 linkage-specification:
8657 extern string-literal { declaration-seq [opt] }
8658 extern string-literal declaration */
8661 cp_parser_linkage_specification (cp_parser* parser)
8665 /* Look for the `extern' keyword. */
8666 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8668 /* Look for the string-literal. */
8669 linkage = cp_parser_string_literal (parser, false, false);
8671 /* Transform the literal into an identifier. If the literal is a
8672 wide-character string, or contains embedded NULs, then we can't
8673 handle it as the user wants. */
8674 if (strlen (TREE_STRING_POINTER (linkage))
8675 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8677 cp_parser_error (parser, "invalid linkage-specification");
8678 /* Assume C++ linkage. */
8679 linkage = lang_name_cplusplus;
8682 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8684 /* We're now using the new linkage. */
8685 push_lang_context (linkage);
8687 /* If the next token is a `{', then we're using the first
8689 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8691 /* Consume the `{' token. */
8692 cp_lexer_consume_token (parser->lexer);
8693 /* Parse the declarations. */
8694 cp_parser_declaration_seq_opt (parser);
8695 /* Look for the closing `}'. */
8696 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8698 /* Otherwise, there's just one declaration. */
8701 bool saved_in_unbraced_linkage_specification_p;
8703 saved_in_unbraced_linkage_specification_p
8704 = parser->in_unbraced_linkage_specification_p;
8705 parser->in_unbraced_linkage_specification_p = true;
8706 cp_parser_declaration (parser);
8707 parser->in_unbraced_linkage_specification_p
8708 = saved_in_unbraced_linkage_specification_p;
8711 /* We're done with the linkage-specification. */
8712 pop_lang_context ();
8715 /* Parse a static_assert-declaration.
8717 static_assert-declaration:
8718 static_assert ( constant-expression , string-literal ) ;
8720 If MEMBER_P, this static_assert is a class member. */
8723 cp_parser_static_assert(cp_parser *parser, bool member_p)
8728 location_t saved_loc;
8730 /* Peek at the `static_assert' token so we can keep track of exactly
8731 where the static assertion started. */
8732 token = cp_lexer_peek_token (parser->lexer);
8733 saved_loc = token->location;
8735 /* Look for the `static_assert' keyword. */
8736 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8737 "%<static_assert%>"))
8740 /* We know we are in a static assertion; commit to any tentative
8742 if (cp_parser_parsing_tentatively (parser))
8743 cp_parser_commit_to_tentative_parse (parser);
8745 /* Parse the `(' starting the static assertion condition. */
8746 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8748 /* Parse the constant-expression. */
8750 cp_parser_constant_expression (parser,
8751 /*allow_non_constant_p=*/false,
8752 /*non_constant_p=*/NULL);
8754 /* Parse the separating `,'. */
8755 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8757 /* Parse the string-literal message. */
8758 message = cp_parser_string_literal (parser,
8759 /*translate=*/false,
8762 /* A `)' completes the static assertion. */
8763 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8764 cp_parser_skip_to_closing_parenthesis (parser,
8765 /*recovering=*/true,
8767 /*consume_paren=*/true);
8769 /* A semicolon terminates the declaration. */
8770 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8772 /* Complete the static assertion, which may mean either processing
8773 the static assert now or saving it for template instantiation. */
8774 finish_static_assert (condition, message, saved_loc, member_p);
8777 /* Parse a `decltype' type. Returns the type.
8779 simple-type-specifier:
8780 decltype ( expression ) */
8783 cp_parser_decltype (cp_parser *parser)
8786 bool id_expression_or_member_access_p = false;
8787 const char *saved_message;
8788 bool saved_integral_constant_expression_p;
8789 bool saved_non_integral_constant_expression_p;
8790 cp_token *id_expr_start_token;
8792 /* Look for the `decltype' token. */
8793 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8794 return error_mark_node;
8796 /* Types cannot be defined in a `decltype' expression. Save away the
8798 saved_message = parser->type_definition_forbidden_message;
8800 /* And create the new one. */
8801 parser->type_definition_forbidden_message
8802 = "types may not be defined in %<decltype%> expressions";
8804 /* The restrictions on constant-expressions do not apply inside
8805 decltype expressions. */
8806 saved_integral_constant_expression_p
8807 = parser->integral_constant_expression_p;
8808 saved_non_integral_constant_expression_p
8809 = parser->non_integral_constant_expression_p;
8810 parser->integral_constant_expression_p = false;
8812 /* Do not actually evaluate the expression. */
8815 /* Parse the opening `('. */
8816 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8817 return error_mark_node;
8819 /* First, try parsing an id-expression. */
8820 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8821 cp_parser_parse_tentatively (parser);
8822 expr = cp_parser_id_expression (parser,
8823 /*template_keyword_p=*/false,
8824 /*check_dependency_p=*/true,
8825 /*template_p=*/NULL,
8826 /*declarator_p=*/false,
8827 /*optional_p=*/false);
8829 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8831 bool non_integral_constant_expression_p = false;
8832 tree id_expression = expr;
8834 const char *error_msg;
8836 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8837 /* Lookup the name we got back from the id-expression. */
8838 expr = cp_parser_lookup_name (parser, expr,
8840 /*is_template=*/false,
8841 /*is_namespace=*/false,
8842 /*check_dependency=*/true,
8843 /*ambiguous_decls=*/NULL,
8844 id_expr_start_token->location);
8847 && expr != error_mark_node
8848 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8849 && TREE_CODE (expr) != TYPE_DECL
8850 && (TREE_CODE (expr) != BIT_NOT_EXPR
8851 || !TYPE_P (TREE_OPERAND (expr, 0)))
8852 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8854 /* Complete lookup of the id-expression. */
8855 expr = (finish_id_expression
8856 (id_expression, expr, parser->scope, &idk,
8857 /*integral_constant_expression_p=*/false,
8858 /*allow_non_integral_constant_expression_p=*/true,
8859 &non_integral_constant_expression_p,
8860 /*template_p=*/false,
8862 /*address_p=*/false,
8863 /*template_arg_p=*/false,
8865 id_expr_start_token->location));
8867 if (expr == error_mark_node)
8868 /* We found an id-expression, but it was something that we
8869 should not have found. This is an error, not something
8870 we can recover from, so note that we found an
8871 id-expression and we'll recover as gracefully as
8873 id_expression_or_member_access_p = true;
8877 && expr != error_mark_node
8878 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8879 /* We have an id-expression. */
8880 id_expression_or_member_access_p = true;
8883 if (!id_expression_or_member_access_p)
8885 /* Abort the id-expression parse. */
8886 cp_parser_abort_tentative_parse (parser);
8888 /* Parsing tentatively, again. */
8889 cp_parser_parse_tentatively (parser);
8891 /* Parse a class member access. */
8892 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8894 /*member_access_only_p=*/true, NULL);
8897 && expr != error_mark_node
8898 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8899 /* We have an id-expression. */
8900 id_expression_or_member_access_p = true;
8903 if (id_expression_or_member_access_p)
8904 /* We have parsed the complete id-expression or member access. */
8905 cp_parser_parse_definitely (parser);
8908 /* Abort our attempt to parse an id-expression or member access
8910 cp_parser_abort_tentative_parse (parser);
8912 /* Parse a full expression. */
8913 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8916 /* Go back to evaluating expressions. */
8919 /* Restore the old message and the integral constant expression
8921 parser->type_definition_forbidden_message = saved_message;
8922 parser->integral_constant_expression_p
8923 = saved_integral_constant_expression_p;
8924 parser->non_integral_constant_expression_p
8925 = saved_non_integral_constant_expression_p;
8927 if (expr == error_mark_node)
8929 /* Skip everything up to the closing `)'. */
8930 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8931 /*consume_paren=*/true);
8932 return error_mark_node;
8935 /* Parse to the closing `)'. */
8936 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8938 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8939 /*consume_paren=*/true);
8940 return error_mark_node;
8943 return finish_decltype_type (expr, id_expression_or_member_access_p);
8946 /* Special member functions [gram.special] */
8948 /* Parse a conversion-function-id.
8950 conversion-function-id:
8951 operator conversion-type-id
8953 Returns an IDENTIFIER_NODE representing the operator. */
8956 cp_parser_conversion_function_id (cp_parser* parser)
8960 tree saved_qualifying_scope;
8961 tree saved_object_scope;
8962 tree pushed_scope = NULL_TREE;
8964 /* Look for the `operator' token. */
8965 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8966 return error_mark_node;
8967 /* When we parse the conversion-type-id, the current scope will be
8968 reset. However, we need that information in able to look up the
8969 conversion function later, so we save it here. */
8970 saved_scope = parser->scope;
8971 saved_qualifying_scope = parser->qualifying_scope;
8972 saved_object_scope = parser->object_scope;
8973 /* We must enter the scope of the class so that the names of
8974 entities declared within the class are available in the
8975 conversion-type-id. For example, consider:
8982 S::operator I() { ... }
8984 In order to see that `I' is a type-name in the definition, we
8985 must be in the scope of `S'. */
8987 pushed_scope = push_scope (saved_scope);
8988 /* Parse the conversion-type-id. */
8989 type = cp_parser_conversion_type_id (parser);
8990 /* Leave the scope of the class, if any. */
8992 pop_scope (pushed_scope);
8993 /* Restore the saved scope. */
8994 parser->scope = saved_scope;
8995 parser->qualifying_scope = saved_qualifying_scope;
8996 parser->object_scope = saved_object_scope;
8997 /* If the TYPE is invalid, indicate failure. */
8998 if (type == error_mark_node)
8999 return error_mark_node;
9000 return mangle_conv_op_name_for_type (type);
9003 /* Parse a conversion-type-id:
9006 type-specifier-seq conversion-declarator [opt]
9008 Returns the TYPE specified. */
9011 cp_parser_conversion_type_id (cp_parser* parser)
9014 cp_decl_specifier_seq type_specifiers;
9015 cp_declarator *declarator;
9016 tree type_specified;
9018 /* Parse the attributes. */
9019 attributes = cp_parser_attributes_opt (parser);
9020 /* Parse the type-specifiers. */
9021 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9023 /* If that didn't work, stop. */
9024 if (type_specifiers.type == error_mark_node)
9025 return error_mark_node;
9026 /* Parse the conversion-declarator. */
9027 declarator = cp_parser_conversion_declarator_opt (parser);
9029 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9030 /*initialized=*/0, &attributes);
9032 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9034 /* Don't give this error when parsing tentatively. This happens to
9035 work because we always parse this definitively once. */
9036 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9037 && type_uses_auto (type_specified))
9039 error ("invalid use of %<auto%> in conversion operator");
9040 return error_mark_node;
9043 return type_specified;
9046 /* Parse an (optional) conversion-declarator.
9048 conversion-declarator:
9049 ptr-operator conversion-declarator [opt]
9053 static cp_declarator *
9054 cp_parser_conversion_declarator_opt (cp_parser* parser)
9056 enum tree_code code;
9058 cp_cv_quals cv_quals;
9060 /* We don't know if there's a ptr-operator next, or not. */
9061 cp_parser_parse_tentatively (parser);
9062 /* Try the ptr-operator. */
9063 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9064 /* If it worked, look for more conversion-declarators. */
9065 if (cp_parser_parse_definitely (parser))
9067 cp_declarator *declarator;
9069 /* Parse another optional declarator. */
9070 declarator = cp_parser_conversion_declarator_opt (parser);
9072 return cp_parser_make_indirect_declarator
9073 (code, class_type, cv_quals, declarator);
9079 /* Parse an (optional) ctor-initializer.
9082 : mem-initializer-list
9084 Returns TRUE iff the ctor-initializer was actually present. */
9087 cp_parser_ctor_initializer_opt (cp_parser* parser)
9089 /* If the next token is not a `:', then there is no
9090 ctor-initializer. */
9091 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9093 /* Do default initialization of any bases and members. */
9094 if (DECL_CONSTRUCTOR_P (current_function_decl))
9095 finish_mem_initializers (NULL_TREE);
9100 /* Consume the `:' token. */
9101 cp_lexer_consume_token (parser->lexer);
9102 /* And the mem-initializer-list. */
9103 cp_parser_mem_initializer_list (parser);
9108 /* Parse a mem-initializer-list.
9110 mem-initializer-list:
9111 mem-initializer ... [opt]
9112 mem-initializer ... [opt] , mem-initializer-list */
9115 cp_parser_mem_initializer_list (cp_parser* parser)
9117 tree mem_initializer_list = NULL_TREE;
9118 cp_token *token = cp_lexer_peek_token (parser->lexer);
9120 /* Let the semantic analysis code know that we are starting the
9121 mem-initializer-list. */
9122 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9123 error ("%Honly constructors take base initializers",
9126 /* Loop through the list. */
9129 tree mem_initializer;
9131 token = cp_lexer_peek_token (parser->lexer);
9132 /* Parse the mem-initializer. */
9133 mem_initializer = cp_parser_mem_initializer (parser);
9134 /* If the next token is a `...', we're expanding member initializers. */
9135 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9137 /* Consume the `...'. */
9138 cp_lexer_consume_token (parser->lexer);
9140 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9141 can be expanded but members cannot. */
9142 if (mem_initializer != error_mark_node
9143 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9145 error ("%Hcannot expand initializer for member %<%D%>",
9146 &token->location, TREE_PURPOSE (mem_initializer));
9147 mem_initializer = error_mark_node;
9150 /* Construct the pack expansion type. */
9151 if (mem_initializer != error_mark_node)
9152 mem_initializer = make_pack_expansion (mem_initializer);
9154 /* Add it to the list, unless it was erroneous. */
9155 if (mem_initializer != error_mark_node)
9157 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9158 mem_initializer_list = mem_initializer;
9160 /* If the next token is not a `,', we're done. */
9161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9163 /* Consume the `,' token. */
9164 cp_lexer_consume_token (parser->lexer);
9167 /* Perform semantic analysis. */
9168 if (DECL_CONSTRUCTOR_P (current_function_decl))
9169 finish_mem_initializers (mem_initializer_list);
9172 /* Parse a mem-initializer.
9175 mem-initializer-id ( expression-list [opt] )
9176 mem-initializer-id braced-init-list
9181 ( expression-list [opt] )
9183 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9184 class) or FIELD_DECL (for a non-static data member) to initialize;
9185 the TREE_VALUE is the expression-list. An empty initialization
9186 list is represented by void_list_node. */
9189 cp_parser_mem_initializer (cp_parser* parser)
9191 tree mem_initializer_id;
9192 tree expression_list;
9194 cp_token *token = cp_lexer_peek_token (parser->lexer);
9196 /* Find out what is being initialized. */
9197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9199 permerror (token->location,
9200 "anachronistic old-style base class initializer");
9201 mem_initializer_id = NULL_TREE;
9205 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9206 if (mem_initializer_id == error_mark_node)
9207 return mem_initializer_id;
9209 member = expand_member_init (mem_initializer_id);
9210 if (member && !DECL_P (member))
9211 in_base_initializer = 1;
9213 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9215 bool expr_non_constant_p;
9216 maybe_warn_cpp0x ("extended initializer lists");
9217 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9218 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9219 expression_list = build_tree_list (NULL_TREE, expression_list);
9223 = cp_parser_parenthesized_expression_list (parser, false,
9225 /*allow_expansion_p=*/true,
9226 /*non_constant_p=*/NULL);
9227 if (expression_list == error_mark_node)
9228 return error_mark_node;
9229 if (!expression_list)
9230 expression_list = void_type_node;
9232 in_base_initializer = 0;
9234 return member ? build_tree_list (member, expression_list) : error_mark_node;
9237 /* Parse a mem-initializer-id.
9240 :: [opt] nested-name-specifier [opt] class-name
9243 Returns a TYPE indicating the class to be initializer for the first
9244 production. Returns an IDENTIFIER_NODE indicating the data member
9245 to be initialized for the second production. */
9248 cp_parser_mem_initializer_id (cp_parser* parser)
9250 bool global_scope_p;
9251 bool nested_name_specifier_p;
9252 bool template_p = false;
9255 cp_token *token = cp_lexer_peek_token (parser->lexer);
9257 /* `typename' is not allowed in this context ([temp.res]). */
9258 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9260 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9261 "member initializer is implicitly a type)",
9263 cp_lexer_consume_token (parser->lexer);
9265 /* Look for the optional `::' operator. */
9267 = (cp_parser_global_scope_opt (parser,
9268 /*current_scope_valid_p=*/false)
9270 /* Look for the optional nested-name-specifier. The simplest way to
9275 The keyword `typename' is not permitted in a base-specifier or
9276 mem-initializer; in these contexts a qualified name that
9277 depends on a template-parameter is implicitly assumed to be a
9280 is to assume that we have seen the `typename' keyword at this
9282 nested_name_specifier_p
9283 = (cp_parser_nested_name_specifier_opt (parser,
9284 /*typename_keyword_p=*/true,
9285 /*check_dependency_p=*/true,
9287 /*is_declaration=*/true)
9289 if (nested_name_specifier_p)
9290 template_p = cp_parser_optional_template_keyword (parser);
9291 /* If there is a `::' operator or a nested-name-specifier, then we
9292 are definitely looking for a class-name. */
9293 if (global_scope_p || nested_name_specifier_p)
9294 return cp_parser_class_name (parser,
9295 /*typename_keyword_p=*/true,
9296 /*template_keyword_p=*/template_p,
9298 /*check_dependency_p=*/true,
9299 /*class_head_p=*/false,
9300 /*is_declaration=*/true);
9301 /* Otherwise, we could also be looking for an ordinary identifier. */
9302 cp_parser_parse_tentatively (parser);
9303 /* Try a class-name. */
9304 id = cp_parser_class_name (parser,
9305 /*typename_keyword_p=*/true,
9306 /*template_keyword_p=*/false,
9308 /*check_dependency_p=*/true,
9309 /*class_head_p=*/false,
9310 /*is_declaration=*/true);
9311 /* If we found one, we're done. */
9312 if (cp_parser_parse_definitely (parser))
9314 /* Otherwise, look for an ordinary identifier. */
9315 return cp_parser_identifier (parser);
9318 /* Overloading [gram.over] */
9320 /* Parse an operator-function-id.
9322 operator-function-id:
9325 Returns an IDENTIFIER_NODE for the operator which is a
9326 human-readable spelling of the identifier, e.g., `operator +'. */
9329 cp_parser_operator_function_id (cp_parser* parser)
9331 /* Look for the `operator' keyword. */
9332 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9333 return error_mark_node;
9334 /* And then the name of the operator itself. */
9335 return cp_parser_operator (parser);
9338 /* Parse an operator.
9341 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9342 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9343 || ++ -- , ->* -> () []
9350 Returns an IDENTIFIER_NODE for the operator which is a
9351 human-readable spelling of the identifier, e.g., `operator +'. */
9354 cp_parser_operator (cp_parser* parser)
9356 tree id = NULL_TREE;
9359 /* Peek at the next token. */
9360 token = cp_lexer_peek_token (parser->lexer);
9361 /* Figure out which operator we have. */
9362 switch (token->type)
9368 /* The keyword should be either `new' or `delete'. */
9369 if (token->keyword == RID_NEW)
9371 else if (token->keyword == RID_DELETE)
9376 /* Consume the `new' or `delete' token. */
9377 cp_lexer_consume_token (parser->lexer);
9379 /* Peek at the next token. */
9380 token = cp_lexer_peek_token (parser->lexer);
9381 /* If it's a `[' token then this is the array variant of the
9383 if (token->type == CPP_OPEN_SQUARE)
9385 /* Consume the `[' token. */
9386 cp_lexer_consume_token (parser->lexer);
9387 /* Look for the `]' token. */
9388 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9389 id = ansi_opname (op == NEW_EXPR
9390 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9392 /* Otherwise, we have the non-array variant. */
9394 id = ansi_opname (op);
9400 id = ansi_opname (PLUS_EXPR);
9404 id = ansi_opname (MINUS_EXPR);
9408 id = ansi_opname (MULT_EXPR);
9412 id = ansi_opname (TRUNC_DIV_EXPR);
9416 id = ansi_opname (TRUNC_MOD_EXPR);
9420 id = ansi_opname (BIT_XOR_EXPR);
9424 id = ansi_opname (BIT_AND_EXPR);
9428 id = ansi_opname (BIT_IOR_EXPR);
9432 id = ansi_opname (BIT_NOT_EXPR);
9436 id = ansi_opname (TRUTH_NOT_EXPR);
9440 id = ansi_assopname (NOP_EXPR);
9444 id = ansi_opname (LT_EXPR);
9448 id = ansi_opname (GT_EXPR);
9452 id = ansi_assopname (PLUS_EXPR);
9456 id = ansi_assopname (MINUS_EXPR);
9460 id = ansi_assopname (MULT_EXPR);
9464 id = ansi_assopname (TRUNC_DIV_EXPR);
9468 id = ansi_assopname (TRUNC_MOD_EXPR);
9472 id = ansi_assopname (BIT_XOR_EXPR);
9476 id = ansi_assopname (BIT_AND_EXPR);
9480 id = ansi_assopname (BIT_IOR_EXPR);
9484 id = ansi_opname (LSHIFT_EXPR);
9488 id = ansi_opname (RSHIFT_EXPR);
9492 id = ansi_assopname (LSHIFT_EXPR);
9496 id = ansi_assopname (RSHIFT_EXPR);
9500 id = ansi_opname (EQ_EXPR);
9504 id = ansi_opname (NE_EXPR);
9508 id = ansi_opname (LE_EXPR);
9511 case CPP_GREATER_EQ:
9512 id = ansi_opname (GE_EXPR);
9516 id = ansi_opname (TRUTH_ANDIF_EXPR);
9520 id = ansi_opname (TRUTH_ORIF_EXPR);
9524 id = ansi_opname (POSTINCREMENT_EXPR);
9527 case CPP_MINUS_MINUS:
9528 id = ansi_opname (PREDECREMENT_EXPR);
9532 id = ansi_opname (COMPOUND_EXPR);
9535 case CPP_DEREF_STAR:
9536 id = ansi_opname (MEMBER_REF);
9540 id = ansi_opname (COMPONENT_REF);
9543 case CPP_OPEN_PAREN:
9544 /* Consume the `('. */
9545 cp_lexer_consume_token (parser->lexer);
9546 /* Look for the matching `)'. */
9547 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9548 return ansi_opname (CALL_EXPR);
9550 case CPP_OPEN_SQUARE:
9551 /* Consume the `['. */
9552 cp_lexer_consume_token (parser->lexer);
9553 /* Look for the matching `]'. */
9554 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9555 return ansi_opname (ARRAY_REF);
9558 /* Anything else is an error. */
9562 /* If we have selected an identifier, we need to consume the
9565 cp_lexer_consume_token (parser->lexer);
9566 /* Otherwise, no valid operator name was present. */
9569 cp_parser_error (parser, "expected operator");
9570 id = error_mark_node;
9576 /* Parse a template-declaration.
9578 template-declaration:
9579 export [opt] template < template-parameter-list > declaration
9581 If MEMBER_P is TRUE, this template-declaration occurs within a
9584 The grammar rule given by the standard isn't correct. What
9587 template-declaration:
9588 export [opt] template-parameter-list-seq
9589 decl-specifier-seq [opt] init-declarator [opt] ;
9590 export [opt] template-parameter-list-seq
9593 template-parameter-list-seq:
9594 template-parameter-list-seq [opt]
9595 template < template-parameter-list > */
9598 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9600 /* Check for `export'. */
9601 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9603 /* Consume the `export' token. */
9604 cp_lexer_consume_token (parser->lexer);
9605 /* Warn that we do not support `export'. */
9606 warning (0, "keyword %<export%> not implemented, and will be ignored");
9609 cp_parser_template_declaration_after_export (parser, member_p);
9612 /* Parse a template-parameter-list.
9614 template-parameter-list:
9616 template-parameter-list , template-parameter
9618 Returns a TREE_LIST. Each node represents a template parameter.
9619 The nodes are connected via their TREE_CHAINs. */
9622 cp_parser_template_parameter_list (cp_parser* parser)
9624 tree parameter_list = NULL_TREE;
9626 begin_template_parm_list ();
9631 bool is_parameter_pack;
9633 /* Parse the template-parameter. */
9634 parameter = cp_parser_template_parameter (parser,
9636 &is_parameter_pack);
9637 /* Add it to the list. */
9638 if (parameter != error_mark_node)
9639 parameter_list = process_template_parm (parameter_list,
9645 tree err_parm = build_tree_list (parameter, parameter);
9646 TREE_VALUE (err_parm) = error_mark_node;
9647 parameter_list = chainon (parameter_list, err_parm);
9650 /* If the next token is not a `,', we're done. */
9651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9653 /* Otherwise, consume the `,' token. */
9654 cp_lexer_consume_token (parser->lexer);
9657 return end_template_parm_list (parameter_list);
9660 /* Parse a template-parameter.
9664 parameter-declaration
9666 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9667 the parameter. The TREE_PURPOSE is the default value, if any.
9668 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9669 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9670 set to true iff this parameter is a parameter pack. */
9673 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9674 bool *is_parameter_pack)
9677 cp_parameter_declarator *parameter_declarator;
9678 cp_declarator *id_declarator;
9681 /* Assume it is a type parameter or a template parameter. */
9682 *is_non_type = false;
9683 /* Assume it not a parameter pack. */
9684 *is_parameter_pack = false;
9685 /* Peek at the next token. */
9686 token = cp_lexer_peek_token (parser->lexer);
9687 /* If it is `class' or `template', we have a type-parameter. */
9688 if (token->keyword == RID_TEMPLATE)
9689 return cp_parser_type_parameter (parser, is_parameter_pack);
9690 /* If it is `class' or `typename' we do not know yet whether it is a
9691 type parameter or a non-type parameter. Consider:
9693 template <typename T, typename T::X X> ...
9697 template <class C, class D*> ...
9699 Here, the first parameter is a type parameter, and the second is
9700 a non-type parameter. We can tell by looking at the token after
9701 the identifier -- if it is a `,', `=', or `>' then we have a type
9703 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9705 /* Peek at the token after `class' or `typename'. */
9706 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9707 /* If it's an ellipsis, we have a template type parameter
9709 if (token->type == CPP_ELLIPSIS)
9710 return cp_parser_type_parameter (parser, is_parameter_pack);
9711 /* If it's an identifier, skip it. */
9712 if (token->type == CPP_NAME)
9713 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9714 /* Now, see if the token looks like the end of a template
9716 if (token->type == CPP_COMMA
9717 || token->type == CPP_EQ
9718 || token->type == CPP_GREATER)
9719 return cp_parser_type_parameter (parser, is_parameter_pack);
9722 /* Otherwise, it is a non-type parameter.
9726 When parsing a default template-argument for a non-type
9727 template-parameter, the first non-nested `>' is taken as the end
9728 of the template parameter-list rather than a greater-than
9730 *is_non_type = true;
9731 parameter_declarator
9732 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9733 /*parenthesized_p=*/NULL);
9735 /* If the parameter declaration is marked as a parameter pack, set
9736 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9737 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9739 if (parameter_declarator
9740 && parameter_declarator->declarator
9741 && parameter_declarator->declarator->parameter_pack_p)
9743 *is_parameter_pack = true;
9744 parameter_declarator->declarator->parameter_pack_p = false;
9747 /* If the next token is an ellipsis, and we don't already have it
9748 marked as a parameter pack, then we have a parameter pack (that
9749 has no declarator). */
9750 if (!*is_parameter_pack
9751 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9752 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9754 /* Consume the `...'. */
9755 cp_lexer_consume_token (parser->lexer);
9756 maybe_warn_variadic_templates ();
9758 *is_parameter_pack = true;
9760 /* We might end up with a pack expansion as the type of the non-type
9761 template parameter, in which case this is a non-type template
9763 else if (parameter_declarator
9764 && parameter_declarator->decl_specifiers.type
9765 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9767 *is_parameter_pack = true;
9768 parameter_declarator->decl_specifiers.type =
9769 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9772 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9774 /* Parameter packs cannot have default arguments. However, a
9775 user may try to do so, so we'll parse them and give an
9776 appropriate diagnostic here. */
9778 /* Consume the `='. */
9779 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9780 cp_lexer_consume_token (parser->lexer);
9782 /* Find the name of the parameter pack. */
9783 id_declarator = parameter_declarator->declarator;
9784 while (id_declarator && id_declarator->kind != cdk_id)
9785 id_declarator = id_declarator->declarator;
9787 if (id_declarator && id_declarator->kind == cdk_id)
9788 error ("%Htemplate parameter pack %qD cannot have a default argument",
9789 &start_token->location, id_declarator->u.id.unqualified_name);
9791 error ("%Htemplate parameter pack cannot have a default argument",
9792 &start_token->location);
9794 /* Parse the default argument, but throw away the result. */
9795 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9798 parm = grokdeclarator (parameter_declarator->declarator,
9799 ¶meter_declarator->decl_specifiers,
9800 PARM, /*initialized=*/0,
9802 if (parm == error_mark_node)
9803 return error_mark_node;
9805 return build_tree_list (parameter_declarator->default_argument, parm);
9808 /* Parse a type-parameter.
9811 class identifier [opt]
9812 class identifier [opt] = type-id
9813 typename identifier [opt]
9814 typename identifier [opt] = type-id
9815 template < template-parameter-list > class identifier [opt]
9816 template < template-parameter-list > class identifier [opt]
9819 GNU Extension (variadic templates):
9822 class ... identifier [opt]
9823 typename ... identifier [opt]
9825 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9826 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9827 the declaration of the parameter.
9829 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9832 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9837 /* Look for a keyword to tell us what kind of parameter this is. */
9838 token = cp_parser_require (parser, CPP_KEYWORD,
9839 "%<class%>, %<typename%>, or %<template%>");
9841 return error_mark_node;
9843 switch (token->keyword)
9849 tree default_argument;
9851 /* If the next token is an ellipsis, we have a template
9853 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9855 /* Consume the `...' token. */
9856 cp_lexer_consume_token (parser->lexer);
9857 maybe_warn_variadic_templates ();
9859 *is_parameter_pack = true;
9862 /* If the next token is an identifier, then it names the
9864 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9865 identifier = cp_parser_identifier (parser);
9867 identifier = NULL_TREE;
9869 /* Create the parameter. */
9870 parameter = finish_template_type_parm (class_type_node, identifier);
9872 /* If the next token is an `=', we have a default argument. */
9873 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9875 /* Consume the `=' token. */
9876 cp_lexer_consume_token (parser->lexer);
9877 /* Parse the default-argument. */
9878 push_deferring_access_checks (dk_no_deferred);
9879 default_argument = cp_parser_type_id (parser);
9881 /* Template parameter packs cannot have default
9883 if (*is_parameter_pack)
9886 error ("%Htemplate parameter pack %qD cannot have a "
9887 "default argument", &token->location, identifier);
9889 error ("%Htemplate parameter packs cannot have "
9890 "default arguments", &token->location);
9891 default_argument = NULL_TREE;
9893 pop_deferring_access_checks ();
9896 default_argument = NULL_TREE;
9898 /* Create the combined representation of the parameter and the
9899 default argument. */
9900 parameter = build_tree_list (default_argument, parameter);
9906 tree parameter_list;
9908 tree default_argument;
9910 /* Look for the `<'. */
9911 cp_parser_require (parser, CPP_LESS, "%<<%>");
9912 /* Parse the template-parameter-list. */
9913 parameter_list = cp_parser_template_parameter_list (parser);
9914 /* Look for the `>'. */
9915 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9916 /* Look for the `class' keyword. */
9917 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9918 /* If the next token is an ellipsis, we have a template
9920 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9922 /* Consume the `...' token. */
9923 cp_lexer_consume_token (parser->lexer);
9924 maybe_warn_variadic_templates ();
9926 *is_parameter_pack = true;
9928 /* If the next token is an `=', then there is a
9929 default-argument. If the next token is a `>', we are at
9930 the end of the parameter-list. If the next token is a `,',
9931 then we are at the end of this parameter. */
9932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9933 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9934 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9936 identifier = cp_parser_identifier (parser);
9937 /* Treat invalid names as if the parameter were nameless. */
9938 if (identifier == error_mark_node)
9939 identifier = NULL_TREE;
9942 identifier = NULL_TREE;
9944 /* Create the template parameter. */
9945 parameter = finish_template_template_parm (class_type_node,
9948 /* If the next token is an `=', then there is a
9949 default-argument. */
9950 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9954 /* Consume the `='. */
9955 cp_lexer_consume_token (parser->lexer);
9956 /* Parse the id-expression. */
9957 push_deferring_access_checks (dk_no_deferred);
9958 /* save token before parsing the id-expression, for error
9960 token = cp_lexer_peek_token (parser->lexer);
9962 = cp_parser_id_expression (parser,
9963 /*template_keyword_p=*/false,
9964 /*check_dependency_p=*/true,
9965 /*template_p=*/&is_template,
9966 /*declarator_p=*/false,
9967 /*optional_p=*/false);
9968 if (TREE_CODE (default_argument) == TYPE_DECL)
9969 /* If the id-expression was a template-id that refers to
9970 a template-class, we already have the declaration here,
9971 so no further lookup is needed. */
9974 /* Look up the name. */
9976 = cp_parser_lookup_name (parser, default_argument,
9978 /*is_template=*/is_template,
9979 /*is_namespace=*/false,
9980 /*check_dependency=*/true,
9981 /*ambiguous_decls=*/NULL,
9983 /* See if the default argument is valid. */
9985 = check_template_template_default_arg (default_argument);
9987 /* Template parameter packs cannot have default
9989 if (*is_parameter_pack)
9992 error ("%Htemplate parameter pack %qD cannot "
9993 "have a default argument",
9994 &token->location, identifier);
9996 error ("%Htemplate parameter packs cannot "
9997 "have default arguments",
9999 default_argument = NULL_TREE;
10001 pop_deferring_access_checks ();
10004 default_argument = NULL_TREE;
10006 /* Create the combined representation of the parameter and the
10007 default argument. */
10008 parameter = build_tree_list (default_argument, parameter);
10013 gcc_unreachable ();
10020 /* Parse a template-id.
10023 template-name < template-argument-list [opt] >
10025 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10026 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10027 returned. Otherwise, if the template-name names a function, or set
10028 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10029 names a class, returns a TYPE_DECL for the specialization.
10031 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10032 uninstantiated templates. */
10035 cp_parser_template_id (cp_parser *parser,
10036 bool template_keyword_p,
10037 bool check_dependency_p,
10038 bool is_declaration)
10044 cp_token_position start_of_id = 0;
10045 deferred_access_check *chk;
10046 VEC (deferred_access_check,gc) *access_check;
10047 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10048 bool is_identifier;
10050 /* If the next token corresponds to a template-id, there is no need
10052 next_token = cp_lexer_peek_token (parser->lexer);
10053 if (next_token->type == CPP_TEMPLATE_ID)
10055 struct tree_check *check_value;
10057 /* Get the stored value. */
10058 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10059 /* Perform any access checks that were deferred. */
10060 access_check = check_value->checks;
10064 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10067 perform_or_defer_access_check (chk->binfo,
10072 /* Return the stored value. */
10073 return check_value->value;
10076 /* Avoid performing name lookup if there is no possibility of
10077 finding a template-id. */
10078 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10079 || (next_token->type == CPP_NAME
10080 && !cp_parser_nth_token_starts_template_argument_list_p
10083 cp_parser_error (parser, "expected template-id");
10084 return error_mark_node;
10087 /* Remember where the template-id starts. */
10088 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10089 start_of_id = cp_lexer_token_position (parser->lexer, false);
10091 push_deferring_access_checks (dk_deferred);
10093 /* Parse the template-name. */
10094 is_identifier = false;
10095 token = cp_lexer_peek_token (parser->lexer);
10096 templ = cp_parser_template_name (parser, template_keyword_p,
10097 check_dependency_p,
10100 if (templ == error_mark_node || is_identifier)
10102 pop_deferring_access_checks ();
10106 /* If we find the sequence `[:' after a template-name, it's probably
10107 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10108 parse correctly the argument list. */
10109 next_token = cp_lexer_peek_token (parser->lexer);
10110 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10111 if (next_token->type == CPP_OPEN_SQUARE
10112 && next_token->flags & DIGRAPH
10113 && next_token_2->type == CPP_COLON
10114 && !(next_token_2->flags & PREV_WHITE))
10116 cp_parser_parse_tentatively (parser);
10117 /* Change `:' into `::'. */
10118 next_token_2->type = CPP_SCOPE;
10119 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10121 cp_lexer_consume_token (parser->lexer);
10123 /* Parse the arguments. */
10124 arguments = cp_parser_enclosed_template_argument_list (parser);
10125 if (!cp_parser_parse_definitely (parser))
10127 /* If we couldn't parse an argument list, then we revert our changes
10128 and return simply an error. Maybe this is not a template-id
10130 next_token_2->type = CPP_COLON;
10131 cp_parser_error (parser, "expected %<<%>");
10132 pop_deferring_access_checks ();
10133 return error_mark_node;
10135 /* Otherwise, emit an error about the invalid digraph, but continue
10136 parsing because we got our argument list. */
10137 if (permerror (next_token->location,
10138 "%<<::%> cannot begin a template-argument list"))
10140 static bool hint = false;
10141 inform (next_token->location,
10142 "%<<:%> is an alternate spelling for %<[%>."
10143 " Insert whitespace between %<<%> and %<::%>");
10144 if (!hint && !flag_permissive)
10146 inform (next_token->location, "(if you use %<-fpermissive%>"
10147 " G++ will accept your code)");
10154 /* Look for the `<' that starts the template-argument-list. */
10155 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10157 pop_deferring_access_checks ();
10158 return error_mark_node;
10160 /* Parse the arguments. */
10161 arguments = cp_parser_enclosed_template_argument_list (parser);
10164 /* Build a representation of the specialization. */
10165 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10166 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10167 else if (DECL_CLASS_TEMPLATE_P (templ)
10168 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10170 bool entering_scope;
10171 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10172 template (rather than some instantiation thereof) only if
10173 is not nested within some other construct. For example, in
10174 "template <typename T> void f(T) { A<T>::", A<T> is just an
10175 instantiation of A. */
10176 entering_scope = (template_parm_scope_p ()
10177 && cp_lexer_next_token_is (parser->lexer,
10180 = finish_template_type (templ, arguments, entering_scope);
10184 /* If it's not a class-template or a template-template, it should be
10185 a function-template. */
10186 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10187 || TREE_CODE (templ) == OVERLOAD
10188 || BASELINK_P (templ)));
10190 template_id = lookup_template_function (templ, arguments);
10193 /* If parsing tentatively, replace the sequence of tokens that makes
10194 up the template-id with a CPP_TEMPLATE_ID token. That way,
10195 should we re-parse the token stream, we will not have to repeat
10196 the effort required to do the parse, nor will we issue duplicate
10197 error messages about problems during instantiation of the
10201 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10203 /* Reset the contents of the START_OF_ID token. */
10204 token->type = CPP_TEMPLATE_ID;
10205 /* Retrieve any deferred checks. Do not pop this access checks yet
10206 so the memory will not be reclaimed during token replacing below. */
10207 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10208 token->u.tree_check_value->value = template_id;
10209 token->u.tree_check_value->checks = get_deferred_access_checks ();
10210 token->keyword = RID_MAX;
10212 /* Purge all subsequent tokens. */
10213 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10215 /* ??? Can we actually assume that, if template_id ==
10216 error_mark_node, we will have issued a diagnostic to the
10217 user, as opposed to simply marking the tentative parse as
10219 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10220 error ("%Hparse error in template argument list",
10224 pop_deferring_access_checks ();
10225 return template_id;
10228 /* Parse a template-name.
10233 The standard should actually say:
10237 operator-function-id
10239 A defect report has been filed about this issue.
10241 A conversion-function-id cannot be a template name because they cannot
10242 be part of a template-id. In fact, looking at this code:
10244 a.operator K<int>()
10246 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10247 It is impossible to call a templated conversion-function-id with an
10248 explicit argument list, since the only allowed template parameter is
10249 the type to which it is converting.
10251 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10252 `template' keyword, in a construction like:
10256 In that case `f' is taken to be a template-name, even though there
10257 is no way of knowing for sure.
10259 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10260 name refers to a set of overloaded functions, at least one of which
10261 is a template, or an IDENTIFIER_NODE with the name of the template,
10262 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10263 names are looked up inside uninstantiated templates. */
10266 cp_parser_template_name (cp_parser* parser,
10267 bool template_keyword_p,
10268 bool check_dependency_p,
10269 bool is_declaration,
10270 bool *is_identifier)
10275 cp_token *token = cp_lexer_peek_token (parser->lexer);
10277 /* If the next token is `operator', then we have either an
10278 operator-function-id or a conversion-function-id. */
10279 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10281 /* We don't know whether we're looking at an
10282 operator-function-id or a conversion-function-id. */
10283 cp_parser_parse_tentatively (parser);
10284 /* Try an operator-function-id. */
10285 identifier = cp_parser_operator_function_id (parser);
10286 /* If that didn't work, try a conversion-function-id. */
10287 if (!cp_parser_parse_definitely (parser))
10289 cp_parser_error (parser, "expected template-name");
10290 return error_mark_node;
10293 /* Look for the identifier. */
10295 identifier = cp_parser_identifier (parser);
10297 /* If we didn't find an identifier, we don't have a template-id. */
10298 if (identifier == error_mark_node)
10299 return error_mark_node;
10301 /* If the name immediately followed the `template' keyword, then it
10302 is a template-name. However, if the next token is not `<', then
10303 we do not treat it as a template-name, since it is not being used
10304 as part of a template-id. This enables us to handle constructs
10307 template <typename T> struct S { S(); };
10308 template <typename T> S<T>::S();
10310 correctly. We would treat `S' as a template -- if it were `S<T>'
10311 -- but we do not if there is no `<'. */
10313 if (processing_template_decl
10314 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10316 /* In a declaration, in a dependent context, we pretend that the
10317 "template" keyword was present in order to improve error
10318 recovery. For example, given:
10320 template <typename T> void f(T::X<int>);
10322 we want to treat "X<int>" as a template-id. */
10324 && !template_keyword_p
10325 && parser->scope && TYPE_P (parser->scope)
10326 && check_dependency_p
10327 && dependent_scope_p (parser->scope)
10328 /* Do not do this for dtors (or ctors), since they never
10329 need the template keyword before their name. */
10330 && !constructor_name_p (identifier, parser->scope))
10332 cp_token_position start = 0;
10334 /* Explain what went wrong. */
10335 error ("%Hnon-template %qD used as template",
10336 &token->location, identifier);
10337 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10338 parser->scope, identifier);
10339 /* If parsing tentatively, find the location of the "<" token. */
10340 if (cp_parser_simulate_error (parser))
10341 start = cp_lexer_token_position (parser->lexer, true);
10342 /* Parse the template arguments so that we can issue error
10343 messages about them. */
10344 cp_lexer_consume_token (parser->lexer);
10345 cp_parser_enclosed_template_argument_list (parser);
10346 /* Skip tokens until we find a good place from which to
10347 continue parsing. */
10348 cp_parser_skip_to_closing_parenthesis (parser,
10349 /*recovering=*/true,
10351 /*consume_paren=*/false);
10352 /* If parsing tentatively, permanently remove the
10353 template argument list. That will prevent duplicate
10354 error messages from being issued about the missing
10355 "template" keyword. */
10357 cp_lexer_purge_tokens_after (parser->lexer, start);
10359 *is_identifier = true;
10363 /* If the "template" keyword is present, then there is generally
10364 no point in doing name-lookup, so we just return IDENTIFIER.
10365 But, if the qualifying scope is non-dependent then we can
10366 (and must) do name-lookup normally. */
10367 if (template_keyword_p
10369 || (TYPE_P (parser->scope)
10370 && dependent_type_p (parser->scope))))
10374 /* Look up the name. */
10375 decl = cp_parser_lookup_name (parser, identifier,
10377 /*is_template=*/false,
10378 /*is_namespace=*/false,
10379 check_dependency_p,
10380 /*ambiguous_decls=*/NULL,
10382 decl = maybe_get_template_decl_from_type_decl (decl);
10384 /* If DECL is a template, then the name was a template-name. */
10385 if (TREE_CODE (decl) == TEMPLATE_DECL)
10389 tree fn = NULL_TREE;
10391 /* The standard does not explicitly indicate whether a name that
10392 names a set of overloaded declarations, some of which are
10393 templates, is a template-name. However, such a name should
10394 be a template-name; otherwise, there is no way to form a
10395 template-id for the overloaded templates. */
10396 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10397 if (TREE_CODE (fns) == OVERLOAD)
10398 for (fn = fns; fn; fn = OVL_NEXT (fn))
10399 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10404 /* The name does not name a template. */
10405 cp_parser_error (parser, "expected template-name");
10406 return error_mark_node;
10410 /* If DECL is dependent, and refers to a function, then just return
10411 its name; we will look it up again during template instantiation. */
10412 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10414 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10415 if (TYPE_P (scope) && dependent_type_p (scope))
10422 /* Parse a template-argument-list.
10424 template-argument-list:
10425 template-argument ... [opt]
10426 template-argument-list , template-argument ... [opt]
10428 Returns a TREE_VEC containing the arguments. */
10431 cp_parser_template_argument_list (cp_parser* parser)
10433 tree fixed_args[10];
10434 unsigned n_args = 0;
10435 unsigned alloced = 10;
10436 tree *arg_ary = fixed_args;
10438 bool saved_in_template_argument_list_p;
10440 bool saved_non_ice_p;
10442 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10443 parser->in_template_argument_list_p = true;
10444 /* Even if the template-id appears in an integral
10445 constant-expression, the contents of the argument list do
10447 saved_ice_p = parser->integral_constant_expression_p;
10448 parser->integral_constant_expression_p = false;
10449 saved_non_ice_p = parser->non_integral_constant_expression_p;
10450 parser->non_integral_constant_expression_p = false;
10451 /* Parse the arguments. */
10457 /* Consume the comma. */
10458 cp_lexer_consume_token (parser->lexer);
10460 /* Parse the template-argument. */
10461 argument = cp_parser_template_argument (parser);
10463 /* If the next token is an ellipsis, we're expanding a template
10465 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10467 /* Consume the `...' token. */
10468 cp_lexer_consume_token (parser->lexer);
10470 /* Make the argument into a TYPE_PACK_EXPANSION or
10471 EXPR_PACK_EXPANSION. */
10472 argument = make_pack_expansion (argument);
10475 if (n_args == alloced)
10479 if (arg_ary == fixed_args)
10481 arg_ary = XNEWVEC (tree, alloced);
10482 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10485 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10487 arg_ary[n_args++] = argument;
10489 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10491 vec = make_tree_vec (n_args);
10494 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10496 if (arg_ary != fixed_args)
10498 parser->non_integral_constant_expression_p = saved_non_ice_p;
10499 parser->integral_constant_expression_p = saved_ice_p;
10500 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10504 /* Parse a template-argument.
10507 assignment-expression
10511 The representation is that of an assignment-expression, type-id, or
10512 id-expression -- except that the qualified id-expression is
10513 evaluated, so that the value returned is either a DECL or an
10516 Although the standard says "assignment-expression", it forbids
10517 throw-expressions or assignments in the template argument.
10518 Therefore, we use "conditional-expression" instead. */
10521 cp_parser_template_argument (cp_parser* parser)
10526 bool maybe_type_id = false;
10527 cp_token *token = NULL, *argument_start_token = NULL;
10530 /* There's really no way to know what we're looking at, so we just
10531 try each alternative in order.
10535 In a template-argument, an ambiguity between a type-id and an
10536 expression is resolved to a type-id, regardless of the form of
10537 the corresponding template-parameter.
10539 Therefore, we try a type-id first. */
10540 cp_parser_parse_tentatively (parser);
10541 argument = cp_parser_type_id (parser);
10542 /* If there was no error parsing the type-id but the next token is a
10543 '>>', our behavior depends on which dialect of C++ we're
10544 parsing. In C++98, we probably found a typo for '> >'. But there
10545 are type-id which are also valid expressions. For instance:
10547 struct X { int operator >> (int); };
10548 template <int V> struct Foo {};
10551 Here 'X()' is a valid type-id of a function type, but the user just
10552 wanted to write the expression "X() >> 5". Thus, we remember that we
10553 found a valid type-id, but we still try to parse the argument as an
10554 expression to see what happens.
10556 In C++0x, the '>>' will be considered two separate '>'
10558 if (!cp_parser_error_occurred (parser)
10559 && cxx_dialect == cxx98
10560 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10562 maybe_type_id = true;
10563 cp_parser_abort_tentative_parse (parser);
10567 /* If the next token isn't a `,' or a `>', then this argument wasn't
10568 really finished. This means that the argument is not a valid
10570 if (!cp_parser_next_token_ends_template_argument_p (parser))
10571 cp_parser_error (parser, "expected template-argument");
10572 /* If that worked, we're done. */
10573 if (cp_parser_parse_definitely (parser))
10576 /* We're still not sure what the argument will be. */
10577 cp_parser_parse_tentatively (parser);
10578 /* Try a template. */
10579 argument_start_token = cp_lexer_peek_token (parser->lexer);
10580 argument = cp_parser_id_expression (parser,
10581 /*template_keyword_p=*/false,
10582 /*check_dependency_p=*/true,
10584 /*declarator_p=*/false,
10585 /*optional_p=*/false);
10586 /* If the next token isn't a `,' or a `>', then this argument wasn't
10587 really finished. */
10588 if (!cp_parser_next_token_ends_template_argument_p (parser))
10589 cp_parser_error (parser, "expected template-argument");
10590 if (!cp_parser_error_occurred (parser))
10592 /* Figure out what is being referred to. If the id-expression
10593 was for a class template specialization, then we will have a
10594 TYPE_DECL at this point. There is no need to do name lookup
10595 at this point in that case. */
10596 if (TREE_CODE (argument) != TYPE_DECL)
10597 argument = cp_parser_lookup_name (parser, argument,
10599 /*is_template=*/template_p,
10600 /*is_namespace=*/false,
10601 /*check_dependency=*/true,
10602 /*ambiguous_decls=*/NULL,
10603 argument_start_token->location);
10604 if (TREE_CODE (argument) != TEMPLATE_DECL
10605 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10606 cp_parser_error (parser, "expected template-name");
10608 if (cp_parser_parse_definitely (parser))
10610 /* It must be a non-type argument. There permitted cases are given
10611 in [temp.arg.nontype]:
10613 -- an integral constant-expression of integral or enumeration
10616 -- the name of a non-type template-parameter; or
10618 -- the name of an object or function with external linkage...
10620 -- the address of an object or function with external linkage...
10622 -- a pointer to member... */
10623 /* Look for a non-type template parameter. */
10624 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10626 cp_parser_parse_tentatively (parser);
10627 argument = cp_parser_primary_expression (parser,
10628 /*address_p=*/false,
10630 /*template_arg_p=*/true,
10632 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10633 || !cp_parser_next_token_ends_template_argument_p (parser))
10634 cp_parser_simulate_error (parser);
10635 if (cp_parser_parse_definitely (parser))
10639 /* If the next token is "&", the argument must be the address of an
10640 object or function with external linkage. */
10641 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10643 cp_lexer_consume_token (parser->lexer);
10644 /* See if we might have an id-expression. */
10645 token = cp_lexer_peek_token (parser->lexer);
10646 if (token->type == CPP_NAME
10647 || token->keyword == RID_OPERATOR
10648 || token->type == CPP_SCOPE
10649 || token->type == CPP_TEMPLATE_ID
10650 || token->type == CPP_NESTED_NAME_SPECIFIER)
10652 cp_parser_parse_tentatively (parser);
10653 argument = cp_parser_primary_expression (parser,
10656 /*template_arg_p=*/true,
10658 if (cp_parser_error_occurred (parser)
10659 || !cp_parser_next_token_ends_template_argument_p (parser))
10660 cp_parser_abort_tentative_parse (parser);
10663 if (TREE_CODE (argument) == INDIRECT_REF)
10665 gcc_assert (REFERENCE_REF_P (argument));
10666 argument = TREE_OPERAND (argument, 0);
10669 if (TREE_CODE (argument) == VAR_DECL)
10671 /* A variable without external linkage might still be a
10672 valid constant-expression, so no error is issued here
10673 if the external-linkage check fails. */
10674 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10675 cp_parser_simulate_error (parser);
10677 else if (is_overloaded_fn (argument))
10678 /* All overloaded functions are allowed; if the external
10679 linkage test does not pass, an error will be issued
10683 && (TREE_CODE (argument) == OFFSET_REF
10684 || TREE_CODE (argument) == SCOPE_REF))
10685 /* A pointer-to-member. */
10687 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10690 cp_parser_simulate_error (parser);
10692 if (cp_parser_parse_definitely (parser))
10695 argument = build_x_unary_op (ADDR_EXPR, argument,
10696 tf_warning_or_error);
10701 /* If the argument started with "&", there are no other valid
10702 alternatives at this point. */
10705 cp_parser_error (parser, "invalid non-type template argument");
10706 return error_mark_node;
10709 /* If the argument wasn't successfully parsed as a type-id followed
10710 by '>>', the argument can only be a constant expression now.
10711 Otherwise, we try parsing the constant-expression tentatively,
10712 because the argument could really be a type-id. */
10714 cp_parser_parse_tentatively (parser);
10715 argument = cp_parser_constant_expression (parser,
10716 /*allow_non_constant_p=*/false,
10717 /*non_constant_p=*/NULL);
10718 argument = fold_non_dependent_expr (argument);
10719 if (!maybe_type_id)
10721 if (!cp_parser_next_token_ends_template_argument_p (parser))
10722 cp_parser_error (parser, "expected template-argument");
10723 if (cp_parser_parse_definitely (parser))
10725 /* We did our best to parse the argument as a non type-id, but that
10726 was the only alternative that matched (albeit with a '>' after
10727 it). We can assume it's just a typo from the user, and a
10728 diagnostic will then be issued. */
10729 return cp_parser_type_id (parser);
10732 /* Parse an explicit-instantiation.
10734 explicit-instantiation:
10735 template declaration
10737 Although the standard says `declaration', what it really means is:
10739 explicit-instantiation:
10740 template decl-specifier-seq [opt] declarator [opt] ;
10742 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10743 supposed to be allowed. A defect report has been filed about this
10748 explicit-instantiation:
10749 storage-class-specifier template
10750 decl-specifier-seq [opt] declarator [opt] ;
10751 function-specifier template
10752 decl-specifier-seq [opt] declarator [opt] ; */
10755 cp_parser_explicit_instantiation (cp_parser* parser)
10757 int declares_class_or_enum;
10758 cp_decl_specifier_seq decl_specifiers;
10759 tree extension_specifier = NULL_TREE;
10762 /* Look for an (optional) storage-class-specifier or
10763 function-specifier. */
10764 if (cp_parser_allow_gnu_extensions_p (parser))
10766 extension_specifier
10767 = cp_parser_storage_class_specifier_opt (parser);
10768 if (!extension_specifier)
10769 extension_specifier
10770 = cp_parser_function_specifier_opt (parser,
10771 /*decl_specs=*/NULL);
10774 /* Look for the `template' keyword. */
10775 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10776 /* Let the front end know that we are processing an explicit
10778 begin_explicit_instantiation ();
10779 /* [temp.explicit] says that we are supposed to ignore access
10780 control while processing explicit instantiation directives. */
10781 push_deferring_access_checks (dk_no_check);
10782 /* Parse a decl-specifier-seq. */
10783 token = cp_lexer_peek_token (parser->lexer);
10784 cp_parser_decl_specifier_seq (parser,
10785 CP_PARSER_FLAGS_OPTIONAL,
10787 &declares_class_or_enum);
10788 /* If there was exactly one decl-specifier, and it declared a class,
10789 and there's no declarator, then we have an explicit type
10791 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10795 type = check_tag_decl (&decl_specifiers);
10796 /* Turn access control back on for names used during
10797 template instantiation. */
10798 pop_deferring_access_checks ();
10800 do_type_instantiation (type, extension_specifier,
10801 /*complain=*/tf_error);
10805 cp_declarator *declarator;
10808 /* Parse the declarator. */
10810 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10811 /*ctor_dtor_or_conv_p=*/NULL,
10812 /*parenthesized_p=*/NULL,
10813 /*member_p=*/false);
10814 if (declares_class_or_enum & 2)
10815 cp_parser_check_for_definition_in_return_type (declarator,
10816 decl_specifiers.type,
10817 decl_specifiers.type_location);
10818 if (declarator != cp_error_declarator)
10820 decl = grokdeclarator (declarator, &decl_specifiers,
10821 NORMAL, 0, &decl_specifiers.attributes);
10822 /* Turn access control back on for names used during
10823 template instantiation. */
10824 pop_deferring_access_checks ();
10825 /* Do the explicit instantiation. */
10826 do_decl_instantiation (decl, extension_specifier);
10830 pop_deferring_access_checks ();
10831 /* Skip the body of the explicit instantiation. */
10832 cp_parser_skip_to_end_of_statement (parser);
10835 /* We're done with the instantiation. */
10836 end_explicit_instantiation ();
10838 cp_parser_consume_semicolon_at_end_of_statement (parser);
10841 /* Parse an explicit-specialization.
10843 explicit-specialization:
10844 template < > declaration
10846 Although the standard says `declaration', what it really means is:
10848 explicit-specialization:
10849 template <> decl-specifier [opt] init-declarator [opt] ;
10850 template <> function-definition
10851 template <> explicit-specialization
10852 template <> template-declaration */
10855 cp_parser_explicit_specialization (cp_parser* parser)
10857 bool need_lang_pop;
10858 cp_token *token = cp_lexer_peek_token (parser->lexer);
10860 /* Look for the `template' keyword. */
10861 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10862 /* Look for the `<'. */
10863 cp_parser_require (parser, CPP_LESS, "%<<%>");
10864 /* Look for the `>'. */
10865 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10866 /* We have processed another parameter list. */
10867 ++parser->num_template_parameter_lists;
10870 A template ... explicit specialization ... shall not have C
10872 if (current_lang_name == lang_name_c)
10874 error ("%Htemplate specialization with C linkage", &token->location);
10875 /* Give it C++ linkage to avoid confusing other parts of the
10877 push_lang_context (lang_name_cplusplus);
10878 need_lang_pop = true;
10881 need_lang_pop = false;
10882 /* Let the front end know that we are beginning a specialization. */
10883 if (!begin_specialization ())
10885 end_specialization ();
10886 cp_parser_skip_to_end_of_block_or_statement (parser);
10890 /* If the next keyword is `template', we need to figure out whether
10891 or not we're looking a template-declaration. */
10892 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10894 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10895 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10896 cp_parser_template_declaration_after_export (parser,
10897 /*member_p=*/false);
10899 cp_parser_explicit_specialization (parser);
10902 /* Parse the dependent declaration. */
10903 cp_parser_single_declaration (parser,
10905 /*member_p=*/false,
10906 /*explicit_specialization_p=*/true,
10907 /*friend_p=*/NULL);
10908 /* We're done with the specialization. */
10909 end_specialization ();
10910 /* For the erroneous case of a template with C linkage, we pushed an
10911 implicit C++ linkage scope; exit that scope now. */
10913 pop_lang_context ();
10914 /* We're done with this parameter list. */
10915 --parser->num_template_parameter_lists;
10918 /* Parse a type-specifier.
10921 simple-type-specifier
10924 elaborated-type-specifier
10932 Returns a representation of the type-specifier. For a
10933 class-specifier, enum-specifier, or elaborated-type-specifier, a
10934 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10936 The parser flags FLAGS is used to control type-specifier parsing.
10938 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10939 in a decl-specifier-seq.
10941 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10942 class-specifier, enum-specifier, or elaborated-type-specifier, then
10943 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10944 if a type is declared; 2 if it is defined. Otherwise, it is set to
10947 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10948 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10949 is set to FALSE. */
10952 cp_parser_type_specifier (cp_parser* parser,
10953 cp_parser_flags flags,
10954 cp_decl_specifier_seq *decl_specs,
10955 bool is_declaration,
10956 int* declares_class_or_enum,
10957 bool* is_cv_qualifier)
10959 tree type_spec = NULL_TREE;
10962 cp_decl_spec ds = ds_last;
10964 /* Assume this type-specifier does not declare a new type. */
10965 if (declares_class_or_enum)
10966 *declares_class_or_enum = 0;
10967 /* And that it does not specify a cv-qualifier. */
10968 if (is_cv_qualifier)
10969 *is_cv_qualifier = false;
10970 /* Peek at the next token. */
10971 token = cp_lexer_peek_token (parser->lexer);
10973 /* If we're looking at a keyword, we can use that to guide the
10974 production we choose. */
10975 keyword = token->keyword;
10979 /* Look for the enum-specifier. */
10980 type_spec = cp_parser_enum_specifier (parser);
10981 /* If that worked, we're done. */
10984 if (declares_class_or_enum)
10985 *declares_class_or_enum = 2;
10987 cp_parser_set_decl_spec_type (decl_specs,
10990 /*user_defined_p=*/true);
10994 goto elaborated_type_specifier;
10996 /* Any of these indicate either a class-specifier, or an
10997 elaborated-type-specifier. */
11001 /* Parse tentatively so that we can back up if we don't find a
11002 class-specifier. */
11003 cp_parser_parse_tentatively (parser);
11004 /* Look for the class-specifier. */
11005 type_spec = cp_parser_class_specifier (parser);
11006 /* If that worked, we're done. */
11007 if (cp_parser_parse_definitely (parser))
11009 if (declares_class_or_enum)
11010 *declares_class_or_enum = 2;
11012 cp_parser_set_decl_spec_type (decl_specs,
11015 /*user_defined_p=*/true);
11019 /* Fall through. */
11020 elaborated_type_specifier:
11021 /* We're declaring (not defining) a class or enum. */
11022 if (declares_class_or_enum)
11023 *declares_class_or_enum = 1;
11025 /* Fall through. */
11027 /* Look for an elaborated-type-specifier. */
11029 = (cp_parser_elaborated_type_specifier
11031 decl_specs && decl_specs->specs[(int) ds_friend],
11034 cp_parser_set_decl_spec_type (decl_specs,
11037 /*user_defined_p=*/true);
11042 if (is_cv_qualifier)
11043 *is_cv_qualifier = true;
11048 if (is_cv_qualifier)
11049 *is_cv_qualifier = true;
11054 if (is_cv_qualifier)
11055 *is_cv_qualifier = true;
11059 /* The `__complex__' keyword is a GNU extension. */
11067 /* Handle simple keywords. */
11072 ++decl_specs->specs[(int)ds];
11073 decl_specs->any_specifiers_p = true;
11075 return cp_lexer_consume_token (parser->lexer)->u.value;
11078 /* If we do not already have a type-specifier, assume we are looking
11079 at a simple-type-specifier. */
11080 type_spec = cp_parser_simple_type_specifier (parser,
11084 /* If we didn't find a type-specifier, and a type-specifier was not
11085 optional in this context, issue an error message. */
11086 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11088 cp_parser_error (parser, "expected type specifier");
11089 return error_mark_node;
11095 /* Parse a simple-type-specifier.
11097 simple-type-specifier:
11098 :: [opt] nested-name-specifier [opt] type-name
11099 :: [opt] nested-name-specifier template template-id
11114 simple-type-specifier:
11116 decltype ( expression )
11122 simple-type-specifier:
11123 __typeof__ unary-expression
11124 __typeof__ ( type-id )
11126 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11127 appropriately updated. */
11130 cp_parser_simple_type_specifier (cp_parser* parser,
11131 cp_decl_specifier_seq *decl_specs,
11132 cp_parser_flags flags)
11134 tree type = NULL_TREE;
11137 /* Peek at the next token. */
11138 token = cp_lexer_peek_token (parser->lexer);
11140 /* If we're looking at a keyword, things are easy. */
11141 switch (token->keyword)
11145 decl_specs->explicit_char_p = true;
11146 type = char_type_node;
11149 type = char16_type_node;
11152 type = char32_type_node;
11155 type = wchar_type_node;
11158 type = boolean_type_node;
11162 ++decl_specs->specs[(int) ds_short];
11163 type = short_integer_type_node;
11167 decl_specs->explicit_int_p = true;
11168 type = integer_type_node;
11172 ++decl_specs->specs[(int) ds_long];
11173 type = long_integer_type_node;
11177 ++decl_specs->specs[(int) ds_signed];
11178 type = integer_type_node;
11182 ++decl_specs->specs[(int) ds_unsigned];
11183 type = unsigned_type_node;
11186 type = float_type_node;
11189 type = double_type_node;
11192 type = void_type_node;
11196 maybe_warn_cpp0x ("C++0x auto");
11197 type = make_auto ();
11201 /* Parse the `decltype' type. */
11202 type = cp_parser_decltype (parser);
11205 cp_parser_set_decl_spec_type (decl_specs, type,
11207 /*user_defined_p=*/true);
11212 /* Consume the `typeof' token. */
11213 cp_lexer_consume_token (parser->lexer);
11214 /* Parse the operand to `typeof'. */
11215 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11216 /* If it is not already a TYPE, take its type. */
11217 if (!TYPE_P (type))
11218 type = finish_typeof (type);
11221 cp_parser_set_decl_spec_type (decl_specs, type,
11223 /*user_defined_p=*/true);
11231 /* If the type-specifier was for a built-in type, we're done. */
11236 /* Record the type. */
11238 && (token->keyword != RID_SIGNED
11239 && token->keyword != RID_UNSIGNED
11240 && token->keyword != RID_SHORT
11241 && token->keyword != RID_LONG))
11242 cp_parser_set_decl_spec_type (decl_specs,
11245 /*user_defined=*/false);
11247 decl_specs->any_specifiers_p = true;
11249 /* Consume the token. */
11250 id = cp_lexer_consume_token (parser->lexer)->u.value;
11252 /* There is no valid C++ program where a non-template type is
11253 followed by a "<". That usually indicates that the user thought
11254 that the type was a template. */
11255 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11257 return TYPE_NAME (type);
11260 /* The type-specifier must be a user-defined type. */
11261 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11266 /* Don't gobble tokens or issue error messages if this is an
11267 optional type-specifier. */
11268 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11269 cp_parser_parse_tentatively (parser);
11271 /* Look for the optional `::' operator. */
11273 = (cp_parser_global_scope_opt (parser,
11274 /*current_scope_valid_p=*/false)
11276 /* Look for the nested-name specifier. */
11278 = (cp_parser_nested_name_specifier_opt (parser,
11279 /*typename_keyword_p=*/false,
11280 /*check_dependency_p=*/true,
11282 /*is_declaration=*/false)
11284 token = cp_lexer_peek_token (parser->lexer);
11285 /* If we have seen a nested-name-specifier, and the next token
11286 is `template', then we are using the template-id production. */
11288 && cp_parser_optional_template_keyword (parser))
11290 /* Look for the template-id. */
11291 type = cp_parser_template_id (parser,
11292 /*template_keyword_p=*/true,
11293 /*check_dependency_p=*/true,
11294 /*is_declaration=*/false);
11295 /* If the template-id did not name a type, we are out of
11297 if (TREE_CODE (type) != TYPE_DECL)
11299 cp_parser_error (parser, "expected template-id for type");
11303 /* Otherwise, look for a type-name. */
11305 type = cp_parser_type_name (parser);
11306 /* Keep track of all name-lookups performed in class scopes. */
11310 && TREE_CODE (type) == TYPE_DECL
11311 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11312 maybe_note_name_used_in_class (DECL_NAME (type), type);
11313 /* If it didn't work out, we don't have a TYPE. */
11314 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11315 && !cp_parser_parse_definitely (parser))
11317 if (type && decl_specs)
11318 cp_parser_set_decl_spec_type (decl_specs, type,
11320 /*user_defined=*/true);
11323 /* If we didn't get a type-name, issue an error message. */
11324 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11326 cp_parser_error (parser, "expected type-name");
11327 return error_mark_node;
11330 /* There is no valid C++ program where a non-template type is
11331 followed by a "<". That usually indicates that the user thought
11332 that the type was a template. */
11333 if (type && type != error_mark_node)
11335 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11336 If it is, then the '<'...'>' enclose protocol names rather than
11337 template arguments, and so everything is fine. */
11338 if (c_dialect_objc ()
11339 && (objc_is_id (type) || objc_is_class_name (type)))
11341 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11342 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11344 /* Clobber the "unqualified" type previously entered into
11345 DECL_SPECS with the new, improved protocol-qualified version. */
11347 decl_specs->type = qual_type;
11352 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11359 /* Parse a type-name.
11372 Returns a TYPE_DECL for the type. */
11375 cp_parser_type_name (cp_parser* parser)
11379 /* We can't know yet whether it is a class-name or not. */
11380 cp_parser_parse_tentatively (parser);
11381 /* Try a class-name. */
11382 type_decl = cp_parser_class_name (parser,
11383 /*typename_keyword_p=*/false,
11384 /*template_keyword_p=*/false,
11386 /*check_dependency_p=*/true,
11387 /*class_head_p=*/false,
11388 /*is_declaration=*/false);
11389 /* If it's not a class-name, keep looking. */
11390 if (!cp_parser_parse_definitely (parser))
11392 /* It must be a typedef-name or an enum-name. */
11393 return cp_parser_nonclass_name (parser);
11399 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11407 Returns a TYPE_DECL for the type. */
11410 cp_parser_nonclass_name (cp_parser* parser)
11415 cp_token *token = cp_lexer_peek_token (parser->lexer);
11416 identifier = cp_parser_identifier (parser);
11417 if (identifier == error_mark_node)
11418 return error_mark_node;
11420 /* Look up the type-name. */
11421 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11423 if (TREE_CODE (type_decl) != TYPE_DECL
11424 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11426 /* See if this is an Objective-C type. */
11427 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11428 tree type = objc_get_protocol_qualified_type (identifier, protos);
11430 type_decl = TYPE_NAME (type);
11433 /* Issue an error if we did not find a type-name. */
11434 if (TREE_CODE (type_decl) != TYPE_DECL)
11436 if (!cp_parser_simulate_error (parser))
11437 cp_parser_name_lookup_error (parser, identifier, type_decl,
11438 "is not a type", token->location);
11439 return error_mark_node;
11441 /* Remember that the name was used in the definition of the
11442 current class so that we can check later to see if the
11443 meaning would have been different after the class was
11444 entirely defined. */
11445 else if (type_decl != error_mark_node
11447 maybe_note_name_used_in_class (identifier, type_decl);
11452 /* Parse an elaborated-type-specifier. Note that the grammar given
11453 here incorporates the resolution to DR68.
11455 elaborated-type-specifier:
11456 class-key :: [opt] nested-name-specifier [opt] identifier
11457 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11458 enum-key :: [opt] nested-name-specifier [opt] identifier
11459 typename :: [opt] nested-name-specifier identifier
11460 typename :: [opt] nested-name-specifier template [opt]
11465 elaborated-type-specifier:
11466 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11467 class-key attributes :: [opt] nested-name-specifier [opt]
11468 template [opt] template-id
11469 enum attributes :: [opt] nested-name-specifier [opt] identifier
11471 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11472 declared `friend'. If IS_DECLARATION is TRUE, then this
11473 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11474 something is being declared.
11476 Returns the TYPE specified. */
11479 cp_parser_elaborated_type_specifier (cp_parser* parser,
11481 bool is_declaration)
11483 enum tag_types tag_type;
11485 tree type = NULL_TREE;
11486 tree attributes = NULL_TREE;
11487 cp_token *token = NULL;
11489 /* See if we're looking at the `enum' keyword. */
11490 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11492 /* Consume the `enum' token. */
11493 cp_lexer_consume_token (parser->lexer);
11494 /* Remember that it's an enumeration type. */
11495 tag_type = enum_type;
11496 /* Parse the optional `struct' or `class' key (for C++0x scoped
11498 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11499 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11501 if (cxx_dialect == cxx98)
11502 maybe_warn_cpp0x ("scoped enums");
11504 /* Consume the `struct' or `class'. */
11505 cp_lexer_consume_token (parser->lexer);
11507 /* Parse the attributes. */
11508 attributes = cp_parser_attributes_opt (parser);
11510 /* Or, it might be `typename'. */
11511 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11514 /* Consume the `typename' token. */
11515 cp_lexer_consume_token (parser->lexer);
11516 /* Remember that it's a `typename' type. */
11517 tag_type = typename_type;
11518 /* The `typename' keyword is only allowed in templates. */
11519 if (!processing_template_decl)
11520 permerror (input_location, "using %<typename%> outside of template");
11522 /* Otherwise it must be a class-key. */
11525 tag_type = cp_parser_class_key (parser);
11526 if (tag_type == none_type)
11527 return error_mark_node;
11528 /* Parse the attributes. */
11529 attributes = cp_parser_attributes_opt (parser);
11532 /* Look for the `::' operator. */
11533 cp_parser_global_scope_opt (parser,
11534 /*current_scope_valid_p=*/false);
11535 /* Look for the nested-name-specifier. */
11536 if (tag_type == typename_type)
11538 if (!cp_parser_nested_name_specifier (parser,
11539 /*typename_keyword_p=*/true,
11540 /*check_dependency_p=*/true,
11543 return error_mark_node;
11546 /* Even though `typename' is not present, the proposed resolution
11547 to Core Issue 180 says that in `class A<T>::B', `B' should be
11548 considered a type-name, even if `A<T>' is dependent. */
11549 cp_parser_nested_name_specifier_opt (parser,
11550 /*typename_keyword_p=*/true,
11551 /*check_dependency_p=*/true,
11554 /* For everything but enumeration types, consider a template-id.
11555 For an enumeration type, consider only a plain identifier. */
11556 if (tag_type != enum_type)
11558 bool template_p = false;
11561 /* Allow the `template' keyword. */
11562 template_p = cp_parser_optional_template_keyword (parser);
11563 /* If we didn't see `template', we don't know if there's a
11564 template-id or not. */
11566 cp_parser_parse_tentatively (parser);
11567 /* Parse the template-id. */
11568 token = cp_lexer_peek_token (parser->lexer);
11569 decl = cp_parser_template_id (parser, template_p,
11570 /*check_dependency_p=*/true,
11572 /* If we didn't find a template-id, look for an ordinary
11574 if (!template_p && !cp_parser_parse_definitely (parser))
11576 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11577 in effect, then we must assume that, upon instantiation, the
11578 template will correspond to a class. */
11579 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11580 && tag_type == typename_type)
11581 type = make_typename_type (parser->scope, decl,
11583 /*complain=*/tf_error);
11585 type = TREE_TYPE (decl);
11590 token = cp_lexer_peek_token (parser->lexer);
11591 identifier = cp_parser_identifier (parser);
11593 if (identifier == error_mark_node)
11595 parser->scope = NULL_TREE;
11596 return error_mark_node;
11599 /* For a `typename', we needn't call xref_tag. */
11600 if (tag_type == typename_type
11601 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11602 return cp_parser_make_typename_type (parser, parser->scope,
11605 /* Look up a qualified name in the usual way. */
11609 tree ambiguous_decls;
11611 decl = cp_parser_lookup_name (parser, identifier,
11613 /*is_template=*/false,
11614 /*is_namespace=*/false,
11615 /*check_dependency=*/true,
11619 /* If the lookup was ambiguous, an error will already have been
11621 if (ambiguous_decls)
11622 return error_mark_node;
11624 /* If we are parsing friend declaration, DECL may be a
11625 TEMPLATE_DECL tree node here. However, we need to check
11626 whether this TEMPLATE_DECL results in valid code. Consider
11627 the following example:
11630 template <class T> class C {};
11633 template <class T> friend class N::C; // #1, valid code
11635 template <class T> class Y {
11636 friend class N::C; // #2, invalid code
11639 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11640 name lookup of `N::C'. We see that friend declaration must
11641 be template for the code to be valid. Note that
11642 processing_template_decl does not work here since it is
11643 always 1 for the above two cases. */
11645 decl = (cp_parser_maybe_treat_template_as_class
11646 (decl, /*tag_name_p=*/is_friend
11647 && parser->num_template_parameter_lists));
11649 if (TREE_CODE (decl) != TYPE_DECL)
11651 cp_parser_diagnose_invalid_type_name (parser,
11655 return error_mark_node;
11658 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11660 bool allow_template = (parser->num_template_parameter_lists
11661 || DECL_SELF_REFERENCE_P (decl));
11662 type = check_elaborated_type_specifier (tag_type, decl,
11665 if (type == error_mark_node)
11666 return error_mark_node;
11669 /* Forward declarations of nested types, such as
11674 are invalid unless all components preceding the final '::'
11675 are complete. If all enclosing types are complete, these
11676 declarations become merely pointless.
11678 Invalid forward declarations of nested types are errors
11679 caught elsewhere in parsing. Those that are pointless arrive
11682 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11683 && !is_friend && !processing_explicit_instantiation)
11684 warning (0, "declaration %qD does not declare anything", decl);
11686 type = TREE_TYPE (decl);
11690 /* An elaborated-type-specifier sometimes introduces a new type and
11691 sometimes names an existing type. Normally, the rule is that it
11692 introduces a new type only if there is not an existing type of
11693 the same name already in scope. For example, given:
11696 void f() { struct S s; }
11698 the `struct S' in the body of `f' is the same `struct S' as in
11699 the global scope; the existing definition is used. However, if
11700 there were no global declaration, this would introduce a new
11701 local class named `S'.
11703 An exception to this rule applies to the following code:
11705 namespace N { struct S; }
11707 Here, the elaborated-type-specifier names a new type
11708 unconditionally; even if there is already an `S' in the
11709 containing scope this declaration names a new type.
11710 This exception only applies if the elaborated-type-specifier
11711 forms the complete declaration:
11715 A declaration consisting solely of `class-key identifier ;' is
11716 either a redeclaration of the name in the current scope or a
11717 forward declaration of the identifier as a class name. It
11718 introduces the name into the current scope.
11720 We are in this situation precisely when the next token is a `;'.
11722 An exception to the exception is that a `friend' declaration does
11723 *not* name a new type; i.e., given:
11725 struct S { friend struct T; };
11727 `T' is not a new type in the scope of `S'.
11729 Also, `new struct S' or `sizeof (struct S)' never results in the
11730 definition of a new type; a new type can only be declared in a
11731 declaration context. */
11737 /* Friends have special name lookup rules. */
11738 ts = ts_within_enclosing_non_class;
11739 else if (is_declaration
11740 && cp_lexer_next_token_is (parser->lexer,
11742 /* This is a `class-key identifier ;' */
11748 (parser->num_template_parameter_lists
11749 && (cp_parser_next_token_starts_class_definition_p (parser)
11750 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11751 /* An unqualified name was used to reference this type, so
11752 there were no qualifying templates. */
11753 if (!cp_parser_check_template_parameters (parser,
11754 /*num_templates=*/0,
11756 return error_mark_node;
11757 type = xref_tag (tag_type, identifier, ts, template_p);
11761 if (type == error_mark_node)
11762 return error_mark_node;
11764 /* Allow attributes on forward declarations of classes. */
11767 if (TREE_CODE (type) == TYPENAME_TYPE)
11768 warning (OPT_Wattributes,
11769 "attributes ignored on uninstantiated type");
11770 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11771 && ! processing_explicit_instantiation)
11772 warning (OPT_Wattributes,
11773 "attributes ignored on template instantiation");
11774 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11775 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11777 warning (OPT_Wattributes,
11778 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11781 if (tag_type != enum_type)
11782 cp_parser_check_class_key (tag_type, type);
11784 /* A "<" cannot follow an elaborated type specifier. If that
11785 happens, the user was probably trying to form a template-id. */
11786 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11791 /* Parse an enum-specifier.
11794 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11799 enum struct [C++0x]
11802 : type-specifier-seq
11805 enum-key attributes[opt] identifier [opt] enum-base [opt]
11806 { enumerator-list [opt] }attributes[opt]
11808 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11809 if the token stream isn't an enum-specifier after all. */
11812 cp_parser_enum_specifier (cp_parser* parser)
11817 bool scoped_enum_p = false;
11818 bool has_underlying_type = false;
11819 tree underlying_type = NULL_TREE;
11821 /* Parse tentatively so that we can back up if we don't find a
11823 cp_parser_parse_tentatively (parser);
11825 /* Caller guarantees that the current token is 'enum', an identifier
11826 possibly follows, and the token after that is an opening brace.
11827 If we don't have an identifier, fabricate an anonymous name for
11828 the enumeration being defined. */
11829 cp_lexer_consume_token (parser->lexer);
11831 /* Parse the "class" or "struct", which indicates a scoped
11832 enumeration type in C++0x. */
11833 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11834 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11836 if (cxx_dialect == cxx98)
11837 maybe_warn_cpp0x ("scoped enums");
11839 /* Consume the `struct' or `class' token. */
11840 cp_lexer_consume_token (parser->lexer);
11842 scoped_enum_p = true;
11845 attributes = cp_parser_attributes_opt (parser);
11847 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11848 identifier = cp_parser_identifier (parser);
11850 identifier = make_anon_name ();
11852 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11853 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11855 cp_decl_specifier_seq type_specifiers;
11857 /* At this point this is surely not elaborated type specifier. */
11858 if (!cp_parser_parse_definitely (parser))
11861 if (cxx_dialect == cxx98)
11862 maybe_warn_cpp0x ("scoped enums");
11864 /* Consume the `:'. */
11865 cp_lexer_consume_token (parser->lexer);
11867 has_underlying_type = true;
11869 /* Parse the type-specifier-seq. */
11870 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11873 /* If that didn't work, stop. */
11874 if (type_specifiers.type != error_mark_node)
11876 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11877 /*initialized=*/0, NULL);
11878 if (underlying_type == error_mark_node)
11879 underlying_type = NULL_TREE;
11883 /* Look for the `{' but don't consume it yet. */
11884 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11886 cp_parser_error (parser, "expected %<{%>");
11887 if (has_underlying_type)
11891 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11894 /* Issue an error message if type-definitions are forbidden here. */
11895 if (!cp_parser_check_type_definition (parser))
11896 type = error_mark_node;
11898 /* Create the new type. We do this before consuming the opening
11899 brace so the enum will be recorded as being on the line of its
11900 tag (or the 'enum' keyword, if there is no tag). */
11901 type = start_enum (identifier, underlying_type, scoped_enum_p);
11903 /* Consume the opening brace. */
11904 cp_lexer_consume_token (parser->lexer);
11906 if (type == error_mark_node)
11908 cp_parser_skip_to_end_of_block_or_statement (parser);
11909 return error_mark_node;
11912 /* If the next token is not '}', then there are some enumerators. */
11913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11914 cp_parser_enumerator_list (parser, type);
11916 /* Consume the final '}'. */
11917 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11919 /* Look for trailing attributes to apply to this enumeration, and
11920 apply them if appropriate. */
11921 if (cp_parser_allow_gnu_extensions_p (parser))
11923 tree trailing_attr = cp_parser_attributes_opt (parser);
11924 trailing_attr = chainon (trailing_attr, attributes);
11925 cplus_decl_attributes (&type,
11927 (int) ATTR_FLAG_TYPE_IN_PLACE);
11930 /* Finish up the enumeration. */
11931 finish_enum (type);
11936 /* Parse an enumerator-list. The enumerators all have the indicated
11940 enumerator-definition
11941 enumerator-list , enumerator-definition */
11944 cp_parser_enumerator_list (cp_parser* parser, tree type)
11948 /* Parse an enumerator-definition. */
11949 cp_parser_enumerator_definition (parser, type);
11951 /* If the next token is not a ',', we've reached the end of
11953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11955 /* Otherwise, consume the `,' and keep going. */
11956 cp_lexer_consume_token (parser->lexer);
11957 /* If the next token is a `}', there is a trailing comma. */
11958 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11960 if (!in_system_header)
11961 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11967 /* Parse an enumerator-definition. The enumerator has the indicated
11970 enumerator-definition:
11972 enumerator = constant-expression
11978 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11983 /* Look for the identifier. */
11984 identifier = cp_parser_identifier (parser);
11985 if (identifier == error_mark_node)
11988 /* If the next token is an '=', then there is an explicit value. */
11989 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11991 /* Consume the `=' token. */
11992 cp_lexer_consume_token (parser->lexer);
11993 /* Parse the value. */
11994 value = cp_parser_constant_expression (parser,
11995 /*allow_non_constant_p=*/false,
12001 /* Create the enumerator. */
12002 build_enumerator (identifier, value, type);
12005 /* Parse a namespace-name.
12008 original-namespace-name
12011 Returns the NAMESPACE_DECL for the namespace. */
12014 cp_parser_namespace_name (cp_parser* parser)
12017 tree namespace_decl;
12019 cp_token *token = cp_lexer_peek_token (parser->lexer);
12021 /* Get the name of the namespace. */
12022 identifier = cp_parser_identifier (parser);
12023 if (identifier == error_mark_node)
12024 return error_mark_node;
12026 /* Look up the identifier in the currently active scope. Look only
12027 for namespaces, due to:
12029 [basic.lookup.udir]
12031 When looking up a namespace-name in a using-directive or alias
12032 definition, only namespace names are considered.
12036 [basic.lookup.qual]
12038 During the lookup of a name preceding the :: scope resolution
12039 operator, object, function, and enumerator names are ignored.
12041 (Note that cp_parser_qualifying_entity only calls this
12042 function if the token after the name is the scope resolution
12044 namespace_decl = cp_parser_lookup_name (parser, identifier,
12046 /*is_template=*/false,
12047 /*is_namespace=*/true,
12048 /*check_dependency=*/true,
12049 /*ambiguous_decls=*/NULL,
12051 /* If it's not a namespace, issue an error. */
12052 if (namespace_decl == error_mark_node
12053 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12055 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12056 error ("%H%qD is not a namespace-name", &token->location, identifier);
12057 cp_parser_error (parser, "expected namespace-name");
12058 namespace_decl = error_mark_node;
12061 return namespace_decl;
12064 /* Parse a namespace-definition.
12066 namespace-definition:
12067 named-namespace-definition
12068 unnamed-namespace-definition
12070 named-namespace-definition:
12071 original-namespace-definition
12072 extension-namespace-definition
12074 original-namespace-definition:
12075 namespace identifier { namespace-body }
12077 extension-namespace-definition:
12078 namespace original-namespace-name { namespace-body }
12080 unnamed-namespace-definition:
12081 namespace { namespace-body } */
12084 cp_parser_namespace_definition (cp_parser* parser)
12086 tree identifier, attribs;
12087 bool has_visibility;
12090 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12093 cp_lexer_consume_token (parser->lexer);
12098 /* Look for the `namespace' keyword. */
12099 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12101 /* Get the name of the namespace. We do not attempt to distinguish
12102 between an original-namespace-definition and an
12103 extension-namespace-definition at this point. The semantic
12104 analysis routines are responsible for that. */
12105 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12106 identifier = cp_parser_identifier (parser);
12108 identifier = NULL_TREE;
12110 /* Parse any specified attributes. */
12111 attribs = cp_parser_attributes_opt (parser);
12113 /* Look for the `{' to start the namespace. */
12114 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12115 /* Start the namespace. */
12116 push_namespace (identifier);
12118 /* "inline namespace" is equivalent to a stub namespace definition
12119 followed by a strong using directive. */
12122 tree name_space = current_namespace;
12123 /* Set up namespace association. */
12124 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12125 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12126 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12127 /* Import the contents of the inline namespace. */
12129 do_using_directive (name_space);
12130 push_namespace (identifier);
12133 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12135 /* Parse the body of the namespace. */
12136 cp_parser_namespace_body (parser);
12138 #ifdef HANDLE_PRAGMA_VISIBILITY
12139 if (has_visibility)
12143 /* Finish the namespace. */
12145 /* Look for the final `}'. */
12146 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12149 /* Parse a namespace-body.
12152 declaration-seq [opt] */
12155 cp_parser_namespace_body (cp_parser* parser)
12157 cp_parser_declaration_seq_opt (parser);
12160 /* Parse a namespace-alias-definition.
12162 namespace-alias-definition:
12163 namespace identifier = qualified-namespace-specifier ; */
12166 cp_parser_namespace_alias_definition (cp_parser* parser)
12169 tree namespace_specifier;
12171 cp_token *token = cp_lexer_peek_token (parser->lexer);
12173 /* Look for the `namespace' keyword. */
12174 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12175 /* Look for the identifier. */
12176 identifier = cp_parser_identifier (parser);
12177 if (identifier == error_mark_node)
12179 /* Look for the `=' token. */
12180 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12181 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12183 error ("%H%<namespace%> definition is not allowed here", &token->location);
12184 /* Skip the definition. */
12185 cp_lexer_consume_token (parser->lexer);
12186 if (cp_parser_skip_to_closing_brace (parser))
12187 cp_lexer_consume_token (parser->lexer);
12190 cp_parser_require (parser, CPP_EQ, "%<=%>");
12191 /* Look for the qualified-namespace-specifier. */
12192 namespace_specifier
12193 = cp_parser_qualified_namespace_specifier (parser);
12194 /* Look for the `;' token. */
12195 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12197 /* Register the alias in the symbol table. */
12198 do_namespace_alias (identifier, namespace_specifier);
12201 /* Parse a qualified-namespace-specifier.
12203 qualified-namespace-specifier:
12204 :: [opt] nested-name-specifier [opt] namespace-name
12206 Returns a NAMESPACE_DECL corresponding to the specified
12210 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12212 /* Look for the optional `::'. */
12213 cp_parser_global_scope_opt (parser,
12214 /*current_scope_valid_p=*/false);
12216 /* Look for the optional nested-name-specifier. */
12217 cp_parser_nested_name_specifier_opt (parser,
12218 /*typename_keyword_p=*/false,
12219 /*check_dependency_p=*/true,
12221 /*is_declaration=*/true);
12223 return cp_parser_namespace_name (parser);
12226 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12227 access declaration.
12230 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12231 using :: unqualified-id ;
12233 access-declaration:
12239 cp_parser_using_declaration (cp_parser* parser,
12240 bool access_declaration_p)
12243 bool typename_p = false;
12244 bool global_scope_p;
12249 if (access_declaration_p)
12250 cp_parser_parse_tentatively (parser);
12253 /* Look for the `using' keyword. */
12254 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12256 /* Peek at the next token. */
12257 token = cp_lexer_peek_token (parser->lexer);
12258 /* See if it's `typename'. */
12259 if (token->keyword == RID_TYPENAME)
12261 /* Remember that we've seen it. */
12263 /* Consume the `typename' token. */
12264 cp_lexer_consume_token (parser->lexer);
12268 /* Look for the optional global scope qualification. */
12270 = (cp_parser_global_scope_opt (parser,
12271 /*current_scope_valid_p=*/false)
12274 /* If we saw `typename', or didn't see `::', then there must be a
12275 nested-name-specifier present. */
12276 if (typename_p || !global_scope_p)
12277 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12278 /*check_dependency_p=*/true,
12280 /*is_declaration=*/true);
12281 /* Otherwise, we could be in either of the two productions. In that
12282 case, treat the nested-name-specifier as optional. */
12284 qscope = cp_parser_nested_name_specifier_opt (parser,
12285 /*typename_keyword_p=*/false,
12286 /*check_dependency_p=*/true,
12288 /*is_declaration=*/true);
12290 qscope = global_namespace;
12292 if (access_declaration_p && cp_parser_error_occurred (parser))
12293 /* Something has already gone wrong; there's no need to parse
12294 further. Since an error has occurred, the return value of
12295 cp_parser_parse_definitely will be false, as required. */
12296 return cp_parser_parse_definitely (parser);
12298 token = cp_lexer_peek_token (parser->lexer);
12299 /* Parse the unqualified-id. */
12300 identifier = cp_parser_unqualified_id (parser,
12301 /*template_keyword_p=*/false,
12302 /*check_dependency_p=*/true,
12303 /*declarator_p=*/true,
12304 /*optional_p=*/false);
12306 if (access_declaration_p)
12308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12309 cp_parser_simulate_error (parser);
12310 if (!cp_parser_parse_definitely (parser))
12314 /* The function we call to handle a using-declaration is different
12315 depending on what scope we are in. */
12316 if (qscope == error_mark_node || identifier == error_mark_node)
12318 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12319 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12320 /* [namespace.udecl]
12322 A using declaration shall not name a template-id. */
12323 error ("%Ha template-id may not appear in a using-declaration",
12327 if (at_class_scope_p ())
12329 /* Create the USING_DECL. */
12330 decl = do_class_using_decl (parser->scope, identifier);
12332 if (check_for_bare_parameter_packs (decl))
12335 /* Add it to the list of members in this class. */
12336 finish_member_declaration (decl);
12340 decl = cp_parser_lookup_name_simple (parser,
12343 if (decl == error_mark_node)
12344 cp_parser_name_lookup_error (parser, identifier,
12347 else if (check_for_bare_parameter_packs (decl))
12349 else if (!at_namespace_scope_p ())
12350 do_local_using_decl (decl, qscope, identifier);
12352 do_toplevel_using_decl (decl, qscope, identifier);
12356 /* Look for the final `;'. */
12357 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12362 /* Parse a using-directive.
12365 using namespace :: [opt] nested-name-specifier [opt]
12366 namespace-name ; */
12369 cp_parser_using_directive (cp_parser* parser)
12371 tree namespace_decl;
12374 /* Look for the `using' keyword. */
12375 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12376 /* And the `namespace' keyword. */
12377 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12378 /* Look for the optional `::' operator. */
12379 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12380 /* And the optional nested-name-specifier. */
12381 cp_parser_nested_name_specifier_opt (parser,
12382 /*typename_keyword_p=*/false,
12383 /*check_dependency_p=*/true,
12385 /*is_declaration=*/true);
12386 /* Get the namespace being used. */
12387 namespace_decl = cp_parser_namespace_name (parser);
12388 /* And any specified attributes. */
12389 attribs = cp_parser_attributes_opt (parser);
12390 /* Update the symbol table. */
12391 parse_using_directive (namespace_decl, attribs);
12392 /* Look for the final `;'. */
12393 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12396 /* Parse an asm-definition.
12399 asm ( string-literal ) ;
12404 asm volatile [opt] ( string-literal ) ;
12405 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12406 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12407 : asm-operand-list [opt] ) ;
12408 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12409 : asm-operand-list [opt]
12410 : asm-operand-list [opt] ) ; */
12413 cp_parser_asm_definition (cp_parser* parser)
12416 tree outputs = NULL_TREE;
12417 tree inputs = NULL_TREE;
12418 tree clobbers = NULL_TREE;
12420 bool volatile_p = false;
12421 bool extended_p = false;
12422 bool invalid_inputs_p = false;
12423 bool invalid_outputs_p = false;
12425 /* Look for the `asm' keyword. */
12426 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12427 /* See if the next token is `volatile'. */
12428 if (cp_parser_allow_gnu_extensions_p (parser)
12429 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12431 /* Remember that we saw the `volatile' keyword. */
12433 /* Consume the token. */
12434 cp_lexer_consume_token (parser->lexer);
12436 /* Look for the opening `('. */
12437 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12439 /* Look for the string. */
12440 string = cp_parser_string_literal (parser, false, false);
12441 if (string == error_mark_node)
12443 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12444 /*consume_paren=*/true);
12448 /* If we're allowing GNU extensions, check for the extended assembly
12449 syntax. Unfortunately, the `:' tokens need not be separated by
12450 a space in C, and so, for compatibility, we tolerate that here
12451 too. Doing that means that we have to treat the `::' operator as
12453 if (cp_parser_allow_gnu_extensions_p (parser)
12454 && parser->in_function_body
12455 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12456 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12458 bool inputs_p = false;
12459 bool clobbers_p = false;
12461 /* The extended syntax was used. */
12464 /* Look for outputs. */
12465 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12467 /* Consume the `:'. */
12468 cp_lexer_consume_token (parser->lexer);
12469 /* Parse the output-operands. */
12470 if (cp_lexer_next_token_is_not (parser->lexer,
12472 && cp_lexer_next_token_is_not (parser->lexer,
12474 && cp_lexer_next_token_is_not (parser->lexer,
12476 outputs = cp_parser_asm_operand_list (parser);
12478 if (outputs == error_mark_node)
12479 invalid_outputs_p = true;
12481 /* If the next token is `::', there are no outputs, and the
12482 next token is the beginning of the inputs. */
12483 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12484 /* The inputs are coming next. */
12487 /* Look for inputs. */
12489 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12491 /* Consume the `:' or `::'. */
12492 cp_lexer_consume_token (parser->lexer);
12493 /* Parse the output-operands. */
12494 if (cp_lexer_next_token_is_not (parser->lexer,
12496 && cp_lexer_next_token_is_not (parser->lexer,
12498 inputs = cp_parser_asm_operand_list (parser);
12500 if (inputs == error_mark_node)
12501 invalid_inputs_p = true;
12503 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12504 /* The clobbers are coming next. */
12507 /* Look for clobbers. */
12509 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12511 /* Consume the `:' or `::'. */
12512 cp_lexer_consume_token (parser->lexer);
12513 /* Parse the clobbers. */
12514 if (cp_lexer_next_token_is_not (parser->lexer,
12516 clobbers = cp_parser_asm_clobber_list (parser);
12519 /* Look for the closing `)'. */
12520 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12521 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12522 /*consume_paren=*/true);
12523 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12525 if (!invalid_inputs_p && !invalid_outputs_p)
12527 /* Create the ASM_EXPR. */
12528 if (parser->in_function_body)
12530 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12532 /* If the extended syntax was not used, mark the ASM_EXPR. */
12535 tree temp = asm_stmt;
12536 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12537 temp = TREE_OPERAND (temp, 0);
12539 ASM_INPUT_P (temp) = 1;
12543 cgraph_add_asm_node (string);
12547 /* Declarators [gram.dcl.decl] */
12549 /* Parse an init-declarator.
12552 declarator initializer [opt]
12557 declarator asm-specification [opt] attributes [opt] initializer [opt]
12559 function-definition:
12560 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12562 decl-specifier-seq [opt] declarator function-try-block
12566 function-definition:
12567 __extension__ function-definition
12569 The DECL_SPECIFIERS apply to this declarator. Returns a
12570 representation of the entity declared. If MEMBER_P is TRUE, then
12571 this declarator appears in a class scope. The new DECL created by
12572 this declarator is returned.
12574 The CHECKS are access checks that should be performed once we know
12575 what entity is being declared (and, therefore, what classes have
12578 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12579 for a function-definition here as well. If the declarator is a
12580 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12581 be TRUE upon return. By that point, the function-definition will
12582 have been completely parsed.
12584 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12588 cp_parser_init_declarator (cp_parser* parser,
12589 cp_decl_specifier_seq *decl_specifiers,
12590 VEC (deferred_access_check,gc)* checks,
12591 bool function_definition_allowed_p,
12593 int declares_class_or_enum,
12594 bool* function_definition_p)
12596 cp_token *token = NULL, *asm_spec_start_token = NULL,
12597 *attributes_start_token = NULL;
12598 cp_declarator *declarator;
12599 tree prefix_attributes;
12601 tree asm_specification;
12603 tree decl = NULL_TREE;
12605 int is_initialized;
12606 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12607 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12609 enum cpp_ttype initialization_kind;
12610 bool is_direct_init = false;
12611 bool is_non_constant_init;
12612 int ctor_dtor_or_conv_p;
12614 tree pushed_scope = NULL;
12616 /* Gather the attributes that were provided with the
12617 decl-specifiers. */
12618 prefix_attributes = decl_specifiers->attributes;
12620 /* Assume that this is not the declarator for a function
12622 if (function_definition_p)
12623 *function_definition_p = false;
12625 /* Defer access checks while parsing the declarator; we cannot know
12626 what names are accessible until we know what is being
12628 resume_deferring_access_checks ();
12630 /* Parse the declarator. */
12631 token = cp_lexer_peek_token (parser->lexer);
12633 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12634 &ctor_dtor_or_conv_p,
12635 /*parenthesized_p=*/NULL,
12636 /*member_p=*/false);
12637 /* Gather up the deferred checks. */
12638 stop_deferring_access_checks ();
12640 /* If the DECLARATOR was erroneous, there's no need to go
12642 if (declarator == cp_error_declarator)
12643 return error_mark_node;
12645 /* Check that the number of template-parameter-lists is OK. */
12646 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12648 return error_mark_node;
12650 if (declares_class_or_enum & 2)
12651 cp_parser_check_for_definition_in_return_type (declarator,
12652 decl_specifiers->type,
12653 decl_specifiers->type_location);
12655 /* Figure out what scope the entity declared by the DECLARATOR is
12656 located in. `grokdeclarator' sometimes changes the scope, so
12657 we compute it now. */
12658 scope = get_scope_of_declarator (declarator);
12660 /* If we're allowing GNU extensions, look for an asm-specification
12662 if (cp_parser_allow_gnu_extensions_p (parser))
12664 /* Look for an asm-specification. */
12665 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12666 asm_specification = cp_parser_asm_specification_opt (parser);
12667 /* And attributes. */
12668 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12669 attributes = cp_parser_attributes_opt (parser);
12673 asm_specification = NULL_TREE;
12674 attributes = NULL_TREE;
12677 /* Peek at the next token. */
12678 token = cp_lexer_peek_token (parser->lexer);
12679 /* Check to see if the token indicates the start of a
12680 function-definition. */
12681 if (function_declarator_p (declarator)
12682 && cp_parser_token_starts_function_definition_p (token))
12684 if (!function_definition_allowed_p)
12686 /* If a function-definition should not appear here, issue an
12688 cp_parser_error (parser,
12689 "a function-definition is not allowed here");
12690 return error_mark_node;
12694 location_t func_brace_location
12695 = cp_lexer_peek_token (parser->lexer)->location;
12697 /* Neither attributes nor an asm-specification are allowed
12698 on a function-definition. */
12699 if (asm_specification)
12700 error ("%Han asm-specification is not allowed "
12701 "on a function-definition",
12702 &asm_spec_start_token->location);
12704 error ("%Hattributes are not allowed on a function-definition",
12705 &attributes_start_token->location);
12706 /* This is a function-definition. */
12707 *function_definition_p = true;
12709 /* Parse the function definition. */
12711 decl = cp_parser_save_member_function_body (parser,
12714 prefix_attributes);
12717 = (cp_parser_function_definition_from_specifiers_and_declarator
12718 (parser, decl_specifiers, prefix_attributes, declarator));
12720 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12722 /* This is where the prologue starts... */
12723 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12724 = func_brace_location;
12733 Only in function declarations for constructors, destructors, and
12734 type conversions can the decl-specifier-seq be omitted.
12736 We explicitly postpone this check past the point where we handle
12737 function-definitions because we tolerate function-definitions
12738 that are missing their return types in some modes. */
12739 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12741 cp_parser_error (parser,
12742 "expected constructor, destructor, or type conversion");
12743 return error_mark_node;
12746 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12747 if (token->type == CPP_EQ
12748 || token->type == CPP_OPEN_PAREN
12749 || token->type == CPP_OPEN_BRACE)
12751 is_initialized = SD_INITIALIZED;
12752 initialization_kind = token->type;
12754 if (token->type == CPP_EQ
12755 && function_declarator_p (declarator))
12757 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12758 if (t2->keyword == RID_DEFAULT)
12759 is_initialized = SD_DEFAULTED;
12760 else if (t2->keyword == RID_DELETE)
12761 is_initialized = SD_DELETED;
12766 /* If the init-declarator isn't initialized and isn't followed by a
12767 `,' or `;', it's not a valid init-declarator. */
12768 if (token->type != CPP_COMMA
12769 && token->type != CPP_SEMICOLON)
12771 cp_parser_error (parser, "expected initializer");
12772 return error_mark_node;
12774 is_initialized = SD_UNINITIALIZED;
12775 initialization_kind = CPP_EOF;
12778 /* Because start_decl has side-effects, we should only call it if we
12779 know we're going ahead. By this point, we know that we cannot
12780 possibly be looking at any other construct. */
12781 cp_parser_commit_to_tentative_parse (parser);
12783 /* If the decl specifiers were bad, issue an error now that we're
12784 sure this was intended to be a declarator. Then continue
12785 declaring the variable(s), as int, to try to cut down on further
12787 if (decl_specifiers->any_specifiers_p
12788 && decl_specifiers->type == error_mark_node)
12790 cp_parser_error (parser, "invalid type in declaration");
12791 decl_specifiers->type = integer_type_node;
12794 /* Check to see whether or not this declaration is a friend. */
12795 friend_p = cp_parser_friend_p (decl_specifiers);
12797 /* Enter the newly declared entry in the symbol table. If we're
12798 processing a declaration in a class-specifier, we wait until
12799 after processing the initializer. */
12802 if (parser->in_unbraced_linkage_specification_p)
12803 decl_specifiers->storage_class = sc_extern;
12804 decl = start_decl (declarator, decl_specifiers,
12805 is_initialized, attributes, prefix_attributes,
12809 /* Enter the SCOPE. That way unqualified names appearing in the
12810 initializer will be looked up in SCOPE. */
12811 pushed_scope = push_scope (scope);
12813 /* Perform deferred access control checks, now that we know in which
12814 SCOPE the declared entity resides. */
12815 if (!member_p && decl)
12817 tree saved_current_function_decl = NULL_TREE;
12819 /* If the entity being declared is a function, pretend that we
12820 are in its scope. If it is a `friend', it may have access to
12821 things that would not otherwise be accessible. */
12822 if (TREE_CODE (decl) == FUNCTION_DECL)
12824 saved_current_function_decl = current_function_decl;
12825 current_function_decl = decl;
12828 /* Perform access checks for template parameters. */
12829 cp_parser_perform_template_parameter_access_checks (checks);
12831 /* Perform the access control checks for the declarator and the
12832 decl-specifiers. */
12833 perform_deferred_access_checks ();
12835 /* Restore the saved value. */
12836 if (TREE_CODE (decl) == FUNCTION_DECL)
12837 current_function_decl = saved_current_function_decl;
12840 /* Parse the initializer. */
12841 initializer = NULL_TREE;
12842 is_direct_init = false;
12843 is_non_constant_init = true;
12844 if (is_initialized)
12846 if (function_declarator_p (declarator))
12848 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12849 if (initialization_kind == CPP_EQ)
12850 initializer = cp_parser_pure_specifier (parser);
12853 /* If the declaration was erroneous, we don't really
12854 know what the user intended, so just silently
12855 consume the initializer. */
12856 if (decl != error_mark_node)
12857 error ("%Hinitializer provided for function",
12858 &initializer_start_token->location);
12859 cp_parser_skip_to_closing_parenthesis (parser,
12860 /*recovering=*/true,
12861 /*or_comma=*/false,
12862 /*consume_paren=*/true);
12866 initializer = cp_parser_initializer (parser,
12868 &is_non_constant_init);
12871 /* The old parser allows attributes to appear after a parenthesized
12872 initializer. Mark Mitchell proposed removing this functionality
12873 on the GCC mailing lists on 2002-08-13. This parser accepts the
12874 attributes -- but ignores them. */
12875 if (cp_parser_allow_gnu_extensions_p (parser)
12876 && initialization_kind == CPP_OPEN_PAREN)
12877 if (cp_parser_attributes_opt (parser))
12878 warning (OPT_Wattributes,
12879 "attributes after parenthesized initializer ignored");
12881 /* For an in-class declaration, use `grokfield' to create the
12887 pop_scope (pushed_scope);
12888 pushed_scope = false;
12890 decl = grokfield (declarator, decl_specifiers,
12891 initializer, !is_non_constant_init,
12892 /*asmspec=*/NULL_TREE,
12893 prefix_attributes);
12894 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12895 cp_parser_save_default_args (parser, decl);
12898 /* Finish processing the declaration. But, skip friend
12900 if (!friend_p && decl && decl != error_mark_node)
12902 cp_finish_decl (decl,
12903 initializer, !is_non_constant_init,
12905 /* If the initializer is in parentheses, then this is
12906 a direct-initialization, which means that an
12907 `explicit' constructor is OK. Otherwise, an
12908 `explicit' constructor cannot be used. */
12909 ((is_direct_init || !is_initialized)
12910 ? 0 : LOOKUP_ONLYCONVERTING));
12912 else if ((cxx_dialect != cxx98) && friend_p
12913 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12914 /* Core issue #226 (C++0x only): A default template-argument
12915 shall not be specified in a friend class template
12917 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12918 /*is_partial=*/0, /*is_friend_decl=*/1);
12920 if (!friend_p && pushed_scope)
12921 pop_scope (pushed_scope);
12926 /* Parse a declarator.
12930 ptr-operator declarator
12932 abstract-declarator:
12933 ptr-operator abstract-declarator [opt]
12934 direct-abstract-declarator
12939 attributes [opt] direct-declarator
12940 attributes [opt] ptr-operator declarator
12942 abstract-declarator:
12943 attributes [opt] ptr-operator abstract-declarator [opt]
12944 attributes [opt] direct-abstract-declarator
12946 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12947 detect constructor, destructor or conversion operators. It is set
12948 to -1 if the declarator is a name, and +1 if it is a
12949 function. Otherwise it is set to zero. Usually you just want to
12950 test for >0, but internally the negative value is used.
12952 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12953 a decl-specifier-seq unless it declares a constructor, destructor,
12954 or conversion. It might seem that we could check this condition in
12955 semantic analysis, rather than parsing, but that makes it difficult
12956 to handle something like `f()'. We want to notice that there are
12957 no decl-specifiers, and therefore realize that this is an
12958 expression, not a declaration.)
12960 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12961 the declarator is a direct-declarator of the form "(...)".
12963 MEMBER_P is true iff this declarator is a member-declarator. */
12965 static cp_declarator *
12966 cp_parser_declarator (cp_parser* parser,
12967 cp_parser_declarator_kind dcl_kind,
12968 int* ctor_dtor_or_conv_p,
12969 bool* parenthesized_p,
12973 cp_declarator *declarator;
12974 enum tree_code code;
12975 cp_cv_quals cv_quals;
12977 tree attributes = NULL_TREE;
12979 /* Assume this is not a constructor, destructor, or type-conversion
12981 if (ctor_dtor_or_conv_p)
12982 *ctor_dtor_or_conv_p = 0;
12984 if (cp_parser_allow_gnu_extensions_p (parser))
12985 attributes = cp_parser_attributes_opt (parser);
12987 /* Peek at the next token. */
12988 token = cp_lexer_peek_token (parser->lexer);
12990 /* Check for the ptr-operator production. */
12991 cp_parser_parse_tentatively (parser);
12992 /* Parse the ptr-operator. */
12993 code = cp_parser_ptr_operator (parser,
12996 /* If that worked, then we have a ptr-operator. */
12997 if (cp_parser_parse_definitely (parser))
12999 /* If a ptr-operator was found, then this declarator was not
13001 if (parenthesized_p)
13002 *parenthesized_p = true;
13003 /* The dependent declarator is optional if we are parsing an
13004 abstract-declarator. */
13005 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13006 cp_parser_parse_tentatively (parser);
13008 /* Parse the dependent declarator. */
13009 declarator = cp_parser_declarator (parser, dcl_kind,
13010 /*ctor_dtor_or_conv_p=*/NULL,
13011 /*parenthesized_p=*/NULL,
13012 /*member_p=*/false);
13014 /* If we are parsing an abstract-declarator, we must handle the
13015 case where the dependent declarator is absent. */
13016 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13017 && !cp_parser_parse_definitely (parser))
13020 declarator = cp_parser_make_indirect_declarator
13021 (code, class_type, cv_quals, declarator);
13023 /* Everything else is a direct-declarator. */
13026 if (parenthesized_p)
13027 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13029 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13030 ctor_dtor_or_conv_p,
13034 if (attributes && declarator && declarator != cp_error_declarator)
13035 declarator->attributes = attributes;
13040 /* Parse a direct-declarator or direct-abstract-declarator.
13044 direct-declarator ( parameter-declaration-clause )
13045 cv-qualifier-seq [opt]
13046 exception-specification [opt]
13047 direct-declarator [ constant-expression [opt] ]
13050 direct-abstract-declarator:
13051 direct-abstract-declarator [opt]
13052 ( parameter-declaration-clause )
13053 cv-qualifier-seq [opt]
13054 exception-specification [opt]
13055 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13056 ( abstract-declarator )
13058 Returns a representation of the declarator. DCL_KIND is
13059 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13060 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13061 we are parsing a direct-declarator. It is
13062 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13063 of ambiguity we prefer an abstract declarator, as per
13064 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13065 cp_parser_declarator. */
13067 static cp_declarator *
13068 cp_parser_direct_declarator (cp_parser* parser,
13069 cp_parser_declarator_kind dcl_kind,
13070 int* ctor_dtor_or_conv_p,
13074 cp_declarator *declarator = NULL;
13075 tree scope = NULL_TREE;
13076 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13077 bool saved_in_declarator_p = parser->in_declarator_p;
13079 tree pushed_scope = NULL_TREE;
13083 /* Peek at the next token. */
13084 token = cp_lexer_peek_token (parser->lexer);
13085 if (token->type == CPP_OPEN_PAREN)
13087 /* This is either a parameter-declaration-clause, or a
13088 parenthesized declarator. When we know we are parsing a
13089 named declarator, it must be a parenthesized declarator
13090 if FIRST is true. For instance, `(int)' is a
13091 parameter-declaration-clause, with an omitted
13092 direct-abstract-declarator. But `((*))', is a
13093 parenthesized abstract declarator. Finally, when T is a
13094 template parameter `(T)' is a
13095 parameter-declaration-clause, and not a parenthesized
13098 We first try and parse a parameter-declaration-clause,
13099 and then try a nested declarator (if FIRST is true).
13101 It is not an error for it not to be a
13102 parameter-declaration-clause, even when FIRST is
13108 The first is the declaration of a function while the
13109 second is the definition of a variable, including its
13112 Having seen only the parenthesis, we cannot know which of
13113 these two alternatives should be selected. Even more
13114 complex are examples like:
13119 The former is a function-declaration; the latter is a
13120 variable initialization.
13122 Thus again, we try a parameter-declaration-clause, and if
13123 that fails, we back out and return. */
13125 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13128 unsigned saved_num_template_parameter_lists;
13129 bool is_declarator = false;
13132 /* In a member-declarator, the only valid interpretation
13133 of a parenthesis is the start of a
13134 parameter-declaration-clause. (It is invalid to
13135 initialize a static data member with a parenthesized
13136 initializer; only the "=" form of initialization is
13139 cp_parser_parse_tentatively (parser);
13141 /* Consume the `('. */
13142 cp_lexer_consume_token (parser->lexer);
13145 /* If this is going to be an abstract declarator, we're
13146 in a declarator and we can't have default args. */
13147 parser->default_arg_ok_p = false;
13148 parser->in_declarator_p = true;
13151 /* Inside the function parameter list, surrounding
13152 template-parameter-lists do not apply. */
13153 saved_num_template_parameter_lists
13154 = parser->num_template_parameter_lists;
13155 parser->num_template_parameter_lists = 0;
13157 begin_scope (sk_function_parms, NULL_TREE);
13159 /* Parse the parameter-declaration-clause. */
13160 params = cp_parser_parameter_declaration_clause (parser);
13162 parser->num_template_parameter_lists
13163 = saved_num_template_parameter_lists;
13165 /* If all went well, parse the cv-qualifier-seq and the
13166 exception-specification. */
13167 if (member_p || cp_parser_parse_definitely (parser))
13169 cp_cv_quals cv_quals;
13170 tree exception_specification;
13173 is_declarator = true;
13175 if (ctor_dtor_or_conv_p)
13176 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13178 /* Consume the `)'. */
13179 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13181 /* Parse the cv-qualifier-seq. */
13182 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13183 /* And the exception-specification. */
13184 exception_specification
13185 = cp_parser_exception_specification_opt (parser);
13188 = cp_parser_late_return_type_opt (parser);
13190 /* Create the function-declarator. */
13191 declarator = make_call_declarator (declarator,
13194 exception_specification,
13196 /* Any subsequent parameter lists are to do with
13197 return type, so are not those of the declared
13199 parser->default_arg_ok_p = false;
13202 /* Remove the function parms from scope. */
13203 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13204 pop_binding (DECL_NAME (t), t);
13208 /* Repeat the main loop. */
13212 /* If this is the first, we can try a parenthesized
13216 bool saved_in_type_id_in_expr_p;
13218 parser->default_arg_ok_p = saved_default_arg_ok_p;
13219 parser->in_declarator_p = saved_in_declarator_p;
13221 /* Consume the `('. */
13222 cp_lexer_consume_token (parser->lexer);
13223 /* Parse the nested declarator. */
13224 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13225 parser->in_type_id_in_expr_p = true;
13227 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13228 /*parenthesized_p=*/NULL,
13230 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13232 /* Expect a `)'. */
13233 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13234 declarator = cp_error_declarator;
13235 if (declarator == cp_error_declarator)
13238 goto handle_declarator;
13240 /* Otherwise, we must be done. */
13244 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13245 && token->type == CPP_OPEN_SQUARE)
13247 /* Parse an array-declarator. */
13250 if (ctor_dtor_or_conv_p)
13251 *ctor_dtor_or_conv_p = 0;
13254 parser->default_arg_ok_p = false;
13255 parser->in_declarator_p = true;
13256 /* Consume the `['. */
13257 cp_lexer_consume_token (parser->lexer);
13258 /* Peek at the next token. */
13259 token = cp_lexer_peek_token (parser->lexer);
13260 /* If the next token is `]', then there is no
13261 constant-expression. */
13262 if (token->type != CPP_CLOSE_SQUARE)
13264 bool non_constant_p;
13267 = cp_parser_constant_expression (parser,
13268 /*allow_non_constant=*/true,
13270 if (!non_constant_p)
13271 bounds = fold_non_dependent_expr (bounds);
13272 /* Normally, the array bound must be an integral constant
13273 expression. However, as an extension, we allow VLAs
13274 in function scopes. */
13275 else if (!parser->in_function_body)
13277 error ("%Harray bound is not an integer constant",
13279 bounds = error_mark_node;
13283 bounds = NULL_TREE;
13284 /* Look for the closing `]'. */
13285 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13287 declarator = cp_error_declarator;
13291 declarator = make_array_declarator (declarator, bounds);
13293 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13295 tree qualifying_scope;
13296 tree unqualified_name;
13297 special_function_kind sfk;
13299 bool pack_expansion_p = false;
13300 cp_token *declarator_id_start_token;
13302 /* Parse a declarator-id */
13303 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13306 cp_parser_parse_tentatively (parser);
13308 /* If we see an ellipsis, we should be looking at a
13310 if (token->type == CPP_ELLIPSIS)
13312 /* Consume the `...' */
13313 cp_lexer_consume_token (parser->lexer);
13315 pack_expansion_p = true;
13319 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13321 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13322 qualifying_scope = parser->scope;
13327 if (!unqualified_name && pack_expansion_p)
13329 /* Check whether an error occurred. */
13330 okay = !cp_parser_error_occurred (parser);
13332 /* We already consumed the ellipsis to mark a
13333 parameter pack, but we have no way to report it,
13334 so abort the tentative parse. We will be exiting
13335 immediately anyway. */
13336 cp_parser_abort_tentative_parse (parser);
13339 okay = cp_parser_parse_definitely (parser);
13342 unqualified_name = error_mark_node;
13343 else if (unqualified_name
13344 && (qualifying_scope
13345 || (TREE_CODE (unqualified_name)
13346 != IDENTIFIER_NODE)))
13348 cp_parser_error (parser, "expected unqualified-id");
13349 unqualified_name = error_mark_node;
13353 if (!unqualified_name)
13355 if (unqualified_name == error_mark_node)
13357 declarator = cp_error_declarator;
13358 pack_expansion_p = false;
13359 declarator->parameter_pack_p = false;
13363 if (qualifying_scope && at_namespace_scope_p ()
13364 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13366 /* In the declaration of a member of a template class
13367 outside of the class itself, the SCOPE will sometimes
13368 be a TYPENAME_TYPE. For example, given:
13370 template <typename T>
13371 int S<T>::R::i = 3;
13373 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13374 this context, we must resolve S<T>::R to an ordinary
13375 type, rather than a typename type.
13377 The reason we normally avoid resolving TYPENAME_TYPEs
13378 is that a specialization of `S' might render
13379 `S<T>::R' not a type. However, if `S' is
13380 specialized, then this `i' will not be used, so there
13381 is no harm in resolving the types here. */
13384 /* Resolve the TYPENAME_TYPE. */
13385 type = resolve_typename_type (qualifying_scope,
13386 /*only_current_p=*/false);
13387 /* If that failed, the declarator is invalid. */
13388 if (TREE_CODE (type) == TYPENAME_TYPE)
13389 error ("%H%<%T::%E%> is not a type",
13390 &declarator_id_start_token->location,
13391 TYPE_CONTEXT (qualifying_scope),
13392 TYPE_IDENTIFIER (qualifying_scope));
13393 qualifying_scope = type;
13398 if (unqualified_name)
13402 if (qualifying_scope
13403 && CLASS_TYPE_P (qualifying_scope))
13404 class_type = qualifying_scope;
13406 class_type = current_class_type;
13408 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13410 tree name_type = TREE_TYPE (unqualified_name);
13411 if (class_type && same_type_p (name_type, class_type))
13413 if (qualifying_scope
13414 && CLASSTYPE_USE_TEMPLATE (name_type))
13416 error ("%Hinvalid use of constructor as a template",
13417 &declarator_id_start_token->location);
13418 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13419 "name the constructor in a qualified name",
13421 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13422 class_type, name_type);
13423 declarator = cp_error_declarator;
13427 unqualified_name = constructor_name (class_type);
13431 /* We do not attempt to print the declarator
13432 here because we do not have enough
13433 information about its original syntactic
13435 cp_parser_error (parser, "invalid declarator");
13436 declarator = cp_error_declarator;
13443 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13444 sfk = sfk_destructor;
13445 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13446 sfk = sfk_conversion;
13447 else if (/* There's no way to declare a constructor
13448 for an anonymous type, even if the type
13449 got a name for linkage purposes. */
13450 !TYPE_WAS_ANONYMOUS (class_type)
13451 && constructor_name_p (unqualified_name,
13454 unqualified_name = constructor_name (class_type);
13455 sfk = sfk_constructor;
13458 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13459 *ctor_dtor_or_conv_p = -1;
13462 declarator = make_id_declarator (qualifying_scope,
13465 declarator->id_loc = token->location;
13466 declarator->parameter_pack_p = pack_expansion_p;
13468 if (pack_expansion_p)
13469 maybe_warn_variadic_templates ();
13471 handle_declarator:;
13472 scope = get_scope_of_declarator (declarator);
13474 /* Any names that appear after the declarator-id for a
13475 member are looked up in the containing scope. */
13476 pushed_scope = push_scope (scope);
13477 parser->in_declarator_p = true;
13478 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13479 || (declarator && declarator->kind == cdk_id))
13480 /* Default args are only allowed on function
13482 parser->default_arg_ok_p = saved_default_arg_ok_p;
13484 parser->default_arg_ok_p = false;
13493 /* For an abstract declarator, we might wind up with nothing at this
13494 point. That's an error; the declarator is not optional. */
13496 cp_parser_error (parser, "expected declarator");
13498 /* If we entered a scope, we must exit it now. */
13500 pop_scope (pushed_scope);
13502 parser->default_arg_ok_p = saved_default_arg_ok_p;
13503 parser->in_declarator_p = saved_in_declarator_p;
13508 /* Parse a ptr-operator.
13511 * cv-qualifier-seq [opt]
13513 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13518 & cv-qualifier-seq [opt]
13520 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13521 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13522 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13523 filled in with the TYPE containing the member. *CV_QUALS is
13524 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13525 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13526 Note that the tree codes returned by this function have nothing
13527 to do with the types of trees that will be eventually be created
13528 to represent the pointer or reference type being parsed. They are
13529 just constants with suggestive names. */
13530 static enum tree_code
13531 cp_parser_ptr_operator (cp_parser* parser,
13533 cp_cv_quals *cv_quals)
13535 enum tree_code code = ERROR_MARK;
13538 /* Assume that it's not a pointer-to-member. */
13540 /* And that there are no cv-qualifiers. */
13541 *cv_quals = TYPE_UNQUALIFIED;
13543 /* Peek at the next token. */
13544 token = cp_lexer_peek_token (parser->lexer);
13546 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13547 if (token->type == CPP_MULT)
13548 code = INDIRECT_REF;
13549 else if (token->type == CPP_AND)
13551 else if ((cxx_dialect != cxx98) &&
13552 token->type == CPP_AND_AND) /* C++0x only */
13553 code = NON_LVALUE_EXPR;
13555 if (code != ERROR_MARK)
13557 /* Consume the `*', `&' or `&&'. */
13558 cp_lexer_consume_token (parser->lexer);
13560 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13561 `&', if we are allowing GNU extensions. (The only qualifier
13562 that can legally appear after `&' is `restrict', but that is
13563 enforced during semantic analysis. */
13564 if (code == INDIRECT_REF
13565 || cp_parser_allow_gnu_extensions_p (parser))
13566 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13570 /* Try the pointer-to-member case. */
13571 cp_parser_parse_tentatively (parser);
13572 /* Look for the optional `::' operator. */
13573 cp_parser_global_scope_opt (parser,
13574 /*current_scope_valid_p=*/false);
13575 /* Look for the nested-name specifier. */
13576 token = cp_lexer_peek_token (parser->lexer);
13577 cp_parser_nested_name_specifier (parser,
13578 /*typename_keyword_p=*/false,
13579 /*check_dependency_p=*/true,
13581 /*is_declaration=*/false);
13582 /* If we found it, and the next token is a `*', then we are
13583 indeed looking at a pointer-to-member operator. */
13584 if (!cp_parser_error_occurred (parser)
13585 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13587 /* Indicate that the `*' operator was used. */
13588 code = INDIRECT_REF;
13590 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13591 error ("%H%qD is a namespace", &token->location, parser->scope);
13594 /* The type of which the member is a member is given by the
13596 *type = parser->scope;
13597 /* The next name will not be qualified. */
13598 parser->scope = NULL_TREE;
13599 parser->qualifying_scope = NULL_TREE;
13600 parser->object_scope = NULL_TREE;
13601 /* Look for the optional cv-qualifier-seq. */
13602 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13605 /* If that didn't work we don't have a ptr-operator. */
13606 if (!cp_parser_parse_definitely (parser))
13607 cp_parser_error (parser, "expected ptr-operator");
13613 /* Parse an (optional) cv-qualifier-seq.
13616 cv-qualifier cv-qualifier-seq [opt]
13627 Returns a bitmask representing the cv-qualifiers. */
13630 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13632 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13637 cp_cv_quals cv_qualifier;
13639 /* Peek at the next token. */
13640 token = cp_lexer_peek_token (parser->lexer);
13641 /* See if it's a cv-qualifier. */
13642 switch (token->keyword)
13645 cv_qualifier = TYPE_QUAL_CONST;
13649 cv_qualifier = TYPE_QUAL_VOLATILE;
13653 cv_qualifier = TYPE_QUAL_RESTRICT;
13657 cv_qualifier = TYPE_UNQUALIFIED;
13664 if (cv_quals & cv_qualifier)
13666 error ("%Hduplicate cv-qualifier", &token->location);
13667 cp_lexer_purge_token (parser->lexer);
13671 cp_lexer_consume_token (parser->lexer);
13672 cv_quals |= cv_qualifier;
13679 /* Parse a late-specified return type, if any. This is not a separate
13680 non-terminal, but part of a function declarator, which looks like
13684 Returns the type indicated by the type-id. */
13687 cp_parser_late_return_type_opt (cp_parser* parser)
13691 /* Peek at the next token. */
13692 token = cp_lexer_peek_token (parser->lexer);
13693 /* A late-specified return type is indicated by an initial '->'. */
13694 if (token->type != CPP_DEREF)
13697 /* Consume the ->. */
13698 cp_lexer_consume_token (parser->lexer);
13700 return cp_parser_type_id (parser);
13703 /* Parse a declarator-id.
13707 :: [opt] nested-name-specifier [opt] type-name
13709 In the `id-expression' case, the value returned is as for
13710 cp_parser_id_expression if the id-expression was an unqualified-id.
13711 If the id-expression was a qualified-id, then a SCOPE_REF is
13712 returned. The first operand is the scope (either a NAMESPACE_DECL
13713 or TREE_TYPE), but the second is still just a representation of an
13717 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13720 /* The expression must be an id-expression. Assume that qualified
13721 names are the names of types so that:
13724 int S<T>::R::i = 3;
13726 will work; we must treat `S<T>::R' as the name of a type.
13727 Similarly, assume that qualified names are templates, where
13731 int S<T>::R<T>::i = 3;
13734 id = cp_parser_id_expression (parser,
13735 /*template_keyword_p=*/false,
13736 /*check_dependency_p=*/false,
13737 /*template_p=*/NULL,
13738 /*declarator_p=*/true,
13740 if (id && BASELINK_P (id))
13741 id = BASELINK_FUNCTIONS (id);
13745 /* Parse a type-id.
13748 type-specifier-seq abstract-declarator [opt]
13750 Returns the TYPE specified. */
13753 cp_parser_type_id (cp_parser* parser)
13755 cp_decl_specifier_seq type_specifier_seq;
13756 cp_declarator *abstract_declarator;
13758 /* Parse the type-specifier-seq. */
13759 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13760 &type_specifier_seq);
13761 if (type_specifier_seq.type == error_mark_node)
13762 return error_mark_node;
13764 /* There might or might not be an abstract declarator. */
13765 cp_parser_parse_tentatively (parser);
13766 /* Look for the declarator. */
13767 abstract_declarator
13768 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13769 /*parenthesized_p=*/NULL,
13770 /*member_p=*/false);
13771 /* Check to see if there really was a declarator. */
13772 if (!cp_parser_parse_definitely (parser))
13773 abstract_declarator = NULL;
13775 if (type_specifier_seq.type
13776 && type_uses_auto (type_specifier_seq.type))
13778 error ("invalid use of %<auto%>");
13779 return error_mark_node;
13782 return groktypename (&type_specifier_seq, abstract_declarator);
13785 /* Parse a type-specifier-seq.
13787 type-specifier-seq:
13788 type-specifier type-specifier-seq [opt]
13792 type-specifier-seq:
13793 attributes type-specifier-seq [opt]
13795 If IS_CONDITION is true, we are at the start of a "condition",
13796 e.g., we've just seen "if (".
13798 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13801 cp_parser_type_specifier_seq (cp_parser* parser,
13803 cp_decl_specifier_seq *type_specifier_seq)
13805 bool seen_type_specifier = false;
13806 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13807 cp_token *start_token = NULL;
13809 /* Clear the TYPE_SPECIFIER_SEQ. */
13810 clear_decl_specs (type_specifier_seq);
13812 /* Parse the type-specifiers and attributes. */
13815 tree type_specifier;
13816 bool is_cv_qualifier;
13818 /* Check for attributes first. */
13819 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13821 type_specifier_seq->attributes =
13822 chainon (type_specifier_seq->attributes,
13823 cp_parser_attributes_opt (parser));
13827 /* record the token of the beginning of the type specifier seq,
13828 for error reporting purposes*/
13830 start_token = cp_lexer_peek_token (parser->lexer);
13832 /* Look for the type-specifier. */
13833 type_specifier = cp_parser_type_specifier (parser,
13835 type_specifier_seq,
13836 /*is_declaration=*/false,
13839 if (!type_specifier)
13841 /* If the first type-specifier could not be found, this is not a
13842 type-specifier-seq at all. */
13843 if (!seen_type_specifier)
13845 cp_parser_error (parser, "expected type-specifier");
13846 type_specifier_seq->type = error_mark_node;
13849 /* If subsequent type-specifiers could not be found, the
13850 type-specifier-seq is complete. */
13854 seen_type_specifier = true;
13855 /* The standard says that a condition can be:
13857 type-specifier-seq declarator = assignment-expression
13864 we should treat the "S" as a declarator, not as a
13865 type-specifier. The standard doesn't say that explicitly for
13866 type-specifier-seq, but it does say that for
13867 decl-specifier-seq in an ordinary declaration. Perhaps it
13868 would be clearer just to allow a decl-specifier-seq here, and
13869 then add a semantic restriction that if any decl-specifiers
13870 that are not type-specifiers appear, the program is invalid. */
13871 if (is_condition && !is_cv_qualifier)
13872 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13875 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13878 /* Parse a parameter-declaration-clause.
13880 parameter-declaration-clause:
13881 parameter-declaration-list [opt] ... [opt]
13882 parameter-declaration-list , ...
13884 Returns a representation for the parameter declarations. A return
13885 value of NULL indicates a parameter-declaration-clause consisting
13886 only of an ellipsis. */
13889 cp_parser_parameter_declaration_clause (cp_parser* parser)
13896 /* Peek at the next token. */
13897 token = cp_lexer_peek_token (parser->lexer);
13898 /* Check for trivial parameter-declaration-clauses. */
13899 if (token->type == CPP_ELLIPSIS)
13901 /* Consume the `...' token. */
13902 cp_lexer_consume_token (parser->lexer);
13905 else if (token->type == CPP_CLOSE_PAREN)
13906 /* There are no parameters. */
13908 #ifndef NO_IMPLICIT_EXTERN_C
13909 if (in_system_header && current_class_type == NULL
13910 && current_lang_name == lang_name_c)
13914 return void_list_node;
13916 /* Check for `(void)', too, which is a special case. */
13917 else if (token->keyword == RID_VOID
13918 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13919 == CPP_CLOSE_PAREN))
13921 /* Consume the `void' token. */
13922 cp_lexer_consume_token (parser->lexer);
13923 /* There are no parameters. */
13924 return void_list_node;
13927 /* Parse the parameter-declaration-list. */
13928 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13929 /* If a parse error occurred while parsing the
13930 parameter-declaration-list, then the entire
13931 parameter-declaration-clause is erroneous. */
13935 /* Peek at the next token. */
13936 token = cp_lexer_peek_token (parser->lexer);
13937 /* If it's a `,', the clause should terminate with an ellipsis. */
13938 if (token->type == CPP_COMMA)
13940 /* Consume the `,'. */
13941 cp_lexer_consume_token (parser->lexer);
13942 /* Expect an ellipsis. */
13944 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13946 /* It might also be `...' if the optional trailing `,' was
13948 else if (token->type == CPP_ELLIPSIS)
13950 /* Consume the `...' token. */
13951 cp_lexer_consume_token (parser->lexer);
13952 /* And remember that we saw it. */
13956 ellipsis_p = false;
13958 /* Finish the parameter list. */
13960 parameters = chainon (parameters, void_list_node);
13965 /* Parse a parameter-declaration-list.
13967 parameter-declaration-list:
13968 parameter-declaration
13969 parameter-declaration-list , parameter-declaration
13971 Returns a representation of the parameter-declaration-list, as for
13972 cp_parser_parameter_declaration_clause. However, the
13973 `void_list_node' is never appended to the list. Upon return,
13974 *IS_ERROR will be true iff an error occurred. */
13977 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13979 tree parameters = NULL_TREE;
13980 tree *tail = ¶meters;
13981 bool saved_in_unbraced_linkage_specification_p;
13983 /* Assume all will go well. */
13985 /* The special considerations that apply to a function within an
13986 unbraced linkage specifications do not apply to the parameters
13987 to the function. */
13988 saved_in_unbraced_linkage_specification_p
13989 = parser->in_unbraced_linkage_specification_p;
13990 parser->in_unbraced_linkage_specification_p = false;
13992 /* Look for more parameters. */
13995 cp_parameter_declarator *parameter;
13996 tree decl = error_mark_node;
13997 bool parenthesized_p;
13998 /* Parse the parameter. */
14000 = cp_parser_parameter_declaration (parser,
14001 /*template_parm_p=*/false,
14004 /* We don't know yet if the enclosing context is deprecated, so wait
14005 and warn in grokparms if appropriate. */
14006 deprecated_state = DEPRECATED_SUPPRESS;
14009 decl = grokdeclarator (parameter->declarator,
14010 ¶meter->decl_specifiers,
14012 parameter->default_argument != NULL_TREE,
14013 ¶meter->decl_specifiers.attributes);
14015 deprecated_state = DEPRECATED_NORMAL;
14017 /* If a parse error occurred parsing the parameter declaration,
14018 then the entire parameter-declaration-list is erroneous. */
14019 if (decl == error_mark_node)
14022 parameters = error_mark_node;
14026 if (parameter->decl_specifiers.attributes)
14027 cplus_decl_attributes (&decl,
14028 parameter->decl_specifiers.attributes,
14030 if (DECL_NAME (decl))
14031 decl = pushdecl (decl);
14033 /* Add the new parameter to the list. */
14034 *tail = build_tree_list (parameter->default_argument, decl);
14035 tail = &TREE_CHAIN (*tail);
14037 /* Peek at the next token. */
14038 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14039 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14040 /* These are for Objective-C++ */
14041 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14042 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14043 /* The parameter-declaration-list is complete. */
14045 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14049 /* Peek at the next token. */
14050 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14051 /* If it's an ellipsis, then the list is complete. */
14052 if (token->type == CPP_ELLIPSIS)
14054 /* Otherwise, there must be more parameters. Consume the
14056 cp_lexer_consume_token (parser->lexer);
14057 /* When parsing something like:
14059 int i(float f, double d)
14061 we can tell after seeing the declaration for "f" that we
14062 are not looking at an initialization of a variable "i",
14063 but rather at the declaration of a function "i".
14065 Due to the fact that the parsing of template arguments
14066 (as specified to a template-id) requires backtracking we
14067 cannot use this technique when inside a template argument
14069 if (!parser->in_template_argument_list_p
14070 && !parser->in_type_id_in_expr_p
14071 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14072 /* However, a parameter-declaration of the form
14073 "foat(f)" (which is a valid declaration of a
14074 parameter "f") can also be interpreted as an
14075 expression (the conversion of "f" to "float"). */
14076 && !parenthesized_p)
14077 cp_parser_commit_to_tentative_parse (parser);
14081 cp_parser_error (parser, "expected %<,%> or %<...%>");
14082 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14083 cp_parser_skip_to_closing_parenthesis (parser,
14084 /*recovering=*/true,
14085 /*or_comma=*/false,
14086 /*consume_paren=*/false);
14091 parser->in_unbraced_linkage_specification_p
14092 = saved_in_unbraced_linkage_specification_p;
14097 /* Parse a parameter declaration.
14099 parameter-declaration:
14100 decl-specifier-seq ... [opt] declarator
14101 decl-specifier-seq declarator = assignment-expression
14102 decl-specifier-seq ... [opt] abstract-declarator [opt]
14103 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14105 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14106 declares a template parameter. (In that case, a non-nested `>'
14107 token encountered during the parsing of the assignment-expression
14108 is not interpreted as a greater-than operator.)
14110 Returns a representation of the parameter, or NULL if an error
14111 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14112 true iff the declarator is of the form "(p)". */
14114 static cp_parameter_declarator *
14115 cp_parser_parameter_declaration (cp_parser *parser,
14116 bool template_parm_p,
14117 bool *parenthesized_p)
14119 int declares_class_or_enum;
14120 bool greater_than_is_operator_p;
14121 cp_decl_specifier_seq decl_specifiers;
14122 cp_declarator *declarator;
14123 tree default_argument;
14124 cp_token *token = NULL, *declarator_token_start = NULL;
14125 const char *saved_message;
14127 /* In a template parameter, `>' is not an operator.
14131 When parsing a default template-argument for a non-type
14132 template-parameter, the first non-nested `>' is taken as the end
14133 of the template parameter-list rather than a greater-than
14135 greater_than_is_operator_p = !template_parm_p;
14137 /* Type definitions may not appear in parameter types. */
14138 saved_message = parser->type_definition_forbidden_message;
14139 parser->type_definition_forbidden_message
14140 = "types may not be defined in parameter types";
14142 /* Parse the declaration-specifiers. */
14143 cp_parser_decl_specifier_seq (parser,
14144 CP_PARSER_FLAGS_NONE,
14146 &declares_class_or_enum);
14147 /* If an error occurred, there's no reason to attempt to parse the
14148 rest of the declaration. */
14149 if (cp_parser_error_occurred (parser))
14151 parser->type_definition_forbidden_message = saved_message;
14155 /* Peek at the next token. */
14156 token = cp_lexer_peek_token (parser->lexer);
14158 /* If the next token is a `)', `,', `=', `>', or `...', then there
14159 is no declarator. However, when variadic templates are enabled,
14160 there may be a declarator following `...'. */
14161 if (token->type == CPP_CLOSE_PAREN
14162 || token->type == CPP_COMMA
14163 || token->type == CPP_EQ
14164 || token->type == CPP_GREATER)
14167 if (parenthesized_p)
14168 *parenthesized_p = false;
14170 /* Otherwise, there should be a declarator. */
14173 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14174 parser->default_arg_ok_p = false;
14176 /* After seeing a decl-specifier-seq, if the next token is not a
14177 "(", there is no possibility that the code is a valid
14178 expression. Therefore, if parsing tentatively, we commit at
14180 if (!parser->in_template_argument_list_p
14181 /* In an expression context, having seen:
14185 we cannot be sure whether we are looking at a
14186 function-type (taking a "char" as a parameter) or a cast
14187 of some object of type "char" to "int". */
14188 && !parser->in_type_id_in_expr_p
14189 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14190 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14191 cp_parser_commit_to_tentative_parse (parser);
14192 /* Parse the declarator. */
14193 declarator_token_start = token;
14194 declarator = cp_parser_declarator (parser,
14195 CP_PARSER_DECLARATOR_EITHER,
14196 /*ctor_dtor_or_conv_p=*/NULL,
14198 /*member_p=*/false);
14199 parser->default_arg_ok_p = saved_default_arg_ok_p;
14200 /* After the declarator, allow more attributes. */
14201 decl_specifiers.attributes
14202 = chainon (decl_specifiers.attributes,
14203 cp_parser_attributes_opt (parser));
14206 /* If the next token is an ellipsis, and we have not seen a
14207 declarator name, and the type of the declarator contains parameter
14208 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14209 a parameter pack expansion expression. Otherwise, leave the
14210 ellipsis for a C-style variadic function. */
14211 token = cp_lexer_peek_token (parser->lexer);
14212 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14214 tree type = decl_specifiers.type;
14216 if (type && DECL_P (type))
14217 type = TREE_TYPE (type);
14220 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14221 && declarator_can_be_parameter_pack (declarator)
14222 && (!declarator || !declarator->parameter_pack_p)
14223 && uses_parameter_packs (type))
14225 /* Consume the `...'. */
14226 cp_lexer_consume_token (parser->lexer);
14227 maybe_warn_variadic_templates ();
14229 /* Build a pack expansion type */
14231 declarator->parameter_pack_p = true;
14233 decl_specifiers.type = make_pack_expansion (type);
14237 /* The restriction on defining new types applies only to the type
14238 of the parameter, not to the default argument. */
14239 parser->type_definition_forbidden_message = saved_message;
14241 /* If the next token is `=', then process a default argument. */
14242 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14244 /* Consume the `='. */
14245 cp_lexer_consume_token (parser->lexer);
14247 /* If we are defining a class, then the tokens that make up the
14248 default argument must be saved and processed later. */
14249 if (!template_parm_p && at_class_scope_p ()
14250 && TYPE_BEING_DEFINED (current_class_type))
14252 unsigned depth = 0;
14253 int maybe_template_id = 0;
14254 cp_token *first_token;
14257 /* Add tokens until we have processed the entire default
14258 argument. We add the range [first_token, token). */
14259 first_token = cp_lexer_peek_token (parser->lexer);
14264 /* Peek at the next token. */
14265 token = cp_lexer_peek_token (parser->lexer);
14266 /* What we do depends on what token we have. */
14267 switch (token->type)
14269 /* In valid code, a default argument must be
14270 immediately followed by a `,' `)', or `...'. */
14272 if (depth == 0 && maybe_template_id)
14274 /* If we've seen a '<', we might be in a
14275 template-argument-list. Until Core issue 325 is
14276 resolved, we don't know how this situation ought
14277 to be handled, so try to DTRT. We check whether
14278 what comes after the comma is a valid parameter
14279 declaration list. If it is, then the comma ends
14280 the default argument; otherwise the default
14281 argument continues. */
14282 bool error = false;
14284 /* Set ITALP so cp_parser_parameter_declaration_list
14285 doesn't decide to commit to this parse. */
14286 bool saved_italp = parser->in_template_argument_list_p;
14287 parser->in_template_argument_list_p = true;
14289 cp_parser_parse_tentatively (parser);
14290 cp_lexer_consume_token (parser->lexer);
14291 cp_parser_parameter_declaration_list (parser, &error);
14292 if (!cp_parser_error_occurred (parser) && !error)
14294 cp_parser_abort_tentative_parse (parser);
14296 parser->in_template_argument_list_p = saved_italp;
14299 case CPP_CLOSE_PAREN:
14301 /* If we run into a non-nested `;', `}', or `]',
14302 then the code is invalid -- but the default
14303 argument is certainly over. */
14304 case CPP_SEMICOLON:
14305 case CPP_CLOSE_BRACE:
14306 case CPP_CLOSE_SQUARE:
14309 /* Update DEPTH, if necessary. */
14310 else if (token->type == CPP_CLOSE_PAREN
14311 || token->type == CPP_CLOSE_BRACE
14312 || token->type == CPP_CLOSE_SQUARE)
14316 case CPP_OPEN_PAREN:
14317 case CPP_OPEN_SQUARE:
14318 case CPP_OPEN_BRACE:
14324 /* This might be the comparison operator, or it might
14325 start a template argument list. */
14326 ++maybe_template_id;
14330 if (cxx_dialect == cxx98)
14332 /* Fall through for C++0x, which treats the `>>'
14333 operator like two `>' tokens in certain
14339 /* This might be an operator, or it might close a
14340 template argument list. But if a previous '<'
14341 started a template argument list, this will have
14342 closed it, so we can't be in one anymore. */
14343 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14344 if (maybe_template_id < 0)
14345 maybe_template_id = 0;
14349 /* If we run out of tokens, issue an error message. */
14351 case CPP_PRAGMA_EOL:
14352 error ("%Hfile ends in default argument", &token->location);
14358 /* In these cases, we should look for template-ids.
14359 For example, if the default argument is
14360 `X<int, double>()', we need to do name lookup to
14361 figure out whether or not `X' is a template; if
14362 so, the `,' does not end the default argument.
14364 That is not yet done. */
14371 /* If we've reached the end, stop. */
14375 /* Add the token to the token block. */
14376 token = cp_lexer_consume_token (parser->lexer);
14379 /* Create a DEFAULT_ARG to represent the unparsed default
14381 default_argument = make_node (DEFAULT_ARG);
14382 DEFARG_TOKENS (default_argument)
14383 = cp_token_cache_new (first_token, token);
14384 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14386 /* Outside of a class definition, we can just parse the
14387 assignment-expression. */
14390 token = cp_lexer_peek_token (parser->lexer);
14392 = cp_parser_default_argument (parser, template_parm_p);
14395 if (!parser->default_arg_ok_p)
14397 if (flag_permissive)
14398 warning (0, "deprecated use of default argument for parameter of non-function");
14401 error ("%Hdefault arguments are only "
14402 "permitted for function parameters",
14404 default_argument = NULL_TREE;
14407 else if ((declarator && declarator->parameter_pack_p)
14408 || (decl_specifiers.type
14409 && PACK_EXPANSION_P (decl_specifiers.type)))
14411 const char* kind = template_parm_p? "template " : "";
14413 /* Find the name of the parameter pack. */
14414 cp_declarator *id_declarator = declarator;
14415 while (id_declarator && id_declarator->kind != cdk_id)
14416 id_declarator = id_declarator->declarator;
14418 if (id_declarator && id_declarator->kind == cdk_id)
14419 error ("%H%sparameter pack %qD cannot have a default argument",
14420 &declarator_token_start->location,
14421 kind, id_declarator->u.id.unqualified_name);
14423 error ("%H%sparameter pack cannot have a default argument",
14424 &declarator_token_start->location, kind);
14426 default_argument = NULL_TREE;
14430 default_argument = NULL_TREE;
14432 return make_parameter_declarator (&decl_specifiers,
14437 /* Parse a default argument and return it.
14439 TEMPLATE_PARM_P is true if this is a default argument for a
14440 non-type template parameter. */
14442 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14444 tree default_argument = NULL_TREE;
14445 bool saved_greater_than_is_operator_p;
14446 bool saved_local_variables_forbidden_p;
14448 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14450 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14451 parser->greater_than_is_operator_p = !template_parm_p;
14452 /* Local variable names (and the `this' keyword) may not
14453 appear in a default argument. */
14454 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14455 parser->local_variables_forbidden_p = true;
14456 /* The default argument expression may cause implicitly
14457 defined member functions to be synthesized, which will
14458 result in garbage collection. We must treat this
14459 situation as if we were within the body of function so as
14460 to avoid collecting live data on the stack. */
14462 /* Parse the assignment-expression. */
14463 if (template_parm_p)
14464 push_deferring_access_checks (dk_no_deferred);
14466 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14467 if (template_parm_p)
14468 pop_deferring_access_checks ();
14469 /* Restore saved state. */
14471 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14472 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14474 return default_argument;
14477 /* Parse a function-body.
14480 compound_statement */
14483 cp_parser_function_body (cp_parser *parser)
14485 cp_parser_compound_statement (parser, NULL, false);
14488 /* Parse a ctor-initializer-opt followed by a function-body. Return
14489 true if a ctor-initializer was present. */
14492 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14495 bool ctor_initializer_p;
14497 /* Begin the function body. */
14498 body = begin_function_body ();
14499 /* Parse the optional ctor-initializer. */
14500 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14501 /* Parse the function-body. */
14502 cp_parser_function_body (parser);
14503 /* Finish the function body. */
14504 finish_function_body (body);
14506 return ctor_initializer_p;
14509 /* Parse an initializer.
14512 = initializer-clause
14513 ( expression-list )
14515 Returns an expression representing the initializer. If no
14516 initializer is present, NULL_TREE is returned.
14518 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14519 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14520 set to TRUE if there is no initializer present. If there is an
14521 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14522 is set to true; otherwise it is set to false. */
14525 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14526 bool* non_constant_p)
14531 /* Peek at the next token. */
14532 token = cp_lexer_peek_token (parser->lexer);
14534 /* Let our caller know whether or not this initializer was
14536 *is_direct_init = (token->type != CPP_EQ);
14537 /* Assume that the initializer is constant. */
14538 *non_constant_p = false;
14540 if (token->type == CPP_EQ)
14542 /* Consume the `='. */
14543 cp_lexer_consume_token (parser->lexer);
14544 /* Parse the initializer-clause. */
14545 init = cp_parser_initializer_clause (parser, non_constant_p);
14547 else if (token->type == CPP_OPEN_PAREN)
14548 init = cp_parser_parenthesized_expression_list (parser, false,
14550 /*allow_expansion_p=*/true,
14552 else if (token->type == CPP_OPEN_BRACE)
14554 maybe_warn_cpp0x ("extended initializer lists");
14555 init = cp_parser_braced_list (parser, non_constant_p);
14556 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14560 /* Anything else is an error. */
14561 cp_parser_error (parser, "expected initializer");
14562 init = error_mark_node;
14568 /* Parse an initializer-clause.
14570 initializer-clause:
14571 assignment-expression
14574 Returns an expression representing the initializer.
14576 If the `assignment-expression' production is used the value
14577 returned is simply a representation for the expression.
14579 Otherwise, calls cp_parser_braced_list. */
14582 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14586 /* Assume the expression is constant. */
14587 *non_constant_p = false;
14589 /* If it is not a `{', then we are looking at an
14590 assignment-expression. */
14591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14594 = cp_parser_constant_expression (parser,
14595 /*allow_non_constant_p=*/true,
14597 if (!*non_constant_p)
14598 initializer = fold_non_dependent_expr (initializer);
14601 initializer = cp_parser_braced_list (parser, non_constant_p);
14603 return initializer;
14606 /* Parse a brace-enclosed initializer list.
14609 { initializer-list , [opt] }
14612 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14613 the elements of the initializer-list (or NULL, if the last
14614 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14615 NULL_TREE. There is no way to detect whether or not the optional
14616 trailing `,' was provided. NON_CONSTANT_P is as for
14617 cp_parser_initializer. */
14620 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14624 /* Consume the `{' token. */
14625 cp_lexer_consume_token (parser->lexer);
14626 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14627 initializer = make_node (CONSTRUCTOR);
14628 /* If it's not a `}', then there is a non-trivial initializer. */
14629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14631 /* Parse the initializer list. */
14632 CONSTRUCTOR_ELTS (initializer)
14633 = cp_parser_initializer_list (parser, non_constant_p);
14634 /* A trailing `,' token is allowed. */
14635 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14636 cp_lexer_consume_token (parser->lexer);
14638 /* Now, there should be a trailing `}'. */
14639 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14640 TREE_TYPE (initializer) = init_list_type_node;
14641 return initializer;
14644 /* Parse an initializer-list.
14647 initializer-clause ... [opt]
14648 initializer-list , initializer-clause ... [opt]
14653 identifier : initializer-clause
14654 initializer-list, identifier : initializer-clause
14656 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14657 for the initializer. If the INDEX of the elt is non-NULL, it is the
14658 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14659 as for cp_parser_initializer. */
14661 static VEC(constructor_elt,gc) *
14662 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14664 VEC(constructor_elt,gc) *v = NULL;
14666 /* Assume all of the expressions are constant. */
14667 *non_constant_p = false;
14669 /* Parse the rest of the list. */
14675 bool clause_non_constant_p;
14677 /* If the next token is an identifier and the following one is a
14678 colon, we are looking at the GNU designated-initializer
14680 if (cp_parser_allow_gnu_extensions_p (parser)
14681 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14682 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14684 /* Warn the user that they are using an extension. */
14685 pedwarn (input_location, OPT_pedantic,
14686 "ISO C++ does not allow designated initializers");
14687 /* Consume the identifier. */
14688 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14689 /* Consume the `:'. */
14690 cp_lexer_consume_token (parser->lexer);
14693 identifier = NULL_TREE;
14695 /* Parse the initializer. */
14696 initializer = cp_parser_initializer_clause (parser,
14697 &clause_non_constant_p);
14698 /* If any clause is non-constant, so is the entire initializer. */
14699 if (clause_non_constant_p)
14700 *non_constant_p = true;
14702 /* If we have an ellipsis, this is an initializer pack
14704 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14706 /* Consume the `...'. */
14707 cp_lexer_consume_token (parser->lexer);
14709 /* Turn the initializer into an initializer expansion. */
14710 initializer = make_pack_expansion (initializer);
14713 /* Add it to the vector. */
14714 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14716 /* If the next token is not a comma, we have reached the end of
14718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14721 /* Peek at the next token. */
14722 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14723 /* If the next token is a `}', then we're still done. An
14724 initializer-clause can have a trailing `,' after the
14725 initializer-list and before the closing `}'. */
14726 if (token->type == CPP_CLOSE_BRACE)
14729 /* Consume the `,' token. */
14730 cp_lexer_consume_token (parser->lexer);
14736 /* Classes [gram.class] */
14738 /* Parse a class-name.
14744 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14745 to indicate that names looked up in dependent types should be
14746 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14747 keyword has been used to indicate that the name that appears next
14748 is a template. TAG_TYPE indicates the explicit tag given before
14749 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14750 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14751 is the class being defined in a class-head.
14753 Returns the TYPE_DECL representing the class. */
14756 cp_parser_class_name (cp_parser *parser,
14757 bool typename_keyword_p,
14758 bool template_keyword_p,
14759 enum tag_types tag_type,
14760 bool check_dependency_p,
14762 bool is_declaration)
14768 tree identifier = NULL_TREE;
14770 /* All class-names start with an identifier. */
14771 token = cp_lexer_peek_token (parser->lexer);
14772 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14774 cp_parser_error (parser, "expected class-name");
14775 return error_mark_node;
14778 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14779 to a template-id, so we save it here. */
14780 scope = parser->scope;
14781 if (scope == error_mark_node)
14782 return error_mark_node;
14784 /* Any name names a type if we're following the `typename' keyword
14785 in a qualified name where the enclosing scope is type-dependent. */
14786 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14787 && dependent_type_p (scope));
14788 /* Handle the common case (an identifier, but not a template-id)
14790 if (token->type == CPP_NAME
14791 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14793 cp_token *identifier_token;
14796 /* Look for the identifier. */
14797 identifier_token = cp_lexer_peek_token (parser->lexer);
14798 ambiguous_p = identifier_token->ambiguous_p;
14799 identifier = cp_parser_identifier (parser);
14800 /* If the next token isn't an identifier, we are certainly not
14801 looking at a class-name. */
14802 if (identifier == error_mark_node)
14803 decl = error_mark_node;
14804 /* If we know this is a type-name, there's no need to look it
14806 else if (typename_p)
14810 tree ambiguous_decls;
14811 /* If we already know that this lookup is ambiguous, then
14812 we've already issued an error message; there's no reason
14816 cp_parser_simulate_error (parser);
14817 return error_mark_node;
14819 /* If the next token is a `::', then the name must be a type
14822 [basic.lookup.qual]
14824 During the lookup for a name preceding the :: scope
14825 resolution operator, object, function, and enumerator
14826 names are ignored. */
14827 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14828 tag_type = typename_type;
14829 /* Look up the name. */
14830 decl = cp_parser_lookup_name (parser, identifier,
14832 /*is_template=*/false,
14833 /*is_namespace=*/false,
14834 check_dependency_p,
14836 identifier_token->location);
14837 if (ambiguous_decls)
14839 error ("%Hreference to %qD is ambiguous",
14840 &identifier_token->location, identifier);
14841 print_candidates (ambiguous_decls);
14842 if (cp_parser_parsing_tentatively (parser))
14844 identifier_token->ambiguous_p = true;
14845 cp_parser_simulate_error (parser);
14847 return error_mark_node;
14853 /* Try a template-id. */
14854 decl = cp_parser_template_id (parser, template_keyword_p,
14855 check_dependency_p,
14857 if (decl == error_mark_node)
14858 return error_mark_node;
14861 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14863 /* If this is a typename, create a TYPENAME_TYPE. */
14864 if (typename_p && decl != error_mark_node)
14866 decl = make_typename_type (scope, decl, typename_type,
14867 /*complain=*/tf_error);
14868 if (decl != error_mark_node)
14869 decl = TYPE_NAME (decl);
14872 /* Check to see that it is really the name of a class. */
14873 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14874 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14875 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14876 /* Situations like this:
14878 template <typename T> struct A {
14879 typename T::template X<int>::I i;
14882 are problematic. Is `T::template X<int>' a class-name? The
14883 standard does not seem to be definitive, but there is no other
14884 valid interpretation of the following `::'. Therefore, those
14885 names are considered class-names. */
14887 decl = make_typename_type (scope, decl, tag_type, tf_error);
14888 if (decl != error_mark_node)
14889 decl = TYPE_NAME (decl);
14891 else if (TREE_CODE (decl) != TYPE_DECL
14892 || TREE_TYPE (decl) == error_mark_node
14893 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14894 decl = error_mark_node;
14896 if (decl == error_mark_node)
14897 cp_parser_error (parser, "expected class-name");
14898 else if (identifier && !parser->scope)
14899 maybe_note_name_used_in_class (identifier, decl);
14904 /* Parse a class-specifier.
14907 class-head { member-specification [opt] }
14909 Returns the TREE_TYPE representing the class. */
14912 cp_parser_class_specifier (cp_parser* parser)
14916 tree attributes = NULL_TREE;
14917 int has_trailing_semicolon;
14918 bool nested_name_specifier_p;
14919 unsigned saved_num_template_parameter_lists;
14920 bool saved_in_function_body;
14921 bool saved_in_unbraced_linkage_specification_p;
14922 tree old_scope = NULL_TREE;
14923 tree scope = NULL_TREE;
14926 push_deferring_access_checks (dk_no_deferred);
14928 /* Parse the class-head. */
14929 type = cp_parser_class_head (parser,
14930 &nested_name_specifier_p,
14933 /* If the class-head was a semantic disaster, skip the entire body
14937 cp_parser_skip_to_end_of_block_or_statement (parser);
14938 pop_deferring_access_checks ();
14939 return error_mark_node;
14942 /* Look for the `{'. */
14943 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14945 pop_deferring_access_checks ();
14946 return error_mark_node;
14949 /* Process the base classes. If they're invalid, skip the
14950 entire class body. */
14951 if (!xref_basetypes (type, bases))
14953 /* Consuming the closing brace yields better error messages
14955 if (cp_parser_skip_to_closing_brace (parser))
14956 cp_lexer_consume_token (parser->lexer);
14957 pop_deferring_access_checks ();
14958 return error_mark_node;
14961 /* Issue an error message if type-definitions are forbidden here. */
14962 cp_parser_check_type_definition (parser);
14963 /* Remember that we are defining one more class. */
14964 ++parser->num_classes_being_defined;
14965 /* Inside the class, surrounding template-parameter-lists do not
14967 saved_num_template_parameter_lists
14968 = parser->num_template_parameter_lists;
14969 parser->num_template_parameter_lists = 0;
14970 /* We are not in a function body. */
14971 saved_in_function_body = parser->in_function_body;
14972 parser->in_function_body = false;
14973 /* We are not immediately inside an extern "lang" block. */
14974 saved_in_unbraced_linkage_specification_p
14975 = parser->in_unbraced_linkage_specification_p;
14976 parser->in_unbraced_linkage_specification_p = false;
14978 /* Start the class. */
14979 if (nested_name_specifier_p)
14981 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14982 old_scope = push_inner_scope (scope);
14984 type = begin_class_definition (type, attributes);
14986 if (type == error_mark_node)
14987 /* If the type is erroneous, skip the entire body of the class. */
14988 cp_parser_skip_to_closing_brace (parser);
14990 /* Parse the member-specification. */
14991 cp_parser_member_specification_opt (parser);
14993 /* Look for the trailing `}'. */
14994 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14995 /* We get better error messages by noticing a common problem: a
14996 missing trailing `;'. */
14997 token = cp_lexer_peek_token (parser->lexer);
14998 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14999 /* Look for trailing attributes to apply to this class. */
15000 if (cp_parser_allow_gnu_extensions_p (parser))
15001 attributes = cp_parser_attributes_opt (parser);
15002 if (type != error_mark_node)
15003 type = finish_struct (type, attributes);
15004 if (nested_name_specifier_p)
15005 pop_inner_scope (old_scope, scope);
15006 /* If this class is not itself within the scope of another class,
15007 then we need to parse the bodies of all of the queued function
15008 definitions. Note that the queued functions defined in a class
15009 are not always processed immediately following the
15010 class-specifier for that class. Consider:
15013 struct B { void f() { sizeof (A); } };
15016 If `f' were processed before the processing of `A' were
15017 completed, there would be no way to compute the size of `A'.
15018 Note that the nesting we are interested in here is lexical --
15019 not the semantic nesting given by TYPE_CONTEXT. In particular,
15022 struct A { struct B; };
15023 struct A::B { void f() { } };
15025 there is no need to delay the parsing of `A::B::f'. */
15026 if (--parser->num_classes_being_defined == 0)
15030 tree class_type = NULL_TREE;
15031 tree pushed_scope = NULL_TREE;
15033 /* In a first pass, parse default arguments to the functions.
15034 Then, in a second pass, parse the bodies of the functions.
15035 This two-phased approach handles cases like:
15043 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15044 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15045 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15046 TREE_PURPOSE (parser->unparsed_functions_queues)
15047 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15049 fn = TREE_VALUE (queue_entry);
15050 /* If there are default arguments that have not yet been processed,
15051 take care of them now. */
15052 if (class_type != TREE_PURPOSE (queue_entry))
15055 pop_scope (pushed_scope);
15056 class_type = TREE_PURPOSE (queue_entry);
15057 pushed_scope = push_scope (class_type);
15059 /* Make sure that any template parameters are in scope. */
15060 maybe_begin_member_template_processing (fn);
15061 /* Parse the default argument expressions. */
15062 cp_parser_late_parsing_default_args (parser, fn);
15063 /* Remove any template parameters from the symbol table. */
15064 maybe_end_member_template_processing ();
15067 pop_scope (pushed_scope);
15068 /* Now parse the body of the functions. */
15069 for (TREE_VALUE (parser->unparsed_functions_queues)
15070 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15071 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15072 TREE_VALUE (parser->unparsed_functions_queues)
15073 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15075 /* Figure out which function we need to process. */
15076 fn = TREE_VALUE (queue_entry);
15077 /* Parse the function. */
15078 cp_parser_late_parsing_for_member (parser, fn);
15082 /* Put back any saved access checks. */
15083 pop_deferring_access_checks ();
15085 /* Restore saved state. */
15086 parser->in_function_body = saved_in_function_body;
15087 parser->num_template_parameter_lists
15088 = saved_num_template_parameter_lists;
15089 parser->in_unbraced_linkage_specification_p
15090 = saved_in_unbraced_linkage_specification_p;
15095 /* Parse a class-head.
15098 class-key identifier [opt] base-clause [opt]
15099 class-key nested-name-specifier identifier base-clause [opt]
15100 class-key nested-name-specifier [opt] template-id
15104 class-key attributes identifier [opt] base-clause [opt]
15105 class-key attributes nested-name-specifier identifier base-clause [opt]
15106 class-key attributes nested-name-specifier [opt] template-id
15109 Upon return BASES is initialized to the list of base classes (or
15110 NULL, if there are none) in the same form returned by
15111 cp_parser_base_clause.
15113 Returns the TYPE of the indicated class. Sets
15114 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15115 involving a nested-name-specifier was used, and FALSE otherwise.
15117 Returns error_mark_node if this is not a class-head.
15119 Returns NULL_TREE if the class-head is syntactically valid, but
15120 semantically invalid in a way that means we should skip the entire
15121 body of the class. */
15124 cp_parser_class_head (cp_parser* parser,
15125 bool* nested_name_specifier_p,
15126 tree *attributes_p,
15129 tree nested_name_specifier;
15130 enum tag_types class_key;
15131 tree id = NULL_TREE;
15132 tree type = NULL_TREE;
15134 bool template_id_p = false;
15135 bool qualified_p = false;
15136 bool invalid_nested_name_p = false;
15137 bool invalid_explicit_specialization_p = false;
15138 tree pushed_scope = NULL_TREE;
15139 unsigned num_templates;
15140 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15141 /* Assume no nested-name-specifier will be present. */
15142 *nested_name_specifier_p = false;
15143 /* Assume no template parameter lists will be used in defining the
15147 *bases = NULL_TREE;
15149 /* Look for the class-key. */
15150 class_key = cp_parser_class_key (parser);
15151 if (class_key == none_type)
15152 return error_mark_node;
15154 /* Parse the attributes. */
15155 attributes = cp_parser_attributes_opt (parser);
15157 /* If the next token is `::', that is invalid -- but sometimes
15158 people do try to write:
15162 Handle this gracefully by accepting the extra qualifier, and then
15163 issuing an error about it later if this really is a
15164 class-head. If it turns out just to be an elaborated type
15165 specifier, remain silent. */
15166 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15167 qualified_p = true;
15169 push_deferring_access_checks (dk_no_check);
15171 /* Determine the name of the class. Begin by looking for an
15172 optional nested-name-specifier. */
15173 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15174 nested_name_specifier
15175 = cp_parser_nested_name_specifier_opt (parser,
15176 /*typename_keyword_p=*/false,
15177 /*check_dependency_p=*/false,
15179 /*is_declaration=*/false);
15180 /* If there was a nested-name-specifier, then there *must* be an
15182 if (nested_name_specifier)
15184 type_start_token = cp_lexer_peek_token (parser->lexer);
15185 /* Although the grammar says `identifier', it really means
15186 `class-name' or `template-name'. You are only allowed to
15187 define a class that has already been declared with this
15190 The proposed resolution for Core Issue 180 says that wherever
15191 you see `class T::X' you should treat `X' as a type-name.
15193 It is OK to define an inaccessible class; for example:
15195 class A { class B; };
15198 We do not know if we will see a class-name, or a
15199 template-name. We look for a class-name first, in case the
15200 class-name is a template-id; if we looked for the
15201 template-name first we would stop after the template-name. */
15202 cp_parser_parse_tentatively (parser);
15203 type = cp_parser_class_name (parser,
15204 /*typename_keyword_p=*/false,
15205 /*template_keyword_p=*/false,
15207 /*check_dependency_p=*/false,
15208 /*class_head_p=*/true,
15209 /*is_declaration=*/false);
15210 /* If that didn't work, ignore the nested-name-specifier. */
15211 if (!cp_parser_parse_definitely (parser))
15213 invalid_nested_name_p = true;
15214 type_start_token = cp_lexer_peek_token (parser->lexer);
15215 id = cp_parser_identifier (parser);
15216 if (id == error_mark_node)
15219 /* If we could not find a corresponding TYPE, treat this
15220 declaration like an unqualified declaration. */
15221 if (type == error_mark_node)
15222 nested_name_specifier = NULL_TREE;
15223 /* Otherwise, count the number of templates used in TYPE and its
15224 containing scopes. */
15229 for (scope = TREE_TYPE (type);
15230 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15231 scope = (TYPE_P (scope)
15232 ? TYPE_CONTEXT (scope)
15233 : DECL_CONTEXT (scope)))
15235 && CLASS_TYPE_P (scope)
15236 && CLASSTYPE_TEMPLATE_INFO (scope)
15237 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15238 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15242 /* Otherwise, the identifier is optional. */
15245 /* We don't know whether what comes next is a template-id,
15246 an identifier, or nothing at all. */
15247 cp_parser_parse_tentatively (parser);
15248 /* Check for a template-id. */
15249 type_start_token = cp_lexer_peek_token (parser->lexer);
15250 id = cp_parser_template_id (parser,
15251 /*template_keyword_p=*/false,
15252 /*check_dependency_p=*/true,
15253 /*is_declaration=*/true);
15254 /* If that didn't work, it could still be an identifier. */
15255 if (!cp_parser_parse_definitely (parser))
15257 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15259 type_start_token = cp_lexer_peek_token (parser->lexer);
15260 id = cp_parser_identifier (parser);
15267 template_id_p = true;
15272 pop_deferring_access_checks ();
15275 cp_parser_check_for_invalid_template_id (parser, id,
15276 type_start_token->location);
15278 /* If it's not a `:' or a `{' then we can't really be looking at a
15279 class-head, since a class-head only appears as part of a
15280 class-specifier. We have to detect this situation before calling
15281 xref_tag, since that has irreversible side-effects. */
15282 if (!cp_parser_next_token_starts_class_definition_p (parser))
15284 cp_parser_error (parser, "expected %<{%> or %<:%>");
15285 return error_mark_node;
15288 /* At this point, we're going ahead with the class-specifier, even
15289 if some other problem occurs. */
15290 cp_parser_commit_to_tentative_parse (parser);
15291 /* Issue the error about the overly-qualified name now. */
15294 cp_parser_error (parser,
15295 "global qualification of class name is invalid");
15296 return error_mark_node;
15298 else if (invalid_nested_name_p)
15300 cp_parser_error (parser,
15301 "qualified name does not name a class");
15302 return error_mark_node;
15304 else if (nested_name_specifier)
15308 /* Reject typedef-names in class heads. */
15309 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15311 error ("%Hinvalid class name in declaration of %qD",
15312 &type_start_token->location, type);
15317 /* Figure out in what scope the declaration is being placed. */
15318 scope = current_scope ();
15319 /* If that scope does not contain the scope in which the
15320 class was originally declared, the program is invalid. */
15321 if (scope && !is_ancestor (scope, nested_name_specifier))
15323 if (at_namespace_scope_p ())
15324 error ("%Hdeclaration of %qD in namespace %qD which does not "
15326 &type_start_token->location,
15327 type, scope, nested_name_specifier);
15329 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15330 &type_start_token->location,
15331 type, scope, nested_name_specifier);
15337 A declarator-id shall not be qualified except for the
15338 definition of a ... nested class outside of its class
15339 ... [or] the definition or explicit instantiation of a
15340 class member of a namespace outside of its namespace. */
15341 if (scope == nested_name_specifier)
15343 permerror (input_location, "%Hextra qualification not allowed",
15344 &nested_name_specifier_token_start->location);
15345 nested_name_specifier = NULL_TREE;
15349 /* An explicit-specialization must be preceded by "template <>". If
15350 it is not, try to recover gracefully. */
15351 if (at_namespace_scope_p ()
15352 && parser->num_template_parameter_lists == 0
15355 error ("%Han explicit specialization must be preceded by %<template <>%>",
15356 &type_start_token->location);
15357 invalid_explicit_specialization_p = true;
15358 /* Take the same action that would have been taken by
15359 cp_parser_explicit_specialization. */
15360 ++parser->num_template_parameter_lists;
15361 begin_specialization ();
15363 /* There must be no "return" statements between this point and the
15364 end of this function; set "type "to the correct return value and
15365 use "goto done;" to return. */
15366 /* Make sure that the right number of template parameters were
15368 if (!cp_parser_check_template_parameters (parser, num_templates,
15369 type_start_token->location))
15371 /* If something went wrong, there is no point in even trying to
15372 process the class-definition. */
15377 /* Look up the type. */
15380 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15381 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15382 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15384 error ("%Hfunction template %qD redeclared as a class template",
15385 &type_start_token->location, id);
15386 type = error_mark_node;
15390 type = TREE_TYPE (id);
15391 type = maybe_process_partial_specialization (type);
15393 if (nested_name_specifier)
15394 pushed_scope = push_scope (nested_name_specifier);
15396 else if (nested_name_specifier)
15402 template <typename T> struct S { struct T };
15403 template <typename T> struct S<T>::T { };
15405 we will get a TYPENAME_TYPE when processing the definition of
15406 `S::T'. We need to resolve it to the actual type before we
15407 try to define it. */
15408 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15410 class_type = resolve_typename_type (TREE_TYPE (type),
15411 /*only_current_p=*/false);
15412 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15413 type = TYPE_NAME (class_type);
15416 cp_parser_error (parser, "could not resolve typename type");
15417 type = error_mark_node;
15421 if (maybe_process_partial_specialization (TREE_TYPE (type))
15422 == error_mark_node)
15428 class_type = current_class_type;
15429 /* Enter the scope indicated by the nested-name-specifier. */
15430 pushed_scope = push_scope (nested_name_specifier);
15431 /* Get the canonical version of this type. */
15432 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15433 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15434 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15436 type = push_template_decl (type);
15437 if (type == error_mark_node)
15444 type = TREE_TYPE (type);
15445 *nested_name_specifier_p = true;
15447 else /* The name is not a nested name. */
15449 /* If the class was unnamed, create a dummy name. */
15451 id = make_anon_name ();
15452 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15453 parser->num_template_parameter_lists);
15456 /* Indicate whether this class was declared as a `class' or as a
15458 if (TREE_CODE (type) == RECORD_TYPE)
15459 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15460 cp_parser_check_class_key (class_key, type);
15462 /* If this type was already complete, and we see another definition,
15463 that's an error. */
15464 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15466 error ("%Hredefinition of %q#T",
15467 &type_start_token->location, type);
15468 error ("%Hprevious definition of %q+#T",
15469 &type_start_token->location, type);
15473 else if (type == error_mark_node)
15476 /* We will have entered the scope containing the class; the names of
15477 base classes should be looked up in that context. For example:
15479 struct A { struct B {}; struct C; };
15480 struct A::C : B {};
15484 /* Get the list of base-classes, if there is one. */
15485 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15486 *bases = cp_parser_base_clause (parser);
15489 /* Leave the scope given by the nested-name-specifier. We will
15490 enter the class scope itself while processing the members. */
15492 pop_scope (pushed_scope);
15494 if (invalid_explicit_specialization_p)
15496 end_specialization ();
15497 --parser->num_template_parameter_lists;
15499 *attributes_p = attributes;
15503 /* Parse a class-key.
15510 Returns the kind of class-key specified, or none_type to indicate
15513 static enum tag_types
15514 cp_parser_class_key (cp_parser* parser)
15517 enum tag_types tag_type;
15519 /* Look for the class-key. */
15520 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15524 /* Check to see if the TOKEN is a class-key. */
15525 tag_type = cp_parser_token_is_class_key (token);
15527 cp_parser_error (parser, "expected class-key");
15531 /* Parse an (optional) member-specification.
15533 member-specification:
15534 member-declaration member-specification [opt]
15535 access-specifier : member-specification [opt] */
15538 cp_parser_member_specification_opt (cp_parser* parser)
15545 /* Peek at the next token. */
15546 token = cp_lexer_peek_token (parser->lexer);
15547 /* If it's a `}', or EOF then we've seen all the members. */
15548 if (token->type == CPP_CLOSE_BRACE
15549 || token->type == CPP_EOF
15550 || token->type == CPP_PRAGMA_EOL)
15553 /* See if this token is a keyword. */
15554 keyword = token->keyword;
15558 case RID_PROTECTED:
15560 /* Consume the access-specifier. */
15561 cp_lexer_consume_token (parser->lexer);
15562 /* Remember which access-specifier is active. */
15563 current_access_specifier = token->u.value;
15564 /* Look for the `:'. */
15565 cp_parser_require (parser, CPP_COLON, "%<:%>");
15569 /* Accept #pragmas at class scope. */
15570 if (token->type == CPP_PRAGMA)
15572 cp_parser_pragma (parser, pragma_external);
15576 /* Otherwise, the next construction must be a
15577 member-declaration. */
15578 cp_parser_member_declaration (parser);
15583 /* Parse a member-declaration.
15585 member-declaration:
15586 decl-specifier-seq [opt] member-declarator-list [opt] ;
15587 function-definition ; [opt]
15588 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15590 template-declaration
15592 member-declarator-list:
15594 member-declarator-list , member-declarator
15597 declarator pure-specifier [opt]
15598 declarator constant-initializer [opt]
15599 identifier [opt] : constant-expression
15603 member-declaration:
15604 __extension__ member-declaration
15607 declarator attributes [opt] pure-specifier [opt]
15608 declarator attributes [opt] constant-initializer [opt]
15609 identifier [opt] attributes [opt] : constant-expression
15613 member-declaration:
15614 static_assert-declaration */
15617 cp_parser_member_declaration (cp_parser* parser)
15619 cp_decl_specifier_seq decl_specifiers;
15620 tree prefix_attributes;
15622 int declares_class_or_enum;
15624 cp_token *token = NULL;
15625 cp_token *decl_spec_token_start = NULL;
15626 cp_token *initializer_token_start = NULL;
15627 int saved_pedantic;
15629 /* Check for the `__extension__' keyword. */
15630 if (cp_parser_extension_opt (parser, &saved_pedantic))
15633 cp_parser_member_declaration (parser);
15634 /* Restore the old value of the PEDANTIC flag. */
15635 pedantic = saved_pedantic;
15640 /* Check for a template-declaration. */
15641 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15643 /* An explicit specialization here is an error condition, and we
15644 expect the specialization handler to detect and report this. */
15645 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15646 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15647 cp_parser_explicit_specialization (parser);
15649 cp_parser_template_declaration (parser, /*member_p=*/true);
15654 /* Check for a using-declaration. */
15655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15657 /* Parse the using-declaration. */
15658 cp_parser_using_declaration (parser,
15659 /*access_declaration_p=*/false);
15663 /* Check for @defs. */
15664 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15667 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15668 ivar = ivar_chains;
15672 ivar = TREE_CHAIN (member);
15673 TREE_CHAIN (member) = NULL_TREE;
15674 finish_member_declaration (member);
15679 /* If the next token is `static_assert' we have a static assertion. */
15680 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15682 cp_parser_static_assert (parser, /*member_p=*/true);
15686 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15689 /* Parse the decl-specifier-seq. */
15690 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15691 cp_parser_decl_specifier_seq (parser,
15692 CP_PARSER_FLAGS_OPTIONAL,
15694 &declares_class_or_enum);
15695 prefix_attributes = decl_specifiers.attributes;
15696 decl_specifiers.attributes = NULL_TREE;
15697 /* Check for an invalid type-name. */
15698 if (!decl_specifiers.type
15699 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15701 /* If there is no declarator, then the decl-specifier-seq should
15703 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15705 /* If there was no decl-specifier-seq, and the next token is a
15706 `;', then we have something like:
15712 Each member-declaration shall declare at least one member
15713 name of the class. */
15714 if (!decl_specifiers.any_specifiers_p)
15716 cp_token *token = cp_lexer_peek_token (parser->lexer);
15717 if (!in_system_header_at (token->location))
15718 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15724 /* See if this declaration is a friend. */
15725 friend_p = cp_parser_friend_p (&decl_specifiers);
15726 /* If there were decl-specifiers, check to see if there was
15727 a class-declaration. */
15728 type = check_tag_decl (&decl_specifiers);
15729 /* Nested classes have already been added to the class, but
15730 a `friend' needs to be explicitly registered. */
15733 /* If the `friend' keyword was present, the friend must
15734 be introduced with a class-key. */
15735 if (!declares_class_or_enum)
15736 error ("%Ha class-key must be used when declaring a friend",
15737 &decl_spec_token_start->location);
15740 template <typename T> struct A {
15741 friend struct A<T>::B;
15744 A<T>::B will be represented by a TYPENAME_TYPE, and
15745 therefore not recognized by check_tag_decl. */
15747 && decl_specifiers.type
15748 && TYPE_P (decl_specifiers.type))
15749 type = decl_specifiers.type;
15750 if (!type || !TYPE_P (type))
15751 error ("%Hfriend declaration does not name a class or "
15752 "function", &decl_spec_token_start->location);
15754 make_friend_class (current_class_type, type,
15755 /*complain=*/true);
15757 /* If there is no TYPE, an error message will already have
15759 else if (!type || type == error_mark_node)
15761 /* An anonymous aggregate has to be handled specially; such
15762 a declaration really declares a data member (with a
15763 particular type), as opposed to a nested class. */
15764 else if (ANON_AGGR_TYPE_P (type))
15766 /* Remove constructors and such from TYPE, now that we
15767 know it is an anonymous aggregate. */
15768 fixup_anonymous_aggr (type);
15769 /* And make the corresponding data member. */
15770 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15771 /* Add it to the class. */
15772 finish_member_declaration (decl);
15775 cp_parser_check_access_in_redeclaration
15777 decl_spec_token_start->location);
15782 /* See if these declarations will be friends. */
15783 friend_p = cp_parser_friend_p (&decl_specifiers);
15785 /* Keep going until we hit the `;' at the end of the
15787 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15789 tree attributes = NULL_TREE;
15790 tree first_attribute;
15792 /* Peek at the next token. */
15793 token = cp_lexer_peek_token (parser->lexer);
15795 /* Check for a bitfield declaration. */
15796 if (token->type == CPP_COLON
15797 || (token->type == CPP_NAME
15798 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15804 /* Get the name of the bitfield. Note that we cannot just
15805 check TOKEN here because it may have been invalidated by
15806 the call to cp_lexer_peek_nth_token above. */
15807 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15808 identifier = cp_parser_identifier (parser);
15810 identifier = NULL_TREE;
15812 /* Consume the `:' token. */
15813 cp_lexer_consume_token (parser->lexer);
15814 /* Get the width of the bitfield. */
15816 = cp_parser_constant_expression (parser,
15817 /*allow_non_constant=*/false,
15820 /* Look for attributes that apply to the bitfield. */
15821 attributes = cp_parser_attributes_opt (parser);
15822 /* Remember which attributes are prefix attributes and
15824 first_attribute = attributes;
15825 /* Combine the attributes. */
15826 attributes = chainon (prefix_attributes, attributes);
15828 /* Create the bitfield declaration. */
15829 decl = grokbitfield (identifier
15830 ? make_id_declarator (NULL_TREE,
15840 cp_declarator *declarator;
15842 tree asm_specification;
15843 int ctor_dtor_or_conv_p;
15845 /* Parse the declarator. */
15847 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15848 &ctor_dtor_or_conv_p,
15849 /*parenthesized_p=*/NULL,
15850 /*member_p=*/true);
15852 /* If something went wrong parsing the declarator, make sure
15853 that we at least consume some tokens. */
15854 if (declarator == cp_error_declarator)
15856 /* Skip to the end of the statement. */
15857 cp_parser_skip_to_end_of_statement (parser);
15858 /* If the next token is not a semicolon, that is
15859 probably because we just skipped over the body of
15860 a function. So, we consume a semicolon if
15861 present, but do not issue an error message if it
15863 if (cp_lexer_next_token_is (parser->lexer,
15865 cp_lexer_consume_token (parser->lexer);
15869 if (declares_class_or_enum & 2)
15870 cp_parser_check_for_definition_in_return_type
15871 (declarator, decl_specifiers.type,
15872 decl_specifiers.type_location);
15874 /* Look for an asm-specification. */
15875 asm_specification = cp_parser_asm_specification_opt (parser);
15876 /* Look for attributes that apply to the declaration. */
15877 attributes = cp_parser_attributes_opt (parser);
15878 /* Remember which attributes are prefix attributes and
15880 first_attribute = attributes;
15881 /* Combine the attributes. */
15882 attributes = chainon (prefix_attributes, attributes);
15884 /* If it's an `=', then we have a constant-initializer or a
15885 pure-specifier. It is not correct to parse the
15886 initializer before registering the member declaration
15887 since the member declaration should be in scope while
15888 its initializer is processed. However, the rest of the
15889 front end does not yet provide an interface that allows
15890 us to handle this correctly. */
15891 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15895 A pure-specifier shall be used only in the declaration of
15896 a virtual function.
15898 A member-declarator can contain a constant-initializer
15899 only if it declares a static member of integral or
15902 Therefore, if the DECLARATOR is for a function, we look
15903 for a pure-specifier; otherwise, we look for a
15904 constant-initializer. When we call `grokfield', it will
15905 perform more stringent semantics checks. */
15906 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15907 if (function_declarator_p (declarator))
15908 initializer = cp_parser_pure_specifier (parser);
15910 /* Parse the initializer. */
15911 initializer = cp_parser_constant_initializer (parser);
15913 /* Otherwise, there is no initializer. */
15915 initializer = NULL_TREE;
15917 /* See if we are probably looking at a function
15918 definition. We are certainly not looking at a
15919 member-declarator. Calling `grokfield' has
15920 side-effects, so we must not do it unless we are sure
15921 that we are looking at a member-declarator. */
15922 if (cp_parser_token_starts_function_definition_p
15923 (cp_lexer_peek_token (parser->lexer)))
15925 /* The grammar does not allow a pure-specifier to be
15926 used when a member function is defined. (It is
15927 possible that this fact is an oversight in the
15928 standard, since a pure function may be defined
15929 outside of the class-specifier. */
15931 error ("%Hpure-specifier on function-definition",
15932 &initializer_token_start->location);
15933 decl = cp_parser_save_member_function_body (parser,
15937 /* If the member was not a friend, declare it here. */
15939 finish_member_declaration (decl);
15940 /* Peek at the next token. */
15941 token = cp_lexer_peek_token (parser->lexer);
15942 /* If the next token is a semicolon, consume it. */
15943 if (token->type == CPP_SEMICOLON)
15944 cp_lexer_consume_token (parser->lexer);
15948 if (declarator->kind == cdk_function)
15949 declarator->id_loc = token->location;
15950 /* Create the declaration. */
15951 decl = grokfield (declarator, &decl_specifiers,
15952 initializer, /*init_const_expr_p=*/true,
15957 /* Reset PREFIX_ATTRIBUTES. */
15958 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15959 attributes = TREE_CHAIN (attributes);
15961 TREE_CHAIN (attributes) = NULL_TREE;
15963 /* If there is any qualification still in effect, clear it
15964 now; we will be starting fresh with the next declarator. */
15965 parser->scope = NULL_TREE;
15966 parser->qualifying_scope = NULL_TREE;
15967 parser->object_scope = NULL_TREE;
15968 /* If it's a `,', then there are more declarators. */
15969 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15970 cp_lexer_consume_token (parser->lexer);
15971 /* If the next token isn't a `;', then we have a parse error. */
15972 else if (cp_lexer_next_token_is_not (parser->lexer,
15975 cp_parser_error (parser, "expected %<;%>");
15976 /* Skip tokens until we find a `;'. */
15977 cp_parser_skip_to_end_of_statement (parser);
15984 /* Add DECL to the list of members. */
15986 finish_member_declaration (decl);
15988 if (TREE_CODE (decl) == FUNCTION_DECL)
15989 cp_parser_save_default_args (parser, decl);
15994 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15997 /* Parse a pure-specifier.
16002 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16003 Otherwise, ERROR_MARK_NODE is returned. */
16006 cp_parser_pure_specifier (cp_parser* parser)
16010 /* Look for the `=' token. */
16011 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16012 return error_mark_node;
16013 /* Look for the `0' token. */
16014 token = cp_lexer_peek_token (parser->lexer);
16016 if (token->type == CPP_EOF
16017 || token->type == CPP_PRAGMA_EOL)
16018 return error_mark_node;
16020 cp_lexer_consume_token (parser->lexer);
16022 /* Accept = default or = delete in c++0x mode. */
16023 if (token->keyword == RID_DEFAULT
16024 || token->keyword == RID_DELETE)
16026 maybe_warn_cpp0x ("defaulted and deleted functions");
16027 return token->u.value;
16030 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16031 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16033 cp_parser_error (parser,
16034 "invalid pure specifier (only %<= 0%> is allowed)");
16035 cp_parser_skip_to_end_of_statement (parser);
16036 return error_mark_node;
16038 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16040 error ("%Htemplates may not be %<virtual%>", &token->location);
16041 return error_mark_node;
16044 return integer_zero_node;
16047 /* Parse a constant-initializer.
16049 constant-initializer:
16050 = constant-expression
16052 Returns a representation of the constant-expression. */
16055 cp_parser_constant_initializer (cp_parser* parser)
16057 /* Look for the `=' token. */
16058 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16059 return error_mark_node;
16061 /* It is invalid to write:
16063 struct S { static const int i = { 7 }; };
16066 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16068 cp_parser_error (parser,
16069 "a brace-enclosed initializer is not allowed here");
16070 /* Consume the opening brace. */
16071 cp_lexer_consume_token (parser->lexer);
16072 /* Skip the initializer. */
16073 cp_parser_skip_to_closing_brace (parser);
16074 /* Look for the trailing `}'. */
16075 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16077 return error_mark_node;
16080 return cp_parser_constant_expression (parser,
16081 /*allow_non_constant=*/false,
16085 /* Derived classes [gram.class.derived] */
16087 /* Parse a base-clause.
16090 : base-specifier-list
16092 base-specifier-list:
16093 base-specifier ... [opt]
16094 base-specifier-list , base-specifier ... [opt]
16096 Returns a TREE_LIST representing the base-classes, in the order in
16097 which they were declared. The representation of each node is as
16098 described by cp_parser_base_specifier.
16100 In the case that no bases are specified, this function will return
16101 NULL_TREE, not ERROR_MARK_NODE. */
16104 cp_parser_base_clause (cp_parser* parser)
16106 tree bases = NULL_TREE;
16108 /* Look for the `:' that begins the list. */
16109 cp_parser_require (parser, CPP_COLON, "%<:%>");
16111 /* Scan the base-specifier-list. */
16116 bool pack_expansion_p = false;
16118 /* Look for the base-specifier. */
16119 base = cp_parser_base_specifier (parser);
16120 /* Look for the (optional) ellipsis. */
16121 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16123 /* Consume the `...'. */
16124 cp_lexer_consume_token (parser->lexer);
16126 pack_expansion_p = true;
16129 /* Add BASE to the front of the list. */
16130 if (base != error_mark_node)
16132 if (pack_expansion_p)
16133 /* Make this a pack expansion type. */
16134 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16137 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16139 TREE_CHAIN (base) = bases;
16143 /* Peek at the next token. */
16144 token = cp_lexer_peek_token (parser->lexer);
16145 /* If it's not a comma, then the list is complete. */
16146 if (token->type != CPP_COMMA)
16148 /* Consume the `,'. */
16149 cp_lexer_consume_token (parser->lexer);
16152 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16153 base class had a qualified name. However, the next name that
16154 appears is certainly not qualified. */
16155 parser->scope = NULL_TREE;
16156 parser->qualifying_scope = NULL_TREE;
16157 parser->object_scope = NULL_TREE;
16159 return nreverse (bases);
16162 /* Parse a base-specifier.
16165 :: [opt] nested-name-specifier [opt] class-name
16166 virtual access-specifier [opt] :: [opt] nested-name-specifier
16168 access-specifier virtual [opt] :: [opt] nested-name-specifier
16171 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16172 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16173 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16174 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16177 cp_parser_base_specifier (cp_parser* parser)
16181 bool virtual_p = false;
16182 bool duplicate_virtual_error_issued_p = false;
16183 bool duplicate_access_error_issued_p = false;
16184 bool class_scope_p, template_p;
16185 tree access = access_default_node;
16188 /* Process the optional `virtual' and `access-specifier'. */
16191 /* Peek at the next token. */
16192 token = cp_lexer_peek_token (parser->lexer);
16193 /* Process `virtual'. */
16194 switch (token->keyword)
16197 /* If `virtual' appears more than once, issue an error. */
16198 if (virtual_p && !duplicate_virtual_error_issued_p)
16200 cp_parser_error (parser,
16201 "%<virtual%> specified more than once in base-specified");
16202 duplicate_virtual_error_issued_p = true;
16207 /* Consume the `virtual' token. */
16208 cp_lexer_consume_token (parser->lexer);
16213 case RID_PROTECTED:
16215 /* If more than one access specifier appears, issue an
16217 if (access != access_default_node
16218 && !duplicate_access_error_issued_p)
16220 cp_parser_error (parser,
16221 "more than one access specifier in base-specified");
16222 duplicate_access_error_issued_p = true;
16225 access = ridpointers[(int) token->keyword];
16227 /* Consume the access-specifier. */
16228 cp_lexer_consume_token (parser->lexer);
16237 /* It is not uncommon to see programs mechanically, erroneously, use
16238 the 'typename' keyword to denote (dependent) qualified types
16239 as base classes. */
16240 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16242 token = cp_lexer_peek_token (parser->lexer);
16243 if (!processing_template_decl)
16244 error ("%Hkeyword %<typename%> not allowed outside of templates",
16247 error ("%Hkeyword %<typename%> not allowed in this context "
16248 "(the base class is implicitly a type)",
16250 cp_lexer_consume_token (parser->lexer);
16253 /* Look for the optional `::' operator. */
16254 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16255 /* Look for the nested-name-specifier. The simplest way to
16260 The keyword `typename' is not permitted in a base-specifier or
16261 mem-initializer; in these contexts a qualified name that
16262 depends on a template-parameter is implicitly assumed to be a
16265 is to pretend that we have seen the `typename' keyword at this
16267 cp_parser_nested_name_specifier_opt (parser,
16268 /*typename_keyword_p=*/true,
16269 /*check_dependency_p=*/true,
16271 /*is_declaration=*/true);
16272 /* If the base class is given by a qualified name, assume that names
16273 we see are type names or templates, as appropriate. */
16274 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16275 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16277 /* Finally, look for the class-name. */
16278 type = cp_parser_class_name (parser,
16282 /*check_dependency_p=*/true,
16283 /*class_head_p=*/false,
16284 /*is_declaration=*/true);
16286 if (type == error_mark_node)
16287 return error_mark_node;
16289 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16292 /* Exception handling [gram.exception] */
16294 /* Parse an (optional) exception-specification.
16296 exception-specification:
16297 throw ( type-id-list [opt] )
16299 Returns a TREE_LIST representing the exception-specification. The
16300 TREE_VALUE of each node is a type. */
16303 cp_parser_exception_specification_opt (cp_parser* parser)
16308 /* Peek at the next token. */
16309 token = cp_lexer_peek_token (parser->lexer);
16310 /* If it's not `throw', then there's no exception-specification. */
16311 if (!cp_parser_is_keyword (token, RID_THROW))
16314 /* Consume the `throw'. */
16315 cp_lexer_consume_token (parser->lexer);
16317 /* Look for the `('. */
16318 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16320 /* Peek at the next token. */
16321 token = cp_lexer_peek_token (parser->lexer);
16322 /* If it's not a `)', then there is a type-id-list. */
16323 if (token->type != CPP_CLOSE_PAREN)
16325 const char *saved_message;
16327 /* Types may not be defined in an exception-specification. */
16328 saved_message = parser->type_definition_forbidden_message;
16329 parser->type_definition_forbidden_message
16330 = "types may not be defined in an exception-specification";
16331 /* Parse the type-id-list. */
16332 type_id_list = cp_parser_type_id_list (parser);
16333 /* Restore the saved message. */
16334 parser->type_definition_forbidden_message = saved_message;
16337 type_id_list = empty_except_spec;
16339 /* Look for the `)'. */
16340 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16342 return type_id_list;
16345 /* Parse an (optional) type-id-list.
16349 type-id-list , type-id ... [opt]
16351 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16352 in the order that the types were presented. */
16355 cp_parser_type_id_list (cp_parser* parser)
16357 tree types = NULL_TREE;
16364 /* Get the next type-id. */
16365 type = cp_parser_type_id (parser);
16366 /* Parse the optional ellipsis. */
16367 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16369 /* Consume the `...'. */
16370 cp_lexer_consume_token (parser->lexer);
16372 /* Turn the type into a pack expansion expression. */
16373 type = make_pack_expansion (type);
16375 /* Add it to the list. */
16376 types = add_exception_specifier (types, type, /*complain=*/1);
16377 /* Peek at the next token. */
16378 token = cp_lexer_peek_token (parser->lexer);
16379 /* If it is not a `,', we are done. */
16380 if (token->type != CPP_COMMA)
16382 /* Consume the `,'. */
16383 cp_lexer_consume_token (parser->lexer);
16386 return nreverse (types);
16389 /* Parse a try-block.
16392 try compound-statement handler-seq */
16395 cp_parser_try_block (cp_parser* parser)
16399 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16400 try_block = begin_try_block ();
16401 cp_parser_compound_statement (parser, NULL, true);
16402 finish_try_block (try_block);
16403 cp_parser_handler_seq (parser);
16404 finish_handler_sequence (try_block);
16409 /* Parse a function-try-block.
16411 function-try-block:
16412 try ctor-initializer [opt] function-body handler-seq */
16415 cp_parser_function_try_block (cp_parser* parser)
16417 tree compound_stmt;
16419 bool ctor_initializer_p;
16421 /* Look for the `try' keyword. */
16422 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16424 /* Let the rest of the front end know where we are. */
16425 try_block = begin_function_try_block (&compound_stmt);
16426 /* Parse the function-body. */
16428 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16429 /* We're done with the `try' part. */
16430 finish_function_try_block (try_block);
16431 /* Parse the handlers. */
16432 cp_parser_handler_seq (parser);
16433 /* We're done with the handlers. */
16434 finish_function_handler_sequence (try_block, compound_stmt);
16436 return ctor_initializer_p;
16439 /* Parse a handler-seq.
16442 handler handler-seq [opt] */
16445 cp_parser_handler_seq (cp_parser* parser)
16451 /* Parse the handler. */
16452 cp_parser_handler (parser);
16453 /* Peek at the next token. */
16454 token = cp_lexer_peek_token (parser->lexer);
16455 /* If it's not `catch' then there are no more handlers. */
16456 if (!cp_parser_is_keyword (token, RID_CATCH))
16461 /* Parse a handler.
16464 catch ( exception-declaration ) compound-statement */
16467 cp_parser_handler (cp_parser* parser)
16472 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16473 handler = begin_handler ();
16474 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16475 declaration = cp_parser_exception_declaration (parser);
16476 finish_handler_parms (declaration, handler);
16477 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16478 cp_parser_compound_statement (parser, NULL, false);
16479 finish_handler (handler);
16482 /* Parse an exception-declaration.
16484 exception-declaration:
16485 type-specifier-seq declarator
16486 type-specifier-seq abstract-declarator
16490 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16491 ellipsis variant is used. */
16494 cp_parser_exception_declaration (cp_parser* parser)
16496 cp_decl_specifier_seq type_specifiers;
16497 cp_declarator *declarator;
16498 const char *saved_message;
16500 /* If it's an ellipsis, it's easy to handle. */
16501 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16503 /* Consume the `...' token. */
16504 cp_lexer_consume_token (parser->lexer);
16508 /* Types may not be defined in exception-declarations. */
16509 saved_message = parser->type_definition_forbidden_message;
16510 parser->type_definition_forbidden_message
16511 = "types may not be defined in exception-declarations";
16513 /* Parse the type-specifier-seq. */
16514 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16516 /* If it's a `)', then there is no declarator. */
16517 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16520 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16521 /*ctor_dtor_or_conv_p=*/NULL,
16522 /*parenthesized_p=*/NULL,
16523 /*member_p=*/false);
16525 /* Restore the saved message. */
16526 parser->type_definition_forbidden_message = saved_message;
16528 if (!type_specifiers.any_specifiers_p)
16529 return error_mark_node;
16531 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16534 /* Parse a throw-expression.
16537 throw assignment-expression [opt]
16539 Returns a THROW_EXPR representing the throw-expression. */
16542 cp_parser_throw_expression (cp_parser* parser)
16547 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16548 token = cp_lexer_peek_token (parser->lexer);
16549 /* Figure out whether or not there is an assignment-expression
16550 following the "throw" keyword. */
16551 if (token->type == CPP_COMMA
16552 || token->type == CPP_SEMICOLON
16553 || token->type == CPP_CLOSE_PAREN
16554 || token->type == CPP_CLOSE_SQUARE
16555 || token->type == CPP_CLOSE_BRACE
16556 || token->type == CPP_COLON)
16557 expression = NULL_TREE;
16559 expression = cp_parser_assignment_expression (parser,
16560 /*cast_p=*/false, NULL);
16562 return build_throw (expression);
16565 /* GNU Extensions */
16567 /* Parse an (optional) asm-specification.
16570 asm ( string-literal )
16572 If the asm-specification is present, returns a STRING_CST
16573 corresponding to the string-literal. Otherwise, returns
16577 cp_parser_asm_specification_opt (cp_parser* parser)
16580 tree asm_specification;
16582 /* Peek at the next token. */
16583 token = cp_lexer_peek_token (parser->lexer);
16584 /* If the next token isn't the `asm' keyword, then there's no
16585 asm-specification. */
16586 if (!cp_parser_is_keyword (token, RID_ASM))
16589 /* Consume the `asm' token. */
16590 cp_lexer_consume_token (parser->lexer);
16591 /* Look for the `('. */
16592 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16594 /* Look for the string-literal. */
16595 asm_specification = cp_parser_string_literal (parser, false, false);
16597 /* Look for the `)'. */
16598 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16600 return asm_specification;
16603 /* Parse an asm-operand-list.
16607 asm-operand-list , asm-operand
16610 string-literal ( expression )
16611 [ string-literal ] string-literal ( expression )
16613 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16614 each node is the expression. The TREE_PURPOSE is itself a
16615 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16616 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16617 is a STRING_CST for the string literal before the parenthesis. Returns
16618 ERROR_MARK_NODE if any of the operands are invalid. */
16621 cp_parser_asm_operand_list (cp_parser* parser)
16623 tree asm_operands = NULL_TREE;
16624 bool invalid_operands = false;
16628 tree string_literal;
16632 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16634 /* Consume the `[' token. */
16635 cp_lexer_consume_token (parser->lexer);
16636 /* Read the operand name. */
16637 name = cp_parser_identifier (parser);
16638 if (name != error_mark_node)
16639 name = build_string (IDENTIFIER_LENGTH (name),
16640 IDENTIFIER_POINTER (name));
16641 /* Look for the closing `]'. */
16642 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16646 /* Look for the string-literal. */
16647 string_literal = cp_parser_string_literal (parser, false, false);
16649 /* Look for the `('. */
16650 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16651 /* Parse the expression. */
16652 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16653 /* Look for the `)'. */
16654 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16656 if (name == error_mark_node
16657 || string_literal == error_mark_node
16658 || expression == error_mark_node)
16659 invalid_operands = true;
16661 /* Add this operand to the list. */
16662 asm_operands = tree_cons (build_tree_list (name, string_literal),
16665 /* If the next token is not a `,', there are no more
16667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16669 /* Consume the `,'. */
16670 cp_lexer_consume_token (parser->lexer);
16673 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16676 /* Parse an asm-clobber-list.
16680 asm-clobber-list , string-literal
16682 Returns a TREE_LIST, indicating the clobbers in the order that they
16683 appeared. The TREE_VALUE of each node is a STRING_CST. */
16686 cp_parser_asm_clobber_list (cp_parser* parser)
16688 tree clobbers = NULL_TREE;
16692 tree string_literal;
16694 /* Look for the string literal. */
16695 string_literal = cp_parser_string_literal (parser, false, false);
16696 /* Add it to the list. */
16697 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16698 /* If the next token is not a `,', then the list is
16700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16702 /* Consume the `,' token. */
16703 cp_lexer_consume_token (parser->lexer);
16709 /* Parse an (optional) series of attributes.
16712 attributes attribute
16715 __attribute__ (( attribute-list [opt] ))
16717 The return value is as for cp_parser_attribute_list. */
16720 cp_parser_attributes_opt (cp_parser* parser)
16722 tree attributes = NULL_TREE;
16727 tree attribute_list;
16729 /* Peek at the next token. */
16730 token = cp_lexer_peek_token (parser->lexer);
16731 /* If it's not `__attribute__', then we're done. */
16732 if (token->keyword != RID_ATTRIBUTE)
16735 /* Consume the `__attribute__' keyword. */
16736 cp_lexer_consume_token (parser->lexer);
16737 /* Look for the two `(' tokens. */
16738 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16739 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16741 /* Peek at the next token. */
16742 token = cp_lexer_peek_token (parser->lexer);
16743 if (token->type != CPP_CLOSE_PAREN)
16744 /* Parse the attribute-list. */
16745 attribute_list = cp_parser_attribute_list (parser);
16747 /* If the next token is a `)', then there is no attribute
16749 attribute_list = NULL;
16751 /* Look for the two `)' tokens. */
16752 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16753 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16755 /* Add these new attributes to the list. */
16756 attributes = chainon (attributes, attribute_list);
16762 /* Parse an attribute-list.
16766 attribute-list , attribute
16770 identifier ( identifier )
16771 identifier ( identifier , expression-list )
16772 identifier ( expression-list )
16774 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16775 to an attribute. The TREE_PURPOSE of each node is the identifier
16776 indicating which attribute is in use. The TREE_VALUE represents
16777 the arguments, if any. */
16780 cp_parser_attribute_list (cp_parser* parser)
16782 tree attribute_list = NULL_TREE;
16783 bool save_translate_strings_p = parser->translate_strings_p;
16785 parser->translate_strings_p = false;
16792 /* Look for the identifier. We also allow keywords here; for
16793 example `__attribute__ ((const))' is legal. */
16794 token = cp_lexer_peek_token (parser->lexer);
16795 if (token->type == CPP_NAME
16796 || token->type == CPP_KEYWORD)
16798 tree arguments = NULL_TREE;
16800 /* Consume the token. */
16801 token = cp_lexer_consume_token (parser->lexer);
16803 /* Save away the identifier that indicates which attribute
16805 identifier = token->u.value;
16806 attribute = build_tree_list (identifier, NULL_TREE);
16808 /* Peek at the next token. */
16809 token = cp_lexer_peek_token (parser->lexer);
16810 /* If it's an `(', then parse the attribute arguments. */
16811 if (token->type == CPP_OPEN_PAREN)
16813 arguments = cp_parser_parenthesized_expression_list
16814 (parser, true, /*cast_p=*/false,
16815 /*allow_expansion_p=*/false,
16816 /*non_constant_p=*/NULL);
16817 /* Save the arguments away. */
16818 TREE_VALUE (attribute) = arguments;
16821 if (arguments != error_mark_node)
16823 /* Add this attribute to the list. */
16824 TREE_CHAIN (attribute) = attribute_list;
16825 attribute_list = attribute;
16828 token = cp_lexer_peek_token (parser->lexer);
16830 /* Now, look for more attributes. If the next token isn't a
16831 `,', we're done. */
16832 if (token->type != CPP_COMMA)
16835 /* Consume the comma and keep going. */
16836 cp_lexer_consume_token (parser->lexer);
16838 parser->translate_strings_p = save_translate_strings_p;
16840 /* We built up the list in reverse order. */
16841 return nreverse (attribute_list);
16844 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16845 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16846 current value of the PEDANTIC flag, regardless of whether or not
16847 the `__extension__' keyword is present. The caller is responsible
16848 for restoring the value of the PEDANTIC flag. */
16851 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16853 /* Save the old value of the PEDANTIC flag. */
16854 *saved_pedantic = pedantic;
16856 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16858 /* Consume the `__extension__' token. */
16859 cp_lexer_consume_token (parser->lexer);
16860 /* We're not being pedantic while the `__extension__' keyword is
16870 /* Parse a label declaration.
16873 __label__ label-declarator-seq ;
16875 label-declarator-seq:
16876 identifier , label-declarator-seq
16880 cp_parser_label_declaration (cp_parser* parser)
16882 /* Look for the `__label__' keyword. */
16883 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16889 /* Look for an identifier. */
16890 identifier = cp_parser_identifier (parser);
16891 /* If we failed, stop. */
16892 if (identifier == error_mark_node)
16894 /* Declare it as a label. */
16895 finish_label_decl (identifier);
16896 /* If the next token is a `;', stop. */
16897 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16899 /* Look for the `,' separating the label declarations. */
16900 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16903 /* Look for the final `;'. */
16904 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16907 /* Support Functions */
16909 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16910 NAME should have one of the representations used for an
16911 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16912 is returned. If PARSER->SCOPE is a dependent type, then a
16913 SCOPE_REF is returned.
16915 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16916 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16917 was formed. Abstractly, such entities should not be passed to this
16918 function, because they do not need to be looked up, but it is
16919 simpler to check for this special case here, rather than at the
16922 In cases not explicitly covered above, this function returns a
16923 DECL, OVERLOAD, or baselink representing the result of the lookup.
16924 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16927 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16928 (e.g., "struct") that was used. In that case bindings that do not
16929 refer to types are ignored.
16931 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16934 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16937 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16940 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16941 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16942 NULL_TREE otherwise. */
16945 cp_parser_lookup_name (cp_parser *parser, tree name,
16946 enum tag_types tag_type,
16949 bool check_dependency,
16950 tree *ambiguous_decls,
16951 location_t name_location)
16955 tree object_type = parser->context->object_type;
16957 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16958 flags |= LOOKUP_COMPLAIN;
16960 /* Assume that the lookup will be unambiguous. */
16961 if (ambiguous_decls)
16962 *ambiguous_decls = NULL_TREE;
16964 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16965 no longer valid. Note that if we are parsing tentatively, and
16966 the parse fails, OBJECT_TYPE will be automatically restored. */
16967 parser->context->object_type = NULL_TREE;
16969 if (name == error_mark_node)
16970 return error_mark_node;
16972 /* A template-id has already been resolved; there is no lookup to
16974 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16976 if (BASELINK_P (name))
16978 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16979 == TEMPLATE_ID_EXPR);
16983 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16984 it should already have been checked to make sure that the name
16985 used matches the type being destroyed. */
16986 if (TREE_CODE (name) == BIT_NOT_EXPR)
16990 /* Figure out to which type this destructor applies. */
16992 type = parser->scope;
16993 else if (object_type)
16994 type = object_type;
16996 type = current_class_type;
16997 /* If that's not a class type, there is no destructor. */
16998 if (!type || !CLASS_TYPE_P (type))
16999 return error_mark_node;
17000 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17001 lazily_declare_fn (sfk_destructor, type);
17002 if (!CLASSTYPE_DESTRUCTORS (type))
17003 return error_mark_node;
17004 /* If it was a class type, return the destructor. */
17005 return CLASSTYPE_DESTRUCTORS (type);
17008 /* By this point, the NAME should be an ordinary identifier. If
17009 the id-expression was a qualified name, the qualifying scope is
17010 stored in PARSER->SCOPE at this point. */
17011 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17013 /* Perform the lookup. */
17018 if (parser->scope == error_mark_node)
17019 return error_mark_node;
17021 /* If the SCOPE is dependent, the lookup must be deferred until
17022 the template is instantiated -- unless we are explicitly
17023 looking up names in uninstantiated templates. Even then, we
17024 cannot look up the name if the scope is not a class type; it
17025 might, for example, be a template type parameter. */
17026 dependent_p = (TYPE_P (parser->scope)
17027 && dependent_scope_p (parser->scope));
17028 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17030 /* Defer lookup. */
17031 decl = error_mark_node;
17034 tree pushed_scope = NULL_TREE;
17036 /* If PARSER->SCOPE is a dependent type, then it must be a
17037 class type, and we must not be checking dependencies;
17038 otherwise, we would have processed this lookup above. So
17039 that PARSER->SCOPE is not considered a dependent base by
17040 lookup_member, we must enter the scope here. */
17042 pushed_scope = push_scope (parser->scope);
17043 /* If the PARSER->SCOPE is a template specialization, it
17044 may be instantiated during name lookup. In that case,
17045 errors may be issued. Even if we rollback the current
17046 tentative parse, those errors are valid. */
17047 decl = lookup_qualified_name (parser->scope, name,
17048 tag_type != none_type,
17049 /*complain=*/true);
17051 /* If we have a single function from a using decl, pull it out. */
17052 if (TREE_CODE (decl) == OVERLOAD
17053 && !really_overloaded_fn (decl))
17054 decl = OVL_FUNCTION (decl);
17057 pop_scope (pushed_scope);
17060 /* If the scope is a dependent type and either we deferred lookup or
17061 we did lookup but didn't find the name, rememeber the name. */
17062 if (decl == error_mark_node && TYPE_P (parser->scope)
17063 && dependent_type_p (parser->scope))
17069 /* The resolution to Core Issue 180 says that `struct
17070 A::B' should be considered a type-name, even if `A'
17072 type = make_typename_type (parser->scope, name, tag_type,
17073 /*complain=*/tf_error);
17074 decl = TYPE_NAME (type);
17076 else if (is_template
17077 && (cp_parser_next_token_ends_template_argument_p (parser)
17078 || cp_lexer_next_token_is (parser->lexer,
17080 decl = make_unbound_class_template (parser->scope,
17082 /*complain=*/tf_error);
17084 decl = build_qualified_name (/*type=*/NULL_TREE,
17085 parser->scope, name,
17088 parser->qualifying_scope = parser->scope;
17089 parser->object_scope = NULL_TREE;
17091 else if (object_type)
17093 tree object_decl = NULL_TREE;
17094 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17095 OBJECT_TYPE is not a class. */
17096 if (CLASS_TYPE_P (object_type))
17097 /* If the OBJECT_TYPE is a template specialization, it may
17098 be instantiated during name lookup. In that case, errors
17099 may be issued. Even if we rollback the current tentative
17100 parse, those errors are valid. */
17101 object_decl = lookup_member (object_type,
17104 tag_type != none_type);
17105 /* Look it up in the enclosing context, too. */
17106 decl = lookup_name_real (name, tag_type != none_type,
17108 /*block_p=*/true, is_namespace, flags);
17109 parser->object_scope = object_type;
17110 parser->qualifying_scope = NULL_TREE;
17112 decl = object_decl;
17116 decl = lookup_name_real (name, tag_type != none_type,
17118 /*block_p=*/true, is_namespace, flags);
17119 parser->qualifying_scope = NULL_TREE;
17120 parser->object_scope = NULL_TREE;
17123 /* If the lookup failed, let our caller know. */
17124 if (!decl || decl == error_mark_node)
17125 return error_mark_node;
17127 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17128 if (TREE_CODE (decl) == TREE_LIST)
17130 if (ambiguous_decls)
17131 *ambiguous_decls = decl;
17132 /* The error message we have to print is too complicated for
17133 cp_parser_error, so we incorporate its actions directly. */
17134 if (!cp_parser_simulate_error (parser))
17136 error ("%Hreference to %qD is ambiguous",
17137 &name_location, name);
17138 print_candidates (decl);
17140 return error_mark_node;
17143 gcc_assert (DECL_P (decl)
17144 || TREE_CODE (decl) == OVERLOAD
17145 || TREE_CODE (decl) == SCOPE_REF
17146 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17147 || BASELINK_P (decl));
17149 /* If we have resolved the name of a member declaration, check to
17150 see if the declaration is accessible. When the name resolves to
17151 set of overloaded functions, accessibility is checked when
17152 overload resolution is done.
17154 During an explicit instantiation, access is not checked at all,
17155 as per [temp.explicit]. */
17157 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17162 /* Like cp_parser_lookup_name, but for use in the typical case where
17163 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17164 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17167 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17169 return cp_parser_lookup_name (parser, name,
17171 /*is_template=*/false,
17172 /*is_namespace=*/false,
17173 /*check_dependency=*/true,
17174 /*ambiguous_decls=*/NULL,
17178 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17179 the current context, return the TYPE_DECL. If TAG_NAME_P is
17180 true, the DECL indicates the class being defined in a class-head,
17181 or declared in an elaborated-type-specifier.
17183 Otherwise, return DECL. */
17186 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17188 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17189 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17192 template <typename T> struct B;
17195 template <typename T> struct A::B {};
17197 Similarly, in an elaborated-type-specifier:
17199 namespace N { struct X{}; }
17202 template <typename T> friend struct N::X;
17205 However, if the DECL refers to a class type, and we are in
17206 the scope of the class, then the name lookup automatically
17207 finds the TYPE_DECL created by build_self_reference rather
17208 than a TEMPLATE_DECL. For example, in:
17210 template <class T> struct S {
17214 there is no need to handle such case. */
17216 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17217 return DECL_TEMPLATE_RESULT (decl);
17222 /* If too many, or too few, template-parameter lists apply to the
17223 declarator, issue an error message. Returns TRUE if all went well,
17224 and FALSE otherwise. */
17227 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17228 cp_declarator *declarator,
17229 location_t declarator_location)
17231 unsigned num_templates;
17233 /* We haven't seen any classes that involve template parameters yet. */
17236 switch (declarator->kind)
17239 if (declarator->u.id.qualifying_scope)
17244 scope = declarator->u.id.qualifying_scope;
17245 member = declarator->u.id.unqualified_name;
17247 while (scope && CLASS_TYPE_P (scope))
17249 /* You're supposed to have one `template <...>'
17250 for every template class, but you don't need one
17251 for a full specialization. For example:
17253 template <class T> struct S{};
17254 template <> struct S<int> { void f(); };
17255 void S<int>::f () {}
17257 is correct; there shouldn't be a `template <>' for
17258 the definition of `S<int>::f'. */
17259 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17260 /* If SCOPE does not have template information of any
17261 kind, then it is not a template, nor is it nested
17262 within a template. */
17264 if (explicit_class_specialization_p (scope))
17266 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17269 scope = TYPE_CONTEXT (scope);
17272 else if (TREE_CODE (declarator->u.id.unqualified_name)
17273 == TEMPLATE_ID_EXPR)
17274 /* If the DECLARATOR has the form `X<y>' then it uses one
17275 additional level of template parameters. */
17278 return cp_parser_check_template_parameters (parser,
17280 declarator_location);
17285 case cdk_reference:
17287 return (cp_parser_check_declarator_template_parameters
17288 (parser, declarator->declarator, declarator_location));
17294 gcc_unreachable ();
17299 /* NUM_TEMPLATES were used in the current declaration. If that is
17300 invalid, return FALSE and issue an error messages. Otherwise,
17304 cp_parser_check_template_parameters (cp_parser* parser,
17305 unsigned num_templates,
17306 location_t location)
17308 /* If there are more template classes than parameter lists, we have
17311 template <class T> void S<T>::R<T>::f (); */
17312 if (parser->num_template_parameter_lists < num_templates)
17314 error ("%Htoo few template-parameter-lists", &location);
17317 /* If there are the same number of template classes and parameter
17318 lists, that's OK. */
17319 if (parser->num_template_parameter_lists == num_templates)
17321 /* If there are more, but only one more, then we are referring to a
17322 member template. That's OK too. */
17323 if (parser->num_template_parameter_lists == num_templates + 1)
17325 /* Otherwise, there are too many template parameter lists. We have
17328 template <class T> template <class U> void S::f(); */
17329 error ("%Htoo many template-parameter-lists", &location);
17333 /* Parse an optional `::' token indicating that the following name is
17334 from the global namespace. If so, PARSER->SCOPE is set to the
17335 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17336 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17337 Returns the new value of PARSER->SCOPE, if the `::' token is
17338 present, and NULL_TREE otherwise. */
17341 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17345 /* Peek at the next token. */
17346 token = cp_lexer_peek_token (parser->lexer);
17347 /* If we're looking at a `::' token then we're starting from the
17348 global namespace, not our current location. */
17349 if (token->type == CPP_SCOPE)
17351 /* Consume the `::' token. */
17352 cp_lexer_consume_token (parser->lexer);
17353 /* Set the SCOPE so that we know where to start the lookup. */
17354 parser->scope = global_namespace;
17355 parser->qualifying_scope = global_namespace;
17356 parser->object_scope = NULL_TREE;
17358 return parser->scope;
17360 else if (!current_scope_valid_p)
17362 parser->scope = NULL_TREE;
17363 parser->qualifying_scope = NULL_TREE;
17364 parser->object_scope = NULL_TREE;
17370 /* Returns TRUE if the upcoming token sequence is the start of a
17371 constructor declarator. If FRIEND_P is true, the declarator is
17372 preceded by the `friend' specifier. */
17375 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17377 bool constructor_p;
17378 tree type_decl = NULL_TREE;
17379 bool nested_name_p;
17380 cp_token *next_token;
17382 /* The common case is that this is not a constructor declarator, so
17383 try to avoid doing lots of work if at all possible. It's not
17384 valid declare a constructor at function scope. */
17385 if (parser->in_function_body)
17387 /* And only certain tokens can begin a constructor declarator. */
17388 next_token = cp_lexer_peek_token (parser->lexer);
17389 if (next_token->type != CPP_NAME
17390 && next_token->type != CPP_SCOPE
17391 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17392 && next_token->type != CPP_TEMPLATE_ID)
17395 /* Parse tentatively; we are going to roll back all of the tokens
17397 cp_parser_parse_tentatively (parser);
17398 /* Assume that we are looking at a constructor declarator. */
17399 constructor_p = true;
17401 /* Look for the optional `::' operator. */
17402 cp_parser_global_scope_opt (parser,
17403 /*current_scope_valid_p=*/false);
17404 /* Look for the nested-name-specifier. */
17406 = (cp_parser_nested_name_specifier_opt (parser,
17407 /*typename_keyword_p=*/false,
17408 /*check_dependency_p=*/false,
17410 /*is_declaration=*/false)
17412 /* Outside of a class-specifier, there must be a
17413 nested-name-specifier. */
17414 if (!nested_name_p &&
17415 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17417 constructor_p = false;
17418 /* If we still think that this might be a constructor-declarator,
17419 look for a class-name. */
17424 template <typename T> struct S { S(); };
17425 template <typename T> S<T>::S ();
17427 we must recognize that the nested `S' names a class.
17430 template <typename T> S<T>::S<T> ();
17432 we must recognize that the nested `S' names a template. */
17433 type_decl = cp_parser_class_name (parser,
17434 /*typename_keyword_p=*/false,
17435 /*template_keyword_p=*/false,
17437 /*check_dependency_p=*/false,
17438 /*class_head_p=*/false,
17439 /*is_declaration=*/false);
17440 /* If there was no class-name, then this is not a constructor. */
17441 constructor_p = !cp_parser_error_occurred (parser);
17444 /* If we're still considering a constructor, we have to see a `(',
17445 to begin the parameter-declaration-clause, followed by either a
17446 `)', an `...', or a decl-specifier. We need to check for a
17447 type-specifier to avoid being fooled into thinking that:
17451 is a constructor. (It is actually a function named `f' that
17452 takes one parameter (of type `int') and returns a value of type
17455 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17458 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17459 /* A parameter declaration begins with a decl-specifier,
17460 which is either the "attribute" keyword, a storage class
17461 specifier, or (usually) a type-specifier. */
17462 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17465 tree pushed_scope = NULL_TREE;
17466 unsigned saved_num_template_parameter_lists;
17468 /* Names appearing in the type-specifier should be looked up
17469 in the scope of the class. */
17470 if (current_class_type)
17474 type = TREE_TYPE (type_decl);
17475 if (TREE_CODE (type) == TYPENAME_TYPE)
17477 type = resolve_typename_type (type,
17478 /*only_current_p=*/false);
17479 if (TREE_CODE (type) == TYPENAME_TYPE)
17481 cp_parser_abort_tentative_parse (parser);
17485 pushed_scope = push_scope (type);
17488 /* Inside the constructor parameter list, surrounding
17489 template-parameter-lists do not apply. */
17490 saved_num_template_parameter_lists
17491 = parser->num_template_parameter_lists;
17492 parser->num_template_parameter_lists = 0;
17494 /* Look for the type-specifier. */
17495 cp_parser_type_specifier (parser,
17496 CP_PARSER_FLAGS_NONE,
17497 /*decl_specs=*/NULL,
17498 /*is_declarator=*/true,
17499 /*declares_class_or_enum=*/NULL,
17500 /*is_cv_qualifier=*/NULL);
17502 parser->num_template_parameter_lists
17503 = saved_num_template_parameter_lists;
17505 /* Leave the scope of the class. */
17507 pop_scope (pushed_scope);
17509 constructor_p = !cp_parser_error_occurred (parser);
17513 constructor_p = false;
17514 /* We did not really want to consume any tokens. */
17515 cp_parser_abort_tentative_parse (parser);
17517 return constructor_p;
17520 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17521 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17522 they must be performed once we are in the scope of the function.
17524 Returns the function defined. */
17527 cp_parser_function_definition_from_specifiers_and_declarator
17528 (cp_parser* parser,
17529 cp_decl_specifier_seq *decl_specifiers,
17531 const cp_declarator *declarator)
17536 /* Begin the function-definition. */
17537 success_p = start_function (decl_specifiers, declarator, attributes);
17539 /* The things we're about to see are not directly qualified by any
17540 template headers we've seen thus far. */
17541 reset_specialization ();
17543 /* If there were names looked up in the decl-specifier-seq that we
17544 did not check, check them now. We must wait until we are in the
17545 scope of the function to perform the checks, since the function
17546 might be a friend. */
17547 perform_deferred_access_checks ();
17551 /* Skip the entire function. */
17552 cp_parser_skip_to_end_of_block_or_statement (parser);
17553 fn = error_mark_node;
17555 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17557 /* Seen already, skip it. An error message has already been output. */
17558 cp_parser_skip_to_end_of_block_or_statement (parser);
17559 fn = current_function_decl;
17560 current_function_decl = NULL_TREE;
17561 /* If this is a function from a class, pop the nested class. */
17562 if (current_class_name)
17563 pop_nested_class ();
17566 fn = cp_parser_function_definition_after_declarator (parser,
17567 /*inline_p=*/false);
17572 /* Parse the part of a function-definition that follows the
17573 declarator. INLINE_P is TRUE iff this function is an inline
17574 function defined with a class-specifier.
17576 Returns the function defined. */
17579 cp_parser_function_definition_after_declarator (cp_parser* parser,
17583 bool ctor_initializer_p = false;
17584 bool saved_in_unbraced_linkage_specification_p;
17585 bool saved_in_function_body;
17586 unsigned saved_num_template_parameter_lists;
17589 saved_in_function_body = parser->in_function_body;
17590 parser->in_function_body = true;
17591 /* If the next token is `return', then the code may be trying to
17592 make use of the "named return value" extension that G++ used to
17594 token = cp_lexer_peek_token (parser->lexer);
17595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17597 /* Consume the `return' keyword. */
17598 cp_lexer_consume_token (parser->lexer);
17599 /* Look for the identifier that indicates what value is to be
17601 cp_parser_identifier (parser);
17602 /* Issue an error message. */
17603 error ("%Hnamed return values are no longer supported",
17605 /* Skip tokens until we reach the start of the function body. */
17608 cp_token *token = cp_lexer_peek_token (parser->lexer);
17609 if (token->type == CPP_OPEN_BRACE
17610 || token->type == CPP_EOF
17611 || token->type == CPP_PRAGMA_EOL)
17613 cp_lexer_consume_token (parser->lexer);
17616 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17617 anything declared inside `f'. */
17618 saved_in_unbraced_linkage_specification_p
17619 = parser->in_unbraced_linkage_specification_p;
17620 parser->in_unbraced_linkage_specification_p = false;
17621 /* Inside the function, surrounding template-parameter-lists do not
17623 saved_num_template_parameter_lists
17624 = parser->num_template_parameter_lists;
17625 parser->num_template_parameter_lists = 0;
17626 /* If the next token is `try', then we are looking at a
17627 function-try-block. */
17628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17629 ctor_initializer_p = cp_parser_function_try_block (parser);
17630 /* A function-try-block includes the function-body, so we only do
17631 this next part if we're not processing a function-try-block. */
17634 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17636 /* Finish the function. */
17637 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17638 (inline_p ? 2 : 0));
17639 /* Generate code for it, if necessary. */
17640 expand_or_defer_fn (fn);
17641 /* Restore the saved values. */
17642 parser->in_unbraced_linkage_specification_p
17643 = saved_in_unbraced_linkage_specification_p;
17644 parser->num_template_parameter_lists
17645 = saved_num_template_parameter_lists;
17646 parser->in_function_body = saved_in_function_body;
17651 /* Parse a template-declaration, assuming that the `export' (and
17652 `extern') keywords, if present, has already been scanned. MEMBER_P
17653 is as for cp_parser_template_declaration. */
17656 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17658 tree decl = NULL_TREE;
17659 VEC (deferred_access_check,gc) *checks;
17660 tree parameter_list;
17661 bool friend_p = false;
17662 bool need_lang_pop;
17665 /* Look for the `template' keyword. */
17666 token = cp_lexer_peek_token (parser->lexer);
17667 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17671 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17673 if (at_class_scope_p () && current_function_decl)
17675 /* 14.5.2.2 [temp.mem]
17677 A local class shall not have member templates. */
17678 error ("%Hinvalid declaration of member template in local class",
17680 cp_parser_skip_to_end_of_block_or_statement (parser);
17685 A template ... shall not have C linkage. */
17686 if (current_lang_name == lang_name_c)
17688 error ("%Htemplate with C linkage", &token->location);
17689 /* Give it C++ linkage to avoid confusing other parts of the
17691 push_lang_context (lang_name_cplusplus);
17692 need_lang_pop = true;
17695 need_lang_pop = false;
17697 /* We cannot perform access checks on the template parameter
17698 declarations until we know what is being declared, just as we
17699 cannot check the decl-specifier list. */
17700 push_deferring_access_checks (dk_deferred);
17702 /* If the next token is `>', then we have an invalid
17703 specialization. Rather than complain about an invalid template
17704 parameter, issue an error message here. */
17705 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17707 cp_parser_error (parser, "invalid explicit specialization");
17708 begin_specialization ();
17709 parameter_list = NULL_TREE;
17712 /* Parse the template parameters. */
17713 parameter_list = cp_parser_template_parameter_list (parser);
17715 /* Get the deferred access checks from the parameter list. These
17716 will be checked once we know what is being declared, as for a
17717 member template the checks must be performed in the scope of the
17718 class containing the member. */
17719 checks = get_deferred_access_checks ();
17721 /* Look for the `>'. */
17722 cp_parser_skip_to_end_of_template_parameter_list (parser);
17723 /* We just processed one more parameter list. */
17724 ++parser->num_template_parameter_lists;
17725 /* If the next token is `template', there are more template
17727 if (cp_lexer_next_token_is_keyword (parser->lexer,
17729 cp_parser_template_declaration_after_export (parser, member_p);
17732 /* There are no access checks when parsing a template, as we do not
17733 know if a specialization will be a friend. */
17734 push_deferring_access_checks (dk_no_check);
17735 token = cp_lexer_peek_token (parser->lexer);
17736 decl = cp_parser_single_declaration (parser,
17739 /*explicit_specialization_p=*/false,
17741 pop_deferring_access_checks ();
17743 /* If this is a member template declaration, let the front
17745 if (member_p && !friend_p && decl)
17747 if (TREE_CODE (decl) == TYPE_DECL)
17748 cp_parser_check_access_in_redeclaration (decl, token->location);
17750 decl = finish_member_template_decl (decl);
17752 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17753 make_friend_class (current_class_type, TREE_TYPE (decl),
17754 /*complain=*/true);
17756 /* We are done with the current parameter list. */
17757 --parser->num_template_parameter_lists;
17759 pop_deferring_access_checks ();
17762 finish_template_decl (parameter_list);
17764 /* Register member declarations. */
17765 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17766 finish_member_declaration (decl);
17767 /* For the erroneous case of a template with C linkage, we pushed an
17768 implicit C++ linkage scope; exit that scope now. */
17770 pop_lang_context ();
17771 /* If DECL is a function template, we must return to parse it later.
17772 (Even though there is no definition, there might be default
17773 arguments that need handling.) */
17774 if (member_p && decl
17775 && (TREE_CODE (decl) == FUNCTION_DECL
17776 || DECL_FUNCTION_TEMPLATE_P (decl)))
17777 TREE_VALUE (parser->unparsed_functions_queues)
17778 = tree_cons (NULL_TREE, decl,
17779 TREE_VALUE (parser->unparsed_functions_queues));
17782 /* Perform the deferred access checks from a template-parameter-list.
17783 CHECKS is a TREE_LIST of access checks, as returned by
17784 get_deferred_access_checks. */
17787 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17789 ++processing_template_parmlist;
17790 perform_access_checks (checks);
17791 --processing_template_parmlist;
17794 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17795 `function-definition' sequence. MEMBER_P is true, this declaration
17796 appears in a class scope.
17798 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17799 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17802 cp_parser_single_declaration (cp_parser* parser,
17803 VEC (deferred_access_check,gc)* checks,
17805 bool explicit_specialization_p,
17808 int declares_class_or_enum;
17809 tree decl = NULL_TREE;
17810 cp_decl_specifier_seq decl_specifiers;
17811 bool function_definition_p = false;
17812 cp_token *decl_spec_token_start;
17814 /* This function is only used when processing a template
17816 gcc_assert (innermost_scope_kind () == sk_template_parms
17817 || innermost_scope_kind () == sk_template_spec);
17819 /* Defer access checks until we know what is being declared. */
17820 push_deferring_access_checks (dk_deferred);
17822 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17824 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17825 cp_parser_decl_specifier_seq (parser,
17826 CP_PARSER_FLAGS_OPTIONAL,
17828 &declares_class_or_enum);
17830 *friend_p = cp_parser_friend_p (&decl_specifiers);
17832 /* There are no template typedefs. */
17833 if (decl_specifiers.specs[(int) ds_typedef])
17835 error ("%Htemplate declaration of %qs",
17836 &decl_spec_token_start->location, "typedef");
17837 decl = error_mark_node;
17840 /* Gather up the access checks that occurred the
17841 decl-specifier-seq. */
17842 stop_deferring_access_checks ();
17844 /* Check for the declaration of a template class. */
17845 if (declares_class_or_enum)
17847 if (cp_parser_declares_only_class_p (parser))
17849 decl = shadow_tag (&decl_specifiers);
17854 friend template <typename T> struct A<T>::B;
17857 A<T>::B will be represented by a TYPENAME_TYPE, and
17858 therefore not recognized by shadow_tag. */
17859 if (friend_p && *friend_p
17861 && decl_specifiers.type
17862 && TYPE_P (decl_specifiers.type))
17863 decl = decl_specifiers.type;
17865 if (decl && decl != error_mark_node)
17866 decl = TYPE_NAME (decl);
17868 decl = error_mark_node;
17870 /* Perform access checks for template parameters. */
17871 cp_parser_perform_template_parameter_access_checks (checks);
17874 /* If it's not a template class, try for a template function. If
17875 the next token is a `;', then this declaration does not declare
17876 anything. But, if there were errors in the decl-specifiers, then
17877 the error might well have come from an attempted class-specifier.
17878 In that case, there's no need to warn about a missing declarator. */
17880 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17881 || decl_specifiers.type != error_mark_node))
17883 decl = cp_parser_init_declarator (parser,
17886 /*function_definition_allowed_p=*/true,
17888 declares_class_or_enum,
17889 &function_definition_p);
17891 /* 7.1.1-1 [dcl.stc]
17893 A storage-class-specifier shall not be specified in an explicit
17894 specialization... */
17896 && explicit_specialization_p
17897 && decl_specifiers.storage_class != sc_none)
17899 error ("%Hexplicit template specialization cannot have a storage class",
17900 &decl_spec_token_start->location);
17901 decl = error_mark_node;
17905 pop_deferring_access_checks ();
17907 /* Clear any current qualification; whatever comes next is the start
17908 of something new. */
17909 parser->scope = NULL_TREE;
17910 parser->qualifying_scope = NULL_TREE;
17911 parser->object_scope = NULL_TREE;
17912 /* Look for a trailing `;' after the declaration. */
17913 if (!function_definition_p
17914 && (decl == error_mark_node
17915 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17916 cp_parser_skip_to_end_of_block_or_statement (parser);
17921 /* Parse a cast-expression that is not the operand of a unary "&". */
17924 cp_parser_simple_cast_expression (cp_parser *parser)
17926 return cp_parser_cast_expression (parser, /*address_p=*/false,
17927 /*cast_p=*/false, NULL);
17930 /* Parse a functional cast to TYPE. Returns an expression
17931 representing the cast. */
17934 cp_parser_functional_cast (cp_parser* parser, tree type)
17936 tree expression_list;
17940 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17942 maybe_warn_cpp0x ("extended initializer lists");
17943 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17944 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17945 if (TREE_CODE (type) == TYPE_DECL)
17946 type = TREE_TYPE (type);
17947 return finish_compound_literal (type, expression_list);
17951 = cp_parser_parenthesized_expression_list (parser, false,
17953 /*allow_expansion_p=*/true,
17954 /*non_constant_p=*/NULL);
17956 cast = build_functional_cast (type, expression_list,
17957 tf_warning_or_error);
17958 /* [expr.const]/1: In an integral constant expression "only type
17959 conversions to integral or enumeration type can be used". */
17960 if (TREE_CODE (type) == TYPE_DECL)
17961 type = TREE_TYPE (type);
17962 if (cast != error_mark_node
17963 && !cast_valid_in_integral_constant_expression_p (type)
17964 && (cp_parser_non_integral_constant_expression
17965 (parser, "a call to a constructor")))
17966 return error_mark_node;
17970 /* Save the tokens that make up the body of a member function defined
17971 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17972 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17973 specifiers applied to the declaration. Returns the FUNCTION_DECL
17974 for the member function. */
17977 cp_parser_save_member_function_body (cp_parser* parser,
17978 cp_decl_specifier_seq *decl_specifiers,
17979 cp_declarator *declarator,
17986 /* Create the function-declaration. */
17987 fn = start_method (decl_specifiers, declarator, attributes);
17988 /* If something went badly wrong, bail out now. */
17989 if (fn == error_mark_node)
17991 /* If there's a function-body, skip it. */
17992 if (cp_parser_token_starts_function_definition_p
17993 (cp_lexer_peek_token (parser->lexer)))
17994 cp_parser_skip_to_end_of_block_or_statement (parser);
17995 return error_mark_node;
17998 /* Remember it, if there default args to post process. */
17999 cp_parser_save_default_args (parser, fn);
18001 /* Save away the tokens that make up the body of the
18003 first = parser->lexer->next_token;
18004 /* We can have braced-init-list mem-initializers before the fn body. */
18005 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18007 cp_lexer_consume_token (parser->lexer);
18008 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18009 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18011 /* cache_group will stop after an un-nested { } pair, too. */
18012 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18015 /* variadic mem-inits have ... after the ')'. */
18016 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18017 cp_lexer_consume_token (parser->lexer);
18020 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18021 /* Handle function try blocks. */
18022 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18023 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18024 last = parser->lexer->next_token;
18026 /* Save away the inline definition; we will process it when the
18027 class is complete. */
18028 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18029 DECL_PENDING_INLINE_P (fn) = 1;
18031 /* We need to know that this was defined in the class, so that
18032 friend templates are handled correctly. */
18033 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18035 /* We're done with the inline definition. */
18036 finish_method (fn);
18038 /* Add FN to the queue of functions to be parsed later. */
18039 TREE_VALUE (parser->unparsed_functions_queues)
18040 = tree_cons (NULL_TREE, fn,
18041 TREE_VALUE (parser->unparsed_functions_queues));
18046 /* Parse a template-argument-list, as well as the trailing ">" (but
18047 not the opening ">"). See cp_parser_template_argument_list for the
18051 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18055 tree saved_qualifying_scope;
18056 tree saved_object_scope;
18057 bool saved_greater_than_is_operator_p;
18058 bool saved_skip_evaluation;
18062 When parsing a template-id, the first non-nested `>' is taken as
18063 the end of the template-argument-list rather than a greater-than
18065 saved_greater_than_is_operator_p
18066 = parser->greater_than_is_operator_p;
18067 parser->greater_than_is_operator_p = false;
18068 /* Parsing the argument list may modify SCOPE, so we save it
18070 saved_scope = parser->scope;
18071 saved_qualifying_scope = parser->qualifying_scope;
18072 saved_object_scope = parser->object_scope;
18073 /* We need to evaluate the template arguments, even though this
18074 template-id may be nested within a "sizeof". */
18075 saved_skip_evaluation = skip_evaluation;
18076 skip_evaluation = false;
18077 /* Parse the template-argument-list itself. */
18078 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18079 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18080 arguments = NULL_TREE;
18082 arguments = cp_parser_template_argument_list (parser);
18083 /* Look for the `>' that ends the template-argument-list. If we find
18084 a '>>' instead, it's probably just a typo. */
18085 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18087 if (cxx_dialect != cxx98)
18089 /* In C++0x, a `>>' in a template argument list or cast
18090 expression is considered to be two separate `>'
18091 tokens. So, change the current token to a `>', but don't
18092 consume it: it will be consumed later when the outer
18093 template argument list (or cast expression) is parsed.
18094 Note that this replacement of `>' for `>>' is necessary
18095 even if we are parsing tentatively: in the tentative
18096 case, after calling
18097 cp_parser_enclosed_template_argument_list we will always
18098 throw away all of the template arguments and the first
18099 closing `>', either because the template argument list
18100 was erroneous or because we are replacing those tokens
18101 with a CPP_TEMPLATE_ID token. The second `>' (which will
18102 not have been thrown away) is needed either to close an
18103 outer template argument list or to complete a new-style
18105 cp_token *token = cp_lexer_peek_token (parser->lexer);
18106 token->type = CPP_GREATER;
18108 else if (!saved_greater_than_is_operator_p)
18110 /* If we're in a nested template argument list, the '>>' has
18111 to be a typo for '> >'. We emit the error message, but we
18112 continue parsing and we push a '>' as next token, so that
18113 the argument list will be parsed correctly. Note that the
18114 global source location is still on the token before the
18115 '>>', so we need to say explicitly where we want it. */
18116 cp_token *token = cp_lexer_peek_token (parser->lexer);
18117 error ("%H%<>>%> should be %<> >%> "
18118 "within a nested template argument list",
18121 token->type = CPP_GREATER;
18125 /* If this is not a nested template argument list, the '>>'
18126 is a typo for '>'. Emit an error message and continue.
18127 Same deal about the token location, but here we can get it
18128 right by consuming the '>>' before issuing the diagnostic. */
18129 cp_token *token = cp_lexer_consume_token (parser->lexer);
18130 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18131 "a template argument list", &token->location);
18135 cp_parser_skip_to_end_of_template_parameter_list (parser);
18136 /* The `>' token might be a greater-than operator again now. */
18137 parser->greater_than_is_operator_p
18138 = saved_greater_than_is_operator_p;
18139 /* Restore the SAVED_SCOPE. */
18140 parser->scope = saved_scope;
18141 parser->qualifying_scope = saved_qualifying_scope;
18142 parser->object_scope = saved_object_scope;
18143 skip_evaluation = saved_skip_evaluation;
18148 /* MEMBER_FUNCTION is a member function, or a friend. If default
18149 arguments, or the body of the function have not yet been parsed,
18153 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18155 /* If this member is a template, get the underlying
18157 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18158 member_function = DECL_TEMPLATE_RESULT (member_function);
18160 /* There should not be any class definitions in progress at this
18161 point; the bodies of members are only parsed outside of all class
18163 gcc_assert (parser->num_classes_being_defined == 0);
18164 /* While we're parsing the member functions we might encounter more
18165 classes. We want to handle them right away, but we don't want
18166 them getting mixed up with functions that are currently in the
18168 parser->unparsed_functions_queues
18169 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18171 /* Make sure that any template parameters are in scope. */
18172 maybe_begin_member_template_processing (member_function);
18174 /* If the body of the function has not yet been parsed, parse it
18176 if (DECL_PENDING_INLINE_P (member_function))
18178 tree function_scope;
18179 cp_token_cache *tokens;
18181 /* The function is no longer pending; we are processing it. */
18182 tokens = DECL_PENDING_INLINE_INFO (member_function);
18183 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18184 DECL_PENDING_INLINE_P (member_function) = 0;
18186 /* If this is a local class, enter the scope of the containing
18188 function_scope = current_function_decl;
18189 if (function_scope)
18190 push_function_context ();
18192 /* Push the body of the function onto the lexer stack. */
18193 cp_parser_push_lexer_for_tokens (parser, tokens);
18195 /* Let the front end know that we going to be defining this
18197 start_preparsed_function (member_function, NULL_TREE,
18198 SF_PRE_PARSED | SF_INCLASS_INLINE);
18200 /* Don't do access checking if it is a templated function. */
18201 if (processing_template_decl)
18202 push_deferring_access_checks (dk_no_check);
18204 /* Now, parse the body of the function. */
18205 cp_parser_function_definition_after_declarator (parser,
18206 /*inline_p=*/true);
18208 if (processing_template_decl)
18209 pop_deferring_access_checks ();
18211 /* Leave the scope of the containing function. */
18212 if (function_scope)
18213 pop_function_context ();
18214 cp_parser_pop_lexer (parser);
18217 /* Remove any template parameters from the symbol table. */
18218 maybe_end_member_template_processing ();
18220 /* Restore the queue. */
18221 parser->unparsed_functions_queues
18222 = TREE_CHAIN (parser->unparsed_functions_queues);
18225 /* If DECL contains any default args, remember it on the unparsed
18226 functions queue. */
18229 cp_parser_save_default_args (cp_parser* parser, tree decl)
18233 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18235 probe = TREE_CHAIN (probe))
18236 if (TREE_PURPOSE (probe))
18238 TREE_PURPOSE (parser->unparsed_functions_queues)
18239 = tree_cons (current_class_type, decl,
18240 TREE_PURPOSE (parser->unparsed_functions_queues));
18245 /* FN is a FUNCTION_DECL which may contains a parameter with an
18246 unparsed DEFAULT_ARG. Parse the default args now. This function
18247 assumes that the current scope is the scope in which the default
18248 argument should be processed. */
18251 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18253 bool saved_local_variables_forbidden_p;
18256 /* While we're parsing the default args, we might (due to the
18257 statement expression extension) encounter more classes. We want
18258 to handle them right away, but we don't want them getting mixed
18259 up with default args that are currently in the queue. */
18260 parser->unparsed_functions_queues
18261 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18263 /* Local variable names (and the `this' keyword) may not appear
18264 in a default argument. */
18265 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18266 parser->local_variables_forbidden_p = true;
18268 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18270 parm = TREE_CHAIN (parm))
18272 cp_token_cache *tokens;
18273 tree default_arg = TREE_PURPOSE (parm);
18275 VEC(tree,gc) *insts;
18282 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18283 /* This can happen for a friend declaration for a function
18284 already declared with default arguments. */
18287 /* Push the saved tokens for the default argument onto the parser's
18289 tokens = DEFARG_TOKENS (default_arg);
18290 cp_parser_push_lexer_for_tokens (parser, tokens);
18292 /* Parse the assignment-expression. */
18293 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18294 if (parsed_arg == error_mark_node)
18296 cp_parser_pop_lexer (parser);
18300 if (!processing_template_decl)
18301 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18303 TREE_PURPOSE (parm) = parsed_arg;
18305 /* Update any instantiations we've already created. */
18306 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18307 VEC_iterate (tree, insts, ix, copy); ix++)
18308 TREE_PURPOSE (copy) = parsed_arg;
18310 /* If the token stream has not been completely used up, then
18311 there was extra junk after the end of the default
18313 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18314 cp_parser_error (parser, "expected %<,%>");
18316 /* Revert to the main lexer. */
18317 cp_parser_pop_lexer (parser);
18320 /* Make sure no default arg is missing. */
18321 check_default_args (fn);
18323 /* Restore the state of local_variables_forbidden_p. */
18324 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18326 /* Restore the queue. */
18327 parser->unparsed_functions_queues
18328 = TREE_CHAIN (parser->unparsed_functions_queues);
18331 /* Parse the operand of `sizeof' (or a similar operator). Returns
18332 either a TYPE or an expression, depending on the form of the
18333 input. The KEYWORD indicates which kind of expression we have
18337 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18339 tree expr = NULL_TREE;
18340 const char *saved_message;
18342 bool saved_integral_constant_expression_p;
18343 bool saved_non_integral_constant_expression_p;
18344 bool pack_expansion_p = false;
18346 /* Types cannot be defined in a `sizeof' expression. Save away the
18348 saved_message = parser->type_definition_forbidden_message;
18349 /* And create the new one. */
18350 tmp = concat ("types may not be defined in %<",
18351 IDENTIFIER_POINTER (ridpointers[keyword]),
18352 "%> expressions", NULL);
18353 parser->type_definition_forbidden_message = tmp;
18355 /* The restrictions on constant-expressions do not apply inside
18356 sizeof expressions. */
18357 saved_integral_constant_expression_p
18358 = parser->integral_constant_expression_p;
18359 saved_non_integral_constant_expression_p
18360 = parser->non_integral_constant_expression_p;
18361 parser->integral_constant_expression_p = false;
18363 /* If it's a `...', then we are computing the length of a parameter
18365 if (keyword == RID_SIZEOF
18366 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18368 /* Consume the `...'. */
18369 cp_lexer_consume_token (parser->lexer);
18370 maybe_warn_variadic_templates ();
18372 /* Note that this is an expansion. */
18373 pack_expansion_p = true;
18376 /* Do not actually evaluate the expression. */
18378 /* If it's a `(', then we might be looking at the type-id
18380 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18383 bool saved_in_type_id_in_expr_p;
18385 /* We can't be sure yet whether we're looking at a type-id or an
18387 cp_parser_parse_tentatively (parser);
18388 /* Consume the `('. */
18389 cp_lexer_consume_token (parser->lexer);
18390 /* Parse the type-id. */
18391 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18392 parser->in_type_id_in_expr_p = true;
18393 type = cp_parser_type_id (parser);
18394 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18395 /* Now, look for the trailing `)'. */
18396 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18397 /* If all went well, then we're done. */
18398 if (cp_parser_parse_definitely (parser))
18400 cp_decl_specifier_seq decl_specs;
18402 /* Build a trivial decl-specifier-seq. */
18403 clear_decl_specs (&decl_specs);
18404 decl_specs.type = type;
18406 /* Call grokdeclarator to figure out what type this is. */
18407 expr = grokdeclarator (NULL,
18411 /*attrlist=*/NULL);
18415 /* If the type-id production did not work out, then we must be
18416 looking at the unary-expression production. */
18418 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18419 /*cast_p=*/false, NULL);
18421 if (pack_expansion_p)
18422 /* Build a pack expansion. */
18423 expr = make_pack_expansion (expr);
18425 /* Go back to evaluating expressions. */
18428 /* Free the message we created. */
18430 /* And restore the old one. */
18431 parser->type_definition_forbidden_message = saved_message;
18432 parser->integral_constant_expression_p
18433 = saved_integral_constant_expression_p;
18434 parser->non_integral_constant_expression_p
18435 = saved_non_integral_constant_expression_p;
18440 /* If the current declaration has no declarator, return true. */
18443 cp_parser_declares_only_class_p (cp_parser *parser)
18445 /* If the next token is a `;' or a `,' then there is no
18447 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18448 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18451 /* Update the DECL_SPECS to reflect the storage class indicated by
18455 cp_parser_set_storage_class (cp_parser *parser,
18456 cp_decl_specifier_seq *decl_specs,
18458 location_t location)
18460 cp_storage_class storage_class;
18462 if (parser->in_unbraced_linkage_specification_p)
18464 error ("%Hinvalid use of %qD in linkage specification",
18465 &location, ridpointers[keyword]);
18468 else if (decl_specs->storage_class != sc_none)
18470 decl_specs->conflicting_specifiers_p = true;
18474 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18475 && decl_specs->specs[(int) ds_thread])
18477 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18478 decl_specs->specs[(int) ds_thread] = 0;
18484 storage_class = sc_auto;
18487 storage_class = sc_register;
18490 storage_class = sc_static;
18493 storage_class = sc_extern;
18496 storage_class = sc_mutable;
18499 gcc_unreachable ();
18501 decl_specs->storage_class = storage_class;
18503 /* A storage class specifier cannot be applied alongside a typedef
18504 specifier. If there is a typedef specifier present then set
18505 conflicting_specifiers_p which will trigger an error later
18506 on in grokdeclarator. */
18507 if (decl_specs->specs[(int)ds_typedef])
18508 decl_specs->conflicting_specifiers_p = true;
18511 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18512 is true, the type is a user-defined type; otherwise it is a
18513 built-in type specified by a keyword. */
18516 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18518 location_t location,
18519 bool user_defined_p)
18521 decl_specs->any_specifiers_p = true;
18523 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18524 (with, for example, in "typedef int wchar_t;") we remember that
18525 this is what happened. In system headers, we ignore these
18526 declarations so that G++ can work with system headers that are not
18528 if (decl_specs->specs[(int) ds_typedef]
18530 && (type_spec == boolean_type_node
18531 || type_spec == char16_type_node
18532 || type_spec == char32_type_node
18533 || type_spec == wchar_type_node)
18534 && (decl_specs->type
18535 || decl_specs->specs[(int) ds_long]
18536 || decl_specs->specs[(int) ds_short]
18537 || decl_specs->specs[(int) ds_unsigned]
18538 || decl_specs->specs[(int) ds_signed]))
18540 decl_specs->redefined_builtin_type = type_spec;
18541 if (!decl_specs->type)
18543 decl_specs->type = type_spec;
18544 decl_specs->user_defined_type_p = false;
18545 decl_specs->type_location = location;
18548 else if (decl_specs->type)
18549 decl_specs->multiple_types_p = true;
18552 decl_specs->type = type_spec;
18553 decl_specs->user_defined_type_p = user_defined_p;
18554 decl_specs->redefined_builtin_type = NULL_TREE;
18555 decl_specs->type_location = location;
18559 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18560 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18563 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18565 return decl_specifiers->specs[(int) ds_friend] != 0;
18568 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18569 issue an error message indicating that TOKEN_DESC was expected.
18571 Returns the token consumed, if the token had the appropriate type.
18572 Otherwise, returns NULL. */
18575 cp_parser_require (cp_parser* parser,
18576 enum cpp_ttype type,
18577 const char* token_desc)
18579 if (cp_lexer_next_token_is (parser->lexer, type))
18580 return cp_lexer_consume_token (parser->lexer);
18583 /* Output the MESSAGE -- unless we're parsing tentatively. */
18584 if (!cp_parser_simulate_error (parser))
18586 char *message = concat ("expected ", token_desc, NULL);
18587 cp_parser_error (parser, message);
18594 /* An error message is produced if the next token is not '>'.
18595 All further tokens are skipped until the desired token is
18596 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18599 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18601 /* Current level of '< ... >'. */
18602 unsigned level = 0;
18603 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18604 unsigned nesting_depth = 0;
18606 /* Are we ready, yet? If not, issue error message. */
18607 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18610 /* Skip tokens until the desired token is found. */
18613 /* Peek at the next token. */
18614 switch (cp_lexer_peek_token (parser->lexer)->type)
18617 if (!nesting_depth)
18622 if (cxx_dialect == cxx98)
18623 /* C++0x views the `>>' operator as two `>' tokens, but
18626 else if (!nesting_depth && level-- == 0)
18628 /* We've hit a `>>' where the first `>' closes the
18629 template argument list, and the second `>' is
18630 spurious. Just consume the `>>' and stop; we've
18631 already produced at least one error. */
18632 cp_lexer_consume_token (parser->lexer);
18635 /* Fall through for C++0x, so we handle the second `>' in
18639 if (!nesting_depth && level-- == 0)
18641 /* We've reached the token we want, consume it and stop. */
18642 cp_lexer_consume_token (parser->lexer);
18647 case CPP_OPEN_PAREN:
18648 case CPP_OPEN_SQUARE:
18652 case CPP_CLOSE_PAREN:
18653 case CPP_CLOSE_SQUARE:
18654 if (nesting_depth-- == 0)
18659 case CPP_PRAGMA_EOL:
18660 case CPP_SEMICOLON:
18661 case CPP_OPEN_BRACE:
18662 case CPP_CLOSE_BRACE:
18663 /* The '>' was probably forgotten, don't look further. */
18670 /* Consume this token. */
18671 cp_lexer_consume_token (parser->lexer);
18675 /* If the next token is the indicated keyword, consume it. Otherwise,
18676 issue an error message indicating that TOKEN_DESC was expected.
18678 Returns the token consumed, if the token had the appropriate type.
18679 Otherwise, returns NULL. */
18682 cp_parser_require_keyword (cp_parser* parser,
18684 const char* token_desc)
18686 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18688 if (token && token->keyword != keyword)
18690 dyn_string_t error_msg;
18692 /* Format the error message. */
18693 error_msg = dyn_string_new (0);
18694 dyn_string_append_cstr (error_msg, "expected ");
18695 dyn_string_append_cstr (error_msg, token_desc);
18696 cp_parser_error (parser, error_msg->s);
18697 dyn_string_delete (error_msg);
18704 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18705 function-definition. */
18708 cp_parser_token_starts_function_definition_p (cp_token* token)
18710 return (/* An ordinary function-body begins with an `{'. */
18711 token->type == CPP_OPEN_BRACE
18712 /* A ctor-initializer begins with a `:'. */
18713 || token->type == CPP_COLON
18714 /* A function-try-block begins with `try'. */
18715 || token->keyword == RID_TRY
18716 /* The named return value extension begins with `return'. */
18717 || token->keyword == RID_RETURN);
18720 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18724 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18728 token = cp_lexer_peek_token (parser->lexer);
18729 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18732 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18733 C++0x) ending a template-argument. */
18736 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18740 token = cp_lexer_peek_token (parser->lexer);
18741 return (token->type == CPP_COMMA
18742 || token->type == CPP_GREATER
18743 || token->type == CPP_ELLIPSIS
18744 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18747 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18748 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18751 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18756 token = cp_lexer_peek_nth_token (parser->lexer, n);
18757 if (token->type == CPP_LESS)
18759 /* Check for the sequence `<::' in the original code. It would be lexed as
18760 `[:', where `[' is a digraph, and there is no whitespace before
18762 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18765 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18766 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18772 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18773 or none_type otherwise. */
18775 static enum tag_types
18776 cp_parser_token_is_class_key (cp_token* token)
18778 switch (token->keyword)
18783 return record_type;
18792 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18795 cp_parser_check_class_key (enum tag_types class_key, tree type)
18797 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18798 permerror (input_location, "%qs tag used in naming %q#T",
18799 class_key == union_type ? "union"
18800 : class_key == record_type ? "struct" : "class",
18804 /* Issue an error message if DECL is redeclared with different
18805 access than its original declaration [class.access.spec/3].
18806 This applies to nested classes and nested class templates.
18810 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18812 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18815 if ((TREE_PRIVATE (decl)
18816 != (current_access_specifier == access_private_node))
18817 || (TREE_PROTECTED (decl)
18818 != (current_access_specifier == access_protected_node)))
18819 error ("%H%qD redeclared with different access", &location, decl);
18822 /* Look for the `template' keyword, as a syntactic disambiguator.
18823 Return TRUE iff it is present, in which case it will be
18827 cp_parser_optional_template_keyword (cp_parser *parser)
18829 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18831 /* The `template' keyword can only be used within templates;
18832 outside templates the parser can always figure out what is a
18833 template and what is not. */
18834 if (!processing_template_decl)
18836 cp_token *token = cp_lexer_peek_token (parser->lexer);
18837 error ("%H%<template%> (as a disambiguator) is only allowed "
18838 "within templates", &token->location);
18839 /* If this part of the token stream is rescanned, the same
18840 error message would be generated. So, we purge the token
18841 from the stream. */
18842 cp_lexer_purge_token (parser->lexer);
18847 /* Consume the `template' keyword. */
18848 cp_lexer_consume_token (parser->lexer);
18856 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18857 set PARSER->SCOPE, and perform other related actions. */
18860 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18863 struct tree_check *check_value;
18864 deferred_access_check *chk;
18865 VEC (deferred_access_check,gc) *checks;
18867 /* Get the stored value. */
18868 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18869 /* Perform any access checks that were deferred. */
18870 checks = check_value->checks;
18874 VEC_iterate (deferred_access_check, checks, i, chk) ;
18877 perform_or_defer_access_check (chk->binfo,
18882 /* Set the scope from the stored value. */
18883 parser->scope = check_value->value;
18884 parser->qualifying_scope = check_value->qualifying_scope;
18885 parser->object_scope = NULL_TREE;
18888 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18889 encounter the end of a block before what we were looking for. */
18892 cp_parser_cache_group (cp_parser *parser,
18893 enum cpp_ttype end,
18898 cp_token *token = cp_lexer_peek_token (parser->lexer);
18900 /* Abort a parenthesized expression if we encounter a semicolon. */
18901 if ((end == CPP_CLOSE_PAREN || depth == 0)
18902 && token->type == CPP_SEMICOLON)
18904 /* If we've reached the end of the file, stop. */
18905 if (token->type == CPP_EOF
18906 || (end != CPP_PRAGMA_EOL
18907 && token->type == CPP_PRAGMA_EOL))
18909 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18910 /* We've hit the end of an enclosing block, so there's been some
18911 kind of syntax error. */
18914 /* Consume the token. */
18915 cp_lexer_consume_token (parser->lexer);
18916 /* See if it starts a new group. */
18917 if (token->type == CPP_OPEN_BRACE)
18919 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18920 /* In theory this should probably check end == '}', but
18921 cp_parser_save_member_function_body needs it to exit
18922 after either '}' or ')' when called with ')'. */
18926 else if (token->type == CPP_OPEN_PAREN)
18928 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18929 if (depth == 0 && end == CPP_CLOSE_PAREN)
18932 else if (token->type == CPP_PRAGMA)
18933 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18934 else if (token->type == end)
18939 /* Begin parsing tentatively. We always save tokens while parsing
18940 tentatively so that if the tentative parsing fails we can restore the
18944 cp_parser_parse_tentatively (cp_parser* parser)
18946 /* Enter a new parsing context. */
18947 parser->context = cp_parser_context_new (parser->context);
18948 /* Begin saving tokens. */
18949 cp_lexer_save_tokens (parser->lexer);
18950 /* In order to avoid repetitive access control error messages,
18951 access checks are queued up until we are no longer parsing
18953 push_deferring_access_checks (dk_deferred);
18956 /* Commit to the currently active tentative parse. */
18959 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18961 cp_parser_context *context;
18964 /* Mark all of the levels as committed. */
18965 lexer = parser->lexer;
18966 for (context = parser->context; context->next; context = context->next)
18968 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18970 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18971 while (!cp_lexer_saving_tokens (lexer))
18972 lexer = lexer->next;
18973 cp_lexer_commit_tokens (lexer);
18977 /* Abort the currently active tentative parse. All consumed tokens
18978 will be rolled back, and no diagnostics will be issued. */
18981 cp_parser_abort_tentative_parse (cp_parser* parser)
18983 cp_parser_simulate_error (parser);
18984 /* Now, pretend that we want to see if the construct was
18985 successfully parsed. */
18986 cp_parser_parse_definitely (parser);
18989 /* Stop parsing tentatively. If a parse error has occurred, restore the
18990 token stream. Otherwise, commit to the tokens we have consumed.
18991 Returns true if no error occurred; false otherwise. */
18994 cp_parser_parse_definitely (cp_parser* parser)
18996 bool error_occurred;
18997 cp_parser_context *context;
18999 /* Remember whether or not an error occurred, since we are about to
19000 destroy that information. */
19001 error_occurred = cp_parser_error_occurred (parser);
19002 /* Remove the topmost context from the stack. */
19003 context = parser->context;
19004 parser->context = context->next;
19005 /* If no parse errors occurred, commit to the tentative parse. */
19006 if (!error_occurred)
19008 /* Commit to the tokens read tentatively, unless that was
19010 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19011 cp_lexer_commit_tokens (parser->lexer);
19013 pop_to_parent_deferring_access_checks ();
19015 /* Otherwise, if errors occurred, roll back our state so that things
19016 are just as they were before we began the tentative parse. */
19019 cp_lexer_rollback_tokens (parser->lexer);
19020 pop_deferring_access_checks ();
19022 /* Add the context to the front of the free list. */
19023 context->next = cp_parser_context_free_list;
19024 cp_parser_context_free_list = context;
19026 return !error_occurred;
19029 /* Returns true if we are parsing tentatively and are not committed to
19030 this tentative parse. */
19033 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19035 return (cp_parser_parsing_tentatively (parser)
19036 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19039 /* Returns nonzero iff an error has occurred during the most recent
19040 tentative parse. */
19043 cp_parser_error_occurred (cp_parser* parser)
19045 return (cp_parser_parsing_tentatively (parser)
19046 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19049 /* Returns nonzero if GNU extensions are allowed. */
19052 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19054 return parser->allow_gnu_extensions_p;
19057 /* Objective-C++ Productions */
19060 /* Parse an Objective-C expression, which feeds into a primary-expression
19064 objc-message-expression
19065 objc-string-literal
19066 objc-encode-expression
19067 objc-protocol-expression
19068 objc-selector-expression
19070 Returns a tree representation of the expression. */
19073 cp_parser_objc_expression (cp_parser* parser)
19075 /* Try to figure out what kind of declaration is present. */
19076 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19080 case CPP_OPEN_SQUARE:
19081 return cp_parser_objc_message_expression (parser);
19083 case CPP_OBJC_STRING:
19084 kwd = cp_lexer_consume_token (parser->lexer);
19085 return objc_build_string_object (kwd->u.value);
19088 switch (kwd->keyword)
19090 case RID_AT_ENCODE:
19091 return cp_parser_objc_encode_expression (parser);
19093 case RID_AT_PROTOCOL:
19094 return cp_parser_objc_protocol_expression (parser);
19096 case RID_AT_SELECTOR:
19097 return cp_parser_objc_selector_expression (parser);
19103 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19104 &kwd->location, kwd->u.value);
19105 cp_parser_skip_to_end_of_block_or_statement (parser);
19108 return error_mark_node;
19111 /* Parse an Objective-C message expression.
19113 objc-message-expression:
19114 [ objc-message-receiver objc-message-args ]
19116 Returns a representation of an Objective-C message. */
19119 cp_parser_objc_message_expression (cp_parser* parser)
19121 tree receiver, messageargs;
19123 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19124 receiver = cp_parser_objc_message_receiver (parser);
19125 messageargs = cp_parser_objc_message_args (parser);
19126 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19128 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19131 /* Parse an objc-message-receiver.
19133 objc-message-receiver:
19135 simple-type-specifier
19137 Returns a representation of the type or expression. */
19140 cp_parser_objc_message_receiver (cp_parser* parser)
19144 /* An Objective-C message receiver may be either (1) a type
19145 or (2) an expression. */
19146 cp_parser_parse_tentatively (parser);
19147 rcv = cp_parser_expression (parser, false, NULL);
19149 if (cp_parser_parse_definitely (parser))
19152 rcv = cp_parser_simple_type_specifier (parser,
19153 /*decl_specs=*/NULL,
19154 CP_PARSER_FLAGS_NONE);
19156 return objc_get_class_reference (rcv);
19159 /* Parse the arguments and selectors comprising an Objective-C message.
19164 objc-selector-args , objc-comma-args
19166 objc-selector-args:
19167 objc-selector [opt] : assignment-expression
19168 objc-selector-args objc-selector [opt] : assignment-expression
19171 assignment-expression
19172 objc-comma-args , assignment-expression
19174 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19175 selector arguments and TREE_VALUE containing a list of comma
19179 cp_parser_objc_message_args (cp_parser* parser)
19181 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19182 bool maybe_unary_selector_p = true;
19183 cp_token *token = cp_lexer_peek_token (parser->lexer);
19185 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19187 tree selector = NULL_TREE, arg;
19189 if (token->type != CPP_COLON)
19190 selector = cp_parser_objc_selector (parser);
19192 /* Detect if we have a unary selector. */
19193 if (maybe_unary_selector_p
19194 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19195 return build_tree_list (selector, NULL_TREE);
19197 maybe_unary_selector_p = false;
19198 cp_parser_require (parser, CPP_COLON, "%<:%>");
19199 arg = cp_parser_assignment_expression (parser, false, NULL);
19202 = chainon (sel_args,
19203 build_tree_list (selector, arg));
19205 token = cp_lexer_peek_token (parser->lexer);
19208 /* Handle non-selector arguments, if any. */
19209 while (token->type == CPP_COMMA)
19213 cp_lexer_consume_token (parser->lexer);
19214 arg = cp_parser_assignment_expression (parser, false, NULL);
19217 = chainon (addl_args,
19218 build_tree_list (NULL_TREE, arg));
19220 token = cp_lexer_peek_token (parser->lexer);
19223 return build_tree_list (sel_args, addl_args);
19226 /* Parse an Objective-C encode expression.
19228 objc-encode-expression:
19229 @encode objc-typename
19231 Returns an encoded representation of the type argument. */
19234 cp_parser_objc_encode_expression (cp_parser* parser)
19239 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19240 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19241 token = cp_lexer_peek_token (parser->lexer);
19242 type = complete_type (cp_parser_type_id (parser));
19243 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19247 error ("%H%<@encode%> must specify a type as an argument",
19249 return error_mark_node;
19252 return objc_build_encode_expr (type);
19255 /* Parse an Objective-C @defs expression. */
19258 cp_parser_objc_defs_expression (cp_parser *parser)
19262 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19263 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19264 name = cp_parser_identifier (parser);
19265 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19267 return objc_get_class_ivars (name);
19270 /* Parse an Objective-C protocol expression.
19272 objc-protocol-expression:
19273 @protocol ( identifier )
19275 Returns a representation of the protocol expression. */
19278 cp_parser_objc_protocol_expression (cp_parser* parser)
19282 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19283 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19284 proto = cp_parser_identifier (parser);
19285 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19287 return objc_build_protocol_expr (proto);
19290 /* Parse an Objective-C selector expression.
19292 objc-selector-expression:
19293 @selector ( objc-method-signature )
19295 objc-method-signature:
19301 objc-selector-seq objc-selector :
19303 Returns a representation of the method selector. */
19306 cp_parser_objc_selector_expression (cp_parser* parser)
19308 tree sel_seq = NULL_TREE;
19309 bool maybe_unary_selector_p = true;
19312 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19313 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19314 token = cp_lexer_peek_token (parser->lexer);
19316 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19317 || token->type == CPP_SCOPE)
19319 tree selector = NULL_TREE;
19321 if (token->type != CPP_COLON
19322 || token->type == CPP_SCOPE)
19323 selector = cp_parser_objc_selector (parser);
19325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19326 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19328 /* Detect if we have a unary selector. */
19329 if (maybe_unary_selector_p)
19331 sel_seq = selector;
19332 goto finish_selector;
19336 cp_parser_error (parser, "expected %<:%>");
19339 maybe_unary_selector_p = false;
19340 token = cp_lexer_consume_token (parser->lexer);
19342 if (token->type == CPP_SCOPE)
19345 = chainon (sel_seq,
19346 build_tree_list (selector, NULL_TREE));
19348 = chainon (sel_seq,
19349 build_tree_list (NULL_TREE, NULL_TREE));
19353 = chainon (sel_seq,
19354 build_tree_list (selector, NULL_TREE));
19356 token = cp_lexer_peek_token (parser->lexer);
19360 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19362 return objc_build_selector_expr (sel_seq);
19365 /* Parse a list of identifiers.
19367 objc-identifier-list:
19369 objc-identifier-list , identifier
19371 Returns a TREE_LIST of identifier nodes. */
19374 cp_parser_objc_identifier_list (cp_parser* parser)
19376 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19377 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19379 while (sep->type == CPP_COMMA)
19381 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19382 list = chainon (list,
19383 build_tree_list (NULL_TREE,
19384 cp_parser_identifier (parser)));
19385 sep = cp_lexer_peek_token (parser->lexer);
19391 /* Parse an Objective-C alias declaration.
19393 objc-alias-declaration:
19394 @compatibility_alias identifier identifier ;
19396 This function registers the alias mapping with the Objective-C front end.
19397 It returns nothing. */
19400 cp_parser_objc_alias_declaration (cp_parser* parser)
19404 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19405 alias = cp_parser_identifier (parser);
19406 orig = cp_parser_identifier (parser);
19407 objc_declare_alias (alias, orig);
19408 cp_parser_consume_semicolon_at_end_of_statement (parser);
19411 /* Parse an Objective-C class forward-declaration.
19413 objc-class-declaration:
19414 @class objc-identifier-list ;
19416 The function registers the forward declarations with the Objective-C
19417 front end. It returns nothing. */
19420 cp_parser_objc_class_declaration (cp_parser* parser)
19422 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19423 objc_declare_class (cp_parser_objc_identifier_list (parser));
19424 cp_parser_consume_semicolon_at_end_of_statement (parser);
19427 /* Parse a list of Objective-C protocol references.
19429 objc-protocol-refs-opt:
19430 objc-protocol-refs [opt]
19432 objc-protocol-refs:
19433 < objc-identifier-list >
19435 Returns a TREE_LIST of identifiers, if any. */
19438 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19440 tree protorefs = NULL_TREE;
19442 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19444 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19445 protorefs = cp_parser_objc_identifier_list (parser);
19446 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19452 /* Parse a Objective-C visibility specification. */
19455 cp_parser_objc_visibility_spec (cp_parser* parser)
19457 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19459 switch (vis->keyword)
19461 case RID_AT_PRIVATE:
19462 objc_set_visibility (2);
19464 case RID_AT_PROTECTED:
19465 objc_set_visibility (0);
19467 case RID_AT_PUBLIC:
19468 objc_set_visibility (1);
19474 /* Eat '@private'/'@protected'/'@public'. */
19475 cp_lexer_consume_token (parser->lexer);
19478 /* Parse an Objective-C method type. */
19481 cp_parser_objc_method_type (cp_parser* parser)
19483 objc_set_method_type
19484 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19489 /* Parse an Objective-C protocol qualifier. */
19492 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19494 tree quals = NULL_TREE, node;
19495 cp_token *token = cp_lexer_peek_token (parser->lexer);
19497 node = token->u.value;
19499 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19500 && (node == ridpointers [(int) RID_IN]
19501 || node == ridpointers [(int) RID_OUT]
19502 || node == ridpointers [(int) RID_INOUT]
19503 || node == ridpointers [(int) RID_BYCOPY]
19504 || node == ridpointers [(int) RID_BYREF]
19505 || node == ridpointers [(int) RID_ONEWAY]))
19507 quals = tree_cons (NULL_TREE, node, quals);
19508 cp_lexer_consume_token (parser->lexer);
19509 token = cp_lexer_peek_token (parser->lexer);
19510 node = token->u.value;
19516 /* Parse an Objective-C typename. */
19519 cp_parser_objc_typename (cp_parser* parser)
19521 tree type_name = NULL_TREE;
19523 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19525 tree proto_quals, cp_type = NULL_TREE;
19527 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19528 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19530 /* An ObjC type name may consist of just protocol qualifiers, in which
19531 case the type shall default to 'id'. */
19532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19533 cp_type = cp_parser_type_id (parser);
19535 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19536 type_name = build_tree_list (proto_quals, cp_type);
19542 /* Check to see if TYPE refers to an Objective-C selector name. */
19545 cp_parser_objc_selector_p (enum cpp_ttype type)
19547 return (type == CPP_NAME || type == CPP_KEYWORD
19548 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19549 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19550 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19551 || type == CPP_XOR || type == CPP_XOR_EQ);
19554 /* Parse an Objective-C selector. */
19557 cp_parser_objc_selector (cp_parser* parser)
19559 cp_token *token = cp_lexer_consume_token (parser->lexer);
19561 if (!cp_parser_objc_selector_p (token->type))
19563 error ("%Hinvalid Objective-C++ selector name", &token->location);
19564 return error_mark_node;
19567 /* C++ operator names are allowed to appear in ObjC selectors. */
19568 switch (token->type)
19570 case CPP_AND_AND: return get_identifier ("and");
19571 case CPP_AND_EQ: return get_identifier ("and_eq");
19572 case CPP_AND: return get_identifier ("bitand");
19573 case CPP_OR: return get_identifier ("bitor");
19574 case CPP_COMPL: return get_identifier ("compl");
19575 case CPP_NOT: return get_identifier ("not");
19576 case CPP_NOT_EQ: return get_identifier ("not_eq");
19577 case CPP_OR_OR: return get_identifier ("or");
19578 case CPP_OR_EQ: return get_identifier ("or_eq");
19579 case CPP_XOR: return get_identifier ("xor");
19580 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19581 default: return token->u.value;
19585 /* Parse an Objective-C params list. */
19588 cp_parser_objc_method_keyword_params (cp_parser* parser)
19590 tree params = NULL_TREE;
19591 bool maybe_unary_selector_p = true;
19592 cp_token *token = cp_lexer_peek_token (parser->lexer);
19594 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19596 tree selector = NULL_TREE, type_name, identifier;
19598 if (token->type != CPP_COLON)
19599 selector = cp_parser_objc_selector (parser);
19601 /* Detect if we have a unary selector. */
19602 if (maybe_unary_selector_p
19603 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19606 maybe_unary_selector_p = false;
19607 cp_parser_require (parser, CPP_COLON, "%<:%>");
19608 type_name = cp_parser_objc_typename (parser);
19609 identifier = cp_parser_identifier (parser);
19613 objc_build_keyword_decl (selector,
19617 token = cp_lexer_peek_token (parser->lexer);
19623 /* Parse the non-keyword Objective-C params. */
19626 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19628 tree params = make_node (TREE_LIST);
19629 cp_token *token = cp_lexer_peek_token (parser->lexer);
19630 *ellipsisp = false; /* Initially, assume no ellipsis. */
19632 while (token->type == CPP_COMMA)
19634 cp_parameter_declarator *parmdecl;
19637 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19638 token = cp_lexer_peek_token (parser->lexer);
19640 if (token->type == CPP_ELLIPSIS)
19642 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19647 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19648 parm = grokdeclarator (parmdecl->declarator,
19649 &parmdecl->decl_specifiers,
19650 PARM, /*initialized=*/0,
19651 /*attrlist=*/NULL);
19653 chainon (params, build_tree_list (NULL_TREE, parm));
19654 token = cp_lexer_peek_token (parser->lexer);
19660 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19663 cp_parser_objc_interstitial_code (cp_parser* parser)
19665 cp_token *token = cp_lexer_peek_token (parser->lexer);
19667 /* If the next token is `extern' and the following token is a string
19668 literal, then we have a linkage specification. */
19669 if (token->keyword == RID_EXTERN
19670 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19671 cp_parser_linkage_specification (parser);
19672 /* Handle #pragma, if any. */
19673 else if (token->type == CPP_PRAGMA)
19674 cp_parser_pragma (parser, pragma_external);
19675 /* Allow stray semicolons. */
19676 else if (token->type == CPP_SEMICOLON)
19677 cp_lexer_consume_token (parser->lexer);
19678 /* Finally, try to parse a block-declaration, or a function-definition. */
19680 cp_parser_block_declaration (parser, /*statement_p=*/false);
19683 /* Parse a method signature. */
19686 cp_parser_objc_method_signature (cp_parser* parser)
19688 tree rettype, kwdparms, optparms;
19689 bool ellipsis = false;
19691 cp_parser_objc_method_type (parser);
19692 rettype = cp_parser_objc_typename (parser);
19693 kwdparms = cp_parser_objc_method_keyword_params (parser);
19694 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19696 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19699 /* Pars an Objective-C method prototype list. */
19702 cp_parser_objc_method_prototype_list (cp_parser* parser)
19704 cp_token *token = cp_lexer_peek_token (parser->lexer);
19706 while (token->keyword != RID_AT_END)
19708 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19710 objc_add_method_declaration
19711 (cp_parser_objc_method_signature (parser));
19712 cp_parser_consume_semicolon_at_end_of_statement (parser);
19715 /* Allow for interspersed non-ObjC++ code. */
19716 cp_parser_objc_interstitial_code (parser);
19718 token = cp_lexer_peek_token (parser->lexer);
19721 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19722 objc_finish_interface ();
19725 /* Parse an Objective-C method definition list. */
19728 cp_parser_objc_method_definition_list (cp_parser* parser)
19730 cp_token *token = cp_lexer_peek_token (parser->lexer);
19732 while (token->keyword != RID_AT_END)
19736 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19738 push_deferring_access_checks (dk_deferred);
19739 objc_start_method_definition
19740 (cp_parser_objc_method_signature (parser));
19742 /* For historical reasons, we accept an optional semicolon. */
19743 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19744 cp_lexer_consume_token (parser->lexer);
19746 perform_deferred_access_checks ();
19747 stop_deferring_access_checks ();
19748 meth = cp_parser_function_definition_after_declarator (parser,
19750 pop_deferring_access_checks ();
19751 objc_finish_method_definition (meth);
19754 /* Allow for interspersed non-ObjC++ code. */
19755 cp_parser_objc_interstitial_code (parser);
19757 token = cp_lexer_peek_token (parser->lexer);
19760 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19761 objc_finish_implementation ();
19764 /* Parse Objective-C ivars. */
19767 cp_parser_objc_class_ivars (cp_parser* parser)
19769 cp_token *token = cp_lexer_peek_token (parser->lexer);
19771 if (token->type != CPP_OPEN_BRACE)
19772 return; /* No ivars specified. */
19774 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19775 token = cp_lexer_peek_token (parser->lexer);
19777 while (token->type != CPP_CLOSE_BRACE)
19779 cp_decl_specifier_seq declspecs;
19780 int decl_class_or_enum_p;
19781 tree prefix_attributes;
19783 cp_parser_objc_visibility_spec (parser);
19785 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19788 cp_parser_decl_specifier_seq (parser,
19789 CP_PARSER_FLAGS_OPTIONAL,
19791 &decl_class_or_enum_p);
19792 prefix_attributes = declspecs.attributes;
19793 declspecs.attributes = NULL_TREE;
19795 /* Keep going until we hit the `;' at the end of the
19797 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19799 tree width = NULL_TREE, attributes, first_attribute, decl;
19800 cp_declarator *declarator = NULL;
19801 int ctor_dtor_or_conv_p;
19803 /* Check for a (possibly unnamed) bitfield declaration. */
19804 token = cp_lexer_peek_token (parser->lexer);
19805 if (token->type == CPP_COLON)
19808 if (token->type == CPP_NAME
19809 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19812 /* Get the name of the bitfield. */
19813 declarator = make_id_declarator (NULL_TREE,
19814 cp_parser_identifier (parser),
19818 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19819 /* Get the width of the bitfield. */
19821 = cp_parser_constant_expression (parser,
19822 /*allow_non_constant=*/false,
19827 /* Parse the declarator. */
19829 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19830 &ctor_dtor_or_conv_p,
19831 /*parenthesized_p=*/NULL,
19832 /*member_p=*/false);
19835 /* Look for attributes that apply to the ivar. */
19836 attributes = cp_parser_attributes_opt (parser);
19837 /* Remember which attributes are prefix attributes and
19839 first_attribute = attributes;
19840 /* Combine the attributes. */
19841 attributes = chainon (prefix_attributes, attributes);
19844 /* Create the bitfield declaration. */
19845 decl = grokbitfield (declarator, &declspecs,
19849 decl = grokfield (declarator, &declspecs,
19850 NULL_TREE, /*init_const_expr_p=*/false,
19851 NULL_TREE, attributes);
19853 /* Add the instance variable. */
19854 objc_add_instance_variable (decl);
19856 /* Reset PREFIX_ATTRIBUTES. */
19857 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19858 attributes = TREE_CHAIN (attributes);
19860 TREE_CHAIN (attributes) = NULL_TREE;
19862 token = cp_lexer_peek_token (parser->lexer);
19864 if (token->type == CPP_COMMA)
19866 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19872 cp_parser_consume_semicolon_at_end_of_statement (parser);
19873 token = cp_lexer_peek_token (parser->lexer);
19876 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19877 /* For historical reasons, we accept an optional semicolon. */
19878 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19879 cp_lexer_consume_token (parser->lexer);
19882 /* Parse an Objective-C protocol declaration. */
19885 cp_parser_objc_protocol_declaration (cp_parser* parser)
19887 tree proto, protorefs;
19890 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19891 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19893 tok = cp_lexer_peek_token (parser->lexer);
19894 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19898 /* See if we have a forward declaration or a definition. */
19899 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19901 /* Try a forward declaration first. */
19902 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19904 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19906 cp_parser_consume_semicolon_at_end_of_statement (parser);
19909 /* Ok, we got a full-fledged definition (or at least should). */
19912 proto = cp_parser_identifier (parser);
19913 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19914 objc_start_protocol (proto, protorefs);
19915 cp_parser_objc_method_prototype_list (parser);
19919 /* Parse an Objective-C superclass or category. */
19922 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19925 cp_token *next = cp_lexer_peek_token (parser->lexer);
19927 *super = *categ = NULL_TREE;
19928 if (next->type == CPP_COLON)
19930 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19931 *super = cp_parser_identifier (parser);
19933 else if (next->type == CPP_OPEN_PAREN)
19935 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19936 *categ = cp_parser_identifier (parser);
19937 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19941 /* Parse an Objective-C class interface. */
19944 cp_parser_objc_class_interface (cp_parser* parser)
19946 tree name, super, categ, protos;
19948 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19949 name = cp_parser_identifier (parser);
19950 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19951 protos = cp_parser_objc_protocol_refs_opt (parser);
19953 /* We have either a class or a category on our hands. */
19955 objc_start_category_interface (name, categ, protos);
19958 objc_start_class_interface (name, super, protos);
19959 /* Handle instance variable declarations, if any. */
19960 cp_parser_objc_class_ivars (parser);
19961 objc_continue_interface ();
19964 cp_parser_objc_method_prototype_list (parser);
19967 /* Parse an Objective-C class implementation. */
19970 cp_parser_objc_class_implementation (cp_parser* parser)
19972 tree name, super, categ;
19974 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19975 name = cp_parser_identifier (parser);
19976 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19978 /* We have either a class or a category on our hands. */
19980 objc_start_category_implementation (name, categ);
19983 objc_start_class_implementation (name, super);
19984 /* Handle instance variable declarations, if any. */
19985 cp_parser_objc_class_ivars (parser);
19986 objc_continue_implementation ();
19989 cp_parser_objc_method_definition_list (parser);
19992 /* Consume the @end token and finish off the implementation. */
19995 cp_parser_objc_end_implementation (cp_parser* parser)
19997 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19998 objc_finish_implementation ();
20001 /* Parse an Objective-C declaration. */
20004 cp_parser_objc_declaration (cp_parser* parser)
20006 /* Try to figure out what kind of declaration is present. */
20007 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20009 switch (kwd->keyword)
20012 cp_parser_objc_alias_declaration (parser);
20015 cp_parser_objc_class_declaration (parser);
20017 case RID_AT_PROTOCOL:
20018 cp_parser_objc_protocol_declaration (parser);
20020 case RID_AT_INTERFACE:
20021 cp_parser_objc_class_interface (parser);
20023 case RID_AT_IMPLEMENTATION:
20024 cp_parser_objc_class_implementation (parser);
20027 cp_parser_objc_end_implementation (parser);
20030 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20031 &kwd->location, kwd->u.value);
20032 cp_parser_skip_to_end_of_block_or_statement (parser);
20036 /* Parse an Objective-C try-catch-finally statement.
20038 objc-try-catch-finally-stmt:
20039 @try compound-statement objc-catch-clause-seq [opt]
20040 objc-finally-clause [opt]
20042 objc-catch-clause-seq:
20043 objc-catch-clause objc-catch-clause-seq [opt]
20046 @catch ( exception-declaration ) compound-statement
20048 objc-finally-clause
20049 @finally compound-statement
20051 Returns NULL_TREE. */
20054 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20055 location_t location;
20058 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20059 location = cp_lexer_peek_token (parser->lexer)->location;
20060 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20061 node, lest it get absorbed into the surrounding block. */
20062 stmt = push_stmt_list ();
20063 cp_parser_compound_statement (parser, NULL, false);
20064 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20066 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20068 cp_parameter_declarator *parmdecl;
20071 cp_lexer_consume_token (parser->lexer);
20072 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20073 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20074 parm = grokdeclarator (parmdecl->declarator,
20075 &parmdecl->decl_specifiers,
20076 PARM, /*initialized=*/0,
20077 /*attrlist=*/NULL);
20078 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20079 objc_begin_catch_clause (parm);
20080 cp_parser_compound_statement (parser, NULL, false);
20081 objc_finish_catch_clause ();
20084 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20086 cp_lexer_consume_token (parser->lexer);
20087 location = cp_lexer_peek_token (parser->lexer)->location;
20088 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20089 node, lest it get absorbed into the surrounding block. */
20090 stmt = push_stmt_list ();
20091 cp_parser_compound_statement (parser, NULL, false);
20092 objc_build_finally_clause (location, pop_stmt_list (stmt));
20095 return objc_finish_try_stmt ();
20098 /* Parse an Objective-C synchronized statement.
20100 objc-synchronized-stmt:
20101 @synchronized ( expression ) compound-statement
20103 Returns NULL_TREE. */
20106 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20107 location_t location;
20110 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20112 location = cp_lexer_peek_token (parser->lexer)->location;
20113 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20114 lock = cp_parser_expression (parser, false, NULL);
20115 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20117 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20118 node, lest it get absorbed into the surrounding block. */
20119 stmt = push_stmt_list ();
20120 cp_parser_compound_statement (parser, NULL, false);
20122 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20125 /* Parse an Objective-C throw statement.
20128 @throw assignment-expression [opt] ;
20130 Returns a constructed '@throw' statement. */
20133 cp_parser_objc_throw_statement (cp_parser *parser) {
20134 tree expr = NULL_TREE;
20136 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20139 expr = cp_parser_assignment_expression (parser, false, NULL);
20141 cp_parser_consume_semicolon_at_end_of_statement (parser);
20143 return objc_build_throw_stmt (expr);
20146 /* Parse an Objective-C statement. */
20149 cp_parser_objc_statement (cp_parser * parser) {
20150 /* Try to figure out what kind of declaration is present. */
20151 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20153 switch (kwd->keyword)
20156 return cp_parser_objc_try_catch_finally_statement (parser);
20157 case RID_AT_SYNCHRONIZED:
20158 return cp_parser_objc_synchronized_statement (parser);
20160 return cp_parser_objc_throw_statement (parser);
20162 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20163 &kwd->location, kwd->u.value);
20164 cp_parser_skip_to_end_of_block_or_statement (parser);
20167 return error_mark_node;
20170 /* OpenMP 2.5 parsing routines. */
20172 /* Returns name of the next clause.
20173 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20174 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20175 returned and the token is consumed. */
20177 static pragma_omp_clause
20178 cp_parser_omp_clause_name (cp_parser *parser)
20180 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20182 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20183 result = PRAGMA_OMP_CLAUSE_IF;
20184 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20185 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20186 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20187 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20188 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20190 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20191 const char *p = IDENTIFIER_POINTER (id);
20196 if (!strcmp ("collapse", p))
20197 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20198 else if (!strcmp ("copyin", p))
20199 result = PRAGMA_OMP_CLAUSE_COPYIN;
20200 else if (!strcmp ("copyprivate", p))
20201 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20204 if (!strcmp ("firstprivate", p))
20205 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20208 if (!strcmp ("lastprivate", p))
20209 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20212 if (!strcmp ("nowait", p))
20213 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20214 else if (!strcmp ("num_threads", p))
20215 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20218 if (!strcmp ("ordered", p))
20219 result = PRAGMA_OMP_CLAUSE_ORDERED;
20222 if (!strcmp ("reduction", p))
20223 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20226 if (!strcmp ("schedule", p))
20227 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20228 else if (!strcmp ("shared", p))
20229 result = PRAGMA_OMP_CLAUSE_SHARED;
20232 if (!strcmp ("untied", p))
20233 result = PRAGMA_OMP_CLAUSE_UNTIED;
20238 if (result != PRAGMA_OMP_CLAUSE_NONE)
20239 cp_lexer_consume_token (parser->lexer);
20244 /* Validate that a clause of the given type does not already exist. */
20247 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20248 const char *name, location_t location)
20252 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20253 if (OMP_CLAUSE_CODE (c) == code)
20255 error ("%Htoo many %qs clauses", &location, name);
20263 variable-list , identifier
20265 In addition, we match a closing parenthesis. An opening parenthesis
20266 will have been consumed by the caller.
20268 If KIND is nonzero, create the appropriate node and install the decl
20269 in OMP_CLAUSE_DECL and add the node to the head of the list.
20271 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20272 return the list created. */
20275 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20283 token = cp_lexer_peek_token (parser->lexer);
20284 name = cp_parser_id_expression (parser, /*template_p=*/false,
20285 /*check_dependency_p=*/true,
20286 /*template_p=*/NULL,
20287 /*declarator_p=*/false,
20288 /*optional_p=*/false);
20289 if (name == error_mark_node)
20292 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20293 if (decl == error_mark_node)
20294 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20295 else if (kind != 0)
20297 tree u = build_omp_clause (kind);
20298 OMP_CLAUSE_DECL (u) = decl;
20299 OMP_CLAUSE_CHAIN (u) = list;
20303 list = tree_cons (decl, NULL_TREE, list);
20306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20308 cp_lexer_consume_token (parser->lexer);
20311 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20315 /* Try to resync to an unnested comma. Copied from
20316 cp_parser_parenthesized_expression_list. */
20318 ending = cp_parser_skip_to_closing_parenthesis (parser,
20319 /*recovering=*/true,
20321 /*consume_paren=*/true);
20329 /* Similarly, but expect leading and trailing parenthesis. This is a very
20330 common case for omp clauses. */
20333 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20335 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20336 return cp_parser_omp_var_list_no_open (parser, kind, list);
20341 collapse ( constant-expression ) */
20344 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20350 loc = cp_lexer_peek_token (parser->lexer)->location;
20351 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20354 num = cp_parser_constant_expression (parser, false, NULL);
20356 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20357 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20358 /*or_comma=*/false,
20359 /*consume_paren=*/true);
20361 if (num == error_mark_node)
20363 num = fold_non_dependent_expr (num);
20364 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20365 || !host_integerp (num, 0)
20366 || (n = tree_low_cst (num, 0)) <= 0
20369 error ("%Hcollapse argument needs positive constant integer expression",
20374 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20375 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20376 OMP_CLAUSE_CHAIN (c) = list;
20377 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20383 default ( shared | none ) */
20386 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20388 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20391 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20393 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20395 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20396 const char *p = IDENTIFIER_POINTER (id);
20401 if (strcmp ("none", p) != 0)
20403 kind = OMP_CLAUSE_DEFAULT_NONE;
20407 if (strcmp ("shared", p) != 0)
20409 kind = OMP_CLAUSE_DEFAULT_SHARED;
20416 cp_lexer_consume_token (parser->lexer);
20421 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20424 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20425 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20426 /*or_comma=*/false,
20427 /*consume_paren=*/true);
20429 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20432 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20433 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20434 OMP_CLAUSE_CHAIN (c) = list;
20435 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20441 if ( expression ) */
20444 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20448 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20451 t = cp_parser_condition (parser);
20453 if (t == error_mark_node
20454 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20455 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20456 /*or_comma=*/false,
20457 /*consume_paren=*/true);
20459 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20461 c = build_omp_clause (OMP_CLAUSE_IF);
20462 OMP_CLAUSE_IF_EXPR (c) = t;
20463 OMP_CLAUSE_CHAIN (c) = list;
20472 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20473 tree list, location_t location)
20477 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20479 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20480 OMP_CLAUSE_CHAIN (c) = list;
20485 num_threads ( expression ) */
20488 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20489 location_t location)
20493 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20496 t = cp_parser_expression (parser, false, NULL);
20498 if (t == error_mark_node
20499 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20500 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20501 /*or_comma=*/false,
20502 /*consume_paren=*/true);
20504 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20505 "num_threads", location);
20507 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20508 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20509 OMP_CLAUSE_CHAIN (c) = list;
20518 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20519 tree list, location_t location)
20523 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20524 "ordered", location);
20526 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20527 OMP_CLAUSE_CHAIN (c) = list;
20532 reduction ( reduction-operator : variable-list )
20534 reduction-operator:
20535 One of: + * - & ^ | && || */
20538 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20540 enum tree_code code;
20543 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20546 switch (cp_lexer_peek_token (parser->lexer)->type)
20558 code = BIT_AND_EXPR;
20561 code = BIT_XOR_EXPR;
20564 code = BIT_IOR_EXPR;
20567 code = TRUTH_ANDIF_EXPR;
20570 code = TRUTH_ORIF_EXPR;
20573 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20574 "%<|%>, %<&&%>, or %<||%>");
20576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20577 /*or_comma=*/false,
20578 /*consume_paren=*/true);
20581 cp_lexer_consume_token (parser->lexer);
20583 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20586 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20587 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20588 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20594 schedule ( schedule-kind )
20595 schedule ( schedule-kind , expression )
20598 static | dynamic | guided | runtime | auto */
20601 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20605 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20608 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20610 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20612 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20613 const char *p = IDENTIFIER_POINTER (id);
20618 if (strcmp ("dynamic", p) != 0)
20620 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20624 if (strcmp ("guided", p) != 0)
20626 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20630 if (strcmp ("runtime", p) != 0)
20632 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20639 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20640 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20641 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20642 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20645 cp_lexer_consume_token (parser->lexer);
20647 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20650 cp_lexer_consume_token (parser->lexer);
20652 token = cp_lexer_peek_token (parser->lexer);
20653 t = cp_parser_assignment_expression (parser, false, NULL);
20655 if (t == error_mark_node)
20657 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20658 error ("%Hschedule %<runtime%> does not take "
20659 "a %<chunk_size%> parameter", &token->location);
20660 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20661 error ("%Hschedule %<auto%> does not take "
20662 "a %<chunk_size%> parameter", &token->location);
20664 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20666 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20669 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20672 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20673 OMP_CLAUSE_CHAIN (c) = list;
20677 cp_parser_error (parser, "invalid schedule kind");
20679 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20680 /*or_comma=*/false,
20681 /*consume_paren=*/true);
20689 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20690 tree list, location_t location)
20694 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20696 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20697 OMP_CLAUSE_CHAIN (c) = list;
20701 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20702 is a bitmask in MASK. Return the list of clauses found; the result
20703 of clause default goes in *pdefault. */
20706 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20707 const char *where, cp_token *pragma_tok)
20709 tree clauses = NULL;
20711 cp_token *token = NULL;
20713 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20715 pragma_omp_clause c_kind;
20716 const char *c_name;
20717 tree prev = clauses;
20719 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20720 cp_lexer_consume_token (parser->lexer);
20722 token = cp_lexer_peek_token (parser->lexer);
20723 c_kind = cp_parser_omp_clause_name (parser);
20728 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20729 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20731 c_name = "collapse";
20733 case PRAGMA_OMP_CLAUSE_COPYIN:
20734 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20737 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20738 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20740 c_name = "copyprivate";
20742 case PRAGMA_OMP_CLAUSE_DEFAULT:
20743 clauses = cp_parser_omp_clause_default (parser, clauses,
20745 c_name = "default";
20747 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20748 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20750 c_name = "firstprivate";
20752 case PRAGMA_OMP_CLAUSE_IF:
20753 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20756 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20757 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20759 c_name = "lastprivate";
20761 case PRAGMA_OMP_CLAUSE_NOWAIT:
20762 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20765 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20766 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20768 c_name = "num_threads";
20770 case PRAGMA_OMP_CLAUSE_ORDERED:
20771 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20773 c_name = "ordered";
20775 case PRAGMA_OMP_CLAUSE_PRIVATE:
20776 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20778 c_name = "private";
20780 case PRAGMA_OMP_CLAUSE_REDUCTION:
20781 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20782 c_name = "reduction";
20784 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20785 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20787 c_name = "schedule";
20789 case PRAGMA_OMP_CLAUSE_SHARED:
20790 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20794 case PRAGMA_OMP_CLAUSE_UNTIED:
20795 clauses = cp_parser_omp_clause_untied (parser, clauses,
20800 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20804 if (((mask >> c_kind) & 1) == 0)
20806 /* Remove the invalid clause(s) from the list to avoid
20807 confusing the rest of the compiler. */
20809 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20813 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20814 return finish_omp_clauses (clauses);
20821 In practice, we're also interested in adding the statement to an
20822 outer node. So it is convenient if we work around the fact that
20823 cp_parser_statement calls add_stmt. */
20826 cp_parser_begin_omp_structured_block (cp_parser *parser)
20828 unsigned save = parser->in_statement;
20830 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20831 This preserves the "not within loop or switch" style error messages
20832 for nonsense cases like
20838 if (parser->in_statement)
20839 parser->in_statement = IN_OMP_BLOCK;
20845 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20847 parser->in_statement = save;
20851 cp_parser_omp_structured_block (cp_parser *parser)
20853 tree stmt = begin_omp_structured_block ();
20854 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20856 cp_parser_statement (parser, NULL_TREE, false, NULL);
20858 cp_parser_end_omp_structured_block (parser, save);
20859 return finish_omp_structured_block (stmt);
20863 # pragma omp atomic new-line
20867 x binop= expr | x++ | ++x | x-- | --x
20869 +, *, -, /, &, ^, |, <<, >>
20871 where x is an lvalue expression with scalar type. */
20874 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20877 enum tree_code code;
20879 cp_parser_require_pragma_eol (parser, pragma_tok);
20881 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20882 /*cast_p=*/false, NULL);
20883 switch (TREE_CODE (lhs))
20888 case PREINCREMENT_EXPR:
20889 case POSTINCREMENT_EXPR:
20890 lhs = TREE_OPERAND (lhs, 0);
20892 rhs = integer_one_node;
20895 case PREDECREMENT_EXPR:
20896 case POSTDECREMENT_EXPR:
20897 lhs = TREE_OPERAND (lhs, 0);
20899 rhs = integer_one_node;
20903 switch (cp_lexer_peek_token (parser->lexer)->type)
20909 code = TRUNC_DIV_EXPR;
20917 case CPP_LSHIFT_EQ:
20918 code = LSHIFT_EXPR;
20920 case CPP_RSHIFT_EQ:
20921 code = RSHIFT_EXPR;
20924 code = BIT_AND_EXPR;
20927 code = BIT_IOR_EXPR;
20930 code = BIT_XOR_EXPR;
20933 cp_parser_error (parser,
20934 "invalid operator for %<#pragma omp atomic%>");
20937 cp_lexer_consume_token (parser->lexer);
20939 rhs = cp_parser_expression (parser, false, NULL);
20940 if (rhs == error_mark_node)
20944 finish_omp_atomic (code, lhs, rhs);
20945 cp_parser_consume_semicolon_at_end_of_statement (parser);
20949 cp_parser_skip_to_end_of_block_or_statement (parser);
20954 # pragma omp barrier new-line */
20957 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20959 cp_parser_require_pragma_eol (parser, pragma_tok);
20960 finish_omp_barrier ();
20964 # pragma omp critical [(name)] new-line
20965 structured-block */
20968 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20970 tree stmt, name = NULL;
20972 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20974 cp_lexer_consume_token (parser->lexer);
20976 name = cp_parser_identifier (parser);
20978 if (name == error_mark_node
20979 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20980 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20981 /*or_comma=*/false,
20982 /*consume_paren=*/true);
20983 if (name == error_mark_node)
20986 cp_parser_require_pragma_eol (parser, pragma_tok);
20988 stmt = cp_parser_omp_structured_block (parser);
20989 return c_finish_omp_critical (stmt, name);
20993 # pragma omp flush flush-vars[opt] new-line
20996 ( variable-list ) */
20999 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21001 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21002 (void) cp_parser_omp_var_list (parser, 0, NULL);
21003 cp_parser_require_pragma_eol (parser, pragma_tok);
21005 finish_omp_flush ();
21008 /* Helper function, to parse omp for increment expression. */
21011 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21013 tree lhs = cp_parser_cast_expression (parser, false, false, NULL), rhs;
21019 cp_parser_skip_to_end_of_statement (parser);
21020 return error_mark_node;
21023 token = cp_lexer_peek_token (parser->lexer);
21024 op = binops_by_token [token->type].tree_type;
21033 cp_parser_skip_to_end_of_statement (parser);
21034 return error_mark_node;
21037 cp_lexer_consume_token (parser->lexer);
21038 rhs = cp_parser_binary_expression (parser, false,
21039 PREC_RELATIONAL_EXPRESSION, NULL);
21040 if (rhs == error_mark_node
21041 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21043 cp_parser_skip_to_end_of_statement (parser);
21044 return error_mark_node;
21047 return build2 (op, boolean_type_node, lhs, rhs);
21050 /* Helper function, to parse omp for increment expression. */
21053 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21055 cp_token *token = cp_lexer_peek_token (parser->lexer);
21061 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21063 op = (token->type == CPP_PLUS_PLUS
21064 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21065 cp_lexer_consume_token (parser->lexer);
21066 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21068 return error_mark_node;
21069 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21072 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21074 return error_mark_node;
21076 token = cp_lexer_peek_token (parser->lexer);
21077 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21079 op = (token->type == CPP_PLUS_PLUS
21080 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21081 cp_lexer_consume_token (parser->lexer);
21082 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21085 op = cp_parser_assignment_operator_opt (parser);
21086 if (op == ERROR_MARK)
21087 return error_mark_node;
21089 if (op != NOP_EXPR)
21091 rhs = cp_parser_assignment_expression (parser, false, NULL);
21092 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21093 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21096 lhs = cp_parser_binary_expression (parser, false,
21097 PREC_ADDITIVE_EXPRESSION, NULL);
21098 token = cp_lexer_peek_token (parser->lexer);
21099 decl_first = lhs == decl;
21102 if (token->type != CPP_PLUS
21103 && token->type != CPP_MINUS)
21104 return error_mark_node;
21108 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21109 cp_lexer_consume_token (parser->lexer);
21110 rhs = cp_parser_binary_expression (parser, false,
21111 PREC_ADDITIVE_EXPRESSION, NULL);
21112 token = cp_lexer_peek_token (parser->lexer);
21113 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21115 if (lhs == NULL_TREE)
21117 if (op == PLUS_EXPR)
21120 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21123 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21124 NULL, tf_warning_or_error);
21127 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21131 if (rhs != decl || op == MINUS_EXPR)
21132 return error_mark_node;
21133 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21136 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21138 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21141 /* Parse the restricted form of the for statement allowed by OpenMP. */
21144 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21146 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21147 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21148 tree this_pre_body, cl;
21149 location_t loc_first;
21150 bool collapse_err = false;
21151 int i, collapse = 1, nbraces = 0;
21153 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21154 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21155 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21157 gcc_assert (collapse >= 1);
21159 declv = make_tree_vec (collapse);
21160 initv = make_tree_vec (collapse);
21161 condv = make_tree_vec (collapse);
21162 incrv = make_tree_vec (collapse);
21164 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21166 for (i = 0; i < collapse; i++)
21168 int bracecount = 0;
21169 bool add_private_clause = false;
21172 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21174 cp_parser_error (parser, "for statement expected");
21177 loc = cp_lexer_consume_token (parser->lexer)->location;
21179 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21182 init = decl = real_decl = NULL;
21183 this_pre_body = push_stmt_list ();
21184 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21186 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21190 integer-type var = lb
21191 random-access-iterator-type var = lb
21192 pointer-type var = lb
21194 cp_decl_specifier_seq type_specifiers;
21196 /* First, try to parse as an initialized declaration. See
21197 cp_parser_condition, from whence the bulk of this is copied. */
21199 cp_parser_parse_tentatively (parser);
21200 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21202 if (cp_parser_parse_definitely (parser))
21204 /* If parsing a type specifier seq succeeded, then this
21205 MUST be a initialized declaration. */
21206 tree asm_specification, attributes;
21207 cp_declarator *declarator;
21209 declarator = cp_parser_declarator (parser,
21210 CP_PARSER_DECLARATOR_NAMED,
21211 /*ctor_dtor_or_conv_p=*/NULL,
21212 /*parenthesized_p=*/NULL,
21213 /*member_p=*/false);
21214 attributes = cp_parser_attributes_opt (parser);
21215 asm_specification = cp_parser_asm_specification_opt (parser);
21217 if (declarator == cp_error_declarator)
21218 cp_parser_skip_to_end_of_statement (parser);
21222 tree pushed_scope, auto_node;
21224 decl = start_decl (declarator, &type_specifiers,
21225 SD_INITIALIZED, attributes,
21226 /*prefix_attributes=*/NULL_TREE,
21229 auto_node = type_uses_auto (TREE_TYPE (decl));
21230 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21232 if (cp_lexer_next_token_is (parser->lexer,
21234 error ("parenthesized initialization is not allowed in "
21235 "OpenMP %<for%> loop");
21237 /* Trigger an error. */
21238 cp_parser_require (parser, CPP_EQ, "%<=%>");
21240 init = error_mark_node;
21241 cp_parser_skip_to_end_of_statement (parser);
21243 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21244 || type_dependent_expression_p (decl)
21247 bool is_direct_init, is_non_constant_init;
21249 init = cp_parser_initializer (parser,
21251 &is_non_constant_init);
21253 if (auto_node && describable_type (init))
21256 = do_auto_deduction (TREE_TYPE (decl), init,
21259 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21260 && !type_dependent_expression_p (decl))
21264 cp_finish_decl (decl, init, !is_non_constant_init,
21266 LOOKUP_ONLYCONVERTING);
21267 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21270 = tree_cons (NULL, this_pre_body, for_block);
21274 init = pop_stmt_list (this_pre_body);
21275 this_pre_body = NULL_TREE;
21280 cp_lexer_consume_token (parser->lexer);
21281 init = cp_parser_assignment_expression (parser, false, NULL);
21284 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21285 init = error_mark_node;
21287 cp_finish_decl (decl, NULL_TREE,
21288 /*init_const_expr_p=*/false,
21290 LOOKUP_ONLYCONVERTING);
21294 pop_scope (pushed_scope);
21300 /* If parsing a type specifier sequence failed, then
21301 this MUST be a simple expression. */
21302 cp_parser_parse_tentatively (parser);
21303 decl = cp_parser_primary_expression (parser, false, false,
21305 if (!cp_parser_error_occurred (parser)
21308 && CLASS_TYPE_P (TREE_TYPE (decl)))
21312 cp_parser_parse_definitely (parser);
21313 cp_parser_require (parser, CPP_EQ, "%<=%>");
21314 rhs = cp_parser_assignment_expression (parser, false, NULL);
21315 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21317 tf_warning_or_error));
21318 add_private_clause = true;
21323 cp_parser_abort_tentative_parse (parser);
21324 init = cp_parser_expression (parser, false, NULL);
21327 if (TREE_CODE (init) == MODIFY_EXPR
21328 || TREE_CODE (init) == MODOP_EXPR)
21329 real_decl = TREE_OPERAND (init, 0);
21334 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21337 this_pre_body = pop_stmt_list (this_pre_body);
21341 pre_body = push_stmt_list ();
21343 add_stmt (this_pre_body);
21344 pre_body = pop_stmt_list (pre_body);
21347 pre_body = this_pre_body;
21352 if (par_clauses != NULL && real_decl != NULL_TREE)
21355 for (c = par_clauses; *c ; )
21356 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21357 && OMP_CLAUSE_DECL (*c) == real_decl)
21359 error ("%Hiteration variable %qD should not be firstprivate",
21361 *c = OMP_CLAUSE_CHAIN (*c);
21363 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21364 && OMP_CLAUSE_DECL (*c) == real_decl)
21366 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21367 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21368 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21369 OMP_CLAUSE_DECL (l) = real_decl;
21370 OMP_CLAUSE_CHAIN (l) = clauses;
21371 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21373 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21374 CP_OMP_CLAUSE_INFO (*c) = NULL;
21375 add_private_clause = false;
21379 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21380 && OMP_CLAUSE_DECL (*c) == real_decl)
21381 add_private_clause = false;
21382 c = &OMP_CLAUSE_CHAIN (*c);
21386 if (add_private_clause)
21389 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21391 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21392 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21393 && OMP_CLAUSE_DECL (c) == decl)
21395 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21396 && OMP_CLAUSE_DECL (c) == decl)
21397 error ("%Hiteration variable %qD should not be firstprivate",
21399 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21400 && OMP_CLAUSE_DECL (c) == decl)
21401 error ("%Hiteration variable %qD should not be reduction",
21406 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21407 OMP_CLAUSE_DECL (c) = decl;
21408 c = finish_omp_clauses (c);
21411 OMP_CLAUSE_CHAIN (c) = clauses;
21418 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21420 /* If decl is an iterator, preserve LHS and RHS of the relational
21421 expr until finish_omp_for. */
21423 && (type_dependent_expression_p (decl)
21424 || CLASS_TYPE_P (TREE_TYPE (decl))))
21425 cond = cp_parser_omp_for_cond (parser, decl);
21427 cond = cp_parser_condition (parser);
21429 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21434 /* If decl is an iterator, preserve the operator on decl
21435 until finish_omp_for. */
21437 && (type_dependent_expression_p (decl)
21438 || CLASS_TYPE_P (TREE_TYPE (decl))))
21439 incr = cp_parser_omp_for_incr (parser, decl);
21441 incr = cp_parser_expression (parser, false, NULL);
21444 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21445 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21446 /*or_comma=*/false,
21447 /*consume_paren=*/true);
21449 TREE_VEC_ELT (declv, i) = decl;
21450 TREE_VEC_ELT (initv, i) = init;
21451 TREE_VEC_ELT (condv, i) = cond;
21452 TREE_VEC_ELT (incrv, i) = incr;
21454 if (i == collapse - 1)
21457 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21458 in between the collapsed for loops to be still considered perfectly
21459 nested. Hopefully the final version clarifies this.
21460 For now handle (multiple) {'s and empty statements. */
21461 cp_parser_parse_tentatively (parser);
21464 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21466 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21468 cp_lexer_consume_token (parser->lexer);
21471 else if (bracecount
21472 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21473 cp_lexer_consume_token (parser->lexer);
21476 loc = cp_lexer_peek_token (parser->lexer)->location;
21477 error ("%Hnot enough collapsed for loops", &loc);
21478 collapse_err = true;
21479 cp_parser_abort_tentative_parse (parser);
21488 cp_parser_parse_definitely (parser);
21489 nbraces += bracecount;
21493 /* Note that we saved the original contents of this flag when we entered
21494 the structured block, and so we don't need to re-save it here. */
21495 parser->in_statement = IN_OMP_FOR;
21497 /* Note that the grammar doesn't call for a structured block here,
21498 though the loop as a whole is a structured block. */
21499 body = push_stmt_list ();
21500 cp_parser_statement (parser, NULL_TREE, false, NULL);
21501 body = pop_stmt_list (body);
21503 if (declv == NULL_TREE)
21506 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21507 pre_body, clauses);
21511 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21513 cp_lexer_consume_token (parser->lexer);
21516 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21517 cp_lexer_consume_token (parser->lexer);
21522 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21523 error ("%Hcollapsed loops not perfectly nested", &loc);
21525 collapse_err = true;
21526 cp_parser_statement_seq_opt (parser, NULL);
21527 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21533 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21534 for_block = TREE_CHAIN (for_block);
21541 #pragma omp for for-clause[optseq] new-line
21544 #define OMP_FOR_CLAUSE_MASK \
21545 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21546 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21547 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21548 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21549 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21550 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21551 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21552 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21555 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21557 tree clauses, sb, ret;
21560 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21561 "#pragma omp for", pragma_tok);
21563 sb = begin_omp_structured_block ();
21564 save = cp_parser_begin_omp_structured_block (parser);
21566 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21568 cp_parser_end_omp_structured_block (parser, save);
21569 add_stmt (finish_omp_structured_block (sb));
21575 # pragma omp master new-line
21576 structured-block */
21579 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21581 cp_parser_require_pragma_eol (parser, pragma_tok);
21582 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21586 # pragma omp ordered new-line
21587 structured-block */
21590 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21592 cp_parser_require_pragma_eol (parser, pragma_tok);
21593 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21599 { section-sequence }
21602 section-directive[opt] structured-block
21603 section-sequence section-directive structured-block */
21606 cp_parser_omp_sections_scope (cp_parser *parser)
21608 tree stmt, substmt;
21609 bool error_suppress = false;
21612 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21615 stmt = push_stmt_list ();
21617 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21621 substmt = begin_omp_structured_block ();
21622 save = cp_parser_begin_omp_structured_block (parser);
21626 cp_parser_statement (parser, NULL_TREE, false, NULL);
21628 tok = cp_lexer_peek_token (parser->lexer);
21629 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21631 if (tok->type == CPP_CLOSE_BRACE)
21633 if (tok->type == CPP_EOF)
21637 cp_parser_end_omp_structured_block (parser, save);
21638 substmt = finish_omp_structured_block (substmt);
21639 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21640 add_stmt (substmt);
21645 tok = cp_lexer_peek_token (parser->lexer);
21646 if (tok->type == CPP_CLOSE_BRACE)
21648 if (tok->type == CPP_EOF)
21651 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21653 cp_lexer_consume_token (parser->lexer);
21654 cp_parser_require_pragma_eol (parser, tok);
21655 error_suppress = false;
21657 else if (!error_suppress)
21659 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21660 error_suppress = true;
21663 substmt = cp_parser_omp_structured_block (parser);
21664 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21665 add_stmt (substmt);
21667 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21669 substmt = pop_stmt_list (stmt);
21671 stmt = make_node (OMP_SECTIONS);
21672 TREE_TYPE (stmt) = void_type_node;
21673 OMP_SECTIONS_BODY (stmt) = substmt;
21680 # pragma omp sections sections-clause[optseq] newline
21683 #define OMP_SECTIONS_CLAUSE_MASK \
21684 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21685 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21686 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21687 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21688 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21691 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21695 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21696 "#pragma omp sections", pragma_tok);
21698 ret = cp_parser_omp_sections_scope (parser);
21700 OMP_SECTIONS_CLAUSES (ret) = clauses;
21706 # pragma parallel parallel-clause new-line
21707 # pragma parallel for parallel-for-clause new-line
21708 # pragma parallel sections parallel-sections-clause new-line */
21710 #define OMP_PARALLEL_CLAUSE_MASK \
21711 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21712 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21713 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21714 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21715 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21716 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21717 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21718 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21721 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21723 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21724 const char *p_name = "#pragma omp parallel";
21725 tree stmt, clauses, par_clause, ws_clause, block;
21726 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21729 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21731 cp_lexer_consume_token (parser->lexer);
21732 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21733 p_name = "#pragma omp parallel for";
21734 mask |= OMP_FOR_CLAUSE_MASK;
21735 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21737 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21739 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21740 const char *p = IDENTIFIER_POINTER (id);
21741 if (strcmp (p, "sections") == 0)
21743 cp_lexer_consume_token (parser->lexer);
21744 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21745 p_name = "#pragma omp parallel sections";
21746 mask |= OMP_SECTIONS_CLAUSE_MASK;
21747 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21751 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21752 block = begin_omp_parallel ();
21753 save = cp_parser_begin_omp_structured_block (parser);
21757 case PRAGMA_OMP_PARALLEL:
21758 cp_parser_statement (parser, NULL_TREE, false, NULL);
21759 par_clause = clauses;
21762 case PRAGMA_OMP_PARALLEL_FOR:
21763 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21764 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21767 case PRAGMA_OMP_PARALLEL_SECTIONS:
21768 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21769 stmt = cp_parser_omp_sections_scope (parser);
21771 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21775 gcc_unreachable ();
21778 cp_parser_end_omp_structured_block (parser, save);
21779 stmt = finish_omp_parallel (par_clause, block);
21780 if (p_kind != PRAGMA_OMP_PARALLEL)
21781 OMP_PARALLEL_COMBINED (stmt) = 1;
21786 # pragma omp single single-clause[optseq] new-line
21787 structured-block */
21789 #define OMP_SINGLE_CLAUSE_MASK \
21790 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21791 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21792 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21793 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21796 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21798 tree stmt = make_node (OMP_SINGLE);
21799 TREE_TYPE (stmt) = void_type_node;
21801 OMP_SINGLE_CLAUSES (stmt)
21802 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21803 "#pragma omp single", pragma_tok);
21804 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21806 return add_stmt (stmt);
21810 # pragma omp task task-clause[optseq] new-line
21811 structured-block */
21813 #define OMP_TASK_CLAUSE_MASK \
21814 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21815 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21816 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21817 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21818 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21819 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21822 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21824 tree clauses, block;
21827 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21828 "#pragma omp task", pragma_tok);
21829 block = begin_omp_task ();
21830 save = cp_parser_begin_omp_structured_block (parser);
21831 cp_parser_statement (parser, NULL_TREE, false, NULL);
21832 cp_parser_end_omp_structured_block (parser, save);
21833 return finish_omp_task (clauses, block);
21837 # pragma omp taskwait new-line */
21840 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21842 cp_parser_require_pragma_eol (parser, pragma_tok);
21843 finish_omp_taskwait ();
21847 # pragma omp threadprivate (variable-list) */
21850 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21854 vars = cp_parser_omp_var_list (parser, 0, NULL);
21855 cp_parser_require_pragma_eol (parser, pragma_tok);
21857 finish_omp_threadprivate (vars);
21860 /* Main entry point to OpenMP statement pragmas. */
21863 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21867 switch (pragma_tok->pragma_kind)
21869 case PRAGMA_OMP_ATOMIC:
21870 cp_parser_omp_atomic (parser, pragma_tok);
21872 case PRAGMA_OMP_CRITICAL:
21873 stmt = cp_parser_omp_critical (parser, pragma_tok);
21875 case PRAGMA_OMP_FOR:
21876 stmt = cp_parser_omp_for (parser, pragma_tok);
21878 case PRAGMA_OMP_MASTER:
21879 stmt = cp_parser_omp_master (parser, pragma_tok);
21881 case PRAGMA_OMP_ORDERED:
21882 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21884 case PRAGMA_OMP_PARALLEL:
21885 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21887 case PRAGMA_OMP_SECTIONS:
21888 stmt = cp_parser_omp_sections (parser, pragma_tok);
21890 case PRAGMA_OMP_SINGLE:
21891 stmt = cp_parser_omp_single (parser, pragma_tok);
21893 case PRAGMA_OMP_TASK:
21894 stmt = cp_parser_omp_task (parser, pragma_tok);
21897 gcc_unreachable ();
21901 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21906 static GTY (()) cp_parser *the_parser;
21909 /* Special handling for the first token or line in the file. The first
21910 thing in the file might be #pragma GCC pch_preprocess, which loads a
21911 PCH file, which is a GC collection point. So we need to handle this
21912 first pragma without benefit of an existing lexer structure.
21914 Always returns one token to the caller in *FIRST_TOKEN. This is
21915 either the true first token of the file, or the first token after
21916 the initial pragma. */
21919 cp_parser_initial_pragma (cp_token *first_token)
21923 cp_lexer_get_preprocessor_token (NULL, first_token);
21924 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21927 cp_lexer_get_preprocessor_token (NULL, first_token);
21928 if (first_token->type == CPP_STRING)
21930 name = first_token->u.value;
21932 cp_lexer_get_preprocessor_token (NULL, first_token);
21933 if (first_token->type != CPP_PRAGMA_EOL)
21934 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21935 &first_token->location);
21938 error ("%Hexpected string literal", &first_token->location);
21940 /* Skip to the end of the pragma. */
21941 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21942 cp_lexer_get_preprocessor_token (NULL, first_token);
21944 /* Now actually load the PCH file. */
21946 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21948 /* Read one more token to return to our caller. We have to do this
21949 after reading the PCH file in, since its pointers have to be
21951 cp_lexer_get_preprocessor_token (NULL, first_token);
21954 /* Normal parsing of a pragma token. Here we can (and must) use the
21958 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21960 cp_token *pragma_tok;
21963 pragma_tok = cp_lexer_consume_token (parser->lexer);
21964 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21965 parser->lexer->in_pragma = true;
21967 id = pragma_tok->pragma_kind;
21970 case PRAGMA_GCC_PCH_PREPROCESS:
21971 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21972 &pragma_tok->location);
21975 case PRAGMA_OMP_BARRIER:
21978 case pragma_compound:
21979 cp_parser_omp_barrier (parser, pragma_tok);
21982 error ("%H%<#pragma omp barrier%> may only be "
21983 "used in compound statements", &pragma_tok->location);
21990 case PRAGMA_OMP_FLUSH:
21993 case pragma_compound:
21994 cp_parser_omp_flush (parser, pragma_tok);
21997 error ("%H%<#pragma omp flush%> may only be "
21998 "used in compound statements", &pragma_tok->location);
22005 case PRAGMA_OMP_TASKWAIT:
22008 case pragma_compound:
22009 cp_parser_omp_taskwait (parser, pragma_tok);
22012 error ("%H%<#pragma omp taskwait%> may only be "
22013 "used in compound statements",
22014 &pragma_tok->location);
22021 case PRAGMA_OMP_THREADPRIVATE:
22022 cp_parser_omp_threadprivate (parser, pragma_tok);
22025 case PRAGMA_OMP_ATOMIC:
22026 case PRAGMA_OMP_CRITICAL:
22027 case PRAGMA_OMP_FOR:
22028 case PRAGMA_OMP_MASTER:
22029 case PRAGMA_OMP_ORDERED:
22030 case PRAGMA_OMP_PARALLEL:
22031 case PRAGMA_OMP_SECTIONS:
22032 case PRAGMA_OMP_SINGLE:
22033 case PRAGMA_OMP_TASK:
22034 if (context == pragma_external)
22036 cp_parser_omp_construct (parser, pragma_tok);
22039 case PRAGMA_OMP_SECTION:
22040 error ("%H%<#pragma omp section%> may only be used in "
22041 "%<#pragma omp sections%> construct", &pragma_tok->location);
22045 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22046 c_invoke_pragma_handler (id);
22050 cp_parser_error (parser, "expected declaration specifiers");
22054 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22058 /* The interface the pragma parsers have to the lexer. */
22061 pragma_lex (tree *value)
22064 enum cpp_ttype ret;
22066 tok = cp_lexer_peek_token (the_parser->lexer);
22069 *value = tok->u.value;
22071 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22073 else if (ret == CPP_STRING)
22074 *value = cp_parser_string_literal (the_parser, false, false);
22077 cp_lexer_consume_token (the_parser->lexer);
22078 if (ret == CPP_KEYWORD)
22086 /* External interface. */
22088 /* Parse one entire translation unit. */
22091 c_parse_file (void)
22093 bool error_occurred;
22094 static bool already_called = false;
22096 if (already_called)
22098 sorry ("inter-module optimizations not implemented for C++");
22101 already_called = true;
22103 the_parser = cp_parser_new ();
22104 push_deferring_access_checks (flag_access_control
22105 ? dk_no_deferred : dk_no_check);
22106 error_occurred = cp_parser_translation_unit (the_parser);
22110 #include "gt-cp-parser.h"