2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 /* The location at which this token was found. */
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
102 /* The cp_lexer structure represents the C++ lexer. It is responsible
103 for managing the token stream from the preprocessor and supplying
104 it to the parser. Tokens are never added to the cp_lexer after
107 typedef struct cp_lexer GTY (())
109 /* The memory allocated for the buffer. NULL if this lexer does not
110 own the token buffer. */
111 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112 /* If the lexer owns the buffer, this is the number of tokens in the
114 size_t buffer_length;
116 /* A pointer just past the last available token. The tokens
117 in this lexer are [buffer, last_token). */
118 cp_token_position GTY ((skip)) last_token;
120 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
121 no more available tokens. */
122 cp_token_position GTY ((skip)) next_token;
124 /* A stack indicating positions at which cp_lexer_save_tokens was
125 called. The top entry is the most recent position at which we
126 began saving tokens. If the stack is non-empty, we are saving
128 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
130 /* The next lexer in a linked list of lexers. */
131 struct cp_lexer *next;
133 /* True if we should output debugging information. */
136 /* True if we're in the context of parsing a pragma, and should not
137 increment past the end-of-line marker. */
141 /* cp_token_cache is a range of tokens. There is no need to represent
142 allocate heap memory for it, since tokens are never removed from the
143 lexer's array. There is also no need for the GC to walk through
144 a cp_token_cache, since everything in here is referenced through
147 typedef struct cp_token_cache GTY(())
149 /* The beginning of the token range. */
150 cp_token * GTY((skip)) first;
152 /* Points immediately after the last token in the range. */
153 cp_token * GTY ((skip)) last;
158 static cp_lexer *cp_lexer_new_main
160 static cp_lexer *cp_lexer_new_from_tokens
161 (cp_token_cache *tokens);
162 static void cp_lexer_destroy
164 static int cp_lexer_saving_tokens
166 static cp_token_position cp_lexer_token_position
168 static cp_token *cp_lexer_token_at
169 (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171 (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
174 static cp_token *cp_lexer_peek_nth_token
175 (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177 (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181 (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
184 static void cp_lexer_purge_token
186 static void cp_lexer_purge_tokens_after
187 (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
190 static void cp_lexer_commit_tokens
192 static void cp_lexer_rollback_tokens
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196 (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
199 static void cp_lexer_start_debugging
200 (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205 about passing NULL to functions that require non-NULL arguments
206 (fputs, fprintf). It will never be used, so all we need is a value
207 of the right type that's guaranteed not to be NULL. */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
213 static cp_token_cache *cp_token_cache_new
214 (cp_token *, cp_token *);
216 static void cp_parser_initial_pragma
219 /* Manifest constants. */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
223 /* A token type for keywords, as opposed to ordinary identifiers. */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
226 /* A token type for template-ids. If a template-id is processed while
227 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228 the value of the CPP_TEMPLATE_ID is whatever was returned by
229 cp_parser_template_id. */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
232 /* A token type for nested-name-specifiers. If a
233 nested-name-specifier is processed while parsing tentatively, it is
234 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236 cp_parser_nested_name_specifier_opt. */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
239 /* A token type for tokens that are not tokens at all; these are used
240 to represent slots in the array where there used to be a token
241 that has now been deleted. */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
244 /* The number of token types, including C++-specific ones. */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written. */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
313 cpp_get_options (parse_in)->client_diagnostic = true;
314 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
348 /* Frees all resources associated with LEXER. */
351 cp_lexer_destroy (cp_lexer *lexer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
364 cp_lexer_debugging_p (cp_lexer *lexer)
366 return lexer->debugging_p;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
376 return lexer->next_token - previous_p;
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
385 /* nonzero if we are presently saving tokens. */
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
401 static int is_extern_c = 0;
403 /* Get a new token from the preprocessor. */
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
420 if (C_IS_RESERVED_WORD (token->u.value))
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
426 /* Update the value. Some keywords are mapped to particular
427 entities, rather than simply having the value of the
428 corresponding IDENTIFIER_NODE. For example, `__const' is
429 mapped to `const'. */
430 token->u.value = ridpointers[token->keyword];
434 if (warn_cxx0x_compat
435 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
438 /* Warn about the C++0x keyword (but still treat it as
440 warning (OPT_Wc__0x_compat,
441 "identifier %<%s%> will become a keyword in C++0x",
442 IDENTIFIER_POINTER (token->u.value));
444 /* Clear out the C_RID_CODE so we don't warn about this
445 particular identifier-turned-keyword again. */
446 C_SET_RID_CODE (token->u.value, RID_MAX);
449 token->ambiguous_p = false;
450 token->keyword = RID_MAX;
453 /* Handle Objective-C++ keywords. */
454 else if (token->type == CPP_AT_NAME)
456 token->type = CPP_KEYWORD;
457 switch (C_RID_CODE (token->u.value))
459 /* Map 'class' to '@class', 'private' to '@private', etc. */
460 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464 case RID_THROW: token->keyword = RID_AT_THROW; break;
465 case RID_TRY: token->keyword = RID_AT_TRY; break;
466 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467 default: token->keyword = C_RID_CODE (token->u.value);
470 else if (token->type == CPP_PRAGMA)
472 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
473 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474 token->u.value = NULL_TREE;
478 /* Update the globals input_location and the input file stack from TOKEN. */
480 cp_lexer_set_source_position_from_token (cp_token *token)
482 if (token->type != CPP_EOF)
484 input_location = token->location;
488 /* Return a pointer to the next token in the token stream, but do not
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
494 if (cp_lexer_debugging_p (lexer))
496 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498 putc ('\n', cp_lexer_debug_stream);
500 return lexer->next_token;
503 /* Return true if the next token has the indicated TYPE. */
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
508 return cp_lexer_peek_token (lexer)->type == type;
511 /* Return true if the next token does not have the indicated TYPE. */
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
516 return !cp_lexer_next_token_is (lexer, type);
519 /* Return true if the next token is the indicated KEYWORD. */
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
524 return cp_lexer_peek_token (lexer)->keyword == keyword;
527 /* Return true if the next token is not the indicated KEYWORD. */
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
532 return cp_lexer_peek_token (lexer)->keyword != keyword;
535 /* Return true if the next token is a keyword for a decl-specifier. */
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
542 token = cp_lexer_peek_token (lexer);
543 switch (token->keyword)
545 /* auto specifier: storage-class-specifier in C++,
546 simple-type-specifier in C++0x. */
548 /* Storage classes. */
554 /* Elaborated type specifiers. */
560 /* Simple type specifiers. */
574 /* GNU extensions. */
577 /* C++0x extensions. */
586 /* Return a pointer to the Nth token in the token stream. If N is 1,
587 then this is precisely equivalent to cp_lexer_peek_token (except
588 that it is not inline). One would like to disallow that case, but
589 there is one case (cp_parser_nth_token_starts_template_id) where
590 the caller passes a variable for N and it might be 1. */
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
597 /* N is 1-based, not zero-based. */
600 if (cp_lexer_debugging_p (lexer))
601 fprintf (cp_lexer_debug_stream,
602 "cp_lexer: peeking ahead %ld at token: ", (long)n);
605 token = lexer->next_token;
606 gcc_assert (!n || token != &eof_token);
610 if (token == lexer->last_token)
616 if (token->type != CPP_PURGED)
620 if (cp_lexer_debugging_p (lexer))
622 cp_lexer_print_token (cp_lexer_debug_stream, token);
623 putc ('\n', cp_lexer_debug_stream);
629 /* Return the next token, and advance the lexer's next_token pointer
630 to point to the next non-purged token. */
633 cp_lexer_consume_token (cp_lexer* lexer)
635 cp_token *token = lexer->next_token;
637 gcc_assert (token != &eof_token);
638 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
643 if (lexer->next_token == lexer->last_token)
645 lexer->next_token = &eof_token;
650 while (lexer->next_token->type == CPP_PURGED);
652 cp_lexer_set_source_position_from_token (token);
654 /* Provide debugging output. */
655 if (cp_lexer_debugging_p (lexer))
657 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658 cp_lexer_print_token (cp_lexer_debug_stream, token);
659 putc ('\n', cp_lexer_debug_stream);
665 /* Permanently remove the next token from the token stream, and
666 advance the next_token pointer to refer to the next non-purged
670 cp_lexer_purge_token (cp_lexer *lexer)
672 cp_token *tok = lexer->next_token;
674 gcc_assert (tok != &eof_token);
675 tok->type = CPP_PURGED;
676 tok->location = UNKNOWN_LOCATION;
677 tok->u.value = NULL_TREE;
678 tok->keyword = RID_MAX;
683 if (tok == lexer->last_token)
689 while (tok->type == CPP_PURGED);
690 lexer->next_token = tok;
693 /* Permanently remove all tokens after TOK, up to, but not
694 including, the token that will be returned next by
695 cp_lexer_peek_token. */
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
700 cp_token *peek = lexer->next_token;
702 if (peek == &eof_token)
703 peek = lexer->last_token;
705 gcc_assert (tok < peek);
707 for ( tok += 1; tok != peek; tok += 1)
709 tok->type = CPP_PURGED;
710 tok->location = UNKNOWN_LOCATION;
711 tok->u.value = NULL_TREE;
712 tok->keyword = RID_MAX;
716 /* Begin saving tokens. All tokens consumed after this point will be
720 cp_lexer_save_tokens (cp_lexer* lexer)
722 /* Provide debugging output. */
723 if (cp_lexer_debugging_p (lexer))
724 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
726 VEC_safe_push (cp_token_position, heap,
727 lexer->saved_tokens, lexer->next_token);
730 /* Commit to the portion of the token stream most recently saved. */
733 cp_lexer_commit_tokens (cp_lexer* lexer)
735 /* Provide debugging output. */
736 if (cp_lexer_debugging_p (lexer))
737 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
739 VEC_pop (cp_token_position, lexer->saved_tokens);
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743 to the token stream. Stop saving tokens. */
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
748 /* Provide debugging output. */
749 if (cp_lexer_debugging_p (lexer))
750 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
752 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
755 /* Print a representation of the TOKEN on the STREAM. */
757 #ifdef ENABLE_CHECKING
760 cp_lexer_print_token (FILE * stream, cp_token *token)
762 /* We don't use cpp_type2name here because the parser defines
763 a few tokens of its own. */
764 static const char *const token_names[] = {
765 /* cpplib-defined token types */
771 /* C++ parser token types - see "Manifest constants", above. */
774 "NESTED_NAME_SPECIFIER",
778 /* If we have a name for the token, print it out. Otherwise, we
779 simply give the numeric code. */
780 gcc_assert (token->type < ARRAY_SIZE(token_names));
781 fputs (token_names[token->type], stream);
783 /* For some tokens, print the associated data. */
787 /* Some keywords have a value that is not an IDENTIFIER_NODE.
788 For example, `struct' is mapped to an INTEGER_CST. */
789 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791 /* else fall through */
793 fputs (IDENTIFIER_POINTER (token->u.value), stream);
800 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
808 /* Start emitting debugging information. */
811 cp_lexer_start_debugging (cp_lexer* lexer)
813 lexer->debugging_p = true;
816 /* Stop emitting debugging information. */
819 cp_lexer_stop_debugging (cp_lexer* lexer)
821 lexer->debugging_p = false;
824 #endif /* ENABLE_CHECKING */
826 /* Create a new cp_token_cache, representing a range of tokens. */
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
831 cp_token_cache *cache = GGC_NEW (cp_token_cache);
832 cache->first = first;
838 /* Decl-specifiers. */
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
845 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
850 /* Nothing other than the parser should be creating declarators;
851 declarators are a semi-syntactic representation of C++ entities.
852 Other parts of the front end that need to create entities (like
853 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
855 static cp_declarator *make_call_declarator
856 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, 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,
1016 cp_parameter_declarator *parms,
1017 cp_cv_quals cv_qualifiers,
1018 tree exception_specification)
1020 cp_declarator *declarator;
1022 declarator = make_declarator (cdk_function);
1023 declarator->declarator = target;
1024 declarator->u.function.parameters = parms;
1025 declarator->u.function.qualifiers = cv_qualifiers;
1026 declarator->u.function.exception_specification = exception_specification;
1029 declarator->parameter_pack_p = target->parameter_pack_p;
1030 target->parameter_pack_p = false;
1033 declarator->parameter_pack_p = false;
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039 defined by ELEMENT. */
1042 make_array_declarator (cp_declarator *element, tree bounds)
1044 cp_declarator *declarator;
1046 declarator = make_declarator (cdk_array);
1047 declarator->declarator = element;
1048 declarator->u.array.bounds = bounds;
1051 declarator->parameter_pack_p = element->parameter_pack_p;
1052 element->parameter_pack_p = false;
1055 declarator->parameter_pack_p = false;
1060 /* Determine whether the declarator we've seen so far can be a
1061 parameter pack, when followed by an ellipsis. */
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1065 /* Search for a declarator name, or any other declarator that goes
1066 after the point where the ellipsis could appear in a parameter
1067 pack. If we find any of these, then this declarator can not be
1068 made into a parameter pack. */
1070 while (declarator && !found)
1072 switch ((int)declarator->kind)
1083 declarator = declarator->declarator;
1091 cp_parameter_declarator *no_parameters;
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094 DECLARATOR and DEFAULT_ARGUMENT. */
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098 cp_declarator *declarator,
1099 tree default_argument)
1101 cp_parameter_declarator *parameter;
1103 parameter = ((cp_parameter_declarator *)
1104 alloc_declarator (sizeof (cp_parameter_declarator)));
1105 parameter->next = NULL;
1106 if (decl_specifiers)
1107 parameter->decl_specifiers = *decl_specifiers;
1109 clear_decl_specs (¶meter->decl_specifiers);
1110 parameter->declarator = declarator;
1111 parameter->default_argument = default_argument;
1112 parameter->ellipsis_p = false;
1117 /* Returns true iff DECLARATOR is a declaration for a function. */
1120 function_declarator_p (const cp_declarator *declarator)
1124 if (declarator->kind == cdk_function
1125 && declarator->declarator->kind == cdk_id)
1127 if (declarator->kind == cdk_id
1128 || declarator->kind == cdk_error)
1130 declarator = declarator->declarator;
1140 A cp_parser parses the token stream as specified by the C++
1141 grammar. Its job is purely parsing, not semantic analysis. For
1142 example, the parser breaks the token stream into declarators,
1143 expressions, statements, and other similar syntactic constructs.
1144 It does not check that the types of the expressions on either side
1145 of an assignment-statement are compatible, or that a function is
1146 not declared with a parameter of type `void'.
1148 The parser invokes routines elsewhere in the compiler to perform
1149 semantic analysis and to build up the abstract syntax tree for the
1152 The parser (and the template instantiation code, which is, in a
1153 way, a close relative of parsing) are the only parts of the
1154 compiler that should be calling push_scope and pop_scope, or
1155 related functions. The parser (and template instantiation code)
1156 keeps track of what scope is presently active; everything else
1157 should simply honor that. (The code that generates static
1158 initializers may also need to set the scope, in order to check
1159 access control correctly when emitting the initializers.)
1164 The parser is of the standard recursive-descent variety. Upcoming
1165 tokens in the token stream are examined in order to determine which
1166 production to use when parsing a non-terminal. Some C++ constructs
1167 require arbitrary look ahead to disambiguate. For example, it is
1168 impossible, in the general case, to tell whether a statement is an
1169 expression or declaration without scanning the entire statement.
1170 Therefore, the parser is capable of "parsing tentatively." When the
1171 parser is not sure what construct comes next, it enters this mode.
1172 Then, while we attempt to parse the construct, the parser queues up
1173 error messages, rather than issuing them immediately, and saves the
1174 tokens it consumes. If the construct is parsed successfully, the
1175 parser "commits", i.e., it issues any queued error messages and
1176 the tokens that were being preserved are permanently discarded.
1177 If, however, the construct is not parsed successfully, the parser
1178 rolls back its state completely so that it can resume parsing using
1179 a different alternative.
1184 The performance of the parser could probably be improved substantially.
1185 We could often eliminate the need to parse tentatively by looking ahead
1186 a little bit. In some places, this approach might not entirely eliminate
1187 the need to parse tentatively, but it might still speed up the average
1190 /* Flags that are passed to some parsing functions. These values can
1191 be bitwise-ored together. */
1193 typedef enum cp_parser_flags
1196 CP_PARSER_FLAGS_NONE = 0x0,
1197 /* The construct is optional. If it is not present, then no error
1198 should be issued. */
1199 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200 /* When parsing a type-specifier, do not allow user-defined types. */
1201 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 /* The different kinds of declarators we want to parse. */
1206 typedef enum cp_parser_declarator_kind
1208 /* We want an abstract declarator. */
1209 CP_PARSER_DECLARATOR_ABSTRACT,
1210 /* We want a named declarator. */
1211 CP_PARSER_DECLARATOR_NAMED,
1212 /* We don't mind, but the name must be an unqualified-id. */
1213 CP_PARSER_DECLARATOR_EITHER
1214 } cp_parser_declarator_kind;
1216 /* The precedence values used to parse binary expressions. The minimum value
1217 of PREC must be 1, because zero is reserved to quickly discriminate
1218 binary operators from other tokens. */
1223 PREC_LOGICAL_OR_EXPRESSION,
1224 PREC_LOGICAL_AND_EXPRESSION,
1225 PREC_INCLUSIVE_OR_EXPRESSION,
1226 PREC_EXCLUSIVE_OR_EXPRESSION,
1227 PREC_AND_EXPRESSION,
1228 PREC_EQUALITY_EXPRESSION,
1229 PREC_RELATIONAL_EXPRESSION,
1230 PREC_SHIFT_EXPRESSION,
1231 PREC_ADDITIVE_EXPRESSION,
1232 PREC_MULTIPLICATIVE_EXPRESSION,
1234 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1237 /* A mapping from a token type to a corresponding tree node type, with a
1238 precedence value. */
1240 typedef struct cp_parser_binary_operations_map_node
1242 /* The token type. */
1243 enum cpp_ttype token_type;
1244 /* The corresponding tree code. */
1245 enum tree_code tree_type;
1246 /* The precedence of this operator. */
1247 enum cp_parser_prec prec;
1248 } cp_parser_binary_operations_map_node;
1250 /* The status of a tentative parse. */
1252 typedef enum cp_parser_status_kind
1254 /* No errors have occurred. */
1255 CP_PARSER_STATUS_KIND_NO_ERROR,
1256 /* An error has occurred. */
1257 CP_PARSER_STATUS_KIND_ERROR,
1258 /* We are committed to this tentative parse, whether or not an error
1260 CP_PARSER_STATUS_KIND_COMMITTED
1261 } cp_parser_status_kind;
1263 typedef struct cp_parser_expression_stack_entry
1265 /* Left hand side of the binary operation we are currently
1268 /* Original tree code for left hand side, if it was a binary
1269 expression itself (used for -Wparentheses). */
1270 enum tree_code lhs_type;
1271 /* Tree code for the binary operation we are parsing. */
1272 enum tree_code tree_type;
1273 /* Precedence of the binary operation we are parsing. */
1275 } cp_parser_expression_stack_entry;
1277 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1278 entries because precedence levels on the stack are monotonically
1280 typedef struct cp_parser_expression_stack_entry
1281 cp_parser_expression_stack[NUM_PREC_VALUES];
1283 /* Context that is saved and restored when parsing tentatively. */
1284 typedef struct cp_parser_context GTY (())
1286 /* If this is a tentative parsing context, the status of the
1288 enum cp_parser_status_kind status;
1289 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1290 that are looked up in this context must be looked up both in the
1291 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292 the context of the containing expression. */
1295 /* The next parsing context in the stack. */
1296 struct cp_parser_context *next;
1297 } cp_parser_context;
1301 /* Constructors and destructors. */
1303 static cp_parser_context *cp_parser_context_new
1304 (cp_parser_context *);
1306 /* Class variables. */
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311 Transformed into an associative array (binops_by_token) by
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1318 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1338 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1340 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1342 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1344 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 /* The same as binops, but initialized by cp_parser_new so that
1348 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1352 /* Constructors and destructors. */
1354 /* Construct a new context. The context below this one on the stack
1355 is given by NEXT. */
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1360 cp_parser_context *context;
1362 /* Allocate the storage. */
1363 if (cp_parser_context_free_list != NULL)
1365 /* Pull the first entry from the free list. */
1366 context = cp_parser_context_free_list;
1367 cp_parser_context_free_list = context->next;
1368 memset (context, 0, sizeof (*context));
1371 context = GGC_CNEW (cp_parser_context);
1373 /* No errors have occurred yet in this context. */
1374 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375 /* If this is not the bottommost context, copy information that we
1376 need from the previous context. */
1379 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380 expression, then we are parsing one in this context, too. */
1381 context->object_type = next->object_type;
1382 /* Thread the stack. */
1383 context->next = next;
1389 /* The cp_parser structure represents the C++ parser. */
1391 typedef struct cp_parser GTY(())
1393 /* The lexer from which we are obtaining tokens. */
1396 /* The scope in which names should be looked up. If NULL_TREE, then
1397 we look up names in the scope that is currently open in the
1398 source program. If non-NULL, this is either a TYPE or
1399 NAMESPACE_DECL for the scope in which we should look. It can
1400 also be ERROR_MARK, when we've parsed a bogus scope.
1402 This value is not cleared automatically after a name is looked
1403 up, so we must be careful to clear it before starting a new look
1404 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1405 will look up `Z' in the scope of `X', rather than the current
1406 scope.) Unfortunately, it is difficult to tell when name lookup
1407 is complete, because we sometimes peek at a token, look it up,
1408 and then decide not to consume it. */
1411 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1412 last lookup took place. OBJECT_SCOPE is used if an expression
1413 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1414 respectively. QUALIFYING_SCOPE is used for an expression of the
1415 form "X::Y"; it refers to X. */
1417 tree qualifying_scope;
1419 /* A stack of parsing contexts. All but the bottom entry on the
1420 stack will be tentative contexts.
1422 We parse tentatively in order to determine which construct is in
1423 use in some situations. For example, in order to determine
1424 whether a statement is an expression-statement or a
1425 declaration-statement we parse it tentatively as a
1426 declaration-statement. If that fails, we then reparse the same
1427 token stream as an expression-statement. */
1428 cp_parser_context *context;
1430 /* True if we are parsing GNU C++. If this flag is not set, then
1431 GNU extensions are not recognized. */
1432 bool allow_gnu_extensions_p;
1434 /* TRUE if the `>' token should be interpreted as the greater-than
1435 operator. FALSE if it is the end of a template-id or
1436 template-parameter-list. In C++0x mode, this flag also applies to
1437 `>>' tokens, which are viewed as two consecutive `>' tokens when
1438 this flag is FALSE. */
1439 bool greater_than_is_operator_p;
1441 /* TRUE if default arguments are allowed within a parameter list
1442 that starts at this point. FALSE if only a gnu extension makes
1443 them permissible. */
1444 bool default_arg_ok_p;
1446 /* TRUE if we are parsing an integral constant-expression. See
1447 [expr.const] for a precise definition. */
1448 bool integral_constant_expression_p;
1450 /* TRUE if we are parsing an integral constant-expression -- but a
1451 non-constant expression should be permitted as well. This flag
1452 is used when parsing an array bound so that GNU variable-length
1453 arrays are tolerated. */
1454 bool allow_non_integral_constant_expression_p;
1456 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1457 been seen that makes the expression non-constant. */
1458 bool non_integral_constant_expression_p;
1460 /* TRUE if local variable names and `this' are forbidden in the
1462 bool local_variables_forbidden_p;
1464 /* TRUE if the declaration we are parsing is part of a
1465 linkage-specification of the form `extern string-literal
1467 bool in_unbraced_linkage_specification_p;
1469 /* TRUE if we are presently parsing a declarator, after the
1470 direct-declarator. */
1471 bool in_declarator_p;
1473 /* TRUE if we are presently parsing a template-argument-list. */
1474 bool in_template_argument_list_p;
1476 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1477 to IN_OMP_BLOCK if parsing OpenMP structured block and
1478 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1479 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1480 iteration-statement, OpenMP block or loop within that switch. */
1481 #define IN_SWITCH_STMT 1
1482 #define IN_ITERATION_STMT 2
1483 #define IN_OMP_BLOCK 4
1484 #define IN_OMP_FOR 8
1485 #define IN_IF_STMT 16
1486 unsigned char in_statement;
1488 /* TRUE if we are presently parsing the body of a switch statement.
1489 Note that this doesn't quite overlap with in_statement above.
1490 The difference relates to giving the right sets of error messages:
1491 "case not in switch" vs "break statement used with OpenMP...". */
1492 bool in_switch_statement_p;
1494 /* TRUE if we are parsing a type-id in an expression context. In
1495 such a situation, both "type (expr)" and "type (type)" are valid
1497 bool in_type_id_in_expr_p;
1499 /* TRUE if we are currently in a header file where declarations are
1500 implicitly extern "C". */
1501 bool implicit_extern_c;
1503 /* TRUE if strings in expressions should be translated to the execution
1505 bool translate_strings_p;
1507 /* TRUE if we are presently parsing the body of a function, but not
1509 bool in_function_body;
1511 /* If non-NULL, then we are parsing a construct where new type
1512 definitions are not permitted. The string stored here will be
1513 issued as an error message if a type is defined. */
1514 const char *type_definition_forbidden_message;
1516 /* A list of lists. The outer list is a stack, used for member
1517 functions of local classes. At each level there are two sub-list,
1518 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1519 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1520 TREE_VALUE's. The functions are chained in reverse declaration
1523 The TREE_PURPOSE sublist contains those functions with default
1524 arguments that need post processing, and the TREE_VALUE sublist
1525 contains those functions with definitions that need post
1528 These lists can only be processed once the outermost class being
1529 defined is complete. */
1530 tree unparsed_functions_queues;
1532 /* The number of classes whose definitions are currently in
1534 unsigned num_classes_being_defined;
1536 /* The number of template parameter lists that apply directly to the
1537 current declaration. */
1538 unsigned num_template_parameter_lists;
1543 /* Constructors and destructors. */
1545 static cp_parser *cp_parser_new
1548 /* Routines to parse various constructs.
1550 Those that return `tree' will return the error_mark_node (rather
1551 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1552 Sometimes, they will return an ordinary node if error-recovery was
1553 attempted, even though a parse error occurred. So, to check
1554 whether or not a parse error occurred, you should always use
1555 cp_parser_error_occurred. If the construct is optional (indicated
1556 either by an `_opt' in the name of the function that does the
1557 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1558 the construct is not present. */
1560 /* Lexical conventions [gram.lex] */
1562 static tree cp_parser_identifier
1564 static tree cp_parser_string_literal
1565 (cp_parser *, bool, bool);
1567 /* Basic concepts [gram.basic] */
1569 static bool cp_parser_translation_unit
1572 /* Expressions [gram.expr] */
1574 static tree cp_parser_primary_expression
1575 (cp_parser *, bool, bool, bool, cp_id_kind *);
1576 static tree cp_parser_id_expression
1577 (cp_parser *, bool, bool, bool *, bool, bool);
1578 static tree cp_parser_unqualified_id
1579 (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier_opt
1581 (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_class_or_namespace_name
1585 (cp_parser *, bool, bool, bool, bool, bool);
1586 static tree cp_parser_postfix_expression
1587 (cp_parser *, bool, bool, bool);
1588 static tree cp_parser_postfix_open_square_expression
1589 (cp_parser *, tree, bool);
1590 static tree cp_parser_postfix_dot_deref_expression
1591 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1592 static tree cp_parser_parenthesized_expression_list
1593 (cp_parser *, bool, bool, bool, bool *);
1594 static void cp_parser_pseudo_destructor_name
1595 (cp_parser *, tree *, tree *);
1596 static tree cp_parser_unary_expression
1597 (cp_parser *, bool, bool);
1598 static enum tree_code cp_parser_unary_operator
1600 static tree cp_parser_new_expression
1602 static tree cp_parser_new_placement
1604 static tree cp_parser_new_type_id
1605 (cp_parser *, tree *);
1606 static cp_declarator *cp_parser_new_declarator_opt
1608 static cp_declarator *cp_parser_direct_new_declarator
1610 static tree cp_parser_new_initializer
1612 static tree cp_parser_delete_expression
1614 static tree cp_parser_cast_expression
1615 (cp_parser *, bool, bool);
1616 static tree cp_parser_binary_expression
1617 (cp_parser *, bool, enum cp_parser_prec);
1618 static tree cp_parser_question_colon_clause
1619 (cp_parser *, tree);
1620 static tree cp_parser_assignment_expression
1621 (cp_parser *, bool);
1622 static enum tree_code cp_parser_assignment_operator_opt
1624 static tree cp_parser_expression
1625 (cp_parser *, bool);
1626 static tree cp_parser_constant_expression
1627 (cp_parser *, bool, bool *);
1628 static tree cp_parser_builtin_offsetof
1631 /* Statements [gram.stmt.stmt] */
1633 static void cp_parser_statement
1634 (cp_parser *, tree, bool, bool *);
1635 static void cp_parser_label_for_labeled_statement
1637 static tree cp_parser_expression_statement
1638 (cp_parser *, tree);
1639 static tree cp_parser_compound_statement
1640 (cp_parser *, tree, bool);
1641 static void cp_parser_statement_seq_opt
1642 (cp_parser *, tree);
1643 static tree cp_parser_selection_statement
1644 (cp_parser *, bool *);
1645 static tree cp_parser_condition
1647 static tree cp_parser_iteration_statement
1649 static void cp_parser_for_init_statement
1651 static tree cp_parser_jump_statement
1653 static void cp_parser_declaration_statement
1656 static tree cp_parser_implicitly_scoped_statement
1657 (cp_parser *, bool *);
1658 static void cp_parser_already_scoped_statement
1661 /* Declarations [gram.dcl.dcl] */
1663 static void cp_parser_declaration_seq_opt
1665 static void cp_parser_declaration
1667 static void cp_parser_block_declaration
1668 (cp_parser *, bool);
1669 static void cp_parser_simple_declaration
1670 (cp_parser *, bool);
1671 static void cp_parser_decl_specifier_seq
1672 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1673 static tree cp_parser_storage_class_specifier_opt
1675 static tree cp_parser_function_specifier_opt
1676 (cp_parser *, cp_decl_specifier_seq *);
1677 static tree cp_parser_type_specifier
1678 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1680 static tree cp_parser_simple_type_specifier
1681 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1682 static tree cp_parser_type_name
1684 static tree cp_parser_nonclass_name
1685 (cp_parser* parser);
1686 static tree cp_parser_elaborated_type_specifier
1687 (cp_parser *, bool, bool);
1688 static tree cp_parser_enum_specifier
1690 static void cp_parser_enumerator_list
1691 (cp_parser *, tree);
1692 static void cp_parser_enumerator_definition
1693 (cp_parser *, tree);
1694 static tree cp_parser_namespace_name
1696 static void cp_parser_namespace_definition
1698 static void cp_parser_namespace_body
1700 static tree cp_parser_qualified_namespace_specifier
1702 static void cp_parser_namespace_alias_definition
1704 static bool cp_parser_using_declaration
1705 (cp_parser *, bool);
1706 static void cp_parser_using_directive
1708 static void cp_parser_asm_definition
1710 static void cp_parser_linkage_specification
1712 static void cp_parser_static_assert
1713 (cp_parser *, bool);
1714 static tree cp_parser_decltype
1717 /* Declarators [gram.dcl.decl] */
1719 static tree cp_parser_init_declarator
1720 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1721 static cp_declarator *cp_parser_declarator
1722 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1723 static cp_declarator *cp_parser_direct_declarator
1724 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1725 static enum tree_code cp_parser_ptr_operator
1726 (cp_parser *, tree *, cp_cv_quals *);
1727 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1729 static tree cp_parser_declarator_id
1730 (cp_parser *, bool);
1731 static tree cp_parser_type_id
1733 static void cp_parser_type_specifier_seq
1734 (cp_parser *, bool, cp_decl_specifier_seq *);
1735 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1737 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1738 (cp_parser *, bool *);
1739 static cp_parameter_declarator *cp_parser_parameter_declaration
1740 (cp_parser *, bool, bool *);
1741 static tree cp_parser_default_argument
1742 (cp_parser *, bool);
1743 static void cp_parser_function_body
1745 static tree cp_parser_initializer
1746 (cp_parser *, bool *, bool *);
1747 static tree cp_parser_initializer_clause
1748 (cp_parser *, bool *);
1749 static tree cp_parser_braced_list
1750 (cp_parser*, bool*);
1751 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1752 (cp_parser *, bool *);
1754 static bool cp_parser_ctor_initializer_opt_and_function_body
1757 /* Classes [gram.class] */
1759 static tree cp_parser_class_name
1760 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1761 static tree cp_parser_class_specifier
1763 static tree cp_parser_class_head
1764 (cp_parser *, bool *, tree *, tree *);
1765 static enum tag_types cp_parser_class_key
1767 static void cp_parser_member_specification_opt
1769 static void cp_parser_member_declaration
1771 static tree cp_parser_pure_specifier
1773 static tree cp_parser_constant_initializer
1776 /* Derived classes [gram.class.derived] */
1778 static tree cp_parser_base_clause
1780 static tree cp_parser_base_specifier
1783 /* Special member functions [gram.special] */
1785 static tree cp_parser_conversion_function_id
1787 static tree cp_parser_conversion_type_id
1789 static cp_declarator *cp_parser_conversion_declarator_opt
1791 static bool cp_parser_ctor_initializer_opt
1793 static void cp_parser_mem_initializer_list
1795 static tree cp_parser_mem_initializer
1797 static tree cp_parser_mem_initializer_id
1800 /* Overloading [gram.over] */
1802 static tree cp_parser_operator_function_id
1804 static tree cp_parser_operator
1807 /* Templates [gram.temp] */
1809 static void cp_parser_template_declaration
1810 (cp_parser *, bool);
1811 static tree cp_parser_template_parameter_list
1813 static tree cp_parser_template_parameter
1814 (cp_parser *, bool *, bool *);
1815 static tree cp_parser_type_parameter
1816 (cp_parser *, bool *);
1817 static tree cp_parser_template_id
1818 (cp_parser *, bool, bool, bool);
1819 static tree cp_parser_template_name
1820 (cp_parser *, bool, bool, bool, bool *);
1821 static tree cp_parser_template_argument_list
1823 static tree cp_parser_template_argument
1825 static void cp_parser_explicit_instantiation
1827 static void cp_parser_explicit_specialization
1830 /* Exception handling [gram.exception] */
1832 static tree cp_parser_try_block
1834 static bool cp_parser_function_try_block
1836 static void cp_parser_handler_seq
1838 static void cp_parser_handler
1840 static tree cp_parser_exception_declaration
1842 static tree cp_parser_throw_expression
1844 static tree cp_parser_exception_specification_opt
1846 static tree cp_parser_type_id_list
1849 /* GNU Extensions */
1851 static tree cp_parser_asm_specification_opt
1853 static tree cp_parser_asm_operand_list
1855 static tree cp_parser_asm_clobber_list
1857 static tree cp_parser_attributes_opt
1859 static tree cp_parser_attribute_list
1861 static bool cp_parser_extension_opt
1862 (cp_parser *, int *);
1863 static void cp_parser_label_declaration
1866 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1867 static bool cp_parser_pragma
1868 (cp_parser *, enum pragma_context);
1870 /* Objective-C++ Productions */
1872 static tree cp_parser_objc_message_receiver
1874 static tree cp_parser_objc_message_args
1876 static tree cp_parser_objc_message_expression
1878 static tree cp_parser_objc_encode_expression
1880 static tree cp_parser_objc_defs_expression
1882 static tree cp_parser_objc_protocol_expression
1884 static tree cp_parser_objc_selector_expression
1886 static tree cp_parser_objc_expression
1888 static bool cp_parser_objc_selector_p
1890 static tree cp_parser_objc_selector
1892 static tree cp_parser_objc_protocol_refs_opt
1894 static void cp_parser_objc_declaration
1896 static tree cp_parser_objc_statement
1899 /* Utility Routines */
1901 static tree cp_parser_lookup_name
1902 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1903 static tree cp_parser_lookup_name_simple
1904 (cp_parser *, tree, location_t);
1905 static tree cp_parser_maybe_treat_template_as_class
1907 static bool cp_parser_check_declarator_template_parameters
1908 (cp_parser *, cp_declarator *, location_t);
1909 static bool cp_parser_check_template_parameters
1910 (cp_parser *, unsigned, location_t);
1911 static tree cp_parser_simple_cast_expression
1913 static tree cp_parser_global_scope_opt
1914 (cp_parser *, bool);
1915 static bool cp_parser_constructor_declarator_p
1916 (cp_parser *, bool);
1917 static tree cp_parser_function_definition_from_specifiers_and_declarator
1918 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1919 static tree cp_parser_function_definition_after_declarator
1920 (cp_parser *, bool);
1921 static void cp_parser_template_declaration_after_export
1922 (cp_parser *, bool);
1923 static void cp_parser_perform_template_parameter_access_checks
1924 (VEC (deferred_access_check,gc)*);
1925 static tree cp_parser_single_declaration
1926 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1927 static tree cp_parser_functional_cast
1928 (cp_parser *, tree);
1929 static tree cp_parser_save_member_function_body
1930 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1931 static tree cp_parser_enclosed_template_argument_list
1933 static void cp_parser_save_default_args
1934 (cp_parser *, tree);
1935 static void cp_parser_late_parsing_for_member
1936 (cp_parser *, tree);
1937 static void cp_parser_late_parsing_default_args
1938 (cp_parser *, tree);
1939 static tree cp_parser_sizeof_operand
1940 (cp_parser *, enum rid);
1941 static tree cp_parser_trait_expr
1942 (cp_parser *, enum rid);
1943 static bool cp_parser_declares_only_class_p
1945 static void cp_parser_set_storage_class
1946 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1947 static void cp_parser_set_decl_spec_type
1948 (cp_decl_specifier_seq *, tree, location_t, bool);
1949 static bool cp_parser_friend_p
1950 (const cp_decl_specifier_seq *);
1951 static cp_token *cp_parser_require
1952 (cp_parser *, enum cpp_ttype, const char *);
1953 static cp_token *cp_parser_require_keyword
1954 (cp_parser *, enum rid, const char *);
1955 static bool cp_parser_token_starts_function_definition_p
1957 static bool cp_parser_next_token_starts_class_definition_p
1959 static bool cp_parser_next_token_ends_template_argument_p
1961 static bool cp_parser_nth_token_starts_template_argument_list_p
1962 (cp_parser *, size_t);
1963 static enum tag_types cp_parser_token_is_class_key
1965 static void cp_parser_check_class_key
1966 (enum tag_types, tree type);
1967 static void cp_parser_check_access_in_redeclaration
1968 (tree type, location_t location);
1969 static bool cp_parser_optional_template_keyword
1971 static void cp_parser_pre_parsed_nested_name_specifier
1973 static bool cp_parser_cache_group
1974 (cp_parser *, enum cpp_ttype, unsigned);
1975 static void cp_parser_parse_tentatively
1977 static void cp_parser_commit_to_tentative_parse
1979 static void cp_parser_abort_tentative_parse
1981 static bool cp_parser_parse_definitely
1983 static inline bool cp_parser_parsing_tentatively
1985 static bool cp_parser_uncommitted_to_tentative_parse_p
1987 static void cp_parser_error
1988 (cp_parser *, const char *);
1989 static void cp_parser_name_lookup_error
1990 (cp_parser *, tree, tree, const char *, location_t);
1991 static bool cp_parser_simulate_error
1993 static bool cp_parser_check_type_definition
1995 static void cp_parser_check_for_definition_in_return_type
1996 (cp_declarator *, tree, location_t type_location);
1997 static void cp_parser_check_for_invalid_template_id
1998 (cp_parser *, tree, location_t location);
1999 static bool cp_parser_non_integral_constant_expression
2000 (cp_parser *, const char *);
2001 static void cp_parser_diagnose_invalid_type_name
2002 (cp_parser *, tree, tree, location_t);
2003 static bool cp_parser_parse_and_diagnose_invalid_type_name
2005 static int cp_parser_skip_to_closing_parenthesis
2006 (cp_parser *, bool, bool, bool);
2007 static void cp_parser_skip_to_end_of_statement
2009 static void cp_parser_consume_semicolon_at_end_of_statement
2011 static void cp_parser_skip_to_end_of_block_or_statement
2013 static bool cp_parser_skip_to_closing_brace
2015 static void cp_parser_skip_to_end_of_template_parameter_list
2017 static void cp_parser_skip_to_pragma_eol
2018 (cp_parser*, cp_token *);
2019 static bool cp_parser_error_occurred
2021 static bool cp_parser_allow_gnu_extensions_p
2023 static bool cp_parser_is_string_literal
2025 static bool cp_parser_is_keyword
2026 (cp_token *, enum rid);
2027 static tree cp_parser_make_typename_type
2028 (cp_parser *, tree, tree, location_t location);
2029 static cp_declarator * cp_parser_make_indirect_declarator
2030 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2032 /* Returns nonzero if we are parsing tentatively. */
2035 cp_parser_parsing_tentatively (cp_parser* parser)
2037 return parser->context->next != NULL;
2040 /* Returns nonzero if TOKEN is a string literal. */
2043 cp_parser_is_string_literal (cp_token* token)
2045 return (token->type == CPP_STRING ||
2046 token->type == CPP_STRING16 ||
2047 token->type == CPP_STRING32 ||
2048 token->type == CPP_WSTRING);
2051 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2054 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2056 return token->keyword == keyword;
2059 /* If not parsing tentatively, issue a diagnostic of the form
2060 FILE:LINE: MESSAGE before TOKEN
2061 where TOKEN is the next token in the input stream. MESSAGE
2062 (specified by the caller) is usually of the form "expected
2066 cp_parser_error (cp_parser* parser, const char* message)
2068 if (!cp_parser_simulate_error (parser))
2070 cp_token *token = cp_lexer_peek_token (parser->lexer);
2071 /* This diagnostic makes more sense if it is tagged to the line
2072 of the token we just peeked at. */
2073 cp_lexer_set_source_position_from_token (token);
2075 if (token->type == CPP_PRAGMA)
2077 error ("%H%<#pragma%> is not allowed here", &token->location);
2078 cp_parser_skip_to_pragma_eol (parser, token);
2082 c_parse_error (message,
2083 /* Because c_parser_error does not understand
2084 CPP_KEYWORD, keywords are treated like
2086 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2091 /* Issue an error about name-lookup failing. NAME is the
2092 IDENTIFIER_NODE DECL is the result of
2093 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2094 the thing that we hoped to find. */
2097 cp_parser_name_lookup_error (cp_parser* parser,
2100 const char* desired,
2101 location_t location)
2103 /* If name lookup completely failed, tell the user that NAME was not
2105 if (decl == error_mark_node)
2107 if (parser->scope && parser->scope != global_namespace)
2108 error ("%H%<%E::%E%> has not been declared",
2109 &location, parser->scope, name);
2110 else if (parser->scope == global_namespace)
2111 error ("%H%<::%E%> has not been declared", &location, name);
2112 else if (parser->object_scope
2113 && !CLASS_TYPE_P (parser->object_scope))
2114 error ("%Hrequest for member %qE in non-class type %qT",
2115 &location, name, parser->object_scope);
2116 else if (parser->object_scope)
2117 error ("%H%<%T::%E%> has not been declared",
2118 &location, parser->object_scope, name);
2120 error ("%H%qE has not been declared", &location, name);
2122 else if (parser->scope && parser->scope != global_namespace)
2123 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2124 else if (parser->scope == global_namespace)
2125 error ("%H%<::%E%> %s", &location, name, desired);
2127 error ("%H%qE %s", &location, name, desired);
2130 /* If we are parsing tentatively, remember that an error has occurred
2131 during this tentative parse. Returns true if the error was
2132 simulated; false if a message should be issued by the caller. */
2135 cp_parser_simulate_error (cp_parser* parser)
2137 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2139 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2145 /* Check for repeated decl-specifiers. */
2148 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2149 location_t location)
2153 for (ds = ds_first; ds != ds_last; ++ds)
2155 unsigned count = decl_specs->specs[(int)ds];
2158 /* The "long" specifier is a special case because of "long long". */
2162 error ("%H%<long long long%> is too long for GCC", &location);
2163 else if (pedantic && !in_system_header && warn_long_long
2164 && cxx_dialect == cxx98)
2165 pedwarn (OPT_Wlong_long,
2166 "%HISO C++ 1998 does not support %<long long%>",
2171 static const char *const decl_spec_names[] = {
2187 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2192 /* This function is called when a type is defined. If type
2193 definitions are forbidden at this point, an error message is
2197 cp_parser_check_type_definition (cp_parser* parser)
2199 /* If types are forbidden here, issue a message. */
2200 if (parser->type_definition_forbidden_message)
2202 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2203 in the message need to be interpreted. */
2204 error (parser->type_definition_forbidden_message);
2210 /* This function is called when the DECLARATOR is processed. The TYPE
2211 was a type defined in the decl-specifiers. If it is invalid to
2212 define a type in the decl-specifiers for DECLARATOR, an error is
2213 issued. TYPE_LOCATION is the location of TYPE and is used
2214 for error reporting. */
2217 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2218 tree type, location_t type_location)
2220 /* [dcl.fct] forbids type definitions in return types.
2221 Unfortunately, it's not easy to know whether or not we are
2222 processing a return type until after the fact. */
2224 && (declarator->kind == cdk_pointer
2225 || declarator->kind == cdk_reference
2226 || declarator->kind == cdk_ptrmem))
2227 declarator = declarator->declarator;
2229 && declarator->kind == cdk_function)
2231 error ("%Hnew types may not be defined in a return type", &type_location);
2232 inform ("%H(perhaps a semicolon is missing after the definition of %qT)",
2233 &type_location, type);
2237 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2238 "<" in any valid C++ program. If the next token is indeed "<",
2239 issue a message warning the user about what appears to be an
2240 invalid attempt to form a template-id. LOCATION is the location
2241 of the type-specifier (TYPE) */
2244 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2245 tree type, location_t location)
2247 cp_token_position start = 0;
2249 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2252 error ("%H%qT is not a template", &location, type);
2253 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2254 error ("%H%qE is not a template", &location, type);
2256 error ("%Hinvalid template-id", &location);
2257 /* Remember the location of the invalid "<". */
2258 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2259 start = cp_lexer_token_position (parser->lexer, true);
2260 /* Consume the "<". */
2261 cp_lexer_consume_token (parser->lexer);
2262 /* Parse the template arguments. */
2263 cp_parser_enclosed_template_argument_list (parser);
2264 /* Permanently remove the invalid template arguments so that
2265 this error message is not issued again. */
2267 cp_lexer_purge_tokens_after (parser->lexer, start);
2271 /* If parsing an integral constant-expression, issue an error message
2272 about the fact that THING appeared and return true. Otherwise,
2273 return false. In either case, set
2274 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2277 cp_parser_non_integral_constant_expression (cp_parser *parser,
2280 parser->non_integral_constant_expression_p = true;
2281 if (parser->integral_constant_expression_p)
2283 if (!parser->allow_non_integral_constant_expression_p)
2285 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2286 in the message need to be interpreted. */
2287 char *message = concat (thing,
2288 " cannot appear in a constant-expression",
2298 /* Emit a diagnostic for an invalid type name. SCOPE is the
2299 qualifying scope (or NULL, if none) for ID. This function commits
2300 to the current active tentative parse, if any. (Otherwise, the
2301 problematic construct might be encountered again later, resulting
2302 in duplicate error messages.) LOCATION is the location of ID. */
2305 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2306 tree scope, tree id,
2307 location_t location)
2309 tree decl, old_scope;
2310 /* Try to lookup the identifier. */
2311 old_scope = parser->scope;
2312 parser->scope = scope;
2313 decl = cp_parser_lookup_name_simple (parser, id, location);
2314 parser->scope = old_scope;
2315 /* If the lookup found a template-name, it means that the user forgot
2316 to specify an argument list. Emit a useful error message. */
2317 if (TREE_CODE (decl) == TEMPLATE_DECL)
2318 error ("%Hinvalid use of template-name %qE without an argument list",
2320 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2321 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2322 else if (TREE_CODE (decl) == TYPE_DECL)
2323 /* Something like 'unsigned A a;' */
2324 error ("%Hinvalid combination of multiple type-specifiers",
2326 else if (!parser->scope)
2328 /* Issue an error message. */
2329 error ("%H%qE does not name a type", &location, id);
2330 /* If we're in a template class, it's possible that the user was
2331 referring to a type from a base class. For example:
2333 template <typename T> struct A { typedef T X; };
2334 template <typename T> struct B : public A<T> { X x; };
2336 The user should have said "typename A<T>::X". */
2337 if (processing_template_decl && current_class_type
2338 && TYPE_BINFO (current_class_type))
2342 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2346 tree base_type = BINFO_TYPE (b);
2347 if (CLASS_TYPE_P (base_type)
2348 && dependent_type_p (base_type))
2351 /* Go from a particular instantiation of the
2352 template (which will have an empty TYPE_FIELDs),
2353 to the main version. */
2354 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2355 for (field = TYPE_FIELDS (base_type);
2357 field = TREE_CHAIN (field))
2358 if (TREE_CODE (field) == TYPE_DECL
2359 && DECL_NAME (field) == id)
2361 inform ("%H(perhaps %<typename %T::%E%> was intended)",
2362 &location, BINFO_TYPE (b), id);
2371 /* Here we diagnose qualified-ids where the scope is actually correct,
2372 but the identifier does not resolve to a valid type name. */
2373 else if (parser->scope != error_mark_node)
2375 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2376 error ("%H%qE in namespace %qE does not name a type",
2377 &location, id, parser->scope);
2378 else if (TYPE_P (parser->scope))
2379 error ("%H%qE in class %qT does not name a type",
2380 &location, id, parser->scope);
2384 cp_parser_commit_to_tentative_parse (parser);
2387 /* Check for a common situation where a type-name should be present,
2388 but is not, and issue a sensible error message. Returns true if an
2389 invalid type-name was detected.
2391 The situation handled by this function are variable declarations of the
2392 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2393 Usually, `ID' should name a type, but if we got here it means that it
2394 does not. We try to emit the best possible error message depending on
2395 how exactly the id-expression looks like. */
2398 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2401 cp_token *token = cp_lexer_peek_token (parser->lexer);
2403 cp_parser_parse_tentatively (parser);
2404 id = cp_parser_id_expression (parser,
2405 /*template_keyword_p=*/false,
2406 /*check_dependency_p=*/true,
2407 /*template_p=*/NULL,
2408 /*declarator_p=*/true,
2409 /*optional_p=*/false);
2410 /* After the id-expression, there should be a plain identifier,
2411 otherwise this is not a simple variable declaration. Also, if
2412 the scope is dependent, we cannot do much. */
2413 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2414 || (parser->scope && TYPE_P (parser->scope)
2415 && dependent_type_p (parser->scope))
2416 || TREE_CODE (id) == TYPE_DECL)
2418 cp_parser_abort_tentative_parse (parser);
2421 if (!cp_parser_parse_definitely (parser))
2424 /* Emit a diagnostic for the invalid type. */
2425 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2426 id, token->location);
2427 /* Skip to the end of the declaration; there's no point in
2428 trying to process it. */
2429 cp_parser_skip_to_end_of_block_or_statement (parser);
2433 /* Consume tokens up to, and including, the next non-nested closing `)'.
2434 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2435 are doing error recovery. Returns -1 if OR_COMMA is true and we
2436 found an unnested comma. */
2439 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2444 unsigned paren_depth = 0;
2445 unsigned brace_depth = 0;
2447 if (recovering && !or_comma
2448 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2453 cp_token * token = cp_lexer_peek_token (parser->lexer);
2455 switch (token->type)
2458 case CPP_PRAGMA_EOL:
2459 /* If we've run out of tokens, then there is no closing `)'. */
2463 /* This matches the processing in skip_to_end_of_statement. */
2468 case CPP_OPEN_BRACE:
2471 case CPP_CLOSE_BRACE:
2477 if (recovering && or_comma && !brace_depth && !paren_depth)
2481 case CPP_OPEN_PAREN:
2486 case CPP_CLOSE_PAREN:
2487 if (!brace_depth && !paren_depth--)
2490 cp_lexer_consume_token (parser->lexer);
2499 /* Consume the token. */
2500 cp_lexer_consume_token (parser->lexer);
2504 /* Consume tokens until we reach the end of the current statement.
2505 Normally, that will be just before consuming a `;'. However, if a
2506 non-nested `}' comes first, then we stop before consuming that. */
2509 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2511 unsigned nesting_depth = 0;
2515 cp_token *token = cp_lexer_peek_token (parser->lexer);
2517 switch (token->type)
2520 case CPP_PRAGMA_EOL:
2521 /* If we've run out of tokens, stop. */
2525 /* If the next token is a `;', we have reached the end of the
2531 case CPP_CLOSE_BRACE:
2532 /* If this is a non-nested '}', stop before consuming it.
2533 That way, when confronted with something like:
2537 we stop before consuming the closing '}', even though we
2538 have not yet reached a `;'. */
2539 if (nesting_depth == 0)
2542 /* If it is the closing '}' for a block that we have
2543 scanned, stop -- but only after consuming the token.
2549 we will stop after the body of the erroneously declared
2550 function, but before consuming the following `typedef'
2552 if (--nesting_depth == 0)
2554 cp_lexer_consume_token (parser->lexer);
2558 case CPP_OPEN_BRACE:
2566 /* Consume the token. */
2567 cp_lexer_consume_token (parser->lexer);
2571 /* This function is called at the end of a statement or declaration.
2572 If the next token is a semicolon, it is consumed; otherwise, error
2573 recovery is attempted. */
2576 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2578 /* Look for the trailing `;'. */
2579 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2581 /* If there is additional (erroneous) input, skip to the end of
2583 cp_parser_skip_to_end_of_statement (parser);
2584 /* If the next token is now a `;', consume it. */
2585 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2586 cp_lexer_consume_token (parser->lexer);
2590 /* Skip tokens until we have consumed an entire block, or until we
2591 have consumed a non-nested `;'. */
2594 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2596 int nesting_depth = 0;
2598 while (nesting_depth >= 0)
2600 cp_token *token = cp_lexer_peek_token (parser->lexer);
2602 switch (token->type)
2605 case CPP_PRAGMA_EOL:
2606 /* If we've run out of tokens, stop. */
2610 /* Stop if this is an unnested ';'. */
2615 case CPP_CLOSE_BRACE:
2616 /* Stop if this is an unnested '}', or closes the outermost
2623 case CPP_OPEN_BRACE:
2632 /* Consume the token. */
2633 cp_lexer_consume_token (parser->lexer);
2637 /* Skip tokens until a non-nested closing curly brace is the next
2638 token, or there are no more tokens. Return true in the first case,
2642 cp_parser_skip_to_closing_brace (cp_parser *parser)
2644 unsigned nesting_depth = 0;
2648 cp_token *token = cp_lexer_peek_token (parser->lexer);
2650 switch (token->type)
2653 case CPP_PRAGMA_EOL:
2654 /* If we've run out of tokens, stop. */
2657 case CPP_CLOSE_BRACE:
2658 /* If the next token is a non-nested `}', then we have reached
2659 the end of the current block. */
2660 if (nesting_depth-- == 0)
2664 case CPP_OPEN_BRACE:
2665 /* If it the next token is a `{', then we are entering a new
2666 block. Consume the entire block. */
2674 /* Consume the token. */
2675 cp_lexer_consume_token (parser->lexer);
2679 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2680 parameter is the PRAGMA token, allowing us to purge the entire pragma
2684 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2688 parser->lexer->in_pragma = false;
2691 token = cp_lexer_consume_token (parser->lexer);
2692 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2694 /* Ensure that the pragma is not parsed again. */
2695 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2698 /* Require pragma end of line, resyncing with it as necessary. The
2699 arguments are as for cp_parser_skip_to_pragma_eol. */
2702 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2704 parser->lexer->in_pragma = false;
2705 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2706 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2709 /* This is a simple wrapper around make_typename_type. When the id is
2710 an unresolved identifier node, we can provide a superior diagnostic
2711 using cp_parser_diagnose_invalid_type_name. */
2714 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2715 tree id, location_t id_location)
2718 if (TREE_CODE (id) == IDENTIFIER_NODE)
2720 result = make_typename_type (scope, id, typename_type,
2721 /*complain=*/tf_none);
2722 if (result == error_mark_node)
2723 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2726 return make_typename_type (scope, id, typename_type, tf_error);
2729 /* This is a wrapper around the
2730 make_{pointer,ptrmem,reference}_declarator functions that decides
2731 which one to call based on the CODE and CLASS_TYPE arguments. The
2732 CODE argument should be one of the values returned by
2733 cp_parser_ptr_operator. */
2734 static cp_declarator *
2735 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2736 cp_cv_quals cv_qualifiers,
2737 cp_declarator *target)
2739 if (code == ERROR_MARK)
2740 return cp_error_declarator;
2742 if (code == INDIRECT_REF)
2743 if (class_type == NULL_TREE)
2744 return make_pointer_declarator (cv_qualifiers, target);
2746 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2747 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2748 return make_reference_declarator (cv_qualifiers, target, false);
2749 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2750 return make_reference_declarator (cv_qualifiers, target, true);
2754 /* Create a new C++ parser. */
2757 cp_parser_new (void)
2763 /* cp_lexer_new_main is called before calling ggc_alloc because
2764 cp_lexer_new_main might load a PCH file. */
2765 lexer = cp_lexer_new_main ();
2767 /* Initialize the binops_by_token so that we can get the tree
2768 directly from the token. */
2769 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2770 binops_by_token[binops[i].token_type] = binops[i];
2772 parser = GGC_CNEW (cp_parser);
2773 parser->lexer = lexer;
2774 parser->context = cp_parser_context_new (NULL);
2776 /* For now, we always accept GNU extensions. */
2777 parser->allow_gnu_extensions_p = 1;
2779 /* The `>' token is a greater-than operator, not the end of a
2781 parser->greater_than_is_operator_p = true;
2783 parser->default_arg_ok_p = true;
2785 /* We are not parsing a constant-expression. */
2786 parser->integral_constant_expression_p = false;
2787 parser->allow_non_integral_constant_expression_p = false;
2788 parser->non_integral_constant_expression_p = false;
2790 /* Local variable names are not forbidden. */
2791 parser->local_variables_forbidden_p = false;
2793 /* We are not processing an `extern "C"' declaration. */
2794 parser->in_unbraced_linkage_specification_p = false;
2796 /* We are not processing a declarator. */
2797 parser->in_declarator_p = false;
2799 /* We are not processing a template-argument-list. */
2800 parser->in_template_argument_list_p = false;
2802 /* We are not in an iteration statement. */
2803 parser->in_statement = 0;
2805 /* We are not in a switch statement. */
2806 parser->in_switch_statement_p = false;
2808 /* We are not parsing a type-id inside an expression. */
2809 parser->in_type_id_in_expr_p = false;
2811 /* Declarations aren't implicitly extern "C". */
2812 parser->implicit_extern_c = false;
2814 /* String literals should be translated to the execution character set. */
2815 parser->translate_strings_p = true;
2817 /* We are not parsing a function body. */
2818 parser->in_function_body = false;
2820 /* The unparsed function queue is empty. */
2821 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2823 /* There are no classes being defined. */
2824 parser->num_classes_being_defined = 0;
2826 /* No template parameters apply. */
2827 parser->num_template_parameter_lists = 0;
2832 /* Create a cp_lexer structure which will emit the tokens in CACHE
2833 and push it onto the parser's lexer stack. This is used for delayed
2834 parsing of in-class method bodies and default arguments, and should
2835 not be confused with tentative parsing. */
2837 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2839 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2840 lexer->next = parser->lexer;
2841 parser->lexer = lexer;
2843 /* Move the current source position to that of the first token in the
2845 cp_lexer_set_source_position_from_token (lexer->next_token);
2848 /* Pop the top lexer off the parser stack. This is never used for the
2849 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2851 cp_parser_pop_lexer (cp_parser *parser)
2853 cp_lexer *lexer = parser->lexer;
2854 parser->lexer = lexer->next;
2855 cp_lexer_destroy (lexer);
2857 /* Put the current source position back where it was before this
2858 lexer was pushed. */
2859 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2862 /* Lexical conventions [gram.lex] */
2864 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2868 cp_parser_identifier (cp_parser* parser)
2872 /* Look for the identifier. */
2873 token = cp_parser_require (parser, CPP_NAME, "identifier");
2874 /* Return the value. */
2875 return token ? token->u.value : error_mark_node;
2878 /* Parse a sequence of adjacent string constants. Returns a
2879 TREE_STRING representing the combined, nul-terminated string
2880 constant. If TRANSLATE is true, translate the string to the
2881 execution character set. If WIDE_OK is true, a wide string is
2884 C++98 [lex.string] says that if a narrow string literal token is
2885 adjacent to a wide string literal token, the behavior is undefined.
2886 However, C99 6.4.5p4 says that this results in a wide string literal.
2887 We follow C99 here, for consistency with the C front end.
2889 This code is largely lifted from lex_string() in c-lex.c.
2891 FUTURE: ObjC++ will need to handle @-strings here. */
2893 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2897 struct obstack str_ob;
2898 cpp_string str, istr, *strs;
2900 enum cpp_ttype type;
2902 tok = cp_lexer_peek_token (parser->lexer);
2903 if (!cp_parser_is_string_literal (tok))
2905 cp_parser_error (parser, "expected string-literal");
2906 return error_mark_node;
2911 /* Try to avoid the overhead of creating and destroying an obstack
2912 for the common case of just one string. */
2913 if (!cp_parser_is_string_literal
2914 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2916 cp_lexer_consume_token (parser->lexer);
2918 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2919 str.len = TREE_STRING_LENGTH (tok->u.value);
2926 gcc_obstack_init (&str_ob);
2931 cp_lexer_consume_token (parser->lexer);
2933 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2934 str.len = TREE_STRING_LENGTH (tok->u.value);
2936 if (type != tok->type)
2938 if (type == CPP_STRING)
2940 else if (tok->type != CPP_STRING)
2941 error ("%Hunsupported non-standard concatenation "
2942 "of string literals", &tok->location);
2945 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2947 tok = cp_lexer_peek_token (parser->lexer);
2949 while (cp_parser_is_string_literal (tok));
2951 strs = (cpp_string *) obstack_finish (&str_ob);
2954 if (type != CPP_STRING && !wide_ok)
2956 cp_parser_error (parser, "a wide string is invalid in this context");
2960 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2961 (parse_in, strs, count, &istr, type))
2963 value = build_string (istr.len, (const char *)istr.text);
2964 free (CONST_CAST (unsigned char *, istr.text));
2970 TREE_TYPE (value) = char_array_type_node;
2973 TREE_TYPE (value) = char16_array_type_node;
2976 TREE_TYPE (value) = char32_array_type_node;
2979 TREE_TYPE (value) = wchar_array_type_node;
2983 value = fix_string_type (value);
2986 /* cpp_interpret_string has issued an error. */
2987 value = error_mark_node;
2990 obstack_free (&str_ob, 0);
2996 /* Basic concepts [gram.basic] */
2998 /* Parse a translation-unit.
3001 declaration-seq [opt]
3003 Returns TRUE if all went well. */
3006 cp_parser_translation_unit (cp_parser* parser)
3008 /* The address of the first non-permanent object on the declarator
3010 static void *declarator_obstack_base;
3014 /* Create the declarator obstack, if necessary. */
3015 if (!cp_error_declarator)
3017 gcc_obstack_init (&declarator_obstack);
3018 /* Create the error declarator. */
3019 cp_error_declarator = make_declarator (cdk_error);
3020 /* Create the empty parameter list. */
3021 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3022 /* Remember where the base of the declarator obstack lies. */
3023 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3026 cp_parser_declaration_seq_opt (parser);
3028 /* If there are no tokens left then all went well. */
3029 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3031 /* Get rid of the token array; we don't need it any more. */
3032 cp_lexer_destroy (parser->lexer);
3033 parser->lexer = NULL;
3035 /* This file might have been a context that's implicitly extern
3036 "C". If so, pop the lang context. (Only relevant for PCH.) */
3037 if (parser->implicit_extern_c)
3039 pop_lang_context ();
3040 parser->implicit_extern_c = false;
3044 finish_translation_unit ();
3050 cp_parser_error (parser, "expected declaration");
3054 /* Make sure the declarator obstack was fully cleaned up. */
3055 gcc_assert (obstack_next_free (&declarator_obstack)
3056 == declarator_obstack_base);
3058 /* All went well. */
3062 /* Expressions [gram.expr] */
3064 /* Parse a primary-expression.
3075 ( compound-statement )
3076 __builtin_va_arg ( assignment-expression , type-id )
3077 __builtin_offsetof ( type-id , offsetof-expression )
3080 __has_nothrow_assign ( type-id )
3081 __has_nothrow_constructor ( type-id )
3082 __has_nothrow_copy ( type-id )
3083 __has_trivial_assign ( type-id )
3084 __has_trivial_constructor ( type-id )
3085 __has_trivial_copy ( type-id )
3086 __has_trivial_destructor ( type-id )
3087 __has_virtual_destructor ( type-id )
3088 __is_abstract ( type-id )
3089 __is_base_of ( type-id , type-id )
3090 __is_class ( type-id )
3091 __is_convertible_to ( type-id , type-id )
3092 __is_empty ( type-id )
3093 __is_enum ( type-id )
3094 __is_pod ( type-id )
3095 __is_polymorphic ( type-id )
3096 __is_union ( type-id )
3098 Objective-C++ Extension:
3106 ADDRESS_P is true iff this expression was immediately preceded by
3107 "&" and therefore might denote a pointer-to-member. CAST_P is true
3108 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3109 true iff this expression is a template argument.
3111 Returns a representation of the expression. Upon return, *IDK
3112 indicates what kind of id-expression (if any) was present. */
3115 cp_parser_primary_expression (cp_parser *parser,
3118 bool template_arg_p,
3121 cp_token *token = NULL;
3123 /* Assume the primary expression is not an id-expression. */
3124 *idk = CP_ID_KIND_NONE;
3126 /* Peek at the next token. */
3127 token = cp_lexer_peek_token (parser->lexer);
3128 switch (token->type)
3141 token = cp_lexer_consume_token (parser->lexer);
3142 /* Floating-point literals are only allowed in an integral
3143 constant expression if they are cast to an integral or
3144 enumeration type. */
3145 if (TREE_CODE (token->u.value) == REAL_CST
3146 && parser->integral_constant_expression_p
3149 /* CAST_P will be set even in invalid code like "int(2.7 +
3150 ...)". Therefore, we have to check that the next token
3151 is sure to end the cast. */
3154 cp_token *next_token;
3156 next_token = cp_lexer_peek_token (parser->lexer);
3157 if (/* The comma at the end of an
3158 enumerator-definition. */
3159 next_token->type != CPP_COMMA
3160 /* The curly brace at the end of an enum-specifier. */
3161 && next_token->type != CPP_CLOSE_BRACE
3162 /* The end of a statement. */
3163 && next_token->type != CPP_SEMICOLON
3164 /* The end of the cast-expression. */
3165 && next_token->type != CPP_CLOSE_PAREN
3166 /* The end of an array bound. */
3167 && next_token->type != CPP_CLOSE_SQUARE
3168 /* The closing ">" in a template-argument-list. */
3169 && (next_token->type != CPP_GREATER
3170 || parser->greater_than_is_operator_p)
3171 /* C++0x only: A ">>" treated like two ">" tokens,
3172 in a template-argument-list. */
3173 && (next_token->type != CPP_RSHIFT
3174 || (cxx_dialect == cxx98)
3175 || parser->greater_than_is_operator_p))
3179 /* If we are within a cast, then the constraint that the
3180 cast is to an integral or enumeration type will be
3181 checked at that point. If we are not within a cast, then
3182 this code is invalid. */
3184 cp_parser_non_integral_constant_expression
3185 (parser, "floating-point literal");
3187 return token->u.value;
3193 /* ??? Should wide strings be allowed when parser->translate_strings_p
3194 is false (i.e. in attributes)? If not, we can kill the third
3195 argument to cp_parser_string_literal. */
3196 return cp_parser_string_literal (parser,
3197 parser->translate_strings_p,
3200 case CPP_OPEN_PAREN:
3203 bool saved_greater_than_is_operator_p;
3205 /* Consume the `('. */
3206 cp_lexer_consume_token (parser->lexer);
3207 /* Within a parenthesized expression, a `>' token is always
3208 the greater-than operator. */
3209 saved_greater_than_is_operator_p
3210 = parser->greater_than_is_operator_p;
3211 parser->greater_than_is_operator_p = true;
3212 /* If we see `( { ' then we are looking at the beginning of
3213 a GNU statement-expression. */
3214 if (cp_parser_allow_gnu_extensions_p (parser)
3215 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3217 /* Statement-expressions are not allowed by the standard. */
3218 pedwarn (OPT_pedantic,
3219 "%HISO C++ forbids braced-groups within expressions",
3222 /* And they're not allowed outside of a function-body; you
3223 cannot, for example, write:
3225 int i = ({ int j = 3; j + 1; });
3227 at class or namespace scope. */
3228 if (!parser->in_function_body
3229 || parser->in_template_argument_list_p)
3231 error ("%Hstatement-expressions are not allowed outside "
3232 "functions nor in template-argument lists",
3234 cp_parser_skip_to_end_of_block_or_statement (parser);
3235 expr = error_mark_node;
3239 /* Start the statement-expression. */
3240 expr = begin_stmt_expr ();
3241 /* Parse the compound-statement. */
3242 cp_parser_compound_statement (parser, expr, false);
3244 expr = finish_stmt_expr (expr, false);
3249 /* Parse the parenthesized expression. */
3250 expr = cp_parser_expression (parser, cast_p);
3251 /* Let the front end know that this expression was
3252 enclosed in parentheses. This matters in case, for
3253 example, the expression is of the form `A::B', since
3254 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3256 finish_parenthesized_expr (expr);
3258 /* The `>' token might be the end of a template-id or
3259 template-parameter-list now. */
3260 parser->greater_than_is_operator_p
3261 = saved_greater_than_is_operator_p;
3262 /* Consume the `)'. */
3263 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3264 cp_parser_skip_to_end_of_statement (parser);
3270 switch (token->keyword)
3272 /* These two are the boolean literals. */
3274 cp_lexer_consume_token (parser->lexer);
3275 return boolean_true_node;
3277 cp_lexer_consume_token (parser->lexer);
3278 return boolean_false_node;
3280 /* The `__null' literal. */
3282 cp_lexer_consume_token (parser->lexer);
3285 /* Recognize the `this' keyword. */
3287 cp_lexer_consume_token (parser->lexer);
3288 if (parser->local_variables_forbidden_p)
3290 error ("%H%<this%> may not be used in this context",
3292 return error_mark_node;
3294 /* Pointers cannot appear in constant-expressions. */
3295 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3296 return error_mark_node;
3297 return finish_this_expr ();
3299 /* The `operator' keyword can be the beginning of an
3304 case RID_FUNCTION_NAME:
3305 case RID_PRETTY_FUNCTION_NAME:
3306 case RID_C99_FUNCTION_NAME:
3307 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3308 __func__ are the names of variables -- but they are
3309 treated specially. Therefore, they are handled here,
3310 rather than relying on the generic id-expression logic
3311 below. Grammatically, these names are id-expressions.
3313 Consume the token. */
3314 token = cp_lexer_consume_token (parser->lexer);
3315 /* Look up the name. */
3316 return finish_fname (token->u.value);
3323 /* The `__builtin_va_arg' construct is used to handle
3324 `va_arg'. Consume the `__builtin_va_arg' token. */
3325 cp_lexer_consume_token (parser->lexer);
3326 /* Look for the opening `('. */
3327 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3328 /* Now, parse the assignment-expression. */
3329 expression = cp_parser_assignment_expression (parser,
3331 /* Look for the `,'. */
3332 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3333 /* Parse the type-id. */
3334 type = cp_parser_type_id (parser);
3335 /* Look for the closing `)'. */
3336 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3337 /* Using `va_arg' in a constant-expression is not
3339 if (cp_parser_non_integral_constant_expression (parser,
3341 return error_mark_node;
3342 return build_x_va_arg (expression, type);
3346 return cp_parser_builtin_offsetof (parser);
3348 case RID_HAS_NOTHROW_ASSIGN:
3349 case RID_HAS_NOTHROW_CONSTRUCTOR:
3350 case RID_HAS_NOTHROW_COPY:
3351 case RID_HAS_TRIVIAL_ASSIGN:
3352 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3353 case RID_HAS_TRIVIAL_COPY:
3354 case RID_HAS_TRIVIAL_DESTRUCTOR:
3355 case RID_HAS_VIRTUAL_DESTRUCTOR:
3356 case RID_IS_ABSTRACT:
3357 case RID_IS_BASE_OF:
3359 case RID_IS_CONVERTIBLE_TO:
3363 case RID_IS_POLYMORPHIC:
3365 return cp_parser_trait_expr (parser, token->keyword);
3367 /* Objective-C++ expressions. */
3369 case RID_AT_PROTOCOL:
3370 case RID_AT_SELECTOR:
3371 return cp_parser_objc_expression (parser);
3374 cp_parser_error (parser, "expected primary-expression");
3375 return error_mark_node;
3378 /* An id-expression can start with either an identifier, a
3379 `::' as the beginning of a qualified-id, or the "operator"
3383 case CPP_TEMPLATE_ID:
3384 case CPP_NESTED_NAME_SPECIFIER:
3388 const char *error_msg;
3391 cp_token *id_expr_token;
3394 /* Parse the id-expression. */
3396 = cp_parser_id_expression (parser,
3397 /*template_keyword_p=*/false,
3398 /*check_dependency_p=*/true,
3400 /*declarator_p=*/false,
3401 /*optional_p=*/false);
3402 if (id_expression == error_mark_node)
3403 return error_mark_node;
3404 id_expr_token = token;
3405 token = cp_lexer_peek_token (parser->lexer);
3406 done = (token->type != CPP_OPEN_SQUARE
3407 && token->type != CPP_OPEN_PAREN
3408 && token->type != CPP_DOT
3409 && token->type != CPP_DEREF
3410 && token->type != CPP_PLUS_PLUS
3411 && token->type != CPP_MINUS_MINUS);
3412 /* If we have a template-id, then no further lookup is
3413 required. If the template-id was for a template-class, we
3414 will sometimes have a TYPE_DECL at this point. */
3415 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3416 || TREE_CODE (id_expression) == TYPE_DECL)
3417 decl = id_expression;
3418 /* Look up the name. */
3421 tree ambiguous_decls;
3423 decl = cp_parser_lookup_name (parser, id_expression,
3426 /*is_namespace=*/false,
3427 /*check_dependency=*/true,
3429 id_expr_token->location);
3430 /* If the lookup was ambiguous, an error will already have
3432 if (ambiguous_decls)
3433 return error_mark_node;
3435 /* In Objective-C++, an instance variable (ivar) may be preferred
3436 to whatever cp_parser_lookup_name() found. */
3437 decl = objc_lookup_ivar (decl, id_expression);
3439 /* If name lookup gives us a SCOPE_REF, then the
3440 qualifying scope was dependent. */
3441 if (TREE_CODE (decl) == SCOPE_REF)
3443 /* At this point, we do not know if DECL is a valid
3444 integral constant expression. We assume that it is
3445 in fact such an expression, so that code like:
3447 template <int N> struct A {
3451 is accepted. At template-instantiation time, we
3452 will check that B<N>::i is actually a constant. */
3455 /* Check to see if DECL is a local variable in a context
3456 where that is forbidden. */
3457 if (parser->local_variables_forbidden_p
3458 && local_variable_p (decl))
3460 /* It might be that we only found DECL because we are
3461 trying to be generous with pre-ISO scoping rules.
3462 For example, consider:
3466 for (int i = 0; i < 10; ++i) {}
3467 extern void f(int j = i);
3470 Here, name look up will originally find the out
3471 of scope `i'. We need to issue a warning message,
3472 but then use the global `i'. */
3473 decl = check_for_out_of_scope_variable (decl);
3474 if (local_variable_p (decl))
3476 error ("%Hlocal variable %qD may not appear in this context",
3477 &id_expr_token->location, decl);
3478 return error_mark_node;
3483 decl = (finish_id_expression
3484 (id_expression, decl, parser->scope,
3486 parser->integral_constant_expression_p,
3487 parser->allow_non_integral_constant_expression_p,
3488 &parser->non_integral_constant_expression_p,
3489 template_p, done, address_p,
3492 id_expr_token->location));
3494 cp_parser_error (parser, error_msg);
3498 /* Anything else is an error. */
3500 /* ...unless we have an Objective-C++ message or string literal,
3502 if (c_dialect_objc ()
3503 && (token->type == CPP_OPEN_SQUARE
3504 || token->type == CPP_OBJC_STRING))
3505 return cp_parser_objc_expression (parser);
3507 cp_parser_error (parser, "expected primary-expression");
3508 return error_mark_node;
3512 /* Parse an id-expression.
3519 :: [opt] nested-name-specifier template [opt] unqualified-id
3521 :: operator-function-id
3524 Return a representation of the unqualified portion of the
3525 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3526 a `::' or nested-name-specifier.
3528 Often, if the id-expression was a qualified-id, the caller will
3529 want to make a SCOPE_REF to represent the qualified-id. This
3530 function does not do this in order to avoid wastefully creating
3531 SCOPE_REFs when they are not required.
3533 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3536 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3537 uninstantiated templates.
3539 If *TEMPLATE_P is non-NULL, it is set to true iff the
3540 `template' keyword is used to explicitly indicate that the entity
3541 named is a template.
3543 If DECLARATOR_P is true, the id-expression is appearing as part of
3544 a declarator, rather than as part of an expression. */
3547 cp_parser_id_expression (cp_parser *parser,
3548 bool template_keyword_p,
3549 bool check_dependency_p,
3554 bool global_scope_p;
3555 bool nested_name_specifier_p;
3557 /* Assume the `template' keyword was not used. */
3559 *template_p = template_keyword_p;
3561 /* Look for the optional `::' operator. */
3563 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3565 /* Look for the optional nested-name-specifier. */
3566 nested_name_specifier_p
3567 = (cp_parser_nested_name_specifier_opt (parser,
3568 /*typename_keyword_p=*/false,
3573 /* If there is a nested-name-specifier, then we are looking at
3574 the first qualified-id production. */
3575 if (nested_name_specifier_p)
3578 tree saved_object_scope;
3579 tree saved_qualifying_scope;
3580 tree unqualified_id;
3583 /* See if the next token is the `template' keyword. */
3585 template_p = &is_template;
3586 *template_p = cp_parser_optional_template_keyword (parser);
3587 /* Name lookup we do during the processing of the
3588 unqualified-id might obliterate SCOPE. */
3589 saved_scope = parser->scope;
3590 saved_object_scope = parser->object_scope;
3591 saved_qualifying_scope = parser->qualifying_scope;
3592 /* Process the final unqualified-id. */
3593 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3596 /*optional_p=*/false);
3597 /* Restore the SAVED_SCOPE for our caller. */
3598 parser->scope = saved_scope;
3599 parser->object_scope = saved_object_scope;
3600 parser->qualifying_scope = saved_qualifying_scope;
3602 return unqualified_id;
3604 /* Otherwise, if we are in global scope, then we are looking at one
3605 of the other qualified-id productions. */
3606 else if (global_scope_p)
3611 /* Peek at the next token. */
3612 token = cp_lexer_peek_token (parser->lexer);
3614 /* If it's an identifier, and the next token is not a "<", then
3615 we can avoid the template-id case. This is an optimization
3616 for this common case. */
3617 if (token->type == CPP_NAME
3618 && !cp_parser_nth_token_starts_template_argument_list_p
3620 return cp_parser_identifier (parser);
3622 cp_parser_parse_tentatively (parser);
3623 /* Try a template-id. */
3624 id = cp_parser_template_id (parser,
3625 /*template_keyword_p=*/false,
3626 /*check_dependency_p=*/true,
3628 /* If that worked, we're done. */
3629 if (cp_parser_parse_definitely (parser))
3632 /* Peek at the next token. (Changes in the token buffer may
3633 have invalidated the pointer obtained above.) */
3634 token = cp_lexer_peek_token (parser->lexer);
3636 switch (token->type)
3639 return cp_parser_identifier (parser);
3642 if (token->keyword == RID_OPERATOR)
3643 return cp_parser_operator_function_id (parser);
3647 cp_parser_error (parser, "expected id-expression");
3648 return error_mark_node;
3652 return cp_parser_unqualified_id (parser, template_keyword_p,
3653 /*check_dependency_p=*/true,
3658 /* Parse an unqualified-id.
3662 operator-function-id
3663 conversion-function-id
3667 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3668 keyword, in a construct like `A::template ...'.
3670 Returns a representation of unqualified-id. For the `identifier'
3671 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3672 production a BIT_NOT_EXPR is returned; the operand of the
3673 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3674 other productions, see the documentation accompanying the
3675 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3676 names are looked up in uninstantiated templates. If DECLARATOR_P
3677 is true, the unqualified-id is appearing as part of a declarator,
3678 rather than as part of an expression. */
3681 cp_parser_unqualified_id (cp_parser* parser,
3682 bool template_keyword_p,
3683 bool check_dependency_p,
3689 /* Peek at the next token. */
3690 token = cp_lexer_peek_token (parser->lexer);
3692 switch (token->type)
3698 /* We don't know yet whether or not this will be a
3700 cp_parser_parse_tentatively (parser);
3701 /* Try a template-id. */
3702 id = cp_parser_template_id (parser, template_keyword_p,
3705 /* If it worked, we're done. */
3706 if (cp_parser_parse_definitely (parser))
3708 /* Otherwise, it's an ordinary identifier. */
3709 return cp_parser_identifier (parser);
3712 case CPP_TEMPLATE_ID:
3713 return cp_parser_template_id (parser, template_keyword_p,
3720 tree qualifying_scope;
3725 /* Consume the `~' token. */
3726 cp_lexer_consume_token (parser->lexer);
3727 /* Parse the class-name. The standard, as written, seems to
3730 template <typename T> struct S { ~S (); };
3731 template <typename T> S<T>::~S() {}
3733 is invalid, since `~' must be followed by a class-name, but
3734 `S<T>' is dependent, and so not known to be a class.
3735 That's not right; we need to look in uninstantiated
3736 templates. A further complication arises from:
3738 template <typename T> void f(T t) {
3742 Here, it is not possible to look up `T' in the scope of `T'
3743 itself. We must look in both the current scope, and the
3744 scope of the containing complete expression.
3746 Yet another issue is:
3755 The standard does not seem to say that the `S' in `~S'
3756 should refer to the type `S' and not the data member
3759 /* DR 244 says that we look up the name after the "~" in the
3760 same scope as we looked up the qualifying name. That idea
3761 isn't fully worked out; it's more complicated than that. */
3762 scope = parser->scope;
3763 object_scope = parser->object_scope;
3764 qualifying_scope = parser->qualifying_scope;
3766 /* Check for invalid scopes. */
3767 if (scope == error_mark_node)
3769 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3770 cp_lexer_consume_token (parser->lexer);
3771 return error_mark_node;
3773 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3775 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3776 error ("%Hscope %qT before %<~%> is not a class-name",
3777 &token->location, scope);
3778 cp_parser_simulate_error (parser);
3779 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3780 cp_lexer_consume_token (parser->lexer);
3781 return error_mark_node;
3783 gcc_assert (!scope || TYPE_P (scope));
3785 /* If the name is of the form "X::~X" it's OK. */
3786 token = cp_lexer_peek_token (parser->lexer);
3788 && token->type == CPP_NAME
3789 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3791 && constructor_name_p (token->u.value, scope))
3793 cp_lexer_consume_token (parser->lexer);
3794 return build_nt (BIT_NOT_EXPR, scope);
3797 /* If there was an explicit qualification (S::~T), first look
3798 in the scope given by the qualification (i.e., S). */
3800 type_decl = NULL_TREE;
3803 cp_parser_parse_tentatively (parser);
3804 type_decl = cp_parser_class_name (parser,
3805 /*typename_keyword_p=*/false,
3806 /*template_keyword_p=*/false,
3808 /*check_dependency=*/false,
3809 /*class_head_p=*/false,
3811 if (cp_parser_parse_definitely (parser))
3814 /* In "N::S::~S", look in "N" as well. */
3815 if (!done && scope && qualifying_scope)
3817 cp_parser_parse_tentatively (parser);
3818 parser->scope = qualifying_scope;
3819 parser->object_scope = NULL_TREE;
3820 parser->qualifying_scope = NULL_TREE;
3822 = cp_parser_class_name (parser,
3823 /*typename_keyword_p=*/false,
3824 /*template_keyword_p=*/false,
3826 /*check_dependency=*/false,
3827 /*class_head_p=*/false,
3829 if (cp_parser_parse_definitely (parser))
3832 /* In "p->S::~T", look in the scope given by "*p" as well. */
3833 else if (!done && object_scope)
3835 cp_parser_parse_tentatively (parser);
3836 parser->scope = object_scope;
3837 parser->object_scope = NULL_TREE;
3838 parser->qualifying_scope = NULL_TREE;
3840 = cp_parser_class_name (parser,
3841 /*typename_keyword_p=*/false,
3842 /*template_keyword_p=*/false,
3844 /*check_dependency=*/false,
3845 /*class_head_p=*/false,
3847 if (cp_parser_parse_definitely (parser))
3850 /* Look in the surrounding context. */
3853 parser->scope = NULL_TREE;
3854 parser->object_scope = NULL_TREE;
3855 parser->qualifying_scope = NULL_TREE;
3857 = cp_parser_class_name (parser,
3858 /*typename_keyword_p=*/false,
3859 /*template_keyword_p=*/false,
3861 /*check_dependency=*/false,
3862 /*class_head_p=*/false,
3865 /* If an error occurred, assume that the name of the
3866 destructor is the same as the name of the qualifying
3867 class. That allows us to keep parsing after running
3868 into ill-formed destructor names. */
3869 if (type_decl == error_mark_node && scope)
3870 return build_nt (BIT_NOT_EXPR, scope);
3871 else if (type_decl == error_mark_node)
3872 return error_mark_node;
3874 /* Check that destructor name and scope match. */
3875 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3877 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3878 error ("%Hdeclaration of %<~%T%> as member of %qT",
3879 &token->location, type_decl, scope);
3880 cp_parser_simulate_error (parser);
3881 return error_mark_node;
3886 A typedef-name that names a class shall not be used as the
3887 identifier in the declarator for a destructor declaration. */
3889 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3890 && !DECL_SELF_REFERENCE_P (type_decl)
3891 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3892 error ("%Htypedef-name %qD used as destructor declarator",
3893 &token->location, type_decl);
3895 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3899 if (token->keyword == RID_OPERATOR)
3903 /* This could be a template-id, so we try that first. */
3904 cp_parser_parse_tentatively (parser);
3905 /* Try a template-id. */
3906 id = cp_parser_template_id (parser, template_keyword_p,
3907 /*check_dependency_p=*/true,
3909 /* If that worked, we're done. */
3910 if (cp_parser_parse_definitely (parser))
3912 /* We still don't know whether we're looking at an
3913 operator-function-id or a conversion-function-id. */
3914 cp_parser_parse_tentatively (parser);
3915 /* Try an operator-function-id. */
3916 id = cp_parser_operator_function_id (parser);
3917 /* If that didn't work, try a conversion-function-id. */
3918 if (!cp_parser_parse_definitely (parser))
3919 id = cp_parser_conversion_function_id (parser);
3928 cp_parser_error (parser, "expected unqualified-id");
3929 return error_mark_node;
3933 /* Parse an (optional) nested-name-specifier.
3935 nested-name-specifier:
3936 class-or-namespace-name :: nested-name-specifier [opt]
3937 class-or-namespace-name :: template nested-name-specifier [opt]
3939 PARSER->SCOPE should be set appropriately before this function is
3940 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3941 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3944 Sets PARSER->SCOPE to the class (TYPE) or namespace
3945 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3946 it unchanged if there is no nested-name-specifier. Returns the new
3947 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3949 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3950 part of a declaration and/or decl-specifier. */
3953 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3954 bool typename_keyword_p,
3955 bool check_dependency_p,
3957 bool is_declaration)
3959 bool success = false;
3960 cp_token_position start = 0;
3963 /* Remember where the nested-name-specifier starts. */
3964 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3966 start = cp_lexer_token_position (parser->lexer, false);
3967 push_deferring_access_checks (dk_deferred);
3974 tree saved_qualifying_scope;
3975 bool template_keyword_p;
3977 /* Spot cases that cannot be the beginning of a
3978 nested-name-specifier. */
3979 token = cp_lexer_peek_token (parser->lexer);
3981 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3982 the already parsed nested-name-specifier. */
3983 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3985 /* Grab the nested-name-specifier and continue the loop. */
3986 cp_parser_pre_parsed_nested_name_specifier (parser);
3987 /* If we originally encountered this nested-name-specifier
3988 with IS_DECLARATION set to false, we will not have
3989 resolved TYPENAME_TYPEs, so we must do so here. */
3991 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3993 new_scope = resolve_typename_type (parser->scope,
3994 /*only_current_p=*/false);
3995 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3996 parser->scope = new_scope;
4002 /* Spot cases that cannot be the beginning of a
4003 nested-name-specifier. On the second and subsequent times
4004 through the loop, we look for the `template' keyword. */
4005 if (success && token->keyword == RID_TEMPLATE)
4007 /* A template-id can start a nested-name-specifier. */
4008 else if (token->type == CPP_TEMPLATE_ID)
4012 /* If the next token is not an identifier, then it is
4013 definitely not a class-or-namespace-name. */
4014 if (token->type != CPP_NAME)
4016 /* If the following token is neither a `<' (to begin a
4017 template-id), nor a `::', then we are not looking at a
4018 nested-name-specifier. */
4019 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4020 if (token->type != CPP_SCOPE
4021 && !cp_parser_nth_token_starts_template_argument_list_p
4026 /* The nested-name-specifier is optional, so we parse
4028 cp_parser_parse_tentatively (parser);
4030 /* Look for the optional `template' keyword, if this isn't the
4031 first time through the loop. */
4033 template_keyword_p = cp_parser_optional_template_keyword (parser);
4035 template_keyword_p = false;
4037 /* Save the old scope since the name lookup we are about to do
4038 might destroy it. */
4039 old_scope = parser->scope;
4040 saved_qualifying_scope = parser->qualifying_scope;
4041 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4042 look up names in "X<T>::I" in order to determine that "Y" is
4043 a template. So, if we have a typename at this point, we make
4044 an effort to look through it. */
4046 && !typename_keyword_p
4048 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4049 parser->scope = resolve_typename_type (parser->scope,
4050 /*only_current_p=*/false);
4051 /* Parse the qualifying entity. */
4053 = cp_parser_class_or_namespace_name (parser,
4059 /* Look for the `::' token. */
4060 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4062 /* If we found what we wanted, we keep going; otherwise, we're
4064 if (!cp_parser_parse_definitely (parser))
4066 bool error_p = false;
4068 /* Restore the OLD_SCOPE since it was valid before the
4069 failed attempt at finding the last
4070 class-or-namespace-name. */
4071 parser->scope = old_scope;
4072 parser->qualifying_scope = saved_qualifying_scope;
4073 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4075 /* If the next token is an identifier, and the one after
4076 that is a `::', then any valid interpretation would have
4077 found a class-or-namespace-name. */
4078 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4079 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4081 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4084 token = cp_lexer_consume_token (parser->lexer);
4087 if (!token->ambiguous_p)
4090 tree ambiguous_decls;
4092 decl = cp_parser_lookup_name (parser, token->u.value,
4094 /*is_template=*/false,
4095 /*is_namespace=*/false,
4096 /*check_dependency=*/true,
4099 if (TREE_CODE (decl) == TEMPLATE_DECL)
4100 error ("%H%qD used without template parameters",
4101 &token->location, decl);
4102 else if (ambiguous_decls)
4104 error ("%Hreference to %qD is ambiguous",
4105 &token->location, token->u.value);
4106 print_candidates (ambiguous_decls);
4107 decl = error_mark_node;
4110 cp_parser_name_lookup_error
4111 (parser, token->u.value, decl,
4112 "is not a class or namespace",
4115 parser->scope = error_mark_node;
4117 /* Treat this as a successful nested-name-specifier
4122 If the name found is not a class-name (clause
4123 _class_) or namespace-name (_namespace.def_), the
4124 program is ill-formed. */
4127 cp_lexer_consume_token (parser->lexer);
4131 /* We've found one valid nested-name-specifier. */
4133 /* Name lookup always gives us a DECL. */
4134 if (TREE_CODE (new_scope) == TYPE_DECL)
4135 new_scope = TREE_TYPE (new_scope);
4136 /* Uses of "template" must be followed by actual templates. */
4137 if (template_keyword_p
4138 && !(CLASS_TYPE_P (new_scope)
4139 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4140 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4141 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4142 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4143 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4144 == TEMPLATE_ID_EXPR)))
4145 permerror (TYPE_P (new_scope)
4146 ? "%qT is not a template"
4147 : "%qD is not a template",
4149 /* If it is a class scope, try to complete it; we are about to
4150 be looking up names inside the class. */
4151 if (TYPE_P (new_scope)
4152 /* Since checking types for dependency can be expensive,
4153 avoid doing it if the type is already complete. */
4154 && !COMPLETE_TYPE_P (new_scope)
4155 /* Do not try to complete dependent types. */
4156 && !dependent_type_p (new_scope))
4158 new_scope = complete_type (new_scope);
4159 /* If it is a typedef to current class, use the current
4160 class instead, as the typedef won't have any names inside
4162 if (!COMPLETE_TYPE_P (new_scope)
4163 && currently_open_class (new_scope))
4164 new_scope = TYPE_MAIN_VARIANT (new_scope);
4166 /* Make sure we look in the right scope the next time through
4168 parser->scope = new_scope;
4171 /* If parsing tentatively, replace the sequence of tokens that makes
4172 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4173 token. That way, should we re-parse the token stream, we will
4174 not have to repeat the effort required to do the parse, nor will
4175 we issue duplicate error messages. */
4176 if (success && start)
4180 token = cp_lexer_token_at (parser->lexer, start);
4181 /* Reset the contents of the START token. */
4182 token->type = CPP_NESTED_NAME_SPECIFIER;
4183 /* Retrieve any deferred checks. Do not pop this access checks yet
4184 so the memory will not be reclaimed during token replacing below. */
4185 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4186 token->u.tree_check_value->value = parser->scope;
4187 token->u.tree_check_value->checks = get_deferred_access_checks ();
4188 token->u.tree_check_value->qualifying_scope =
4189 parser->qualifying_scope;
4190 token->keyword = RID_MAX;
4192 /* Purge all subsequent tokens. */
4193 cp_lexer_purge_tokens_after (parser->lexer, start);
4197 pop_to_parent_deferring_access_checks ();
4199 return success ? parser->scope : NULL_TREE;
4202 /* Parse a nested-name-specifier. See
4203 cp_parser_nested_name_specifier_opt for details. This function
4204 behaves identically, except that it will an issue an error if no
4205 nested-name-specifier is present. */
4208 cp_parser_nested_name_specifier (cp_parser *parser,
4209 bool typename_keyword_p,
4210 bool check_dependency_p,
4212 bool is_declaration)
4216 /* Look for the nested-name-specifier. */
4217 scope = cp_parser_nested_name_specifier_opt (parser,
4222 /* If it was not present, issue an error message. */
4225 cp_parser_error (parser, "expected nested-name-specifier");
4226 parser->scope = NULL_TREE;
4232 /* Parse a class-or-namespace-name.
4234 class-or-namespace-name:
4238 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4239 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4240 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4241 TYPE_P is TRUE iff the next name should be taken as a class-name,
4242 even the same name is declared to be another entity in the same
4245 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4246 specified by the class-or-namespace-name. If neither is found the
4247 ERROR_MARK_NODE is returned. */
4250 cp_parser_class_or_namespace_name (cp_parser *parser,
4251 bool typename_keyword_p,
4252 bool template_keyword_p,
4253 bool check_dependency_p,
4255 bool is_declaration)
4258 tree saved_qualifying_scope;
4259 tree saved_object_scope;
4263 /* Before we try to parse the class-name, we must save away the
4264 current PARSER->SCOPE since cp_parser_class_name will destroy
4266 saved_scope = parser->scope;
4267 saved_qualifying_scope = parser->qualifying_scope;
4268 saved_object_scope = parser->object_scope;
4269 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4270 there is no need to look for a namespace-name. */
4271 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4273 cp_parser_parse_tentatively (parser);
4274 scope = cp_parser_class_name (parser,
4277 type_p ? class_type : none_type,
4279 /*class_head_p=*/false,
4281 /* If that didn't work, try for a namespace-name. */
4282 if (!only_class_p && !cp_parser_parse_definitely (parser))
4284 /* Restore the saved scope. */
4285 parser->scope = saved_scope;
4286 parser->qualifying_scope = saved_qualifying_scope;
4287 parser->object_scope = saved_object_scope;
4288 /* If we are not looking at an identifier followed by the scope
4289 resolution operator, then this is not part of a
4290 nested-name-specifier. (Note that this function is only used
4291 to parse the components of a nested-name-specifier.) */
4292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4293 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4294 return error_mark_node;
4295 scope = cp_parser_namespace_name (parser);
4301 /* Parse a postfix-expression.
4305 postfix-expression [ expression ]
4306 postfix-expression ( expression-list [opt] )
4307 simple-type-specifier ( expression-list [opt] )
4308 typename :: [opt] nested-name-specifier identifier
4309 ( expression-list [opt] )
4310 typename :: [opt] nested-name-specifier template [opt] template-id
4311 ( expression-list [opt] )
4312 postfix-expression . template [opt] id-expression
4313 postfix-expression -> template [opt] id-expression
4314 postfix-expression . pseudo-destructor-name
4315 postfix-expression -> pseudo-destructor-name
4316 postfix-expression ++
4317 postfix-expression --
4318 dynamic_cast < type-id > ( expression )
4319 static_cast < type-id > ( expression )
4320 reinterpret_cast < type-id > ( expression )
4321 const_cast < type-id > ( expression )
4322 typeid ( expression )
4328 ( type-id ) { initializer-list , [opt] }
4330 This extension is a GNU version of the C99 compound-literal
4331 construct. (The C99 grammar uses `type-name' instead of `type-id',
4332 but they are essentially the same concept.)
4334 If ADDRESS_P is true, the postfix expression is the operand of the
4335 `&' operator. CAST_P is true if this expression is the target of a
4338 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4339 class member access expressions [expr.ref].
4341 Returns a representation of the expression. */
4344 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4345 bool member_access_only_p)
4349 cp_id_kind idk = CP_ID_KIND_NONE;
4350 tree postfix_expression = NULL_TREE;
4351 bool is_member_access = false;
4353 /* Peek at the next token. */
4354 token = cp_lexer_peek_token (parser->lexer);
4355 /* Some of the productions are determined by keywords. */
4356 keyword = token->keyword;
4366 const char *saved_message;
4368 /* All of these can be handled in the same way from the point
4369 of view of parsing. Begin by consuming the token
4370 identifying the cast. */
4371 cp_lexer_consume_token (parser->lexer);
4373 /* New types cannot be defined in the cast. */
4374 saved_message = parser->type_definition_forbidden_message;
4375 parser->type_definition_forbidden_message
4376 = "types may not be defined in casts";
4378 /* Look for the opening `<'. */
4379 cp_parser_require (parser, CPP_LESS, "%<<%>");
4380 /* Parse the type to which we are casting. */
4381 type = cp_parser_type_id (parser);
4382 /* Look for the closing `>'. */
4383 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4384 /* Restore the old message. */
4385 parser->type_definition_forbidden_message = saved_message;
4387 /* And the expression which is being cast. */
4388 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4389 expression = cp_parser_expression (parser, /*cast_p=*/true);
4390 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4392 /* Only type conversions to integral or enumeration types
4393 can be used in constant-expressions. */
4394 if (!cast_valid_in_integral_constant_expression_p (type)
4395 && (cp_parser_non_integral_constant_expression
4397 "a cast to a type other than an integral or "
4398 "enumeration type")))
4399 return error_mark_node;
4405 = build_dynamic_cast (type, expression, tf_warning_or_error);
4409 = build_static_cast (type, expression, tf_warning_or_error);
4413 = build_reinterpret_cast (type, expression,
4414 tf_warning_or_error);
4418 = build_const_cast (type, expression, tf_warning_or_error);
4429 const char *saved_message;
4430 bool saved_in_type_id_in_expr_p;
4432 /* Consume the `typeid' token. */
4433 cp_lexer_consume_token (parser->lexer);
4434 /* Look for the `(' token. */
4435 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4436 /* Types cannot be defined in a `typeid' expression. */
4437 saved_message = parser->type_definition_forbidden_message;
4438 parser->type_definition_forbidden_message
4439 = "types may not be defined in a %<typeid%> expression";
4440 /* We can't be sure yet whether we're looking at a type-id or an
4442 cp_parser_parse_tentatively (parser);
4443 /* Try a type-id first. */
4444 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4445 parser->in_type_id_in_expr_p = true;
4446 type = cp_parser_type_id (parser);
4447 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4448 /* Look for the `)' token. Otherwise, we can't be sure that
4449 we're not looking at an expression: consider `typeid (int
4450 (3))', for example. */
4451 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4452 /* If all went well, simply lookup the type-id. */
4453 if (cp_parser_parse_definitely (parser))
4454 postfix_expression = get_typeid (type);
4455 /* Otherwise, fall back to the expression variant. */
4460 /* Look for an expression. */
4461 expression = cp_parser_expression (parser, /*cast_p=*/false);
4462 /* Compute its typeid. */
4463 postfix_expression = build_typeid (expression);
4464 /* Look for the `)' token. */
4465 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4467 /* Restore the saved message. */
4468 parser->type_definition_forbidden_message = saved_message;
4469 /* `typeid' may not appear in an integral constant expression. */
4470 if (cp_parser_non_integral_constant_expression(parser,
4471 "%<typeid%> operator"))
4472 return error_mark_node;
4479 /* The syntax permitted here is the same permitted for an
4480 elaborated-type-specifier. */
4481 type = cp_parser_elaborated_type_specifier (parser,
4482 /*is_friend=*/false,
4483 /*is_declaration=*/false);
4484 postfix_expression = cp_parser_functional_cast (parser, type);
4492 /* If the next thing is a simple-type-specifier, we may be
4493 looking at a functional cast. We could also be looking at
4494 an id-expression. So, we try the functional cast, and if
4495 that doesn't work we fall back to the primary-expression. */
4496 cp_parser_parse_tentatively (parser);
4497 /* Look for the simple-type-specifier. */
4498 type = cp_parser_simple_type_specifier (parser,
4499 /*decl_specs=*/NULL,
4500 CP_PARSER_FLAGS_NONE);
4501 /* Parse the cast itself. */
4502 if (!cp_parser_error_occurred (parser))
4504 = cp_parser_functional_cast (parser, type);
4505 /* If that worked, we're done. */
4506 if (cp_parser_parse_definitely (parser))
4509 /* If the functional-cast didn't work out, try a
4510 compound-literal. */
4511 if (cp_parser_allow_gnu_extensions_p (parser)
4512 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4514 VEC(constructor_elt,gc) *initializer_list = NULL;
4515 bool saved_in_type_id_in_expr_p;
4517 cp_parser_parse_tentatively (parser);
4518 /* Consume the `('. */
4519 cp_lexer_consume_token (parser->lexer);
4520 /* Parse the type. */
4521 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4522 parser->in_type_id_in_expr_p = true;
4523 type = cp_parser_type_id (parser);
4524 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4525 /* Look for the `)'. */
4526 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4527 /* Look for the `{'. */
4528 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4529 /* If things aren't going well, there's no need to
4531 if (!cp_parser_error_occurred (parser))
4533 bool non_constant_p;
4534 /* Parse the initializer-list. */
4536 = cp_parser_initializer_list (parser, &non_constant_p);
4537 /* Allow a trailing `,'. */
4538 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4539 cp_lexer_consume_token (parser->lexer);
4540 /* Look for the final `}'. */
4541 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4543 /* If that worked, we're definitely looking at a
4544 compound-literal expression. */
4545 if (cp_parser_parse_definitely (parser))
4547 /* Warn the user that a compound literal is not
4548 allowed in standard C++. */
4549 pedwarn (OPT_pedantic, "ISO C++ forbids compound-literals");
4550 /* For simplicity, we disallow compound literals in
4551 constant-expressions. We could
4552 allow compound literals of integer type, whose
4553 initializer was a constant, in constant
4554 expressions. Permitting that usage, as a further
4555 extension, would not change the meaning of any
4556 currently accepted programs. (Of course, as
4557 compound literals are not part of ISO C++, the
4558 standard has nothing to say.) */
4559 if (cp_parser_non_integral_constant_expression
4560 (parser, "non-constant compound literals"))
4562 postfix_expression = error_mark_node;
4565 /* Form the representation of the compound-literal. */
4567 = (finish_compound_literal
4568 (type, build_constructor (init_list_type_node,
4569 initializer_list)));
4574 /* It must be a primary-expression. */
4576 = cp_parser_primary_expression (parser, address_p, cast_p,
4577 /*template_arg_p=*/false,
4583 /* Keep looping until the postfix-expression is complete. */
4586 if (idk == CP_ID_KIND_UNQUALIFIED
4587 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4588 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4589 /* It is not a Koenig lookup function call. */
4591 = unqualified_name_lookup_error (postfix_expression);
4593 /* Peek at the next token. */
4594 token = cp_lexer_peek_token (parser->lexer);
4596 switch (token->type)
4598 case CPP_OPEN_SQUARE:
4600 = cp_parser_postfix_open_square_expression (parser,
4603 idk = CP_ID_KIND_NONE;
4604 is_member_access = false;
4607 case CPP_OPEN_PAREN:
4608 /* postfix-expression ( expression-list [opt] ) */
4611 bool is_builtin_constant_p;
4612 bool saved_integral_constant_expression_p = false;
4613 bool saved_non_integral_constant_expression_p = false;
4616 is_member_access = false;
4618 is_builtin_constant_p
4619 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4620 if (is_builtin_constant_p)
4622 /* The whole point of __builtin_constant_p is to allow
4623 non-constant expressions to appear as arguments. */
4624 saved_integral_constant_expression_p
4625 = parser->integral_constant_expression_p;
4626 saved_non_integral_constant_expression_p
4627 = parser->non_integral_constant_expression_p;
4628 parser->integral_constant_expression_p = false;
4630 args = (cp_parser_parenthesized_expression_list
4631 (parser, /*is_attribute_list=*/false,
4632 /*cast_p=*/false, /*allow_expansion_p=*/true,
4633 /*non_constant_p=*/NULL));
4634 if (is_builtin_constant_p)
4636 parser->integral_constant_expression_p
4637 = saved_integral_constant_expression_p;
4638 parser->non_integral_constant_expression_p
4639 = saved_non_integral_constant_expression_p;
4642 if (args == error_mark_node)
4644 postfix_expression = error_mark_node;
4648 /* Function calls are not permitted in
4649 constant-expressions. */
4650 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4651 && cp_parser_non_integral_constant_expression (parser,
4654 postfix_expression = error_mark_node;
4659 if (idk == CP_ID_KIND_UNQUALIFIED)
4661 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4667 = perform_koenig_lookup (postfix_expression, args);
4671 = unqualified_fn_lookup_error (postfix_expression);
4673 /* We do not perform argument-dependent lookup if
4674 normal lookup finds a non-function, in accordance
4675 with the expected resolution of DR 218. */
4676 else if (args && is_overloaded_fn (postfix_expression))
4678 tree fn = get_first_fn (postfix_expression);
4680 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4681 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4683 /* Only do argument dependent lookup if regular
4684 lookup does not find a set of member functions.
4685 [basic.lookup.koenig]/2a */
4686 if (!DECL_FUNCTION_MEMBER_P (fn))
4690 = perform_koenig_lookup (postfix_expression, args);
4695 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4697 tree instance = TREE_OPERAND (postfix_expression, 0);
4698 tree fn = TREE_OPERAND (postfix_expression, 1);
4700 if (processing_template_decl
4701 && (type_dependent_expression_p (instance)
4702 || (!BASELINK_P (fn)
4703 && TREE_CODE (fn) != FIELD_DECL)
4704 || type_dependent_expression_p (fn)
4705 || any_type_dependent_arguments_p (args)))
4708 = build_nt_call_list (postfix_expression, args);
4712 if (BASELINK_P (fn))
4714 = (build_new_method_call
4715 (instance, fn, args, NULL_TREE,
4716 (idk == CP_ID_KIND_QUALIFIED
4717 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4719 tf_warning_or_error));
4722 = finish_call_expr (postfix_expression, args,
4723 /*disallow_virtual=*/false,
4725 tf_warning_or_error);
4727 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4728 || TREE_CODE (postfix_expression) == MEMBER_REF
4729 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4730 postfix_expression = (build_offset_ref_call_from_tree
4731 (postfix_expression, args));
4732 else if (idk == CP_ID_KIND_QUALIFIED)
4733 /* A call to a static class member, or a namespace-scope
4736 = finish_call_expr (postfix_expression, args,
4737 /*disallow_virtual=*/true,
4739 tf_warning_or_error);
4741 /* All other function calls. */
4743 = finish_call_expr (postfix_expression, args,
4744 /*disallow_virtual=*/false,
4746 tf_warning_or_error);
4748 if (warn_disallowed_functions)
4749 warn_if_disallowed_function_p (postfix_expression);
4751 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4752 idk = CP_ID_KIND_NONE;
4758 /* postfix-expression . template [opt] id-expression
4759 postfix-expression . pseudo-destructor-name
4760 postfix-expression -> template [opt] id-expression
4761 postfix-expression -> pseudo-destructor-name */
4763 /* Consume the `.' or `->' operator. */
4764 cp_lexer_consume_token (parser->lexer);
4767 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4772 is_member_access = true;
4776 /* postfix-expression ++ */
4777 /* Consume the `++' token. */
4778 cp_lexer_consume_token (parser->lexer);
4779 /* Generate a representation for the complete expression. */
4781 = finish_increment_expr (postfix_expression,
4782 POSTINCREMENT_EXPR);
4783 /* Increments may not appear in constant-expressions. */
4784 if (cp_parser_non_integral_constant_expression (parser,
4786 postfix_expression = error_mark_node;
4787 idk = CP_ID_KIND_NONE;
4788 is_member_access = false;
4791 case CPP_MINUS_MINUS:
4792 /* postfix-expression -- */
4793 /* Consume the `--' token. */
4794 cp_lexer_consume_token (parser->lexer);
4795 /* Generate a representation for the complete expression. */
4797 = finish_increment_expr (postfix_expression,
4798 POSTDECREMENT_EXPR);
4799 /* Decrements may not appear in constant-expressions. */
4800 if (cp_parser_non_integral_constant_expression (parser,
4802 postfix_expression = error_mark_node;
4803 idk = CP_ID_KIND_NONE;
4804 is_member_access = false;
4808 if (member_access_only_p)
4809 return is_member_access? postfix_expression : error_mark_node;
4811 return postfix_expression;
4815 /* We should never get here. */
4817 return error_mark_node;
4820 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4821 by cp_parser_builtin_offsetof. We're looking for
4823 postfix-expression [ expression ]
4825 FOR_OFFSETOF is set if we're being called in that context, which
4826 changes how we deal with integer constant expressions. */
4829 cp_parser_postfix_open_square_expression (cp_parser *parser,
4830 tree postfix_expression,
4835 /* Consume the `[' token. */
4836 cp_lexer_consume_token (parser->lexer);
4838 /* Parse the index expression. */
4839 /* ??? For offsetof, there is a question of what to allow here. If
4840 offsetof is not being used in an integral constant expression context,
4841 then we *could* get the right answer by computing the value at runtime.
4842 If we are in an integral constant expression context, then we might
4843 could accept any constant expression; hard to say without analysis.
4844 Rather than open the barn door too wide right away, allow only integer
4845 constant expressions here. */
4847 index = cp_parser_constant_expression (parser, false, NULL);
4849 index = cp_parser_expression (parser, /*cast_p=*/false);
4851 /* Look for the closing `]'. */
4852 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4854 /* Build the ARRAY_REF. */
4855 postfix_expression = grok_array_decl (postfix_expression, index);
4857 /* When not doing offsetof, array references are not permitted in
4858 constant-expressions. */
4860 && (cp_parser_non_integral_constant_expression
4861 (parser, "an array reference")))
4862 postfix_expression = error_mark_node;
4864 return postfix_expression;
4867 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4868 by cp_parser_builtin_offsetof. We're looking for
4870 postfix-expression . template [opt] id-expression
4871 postfix-expression . pseudo-destructor-name
4872 postfix-expression -> template [opt] id-expression
4873 postfix-expression -> pseudo-destructor-name
4875 FOR_OFFSETOF is set if we're being called in that context. That sorta
4876 limits what of the above we'll actually accept, but nevermind.
4877 TOKEN_TYPE is the "." or "->" token, which will already have been
4878 removed from the stream. */
4881 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4882 enum cpp_ttype token_type,
4883 tree postfix_expression,
4884 bool for_offsetof, cp_id_kind *idk,
4885 location_t location)
4889 bool pseudo_destructor_p;
4890 tree scope = NULL_TREE;
4892 /* If this is a `->' operator, dereference the pointer. */
4893 if (token_type == CPP_DEREF)
4894 postfix_expression = build_x_arrow (postfix_expression);
4895 /* Check to see whether or not the expression is type-dependent. */
4896 dependent_p = type_dependent_expression_p (postfix_expression);
4897 /* The identifier following the `->' or `.' is not qualified. */
4898 parser->scope = NULL_TREE;
4899 parser->qualifying_scope = NULL_TREE;
4900 parser->object_scope = NULL_TREE;
4901 *idk = CP_ID_KIND_NONE;
4902 /* Enter the scope corresponding to the type of the object
4903 given by the POSTFIX_EXPRESSION. */
4904 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4906 scope = TREE_TYPE (postfix_expression);
4907 /* According to the standard, no expression should ever have
4908 reference type. Unfortunately, we do not currently match
4909 the standard in this respect in that our internal representation
4910 of an expression may have reference type even when the standard
4911 says it does not. Therefore, we have to manually obtain the
4912 underlying type here. */
4913 scope = non_reference (scope);
4914 /* The type of the POSTFIX_EXPRESSION must be complete. */
4915 if (scope == unknown_type_node)
4917 error ("%H%qE does not have class type", &location, postfix_expression);
4921 scope = complete_type_or_else (scope, NULL_TREE);
4922 /* Let the name lookup machinery know that we are processing a
4923 class member access expression. */
4924 parser->context->object_type = scope;
4925 /* If something went wrong, we want to be able to discern that case,
4926 as opposed to the case where there was no SCOPE due to the type
4927 of expression being dependent. */
4929 scope = error_mark_node;
4930 /* If the SCOPE was erroneous, make the various semantic analysis
4931 functions exit quickly -- and without issuing additional error
4933 if (scope == error_mark_node)
4934 postfix_expression = error_mark_node;
4937 /* Assume this expression is not a pseudo-destructor access. */
4938 pseudo_destructor_p = false;
4940 /* If the SCOPE is a scalar type, then, if this is a valid program,
4941 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4942 is type dependent, it can be pseudo-destructor-name or something else.
4943 Try to parse it as pseudo-destructor-name first. */
4944 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4949 cp_parser_parse_tentatively (parser);
4950 /* Parse the pseudo-destructor-name. */
4952 cp_parser_pseudo_destructor_name (parser, &s, &type);
4954 && (cp_parser_error_occurred (parser)
4955 || TREE_CODE (type) != TYPE_DECL
4956 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4957 cp_parser_abort_tentative_parse (parser);
4958 else if (cp_parser_parse_definitely (parser))
4960 pseudo_destructor_p = true;
4962 = finish_pseudo_destructor_expr (postfix_expression,
4963 s, TREE_TYPE (type));
4967 if (!pseudo_destructor_p)
4969 /* If the SCOPE is not a scalar type, we are looking at an
4970 ordinary class member access expression, rather than a
4971 pseudo-destructor-name. */
4973 cp_token *token = cp_lexer_peek_token (parser->lexer);
4974 /* Parse the id-expression. */
4975 name = (cp_parser_id_expression
4977 cp_parser_optional_template_keyword (parser),
4978 /*check_dependency_p=*/true,
4980 /*declarator_p=*/false,
4981 /*optional_p=*/false));
4982 /* In general, build a SCOPE_REF if the member name is qualified.
4983 However, if the name was not dependent and has already been
4984 resolved; there is no need to build the SCOPE_REF. For example;
4986 struct X { void f(); };
4987 template <typename T> void f(T* t) { t->X::f(); }
4989 Even though "t" is dependent, "X::f" is not and has been resolved
4990 to a BASELINK; there is no need to include scope information. */
4992 /* But we do need to remember that there was an explicit scope for
4993 virtual function calls. */
4995 *idk = CP_ID_KIND_QUALIFIED;
4997 /* If the name is a template-id that names a type, we will get a
4998 TYPE_DECL here. That is invalid code. */
4999 if (TREE_CODE (name) == TYPE_DECL)
5001 error ("%Hinvalid use of %qD", &token->location, name);
5002 postfix_expression = error_mark_node;
5006 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5008 name = build_qualified_name (/*type=*/NULL_TREE,
5012 parser->scope = NULL_TREE;
5013 parser->qualifying_scope = NULL_TREE;
5014 parser->object_scope = NULL_TREE;
5016 if (scope && name && BASELINK_P (name))
5017 adjust_result_of_qualified_name_lookup
5018 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5020 = finish_class_member_access_expr (postfix_expression, name,
5022 tf_warning_or_error);
5026 /* We no longer need to look up names in the scope of the object on
5027 the left-hand side of the `.' or `->' operator. */
5028 parser->context->object_type = NULL_TREE;
5030 /* Outside of offsetof, these operators may not appear in
5031 constant-expressions. */
5033 && (cp_parser_non_integral_constant_expression
5034 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5035 postfix_expression = error_mark_node;
5037 return postfix_expression;
5040 /* Parse a parenthesized expression-list.
5043 assignment-expression
5044 expression-list, assignment-expression
5049 identifier, expression-list
5051 CAST_P is true if this expression is the target of a cast.
5053 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5056 Returns a TREE_LIST. The TREE_VALUE of each node is a
5057 representation of an assignment-expression. Note that a TREE_LIST
5058 is returned even if there is only a single expression in the list.
5059 error_mark_node is returned if the ( and or ) are
5060 missing. NULL_TREE is returned on no expressions. The parentheses
5061 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5062 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5063 indicates whether or not all of the expressions in the list were
5067 cp_parser_parenthesized_expression_list (cp_parser* parser,
5068 bool is_attribute_list,
5070 bool allow_expansion_p,
5071 bool *non_constant_p)
5073 tree expression_list = NULL_TREE;
5074 bool fold_expr_p = is_attribute_list;
5075 tree identifier = NULL_TREE;
5076 bool saved_greater_than_is_operator_p;
5078 /* Assume all the expressions will be constant. */
5080 *non_constant_p = false;
5082 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5083 return error_mark_node;
5085 /* Within a parenthesized expression, a `>' token is always
5086 the greater-than operator. */
5087 saved_greater_than_is_operator_p
5088 = parser->greater_than_is_operator_p;
5089 parser->greater_than_is_operator_p = true;
5091 /* Consume expressions until there are no more. */
5092 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5097 /* At the beginning of attribute lists, check to see if the
5098 next token is an identifier. */
5099 if (is_attribute_list
5100 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5104 /* Consume the identifier. */
5105 token = cp_lexer_consume_token (parser->lexer);
5106 /* Save the identifier. */
5107 identifier = token->u.value;
5111 bool expr_non_constant_p;
5113 /* Parse the next assignment-expression. */
5114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5116 /* A braced-init-list. */
5117 maybe_warn_cpp0x ("extended initializer lists");
5118 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5119 if (non_constant_p && expr_non_constant_p)
5120 *non_constant_p = true;
5122 else if (non_constant_p)
5124 expr = (cp_parser_constant_expression
5125 (parser, /*allow_non_constant_p=*/true,
5126 &expr_non_constant_p));
5127 if (expr_non_constant_p)
5128 *non_constant_p = true;
5131 expr = cp_parser_assignment_expression (parser, cast_p);
5134 expr = fold_non_dependent_expr (expr);
5136 /* If we have an ellipsis, then this is an expression
5138 if (allow_expansion_p
5139 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5141 /* Consume the `...'. */
5142 cp_lexer_consume_token (parser->lexer);
5144 /* Build the argument pack. */
5145 expr = make_pack_expansion (expr);
5148 /* Add it to the list. We add error_mark_node
5149 expressions to the list, so that we can still tell if
5150 the correct form for a parenthesized expression-list
5151 is found. That gives better errors. */
5152 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5154 if (expr == error_mark_node)
5158 /* After the first item, attribute lists look the same as
5159 expression lists. */
5160 is_attribute_list = false;
5163 /* If the next token isn't a `,', then we are done. */
5164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5167 /* Otherwise, consume the `,' and keep going. */
5168 cp_lexer_consume_token (parser->lexer);
5171 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5176 /* We try and resync to an unnested comma, as that will give the
5177 user better diagnostics. */
5178 ending = cp_parser_skip_to_closing_parenthesis (parser,
5179 /*recovering=*/true,
5181 /*consume_paren=*/true);
5186 parser->greater_than_is_operator_p
5187 = saved_greater_than_is_operator_p;
5188 return error_mark_node;
5192 parser->greater_than_is_operator_p
5193 = saved_greater_than_is_operator_p;
5195 /* We built up the list in reverse order so we must reverse it now. */
5196 expression_list = nreverse (expression_list);
5198 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5200 return expression_list;
5203 /* Parse a pseudo-destructor-name.
5205 pseudo-destructor-name:
5206 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5207 :: [opt] nested-name-specifier template template-id :: ~ type-name
5208 :: [opt] nested-name-specifier [opt] ~ type-name
5210 If either of the first two productions is used, sets *SCOPE to the
5211 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5212 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5213 or ERROR_MARK_NODE if the parse fails. */
5216 cp_parser_pseudo_destructor_name (cp_parser* parser,
5220 bool nested_name_specifier_p;
5222 /* Assume that things will not work out. */
5223 *type = error_mark_node;
5225 /* Look for the optional `::' operator. */
5226 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5227 /* Look for the optional nested-name-specifier. */
5228 nested_name_specifier_p
5229 = (cp_parser_nested_name_specifier_opt (parser,
5230 /*typename_keyword_p=*/false,
5231 /*check_dependency_p=*/true,
5233 /*is_declaration=*/true)
5235 /* Now, if we saw a nested-name-specifier, we might be doing the
5236 second production. */
5237 if (nested_name_specifier_p
5238 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5240 /* Consume the `template' keyword. */
5241 cp_lexer_consume_token (parser->lexer);
5242 /* Parse the template-id. */
5243 cp_parser_template_id (parser,
5244 /*template_keyword_p=*/true,
5245 /*check_dependency_p=*/false,
5246 /*is_declaration=*/true);
5247 /* Look for the `::' token. */
5248 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5250 /* If the next token is not a `~', then there might be some
5251 additional qualification. */
5252 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5254 /* At this point, we're looking for "type-name :: ~". The type-name
5255 must not be a class-name, since this is a pseudo-destructor. So,
5256 it must be either an enum-name, or a typedef-name -- both of which
5257 are just identifiers. So, we peek ahead to check that the "::"
5258 and "~" tokens are present; if they are not, then we can avoid
5259 calling type_name. */
5260 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5261 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5262 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5264 cp_parser_error (parser, "non-scalar type");
5268 /* Look for the type-name. */
5269 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5270 if (*scope == error_mark_node)
5273 /* Look for the `::' token. */
5274 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5279 /* Look for the `~'. */
5280 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5281 /* Look for the type-name again. We are not responsible for
5282 checking that it matches the first type-name. */
5283 *type = cp_parser_nonclass_name (parser);
5286 /* Parse a unary-expression.
5292 unary-operator cast-expression
5293 sizeof unary-expression
5301 __extension__ cast-expression
5302 __alignof__ unary-expression
5303 __alignof__ ( type-id )
5304 __real__ cast-expression
5305 __imag__ cast-expression
5308 ADDRESS_P is true iff the unary-expression is appearing as the
5309 operand of the `&' operator. CAST_P is true if this expression is
5310 the target of a cast.
5312 Returns a representation of the expression. */
5315 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5318 enum tree_code unary_operator;
5320 /* Peek at the next token. */
5321 token = cp_lexer_peek_token (parser->lexer);
5322 /* Some keywords give away the kind of expression. */
5323 if (token->type == CPP_KEYWORD)
5325 enum rid keyword = token->keyword;
5335 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5336 /* Consume the token. */
5337 cp_lexer_consume_token (parser->lexer);
5338 /* Parse the operand. */
5339 operand = cp_parser_sizeof_operand (parser, keyword);
5341 if (TYPE_P (operand))
5342 return cxx_sizeof_or_alignof_type (operand, op, true);
5344 return cxx_sizeof_or_alignof_expr (operand, op, true);
5348 return cp_parser_new_expression (parser);
5351 return cp_parser_delete_expression (parser);
5355 /* The saved value of the PEDANTIC flag. */
5359 /* Save away the PEDANTIC flag. */
5360 cp_parser_extension_opt (parser, &saved_pedantic);
5361 /* Parse the cast-expression. */
5362 expr = cp_parser_simple_cast_expression (parser);
5363 /* Restore the PEDANTIC flag. */
5364 pedantic = saved_pedantic;
5374 /* Consume the `__real__' or `__imag__' token. */
5375 cp_lexer_consume_token (parser->lexer);
5376 /* Parse the cast-expression. */
5377 expression = cp_parser_simple_cast_expression (parser);
5378 /* Create the complete representation. */
5379 return build_x_unary_op ((keyword == RID_REALPART
5380 ? REALPART_EXPR : IMAGPART_EXPR),
5382 tf_warning_or_error);
5391 /* Look for the `:: new' and `:: delete', which also signal the
5392 beginning of a new-expression, or delete-expression,
5393 respectively. If the next token is `::', then it might be one of
5395 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5399 /* See if the token after the `::' is one of the keywords in
5400 which we're interested. */
5401 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5402 /* If it's `new', we have a new-expression. */
5403 if (keyword == RID_NEW)
5404 return cp_parser_new_expression (parser);
5405 /* Similarly, for `delete'. */
5406 else if (keyword == RID_DELETE)
5407 return cp_parser_delete_expression (parser);
5410 /* Look for a unary operator. */
5411 unary_operator = cp_parser_unary_operator (token);
5412 /* The `++' and `--' operators can be handled similarly, even though
5413 they are not technically unary-operators in the grammar. */
5414 if (unary_operator == ERROR_MARK)
5416 if (token->type == CPP_PLUS_PLUS)
5417 unary_operator = PREINCREMENT_EXPR;
5418 else if (token->type == CPP_MINUS_MINUS)
5419 unary_operator = PREDECREMENT_EXPR;
5420 /* Handle the GNU address-of-label extension. */
5421 else if (cp_parser_allow_gnu_extensions_p (parser)
5422 && token->type == CPP_AND_AND)
5427 /* Consume the '&&' token. */
5428 cp_lexer_consume_token (parser->lexer);
5429 /* Look for the identifier. */
5430 identifier = cp_parser_identifier (parser);
5431 /* Create an expression representing the address. */
5432 expression = finish_label_address_expr (identifier);
5433 if (cp_parser_non_integral_constant_expression (parser,
5434 "the address of a label"))
5435 expression = error_mark_node;
5439 if (unary_operator != ERROR_MARK)
5441 tree cast_expression;
5442 tree expression = error_mark_node;
5443 const char *non_constant_p = NULL;
5445 /* Consume the operator token. */
5446 token = cp_lexer_consume_token (parser->lexer);
5447 /* Parse the cast-expression. */
5449 = cp_parser_cast_expression (parser,
5450 unary_operator == ADDR_EXPR,
5452 /* Now, build an appropriate representation. */
5453 switch (unary_operator)
5456 non_constant_p = "%<*%>";
5457 expression = build_x_indirect_ref (cast_expression, "unary *",
5458 tf_warning_or_error);
5462 non_constant_p = "%<&%>";
5465 expression = build_x_unary_op (unary_operator, cast_expression,
5466 tf_warning_or_error);
5469 case PREINCREMENT_EXPR:
5470 case PREDECREMENT_EXPR:
5471 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5472 ? "%<++%>" : "%<--%>");
5474 case UNARY_PLUS_EXPR:
5476 case TRUTH_NOT_EXPR:
5477 expression = finish_unary_op_expr (unary_operator, cast_expression);
5485 && cp_parser_non_integral_constant_expression (parser,
5487 expression = error_mark_node;
5492 return cp_parser_postfix_expression (parser, address_p, cast_p,
5493 /*member_access_only_p=*/false);
5496 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5497 unary-operator, the corresponding tree code is returned. */
5499 static enum tree_code
5500 cp_parser_unary_operator (cp_token* token)
5502 switch (token->type)
5505 return INDIRECT_REF;
5511 return UNARY_PLUS_EXPR;
5517 return TRUTH_NOT_EXPR;
5520 return BIT_NOT_EXPR;
5527 /* Parse a new-expression.
5530 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5531 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5533 Returns a representation of the expression. */
5536 cp_parser_new_expression (cp_parser* parser)
5538 bool global_scope_p;
5544 /* Look for the optional `::' operator. */
5546 = (cp_parser_global_scope_opt (parser,
5547 /*current_scope_valid_p=*/false)
5549 /* Look for the `new' operator. */
5550 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5551 /* There's no easy way to tell a new-placement from the
5552 `( type-id )' construct. */
5553 cp_parser_parse_tentatively (parser);
5554 /* Look for a new-placement. */
5555 placement = cp_parser_new_placement (parser);
5556 /* If that didn't work out, there's no new-placement. */
5557 if (!cp_parser_parse_definitely (parser))
5558 placement = NULL_TREE;
5560 /* If the next token is a `(', then we have a parenthesized
5562 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5565 /* Consume the `('. */
5566 cp_lexer_consume_token (parser->lexer);
5567 /* Parse the type-id. */
5568 type = cp_parser_type_id (parser);
5569 /* Look for the closing `)'. */
5570 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5571 token = cp_lexer_peek_token (parser->lexer);
5572 /* There should not be a direct-new-declarator in this production,
5573 but GCC used to allowed this, so we check and emit a sensible error
5574 message for this case. */
5575 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5577 error ("%Harray bound forbidden after parenthesized type-id",
5579 inform ("%Htry removing the parentheses around the type-id",
5581 cp_parser_direct_new_declarator (parser);
5585 /* Otherwise, there must be a new-type-id. */
5587 type = cp_parser_new_type_id (parser, &nelts);
5589 /* If the next token is a `(' or '{', then we have a new-initializer. */
5590 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5591 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5592 initializer = cp_parser_new_initializer (parser);
5594 initializer = NULL_TREE;
5596 /* A new-expression may not appear in an integral constant
5598 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5599 return error_mark_node;
5601 /* Create a representation of the new-expression. */
5602 return build_new (placement, type, nelts, initializer, global_scope_p,
5603 tf_warning_or_error);
5606 /* Parse a new-placement.
5611 Returns the same representation as for an expression-list. */
5614 cp_parser_new_placement (cp_parser* parser)
5616 tree expression_list;
5618 /* Parse the expression-list. */
5619 expression_list = (cp_parser_parenthesized_expression_list
5620 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5621 /*non_constant_p=*/NULL));
5623 return expression_list;
5626 /* Parse a new-type-id.
5629 type-specifier-seq new-declarator [opt]
5631 Returns the TYPE allocated. If the new-type-id indicates an array
5632 type, *NELTS is set to the number of elements in the last array
5633 bound; the TYPE will not include the last array bound. */
5636 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5638 cp_decl_specifier_seq type_specifier_seq;
5639 cp_declarator *new_declarator;
5640 cp_declarator *declarator;
5641 cp_declarator *outer_declarator;
5642 const char *saved_message;
5645 /* The type-specifier sequence must not contain type definitions.
5646 (It cannot contain declarations of new types either, but if they
5647 are not definitions we will catch that because they are not
5649 saved_message = parser->type_definition_forbidden_message;
5650 parser->type_definition_forbidden_message
5651 = "types may not be defined in a new-type-id";
5652 /* Parse the type-specifier-seq. */
5653 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5654 &type_specifier_seq);
5655 /* Restore the old message. */
5656 parser->type_definition_forbidden_message = saved_message;
5657 /* Parse the new-declarator. */
5658 new_declarator = cp_parser_new_declarator_opt (parser);
5660 /* Determine the number of elements in the last array dimension, if
5663 /* Skip down to the last array dimension. */
5664 declarator = new_declarator;
5665 outer_declarator = NULL;
5666 while (declarator && (declarator->kind == cdk_pointer
5667 || declarator->kind == cdk_ptrmem))
5669 outer_declarator = declarator;
5670 declarator = declarator->declarator;
5673 && declarator->kind == cdk_array
5674 && declarator->declarator
5675 && declarator->declarator->kind == cdk_array)
5677 outer_declarator = declarator;
5678 declarator = declarator->declarator;
5681 if (declarator && declarator->kind == cdk_array)
5683 *nelts = declarator->u.array.bounds;
5684 if (*nelts == error_mark_node)
5685 *nelts = integer_one_node;
5687 if (outer_declarator)
5688 outer_declarator->declarator = declarator->declarator;
5690 new_declarator = NULL;
5693 type = groktypename (&type_specifier_seq, new_declarator);
5697 /* Parse an (optional) new-declarator.
5700 ptr-operator new-declarator [opt]
5701 direct-new-declarator
5703 Returns the declarator. */
5705 static cp_declarator *
5706 cp_parser_new_declarator_opt (cp_parser* parser)
5708 enum tree_code code;
5710 cp_cv_quals cv_quals;
5712 /* We don't know if there's a ptr-operator next, or not. */
5713 cp_parser_parse_tentatively (parser);
5714 /* Look for a ptr-operator. */
5715 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5716 /* If that worked, look for more new-declarators. */
5717 if (cp_parser_parse_definitely (parser))
5719 cp_declarator *declarator;
5721 /* Parse another optional declarator. */
5722 declarator = cp_parser_new_declarator_opt (parser);
5724 return cp_parser_make_indirect_declarator
5725 (code, type, cv_quals, declarator);
5728 /* If the next token is a `[', there is a direct-new-declarator. */
5729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5730 return cp_parser_direct_new_declarator (parser);
5735 /* Parse a direct-new-declarator.
5737 direct-new-declarator:
5739 direct-new-declarator [constant-expression]
5743 static cp_declarator *
5744 cp_parser_direct_new_declarator (cp_parser* parser)
5746 cp_declarator *declarator = NULL;
5752 /* Look for the opening `['. */
5753 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5754 /* The first expression is not required to be constant. */
5757 cp_token *token = cp_lexer_peek_token (parser->lexer);
5758 expression = cp_parser_expression (parser, /*cast_p=*/false);
5759 /* The standard requires that the expression have integral
5760 type. DR 74 adds enumeration types. We believe that the
5761 real intent is that these expressions be handled like the
5762 expression in a `switch' condition, which also allows
5763 classes with a single conversion to integral or
5764 enumeration type. */
5765 if (!processing_template_decl)
5768 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5773 error ("%Hexpression in new-declarator must have integral "
5774 "or enumeration type", &token->location);
5775 expression = error_mark_node;
5779 /* But all the other expressions must be. */
5782 = cp_parser_constant_expression (parser,
5783 /*allow_non_constant=*/false,
5785 /* Look for the closing `]'. */
5786 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5788 /* Add this bound to the declarator. */
5789 declarator = make_array_declarator (declarator, expression);
5791 /* If the next token is not a `[', then there are no more
5793 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5800 /* Parse a new-initializer.
5803 ( expression-list [opt] )
5806 Returns a representation of the expression-list. If there is no
5807 expression-list, VOID_ZERO_NODE is returned. */
5810 cp_parser_new_initializer (cp_parser* parser)
5812 tree expression_list;
5814 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5816 bool expr_non_constant_p;
5817 maybe_warn_cpp0x ("extended initializer lists");
5818 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5819 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5820 expression_list = build_tree_list (NULL_TREE, expression_list);
5823 expression_list = (cp_parser_parenthesized_expression_list
5824 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5825 /*non_constant_p=*/NULL));
5826 if (!expression_list)
5827 expression_list = void_zero_node;
5829 return expression_list;
5832 /* Parse a delete-expression.
5835 :: [opt] delete cast-expression
5836 :: [opt] delete [ ] cast-expression
5838 Returns a representation of the expression. */
5841 cp_parser_delete_expression (cp_parser* parser)
5843 bool global_scope_p;
5847 /* Look for the optional `::' operator. */
5849 = (cp_parser_global_scope_opt (parser,
5850 /*current_scope_valid_p=*/false)
5852 /* Look for the `delete' keyword. */
5853 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5854 /* See if the array syntax is in use. */
5855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5857 /* Consume the `[' token. */
5858 cp_lexer_consume_token (parser->lexer);
5859 /* Look for the `]' token. */
5860 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5861 /* Remember that this is the `[]' construct. */
5867 /* Parse the cast-expression. */
5868 expression = cp_parser_simple_cast_expression (parser);
5870 /* A delete-expression may not appear in an integral constant
5872 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5873 return error_mark_node;
5875 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5878 /* Parse a cast-expression.
5882 ( type-id ) cast-expression
5884 ADDRESS_P is true iff the unary-expression is appearing as the
5885 operand of the `&' operator. CAST_P is true if this expression is
5886 the target of a cast.
5888 Returns a representation of the expression. */
5891 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5893 /* If it's a `(', then we might be looking at a cast. */
5894 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5896 tree type = NULL_TREE;
5897 tree expr = NULL_TREE;
5898 bool compound_literal_p;
5899 const char *saved_message;
5901 /* There's no way to know yet whether or not this is a cast.
5902 For example, `(int (3))' is a unary-expression, while `(int)
5903 3' is a cast. So, we resort to parsing tentatively. */
5904 cp_parser_parse_tentatively (parser);
5905 /* Types may not be defined in a cast. */
5906 saved_message = parser->type_definition_forbidden_message;
5907 parser->type_definition_forbidden_message
5908 = "types may not be defined in casts";
5909 /* Consume the `('. */
5910 cp_lexer_consume_token (parser->lexer);
5911 /* A very tricky bit is that `(struct S) { 3 }' is a
5912 compound-literal (which we permit in C++ as an extension).
5913 But, that construct is not a cast-expression -- it is a
5914 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5915 is legal; if the compound-literal were a cast-expression,
5916 you'd need an extra set of parentheses.) But, if we parse
5917 the type-id, and it happens to be a class-specifier, then we
5918 will commit to the parse at that point, because we cannot
5919 undo the action that is done when creating a new class. So,
5920 then we cannot back up and do a postfix-expression.
5922 Therefore, we scan ahead to the closing `)', and check to see
5923 if the token after the `)' is a `{'. If so, we are not
5924 looking at a cast-expression.
5926 Save tokens so that we can put them back. */
5927 cp_lexer_save_tokens (parser->lexer);
5928 /* Skip tokens until the next token is a closing parenthesis.
5929 If we find the closing `)', and the next token is a `{', then
5930 we are looking at a compound-literal. */
5932 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5933 /*consume_paren=*/true)
5934 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5935 /* Roll back the tokens we skipped. */
5936 cp_lexer_rollback_tokens (parser->lexer);
5937 /* If we were looking at a compound-literal, simulate an error
5938 so that the call to cp_parser_parse_definitely below will
5940 if (compound_literal_p)
5941 cp_parser_simulate_error (parser);
5944 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5945 parser->in_type_id_in_expr_p = true;
5946 /* Look for the type-id. */
5947 type = cp_parser_type_id (parser);
5948 /* Look for the closing `)'. */
5949 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5950 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5953 /* Restore the saved message. */
5954 parser->type_definition_forbidden_message = saved_message;
5956 /* If ok so far, parse the dependent expression. We cannot be
5957 sure it is a cast. Consider `(T ())'. It is a parenthesized
5958 ctor of T, but looks like a cast to function returning T
5959 without a dependent expression. */
5960 if (!cp_parser_error_occurred (parser))
5961 expr = cp_parser_cast_expression (parser,
5962 /*address_p=*/false,
5965 if (cp_parser_parse_definitely (parser))
5967 /* Warn about old-style casts, if so requested. */
5968 if (warn_old_style_cast
5969 && !in_system_header
5970 && !VOID_TYPE_P (type)
5971 && current_lang_name != lang_name_c)
5972 warning (OPT_Wold_style_cast, "use of old-style cast");
5974 /* Only type conversions to integral or enumeration types
5975 can be used in constant-expressions. */
5976 if (!cast_valid_in_integral_constant_expression_p (type)
5977 && (cp_parser_non_integral_constant_expression
5979 "a cast to a type other than an integral or "
5980 "enumeration type")))
5981 return error_mark_node;
5983 /* Perform the cast. */
5984 expr = build_c_cast (type, expr);
5989 /* If we get here, then it's not a cast, so it must be a
5990 unary-expression. */
5991 return cp_parser_unary_expression (parser, address_p, cast_p);
5994 /* Parse a binary expression of the general form:
5998 pm-expression .* cast-expression
5999 pm-expression ->* cast-expression
6001 multiplicative-expression:
6003 multiplicative-expression * pm-expression
6004 multiplicative-expression / pm-expression
6005 multiplicative-expression % pm-expression
6007 additive-expression:
6008 multiplicative-expression
6009 additive-expression + multiplicative-expression
6010 additive-expression - multiplicative-expression
6014 shift-expression << additive-expression
6015 shift-expression >> additive-expression
6017 relational-expression:
6019 relational-expression < shift-expression
6020 relational-expression > shift-expression
6021 relational-expression <= shift-expression
6022 relational-expression >= shift-expression
6026 relational-expression:
6027 relational-expression <? shift-expression
6028 relational-expression >? shift-expression
6030 equality-expression:
6031 relational-expression
6032 equality-expression == relational-expression
6033 equality-expression != relational-expression
6037 and-expression & equality-expression
6039 exclusive-or-expression:
6041 exclusive-or-expression ^ and-expression
6043 inclusive-or-expression:
6044 exclusive-or-expression
6045 inclusive-or-expression | exclusive-or-expression
6047 logical-and-expression:
6048 inclusive-or-expression
6049 logical-and-expression && inclusive-or-expression
6051 logical-or-expression:
6052 logical-and-expression
6053 logical-or-expression || logical-and-expression
6055 All these are implemented with a single function like:
6058 simple-cast-expression
6059 binary-expression <token> binary-expression
6061 CAST_P is true if this expression is the target of a cast.
6063 The binops_by_token map is used to get the tree codes for each <token> type.
6064 binary-expressions are associated according to a precedence table. */
6066 #define TOKEN_PRECEDENCE(token) \
6067 (((token->type == CPP_GREATER \
6068 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6069 && !parser->greater_than_is_operator_p) \
6070 ? PREC_NOT_OPERATOR \
6071 : binops_by_token[token->type].prec)
6074 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6075 enum cp_parser_prec prec)
6077 cp_parser_expression_stack stack;
6078 cp_parser_expression_stack_entry *sp = &stack[0];
6081 enum tree_code tree_type, lhs_type, rhs_type;
6082 enum cp_parser_prec new_prec, lookahead_prec;
6085 /* Parse the first expression. */
6086 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6087 lhs_type = ERROR_MARK;
6091 /* Get an operator token. */
6092 token = cp_lexer_peek_token (parser->lexer);
6094 if (warn_cxx0x_compat
6095 && token->type == CPP_RSHIFT
6096 && !parser->greater_than_is_operator_p)
6098 warning (OPT_Wc__0x_compat,
6099 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6101 warning (OPT_Wc__0x_compat,
6102 "suggest parentheses around %<>>%> expression");
6105 new_prec = TOKEN_PRECEDENCE (token);
6107 /* Popping an entry off the stack means we completed a subexpression:
6108 - either we found a token which is not an operator (`>' where it is not
6109 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6110 will happen repeatedly;
6111 - or, we found an operator which has lower priority. This is the case
6112 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6114 if (new_prec <= prec)
6123 tree_type = binops_by_token[token->type].tree_type;
6125 /* We used the operator token. */
6126 cp_lexer_consume_token (parser->lexer);
6128 /* Extract another operand. It may be the RHS of this expression
6129 or the LHS of a new, higher priority expression. */
6130 rhs = cp_parser_simple_cast_expression (parser);
6131 rhs_type = ERROR_MARK;
6133 /* Get another operator token. Look up its precedence to avoid
6134 building a useless (immediately popped) stack entry for common
6135 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6136 token = cp_lexer_peek_token (parser->lexer);
6137 lookahead_prec = TOKEN_PRECEDENCE (token);
6138 if (lookahead_prec > new_prec)
6140 /* ... and prepare to parse the RHS of the new, higher priority
6141 expression. Since precedence levels on the stack are
6142 monotonically increasing, we do not have to care about
6145 sp->tree_type = tree_type;
6147 sp->lhs_type = lhs_type;
6150 lhs_type = rhs_type;
6152 new_prec = lookahead_prec;
6156 /* If the stack is not empty, we have parsed into LHS the right side
6157 (`4' in the example above) of an expression we had suspended.
6158 We can use the information on the stack to recover the LHS (`3')
6159 from the stack together with the tree code (`MULT_EXPR'), and
6160 the precedence of the higher level subexpression
6161 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6162 which will be used to actually build the additive expression. */
6165 tree_type = sp->tree_type;
6167 rhs_type = lhs_type;
6169 lhs_type = sp->lhs_type;
6172 overloaded_p = false;
6173 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6174 &overloaded_p, tf_warning_or_error);
6175 lhs_type = tree_type;
6177 /* If the binary operator required the use of an overloaded operator,
6178 then this expression cannot be an integral constant-expression.
6179 An overloaded operator can be used even if both operands are
6180 otherwise permissible in an integral constant-expression if at
6181 least one of the operands is of enumeration type. */
6184 && (cp_parser_non_integral_constant_expression
6185 (parser, "calls to overloaded operators")))
6186 return error_mark_node;
6193 /* Parse the `? expression : assignment-expression' part of a
6194 conditional-expression. The LOGICAL_OR_EXPR is the
6195 logical-or-expression that started the conditional-expression.
6196 Returns a representation of the entire conditional-expression.
6198 This routine is used by cp_parser_assignment_expression.
6200 ? expression : assignment-expression
6204 ? : assignment-expression */
6207 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6210 tree assignment_expr;
6212 /* Consume the `?' token. */
6213 cp_lexer_consume_token (parser->lexer);
6214 if (cp_parser_allow_gnu_extensions_p (parser)
6215 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6216 /* Implicit true clause. */
6219 /* Parse the expression. */
6220 expr = cp_parser_expression (parser, /*cast_p=*/false);
6222 /* The next token should be a `:'. */
6223 cp_parser_require (parser, CPP_COLON, "%<:%>");
6224 /* Parse the assignment-expression. */
6225 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6227 /* Build the conditional-expression. */
6228 return build_x_conditional_expr (logical_or_expr,
6231 tf_warning_or_error);
6234 /* Parse an assignment-expression.
6236 assignment-expression:
6237 conditional-expression
6238 logical-or-expression assignment-operator assignment_expression
6241 CAST_P is true if this expression is the target of a cast.
6243 Returns a representation for the expression. */
6246 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6250 /* If the next token is the `throw' keyword, then we're looking at
6251 a throw-expression. */
6252 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6253 expr = cp_parser_throw_expression (parser);
6254 /* Otherwise, it must be that we are looking at a
6255 logical-or-expression. */
6258 /* Parse the binary expressions (logical-or-expression). */
6259 expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6260 /* If the next token is a `?' then we're actually looking at a
6261 conditional-expression. */
6262 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6263 return cp_parser_question_colon_clause (parser, expr);
6266 enum tree_code assignment_operator;
6268 /* If it's an assignment-operator, we're using the second
6271 = cp_parser_assignment_operator_opt (parser);
6272 if (assignment_operator != ERROR_MARK)
6274 bool non_constant_p;
6276 /* Parse the right-hand side of the assignment. */
6277 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6279 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6280 maybe_warn_cpp0x ("extended initializer lists");
6282 /* An assignment may not appear in a
6283 constant-expression. */
6284 if (cp_parser_non_integral_constant_expression (parser,
6286 return error_mark_node;
6287 /* Build the assignment expression. */
6288 expr = build_x_modify_expr (expr,
6289 assignment_operator,
6291 tf_warning_or_error);
6299 /* Parse an (optional) assignment-operator.
6301 assignment-operator: one of
6302 = *= /= %= += -= >>= <<= &= ^= |=
6306 assignment-operator: one of
6309 If the next token is an assignment operator, the corresponding tree
6310 code is returned, and the token is consumed. For example, for
6311 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6312 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6313 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6314 operator, ERROR_MARK is returned. */
6316 static enum tree_code
6317 cp_parser_assignment_operator_opt (cp_parser* parser)
6322 /* Peek at the next token. */
6323 token = cp_lexer_peek_token (parser->lexer);
6325 switch (token->type)
6336 op = TRUNC_DIV_EXPR;
6340 op = TRUNC_MOD_EXPR;
6372 /* Nothing else is an assignment operator. */
6376 /* If it was an assignment operator, consume it. */
6377 if (op != ERROR_MARK)
6378 cp_lexer_consume_token (parser->lexer);
6383 /* Parse an expression.
6386 assignment-expression
6387 expression , assignment-expression
6389 CAST_P is true if this expression is the target of a cast.
6391 Returns a representation of the expression. */
6394 cp_parser_expression (cp_parser* parser, bool cast_p)
6396 tree expression = NULL_TREE;
6400 tree assignment_expression;
6402 /* Parse the next assignment-expression. */
6403 assignment_expression
6404 = cp_parser_assignment_expression (parser, cast_p);
6405 /* If this is the first assignment-expression, we can just
6408 expression = assignment_expression;
6410 expression = build_x_compound_expr (expression,
6411 assignment_expression,
6412 tf_warning_or_error);
6413 /* If the next token is not a comma, then we are done with the
6415 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6417 /* Consume the `,'. */
6418 cp_lexer_consume_token (parser->lexer);
6419 /* A comma operator cannot appear in a constant-expression. */
6420 if (cp_parser_non_integral_constant_expression (parser,
6421 "a comma operator"))
6422 expression = error_mark_node;
6428 /* Parse a constant-expression.
6430 constant-expression:
6431 conditional-expression
6433 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6434 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6435 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6436 is false, NON_CONSTANT_P should be NULL. */
6439 cp_parser_constant_expression (cp_parser* parser,
6440 bool allow_non_constant_p,
6441 bool *non_constant_p)
6443 bool saved_integral_constant_expression_p;
6444 bool saved_allow_non_integral_constant_expression_p;
6445 bool saved_non_integral_constant_expression_p;
6448 /* It might seem that we could simply parse the
6449 conditional-expression, and then check to see if it were
6450 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6451 one that the compiler can figure out is constant, possibly after
6452 doing some simplifications or optimizations. The standard has a
6453 precise definition of constant-expression, and we must honor
6454 that, even though it is somewhat more restrictive.
6460 is not a legal declaration, because `(2, 3)' is not a
6461 constant-expression. The `,' operator is forbidden in a
6462 constant-expression. However, GCC's constant-folding machinery
6463 will fold this operation to an INTEGER_CST for `3'. */
6465 /* Save the old settings. */
6466 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6467 saved_allow_non_integral_constant_expression_p
6468 = parser->allow_non_integral_constant_expression_p;
6469 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6470 /* We are now parsing a constant-expression. */
6471 parser->integral_constant_expression_p = true;
6472 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6473 parser->non_integral_constant_expression_p = false;
6474 /* Although the grammar says "conditional-expression", we parse an
6475 "assignment-expression", which also permits "throw-expression"
6476 and the use of assignment operators. In the case that
6477 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6478 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6479 actually essential that we look for an assignment-expression.
6480 For example, cp_parser_initializer_clauses uses this function to
6481 determine whether a particular assignment-expression is in fact
6483 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6484 /* Restore the old settings. */
6485 parser->integral_constant_expression_p
6486 = saved_integral_constant_expression_p;
6487 parser->allow_non_integral_constant_expression_p
6488 = saved_allow_non_integral_constant_expression_p;
6489 if (allow_non_constant_p)
6490 *non_constant_p = parser->non_integral_constant_expression_p;
6491 else if (parser->non_integral_constant_expression_p)
6492 expression = error_mark_node;
6493 parser->non_integral_constant_expression_p
6494 = saved_non_integral_constant_expression_p;
6499 /* Parse __builtin_offsetof.
6501 offsetof-expression:
6502 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6504 offsetof-member-designator:
6506 | offsetof-member-designator "." id-expression
6507 | offsetof-member-designator "[" expression "]" */
6510 cp_parser_builtin_offsetof (cp_parser *parser)
6512 int save_ice_p, save_non_ice_p;
6517 /* We're about to accept non-integral-constant things, but will
6518 definitely yield an integral constant expression. Save and
6519 restore these values around our local parsing. */
6520 save_ice_p = parser->integral_constant_expression_p;
6521 save_non_ice_p = parser->non_integral_constant_expression_p;
6523 /* Consume the "__builtin_offsetof" token. */
6524 cp_lexer_consume_token (parser->lexer);
6525 /* Consume the opening `('. */
6526 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6527 /* Parse the type-id. */
6528 type = cp_parser_type_id (parser);
6529 /* Look for the `,'. */
6530 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6531 token = cp_lexer_peek_token (parser->lexer);
6533 /* Build the (type *)null that begins the traditional offsetof macro. */
6534 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6535 tf_warning_or_error);
6537 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6538 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6539 true, &dummy, token->location);
6542 token = cp_lexer_peek_token (parser->lexer);
6543 switch (token->type)
6545 case CPP_OPEN_SQUARE:
6546 /* offsetof-member-designator "[" expression "]" */
6547 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6551 /* offsetof-member-designator "." identifier */
6552 cp_lexer_consume_token (parser->lexer);
6553 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6558 case CPP_CLOSE_PAREN:
6559 /* Consume the ")" token. */
6560 cp_lexer_consume_token (parser->lexer);
6564 /* Error. We know the following require will fail, but
6565 that gives the proper error message. */
6566 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6567 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6568 expr = error_mark_node;
6574 /* If we're processing a template, we can't finish the semantics yet.
6575 Otherwise we can fold the entire expression now. */
6576 if (processing_template_decl)
6577 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6579 expr = finish_offsetof (expr);
6582 parser->integral_constant_expression_p = save_ice_p;
6583 parser->non_integral_constant_expression_p = save_non_ice_p;
6588 /* Parse a trait expression. */
6591 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6594 tree type1, type2 = NULL_TREE;
6595 bool binary = false;
6596 cp_decl_specifier_seq decl_specs;
6600 case RID_HAS_NOTHROW_ASSIGN:
6601 kind = CPTK_HAS_NOTHROW_ASSIGN;
6603 case RID_HAS_NOTHROW_CONSTRUCTOR:
6604 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6606 case RID_HAS_NOTHROW_COPY:
6607 kind = CPTK_HAS_NOTHROW_COPY;
6609 case RID_HAS_TRIVIAL_ASSIGN:
6610 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6612 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6613 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6615 case RID_HAS_TRIVIAL_COPY:
6616 kind = CPTK_HAS_TRIVIAL_COPY;
6618 case RID_HAS_TRIVIAL_DESTRUCTOR:
6619 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6621 case RID_HAS_VIRTUAL_DESTRUCTOR:
6622 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6624 case RID_IS_ABSTRACT:
6625 kind = CPTK_IS_ABSTRACT;
6627 case RID_IS_BASE_OF:
6628 kind = CPTK_IS_BASE_OF;
6632 kind = CPTK_IS_CLASS;
6634 case RID_IS_CONVERTIBLE_TO:
6635 kind = CPTK_IS_CONVERTIBLE_TO;
6639 kind = CPTK_IS_EMPTY;
6642 kind = CPTK_IS_ENUM;
6647 case RID_IS_POLYMORPHIC:
6648 kind = CPTK_IS_POLYMORPHIC;
6651 kind = CPTK_IS_UNION;
6657 /* Consume the token. */
6658 cp_lexer_consume_token (parser->lexer);
6660 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6662 type1 = cp_parser_type_id (parser);
6664 if (type1 == error_mark_node)
6665 return error_mark_node;
6667 /* Build a trivial decl-specifier-seq. */
6668 clear_decl_specs (&decl_specs);
6669 decl_specs.type = type1;
6671 /* Call grokdeclarator to figure out what type this is. */
6672 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6673 /*initialized=*/0, /*attrlist=*/NULL);
6677 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6679 type2 = cp_parser_type_id (parser);
6681 if (type2 == error_mark_node)
6682 return error_mark_node;
6684 /* Build a trivial decl-specifier-seq. */
6685 clear_decl_specs (&decl_specs);
6686 decl_specs.type = type2;
6688 /* Call grokdeclarator to figure out what type this is. */
6689 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6690 /*initialized=*/0, /*attrlist=*/NULL);
6693 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6695 /* Complete the trait expression, which may mean either processing
6696 the trait expr now or saving it for template instantiation. */
6697 return finish_trait_expr (kind, type1, type2);
6700 /* Statements [gram.stmt.stmt] */
6702 /* Parse a statement.
6706 expression-statement
6711 declaration-statement
6714 IN_COMPOUND is true when the statement is nested inside a
6715 cp_parser_compound_statement; this matters for certain pragmas.
6717 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6718 is a (possibly labeled) if statement which is not enclosed in braces
6719 and has an else clause. This is used to implement -Wparentheses. */
6722 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6723 bool in_compound, bool *if_p)
6727 location_t statement_location;
6732 /* There is no statement yet. */
6733 statement = NULL_TREE;
6734 /* Peek at the next token. */
6735 token = cp_lexer_peek_token (parser->lexer);
6736 /* Remember the location of the first token in the statement. */
6737 statement_location = token->location;
6738 /* If this is a keyword, then that will often determine what kind of
6739 statement we have. */
6740 if (token->type == CPP_KEYWORD)
6742 enum rid keyword = token->keyword;
6748 /* Looks like a labeled-statement with a case label.
6749 Parse the label, and then use tail recursion to parse
6751 cp_parser_label_for_labeled_statement (parser);
6756 statement = cp_parser_selection_statement (parser, if_p);
6762 statement = cp_parser_iteration_statement (parser);
6769 statement = cp_parser_jump_statement (parser);
6772 /* Objective-C++ exception-handling constructs. */
6775 case RID_AT_FINALLY:
6776 case RID_AT_SYNCHRONIZED:
6778 statement = cp_parser_objc_statement (parser);
6782 statement = cp_parser_try_block (parser);
6786 /* This must be a namespace alias definition. */
6787 cp_parser_declaration_statement (parser);
6791 /* It might be a keyword like `int' that can start a
6792 declaration-statement. */
6796 else if (token->type == CPP_NAME)
6798 /* If the next token is a `:', then we are looking at a
6799 labeled-statement. */
6800 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6801 if (token->type == CPP_COLON)
6803 /* Looks like a labeled-statement with an ordinary label.
6804 Parse the label, and then use tail recursion to parse
6806 cp_parser_label_for_labeled_statement (parser);
6810 /* Anything that starts with a `{' must be a compound-statement. */
6811 else if (token->type == CPP_OPEN_BRACE)
6812 statement = cp_parser_compound_statement (parser, NULL, false);
6813 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6814 a statement all its own. */
6815 else if (token->type == CPP_PRAGMA)
6817 /* Only certain OpenMP pragmas are attached to statements, and thus
6818 are considered statements themselves. All others are not. In
6819 the context of a compound, accept the pragma as a "statement" and
6820 return so that we can check for a close brace. Otherwise we
6821 require a real statement and must go back and read one. */
6823 cp_parser_pragma (parser, pragma_compound);
6824 else if (!cp_parser_pragma (parser, pragma_stmt))
6828 else if (token->type == CPP_EOF)
6830 cp_parser_error (parser, "expected statement");
6834 /* Everything else must be a declaration-statement or an
6835 expression-statement. Try for the declaration-statement
6836 first, unless we are looking at a `;', in which case we know that
6837 we have an expression-statement. */
6840 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6842 cp_parser_parse_tentatively (parser);
6843 /* Try to parse the declaration-statement. */
6844 cp_parser_declaration_statement (parser);
6845 /* If that worked, we're done. */
6846 if (cp_parser_parse_definitely (parser))
6849 /* Look for an expression-statement instead. */
6850 statement = cp_parser_expression_statement (parser, in_statement_expr);
6853 /* Set the line number for the statement. */
6854 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6855 SET_EXPR_LOCATION (statement, statement_location);
6858 /* Parse the label for a labeled-statement, i.e.
6861 case constant-expression :
6865 case constant-expression ... constant-expression : statement
6867 When a label is parsed without errors, the label is added to the
6868 parse tree by the finish_* functions, so this function doesn't
6869 have to return the label. */
6872 cp_parser_label_for_labeled_statement (cp_parser* parser)
6876 /* The next token should be an identifier. */
6877 token = cp_lexer_peek_token (parser->lexer);
6878 if (token->type != CPP_NAME
6879 && token->type != CPP_KEYWORD)
6881 cp_parser_error (parser, "expected labeled-statement");
6885 switch (token->keyword)
6892 /* Consume the `case' token. */
6893 cp_lexer_consume_token (parser->lexer);
6894 /* Parse the constant-expression. */
6895 expr = cp_parser_constant_expression (parser,
6896 /*allow_non_constant_p=*/false,
6899 ellipsis = cp_lexer_peek_token (parser->lexer);
6900 if (ellipsis->type == CPP_ELLIPSIS)
6902 /* Consume the `...' token. */
6903 cp_lexer_consume_token (parser->lexer);
6905 cp_parser_constant_expression (parser,
6906 /*allow_non_constant_p=*/false,
6908 /* We don't need to emit warnings here, as the common code
6909 will do this for us. */
6912 expr_hi = NULL_TREE;
6914 if (parser->in_switch_statement_p)
6915 finish_case_label (expr, expr_hi);
6917 error ("%Hcase label %qE not within a switch statement",
6918 &token->location, expr);
6923 /* Consume the `default' token. */
6924 cp_lexer_consume_token (parser->lexer);
6926 if (parser->in_switch_statement_p)
6927 finish_case_label (NULL_TREE, NULL_TREE);
6929 error ("%Hcase label not within a switch statement", &token->location);
6933 /* Anything else must be an ordinary label. */
6934 finish_label_stmt (cp_parser_identifier (parser));
6938 /* Require the `:' token. */
6939 cp_parser_require (parser, CPP_COLON, "%<:%>");
6942 /* Parse an expression-statement.
6944 expression-statement:
6947 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6948 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6949 indicates whether this expression-statement is part of an
6950 expression statement. */
6953 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6955 tree statement = NULL_TREE;
6957 /* If the next token is a ';', then there is no expression
6959 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6960 statement = cp_parser_expression (parser, /*cast_p=*/false);
6962 /* Consume the final `;'. */
6963 cp_parser_consume_semicolon_at_end_of_statement (parser);
6965 if (in_statement_expr
6966 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6967 /* This is the final expression statement of a statement
6969 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6971 statement = finish_expr_stmt (statement);
6978 /* Parse a compound-statement.
6981 { statement-seq [opt] }
6986 { label-declaration-seq [opt] statement-seq [opt] }
6988 label-declaration-seq:
6990 label-declaration-seq label-declaration
6992 Returns a tree representing the statement. */
6995 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7000 /* Consume the `{'. */
7001 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7002 return error_mark_node;
7003 /* Begin the compound-statement. */
7004 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7005 /* If the next keyword is `__label__' we have a label declaration. */
7006 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7007 cp_parser_label_declaration (parser);
7008 /* Parse an (optional) statement-seq. */
7009 cp_parser_statement_seq_opt (parser, in_statement_expr);
7010 /* Finish the compound-statement. */
7011 finish_compound_stmt (compound_stmt);
7012 /* Consume the `}'. */
7013 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7015 return compound_stmt;
7018 /* Parse an (optional) statement-seq.
7022 statement-seq [opt] statement */
7025 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7027 /* Scan statements until there aren't any more. */
7030 cp_token *token = cp_lexer_peek_token (parser->lexer);
7032 /* If we're looking at a `}', then we've run out of statements. */
7033 if (token->type == CPP_CLOSE_BRACE
7034 || token->type == CPP_EOF
7035 || token->type == CPP_PRAGMA_EOL)
7038 /* If we are in a compound statement and find 'else' then
7039 something went wrong. */
7040 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7042 if (parser->in_statement & IN_IF_STMT)
7046 token = cp_lexer_consume_token (parser->lexer);
7047 error ("%H%<else%> without a previous %<if%>", &token->location);
7051 /* Parse the statement. */
7052 cp_parser_statement (parser, in_statement_expr, true, NULL);
7056 /* Parse a selection-statement.
7058 selection-statement:
7059 if ( condition ) statement
7060 if ( condition ) statement else statement
7061 switch ( condition ) statement
7063 Returns the new IF_STMT or SWITCH_STMT.
7065 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7066 is a (possibly labeled) if statement which is not enclosed in
7067 braces and has an else clause. This is used to implement
7071 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7079 /* Peek at the next token. */
7080 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7082 /* See what kind of keyword it is. */
7083 keyword = token->keyword;
7092 /* Look for the `('. */
7093 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7095 cp_parser_skip_to_end_of_statement (parser);
7096 return error_mark_node;
7099 /* Begin the selection-statement. */
7100 if (keyword == RID_IF)
7101 statement = begin_if_stmt ();
7103 statement = begin_switch_stmt ();
7105 /* Parse the condition. */
7106 condition = cp_parser_condition (parser);
7107 /* Look for the `)'. */
7108 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7109 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7110 /*consume_paren=*/true);
7112 if (keyword == RID_IF)
7115 unsigned char in_statement;
7117 /* Add the condition. */
7118 finish_if_stmt_cond (condition, statement);
7120 /* Parse the then-clause. */
7121 in_statement = parser->in_statement;
7122 parser->in_statement |= IN_IF_STMT;
7123 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7124 parser->in_statement = in_statement;
7126 finish_then_clause (statement);
7128 /* If the next token is `else', parse the else-clause. */
7129 if (cp_lexer_next_token_is_keyword (parser->lexer,
7132 /* Consume the `else' keyword. */
7133 cp_lexer_consume_token (parser->lexer);
7134 begin_else_clause (statement);
7135 /* Parse the else-clause. */
7136 cp_parser_implicitly_scoped_statement (parser, NULL);
7137 finish_else_clause (statement);
7139 /* If we are currently parsing a then-clause, then
7140 IF_P will not be NULL. We set it to true to
7141 indicate that this if statement has an else clause.
7142 This may trigger the Wparentheses warning below
7143 when we get back up to the parent if statement. */
7149 /* This if statement does not have an else clause. If
7150 NESTED_IF is true, then the then-clause is an if
7151 statement which does have an else clause. We warn
7152 about the potential ambiguity. */
7154 warning (OPT_Wparentheses,
7155 ("%Hsuggest explicit braces "
7156 "to avoid ambiguous %<else%>"),
7157 EXPR_LOCUS (statement));
7160 /* Now we're all done with the if-statement. */
7161 finish_if_stmt (statement);
7165 bool in_switch_statement_p;
7166 unsigned char in_statement;
7168 /* Add the condition. */
7169 finish_switch_cond (condition, statement);
7171 /* Parse the body of the switch-statement. */
7172 in_switch_statement_p = parser->in_switch_statement_p;
7173 in_statement = parser->in_statement;
7174 parser->in_switch_statement_p = true;
7175 parser->in_statement |= IN_SWITCH_STMT;
7176 cp_parser_implicitly_scoped_statement (parser, NULL);
7177 parser->in_switch_statement_p = in_switch_statement_p;
7178 parser->in_statement = in_statement;
7180 /* Now we're all done with the switch-statement. */
7181 finish_switch_stmt (statement);
7189 cp_parser_error (parser, "expected selection-statement");
7190 return error_mark_node;
7194 /* Parse a condition.
7198 type-specifier-seq declarator = initializer-clause
7199 type-specifier-seq declarator braced-init-list
7204 type-specifier-seq declarator asm-specification [opt]
7205 attributes [opt] = assignment-expression
7207 Returns the expression that should be tested. */
7210 cp_parser_condition (cp_parser* parser)
7212 cp_decl_specifier_seq type_specifiers;
7213 const char *saved_message;
7215 /* Try the declaration first. */
7216 cp_parser_parse_tentatively (parser);
7217 /* New types are not allowed in the type-specifier-seq for a
7219 saved_message = parser->type_definition_forbidden_message;
7220 parser->type_definition_forbidden_message
7221 = "types may not be defined in conditions";
7222 /* Parse the type-specifier-seq. */
7223 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7225 /* Restore the saved message. */
7226 parser->type_definition_forbidden_message = saved_message;
7227 /* If all is well, we might be looking at a declaration. */
7228 if (!cp_parser_error_occurred (parser))
7231 tree asm_specification;
7233 cp_declarator *declarator;
7234 tree initializer = NULL_TREE;
7236 /* Parse the declarator. */
7237 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7238 /*ctor_dtor_or_conv_p=*/NULL,
7239 /*parenthesized_p=*/NULL,
7240 /*member_p=*/false);
7241 /* Parse the attributes. */
7242 attributes = cp_parser_attributes_opt (parser);
7243 /* Parse the asm-specification. */
7244 asm_specification = cp_parser_asm_specification_opt (parser);
7245 /* If the next token is not an `=' or '{', then we might still be
7246 looking at an expression. For example:
7250 looks like a decl-specifier-seq and a declarator -- but then
7251 there is no `=', so this is an expression. */
7252 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7253 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7254 cp_parser_simulate_error (parser);
7256 /* If we did see an `=' or '{', then we are looking at a declaration
7258 if (cp_parser_parse_definitely (parser))
7261 bool non_constant_p;
7262 bool flags = LOOKUP_ONLYCONVERTING;
7264 /* Create the declaration. */
7265 decl = start_decl (declarator, &type_specifiers,
7266 /*initialized_p=*/true,
7267 attributes, /*prefix_attributes=*/NULL_TREE,
7270 /* Parse the initializer. */
7271 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7273 initializer = cp_parser_braced_list (parser, &non_constant_p);
7274 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7279 /* Consume the `='. */
7280 cp_lexer_consume_token (parser->lexer);
7281 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7283 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7284 maybe_warn_cpp0x ("extended initializer lists");
7286 if (!non_constant_p)
7287 initializer = fold_non_dependent_expr (initializer);
7289 /* Process the initializer. */
7290 cp_finish_decl (decl,
7291 initializer, !non_constant_p,
7296 pop_scope (pushed_scope);
7298 return convert_from_reference (decl);
7301 /* If we didn't even get past the declarator successfully, we are
7302 definitely not looking at a declaration. */
7304 cp_parser_abort_tentative_parse (parser);
7306 /* Otherwise, we are looking at an expression. */
7307 return cp_parser_expression (parser, /*cast_p=*/false);
7310 /* We check for a ) immediately followed by ; with no whitespacing
7311 between. This is used to issue a warning for:
7319 as the semicolon is probably extraneous.
7321 On parse errors, the next token might not be a ), so do nothing in
7325 check_empty_body (cp_parser* parser, const char* type)
7328 cp_token *close_paren;
7329 expanded_location close_loc;
7330 expanded_location semi_loc;
7332 close_paren = cp_lexer_peek_token (parser->lexer);
7333 if (close_paren->type != CPP_CLOSE_PAREN)
7336 close_loc = expand_location (close_paren->location);
7337 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7339 if (token->type != CPP_SEMICOLON
7340 || (token->flags & PREV_WHITE))
7343 semi_loc = expand_location (token->location);
7344 if (close_loc.line == semi_loc.line
7345 && close_loc.column+1 == semi_loc.column)
7346 warning (OPT_Wempty_body,
7347 "suggest a space before %<;%> or explicit braces around empty "
7348 "body in %<%s%> statement",
7352 /* Parse an iteration-statement.
7354 iteration-statement:
7355 while ( condition ) statement
7356 do statement while ( expression ) ;
7357 for ( for-init-statement condition [opt] ; expression [opt] )
7360 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7363 cp_parser_iteration_statement (cp_parser* parser)
7368 unsigned char in_statement;
7370 /* Peek at the next token. */
7371 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7373 return error_mark_node;
7375 /* Remember whether or not we are already within an iteration
7377 in_statement = parser->in_statement;
7379 /* See what kind of keyword it is. */
7380 keyword = token->keyword;
7387 /* Begin the while-statement. */
7388 statement = begin_while_stmt ();
7389 /* Look for the `('. */
7390 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7391 /* Parse the condition. */
7392 condition = cp_parser_condition (parser);
7393 finish_while_stmt_cond (condition, statement);
7394 check_empty_body (parser, "while");
7395 /* Look for the `)'. */
7396 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7397 /* Parse the dependent statement. */
7398 parser->in_statement = IN_ITERATION_STMT;
7399 cp_parser_already_scoped_statement (parser);
7400 parser->in_statement = in_statement;
7401 /* We're done with the while-statement. */
7402 finish_while_stmt (statement);
7410 /* Begin the do-statement. */
7411 statement = begin_do_stmt ();
7412 /* Parse the body of the do-statement. */
7413 parser->in_statement = IN_ITERATION_STMT;
7414 cp_parser_implicitly_scoped_statement (parser, NULL);
7415 parser->in_statement = in_statement;
7416 finish_do_body (statement);
7417 /* Look for the `while' keyword. */
7418 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7419 /* Look for the `('. */
7420 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7421 /* Parse the expression. */
7422 expression = cp_parser_expression (parser, /*cast_p=*/false);
7423 /* We're done with the do-statement. */
7424 finish_do_stmt (expression, statement);
7425 /* Look for the `)'. */
7426 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7427 /* Look for the `;'. */
7428 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7434 tree condition = NULL_TREE;
7435 tree expression = NULL_TREE;
7437 /* Begin the for-statement. */
7438 statement = begin_for_stmt ();
7439 /* Look for the `('. */
7440 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7441 /* Parse the initialization. */
7442 cp_parser_for_init_statement (parser);
7443 finish_for_init_stmt (statement);
7445 /* If there's a condition, process it. */
7446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7447 condition = cp_parser_condition (parser);
7448 finish_for_cond (condition, statement);
7449 /* Look for the `;'. */
7450 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7452 /* If there's an expression, process it. */
7453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7454 expression = cp_parser_expression (parser, /*cast_p=*/false);
7455 finish_for_expr (expression, statement);
7456 check_empty_body (parser, "for");
7457 /* Look for the `)'. */
7458 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7460 /* Parse the body of the for-statement. */
7461 parser->in_statement = IN_ITERATION_STMT;
7462 cp_parser_already_scoped_statement (parser);
7463 parser->in_statement = in_statement;
7465 /* We're done with the for-statement. */
7466 finish_for_stmt (statement);
7471 cp_parser_error (parser, "expected iteration-statement");
7472 statement = error_mark_node;
7479 /* Parse a for-init-statement.
7482 expression-statement
7483 simple-declaration */
7486 cp_parser_for_init_statement (cp_parser* parser)
7488 /* If the next token is a `;', then we have an empty
7489 expression-statement. Grammatically, this is also a
7490 simple-declaration, but an invalid one, because it does not
7491 declare anything. Therefore, if we did not handle this case
7492 specially, we would issue an error message about an invalid
7494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7496 /* We're going to speculatively look for a declaration, falling back
7497 to an expression, if necessary. */
7498 cp_parser_parse_tentatively (parser);
7499 /* Parse the declaration. */
7500 cp_parser_simple_declaration (parser,
7501 /*function_definition_allowed_p=*/false);
7502 /* If the tentative parse failed, then we shall need to look for an
7503 expression-statement. */
7504 if (cp_parser_parse_definitely (parser))
7508 cp_parser_expression_statement (parser, false);
7511 /* Parse a jump-statement.
7516 return expression [opt] ;
7517 return braced-init-list ;
7525 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7528 cp_parser_jump_statement (cp_parser* parser)
7530 tree statement = error_mark_node;
7533 unsigned char in_statement;
7535 /* Peek at the next token. */
7536 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7538 return error_mark_node;
7540 /* See what kind of keyword it is. */
7541 keyword = token->keyword;
7545 in_statement = parser->in_statement & ~IN_IF_STMT;
7546 switch (in_statement)
7549 error ("%Hbreak statement not within loop or switch", &token->location);
7552 gcc_assert ((in_statement & IN_SWITCH_STMT)
7553 || in_statement == IN_ITERATION_STMT);
7554 statement = finish_break_stmt ();
7557 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7560 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7563 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7567 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7570 error ("%Hcontinue statement not within a loop", &token->location);
7572 case IN_ITERATION_STMT:
7574 statement = finish_continue_stmt ();
7577 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7582 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7588 bool expr_non_constant_p;
7590 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7592 maybe_warn_cpp0x ("extended initializer lists");
7593 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7595 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7596 expr = cp_parser_expression (parser, /*cast_p=*/false);
7598 /* If the next token is a `;', then there is no
7601 /* Build the return-statement. */
7602 statement = finish_return_stmt (expr);
7603 /* Look for the final `;'. */
7604 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7609 /* Create the goto-statement. */
7610 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7612 /* Issue a warning about this use of a GNU extension. */
7613 pedwarn (OPT_pedantic, "%HISO C++ forbids computed gotos", &token->location);
7614 /* Consume the '*' token. */
7615 cp_lexer_consume_token (parser->lexer);
7616 /* Parse the dependent expression. */
7617 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7620 finish_goto_stmt (cp_parser_identifier (parser));
7621 /* Look for the final `;'. */
7622 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7626 cp_parser_error (parser, "expected jump-statement");
7633 /* Parse a declaration-statement.
7635 declaration-statement:
7636 block-declaration */
7639 cp_parser_declaration_statement (cp_parser* parser)
7643 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7644 p = obstack_alloc (&declarator_obstack, 0);
7646 /* Parse the block-declaration. */
7647 cp_parser_block_declaration (parser, /*statement_p=*/true);
7649 /* Free any declarators allocated. */
7650 obstack_free (&declarator_obstack, p);
7652 /* Finish off the statement. */
7656 /* Some dependent statements (like `if (cond) statement'), are
7657 implicitly in their own scope. In other words, if the statement is
7658 a single statement (as opposed to a compound-statement), it is
7659 none-the-less treated as if it were enclosed in braces. Any
7660 declarations appearing in the dependent statement are out of scope
7661 after control passes that point. This function parses a statement,
7662 but ensures that is in its own scope, even if it is not a
7665 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7666 is a (possibly labeled) if statement which is not enclosed in
7667 braces and has an else clause. This is used to implement
7670 Returns the new statement. */
7673 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7680 /* Mark if () ; with a special NOP_EXPR. */
7681 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7683 cp_lexer_consume_token (parser->lexer);
7684 statement = add_stmt (build_empty_stmt ());
7686 /* if a compound is opened, we simply parse the statement directly. */
7687 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7688 statement = cp_parser_compound_statement (parser, NULL, false);
7689 /* If the token is not a `{', then we must take special action. */
7692 /* Create a compound-statement. */
7693 statement = begin_compound_stmt (0);
7694 /* Parse the dependent-statement. */
7695 cp_parser_statement (parser, NULL_TREE, false, if_p);
7696 /* Finish the dummy compound-statement. */
7697 finish_compound_stmt (statement);
7700 /* Return the statement. */
7704 /* For some dependent statements (like `while (cond) statement'), we
7705 have already created a scope. Therefore, even if the dependent
7706 statement is a compound-statement, we do not want to create another
7710 cp_parser_already_scoped_statement (cp_parser* parser)
7712 /* If the token is a `{', then we must take special action. */
7713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7714 cp_parser_statement (parser, NULL_TREE, false, NULL);
7717 /* Avoid calling cp_parser_compound_statement, so that we
7718 don't create a new scope. Do everything else by hand. */
7719 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7720 cp_parser_statement_seq_opt (parser, NULL_TREE);
7721 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7725 /* Declarations [gram.dcl.dcl] */
7727 /* Parse an optional declaration-sequence.
7731 declaration-seq declaration */
7734 cp_parser_declaration_seq_opt (cp_parser* parser)
7740 token = cp_lexer_peek_token (parser->lexer);
7742 if (token->type == CPP_CLOSE_BRACE
7743 || token->type == CPP_EOF
7744 || token->type == CPP_PRAGMA_EOL)
7747 if (token->type == CPP_SEMICOLON)
7749 /* A declaration consisting of a single semicolon is
7750 invalid. Allow it unless we're being pedantic. */
7751 cp_lexer_consume_token (parser->lexer);
7752 if (!in_system_header)
7753 pedwarn (OPT_pedantic, "extra %<;%>");
7757 /* If we're entering or exiting a region that's implicitly
7758 extern "C", modify the lang context appropriately. */
7759 if (!parser->implicit_extern_c && token->implicit_extern_c)
7761 push_lang_context (lang_name_c);
7762 parser->implicit_extern_c = true;
7764 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7766 pop_lang_context ();
7767 parser->implicit_extern_c = false;
7770 if (token->type == CPP_PRAGMA)
7772 /* A top-level declaration can consist solely of a #pragma.
7773 A nested declaration cannot, so this is done here and not
7774 in cp_parser_declaration. (A #pragma at block scope is
7775 handled in cp_parser_statement.) */
7776 cp_parser_pragma (parser, pragma_external);
7780 /* Parse the declaration itself. */
7781 cp_parser_declaration (parser);
7785 /* Parse a declaration.
7790 template-declaration
7791 explicit-instantiation
7792 explicit-specialization
7793 linkage-specification
7794 namespace-definition
7799 __extension__ declaration */
7802 cp_parser_declaration (cp_parser* parser)
7809 /* Check for the `__extension__' keyword. */
7810 if (cp_parser_extension_opt (parser, &saved_pedantic))
7812 /* Parse the qualified declaration. */
7813 cp_parser_declaration (parser);
7814 /* Restore the PEDANTIC flag. */
7815 pedantic = saved_pedantic;
7820 /* Try to figure out what kind of declaration is present. */
7821 token1 = *cp_lexer_peek_token (parser->lexer);
7823 if (token1.type != CPP_EOF)
7824 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7827 token2.type = CPP_EOF;
7828 token2.keyword = RID_MAX;
7831 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7832 p = obstack_alloc (&declarator_obstack, 0);
7834 /* If the next token is `extern' and the following token is a string
7835 literal, then we have a linkage specification. */
7836 if (token1.keyword == RID_EXTERN
7837 && cp_parser_is_string_literal (&token2))
7838 cp_parser_linkage_specification (parser);
7839 /* If the next token is `template', then we have either a template
7840 declaration, an explicit instantiation, or an explicit
7842 else if (token1.keyword == RID_TEMPLATE)
7844 /* `template <>' indicates a template specialization. */
7845 if (token2.type == CPP_LESS
7846 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7847 cp_parser_explicit_specialization (parser);
7848 /* `template <' indicates a template declaration. */
7849 else if (token2.type == CPP_LESS)
7850 cp_parser_template_declaration (parser, /*member_p=*/false);
7851 /* Anything else must be an explicit instantiation. */
7853 cp_parser_explicit_instantiation (parser);
7855 /* If the next token is `export', then we have a template
7857 else if (token1.keyword == RID_EXPORT)
7858 cp_parser_template_declaration (parser, /*member_p=*/false);
7859 /* If the next token is `extern', 'static' or 'inline' and the one
7860 after that is `template', we have a GNU extended explicit
7861 instantiation directive. */
7862 else if (cp_parser_allow_gnu_extensions_p (parser)
7863 && (token1.keyword == RID_EXTERN
7864 || token1.keyword == RID_STATIC
7865 || token1.keyword == RID_INLINE)
7866 && token2.keyword == RID_TEMPLATE)
7867 cp_parser_explicit_instantiation (parser);
7868 /* If the next token is `namespace', check for a named or unnamed
7869 namespace definition. */
7870 else if (token1.keyword == RID_NAMESPACE
7871 && (/* A named namespace definition. */
7872 (token2.type == CPP_NAME
7873 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7875 /* An unnamed namespace definition. */
7876 || token2.type == CPP_OPEN_BRACE
7877 || token2.keyword == RID_ATTRIBUTE))
7878 cp_parser_namespace_definition (parser);
7879 /* An inline (associated) namespace definition. */
7880 else if (token1.keyword == RID_INLINE
7881 && token2.keyword == RID_NAMESPACE)
7882 cp_parser_namespace_definition (parser);
7883 /* Objective-C++ declaration/definition. */
7884 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7885 cp_parser_objc_declaration (parser);
7886 /* We must have either a block declaration or a function
7889 /* Try to parse a block-declaration, or a function-definition. */
7890 cp_parser_block_declaration (parser, /*statement_p=*/false);
7892 /* Free any declarators allocated. */
7893 obstack_free (&declarator_obstack, p);
7896 /* Parse a block-declaration.
7901 namespace-alias-definition
7908 __extension__ block-declaration
7913 static_assert-declaration
7915 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7916 part of a declaration-statement. */
7919 cp_parser_block_declaration (cp_parser *parser,
7925 /* Check for the `__extension__' keyword. */
7926 if (cp_parser_extension_opt (parser, &saved_pedantic))
7928 /* Parse the qualified declaration. */
7929 cp_parser_block_declaration (parser, statement_p);
7930 /* Restore the PEDANTIC flag. */
7931 pedantic = saved_pedantic;
7936 /* Peek at the next token to figure out which kind of declaration is
7938 token1 = cp_lexer_peek_token (parser->lexer);
7940 /* If the next keyword is `asm', we have an asm-definition. */
7941 if (token1->keyword == RID_ASM)
7944 cp_parser_commit_to_tentative_parse (parser);
7945 cp_parser_asm_definition (parser);
7947 /* If the next keyword is `namespace', we have a
7948 namespace-alias-definition. */
7949 else if (token1->keyword == RID_NAMESPACE)
7950 cp_parser_namespace_alias_definition (parser);
7951 /* If the next keyword is `using', we have either a
7952 using-declaration or a using-directive. */
7953 else if (token1->keyword == RID_USING)
7958 cp_parser_commit_to_tentative_parse (parser);
7959 /* If the token after `using' is `namespace', then we have a
7961 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7962 if (token2->keyword == RID_NAMESPACE)
7963 cp_parser_using_directive (parser);
7964 /* Otherwise, it's a using-declaration. */
7966 cp_parser_using_declaration (parser,
7967 /*access_declaration_p=*/false);
7969 /* If the next keyword is `__label__' we have a misplaced label
7971 else if (token1->keyword == RID_LABEL)
7973 cp_lexer_consume_token (parser->lexer);
7974 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
7975 cp_parser_skip_to_end_of_statement (parser);
7976 /* If the next token is now a `;', consume it. */
7977 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7978 cp_lexer_consume_token (parser->lexer);
7980 /* If the next token is `static_assert' we have a static assertion. */
7981 else if (token1->keyword == RID_STATIC_ASSERT)
7982 cp_parser_static_assert (parser, /*member_p=*/false);
7983 /* Anything else must be a simple-declaration. */
7985 cp_parser_simple_declaration (parser, !statement_p);
7988 /* Parse a simple-declaration.
7991 decl-specifier-seq [opt] init-declarator-list [opt] ;
7993 init-declarator-list:
7995 init-declarator-list , init-declarator
7997 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7998 function-definition as a simple-declaration. */
8001 cp_parser_simple_declaration (cp_parser* parser,
8002 bool function_definition_allowed_p)
8004 cp_decl_specifier_seq decl_specifiers;
8005 int declares_class_or_enum;
8006 bool saw_declarator;
8008 /* Defer access checks until we know what is being declared; the
8009 checks for names appearing in the decl-specifier-seq should be
8010 done as if we were in the scope of the thing being declared. */
8011 push_deferring_access_checks (dk_deferred);
8013 /* Parse the decl-specifier-seq. We have to keep track of whether
8014 or not the decl-specifier-seq declares a named class or
8015 enumeration type, since that is the only case in which the
8016 init-declarator-list is allowed to be empty.
8020 In a simple-declaration, the optional init-declarator-list can be
8021 omitted only when declaring a class or enumeration, that is when
8022 the decl-specifier-seq contains either a class-specifier, an
8023 elaborated-type-specifier, or an enum-specifier. */
8024 cp_parser_decl_specifier_seq (parser,
8025 CP_PARSER_FLAGS_OPTIONAL,
8027 &declares_class_or_enum);
8028 /* We no longer need to defer access checks. */
8029 stop_deferring_access_checks ();
8031 /* In a block scope, a valid declaration must always have a
8032 decl-specifier-seq. By not trying to parse declarators, we can
8033 resolve the declaration/expression ambiguity more quickly. */
8034 if (!function_definition_allowed_p
8035 && !decl_specifiers.any_specifiers_p)
8037 cp_parser_error (parser, "expected declaration");
8041 /* If the next two tokens are both identifiers, the code is
8042 erroneous. The usual cause of this situation is code like:
8046 where "T" should name a type -- but does not. */
8047 if (!decl_specifiers.type
8048 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8050 /* If parsing tentatively, we should commit; we really are
8051 looking at a declaration. */
8052 cp_parser_commit_to_tentative_parse (parser);
8057 /* If we have seen at least one decl-specifier, and the next token
8058 is not a parenthesis, then we must be looking at a declaration.
8059 (After "int (" we might be looking at a functional cast.) */
8060 if (decl_specifiers.any_specifiers_p
8061 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8062 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8063 cp_parser_commit_to_tentative_parse (parser);
8065 /* Keep going until we hit the `;' at the end of the simple
8067 saw_declarator = false;
8068 while (cp_lexer_next_token_is_not (parser->lexer,
8072 bool function_definition_p;
8077 /* If we are processing next declarator, coma is expected */
8078 token = cp_lexer_peek_token (parser->lexer);
8079 gcc_assert (token->type == CPP_COMMA);
8080 cp_lexer_consume_token (parser->lexer);
8083 saw_declarator = true;
8085 /* Parse the init-declarator. */
8086 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8088 function_definition_allowed_p,
8090 declares_class_or_enum,
8091 &function_definition_p);
8092 /* If an error occurred while parsing tentatively, exit quickly.
8093 (That usually happens when in the body of a function; each
8094 statement is treated as a declaration-statement until proven
8096 if (cp_parser_error_occurred (parser))
8098 /* Handle function definitions specially. */
8099 if (function_definition_p)
8101 /* If the next token is a `,', then we are probably
8102 processing something like:
8106 which is erroneous. */
8107 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8109 cp_token *token = cp_lexer_peek_token (parser->lexer);
8110 error ("%Hmixing declarations and function-definitions is forbidden",
8113 /* Otherwise, we're done with the list of declarators. */
8116 pop_deferring_access_checks ();
8120 /* The next token should be either a `,' or a `;'. */
8121 token = cp_lexer_peek_token (parser->lexer);
8122 /* If it's a `,', there are more declarators to come. */
8123 if (token->type == CPP_COMMA)
8124 /* will be consumed next time around */;
8125 /* If it's a `;', we are done. */
8126 else if (token->type == CPP_SEMICOLON)
8128 /* Anything else is an error. */
8131 /* If we have already issued an error message we don't need
8132 to issue another one. */
8133 if (decl != error_mark_node
8134 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8135 cp_parser_error (parser, "expected %<,%> or %<;%>");
8136 /* Skip tokens until we reach the end of the statement. */
8137 cp_parser_skip_to_end_of_statement (parser);
8138 /* If the next token is now a `;', consume it. */
8139 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8140 cp_lexer_consume_token (parser->lexer);
8143 /* After the first time around, a function-definition is not
8144 allowed -- even if it was OK at first. For example:
8149 function_definition_allowed_p = false;
8152 /* Issue an error message if no declarators are present, and the
8153 decl-specifier-seq does not itself declare a class or
8155 if (!saw_declarator)
8157 if (cp_parser_declares_only_class_p (parser))
8158 shadow_tag (&decl_specifiers);
8159 /* Perform any deferred access checks. */
8160 perform_deferred_access_checks ();
8163 /* Consume the `;'. */
8164 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8167 pop_deferring_access_checks ();
8170 /* Parse a decl-specifier-seq.
8173 decl-specifier-seq [opt] decl-specifier
8176 storage-class-specifier
8187 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8189 The parser flags FLAGS is used to control type-specifier parsing.
8191 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8194 1: one of the decl-specifiers is an elaborated-type-specifier
8195 (i.e., a type declaration)
8196 2: one of the decl-specifiers is an enum-specifier or a
8197 class-specifier (i.e., a type definition)
8202 cp_parser_decl_specifier_seq (cp_parser* parser,
8203 cp_parser_flags flags,
8204 cp_decl_specifier_seq *decl_specs,
8205 int* declares_class_or_enum)
8207 bool constructor_possible_p = !parser->in_declarator_p;
8208 cp_token *start_token = NULL;
8210 /* Clear DECL_SPECS. */
8211 clear_decl_specs (decl_specs);
8213 /* Assume no class or enumeration type is declared. */
8214 *declares_class_or_enum = 0;
8216 /* Keep reading specifiers until there are no more to read. */
8220 bool found_decl_spec;
8223 /* Peek at the next token. */
8224 token = cp_lexer_peek_token (parser->lexer);
8226 /* Save the first token of the decl spec list for error
8229 start_token = token;
8230 /* Handle attributes. */
8231 if (token->keyword == RID_ATTRIBUTE)
8233 /* Parse the attributes. */
8234 decl_specs->attributes
8235 = chainon (decl_specs->attributes,
8236 cp_parser_attributes_opt (parser));
8239 /* Assume we will find a decl-specifier keyword. */
8240 found_decl_spec = true;
8241 /* If the next token is an appropriate keyword, we can simply
8242 add it to the list. */
8243 switch (token->keyword)
8248 if (!at_class_scope_p ())
8250 error ("%H%<friend%> used outside of class", &token->location);
8251 cp_lexer_purge_token (parser->lexer);
8255 ++decl_specs->specs[(int) ds_friend];
8256 /* Consume the token. */
8257 cp_lexer_consume_token (parser->lexer);
8261 /* function-specifier:
8268 cp_parser_function_specifier_opt (parser, decl_specs);
8274 ++decl_specs->specs[(int) ds_typedef];
8275 /* Consume the token. */
8276 cp_lexer_consume_token (parser->lexer);
8277 /* A constructor declarator cannot appear in a typedef. */
8278 constructor_possible_p = false;
8279 /* The "typedef" keyword can only occur in a declaration; we
8280 may as well commit at this point. */
8281 cp_parser_commit_to_tentative_parse (parser);
8283 if (decl_specs->storage_class != sc_none)
8284 decl_specs->conflicting_specifiers_p = true;
8287 /* storage-class-specifier:
8297 /* Consume the token. */
8298 cp_lexer_consume_token (parser->lexer);
8300 if (cxx_dialect == cxx98)
8302 /* Complain about `auto' as a storage specifier, if
8303 we're complaining about C++0x compatibility. */
8306 "%H%<auto%> will change meaning in C++0x; please remove it",
8309 /* Set the storage class anyway. */
8310 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8314 /* We do not yet support the use of `auto' as a
8316 error ("%HC++0x %<auto%> specifier not supported", &token->location);
8323 /* Consume the token. */
8324 cp_lexer_consume_token (parser->lexer);
8325 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8329 /* Consume the token. */
8330 cp_lexer_consume_token (parser->lexer);
8331 ++decl_specs->specs[(int) ds_thread];
8335 /* We did not yet find a decl-specifier yet. */
8336 found_decl_spec = false;
8340 /* Constructors are a special case. The `S' in `S()' is not a
8341 decl-specifier; it is the beginning of the declarator. */
8344 && constructor_possible_p
8345 && (cp_parser_constructor_declarator_p
8346 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8348 /* If we don't have a DECL_SPEC yet, then we must be looking at
8349 a type-specifier. */
8350 if (!found_decl_spec && !constructor_p)
8352 int decl_spec_declares_class_or_enum;
8353 bool is_cv_qualifier;
8357 = cp_parser_type_specifier (parser, flags,
8359 /*is_declaration=*/true,
8360 &decl_spec_declares_class_or_enum,
8362 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8364 /* If this type-specifier referenced a user-defined type
8365 (a typedef, class-name, etc.), then we can't allow any
8366 more such type-specifiers henceforth.
8370 The longest sequence of decl-specifiers that could
8371 possibly be a type name is taken as the
8372 decl-specifier-seq of a declaration. The sequence shall
8373 be self-consistent as described below.
8377 As a general rule, at most one type-specifier is allowed
8378 in the complete decl-specifier-seq of a declaration. The
8379 only exceptions are the following:
8381 -- const or volatile can be combined with any other
8384 -- signed or unsigned can be combined with char, long,
8392 void g (const int Pc);
8394 Here, Pc is *not* part of the decl-specifier seq; it's
8395 the declarator. Therefore, once we see a type-specifier
8396 (other than a cv-qualifier), we forbid any additional
8397 user-defined types. We *do* still allow things like `int
8398 int' to be considered a decl-specifier-seq, and issue the
8399 error message later. */
8400 if (type_spec && !is_cv_qualifier)
8401 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8402 /* A constructor declarator cannot follow a type-specifier. */
8405 constructor_possible_p = false;
8406 found_decl_spec = true;
8410 /* If we still do not have a DECL_SPEC, then there are no more
8412 if (!found_decl_spec)
8415 decl_specs->any_specifiers_p = true;
8416 /* After we see one decl-specifier, further decl-specifiers are
8418 flags |= CP_PARSER_FLAGS_OPTIONAL;
8421 cp_parser_check_decl_spec (decl_specs, start_token->location);
8423 /* Don't allow a friend specifier with a class definition. */
8424 if (decl_specs->specs[(int) ds_friend] != 0
8425 && (*declares_class_or_enum & 2))
8426 error ("%Hclass definition may not be declared a friend",
8427 &start_token->location);
8430 /* Parse an (optional) storage-class-specifier.
8432 storage-class-specifier:
8441 storage-class-specifier:
8444 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8447 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8449 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8452 if (cxx_dialect != cxx98)
8454 /* Fall through for C++98. */
8461 /* Consume the token. */
8462 return cp_lexer_consume_token (parser->lexer)->u.value;
8469 /* Parse an (optional) function-specifier.
8476 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8477 Updates DECL_SPECS, if it is non-NULL. */
8480 cp_parser_function_specifier_opt (cp_parser* parser,
8481 cp_decl_specifier_seq *decl_specs)
8483 cp_token *token = cp_lexer_peek_token (parser->lexer);
8484 switch (token->keyword)
8488 ++decl_specs->specs[(int) ds_inline];
8492 /* 14.5.2.3 [temp.mem]
8494 A member function template shall not be virtual. */
8495 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8496 error ("%Htemplates may not be %<virtual%>", &token->location);
8497 else if (decl_specs)
8498 ++decl_specs->specs[(int) ds_virtual];
8503 ++decl_specs->specs[(int) ds_explicit];
8510 /* Consume the token. */
8511 return cp_lexer_consume_token (parser->lexer)->u.value;
8514 /* Parse a linkage-specification.
8516 linkage-specification:
8517 extern string-literal { declaration-seq [opt] }
8518 extern string-literal declaration */
8521 cp_parser_linkage_specification (cp_parser* parser)
8525 /* Look for the `extern' keyword. */
8526 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8528 /* Look for the string-literal. */
8529 linkage = cp_parser_string_literal (parser, false, false);
8531 /* Transform the literal into an identifier. If the literal is a
8532 wide-character string, or contains embedded NULs, then we can't
8533 handle it as the user wants. */
8534 if (strlen (TREE_STRING_POINTER (linkage))
8535 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8537 cp_parser_error (parser, "invalid linkage-specification");
8538 /* Assume C++ linkage. */
8539 linkage = lang_name_cplusplus;
8542 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8544 /* We're now using the new linkage. */
8545 push_lang_context (linkage);
8547 /* If the next token is a `{', then we're using the first
8549 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8551 /* Consume the `{' token. */
8552 cp_lexer_consume_token (parser->lexer);
8553 /* Parse the declarations. */
8554 cp_parser_declaration_seq_opt (parser);
8555 /* Look for the closing `}'. */
8556 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8558 /* Otherwise, there's just one declaration. */
8561 bool saved_in_unbraced_linkage_specification_p;
8563 saved_in_unbraced_linkage_specification_p
8564 = parser->in_unbraced_linkage_specification_p;
8565 parser->in_unbraced_linkage_specification_p = true;
8566 cp_parser_declaration (parser);
8567 parser->in_unbraced_linkage_specification_p
8568 = saved_in_unbraced_linkage_specification_p;
8571 /* We're done with the linkage-specification. */
8572 pop_lang_context ();
8575 /* Parse a static_assert-declaration.
8577 static_assert-declaration:
8578 static_assert ( constant-expression , string-literal ) ;
8580 If MEMBER_P, this static_assert is a class member. */
8583 cp_parser_static_assert(cp_parser *parser, bool member_p)
8588 location_t saved_loc;
8590 /* Peek at the `static_assert' token so we can keep track of exactly
8591 where the static assertion started. */
8592 token = cp_lexer_peek_token (parser->lexer);
8593 saved_loc = token->location;
8595 /* Look for the `static_assert' keyword. */
8596 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8597 "%<static_assert%>"))
8600 /* We know we are in a static assertion; commit to any tentative
8602 if (cp_parser_parsing_tentatively (parser))
8603 cp_parser_commit_to_tentative_parse (parser);
8605 /* Parse the `(' starting the static assertion condition. */
8606 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8608 /* Parse the constant-expression. */
8610 cp_parser_constant_expression (parser,
8611 /*allow_non_constant_p=*/false,
8612 /*non_constant_p=*/NULL);
8614 /* Parse the separating `,'. */
8615 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8617 /* Parse the string-literal message. */
8618 message = cp_parser_string_literal (parser,
8619 /*translate=*/false,
8622 /* A `)' completes the static assertion. */
8623 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8624 cp_parser_skip_to_closing_parenthesis (parser,
8625 /*recovering=*/true,
8627 /*consume_paren=*/true);
8629 /* A semicolon terminates the declaration. */
8630 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8632 /* Complete the static assertion, which may mean either processing
8633 the static assert now or saving it for template instantiation. */
8634 finish_static_assert (condition, message, saved_loc, member_p);
8637 /* Parse a `decltype' type. Returns the type.
8639 simple-type-specifier:
8640 decltype ( expression ) */
8643 cp_parser_decltype (cp_parser *parser)
8646 bool id_expression_or_member_access_p = false;
8647 const char *saved_message;
8648 bool saved_integral_constant_expression_p;
8649 bool saved_non_integral_constant_expression_p;
8650 cp_token *id_expr_start_token;
8652 /* Look for the `decltype' token. */
8653 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8654 return error_mark_node;
8656 /* Types cannot be defined in a `decltype' expression. Save away the
8658 saved_message = parser->type_definition_forbidden_message;
8660 /* And create the new one. */
8661 parser->type_definition_forbidden_message
8662 = "types may not be defined in %<decltype%> expressions";
8664 /* The restrictions on constant-expressions do not apply inside
8665 decltype expressions. */
8666 saved_integral_constant_expression_p
8667 = parser->integral_constant_expression_p;
8668 saved_non_integral_constant_expression_p
8669 = parser->non_integral_constant_expression_p;
8670 parser->integral_constant_expression_p = false;
8672 /* Do not actually evaluate the expression. */
8675 /* Parse the opening `('. */
8676 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8677 return error_mark_node;
8679 /* First, try parsing an id-expression. */
8680 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8681 cp_parser_parse_tentatively (parser);
8682 expr = cp_parser_id_expression (parser,
8683 /*template_keyword_p=*/false,
8684 /*check_dependency_p=*/true,
8685 /*template_p=*/NULL,
8686 /*declarator_p=*/false,
8687 /*optional_p=*/false);
8689 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8691 bool non_integral_constant_expression_p = false;
8692 tree id_expression = expr;
8694 const char *error_msg;
8696 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8697 /* Lookup the name we got back from the id-expression. */
8698 expr = cp_parser_lookup_name (parser, expr,
8700 /*is_template=*/false,
8701 /*is_namespace=*/false,
8702 /*check_dependency=*/true,
8703 /*ambiguous_decls=*/NULL,
8704 id_expr_start_token->location);
8707 && expr != error_mark_node
8708 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8709 && TREE_CODE (expr) != TYPE_DECL
8710 && (TREE_CODE (expr) != BIT_NOT_EXPR
8711 || !TYPE_P (TREE_OPERAND (expr, 0)))
8712 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8714 /* Complete lookup of the id-expression. */
8715 expr = (finish_id_expression
8716 (id_expression, expr, parser->scope, &idk,
8717 /*integral_constant_expression_p=*/false,
8718 /*allow_non_integral_constant_expression_p=*/true,
8719 &non_integral_constant_expression_p,
8720 /*template_p=*/false,
8722 /*address_p=*/false,
8723 /*template_arg_p=*/false,
8725 id_expr_start_token->location));
8727 if (expr == error_mark_node)
8728 /* We found an id-expression, but it was something that we
8729 should not have found. This is an error, not something
8730 we can recover from, so note that we found an
8731 id-expression and we'll recover as gracefully as
8733 id_expression_or_member_access_p = true;
8737 && expr != error_mark_node
8738 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8739 /* We have an id-expression. */
8740 id_expression_or_member_access_p = true;
8743 if (!id_expression_or_member_access_p)
8745 /* Abort the id-expression parse. */
8746 cp_parser_abort_tentative_parse (parser);
8748 /* Parsing tentatively, again. */
8749 cp_parser_parse_tentatively (parser);
8751 /* Parse a class member access. */
8752 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8754 /*member_access_only_p=*/true);
8757 && expr != error_mark_node
8758 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8759 /* We have an id-expression. */
8760 id_expression_or_member_access_p = true;
8763 if (id_expression_or_member_access_p)
8764 /* We have parsed the complete id-expression or member access. */
8765 cp_parser_parse_definitely (parser);
8768 /* Abort our attempt to parse an id-expression or member access
8770 cp_parser_abort_tentative_parse (parser);
8772 /* Parse a full expression. */
8773 expr = cp_parser_expression (parser, /*cast_p=*/false);
8776 /* Go back to evaluating expressions. */
8779 /* Restore the old message and the integral constant expression
8781 parser->type_definition_forbidden_message = saved_message;
8782 parser->integral_constant_expression_p
8783 = saved_integral_constant_expression_p;
8784 parser->non_integral_constant_expression_p
8785 = saved_non_integral_constant_expression_p;
8787 if (expr == error_mark_node)
8789 /* Skip everything up to the closing `)'. */
8790 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8791 /*consume_paren=*/true);
8792 return error_mark_node;
8795 /* Parse to the closing `)'. */
8796 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8798 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8799 /*consume_paren=*/true);
8800 return error_mark_node;
8803 return finish_decltype_type (expr, id_expression_or_member_access_p);
8806 /* Special member functions [gram.special] */
8808 /* Parse a conversion-function-id.
8810 conversion-function-id:
8811 operator conversion-type-id
8813 Returns an IDENTIFIER_NODE representing the operator. */
8816 cp_parser_conversion_function_id (cp_parser* parser)
8820 tree saved_qualifying_scope;
8821 tree saved_object_scope;
8822 tree pushed_scope = NULL_TREE;
8824 /* Look for the `operator' token. */
8825 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8826 return error_mark_node;
8827 /* When we parse the conversion-type-id, the current scope will be
8828 reset. However, we need that information in able to look up the
8829 conversion function later, so we save it here. */
8830 saved_scope = parser->scope;
8831 saved_qualifying_scope = parser->qualifying_scope;
8832 saved_object_scope = parser->object_scope;
8833 /* We must enter the scope of the class so that the names of
8834 entities declared within the class are available in the
8835 conversion-type-id. For example, consider:
8842 S::operator I() { ... }
8844 In order to see that `I' is a type-name in the definition, we
8845 must be in the scope of `S'. */
8847 pushed_scope = push_scope (saved_scope);
8848 /* Parse the conversion-type-id. */
8849 type = cp_parser_conversion_type_id (parser);
8850 /* Leave the scope of the class, if any. */
8852 pop_scope (pushed_scope);
8853 /* Restore the saved scope. */
8854 parser->scope = saved_scope;
8855 parser->qualifying_scope = saved_qualifying_scope;
8856 parser->object_scope = saved_object_scope;
8857 /* If the TYPE is invalid, indicate failure. */
8858 if (type == error_mark_node)
8859 return error_mark_node;
8860 return mangle_conv_op_name_for_type (type);
8863 /* Parse a conversion-type-id:
8866 type-specifier-seq conversion-declarator [opt]
8868 Returns the TYPE specified. */
8871 cp_parser_conversion_type_id (cp_parser* parser)
8874 cp_decl_specifier_seq type_specifiers;
8875 cp_declarator *declarator;
8876 tree type_specified;
8878 /* Parse the attributes. */
8879 attributes = cp_parser_attributes_opt (parser);
8880 /* Parse the type-specifiers. */
8881 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8883 /* If that didn't work, stop. */
8884 if (type_specifiers.type == error_mark_node)
8885 return error_mark_node;
8886 /* Parse the conversion-declarator. */
8887 declarator = cp_parser_conversion_declarator_opt (parser);
8889 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8890 /*initialized=*/0, &attributes);
8892 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8893 return type_specified;
8896 /* Parse an (optional) conversion-declarator.
8898 conversion-declarator:
8899 ptr-operator conversion-declarator [opt]
8903 static cp_declarator *
8904 cp_parser_conversion_declarator_opt (cp_parser* parser)
8906 enum tree_code code;
8908 cp_cv_quals cv_quals;
8910 /* We don't know if there's a ptr-operator next, or not. */
8911 cp_parser_parse_tentatively (parser);
8912 /* Try the ptr-operator. */
8913 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8914 /* If it worked, look for more conversion-declarators. */
8915 if (cp_parser_parse_definitely (parser))
8917 cp_declarator *declarator;
8919 /* Parse another optional declarator. */
8920 declarator = cp_parser_conversion_declarator_opt (parser);
8922 return cp_parser_make_indirect_declarator
8923 (code, class_type, cv_quals, declarator);
8929 /* Parse an (optional) ctor-initializer.
8932 : mem-initializer-list
8934 Returns TRUE iff the ctor-initializer was actually present. */
8937 cp_parser_ctor_initializer_opt (cp_parser* parser)
8939 /* If the next token is not a `:', then there is no
8940 ctor-initializer. */
8941 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8943 /* Do default initialization of any bases and members. */
8944 if (DECL_CONSTRUCTOR_P (current_function_decl))
8945 finish_mem_initializers (NULL_TREE);
8950 /* Consume the `:' token. */
8951 cp_lexer_consume_token (parser->lexer);
8952 /* And the mem-initializer-list. */
8953 cp_parser_mem_initializer_list (parser);
8958 /* Parse a mem-initializer-list.
8960 mem-initializer-list:
8961 mem-initializer ... [opt]
8962 mem-initializer ... [opt] , mem-initializer-list */
8965 cp_parser_mem_initializer_list (cp_parser* parser)
8967 tree mem_initializer_list = NULL_TREE;
8968 cp_token *token = cp_lexer_peek_token (parser->lexer);
8970 /* Let the semantic analysis code know that we are starting the
8971 mem-initializer-list. */
8972 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8973 error ("%Honly constructors take base initializers",
8976 /* Loop through the list. */
8979 tree mem_initializer;
8981 token = cp_lexer_peek_token (parser->lexer);
8982 /* Parse the mem-initializer. */
8983 mem_initializer = cp_parser_mem_initializer (parser);
8984 /* If the next token is a `...', we're expanding member initializers. */
8985 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8987 /* Consume the `...'. */
8988 cp_lexer_consume_token (parser->lexer);
8990 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8991 can be expanded but members cannot. */
8992 if (mem_initializer != error_mark_node
8993 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8995 error ("%Hcannot expand initializer for member %<%D%>",
8996 &token->location, TREE_PURPOSE (mem_initializer));
8997 mem_initializer = error_mark_node;
9000 /* Construct the pack expansion type. */
9001 if (mem_initializer != error_mark_node)
9002 mem_initializer = make_pack_expansion (mem_initializer);
9004 /* Add it to the list, unless it was erroneous. */
9005 if (mem_initializer != error_mark_node)
9007 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9008 mem_initializer_list = mem_initializer;
9010 /* If the next token is not a `,', we're done. */
9011 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9013 /* Consume the `,' token. */
9014 cp_lexer_consume_token (parser->lexer);
9017 /* Perform semantic analysis. */
9018 if (DECL_CONSTRUCTOR_P (current_function_decl))
9019 finish_mem_initializers (mem_initializer_list);
9022 /* Parse a mem-initializer.
9025 mem-initializer-id ( expression-list [opt] )
9026 mem-initializer-id braced-init-list
9031 ( expression-list [opt] )
9033 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9034 class) or FIELD_DECL (for a non-static data member) to initialize;
9035 the TREE_VALUE is the expression-list. An empty initialization
9036 list is represented by void_list_node. */
9039 cp_parser_mem_initializer (cp_parser* parser)
9041 tree mem_initializer_id;
9042 tree expression_list;
9044 cp_token *token = cp_lexer_peek_token (parser->lexer);
9046 /* Find out what is being initialized. */
9047 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9049 permerror ("%Hanachronistic old-style base class initializer",
9051 mem_initializer_id = NULL_TREE;
9054 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9055 member = expand_member_init (mem_initializer_id);
9056 if (member && !DECL_P (member))
9057 in_base_initializer = 1;
9059 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9061 bool expr_non_constant_p;
9062 maybe_warn_cpp0x ("extended initializer lists");
9063 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9064 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9065 expression_list = build_tree_list (NULL_TREE, expression_list);
9069 = cp_parser_parenthesized_expression_list (parser, false,
9071 /*allow_expansion_p=*/true,
9072 /*non_constant_p=*/NULL);
9073 if (expression_list == error_mark_node)
9074 return error_mark_node;
9075 if (!expression_list)
9076 expression_list = void_type_node;
9078 in_base_initializer = 0;
9080 return member ? build_tree_list (member, expression_list) : error_mark_node;
9083 /* Parse a mem-initializer-id.
9086 :: [opt] nested-name-specifier [opt] class-name
9089 Returns a TYPE indicating the class to be initializer for the first
9090 production. Returns an IDENTIFIER_NODE indicating the data member
9091 to be initialized for the second production. */
9094 cp_parser_mem_initializer_id (cp_parser* parser)
9096 bool global_scope_p;
9097 bool nested_name_specifier_p;
9098 bool template_p = false;
9101 cp_token *token = cp_lexer_peek_token (parser->lexer);
9103 /* `typename' is not allowed in this context ([temp.res]). */
9104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9106 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9107 "member initializer is implicitly a type)",
9109 cp_lexer_consume_token (parser->lexer);
9111 /* Look for the optional `::' operator. */
9113 = (cp_parser_global_scope_opt (parser,
9114 /*current_scope_valid_p=*/false)
9116 /* Look for the optional nested-name-specifier. The simplest way to
9121 The keyword `typename' is not permitted in a base-specifier or
9122 mem-initializer; in these contexts a qualified name that
9123 depends on a template-parameter is implicitly assumed to be a
9126 is to assume that we have seen the `typename' keyword at this
9128 nested_name_specifier_p
9129 = (cp_parser_nested_name_specifier_opt (parser,
9130 /*typename_keyword_p=*/true,
9131 /*check_dependency_p=*/true,
9133 /*is_declaration=*/true)
9135 if (nested_name_specifier_p)
9136 template_p = cp_parser_optional_template_keyword (parser);
9137 /* If there is a `::' operator or a nested-name-specifier, then we
9138 are definitely looking for a class-name. */
9139 if (global_scope_p || nested_name_specifier_p)
9140 return cp_parser_class_name (parser,
9141 /*typename_keyword_p=*/true,
9142 /*template_keyword_p=*/template_p,
9144 /*check_dependency_p=*/true,
9145 /*class_head_p=*/false,
9146 /*is_declaration=*/true);
9147 /* Otherwise, we could also be looking for an ordinary identifier. */
9148 cp_parser_parse_tentatively (parser);
9149 /* Try a class-name. */
9150 id = cp_parser_class_name (parser,
9151 /*typename_keyword_p=*/true,
9152 /*template_keyword_p=*/false,
9154 /*check_dependency_p=*/true,
9155 /*class_head_p=*/false,
9156 /*is_declaration=*/true);
9157 /* If we found one, we're done. */
9158 if (cp_parser_parse_definitely (parser))
9160 /* Otherwise, look for an ordinary identifier. */
9161 return cp_parser_identifier (parser);
9164 /* Overloading [gram.over] */
9166 /* Parse an operator-function-id.
9168 operator-function-id:
9171 Returns an IDENTIFIER_NODE for the operator which is a
9172 human-readable spelling of the identifier, e.g., `operator +'. */
9175 cp_parser_operator_function_id (cp_parser* parser)
9177 /* Look for the `operator' keyword. */
9178 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9179 return error_mark_node;
9180 /* And then the name of the operator itself. */
9181 return cp_parser_operator (parser);
9184 /* Parse an operator.
9187 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9188 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9189 || ++ -- , ->* -> () []
9196 Returns an IDENTIFIER_NODE for the operator which is a
9197 human-readable spelling of the identifier, e.g., `operator +'. */
9200 cp_parser_operator (cp_parser* parser)
9202 tree id = NULL_TREE;
9205 /* Peek at the next token. */
9206 token = cp_lexer_peek_token (parser->lexer);
9207 /* Figure out which operator we have. */
9208 switch (token->type)
9214 /* The keyword should be either `new' or `delete'. */
9215 if (token->keyword == RID_NEW)
9217 else if (token->keyword == RID_DELETE)
9222 /* Consume the `new' or `delete' token. */
9223 cp_lexer_consume_token (parser->lexer);
9225 /* Peek at the next token. */
9226 token = cp_lexer_peek_token (parser->lexer);
9227 /* If it's a `[' token then this is the array variant of the
9229 if (token->type == CPP_OPEN_SQUARE)
9231 /* Consume the `[' token. */
9232 cp_lexer_consume_token (parser->lexer);
9233 /* Look for the `]' token. */
9234 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9235 id = ansi_opname (op == NEW_EXPR
9236 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9238 /* Otherwise, we have the non-array variant. */
9240 id = ansi_opname (op);
9246 id = ansi_opname (PLUS_EXPR);
9250 id = ansi_opname (MINUS_EXPR);
9254 id = ansi_opname (MULT_EXPR);
9258 id = ansi_opname (TRUNC_DIV_EXPR);
9262 id = ansi_opname (TRUNC_MOD_EXPR);
9266 id = ansi_opname (BIT_XOR_EXPR);
9270 id = ansi_opname (BIT_AND_EXPR);
9274 id = ansi_opname (BIT_IOR_EXPR);
9278 id = ansi_opname (BIT_NOT_EXPR);
9282 id = ansi_opname (TRUTH_NOT_EXPR);
9286 id = ansi_assopname (NOP_EXPR);
9290 id = ansi_opname (LT_EXPR);
9294 id = ansi_opname (GT_EXPR);
9298 id = ansi_assopname (PLUS_EXPR);
9302 id = ansi_assopname (MINUS_EXPR);
9306 id = ansi_assopname (MULT_EXPR);
9310 id = ansi_assopname (TRUNC_DIV_EXPR);
9314 id = ansi_assopname (TRUNC_MOD_EXPR);
9318 id = ansi_assopname (BIT_XOR_EXPR);
9322 id = ansi_assopname (BIT_AND_EXPR);
9326 id = ansi_assopname (BIT_IOR_EXPR);
9330 id = ansi_opname (LSHIFT_EXPR);
9334 id = ansi_opname (RSHIFT_EXPR);
9338 id = ansi_assopname (LSHIFT_EXPR);
9342 id = ansi_assopname (RSHIFT_EXPR);
9346 id = ansi_opname (EQ_EXPR);
9350 id = ansi_opname (NE_EXPR);
9354 id = ansi_opname (LE_EXPR);
9357 case CPP_GREATER_EQ:
9358 id = ansi_opname (GE_EXPR);
9362 id = ansi_opname (TRUTH_ANDIF_EXPR);
9366 id = ansi_opname (TRUTH_ORIF_EXPR);
9370 id = ansi_opname (POSTINCREMENT_EXPR);
9373 case CPP_MINUS_MINUS:
9374 id = ansi_opname (PREDECREMENT_EXPR);
9378 id = ansi_opname (COMPOUND_EXPR);
9381 case CPP_DEREF_STAR:
9382 id = ansi_opname (MEMBER_REF);
9386 id = ansi_opname (COMPONENT_REF);
9389 case CPP_OPEN_PAREN:
9390 /* Consume the `('. */
9391 cp_lexer_consume_token (parser->lexer);
9392 /* Look for the matching `)'. */
9393 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9394 return ansi_opname (CALL_EXPR);
9396 case CPP_OPEN_SQUARE:
9397 /* Consume the `['. */
9398 cp_lexer_consume_token (parser->lexer);
9399 /* Look for the matching `]'. */
9400 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9401 return ansi_opname (ARRAY_REF);
9404 /* Anything else is an error. */
9408 /* If we have selected an identifier, we need to consume the
9411 cp_lexer_consume_token (parser->lexer);
9412 /* Otherwise, no valid operator name was present. */
9415 cp_parser_error (parser, "expected operator");
9416 id = error_mark_node;
9422 /* Parse a template-declaration.
9424 template-declaration:
9425 export [opt] template < template-parameter-list > declaration
9427 If MEMBER_P is TRUE, this template-declaration occurs within a
9430 The grammar rule given by the standard isn't correct. What
9433 template-declaration:
9434 export [opt] template-parameter-list-seq
9435 decl-specifier-seq [opt] init-declarator [opt] ;
9436 export [opt] template-parameter-list-seq
9439 template-parameter-list-seq:
9440 template-parameter-list-seq [opt]
9441 template < template-parameter-list > */
9444 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9446 /* Check for `export'. */
9447 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9449 /* Consume the `export' token. */
9450 cp_lexer_consume_token (parser->lexer);
9451 /* Warn that we do not support `export'. */
9452 warning (0, "keyword %<export%> not implemented, and will be ignored");
9455 cp_parser_template_declaration_after_export (parser, member_p);
9458 /* Parse a template-parameter-list.
9460 template-parameter-list:
9462 template-parameter-list , template-parameter
9464 Returns a TREE_LIST. Each node represents a template parameter.
9465 The nodes are connected via their TREE_CHAINs. */
9468 cp_parser_template_parameter_list (cp_parser* parser)
9470 tree parameter_list = NULL_TREE;
9472 begin_template_parm_list ();
9477 bool is_parameter_pack;
9479 /* Parse the template-parameter. */
9480 parameter = cp_parser_template_parameter (parser,
9482 &is_parameter_pack);
9483 /* Add it to the list. */
9484 if (parameter != error_mark_node)
9485 parameter_list = process_template_parm (parameter_list,
9491 tree err_parm = build_tree_list (parameter, parameter);
9492 TREE_VALUE (err_parm) = error_mark_node;
9493 parameter_list = chainon (parameter_list, err_parm);
9496 /* If the next token is not a `,', we're done. */
9497 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9499 /* Otherwise, consume the `,' token. */
9500 cp_lexer_consume_token (parser->lexer);
9503 return end_template_parm_list (parameter_list);
9506 /* Parse a template-parameter.
9510 parameter-declaration
9512 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9513 the parameter. The TREE_PURPOSE is the default value, if any.
9514 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9515 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9516 set to true iff this parameter is a parameter pack. */
9519 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9520 bool *is_parameter_pack)
9523 cp_parameter_declarator *parameter_declarator;
9524 cp_declarator *id_declarator;
9527 /* Assume it is a type parameter or a template parameter. */
9528 *is_non_type = false;
9529 /* Assume it not a parameter pack. */
9530 *is_parameter_pack = false;
9531 /* Peek at the next token. */
9532 token = cp_lexer_peek_token (parser->lexer);
9533 /* If it is `class' or `template', we have a type-parameter. */
9534 if (token->keyword == RID_TEMPLATE)
9535 return cp_parser_type_parameter (parser, is_parameter_pack);
9536 /* If it is `class' or `typename' we do not know yet whether it is a
9537 type parameter or a non-type parameter. Consider:
9539 template <typename T, typename T::X X> ...
9543 template <class C, class D*> ...
9545 Here, the first parameter is a type parameter, and the second is
9546 a non-type parameter. We can tell by looking at the token after
9547 the identifier -- if it is a `,', `=', or `>' then we have a type
9549 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9551 /* Peek at the token after `class' or `typename'. */
9552 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9553 /* If it's an ellipsis, we have a template type parameter
9555 if (token->type == CPP_ELLIPSIS)
9556 return cp_parser_type_parameter (parser, is_parameter_pack);
9557 /* If it's an identifier, skip it. */
9558 if (token->type == CPP_NAME)
9559 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9560 /* Now, see if the token looks like the end of a template
9562 if (token->type == CPP_COMMA
9563 || token->type == CPP_EQ
9564 || token->type == CPP_GREATER)
9565 return cp_parser_type_parameter (parser, is_parameter_pack);
9568 /* Otherwise, it is a non-type parameter.
9572 When parsing a default template-argument for a non-type
9573 template-parameter, the first non-nested `>' is taken as the end
9574 of the template parameter-list rather than a greater-than
9576 *is_non_type = true;
9577 parameter_declarator
9578 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9579 /*parenthesized_p=*/NULL);
9581 /* If the parameter declaration is marked as a parameter pack, set
9582 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9583 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9585 if (parameter_declarator
9586 && parameter_declarator->declarator
9587 && parameter_declarator->declarator->parameter_pack_p)
9589 *is_parameter_pack = true;
9590 parameter_declarator->declarator->parameter_pack_p = false;
9593 /* If the next token is an ellipsis, and we don't already have it
9594 marked as a parameter pack, then we have a parameter pack (that
9595 has no declarator). */
9596 if (!*is_parameter_pack
9597 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9598 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9600 /* Consume the `...'. */
9601 cp_lexer_consume_token (parser->lexer);
9602 maybe_warn_variadic_templates ();
9604 *is_parameter_pack = true;
9606 /* We might end up with a pack expansion as the type of the non-type
9607 template parameter, in which case this is a non-type template
9609 else if (parameter_declarator
9610 && parameter_declarator->decl_specifiers.type
9611 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9613 *is_parameter_pack = true;
9614 parameter_declarator->decl_specifiers.type =
9615 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9618 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9620 /* Parameter packs cannot have default arguments. However, a
9621 user may try to do so, so we'll parse them and give an
9622 appropriate diagnostic here. */
9624 /* Consume the `='. */
9625 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9626 cp_lexer_consume_token (parser->lexer);
9628 /* Find the name of the parameter pack. */
9629 id_declarator = parameter_declarator->declarator;
9630 while (id_declarator && id_declarator->kind != cdk_id)
9631 id_declarator = id_declarator->declarator;
9633 if (id_declarator && id_declarator->kind == cdk_id)
9634 error ("%Htemplate parameter pack %qD cannot have a default argument",
9635 &start_token->location, id_declarator->u.id.unqualified_name);
9637 error ("%Htemplate parameter pack cannot have a default argument",
9638 &start_token->location);
9640 /* Parse the default argument, but throw away the result. */
9641 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9644 parm = grokdeclarator (parameter_declarator->declarator,
9645 ¶meter_declarator->decl_specifiers,
9646 PARM, /*initialized=*/0,
9648 if (parm == error_mark_node)
9649 return error_mark_node;
9651 return build_tree_list (parameter_declarator->default_argument, parm);
9654 /* Parse a type-parameter.
9657 class identifier [opt]
9658 class identifier [opt] = type-id
9659 typename identifier [opt]
9660 typename identifier [opt] = type-id
9661 template < template-parameter-list > class identifier [opt]
9662 template < template-parameter-list > class identifier [opt]
9665 GNU Extension (variadic templates):
9668 class ... identifier [opt]
9669 typename ... identifier [opt]
9671 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9672 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9673 the declaration of the parameter.
9675 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9678 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9683 /* Look for a keyword to tell us what kind of parameter this is. */
9684 token = cp_parser_require (parser, CPP_KEYWORD,
9685 "%<class%>, %<typename%>, or %<template%>");
9687 return error_mark_node;
9689 switch (token->keyword)
9695 tree default_argument;
9697 /* If the next token is an ellipsis, we have a template
9699 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9701 /* Consume the `...' token. */
9702 cp_lexer_consume_token (parser->lexer);
9703 maybe_warn_variadic_templates ();
9705 *is_parameter_pack = true;
9708 /* If the next token is an identifier, then it names the
9710 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9711 identifier = cp_parser_identifier (parser);
9713 identifier = NULL_TREE;
9715 /* Create the parameter. */
9716 parameter = finish_template_type_parm (class_type_node, identifier);
9718 /* If the next token is an `=', we have a default argument. */
9719 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9721 /* Consume the `=' token. */
9722 cp_lexer_consume_token (parser->lexer);
9723 /* Parse the default-argument. */
9724 push_deferring_access_checks (dk_no_deferred);
9725 default_argument = cp_parser_type_id (parser);
9727 /* Template parameter packs cannot have default
9729 if (*is_parameter_pack)
9732 error ("%Htemplate parameter pack %qD cannot have a "
9733 "default argument", &token->location, identifier);
9735 error ("%Htemplate parameter packs cannot have "
9736 "default arguments", &token->location);
9737 default_argument = NULL_TREE;
9739 pop_deferring_access_checks ();
9742 default_argument = NULL_TREE;
9744 /* Create the combined representation of the parameter and the
9745 default argument. */
9746 parameter = build_tree_list (default_argument, parameter);
9752 tree parameter_list;
9754 tree default_argument;
9756 /* Look for the `<'. */
9757 cp_parser_require (parser, CPP_LESS, "%<<%>");
9758 /* Parse the template-parameter-list. */
9759 parameter_list = cp_parser_template_parameter_list (parser);
9760 /* Look for the `>'. */
9761 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9762 /* Look for the `class' keyword. */
9763 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9764 /* If the next token is an ellipsis, we have a template
9766 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9768 /* Consume the `...' token. */
9769 cp_lexer_consume_token (parser->lexer);
9770 maybe_warn_variadic_templates ();
9772 *is_parameter_pack = true;
9774 /* If the next token is an `=', then there is a
9775 default-argument. If the next token is a `>', we are at
9776 the end of the parameter-list. If the next token is a `,',
9777 then we are at the end of this parameter. */
9778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9779 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9780 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9782 identifier = cp_parser_identifier (parser);
9783 /* Treat invalid names as if the parameter were nameless. */
9784 if (identifier == error_mark_node)
9785 identifier = NULL_TREE;
9788 identifier = NULL_TREE;
9790 /* Create the template parameter. */
9791 parameter = finish_template_template_parm (class_type_node,
9794 /* If the next token is an `=', then there is a
9795 default-argument. */
9796 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9800 /* Consume the `='. */
9801 cp_lexer_consume_token (parser->lexer);
9802 /* Parse the id-expression. */
9803 push_deferring_access_checks (dk_no_deferred);
9804 /* save token before parsing the id-expression, for error
9806 token = cp_lexer_peek_token (parser->lexer);
9808 = cp_parser_id_expression (parser,
9809 /*template_keyword_p=*/false,
9810 /*check_dependency_p=*/true,
9811 /*template_p=*/&is_template,
9812 /*declarator_p=*/false,
9813 /*optional_p=*/false);
9814 if (TREE_CODE (default_argument) == TYPE_DECL)
9815 /* If the id-expression was a template-id that refers to
9816 a template-class, we already have the declaration here,
9817 so no further lookup is needed. */
9820 /* Look up the name. */
9822 = cp_parser_lookup_name (parser, default_argument,
9824 /*is_template=*/is_template,
9825 /*is_namespace=*/false,
9826 /*check_dependency=*/true,
9827 /*ambiguous_decls=*/NULL,
9829 /* See if the default argument is valid. */
9831 = check_template_template_default_arg (default_argument);
9833 /* Template parameter packs cannot have default
9835 if (*is_parameter_pack)
9838 error ("%Htemplate parameter pack %qD cannot "
9839 "have a default argument",
9840 &token->location, identifier);
9842 error ("%Htemplate parameter packs cannot "
9843 "have default arguments",
9845 default_argument = NULL_TREE;
9847 pop_deferring_access_checks ();
9850 default_argument = NULL_TREE;
9852 /* Create the combined representation of the parameter and the
9853 default argument. */
9854 parameter = build_tree_list (default_argument, parameter);
9866 /* Parse a template-id.
9869 template-name < template-argument-list [opt] >
9871 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9872 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9873 returned. Otherwise, if the template-name names a function, or set
9874 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9875 names a class, returns a TYPE_DECL for the specialization.
9877 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9878 uninstantiated templates. */
9881 cp_parser_template_id (cp_parser *parser,
9882 bool template_keyword_p,
9883 bool check_dependency_p,
9884 bool is_declaration)
9890 cp_token_position start_of_id = 0;
9891 deferred_access_check *chk;
9892 VEC (deferred_access_check,gc) *access_check;
9893 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9896 /* If the next token corresponds to a template-id, there is no need
9898 next_token = cp_lexer_peek_token (parser->lexer);
9899 if (next_token->type == CPP_TEMPLATE_ID)
9901 struct tree_check *check_value;
9903 /* Get the stored value. */
9904 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9905 /* Perform any access checks that were deferred. */
9906 access_check = check_value->checks;
9910 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9913 perform_or_defer_access_check (chk->binfo,
9918 /* Return the stored value. */
9919 return check_value->value;
9922 /* Avoid performing name lookup if there is no possibility of
9923 finding a template-id. */
9924 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9925 || (next_token->type == CPP_NAME
9926 && !cp_parser_nth_token_starts_template_argument_list_p
9929 cp_parser_error (parser, "expected template-id");
9930 return error_mark_node;
9933 /* Remember where the template-id starts. */
9934 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9935 start_of_id = cp_lexer_token_position (parser->lexer, false);
9937 push_deferring_access_checks (dk_deferred);
9939 /* Parse the template-name. */
9940 is_identifier = false;
9941 token = cp_lexer_peek_token (parser->lexer);
9942 templ = cp_parser_template_name (parser, template_keyword_p,
9946 if (templ == error_mark_node || is_identifier)
9948 pop_deferring_access_checks ();
9952 /* If we find the sequence `[:' after a template-name, it's probably
9953 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9954 parse correctly the argument list. */
9955 next_token = cp_lexer_peek_token (parser->lexer);
9956 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9957 if (next_token->type == CPP_OPEN_SQUARE
9958 && next_token->flags & DIGRAPH
9959 && next_token_2->type == CPP_COLON
9960 && !(next_token_2->flags & PREV_WHITE))
9962 cp_parser_parse_tentatively (parser);
9963 /* Change `:' into `::'. */
9964 next_token_2->type = CPP_SCOPE;
9965 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9967 cp_lexer_consume_token (parser->lexer);
9969 /* Parse the arguments. */
9970 arguments = cp_parser_enclosed_template_argument_list (parser);
9971 if (!cp_parser_parse_definitely (parser))
9973 /* If we couldn't parse an argument list, then we revert our changes
9974 and return simply an error. Maybe this is not a template-id
9976 next_token_2->type = CPP_COLON;
9977 cp_parser_error (parser, "expected %<<%>");
9978 pop_deferring_access_checks ();
9979 return error_mark_node;
9981 /* Otherwise, emit an error about the invalid digraph, but continue
9982 parsing because we got our argument list. */
9983 permerror ("%H%<<::%> cannot begin a template-argument list",
9984 &next_token->location);
9985 inform ("%H%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9986 "between %<<%> and %<::%>",
9987 &next_token->location);
9988 if (!flag_permissive)
9993 inform ("%H(if you use %<-fpermissive%> G++ will accept your code)",
9994 &next_token->location);
10001 /* Look for the `<' that starts the template-argument-list. */
10002 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10004 pop_deferring_access_checks ();
10005 return error_mark_node;
10007 /* Parse the arguments. */
10008 arguments = cp_parser_enclosed_template_argument_list (parser);
10011 /* Build a representation of the specialization. */
10012 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10013 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10014 else if (DECL_CLASS_TEMPLATE_P (templ)
10015 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10017 bool entering_scope;
10018 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10019 template (rather than some instantiation thereof) only if
10020 is not nested within some other construct. For example, in
10021 "template <typename T> void f(T) { A<T>::", A<T> is just an
10022 instantiation of A. */
10023 entering_scope = (template_parm_scope_p ()
10024 && cp_lexer_next_token_is (parser->lexer,
10027 = finish_template_type (templ, arguments, entering_scope);
10031 /* If it's not a class-template or a template-template, it should be
10032 a function-template. */
10033 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10034 || TREE_CODE (templ) == OVERLOAD
10035 || BASELINK_P (templ)));
10037 template_id = lookup_template_function (templ, arguments);
10040 /* If parsing tentatively, replace the sequence of tokens that makes
10041 up the template-id with a CPP_TEMPLATE_ID token. That way,
10042 should we re-parse the token stream, we will not have to repeat
10043 the effort required to do the parse, nor will we issue duplicate
10044 error messages about problems during instantiation of the
10048 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10050 /* Reset the contents of the START_OF_ID token. */
10051 token->type = CPP_TEMPLATE_ID;
10052 /* Retrieve any deferred checks. Do not pop this access checks yet
10053 so the memory will not be reclaimed during token replacing below. */
10054 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10055 token->u.tree_check_value->value = template_id;
10056 token->u.tree_check_value->checks = get_deferred_access_checks ();
10057 token->keyword = RID_MAX;
10059 /* Purge all subsequent tokens. */
10060 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10062 /* ??? Can we actually assume that, if template_id ==
10063 error_mark_node, we will have issued a diagnostic to the
10064 user, as opposed to simply marking the tentative parse as
10066 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10067 error ("%Hparse error in template argument list",
10071 pop_deferring_access_checks ();
10072 return template_id;
10075 /* Parse a template-name.
10080 The standard should actually say:
10084 operator-function-id
10086 A defect report has been filed about this issue.
10088 A conversion-function-id cannot be a template name because they cannot
10089 be part of a template-id. In fact, looking at this code:
10091 a.operator K<int>()
10093 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10094 It is impossible to call a templated conversion-function-id with an
10095 explicit argument list, since the only allowed template parameter is
10096 the type to which it is converting.
10098 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10099 `template' keyword, in a construction like:
10103 In that case `f' is taken to be a template-name, even though there
10104 is no way of knowing for sure.
10106 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10107 name refers to a set of overloaded functions, at least one of which
10108 is a template, or an IDENTIFIER_NODE with the name of the template,
10109 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10110 names are looked up inside uninstantiated templates. */
10113 cp_parser_template_name (cp_parser* parser,
10114 bool template_keyword_p,
10115 bool check_dependency_p,
10116 bool is_declaration,
10117 bool *is_identifier)
10122 cp_token *token = cp_lexer_peek_token (parser->lexer);
10124 /* If the next token is `operator', then we have either an
10125 operator-function-id or a conversion-function-id. */
10126 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10128 /* We don't know whether we're looking at an
10129 operator-function-id or a conversion-function-id. */
10130 cp_parser_parse_tentatively (parser);
10131 /* Try an operator-function-id. */
10132 identifier = cp_parser_operator_function_id (parser);
10133 /* If that didn't work, try a conversion-function-id. */
10134 if (!cp_parser_parse_definitely (parser))
10136 cp_parser_error (parser, "expected template-name");
10137 return error_mark_node;
10140 /* Look for the identifier. */
10142 identifier = cp_parser_identifier (parser);
10144 /* If we didn't find an identifier, we don't have a template-id. */
10145 if (identifier == error_mark_node)
10146 return error_mark_node;
10148 /* If the name immediately followed the `template' keyword, then it
10149 is a template-name. However, if the next token is not `<', then
10150 we do not treat it as a template-name, since it is not being used
10151 as part of a template-id. This enables us to handle constructs
10154 template <typename T> struct S { S(); };
10155 template <typename T> S<T>::S();
10157 correctly. We would treat `S' as a template -- if it were `S<T>'
10158 -- but we do not if there is no `<'. */
10160 if (processing_template_decl
10161 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10163 /* In a declaration, in a dependent context, we pretend that the
10164 "template" keyword was present in order to improve error
10165 recovery. For example, given:
10167 template <typename T> void f(T::X<int>);
10169 we want to treat "X<int>" as a template-id. */
10171 && !template_keyword_p
10172 && parser->scope && TYPE_P (parser->scope)
10173 && check_dependency_p
10174 && dependent_type_p (parser->scope)
10175 /* Do not do this for dtors (or ctors), since they never
10176 need the template keyword before their name. */
10177 && !constructor_name_p (identifier, parser->scope))
10179 cp_token_position start = 0;
10181 /* Explain what went wrong. */
10182 error ("%Hnon-template %qD used as template",
10183 &token->location, identifier);
10184 inform ("use %<%T::template %D%> to indicate that it is a template",
10185 parser->scope, identifier);
10186 /* If parsing tentatively, find the location of the "<" token. */
10187 if (cp_parser_simulate_error (parser))
10188 start = cp_lexer_token_position (parser->lexer, true);
10189 /* Parse the template arguments so that we can issue error
10190 messages about them. */
10191 cp_lexer_consume_token (parser->lexer);
10192 cp_parser_enclosed_template_argument_list (parser);
10193 /* Skip tokens until we find a good place from which to
10194 continue parsing. */
10195 cp_parser_skip_to_closing_parenthesis (parser,
10196 /*recovering=*/true,
10198 /*consume_paren=*/false);
10199 /* If parsing tentatively, permanently remove the
10200 template argument list. That will prevent duplicate
10201 error messages from being issued about the missing
10202 "template" keyword. */
10204 cp_lexer_purge_tokens_after (parser->lexer, start);
10206 *is_identifier = true;
10210 /* If the "template" keyword is present, then there is generally
10211 no point in doing name-lookup, so we just return IDENTIFIER.
10212 But, if the qualifying scope is non-dependent then we can
10213 (and must) do name-lookup normally. */
10214 if (template_keyword_p
10216 || (TYPE_P (parser->scope)
10217 && dependent_type_p (parser->scope))))
10221 /* Look up the name. */
10222 decl = cp_parser_lookup_name (parser, identifier,
10224 /*is_template=*/false,
10225 /*is_namespace=*/false,
10226 check_dependency_p,
10227 /*ambiguous_decls=*/NULL,
10229 decl = maybe_get_template_decl_from_type_decl (decl);
10231 /* If DECL is a template, then the name was a template-name. */
10232 if (TREE_CODE (decl) == TEMPLATE_DECL)
10236 tree fn = NULL_TREE;
10238 /* The standard does not explicitly indicate whether a name that
10239 names a set of overloaded declarations, some of which are
10240 templates, is a template-name. However, such a name should
10241 be a template-name; otherwise, there is no way to form a
10242 template-id for the overloaded templates. */
10243 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10244 if (TREE_CODE (fns) == OVERLOAD)
10245 for (fn = fns; fn; fn = OVL_NEXT (fn))
10246 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10251 /* The name does not name a template. */
10252 cp_parser_error (parser, "expected template-name");
10253 return error_mark_node;
10257 /* If DECL is dependent, and refers to a function, then just return
10258 its name; we will look it up again during template instantiation. */
10259 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10261 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10262 if (TYPE_P (scope) && dependent_type_p (scope))
10269 /* Parse a template-argument-list.
10271 template-argument-list:
10272 template-argument ... [opt]
10273 template-argument-list , template-argument ... [opt]
10275 Returns a TREE_VEC containing the arguments. */
10278 cp_parser_template_argument_list (cp_parser* parser)
10280 tree fixed_args[10];
10281 unsigned n_args = 0;
10282 unsigned alloced = 10;
10283 tree *arg_ary = fixed_args;
10285 bool saved_in_template_argument_list_p;
10287 bool saved_non_ice_p;
10289 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10290 parser->in_template_argument_list_p = true;
10291 /* Even if the template-id appears in an integral
10292 constant-expression, the contents of the argument list do
10294 saved_ice_p = parser->integral_constant_expression_p;
10295 parser->integral_constant_expression_p = false;
10296 saved_non_ice_p = parser->non_integral_constant_expression_p;
10297 parser->non_integral_constant_expression_p = false;
10298 /* Parse the arguments. */
10304 /* Consume the comma. */
10305 cp_lexer_consume_token (parser->lexer);
10307 /* Parse the template-argument. */
10308 argument = cp_parser_template_argument (parser);
10310 /* If the next token is an ellipsis, we're expanding a template
10312 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10314 /* Consume the `...' token. */
10315 cp_lexer_consume_token (parser->lexer);
10317 /* Make the argument into a TYPE_PACK_EXPANSION or
10318 EXPR_PACK_EXPANSION. */
10319 argument = make_pack_expansion (argument);
10322 if (n_args == alloced)
10326 if (arg_ary == fixed_args)
10328 arg_ary = XNEWVEC (tree, alloced);
10329 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10332 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10334 arg_ary[n_args++] = argument;
10336 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10338 vec = make_tree_vec (n_args);
10341 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10343 if (arg_ary != fixed_args)
10345 parser->non_integral_constant_expression_p = saved_non_ice_p;
10346 parser->integral_constant_expression_p = saved_ice_p;
10347 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10351 /* Parse a template-argument.
10354 assignment-expression
10358 The representation is that of an assignment-expression, type-id, or
10359 id-expression -- except that the qualified id-expression is
10360 evaluated, so that the value returned is either a DECL or an
10363 Although the standard says "assignment-expression", it forbids
10364 throw-expressions or assignments in the template argument.
10365 Therefore, we use "conditional-expression" instead. */
10368 cp_parser_template_argument (cp_parser* parser)
10373 bool maybe_type_id = false;
10374 cp_token *token = NULL, *argument_start_token = NULL;
10377 /* There's really no way to know what we're looking at, so we just
10378 try each alternative in order.
10382 In a template-argument, an ambiguity between a type-id and an
10383 expression is resolved to a type-id, regardless of the form of
10384 the corresponding template-parameter.
10386 Therefore, we try a type-id first. */
10387 cp_parser_parse_tentatively (parser);
10388 argument = cp_parser_type_id (parser);
10389 /* If there was no error parsing the type-id but the next token is a
10390 '>>', our behavior depends on which dialect of C++ we're
10391 parsing. In C++98, we probably found a typo for '> >'. But there
10392 are type-id which are also valid expressions. For instance:
10394 struct X { int operator >> (int); };
10395 template <int V> struct Foo {};
10398 Here 'X()' is a valid type-id of a function type, but the user just
10399 wanted to write the expression "X() >> 5". Thus, we remember that we
10400 found a valid type-id, but we still try to parse the argument as an
10401 expression to see what happens.
10403 In C++0x, the '>>' will be considered two separate '>'
10405 if (!cp_parser_error_occurred (parser)
10406 && cxx_dialect == cxx98
10407 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10409 maybe_type_id = true;
10410 cp_parser_abort_tentative_parse (parser);
10414 /* If the next token isn't a `,' or a `>', then this argument wasn't
10415 really finished. This means that the argument is not a valid
10417 if (!cp_parser_next_token_ends_template_argument_p (parser))
10418 cp_parser_error (parser, "expected template-argument");
10419 /* If that worked, we're done. */
10420 if (cp_parser_parse_definitely (parser))
10423 /* We're still not sure what the argument will be. */
10424 cp_parser_parse_tentatively (parser);
10425 /* Try a template. */
10426 argument_start_token = cp_lexer_peek_token (parser->lexer);
10427 argument = cp_parser_id_expression (parser,
10428 /*template_keyword_p=*/false,
10429 /*check_dependency_p=*/true,
10431 /*declarator_p=*/false,
10432 /*optional_p=*/false);
10433 /* If the next token isn't a `,' or a `>', then this argument wasn't
10434 really finished. */
10435 if (!cp_parser_next_token_ends_template_argument_p (parser))
10436 cp_parser_error (parser, "expected template-argument");
10437 if (!cp_parser_error_occurred (parser))
10439 /* Figure out what is being referred to. If the id-expression
10440 was for a class template specialization, then we will have a
10441 TYPE_DECL at this point. There is no need to do name lookup
10442 at this point in that case. */
10443 if (TREE_CODE (argument) != TYPE_DECL)
10444 argument = cp_parser_lookup_name (parser, argument,
10446 /*is_template=*/template_p,
10447 /*is_namespace=*/false,
10448 /*check_dependency=*/true,
10449 /*ambiguous_decls=*/NULL,
10450 argument_start_token->location);
10451 if (TREE_CODE (argument) != TEMPLATE_DECL
10452 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10453 cp_parser_error (parser, "expected template-name");
10455 if (cp_parser_parse_definitely (parser))
10457 /* It must be a non-type argument. There permitted cases are given
10458 in [temp.arg.nontype]:
10460 -- an integral constant-expression of integral or enumeration
10463 -- the name of a non-type template-parameter; or
10465 -- the name of an object or function with external linkage...
10467 -- the address of an object or function with external linkage...
10469 -- a pointer to member... */
10470 /* Look for a non-type template parameter. */
10471 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10473 cp_parser_parse_tentatively (parser);
10474 argument = cp_parser_primary_expression (parser,
10475 /*address_p=*/false,
10477 /*template_arg_p=*/true,
10479 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10480 || !cp_parser_next_token_ends_template_argument_p (parser))
10481 cp_parser_simulate_error (parser);
10482 if (cp_parser_parse_definitely (parser))
10486 /* If the next token is "&", the argument must be the address of an
10487 object or function with external linkage. */
10488 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10490 cp_lexer_consume_token (parser->lexer);
10491 /* See if we might have an id-expression. */
10492 token = cp_lexer_peek_token (parser->lexer);
10493 if (token->type == CPP_NAME
10494 || token->keyword == RID_OPERATOR
10495 || token->type == CPP_SCOPE
10496 || token->type == CPP_TEMPLATE_ID
10497 || token->type == CPP_NESTED_NAME_SPECIFIER)
10499 cp_parser_parse_tentatively (parser);
10500 argument = cp_parser_primary_expression (parser,
10503 /*template_arg_p=*/true,
10505 if (cp_parser_error_occurred (parser)
10506 || !cp_parser_next_token_ends_template_argument_p (parser))
10507 cp_parser_abort_tentative_parse (parser);
10510 if (TREE_CODE (argument) == INDIRECT_REF)
10512 gcc_assert (REFERENCE_REF_P (argument));
10513 argument = TREE_OPERAND (argument, 0);
10516 if (TREE_CODE (argument) == VAR_DECL)
10518 /* A variable without external linkage might still be a
10519 valid constant-expression, so no error is issued here
10520 if the external-linkage check fails. */
10521 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10522 cp_parser_simulate_error (parser);
10524 else if (is_overloaded_fn (argument))
10525 /* All overloaded functions are allowed; if the external
10526 linkage test does not pass, an error will be issued
10530 && (TREE_CODE (argument) == OFFSET_REF
10531 || TREE_CODE (argument) == SCOPE_REF))
10532 /* A pointer-to-member. */
10534 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10537 cp_parser_simulate_error (parser);
10539 if (cp_parser_parse_definitely (parser))
10542 argument = build_x_unary_op (ADDR_EXPR, argument,
10543 tf_warning_or_error);
10548 /* If the argument started with "&", there are no other valid
10549 alternatives at this point. */
10552 cp_parser_error (parser, "invalid non-type template argument");
10553 return error_mark_node;
10556 /* If the argument wasn't successfully parsed as a type-id followed
10557 by '>>', the argument can only be a constant expression now.
10558 Otherwise, we try parsing the constant-expression tentatively,
10559 because the argument could really be a type-id. */
10561 cp_parser_parse_tentatively (parser);
10562 argument = cp_parser_constant_expression (parser,
10563 /*allow_non_constant_p=*/false,
10564 /*non_constant_p=*/NULL);
10565 argument = fold_non_dependent_expr (argument);
10566 if (!maybe_type_id)
10568 if (!cp_parser_next_token_ends_template_argument_p (parser))
10569 cp_parser_error (parser, "expected template-argument");
10570 if (cp_parser_parse_definitely (parser))
10572 /* We did our best to parse the argument as a non type-id, but that
10573 was the only alternative that matched (albeit with a '>' after
10574 it). We can assume it's just a typo from the user, and a
10575 diagnostic will then be issued. */
10576 return cp_parser_type_id (parser);
10579 /* Parse an explicit-instantiation.
10581 explicit-instantiation:
10582 template declaration
10584 Although the standard says `declaration', what it really means is:
10586 explicit-instantiation:
10587 template decl-specifier-seq [opt] declarator [opt] ;
10589 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10590 supposed to be allowed. A defect report has been filed about this
10595 explicit-instantiation:
10596 storage-class-specifier template
10597 decl-specifier-seq [opt] declarator [opt] ;
10598 function-specifier template
10599 decl-specifier-seq [opt] declarator [opt] ; */
10602 cp_parser_explicit_instantiation (cp_parser* parser)
10604 int declares_class_or_enum;
10605 cp_decl_specifier_seq decl_specifiers;
10606 tree extension_specifier = NULL_TREE;
10609 /* Look for an (optional) storage-class-specifier or
10610 function-specifier. */
10611 if (cp_parser_allow_gnu_extensions_p (parser))
10613 extension_specifier
10614 = cp_parser_storage_class_specifier_opt (parser);
10615 if (!extension_specifier)
10616 extension_specifier
10617 = cp_parser_function_specifier_opt (parser,
10618 /*decl_specs=*/NULL);
10621 /* Look for the `template' keyword. */
10622 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10623 /* Let the front end know that we are processing an explicit
10625 begin_explicit_instantiation ();
10626 /* [temp.explicit] says that we are supposed to ignore access
10627 control while processing explicit instantiation directives. */
10628 push_deferring_access_checks (dk_no_check);
10629 /* Parse a decl-specifier-seq. */
10630 token = cp_lexer_peek_token (parser->lexer);
10631 cp_parser_decl_specifier_seq (parser,
10632 CP_PARSER_FLAGS_OPTIONAL,
10634 &declares_class_or_enum);
10635 /* If there was exactly one decl-specifier, and it declared a class,
10636 and there's no declarator, then we have an explicit type
10638 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10642 type = check_tag_decl (&decl_specifiers);
10643 /* Turn access control back on for names used during
10644 template instantiation. */
10645 pop_deferring_access_checks ();
10647 do_type_instantiation (type, extension_specifier,
10648 /*complain=*/tf_error);
10652 cp_declarator *declarator;
10655 /* Parse the declarator. */
10657 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10658 /*ctor_dtor_or_conv_p=*/NULL,
10659 /*parenthesized_p=*/NULL,
10660 /*member_p=*/false);
10661 if (declares_class_or_enum & 2)
10662 cp_parser_check_for_definition_in_return_type (declarator,
10663 decl_specifiers.type,
10664 decl_specifiers.type_location);
10665 if (declarator != cp_error_declarator)
10667 decl = grokdeclarator (declarator, &decl_specifiers,
10668 NORMAL, 0, &decl_specifiers.attributes);
10669 /* Turn access control back on for names used during
10670 template instantiation. */
10671 pop_deferring_access_checks ();
10672 /* Do the explicit instantiation. */
10673 do_decl_instantiation (decl, extension_specifier);
10677 pop_deferring_access_checks ();
10678 /* Skip the body of the explicit instantiation. */
10679 cp_parser_skip_to_end_of_statement (parser);
10682 /* We're done with the instantiation. */
10683 end_explicit_instantiation ();
10685 cp_parser_consume_semicolon_at_end_of_statement (parser);
10688 /* Parse an explicit-specialization.
10690 explicit-specialization:
10691 template < > declaration
10693 Although the standard says `declaration', what it really means is:
10695 explicit-specialization:
10696 template <> decl-specifier [opt] init-declarator [opt] ;
10697 template <> function-definition
10698 template <> explicit-specialization
10699 template <> template-declaration */
10702 cp_parser_explicit_specialization (cp_parser* parser)
10704 bool need_lang_pop;
10705 cp_token *token = cp_lexer_peek_token (parser->lexer);
10707 /* Look for the `template' keyword. */
10708 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10709 /* Look for the `<'. */
10710 cp_parser_require (parser, CPP_LESS, "%<<%>");
10711 /* Look for the `>'. */
10712 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10713 /* We have processed another parameter list. */
10714 ++parser->num_template_parameter_lists;
10717 A template ... explicit specialization ... shall not have C
10719 if (current_lang_name == lang_name_c)
10721 error ("%Htemplate specialization with C linkage", &token->location);
10722 /* Give it C++ linkage to avoid confusing other parts of the
10724 push_lang_context (lang_name_cplusplus);
10725 need_lang_pop = true;
10728 need_lang_pop = false;
10729 /* Let the front end know that we are beginning a specialization. */
10730 if (!begin_specialization ())
10732 end_specialization ();
10733 cp_parser_skip_to_end_of_block_or_statement (parser);
10737 /* If the next keyword is `template', we need to figure out whether
10738 or not we're looking a template-declaration. */
10739 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10741 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10742 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10743 cp_parser_template_declaration_after_export (parser,
10744 /*member_p=*/false);
10746 cp_parser_explicit_specialization (parser);
10749 /* Parse the dependent declaration. */
10750 cp_parser_single_declaration (parser,
10752 /*member_p=*/false,
10753 /*explicit_specialization_p=*/true,
10754 /*friend_p=*/NULL);
10755 /* We're done with the specialization. */
10756 end_specialization ();
10757 /* For the erroneous case of a template with C linkage, we pushed an
10758 implicit C++ linkage scope; exit that scope now. */
10760 pop_lang_context ();
10761 /* We're done with this parameter list. */
10762 --parser->num_template_parameter_lists;
10765 /* Parse a type-specifier.
10768 simple-type-specifier
10771 elaborated-type-specifier
10779 Returns a representation of the type-specifier. For a
10780 class-specifier, enum-specifier, or elaborated-type-specifier, a
10781 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10783 The parser flags FLAGS is used to control type-specifier parsing.
10785 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10786 in a decl-specifier-seq.
10788 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10789 class-specifier, enum-specifier, or elaborated-type-specifier, then
10790 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10791 if a type is declared; 2 if it is defined. Otherwise, it is set to
10794 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10795 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10796 is set to FALSE. */
10799 cp_parser_type_specifier (cp_parser* parser,
10800 cp_parser_flags flags,
10801 cp_decl_specifier_seq *decl_specs,
10802 bool is_declaration,
10803 int* declares_class_or_enum,
10804 bool* is_cv_qualifier)
10806 tree type_spec = NULL_TREE;
10809 cp_decl_spec ds = ds_last;
10811 /* Assume this type-specifier does not declare a new type. */
10812 if (declares_class_or_enum)
10813 *declares_class_or_enum = 0;
10814 /* And that it does not specify a cv-qualifier. */
10815 if (is_cv_qualifier)
10816 *is_cv_qualifier = false;
10817 /* Peek at the next token. */
10818 token = cp_lexer_peek_token (parser->lexer);
10820 /* If we're looking at a keyword, we can use that to guide the
10821 production we choose. */
10822 keyword = token->keyword;
10826 /* Look for the enum-specifier. */
10827 type_spec = cp_parser_enum_specifier (parser);
10828 /* If that worked, we're done. */
10831 if (declares_class_or_enum)
10832 *declares_class_or_enum = 2;
10834 cp_parser_set_decl_spec_type (decl_specs,
10837 /*user_defined_p=*/true);
10841 goto elaborated_type_specifier;
10843 /* Any of these indicate either a class-specifier, or an
10844 elaborated-type-specifier. */
10848 /* Parse tentatively so that we can back up if we don't find a
10849 class-specifier. */
10850 cp_parser_parse_tentatively (parser);
10851 /* Look for the class-specifier. */
10852 type_spec = cp_parser_class_specifier (parser);
10853 /* If that worked, we're done. */
10854 if (cp_parser_parse_definitely (parser))
10856 if (declares_class_or_enum)
10857 *declares_class_or_enum = 2;
10859 cp_parser_set_decl_spec_type (decl_specs,
10862 /*user_defined_p=*/true);
10866 /* Fall through. */
10867 elaborated_type_specifier:
10868 /* We're declaring (not defining) a class or enum. */
10869 if (declares_class_or_enum)
10870 *declares_class_or_enum = 1;
10872 /* Fall through. */
10874 /* Look for an elaborated-type-specifier. */
10876 = (cp_parser_elaborated_type_specifier
10878 decl_specs && decl_specs->specs[(int) ds_friend],
10881 cp_parser_set_decl_spec_type (decl_specs,
10884 /*user_defined_p=*/true);
10889 if (is_cv_qualifier)
10890 *is_cv_qualifier = true;
10895 if (is_cv_qualifier)
10896 *is_cv_qualifier = true;
10901 if (is_cv_qualifier)
10902 *is_cv_qualifier = true;
10906 /* The `__complex__' keyword is a GNU extension. */
10914 /* Handle simple keywords. */
10919 ++decl_specs->specs[(int)ds];
10920 decl_specs->any_specifiers_p = true;
10922 return cp_lexer_consume_token (parser->lexer)->u.value;
10925 /* If we do not already have a type-specifier, assume we are looking
10926 at a simple-type-specifier. */
10927 type_spec = cp_parser_simple_type_specifier (parser,
10931 /* If we didn't find a type-specifier, and a type-specifier was not
10932 optional in this context, issue an error message. */
10933 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10935 cp_parser_error (parser, "expected type specifier");
10936 return error_mark_node;
10942 /* Parse a simple-type-specifier.
10944 simple-type-specifier:
10945 :: [opt] nested-name-specifier [opt] type-name
10946 :: [opt] nested-name-specifier template template-id
10961 simple-type-specifier:
10963 decltype ( expression )
10969 simple-type-specifier:
10970 __typeof__ unary-expression
10971 __typeof__ ( type-id )
10973 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10974 appropriately updated. */
10977 cp_parser_simple_type_specifier (cp_parser* parser,
10978 cp_decl_specifier_seq *decl_specs,
10979 cp_parser_flags flags)
10981 tree type = NULL_TREE;
10984 /* Peek at the next token. */
10985 token = cp_lexer_peek_token (parser->lexer);
10987 /* If we're looking at a keyword, things are easy. */
10988 switch (token->keyword)
10992 decl_specs->explicit_char_p = true;
10993 type = char_type_node;
10996 type = char16_type_node;
10999 type = char32_type_node;
11002 type = wchar_type_node;
11005 type = boolean_type_node;
11009 ++decl_specs->specs[(int) ds_short];
11010 type = short_integer_type_node;
11014 decl_specs->explicit_int_p = true;
11015 type = integer_type_node;
11019 ++decl_specs->specs[(int) ds_long];
11020 type = long_integer_type_node;
11024 ++decl_specs->specs[(int) ds_signed];
11025 type = integer_type_node;
11029 ++decl_specs->specs[(int) ds_unsigned];
11030 type = unsigned_type_node;
11033 type = float_type_node;
11036 type = double_type_node;
11039 type = void_type_node;
11043 if (cxx_dialect != cxx98)
11045 /* Consume the token. */
11046 cp_lexer_consume_token (parser->lexer);
11047 /* We do not yet support the use of `auto' as a
11049 error ("%HC++0x %<auto%> specifier not supported", &token->location);
11054 /* Parse the `decltype' type. */
11055 type = cp_parser_decltype (parser);
11058 cp_parser_set_decl_spec_type (decl_specs, type,
11060 /*user_defined_p=*/true);
11065 /* Consume the `typeof' token. */
11066 cp_lexer_consume_token (parser->lexer);
11067 /* Parse the operand to `typeof'. */
11068 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11069 /* If it is not already a TYPE, take its type. */
11070 if (!TYPE_P (type))
11071 type = finish_typeof (type);
11074 cp_parser_set_decl_spec_type (decl_specs, type,
11076 /*user_defined_p=*/true);
11084 /* If the type-specifier was for a built-in type, we're done. */
11089 /* Record the type. */
11091 && (token->keyword != RID_SIGNED
11092 && token->keyword != RID_UNSIGNED
11093 && token->keyword != RID_SHORT
11094 && token->keyword != RID_LONG))
11095 cp_parser_set_decl_spec_type (decl_specs,
11098 /*user_defined=*/false);
11100 decl_specs->any_specifiers_p = true;
11102 /* Consume the token. */
11103 id = cp_lexer_consume_token (parser->lexer)->u.value;
11105 /* There is no valid C++ program where a non-template type is
11106 followed by a "<". That usually indicates that the user thought
11107 that the type was a template. */
11108 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11110 return TYPE_NAME (type);
11113 /* The type-specifier must be a user-defined type. */
11114 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11119 /* Don't gobble tokens or issue error messages if this is an
11120 optional type-specifier. */
11121 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11122 cp_parser_parse_tentatively (parser);
11124 /* Look for the optional `::' operator. */
11126 = (cp_parser_global_scope_opt (parser,
11127 /*current_scope_valid_p=*/false)
11129 /* Look for the nested-name specifier. */
11131 = (cp_parser_nested_name_specifier_opt (parser,
11132 /*typename_keyword_p=*/false,
11133 /*check_dependency_p=*/true,
11135 /*is_declaration=*/false)
11137 token = cp_lexer_peek_token (parser->lexer);
11138 /* If we have seen a nested-name-specifier, and the next token
11139 is `template', then we are using the template-id production. */
11141 && cp_parser_optional_template_keyword (parser))
11143 /* Look for the template-id. */
11144 type = cp_parser_template_id (parser,
11145 /*template_keyword_p=*/true,
11146 /*check_dependency_p=*/true,
11147 /*is_declaration=*/false);
11148 /* If the template-id did not name a type, we are out of
11150 if (TREE_CODE (type) != TYPE_DECL)
11152 cp_parser_error (parser, "expected template-id for type");
11156 /* Otherwise, look for a type-name. */
11158 type = cp_parser_type_name (parser);
11159 /* Keep track of all name-lookups performed in class scopes. */
11163 && TREE_CODE (type) == TYPE_DECL
11164 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11165 maybe_note_name_used_in_class (DECL_NAME (type), type);
11166 /* If it didn't work out, we don't have a TYPE. */
11167 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11168 && !cp_parser_parse_definitely (parser))
11170 if (type && decl_specs)
11171 cp_parser_set_decl_spec_type (decl_specs, type,
11173 /*user_defined=*/true);
11176 /* If we didn't get a type-name, issue an error message. */
11177 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11179 cp_parser_error (parser, "expected type-name");
11180 return error_mark_node;
11183 /* There is no valid C++ program where a non-template type is
11184 followed by a "<". That usually indicates that the user thought
11185 that the type was a template. */
11186 if (type && type != error_mark_node)
11188 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11189 If it is, then the '<'...'>' enclose protocol names rather than
11190 template arguments, and so everything is fine. */
11191 if (c_dialect_objc ()
11192 && (objc_is_id (type) || objc_is_class_name (type)))
11194 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11195 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11197 /* Clobber the "unqualified" type previously entered into
11198 DECL_SPECS with the new, improved protocol-qualified version. */
11200 decl_specs->type = qual_type;
11205 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11212 /* Parse a type-name.
11225 Returns a TYPE_DECL for the type. */
11228 cp_parser_type_name (cp_parser* parser)
11232 /* We can't know yet whether it is a class-name or not. */
11233 cp_parser_parse_tentatively (parser);
11234 /* Try a class-name. */
11235 type_decl = cp_parser_class_name (parser,
11236 /*typename_keyword_p=*/false,
11237 /*template_keyword_p=*/false,
11239 /*check_dependency_p=*/true,
11240 /*class_head_p=*/false,
11241 /*is_declaration=*/false);
11242 /* If it's not a class-name, keep looking. */
11243 if (!cp_parser_parse_definitely (parser))
11245 /* It must be a typedef-name or an enum-name. */
11246 return cp_parser_nonclass_name (parser);
11252 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11260 Returns a TYPE_DECL for the type. */
11263 cp_parser_nonclass_name (cp_parser* parser)
11268 cp_token *token = cp_lexer_peek_token (parser->lexer);
11269 identifier = cp_parser_identifier (parser);
11270 if (identifier == error_mark_node)
11271 return error_mark_node;
11273 /* Look up the type-name. */
11274 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11276 if (TREE_CODE (type_decl) != TYPE_DECL
11277 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11279 /* See if this is an Objective-C type. */
11280 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11281 tree type = objc_get_protocol_qualified_type (identifier, protos);
11283 type_decl = TYPE_NAME (type);
11286 /* Issue an error if we did not find a type-name. */
11287 if (TREE_CODE (type_decl) != TYPE_DECL)
11289 if (!cp_parser_simulate_error (parser))
11290 cp_parser_name_lookup_error (parser, identifier, type_decl,
11291 "is not a type", token->location);
11292 return error_mark_node;
11294 /* Remember that the name was used in the definition of the
11295 current class so that we can check later to see if the
11296 meaning would have been different after the class was
11297 entirely defined. */
11298 else if (type_decl != error_mark_node
11300 maybe_note_name_used_in_class (identifier, type_decl);
11305 /* Parse an elaborated-type-specifier. Note that the grammar given
11306 here incorporates the resolution to DR68.
11308 elaborated-type-specifier:
11309 class-key :: [opt] nested-name-specifier [opt] identifier
11310 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11311 enum :: [opt] nested-name-specifier [opt] identifier
11312 typename :: [opt] nested-name-specifier identifier
11313 typename :: [opt] nested-name-specifier template [opt]
11318 elaborated-type-specifier:
11319 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11320 class-key attributes :: [opt] nested-name-specifier [opt]
11321 template [opt] template-id
11322 enum attributes :: [opt] nested-name-specifier [opt] identifier
11324 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11325 declared `friend'. If IS_DECLARATION is TRUE, then this
11326 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11327 something is being declared.
11329 Returns the TYPE specified. */
11332 cp_parser_elaborated_type_specifier (cp_parser* parser,
11334 bool is_declaration)
11336 enum tag_types tag_type;
11338 tree type = NULL_TREE;
11339 tree attributes = NULL_TREE;
11340 cp_token *token = NULL;
11342 /* See if we're looking at the `enum' keyword. */
11343 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11345 /* Consume the `enum' token. */
11346 cp_lexer_consume_token (parser->lexer);
11347 /* Remember that it's an enumeration type. */
11348 tag_type = enum_type;
11349 /* Parse the attributes. */
11350 attributes = cp_parser_attributes_opt (parser);
11352 /* Or, it might be `typename'. */
11353 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11356 /* Consume the `typename' token. */
11357 cp_lexer_consume_token (parser->lexer);
11358 /* Remember that it's a `typename' type. */
11359 tag_type = typename_type;
11360 /* The `typename' keyword is only allowed in templates. */
11361 if (!processing_template_decl)
11362 permerror ("using %<typename%> outside of template");
11364 /* Otherwise it must be a class-key. */
11367 tag_type = cp_parser_class_key (parser);
11368 if (tag_type == none_type)
11369 return error_mark_node;
11370 /* Parse the attributes. */
11371 attributes = cp_parser_attributes_opt (parser);
11374 /* Look for the `::' operator. */
11375 cp_parser_global_scope_opt (parser,
11376 /*current_scope_valid_p=*/false);
11377 /* Look for the nested-name-specifier. */
11378 if (tag_type == typename_type)
11380 if (!cp_parser_nested_name_specifier (parser,
11381 /*typename_keyword_p=*/true,
11382 /*check_dependency_p=*/true,
11385 return error_mark_node;
11388 /* Even though `typename' is not present, the proposed resolution
11389 to Core Issue 180 says that in `class A<T>::B', `B' should be
11390 considered a type-name, even if `A<T>' is dependent. */
11391 cp_parser_nested_name_specifier_opt (parser,
11392 /*typename_keyword_p=*/true,
11393 /*check_dependency_p=*/true,
11396 /* For everything but enumeration types, consider a template-id.
11397 For an enumeration type, consider only a plain identifier. */
11398 if (tag_type != enum_type)
11400 bool template_p = false;
11403 /* Allow the `template' keyword. */
11404 template_p = cp_parser_optional_template_keyword (parser);
11405 /* If we didn't see `template', we don't know if there's a
11406 template-id or not. */
11408 cp_parser_parse_tentatively (parser);
11409 /* Parse the template-id. */
11410 token = cp_lexer_peek_token (parser->lexer);
11411 decl = cp_parser_template_id (parser, template_p,
11412 /*check_dependency_p=*/true,
11414 /* If we didn't find a template-id, look for an ordinary
11416 if (!template_p && !cp_parser_parse_definitely (parser))
11418 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11419 in effect, then we must assume that, upon instantiation, the
11420 template will correspond to a class. */
11421 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11422 && tag_type == typename_type)
11423 type = make_typename_type (parser->scope, decl,
11425 /*complain=*/tf_error);
11427 type = TREE_TYPE (decl);
11432 token = cp_lexer_peek_token (parser->lexer);
11433 identifier = cp_parser_identifier (parser);
11435 if (identifier == error_mark_node)
11437 parser->scope = NULL_TREE;
11438 return error_mark_node;
11441 /* For a `typename', we needn't call xref_tag. */
11442 if (tag_type == typename_type
11443 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11444 return cp_parser_make_typename_type (parser, parser->scope,
11447 /* Look up a qualified name in the usual way. */
11451 tree ambiguous_decls;
11453 decl = cp_parser_lookup_name (parser, identifier,
11455 /*is_template=*/false,
11456 /*is_namespace=*/false,
11457 /*check_dependency=*/true,
11461 /* If the lookup was ambiguous, an error will already have been
11463 if (ambiguous_decls)
11464 return error_mark_node;
11466 /* If we are parsing friend declaration, DECL may be a
11467 TEMPLATE_DECL tree node here. However, we need to check
11468 whether this TEMPLATE_DECL results in valid code. Consider
11469 the following example:
11472 template <class T> class C {};
11475 template <class T> friend class N::C; // #1, valid code
11477 template <class T> class Y {
11478 friend class N::C; // #2, invalid code
11481 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11482 name lookup of `N::C'. We see that friend declaration must
11483 be template for the code to be valid. Note that
11484 processing_template_decl does not work here since it is
11485 always 1 for the above two cases. */
11487 decl = (cp_parser_maybe_treat_template_as_class
11488 (decl, /*tag_name_p=*/is_friend
11489 && parser->num_template_parameter_lists));
11491 if (TREE_CODE (decl) != TYPE_DECL)
11493 cp_parser_diagnose_invalid_type_name (parser,
11497 return error_mark_node;
11500 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11502 bool allow_template = (parser->num_template_parameter_lists
11503 || DECL_SELF_REFERENCE_P (decl));
11504 type = check_elaborated_type_specifier (tag_type, decl,
11507 if (type == error_mark_node)
11508 return error_mark_node;
11511 /* Forward declarations of nested types, such as
11516 are invalid unless all components preceding the final '::'
11517 are complete. If all enclosing types are complete, these
11518 declarations become merely pointless.
11520 Invalid forward declarations of nested types are errors
11521 caught elsewhere in parsing. Those that are pointless arrive
11524 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11525 && !is_friend && !processing_explicit_instantiation)
11526 warning (0, "declaration %qD does not declare anything", decl);
11528 type = TREE_TYPE (decl);
11532 /* An elaborated-type-specifier sometimes introduces a new type and
11533 sometimes names an existing type. Normally, the rule is that it
11534 introduces a new type only if there is not an existing type of
11535 the same name already in scope. For example, given:
11538 void f() { struct S s; }
11540 the `struct S' in the body of `f' is the same `struct S' as in
11541 the global scope; the existing definition is used. However, if
11542 there were no global declaration, this would introduce a new
11543 local class named `S'.
11545 An exception to this rule applies to the following code:
11547 namespace N { struct S; }
11549 Here, the elaborated-type-specifier names a new type
11550 unconditionally; even if there is already an `S' in the
11551 containing scope this declaration names a new type.
11552 This exception only applies if the elaborated-type-specifier
11553 forms the complete declaration:
11557 A declaration consisting solely of `class-key identifier ;' is
11558 either a redeclaration of the name in the current scope or a
11559 forward declaration of the identifier as a class name. It
11560 introduces the name into the current scope.
11562 We are in this situation precisely when the next token is a `;'.
11564 An exception to the exception is that a `friend' declaration does
11565 *not* name a new type; i.e., given:
11567 struct S { friend struct T; };
11569 `T' is not a new type in the scope of `S'.
11571 Also, `new struct S' or `sizeof (struct S)' never results in the
11572 definition of a new type; a new type can only be declared in a
11573 declaration context. */
11579 /* Friends have special name lookup rules. */
11580 ts = ts_within_enclosing_non_class;
11581 else if (is_declaration
11582 && cp_lexer_next_token_is (parser->lexer,
11584 /* This is a `class-key identifier ;' */
11590 (parser->num_template_parameter_lists
11591 && (cp_parser_next_token_starts_class_definition_p (parser)
11592 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11593 /* An unqualified name was used to reference this type, so
11594 there were no qualifying templates. */
11595 if (!cp_parser_check_template_parameters (parser,
11596 /*num_templates=*/0,
11598 return error_mark_node;
11599 type = xref_tag (tag_type, identifier, ts, template_p);
11603 if (type == error_mark_node)
11604 return error_mark_node;
11606 /* Allow attributes on forward declarations of classes. */
11609 if (TREE_CODE (type) == TYPENAME_TYPE)
11610 warning (OPT_Wattributes,
11611 "attributes ignored on uninstantiated type");
11612 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11613 && ! processing_explicit_instantiation)
11614 warning (OPT_Wattributes,
11615 "attributes ignored on template instantiation");
11616 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11617 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11619 warning (OPT_Wattributes,
11620 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11623 if (tag_type != enum_type)
11624 cp_parser_check_class_key (tag_type, type);
11626 /* A "<" cannot follow an elaborated type specifier. If that
11627 happens, the user was probably trying to form a template-id. */
11628 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11633 /* Parse an enum-specifier.
11636 enum identifier [opt] { enumerator-list [opt] }
11639 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11642 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11643 if the token stream isn't an enum-specifier after all. */
11646 cp_parser_enum_specifier (cp_parser* parser)
11652 /* Parse tentatively so that we can back up if we don't find a
11654 cp_parser_parse_tentatively (parser);
11656 /* Caller guarantees that the current token is 'enum', an identifier
11657 possibly follows, and the token after that is an opening brace.
11658 If we don't have an identifier, fabricate an anonymous name for
11659 the enumeration being defined. */
11660 cp_lexer_consume_token (parser->lexer);
11662 attributes = cp_parser_attributes_opt (parser);
11664 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11665 identifier = cp_parser_identifier (parser);
11667 identifier = make_anon_name ();
11669 /* Look for the `{' but don't consume it yet. */
11670 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11671 cp_parser_simulate_error (parser);
11673 if (!cp_parser_parse_definitely (parser))
11676 /* Issue an error message if type-definitions are forbidden here. */
11677 if (!cp_parser_check_type_definition (parser))
11678 type = error_mark_node;
11680 /* Create the new type. We do this before consuming the opening
11681 brace so the enum will be recorded as being on the line of its
11682 tag (or the 'enum' keyword, if there is no tag). */
11683 type = start_enum (identifier);
11685 /* Consume the opening brace. */
11686 cp_lexer_consume_token (parser->lexer);
11688 if (type == error_mark_node)
11690 cp_parser_skip_to_end_of_block_or_statement (parser);
11691 return error_mark_node;
11694 /* If the next token is not '}', then there are some enumerators. */
11695 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11696 cp_parser_enumerator_list (parser, type);
11698 /* Consume the final '}'. */
11699 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11701 /* Look for trailing attributes to apply to this enumeration, and
11702 apply them if appropriate. */
11703 if (cp_parser_allow_gnu_extensions_p (parser))
11705 tree trailing_attr = cp_parser_attributes_opt (parser);
11706 cplus_decl_attributes (&type,
11708 (int) ATTR_FLAG_TYPE_IN_PLACE);
11711 /* Finish up the enumeration. */
11712 finish_enum (type);
11717 /* Parse an enumerator-list. The enumerators all have the indicated
11721 enumerator-definition
11722 enumerator-list , enumerator-definition */
11725 cp_parser_enumerator_list (cp_parser* parser, tree type)
11729 /* Parse an enumerator-definition. */
11730 cp_parser_enumerator_definition (parser, type);
11732 /* If the next token is not a ',', we've reached the end of
11734 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11736 /* Otherwise, consume the `,' and keep going. */
11737 cp_lexer_consume_token (parser->lexer);
11738 /* If the next token is a `}', there is a trailing comma. */
11739 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11741 if (!in_system_header)
11742 pedwarn (OPT_pedantic, "comma at end of enumerator list");
11748 /* Parse an enumerator-definition. The enumerator has the indicated
11751 enumerator-definition:
11753 enumerator = constant-expression
11759 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11764 /* Look for the identifier. */
11765 identifier = cp_parser_identifier (parser);
11766 if (identifier == error_mark_node)
11769 /* If the next token is an '=', then there is an explicit value. */
11770 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11772 /* Consume the `=' token. */
11773 cp_lexer_consume_token (parser->lexer);
11774 /* Parse the value. */
11775 value = cp_parser_constant_expression (parser,
11776 /*allow_non_constant_p=*/false,
11782 /* Create the enumerator. */
11783 build_enumerator (identifier, value, type);
11786 /* Parse a namespace-name.
11789 original-namespace-name
11792 Returns the NAMESPACE_DECL for the namespace. */
11795 cp_parser_namespace_name (cp_parser* parser)
11798 tree namespace_decl;
11800 cp_token *token = cp_lexer_peek_token (parser->lexer);
11802 /* Get the name of the namespace. */
11803 identifier = cp_parser_identifier (parser);
11804 if (identifier == error_mark_node)
11805 return error_mark_node;
11807 /* Look up the identifier in the currently active scope. Look only
11808 for namespaces, due to:
11810 [basic.lookup.udir]
11812 When looking up a namespace-name in a using-directive or alias
11813 definition, only namespace names are considered.
11817 [basic.lookup.qual]
11819 During the lookup of a name preceding the :: scope resolution
11820 operator, object, function, and enumerator names are ignored.
11822 (Note that cp_parser_class_or_namespace_name only calls this
11823 function if the token after the name is the scope resolution
11825 namespace_decl = cp_parser_lookup_name (parser, identifier,
11827 /*is_template=*/false,
11828 /*is_namespace=*/true,
11829 /*check_dependency=*/true,
11830 /*ambiguous_decls=*/NULL,
11832 /* If it's not a namespace, issue an error. */
11833 if (namespace_decl == error_mark_node
11834 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11836 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11837 error ("%H%qD is not a namespace-name", &token->location, identifier);
11838 cp_parser_error (parser, "expected namespace-name");
11839 namespace_decl = error_mark_node;
11842 return namespace_decl;
11845 /* Parse a namespace-definition.
11847 namespace-definition:
11848 named-namespace-definition
11849 unnamed-namespace-definition
11851 named-namespace-definition:
11852 original-namespace-definition
11853 extension-namespace-definition
11855 original-namespace-definition:
11856 namespace identifier { namespace-body }
11858 extension-namespace-definition:
11859 namespace original-namespace-name { namespace-body }
11861 unnamed-namespace-definition:
11862 namespace { namespace-body } */
11865 cp_parser_namespace_definition (cp_parser* parser)
11867 tree identifier, attribs;
11868 bool has_visibility;
11871 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11874 cp_lexer_consume_token (parser->lexer);
11879 /* Look for the `namespace' keyword. */
11880 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11882 /* Get the name of the namespace. We do not attempt to distinguish
11883 between an original-namespace-definition and an
11884 extension-namespace-definition at this point. The semantic
11885 analysis routines are responsible for that. */
11886 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11887 identifier = cp_parser_identifier (parser);
11889 identifier = NULL_TREE;
11891 /* Parse any specified attributes. */
11892 attribs = cp_parser_attributes_opt (parser);
11894 /* Look for the `{' to start the namespace. */
11895 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11896 /* Start the namespace. */
11897 push_namespace (identifier);
11899 /* "inline namespace" is equivalent to a stub namespace definition
11900 followed by a strong using directive. */
11903 tree name_space = current_namespace;
11904 /* Set up namespace association. */
11905 DECL_NAMESPACE_ASSOCIATIONS (name_space)
11906 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
11907 DECL_NAMESPACE_ASSOCIATIONS (name_space));
11908 /* Import the contents of the inline namespace. */
11910 do_using_directive (name_space);
11911 push_namespace (identifier);
11914 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11916 /* Parse the body of the namespace. */
11917 cp_parser_namespace_body (parser);
11919 #ifdef HANDLE_PRAGMA_VISIBILITY
11920 if (has_visibility)
11924 /* Finish the namespace. */
11926 /* Look for the final `}'. */
11927 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11930 /* Parse a namespace-body.
11933 declaration-seq [opt] */
11936 cp_parser_namespace_body (cp_parser* parser)
11938 cp_parser_declaration_seq_opt (parser);
11941 /* Parse a namespace-alias-definition.
11943 namespace-alias-definition:
11944 namespace identifier = qualified-namespace-specifier ; */
11947 cp_parser_namespace_alias_definition (cp_parser* parser)
11950 tree namespace_specifier;
11952 cp_token *token = cp_lexer_peek_token (parser->lexer);
11954 /* Look for the `namespace' keyword. */
11955 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11956 /* Look for the identifier. */
11957 identifier = cp_parser_identifier (parser);
11958 if (identifier == error_mark_node)
11960 /* Look for the `=' token. */
11961 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11962 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11964 error ("%H%<namespace%> definition is not allowed here", &token->location);
11965 /* Skip the definition. */
11966 cp_lexer_consume_token (parser->lexer);
11967 if (cp_parser_skip_to_closing_brace (parser))
11968 cp_lexer_consume_token (parser->lexer);
11971 cp_parser_require (parser, CPP_EQ, "%<=%>");
11972 /* Look for the qualified-namespace-specifier. */
11973 namespace_specifier
11974 = cp_parser_qualified_namespace_specifier (parser);
11975 /* Look for the `;' token. */
11976 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11978 /* Register the alias in the symbol table. */
11979 do_namespace_alias (identifier, namespace_specifier);
11982 /* Parse a qualified-namespace-specifier.
11984 qualified-namespace-specifier:
11985 :: [opt] nested-name-specifier [opt] namespace-name
11987 Returns a NAMESPACE_DECL corresponding to the specified
11991 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11993 /* Look for the optional `::'. */
11994 cp_parser_global_scope_opt (parser,
11995 /*current_scope_valid_p=*/false);
11997 /* Look for the optional nested-name-specifier. */
11998 cp_parser_nested_name_specifier_opt (parser,
11999 /*typename_keyword_p=*/false,
12000 /*check_dependency_p=*/true,
12002 /*is_declaration=*/true);
12004 return cp_parser_namespace_name (parser);
12007 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12008 access declaration.
12011 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12012 using :: unqualified-id ;
12014 access-declaration:
12020 cp_parser_using_declaration (cp_parser* parser,
12021 bool access_declaration_p)
12024 bool typename_p = false;
12025 bool global_scope_p;
12030 if (access_declaration_p)
12031 cp_parser_parse_tentatively (parser);
12034 /* Look for the `using' keyword. */
12035 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12037 /* Peek at the next token. */
12038 token = cp_lexer_peek_token (parser->lexer);
12039 /* See if it's `typename'. */
12040 if (token->keyword == RID_TYPENAME)
12042 /* Remember that we've seen it. */
12044 /* Consume the `typename' token. */
12045 cp_lexer_consume_token (parser->lexer);
12049 /* Look for the optional global scope qualification. */
12051 = (cp_parser_global_scope_opt (parser,
12052 /*current_scope_valid_p=*/false)
12055 /* If we saw `typename', or didn't see `::', then there must be a
12056 nested-name-specifier present. */
12057 if (typename_p || !global_scope_p)
12058 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12059 /*check_dependency_p=*/true,
12061 /*is_declaration=*/true);
12062 /* Otherwise, we could be in either of the two productions. In that
12063 case, treat the nested-name-specifier as optional. */
12065 qscope = cp_parser_nested_name_specifier_opt (parser,
12066 /*typename_keyword_p=*/false,
12067 /*check_dependency_p=*/true,
12069 /*is_declaration=*/true);
12071 qscope = global_namespace;
12073 if (access_declaration_p && cp_parser_error_occurred (parser))
12074 /* Something has already gone wrong; there's no need to parse
12075 further. Since an error has occurred, the return value of
12076 cp_parser_parse_definitely will be false, as required. */
12077 return cp_parser_parse_definitely (parser);
12079 token = cp_lexer_peek_token (parser->lexer);
12080 /* Parse the unqualified-id. */
12081 identifier = cp_parser_unqualified_id (parser,
12082 /*template_keyword_p=*/false,
12083 /*check_dependency_p=*/true,
12084 /*declarator_p=*/true,
12085 /*optional_p=*/false);
12087 if (access_declaration_p)
12089 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12090 cp_parser_simulate_error (parser);
12091 if (!cp_parser_parse_definitely (parser))
12095 /* The function we call to handle a using-declaration is different
12096 depending on what scope we are in. */
12097 if (qscope == error_mark_node || identifier == error_mark_node)
12099 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12100 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12101 /* [namespace.udecl]
12103 A using declaration shall not name a template-id. */
12104 error ("%Ha template-id may not appear in a using-declaration",
12108 if (at_class_scope_p ())
12110 /* Create the USING_DECL. */
12111 decl = do_class_using_decl (parser->scope, identifier);
12113 if (check_for_bare_parameter_packs (decl))
12116 /* Add it to the list of members in this class. */
12117 finish_member_declaration (decl);
12121 decl = cp_parser_lookup_name_simple (parser,
12124 if (decl == error_mark_node)
12125 cp_parser_name_lookup_error (parser, identifier,
12128 else if (check_for_bare_parameter_packs (decl))
12130 else if (!at_namespace_scope_p ())
12131 do_local_using_decl (decl, qscope, identifier);
12133 do_toplevel_using_decl (decl, qscope, identifier);
12137 /* Look for the final `;'. */
12138 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12143 /* Parse a using-directive.
12146 using namespace :: [opt] nested-name-specifier [opt]
12147 namespace-name ; */
12150 cp_parser_using_directive (cp_parser* parser)
12152 tree namespace_decl;
12155 /* Look for the `using' keyword. */
12156 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12157 /* And the `namespace' keyword. */
12158 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12159 /* Look for the optional `::' operator. */
12160 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12161 /* And the optional nested-name-specifier. */
12162 cp_parser_nested_name_specifier_opt (parser,
12163 /*typename_keyword_p=*/false,
12164 /*check_dependency_p=*/true,
12166 /*is_declaration=*/true);
12167 /* Get the namespace being used. */
12168 namespace_decl = cp_parser_namespace_name (parser);
12169 /* And any specified attributes. */
12170 attribs = cp_parser_attributes_opt (parser);
12171 /* Update the symbol table. */
12172 parse_using_directive (namespace_decl, attribs);
12173 /* Look for the final `;'. */
12174 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12177 /* Parse an asm-definition.
12180 asm ( string-literal ) ;
12185 asm volatile [opt] ( string-literal ) ;
12186 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12187 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12188 : asm-operand-list [opt] ) ;
12189 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12190 : asm-operand-list [opt]
12191 : asm-operand-list [opt] ) ; */
12194 cp_parser_asm_definition (cp_parser* parser)
12197 tree outputs = NULL_TREE;
12198 tree inputs = NULL_TREE;
12199 tree clobbers = NULL_TREE;
12201 bool volatile_p = false;
12202 bool extended_p = false;
12203 bool invalid_inputs_p = false;
12204 bool invalid_outputs_p = false;
12206 /* Look for the `asm' keyword. */
12207 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12208 /* See if the next token is `volatile'. */
12209 if (cp_parser_allow_gnu_extensions_p (parser)
12210 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12212 /* Remember that we saw the `volatile' keyword. */
12214 /* Consume the token. */
12215 cp_lexer_consume_token (parser->lexer);
12217 /* Look for the opening `('. */
12218 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12220 /* Look for the string. */
12221 string = cp_parser_string_literal (parser, false, false);
12222 if (string == error_mark_node)
12224 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12225 /*consume_paren=*/true);
12229 /* If we're allowing GNU extensions, check for the extended assembly
12230 syntax. Unfortunately, the `:' tokens need not be separated by
12231 a space in C, and so, for compatibility, we tolerate that here
12232 too. Doing that means that we have to treat the `::' operator as
12234 if (cp_parser_allow_gnu_extensions_p (parser)
12235 && parser->in_function_body
12236 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12237 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12239 bool inputs_p = false;
12240 bool clobbers_p = false;
12242 /* The extended syntax was used. */
12245 /* Look for outputs. */
12246 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12248 /* Consume the `:'. */
12249 cp_lexer_consume_token (parser->lexer);
12250 /* Parse the output-operands. */
12251 if (cp_lexer_next_token_is_not (parser->lexer,
12253 && cp_lexer_next_token_is_not (parser->lexer,
12255 && cp_lexer_next_token_is_not (parser->lexer,
12257 outputs = cp_parser_asm_operand_list (parser);
12259 if (outputs == error_mark_node)
12260 invalid_outputs_p = true;
12262 /* If the next token is `::', there are no outputs, and the
12263 next token is the beginning of the inputs. */
12264 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12265 /* The inputs are coming next. */
12268 /* Look for inputs. */
12270 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12272 /* Consume the `:' or `::'. */
12273 cp_lexer_consume_token (parser->lexer);
12274 /* Parse the output-operands. */
12275 if (cp_lexer_next_token_is_not (parser->lexer,
12277 && cp_lexer_next_token_is_not (parser->lexer,
12279 inputs = cp_parser_asm_operand_list (parser);
12281 if (inputs == error_mark_node)
12282 invalid_inputs_p = true;
12284 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12285 /* The clobbers are coming next. */
12288 /* Look for clobbers. */
12290 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12292 /* Consume the `:' or `::'. */
12293 cp_lexer_consume_token (parser->lexer);
12294 /* Parse the clobbers. */
12295 if (cp_lexer_next_token_is_not (parser->lexer,
12297 clobbers = cp_parser_asm_clobber_list (parser);
12300 /* Look for the closing `)'. */
12301 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12302 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12303 /*consume_paren=*/true);
12304 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12306 if (!invalid_inputs_p && !invalid_outputs_p)
12308 /* Create the ASM_EXPR. */
12309 if (parser->in_function_body)
12311 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12313 /* If the extended syntax was not used, mark the ASM_EXPR. */
12316 tree temp = asm_stmt;
12317 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12318 temp = TREE_OPERAND (temp, 0);
12320 ASM_INPUT_P (temp) = 1;
12324 cgraph_add_asm_node (string);
12328 /* Declarators [gram.dcl.decl] */
12330 /* Parse an init-declarator.
12333 declarator initializer [opt]
12338 declarator asm-specification [opt] attributes [opt] initializer [opt]
12340 function-definition:
12341 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12343 decl-specifier-seq [opt] declarator function-try-block
12347 function-definition:
12348 __extension__ function-definition
12350 The DECL_SPECIFIERS apply to this declarator. Returns a
12351 representation of the entity declared. If MEMBER_P is TRUE, then
12352 this declarator appears in a class scope. The new DECL created by
12353 this declarator is returned.
12355 The CHECKS are access checks that should be performed once we know
12356 what entity is being declared (and, therefore, what classes have
12359 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12360 for a function-definition here as well. If the declarator is a
12361 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12362 be TRUE upon return. By that point, the function-definition will
12363 have been completely parsed.
12365 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12369 cp_parser_init_declarator (cp_parser* parser,
12370 cp_decl_specifier_seq *decl_specifiers,
12371 VEC (deferred_access_check,gc)* checks,
12372 bool function_definition_allowed_p,
12374 int declares_class_or_enum,
12375 bool* function_definition_p)
12377 cp_token *token = NULL, *asm_spec_start_token = NULL,
12378 *attributes_start_token = NULL;
12379 cp_declarator *declarator;
12380 tree prefix_attributes;
12382 tree asm_specification;
12384 tree decl = NULL_TREE;
12386 int is_initialized;
12387 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12388 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12390 enum cpp_ttype initialization_kind;
12391 bool is_direct_init = false;
12392 bool is_non_constant_init;
12393 int ctor_dtor_or_conv_p;
12395 tree pushed_scope = NULL;
12397 /* Gather the attributes that were provided with the
12398 decl-specifiers. */
12399 prefix_attributes = decl_specifiers->attributes;
12401 /* Assume that this is not the declarator for a function
12403 if (function_definition_p)
12404 *function_definition_p = false;
12406 /* Defer access checks while parsing the declarator; we cannot know
12407 what names are accessible until we know what is being
12409 resume_deferring_access_checks ();
12411 /* Parse the declarator. */
12412 token = cp_lexer_peek_token (parser->lexer);
12414 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12415 &ctor_dtor_or_conv_p,
12416 /*parenthesized_p=*/NULL,
12417 /*member_p=*/false);
12418 /* Gather up the deferred checks. */
12419 stop_deferring_access_checks ();
12421 /* If the DECLARATOR was erroneous, there's no need to go
12423 if (declarator == cp_error_declarator)
12424 return error_mark_node;
12426 /* Check that the number of template-parameter-lists is OK. */
12427 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12429 return error_mark_node;
12431 if (declares_class_or_enum & 2)
12432 cp_parser_check_for_definition_in_return_type (declarator,
12433 decl_specifiers->type,
12434 decl_specifiers->type_location);
12436 /* Figure out what scope the entity declared by the DECLARATOR is
12437 located in. `grokdeclarator' sometimes changes the scope, so
12438 we compute it now. */
12439 scope = get_scope_of_declarator (declarator);
12441 /* If we're allowing GNU extensions, look for an asm-specification
12443 if (cp_parser_allow_gnu_extensions_p (parser))
12445 /* Look for an asm-specification. */
12446 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12447 asm_specification = cp_parser_asm_specification_opt (parser);
12448 /* And attributes. */
12449 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12450 attributes = cp_parser_attributes_opt (parser);
12454 asm_specification = NULL_TREE;
12455 attributes = NULL_TREE;
12458 /* Peek at the next token. */
12459 token = cp_lexer_peek_token (parser->lexer);
12460 /* Check to see if the token indicates the start of a
12461 function-definition. */
12462 if (function_declarator_p (declarator)
12463 && cp_parser_token_starts_function_definition_p (token))
12465 if (!function_definition_allowed_p)
12467 /* If a function-definition should not appear here, issue an
12469 cp_parser_error (parser,
12470 "a function-definition is not allowed here");
12471 return error_mark_node;
12475 /* Neither attributes nor an asm-specification are allowed
12476 on a function-definition. */
12477 if (asm_specification)
12478 error ("%Han asm-specification is not allowed "
12479 "on a function-definition",
12480 &asm_spec_start_token->location);
12482 error ("%Hattributes are not allowed on a function-definition",
12483 &attributes_start_token->location);
12484 /* This is a function-definition. */
12485 *function_definition_p = true;
12487 /* Parse the function definition. */
12489 decl = cp_parser_save_member_function_body (parser,
12492 prefix_attributes);
12495 = (cp_parser_function_definition_from_specifiers_and_declarator
12496 (parser, decl_specifiers, prefix_attributes, declarator));
12504 Only in function declarations for constructors, destructors, and
12505 type conversions can the decl-specifier-seq be omitted.
12507 We explicitly postpone this check past the point where we handle
12508 function-definitions because we tolerate function-definitions
12509 that are missing their return types in some modes. */
12510 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12512 cp_parser_error (parser,
12513 "expected constructor, destructor, or type conversion");
12514 return error_mark_node;
12517 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12518 if (token->type == CPP_EQ
12519 || token->type == CPP_OPEN_PAREN
12520 || token->type == CPP_OPEN_BRACE)
12522 is_initialized = 1;
12523 initialization_kind = token->type;
12525 if (token->type == CPP_EQ
12526 && function_declarator_p (declarator))
12528 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12529 if (t2->keyword == RID_DEFAULT)
12530 is_initialized = 2;
12531 else if (t2->keyword == RID_DELETE)
12532 is_initialized = 3;
12537 /* If the init-declarator isn't initialized and isn't followed by a
12538 `,' or `;', it's not a valid init-declarator. */
12539 if (token->type != CPP_COMMA
12540 && token->type != CPP_SEMICOLON)
12542 cp_parser_error (parser, "expected initializer");
12543 return error_mark_node;
12545 is_initialized = 0;
12546 initialization_kind = CPP_EOF;
12549 /* Because start_decl has side-effects, we should only call it if we
12550 know we're going ahead. By this point, we know that we cannot
12551 possibly be looking at any other construct. */
12552 cp_parser_commit_to_tentative_parse (parser);
12554 /* If the decl specifiers were bad, issue an error now that we're
12555 sure this was intended to be a declarator. Then continue
12556 declaring the variable(s), as int, to try to cut down on further
12558 if (decl_specifiers->any_specifiers_p
12559 && decl_specifiers->type == error_mark_node)
12561 cp_parser_error (parser, "invalid type in declaration");
12562 decl_specifiers->type = integer_type_node;
12565 /* Check to see whether or not this declaration is a friend. */
12566 friend_p = cp_parser_friend_p (decl_specifiers);
12568 /* Enter the newly declared entry in the symbol table. If we're
12569 processing a declaration in a class-specifier, we wait until
12570 after processing the initializer. */
12573 if (parser->in_unbraced_linkage_specification_p)
12574 decl_specifiers->storage_class = sc_extern;
12575 decl = start_decl (declarator, decl_specifiers,
12576 is_initialized, attributes, prefix_attributes,
12580 /* Enter the SCOPE. That way unqualified names appearing in the
12581 initializer will be looked up in SCOPE. */
12582 pushed_scope = push_scope (scope);
12584 /* Perform deferred access control checks, now that we know in which
12585 SCOPE the declared entity resides. */
12586 if (!member_p && decl)
12588 tree saved_current_function_decl = NULL_TREE;
12590 /* If the entity being declared is a function, pretend that we
12591 are in its scope. If it is a `friend', it may have access to
12592 things that would not otherwise be accessible. */
12593 if (TREE_CODE (decl) == FUNCTION_DECL)
12595 saved_current_function_decl = current_function_decl;
12596 current_function_decl = decl;
12599 /* Perform access checks for template parameters. */
12600 cp_parser_perform_template_parameter_access_checks (checks);
12602 /* Perform the access control checks for the declarator and the
12603 decl-specifiers. */
12604 perform_deferred_access_checks ();
12606 /* Restore the saved value. */
12607 if (TREE_CODE (decl) == FUNCTION_DECL)
12608 current_function_decl = saved_current_function_decl;
12611 /* Parse the initializer. */
12612 initializer = NULL_TREE;
12613 is_direct_init = false;
12614 is_non_constant_init = true;
12615 if (is_initialized)
12617 if (function_declarator_p (declarator))
12619 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12620 if (initialization_kind == CPP_EQ)
12621 initializer = cp_parser_pure_specifier (parser);
12624 /* If the declaration was erroneous, we don't really
12625 know what the user intended, so just silently
12626 consume the initializer. */
12627 if (decl != error_mark_node)
12628 error ("%Hinitializer provided for function",
12629 &initializer_start_token->location);
12630 cp_parser_skip_to_closing_parenthesis (parser,
12631 /*recovering=*/true,
12632 /*or_comma=*/false,
12633 /*consume_paren=*/true);
12637 initializer = cp_parser_initializer (parser,
12639 &is_non_constant_init);
12642 /* The old parser allows attributes to appear after a parenthesized
12643 initializer. Mark Mitchell proposed removing this functionality
12644 on the GCC mailing lists on 2002-08-13. This parser accepts the
12645 attributes -- but ignores them. */
12646 if (cp_parser_allow_gnu_extensions_p (parser)
12647 && initialization_kind == CPP_OPEN_PAREN)
12648 if (cp_parser_attributes_opt (parser))
12649 warning (OPT_Wattributes,
12650 "attributes after parenthesized initializer ignored");
12652 /* For an in-class declaration, use `grokfield' to create the
12658 pop_scope (pushed_scope);
12659 pushed_scope = false;
12661 decl = grokfield (declarator, decl_specifiers,
12662 initializer, !is_non_constant_init,
12663 /*asmspec=*/NULL_TREE,
12664 prefix_attributes);
12665 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12666 cp_parser_save_default_args (parser, decl);
12669 /* Finish processing the declaration. But, skip friend
12671 if (!friend_p && decl && decl != error_mark_node)
12673 cp_finish_decl (decl,
12674 initializer, !is_non_constant_init,
12676 /* If the initializer is in parentheses, then this is
12677 a direct-initialization, which means that an
12678 `explicit' constructor is OK. Otherwise, an
12679 `explicit' constructor cannot be used. */
12680 ((is_direct_init || !is_initialized)
12681 ? 0 : LOOKUP_ONLYCONVERTING));
12683 else if ((cxx_dialect != cxx98) && friend_p
12684 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12685 /* Core issue #226 (C++0x only): A default template-argument
12686 shall not be specified in a friend class template
12688 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12689 /*is_partial=*/0, /*is_friend_decl=*/1);
12691 if (!friend_p && pushed_scope)
12692 pop_scope (pushed_scope);
12697 /* Parse a declarator.
12701 ptr-operator declarator
12703 abstract-declarator:
12704 ptr-operator abstract-declarator [opt]
12705 direct-abstract-declarator
12710 attributes [opt] direct-declarator
12711 attributes [opt] ptr-operator declarator
12713 abstract-declarator:
12714 attributes [opt] ptr-operator abstract-declarator [opt]
12715 attributes [opt] direct-abstract-declarator
12717 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12718 detect constructor, destructor or conversion operators. It is set
12719 to -1 if the declarator is a name, and +1 if it is a
12720 function. Otherwise it is set to zero. Usually you just want to
12721 test for >0, but internally the negative value is used.
12723 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12724 a decl-specifier-seq unless it declares a constructor, destructor,
12725 or conversion. It might seem that we could check this condition in
12726 semantic analysis, rather than parsing, but that makes it difficult
12727 to handle something like `f()'. We want to notice that there are
12728 no decl-specifiers, and therefore realize that this is an
12729 expression, not a declaration.)
12731 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12732 the declarator is a direct-declarator of the form "(...)".
12734 MEMBER_P is true iff this declarator is a member-declarator. */
12736 static cp_declarator *
12737 cp_parser_declarator (cp_parser* parser,
12738 cp_parser_declarator_kind dcl_kind,
12739 int* ctor_dtor_or_conv_p,
12740 bool* parenthesized_p,
12744 cp_declarator *declarator;
12745 enum tree_code code;
12746 cp_cv_quals cv_quals;
12748 tree attributes = NULL_TREE;
12750 /* Assume this is not a constructor, destructor, or type-conversion
12752 if (ctor_dtor_or_conv_p)
12753 *ctor_dtor_or_conv_p = 0;
12755 if (cp_parser_allow_gnu_extensions_p (parser))
12756 attributes = cp_parser_attributes_opt (parser);
12758 /* Peek at the next token. */
12759 token = cp_lexer_peek_token (parser->lexer);
12761 /* Check for the ptr-operator production. */
12762 cp_parser_parse_tentatively (parser);
12763 /* Parse the ptr-operator. */
12764 code = cp_parser_ptr_operator (parser,
12767 /* If that worked, then we have a ptr-operator. */
12768 if (cp_parser_parse_definitely (parser))
12770 /* If a ptr-operator was found, then this declarator was not
12772 if (parenthesized_p)
12773 *parenthesized_p = true;
12774 /* The dependent declarator is optional if we are parsing an
12775 abstract-declarator. */
12776 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12777 cp_parser_parse_tentatively (parser);
12779 /* Parse the dependent declarator. */
12780 declarator = cp_parser_declarator (parser, dcl_kind,
12781 /*ctor_dtor_or_conv_p=*/NULL,
12782 /*parenthesized_p=*/NULL,
12783 /*member_p=*/false);
12785 /* If we are parsing an abstract-declarator, we must handle the
12786 case where the dependent declarator is absent. */
12787 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12788 && !cp_parser_parse_definitely (parser))
12791 declarator = cp_parser_make_indirect_declarator
12792 (code, class_type, cv_quals, declarator);
12794 /* Everything else is a direct-declarator. */
12797 if (parenthesized_p)
12798 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12800 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12801 ctor_dtor_or_conv_p,
12805 if (attributes && declarator && declarator != cp_error_declarator)
12806 declarator->attributes = attributes;
12811 /* Parse a direct-declarator or direct-abstract-declarator.
12815 direct-declarator ( parameter-declaration-clause )
12816 cv-qualifier-seq [opt]
12817 exception-specification [opt]
12818 direct-declarator [ constant-expression [opt] ]
12821 direct-abstract-declarator:
12822 direct-abstract-declarator [opt]
12823 ( parameter-declaration-clause )
12824 cv-qualifier-seq [opt]
12825 exception-specification [opt]
12826 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12827 ( abstract-declarator )
12829 Returns a representation of the declarator. DCL_KIND is
12830 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12831 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12832 we are parsing a direct-declarator. It is
12833 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12834 of ambiguity we prefer an abstract declarator, as per
12835 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12836 cp_parser_declarator. */
12838 static cp_declarator *
12839 cp_parser_direct_declarator (cp_parser* parser,
12840 cp_parser_declarator_kind dcl_kind,
12841 int* ctor_dtor_or_conv_p,
12845 cp_declarator *declarator = NULL;
12846 tree scope = NULL_TREE;
12847 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12848 bool saved_in_declarator_p = parser->in_declarator_p;
12850 tree pushed_scope = NULL_TREE;
12854 /* Peek at the next token. */
12855 token = cp_lexer_peek_token (parser->lexer);
12856 if (token->type == CPP_OPEN_PAREN)
12858 /* This is either a parameter-declaration-clause, or a
12859 parenthesized declarator. When we know we are parsing a
12860 named declarator, it must be a parenthesized declarator
12861 if FIRST is true. For instance, `(int)' is a
12862 parameter-declaration-clause, with an omitted
12863 direct-abstract-declarator. But `((*))', is a
12864 parenthesized abstract declarator. Finally, when T is a
12865 template parameter `(T)' is a
12866 parameter-declaration-clause, and not a parenthesized
12869 We first try and parse a parameter-declaration-clause,
12870 and then try a nested declarator (if FIRST is true).
12872 It is not an error for it not to be a
12873 parameter-declaration-clause, even when FIRST is
12879 The first is the declaration of a function while the
12880 second is the definition of a variable, including its
12883 Having seen only the parenthesis, we cannot know which of
12884 these two alternatives should be selected. Even more
12885 complex are examples like:
12890 The former is a function-declaration; the latter is a
12891 variable initialization.
12893 Thus again, we try a parameter-declaration-clause, and if
12894 that fails, we back out and return. */
12896 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12898 cp_parameter_declarator *params;
12899 unsigned saved_num_template_parameter_lists;
12901 /* In a member-declarator, the only valid interpretation
12902 of a parenthesis is the start of a
12903 parameter-declaration-clause. (It is invalid to
12904 initialize a static data member with a parenthesized
12905 initializer; only the "=" form of initialization is
12908 cp_parser_parse_tentatively (parser);
12910 /* Consume the `('. */
12911 cp_lexer_consume_token (parser->lexer);
12914 /* If this is going to be an abstract declarator, we're
12915 in a declarator and we can't have default args. */
12916 parser->default_arg_ok_p = false;
12917 parser->in_declarator_p = true;
12920 /* Inside the function parameter list, surrounding
12921 template-parameter-lists do not apply. */
12922 saved_num_template_parameter_lists
12923 = parser->num_template_parameter_lists;
12924 parser->num_template_parameter_lists = 0;
12926 /* Parse the parameter-declaration-clause. */
12927 params = cp_parser_parameter_declaration_clause (parser);
12929 parser->num_template_parameter_lists
12930 = saved_num_template_parameter_lists;
12932 /* If all went well, parse the cv-qualifier-seq and the
12933 exception-specification. */
12934 if (member_p || cp_parser_parse_definitely (parser))
12936 cp_cv_quals cv_quals;
12937 tree exception_specification;
12939 if (ctor_dtor_or_conv_p)
12940 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12942 /* Consume the `)'. */
12943 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
12945 /* Parse the cv-qualifier-seq. */
12946 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12947 /* And the exception-specification. */
12948 exception_specification
12949 = cp_parser_exception_specification_opt (parser);
12951 /* Create the function-declarator. */
12952 declarator = make_call_declarator (declarator,
12955 exception_specification);
12956 /* Any subsequent parameter lists are to do with
12957 return type, so are not those of the declared
12959 parser->default_arg_ok_p = false;
12961 /* Repeat the main loop. */
12966 /* If this is the first, we can try a parenthesized
12970 bool saved_in_type_id_in_expr_p;
12972 parser->default_arg_ok_p = saved_default_arg_ok_p;
12973 parser->in_declarator_p = saved_in_declarator_p;
12975 /* Consume the `('. */
12976 cp_lexer_consume_token (parser->lexer);
12977 /* Parse the nested declarator. */
12978 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12979 parser->in_type_id_in_expr_p = true;
12981 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12982 /*parenthesized_p=*/NULL,
12984 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12986 /* Expect a `)'. */
12987 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12988 declarator = cp_error_declarator;
12989 if (declarator == cp_error_declarator)
12992 goto handle_declarator;
12994 /* Otherwise, we must be done. */
12998 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12999 && token->type == CPP_OPEN_SQUARE)
13001 /* Parse an array-declarator. */
13004 if (ctor_dtor_or_conv_p)
13005 *ctor_dtor_or_conv_p = 0;
13008 parser->default_arg_ok_p = false;
13009 parser->in_declarator_p = true;
13010 /* Consume the `['. */
13011 cp_lexer_consume_token (parser->lexer);
13012 /* Peek at the next token. */
13013 token = cp_lexer_peek_token (parser->lexer);
13014 /* If the next token is `]', then there is no
13015 constant-expression. */
13016 if (token->type != CPP_CLOSE_SQUARE)
13018 bool non_constant_p;
13021 = cp_parser_constant_expression (parser,
13022 /*allow_non_constant=*/true,
13024 if (!non_constant_p)
13025 bounds = fold_non_dependent_expr (bounds);
13026 /* Normally, the array bound must be an integral constant
13027 expression. However, as an extension, we allow VLAs
13028 in function scopes. */
13029 else if (!parser->in_function_body)
13031 error ("%Harray bound is not an integer constant",
13033 bounds = error_mark_node;
13037 bounds = NULL_TREE;
13038 /* Look for the closing `]'. */
13039 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13041 declarator = cp_error_declarator;
13045 declarator = make_array_declarator (declarator, bounds);
13047 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13049 tree qualifying_scope;
13050 tree unqualified_name;
13051 special_function_kind sfk;
13053 bool pack_expansion_p = false;
13054 cp_token *declarator_id_start_token;
13056 /* Parse a declarator-id */
13057 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13060 cp_parser_parse_tentatively (parser);
13062 /* If we see an ellipsis, we should be looking at a
13064 if (token->type == CPP_ELLIPSIS)
13066 /* Consume the `...' */
13067 cp_lexer_consume_token (parser->lexer);
13069 pack_expansion_p = true;
13073 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13075 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13076 qualifying_scope = parser->scope;
13081 if (!unqualified_name && pack_expansion_p)
13083 /* Check whether an error occurred. */
13084 okay = !cp_parser_error_occurred (parser);
13086 /* We already consumed the ellipsis to mark a
13087 parameter pack, but we have no way to report it,
13088 so abort the tentative parse. We will be exiting
13089 immediately anyway. */
13090 cp_parser_abort_tentative_parse (parser);
13093 okay = cp_parser_parse_definitely (parser);
13096 unqualified_name = error_mark_node;
13097 else if (unqualified_name
13098 && (qualifying_scope
13099 || (TREE_CODE (unqualified_name)
13100 != IDENTIFIER_NODE)))
13102 cp_parser_error (parser, "expected unqualified-id");
13103 unqualified_name = error_mark_node;
13107 if (!unqualified_name)
13109 if (unqualified_name == error_mark_node)
13111 declarator = cp_error_declarator;
13112 pack_expansion_p = false;
13113 declarator->parameter_pack_p = false;
13117 if (qualifying_scope && at_namespace_scope_p ()
13118 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13120 /* In the declaration of a member of a template class
13121 outside of the class itself, the SCOPE will sometimes
13122 be a TYPENAME_TYPE. For example, given:
13124 template <typename T>
13125 int S<T>::R::i = 3;
13127 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13128 this context, we must resolve S<T>::R to an ordinary
13129 type, rather than a typename type.
13131 The reason we normally avoid resolving TYPENAME_TYPEs
13132 is that a specialization of `S' might render
13133 `S<T>::R' not a type. However, if `S' is
13134 specialized, then this `i' will not be used, so there
13135 is no harm in resolving the types here. */
13138 /* Resolve the TYPENAME_TYPE. */
13139 type = resolve_typename_type (qualifying_scope,
13140 /*only_current_p=*/false);
13141 /* If that failed, the declarator is invalid. */
13142 if (TREE_CODE (type) == TYPENAME_TYPE)
13143 error ("%H%<%T::%E%> is not a type",
13144 &declarator_id_start_token->location,
13145 TYPE_CONTEXT (qualifying_scope),
13146 TYPE_IDENTIFIER (qualifying_scope));
13147 qualifying_scope = type;
13152 if (unqualified_name)
13156 if (qualifying_scope
13157 && CLASS_TYPE_P (qualifying_scope))
13158 class_type = qualifying_scope;
13160 class_type = current_class_type;
13162 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13164 tree name_type = TREE_TYPE (unqualified_name);
13165 if (class_type && same_type_p (name_type, class_type))
13167 if (qualifying_scope
13168 && CLASSTYPE_USE_TEMPLATE (name_type))
13170 error ("%Hinvalid use of constructor as a template",
13171 &declarator_id_start_token->location);
13172 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
13173 "name the constructor in a qualified name",
13175 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13176 class_type, name_type);
13177 declarator = cp_error_declarator;
13181 unqualified_name = constructor_name (class_type);
13185 /* We do not attempt to print the declarator
13186 here because we do not have enough
13187 information about its original syntactic
13189 cp_parser_error (parser, "invalid declarator");
13190 declarator = cp_error_declarator;
13197 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13198 sfk = sfk_destructor;
13199 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13200 sfk = sfk_conversion;
13201 else if (/* There's no way to declare a constructor
13202 for an anonymous type, even if the type
13203 got a name for linkage purposes. */
13204 !TYPE_WAS_ANONYMOUS (class_type)
13205 && constructor_name_p (unqualified_name,
13208 unqualified_name = constructor_name (class_type);
13209 sfk = sfk_constructor;
13212 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13213 *ctor_dtor_or_conv_p = -1;
13216 declarator = make_id_declarator (qualifying_scope,
13219 declarator->id_loc = token->location;
13220 declarator->parameter_pack_p = pack_expansion_p;
13222 if (pack_expansion_p)
13223 maybe_warn_variadic_templates ();
13225 handle_declarator:;
13226 scope = get_scope_of_declarator (declarator);
13228 /* Any names that appear after the declarator-id for a
13229 member are looked up in the containing scope. */
13230 pushed_scope = push_scope (scope);
13231 parser->in_declarator_p = true;
13232 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13233 || (declarator && declarator->kind == cdk_id))
13234 /* Default args are only allowed on function
13236 parser->default_arg_ok_p = saved_default_arg_ok_p;
13238 parser->default_arg_ok_p = false;
13247 /* For an abstract declarator, we might wind up with nothing at this
13248 point. That's an error; the declarator is not optional. */
13250 cp_parser_error (parser, "expected declarator");
13252 /* If we entered a scope, we must exit it now. */
13254 pop_scope (pushed_scope);
13256 parser->default_arg_ok_p = saved_default_arg_ok_p;
13257 parser->in_declarator_p = saved_in_declarator_p;
13262 /* Parse a ptr-operator.
13265 * cv-qualifier-seq [opt]
13267 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13272 & cv-qualifier-seq [opt]
13274 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13275 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13276 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13277 filled in with the TYPE containing the member. *CV_QUALS is
13278 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13279 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13280 Note that the tree codes returned by this function have nothing
13281 to do with the types of trees that will be eventually be created
13282 to represent the pointer or reference type being parsed. They are
13283 just constants with suggestive names. */
13284 static enum tree_code
13285 cp_parser_ptr_operator (cp_parser* parser,
13287 cp_cv_quals *cv_quals)
13289 enum tree_code code = ERROR_MARK;
13292 /* Assume that it's not a pointer-to-member. */
13294 /* And that there are no cv-qualifiers. */
13295 *cv_quals = TYPE_UNQUALIFIED;
13297 /* Peek at the next token. */
13298 token = cp_lexer_peek_token (parser->lexer);
13300 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13301 if (token->type == CPP_MULT)
13302 code = INDIRECT_REF;
13303 else if (token->type == CPP_AND)
13305 else if ((cxx_dialect != cxx98) &&
13306 token->type == CPP_AND_AND) /* C++0x only */
13307 code = NON_LVALUE_EXPR;
13309 if (code != ERROR_MARK)
13311 /* Consume the `*', `&' or `&&'. */
13312 cp_lexer_consume_token (parser->lexer);
13314 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13315 `&', if we are allowing GNU extensions. (The only qualifier
13316 that can legally appear after `&' is `restrict', but that is
13317 enforced during semantic analysis. */
13318 if (code == INDIRECT_REF
13319 || cp_parser_allow_gnu_extensions_p (parser))
13320 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13324 /* Try the pointer-to-member case. */
13325 cp_parser_parse_tentatively (parser);
13326 /* Look for the optional `::' operator. */
13327 cp_parser_global_scope_opt (parser,
13328 /*current_scope_valid_p=*/false);
13329 /* Look for the nested-name specifier. */
13330 token = cp_lexer_peek_token (parser->lexer);
13331 cp_parser_nested_name_specifier (parser,
13332 /*typename_keyword_p=*/false,
13333 /*check_dependency_p=*/true,
13335 /*is_declaration=*/false);
13336 /* If we found it, and the next token is a `*', then we are
13337 indeed looking at a pointer-to-member operator. */
13338 if (!cp_parser_error_occurred (parser)
13339 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13341 /* Indicate that the `*' operator was used. */
13342 code = INDIRECT_REF;
13344 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13345 error ("%H%qD is a namespace", &token->location, parser->scope);
13348 /* The type of which the member is a member is given by the
13350 *type = parser->scope;
13351 /* The next name will not be qualified. */
13352 parser->scope = NULL_TREE;
13353 parser->qualifying_scope = NULL_TREE;
13354 parser->object_scope = NULL_TREE;
13355 /* Look for the optional cv-qualifier-seq. */
13356 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13359 /* If that didn't work we don't have a ptr-operator. */
13360 if (!cp_parser_parse_definitely (parser))
13361 cp_parser_error (parser, "expected ptr-operator");
13367 /* Parse an (optional) cv-qualifier-seq.
13370 cv-qualifier cv-qualifier-seq [opt]
13381 Returns a bitmask representing the cv-qualifiers. */
13384 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13386 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13391 cp_cv_quals cv_qualifier;
13393 /* Peek at the next token. */
13394 token = cp_lexer_peek_token (parser->lexer);
13395 /* See if it's a cv-qualifier. */
13396 switch (token->keyword)
13399 cv_qualifier = TYPE_QUAL_CONST;
13403 cv_qualifier = TYPE_QUAL_VOLATILE;
13407 cv_qualifier = TYPE_QUAL_RESTRICT;
13411 cv_qualifier = TYPE_UNQUALIFIED;
13418 if (cv_quals & cv_qualifier)
13420 error ("%Hduplicate cv-qualifier", &token->location);
13421 cp_lexer_purge_token (parser->lexer);
13425 cp_lexer_consume_token (parser->lexer);
13426 cv_quals |= cv_qualifier;
13433 /* Parse a declarator-id.
13437 :: [opt] nested-name-specifier [opt] type-name
13439 In the `id-expression' case, the value returned is as for
13440 cp_parser_id_expression if the id-expression was an unqualified-id.
13441 If the id-expression was a qualified-id, then a SCOPE_REF is
13442 returned. The first operand is the scope (either a NAMESPACE_DECL
13443 or TREE_TYPE), but the second is still just a representation of an
13447 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13450 /* The expression must be an id-expression. Assume that qualified
13451 names are the names of types so that:
13454 int S<T>::R::i = 3;
13456 will work; we must treat `S<T>::R' as the name of a type.
13457 Similarly, assume that qualified names are templates, where
13461 int S<T>::R<T>::i = 3;
13464 id = cp_parser_id_expression (parser,
13465 /*template_keyword_p=*/false,
13466 /*check_dependency_p=*/false,
13467 /*template_p=*/NULL,
13468 /*declarator_p=*/true,
13470 if (id && BASELINK_P (id))
13471 id = BASELINK_FUNCTIONS (id);
13475 /* Parse a type-id.
13478 type-specifier-seq abstract-declarator [opt]
13480 Returns the TYPE specified. */
13483 cp_parser_type_id (cp_parser* parser)
13485 cp_decl_specifier_seq type_specifier_seq;
13486 cp_declarator *abstract_declarator;
13488 /* Parse the type-specifier-seq. */
13489 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13490 &type_specifier_seq);
13491 if (type_specifier_seq.type == error_mark_node)
13492 return error_mark_node;
13494 /* There might or might not be an abstract declarator. */
13495 cp_parser_parse_tentatively (parser);
13496 /* Look for the declarator. */
13497 abstract_declarator
13498 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13499 /*parenthesized_p=*/NULL,
13500 /*member_p=*/false);
13501 /* Check to see if there really was a declarator. */
13502 if (!cp_parser_parse_definitely (parser))
13503 abstract_declarator = NULL;
13505 return groktypename (&type_specifier_seq, abstract_declarator);
13508 /* Parse a type-specifier-seq.
13510 type-specifier-seq:
13511 type-specifier type-specifier-seq [opt]
13515 type-specifier-seq:
13516 attributes type-specifier-seq [opt]
13518 If IS_CONDITION is true, we are at the start of a "condition",
13519 e.g., we've just seen "if (".
13521 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13524 cp_parser_type_specifier_seq (cp_parser* parser,
13526 cp_decl_specifier_seq *type_specifier_seq)
13528 bool seen_type_specifier = false;
13529 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13530 cp_token *start_token = NULL;
13532 /* Clear the TYPE_SPECIFIER_SEQ. */
13533 clear_decl_specs (type_specifier_seq);
13535 /* Parse the type-specifiers and attributes. */
13538 tree type_specifier;
13539 bool is_cv_qualifier;
13541 /* Check for attributes first. */
13542 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13544 type_specifier_seq->attributes =
13545 chainon (type_specifier_seq->attributes,
13546 cp_parser_attributes_opt (parser));
13550 /* record the token of the beginning of the type specifier seq,
13551 for error reporting purposes*/
13553 start_token = cp_lexer_peek_token (parser->lexer);
13555 /* Look for the type-specifier. */
13556 type_specifier = cp_parser_type_specifier (parser,
13558 type_specifier_seq,
13559 /*is_declaration=*/false,
13562 if (!type_specifier)
13564 /* If the first type-specifier could not be found, this is not a
13565 type-specifier-seq at all. */
13566 if (!seen_type_specifier)
13568 cp_parser_error (parser, "expected type-specifier");
13569 type_specifier_seq->type = error_mark_node;
13572 /* If subsequent type-specifiers could not be found, the
13573 type-specifier-seq is complete. */
13577 seen_type_specifier = true;
13578 /* The standard says that a condition can be:
13580 type-specifier-seq declarator = assignment-expression
13587 we should treat the "S" as a declarator, not as a
13588 type-specifier. The standard doesn't say that explicitly for
13589 type-specifier-seq, but it does say that for
13590 decl-specifier-seq in an ordinary declaration. Perhaps it
13591 would be clearer just to allow a decl-specifier-seq here, and
13592 then add a semantic restriction that if any decl-specifiers
13593 that are not type-specifiers appear, the program is invalid. */
13594 if (is_condition && !is_cv_qualifier)
13595 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13598 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13601 /* Parse a parameter-declaration-clause.
13603 parameter-declaration-clause:
13604 parameter-declaration-list [opt] ... [opt]
13605 parameter-declaration-list , ...
13607 Returns a representation for the parameter declarations. A return
13608 value of NULL indicates a parameter-declaration-clause consisting
13609 only of an ellipsis. */
13611 static cp_parameter_declarator *
13612 cp_parser_parameter_declaration_clause (cp_parser* parser)
13614 cp_parameter_declarator *parameters;
13619 /* Peek at the next token. */
13620 token = cp_lexer_peek_token (parser->lexer);
13621 /* Check for trivial parameter-declaration-clauses. */
13622 if (token->type == CPP_ELLIPSIS)
13624 /* Consume the `...' token. */
13625 cp_lexer_consume_token (parser->lexer);
13628 else if (token->type == CPP_CLOSE_PAREN)
13629 /* There are no parameters. */
13631 #ifndef NO_IMPLICIT_EXTERN_C
13632 if (in_system_header && current_class_type == NULL
13633 && current_lang_name == lang_name_c)
13637 return no_parameters;
13639 /* Check for `(void)', too, which is a special case. */
13640 else if (token->keyword == RID_VOID
13641 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13642 == CPP_CLOSE_PAREN))
13644 /* Consume the `void' token. */
13645 cp_lexer_consume_token (parser->lexer);
13646 /* There are no parameters. */
13647 return no_parameters;
13650 /* Parse the parameter-declaration-list. */
13651 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13652 /* If a parse error occurred while parsing the
13653 parameter-declaration-list, then the entire
13654 parameter-declaration-clause is erroneous. */
13658 /* Peek at the next token. */
13659 token = cp_lexer_peek_token (parser->lexer);
13660 /* If it's a `,', the clause should terminate with an ellipsis. */
13661 if (token->type == CPP_COMMA)
13663 /* Consume the `,'. */
13664 cp_lexer_consume_token (parser->lexer);
13665 /* Expect an ellipsis. */
13667 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13669 /* It might also be `...' if the optional trailing `,' was
13671 else if (token->type == CPP_ELLIPSIS)
13673 /* Consume the `...' token. */
13674 cp_lexer_consume_token (parser->lexer);
13675 /* And remember that we saw it. */
13679 ellipsis_p = false;
13681 /* Finish the parameter list. */
13682 if (parameters && ellipsis_p)
13683 parameters->ellipsis_p = true;
13688 /* Parse a parameter-declaration-list.
13690 parameter-declaration-list:
13691 parameter-declaration
13692 parameter-declaration-list , parameter-declaration
13694 Returns a representation of the parameter-declaration-list, as for
13695 cp_parser_parameter_declaration_clause. However, the
13696 `void_list_node' is never appended to the list. Upon return,
13697 *IS_ERROR will be true iff an error occurred. */
13699 static cp_parameter_declarator *
13700 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13702 cp_parameter_declarator *parameters = NULL;
13703 cp_parameter_declarator **tail = ¶meters;
13704 bool saved_in_unbraced_linkage_specification_p;
13706 /* Assume all will go well. */
13708 /* The special considerations that apply to a function within an
13709 unbraced linkage specifications do not apply to the parameters
13710 to the function. */
13711 saved_in_unbraced_linkage_specification_p
13712 = parser->in_unbraced_linkage_specification_p;
13713 parser->in_unbraced_linkage_specification_p = false;
13715 /* Look for more parameters. */
13718 cp_parameter_declarator *parameter;
13719 bool parenthesized_p;
13720 /* Parse the parameter. */
13722 = cp_parser_parameter_declaration (parser,
13723 /*template_parm_p=*/false,
13726 /* If a parse error occurred parsing the parameter declaration,
13727 then the entire parameter-declaration-list is erroneous. */
13734 /* Add the new parameter to the list. */
13736 tail = ¶meter->next;
13738 /* Peek at the next token. */
13739 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13740 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13741 /* These are for Objective-C++ */
13742 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13743 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13744 /* The parameter-declaration-list is complete. */
13746 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13750 /* Peek at the next token. */
13751 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13752 /* If it's an ellipsis, then the list is complete. */
13753 if (token->type == CPP_ELLIPSIS)
13755 /* Otherwise, there must be more parameters. Consume the
13757 cp_lexer_consume_token (parser->lexer);
13758 /* When parsing something like:
13760 int i(float f, double d)
13762 we can tell after seeing the declaration for "f" that we
13763 are not looking at an initialization of a variable "i",
13764 but rather at the declaration of a function "i".
13766 Due to the fact that the parsing of template arguments
13767 (as specified to a template-id) requires backtracking we
13768 cannot use this technique when inside a template argument
13770 if (!parser->in_template_argument_list_p
13771 && !parser->in_type_id_in_expr_p
13772 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13773 /* However, a parameter-declaration of the form
13774 "foat(f)" (which is a valid declaration of a
13775 parameter "f") can also be interpreted as an
13776 expression (the conversion of "f" to "float"). */
13777 && !parenthesized_p)
13778 cp_parser_commit_to_tentative_parse (parser);
13782 cp_parser_error (parser, "expected %<,%> or %<...%>");
13783 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13784 cp_parser_skip_to_closing_parenthesis (parser,
13785 /*recovering=*/true,
13786 /*or_comma=*/false,
13787 /*consume_paren=*/false);
13792 parser->in_unbraced_linkage_specification_p
13793 = saved_in_unbraced_linkage_specification_p;
13798 /* Parse a parameter declaration.
13800 parameter-declaration:
13801 decl-specifier-seq ... [opt] declarator
13802 decl-specifier-seq declarator = assignment-expression
13803 decl-specifier-seq ... [opt] abstract-declarator [opt]
13804 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13806 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13807 declares a template parameter. (In that case, a non-nested `>'
13808 token encountered during the parsing of the assignment-expression
13809 is not interpreted as a greater-than operator.)
13811 Returns a representation of the parameter, or NULL if an error
13812 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13813 true iff the declarator is of the form "(p)". */
13815 static cp_parameter_declarator *
13816 cp_parser_parameter_declaration (cp_parser *parser,
13817 bool template_parm_p,
13818 bool *parenthesized_p)
13820 int declares_class_or_enum;
13821 bool greater_than_is_operator_p;
13822 cp_decl_specifier_seq decl_specifiers;
13823 cp_declarator *declarator;
13824 tree default_argument;
13825 cp_token *token = NULL, *declarator_token_start = NULL;
13826 const char *saved_message;
13828 /* In a template parameter, `>' is not an operator.
13832 When parsing a default template-argument for a non-type
13833 template-parameter, the first non-nested `>' is taken as the end
13834 of the template parameter-list rather than a greater-than
13836 greater_than_is_operator_p = !template_parm_p;
13838 /* Type definitions may not appear in parameter types. */
13839 saved_message = parser->type_definition_forbidden_message;
13840 parser->type_definition_forbidden_message
13841 = "types may not be defined in parameter types";
13843 /* Parse the declaration-specifiers. */
13844 cp_parser_decl_specifier_seq (parser,
13845 CP_PARSER_FLAGS_NONE,
13847 &declares_class_or_enum);
13848 /* If an error occurred, there's no reason to attempt to parse the
13849 rest of the declaration. */
13850 if (cp_parser_error_occurred (parser))
13852 parser->type_definition_forbidden_message = saved_message;
13856 /* Peek at the next token. */
13857 token = cp_lexer_peek_token (parser->lexer);
13859 /* If the next token is a `)', `,', `=', `>', or `...', then there
13860 is no declarator. However, when variadic templates are enabled,
13861 there may be a declarator following `...'. */
13862 if (token->type == CPP_CLOSE_PAREN
13863 || token->type == CPP_COMMA
13864 || token->type == CPP_EQ
13865 || token->type == CPP_GREATER)
13868 if (parenthesized_p)
13869 *parenthesized_p = false;
13871 /* Otherwise, there should be a declarator. */
13874 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13875 parser->default_arg_ok_p = false;
13877 /* After seeing a decl-specifier-seq, if the next token is not a
13878 "(", there is no possibility that the code is a valid
13879 expression. Therefore, if parsing tentatively, we commit at
13881 if (!parser->in_template_argument_list_p
13882 /* In an expression context, having seen:
13886 we cannot be sure whether we are looking at a
13887 function-type (taking a "char" as a parameter) or a cast
13888 of some object of type "char" to "int". */
13889 && !parser->in_type_id_in_expr_p
13890 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13891 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13892 cp_parser_commit_to_tentative_parse (parser);
13893 /* Parse the declarator. */
13894 declarator_token_start = token;
13895 declarator = cp_parser_declarator (parser,
13896 CP_PARSER_DECLARATOR_EITHER,
13897 /*ctor_dtor_or_conv_p=*/NULL,
13899 /*member_p=*/false);
13900 parser->default_arg_ok_p = saved_default_arg_ok_p;
13901 /* After the declarator, allow more attributes. */
13902 decl_specifiers.attributes
13903 = chainon (decl_specifiers.attributes,
13904 cp_parser_attributes_opt (parser));
13907 /* If the next token is an ellipsis, and we have not seen a
13908 declarator name, and the type of the declarator contains parameter
13909 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13910 a parameter pack expansion expression. Otherwise, leave the
13911 ellipsis for a C-style variadic function. */
13912 token = cp_lexer_peek_token (parser->lexer);
13913 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13915 tree type = decl_specifiers.type;
13917 if (type && DECL_P (type))
13918 type = TREE_TYPE (type);
13921 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13922 && declarator_can_be_parameter_pack (declarator)
13923 && (!declarator || !declarator->parameter_pack_p)
13924 && uses_parameter_packs (type))
13926 /* Consume the `...'. */
13927 cp_lexer_consume_token (parser->lexer);
13928 maybe_warn_variadic_templates ();
13930 /* Build a pack expansion type */
13932 declarator->parameter_pack_p = true;
13934 decl_specifiers.type = make_pack_expansion (type);
13938 /* The restriction on defining new types applies only to the type
13939 of the parameter, not to the default argument. */
13940 parser->type_definition_forbidden_message = saved_message;
13942 /* If the next token is `=', then process a default argument. */
13943 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13945 /* Consume the `='. */
13946 cp_lexer_consume_token (parser->lexer);
13948 /* If we are defining a class, then the tokens that make up the
13949 default argument must be saved and processed later. */
13950 if (!template_parm_p && at_class_scope_p ()
13951 && TYPE_BEING_DEFINED (current_class_type))
13953 unsigned depth = 0;
13954 int maybe_template_id = 0;
13955 cp_token *first_token;
13958 /* Add tokens until we have processed the entire default
13959 argument. We add the range [first_token, token). */
13960 first_token = cp_lexer_peek_token (parser->lexer);
13965 /* Peek at the next token. */
13966 token = cp_lexer_peek_token (parser->lexer);
13967 /* What we do depends on what token we have. */
13968 switch (token->type)
13970 /* In valid code, a default argument must be
13971 immediately followed by a `,' `)', or `...'. */
13973 if (depth == 0 && maybe_template_id)
13975 /* If we've seen a '<', we might be in a
13976 template-argument-list. Until Core issue 325 is
13977 resolved, we don't know how this situation ought
13978 to be handled, so try to DTRT. We check whether
13979 what comes after the comma is a valid parameter
13980 declaration list. If it is, then the comma ends
13981 the default argument; otherwise the default
13982 argument continues. */
13983 bool error = false;
13985 /* Set ITALP so cp_parser_parameter_declaration_list
13986 doesn't decide to commit to this parse. */
13987 bool saved_italp = parser->in_template_argument_list_p;
13988 parser->in_template_argument_list_p = true;
13990 cp_parser_parse_tentatively (parser);
13991 cp_lexer_consume_token (parser->lexer);
13992 cp_parser_parameter_declaration_list (parser, &error);
13993 if (!cp_parser_error_occurred (parser) && !error)
13995 cp_parser_abort_tentative_parse (parser);
13997 parser->in_template_argument_list_p = saved_italp;
14000 case CPP_CLOSE_PAREN:
14002 /* If we run into a non-nested `;', `}', or `]',
14003 then the code is invalid -- but the default
14004 argument is certainly over. */
14005 case CPP_SEMICOLON:
14006 case CPP_CLOSE_BRACE:
14007 case CPP_CLOSE_SQUARE:
14010 /* Update DEPTH, if necessary. */
14011 else if (token->type == CPP_CLOSE_PAREN
14012 || token->type == CPP_CLOSE_BRACE
14013 || token->type == CPP_CLOSE_SQUARE)
14017 case CPP_OPEN_PAREN:
14018 case CPP_OPEN_SQUARE:
14019 case CPP_OPEN_BRACE:
14025 /* This might be the comparison operator, or it might
14026 start a template argument list. */
14027 ++maybe_template_id;
14031 if (cxx_dialect == cxx98)
14033 /* Fall through for C++0x, which treats the `>>'
14034 operator like two `>' tokens in certain
14040 /* This might be an operator, or it might close a
14041 template argument list. But if a previous '<'
14042 started a template argument list, this will have
14043 closed it, so we can't be in one anymore. */
14044 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14045 if (maybe_template_id < 0)
14046 maybe_template_id = 0;
14050 /* If we run out of tokens, issue an error message. */
14052 case CPP_PRAGMA_EOL:
14053 error ("%Hfile ends in default argument", &token->location);
14059 /* In these cases, we should look for template-ids.
14060 For example, if the default argument is
14061 `X<int, double>()', we need to do name lookup to
14062 figure out whether or not `X' is a template; if
14063 so, the `,' does not end the default argument.
14065 That is not yet done. */
14072 /* If we've reached the end, stop. */
14076 /* Add the token to the token block. */
14077 token = cp_lexer_consume_token (parser->lexer);
14080 /* Create a DEFAULT_ARG to represent the unparsed default
14082 default_argument = make_node (DEFAULT_ARG);
14083 DEFARG_TOKENS (default_argument)
14084 = cp_token_cache_new (first_token, token);
14085 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14087 /* Outside of a class definition, we can just parse the
14088 assignment-expression. */
14091 token = cp_lexer_peek_token (parser->lexer);
14093 = cp_parser_default_argument (parser, template_parm_p);
14096 if (!parser->default_arg_ok_p)
14098 if (flag_permissive)
14099 warning (0, "deprecated use of default argument for parameter of non-function");
14102 error ("%Hdefault arguments are only "
14103 "permitted for function parameters",
14105 default_argument = NULL_TREE;
14108 else if ((declarator && declarator->parameter_pack_p)
14109 || (decl_specifiers.type
14110 && PACK_EXPANSION_P (decl_specifiers.type)))
14112 const char* kind = template_parm_p? "template " : "";
14114 /* Find the name of the parameter pack. */
14115 cp_declarator *id_declarator = declarator;
14116 while (id_declarator && id_declarator->kind != cdk_id)
14117 id_declarator = id_declarator->declarator;
14119 if (id_declarator && id_declarator->kind == cdk_id)
14120 error ("%H%sparameter pack %qD cannot have a default argument",
14121 &declarator_token_start->location,
14122 kind, id_declarator->u.id.unqualified_name);
14124 error ("%H%sparameter pack cannot have a default argument",
14125 &declarator_token_start->location, kind);
14127 default_argument = NULL_TREE;
14131 default_argument = NULL_TREE;
14133 return make_parameter_declarator (&decl_specifiers,
14138 /* Parse a default argument and return it.
14140 TEMPLATE_PARM_P is true if this is a default argument for a
14141 non-type template parameter. */
14143 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14145 tree default_argument = NULL_TREE;
14146 bool saved_greater_than_is_operator_p;
14147 bool saved_local_variables_forbidden_p;
14149 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14151 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14152 parser->greater_than_is_operator_p = !template_parm_p;
14153 /* Local variable names (and the `this' keyword) may not
14154 appear in a default argument. */
14155 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14156 parser->local_variables_forbidden_p = true;
14157 /* The default argument expression may cause implicitly
14158 defined member functions to be synthesized, which will
14159 result in garbage collection. We must treat this
14160 situation as if we were within the body of function so as
14161 to avoid collecting live data on the stack. */
14163 /* Parse the assignment-expression. */
14164 if (template_parm_p)
14165 push_deferring_access_checks (dk_no_deferred);
14167 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14168 if (template_parm_p)
14169 pop_deferring_access_checks ();
14170 /* Restore saved state. */
14172 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14173 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14175 return default_argument;
14178 /* Parse a function-body.
14181 compound_statement */
14184 cp_parser_function_body (cp_parser *parser)
14186 cp_parser_compound_statement (parser, NULL, false);
14189 /* Parse a ctor-initializer-opt followed by a function-body. Return
14190 true if a ctor-initializer was present. */
14193 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14196 bool ctor_initializer_p;
14198 /* Begin the function body. */
14199 body = begin_function_body ();
14200 /* Parse the optional ctor-initializer. */
14201 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14202 /* Parse the function-body. */
14203 cp_parser_function_body (parser);
14204 /* Finish the function body. */
14205 finish_function_body (body);
14207 return ctor_initializer_p;
14210 /* Parse an initializer.
14213 = initializer-clause
14214 ( expression-list )
14216 Returns an expression representing the initializer. If no
14217 initializer is present, NULL_TREE is returned.
14219 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14220 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14221 set to TRUE if there is no initializer present. If there is an
14222 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14223 is set to true; otherwise it is set to false. */
14226 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14227 bool* non_constant_p)
14232 /* Peek at the next token. */
14233 token = cp_lexer_peek_token (parser->lexer);
14235 /* Let our caller know whether or not this initializer was
14237 *is_direct_init = (token->type != CPP_EQ);
14238 /* Assume that the initializer is constant. */
14239 *non_constant_p = false;
14241 if (token->type == CPP_EQ)
14243 /* Consume the `='. */
14244 cp_lexer_consume_token (parser->lexer);
14245 /* Parse the initializer-clause. */
14246 init = cp_parser_initializer_clause (parser, non_constant_p);
14248 else if (token->type == CPP_OPEN_PAREN)
14249 init = cp_parser_parenthesized_expression_list (parser, false,
14251 /*allow_expansion_p=*/true,
14253 else if (token->type == CPP_OPEN_BRACE)
14255 maybe_warn_cpp0x ("extended initializer lists");
14256 init = cp_parser_braced_list (parser, non_constant_p);
14257 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14261 /* Anything else is an error. */
14262 cp_parser_error (parser, "expected initializer");
14263 init = error_mark_node;
14269 /* Parse an initializer-clause.
14271 initializer-clause:
14272 assignment-expression
14275 Returns an expression representing the initializer.
14277 If the `assignment-expression' production is used the value
14278 returned is simply a representation for the expression.
14280 Otherwise, calls cp_parser_braced_list. */
14283 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14287 /* Assume the expression is constant. */
14288 *non_constant_p = false;
14290 /* If it is not a `{', then we are looking at an
14291 assignment-expression. */
14292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14295 = cp_parser_constant_expression (parser,
14296 /*allow_non_constant_p=*/true,
14298 if (!*non_constant_p)
14299 initializer = fold_non_dependent_expr (initializer);
14302 initializer = cp_parser_braced_list (parser, non_constant_p);
14304 return initializer;
14307 /* Parse a brace-enclosed initializer list.
14310 { initializer-list , [opt] }
14313 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14314 the elements of the initializer-list (or NULL, if the last
14315 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14316 NULL_TREE. There is no way to detect whether or not the optional
14317 trailing `,' was provided. NON_CONSTANT_P is as for
14318 cp_parser_initializer. */
14321 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14325 /* Consume the `{' token. */
14326 cp_lexer_consume_token (parser->lexer);
14327 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14328 initializer = make_node (CONSTRUCTOR);
14329 /* If it's not a `}', then there is a non-trivial initializer. */
14330 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14332 /* Parse the initializer list. */
14333 CONSTRUCTOR_ELTS (initializer)
14334 = cp_parser_initializer_list (parser, non_constant_p);
14335 /* A trailing `,' token is allowed. */
14336 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14337 cp_lexer_consume_token (parser->lexer);
14339 /* Now, there should be a trailing `}'. */
14340 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14341 TREE_TYPE (initializer) = init_list_type_node;
14342 return initializer;
14345 /* Parse an initializer-list.
14348 initializer-clause ... [opt]
14349 initializer-list , initializer-clause ... [opt]
14354 identifier : initializer-clause
14355 initializer-list, identifier : initializer-clause
14357 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14358 for the initializer. If the INDEX of the elt is non-NULL, it is the
14359 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14360 as for cp_parser_initializer. */
14362 static VEC(constructor_elt,gc) *
14363 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14365 VEC(constructor_elt,gc) *v = NULL;
14367 /* Assume all of the expressions are constant. */
14368 *non_constant_p = false;
14370 /* Parse the rest of the list. */
14376 bool clause_non_constant_p;
14378 /* If the next token is an identifier and the following one is a
14379 colon, we are looking at the GNU designated-initializer
14381 if (cp_parser_allow_gnu_extensions_p (parser)
14382 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14383 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14385 /* Warn the user that they are using an extension. */
14386 pedwarn (OPT_pedantic,
14387 "ISO C++ does not allow designated initializers");
14388 /* Consume the identifier. */
14389 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14390 /* Consume the `:'. */
14391 cp_lexer_consume_token (parser->lexer);
14394 identifier = NULL_TREE;
14396 /* Parse the initializer. */
14397 initializer = cp_parser_initializer_clause (parser,
14398 &clause_non_constant_p);
14399 /* If any clause is non-constant, so is the entire initializer. */
14400 if (clause_non_constant_p)
14401 *non_constant_p = true;
14403 /* If we have an ellipsis, this is an initializer pack
14405 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14407 /* Consume the `...'. */
14408 cp_lexer_consume_token (parser->lexer);
14410 /* Turn the initializer into an initializer expansion. */
14411 initializer = make_pack_expansion (initializer);
14414 /* Add it to the vector. */
14415 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14417 /* If the next token is not a comma, we have reached the end of
14419 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14422 /* Peek at the next token. */
14423 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14424 /* If the next token is a `}', then we're still done. An
14425 initializer-clause can have a trailing `,' after the
14426 initializer-list and before the closing `}'. */
14427 if (token->type == CPP_CLOSE_BRACE)
14430 /* Consume the `,' token. */
14431 cp_lexer_consume_token (parser->lexer);
14437 /* Classes [gram.class] */
14439 /* Parse a class-name.
14445 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14446 to indicate that names looked up in dependent types should be
14447 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14448 keyword has been used to indicate that the name that appears next
14449 is a template. TAG_TYPE indicates the explicit tag given before
14450 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14451 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14452 is the class being defined in a class-head.
14454 Returns the TYPE_DECL representing the class. */
14457 cp_parser_class_name (cp_parser *parser,
14458 bool typename_keyword_p,
14459 bool template_keyword_p,
14460 enum tag_types tag_type,
14461 bool check_dependency_p,
14463 bool is_declaration)
14470 /* All class-names start with an identifier. */
14471 token = cp_lexer_peek_token (parser->lexer);
14472 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14474 cp_parser_error (parser, "expected class-name");
14475 return error_mark_node;
14478 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14479 to a template-id, so we save it here. */
14480 scope = parser->scope;
14481 if (scope == error_mark_node)
14482 return error_mark_node;
14484 /* Any name names a type if we're following the `typename' keyword
14485 in a qualified name where the enclosing scope is type-dependent. */
14486 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14487 && dependent_type_p (scope));
14488 /* Handle the common case (an identifier, but not a template-id)
14490 if (token->type == CPP_NAME
14491 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14493 cp_token *identifier_token;
14497 /* Look for the identifier. */
14498 identifier_token = cp_lexer_peek_token (parser->lexer);
14499 ambiguous_p = identifier_token->ambiguous_p;
14500 identifier = cp_parser_identifier (parser);
14501 /* If the next token isn't an identifier, we are certainly not
14502 looking at a class-name. */
14503 if (identifier == error_mark_node)
14504 decl = error_mark_node;
14505 /* If we know this is a type-name, there's no need to look it
14507 else if (typename_p)
14511 tree ambiguous_decls;
14512 /* If we already know that this lookup is ambiguous, then
14513 we've already issued an error message; there's no reason
14517 cp_parser_simulate_error (parser);
14518 return error_mark_node;
14520 /* If the next token is a `::', then the name must be a type
14523 [basic.lookup.qual]
14525 During the lookup for a name preceding the :: scope
14526 resolution operator, object, function, and enumerator
14527 names are ignored. */
14528 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14529 tag_type = typename_type;
14530 /* Look up the name. */
14531 decl = cp_parser_lookup_name (parser, identifier,
14533 /*is_template=*/false,
14534 /*is_namespace=*/false,
14535 check_dependency_p,
14537 identifier_token->location);
14538 if (ambiguous_decls)
14540 error ("%Hreference to %qD is ambiguous",
14541 &identifier_token->location, identifier);
14542 print_candidates (ambiguous_decls);
14543 if (cp_parser_parsing_tentatively (parser))
14545 identifier_token->ambiguous_p = true;
14546 cp_parser_simulate_error (parser);
14548 return error_mark_node;
14554 /* Try a template-id. */
14555 decl = cp_parser_template_id (parser, template_keyword_p,
14556 check_dependency_p,
14558 if (decl == error_mark_node)
14559 return error_mark_node;
14562 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14564 /* If this is a typename, create a TYPENAME_TYPE. */
14565 if (typename_p && decl != error_mark_node)
14567 decl = make_typename_type (scope, decl, typename_type,
14568 /*complain=*/tf_error);
14569 if (decl != error_mark_node)
14570 decl = TYPE_NAME (decl);
14573 /* Check to see that it is really the name of a class. */
14574 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14575 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14576 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14577 /* Situations like this:
14579 template <typename T> struct A {
14580 typename T::template X<int>::I i;
14583 are problematic. Is `T::template X<int>' a class-name? The
14584 standard does not seem to be definitive, but there is no other
14585 valid interpretation of the following `::'. Therefore, those
14586 names are considered class-names. */
14588 decl = make_typename_type (scope, decl, tag_type, tf_error);
14589 if (decl != error_mark_node)
14590 decl = TYPE_NAME (decl);
14592 else if (TREE_CODE (decl) != TYPE_DECL
14593 || TREE_TYPE (decl) == error_mark_node
14594 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14595 decl = error_mark_node;
14597 if (decl == error_mark_node)
14598 cp_parser_error (parser, "expected class-name");
14603 /* Parse a class-specifier.
14606 class-head { member-specification [opt] }
14608 Returns the TREE_TYPE representing the class. */
14611 cp_parser_class_specifier (cp_parser* parser)
14615 tree attributes = NULL_TREE;
14616 int has_trailing_semicolon;
14617 bool nested_name_specifier_p;
14618 unsigned saved_num_template_parameter_lists;
14619 bool saved_in_function_body;
14620 tree old_scope = NULL_TREE;
14621 tree scope = NULL_TREE;
14624 push_deferring_access_checks (dk_no_deferred);
14626 /* Parse the class-head. */
14627 type = cp_parser_class_head (parser,
14628 &nested_name_specifier_p,
14631 /* If the class-head was a semantic disaster, skip the entire body
14635 cp_parser_skip_to_end_of_block_or_statement (parser);
14636 pop_deferring_access_checks ();
14637 return error_mark_node;
14640 /* Look for the `{'. */
14641 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14643 pop_deferring_access_checks ();
14644 return error_mark_node;
14647 /* Process the base classes. If they're invalid, skip the
14648 entire class body. */
14649 if (!xref_basetypes (type, bases))
14651 /* Consuming the closing brace yields better error messages
14653 if (cp_parser_skip_to_closing_brace (parser))
14654 cp_lexer_consume_token (parser->lexer);
14655 pop_deferring_access_checks ();
14656 return error_mark_node;
14659 /* Issue an error message if type-definitions are forbidden here. */
14660 cp_parser_check_type_definition (parser);
14661 /* Remember that we are defining one more class. */
14662 ++parser->num_classes_being_defined;
14663 /* Inside the class, surrounding template-parameter-lists do not
14665 saved_num_template_parameter_lists
14666 = parser->num_template_parameter_lists;
14667 parser->num_template_parameter_lists = 0;
14668 /* We are not in a function body. */
14669 saved_in_function_body = parser->in_function_body;
14670 parser->in_function_body = false;
14672 /* Start the class. */
14673 if (nested_name_specifier_p)
14675 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14676 old_scope = push_inner_scope (scope);
14678 type = begin_class_definition (type, attributes);
14680 if (type == error_mark_node)
14681 /* If the type is erroneous, skip the entire body of the class. */
14682 cp_parser_skip_to_closing_brace (parser);
14684 /* Parse the member-specification. */
14685 cp_parser_member_specification_opt (parser);
14687 /* Look for the trailing `}'. */
14688 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14689 /* We get better error messages by noticing a common problem: a
14690 missing trailing `;'. */
14691 token = cp_lexer_peek_token (parser->lexer);
14692 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14693 /* Look for trailing attributes to apply to this class. */
14694 if (cp_parser_allow_gnu_extensions_p (parser))
14695 attributes = cp_parser_attributes_opt (parser);
14696 if (type != error_mark_node)
14697 type = finish_struct (type, attributes);
14698 if (nested_name_specifier_p)
14699 pop_inner_scope (old_scope, scope);
14700 /* If this class is not itself within the scope of another class,
14701 then we need to parse the bodies of all of the queued function
14702 definitions. Note that the queued functions defined in a class
14703 are not always processed immediately following the
14704 class-specifier for that class. Consider:
14707 struct B { void f() { sizeof (A); } };
14710 If `f' were processed before the processing of `A' were
14711 completed, there would be no way to compute the size of `A'.
14712 Note that the nesting we are interested in here is lexical --
14713 not the semantic nesting given by TYPE_CONTEXT. In particular,
14716 struct A { struct B; };
14717 struct A::B { void f() { } };
14719 there is no need to delay the parsing of `A::B::f'. */
14720 if (--parser->num_classes_being_defined == 0)
14724 tree class_type = NULL_TREE;
14725 tree pushed_scope = NULL_TREE;
14727 /* In a first pass, parse default arguments to the functions.
14728 Then, in a second pass, parse the bodies of the functions.
14729 This two-phased approach handles cases like:
14737 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14738 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14739 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14740 TREE_PURPOSE (parser->unparsed_functions_queues)
14741 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14743 fn = TREE_VALUE (queue_entry);
14744 /* If there are default arguments that have not yet been processed,
14745 take care of them now. */
14746 if (class_type != TREE_PURPOSE (queue_entry))
14749 pop_scope (pushed_scope);
14750 class_type = TREE_PURPOSE (queue_entry);
14751 pushed_scope = push_scope (class_type);
14753 /* Make sure that any template parameters are in scope. */
14754 maybe_begin_member_template_processing (fn);
14755 /* Parse the default argument expressions. */
14756 cp_parser_late_parsing_default_args (parser, fn);
14757 /* Remove any template parameters from the symbol table. */
14758 maybe_end_member_template_processing ();
14761 pop_scope (pushed_scope);
14762 /* Now parse the body of the functions. */
14763 for (TREE_VALUE (parser->unparsed_functions_queues)
14764 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14765 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14766 TREE_VALUE (parser->unparsed_functions_queues)
14767 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14769 /* Figure out which function we need to process. */
14770 fn = TREE_VALUE (queue_entry);
14771 /* Parse the function. */
14772 cp_parser_late_parsing_for_member (parser, fn);
14776 /* Put back any saved access checks. */
14777 pop_deferring_access_checks ();
14779 /* Restore saved state. */
14780 parser->in_function_body = saved_in_function_body;
14781 parser->num_template_parameter_lists
14782 = saved_num_template_parameter_lists;
14787 /* Parse a class-head.
14790 class-key identifier [opt] base-clause [opt]
14791 class-key nested-name-specifier identifier base-clause [opt]
14792 class-key nested-name-specifier [opt] template-id
14796 class-key attributes identifier [opt] base-clause [opt]
14797 class-key attributes nested-name-specifier identifier base-clause [opt]
14798 class-key attributes nested-name-specifier [opt] template-id
14801 Upon return BASES is initialized to the list of base classes (or
14802 NULL, if there are none) in the same form returned by
14803 cp_parser_base_clause.
14805 Returns the TYPE of the indicated class. Sets
14806 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14807 involving a nested-name-specifier was used, and FALSE otherwise.
14809 Returns error_mark_node if this is not a class-head.
14811 Returns NULL_TREE if the class-head is syntactically valid, but
14812 semantically invalid in a way that means we should skip the entire
14813 body of the class. */
14816 cp_parser_class_head (cp_parser* parser,
14817 bool* nested_name_specifier_p,
14818 tree *attributes_p,
14821 tree nested_name_specifier;
14822 enum tag_types class_key;
14823 tree id = NULL_TREE;
14824 tree type = NULL_TREE;
14826 bool template_id_p = false;
14827 bool qualified_p = false;
14828 bool invalid_nested_name_p = false;
14829 bool invalid_explicit_specialization_p = false;
14830 tree pushed_scope = NULL_TREE;
14831 unsigned num_templates;
14832 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14833 /* Assume no nested-name-specifier will be present. */
14834 *nested_name_specifier_p = false;
14835 /* Assume no template parameter lists will be used in defining the
14839 *bases = NULL_TREE;
14841 /* Look for the class-key. */
14842 class_key = cp_parser_class_key (parser);
14843 if (class_key == none_type)
14844 return error_mark_node;
14846 /* Parse the attributes. */
14847 attributes = cp_parser_attributes_opt (parser);
14849 /* If the next token is `::', that is invalid -- but sometimes
14850 people do try to write:
14854 Handle this gracefully by accepting the extra qualifier, and then
14855 issuing an error about it later if this really is a
14856 class-head. If it turns out just to be an elaborated type
14857 specifier, remain silent. */
14858 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14859 qualified_p = true;
14861 push_deferring_access_checks (dk_no_check);
14863 /* Determine the name of the class. Begin by looking for an
14864 optional nested-name-specifier. */
14865 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
14866 nested_name_specifier
14867 = cp_parser_nested_name_specifier_opt (parser,
14868 /*typename_keyword_p=*/false,
14869 /*check_dependency_p=*/false,
14871 /*is_declaration=*/false);
14872 /* If there was a nested-name-specifier, then there *must* be an
14874 if (nested_name_specifier)
14876 type_start_token = cp_lexer_peek_token (parser->lexer);
14877 /* Although the grammar says `identifier', it really means
14878 `class-name' or `template-name'. You are only allowed to
14879 define a class that has already been declared with this
14882 The proposed resolution for Core Issue 180 says that wherever
14883 you see `class T::X' you should treat `X' as a type-name.
14885 It is OK to define an inaccessible class; for example:
14887 class A { class B; };
14890 We do not know if we will see a class-name, or a
14891 template-name. We look for a class-name first, in case the
14892 class-name is a template-id; if we looked for the
14893 template-name first we would stop after the template-name. */
14894 cp_parser_parse_tentatively (parser);
14895 type = cp_parser_class_name (parser,
14896 /*typename_keyword_p=*/false,
14897 /*template_keyword_p=*/false,
14899 /*check_dependency_p=*/false,
14900 /*class_head_p=*/true,
14901 /*is_declaration=*/false);
14902 /* If that didn't work, ignore the nested-name-specifier. */
14903 if (!cp_parser_parse_definitely (parser))
14905 invalid_nested_name_p = true;
14906 type_start_token = cp_lexer_peek_token (parser->lexer);
14907 id = cp_parser_identifier (parser);
14908 if (id == error_mark_node)
14911 /* If we could not find a corresponding TYPE, treat this
14912 declaration like an unqualified declaration. */
14913 if (type == error_mark_node)
14914 nested_name_specifier = NULL_TREE;
14915 /* Otherwise, count the number of templates used in TYPE and its
14916 containing scopes. */
14921 for (scope = TREE_TYPE (type);
14922 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14923 scope = (TYPE_P (scope)
14924 ? TYPE_CONTEXT (scope)
14925 : DECL_CONTEXT (scope)))
14927 && CLASS_TYPE_P (scope)
14928 && CLASSTYPE_TEMPLATE_INFO (scope)
14929 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14930 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14934 /* Otherwise, the identifier is optional. */
14937 /* We don't know whether what comes next is a template-id,
14938 an identifier, or nothing at all. */
14939 cp_parser_parse_tentatively (parser);
14940 /* Check for a template-id. */
14941 type_start_token = cp_lexer_peek_token (parser->lexer);
14942 id = cp_parser_template_id (parser,
14943 /*template_keyword_p=*/false,
14944 /*check_dependency_p=*/true,
14945 /*is_declaration=*/true);
14946 /* If that didn't work, it could still be an identifier. */
14947 if (!cp_parser_parse_definitely (parser))
14949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14951 type_start_token = cp_lexer_peek_token (parser->lexer);
14952 id = cp_parser_identifier (parser);
14959 template_id_p = true;
14964 pop_deferring_access_checks ();
14967 cp_parser_check_for_invalid_template_id (parser, id,
14968 type_start_token->location);
14970 /* If it's not a `:' or a `{' then we can't really be looking at a
14971 class-head, since a class-head only appears as part of a
14972 class-specifier. We have to detect this situation before calling
14973 xref_tag, since that has irreversible side-effects. */
14974 if (!cp_parser_next_token_starts_class_definition_p (parser))
14976 cp_parser_error (parser, "expected %<{%> or %<:%>");
14977 return error_mark_node;
14980 /* At this point, we're going ahead with the class-specifier, even
14981 if some other problem occurs. */
14982 cp_parser_commit_to_tentative_parse (parser);
14983 /* Issue the error about the overly-qualified name now. */
14985 cp_parser_error (parser,
14986 "global qualification of class name is invalid");
14987 else if (invalid_nested_name_p)
14988 cp_parser_error (parser,
14989 "qualified name does not name a class");
14990 else if (nested_name_specifier)
14994 /* Reject typedef-names in class heads. */
14995 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14997 error ("%Hinvalid class name in declaration of %qD",
14998 &type_start_token->location, type);
15003 /* Figure out in what scope the declaration is being placed. */
15004 scope = current_scope ();
15005 /* If that scope does not contain the scope in which the
15006 class was originally declared, the program is invalid. */
15007 if (scope && !is_ancestor (scope, nested_name_specifier))
15009 if (at_namespace_scope_p ())
15010 error ("%Hdeclaration of %qD in namespace %qD which does not "
15012 &type_start_token->location,
15013 type, scope, nested_name_specifier);
15015 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15016 &type_start_token->location,
15017 type, scope, nested_name_specifier);
15023 A declarator-id shall not be qualified except for the
15024 definition of a ... nested class outside of its class
15025 ... [or] the definition or explicit instantiation of a
15026 class member of a namespace outside of its namespace. */
15027 if (scope == nested_name_specifier)
15029 permerror ("%Hextra qualification not allowed",
15030 &nested_name_specifier_token_start->location);
15031 nested_name_specifier = NULL_TREE;
15035 /* An explicit-specialization must be preceded by "template <>". If
15036 it is not, try to recover gracefully. */
15037 if (at_namespace_scope_p ()
15038 && parser->num_template_parameter_lists == 0
15041 error ("%Han explicit specialization must be preceded by %<template <>%>",
15042 &type_start_token->location);
15043 invalid_explicit_specialization_p = true;
15044 /* Take the same action that would have been taken by
15045 cp_parser_explicit_specialization. */
15046 ++parser->num_template_parameter_lists;
15047 begin_specialization ();
15049 /* There must be no "return" statements between this point and the
15050 end of this function; set "type "to the correct return value and
15051 use "goto done;" to return. */
15052 /* Make sure that the right number of template parameters were
15054 if (!cp_parser_check_template_parameters (parser, num_templates,
15055 type_start_token->location))
15057 /* If something went wrong, there is no point in even trying to
15058 process the class-definition. */
15063 /* Look up the type. */
15066 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15067 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15068 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15070 error ("%Hfunction template %qD redeclared as a class template",
15071 &type_start_token->location, id);
15072 type = error_mark_node;
15076 type = TREE_TYPE (id);
15077 type = maybe_process_partial_specialization (type);
15079 if (nested_name_specifier)
15080 pushed_scope = push_scope (nested_name_specifier);
15082 else if (nested_name_specifier)
15088 template <typename T> struct S { struct T };
15089 template <typename T> struct S<T>::T { };
15091 we will get a TYPENAME_TYPE when processing the definition of
15092 `S::T'. We need to resolve it to the actual type before we
15093 try to define it. */
15094 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15096 class_type = resolve_typename_type (TREE_TYPE (type),
15097 /*only_current_p=*/false);
15098 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15099 type = TYPE_NAME (class_type);
15102 cp_parser_error (parser, "could not resolve typename type");
15103 type = error_mark_node;
15107 if (maybe_process_partial_specialization (TREE_TYPE (type))
15108 == error_mark_node)
15114 class_type = current_class_type;
15115 /* Enter the scope indicated by the nested-name-specifier. */
15116 pushed_scope = push_scope (nested_name_specifier);
15117 /* Get the canonical version of this type. */
15118 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15119 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15120 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15122 type = push_template_decl (type);
15123 if (type == error_mark_node)
15130 type = TREE_TYPE (type);
15131 *nested_name_specifier_p = true;
15133 else /* The name is not a nested name. */
15135 /* If the class was unnamed, create a dummy name. */
15137 id = make_anon_name ();
15138 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15139 parser->num_template_parameter_lists);
15142 /* Indicate whether this class was declared as a `class' or as a
15144 if (TREE_CODE (type) == RECORD_TYPE)
15145 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15146 cp_parser_check_class_key (class_key, type);
15148 /* If this type was already complete, and we see another definition,
15149 that's an error. */
15150 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15152 error ("%Hredefinition of %q#T",
15153 &type_start_token->location, type);
15154 error ("%Hprevious definition of %q+#T",
15155 &type_start_token->location, type);
15159 else if (type == error_mark_node)
15162 /* We will have entered the scope containing the class; the names of
15163 base classes should be looked up in that context. For example:
15165 struct A { struct B {}; struct C; };
15166 struct A::C : B {};
15170 /* Get the list of base-classes, if there is one. */
15171 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15172 *bases = cp_parser_base_clause (parser);
15175 /* Leave the scope given by the nested-name-specifier. We will
15176 enter the class scope itself while processing the members. */
15178 pop_scope (pushed_scope);
15180 if (invalid_explicit_specialization_p)
15182 end_specialization ();
15183 --parser->num_template_parameter_lists;
15185 *attributes_p = attributes;
15189 /* Parse a class-key.
15196 Returns the kind of class-key specified, or none_type to indicate
15199 static enum tag_types
15200 cp_parser_class_key (cp_parser* parser)
15203 enum tag_types tag_type;
15205 /* Look for the class-key. */
15206 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15210 /* Check to see if the TOKEN is a class-key. */
15211 tag_type = cp_parser_token_is_class_key (token);
15213 cp_parser_error (parser, "expected class-key");
15217 /* Parse an (optional) member-specification.
15219 member-specification:
15220 member-declaration member-specification [opt]
15221 access-specifier : member-specification [opt] */
15224 cp_parser_member_specification_opt (cp_parser* parser)
15231 /* Peek at the next token. */
15232 token = cp_lexer_peek_token (parser->lexer);
15233 /* If it's a `}', or EOF then we've seen all the members. */
15234 if (token->type == CPP_CLOSE_BRACE
15235 || token->type == CPP_EOF
15236 || token->type == CPP_PRAGMA_EOL)
15239 /* See if this token is a keyword. */
15240 keyword = token->keyword;
15244 case RID_PROTECTED:
15246 /* Consume the access-specifier. */
15247 cp_lexer_consume_token (parser->lexer);
15248 /* Remember which access-specifier is active. */
15249 current_access_specifier = token->u.value;
15250 /* Look for the `:'. */
15251 cp_parser_require (parser, CPP_COLON, "%<:%>");
15255 /* Accept #pragmas at class scope. */
15256 if (token->type == CPP_PRAGMA)
15258 cp_parser_pragma (parser, pragma_external);
15262 /* Otherwise, the next construction must be a
15263 member-declaration. */
15264 cp_parser_member_declaration (parser);
15269 /* Parse a member-declaration.
15271 member-declaration:
15272 decl-specifier-seq [opt] member-declarator-list [opt] ;
15273 function-definition ; [opt]
15274 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15276 template-declaration
15278 member-declarator-list:
15280 member-declarator-list , member-declarator
15283 declarator pure-specifier [opt]
15284 declarator constant-initializer [opt]
15285 identifier [opt] : constant-expression
15289 member-declaration:
15290 __extension__ member-declaration
15293 declarator attributes [opt] pure-specifier [opt]
15294 declarator attributes [opt] constant-initializer [opt]
15295 identifier [opt] attributes [opt] : constant-expression
15299 member-declaration:
15300 static_assert-declaration */
15303 cp_parser_member_declaration (cp_parser* parser)
15305 cp_decl_specifier_seq decl_specifiers;
15306 tree prefix_attributes;
15308 int declares_class_or_enum;
15310 cp_token *token = NULL;
15311 cp_token *decl_spec_token_start = NULL;
15312 cp_token *initializer_token_start = NULL;
15313 int saved_pedantic;
15315 /* Check for the `__extension__' keyword. */
15316 if (cp_parser_extension_opt (parser, &saved_pedantic))
15319 cp_parser_member_declaration (parser);
15320 /* Restore the old value of the PEDANTIC flag. */
15321 pedantic = saved_pedantic;
15326 /* Check for a template-declaration. */
15327 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15329 /* An explicit specialization here is an error condition, and we
15330 expect the specialization handler to detect and report this. */
15331 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15332 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15333 cp_parser_explicit_specialization (parser);
15335 cp_parser_template_declaration (parser, /*member_p=*/true);
15340 /* Check for a using-declaration. */
15341 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15343 /* Parse the using-declaration. */
15344 cp_parser_using_declaration (parser,
15345 /*access_declaration_p=*/false);
15349 /* Check for @defs. */
15350 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15353 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15354 ivar = ivar_chains;
15358 ivar = TREE_CHAIN (member);
15359 TREE_CHAIN (member) = NULL_TREE;
15360 finish_member_declaration (member);
15365 /* If the next token is `static_assert' we have a static assertion. */
15366 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15368 cp_parser_static_assert (parser, /*member_p=*/true);
15372 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15375 /* Parse the decl-specifier-seq. */
15376 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15377 cp_parser_decl_specifier_seq (parser,
15378 CP_PARSER_FLAGS_OPTIONAL,
15380 &declares_class_or_enum);
15381 prefix_attributes = decl_specifiers.attributes;
15382 decl_specifiers.attributes = NULL_TREE;
15383 /* Check for an invalid type-name. */
15384 if (!decl_specifiers.type
15385 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15387 /* If there is no declarator, then the decl-specifier-seq should
15389 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15391 /* If there was no decl-specifier-seq, and the next token is a
15392 `;', then we have something like:
15398 Each member-declaration shall declare at least one member
15399 name of the class. */
15400 if (!decl_specifiers.any_specifiers_p)
15402 cp_token *token = cp_lexer_peek_token (parser->lexer);
15403 if (!in_system_header_at (token->location))
15404 pedwarn (OPT_pedantic, "%Hextra %<;%>", &token->location);
15410 /* See if this declaration is a friend. */
15411 friend_p = cp_parser_friend_p (&decl_specifiers);
15412 /* If there were decl-specifiers, check to see if there was
15413 a class-declaration. */
15414 type = check_tag_decl (&decl_specifiers);
15415 /* Nested classes have already been added to the class, but
15416 a `friend' needs to be explicitly registered. */
15419 /* If the `friend' keyword was present, the friend must
15420 be introduced with a class-key. */
15421 if (!declares_class_or_enum)
15422 error ("%Ha class-key must be used when declaring a friend",
15423 &decl_spec_token_start->location);
15426 template <typename T> struct A {
15427 friend struct A<T>::B;
15430 A<T>::B will be represented by a TYPENAME_TYPE, and
15431 therefore not recognized by check_tag_decl. */
15433 && decl_specifiers.type
15434 && TYPE_P (decl_specifiers.type))
15435 type = decl_specifiers.type;
15436 if (!type || !TYPE_P (type))
15437 error ("%Hfriend declaration does not name a class or "
15438 "function", &decl_spec_token_start->location);
15440 make_friend_class (current_class_type, type,
15441 /*complain=*/true);
15443 /* If there is no TYPE, an error message will already have
15445 else if (!type || type == error_mark_node)
15447 /* An anonymous aggregate has to be handled specially; such
15448 a declaration really declares a data member (with a
15449 particular type), as opposed to a nested class. */
15450 else if (ANON_AGGR_TYPE_P (type))
15452 /* Remove constructors and such from TYPE, now that we
15453 know it is an anonymous aggregate. */
15454 fixup_anonymous_aggr (type);
15455 /* And make the corresponding data member. */
15456 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15457 /* Add it to the class. */
15458 finish_member_declaration (decl);
15461 cp_parser_check_access_in_redeclaration
15463 decl_spec_token_start->location);
15468 /* See if these declarations will be friends. */
15469 friend_p = cp_parser_friend_p (&decl_specifiers);
15471 /* Keep going until we hit the `;' at the end of the
15473 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15475 tree attributes = NULL_TREE;
15476 tree first_attribute;
15478 /* Peek at the next token. */
15479 token = cp_lexer_peek_token (parser->lexer);
15481 /* Check for a bitfield declaration. */
15482 if (token->type == CPP_COLON
15483 || (token->type == CPP_NAME
15484 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15490 /* Get the name of the bitfield. Note that we cannot just
15491 check TOKEN here because it may have been invalidated by
15492 the call to cp_lexer_peek_nth_token above. */
15493 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15494 identifier = cp_parser_identifier (parser);
15496 identifier = NULL_TREE;
15498 /* Consume the `:' token. */
15499 cp_lexer_consume_token (parser->lexer);
15500 /* Get the width of the bitfield. */
15502 = cp_parser_constant_expression (parser,
15503 /*allow_non_constant=*/false,
15506 /* Look for attributes that apply to the bitfield. */
15507 attributes = cp_parser_attributes_opt (parser);
15508 /* Remember which attributes are prefix attributes and
15510 first_attribute = attributes;
15511 /* Combine the attributes. */
15512 attributes = chainon (prefix_attributes, attributes);
15514 /* Create the bitfield declaration. */
15515 decl = grokbitfield (identifier
15516 ? make_id_declarator (NULL_TREE,
15526 cp_declarator *declarator;
15528 tree asm_specification;
15529 int ctor_dtor_or_conv_p;
15531 /* Parse the declarator. */
15533 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15534 &ctor_dtor_or_conv_p,
15535 /*parenthesized_p=*/NULL,
15536 /*member_p=*/true);
15538 /* If something went wrong parsing the declarator, make sure
15539 that we at least consume some tokens. */
15540 if (declarator == cp_error_declarator)
15542 /* Skip to the end of the statement. */
15543 cp_parser_skip_to_end_of_statement (parser);
15544 /* If the next token is not a semicolon, that is
15545 probably because we just skipped over the body of
15546 a function. So, we consume a semicolon if
15547 present, but do not issue an error message if it
15549 if (cp_lexer_next_token_is (parser->lexer,
15551 cp_lexer_consume_token (parser->lexer);
15555 if (declares_class_or_enum & 2)
15556 cp_parser_check_for_definition_in_return_type
15557 (declarator, decl_specifiers.type,
15558 decl_specifiers.type_location);
15560 /* Look for an asm-specification. */
15561 asm_specification = cp_parser_asm_specification_opt (parser);
15562 /* Look for attributes that apply to the declaration. */
15563 attributes = cp_parser_attributes_opt (parser);
15564 /* Remember which attributes are prefix attributes and
15566 first_attribute = attributes;
15567 /* Combine the attributes. */
15568 attributes = chainon (prefix_attributes, attributes);
15570 /* If it's an `=', then we have a constant-initializer or a
15571 pure-specifier. It is not correct to parse the
15572 initializer before registering the member declaration
15573 since the member declaration should be in scope while
15574 its initializer is processed. However, the rest of the
15575 front end does not yet provide an interface that allows
15576 us to handle this correctly. */
15577 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15581 A pure-specifier shall be used only in the declaration of
15582 a virtual function.
15584 A member-declarator can contain a constant-initializer
15585 only if it declares a static member of integral or
15588 Therefore, if the DECLARATOR is for a function, we look
15589 for a pure-specifier; otherwise, we look for a
15590 constant-initializer. When we call `grokfield', it will
15591 perform more stringent semantics checks. */
15592 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15593 if (function_declarator_p (declarator))
15594 initializer = cp_parser_pure_specifier (parser);
15596 /* Parse the initializer. */
15597 initializer = cp_parser_constant_initializer (parser);
15599 /* Otherwise, there is no initializer. */
15601 initializer = NULL_TREE;
15603 /* See if we are probably looking at a function
15604 definition. We are certainly not looking at a
15605 member-declarator. Calling `grokfield' has
15606 side-effects, so we must not do it unless we are sure
15607 that we are looking at a member-declarator. */
15608 if (cp_parser_token_starts_function_definition_p
15609 (cp_lexer_peek_token (parser->lexer)))
15611 /* The grammar does not allow a pure-specifier to be
15612 used when a member function is defined. (It is
15613 possible that this fact is an oversight in the
15614 standard, since a pure function may be defined
15615 outside of the class-specifier. */
15617 error ("%Hpure-specifier on function-definition",
15618 &initializer_token_start->location);
15619 decl = cp_parser_save_member_function_body (parser,
15623 /* If the member was not a friend, declare it here. */
15625 finish_member_declaration (decl);
15626 /* Peek at the next token. */
15627 token = cp_lexer_peek_token (parser->lexer);
15628 /* If the next token is a semicolon, consume it. */
15629 if (token->type == CPP_SEMICOLON)
15630 cp_lexer_consume_token (parser->lexer);
15634 /* Create the declaration. */
15635 decl = grokfield (declarator, &decl_specifiers,
15636 initializer, /*init_const_expr_p=*/true,
15641 /* Reset PREFIX_ATTRIBUTES. */
15642 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15643 attributes = TREE_CHAIN (attributes);
15645 TREE_CHAIN (attributes) = NULL_TREE;
15647 /* If there is any qualification still in effect, clear it
15648 now; we will be starting fresh with the next declarator. */
15649 parser->scope = NULL_TREE;
15650 parser->qualifying_scope = NULL_TREE;
15651 parser->object_scope = NULL_TREE;
15652 /* If it's a `,', then there are more declarators. */
15653 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15654 cp_lexer_consume_token (parser->lexer);
15655 /* If the next token isn't a `;', then we have a parse error. */
15656 else if (cp_lexer_next_token_is_not (parser->lexer,
15659 cp_parser_error (parser, "expected %<;%>");
15660 /* Skip tokens until we find a `;'. */
15661 cp_parser_skip_to_end_of_statement (parser);
15668 /* Add DECL to the list of members. */
15670 finish_member_declaration (decl);
15672 if (TREE_CODE (decl) == FUNCTION_DECL)
15673 cp_parser_save_default_args (parser, decl);
15678 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15681 /* Parse a pure-specifier.
15686 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15687 Otherwise, ERROR_MARK_NODE is returned. */
15690 cp_parser_pure_specifier (cp_parser* parser)
15694 /* Look for the `=' token. */
15695 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15696 return error_mark_node;
15697 /* Look for the `0' token. */
15698 token = cp_lexer_consume_token (parser->lexer);
15700 /* Accept = default or = delete in c++0x mode. */
15701 if (token->keyword == RID_DEFAULT
15702 || token->keyword == RID_DELETE)
15704 maybe_warn_cpp0x ("defaulted and deleted functions");
15705 return token->u.value;
15708 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15709 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15711 cp_parser_error (parser,
15712 "invalid pure specifier (only %<= 0%> is allowed)");
15713 cp_parser_skip_to_end_of_statement (parser);
15714 return error_mark_node;
15716 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15718 error ("%Htemplates may not be %<virtual%>", &token->location);
15719 return error_mark_node;
15722 return integer_zero_node;
15725 /* Parse a constant-initializer.
15727 constant-initializer:
15728 = constant-expression
15730 Returns a representation of the constant-expression. */
15733 cp_parser_constant_initializer (cp_parser* parser)
15735 /* Look for the `=' token. */
15736 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15737 return error_mark_node;
15739 /* It is invalid to write:
15741 struct S { static const int i = { 7 }; };
15744 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15746 cp_parser_error (parser,
15747 "a brace-enclosed initializer is not allowed here");
15748 /* Consume the opening brace. */
15749 cp_lexer_consume_token (parser->lexer);
15750 /* Skip the initializer. */
15751 cp_parser_skip_to_closing_brace (parser);
15752 /* Look for the trailing `}'. */
15753 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15755 return error_mark_node;
15758 return cp_parser_constant_expression (parser,
15759 /*allow_non_constant=*/false,
15763 /* Derived classes [gram.class.derived] */
15765 /* Parse a base-clause.
15768 : base-specifier-list
15770 base-specifier-list:
15771 base-specifier ... [opt]
15772 base-specifier-list , base-specifier ... [opt]
15774 Returns a TREE_LIST representing the base-classes, in the order in
15775 which they were declared. The representation of each node is as
15776 described by cp_parser_base_specifier.
15778 In the case that no bases are specified, this function will return
15779 NULL_TREE, not ERROR_MARK_NODE. */
15782 cp_parser_base_clause (cp_parser* parser)
15784 tree bases = NULL_TREE;
15786 /* Look for the `:' that begins the list. */
15787 cp_parser_require (parser, CPP_COLON, "%<:%>");
15789 /* Scan the base-specifier-list. */
15794 bool pack_expansion_p = false;
15796 /* Look for the base-specifier. */
15797 base = cp_parser_base_specifier (parser);
15798 /* Look for the (optional) ellipsis. */
15799 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15801 /* Consume the `...'. */
15802 cp_lexer_consume_token (parser->lexer);
15804 pack_expansion_p = true;
15807 /* Add BASE to the front of the list. */
15808 if (base != error_mark_node)
15810 if (pack_expansion_p)
15811 /* Make this a pack expansion type. */
15812 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15815 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15817 TREE_CHAIN (base) = bases;
15821 /* Peek at the next token. */
15822 token = cp_lexer_peek_token (parser->lexer);
15823 /* If it's not a comma, then the list is complete. */
15824 if (token->type != CPP_COMMA)
15826 /* Consume the `,'. */
15827 cp_lexer_consume_token (parser->lexer);
15830 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15831 base class had a qualified name. However, the next name that
15832 appears is certainly not qualified. */
15833 parser->scope = NULL_TREE;
15834 parser->qualifying_scope = NULL_TREE;
15835 parser->object_scope = NULL_TREE;
15837 return nreverse (bases);
15840 /* Parse a base-specifier.
15843 :: [opt] nested-name-specifier [opt] class-name
15844 virtual access-specifier [opt] :: [opt] nested-name-specifier
15846 access-specifier virtual [opt] :: [opt] nested-name-specifier
15849 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15850 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15851 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15852 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15855 cp_parser_base_specifier (cp_parser* parser)
15859 bool virtual_p = false;
15860 bool duplicate_virtual_error_issued_p = false;
15861 bool duplicate_access_error_issued_p = false;
15862 bool class_scope_p, template_p;
15863 tree access = access_default_node;
15866 /* Process the optional `virtual' and `access-specifier'. */
15869 /* Peek at the next token. */
15870 token = cp_lexer_peek_token (parser->lexer);
15871 /* Process `virtual'. */
15872 switch (token->keyword)
15875 /* If `virtual' appears more than once, issue an error. */
15876 if (virtual_p && !duplicate_virtual_error_issued_p)
15878 cp_parser_error (parser,
15879 "%<virtual%> specified more than once in base-specified");
15880 duplicate_virtual_error_issued_p = true;
15885 /* Consume the `virtual' token. */
15886 cp_lexer_consume_token (parser->lexer);
15891 case RID_PROTECTED:
15893 /* If more than one access specifier appears, issue an
15895 if (access != access_default_node
15896 && !duplicate_access_error_issued_p)
15898 cp_parser_error (parser,
15899 "more than one access specifier in base-specified");
15900 duplicate_access_error_issued_p = true;
15903 access = ridpointers[(int) token->keyword];
15905 /* Consume the access-specifier. */
15906 cp_lexer_consume_token (parser->lexer);
15915 /* It is not uncommon to see programs mechanically, erroneously, use
15916 the 'typename' keyword to denote (dependent) qualified types
15917 as base classes. */
15918 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15920 token = cp_lexer_peek_token (parser->lexer);
15921 if (!processing_template_decl)
15922 error ("%Hkeyword %<typename%> not allowed outside of templates",
15925 error ("%Hkeyword %<typename%> not allowed in this context "
15926 "(the base class is implicitly a type)",
15928 cp_lexer_consume_token (parser->lexer);
15931 /* Look for the optional `::' operator. */
15932 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15933 /* Look for the nested-name-specifier. The simplest way to
15938 The keyword `typename' is not permitted in a base-specifier or
15939 mem-initializer; in these contexts a qualified name that
15940 depends on a template-parameter is implicitly assumed to be a
15943 is to pretend that we have seen the `typename' keyword at this
15945 cp_parser_nested_name_specifier_opt (parser,
15946 /*typename_keyword_p=*/true,
15947 /*check_dependency_p=*/true,
15949 /*is_declaration=*/true);
15950 /* If the base class is given by a qualified name, assume that names
15951 we see are type names or templates, as appropriate. */
15952 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15953 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15955 /* Finally, look for the class-name. */
15956 type = cp_parser_class_name (parser,
15960 /*check_dependency_p=*/true,
15961 /*class_head_p=*/false,
15962 /*is_declaration=*/true);
15964 if (type == error_mark_node)
15965 return error_mark_node;
15967 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15970 /* Exception handling [gram.exception] */
15972 /* Parse an (optional) exception-specification.
15974 exception-specification:
15975 throw ( type-id-list [opt] )
15977 Returns a TREE_LIST representing the exception-specification. The
15978 TREE_VALUE of each node is a type. */
15981 cp_parser_exception_specification_opt (cp_parser* parser)
15986 /* Peek at the next token. */
15987 token = cp_lexer_peek_token (parser->lexer);
15988 /* If it's not `throw', then there's no exception-specification. */
15989 if (!cp_parser_is_keyword (token, RID_THROW))
15992 /* Consume the `throw'. */
15993 cp_lexer_consume_token (parser->lexer);
15995 /* Look for the `('. */
15996 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15998 /* Peek at the next token. */
15999 token = cp_lexer_peek_token (parser->lexer);
16000 /* If it's not a `)', then there is a type-id-list. */
16001 if (token->type != CPP_CLOSE_PAREN)
16003 const char *saved_message;
16005 /* Types may not be defined in an exception-specification. */
16006 saved_message = parser->type_definition_forbidden_message;
16007 parser->type_definition_forbidden_message
16008 = "types may not be defined in an exception-specification";
16009 /* Parse the type-id-list. */
16010 type_id_list = cp_parser_type_id_list (parser);
16011 /* Restore the saved message. */
16012 parser->type_definition_forbidden_message = saved_message;
16015 type_id_list = empty_except_spec;
16017 /* Look for the `)'. */
16018 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16020 return type_id_list;
16023 /* Parse an (optional) type-id-list.
16027 type-id-list , type-id ... [opt]
16029 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16030 in the order that the types were presented. */
16033 cp_parser_type_id_list (cp_parser* parser)
16035 tree types = NULL_TREE;
16042 /* Get the next type-id. */
16043 type = cp_parser_type_id (parser);
16044 /* Parse the optional ellipsis. */
16045 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16047 /* Consume the `...'. */
16048 cp_lexer_consume_token (parser->lexer);
16050 /* Turn the type into a pack expansion expression. */
16051 type = make_pack_expansion (type);
16053 /* Add it to the list. */
16054 types = add_exception_specifier (types, type, /*complain=*/1);
16055 /* Peek at the next token. */
16056 token = cp_lexer_peek_token (parser->lexer);
16057 /* If it is not a `,', we are done. */
16058 if (token->type != CPP_COMMA)
16060 /* Consume the `,'. */
16061 cp_lexer_consume_token (parser->lexer);
16064 return nreverse (types);
16067 /* Parse a try-block.
16070 try compound-statement handler-seq */
16073 cp_parser_try_block (cp_parser* parser)
16077 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16078 try_block = begin_try_block ();
16079 cp_parser_compound_statement (parser, NULL, true);
16080 finish_try_block (try_block);
16081 cp_parser_handler_seq (parser);
16082 finish_handler_sequence (try_block);
16087 /* Parse a function-try-block.
16089 function-try-block:
16090 try ctor-initializer [opt] function-body handler-seq */
16093 cp_parser_function_try_block (cp_parser* parser)
16095 tree compound_stmt;
16097 bool ctor_initializer_p;
16099 /* Look for the `try' keyword. */
16100 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16102 /* Let the rest of the front end know where we are. */
16103 try_block = begin_function_try_block (&compound_stmt);
16104 /* Parse the function-body. */
16106 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16107 /* We're done with the `try' part. */
16108 finish_function_try_block (try_block);
16109 /* Parse the handlers. */
16110 cp_parser_handler_seq (parser);
16111 /* We're done with the handlers. */
16112 finish_function_handler_sequence (try_block, compound_stmt);
16114 return ctor_initializer_p;
16117 /* Parse a handler-seq.
16120 handler handler-seq [opt] */
16123 cp_parser_handler_seq (cp_parser* parser)
16129 /* Parse the handler. */
16130 cp_parser_handler (parser);
16131 /* Peek at the next token. */
16132 token = cp_lexer_peek_token (parser->lexer);
16133 /* If it's not `catch' then there are no more handlers. */
16134 if (!cp_parser_is_keyword (token, RID_CATCH))
16139 /* Parse a handler.
16142 catch ( exception-declaration ) compound-statement */
16145 cp_parser_handler (cp_parser* parser)
16150 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16151 handler = begin_handler ();
16152 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16153 declaration = cp_parser_exception_declaration (parser);
16154 finish_handler_parms (declaration, handler);
16155 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16156 cp_parser_compound_statement (parser, NULL, false);
16157 finish_handler (handler);
16160 /* Parse an exception-declaration.
16162 exception-declaration:
16163 type-specifier-seq declarator
16164 type-specifier-seq abstract-declarator
16168 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16169 ellipsis variant is used. */
16172 cp_parser_exception_declaration (cp_parser* parser)
16174 cp_decl_specifier_seq type_specifiers;
16175 cp_declarator *declarator;
16176 const char *saved_message;
16178 /* If it's an ellipsis, it's easy to handle. */
16179 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16181 /* Consume the `...' token. */
16182 cp_lexer_consume_token (parser->lexer);
16186 /* Types may not be defined in exception-declarations. */
16187 saved_message = parser->type_definition_forbidden_message;
16188 parser->type_definition_forbidden_message
16189 = "types may not be defined in exception-declarations";
16191 /* Parse the type-specifier-seq. */
16192 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16194 /* If it's a `)', then there is no declarator. */
16195 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16198 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16199 /*ctor_dtor_or_conv_p=*/NULL,
16200 /*parenthesized_p=*/NULL,
16201 /*member_p=*/false);
16203 /* Restore the saved message. */
16204 parser->type_definition_forbidden_message = saved_message;
16206 if (!type_specifiers.any_specifiers_p)
16207 return error_mark_node;
16209 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16212 /* Parse a throw-expression.
16215 throw assignment-expression [opt]
16217 Returns a THROW_EXPR representing the throw-expression. */
16220 cp_parser_throw_expression (cp_parser* parser)
16225 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16226 token = cp_lexer_peek_token (parser->lexer);
16227 /* Figure out whether or not there is an assignment-expression
16228 following the "throw" keyword. */
16229 if (token->type == CPP_COMMA
16230 || token->type == CPP_SEMICOLON
16231 || token->type == CPP_CLOSE_PAREN
16232 || token->type == CPP_CLOSE_SQUARE
16233 || token->type == CPP_CLOSE_BRACE
16234 || token->type == CPP_COLON)
16235 expression = NULL_TREE;
16237 expression = cp_parser_assignment_expression (parser,
16240 return build_throw (expression);
16243 /* GNU Extensions */
16245 /* Parse an (optional) asm-specification.
16248 asm ( string-literal )
16250 If the asm-specification is present, returns a STRING_CST
16251 corresponding to the string-literal. Otherwise, returns
16255 cp_parser_asm_specification_opt (cp_parser* parser)
16258 tree asm_specification;
16260 /* Peek at the next token. */
16261 token = cp_lexer_peek_token (parser->lexer);
16262 /* If the next token isn't the `asm' keyword, then there's no
16263 asm-specification. */
16264 if (!cp_parser_is_keyword (token, RID_ASM))
16267 /* Consume the `asm' token. */
16268 cp_lexer_consume_token (parser->lexer);
16269 /* Look for the `('. */
16270 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16272 /* Look for the string-literal. */
16273 asm_specification = cp_parser_string_literal (parser, false, false);
16275 /* Look for the `)'. */
16276 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16278 return asm_specification;
16281 /* Parse an asm-operand-list.
16285 asm-operand-list , asm-operand
16288 string-literal ( expression )
16289 [ string-literal ] string-literal ( expression )
16291 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16292 each node is the expression. The TREE_PURPOSE is itself a
16293 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16294 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16295 is a STRING_CST for the string literal before the parenthesis. Returns
16296 ERROR_MARK_NODE if any of the operands are invalid. */
16299 cp_parser_asm_operand_list (cp_parser* parser)
16301 tree asm_operands = NULL_TREE;
16302 bool invalid_operands = false;
16306 tree string_literal;
16310 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16312 /* Consume the `[' token. */
16313 cp_lexer_consume_token (parser->lexer);
16314 /* Read the operand name. */
16315 name = cp_parser_identifier (parser);
16316 if (name != error_mark_node)
16317 name = build_string (IDENTIFIER_LENGTH (name),
16318 IDENTIFIER_POINTER (name));
16319 /* Look for the closing `]'. */
16320 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16324 /* Look for the string-literal. */
16325 string_literal = cp_parser_string_literal (parser, false, false);
16327 /* Look for the `('. */
16328 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16329 /* Parse the expression. */
16330 expression = cp_parser_expression (parser, /*cast_p=*/false);
16331 /* Look for the `)'. */
16332 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16334 if (name == error_mark_node
16335 || string_literal == error_mark_node
16336 || expression == error_mark_node)
16337 invalid_operands = true;
16339 /* Add this operand to the list. */
16340 asm_operands = tree_cons (build_tree_list (name, string_literal),
16343 /* If the next token is not a `,', there are no more
16345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16347 /* Consume the `,'. */
16348 cp_lexer_consume_token (parser->lexer);
16351 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16354 /* Parse an asm-clobber-list.
16358 asm-clobber-list , string-literal
16360 Returns a TREE_LIST, indicating the clobbers in the order that they
16361 appeared. The TREE_VALUE of each node is a STRING_CST. */
16364 cp_parser_asm_clobber_list (cp_parser* parser)
16366 tree clobbers = NULL_TREE;
16370 tree string_literal;
16372 /* Look for the string literal. */
16373 string_literal = cp_parser_string_literal (parser, false, false);
16374 /* Add it to the list. */
16375 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16376 /* If the next token is not a `,', then the list is
16378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16380 /* Consume the `,' token. */
16381 cp_lexer_consume_token (parser->lexer);
16387 /* Parse an (optional) series of attributes.
16390 attributes attribute
16393 __attribute__ (( attribute-list [opt] ))
16395 The return value is as for cp_parser_attribute_list. */
16398 cp_parser_attributes_opt (cp_parser* parser)
16400 tree attributes = NULL_TREE;
16405 tree attribute_list;
16407 /* Peek at the next token. */
16408 token = cp_lexer_peek_token (parser->lexer);
16409 /* If it's not `__attribute__', then we're done. */
16410 if (token->keyword != RID_ATTRIBUTE)
16413 /* Consume the `__attribute__' keyword. */
16414 cp_lexer_consume_token (parser->lexer);
16415 /* Look for the two `(' tokens. */
16416 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16417 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16419 /* Peek at the next token. */
16420 token = cp_lexer_peek_token (parser->lexer);
16421 if (token->type != CPP_CLOSE_PAREN)
16422 /* Parse the attribute-list. */
16423 attribute_list = cp_parser_attribute_list (parser);
16425 /* If the next token is a `)', then there is no attribute
16427 attribute_list = NULL;
16429 /* Look for the two `)' tokens. */
16430 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16431 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16433 /* Add these new attributes to the list. */
16434 attributes = chainon (attributes, attribute_list);
16440 /* Parse an attribute-list.
16444 attribute-list , attribute
16448 identifier ( identifier )
16449 identifier ( identifier , expression-list )
16450 identifier ( expression-list )
16452 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16453 to an attribute. The TREE_PURPOSE of each node is the identifier
16454 indicating which attribute is in use. The TREE_VALUE represents
16455 the arguments, if any. */
16458 cp_parser_attribute_list (cp_parser* parser)
16460 tree attribute_list = NULL_TREE;
16461 bool save_translate_strings_p = parser->translate_strings_p;
16463 parser->translate_strings_p = false;
16470 /* Look for the identifier. We also allow keywords here; for
16471 example `__attribute__ ((const))' is legal. */
16472 token = cp_lexer_peek_token (parser->lexer);
16473 if (token->type == CPP_NAME
16474 || token->type == CPP_KEYWORD)
16476 tree arguments = NULL_TREE;
16478 /* Consume the token. */
16479 token = cp_lexer_consume_token (parser->lexer);
16481 /* Save away the identifier that indicates which attribute
16483 identifier = token->u.value;
16484 attribute = build_tree_list (identifier, NULL_TREE);
16486 /* Peek at the next token. */
16487 token = cp_lexer_peek_token (parser->lexer);
16488 /* If it's an `(', then parse the attribute arguments. */
16489 if (token->type == CPP_OPEN_PAREN)
16491 arguments = cp_parser_parenthesized_expression_list
16492 (parser, true, /*cast_p=*/false,
16493 /*allow_expansion_p=*/false,
16494 /*non_constant_p=*/NULL);
16495 /* Save the arguments away. */
16496 TREE_VALUE (attribute) = arguments;
16499 if (arguments != error_mark_node)
16501 /* Add this attribute to the list. */
16502 TREE_CHAIN (attribute) = attribute_list;
16503 attribute_list = attribute;
16506 token = cp_lexer_peek_token (parser->lexer);
16508 /* Now, look for more attributes. If the next token isn't a
16509 `,', we're done. */
16510 if (token->type != CPP_COMMA)
16513 /* Consume the comma and keep going. */
16514 cp_lexer_consume_token (parser->lexer);
16516 parser->translate_strings_p = save_translate_strings_p;
16518 /* We built up the list in reverse order. */
16519 return nreverse (attribute_list);
16522 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16523 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16524 current value of the PEDANTIC flag, regardless of whether or not
16525 the `__extension__' keyword is present. The caller is responsible
16526 for restoring the value of the PEDANTIC flag. */
16529 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16531 /* Save the old value of the PEDANTIC flag. */
16532 *saved_pedantic = pedantic;
16534 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16536 /* Consume the `__extension__' token. */
16537 cp_lexer_consume_token (parser->lexer);
16538 /* We're not being pedantic while the `__extension__' keyword is
16548 /* Parse a label declaration.
16551 __label__ label-declarator-seq ;
16553 label-declarator-seq:
16554 identifier , label-declarator-seq
16558 cp_parser_label_declaration (cp_parser* parser)
16560 /* Look for the `__label__' keyword. */
16561 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16567 /* Look for an identifier. */
16568 identifier = cp_parser_identifier (parser);
16569 /* If we failed, stop. */
16570 if (identifier == error_mark_node)
16572 /* Declare it as a label. */
16573 finish_label_decl (identifier);
16574 /* If the next token is a `;', stop. */
16575 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16577 /* Look for the `,' separating the label declarations. */
16578 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16581 /* Look for the final `;'. */
16582 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16585 /* Support Functions */
16587 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16588 NAME should have one of the representations used for an
16589 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16590 is returned. If PARSER->SCOPE is a dependent type, then a
16591 SCOPE_REF is returned.
16593 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16594 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16595 was formed. Abstractly, such entities should not be passed to this
16596 function, because they do not need to be looked up, but it is
16597 simpler to check for this special case here, rather than at the
16600 In cases not explicitly covered above, this function returns a
16601 DECL, OVERLOAD, or baselink representing the result of the lookup.
16602 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16605 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16606 (e.g., "struct") that was used. In that case bindings that do not
16607 refer to types are ignored.
16609 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16612 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16615 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16618 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16619 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16620 NULL_TREE otherwise. */
16623 cp_parser_lookup_name (cp_parser *parser, tree name,
16624 enum tag_types tag_type,
16627 bool check_dependency,
16628 tree *ambiguous_decls,
16629 location_t name_location)
16633 tree object_type = parser->context->object_type;
16635 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16636 flags |= LOOKUP_COMPLAIN;
16638 /* Assume that the lookup will be unambiguous. */
16639 if (ambiguous_decls)
16640 *ambiguous_decls = NULL_TREE;
16642 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16643 no longer valid. Note that if we are parsing tentatively, and
16644 the parse fails, OBJECT_TYPE will be automatically restored. */
16645 parser->context->object_type = NULL_TREE;
16647 if (name == error_mark_node)
16648 return error_mark_node;
16650 /* A template-id has already been resolved; there is no lookup to
16652 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16654 if (BASELINK_P (name))
16656 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16657 == TEMPLATE_ID_EXPR);
16661 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16662 it should already have been checked to make sure that the name
16663 used matches the type being destroyed. */
16664 if (TREE_CODE (name) == BIT_NOT_EXPR)
16668 /* Figure out to which type this destructor applies. */
16670 type = parser->scope;
16671 else if (object_type)
16672 type = object_type;
16674 type = current_class_type;
16675 /* If that's not a class type, there is no destructor. */
16676 if (!type || !CLASS_TYPE_P (type))
16677 return error_mark_node;
16678 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16679 lazily_declare_fn (sfk_destructor, type);
16680 if (!CLASSTYPE_DESTRUCTORS (type))
16681 return error_mark_node;
16682 /* If it was a class type, return the destructor. */
16683 return CLASSTYPE_DESTRUCTORS (type);
16686 /* By this point, the NAME should be an ordinary identifier. If
16687 the id-expression was a qualified name, the qualifying scope is
16688 stored in PARSER->SCOPE at this point. */
16689 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16691 /* Perform the lookup. */
16696 if (parser->scope == error_mark_node)
16697 return error_mark_node;
16699 /* If the SCOPE is dependent, the lookup must be deferred until
16700 the template is instantiated -- unless we are explicitly
16701 looking up names in uninstantiated templates. Even then, we
16702 cannot look up the name if the scope is not a class type; it
16703 might, for example, be a template type parameter. */
16704 dependent_p = (TYPE_P (parser->scope)
16705 && !(parser->in_declarator_p
16706 && currently_open_class (parser->scope))
16707 && dependent_type_p (parser->scope));
16708 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16715 /* The resolution to Core Issue 180 says that `struct
16716 A::B' should be considered a type-name, even if `A'
16718 type = make_typename_type (parser->scope, name, tag_type,
16719 /*complain=*/tf_error);
16720 decl = TYPE_NAME (type);
16722 else if (is_template
16723 && (cp_parser_next_token_ends_template_argument_p (parser)
16724 || cp_lexer_next_token_is (parser->lexer,
16726 decl = make_unbound_class_template (parser->scope,
16728 /*complain=*/tf_error);
16730 decl = build_qualified_name (/*type=*/NULL_TREE,
16731 parser->scope, name,
16736 tree pushed_scope = NULL_TREE;
16738 /* If PARSER->SCOPE is a dependent type, then it must be a
16739 class type, and we must not be checking dependencies;
16740 otherwise, we would have processed this lookup above. So
16741 that PARSER->SCOPE is not considered a dependent base by
16742 lookup_member, we must enter the scope here. */
16744 pushed_scope = push_scope (parser->scope);
16745 /* If the PARSER->SCOPE is a template specialization, it
16746 may be instantiated during name lookup. In that case,
16747 errors may be issued. Even if we rollback the current
16748 tentative parse, those errors are valid. */
16749 decl = lookup_qualified_name (parser->scope, name,
16750 tag_type != none_type,
16751 /*complain=*/true);
16753 /* If we have a single function from a using decl, pull it out. */
16755 && TREE_CODE (decl) == OVERLOAD
16756 && !really_overloaded_fn (decl))
16757 decl = OVL_FUNCTION (decl);
16760 pop_scope (pushed_scope);
16762 parser->qualifying_scope = parser->scope;
16763 parser->object_scope = NULL_TREE;
16765 else if (object_type)
16767 tree object_decl = NULL_TREE;
16768 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16769 OBJECT_TYPE is not a class. */
16770 if (CLASS_TYPE_P (object_type))
16771 /* If the OBJECT_TYPE is a template specialization, it may
16772 be instantiated during name lookup. In that case, errors
16773 may be issued. Even if we rollback the current tentative
16774 parse, those errors are valid. */
16775 object_decl = lookup_member (object_type,
16778 tag_type != none_type);
16779 /* Look it up in the enclosing context, too. */
16780 decl = lookup_name_real (name, tag_type != none_type,
16782 /*block_p=*/true, is_namespace, flags);
16783 parser->object_scope = object_type;
16784 parser->qualifying_scope = NULL_TREE;
16786 decl = object_decl;
16790 decl = lookup_name_real (name, tag_type != none_type,
16792 /*block_p=*/true, is_namespace, flags);
16793 parser->qualifying_scope = NULL_TREE;
16794 parser->object_scope = NULL_TREE;
16797 /* If the lookup failed, let our caller know. */
16798 if (!decl || decl == error_mark_node)
16799 return error_mark_node;
16801 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16802 if (TREE_CODE (decl) == TREE_LIST)
16804 if (ambiguous_decls)
16805 *ambiguous_decls = decl;
16806 /* The error message we have to print is too complicated for
16807 cp_parser_error, so we incorporate its actions directly. */
16808 if (!cp_parser_simulate_error (parser))
16810 error ("%Hreference to %qD is ambiguous",
16811 &name_location, name);
16812 print_candidates (decl);
16814 return error_mark_node;
16817 gcc_assert (DECL_P (decl)
16818 || TREE_CODE (decl) == OVERLOAD
16819 || TREE_CODE (decl) == SCOPE_REF
16820 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16821 || BASELINK_P (decl));
16823 /* If we have resolved the name of a member declaration, check to
16824 see if the declaration is accessible. When the name resolves to
16825 set of overloaded functions, accessibility is checked when
16826 overload resolution is done.
16828 During an explicit instantiation, access is not checked at all,
16829 as per [temp.explicit]. */
16831 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16836 /* Like cp_parser_lookup_name, but for use in the typical case where
16837 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16838 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16841 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
16843 return cp_parser_lookup_name (parser, name,
16845 /*is_template=*/false,
16846 /*is_namespace=*/false,
16847 /*check_dependency=*/true,
16848 /*ambiguous_decls=*/NULL,
16852 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16853 the current context, return the TYPE_DECL. If TAG_NAME_P is
16854 true, the DECL indicates the class being defined in a class-head,
16855 or declared in an elaborated-type-specifier.
16857 Otherwise, return DECL. */
16860 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16862 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16863 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16866 template <typename T> struct B;
16869 template <typename T> struct A::B {};
16871 Similarly, in an elaborated-type-specifier:
16873 namespace N { struct X{}; }
16876 template <typename T> friend struct N::X;
16879 However, if the DECL refers to a class type, and we are in
16880 the scope of the class, then the name lookup automatically
16881 finds the TYPE_DECL created by build_self_reference rather
16882 than a TEMPLATE_DECL. For example, in:
16884 template <class T> struct S {
16888 there is no need to handle such case. */
16890 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16891 return DECL_TEMPLATE_RESULT (decl);
16896 /* If too many, or too few, template-parameter lists apply to the
16897 declarator, issue an error message. Returns TRUE if all went well,
16898 and FALSE otherwise. */
16901 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16902 cp_declarator *declarator,
16903 location_t declarator_location)
16905 unsigned num_templates;
16907 /* We haven't seen any classes that involve template parameters yet. */
16910 switch (declarator->kind)
16913 if (declarator->u.id.qualifying_scope)
16918 scope = declarator->u.id.qualifying_scope;
16919 member = declarator->u.id.unqualified_name;
16921 while (scope && CLASS_TYPE_P (scope))
16923 /* You're supposed to have one `template <...>'
16924 for every template class, but you don't need one
16925 for a full specialization. For example:
16927 template <class T> struct S{};
16928 template <> struct S<int> { void f(); };
16929 void S<int>::f () {}
16931 is correct; there shouldn't be a `template <>' for
16932 the definition of `S<int>::f'. */
16933 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16934 /* If SCOPE does not have template information of any
16935 kind, then it is not a template, nor is it nested
16936 within a template. */
16938 if (explicit_class_specialization_p (scope))
16940 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16943 scope = TYPE_CONTEXT (scope);
16946 else if (TREE_CODE (declarator->u.id.unqualified_name)
16947 == TEMPLATE_ID_EXPR)
16948 /* If the DECLARATOR has the form `X<y>' then it uses one
16949 additional level of template parameters. */
16952 return cp_parser_check_template_parameters (parser,
16954 declarator_location);
16959 case cdk_reference:
16961 return (cp_parser_check_declarator_template_parameters
16962 (parser, declarator->declarator, declarator_location));
16968 gcc_unreachable ();
16973 /* NUM_TEMPLATES were used in the current declaration. If that is
16974 invalid, return FALSE and issue an error messages. Otherwise,
16978 cp_parser_check_template_parameters (cp_parser* parser,
16979 unsigned num_templates,
16980 location_t location)
16982 /* If there are more template classes than parameter lists, we have
16985 template <class T> void S<T>::R<T>::f (); */
16986 if (parser->num_template_parameter_lists < num_templates)
16988 error ("%Htoo few template-parameter-lists", &location);
16991 /* If there are the same number of template classes and parameter
16992 lists, that's OK. */
16993 if (parser->num_template_parameter_lists == num_templates)
16995 /* If there are more, but only one more, then we are referring to a
16996 member template. That's OK too. */
16997 if (parser->num_template_parameter_lists == num_templates + 1)
16999 /* Otherwise, there are too many template parameter lists. We have
17002 template <class T> template <class U> void S::f(); */
17003 error ("%Htoo many template-parameter-lists", &location);
17007 /* Parse an optional `::' token indicating that the following name is
17008 from the global namespace. If so, PARSER->SCOPE is set to the
17009 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17010 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17011 Returns the new value of PARSER->SCOPE, if the `::' token is
17012 present, and NULL_TREE otherwise. */
17015 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17019 /* Peek at the next token. */
17020 token = cp_lexer_peek_token (parser->lexer);
17021 /* If we're looking at a `::' token then we're starting from the
17022 global namespace, not our current location. */
17023 if (token->type == CPP_SCOPE)
17025 /* Consume the `::' token. */
17026 cp_lexer_consume_token (parser->lexer);
17027 /* Set the SCOPE so that we know where to start the lookup. */
17028 parser->scope = global_namespace;
17029 parser->qualifying_scope = global_namespace;
17030 parser->object_scope = NULL_TREE;
17032 return parser->scope;
17034 else if (!current_scope_valid_p)
17036 parser->scope = NULL_TREE;
17037 parser->qualifying_scope = NULL_TREE;
17038 parser->object_scope = NULL_TREE;
17044 /* Returns TRUE if the upcoming token sequence is the start of a
17045 constructor declarator. If FRIEND_P is true, the declarator is
17046 preceded by the `friend' specifier. */
17049 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17051 bool constructor_p;
17052 tree type_decl = NULL_TREE;
17053 bool nested_name_p;
17054 cp_token *next_token;
17056 /* The common case is that this is not a constructor declarator, so
17057 try to avoid doing lots of work if at all possible. It's not
17058 valid declare a constructor at function scope. */
17059 if (parser->in_function_body)
17061 /* And only certain tokens can begin a constructor declarator. */
17062 next_token = cp_lexer_peek_token (parser->lexer);
17063 if (next_token->type != CPP_NAME
17064 && next_token->type != CPP_SCOPE
17065 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17066 && next_token->type != CPP_TEMPLATE_ID)
17069 /* Parse tentatively; we are going to roll back all of the tokens
17071 cp_parser_parse_tentatively (parser);
17072 /* Assume that we are looking at a constructor declarator. */
17073 constructor_p = true;
17075 /* Look for the optional `::' operator. */
17076 cp_parser_global_scope_opt (parser,
17077 /*current_scope_valid_p=*/false);
17078 /* Look for the nested-name-specifier. */
17080 = (cp_parser_nested_name_specifier_opt (parser,
17081 /*typename_keyword_p=*/false,
17082 /*check_dependency_p=*/false,
17084 /*is_declaration=*/false)
17086 /* Outside of a class-specifier, there must be a
17087 nested-name-specifier. */
17088 if (!nested_name_p &&
17089 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17091 constructor_p = false;
17092 /* If we still think that this might be a constructor-declarator,
17093 look for a class-name. */
17098 template <typename T> struct S { S(); };
17099 template <typename T> S<T>::S ();
17101 we must recognize that the nested `S' names a class.
17104 template <typename T> S<T>::S<T> ();
17106 we must recognize that the nested `S' names a template. */
17107 type_decl = cp_parser_class_name (parser,
17108 /*typename_keyword_p=*/false,
17109 /*template_keyword_p=*/false,
17111 /*check_dependency_p=*/false,
17112 /*class_head_p=*/false,
17113 /*is_declaration=*/false);
17114 /* If there was no class-name, then this is not a constructor. */
17115 constructor_p = !cp_parser_error_occurred (parser);
17118 /* If we're still considering a constructor, we have to see a `(',
17119 to begin the parameter-declaration-clause, followed by either a
17120 `)', an `...', or a decl-specifier. We need to check for a
17121 type-specifier to avoid being fooled into thinking that:
17125 is a constructor. (It is actually a function named `f' that
17126 takes one parameter (of type `int') and returns a value of type
17129 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17132 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17133 /* A parameter declaration begins with a decl-specifier,
17134 which is either the "attribute" keyword, a storage class
17135 specifier, or (usually) a type-specifier. */
17136 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17139 tree pushed_scope = NULL_TREE;
17140 unsigned saved_num_template_parameter_lists;
17142 /* Names appearing in the type-specifier should be looked up
17143 in the scope of the class. */
17144 if (current_class_type)
17148 type = TREE_TYPE (type_decl);
17149 if (TREE_CODE (type) == TYPENAME_TYPE)
17151 type = resolve_typename_type (type,
17152 /*only_current_p=*/false);
17153 if (TREE_CODE (type) == TYPENAME_TYPE)
17155 cp_parser_abort_tentative_parse (parser);
17159 pushed_scope = push_scope (type);
17162 /* Inside the constructor parameter list, surrounding
17163 template-parameter-lists do not apply. */
17164 saved_num_template_parameter_lists
17165 = parser->num_template_parameter_lists;
17166 parser->num_template_parameter_lists = 0;
17168 /* Look for the type-specifier. */
17169 cp_parser_type_specifier (parser,
17170 CP_PARSER_FLAGS_NONE,
17171 /*decl_specs=*/NULL,
17172 /*is_declarator=*/true,
17173 /*declares_class_or_enum=*/NULL,
17174 /*is_cv_qualifier=*/NULL);
17176 parser->num_template_parameter_lists
17177 = saved_num_template_parameter_lists;
17179 /* Leave the scope of the class. */
17181 pop_scope (pushed_scope);
17183 constructor_p = !cp_parser_error_occurred (parser);
17187 constructor_p = false;
17188 /* We did not really want to consume any tokens. */
17189 cp_parser_abort_tentative_parse (parser);
17191 return constructor_p;
17194 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17195 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17196 they must be performed once we are in the scope of the function.
17198 Returns the function defined. */
17201 cp_parser_function_definition_from_specifiers_and_declarator
17202 (cp_parser* parser,
17203 cp_decl_specifier_seq *decl_specifiers,
17205 const cp_declarator *declarator)
17210 /* Begin the function-definition. */
17211 success_p = start_function (decl_specifiers, declarator, attributes);
17213 /* The things we're about to see are not directly qualified by any
17214 template headers we've seen thus far. */
17215 reset_specialization ();
17217 /* If there were names looked up in the decl-specifier-seq that we
17218 did not check, check them now. We must wait until we are in the
17219 scope of the function to perform the checks, since the function
17220 might be a friend. */
17221 perform_deferred_access_checks ();
17225 /* Skip the entire function. */
17226 cp_parser_skip_to_end_of_block_or_statement (parser);
17227 fn = error_mark_node;
17229 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17231 /* Seen already, skip it. An error message has already been output. */
17232 cp_parser_skip_to_end_of_block_or_statement (parser);
17233 fn = current_function_decl;
17234 current_function_decl = NULL_TREE;
17235 /* If this is a function from a class, pop the nested class. */
17236 if (current_class_name)
17237 pop_nested_class ();
17240 fn = cp_parser_function_definition_after_declarator (parser,
17241 /*inline_p=*/false);
17246 /* Parse the part of a function-definition that follows the
17247 declarator. INLINE_P is TRUE iff this function is an inline
17248 function defined with a class-specifier.
17250 Returns the function defined. */
17253 cp_parser_function_definition_after_declarator (cp_parser* parser,
17257 bool ctor_initializer_p = false;
17258 bool saved_in_unbraced_linkage_specification_p;
17259 bool saved_in_function_body;
17260 unsigned saved_num_template_parameter_lists;
17263 saved_in_function_body = parser->in_function_body;
17264 parser->in_function_body = true;
17265 /* If the next token is `return', then the code may be trying to
17266 make use of the "named return value" extension that G++ used to
17268 token = cp_lexer_peek_token (parser->lexer);
17269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17271 /* Consume the `return' keyword. */
17272 cp_lexer_consume_token (parser->lexer);
17273 /* Look for the identifier that indicates what value is to be
17275 cp_parser_identifier (parser);
17276 /* Issue an error message. */
17277 error ("%Hnamed return values are no longer supported",
17279 /* Skip tokens until we reach the start of the function body. */
17282 cp_token *token = cp_lexer_peek_token (parser->lexer);
17283 if (token->type == CPP_OPEN_BRACE
17284 || token->type == CPP_EOF
17285 || token->type == CPP_PRAGMA_EOL)
17287 cp_lexer_consume_token (parser->lexer);
17290 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17291 anything declared inside `f'. */
17292 saved_in_unbraced_linkage_specification_p
17293 = parser->in_unbraced_linkage_specification_p;
17294 parser->in_unbraced_linkage_specification_p = false;
17295 /* Inside the function, surrounding template-parameter-lists do not
17297 saved_num_template_parameter_lists
17298 = parser->num_template_parameter_lists;
17299 parser->num_template_parameter_lists = 0;
17300 /* If the next token is `try', then we are looking at a
17301 function-try-block. */
17302 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17303 ctor_initializer_p = cp_parser_function_try_block (parser);
17304 /* A function-try-block includes the function-body, so we only do
17305 this next part if we're not processing a function-try-block. */
17308 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17310 /* Finish the function. */
17311 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17312 (inline_p ? 2 : 0));
17313 /* Generate code for it, if necessary. */
17314 expand_or_defer_fn (fn);
17315 /* Restore the saved values. */
17316 parser->in_unbraced_linkage_specification_p
17317 = saved_in_unbraced_linkage_specification_p;
17318 parser->num_template_parameter_lists
17319 = saved_num_template_parameter_lists;
17320 parser->in_function_body = saved_in_function_body;
17325 /* Parse a template-declaration, assuming that the `export' (and
17326 `extern') keywords, if present, has already been scanned. MEMBER_P
17327 is as for cp_parser_template_declaration. */
17330 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17332 tree decl = NULL_TREE;
17333 VEC (deferred_access_check,gc) *checks;
17334 tree parameter_list;
17335 bool friend_p = false;
17336 bool need_lang_pop;
17339 /* Look for the `template' keyword. */
17340 token = cp_lexer_peek_token (parser->lexer);
17341 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17345 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17347 if (at_class_scope_p () && current_function_decl)
17349 /* 14.5.2.2 [temp.mem]
17351 A local class shall not have member templates. */
17352 error ("%Hinvalid declaration of member template in local class",
17354 cp_parser_skip_to_end_of_block_or_statement (parser);
17359 A template ... shall not have C linkage. */
17360 if (current_lang_name == lang_name_c)
17362 error ("%Htemplate with C linkage", &token->location);
17363 /* Give it C++ linkage to avoid confusing other parts of the
17365 push_lang_context (lang_name_cplusplus);
17366 need_lang_pop = true;
17369 need_lang_pop = false;
17371 /* We cannot perform access checks on the template parameter
17372 declarations until we know what is being declared, just as we
17373 cannot check the decl-specifier list. */
17374 push_deferring_access_checks (dk_deferred);
17376 /* If the next token is `>', then we have an invalid
17377 specialization. Rather than complain about an invalid template
17378 parameter, issue an error message here. */
17379 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17381 cp_parser_error (parser, "invalid explicit specialization");
17382 begin_specialization ();
17383 parameter_list = NULL_TREE;
17386 /* Parse the template parameters. */
17387 parameter_list = cp_parser_template_parameter_list (parser);
17389 /* Get the deferred access checks from the parameter list. These
17390 will be checked once we know what is being declared, as for a
17391 member template the checks must be performed in the scope of the
17392 class containing the member. */
17393 checks = get_deferred_access_checks ();
17395 /* Look for the `>'. */
17396 cp_parser_skip_to_end_of_template_parameter_list (parser);
17397 /* We just processed one more parameter list. */
17398 ++parser->num_template_parameter_lists;
17399 /* If the next token is `template', there are more template
17401 if (cp_lexer_next_token_is_keyword (parser->lexer,
17403 cp_parser_template_declaration_after_export (parser, member_p);
17406 /* There are no access checks when parsing a template, as we do not
17407 know if a specialization will be a friend. */
17408 push_deferring_access_checks (dk_no_check);
17409 token = cp_lexer_peek_token (parser->lexer);
17410 decl = cp_parser_single_declaration (parser,
17413 /*explicit_specialization_p=*/false,
17415 pop_deferring_access_checks ();
17417 /* If this is a member template declaration, let the front
17419 if (member_p && !friend_p && decl)
17421 if (TREE_CODE (decl) == TYPE_DECL)
17422 cp_parser_check_access_in_redeclaration (decl, token->location);
17424 decl = finish_member_template_decl (decl);
17426 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17427 make_friend_class (current_class_type, TREE_TYPE (decl),
17428 /*complain=*/true);
17430 /* We are done with the current parameter list. */
17431 --parser->num_template_parameter_lists;
17433 pop_deferring_access_checks ();
17436 finish_template_decl (parameter_list);
17438 /* Register member declarations. */
17439 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17440 finish_member_declaration (decl);
17441 /* For the erroneous case of a template with C linkage, we pushed an
17442 implicit C++ linkage scope; exit that scope now. */
17444 pop_lang_context ();
17445 /* If DECL is a function template, we must return to parse it later.
17446 (Even though there is no definition, there might be default
17447 arguments that need handling.) */
17448 if (member_p && decl
17449 && (TREE_CODE (decl) == FUNCTION_DECL
17450 || DECL_FUNCTION_TEMPLATE_P (decl)))
17451 TREE_VALUE (parser->unparsed_functions_queues)
17452 = tree_cons (NULL_TREE, decl,
17453 TREE_VALUE (parser->unparsed_functions_queues));
17456 /* Perform the deferred access checks from a template-parameter-list.
17457 CHECKS is a TREE_LIST of access checks, as returned by
17458 get_deferred_access_checks. */
17461 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17463 ++processing_template_parmlist;
17464 perform_access_checks (checks);
17465 --processing_template_parmlist;
17468 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17469 `function-definition' sequence. MEMBER_P is true, this declaration
17470 appears in a class scope.
17472 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17473 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17476 cp_parser_single_declaration (cp_parser* parser,
17477 VEC (deferred_access_check,gc)* checks,
17479 bool explicit_specialization_p,
17482 int declares_class_or_enum;
17483 tree decl = NULL_TREE;
17484 cp_decl_specifier_seq decl_specifiers;
17485 bool function_definition_p = false;
17486 cp_token *decl_spec_token_start;
17488 /* This function is only used when processing a template
17490 gcc_assert (innermost_scope_kind () == sk_template_parms
17491 || innermost_scope_kind () == sk_template_spec);
17493 /* Defer access checks until we know what is being declared. */
17494 push_deferring_access_checks (dk_deferred);
17496 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17498 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17499 cp_parser_decl_specifier_seq (parser,
17500 CP_PARSER_FLAGS_OPTIONAL,
17502 &declares_class_or_enum);
17504 *friend_p = cp_parser_friend_p (&decl_specifiers);
17506 /* There are no template typedefs. */
17507 if (decl_specifiers.specs[(int) ds_typedef])
17509 error ("%Htemplate declaration of %qs",
17510 &decl_spec_token_start->location, "typedef");
17511 decl = error_mark_node;
17514 /* Gather up the access checks that occurred the
17515 decl-specifier-seq. */
17516 stop_deferring_access_checks ();
17518 /* Check for the declaration of a template class. */
17519 if (declares_class_or_enum)
17521 if (cp_parser_declares_only_class_p (parser))
17523 decl = shadow_tag (&decl_specifiers);
17528 friend template <typename T> struct A<T>::B;
17531 A<T>::B will be represented by a TYPENAME_TYPE, and
17532 therefore not recognized by shadow_tag. */
17533 if (friend_p && *friend_p
17535 && decl_specifiers.type
17536 && TYPE_P (decl_specifiers.type))
17537 decl = decl_specifiers.type;
17539 if (decl && decl != error_mark_node)
17540 decl = TYPE_NAME (decl);
17542 decl = error_mark_node;
17544 /* Perform access checks for template parameters. */
17545 cp_parser_perform_template_parameter_access_checks (checks);
17548 /* If it's not a template class, try for a template function. If
17549 the next token is a `;', then this declaration does not declare
17550 anything. But, if there were errors in the decl-specifiers, then
17551 the error might well have come from an attempted class-specifier.
17552 In that case, there's no need to warn about a missing declarator. */
17554 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17555 || decl_specifiers.type != error_mark_node))
17557 decl = cp_parser_init_declarator (parser,
17560 /*function_definition_allowed_p=*/true,
17562 declares_class_or_enum,
17563 &function_definition_p);
17565 /* 7.1.1-1 [dcl.stc]
17567 A storage-class-specifier shall not be specified in an explicit
17568 specialization... */
17570 && explicit_specialization_p
17571 && decl_specifiers.storage_class != sc_none)
17573 error ("%Hexplicit template specialization cannot have a storage class",
17574 &decl_spec_token_start->location);
17575 decl = error_mark_node;
17579 pop_deferring_access_checks ();
17581 /* Clear any current qualification; whatever comes next is the start
17582 of something new. */
17583 parser->scope = NULL_TREE;
17584 parser->qualifying_scope = NULL_TREE;
17585 parser->object_scope = NULL_TREE;
17586 /* Look for a trailing `;' after the declaration. */
17587 if (!function_definition_p
17588 && (decl == error_mark_node
17589 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17590 cp_parser_skip_to_end_of_block_or_statement (parser);
17595 /* Parse a cast-expression that is not the operand of a unary "&". */
17598 cp_parser_simple_cast_expression (cp_parser *parser)
17600 return cp_parser_cast_expression (parser, /*address_p=*/false,
17604 /* Parse a functional cast to TYPE. Returns an expression
17605 representing the cast. */
17608 cp_parser_functional_cast (cp_parser* parser, tree type)
17610 tree expression_list;
17614 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17616 maybe_warn_cpp0x ("extended initializer lists");
17617 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17618 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17619 if (TREE_CODE (type) == TYPE_DECL)
17620 type = TREE_TYPE (type);
17621 return finish_compound_literal (type, expression_list);
17625 = cp_parser_parenthesized_expression_list (parser, false,
17627 /*allow_expansion_p=*/true,
17628 /*non_constant_p=*/NULL);
17630 cast = build_functional_cast (type, expression_list,
17631 tf_warning_or_error);
17632 /* [expr.const]/1: In an integral constant expression "only type
17633 conversions to integral or enumeration type can be used". */
17634 if (TREE_CODE (type) == TYPE_DECL)
17635 type = TREE_TYPE (type);
17636 if (cast != error_mark_node
17637 && !cast_valid_in_integral_constant_expression_p (type)
17638 && (cp_parser_non_integral_constant_expression
17639 (parser, "a call to a constructor")))
17640 return error_mark_node;
17644 /* Save the tokens that make up the body of a member function defined
17645 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17646 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17647 specifiers applied to the declaration. Returns the FUNCTION_DECL
17648 for the member function. */
17651 cp_parser_save_member_function_body (cp_parser* parser,
17652 cp_decl_specifier_seq *decl_specifiers,
17653 cp_declarator *declarator,
17660 /* Create the function-declaration. */
17661 fn = start_method (decl_specifiers, declarator, attributes);
17662 /* If something went badly wrong, bail out now. */
17663 if (fn == error_mark_node)
17665 /* If there's a function-body, skip it. */
17666 if (cp_parser_token_starts_function_definition_p
17667 (cp_lexer_peek_token (parser->lexer)))
17668 cp_parser_skip_to_end_of_block_or_statement (parser);
17669 return error_mark_node;
17672 /* Remember it, if there default args to post process. */
17673 cp_parser_save_default_args (parser, fn);
17675 /* Save away the tokens that make up the body of the
17677 first = parser->lexer->next_token;
17678 /* We can have braced-init-list mem-initializers before the fn body. */
17679 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17681 cp_lexer_consume_token (parser->lexer);
17682 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17683 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17685 /* cache_group will stop after an un-nested { } pair, too. */
17686 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17689 /* variadic mem-inits have ... after the ')'. */
17690 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17691 cp_lexer_consume_token (parser->lexer);
17694 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17695 /* Handle function try blocks. */
17696 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17697 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17698 last = parser->lexer->next_token;
17700 /* Save away the inline definition; we will process it when the
17701 class is complete. */
17702 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17703 DECL_PENDING_INLINE_P (fn) = 1;
17705 /* We need to know that this was defined in the class, so that
17706 friend templates are handled correctly. */
17707 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17709 /* We're done with the inline definition. */
17710 finish_method (fn);
17712 /* Add FN to the queue of functions to be parsed later. */
17713 TREE_VALUE (parser->unparsed_functions_queues)
17714 = tree_cons (NULL_TREE, fn,
17715 TREE_VALUE (parser->unparsed_functions_queues));
17720 /* Parse a template-argument-list, as well as the trailing ">" (but
17721 not the opening ">"). See cp_parser_template_argument_list for the
17725 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17729 tree saved_qualifying_scope;
17730 tree saved_object_scope;
17731 bool saved_greater_than_is_operator_p;
17732 bool saved_skip_evaluation;
17736 When parsing a template-id, the first non-nested `>' is taken as
17737 the end of the template-argument-list rather than a greater-than
17739 saved_greater_than_is_operator_p
17740 = parser->greater_than_is_operator_p;
17741 parser->greater_than_is_operator_p = false;
17742 /* Parsing the argument list may modify SCOPE, so we save it
17744 saved_scope = parser->scope;
17745 saved_qualifying_scope = parser->qualifying_scope;
17746 saved_object_scope = parser->object_scope;
17747 /* We need to evaluate the template arguments, even though this
17748 template-id may be nested within a "sizeof". */
17749 saved_skip_evaluation = skip_evaluation;
17750 skip_evaluation = false;
17751 /* Parse the template-argument-list itself. */
17752 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17753 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17754 arguments = NULL_TREE;
17756 arguments = cp_parser_template_argument_list (parser);
17757 /* Look for the `>' that ends the template-argument-list. If we find
17758 a '>>' instead, it's probably just a typo. */
17759 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17761 if (cxx_dialect != cxx98)
17763 /* In C++0x, a `>>' in a template argument list or cast
17764 expression is considered to be two separate `>'
17765 tokens. So, change the current token to a `>', but don't
17766 consume it: it will be consumed later when the outer
17767 template argument list (or cast expression) is parsed.
17768 Note that this replacement of `>' for `>>' is necessary
17769 even if we are parsing tentatively: in the tentative
17770 case, after calling
17771 cp_parser_enclosed_template_argument_list we will always
17772 throw away all of the template arguments and the first
17773 closing `>', either because the template argument list
17774 was erroneous or because we are replacing those tokens
17775 with a CPP_TEMPLATE_ID token. The second `>' (which will
17776 not have been thrown away) is needed either to close an
17777 outer template argument list or to complete a new-style
17779 cp_token *token = cp_lexer_peek_token (parser->lexer);
17780 token->type = CPP_GREATER;
17782 else if (!saved_greater_than_is_operator_p)
17784 /* If we're in a nested template argument list, the '>>' has
17785 to be a typo for '> >'. We emit the error message, but we
17786 continue parsing and we push a '>' as next token, so that
17787 the argument list will be parsed correctly. Note that the
17788 global source location is still on the token before the
17789 '>>', so we need to say explicitly where we want it. */
17790 cp_token *token = cp_lexer_peek_token (parser->lexer);
17791 error ("%H%<>>%> should be %<> >%> "
17792 "within a nested template argument list",
17795 token->type = CPP_GREATER;
17799 /* If this is not a nested template argument list, the '>>'
17800 is a typo for '>'. Emit an error message and continue.
17801 Same deal about the token location, but here we can get it
17802 right by consuming the '>>' before issuing the diagnostic. */
17803 cp_token *token = cp_lexer_consume_token (parser->lexer);
17804 error ("%Hspurious %<>>%>, use %<>%> to terminate "
17805 "a template argument list", &token->location);
17809 cp_parser_skip_to_end_of_template_parameter_list (parser);
17810 /* The `>' token might be a greater-than operator again now. */
17811 parser->greater_than_is_operator_p
17812 = saved_greater_than_is_operator_p;
17813 /* Restore the SAVED_SCOPE. */
17814 parser->scope = saved_scope;
17815 parser->qualifying_scope = saved_qualifying_scope;
17816 parser->object_scope = saved_object_scope;
17817 skip_evaluation = saved_skip_evaluation;
17822 /* MEMBER_FUNCTION is a member function, or a friend. If default
17823 arguments, or the body of the function have not yet been parsed,
17827 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17829 /* If this member is a template, get the underlying
17831 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17832 member_function = DECL_TEMPLATE_RESULT (member_function);
17834 /* There should not be any class definitions in progress at this
17835 point; the bodies of members are only parsed outside of all class
17837 gcc_assert (parser->num_classes_being_defined == 0);
17838 /* While we're parsing the member functions we might encounter more
17839 classes. We want to handle them right away, but we don't want
17840 them getting mixed up with functions that are currently in the
17842 parser->unparsed_functions_queues
17843 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17845 /* Make sure that any template parameters are in scope. */
17846 maybe_begin_member_template_processing (member_function);
17848 /* If the body of the function has not yet been parsed, parse it
17850 if (DECL_PENDING_INLINE_P (member_function))
17852 tree function_scope;
17853 cp_token_cache *tokens;
17855 /* The function is no longer pending; we are processing it. */
17856 tokens = DECL_PENDING_INLINE_INFO (member_function);
17857 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17858 DECL_PENDING_INLINE_P (member_function) = 0;
17860 /* If this is a local class, enter the scope of the containing
17862 function_scope = current_function_decl;
17863 if (function_scope)
17864 push_function_context ();
17866 /* Push the body of the function onto the lexer stack. */
17867 cp_parser_push_lexer_for_tokens (parser, tokens);
17869 /* Let the front end know that we going to be defining this
17871 start_preparsed_function (member_function, NULL_TREE,
17872 SF_PRE_PARSED | SF_INCLASS_INLINE);
17874 /* Don't do access checking if it is a templated function. */
17875 if (processing_template_decl)
17876 push_deferring_access_checks (dk_no_check);
17878 /* Now, parse the body of the function. */
17879 cp_parser_function_definition_after_declarator (parser,
17880 /*inline_p=*/true);
17882 if (processing_template_decl)
17883 pop_deferring_access_checks ();
17885 /* Leave the scope of the containing function. */
17886 if (function_scope)
17887 pop_function_context ();
17888 cp_parser_pop_lexer (parser);
17891 /* Remove any template parameters from the symbol table. */
17892 maybe_end_member_template_processing ();
17894 /* Restore the queue. */
17895 parser->unparsed_functions_queues
17896 = TREE_CHAIN (parser->unparsed_functions_queues);
17899 /* If DECL contains any default args, remember it on the unparsed
17900 functions queue. */
17903 cp_parser_save_default_args (cp_parser* parser, tree decl)
17907 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17909 probe = TREE_CHAIN (probe))
17910 if (TREE_PURPOSE (probe))
17912 TREE_PURPOSE (parser->unparsed_functions_queues)
17913 = tree_cons (current_class_type, decl,
17914 TREE_PURPOSE (parser->unparsed_functions_queues));
17919 /* FN is a FUNCTION_DECL which may contains a parameter with an
17920 unparsed DEFAULT_ARG. Parse the default args now. This function
17921 assumes that the current scope is the scope in which the default
17922 argument should be processed. */
17925 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17927 bool saved_local_variables_forbidden_p;
17930 /* While we're parsing the default args, we might (due to the
17931 statement expression extension) encounter more classes. We want
17932 to handle them right away, but we don't want them getting mixed
17933 up with default args that are currently in the queue. */
17934 parser->unparsed_functions_queues
17935 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17937 /* Local variable names (and the `this' keyword) may not appear
17938 in a default argument. */
17939 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17940 parser->local_variables_forbidden_p = true;
17942 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17944 parm = TREE_CHAIN (parm))
17946 cp_token_cache *tokens;
17947 tree default_arg = TREE_PURPOSE (parm);
17949 VEC(tree,gc) *insts;
17956 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17957 /* This can happen for a friend declaration for a function
17958 already declared with default arguments. */
17961 /* Push the saved tokens for the default argument onto the parser's
17963 tokens = DEFARG_TOKENS (default_arg);
17964 cp_parser_push_lexer_for_tokens (parser, tokens);
17966 /* Parse the assignment-expression. */
17967 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17969 if (!processing_template_decl)
17970 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17972 TREE_PURPOSE (parm) = parsed_arg;
17974 /* Update any instantiations we've already created. */
17975 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17976 VEC_iterate (tree, insts, ix, copy); ix++)
17977 TREE_PURPOSE (copy) = parsed_arg;
17979 /* If the token stream has not been completely used up, then
17980 there was extra junk after the end of the default
17982 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17983 cp_parser_error (parser, "expected %<,%>");
17985 /* Revert to the main lexer. */
17986 cp_parser_pop_lexer (parser);
17989 /* Make sure no default arg is missing. */
17990 check_default_args (fn);
17992 /* Restore the state of local_variables_forbidden_p. */
17993 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17995 /* Restore the queue. */
17996 parser->unparsed_functions_queues
17997 = TREE_CHAIN (parser->unparsed_functions_queues);
18000 /* Parse the operand of `sizeof' (or a similar operator). Returns
18001 either a TYPE or an expression, depending on the form of the
18002 input. The KEYWORD indicates which kind of expression we have
18006 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18008 tree expr = NULL_TREE;
18009 const char *saved_message;
18011 bool saved_integral_constant_expression_p;
18012 bool saved_non_integral_constant_expression_p;
18013 bool pack_expansion_p = false;
18015 /* Types cannot be defined in a `sizeof' expression. Save away the
18017 saved_message = parser->type_definition_forbidden_message;
18018 /* And create the new one. */
18019 tmp = concat ("types may not be defined in %<",
18020 IDENTIFIER_POINTER (ridpointers[keyword]),
18021 "%> expressions", NULL);
18022 parser->type_definition_forbidden_message = tmp;
18024 /* The restrictions on constant-expressions do not apply inside
18025 sizeof expressions. */
18026 saved_integral_constant_expression_p
18027 = parser->integral_constant_expression_p;
18028 saved_non_integral_constant_expression_p
18029 = parser->non_integral_constant_expression_p;
18030 parser->integral_constant_expression_p = false;
18032 /* If it's a `...', then we are computing the length of a parameter
18034 if (keyword == RID_SIZEOF
18035 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18037 /* Consume the `...'. */
18038 cp_lexer_consume_token (parser->lexer);
18039 maybe_warn_variadic_templates ();
18041 /* Note that this is an expansion. */
18042 pack_expansion_p = true;
18045 /* Do not actually evaluate the expression. */
18047 /* If it's a `(', then we might be looking at the type-id
18049 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18052 bool saved_in_type_id_in_expr_p;
18054 /* We can't be sure yet whether we're looking at a type-id or an
18056 cp_parser_parse_tentatively (parser);
18057 /* Consume the `('. */
18058 cp_lexer_consume_token (parser->lexer);
18059 /* Parse the type-id. */
18060 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18061 parser->in_type_id_in_expr_p = true;
18062 type = cp_parser_type_id (parser);
18063 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18064 /* Now, look for the trailing `)'. */
18065 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18066 /* If all went well, then we're done. */
18067 if (cp_parser_parse_definitely (parser))
18069 cp_decl_specifier_seq decl_specs;
18071 /* Build a trivial decl-specifier-seq. */
18072 clear_decl_specs (&decl_specs);
18073 decl_specs.type = type;
18075 /* Call grokdeclarator to figure out what type this is. */
18076 expr = grokdeclarator (NULL,
18080 /*attrlist=*/NULL);
18084 /* If the type-id production did not work out, then we must be
18085 looking at the unary-expression production. */
18087 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18090 if (pack_expansion_p)
18091 /* Build a pack expansion. */
18092 expr = make_pack_expansion (expr);
18094 /* Go back to evaluating expressions. */
18097 /* Free the message we created. */
18099 /* And restore the old one. */
18100 parser->type_definition_forbidden_message = saved_message;
18101 parser->integral_constant_expression_p
18102 = saved_integral_constant_expression_p;
18103 parser->non_integral_constant_expression_p
18104 = saved_non_integral_constant_expression_p;
18109 /* If the current declaration has no declarator, return true. */
18112 cp_parser_declares_only_class_p (cp_parser *parser)
18114 /* If the next token is a `;' or a `,' then there is no
18116 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18117 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18120 /* Update the DECL_SPECS to reflect the storage class indicated by
18124 cp_parser_set_storage_class (cp_parser *parser,
18125 cp_decl_specifier_seq *decl_specs,
18127 location_t location)
18129 cp_storage_class storage_class;
18131 if (parser->in_unbraced_linkage_specification_p)
18133 error ("%Hinvalid use of %qD in linkage specification",
18134 &location, ridpointers[keyword]);
18137 else if (decl_specs->storage_class != sc_none)
18139 decl_specs->conflicting_specifiers_p = true;
18143 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18144 && decl_specs->specs[(int) ds_thread])
18146 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18147 decl_specs->specs[(int) ds_thread] = 0;
18153 storage_class = sc_auto;
18156 storage_class = sc_register;
18159 storage_class = sc_static;
18162 storage_class = sc_extern;
18165 storage_class = sc_mutable;
18168 gcc_unreachable ();
18170 decl_specs->storage_class = storage_class;
18172 /* A storage class specifier cannot be applied alongside a typedef
18173 specifier. If there is a typedef specifier present then set
18174 conflicting_specifiers_p which will trigger an error later
18175 on in grokdeclarator. */
18176 if (decl_specs->specs[(int)ds_typedef])
18177 decl_specs->conflicting_specifiers_p = true;
18180 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18181 is true, the type is a user-defined type; otherwise it is a
18182 built-in type specified by a keyword. */
18185 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18187 location_t location,
18188 bool user_defined_p)
18190 decl_specs->any_specifiers_p = true;
18192 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18193 (with, for example, in "typedef int wchar_t;") we remember that
18194 this is what happened. In system headers, we ignore these
18195 declarations so that G++ can work with system headers that are not
18197 if (decl_specs->specs[(int) ds_typedef]
18199 && (type_spec == boolean_type_node
18200 || type_spec == char16_type_node
18201 || type_spec == char32_type_node
18202 || type_spec == wchar_type_node)
18203 && (decl_specs->type
18204 || decl_specs->specs[(int) ds_long]
18205 || decl_specs->specs[(int) ds_short]
18206 || decl_specs->specs[(int) ds_unsigned]
18207 || decl_specs->specs[(int) ds_signed]))
18209 decl_specs->redefined_builtin_type = type_spec;
18210 if (!decl_specs->type)
18212 decl_specs->type = type_spec;
18213 decl_specs->user_defined_type_p = false;
18214 decl_specs->type_location = location;
18217 else if (decl_specs->type)
18218 decl_specs->multiple_types_p = true;
18221 decl_specs->type = type_spec;
18222 decl_specs->user_defined_type_p = user_defined_p;
18223 decl_specs->redefined_builtin_type = NULL_TREE;
18224 decl_specs->type_location = location;
18228 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18229 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18232 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18234 return decl_specifiers->specs[(int) ds_friend] != 0;
18237 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18238 issue an error message indicating that TOKEN_DESC was expected.
18240 Returns the token consumed, if the token had the appropriate type.
18241 Otherwise, returns NULL. */
18244 cp_parser_require (cp_parser* parser,
18245 enum cpp_ttype type,
18246 const char* token_desc)
18248 if (cp_lexer_next_token_is (parser->lexer, type))
18249 return cp_lexer_consume_token (parser->lexer);
18252 /* Output the MESSAGE -- unless we're parsing tentatively. */
18253 if (!cp_parser_simulate_error (parser))
18255 char *message = concat ("expected ", token_desc, NULL);
18256 cp_parser_error (parser, message);
18263 /* An error message is produced if the next token is not '>'.
18264 All further tokens are skipped until the desired token is
18265 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18268 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18270 /* Current level of '< ... >'. */
18271 unsigned level = 0;
18272 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18273 unsigned nesting_depth = 0;
18275 /* Are we ready, yet? If not, issue error message. */
18276 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18279 /* Skip tokens until the desired token is found. */
18282 /* Peek at the next token. */
18283 switch (cp_lexer_peek_token (parser->lexer)->type)
18286 if (!nesting_depth)
18291 if (cxx_dialect == cxx98)
18292 /* C++0x views the `>>' operator as two `>' tokens, but
18295 else if (!nesting_depth && level-- == 0)
18297 /* We've hit a `>>' where the first `>' closes the
18298 template argument list, and the second `>' is
18299 spurious. Just consume the `>>' and stop; we've
18300 already produced at least one error. */
18301 cp_lexer_consume_token (parser->lexer);
18304 /* Fall through for C++0x, so we handle the second `>' in
18308 if (!nesting_depth && level-- == 0)
18310 /* We've reached the token we want, consume it and stop. */
18311 cp_lexer_consume_token (parser->lexer);
18316 case CPP_OPEN_PAREN:
18317 case CPP_OPEN_SQUARE:
18321 case CPP_CLOSE_PAREN:
18322 case CPP_CLOSE_SQUARE:
18323 if (nesting_depth-- == 0)
18328 case CPP_PRAGMA_EOL:
18329 case CPP_SEMICOLON:
18330 case CPP_OPEN_BRACE:
18331 case CPP_CLOSE_BRACE:
18332 /* The '>' was probably forgotten, don't look further. */
18339 /* Consume this token. */
18340 cp_lexer_consume_token (parser->lexer);
18344 /* If the next token is the indicated keyword, consume it. Otherwise,
18345 issue an error message indicating that TOKEN_DESC was expected.
18347 Returns the token consumed, if the token had the appropriate type.
18348 Otherwise, returns NULL. */
18351 cp_parser_require_keyword (cp_parser* parser,
18353 const char* token_desc)
18355 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18357 if (token && token->keyword != keyword)
18359 dyn_string_t error_msg;
18361 /* Format the error message. */
18362 error_msg = dyn_string_new (0);
18363 dyn_string_append_cstr (error_msg, "expected ");
18364 dyn_string_append_cstr (error_msg, token_desc);
18365 cp_parser_error (parser, error_msg->s);
18366 dyn_string_delete (error_msg);
18373 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18374 function-definition. */
18377 cp_parser_token_starts_function_definition_p (cp_token* token)
18379 return (/* An ordinary function-body begins with an `{'. */
18380 token->type == CPP_OPEN_BRACE
18381 /* A ctor-initializer begins with a `:'. */
18382 || token->type == CPP_COLON
18383 /* A function-try-block begins with `try'. */
18384 || token->keyword == RID_TRY
18385 /* The named return value extension begins with `return'. */
18386 || token->keyword == RID_RETURN);
18389 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18393 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18397 token = cp_lexer_peek_token (parser->lexer);
18398 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18401 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18402 C++0x) ending a template-argument. */
18405 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18409 token = cp_lexer_peek_token (parser->lexer);
18410 return (token->type == CPP_COMMA
18411 || token->type == CPP_GREATER
18412 || token->type == CPP_ELLIPSIS
18413 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18416 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18417 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18420 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18425 token = cp_lexer_peek_nth_token (parser->lexer, n);
18426 if (token->type == CPP_LESS)
18428 /* Check for the sequence `<::' in the original code. It would be lexed as
18429 `[:', where `[' is a digraph, and there is no whitespace before
18431 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18434 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18435 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18441 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18442 or none_type otherwise. */
18444 static enum tag_types
18445 cp_parser_token_is_class_key (cp_token* token)
18447 switch (token->keyword)
18452 return record_type;
18461 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18464 cp_parser_check_class_key (enum tag_types class_key, tree type)
18466 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18467 permerror ("%qs tag used in naming %q#T",
18468 class_key == union_type ? "union"
18469 : class_key == record_type ? "struct" : "class",
18473 /* Issue an error message if DECL is redeclared with different
18474 access than its original declaration [class.access.spec/3].
18475 This applies to nested classes and nested class templates.
18479 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18481 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18484 if ((TREE_PRIVATE (decl)
18485 != (current_access_specifier == access_private_node))
18486 || (TREE_PROTECTED (decl)
18487 != (current_access_specifier == access_protected_node)))
18488 error ("%H%qD redeclared with different access", &location, decl);
18491 /* Look for the `template' keyword, as a syntactic disambiguator.
18492 Return TRUE iff it is present, in which case it will be
18496 cp_parser_optional_template_keyword (cp_parser *parser)
18498 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18500 /* The `template' keyword can only be used within templates;
18501 outside templates the parser can always figure out what is a
18502 template and what is not. */
18503 if (!processing_template_decl)
18505 cp_token *token = cp_lexer_peek_token (parser->lexer);
18506 error ("%H%<template%> (as a disambiguator) is only allowed "
18507 "within templates", &token->location);
18508 /* If this part of the token stream is rescanned, the same
18509 error message would be generated. So, we purge the token
18510 from the stream. */
18511 cp_lexer_purge_token (parser->lexer);
18516 /* Consume the `template' keyword. */
18517 cp_lexer_consume_token (parser->lexer);
18525 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18526 set PARSER->SCOPE, and perform other related actions. */
18529 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18532 struct tree_check *check_value;
18533 deferred_access_check *chk;
18534 VEC (deferred_access_check,gc) *checks;
18536 /* Get the stored value. */
18537 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18538 /* Perform any access checks that were deferred. */
18539 checks = check_value->checks;
18543 VEC_iterate (deferred_access_check, checks, i, chk) ;
18546 perform_or_defer_access_check (chk->binfo,
18551 /* Set the scope from the stored value. */
18552 parser->scope = check_value->value;
18553 parser->qualifying_scope = check_value->qualifying_scope;
18554 parser->object_scope = NULL_TREE;
18557 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18558 encounter the end of a block before what we were looking for. */
18561 cp_parser_cache_group (cp_parser *parser,
18562 enum cpp_ttype end,
18567 cp_token *token = cp_lexer_peek_token (parser->lexer);
18569 /* Abort a parenthesized expression if we encounter a semicolon. */
18570 if ((end == CPP_CLOSE_PAREN || depth == 0)
18571 && token->type == CPP_SEMICOLON)
18573 /* If we've reached the end of the file, stop. */
18574 if (token->type == CPP_EOF
18575 || (end != CPP_PRAGMA_EOL
18576 && token->type == CPP_PRAGMA_EOL))
18578 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18579 /* We've hit the end of an enclosing block, so there's been some
18580 kind of syntax error. */
18583 /* Consume the token. */
18584 cp_lexer_consume_token (parser->lexer);
18585 /* See if it starts a new group. */
18586 if (token->type == CPP_OPEN_BRACE)
18588 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18589 /* In theory this should probably check end == '}', but
18590 cp_parser_save_member_function_body needs it to exit
18591 after either '}' or ')' when called with ')'. */
18595 else if (token->type == CPP_OPEN_PAREN)
18597 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18598 if (depth == 0 && end == CPP_CLOSE_PAREN)
18601 else if (token->type == CPP_PRAGMA)
18602 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18603 else if (token->type == end)
18608 /* Begin parsing tentatively. We always save tokens while parsing
18609 tentatively so that if the tentative parsing fails we can restore the
18613 cp_parser_parse_tentatively (cp_parser* parser)
18615 /* Enter a new parsing context. */
18616 parser->context = cp_parser_context_new (parser->context);
18617 /* Begin saving tokens. */
18618 cp_lexer_save_tokens (parser->lexer);
18619 /* In order to avoid repetitive access control error messages,
18620 access checks are queued up until we are no longer parsing
18622 push_deferring_access_checks (dk_deferred);
18625 /* Commit to the currently active tentative parse. */
18628 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18630 cp_parser_context *context;
18633 /* Mark all of the levels as committed. */
18634 lexer = parser->lexer;
18635 for (context = parser->context; context->next; context = context->next)
18637 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18639 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18640 while (!cp_lexer_saving_tokens (lexer))
18641 lexer = lexer->next;
18642 cp_lexer_commit_tokens (lexer);
18646 /* Abort the currently active tentative parse. All consumed tokens
18647 will be rolled back, and no diagnostics will be issued. */
18650 cp_parser_abort_tentative_parse (cp_parser* parser)
18652 cp_parser_simulate_error (parser);
18653 /* Now, pretend that we want to see if the construct was
18654 successfully parsed. */
18655 cp_parser_parse_definitely (parser);
18658 /* Stop parsing tentatively. If a parse error has occurred, restore the
18659 token stream. Otherwise, commit to the tokens we have consumed.
18660 Returns true if no error occurred; false otherwise. */
18663 cp_parser_parse_definitely (cp_parser* parser)
18665 bool error_occurred;
18666 cp_parser_context *context;
18668 /* Remember whether or not an error occurred, since we are about to
18669 destroy that information. */
18670 error_occurred = cp_parser_error_occurred (parser);
18671 /* Remove the topmost context from the stack. */
18672 context = parser->context;
18673 parser->context = context->next;
18674 /* If no parse errors occurred, commit to the tentative parse. */
18675 if (!error_occurred)
18677 /* Commit to the tokens read tentatively, unless that was
18679 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18680 cp_lexer_commit_tokens (parser->lexer);
18682 pop_to_parent_deferring_access_checks ();
18684 /* Otherwise, if errors occurred, roll back our state so that things
18685 are just as they were before we began the tentative parse. */
18688 cp_lexer_rollback_tokens (parser->lexer);
18689 pop_deferring_access_checks ();
18691 /* Add the context to the front of the free list. */
18692 context->next = cp_parser_context_free_list;
18693 cp_parser_context_free_list = context;
18695 return !error_occurred;
18698 /* Returns true if we are parsing tentatively and are not committed to
18699 this tentative parse. */
18702 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18704 return (cp_parser_parsing_tentatively (parser)
18705 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18708 /* Returns nonzero iff an error has occurred during the most recent
18709 tentative parse. */
18712 cp_parser_error_occurred (cp_parser* parser)
18714 return (cp_parser_parsing_tentatively (parser)
18715 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18718 /* Returns nonzero if GNU extensions are allowed. */
18721 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18723 return parser->allow_gnu_extensions_p;
18726 /* Objective-C++ Productions */
18729 /* Parse an Objective-C expression, which feeds into a primary-expression
18733 objc-message-expression
18734 objc-string-literal
18735 objc-encode-expression
18736 objc-protocol-expression
18737 objc-selector-expression
18739 Returns a tree representation of the expression. */
18742 cp_parser_objc_expression (cp_parser* parser)
18744 /* Try to figure out what kind of declaration is present. */
18745 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18749 case CPP_OPEN_SQUARE:
18750 return cp_parser_objc_message_expression (parser);
18752 case CPP_OBJC_STRING:
18753 kwd = cp_lexer_consume_token (parser->lexer);
18754 return objc_build_string_object (kwd->u.value);
18757 switch (kwd->keyword)
18759 case RID_AT_ENCODE:
18760 return cp_parser_objc_encode_expression (parser);
18762 case RID_AT_PROTOCOL:
18763 return cp_parser_objc_protocol_expression (parser);
18765 case RID_AT_SELECTOR:
18766 return cp_parser_objc_selector_expression (parser);
18772 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18773 &kwd->location, kwd->u.value);
18774 cp_parser_skip_to_end_of_block_or_statement (parser);
18777 return error_mark_node;
18780 /* Parse an Objective-C message expression.
18782 objc-message-expression:
18783 [ objc-message-receiver objc-message-args ]
18785 Returns a representation of an Objective-C message. */
18788 cp_parser_objc_message_expression (cp_parser* parser)
18790 tree receiver, messageargs;
18792 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18793 receiver = cp_parser_objc_message_receiver (parser);
18794 messageargs = cp_parser_objc_message_args (parser);
18795 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18797 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18800 /* Parse an objc-message-receiver.
18802 objc-message-receiver:
18804 simple-type-specifier
18806 Returns a representation of the type or expression. */
18809 cp_parser_objc_message_receiver (cp_parser* parser)
18813 /* An Objective-C message receiver may be either (1) a type
18814 or (2) an expression. */
18815 cp_parser_parse_tentatively (parser);
18816 rcv = cp_parser_expression (parser, false);
18818 if (cp_parser_parse_definitely (parser))
18821 rcv = cp_parser_simple_type_specifier (parser,
18822 /*decl_specs=*/NULL,
18823 CP_PARSER_FLAGS_NONE);
18825 return objc_get_class_reference (rcv);
18828 /* Parse the arguments and selectors comprising an Objective-C message.
18833 objc-selector-args , objc-comma-args
18835 objc-selector-args:
18836 objc-selector [opt] : assignment-expression
18837 objc-selector-args objc-selector [opt] : assignment-expression
18840 assignment-expression
18841 objc-comma-args , assignment-expression
18843 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18844 selector arguments and TREE_VALUE containing a list of comma
18848 cp_parser_objc_message_args (cp_parser* parser)
18850 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18851 bool maybe_unary_selector_p = true;
18852 cp_token *token = cp_lexer_peek_token (parser->lexer);
18854 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18856 tree selector = NULL_TREE, arg;
18858 if (token->type != CPP_COLON)
18859 selector = cp_parser_objc_selector (parser);
18861 /* Detect if we have a unary selector. */
18862 if (maybe_unary_selector_p
18863 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18864 return build_tree_list (selector, NULL_TREE);
18866 maybe_unary_selector_p = false;
18867 cp_parser_require (parser, CPP_COLON, "%<:%>");
18868 arg = cp_parser_assignment_expression (parser, false);
18871 = chainon (sel_args,
18872 build_tree_list (selector, arg));
18874 token = cp_lexer_peek_token (parser->lexer);
18877 /* Handle non-selector arguments, if any. */
18878 while (token->type == CPP_COMMA)
18882 cp_lexer_consume_token (parser->lexer);
18883 arg = cp_parser_assignment_expression (parser, false);
18886 = chainon (addl_args,
18887 build_tree_list (NULL_TREE, arg));
18889 token = cp_lexer_peek_token (parser->lexer);
18892 return build_tree_list (sel_args, addl_args);
18895 /* Parse an Objective-C encode expression.
18897 objc-encode-expression:
18898 @encode objc-typename
18900 Returns an encoded representation of the type argument. */
18903 cp_parser_objc_encode_expression (cp_parser* parser)
18908 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18909 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18910 token = cp_lexer_peek_token (parser->lexer);
18911 type = complete_type (cp_parser_type_id (parser));
18912 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18916 error ("%H%<@encode%> must specify a type as an argument",
18918 return error_mark_node;
18921 return objc_build_encode_expr (type);
18924 /* Parse an Objective-C @defs expression. */
18927 cp_parser_objc_defs_expression (cp_parser *parser)
18931 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18932 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18933 name = cp_parser_identifier (parser);
18934 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18936 return objc_get_class_ivars (name);
18939 /* Parse an Objective-C protocol expression.
18941 objc-protocol-expression:
18942 @protocol ( identifier )
18944 Returns a representation of the protocol expression. */
18947 cp_parser_objc_protocol_expression (cp_parser* parser)
18951 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18952 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18953 proto = cp_parser_identifier (parser);
18954 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18956 return objc_build_protocol_expr (proto);
18959 /* Parse an Objective-C selector expression.
18961 objc-selector-expression:
18962 @selector ( objc-method-signature )
18964 objc-method-signature:
18970 objc-selector-seq objc-selector :
18972 Returns a representation of the method selector. */
18975 cp_parser_objc_selector_expression (cp_parser* parser)
18977 tree sel_seq = NULL_TREE;
18978 bool maybe_unary_selector_p = true;
18981 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18982 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18983 token = cp_lexer_peek_token (parser->lexer);
18985 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18986 || token->type == CPP_SCOPE)
18988 tree selector = NULL_TREE;
18990 if (token->type != CPP_COLON
18991 || token->type == CPP_SCOPE)
18992 selector = cp_parser_objc_selector (parser);
18994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18995 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18997 /* Detect if we have a unary selector. */
18998 if (maybe_unary_selector_p)
19000 sel_seq = selector;
19001 goto finish_selector;
19005 cp_parser_error (parser, "expected %<:%>");
19008 maybe_unary_selector_p = false;
19009 token = cp_lexer_consume_token (parser->lexer);
19011 if (token->type == CPP_SCOPE)
19014 = chainon (sel_seq,
19015 build_tree_list (selector, NULL_TREE));
19017 = chainon (sel_seq,
19018 build_tree_list (NULL_TREE, NULL_TREE));
19022 = chainon (sel_seq,
19023 build_tree_list (selector, NULL_TREE));
19025 token = cp_lexer_peek_token (parser->lexer);
19029 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19031 return objc_build_selector_expr (sel_seq);
19034 /* Parse a list of identifiers.
19036 objc-identifier-list:
19038 objc-identifier-list , identifier
19040 Returns a TREE_LIST of identifier nodes. */
19043 cp_parser_objc_identifier_list (cp_parser* parser)
19045 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19046 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19048 while (sep->type == CPP_COMMA)
19050 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19051 list = chainon (list,
19052 build_tree_list (NULL_TREE,
19053 cp_parser_identifier (parser)));
19054 sep = cp_lexer_peek_token (parser->lexer);
19060 /* Parse an Objective-C alias declaration.
19062 objc-alias-declaration:
19063 @compatibility_alias identifier identifier ;
19065 This function registers the alias mapping with the Objective-C front end.
19066 It returns nothing. */
19069 cp_parser_objc_alias_declaration (cp_parser* parser)
19073 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19074 alias = cp_parser_identifier (parser);
19075 orig = cp_parser_identifier (parser);
19076 objc_declare_alias (alias, orig);
19077 cp_parser_consume_semicolon_at_end_of_statement (parser);
19080 /* Parse an Objective-C class forward-declaration.
19082 objc-class-declaration:
19083 @class objc-identifier-list ;
19085 The function registers the forward declarations with the Objective-C
19086 front end. It returns nothing. */
19089 cp_parser_objc_class_declaration (cp_parser* parser)
19091 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19092 objc_declare_class (cp_parser_objc_identifier_list (parser));
19093 cp_parser_consume_semicolon_at_end_of_statement (parser);
19096 /* Parse a list of Objective-C protocol references.
19098 objc-protocol-refs-opt:
19099 objc-protocol-refs [opt]
19101 objc-protocol-refs:
19102 < objc-identifier-list >
19104 Returns a TREE_LIST of identifiers, if any. */
19107 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19109 tree protorefs = NULL_TREE;
19111 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19113 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19114 protorefs = cp_parser_objc_identifier_list (parser);
19115 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19121 /* Parse a Objective-C visibility specification. */
19124 cp_parser_objc_visibility_spec (cp_parser* parser)
19126 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19128 switch (vis->keyword)
19130 case RID_AT_PRIVATE:
19131 objc_set_visibility (2);
19133 case RID_AT_PROTECTED:
19134 objc_set_visibility (0);
19136 case RID_AT_PUBLIC:
19137 objc_set_visibility (1);
19143 /* Eat '@private'/'@protected'/'@public'. */
19144 cp_lexer_consume_token (parser->lexer);
19147 /* Parse an Objective-C method type. */
19150 cp_parser_objc_method_type (cp_parser* parser)
19152 objc_set_method_type
19153 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19158 /* Parse an Objective-C protocol qualifier. */
19161 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19163 tree quals = NULL_TREE, node;
19164 cp_token *token = cp_lexer_peek_token (parser->lexer);
19166 node = token->u.value;
19168 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19169 && (node == ridpointers [(int) RID_IN]
19170 || node == ridpointers [(int) RID_OUT]
19171 || node == ridpointers [(int) RID_INOUT]
19172 || node == ridpointers [(int) RID_BYCOPY]
19173 || node == ridpointers [(int) RID_BYREF]
19174 || node == ridpointers [(int) RID_ONEWAY]))
19176 quals = tree_cons (NULL_TREE, node, quals);
19177 cp_lexer_consume_token (parser->lexer);
19178 token = cp_lexer_peek_token (parser->lexer);
19179 node = token->u.value;
19185 /* Parse an Objective-C typename. */
19188 cp_parser_objc_typename (cp_parser* parser)
19190 tree type_name = NULL_TREE;
19192 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19194 tree proto_quals, cp_type = NULL_TREE;
19196 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19197 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19199 /* An ObjC type name may consist of just protocol qualifiers, in which
19200 case the type shall default to 'id'. */
19201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19202 cp_type = cp_parser_type_id (parser);
19204 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19205 type_name = build_tree_list (proto_quals, cp_type);
19211 /* Check to see if TYPE refers to an Objective-C selector name. */
19214 cp_parser_objc_selector_p (enum cpp_ttype type)
19216 return (type == CPP_NAME || type == CPP_KEYWORD
19217 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19218 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19219 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19220 || type == CPP_XOR || type == CPP_XOR_EQ);
19223 /* Parse an Objective-C selector. */
19226 cp_parser_objc_selector (cp_parser* parser)
19228 cp_token *token = cp_lexer_consume_token (parser->lexer);
19230 if (!cp_parser_objc_selector_p (token->type))
19232 error ("%Hinvalid Objective-C++ selector name", &token->location);
19233 return error_mark_node;
19236 /* C++ operator names are allowed to appear in ObjC selectors. */
19237 switch (token->type)
19239 case CPP_AND_AND: return get_identifier ("and");
19240 case CPP_AND_EQ: return get_identifier ("and_eq");
19241 case CPP_AND: return get_identifier ("bitand");
19242 case CPP_OR: return get_identifier ("bitor");
19243 case CPP_COMPL: return get_identifier ("compl");
19244 case CPP_NOT: return get_identifier ("not");
19245 case CPP_NOT_EQ: return get_identifier ("not_eq");
19246 case CPP_OR_OR: return get_identifier ("or");
19247 case CPP_OR_EQ: return get_identifier ("or_eq");
19248 case CPP_XOR: return get_identifier ("xor");
19249 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19250 default: return token->u.value;
19254 /* Parse an Objective-C params list. */
19257 cp_parser_objc_method_keyword_params (cp_parser* parser)
19259 tree params = NULL_TREE;
19260 bool maybe_unary_selector_p = true;
19261 cp_token *token = cp_lexer_peek_token (parser->lexer);
19263 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19265 tree selector = NULL_TREE, type_name, identifier;
19267 if (token->type != CPP_COLON)
19268 selector = cp_parser_objc_selector (parser);
19270 /* Detect if we have a unary selector. */
19271 if (maybe_unary_selector_p
19272 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19275 maybe_unary_selector_p = false;
19276 cp_parser_require (parser, CPP_COLON, "%<:%>");
19277 type_name = cp_parser_objc_typename (parser);
19278 identifier = cp_parser_identifier (parser);
19282 objc_build_keyword_decl (selector,
19286 token = cp_lexer_peek_token (parser->lexer);
19292 /* Parse the non-keyword Objective-C params. */
19295 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19297 tree params = make_node (TREE_LIST);
19298 cp_token *token = cp_lexer_peek_token (parser->lexer);
19299 *ellipsisp = false; /* Initially, assume no ellipsis. */
19301 while (token->type == CPP_COMMA)
19303 cp_parameter_declarator *parmdecl;
19306 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19307 token = cp_lexer_peek_token (parser->lexer);
19309 if (token->type == CPP_ELLIPSIS)
19311 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19316 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19317 parm = grokdeclarator (parmdecl->declarator,
19318 &parmdecl->decl_specifiers,
19319 PARM, /*initialized=*/0,
19320 /*attrlist=*/NULL);
19322 chainon (params, build_tree_list (NULL_TREE, parm));
19323 token = cp_lexer_peek_token (parser->lexer);
19329 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19332 cp_parser_objc_interstitial_code (cp_parser* parser)
19334 cp_token *token = cp_lexer_peek_token (parser->lexer);
19336 /* If the next token is `extern' and the following token is a string
19337 literal, then we have a linkage specification. */
19338 if (token->keyword == RID_EXTERN
19339 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19340 cp_parser_linkage_specification (parser);
19341 /* Handle #pragma, if any. */
19342 else if (token->type == CPP_PRAGMA)
19343 cp_parser_pragma (parser, pragma_external);
19344 /* Allow stray semicolons. */
19345 else if (token->type == CPP_SEMICOLON)
19346 cp_lexer_consume_token (parser->lexer);
19347 /* Finally, try to parse a block-declaration, or a function-definition. */
19349 cp_parser_block_declaration (parser, /*statement_p=*/false);
19352 /* Parse a method signature. */
19355 cp_parser_objc_method_signature (cp_parser* parser)
19357 tree rettype, kwdparms, optparms;
19358 bool ellipsis = false;
19360 cp_parser_objc_method_type (parser);
19361 rettype = cp_parser_objc_typename (parser);
19362 kwdparms = cp_parser_objc_method_keyword_params (parser);
19363 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19365 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19368 /* Pars an Objective-C method prototype list. */
19371 cp_parser_objc_method_prototype_list (cp_parser* parser)
19373 cp_token *token = cp_lexer_peek_token (parser->lexer);
19375 while (token->keyword != RID_AT_END)
19377 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19379 objc_add_method_declaration
19380 (cp_parser_objc_method_signature (parser));
19381 cp_parser_consume_semicolon_at_end_of_statement (parser);
19384 /* Allow for interspersed non-ObjC++ code. */
19385 cp_parser_objc_interstitial_code (parser);
19387 token = cp_lexer_peek_token (parser->lexer);
19390 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19391 objc_finish_interface ();
19394 /* Parse an Objective-C method definition list. */
19397 cp_parser_objc_method_definition_list (cp_parser* parser)
19399 cp_token *token = cp_lexer_peek_token (parser->lexer);
19401 while (token->keyword != RID_AT_END)
19405 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19407 push_deferring_access_checks (dk_deferred);
19408 objc_start_method_definition
19409 (cp_parser_objc_method_signature (parser));
19411 /* For historical reasons, we accept an optional semicolon. */
19412 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19413 cp_lexer_consume_token (parser->lexer);
19415 perform_deferred_access_checks ();
19416 stop_deferring_access_checks ();
19417 meth = cp_parser_function_definition_after_declarator (parser,
19419 pop_deferring_access_checks ();
19420 objc_finish_method_definition (meth);
19423 /* Allow for interspersed non-ObjC++ code. */
19424 cp_parser_objc_interstitial_code (parser);
19426 token = cp_lexer_peek_token (parser->lexer);
19429 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19430 objc_finish_implementation ();
19433 /* Parse Objective-C ivars. */
19436 cp_parser_objc_class_ivars (cp_parser* parser)
19438 cp_token *token = cp_lexer_peek_token (parser->lexer);
19440 if (token->type != CPP_OPEN_BRACE)
19441 return; /* No ivars specified. */
19443 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19444 token = cp_lexer_peek_token (parser->lexer);
19446 while (token->type != CPP_CLOSE_BRACE)
19448 cp_decl_specifier_seq declspecs;
19449 int decl_class_or_enum_p;
19450 tree prefix_attributes;
19452 cp_parser_objc_visibility_spec (parser);
19454 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19457 cp_parser_decl_specifier_seq (parser,
19458 CP_PARSER_FLAGS_OPTIONAL,
19460 &decl_class_or_enum_p);
19461 prefix_attributes = declspecs.attributes;
19462 declspecs.attributes = NULL_TREE;
19464 /* Keep going until we hit the `;' at the end of the
19466 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19468 tree width = NULL_TREE, attributes, first_attribute, decl;
19469 cp_declarator *declarator = NULL;
19470 int ctor_dtor_or_conv_p;
19472 /* Check for a (possibly unnamed) bitfield declaration. */
19473 token = cp_lexer_peek_token (parser->lexer);
19474 if (token->type == CPP_COLON)
19477 if (token->type == CPP_NAME
19478 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19481 /* Get the name of the bitfield. */
19482 declarator = make_id_declarator (NULL_TREE,
19483 cp_parser_identifier (parser),
19487 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19488 /* Get the width of the bitfield. */
19490 = cp_parser_constant_expression (parser,
19491 /*allow_non_constant=*/false,
19496 /* Parse the declarator. */
19498 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19499 &ctor_dtor_or_conv_p,
19500 /*parenthesized_p=*/NULL,
19501 /*member_p=*/false);
19504 /* Look for attributes that apply to the ivar. */
19505 attributes = cp_parser_attributes_opt (parser);
19506 /* Remember which attributes are prefix attributes and
19508 first_attribute = attributes;
19509 /* Combine the attributes. */
19510 attributes = chainon (prefix_attributes, attributes);
19513 /* Create the bitfield declaration. */
19514 decl = grokbitfield (declarator, &declspecs,
19518 decl = grokfield (declarator, &declspecs,
19519 NULL_TREE, /*init_const_expr_p=*/false,
19520 NULL_TREE, attributes);
19522 /* Add the instance variable. */
19523 objc_add_instance_variable (decl);
19525 /* Reset PREFIX_ATTRIBUTES. */
19526 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19527 attributes = TREE_CHAIN (attributes);
19529 TREE_CHAIN (attributes) = NULL_TREE;
19531 token = cp_lexer_peek_token (parser->lexer);
19533 if (token->type == CPP_COMMA)
19535 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19541 cp_parser_consume_semicolon_at_end_of_statement (parser);
19542 token = cp_lexer_peek_token (parser->lexer);
19545 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19546 /* For historical reasons, we accept an optional semicolon. */
19547 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19548 cp_lexer_consume_token (parser->lexer);
19551 /* Parse an Objective-C protocol declaration. */
19554 cp_parser_objc_protocol_declaration (cp_parser* parser)
19556 tree proto, protorefs;
19559 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19562 tok = cp_lexer_peek_token (parser->lexer);
19563 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19567 /* See if we have a forward declaration or a definition. */
19568 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19570 /* Try a forward declaration first. */
19571 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19573 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19575 cp_parser_consume_semicolon_at_end_of_statement (parser);
19578 /* Ok, we got a full-fledged definition (or at least should). */
19581 proto = cp_parser_identifier (parser);
19582 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19583 objc_start_protocol (proto, protorefs);
19584 cp_parser_objc_method_prototype_list (parser);
19588 /* Parse an Objective-C superclass or category. */
19591 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19594 cp_token *next = cp_lexer_peek_token (parser->lexer);
19596 *super = *categ = NULL_TREE;
19597 if (next->type == CPP_COLON)
19599 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19600 *super = cp_parser_identifier (parser);
19602 else if (next->type == CPP_OPEN_PAREN)
19604 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19605 *categ = cp_parser_identifier (parser);
19606 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19610 /* Parse an Objective-C class interface. */
19613 cp_parser_objc_class_interface (cp_parser* parser)
19615 tree name, super, categ, protos;
19617 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19618 name = cp_parser_identifier (parser);
19619 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19620 protos = cp_parser_objc_protocol_refs_opt (parser);
19622 /* We have either a class or a category on our hands. */
19624 objc_start_category_interface (name, categ, protos);
19627 objc_start_class_interface (name, super, protos);
19628 /* Handle instance variable declarations, if any. */
19629 cp_parser_objc_class_ivars (parser);
19630 objc_continue_interface ();
19633 cp_parser_objc_method_prototype_list (parser);
19636 /* Parse an Objective-C class implementation. */
19639 cp_parser_objc_class_implementation (cp_parser* parser)
19641 tree name, super, categ;
19643 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19644 name = cp_parser_identifier (parser);
19645 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19647 /* We have either a class or a category on our hands. */
19649 objc_start_category_implementation (name, categ);
19652 objc_start_class_implementation (name, super);
19653 /* Handle instance variable declarations, if any. */
19654 cp_parser_objc_class_ivars (parser);
19655 objc_continue_implementation ();
19658 cp_parser_objc_method_definition_list (parser);
19661 /* Consume the @end token and finish off the implementation. */
19664 cp_parser_objc_end_implementation (cp_parser* parser)
19666 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19667 objc_finish_implementation ();
19670 /* Parse an Objective-C declaration. */
19673 cp_parser_objc_declaration (cp_parser* parser)
19675 /* Try to figure out what kind of declaration is present. */
19676 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19678 switch (kwd->keyword)
19681 cp_parser_objc_alias_declaration (parser);
19684 cp_parser_objc_class_declaration (parser);
19686 case RID_AT_PROTOCOL:
19687 cp_parser_objc_protocol_declaration (parser);
19689 case RID_AT_INTERFACE:
19690 cp_parser_objc_class_interface (parser);
19692 case RID_AT_IMPLEMENTATION:
19693 cp_parser_objc_class_implementation (parser);
19696 cp_parser_objc_end_implementation (parser);
19699 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19700 &kwd->location, kwd->u.value);
19701 cp_parser_skip_to_end_of_block_or_statement (parser);
19705 /* Parse an Objective-C try-catch-finally statement.
19707 objc-try-catch-finally-stmt:
19708 @try compound-statement objc-catch-clause-seq [opt]
19709 objc-finally-clause [opt]
19711 objc-catch-clause-seq:
19712 objc-catch-clause objc-catch-clause-seq [opt]
19715 @catch ( exception-declaration ) compound-statement
19717 objc-finally-clause
19718 @finally compound-statement
19720 Returns NULL_TREE. */
19723 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19724 location_t location;
19727 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19728 location = cp_lexer_peek_token (parser->lexer)->location;
19729 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19730 node, lest it get absorbed into the surrounding block. */
19731 stmt = push_stmt_list ();
19732 cp_parser_compound_statement (parser, NULL, false);
19733 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19735 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19737 cp_parameter_declarator *parmdecl;
19740 cp_lexer_consume_token (parser->lexer);
19741 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19742 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19743 parm = grokdeclarator (parmdecl->declarator,
19744 &parmdecl->decl_specifiers,
19745 PARM, /*initialized=*/0,
19746 /*attrlist=*/NULL);
19747 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19748 objc_begin_catch_clause (parm);
19749 cp_parser_compound_statement (parser, NULL, false);
19750 objc_finish_catch_clause ();
19753 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19755 cp_lexer_consume_token (parser->lexer);
19756 location = cp_lexer_peek_token (parser->lexer)->location;
19757 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19758 node, lest it get absorbed into the surrounding block. */
19759 stmt = push_stmt_list ();
19760 cp_parser_compound_statement (parser, NULL, false);
19761 objc_build_finally_clause (location, pop_stmt_list (stmt));
19764 return objc_finish_try_stmt ();
19767 /* Parse an Objective-C synchronized statement.
19769 objc-synchronized-stmt:
19770 @synchronized ( expression ) compound-statement
19772 Returns NULL_TREE. */
19775 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19776 location_t location;
19779 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19781 location = cp_lexer_peek_token (parser->lexer)->location;
19782 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19783 lock = cp_parser_expression (parser, false);
19784 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19786 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19787 node, lest it get absorbed into the surrounding block. */
19788 stmt = push_stmt_list ();
19789 cp_parser_compound_statement (parser, NULL, false);
19791 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19794 /* Parse an Objective-C throw statement.
19797 @throw assignment-expression [opt] ;
19799 Returns a constructed '@throw' statement. */
19802 cp_parser_objc_throw_statement (cp_parser *parser) {
19803 tree expr = NULL_TREE;
19805 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19808 expr = cp_parser_assignment_expression (parser, false);
19810 cp_parser_consume_semicolon_at_end_of_statement (parser);
19812 return objc_build_throw_stmt (expr);
19815 /* Parse an Objective-C statement. */
19818 cp_parser_objc_statement (cp_parser * parser) {
19819 /* Try to figure out what kind of declaration is present. */
19820 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19822 switch (kwd->keyword)
19825 return cp_parser_objc_try_catch_finally_statement (parser);
19826 case RID_AT_SYNCHRONIZED:
19827 return cp_parser_objc_synchronized_statement (parser);
19829 return cp_parser_objc_throw_statement (parser);
19831 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19832 &kwd->location, kwd->u.value);
19833 cp_parser_skip_to_end_of_block_or_statement (parser);
19836 return error_mark_node;
19839 /* OpenMP 2.5 parsing routines. */
19841 /* Returns name of the next clause.
19842 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19843 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19844 returned and the token is consumed. */
19846 static pragma_omp_clause
19847 cp_parser_omp_clause_name (cp_parser *parser)
19849 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19851 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19852 result = PRAGMA_OMP_CLAUSE_IF;
19853 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19854 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19855 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19856 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19857 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19859 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19860 const char *p = IDENTIFIER_POINTER (id);
19865 if (!strcmp ("collapse", p))
19866 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19867 else if (!strcmp ("copyin", p))
19868 result = PRAGMA_OMP_CLAUSE_COPYIN;
19869 else if (!strcmp ("copyprivate", p))
19870 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19873 if (!strcmp ("firstprivate", p))
19874 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19877 if (!strcmp ("lastprivate", p))
19878 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19881 if (!strcmp ("nowait", p))
19882 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19883 else if (!strcmp ("num_threads", p))
19884 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19887 if (!strcmp ("ordered", p))
19888 result = PRAGMA_OMP_CLAUSE_ORDERED;
19891 if (!strcmp ("reduction", p))
19892 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19895 if (!strcmp ("schedule", p))
19896 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19897 else if (!strcmp ("shared", p))
19898 result = PRAGMA_OMP_CLAUSE_SHARED;
19901 if (!strcmp ("untied", p))
19902 result = PRAGMA_OMP_CLAUSE_UNTIED;
19907 if (result != PRAGMA_OMP_CLAUSE_NONE)
19908 cp_lexer_consume_token (parser->lexer);
19913 /* Validate that a clause of the given type does not already exist. */
19916 check_no_duplicate_clause (tree clauses, enum tree_code code,
19917 const char *name, location_t location)
19921 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19922 if (OMP_CLAUSE_CODE (c) == code)
19924 error ("%Htoo many %qs clauses", &location, name);
19932 variable-list , identifier
19934 In addition, we match a closing parenthesis. An opening parenthesis
19935 will have been consumed by the caller.
19937 If KIND is nonzero, create the appropriate node and install the decl
19938 in OMP_CLAUSE_DECL and add the node to the head of the list.
19940 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19941 return the list created. */
19944 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19952 token = cp_lexer_peek_token (parser->lexer);
19953 name = cp_parser_id_expression (parser, /*template_p=*/false,
19954 /*check_dependency_p=*/true,
19955 /*template_p=*/NULL,
19956 /*declarator_p=*/false,
19957 /*optional_p=*/false);
19958 if (name == error_mark_node)
19961 decl = cp_parser_lookup_name_simple (parser, name, token->location);
19962 if (decl == error_mark_node)
19963 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
19964 else if (kind != 0)
19966 tree u = build_omp_clause (kind);
19967 OMP_CLAUSE_DECL (u) = decl;
19968 OMP_CLAUSE_CHAIN (u) = list;
19972 list = tree_cons (decl, NULL_TREE, list);
19975 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19977 cp_lexer_consume_token (parser->lexer);
19980 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19984 /* Try to resync to an unnested comma. Copied from
19985 cp_parser_parenthesized_expression_list. */
19987 ending = cp_parser_skip_to_closing_parenthesis (parser,
19988 /*recovering=*/true,
19990 /*consume_paren=*/true);
19998 /* Similarly, but expect leading and trailing parenthesis. This is a very
19999 common case for omp clauses. */
20002 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20004 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20005 return cp_parser_omp_var_list_no_open (parser, kind, list);
20010 collapse ( constant-expression ) */
20013 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20019 loc = cp_lexer_peek_token (parser->lexer)->location;
20020 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20023 num = cp_parser_constant_expression (parser, false, NULL);
20025 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20026 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20027 /*or_comma=*/false,
20028 /*consume_paren=*/true);
20030 if (num == error_mark_node)
20032 num = fold_non_dependent_expr (num);
20033 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20034 || !host_integerp (num, 0)
20035 || (n = tree_low_cst (num, 0)) <= 0
20038 error ("%Hcollapse argument needs positive constant integer expression",
20043 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20044 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20045 OMP_CLAUSE_CHAIN (c) = list;
20046 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20052 default ( shared | none ) */
20055 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20057 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20060 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20062 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20064 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20065 const char *p = IDENTIFIER_POINTER (id);
20070 if (strcmp ("none", p) != 0)
20072 kind = OMP_CLAUSE_DEFAULT_NONE;
20076 if (strcmp ("shared", p) != 0)
20078 kind = OMP_CLAUSE_DEFAULT_SHARED;
20085 cp_lexer_consume_token (parser->lexer);
20090 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20093 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20094 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20095 /*or_comma=*/false,
20096 /*consume_paren=*/true);
20098 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20101 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20102 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20103 OMP_CLAUSE_CHAIN (c) = list;
20104 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20110 if ( expression ) */
20113 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20117 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20120 t = cp_parser_condition (parser);
20122 if (t == error_mark_node
20123 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20124 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20125 /*or_comma=*/false,
20126 /*consume_paren=*/true);
20128 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20130 c = build_omp_clause (OMP_CLAUSE_IF);
20131 OMP_CLAUSE_IF_EXPR (c) = t;
20132 OMP_CLAUSE_CHAIN (c) = list;
20141 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20142 tree list, location_t location)
20146 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20148 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20149 OMP_CLAUSE_CHAIN (c) = list;
20154 num_threads ( expression ) */
20157 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20158 location_t location)
20162 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20165 t = cp_parser_expression (parser, false);
20167 if (t == error_mark_node
20168 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20169 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20170 /*or_comma=*/false,
20171 /*consume_paren=*/true);
20173 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20174 "num_threads", location);
20176 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20177 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20178 OMP_CLAUSE_CHAIN (c) = list;
20187 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20188 tree list, location_t location)
20192 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20193 "ordered", location);
20195 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20196 OMP_CLAUSE_CHAIN (c) = list;
20201 reduction ( reduction-operator : variable-list )
20203 reduction-operator:
20204 One of: + * - & ^ | && || */
20207 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20209 enum tree_code code;
20212 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20215 switch (cp_lexer_peek_token (parser->lexer)->type)
20227 code = BIT_AND_EXPR;
20230 code = BIT_XOR_EXPR;
20233 code = BIT_IOR_EXPR;
20236 code = TRUTH_ANDIF_EXPR;
20239 code = TRUTH_ORIF_EXPR;
20242 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20243 "%<|%>, %<&&%>, or %<||%>");
20245 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20246 /*or_comma=*/false,
20247 /*consume_paren=*/true);
20250 cp_lexer_consume_token (parser->lexer);
20252 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20255 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20256 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20257 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20263 schedule ( schedule-kind )
20264 schedule ( schedule-kind , expression )
20267 static | dynamic | guided | runtime | auto */
20270 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20274 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20277 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20279 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20281 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20282 const char *p = IDENTIFIER_POINTER (id);
20287 if (strcmp ("dynamic", p) != 0)
20289 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20293 if (strcmp ("guided", p) != 0)
20295 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20299 if (strcmp ("runtime", p) != 0)
20301 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20308 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20309 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20310 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20311 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20314 cp_lexer_consume_token (parser->lexer);
20316 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20319 cp_lexer_consume_token (parser->lexer);
20321 token = cp_lexer_peek_token (parser->lexer);
20322 t = cp_parser_assignment_expression (parser, false);
20324 if (t == error_mark_node)
20326 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20327 error ("%Hschedule %<runtime%> does not take "
20328 "a %<chunk_size%> parameter", &token->location);
20329 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20330 error ("%Hschedule %<auto%> does not take "
20331 "a %<chunk_size%> parameter", &token->location);
20333 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20335 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20338 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20341 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20342 OMP_CLAUSE_CHAIN (c) = list;
20346 cp_parser_error (parser, "invalid schedule kind");
20348 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20349 /*or_comma=*/false,
20350 /*consume_paren=*/true);
20358 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20359 tree list, location_t location)
20363 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20365 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20366 OMP_CLAUSE_CHAIN (c) = list;
20370 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20371 is a bitmask in MASK. Return the list of clauses found; the result
20372 of clause default goes in *pdefault. */
20375 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20376 const char *where, cp_token *pragma_tok)
20378 tree clauses = NULL;
20380 cp_token *token = NULL;
20382 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20384 pragma_omp_clause c_kind;
20385 const char *c_name;
20386 tree prev = clauses;
20388 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20389 cp_lexer_consume_token (parser->lexer);
20391 token = cp_lexer_peek_token (parser->lexer);
20392 c_kind = cp_parser_omp_clause_name (parser);
20397 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20398 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20400 c_name = "collapse";
20402 case PRAGMA_OMP_CLAUSE_COPYIN:
20403 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20406 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20407 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20409 c_name = "copyprivate";
20411 case PRAGMA_OMP_CLAUSE_DEFAULT:
20412 clauses = cp_parser_omp_clause_default (parser, clauses,
20414 c_name = "default";
20416 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20417 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20419 c_name = "firstprivate";
20421 case PRAGMA_OMP_CLAUSE_IF:
20422 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20425 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20426 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20428 c_name = "lastprivate";
20430 case PRAGMA_OMP_CLAUSE_NOWAIT:
20431 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20434 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20435 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20437 c_name = "num_threads";
20439 case PRAGMA_OMP_CLAUSE_ORDERED:
20440 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20442 c_name = "ordered";
20444 case PRAGMA_OMP_CLAUSE_PRIVATE:
20445 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20447 c_name = "private";
20449 case PRAGMA_OMP_CLAUSE_REDUCTION:
20450 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20451 c_name = "reduction";
20453 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20454 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20456 c_name = "schedule";
20458 case PRAGMA_OMP_CLAUSE_SHARED:
20459 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20463 case PRAGMA_OMP_CLAUSE_UNTIED:
20464 clauses = cp_parser_omp_clause_untied (parser, clauses,
20469 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20473 if (((mask >> c_kind) & 1) == 0)
20475 /* Remove the invalid clause(s) from the list to avoid
20476 confusing the rest of the compiler. */
20478 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20482 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20483 return finish_omp_clauses (clauses);
20490 In practice, we're also interested in adding the statement to an
20491 outer node. So it is convenient if we work around the fact that
20492 cp_parser_statement calls add_stmt. */
20495 cp_parser_begin_omp_structured_block (cp_parser *parser)
20497 unsigned save = parser->in_statement;
20499 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20500 This preserves the "not within loop or switch" style error messages
20501 for nonsense cases like
20507 if (parser->in_statement)
20508 parser->in_statement = IN_OMP_BLOCK;
20514 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20516 parser->in_statement = save;
20520 cp_parser_omp_structured_block (cp_parser *parser)
20522 tree stmt = begin_omp_structured_block ();
20523 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20525 cp_parser_statement (parser, NULL_TREE, false, NULL);
20527 cp_parser_end_omp_structured_block (parser, save);
20528 return finish_omp_structured_block (stmt);
20532 # pragma omp atomic new-line
20536 x binop= expr | x++ | ++x | x-- | --x
20538 +, *, -, /, &, ^, |, <<, >>
20540 where x is an lvalue expression with scalar type. */
20543 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20546 enum tree_code code;
20548 cp_parser_require_pragma_eol (parser, pragma_tok);
20550 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20552 switch (TREE_CODE (lhs))
20557 case PREINCREMENT_EXPR:
20558 case POSTINCREMENT_EXPR:
20559 lhs = TREE_OPERAND (lhs, 0);
20561 rhs = integer_one_node;
20564 case PREDECREMENT_EXPR:
20565 case POSTDECREMENT_EXPR:
20566 lhs = TREE_OPERAND (lhs, 0);
20568 rhs = integer_one_node;
20572 switch (cp_lexer_peek_token (parser->lexer)->type)
20578 code = TRUNC_DIV_EXPR;
20586 case CPP_LSHIFT_EQ:
20587 code = LSHIFT_EXPR;
20589 case CPP_RSHIFT_EQ:
20590 code = RSHIFT_EXPR;
20593 code = BIT_AND_EXPR;
20596 code = BIT_IOR_EXPR;
20599 code = BIT_XOR_EXPR;
20602 cp_parser_error (parser,
20603 "invalid operator for %<#pragma omp atomic%>");
20606 cp_lexer_consume_token (parser->lexer);
20608 rhs = cp_parser_expression (parser, false);
20609 if (rhs == error_mark_node)
20613 finish_omp_atomic (code, lhs, rhs);
20614 cp_parser_consume_semicolon_at_end_of_statement (parser);
20618 cp_parser_skip_to_end_of_block_or_statement (parser);
20623 # pragma omp barrier new-line */
20626 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20628 cp_parser_require_pragma_eol (parser, pragma_tok);
20629 finish_omp_barrier ();
20633 # pragma omp critical [(name)] new-line
20634 structured-block */
20637 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20639 tree stmt, name = NULL;
20641 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20643 cp_lexer_consume_token (parser->lexer);
20645 name = cp_parser_identifier (parser);
20647 if (name == error_mark_node
20648 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20649 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20650 /*or_comma=*/false,
20651 /*consume_paren=*/true);
20652 if (name == error_mark_node)
20655 cp_parser_require_pragma_eol (parser, pragma_tok);
20657 stmt = cp_parser_omp_structured_block (parser);
20658 return c_finish_omp_critical (stmt, name);
20662 # pragma omp flush flush-vars[opt] new-line
20665 ( variable-list ) */
20668 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20671 (void) cp_parser_omp_var_list (parser, 0, NULL);
20672 cp_parser_require_pragma_eol (parser, pragma_tok);
20674 finish_omp_flush ();
20677 /* Helper function, to parse omp for increment expression. */
20680 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20682 tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20688 cp_parser_skip_to_end_of_statement (parser);
20689 return error_mark_node;
20692 token = cp_lexer_peek_token (parser->lexer);
20693 op = binops_by_token [token->type].tree_type;
20702 cp_parser_skip_to_end_of_statement (parser);
20703 return error_mark_node;
20706 cp_lexer_consume_token (parser->lexer);
20707 rhs = cp_parser_binary_expression (parser, false,
20708 PREC_RELATIONAL_EXPRESSION);
20709 if (rhs == error_mark_node
20710 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20712 cp_parser_skip_to_end_of_statement (parser);
20713 return error_mark_node;
20716 return build2 (op, boolean_type_node, lhs, rhs);
20719 /* Helper function, to parse omp for increment expression. */
20722 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20724 cp_token *token = cp_lexer_peek_token (parser->lexer);
20730 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20732 op = (token->type == CPP_PLUS_PLUS
20733 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20734 cp_lexer_consume_token (parser->lexer);
20735 lhs = cp_parser_cast_expression (parser, false, false);
20737 return error_mark_node;
20738 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20741 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20743 return error_mark_node;
20745 token = cp_lexer_peek_token (parser->lexer);
20746 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20748 op = (token->type == CPP_PLUS_PLUS
20749 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20750 cp_lexer_consume_token (parser->lexer);
20751 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20754 op = cp_parser_assignment_operator_opt (parser);
20755 if (op == ERROR_MARK)
20756 return error_mark_node;
20758 if (op != NOP_EXPR)
20760 rhs = cp_parser_assignment_expression (parser, false);
20761 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20762 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20765 lhs = cp_parser_binary_expression (parser, false,
20766 PREC_ADDITIVE_EXPRESSION);
20767 token = cp_lexer_peek_token (parser->lexer);
20768 decl_first = lhs == decl;
20771 if (token->type != CPP_PLUS
20772 && token->type != CPP_MINUS)
20773 return error_mark_node;
20777 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20778 cp_lexer_consume_token (parser->lexer);
20779 rhs = cp_parser_binary_expression (parser, false,
20780 PREC_ADDITIVE_EXPRESSION);
20781 token = cp_lexer_peek_token (parser->lexer);
20782 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20784 if (lhs == NULL_TREE)
20786 if (op == PLUS_EXPR)
20789 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20792 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20793 NULL, tf_warning_or_error);
20796 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20800 if (rhs != decl || op == MINUS_EXPR)
20801 return error_mark_node;
20802 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20805 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20807 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20810 /* Parse the restricted form of the for statement allowed by OpenMP. */
20813 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20815 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20816 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20817 tree this_pre_body, cl;
20818 location_t loc_first;
20819 bool collapse_err = false;
20820 int i, collapse = 1, nbraces = 0;
20822 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20823 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20824 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20826 gcc_assert (collapse >= 1);
20828 declv = make_tree_vec (collapse);
20829 initv = make_tree_vec (collapse);
20830 condv = make_tree_vec (collapse);
20831 incrv = make_tree_vec (collapse);
20833 loc_first = cp_lexer_peek_token (parser->lexer)->location;
20835 for (i = 0; i < collapse; i++)
20837 int bracecount = 0;
20838 bool add_private_clause = false;
20841 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20843 cp_parser_error (parser, "for statement expected");
20846 loc = cp_lexer_consume_token (parser->lexer)->location;
20848 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20851 init = decl = real_decl = NULL;
20852 this_pre_body = push_stmt_list ();
20853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20855 cp_decl_specifier_seq type_specifiers;
20857 /* First, try to parse as an initialized declaration. See
20858 cp_parser_condition, from whence the bulk of this is copied. */
20860 cp_parser_parse_tentatively (parser);
20861 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20863 if (!cp_parser_error_occurred (parser))
20865 tree asm_specification, attributes;
20866 cp_declarator *declarator;
20868 declarator = cp_parser_declarator (parser,
20869 CP_PARSER_DECLARATOR_NAMED,
20870 /*ctor_dtor_or_conv_p=*/NULL,
20871 /*parenthesized_p=*/NULL,
20872 /*member_p=*/false);
20873 attributes = cp_parser_attributes_opt (parser);
20874 asm_specification = cp_parser_asm_specification_opt (parser);
20876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
20877 cp_parser_require (parser, CPP_EQ, "%<=%>");
20878 if (cp_parser_parse_definitely (parser))
20882 decl = start_decl (declarator, &type_specifiers,
20883 /*initialized_p=*/false, attributes,
20884 /*prefix_attributes=*/NULL_TREE,
20887 if (CLASS_TYPE_P (TREE_TYPE (decl))
20888 || type_dependent_expression_p (decl))
20890 bool is_direct_init, is_non_constant_init;
20892 init = cp_parser_initializer (parser,
20894 &is_non_constant_init);
20896 cp_finish_decl (decl, init, !is_non_constant_init,
20898 LOOKUP_ONLYCONVERTING);
20899 if (CLASS_TYPE_P (TREE_TYPE (decl)))
20902 = tree_cons (NULL, this_pre_body, for_block);
20906 init = pop_stmt_list (this_pre_body);
20907 this_pre_body = NULL_TREE;
20911 cp_parser_require (parser, CPP_EQ, "%<=%>");
20912 init = cp_parser_assignment_expression (parser, false);
20914 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20915 init = error_mark_node;
20917 cp_finish_decl (decl, NULL_TREE,
20918 /*init_const_expr_p=*/false,
20920 LOOKUP_ONLYCONVERTING);
20924 pop_scope (pushed_scope);
20928 cp_parser_abort_tentative_parse (parser);
20930 /* If parsing as an initialized declaration failed, try again as
20931 a simple expression. */
20935 cp_parser_parse_tentatively (parser);
20936 decl = cp_parser_primary_expression (parser, false, false,
20938 if (!cp_parser_error_occurred (parser)
20941 && CLASS_TYPE_P (TREE_TYPE (decl)))
20945 cp_parser_parse_definitely (parser);
20946 cp_parser_require (parser, CPP_EQ, "%<=%>");
20947 rhs = cp_parser_assignment_expression (parser, false);
20948 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
20950 tf_warning_or_error));
20951 add_private_clause = true;
20956 cp_parser_abort_tentative_parse (parser);
20957 init = cp_parser_expression (parser, false);
20960 if (TREE_CODE (init) == MODIFY_EXPR
20961 || TREE_CODE (init) == MODOP_EXPR)
20962 real_decl = TREE_OPERAND (init, 0);
20967 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20970 this_pre_body = pop_stmt_list (this_pre_body);
20974 pre_body = push_stmt_list ();
20976 add_stmt (this_pre_body);
20977 pre_body = pop_stmt_list (pre_body);
20980 pre_body = this_pre_body;
20985 if (par_clauses != NULL && real_decl != NULL_TREE)
20988 for (c = par_clauses; *c ; )
20989 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
20990 && OMP_CLAUSE_DECL (*c) == real_decl)
20992 error ("%Hiteration variable %qD should not be firstprivate",
20994 *c = OMP_CLAUSE_CHAIN (*c);
20996 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
20997 && OMP_CLAUSE_DECL (*c) == real_decl)
20999 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21000 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21001 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21002 OMP_CLAUSE_DECL (l) = real_decl;
21003 OMP_CLAUSE_CHAIN (l) = clauses;
21004 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21006 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21007 CP_OMP_CLAUSE_INFO (*c) = NULL;
21008 add_private_clause = false;
21012 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21013 && OMP_CLAUSE_DECL (*c) == real_decl)
21014 add_private_clause = false;
21015 c = &OMP_CLAUSE_CHAIN (*c);
21019 if (add_private_clause)
21022 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21024 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21025 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21026 && OMP_CLAUSE_DECL (c) == decl)
21028 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21029 && OMP_CLAUSE_DECL (c) == decl)
21030 error ("%Hiteration variable %qD should not be firstprivate",
21032 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21033 && OMP_CLAUSE_DECL (c) == decl)
21034 error ("%Hiteration variable %qD should not be reduction",
21039 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21040 OMP_CLAUSE_DECL (c) = decl;
21041 c = finish_omp_clauses (c);
21044 OMP_CLAUSE_CHAIN (c) = clauses;
21051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21053 /* If decl is an iterator, preserve LHS and RHS of the relational
21054 expr until finish_omp_for. */
21056 && (type_dependent_expression_p (decl)
21057 || CLASS_TYPE_P (TREE_TYPE (decl))))
21058 cond = cp_parser_omp_for_cond (parser, decl);
21060 cond = cp_parser_condition (parser);
21062 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21067 /* If decl is an iterator, preserve the operator on decl
21068 until finish_omp_for. */
21070 && (type_dependent_expression_p (decl)
21071 || CLASS_TYPE_P (TREE_TYPE (decl))))
21072 incr = cp_parser_omp_for_incr (parser, decl);
21074 incr = cp_parser_expression (parser, false);
21077 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21078 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21079 /*or_comma=*/false,
21080 /*consume_paren=*/true);
21082 TREE_VEC_ELT (declv, i) = decl;
21083 TREE_VEC_ELT (initv, i) = init;
21084 TREE_VEC_ELT (condv, i) = cond;
21085 TREE_VEC_ELT (incrv, i) = incr;
21087 if (i == collapse - 1)
21090 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21091 in between the collapsed for loops to be still considered perfectly
21092 nested. Hopefully the final version clarifies this.
21093 For now handle (multiple) {'s and empty statements. */
21094 cp_parser_parse_tentatively (parser);
21097 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21099 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21101 cp_lexer_consume_token (parser->lexer);
21104 else if (bracecount
21105 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21106 cp_lexer_consume_token (parser->lexer);
21109 loc = cp_lexer_peek_token (parser->lexer)->location;
21110 error ("%Hnot enough collapsed for loops", &loc);
21111 collapse_err = true;
21112 cp_parser_abort_tentative_parse (parser);
21121 cp_parser_parse_definitely (parser);
21122 nbraces += bracecount;
21126 /* Note that we saved the original contents of this flag when we entered
21127 the structured block, and so we don't need to re-save it here. */
21128 parser->in_statement = IN_OMP_FOR;
21130 /* Note that the grammar doesn't call for a structured block here,
21131 though the loop as a whole is a structured block. */
21132 body = push_stmt_list ();
21133 cp_parser_statement (parser, NULL_TREE, false, NULL);
21134 body = pop_stmt_list (body);
21136 if (declv == NULL_TREE)
21139 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21140 pre_body, clauses);
21144 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21146 cp_lexer_consume_token (parser->lexer);
21149 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21150 cp_lexer_consume_token (parser->lexer);
21155 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21156 error ("%Hcollapsed loops not perfectly nested", &loc);
21158 collapse_err = true;
21159 cp_parser_statement_seq_opt (parser, NULL);
21160 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21166 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21167 for_block = TREE_CHAIN (for_block);
21174 #pragma omp for for-clause[optseq] new-line
21177 #define OMP_FOR_CLAUSE_MASK \
21178 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21179 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21180 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21181 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21182 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21183 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21184 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21185 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21188 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21190 tree clauses, sb, ret;
21193 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21194 "#pragma omp for", pragma_tok);
21196 sb = begin_omp_structured_block ();
21197 save = cp_parser_begin_omp_structured_block (parser);
21199 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21201 cp_parser_end_omp_structured_block (parser, save);
21202 add_stmt (finish_omp_structured_block (sb));
21208 # pragma omp master new-line
21209 structured-block */
21212 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21214 cp_parser_require_pragma_eol (parser, pragma_tok);
21215 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21219 # pragma omp ordered new-line
21220 structured-block */
21223 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21225 cp_parser_require_pragma_eol (parser, pragma_tok);
21226 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21232 { section-sequence }
21235 section-directive[opt] structured-block
21236 section-sequence section-directive structured-block */
21239 cp_parser_omp_sections_scope (cp_parser *parser)
21241 tree stmt, substmt;
21242 bool error_suppress = false;
21245 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21248 stmt = push_stmt_list ();
21250 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21254 substmt = begin_omp_structured_block ();
21255 save = cp_parser_begin_omp_structured_block (parser);
21259 cp_parser_statement (parser, NULL_TREE, false, NULL);
21261 tok = cp_lexer_peek_token (parser->lexer);
21262 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21264 if (tok->type == CPP_CLOSE_BRACE)
21266 if (tok->type == CPP_EOF)
21270 cp_parser_end_omp_structured_block (parser, save);
21271 substmt = finish_omp_structured_block (substmt);
21272 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21273 add_stmt (substmt);
21278 tok = cp_lexer_peek_token (parser->lexer);
21279 if (tok->type == CPP_CLOSE_BRACE)
21281 if (tok->type == CPP_EOF)
21284 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21286 cp_lexer_consume_token (parser->lexer);
21287 cp_parser_require_pragma_eol (parser, tok);
21288 error_suppress = false;
21290 else if (!error_suppress)
21292 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21293 error_suppress = true;
21296 substmt = cp_parser_omp_structured_block (parser);
21297 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21298 add_stmt (substmt);
21300 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21302 substmt = pop_stmt_list (stmt);
21304 stmt = make_node (OMP_SECTIONS);
21305 TREE_TYPE (stmt) = void_type_node;
21306 OMP_SECTIONS_BODY (stmt) = substmt;
21313 # pragma omp sections sections-clause[optseq] newline
21316 #define OMP_SECTIONS_CLAUSE_MASK \
21317 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21318 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21319 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21320 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21321 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21324 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21328 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21329 "#pragma omp sections", pragma_tok);
21331 ret = cp_parser_omp_sections_scope (parser);
21333 OMP_SECTIONS_CLAUSES (ret) = clauses;
21339 # pragma parallel parallel-clause new-line
21340 # pragma parallel for parallel-for-clause new-line
21341 # pragma parallel sections parallel-sections-clause new-line */
21343 #define OMP_PARALLEL_CLAUSE_MASK \
21344 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21345 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21346 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21347 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21348 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21349 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21350 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21351 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21354 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21356 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21357 const char *p_name = "#pragma omp parallel";
21358 tree stmt, clauses, par_clause, ws_clause, block;
21359 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21364 cp_lexer_consume_token (parser->lexer);
21365 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21366 p_name = "#pragma omp parallel for";
21367 mask |= OMP_FOR_CLAUSE_MASK;
21368 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21370 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21372 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21373 const char *p = IDENTIFIER_POINTER (id);
21374 if (strcmp (p, "sections") == 0)
21376 cp_lexer_consume_token (parser->lexer);
21377 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21378 p_name = "#pragma omp parallel sections";
21379 mask |= OMP_SECTIONS_CLAUSE_MASK;
21380 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21384 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21385 block = begin_omp_parallel ();
21386 save = cp_parser_begin_omp_structured_block (parser);
21390 case PRAGMA_OMP_PARALLEL:
21391 cp_parser_statement (parser, NULL_TREE, false, NULL);
21392 par_clause = clauses;
21395 case PRAGMA_OMP_PARALLEL_FOR:
21396 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21397 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21400 case PRAGMA_OMP_PARALLEL_SECTIONS:
21401 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21402 stmt = cp_parser_omp_sections_scope (parser);
21404 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21408 gcc_unreachable ();
21411 cp_parser_end_omp_structured_block (parser, save);
21412 stmt = finish_omp_parallel (par_clause, block);
21413 if (p_kind != PRAGMA_OMP_PARALLEL)
21414 OMP_PARALLEL_COMBINED (stmt) = 1;
21419 # pragma omp single single-clause[optseq] new-line
21420 structured-block */
21422 #define OMP_SINGLE_CLAUSE_MASK \
21423 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21424 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21425 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21426 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21429 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21431 tree stmt = make_node (OMP_SINGLE);
21432 TREE_TYPE (stmt) = void_type_node;
21434 OMP_SINGLE_CLAUSES (stmt)
21435 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21436 "#pragma omp single", pragma_tok);
21437 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21439 return add_stmt (stmt);
21443 # pragma omp task task-clause[optseq] new-line
21444 structured-block */
21446 #define OMP_TASK_CLAUSE_MASK \
21447 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21448 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21449 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21450 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21451 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21452 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21455 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21457 tree clauses, block;
21460 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21461 "#pragma omp task", pragma_tok);
21462 block = begin_omp_task ();
21463 save = cp_parser_begin_omp_structured_block (parser);
21464 cp_parser_statement (parser, NULL_TREE, false, NULL);
21465 cp_parser_end_omp_structured_block (parser, save);
21466 return finish_omp_task (clauses, block);
21470 # pragma omp taskwait new-line */
21473 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21475 cp_parser_require_pragma_eol (parser, pragma_tok);
21476 finish_omp_taskwait ();
21480 # pragma omp threadprivate (variable-list) */
21483 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21487 vars = cp_parser_omp_var_list (parser, 0, NULL);
21488 cp_parser_require_pragma_eol (parser, pragma_tok);
21490 finish_omp_threadprivate (vars);
21493 /* Main entry point to OpenMP statement pragmas. */
21496 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21500 switch (pragma_tok->pragma_kind)
21502 case PRAGMA_OMP_ATOMIC:
21503 cp_parser_omp_atomic (parser, pragma_tok);
21505 case PRAGMA_OMP_CRITICAL:
21506 stmt = cp_parser_omp_critical (parser, pragma_tok);
21508 case PRAGMA_OMP_FOR:
21509 stmt = cp_parser_omp_for (parser, pragma_tok);
21511 case PRAGMA_OMP_MASTER:
21512 stmt = cp_parser_omp_master (parser, pragma_tok);
21514 case PRAGMA_OMP_ORDERED:
21515 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21517 case PRAGMA_OMP_PARALLEL:
21518 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21520 case PRAGMA_OMP_SECTIONS:
21521 stmt = cp_parser_omp_sections (parser, pragma_tok);
21523 case PRAGMA_OMP_SINGLE:
21524 stmt = cp_parser_omp_single (parser, pragma_tok);
21526 case PRAGMA_OMP_TASK:
21527 stmt = cp_parser_omp_task (parser, pragma_tok);
21530 gcc_unreachable ();
21534 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21539 static GTY (()) cp_parser *the_parser;
21542 /* Special handling for the first token or line in the file. The first
21543 thing in the file might be #pragma GCC pch_preprocess, which loads a
21544 PCH file, which is a GC collection point. So we need to handle this
21545 first pragma without benefit of an existing lexer structure.
21547 Always returns one token to the caller in *FIRST_TOKEN. This is
21548 either the true first token of the file, or the first token after
21549 the initial pragma. */
21552 cp_parser_initial_pragma (cp_token *first_token)
21556 cp_lexer_get_preprocessor_token (NULL, first_token);
21557 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21560 cp_lexer_get_preprocessor_token (NULL, first_token);
21561 if (first_token->type == CPP_STRING)
21563 name = first_token->u.value;
21565 cp_lexer_get_preprocessor_token (NULL, first_token);
21566 if (first_token->type != CPP_PRAGMA_EOL)
21567 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21568 &first_token->location);
21571 error ("%Hexpected string literal", &first_token->location);
21573 /* Skip to the end of the pragma. */
21574 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21575 cp_lexer_get_preprocessor_token (NULL, first_token);
21577 /* Now actually load the PCH file. */
21579 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21581 /* Read one more token to return to our caller. We have to do this
21582 after reading the PCH file in, since its pointers have to be
21584 cp_lexer_get_preprocessor_token (NULL, first_token);
21587 /* Normal parsing of a pragma token. Here we can (and must) use the
21591 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21593 cp_token *pragma_tok;
21596 pragma_tok = cp_lexer_consume_token (parser->lexer);
21597 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21598 parser->lexer->in_pragma = true;
21600 id = pragma_tok->pragma_kind;
21603 case PRAGMA_GCC_PCH_PREPROCESS:
21604 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21605 &pragma_tok->location);
21608 case PRAGMA_OMP_BARRIER:
21611 case pragma_compound:
21612 cp_parser_omp_barrier (parser, pragma_tok);
21615 error ("%H%<#pragma omp barrier%> may only be "
21616 "used in compound statements", &pragma_tok->location);
21623 case PRAGMA_OMP_FLUSH:
21626 case pragma_compound:
21627 cp_parser_omp_flush (parser, pragma_tok);
21630 error ("%H%<#pragma omp flush%> may only be "
21631 "used in compound statements", &pragma_tok->location);
21638 case PRAGMA_OMP_TASKWAIT:
21641 case pragma_compound:
21642 cp_parser_omp_taskwait (parser, pragma_tok);
21645 error ("%H%<#pragma omp taskwait%> may only be "
21646 "used in compound statements",
21647 &pragma_tok->location);
21654 case PRAGMA_OMP_THREADPRIVATE:
21655 cp_parser_omp_threadprivate (parser, pragma_tok);
21658 case PRAGMA_OMP_ATOMIC:
21659 case PRAGMA_OMP_CRITICAL:
21660 case PRAGMA_OMP_FOR:
21661 case PRAGMA_OMP_MASTER:
21662 case PRAGMA_OMP_ORDERED:
21663 case PRAGMA_OMP_PARALLEL:
21664 case PRAGMA_OMP_SECTIONS:
21665 case PRAGMA_OMP_SINGLE:
21666 case PRAGMA_OMP_TASK:
21667 if (context == pragma_external)
21669 cp_parser_omp_construct (parser, pragma_tok);
21672 case PRAGMA_OMP_SECTION:
21673 error ("%H%<#pragma omp section%> may only be used in "
21674 "%<#pragma omp sections%> construct", &pragma_tok->location);
21678 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21679 c_invoke_pragma_handler (id);
21683 cp_parser_error (parser, "expected declaration specifiers");
21687 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21691 /* The interface the pragma parsers have to the lexer. */
21694 pragma_lex (tree *value)
21697 enum cpp_ttype ret;
21699 tok = cp_lexer_peek_token (the_parser->lexer);
21702 *value = tok->u.value;
21704 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21706 else if (ret == CPP_STRING)
21707 *value = cp_parser_string_literal (the_parser, false, false);
21710 cp_lexer_consume_token (the_parser->lexer);
21711 if (ret == CPP_KEYWORD)
21719 /* External interface. */
21721 /* Parse one entire translation unit. */
21724 c_parse_file (void)
21726 bool error_occurred;
21727 static bool already_called = false;
21729 if (already_called)
21731 sorry ("inter-module optimizations not implemented for C++");
21734 already_called = true;
21736 the_parser = cp_parser_new ();
21737 push_deferring_access_checks (flag_access_control
21738 ? dk_no_deferred : dk_no_check);
21739 error_occurred = cp_parser_translation_unit (the_parser);
21743 #include "gt-cp-parser.h"