2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 /* The location at which this token was found. */
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
102 /* The cp_lexer structure represents the C++ lexer. It is responsible
103 for managing the token stream from the preprocessor and supplying
104 it to the parser. Tokens are never added to the cp_lexer after
107 typedef struct cp_lexer GTY (())
109 /* The memory allocated for the buffer. NULL if this lexer does not
110 own the token buffer. */
111 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112 /* If the lexer owns the buffer, this is the number of tokens in the
114 size_t buffer_length;
116 /* A pointer just past the last available token. The tokens
117 in this lexer are [buffer, last_token). */
118 cp_token_position GTY ((skip)) last_token;
120 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
121 no more available tokens. */
122 cp_token_position GTY ((skip)) next_token;
124 /* A stack indicating positions at which cp_lexer_save_tokens was
125 called. The top entry is the most recent position at which we
126 began saving tokens. If the stack is non-empty, we are saving
128 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
130 /* The next lexer in a linked list of lexers. */
131 struct cp_lexer *next;
133 /* True if we should output debugging information. */
136 /* True if we're in the context of parsing a pragma, and should not
137 increment past the end-of-line marker. */
141 /* cp_token_cache is a range of tokens. There is no need to represent
142 allocate heap memory for it, since tokens are never removed from the
143 lexer's array. There is also no need for the GC to walk through
144 a cp_token_cache, since everything in here is referenced through
147 typedef struct cp_token_cache GTY(())
149 /* The beginning of the token range. */
150 cp_token * GTY((skip)) first;
152 /* Points immediately after the last token in the range. */
153 cp_token * GTY ((skip)) last;
158 static cp_lexer *cp_lexer_new_main
160 static cp_lexer *cp_lexer_new_from_tokens
161 (cp_token_cache *tokens);
162 static void cp_lexer_destroy
164 static int cp_lexer_saving_tokens
166 static cp_token_position cp_lexer_token_position
168 static cp_token *cp_lexer_token_at
169 (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171 (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
174 static cp_token *cp_lexer_peek_nth_token
175 (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177 (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181 (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
184 static void cp_lexer_purge_token
186 static void cp_lexer_purge_tokens_after
187 (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
190 static void cp_lexer_commit_tokens
192 static void cp_lexer_rollback_tokens
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196 (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
199 static void cp_lexer_start_debugging
200 (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205 about passing NULL to functions that require non-NULL arguments
206 (fputs, fprintf). It will never be used, so all we need is a value
207 of the right type that's guaranteed not to be NULL. */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
213 static cp_token_cache *cp_token_cache_new
214 (cp_token *, cp_token *);
216 static void cp_parser_initial_pragma
219 /* Manifest constants. */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
223 /* A token type for keywords, as opposed to ordinary identifiers. */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
226 /* A token type for template-ids. If a template-id is processed while
227 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228 the value of the CPP_TEMPLATE_ID is whatever was returned by
229 cp_parser_template_id. */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
232 /* A token type for nested-name-specifiers. If a
233 nested-name-specifier is processed while parsing tentatively, it is
234 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236 cp_parser_nested_name_specifier_opt. */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
239 /* A token type for tokens that are not tokens at all; these are used
240 to represent slots in the array where there used to be a token
241 that has now been deleted. */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
244 /* The number of token types, including C++-specific ones. */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written. */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
313 cpp_get_options (parse_in)->client_diagnostic = true;
314 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
348 /* Frees all resources associated with LEXER. */
351 cp_lexer_destroy (cp_lexer *lexer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
364 cp_lexer_debugging_p (cp_lexer *lexer)
366 return lexer->debugging_p;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
376 return lexer->next_token - previous_p;
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
385 /* nonzero if we are presently saving tokens. */
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
401 static int is_extern_c = 0;
403 /* Get a new token from the preprocessor. */
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
420 if (C_IS_RESERVED_WORD (token->u.value))
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
426 /* Update the value. Some keywords are mapped to particular
427 entities, rather than simply having the value of the
428 corresponding IDENTIFIER_NODE. For example, `__const' is
429 mapped to `const'. */
430 token->u.value = ridpointers[token->keyword];
434 if (warn_cxx0x_compat
435 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
438 /* Warn about the C++0x keyword (but still treat it as
440 warning (OPT_Wc__0x_compat,
441 "identifier %<%s%> will become a keyword in C++0x",
442 IDENTIFIER_POINTER (token->u.value));
444 /* Clear out the C_RID_CODE so we don't warn about this
445 particular identifier-turned-keyword again. */
446 C_SET_RID_CODE (token->u.value, RID_MAX);
449 token->ambiguous_p = false;
450 token->keyword = RID_MAX;
453 /* Handle Objective-C++ keywords. */
454 else if (token->type == CPP_AT_NAME)
456 token->type = CPP_KEYWORD;
457 switch (C_RID_CODE (token->u.value))
459 /* Map 'class' to '@class', 'private' to '@private', etc. */
460 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464 case RID_THROW: token->keyword = RID_AT_THROW; break;
465 case RID_TRY: token->keyword = RID_AT_TRY; break;
466 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467 default: token->keyword = C_RID_CODE (token->u.value);
470 else if (token->type == CPP_PRAGMA)
472 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
473 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474 token->u.value = NULL_TREE;
478 /* Update the globals input_location and the input file stack from TOKEN. */
480 cp_lexer_set_source_position_from_token (cp_token *token)
482 if (token->type != CPP_EOF)
484 input_location = token->location;
488 /* Return a pointer to the next token in the token stream, but do not
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
494 if (cp_lexer_debugging_p (lexer))
496 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498 putc ('\n', cp_lexer_debug_stream);
500 return lexer->next_token;
503 /* Return true if the next token has the indicated TYPE. */
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
508 return cp_lexer_peek_token (lexer)->type == type;
511 /* Return true if the next token does not have the indicated TYPE. */
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
516 return !cp_lexer_next_token_is (lexer, type);
519 /* Return true if the next token is the indicated KEYWORD. */
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
524 return cp_lexer_peek_token (lexer)->keyword == keyword;
527 /* Return true if the next token is not the indicated KEYWORD. */
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
532 return cp_lexer_peek_token (lexer)->keyword != keyword;
535 /* Return true if the next token is a keyword for a decl-specifier. */
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
542 token = cp_lexer_peek_token (lexer);
543 switch (token->keyword)
545 /* auto specifier: storage-class-specifier in C++,
546 simple-type-specifier in C++0x. */
548 /* Storage classes. */
554 /* Elaborated type specifiers. */
560 /* Simple type specifiers. */
574 /* GNU extensions. */
577 /* C++0x extensions. */
586 /* Return a pointer to the Nth token in the token stream. If N is 1,
587 then this is precisely equivalent to cp_lexer_peek_token (except
588 that it is not inline). One would like to disallow that case, but
589 there is one case (cp_parser_nth_token_starts_template_id) where
590 the caller passes a variable for N and it might be 1. */
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
597 /* N is 1-based, not zero-based. */
600 if (cp_lexer_debugging_p (lexer))
601 fprintf (cp_lexer_debug_stream,
602 "cp_lexer: peeking ahead %ld at token: ", (long)n);
605 token = lexer->next_token;
606 gcc_assert (!n || token != &eof_token);
610 if (token == lexer->last_token)
616 if (token->type != CPP_PURGED)
620 if (cp_lexer_debugging_p (lexer))
622 cp_lexer_print_token (cp_lexer_debug_stream, token);
623 putc ('\n', cp_lexer_debug_stream);
629 /* Return the next token, and advance the lexer's next_token pointer
630 to point to the next non-purged token. */
633 cp_lexer_consume_token (cp_lexer* lexer)
635 cp_token *token = lexer->next_token;
637 gcc_assert (token != &eof_token);
638 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
643 if (lexer->next_token == lexer->last_token)
645 lexer->next_token = &eof_token;
650 while (lexer->next_token->type == CPP_PURGED);
652 cp_lexer_set_source_position_from_token (token);
654 /* Provide debugging output. */
655 if (cp_lexer_debugging_p (lexer))
657 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658 cp_lexer_print_token (cp_lexer_debug_stream, token);
659 putc ('\n', cp_lexer_debug_stream);
665 /* Permanently remove the next token from the token stream, and
666 advance the next_token pointer to refer to the next non-purged
670 cp_lexer_purge_token (cp_lexer *lexer)
672 cp_token *tok = lexer->next_token;
674 gcc_assert (tok != &eof_token);
675 tok->type = CPP_PURGED;
676 tok->location = UNKNOWN_LOCATION;
677 tok->u.value = NULL_TREE;
678 tok->keyword = RID_MAX;
683 if (tok == lexer->last_token)
689 while (tok->type == CPP_PURGED);
690 lexer->next_token = tok;
693 /* Permanently remove all tokens after TOK, up to, but not
694 including, the token that will be returned next by
695 cp_lexer_peek_token. */
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
700 cp_token *peek = lexer->next_token;
702 if (peek == &eof_token)
703 peek = lexer->last_token;
705 gcc_assert (tok < peek);
707 for ( tok += 1; tok != peek; tok += 1)
709 tok->type = CPP_PURGED;
710 tok->location = UNKNOWN_LOCATION;
711 tok->u.value = NULL_TREE;
712 tok->keyword = RID_MAX;
716 /* Begin saving tokens. All tokens consumed after this point will be
720 cp_lexer_save_tokens (cp_lexer* lexer)
722 /* Provide debugging output. */
723 if (cp_lexer_debugging_p (lexer))
724 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
726 VEC_safe_push (cp_token_position, heap,
727 lexer->saved_tokens, lexer->next_token);
730 /* Commit to the portion of the token stream most recently saved. */
733 cp_lexer_commit_tokens (cp_lexer* lexer)
735 /* Provide debugging output. */
736 if (cp_lexer_debugging_p (lexer))
737 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
739 VEC_pop (cp_token_position, lexer->saved_tokens);
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743 to the token stream. Stop saving tokens. */
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
748 /* Provide debugging output. */
749 if (cp_lexer_debugging_p (lexer))
750 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
752 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
755 /* Print a representation of the TOKEN on the STREAM. */
757 #ifdef ENABLE_CHECKING
760 cp_lexer_print_token (FILE * stream, cp_token *token)
762 /* We don't use cpp_type2name here because the parser defines
763 a few tokens of its own. */
764 static const char *const token_names[] = {
765 /* cpplib-defined token types */
771 /* C++ parser token types - see "Manifest constants", above. */
774 "NESTED_NAME_SPECIFIER",
778 /* If we have a name for the token, print it out. Otherwise, we
779 simply give the numeric code. */
780 gcc_assert (token->type < ARRAY_SIZE(token_names));
781 fputs (token_names[token->type], stream);
783 /* For some tokens, print the associated data. */
787 /* Some keywords have a value that is not an IDENTIFIER_NODE.
788 For example, `struct' is mapped to an INTEGER_CST. */
789 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791 /* else fall through */
793 fputs (IDENTIFIER_POINTER (token->u.value), stream);
800 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
808 /* Start emitting debugging information. */
811 cp_lexer_start_debugging (cp_lexer* lexer)
813 lexer->debugging_p = true;
816 /* Stop emitting debugging information. */
819 cp_lexer_stop_debugging (cp_lexer* lexer)
821 lexer->debugging_p = false;
824 #endif /* ENABLE_CHECKING */
826 /* Create a new cp_token_cache, representing a range of tokens. */
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
831 cp_token_cache *cache = GGC_NEW (cp_token_cache);
832 cache->first = first;
838 /* Decl-specifiers. */
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
845 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
850 /* Nothing other than the parser should be creating declarators;
851 declarators are a semi-syntactic representation of C++ entities.
852 Other parts of the front end that need to create entities (like
853 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
855 static cp_declarator *make_call_declarator
856 (cp_declarator *, tree, cp_cv_quals, tree, tree);
857 static cp_declarator *make_array_declarator
858 (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860 (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862 (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864 (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866 (cp_cv_quals, tree, cp_declarator *);
868 /* An erroneous declarator. */
869 static cp_declarator *cp_error_declarator;
871 /* The obstack on which declarators and related data structures are
873 static struct obstack declarator_obstack;
875 /* Alloc BYTES from the declarator memory pool. */
878 alloc_declarator (size_t bytes)
880 return obstack_alloc (&declarator_obstack, bytes);
883 /* Allocate a declarator of the indicated KIND. Clear fields that are
884 common to all declarators. */
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
889 cp_declarator *declarator;
891 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892 declarator->kind = kind;
893 declarator->attributes = NULL_TREE;
894 declarator->declarator = NULL;
895 declarator->parameter_pack_p = false;
900 /* Make a declarator for a generalized identifier. If
901 QUALIFYING_SCOPE is non-NULL, the identifier is
902 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903 UNQUALIFIED_NAME. SFK indicates the kind of special function this
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908 special_function_kind sfk)
910 cp_declarator *declarator;
912 /* It is valid to write:
914 class C { void f(); };
918 The standard is not clear about whether `typedef const C D' is
919 legal; as of 2002-09-15 the committee is considering that
920 question. EDG 3.0 allows that syntax. Therefore, we do as
922 if (qualifying_scope && TYPE_P (qualifying_scope))
923 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
925 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
929 declarator = make_declarator (cdk_id);
930 declarator->u.id.qualifying_scope = qualifying_scope;
931 declarator->u.id.unqualified_name = unqualified_name;
932 declarator->u.id.sfk = sfk;
937 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
938 of modifiers such as const or volatile to apply to the pointer
939 type, represented as identifiers. */
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
944 cp_declarator *declarator;
946 declarator = make_declarator (cdk_pointer);
947 declarator->declarator = target;
948 declarator->u.pointer.qualifiers = cv_qualifiers;
949 declarator->u.pointer.class_type = NULL_TREE;
952 declarator->parameter_pack_p = target->parameter_pack_p;
953 target->parameter_pack_p = false;
956 declarator->parameter_pack_p = false;
961 /* Like make_pointer_declarator -- but for references. */
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
967 cp_declarator *declarator;
969 declarator = make_declarator (cdk_reference);
970 declarator->declarator = target;
971 declarator->u.reference.qualifiers = cv_qualifiers;
972 declarator->u.reference.rvalue_ref = rvalue_ref;
975 declarator->parameter_pack_p = target->parameter_pack_p;
976 target->parameter_pack_p = false;
979 declarator->parameter_pack_p = false;
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985 member of CLASS_TYPE. */
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989 cp_declarator *pointee)
991 cp_declarator *declarator;
993 declarator = make_declarator (cdk_ptrmem);
994 declarator->declarator = pointee;
995 declarator->u.pointer.qualifiers = cv_qualifiers;
996 declarator->u.pointer.class_type = class_type;
1000 declarator->parameter_pack_p = pointee->parameter_pack_p;
1001 pointee->parameter_pack_p = false;
1004 declarator->parameter_pack_p = false;
1009 /* Make a declarator for the function given by TARGET, with the
1010 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1011 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1012 indicates what exceptions can be thrown. */
1015 make_call_declarator (cp_declarator *target,
1017 cp_cv_quals cv_qualifiers,
1018 tree exception_specification,
1019 tree late_return_type)
1021 cp_declarator *declarator;
1023 declarator = make_declarator (cdk_function);
1024 declarator->declarator = target;
1025 declarator->u.function.parameters = parms;
1026 declarator->u.function.qualifiers = cv_qualifiers;
1027 declarator->u.function.exception_specification = exception_specification;
1028 declarator->u.function.late_return_type = late_return_type;
1031 declarator->parameter_pack_p = target->parameter_pack_p;
1032 target->parameter_pack_p = false;
1035 declarator->parameter_pack_p = false;
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041 defined by ELEMENT. */
1044 make_array_declarator (cp_declarator *element, tree bounds)
1046 cp_declarator *declarator;
1048 declarator = make_declarator (cdk_array);
1049 declarator->declarator = element;
1050 declarator->u.array.bounds = bounds;
1053 declarator->parameter_pack_p = element->parameter_pack_p;
1054 element->parameter_pack_p = false;
1057 declarator->parameter_pack_p = false;
1062 /* Determine whether the declarator we've seen so far can be a
1063 parameter pack, when followed by an ellipsis. */
1065 declarator_can_be_parameter_pack (cp_declarator *declarator)
1067 /* Search for a declarator name, or any other declarator that goes
1068 after the point where the ellipsis could appear in a parameter
1069 pack. If we find any of these, then this declarator can not be
1070 made into a parameter pack. */
1072 while (declarator && !found)
1074 switch ((int)declarator->kind)
1085 declarator = declarator->declarator;
1093 cp_parameter_declarator *no_parameters;
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096 DECLARATOR and DEFAULT_ARGUMENT. */
1098 cp_parameter_declarator *
1099 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1100 cp_declarator *declarator,
1101 tree default_argument)
1103 cp_parameter_declarator *parameter;
1105 parameter = ((cp_parameter_declarator *)
1106 alloc_declarator (sizeof (cp_parameter_declarator)));
1107 parameter->next = NULL;
1108 if (decl_specifiers)
1109 parameter->decl_specifiers = *decl_specifiers;
1111 clear_decl_specs (¶meter->decl_specifiers);
1112 parameter->declarator = declarator;
1113 parameter->default_argument = default_argument;
1114 parameter->ellipsis_p = false;
1119 /* Returns true iff DECLARATOR is a declaration for a function. */
1122 function_declarator_p (const cp_declarator *declarator)
1126 if (declarator->kind == cdk_function
1127 && declarator->declarator->kind == cdk_id)
1129 if (declarator->kind == cdk_id
1130 || declarator->kind == cdk_error)
1132 declarator = declarator->declarator;
1142 A cp_parser parses the token stream as specified by the C++
1143 grammar. Its job is purely parsing, not semantic analysis. For
1144 example, the parser breaks the token stream into declarators,
1145 expressions, statements, and other similar syntactic constructs.
1146 It does not check that the types of the expressions on either side
1147 of an assignment-statement are compatible, or that a function is
1148 not declared with a parameter of type `void'.
1150 The parser invokes routines elsewhere in the compiler to perform
1151 semantic analysis and to build up the abstract syntax tree for the
1154 The parser (and the template instantiation code, which is, in a
1155 way, a close relative of parsing) are the only parts of the
1156 compiler that should be calling push_scope and pop_scope, or
1157 related functions. The parser (and template instantiation code)
1158 keeps track of what scope is presently active; everything else
1159 should simply honor that. (The code that generates static
1160 initializers may also need to set the scope, in order to check
1161 access control correctly when emitting the initializers.)
1166 The parser is of the standard recursive-descent variety. Upcoming
1167 tokens in the token stream are examined in order to determine which
1168 production to use when parsing a non-terminal. Some C++ constructs
1169 require arbitrary look ahead to disambiguate. For example, it is
1170 impossible, in the general case, to tell whether a statement is an
1171 expression or declaration without scanning the entire statement.
1172 Therefore, the parser is capable of "parsing tentatively." When the
1173 parser is not sure what construct comes next, it enters this mode.
1174 Then, while we attempt to parse the construct, the parser queues up
1175 error messages, rather than issuing them immediately, and saves the
1176 tokens it consumes. If the construct is parsed successfully, the
1177 parser "commits", i.e., it issues any queued error messages and
1178 the tokens that were being preserved are permanently discarded.
1179 If, however, the construct is not parsed successfully, the parser
1180 rolls back its state completely so that it can resume parsing using
1181 a different alternative.
1186 The performance of the parser could probably be improved substantially.
1187 We could often eliminate the need to parse tentatively by looking ahead
1188 a little bit. In some places, this approach might not entirely eliminate
1189 the need to parse tentatively, but it might still speed up the average
1192 /* Flags that are passed to some parsing functions. These values can
1193 be bitwise-ored together. */
1195 typedef enum cp_parser_flags
1198 CP_PARSER_FLAGS_NONE = 0x0,
1199 /* The construct is optional. If it is not present, then no error
1200 should be issued. */
1201 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1202 /* When parsing a type-specifier, do not allow user-defined types. */
1203 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1206 /* The different kinds of declarators we want to parse. */
1208 typedef enum cp_parser_declarator_kind
1210 /* We want an abstract declarator. */
1211 CP_PARSER_DECLARATOR_ABSTRACT,
1212 /* We want a named declarator. */
1213 CP_PARSER_DECLARATOR_NAMED,
1214 /* We don't mind, but the name must be an unqualified-id. */
1215 CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind;
1218 /* The precedence values used to parse binary expressions. The minimum value
1219 of PREC must be 1, because zero is reserved to quickly discriminate
1220 binary operators from other tokens. */
1225 PREC_LOGICAL_OR_EXPRESSION,
1226 PREC_LOGICAL_AND_EXPRESSION,
1227 PREC_INCLUSIVE_OR_EXPRESSION,
1228 PREC_EXCLUSIVE_OR_EXPRESSION,
1229 PREC_AND_EXPRESSION,
1230 PREC_EQUALITY_EXPRESSION,
1231 PREC_RELATIONAL_EXPRESSION,
1232 PREC_SHIFT_EXPRESSION,
1233 PREC_ADDITIVE_EXPRESSION,
1234 PREC_MULTIPLICATIVE_EXPRESSION,
1236 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240 precedence value. */
1242 typedef struct cp_parser_binary_operations_map_node
1244 /* The token type. */
1245 enum cpp_ttype token_type;
1246 /* The corresponding tree code. */
1247 enum tree_code tree_type;
1248 /* The precedence of this operator. */
1249 enum cp_parser_prec prec;
1250 } cp_parser_binary_operations_map_node;
1252 /* The status of a tentative parse. */
1254 typedef enum cp_parser_status_kind
1256 /* No errors have occurred. */
1257 CP_PARSER_STATUS_KIND_NO_ERROR,
1258 /* An error has occurred. */
1259 CP_PARSER_STATUS_KIND_ERROR,
1260 /* We are committed to this tentative parse, whether or not an error
1262 CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind;
1265 typedef struct cp_parser_expression_stack_entry
1267 /* Left hand side of the binary operation we are currently
1270 /* Original tree code for left hand side, if it was a binary
1271 expression itself (used for -Wparentheses). */
1272 enum tree_code lhs_type;
1273 /* Tree code for the binary operation we are parsing. */
1274 enum tree_code tree_type;
1275 /* Precedence of the binary operation we are parsing. */
1277 } cp_parser_expression_stack_entry;
1279 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1280 entries because precedence levels on the stack are monotonically
1282 typedef struct cp_parser_expression_stack_entry
1283 cp_parser_expression_stack[NUM_PREC_VALUES];
1285 /* Context that is saved and restored when parsing tentatively. */
1286 typedef struct cp_parser_context GTY (())
1288 /* If this is a tentative parsing context, the status of the
1290 enum cp_parser_status_kind status;
1291 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1292 that are looked up in this context must be looked up both in the
1293 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294 the context of the containing expression. */
1297 /* The next parsing context in the stack. */
1298 struct cp_parser_context *next;
1299 } cp_parser_context;
1303 /* Constructors and destructors. */
1305 static cp_parser_context *cp_parser_context_new
1306 (cp_parser_context *);
1308 /* Class variables. */
1310 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313 Transformed into an associative array (binops_by_token) by
1316 static const cp_parser_binary_operations_map_node binops[] = {
1317 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1318 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1320 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1327 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1330 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1332 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1338 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1340 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1342 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1344 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1346 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1349 /* The same as binops, but initialized by cp_parser_new so that
1350 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1352 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1354 /* Constructors and destructors. */
1356 /* Construct a new context. The context below this one on the stack
1357 is given by NEXT. */
1359 static cp_parser_context *
1360 cp_parser_context_new (cp_parser_context* next)
1362 cp_parser_context *context;
1364 /* Allocate the storage. */
1365 if (cp_parser_context_free_list != NULL)
1367 /* Pull the first entry from the free list. */
1368 context = cp_parser_context_free_list;
1369 cp_parser_context_free_list = context->next;
1370 memset (context, 0, sizeof (*context));
1373 context = GGC_CNEW (cp_parser_context);
1375 /* No errors have occurred yet in this context. */
1376 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1377 /* If this is not the bottommost context, copy information that we
1378 need from the previous context. */
1381 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382 expression, then we are parsing one in this context, too. */
1383 context->object_type = next->object_type;
1384 /* Thread the stack. */
1385 context->next = next;
1391 /* The cp_parser structure represents the C++ parser. */
1393 typedef struct cp_parser GTY(())
1395 /* The lexer from which we are obtaining tokens. */
1398 /* The scope in which names should be looked up. If NULL_TREE, then
1399 we look up names in the scope that is currently open in the
1400 source program. If non-NULL, this is either a TYPE or
1401 NAMESPACE_DECL for the scope in which we should look. It can
1402 also be ERROR_MARK, when we've parsed a bogus scope.
1404 This value is not cleared automatically after a name is looked
1405 up, so we must be careful to clear it before starting a new look
1406 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1407 will look up `Z' in the scope of `X', rather than the current
1408 scope.) Unfortunately, it is difficult to tell when name lookup
1409 is complete, because we sometimes peek at a token, look it up,
1410 and then decide not to consume it. */
1413 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414 last lookup took place. OBJECT_SCOPE is used if an expression
1415 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416 respectively. QUALIFYING_SCOPE is used for an expression of the
1417 form "X::Y"; it refers to X. */
1419 tree qualifying_scope;
1421 /* A stack of parsing contexts. All but the bottom entry on the
1422 stack will be tentative contexts.
1424 We parse tentatively in order to determine which construct is in
1425 use in some situations. For example, in order to determine
1426 whether a statement is an expression-statement or a
1427 declaration-statement we parse it tentatively as a
1428 declaration-statement. If that fails, we then reparse the same
1429 token stream as an expression-statement. */
1430 cp_parser_context *context;
1432 /* True if we are parsing GNU C++. If this flag is not set, then
1433 GNU extensions are not recognized. */
1434 bool allow_gnu_extensions_p;
1436 /* TRUE if the `>' token should be interpreted as the greater-than
1437 operator. FALSE if it is the end of a template-id or
1438 template-parameter-list. In C++0x mode, this flag also applies to
1439 `>>' tokens, which are viewed as two consecutive `>' tokens when
1440 this flag is FALSE. */
1441 bool greater_than_is_operator_p;
1443 /* TRUE if default arguments are allowed within a parameter list
1444 that starts at this point. FALSE if only a gnu extension makes
1445 them permissible. */
1446 bool default_arg_ok_p;
1448 /* TRUE if we are parsing an integral constant-expression. See
1449 [expr.const] for a precise definition. */
1450 bool integral_constant_expression_p;
1452 /* TRUE if we are parsing an integral constant-expression -- but a
1453 non-constant expression should be permitted as well. This flag
1454 is used when parsing an array bound so that GNU variable-length
1455 arrays are tolerated. */
1456 bool allow_non_integral_constant_expression_p;
1458 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459 been seen that makes the expression non-constant. */
1460 bool non_integral_constant_expression_p;
1462 /* TRUE if local variable names and `this' are forbidden in the
1464 bool local_variables_forbidden_p;
1466 /* TRUE if the declaration we are parsing is part of a
1467 linkage-specification of the form `extern string-literal
1469 bool in_unbraced_linkage_specification_p;
1471 /* TRUE if we are presently parsing a declarator, after the
1472 direct-declarator. */
1473 bool in_declarator_p;
1475 /* TRUE if we are presently parsing a template-argument-list. */
1476 bool in_template_argument_list_p;
1478 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479 to IN_OMP_BLOCK if parsing OpenMP structured block and
1480 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1481 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482 iteration-statement, OpenMP block or loop within that switch. */
1483 #define IN_SWITCH_STMT 1
1484 #define IN_ITERATION_STMT 2
1485 #define IN_OMP_BLOCK 4
1486 #define IN_OMP_FOR 8
1487 #define IN_IF_STMT 16
1488 unsigned char in_statement;
1490 /* TRUE if we are presently parsing the body of a switch statement.
1491 Note that this doesn't quite overlap with in_statement above.
1492 The difference relates to giving the right sets of error messages:
1493 "case not in switch" vs "break statement used with OpenMP...". */
1494 bool in_switch_statement_p;
1496 /* TRUE if we are parsing a type-id in an expression context. In
1497 such a situation, both "type (expr)" and "type (type)" are valid
1499 bool in_type_id_in_expr_p;
1501 /* TRUE if we are currently in a header file where declarations are
1502 implicitly extern "C". */
1503 bool implicit_extern_c;
1505 /* TRUE if strings in expressions should be translated to the execution
1507 bool translate_strings_p;
1509 /* TRUE if we are presently parsing the body of a function, but not
1511 bool in_function_body;
1513 /* If non-NULL, then we are parsing a construct where new type
1514 definitions are not permitted. The string stored here will be
1515 issued as an error message if a type is defined. */
1516 const char *type_definition_forbidden_message;
1518 /* A list of lists. The outer list is a stack, used for member
1519 functions of local classes. At each level there are two sub-list,
1520 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522 TREE_VALUE's. The functions are chained in reverse declaration
1525 The TREE_PURPOSE sublist contains those functions with default
1526 arguments that need post processing, and the TREE_VALUE sublist
1527 contains those functions with definitions that need post
1530 These lists can only be processed once the outermost class being
1531 defined is complete. */
1532 tree unparsed_functions_queues;
1534 /* The number of classes whose definitions are currently in
1536 unsigned num_classes_being_defined;
1538 /* The number of template parameter lists that apply directly to the
1539 current declaration. */
1540 unsigned num_template_parameter_lists;
1545 /* Constructors and destructors. */
1547 static cp_parser *cp_parser_new
1550 /* Routines to parse various constructs.
1552 Those that return `tree' will return the error_mark_node (rather
1553 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554 Sometimes, they will return an ordinary node if error-recovery was
1555 attempted, even though a parse error occurred. So, to check
1556 whether or not a parse error occurred, you should always use
1557 cp_parser_error_occurred. If the construct is optional (indicated
1558 either by an `_opt' in the name of the function that does the
1559 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560 the construct is not present. */
1562 /* Lexical conventions [gram.lex] */
1564 static tree cp_parser_identifier
1566 static tree cp_parser_string_literal
1567 (cp_parser *, bool, bool);
1569 /* Basic concepts [gram.basic] */
1571 static bool cp_parser_translation_unit
1574 /* Expressions [gram.expr] */
1576 static tree cp_parser_primary_expression
1577 (cp_parser *, bool, bool, bool, cp_id_kind *);
1578 static tree cp_parser_id_expression
1579 (cp_parser *, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581 (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587 (cp_parser *, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589 (cp_parser *, bool, bool, bool, cp_id_kind *);
1590 static tree cp_parser_postfix_open_square_expression
1591 (cp_parser *, tree, bool);
1592 static tree cp_parser_postfix_dot_deref_expression
1593 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1594 static tree cp_parser_parenthesized_expression_list
1595 (cp_parser *, bool, bool, bool, bool *);
1596 static void cp_parser_pseudo_destructor_name
1597 (cp_parser *, tree *, tree *);
1598 static tree cp_parser_unary_expression
1599 (cp_parser *, bool, bool, cp_id_kind *);
1600 static enum tree_code cp_parser_unary_operator
1602 static tree cp_parser_new_expression
1604 static tree cp_parser_new_placement
1606 static tree cp_parser_new_type_id
1607 (cp_parser *, tree *);
1608 static cp_declarator *cp_parser_new_declarator_opt
1610 static cp_declarator *cp_parser_direct_new_declarator
1612 static tree cp_parser_new_initializer
1614 static tree cp_parser_delete_expression
1616 static tree cp_parser_cast_expression
1617 (cp_parser *, bool, bool, cp_id_kind *);
1618 static tree cp_parser_binary_expression
1619 (cp_parser *, bool, enum cp_parser_prec, cp_id_kind *);
1620 static tree cp_parser_question_colon_clause
1621 (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623 (cp_parser *, bool, cp_id_kind *);
1624 static enum tree_code cp_parser_assignment_operator_opt
1626 static tree cp_parser_expression
1627 (cp_parser *, bool, cp_id_kind *);
1628 static tree cp_parser_constant_expression
1629 (cp_parser *, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1633 /* Statements [gram.stmt.stmt] */
1635 static void cp_parser_statement
1636 (cp_parser *, tree, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1639 static tree cp_parser_expression_statement
1640 (cp_parser *, tree);
1641 static tree cp_parser_compound_statement
1642 (cp_parser *, tree, bool);
1643 static void cp_parser_statement_seq_opt
1644 (cp_parser *, tree);
1645 static tree cp_parser_selection_statement
1646 (cp_parser *, bool *);
1647 static tree cp_parser_condition
1649 static tree cp_parser_iteration_statement
1651 static void cp_parser_for_init_statement
1653 static tree cp_parser_jump_statement
1655 static void cp_parser_declaration_statement
1658 static tree cp_parser_implicitly_scoped_statement
1659 (cp_parser *, bool *);
1660 static void cp_parser_already_scoped_statement
1663 /* Declarations [gram.dcl.dcl] */
1665 static void cp_parser_declaration_seq_opt
1667 static void cp_parser_declaration
1669 static void cp_parser_block_declaration
1670 (cp_parser *, bool);
1671 static void cp_parser_simple_declaration
1672 (cp_parser *, bool);
1673 static void cp_parser_decl_specifier_seq
1674 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1677 static tree cp_parser_function_specifier_opt
1678 (cp_parser *, cp_decl_specifier_seq *);
1679 static tree cp_parser_type_specifier
1680 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1682 static tree cp_parser_simple_type_specifier
1683 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1684 static tree cp_parser_type_name
1686 static tree cp_parser_nonclass_name
1687 (cp_parser* parser);
1688 static tree cp_parser_elaborated_type_specifier
1689 (cp_parser *, bool, bool);
1690 static tree cp_parser_enum_specifier
1692 static void cp_parser_enumerator_list
1693 (cp_parser *, tree);
1694 static void cp_parser_enumerator_definition
1695 (cp_parser *, tree);
1696 static tree cp_parser_namespace_name
1698 static void cp_parser_namespace_definition
1700 static void cp_parser_namespace_body
1702 static tree cp_parser_qualified_namespace_specifier
1704 static void cp_parser_namespace_alias_definition
1706 static bool cp_parser_using_declaration
1707 (cp_parser *, bool);
1708 static void cp_parser_using_directive
1710 static void cp_parser_asm_definition
1712 static void cp_parser_linkage_specification
1714 static void cp_parser_static_assert
1715 (cp_parser *, bool);
1716 static tree cp_parser_decltype
1719 /* Declarators [gram.dcl.decl] */
1721 static tree cp_parser_init_declarator
1722 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1723 static cp_declarator *cp_parser_declarator
1724 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1725 static cp_declarator *cp_parser_direct_declarator
1726 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728 (cp_parser *, tree *, cp_cv_quals *);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1731 static tree cp_parser_late_return_type_opt
1733 static tree cp_parser_declarator_id
1734 (cp_parser *, bool);
1735 static tree cp_parser_type_id
1737 static void cp_parser_type_specifier_seq
1738 (cp_parser *, bool, cp_decl_specifier_seq *);
1739 static tree cp_parser_parameter_declaration_clause
1741 static tree cp_parser_parameter_declaration_list
1742 (cp_parser *, bool *);
1743 static cp_parameter_declarator *cp_parser_parameter_declaration
1744 (cp_parser *, bool, bool *);
1745 static tree cp_parser_default_argument
1746 (cp_parser *, bool);
1747 static void cp_parser_function_body
1749 static tree cp_parser_initializer
1750 (cp_parser *, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752 (cp_parser *, bool *);
1753 static tree cp_parser_braced_list
1754 (cp_parser*, bool*);
1755 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1756 (cp_parser *, bool *);
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1761 /* Classes [gram.class] */
1763 static tree cp_parser_class_name
1764 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1767 static tree cp_parser_class_head
1768 (cp_parser *, bool *, tree *, tree *);
1769 static enum tag_types cp_parser_class_key
1771 static void cp_parser_member_specification_opt
1773 static void cp_parser_member_declaration
1775 static tree cp_parser_pure_specifier
1777 static tree cp_parser_constant_initializer
1780 /* Derived classes [gram.class.derived] */
1782 static tree cp_parser_base_clause
1784 static tree cp_parser_base_specifier
1787 /* Special member functions [gram.special] */
1789 static tree cp_parser_conversion_function_id
1791 static tree cp_parser_conversion_type_id
1793 static cp_declarator *cp_parser_conversion_declarator_opt
1795 static bool cp_parser_ctor_initializer_opt
1797 static void cp_parser_mem_initializer_list
1799 static tree cp_parser_mem_initializer
1801 static tree cp_parser_mem_initializer_id
1804 /* Overloading [gram.over] */
1806 static tree cp_parser_operator_function_id
1808 static tree cp_parser_operator
1811 /* Templates [gram.temp] */
1813 static void cp_parser_template_declaration
1814 (cp_parser *, bool);
1815 static tree cp_parser_template_parameter_list
1817 static tree cp_parser_template_parameter
1818 (cp_parser *, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820 (cp_parser *, bool *);
1821 static tree cp_parser_template_id
1822 (cp_parser *, bool, bool, bool);
1823 static tree cp_parser_template_name
1824 (cp_parser *, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1827 static tree cp_parser_template_argument
1829 static void cp_parser_explicit_instantiation
1831 static void cp_parser_explicit_specialization
1834 /* Exception handling [gram.exception] */
1836 static tree cp_parser_try_block
1838 static bool cp_parser_function_try_block
1840 static void cp_parser_handler_seq
1842 static void cp_parser_handler
1844 static tree cp_parser_exception_declaration
1846 static tree cp_parser_throw_expression
1848 static tree cp_parser_exception_specification_opt
1850 static tree cp_parser_type_id_list
1853 /* GNU Extensions */
1855 static tree cp_parser_asm_specification_opt
1857 static tree cp_parser_asm_operand_list
1859 static tree cp_parser_asm_clobber_list
1861 static tree cp_parser_attributes_opt
1863 static tree cp_parser_attribute_list
1865 static bool cp_parser_extension_opt
1866 (cp_parser *, int *);
1867 static void cp_parser_label_declaration
1870 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1871 static bool cp_parser_pragma
1872 (cp_parser *, enum pragma_context);
1874 /* Objective-C++ Productions */
1876 static tree cp_parser_objc_message_receiver
1878 static tree cp_parser_objc_message_args
1880 static tree cp_parser_objc_message_expression
1882 static tree cp_parser_objc_encode_expression
1884 static tree cp_parser_objc_defs_expression
1886 static tree cp_parser_objc_protocol_expression
1888 static tree cp_parser_objc_selector_expression
1890 static tree cp_parser_objc_expression
1892 static bool cp_parser_objc_selector_p
1894 static tree cp_parser_objc_selector
1896 static tree cp_parser_objc_protocol_refs_opt
1898 static void cp_parser_objc_declaration
1900 static tree cp_parser_objc_statement
1903 /* Utility Routines */
1905 static tree cp_parser_lookup_name
1906 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1907 static tree cp_parser_lookup_name_simple
1908 (cp_parser *, tree, location_t);
1909 static tree cp_parser_maybe_treat_template_as_class
1911 static bool cp_parser_check_declarator_template_parameters
1912 (cp_parser *, cp_declarator *, location_t);
1913 static bool cp_parser_check_template_parameters
1914 (cp_parser *, unsigned, location_t);
1915 static tree cp_parser_simple_cast_expression
1917 static tree cp_parser_global_scope_opt
1918 (cp_parser *, bool);
1919 static bool cp_parser_constructor_declarator_p
1920 (cp_parser *, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1923 static tree cp_parser_function_definition_after_declarator
1924 (cp_parser *, bool);
1925 static void cp_parser_template_declaration_after_export
1926 (cp_parser *, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928 (VEC (deferred_access_check,gc)*);
1929 static tree cp_parser_single_declaration
1930 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932 (cp_parser *, tree);
1933 static tree cp_parser_save_member_function_body
1934 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1935 static tree cp_parser_enclosed_template_argument_list
1937 static void cp_parser_save_default_args
1938 (cp_parser *, tree);
1939 static void cp_parser_late_parsing_for_member
1940 (cp_parser *, tree);
1941 static void cp_parser_late_parsing_default_args
1942 (cp_parser *, tree);
1943 static tree cp_parser_sizeof_operand
1944 (cp_parser *, enum rid);
1945 static tree cp_parser_trait_expr
1946 (cp_parser *, enum rid);
1947 static bool cp_parser_declares_only_class_p
1949 static void cp_parser_set_storage_class
1950 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1951 static void cp_parser_set_decl_spec_type
1952 (cp_decl_specifier_seq *, tree, location_t, bool);
1953 static bool cp_parser_friend_p
1954 (const cp_decl_specifier_seq *);
1955 static cp_token *cp_parser_require
1956 (cp_parser *, enum cpp_ttype, const char *);
1957 static cp_token *cp_parser_require_keyword
1958 (cp_parser *, enum rid, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1961 static bool cp_parser_next_token_starts_class_definition_p
1963 static bool cp_parser_next_token_ends_template_argument_p
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966 (cp_parser *, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1969 static void cp_parser_check_class_key
1970 (enum tag_types, tree type);
1971 static void cp_parser_check_access_in_redeclaration
1972 (tree type, location_t location);
1973 static bool cp_parser_optional_template_keyword
1975 static void cp_parser_pre_parsed_nested_name_specifier
1977 static bool cp_parser_cache_group
1978 (cp_parser *, enum cpp_ttype, unsigned);
1979 static void cp_parser_parse_tentatively
1981 static void cp_parser_commit_to_tentative_parse
1983 static void cp_parser_abort_tentative_parse
1985 static bool cp_parser_parse_definitely
1987 static inline bool cp_parser_parsing_tentatively
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1991 static void cp_parser_error
1992 (cp_parser *, const char *);
1993 static void cp_parser_name_lookup_error
1994 (cp_parser *, tree, tree, const char *, location_t);
1995 static bool cp_parser_simulate_error
1997 static bool cp_parser_check_type_definition
1999 static void cp_parser_check_for_definition_in_return_type
2000 (cp_declarator *, tree, location_t type_location);
2001 static void cp_parser_check_for_invalid_template_id
2002 (cp_parser *, tree, location_t location);
2003 static bool cp_parser_non_integral_constant_expression
2004 (cp_parser *, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006 (cp_parser *, tree, tree, location_t);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2009 static int cp_parser_skip_to_closing_parenthesis
2010 (cp_parser *, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2015 static void cp_parser_skip_to_end_of_block_or_statement
2017 static bool cp_parser_skip_to_closing_brace
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2021 static void cp_parser_skip_to_pragma_eol
2022 (cp_parser*, cp_token *);
2023 static bool cp_parser_error_occurred
2025 static bool cp_parser_allow_gnu_extensions_p
2027 static bool cp_parser_is_string_literal
2029 static bool cp_parser_is_keyword
2030 (cp_token *, enum rid);
2031 static tree cp_parser_make_typename_type
2032 (cp_parser *, tree, tree, location_t location);
2033 static cp_declarator * cp_parser_make_indirect_declarator
2034 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2036 /* Returns nonzero if we are parsing tentatively. */
2039 cp_parser_parsing_tentatively (cp_parser* parser)
2041 return parser->context->next != NULL;
2044 /* Returns nonzero if TOKEN is a string literal. */
2047 cp_parser_is_string_literal (cp_token* token)
2049 return (token->type == CPP_STRING ||
2050 token->type == CPP_STRING16 ||
2051 token->type == CPP_STRING32 ||
2052 token->type == CPP_WSTRING);
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2058 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2060 return token->keyword == keyword;
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064 FILE:LINE: MESSAGE before TOKEN
2065 where TOKEN is the next token in the input stream. MESSAGE
2066 (specified by the caller) is usually of the form "expected
2070 cp_parser_error (cp_parser* parser, const char* message)
2072 if (!cp_parser_simulate_error (parser))
2074 cp_token *token = cp_lexer_peek_token (parser->lexer);
2075 /* This diagnostic makes more sense if it is tagged to the line
2076 of the token we just peeked at. */
2077 cp_lexer_set_source_position_from_token (token);
2079 if (token->type == CPP_PRAGMA)
2081 error ("%H%<#pragma%> is not allowed here", &token->location);
2082 cp_parser_skip_to_pragma_eol (parser, token);
2086 c_parse_error (message,
2087 /* Because c_parser_error does not understand
2088 CPP_KEYWORD, keywords are treated like
2090 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2095 /* Issue an error about name-lookup failing. NAME is the
2096 IDENTIFIER_NODE DECL is the result of
2097 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2098 the thing that we hoped to find. */
2101 cp_parser_name_lookup_error (cp_parser* parser,
2104 const char* desired,
2105 location_t location)
2107 /* If name lookup completely failed, tell the user that NAME was not
2109 if (decl == error_mark_node)
2111 if (parser->scope && parser->scope != global_namespace)
2112 error ("%H%<%E::%E%> has not been declared",
2113 &location, parser->scope, name);
2114 else if (parser->scope == global_namespace)
2115 error ("%H%<::%E%> has not been declared", &location, name);
2116 else if (parser->object_scope
2117 && !CLASS_TYPE_P (parser->object_scope))
2118 error ("%Hrequest for member %qE in non-class type %qT",
2119 &location, name, parser->object_scope);
2120 else if (parser->object_scope)
2121 error ("%H%<%T::%E%> has not been declared",
2122 &location, parser->object_scope, name);
2124 error ("%H%qE has not been declared", &location, name);
2126 else if (parser->scope && parser->scope != global_namespace)
2127 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2128 else if (parser->scope == global_namespace)
2129 error ("%H%<::%E%> %s", &location, name, desired);
2131 error ("%H%qE %s", &location, name, desired);
2134 /* If we are parsing tentatively, remember that an error has occurred
2135 during this tentative parse. Returns true if the error was
2136 simulated; false if a message should be issued by the caller. */
2139 cp_parser_simulate_error (cp_parser* parser)
2141 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2143 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2149 /* Check for repeated decl-specifiers. */
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2153 location_t location)
2157 for (ds = ds_first; ds != ds_last; ++ds)
2159 unsigned count = decl_specs->specs[(int)ds];
2162 /* The "long" specifier is a special case because of "long long". */
2166 error ("%H%<long long long%> is too long for GCC", &location);
2167 else if (pedantic && !in_system_header && warn_long_long
2168 && cxx_dialect == cxx98)
2169 pedwarn (location, OPT_Wlong_long,
2170 "ISO C++ 1998 does not support %<long long%>");
2174 static const char *const decl_spec_names[] = {
2190 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2195 /* This function is called when a type is defined. If type
2196 definitions are forbidden at this point, an error message is
2200 cp_parser_check_type_definition (cp_parser* parser)
2202 /* If types are forbidden here, issue a message. */
2203 if (parser->type_definition_forbidden_message)
2205 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206 in the message need to be interpreted. */
2207 error (parser->type_definition_forbidden_message);
2213 /* This function is called when the DECLARATOR is processed. The TYPE
2214 was a type defined in the decl-specifiers. If it is invalid to
2215 define a type in the decl-specifiers for DECLARATOR, an error is
2216 issued. TYPE_LOCATION is the location of TYPE and is used
2217 for error reporting. */
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221 tree type, location_t type_location)
2223 /* [dcl.fct] forbids type definitions in return types.
2224 Unfortunately, it's not easy to know whether or not we are
2225 processing a return type until after the fact. */
2227 && (declarator->kind == cdk_pointer
2228 || declarator->kind == cdk_reference
2229 || declarator->kind == cdk_ptrmem))
2230 declarator = declarator->declarator;
2232 && declarator->kind == cdk_function)
2234 error ("%Hnew types may not be defined in a return type", &type_location);
2235 inform (type_location,
2236 "(perhaps a semicolon is missing after the definition of %qT)",
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242 "<" in any valid C++ program. If the next token is indeed "<",
2243 issue a message warning the user about what appears to be an
2244 invalid attempt to form a template-id. LOCATION is the location
2245 of the type-specifier (TYPE) */
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249 tree type, location_t location)
2251 cp_token_position start = 0;
2253 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2256 error ("%H%qT is not a template", &location, type);
2257 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258 error ("%H%qE is not a template", &location, type);
2260 error ("%Hinvalid template-id", &location);
2261 /* Remember the location of the invalid "<". */
2262 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263 start = cp_lexer_token_position (parser->lexer, true);
2264 /* Consume the "<". */
2265 cp_lexer_consume_token (parser->lexer);
2266 /* Parse the template arguments. */
2267 cp_parser_enclosed_template_argument_list (parser);
2268 /* Permanently remove the invalid template arguments so that
2269 this error message is not issued again. */
2271 cp_lexer_purge_tokens_after (parser->lexer, start);
2275 /* If parsing an integral constant-expression, issue an error message
2276 about the fact that THING appeared and return true. Otherwise,
2277 return false. In either case, set
2278 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2281 cp_parser_non_integral_constant_expression (cp_parser *parser,
2284 parser->non_integral_constant_expression_p = true;
2285 if (parser->integral_constant_expression_p)
2287 if (!parser->allow_non_integral_constant_expression_p)
2289 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290 in the message need to be interpreted. */
2291 char *message = concat (thing,
2292 " cannot appear in a constant-expression",
2302 /* Emit a diagnostic for an invalid type name. SCOPE is the
2303 qualifying scope (or NULL, if none) for ID. This function commits
2304 to the current active tentative parse, if any. (Otherwise, the
2305 problematic construct might be encountered again later, resulting
2306 in duplicate error messages.) LOCATION is the location of ID. */
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310 tree scope, tree id,
2311 location_t location)
2313 tree decl, old_scope;
2314 /* Try to lookup the identifier. */
2315 old_scope = parser->scope;
2316 parser->scope = scope;
2317 decl = cp_parser_lookup_name_simple (parser, id, location);
2318 parser->scope = old_scope;
2319 /* If the lookup found a template-name, it means that the user forgot
2320 to specify an argument list. Emit a useful error message. */
2321 if (TREE_CODE (decl) == TEMPLATE_DECL)
2322 error ("%Hinvalid use of template-name %qE without an argument list",
2324 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326 else if (TREE_CODE (decl) == TYPE_DECL)
2327 /* Something like 'unsigned A a;' */
2328 error ("%Hinvalid combination of multiple type-specifiers",
2330 else if (!parser->scope)
2332 /* Issue an error message. */
2333 error ("%H%qE does not name a type", &location, id);
2334 /* If we're in a template class, it's possible that the user was
2335 referring to a type from a base class. For example:
2337 template <typename T> struct A { typedef T X; };
2338 template <typename T> struct B : public A<T> { X x; };
2340 The user should have said "typename A<T>::X". */
2341 if (processing_template_decl && current_class_type
2342 && TYPE_BINFO (current_class_type))
2346 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2350 tree base_type = BINFO_TYPE (b);
2351 if (CLASS_TYPE_P (base_type)
2352 && dependent_type_p (base_type))
2355 /* Go from a particular instantiation of the
2356 template (which will have an empty TYPE_FIELDs),
2357 to the main version. */
2358 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359 for (field = TYPE_FIELDS (base_type);
2361 field = TREE_CHAIN (field))
2362 if (TREE_CODE (field) == TYPE_DECL
2363 && DECL_NAME (field) == id)
2366 "(perhaps %<typename %T::%E%> was intended)",
2367 BINFO_TYPE (b), id);
2376 /* Here we diagnose qualified-ids where the scope is actually correct,
2377 but the identifier does not resolve to a valid type name. */
2378 else if (parser->scope != error_mark_node)
2380 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381 error ("%H%qE in namespace %qE does not name a type",
2382 &location, id, parser->scope);
2383 else if (TYPE_P (parser->scope))
2384 error ("%H%qE in class %qT does not name a type",
2385 &location, id, parser->scope);
2389 cp_parser_commit_to_tentative_parse (parser);
2392 /* Check for a common situation where a type-name should be present,
2393 but is not, and issue a sensible error message. Returns true if an
2394 invalid type-name was detected.
2396 The situation handled by this function are variable declarations of the
2397 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398 Usually, `ID' should name a type, but if we got here it means that it
2399 does not. We try to emit the best possible error message depending on
2400 how exactly the id-expression looks like. */
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2406 cp_token *token = cp_lexer_peek_token (parser->lexer);
2408 cp_parser_parse_tentatively (parser);
2409 id = cp_parser_id_expression (parser,
2410 /*template_keyword_p=*/false,
2411 /*check_dependency_p=*/true,
2412 /*template_p=*/NULL,
2413 /*declarator_p=*/true,
2414 /*optional_p=*/false);
2415 /* After the id-expression, there should be a plain identifier,
2416 otherwise this is not a simple variable declaration. Also, if
2417 the scope is dependent, we cannot do much. */
2418 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419 || (parser->scope && TYPE_P (parser->scope)
2420 && dependent_type_p (parser->scope))
2421 || TREE_CODE (id) == TYPE_DECL)
2423 cp_parser_abort_tentative_parse (parser);
2426 if (!cp_parser_parse_definitely (parser))
2429 /* Emit a diagnostic for the invalid type. */
2430 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431 id, token->location);
2432 /* Skip to the end of the declaration; there's no point in
2433 trying to process it. */
2434 cp_parser_skip_to_end_of_block_or_statement (parser);
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2440 are doing error recovery. Returns -1 if OR_COMMA is true and we
2441 found an unnested comma. */
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2449 unsigned paren_depth = 0;
2450 unsigned brace_depth = 0;
2452 if (recovering && !or_comma
2453 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2458 cp_token * token = cp_lexer_peek_token (parser->lexer);
2460 switch (token->type)
2463 case CPP_PRAGMA_EOL:
2464 /* If we've run out of tokens, then there is no closing `)'. */
2468 /* This matches the processing in skip_to_end_of_statement. */
2473 case CPP_OPEN_BRACE:
2476 case CPP_CLOSE_BRACE:
2482 if (recovering && or_comma && !brace_depth && !paren_depth)
2486 case CPP_OPEN_PAREN:
2491 case CPP_CLOSE_PAREN:
2492 if (!brace_depth && !paren_depth--)
2495 cp_lexer_consume_token (parser->lexer);
2504 /* Consume the token. */
2505 cp_lexer_consume_token (parser->lexer);
2509 /* Consume tokens until we reach the end of the current statement.
2510 Normally, that will be just before consuming a `;'. However, if a
2511 non-nested `}' comes first, then we stop before consuming that. */
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2516 unsigned nesting_depth = 0;
2520 cp_token *token = cp_lexer_peek_token (parser->lexer);
2522 switch (token->type)
2525 case CPP_PRAGMA_EOL:
2526 /* If we've run out of tokens, stop. */
2530 /* If the next token is a `;', we have reached the end of the
2536 case CPP_CLOSE_BRACE:
2537 /* If this is a non-nested '}', stop before consuming it.
2538 That way, when confronted with something like:
2542 we stop before consuming the closing '}', even though we
2543 have not yet reached a `;'. */
2544 if (nesting_depth == 0)
2547 /* If it is the closing '}' for a block that we have
2548 scanned, stop -- but only after consuming the token.
2554 we will stop after the body of the erroneously declared
2555 function, but before consuming the following `typedef'
2557 if (--nesting_depth == 0)
2559 cp_lexer_consume_token (parser->lexer);
2563 case CPP_OPEN_BRACE:
2571 /* Consume the token. */
2572 cp_lexer_consume_token (parser->lexer);
2576 /* This function is called at the end of a statement or declaration.
2577 If the next token is a semicolon, it is consumed; otherwise, error
2578 recovery is attempted. */
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2583 /* Look for the trailing `;'. */
2584 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2586 /* If there is additional (erroneous) input, skip to the end of
2588 cp_parser_skip_to_end_of_statement (parser);
2589 /* If the next token is now a `;', consume it. */
2590 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591 cp_lexer_consume_token (parser->lexer);
2595 /* Skip tokens until we have consumed an entire block, or until we
2596 have consumed a non-nested `;'. */
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2601 int nesting_depth = 0;
2603 while (nesting_depth >= 0)
2605 cp_token *token = cp_lexer_peek_token (parser->lexer);
2607 switch (token->type)
2610 case CPP_PRAGMA_EOL:
2611 /* If we've run out of tokens, stop. */
2615 /* Stop if this is an unnested ';'. */
2620 case CPP_CLOSE_BRACE:
2621 /* Stop if this is an unnested '}', or closes the outermost
2628 case CPP_OPEN_BRACE:
2637 /* Consume the token. */
2638 cp_lexer_consume_token (parser->lexer);
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643 token, or there are no more tokens. Return true in the first case,
2647 cp_parser_skip_to_closing_brace (cp_parser *parser)
2649 unsigned nesting_depth = 0;
2653 cp_token *token = cp_lexer_peek_token (parser->lexer);
2655 switch (token->type)
2658 case CPP_PRAGMA_EOL:
2659 /* If we've run out of tokens, stop. */
2662 case CPP_CLOSE_BRACE:
2663 /* If the next token is a non-nested `}', then we have reached
2664 the end of the current block. */
2665 if (nesting_depth-- == 0)
2669 case CPP_OPEN_BRACE:
2670 /* If it the next token is a `{', then we are entering a new
2671 block. Consume the entire block. */
2679 /* Consume the token. */
2680 cp_lexer_consume_token (parser->lexer);
2684 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2685 parameter is the PRAGMA token, allowing us to purge the entire pragma
2689 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2693 parser->lexer->in_pragma = false;
2696 token = cp_lexer_consume_token (parser->lexer);
2697 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2699 /* Ensure that the pragma is not parsed again. */
2700 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2703 /* Require pragma end of line, resyncing with it as necessary. The
2704 arguments are as for cp_parser_skip_to_pragma_eol. */
2707 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2709 parser->lexer->in_pragma = false;
2710 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2711 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715 an unresolved identifier node, we can provide a superior diagnostic
2716 using cp_parser_diagnose_invalid_type_name. */
2719 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2720 tree id, location_t id_location)
2723 if (TREE_CODE (id) == IDENTIFIER_NODE)
2725 result = make_typename_type (scope, id, typename_type,
2726 /*complain=*/tf_none);
2727 if (result == error_mark_node)
2728 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2731 return make_typename_type (scope, id, typename_type, tf_error);
2734 /* This is a wrapper around the
2735 make_{pointer,ptrmem,reference}_declarator functions that decides
2736 which one to call based on the CODE and CLASS_TYPE arguments. The
2737 CODE argument should be one of the values returned by
2738 cp_parser_ptr_operator. */
2739 static cp_declarator *
2740 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2741 cp_cv_quals cv_qualifiers,
2742 cp_declarator *target)
2744 if (code == ERROR_MARK)
2745 return cp_error_declarator;
2747 if (code == INDIRECT_REF)
2748 if (class_type == NULL_TREE)
2749 return make_pointer_declarator (cv_qualifiers, target);
2751 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2752 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2753 return make_reference_declarator (cv_qualifiers, target, false);
2754 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2755 return make_reference_declarator (cv_qualifiers, target, true);
2759 /* Create a new C++ parser. */
2762 cp_parser_new (void)
2768 /* cp_lexer_new_main is called before calling ggc_alloc because
2769 cp_lexer_new_main might load a PCH file. */
2770 lexer = cp_lexer_new_main ();
2772 /* Initialize the binops_by_token so that we can get the tree
2773 directly from the token. */
2774 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2775 binops_by_token[binops[i].token_type] = binops[i];
2777 parser = GGC_CNEW (cp_parser);
2778 parser->lexer = lexer;
2779 parser->context = cp_parser_context_new (NULL);
2781 /* For now, we always accept GNU extensions. */
2782 parser->allow_gnu_extensions_p = 1;
2784 /* The `>' token is a greater-than operator, not the end of a
2786 parser->greater_than_is_operator_p = true;
2788 parser->default_arg_ok_p = true;
2790 /* We are not parsing a constant-expression. */
2791 parser->integral_constant_expression_p = false;
2792 parser->allow_non_integral_constant_expression_p = false;
2793 parser->non_integral_constant_expression_p = false;
2795 /* Local variable names are not forbidden. */
2796 parser->local_variables_forbidden_p = false;
2798 /* We are not processing an `extern "C"' declaration. */
2799 parser->in_unbraced_linkage_specification_p = false;
2801 /* We are not processing a declarator. */
2802 parser->in_declarator_p = false;
2804 /* We are not processing a template-argument-list. */
2805 parser->in_template_argument_list_p = false;
2807 /* We are not in an iteration statement. */
2808 parser->in_statement = 0;
2810 /* We are not in a switch statement. */
2811 parser->in_switch_statement_p = false;
2813 /* We are not parsing a type-id inside an expression. */
2814 parser->in_type_id_in_expr_p = false;
2816 /* Declarations aren't implicitly extern "C". */
2817 parser->implicit_extern_c = false;
2819 /* String literals should be translated to the execution character set. */
2820 parser->translate_strings_p = true;
2822 /* We are not parsing a function body. */
2823 parser->in_function_body = false;
2825 /* The unparsed function queue is empty. */
2826 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2828 /* There are no classes being defined. */
2829 parser->num_classes_being_defined = 0;
2831 /* No template parameters apply. */
2832 parser->num_template_parameter_lists = 0;
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838 and push it onto the parser's lexer stack. This is used for delayed
2839 parsing of in-class method bodies and default arguments, and should
2840 not be confused with tentative parsing. */
2842 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2844 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2845 lexer->next = parser->lexer;
2846 parser->lexer = lexer;
2848 /* Move the current source position to that of the first token in the
2850 cp_lexer_set_source_position_from_token (lexer->next_token);
2853 /* Pop the top lexer off the parser stack. This is never used for the
2854 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2856 cp_parser_pop_lexer (cp_parser *parser)
2858 cp_lexer *lexer = parser->lexer;
2859 parser->lexer = lexer->next;
2860 cp_lexer_destroy (lexer);
2862 /* Put the current source position back where it was before this
2863 lexer was pushed. */
2864 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2867 /* Lexical conventions [gram.lex] */
2869 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2873 cp_parser_identifier (cp_parser* parser)
2877 /* Look for the identifier. */
2878 token = cp_parser_require (parser, CPP_NAME, "identifier");
2879 /* Return the value. */
2880 return token ? token->u.value : error_mark_node;
2883 /* Parse a sequence of adjacent string constants. Returns a
2884 TREE_STRING representing the combined, nul-terminated string
2885 constant. If TRANSLATE is true, translate the string to the
2886 execution character set. If WIDE_OK is true, a wide string is
2889 C++98 [lex.string] says that if a narrow string literal token is
2890 adjacent to a wide string literal token, the behavior is undefined.
2891 However, C99 6.4.5p4 says that this results in a wide string literal.
2892 We follow C99 here, for consistency with the C front end.
2894 This code is largely lifted from lex_string() in c-lex.c.
2896 FUTURE: ObjC++ will need to handle @-strings here. */
2898 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2902 struct obstack str_ob;
2903 cpp_string str, istr, *strs;
2905 enum cpp_ttype type;
2907 tok = cp_lexer_peek_token (parser->lexer);
2908 if (!cp_parser_is_string_literal (tok))
2910 cp_parser_error (parser, "expected string-literal");
2911 return error_mark_node;
2916 /* Try to avoid the overhead of creating and destroying an obstack
2917 for the common case of just one string. */
2918 if (!cp_parser_is_string_literal
2919 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2921 cp_lexer_consume_token (parser->lexer);
2923 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2924 str.len = TREE_STRING_LENGTH (tok->u.value);
2931 gcc_obstack_init (&str_ob);
2936 cp_lexer_consume_token (parser->lexer);
2938 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2939 str.len = TREE_STRING_LENGTH (tok->u.value);
2941 if (type != tok->type)
2943 if (type == CPP_STRING)
2945 else if (tok->type != CPP_STRING)
2946 error ("%Hunsupported non-standard concatenation "
2947 "of string literals", &tok->location);
2950 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2952 tok = cp_lexer_peek_token (parser->lexer);
2954 while (cp_parser_is_string_literal (tok));
2956 strs = (cpp_string *) obstack_finish (&str_ob);
2959 if (type != CPP_STRING && !wide_ok)
2961 cp_parser_error (parser, "a wide string is invalid in this context");
2965 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2966 (parse_in, strs, count, &istr, type))
2968 value = build_string (istr.len, (const char *)istr.text);
2969 free (CONST_CAST (unsigned char *, istr.text));
2975 TREE_TYPE (value) = char_array_type_node;
2978 TREE_TYPE (value) = char16_array_type_node;
2981 TREE_TYPE (value) = char32_array_type_node;
2984 TREE_TYPE (value) = wchar_array_type_node;
2988 value = fix_string_type (value);
2991 /* cpp_interpret_string has issued an error. */
2992 value = error_mark_node;
2995 obstack_free (&str_ob, 0);
3001 /* Basic concepts [gram.basic] */
3003 /* Parse a translation-unit.
3006 declaration-seq [opt]
3008 Returns TRUE if all went well. */
3011 cp_parser_translation_unit (cp_parser* parser)
3013 /* The address of the first non-permanent object on the declarator
3015 static void *declarator_obstack_base;
3019 /* Create the declarator obstack, if necessary. */
3020 if (!cp_error_declarator)
3022 gcc_obstack_init (&declarator_obstack);
3023 /* Create the error declarator. */
3024 cp_error_declarator = make_declarator (cdk_error);
3025 /* Create the empty parameter list. */
3026 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3027 /* Remember where the base of the declarator obstack lies. */
3028 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3031 cp_parser_declaration_seq_opt (parser);
3033 /* If there are no tokens left then all went well. */
3034 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3036 /* Get rid of the token array; we don't need it any more. */
3037 cp_lexer_destroy (parser->lexer);
3038 parser->lexer = NULL;
3040 /* This file might have been a context that's implicitly extern
3041 "C". If so, pop the lang context. (Only relevant for PCH.) */
3042 if (parser->implicit_extern_c)
3044 pop_lang_context ();
3045 parser->implicit_extern_c = false;
3049 finish_translation_unit ();
3055 cp_parser_error (parser, "expected declaration");
3059 /* Make sure the declarator obstack was fully cleaned up. */
3060 gcc_assert (obstack_next_free (&declarator_obstack)
3061 == declarator_obstack_base);
3063 /* All went well. */
3067 /* Expressions [gram.expr] */
3069 /* Parse a primary-expression.
3080 ( compound-statement )
3081 __builtin_va_arg ( assignment-expression , type-id )
3082 __builtin_offsetof ( type-id , offsetof-expression )
3085 __has_nothrow_assign ( type-id )
3086 __has_nothrow_constructor ( type-id )
3087 __has_nothrow_copy ( type-id )
3088 __has_trivial_assign ( type-id )
3089 __has_trivial_constructor ( type-id )
3090 __has_trivial_copy ( type-id )
3091 __has_trivial_destructor ( type-id )
3092 __has_virtual_destructor ( type-id )
3093 __is_abstract ( type-id )
3094 __is_base_of ( type-id , type-id )
3095 __is_class ( type-id )
3096 __is_convertible_to ( type-id , type-id )
3097 __is_empty ( type-id )
3098 __is_enum ( type-id )
3099 __is_pod ( type-id )
3100 __is_polymorphic ( type-id )
3101 __is_union ( type-id )
3103 Objective-C++ Extension:
3111 ADDRESS_P is true iff this expression was immediately preceded by
3112 "&" and therefore might denote a pointer-to-member. CAST_P is true
3113 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3114 true iff this expression is a template argument.
3116 Returns a representation of the expression. Upon return, *IDK
3117 indicates what kind of id-expression (if any) was present. */
3120 cp_parser_primary_expression (cp_parser *parser,
3123 bool template_arg_p,
3126 cp_token *token = NULL;
3128 /* Assume the primary expression is not an id-expression. */
3129 *idk = CP_ID_KIND_NONE;
3131 /* Peek at the next token. */
3132 token = cp_lexer_peek_token (parser->lexer);
3133 switch (token->type)
3146 token = cp_lexer_consume_token (parser->lexer);
3147 if (TREE_CODE (token->u.value) == FIXED_CST)
3149 error ("%Hfixed-point types not supported in C++",
3151 return error_mark_node;
3153 /* Floating-point literals are only allowed in an integral
3154 constant expression if they are cast to an integral or
3155 enumeration type. */
3156 if (TREE_CODE (token->u.value) == REAL_CST
3157 && parser->integral_constant_expression_p
3160 /* CAST_P will be set even in invalid code like "int(2.7 +
3161 ...)". Therefore, we have to check that the next token
3162 is sure to end the cast. */
3165 cp_token *next_token;
3167 next_token = cp_lexer_peek_token (parser->lexer);
3168 if (/* The comma at the end of an
3169 enumerator-definition. */
3170 next_token->type != CPP_COMMA
3171 /* The curly brace at the end of an enum-specifier. */
3172 && next_token->type != CPP_CLOSE_BRACE
3173 /* The end of a statement. */
3174 && next_token->type != CPP_SEMICOLON
3175 /* The end of the cast-expression. */
3176 && next_token->type != CPP_CLOSE_PAREN
3177 /* The end of an array bound. */
3178 && next_token->type != CPP_CLOSE_SQUARE
3179 /* The closing ">" in a template-argument-list. */
3180 && (next_token->type != CPP_GREATER
3181 || parser->greater_than_is_operator_p)
3182 /* C++0x only: A ">>" treated like two ">" tokens,
3183 in a template-argument-list. */
3184 && (next_token->type != CPP_RSHIFT
3185 || (cxx_dialect == cxx98)
3186 || parser->greater_than_is_operator_p))
3190 /* If we are within a cast, then the constraint that the
3191 cast is to an integral or enumeration type will be
3192 checked at that point. If we are not within a cast, then
3193 this code is invalid. */
3195 cp_parser_non_integral_constant_expression
3196 (parser, "floating-point literal");
3198 return token->u.value;
3204 /* ??? Should wide strings be allowed when parser->translate_strings_p
3205 is false (i.e. in attributes)? If not, we can kill the third
3206 argument to cp_parser_string_literal. */
3207 return cp_parser_string_literal (parser,
3208 parser->translate_strings_p,
3211 case CPP_OPEN_PAREN:
3214 bool saved_greater_than_is_operator_p;
3216 /* Consume the `('. */
3217 cp_lexer_consume_token (parser->lexer);
3218 /* Within a parenthesized expression, a `>' token is always
3219 the greater-than operator. */
3220 saved_greater_than_is_operator_p
3221 = parser->greater_than_is_operator_p;
3222 parser->greater_than_is_operator_p = true;
3223 /* If we see `( { ' then we are looking at the beginning of
3224 a GNU statement-expression. */
3225 if (cp_parser_allow_gnu_extensions_p (parser)
3226 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3228 /* Statement-expressions are not allowed by the standard. */
3229 pedwarn (token->location, OPT_pedantic,
3230 "ISO C++ forbids braced-groups within expressions");
3232 /* And they're not allowed outside of a function-body; you
3233 cannot, for example, write:
3235 int i = ({ int j = 3; j + 1; });
3237 at class or namespace scope. */
3238 if (!parser->in_function_body
3239 || parser->in_template_argument_list_p)
3241 error ("%Hstatement-expressions are not allowed outside "
3242 "functions nor in template-argument lists",
3244 cp_parser_skip_to_end_of_block_or_statement (parser);
3245 expr = error_mark_node;
3249 /* Start the statement-expression. */
3250 expr = begin_stmt_expr ();
3251 /* Parse the compound-statement. */
3252 cp_parser_compound_statement (parser, expr, false);
3254 expr = finish_stmt_expr (expr, false);
3259 /* Parse the parenthesized expression. */
3260 expr = cp_parser_expression (parser, cast_p, idk);
3261 /* Let the front end know that this expression was
3262 enclosed in parentheses. This matters in case, for
3263 example, the expression is of the form `A::B', since
3264 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3266 finish_parenthesized_expr (expr);
3268 /* The `>' token might be the end of a template-id or
3269 template-parameter-list now. */
3270 parser->greater_than_is_operator_p
3271 = saved_greater_than_is_operator_p;
3272 /* Consume the `)'. */
3273 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3274 cp_parser_skip_to_end_of_statement (parser);
3280 switch (token->keyword)
3282 /* These two are the boolean literals. */
3284 cp_lexer_consume_token (parser->lexer);
3285 return boolean_true_node;
3287 cp_lexer_consume_token (parser->lexer);
3288 return boolean_false_node;
3290 /* The `__null' literal. */
3292 cp_lexer_consume_token (parser->lexer);
3295 /* Recognize the `this' keyword. */
3297 cp_lexer_consume_token (parser->lexer);
3298 if (parser->local_variables_forbidden_p)
3300 error ("%H%<this%> may not be used in this context",
3302 return error_mark_node;
3304 /* Pointers cannot appear in constant-expressions. */
3305 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3306 return error_mark_node;
3307 return finish_this_expr ();
3309 /* The `operator' keyword can be the beginning of an
3314 case RID_FUNCTION_NAME:
3315 case RID_PRETTY_FUNCTION_NAME:
3316 case RID_C99_FUNCTION_NAME:
3320 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3321 __func__ are the names of variables -- but they are
3322 treated specially. Therefore, they are handled here,
3323 rather than relying on the generic id-expression logic
3324 below. Grammatically, these names are id-expressions.
3326 Consume the token. */
3327 token = cp_lexer_consume_token (parser->lexer);
3329 switch (token->keyword)
3331 case RID_FUNCTION_NAME:
3332 name = "%<__FUNCTION__%>";
3334 case RID_PRETTY_FUNCTION_NAME:
3335 name = "%<__PRETTY_FUNCTION__%>";
3337 case RID_C99_FUNCTION_NAME:
3338 name = "%<__func__%>";
3344 if (cp_parser_non_integral_constant_expression (parser, name))
3345 return error_mark_node;
3347 /* Look up the name. */
3348 return finish_fname (token->u.value);
3356 /* The `__builtin_va_arg' construct is used to handle
3357 `va_arg'. Consume the `__builtin_va_arg' token. */
3358 cp_lexer_consume_token (parser->lexer);
3359 /* Look for the opening `('. */
3360 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3361 /* Now, parse the assignment-expression. */
3362 expression = cp_parser_assignment_expression (parser,
3363 /*cast_p=*/false, NULL);
3364 /* Look for the `,'. */
3365 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3366 /* Parse the type-id. */
3367 type = cp_parser_type_id (parser);
3368 /* Look for the closing `)'. */
3369 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3370 /* Using `va_arg' in a constant-expression is not
3372 if (cp_parser_non_integral_constant_expression (parser,
3374 return error_mark_node;
3375 return build_x_va_arg (expression, type);
3379 return cp_parser_builtin_offsetof (parser);
3381 case RID_HAS_NOTHROW_ASSIGN:
3382 case RID_HAS_NOTHROW_CONSTRUCTOR:
3383 case RID_HAS_NOTHROW_COPY:
3384 case RID_HAS_TRIVIAL_ASSIGN:
3385 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3386 case RID_HAS_TRIVIAL_COPY:
3387 case RID_HAS_TRIVIAL_DESTRUCTOR:
3388 case RID_HAS_VIRTUAL_DESTRUCTOR:
3389 case RID_IS_ABSTRACT:
3390 case RID_IS_BASE_OF:
3392 case RID_IS_CONVERTIBLE_TO:
3396 case RID_IS_POLYMORPHIC:
3398 return cp_parser_trait_expr (parser, token->keyword);
3400 /* Objective-C++ expressions. */
3402 case RID_AT_PROTOCOL:
3403 case RID_AT_SELECTOR:
3404 return cp_parser_objc_expression (parser);
3407 cp_parser_error (parser, "expected primary-expression");
3408 return error_mark_node;
3411 /* An id-expression can start with either an identifier, a
3412 `::' as the beginning of a qualified-id, or the "operator"
3416 case CPP_TEMPLATE_ID:
3417 case CPP_NESTED_NAME_SPECIFIER:
3421 const char *error_msg;
3424 cp_token *id_expr_token;
3427 /* Parse the id-expression. */
3429 = cp_parser_id_expression (parser,
3430 /*template_keyword_p=*/false,
3431 /*check_dependency_p=*/true,
3433 /*declarator_p=*/false,
3434 /*optional_p=*/false);
3435 if (id_expression == error_mark_node)
3436 return error_mark_node;
3437 id_expr_token = token;
3438 token = cp_lexer_peek_token (parser->lexer);
3439 done = (token->type != CPP_OPEN_SQUARE
3440 && token->type != CPP_OPEN_PAREN
3441 && token->type != CPP_DOT
3442 && token->type != CPP_DEREF
3443 && token->type != CPP_PLUS_PLUS
3444 && token->type != CPP_MINUS_MINUS);
3445 /* If we have a template-id, then no further lookup is
3446 required. If the template-id was for a template-class, we
3447 will sometimes have a TYPE_DECL at this point. */
3448 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3449 || TREE_CODE (id_expression) == TYPE_DECL)
3450 decl = id_expression;
3451 /* Look up the name. */
3454 tree ambiguous_decls;
3456 decl = cp_parser_lookup_name (parser, id_expression,
3459 /*is_namespace=*/false,
3460 /*check_dependency=*/true,
3462 id_expr_token->location);
3463 /* If the lookup was ambiguous, an error will already have
3465 if (ambiguous_decls)
3466 return error_mark_node;
3468 /* In Objective-C++, an instance variable (ivar) may be preferred
3469 to whatever cp_parser_lookup_name() found. */
3470 decl = objc_lookup_ivar (decl, id_expression);
3472 /* If name lookup gives us a SCOPE_REF, then the
3473 qualifying scope was dependent. */
3474 if (TREE_CODE (decl) == SCOPE_REF)
3476 /* At this point, we do not know if DECL is a valid
3477 integral constant expression. We assume that it is
3478 in fact such an expression, so that code like:
3480 template <int N> struct A {
3484 is accepted. At template-instantiation time, we
3485 will check that B<N>::i is actually a constant. */
3488 /* Check to see if DECL is a local variable in a context
3489 where that is forbidden. */
3490 if (parser->local_variables_forbidden_p
3491 && local_variable_p (decl))
3493 /* It might be that we only found DECL because we are
3494 trying to be generous with pre-ISO scoping rules.
3495 For example, consider:
3499 for (int i = 0; i < 10; ++i) {}
3500 extern void f(int j = i);
3503 Here, name look up will originally find the out
3504 of scope `i'. We need to issue a warning message,
3505 but then use the global `i'. */
3506 decl = check_for_out_of_scope_variable (decl);
3507 if (local_variable_p (decl))
3509 error ("%Hlocal variable %qD may not appear in this context",
3510 &id_expr_token->location, decl);
3511 return error_mark_node;
3516 decl = (finish_id_expression
3517 (id_expression, decl, parser->scope,
3519 parser->integral_constant_expression_p,
3520 parser->allow_non_integral_constant_expression_p,
3521 &parser->non_integral_constant_expression_p,
3522 template_p, done, address_p,
3525 id_expr_token->location));
3527 cp_parser_error (parser, error_msg);
3531 /* Anything else is an error. */
3533 /* ...unless we have an Objective-C++ message or string literal,
3535 if (c_dialect_objc ()
3536 && (token->type == CPP_OPEN_SQUARE
3537 || token->type == CPP_OBJC_STRING))
3538 return cp_parser_objc_expression (parser);
3540 cp_parser_error (parser, "expected primary-expression");
3541 return error_mark_node;
3545 /* Parse an id-expression.
3552 :: [opt] nested-name-specifier template [opt] unqualified-id
3554 :: operator-function-id
3557 Return a representation of the unqualified portion of the
3558 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3559 a `::' or nested-name-specifier.
3561 Often, if the id-expression was a qualified-id, the caller will
3562 want to make a SCOPE_REF to represent the qualified-id. This
3563 function does not do this in order to avoid wastefully creating
3564 SCOPE_REFs when they are not required.
3566 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3569 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3570 uninstantiated templates.
3572 If *TEMPLATE_P is non-NULL, it is set to true iff the
3573 `template' keyword is used to explicitly indicate that the entity
3574 named is a template.
3576 If DECLARATOR_P is true, the id-expression is appearing as part of
3577 a declarator, rather than as part of an expression. */
3580 cp_parser_id_expression (cp_parser *parser,
3581 bool template_keyword_p,
3582 bool check_dependency_p,
3587 bool global_scope_p;
3588 bool nested_name_specifier_p;
3590 /* Assume the `template' keyword was not used. */
3592 *template_p = template_keyword_p;
3594 /* Look for the optional `::' operator. */
3596 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3598 /* Look for the optional nested-name-specifier. */
3599 nested_name_specifier_p
3600 = (cp_parser_nested_name_specifier_opt (parser,
3601 /*typename_keyword_p=*/false,
3606 /* If there is a nested-name-specifier, then we are looking at
3607 the first qualified-id production. */
3608 if (nested_name_specifier_p)
3611 tree saved_object_scope;
3612 tree saved_qualifying_scope;
3613 tree unqualified_id;
3616 /* See if the next token is the `template' keyword. */
3618 template_p = &is_template;
3619 *template_p = cp_parser_optional_template_keyword (parser);
3620 /* Name lookup we do during the processing of the
3621 unqualified-id might obliterate SCOPE. */
3622 saved_scope = parser->scope;
3623 saved_object_scope = parser->object_scope;
3624 saved_qualifying_scope = parser->qualifying_scope;
3625 /* Process the final unqualified-id. */
3626 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3629 /*optional_p=*/false);
3630 /* Restore the SAVED_SCOPE for our caller. */
3631 parser->scope = saved_scope;
3632 parser->object_scope = saved_object_scope;
3633 parser->qualifying_scope = saved_qualifying_scope;
3635 return unqualified_id;
3637 /* Otherwise, if we are in global scope, then we are looking at one
3638 of the other qualified-id productions. */
3639 else if (global_scope_p)
3644 /* Peek at the next token. */
3645 token = cp_lexer_peek_token (parser->lexer);
3647 /* If it's an identifier, and the next token is not a "<", then
3648 we can avoid the template-id case. This is an optimization
3649 for this common case. */
3650 if (token->type == CPP_NAME
3651 && !cp_parser_nth_token_starts_template_argument_list_p
3653 return cp_parser_identifier (parser);
3655 cp_parser_parse_tentatively (parser);
3656 /* Try a template-id. */
3657 id = cp_parser_template_id (parser,
3658 /*template_keyword_p=*/false,
3659 /*check_dependency_p=*/true,
3661 /* If that worked, we're done. */
3662 if (cp_parser_parse_definitely (parser))
3665 /* Peek at the next token. (Changes in the token buffer may
3666 have invalidated the pointer obtained above.) */
3667 token = cp_lexer_peek_token (parser->lexer);
3669 switch (token->type)
3672 return cp_parser_identifier (parser);
3675 if (token->keyword == RID_OPERATOR)
3676 return cp_parser_operator_function_id (parser);
3680 cp_parser_error (parser, "expected id-expression");
3681 return error_mark_node;
3685 return cp_parser_unqualified_id (parser, template_keyword_p,
3686 /*check_dependency_p=*/true,
3691 /* Parse an unqualified-id.
3695 operator-function-id
3696 conversion-function-id
3700 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3701 keyword, in a construct like `A::template ...'.
3703 Returns a representation of unqualified-id. For the `identifier'
3704 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3705 production a BIT_NOT_EXPR is returned; the operand of the
3706 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3707 other productions, see the documentation accompanying the
3708 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3709 names are looked up in uninstantiated templates. If DECLARATOR_P
3710 is true, the unqualified-id is appearing as part of a declarator,
3711 rather than as part of an expression. */
3714 cp_parser_unqualified_id (cp_parser* parser,
3715 bool template_keyword_p,
3716 bool check_dependency_p,
3722 /* Peek at the next token. */
3723 token = cp_lexer_peek_token (parser->lexer);
3725 switch (token->type)
3731 /* We don't know yet whether or not this will be a
3733 cp_parser_parse_tentatively (parser);
3734 /* Try a template-id. */
3735 id = cp_parser_template_id (parser, template_keyword_p,
3738 /* If it worked, we're done. */
3739 if (cp_parser_parse_definitely (parser))
3741 /* Otherwise, it's an ordinary identifier. */
3742 return cp_parser_identifier (parser);
3745 case CPP_TEMPLATE_ID:
3746 return cp_parser_template_id (parser, template_keyword_p,
3753 tree qualifying_scope;
3758 /* Consume the `~' token. */
3759 cp_lexer_consume_token (parser->lexer);
3760 /* Parse the class-name. The standard, as written, seems to
3763 template <typename T> struct S { ~S (); };
3764 template <typename T> S<T>::~S() {}
3766 is invalid, since `~' must be followed by a class-name, but
3767 `S<T>' is dependent, and so not known to be a class.
3768 That's not right; we need to look in uninstantiated
3769 templates. A further complication arises from:
3771 template <typename T> void f(T t) {
3775 Here, it is not possible to look up `T' in the scope of `T'
3776 itself. We must look in both the current scope, and the
3777 scope of the containing complete expression.
3779 Yet another issue is:
3788 The standard does not seem to say that the `S' in `~S'
3789 should refer to the type `S' and not the data member
3792 /* DR 244 says that we look up the name after the "~" in the
3793 same scope as we looked up the qualifying name. That idea
3794 isn't fully worked out; it's more complicated than that. */
3795 scope = parser->scope;
3796 object_scope = parser->object_scope;
3797 qualifying_scope = parser->qualifying_scope;
3799 /* Check for invalid scopes. */
3800 if (scope == error_mark_node)
3802 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3803 cp_lexer_consume_token (parser->lexer);
3804 return error_mark_node;
3806 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3808 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3809 error ("%Hscope %qT before %<~%> is not a class-name",
3810 &token->location, scope);
3811 cp_parser_simulate_error (parser);
3812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3813 cp_lexer_consume_token (parser->lexer);
3814 return error_mark_node;
3816 gcc_assert (!scope || TYPE_P (scope));
3818 /* If the name is of the form "X::~X" it's OK. */
3819 token = cp_lexer_peek_token (parser->lexer);
3821 && token->type == CPP_NAME
3822 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3824 && constructor_name_p (token->u.value, scope))
3826 cp_lexer_consume_token (parser->lexer);
3827 return build_nt (BIT_NOT_EXPR, scope);
3830 /* If there was an explicit qualification (S::~T), first look
3831 in the scope given by the qualification (i.e., S). */
3833 type_decl = NULL_TREE;
3836 cp_parser_parse_tentatively (parser);
3837 type_decl = cp_parser_class_name (parser,
3838 /*typename_keyword_p=*/false,
3839 /*template_keyword_p=*/false,
3841 /*check_dependency=*/false,
3842 /*class_head_p=*/false,
3844 if (cp_parser_parse_definitely (parser))
3847 /* In "N::S::~S", look in "N" as well. */
3848 if (!done && scope && qualifying_scope)
3850 cp_parser_parse_tentatively (parser);
3851 parser->scope = qualifying_scope;
3852 parser->object_scope = NULL_TREE;
3853 parser->qualifying_scope = NULL_TREE;
3855 = cp_parser_class_name (parser,
3856 /*typename_keyword_p=*/false,
3857 /*template_keyword_p=*/false,
3859 /*check_dependency=*/false,
3860 /*class_head_p=*/false,
3862 if (cp_parser_parse_definitely (parser))
3865 /* In "p->S::~T", look in the scope given by "*p" as well. */
3866 else if (!done && object_scope)
3868 cp_parser_parse_tentatively (parser);
3869 parser->scope = object_scope;
3870 parser->object_scope = NULL_TREE;
3871 parser->qualifying_scope = NULL_TREE;
3873 = cp_parser_class_name (parser,
3874 /*typename_keyword_p=*/false,
3875 /*template_keyword_p=*/false,
3877 /*check_dependency=*/false,
3878 /*class_head_p=*/false,
3880 if (cp_parser_parse_definitely (parser))
3883 /* Look in the surrounding context. */
3886 parser->scope = NULL_TREE;
3887 parser->object_scope = NULL_TREE;
3888 parser->qualifying_scope = NULL_TREE;
3889 if (processing_template_decl)
3890 cp_parser_parse_tentatively (parser);
3892 = cp_parser_class_name (parser,
3893 /*typename_keyword_p=*/false,
3894 /*template_keyword_p=*/false,
3896 /*check_dependency=*/false,
3897 /*class_head_p=*/false,
3899 if (processing_template_decl
3900 && ! cp_parser_parse_definitely (parser))
3902 /* We couldn't find a type with this name, so just accept
3903 it and check for a match at instantiation time. */
3904 type_decl = cp_parser_identifier (parser);
3905 if (type_decl != error_mark_node)
3906 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3910 /* If an error occurred, assume that the name of the
3911 destructor is the same as the name of the qualifying
3912 class. That allows us to keep parsing after running
3913 into ill-formed destructor names. */
3914 if (type_decl == error_mark_node && scope)
3915 return build_nt (BIT_NOT_EXPR, scope);
3916 else if (type_decl == error_mark_node)
3917 return error_mark_node;
3919 /* Check that destructor name and scope match. */
3920 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3922 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3923 error ("%Hdeclaration of %<~%T%> as member of %qT",
3924 &token->location, type_decl, scope);
3925 cp_parser_simulate_error (parser);
3926 return error_mark_node;
3931 A typedef-name that names a class shall not be used as the
3932 identifier in the declarator for a destructor declaration. */
3934 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3935 && !DECL_SELF_REFERENCE_P (type_decl)
3936 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3937 error ("%Htypedef-name %qD used as destructor declarator",
3938 &token->location, type_decl);
3940 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3944 if (token->keyword == RID_OPERATOR)
3948 /* This could be a template-id, so we try that first. */
3949 cp_parser_parse_tentatively (parser);
3950 /* Try a template-id. */
3951 id = cp_parser_template_id (parser, template_keyword_p,
3952 /*check_dependency_p=*/true,
3954 /* If that worked, we're done. */
3955 if (cp_parser_parse_definitely (parser))
3957 /* We still don't know whether we're looking at an
3958 operator-function-id or a conversion-function-id. */
3959 cp_parser_parse_tentatively (parser);
3960 /* Try an operator-function-id. */
3961 id = cp_parser_operator_function_id (parser);
3962 /* If that didn't work, try a conversion-function-id. */
3963 if (!cp_parser_parse_definitely (parser))
3964 id = cp_parser_conversion_function_id (parser);
3973 cp_parser_error (parser, "expected unqualified-id");
3974 return error_mark_node;
3978 /* Parse an (optional) nested-name-specifier.
3980 nested-name-specifier: [C++98]
3981 class-or-namespace-name :: nested-name-specifier [opt]
3982 class-or-namespace-name :: template nested-name-specifier [opt]
3984 nested-name-specifier: [C++0x]
3987 nested-name-specifier identifier ::
3988 nested-name-specifier template [opt] simple-template-id ::
3990 PARSER->SCOPE should be set appropriately before this function is
3991 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3992 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3995 Sets PARSER->SCOPE to the class (TYPE) or namespace
3996 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3997 it unchanged if there is no nested-name-specifier. Returns the new
3998 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4000 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4001 part of a declaration and/or decl-specifier. */
4004 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4005 bool typename_keyword_p,
4006 bool check_dependency_p,
4008 bool is_declaration)
4010 bool success = false;
4011 cp_token_position start = 0;
4014 /* Remember where the nested-name-specifier starts. */
4015 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4017 start = cp_lexer_token_position (parser->lexer, false);
4018 push_deferring_access_checks (dk_deferred);
4025 tree saved_qualifying_scope;
4026 bool template_keyword_p;
4028 /* Spot cases that cannot be the beginning of a
4029 nested-name-specifier. */
4030 token = cp_lexer_peek_token (parser->lexer);
4032 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4033 the already parsed nested-name-specifier. */
4034 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4036 /* Grab the nested-name-specifier and continue the loop. */
4037 cp_parser_pre_parsed_nested_name_specifier (parser);
4038 /* If we originally encountered this nested-name-specifier
4039 with IS_DECLARATION set to false, we will not have
4040 resolved TYPENAME_TYPEs, so we must do so here. */
4042 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4044 new_scope = resolve_typename_type (parser->scope,
4045 /*only_current_p=*/false);
4046 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4047 parser->scope = new_scope;
4053 /* Spot cases that cannot be the beginning of a
4054 nested-name-specifier. On the second and subsequent times
4055 through the loop, we look for the `template' keyword. */
4056 if (success && token->keyword == RID_TEMPLATE)
4058 /* A template-id can start a nested-name-specifier. */
4059 else if (token->type == CPP_TEMPLATE_ID)
4063 /* If the next token is not an identifier, then it is
4064 definitely not a type-name or namespace-name. */
4065 if (token->type != CPP_NAME)
4067 /* If the following token is neither a `<' (to begin a
4068 template-id), nor a `::', then we are not looking at a
4069 nested-name-specifier. */
4070 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4071 if (token->type != CPP_SCOPE
4072 && !cp_parser_nth_token_starts_template_argument_list_p
4077 /* The nested-name-specifier is optional, so we parse
4079 cp_parser_parse_tentatively (parser);
4081 /* Look for the optional `template' keyword, if this isn't the
4082 first time through the loop. */
4084 template_keyword_p = cp_parser_optional_template_keyword (parser);
4086 template_keyword_p = false;
4088 /* Save the old scope since the name lookup we are about to do
4089 might destroy it. */
4090 old_scope = parser->scope;
4091 saved_qualifying_scope = parser->qualifying_scope;
4092 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4093 look up names in "X<T>::I" in order to determine that "Y" is
4094 a template. So, if we have a typename at this point, we make
4095 an effort to look through it. */
4097 && !typename_keyword_p
4099 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4100 parser->scope = resolve_typename_type (parser->scope,
4101 /*only_current_p=*/false);
4102 /* Parse the qualifying entity. */
4104 = cp_parser_qualifying_entity (parser,
4110 /* Look for the `::' token. */
4111 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4113 /* If we found what we wanted, we keep going; otherwise, we're
4115 if (!cp_parser_parse_definitely (parser))
4117 bool error_p = false;
4119 /* Restore the OLD_SCOPE since it was valid before the
4120 failed attempt at finding the last
4121 class-or-namespace-name. */
4122 parser->scope = old_scope;
4123 parser->qualifying_scope = saved_qualifying_scope;
4124 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4126 /* If the next token is an identifier, and the one after
4127 that is a `::', then any valid interpretation would have
4128 found a class-or-namespace-name. */
4129 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4130 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4132 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4135 token = cp_lexer_consume_token (parser->lexer);
4138 if (!token->ambiguous_p)
4141 tree ambiguous_decls;
4143 decl = cp_parser_lookup_name (parser, token->u.value,
4145 /*is_template=*/false,
4146 /*is_namespace=*/false,
4147 /*check_dependency=*/true,
4150 if (TREE_CODE (decl) == TEMPLATE_DECL)
4151 error ("%H%qD used without template parameters",
4152 &token->location, decl);
4153 else if (ambiguous_decls)
4155 error ("%Hreference to %qD is ambiguous",
4156 &token->location, token->u.value);
4157 print_candidates (ambiguous_decls);
4158 decl = error_mark_node;
4162 const char* msg = "is not a class or namespace";
4163 if (cxx_dialect != cxx98)
4164 msg = "is not a class, namespace, or enumeration";
4165 cp_parser_name_lookup_error
4166 (parser, token->u.value, decl, msg,
4170 parser->scope = error_mark_node;
4172 /* Treat this as a successful nested-name-specifier
4177 If the name found is not a class-name (clause
4178 _class_) or namespace-name (_namespace.def_), the
4179 program is ill-formed. */
4182 cp_lexer_consume_token (parser->lexer);
4186 /* We've found one valid nested-name-specifier. */
4188 /* Name lookup always gives us a DECL. */
4189 if (TREE_CODE (new_scope) == TYPE_DECL)
4190 new_scope = TREE_TYPE (new_scope);
4191 /* Uses of "template" must be followed by actual templates. */
4192 if (template_keyword_p
4193 && !(CLASS_TYPE_P (new_scope)
4194 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4195 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4196 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4197 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4198 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4199 == TEMPLATE_ID_EXPR)))
4200 permerror (input_location, TYPE_P (new_scope)
4201 ? "%qT is not a template"
4202 : "%qD is not a template",
4204 /* If it is a class scope, try to complete it; we are about to
4205 be looking up names inside the class. */
4206 if (TYPE_P (new_scope)
4207 /* Since checking types for dependency can be expensive,
4208 avoid doing it if the type is already complete. */
4209 && !COMPLETE_TYPE_P (new_scope)
4210 /* Do not try to complete dependent types. */
4211 && !dependent_type_p (new_scope))
4213 new_scope = complete_type (new_scope);
4214 /* If it is a typedef to current class, use the current
4215 class instead, as the typedef won't have any names inside
4217 if (!COMPLETE_TYPE_P (new_scope)
4218 && currently_open_class (new_scope))
4219 new_scope = TYPE_MAIN_VARIANT (new_scope);
4221 /* Make sure we look in the right scope the next time through
4223 parser->scope = new_scope;
4226 /* If parsing tentatively, replace the sequence of tokens that makes
4227 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4228 token. That way, should we re-parse the token stream, we will
4229 not have to repeat the effort required to do the parse, nor will
4230 we issue duplicate error messages. */
4231 if (success && start)
4235 token = cp_lexer_token_at (parser->lexer, start);
4236 /* Reset the contents of the START token. */
4237 token->type = CPP_NESTED_NAME_SPECIFIER;
4238 /* Retrieve any deferred checks. Do not pop this access checks yet
4239 so the memory will not be reclaimed during token replacing below. */
4240 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4241 token->u.tree_check_value->value = parser->scope;
4242 token->u.tree_check_value->checks = get_deferred_access_checks ();
4243 token->u.tree_check_value->qualifying_scope =
4244 parser->qualifying_scope;
4245 token->keyword = RID_MAX;
4247 /* Purge all subsequent tokens. */
4248 cp_lexer_purge_tokens_after (parser->lexer, start);
4252 pop_to_parent_deferring_access_checks ();
4254 return success ? parser->scope : NULL_TREE;
4257 /* Parse a nested-name-specifier. See
4258 cp_parser_nested_name_specifier_opt for details. This function
4259 behaves identically, except that it will an issue an error if no
4260 nested-name-specifier is present. */
4263 cp_parser_nested_name_specifier (cp_parser *parser,
4264 bool typename_keyword_p,
4265 bool check_dependency_p,
4267 bool is_declaration)
4271 /* Look for the nested-name-specifier. */
4272 scope = cp_parser_nested_name_specifier_opt (parser,
4277 /* If it was not present, issue an error message. */
4280 cp_parser_error (parser, "expected nested-name-specifier");
4281 parser->scope = NULL_TREE;
4287 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4288 this is either a class-name or a namespace-name (which corresponds
4289 to the class-or-namespace-name production in the grammar). For
4290 C++0x, it can also be a type-name that refers to an enumeration
4293 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4294 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4295 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4296 TYPE_P is TRUE iff the next name should be taken as a class-name,
4297 even the same name is declared to be another entity in the same
4300 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4301 specified by the class-or-namespace-name. If neither is found the
4302 ERROR_MARK_NODE is returned. */
4305 cp_parser_qualifying_entity (cp_parser *parser,
4306 bool typename_keyword_p,
4307 bool template_keyword_p,
4308 bool check_dependency_p,
4310 bool is_declaration)
4313 tree saved_qualifying_scope;
4314 tree saved_object_scope;
4317 bool successful_parse_p;
4319 /* Before we try to parse the class-name, we must save away the
4320 current PARSER->SCOPE since cp_parser_class_name will destroy
4322 saved_scope = parser->scope;
4323 saved_qualifying_scope = parser->qualifying_scope;
4324 saved_object_scope = parser->object_scope;
4325 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4326 there is no need to look for a namespace-name. */
4327 only_class_p = template_keyword_p
4328 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4330 cp_parser_parse_tentatively (parser);
4331 scope = cp_parser_class_name (parser,
4334 type_p ? class_type : none_type,
4336 /*class_head_p=*/false,
4338 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4339 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4341 && cxx_dialect != cxx98
4342 && !successful_parse_p)
4344 /* Restore the saved scope. */
4345 parser->scope = saved_scope;
4346 parser->qualifying_scope = saved_qualifying_scope;
4347 parser->object_scope = saved_object_scope;
4349 /* Parse tentatively. */
4350 cp_parser_parse_tentatively (parser);
4352 /* Parse a typedef-name or enum-name. */
4353 scope = cp_parser_nonclass_name (parser);
4354 successful_parse_p = cp_parser_parse_definitely (parser);
4356 /* If that didn't work, try for a namespace-name. */
4357 if (!only_class_p && !successful_parse_p)
4359 /* Restore the saved scope. */
4360 parser->scope = saved_scope;
4361 parser->qualifying_scope = saved_qualifying_scope;
4362 parser->object_scope = saved_object_scope;
4363 /* If we are not looking at an identifier followed by the scope
4364 resolution operator, then this is not part of a
4365 nested-name-specifier. (Note that this function is only used
4366 to parse the components of a nested-name-specifier.) */
4367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4368 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4369 return error_mark_node;
4370 scope = cp_parser_namespace_name (parser);
4376 /* Parse a postfix-expression.
4380 postfix-expression [ expression ]
4381 postfix-expression ( expression-list [opt] )
4382 simple-type-specifier ( expression-list [opt] )
4383 typename :: [opt] nested-name-specifier identifier
4384 ( expression-list [opt] )
4385 typename :: [opt] nested-name-specifier template [opt] template-id
4386 ( expression-list [opt] )
4387 postfix-expression . template [opt] id-expression
4388 postfix-expression -> template [opt] id-expression
4389 postfix-expression . pseudo-destructor-name
4390 postfix-expression -> pseudo-destructor-name
4391 postfix-expression ++
4392 postfix-expression --
4393 dynamic_cast < type-id > ( expression )
4394 static_cast < type-id > ( expression )
4395 reinterpret_cast < type-id > ( expression )
4396 const_cast < type-id > ( expression )
4397 typeid ( expression )
4403 ( type-id ) { initializer-list , [opt] }
4405 This extension is a GNU version of the C99 compound-literal
4406 construct. (The C99 grammar uses `type-name' instead of `type-id',
4407 but they are essentially the same concept.)
4409 If ADDRESS_P is true, the postfix expression is the operand of the
4410 `&' operator. CAST_P is true if this expression is the target of a
4413 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4414 class member access expressions [expr.ref].
4416 Returns a representation of the expression. */
4419 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4420 bool member_access_only_p,
4421 cp_id_kind * pidk_return)
4425 cp_id_kind idk = CP_ID_KIND_NONE;
4426 tree postfix_expression = NULL_TREE;
4427 bool is_member_access = false;
4429 /* Peek at the next token. */
4430 token = cp_lexer_peek_token (parser->lexer);
4431 /* Some of the productions are determined by keywords. */
4432 keyword = token->keyword;
4442 const char *saved_message;
4444 /* All of these can be handled in the same way from the point
4445 of view of parsing. Begin by consuming the token
4446 identifying the cast. */
4447 cp_lexer_consume_token (parser->lexer);
4449 /* New types cannot be defined in the cast. */
4450 saved_message = parser->type_definition_forbidden_message;
4451 parser->type_definition_forbidden_message
4452 = "types may not be defined in casts";
4454 /* Look for the opening `<'. */
4455 cp_parser_require (parser, CPP_LESS, "%<<%>");
4456 /* Parse the type to which we are casting. */
4457 type = cp_parser_type_id (parser);
4458 /* Look for the closing `>'. */
4459 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4460 /* Restore the old message. */
4461 parser->type_definition_forbidden_message = saved_message;
4463 /* And the expression which is being cast. */
4464 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4465 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4466 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4468 /* Only type conversions to integral or enumeration types
4469 can be used in constant-expressions. */
4470 if (!cast_valid_in_integral_constant_expression_p (type)
4471 && (cp_parser_non_integral_constant_expression
4473 "a cast to a type other than an integral or "
4474 "enumeration type")))
4475 return error_mark_node;
4481 = build_dynamic_cast (type, expression, tf_warning_or_error);
4485 = build_static_cast (type, expression, tf_warning_or_error);
4489 = build_reinterpret_cast (type, expression,
4490 tf_warning_or_error);
4494 = build_const_cast (type, expression, tf_warning_or_error);
4505 const char *saved_message;
4506 bool saved_in_type_id_in_expr_p;
4508 /* Consume the `typeid' token. */
4509 cp_lexer_consume_token (parser->lexer);
4510 /* Look for the `(' token. */
4511 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4512 /* Types cannot be defined in a `typeid' expression. */
4513 saved_message = parser->type_definition_forbidden_message;
4514 parser->type_definition_forbidden_message
4515 = "types may not be defined in a %<typeid%> expression";
4516 /* We can't be sure yet whether we're looking at a type-id or an
4518 cp_parser_parse_tentatively (parser);
4519 /* Try a type-id first. */
4520 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4521 parser->in_type_id_in_expr_p = true;
4522 type = cp_parser_type_id (parser);
4523 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4524 /* Look for the `)' token. Otherwise, we can't be sure that
4525 we're not looking at an expression: consider `typeid (int
4526 (3))', for example. */
4527 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4528 /* If all went well, simply lookup the type-id. */
4529 if (cp_parser_parse_definitely (parser))
4530 postfix_expression = get_typeid (type);
4531 /* Otherwise, fall back to the expression variant. */
4536 /* Look for an expression. */
4537 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4538 /* Compute its typeid. */
4539 postfix_expression = build_typeid (expression);
4540 /* Look for the `)' token. */
4541 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4543 /* Restore the saved message. */
4544 parser->type_definition_forbidden_message = saved_message;
4545 /* `typeid' may not appear in an integral constant expression. */
4546 if (cp_parser_non_integral_constant_expression(parser,
4547 "%<typeid%> operator"))
4548 return error_mark_node;
4555 /* The syntax permitted here is the same permitted for an
4556 elaborated-type-specifier. */
4557 type = cp_parser_elaborated_type_specifier (parser,
4558 /*is_friend=*/false,
4559 /*is_declaration=*/false);
4560 postfix_expression = cp_parser_functional_cast (parser, type);
4568 /* If the next thing is a simple-type-specifier, we may be
4569 looking at a functional cast. We could also be looking at
4570 an id-expression. So, we try the functional cast, and if
4571 that doesn't work we fall back to the primary-expression. */
4572 cp_parser_parse_tentatively (parser);
4573 /* Look for the simple-type-specifier. */
4574 type = cp_parser_simple_type_specifier (parser,
4575 /*decl_specs=*/NULL,
4576 CP_PARSER_FLAGS_NONE);
4577 /* Parse the cast itself. */
4578 if (!cp_parser_error_occurred (parser))
4580 = cp_parser_functional_cast (parser, type);
4581 /* If that worked, we're done. */
4582 if (cp_parser_parse_definitely (parser))
4585 /* If the functional-cast didn't work out, try a
4586 compound-literal. */
4587 if (cp_parser_allow_gnu_extensions_p (parser)
4588 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4590 VEC(constructor_elt,gc) *initializer_list = NULL;
4591 bool saved_in_type_id_in_expr_p;
4593 cp_parser_parse_tentatively (parser);
4594 /* Consume the `('. */
4595 cp_lexer_consume_token (parser->lexer);
4596 /* Parse the type. */
4597 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4598 parser->in_type_id_in_expr_p = true;
4599 type = cp_parser_type_id (parser);
4600 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4601 /* Look for the `)'. */
4602 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4603 /* Look for the `{'. */
4604 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4605 /* If things aren't going well, there's no need to
4607 if (!cp_parser_error_occurred (parser))
4609 bool non_constant_p;
4610 /* Parse the initializer-list. */
4612 = cp_parser_initializer_list (parser, &non_constant_p);
4613 /* Allow a trailing `,'. */
4614 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4615 cp_lexer_consume_token (parser->lexer);
4616 /* Look for the final `}'. */
4617 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4619 /* If that worked, we're definitely looking at a
4620 compound-literal expression. */
4621 if (cp_parser_parse_definitely (parser))
4623 /* Warn the user that a compound literal is not
4624 allowed in standard C++. */
4625 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4626 /* For simplicity, we disallow compound literals in
4627 constant-expressions. We could
4628 allow compound literals of integer type, whose
4629 initializer was a constant, in constant
4630 expressions. Permitting that usage, as a further
4631 extension, would not change the meaning of any
4632 currently accepted programs. (Of course, as
4633 compound literals are not part of ISO C++, the
4634 standard has nothing to say.) */
4635 if (cp_parser_non_integral_constant_expression
4636 (parser, "non-constant compound literals"))
4638 postfix_expression = error_mark_node;
4641 /* Form the representation of the compound-literal. */
4643 = (finish_compound_literal
4644 (type, build_constructor (init_list_type_node,
4645 initializer_list)));
4650 /* It must be a primary-expression. */
4652 = cp_parser_primary_expression (parser, address_p, cast_p,
4653 /*template_arg_p=*/false,
4659 /* Keep looping until the postfix-expression is complete. */
4662 if (idk == CP_ID_KIND_UNQUALIFIED
4663 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4664 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4665 /* It is not a Koenig lookup function call. */
4667 = unqualified_name_lookup_error (postfix_expression);
4669 /* Peek at the next token. */
4670 token = cp_lexer_peek_token (parser->lexer);
4672 switch (token->type)
4674 case CPP_OPEN_SQUARE:
4676 = cp_parser_postfix_open_square_expression (parser,
4679 idk = CP_ID_KIND_NONE;
4680 is_member_access = false;
4683 case CPP_OPEN_PAREN:
4684 /* postfix-expression ( expression-list [opt] ) */
4687 bool is_builtin_constant_p;
4688 bool saved_integral_constant_expression_p = false;
4689 bool saved_non_integral_constant_expression_p = false;
4692 is_member_access = false;
4694 is_builtin_constant_p
4695 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4696 if (is_builtin_constant_p)
4698 /* The whole point of __builtin_constant_p is to allow
4699 non-constant expressions to appear as arguments. */
4700 saved_integral_constant_expression_p
4701 = parser->integral_constant_expression_p;
4702 saved_non_integral_constant_expression_p
4703 = parser->non_integral_constant_expression_p;
4704 parser->integral_constant_expression_p = false;
4706 args = (cp_parser_parenthesized_expression_list
4707 (parser, /*is_attribute_list=*/false,
4708 /*cast_p=*/false, /*allow_expansion_p=*/true,
4709 /*non_constant_p=*/NULL));
4710 if (is_builtin_constant_p)
4712 parser->integral_constant_expression_p
4713 = saved_integral_constant_expression_p;
4714 parser->non_integral_constant_expression_p
4715 = saved_non_integral_constant_expression_p;
4718 if (args == error_mark_node)
4720 postfix_expression = error_mark_node;
4724 /* Function calls are not permitted in
4725 constant-expressions. */
4726 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4727 && cp_parser_non_integral_constant_expression (parser,
4730 postfix_expression = error_mark_node;
4735 if (idk == CP_ID_KIND_UNQUALIFIED)
4737 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4743 = perform_koenig_lookup (postfix_expression, args);
4747 = unqualified_fn_lookup_error (postfix_expression);
4749 /* We do not perform argument-dependent lookup if
4750 normal lookup finds a non-function, in accordance
4751 with the expected resolution of DR 218. */
4752 else if (args && is_overloaded_fn (postfix_expression))
4754 tree fn = get_first_fn (postfix_expression);
4756 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4757 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4759 /* Only do argument dependent lookup if regular
4760 lookup does not find a set of member functions.
4761 [basic.lookup.koenig]/2a */
4762 if (!DECL_FUNCTION_MEMBER_P (fn))
4766 = perform_koenig_lookup (postfix_expression, args);
4771 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4773 tree instance = TREE_OPERAND (postfix_expression, 0);
4774 tree fn = TREE_OPERAND (postfix_expression, 1);
4776 if (processing_template_decl
4777 && (type_dependent_expression_p (instance)
4778 || (!BASELINK_P (fn)
4779 && TREE_CODE (fn) != FIELD_DECL)
4780 || type_dependent_expression_p (fn)
4781 || any_type_dependent_arguments_p (args)))
4784 = build_nt_call_list (postfix_expression, args);
4788 if (BASELINK_P (fn))
4791 = (build_new_method_call
4792 (instance, fn, args, NULL_TREE,
4793 (idk == CP_ID_KIND_QUALIFIED
4794 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4796 tf_warning_or_error));
4800 = finish_call_expr (postfix_expression, args,
4801 /*disallow_virtual=*/false,
4803 tf_warning_or_error);
4805 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4806 || TREE_CODE (postfix_expression) == MEMBER_REF
4807 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4808 postfix_expression = (build_offset_ref_call_from_tree
4809 (postfix_expression, args));
4810 else if (idk == CP_ID_KIND_QUALIFIED)
4811 /* A call to a static class member, or a namespace-scope
4814 = finish_call_expr (postfix_expression, args,
4815 /*disallow_virtual=*/true,
4817 tf_warning_or_error);
4819 /* All other function calls. */
4821 = finish_call_expr (postfix_expression, args,
4822 /*disallow_virtual=*/false,
4824 tf_warning_or_error);
4826 if (warn_disallowed_functions)
4827 warn_if_disallowed_function_p (postfix_expression);
4829 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4830 idk = CP_ID_KIND_NONE;
4836 /* postfix-expression . template [opt] id-expression
4837 postfix-expression . pseudo-destructor-name
4838 postfix-expression -> template [opt] id-expression
4839 postfix-expression -> pseudo-destructor-name */
4841 /* Consume the `.' or `->' operator. */
4842 cp_lexer_consume_token (parser->lexer);
4845 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4850 is_member_access = true;
4854 /* postfix-expression ++ */
4855 /* Consume the `++' token. */
4856 cp_lexer_consume_token (parser->lexer);
4857 /* Generate a representation for the complete expression. */
4859 = finish_increment_expr (postfix_expression,
4860 POSTINCREMENT_EXPR);
4861 /* Increments may not appear in constant-expressions. */
4862 if (cp_parser_non_integral_constant_expression (parser,
4864 postfix_expression = error_mark_node;
4865 idk = CP_ID_KIND_NONE;
4866 is_member_access = false;
4869 case CPP_MINUS_MINUS:
4870 /* postfix-expression -- */
4871 /* Consume the `--' token. */
4872 cp_lexer_consume_token (parser->lexer);
4873 /* Generate a representation for the complete expression. */
4875 = finish_increment_expr (postfix_expression,
4876 POSTDECREMENT_EXPR);
4877 /* Decrements may not appear in constant-expressions. */
4878 if (cp_parser_non_integral_constant_expression (parser,
4880 postfix_expression = error_mark_node;
4881 idk = CP_ID_KIND_NONE;
4882 is_member_access = false;
4886 if (pidk_return != NULL)
4887 * pidk_return = idk;
4888 if (member_access_only_p)
4889 return is_member_access? postfix_expression : error_mark_node;
4891 return postfix_expression;
4895 /* We should never get here. */
4897 return error_mark_node;
4900 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4901 by cp_parser_builtin_offsetof. We're looking for
4903 postfix-expression [ expression ]
4905 FOR_OFFSETOF is set if we're being called in that context, which
4906 changes how we deal with integer constant expressions. */
4909 cp_parser_postfix_open_square_expression (cp_parser *parser,
4910 tree postfix_expression,
4915 /* Consume the `[' token. */
4916 cp_lexer_consume_token (parser->lexer);
4918 /* Parse the index expression. */
4919 /* ??? For offsetof, there is a question of what to allow here. If
4920 offsetof is not being used in an integral constant expression context,
4921 then we *could* get the right answer by computing the value at runtime.
4922 If we are in an integral constant expression context, then we might
4923 could accept any constant expression; hard to say without analysis.
4924 Rather than open the barn door too wide right away, allow only integer
4925 constant expressions here. */
4927 index = cp_parser_constant_expression (parser, false, NULL);
4929 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4931 /* Look for the closing `]'. */
4932 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4934 /* Build the ARRAY_REF. */
4935 postfix_expression = grok_array_decl (postfix_expression, index);
4937 /* When not doing offsetof, array references are not permitted in
4938 constant-expressions. */
4940 && (cp_parser_non_integral_constant_expression
4941 (parser, "an array reference")))
4942 postfix_expression = error_mark_node;
4944 return postfix_expression;
4947 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4948 by cp_parser_builtin_offsetof. We're looking for
4950 postfix-expression . template [opt] id-expression
4951 postfix-expression . pseudo-destructor-name
4952 postfix-expression -> template [opt] id-expression
4953 postfix-expression -> pseudo-destructor-name
4955 FOR_OFFSETOF is set if we're being called in that context. That sorta
4956 limits what of the above we'll actually accept, but nevermind.
4957 TOKEN_TYPE is the "." or "->" token, which will already have been
4958 removed from the stream. */
4961 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4962 enum cpp_ttype token_type,
4963 tree postfix_expression,
4964 bool for_offsetof, cp_id_kind *idk,
4965 location_t location)
4969 bool pseudo_destructor_p;
4970 tree scope = NULL_TREE;
4972 /* If this is a `->' operator, dereference the pointer. */
4973 if (token_type == CPP_DEREF)
4974 postfix_expression = build_x_arrow (postfix_expression);
4975 /* Check to see whether or not the expression is type-dependent. */
4976 dependent_p = type_dependent_expression_p (postfix_expression);
4977 /* The identifier following the `->' or `.' is not qualified. */
4978 parser->scope = NULL_TREE;
4979 parser->qualifying_scope = NULL_TREE;
4980 parser->object_scope = NULL_TREE;
4981 *idk = CP_ID_KIND_NONE;
4983 /* Enter the scope corresponding to the type of the object
4984 given by the POSTFIX_EXPRESSION. */
4985 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4987 scope = TREE_TYPE (postfix_expression);
4988 /* According to the standard, no expression should ever have
4989 reference type. Unfortunately, we do not currently match
4990 the standard in this respect in that our internal representation
4991 of an expression may have reference type even when the standard
4992 says it does not. Therefore, we have to manually obtain the
4993 underlying type here. */
4994 scope = non_reference (scope);
4995 /* The type of the POSTFIX_EXPRESSION must be complete. */
4996 if (scope == unknown_type_node)
4998 error ("%H%qE does not have class type", &location, postfix_expression);
5002 scope = complete_type_or_else (scope, NULL_TREE);
5003 /* Let the name lookup machinery know that we are processing a
5004 class member access expression. */
5005 parser->context->object_type = scope;
5006 /* If something went wrong, we want to be able to discern that case,
5007 as opposed to the case where there was no SCOPE due to the type
5008 of expression being dependent. */
5010 scope = error_mark_node;
5011 /* If the SCOPE was erroneous, make the various semantic analysis
5012 functions exit quickly -- and without issuing additional error
5014 if (scope == error_mark_node)
5015 postfix_expression = error_mark_node;
5018 /* Assume this expression is not a pseudo-destructor access. */
5019 pseudo_destructor_p = false;
5021 /* If the SCOPE is a scalar type, then, if this is a valid program,
5022 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5023 is type dependent, it can be pseudo-destructor-name or something else.
5024 Try to parse it as pseudo-destructor-name first. */
5025 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5030 cp_parser_parse_tentatively (parser);
5031 /* Parse the pseudo-destructor-name. */
5033 cp_parser_pseudo_destructor_name (parser, &s, &type);
5035 && (cp_parser_error_occurred (parser)
5036 || TREE_CODE (type) != TYPE_DECL
5037 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5038 cp_parser_abort_tentative_parse (parser);
5039 else if (cp_parser_parse_definitely (parser))
5041 pseudo_destructor_p = true;
5043 = finish_pseudo_destructor_expr (postfix_expression,
5044 s, TREE_TYPE (type));
5048 if (!pseudo_destructor_p)
5050 /* If the SCOPE is not a scalar type, we are looking at an
5051 ordinary class member access expression, rather than a
5052 pseudo-destructor-name. */
5054 cp_token *token = cp_lexer_peek_token (parser->lexer);
5055 /* Parse the id-expression. */
5056 name = (cp_parser_id_expression
5058 cp_parser_optional_template_keyword (parser),
5059 /*check_dependency_p=*/true,
5061 /*declarator_p=*/false,
5062 /*optional_p=*/false));
5063 /* In general, build a SCOPE_REF if the member name is qualified.
5064 However, if the name was not dependent and has already been
5065 resolved; there is no need to build the SCOPE_REF. For example;
5067 struct X { void f(); };
5068 template <typename T> void f(T* t) { t->X::f(); }
5070 Even though "t" is dependent, "X::f" is not and has been resolved
5071 to a BASELINK; there is no need to include scope information. */
5073 /* But we do need to remember that there was an explicit scope for
5074 virtual function calls. */
5076 *idk = CP_ID_KIND_QUALIFIED;
5078 /* If the name is a template-id that names a type, we will get a
5079 TYPE_DECL here. That is invalid code. */
5080 if (TREE_CODE (name) == TYPE_DECL)
5082 error ("%Hinvalid use of %qD", &token->location, name);
5083 postfix_expression = error_mark_node;
5087 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5089 name = build_qualified_name (/*type=*/NULL_TREE,
5093 parser->scope = NULL_TREE;
5094 parser->qualifying_scope = NULL_TREE;
5095 parser->object_scope = NULL_TREE;
5097 if (scope && name && BASELINK_P (name))
5098 adjust_result_of_qualified_name_lookup
5099 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5101 = finish_class_member_access_expr (postfix_expression, name,
5103 tf_warning_or_error);
5107 /* We no longer need to look up names in the scope of the object on
5108 the left-hand side of the `.' or `->' operator. */
5109 parser->context->object_type = NULL_TREE;
5111 /* Outside of offsetof, these operators may not appear in
5112 constant-expressions. */
5114 && (cp_parser_non_integral_constant_expression
5115 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5116 postfix_expression = error_mark_node;
5118 return postfix_expression;
5121 /* Parse a parenthesized expression-list.
5124 assignment-expression
5125 expression-list, assignment-expression
5130 identifier, expression-list
5132 CAST_P is true if this expression is the target of a cast.
5134 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5137 Returns a TREE_LIST. The TREE_VALUE of each node is a
5138 representation of an assignment-expression. Note that a TREE_LIST
5139 is returned even if there is only a single expression in the list.
5140 error_mark_node is returned if the ( and or ) are
5141 missing. NULL_TREE is returned on no expressions. The parentheses
5142 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5143 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5144 indicates whether or not all of the expressions in the list were
5148 cp_parser_parenthesized_expression_list (cp_parser* parser,
5149 bool is_attribute_list,
5151 bool allow_expansion_p,
5152 bool *non_constant_p)
5154 tree expression_list = NULL_TREE;
5155 bool fold_expr_p = is_attribute_list;
5156 tree identifier = NULL_TREE;
5157 bool saved_greater_than_is_operator_p;
5159 /* Assume all the expressions will be constant. */
5161 *non_constant_p = false;
5163 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5164 return error_mark_node;
5166 /* Within a parenthesized expression, a `>' token is always
5167 the greater-than operator. */
5168 saved_greater_than_is_operator_p
5169 = parser->greater_than_is_operator_p;
5170 parser->greater_than_is_operator_p = true;
5172 /* Consume expressions until there are no more. */
5173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5178 /* At the beginning of attribute lists, check to see if the
5179 next token is an identifier. */
5180 if (is_attribute_list
5181 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5185 /* Consume the identifier. */
5186 token = cp_lexer_consume_token (parser->lexer);
5187 /* Save the identifier. */
5188 identifier = token->u.value;
5192 bool expr_non_constant_p;
5194 /* Parse the next assignment-expression. */
5195 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5197 /* A braced-init-list. */
5198 maybe_warn_cpp0x ("extended initializer lists");
5199 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5200 if (non_constant_p && expr_non_constant_p)
5201 *non_constant_p = true;
5203 else if (non_constant_p)
5205 expr = (cp_parser_constant_expression
5206 (parser, /*allow_non_constant_p=*/true,
5207 &expr_non_constant_p));
5208 if (expr_non_constant_p)
5209 *non_constant_p = true;
5212 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5215 expr = fold_non_dependent_expr (expr);
5217 /* If we have an ellipsis, then this is an expression
5219 if (allow_expansion_p
5220 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5222 /* Consume the `...'. */
5223 cp_lexer_consume_token (parser->lexer);
5225 /* Build the argument pack. */
5226 expr = make_pack_expansion (expr);
5229 /* Add it to the list. We add error_mark_node
5230 expressions to the list, so that we can still tell if
5231 the correct form for a parenthesized expression-list
5232 is found. That gives better errors. */
5233 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5235 if (expr == error_mark_node)
5239 /* After the first item, attribute lists look the same as
5240 expression lists. */
5241 is_attribute_list = false;
5244 /* If the next token isn't a `,', then we are done. */
5245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5248 /* Otherwise, consume the `,' and keep going. */
5249 cp_lexer_consume_token (parser->lexer);
5252 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5257 /* We try and resync to an unnested comma, as that will give the
5258 user better diagnostics. */
5259 ending = cp_parser_skip_to_closing_parenthesis (parser,
5260 /*recovering=*/true,
5262 /*consume_paren=*/true);
5267 parser->greater_than_is_operator_p
5268 = saved_greater_than_is_operator_p;
5269 return error_mark_node;
5273 parser->greater_than_is_operator_p
5274 = saved_greater_than_is_operator_p;
5276 /* We built up the list in reverse order so we must reverse it now. */
5277 expression_list = nreverse (expression_list);
5279 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5281 return expression_list;
5284 /* Parse a pseudo-destructor-name.
5286 pseudo-destructor-name:
5287 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5288 :: [opt] nested-name-specifier template template-id :: ~ type-name
5289 :: [opt] nested-name-specifier [opt] ~ type-name
5291 If either of the first two productions is used, sets *SCOPE to the
5292 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5293 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5294 or ERROR_MARK_NODE if the parse fails. */
5297 cp_parser_pseudo_destructor_name (cp_parser* parser,
5301 bool nested_name_specifier_p;
5303 /* Assume that things will not work out. */
5304 *type = error_mark_node;
5306 /* Look for the optional `::' operator. */
5307 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5308 /* Look for the optional nested-name-specifier. */
5309 nested_name_specifier_p
5310 = (cp_parser_nested_name_specifier_opt (parser,
5311 /*typename_keyword_p=*/false,
5312 /*check_dependency_p=*/true,
5314 /*is_declaration=*/false)
5316 /* Now, if we saw a nested-name-specifier, we might be doing the
5317 second production. */
5318 if (nested_name_specifier_p
5319 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5321 /* Consume the `template' keyword. */
5322 cp_lexer_consume_token (parser->lexer);
5323 /* Parse the template-id. */
5324 cp_parser_template_id (parser,
5325 /*template_keyword_p=*/true,
5326 /*check_dependency_p=*/false,
5327 /*is_declaration=*/true);
5328 /* Look for the `::' token. */
5329 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5331 /* If the next token is not a `~', then there might be some
5332 additional qualification. */
5333 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5335 /* At this point, we're looking for "type-name :: ~". The type-name
5336 must not be a class-name, since this is a pseudo-destructor. So,
5337 it must be either an enum-name, or a typedef-name -- both of which
5338 are just identifiers. So, we peek ahead to check that the "::"
5339 and "~" tokens are present; if they are not, then we can avoid
5340 calling type_name. */
5341 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5342 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5343 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5345 cp_parser_error (parser, "non-scalar type");
5349 /* Look for the type-name. */
5350 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5351 if (*scope == error_mark_node)
5354 /* Look for the `::' token. */
5355 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5360 /* Look for the `~'. */
5361 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5362 /* Look for the type-name again. We are not responsible for
5363 checking that it matches the first type-name. */
5364 *type = cp_parser_nonclass_name (parser);
5367 /* Parse a unary-expression.
5373 unary-operator cast-expression
5374 sizeof unary-expression
5382 __extension__ cast-expression
5383 __alignof__ unary-expression
5384 __alignof__ ( type-id )
5385 __real__ cast-expression
5386 __imag__ cast-expression
5389 ADDRESS_P is true iff the unary-expression is appearing as the
5390 operand of the `&' operator. CAST_P is true if this expression is
5391 the target of a cast.
5393 Returns a representation of the expression. */
5396 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5400 enum tree_code unary_operator;
5402 /* Peek at the next token. */
5403 token = cp_lexer_peek_token (parser->lexer);
5404 /* Some keywords give away the kind of expression. */
5405 if (token->type == CPP_KEYWORD)
5407 enum rid keyword = token->keyword;
5417 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5418 /* Consume the token. */
5419 cp_lexer_consume_token (parser->lexer);
5420 /* Parse the operand. */
5421 operand = cp_parser_sizeof_operand (parser, keyword);
5423 if (TYPE_P (operand))
5424 return cxx_sizeof_or_alignof_type (operand, op, true);
5426 return cxx_sizeof_or_alignof_expr (operand, op, true);
5430 return cp_parser_new_expression (parser);
5433 return cp_parser_delete_expression (parser);
5437 /* The saved value of the PEDANTIC flag. */
5441 /* Save away the PEDANTIC flag. */
5442 cp_parser_extension_opt (parser, &saved_pedantic);
5443 /* Parse the cast-expression. */
5444 expr = cp_parser_simple_cast_expression (parser);
5445 /* Restore the PEDANTIC flag. */
5446 pedantic = saved_pedantic;
5456 /* Consume the `__real__' or `__imag__' token. */
5457 cp_lexer_consume_token (parser->lexer);
5458 /* Parse the cast-expression. */
5459 expression = cp_parser_simple_cast_expression (parser);
5460 /* Create the complete representation. */
5461 return build_x_unary_op ((keyword == RID_REALPART
5462 ? REALPART_EXPR : IMAGPART_EXPR),
5464 tf_warning_or_error);
5473 /* Look for the `:: new' and `:: delete', which also signal the
5474 beginning of a new-expression, or delete-expression,
5475 respectively. If the next token is `::', then it might be one of
5477 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5481 /* See if the token after the `::' is one of the keywords in
5482 which we're interested. */
5483 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5484 /* If it's `new', we have a new-expression. */
5485 if (keyword == RID_NEW)
5486 return cp_parser_new_expression (parser);
5487 /* Similarly, for `delete'. */
5488 else if (keyword == RID_DELETE)
5489 return cp_parser_delete_expression (parser);
5492 /* Look for a unary operator. */
5493 unary_operator = cp_parser_unary_operator (token);
5494 /* The `++' and `--' operators can be handled similarly, even though
5495 they are not technically unary-operators in the grammar. */
5496 if (unary_operator == ERROR_MARK)
5498 if (token->type == CPP_PLUS_PLUS)
5499 unary_operator = PREINCREMENT_EXPR;
5500 else if (token->type == CPP_MINUS_MINUS)
5501 unary_operator = PREDECREMENT_EXPR;
5502 /* Handle the GNU address-of-label extension. */
5503 else if (cp_parser_allow_gnu_extensions_p (parser)
5504 && token->type == CPP_AND_AND)
5508 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5510 /* Consume the '&&' token. */
5511 cp_lexer_consume_token (parser->lexer);
5512 /* Look for the identifier. */
5513 identifier = cp_parser_identifier (parser);
5514 /* Create an expression representing the address. */
5515 expression = finish_label_address_expr (identifier, loc);
5516 if (cp_parser_non_integral_constant_expression (parser,
5517 "the address of a label"))
5518 expression = error_mark_node;
5522 if (unary_operator != ERROR_MARK)
5524 tree cast_expression;
5525 tree expression = error_mark_node;
5526 const char *non_constant_p = NULL;
5528 /* Consume the operator token. */
5529 token = cp_lexer_consume_token (parser->lexer);
5530 /* Parse the cast-expression. */
5532 = cp_parser_cast_expression (parser,
5533 unary_operator == ADDR_EXPR,
5534 /*cast_p=*/false, pidk);
5535 /* Now, build an appropriate representation. */
5536 switch (unary_operator)
5539 non_constant_p = "%<*%>";
5540 expression = build_x_indirect_ref (cast_expression, "unary *",
5541 tf_warning_or_error);
5545 non_constant_p = "%<&%>";
5548 expression = build_x_unary_op (unary_operator, cast_expression,
5549 tf_warning_or_error);
5552 case PREINCREMENT_EXPR:
5553 case PREDECREMENT_EXPR:
5554 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5555 ? "%<++%>" : "%<--%>");
5557 case UNARY_PLUS_EXPR:
5559 case TRUTH_NOT_EXPR:
5560 expression = finish_unary_op_expr (unary_operator, cast_expression);
5568 && cp_parser_non_integral_constant_expression (parser,
5570 expression = error_mark_node;
5575 return cp_parser_postfix_expression (parser, address_p, cast_p,
5576 /*member_access_only_p=*/false,
5580 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5581 unary-operator, the corresponding tree code is returned. */
5583 static enum tree_code
5584 cp_parser_unary_operator (cp_token* token)
5586 switch (token->type)
5589 return INDIRECT_REF;
5595 return UNARY_PLUS_EXPR;
5601 return TRUTH_NOT_EXPR;
5604 return BIT_NOT_EXPR;
5611 /* Parse a new-expression.
5614 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5615 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5617 Returns a representation of the expression. */
5620 cp_parser_new_expression (cp_parser* parser)
5622 bool global_scope_p;
5628 /* Look for the optional `::' operator. */
5630 = (cp_parser_global_scope_opt (parser,
5631 /*current_scope_valid_p=*/false)
5633 /* Look for the `new' operator. */
5634 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5635 /* There's no easy way to tell a new-placement from the
5636 `( type-id )' construct. */
5637 cp_parser_parse_tentatively (parser);
5638 /* Look for a new-placement. */
5639 placement = cp_parser_new_placement (parser);
5640 /* If that didn't work out, there's no new-placement. */
5641 if (!cp_parser_parse_definitely (parser))
5642 placement = NULL_TREE;
5644 /* If the next token is a `(', then we have a parenthesized
5646 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5649 /* Consume the `('. */
5650 cp_lexer_consume_token (parser->lexer);
5651 /* Parse the type-id. */
5652 type = cp_parser_type_id (parser);
5653 /* Look for the closing `)'. */
5654 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5655 token = cp_lexer_peek_token (parser->lexer);
5656 /* There should not be a direct-new-declarator in this production,
5657 but GCC used to allowed this, so we check and emit a sensible error
5658 message for this case. */
5659 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5661 error ("%Harray bound forbidden after parenthesized type-id",
5663 inform (token->location,
5664 "try removing the parentheses around the type-id");
5665 cp_parser_direct_new_declarator (parser);
5669 /* Otherwise, there must be a new-type-id. */
5671 type = cp_parser_new_type_id (parser, &nelts);
5673 /* If the next token is a `(' or '{', then we have a new-initializer. */
5674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5675 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5676 initializer = cp_parser_new_initializer (parser);
5678 initializer = NULL_TREE;
5680 /* A new-expression may not appear in an integral constant
5682 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5683 return error_mark_node;
5685 /* Create a representation of the new-expression. */
5686 return build_new (placement, type, nelts, initializer, global_scope_p,
5687 tf_warning_or_error);
5690 /* Parse a new-placement.
5695 Returns the same representation as for an expression-list. */
5698 cp_parser_new_placement (cp_parser* parser)
5700 tree expression_list;
5702 /* Parse the expression-list. */
5703 expression_list = (cp_parser_parenthesized_expression_list
5704 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5705 /*non_constant_p=*/NULL));
5707 return expression_list;
5710 /* Parse a new-type-id.
5713 type-specifier-seq new-declarator [opt]
5715 Returns the TYPE allocated. If the new-type-id indicates an array
5716 type, *NELTS is set to the number of elements in the last array
5717 bound; the TYPE will not include the last array bound. */
5720 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5722 cp_decl_specifier_seq type_specifier_seq;
5723 cp_declarator *new_declarator;
5724 cp_declarator *declarator;
5725 cp_declarator *outer_declarator;
5726 const char *saved_message;
5729 /* The type-specifier sequence must not contain type definitions.
5730 (It cannot contain declarations of new types either, but if they
5731 are not definitions we will catch that because they are not
5733 saved_message = parser->type_definition_forbidden_message;
5734 parser->type_definition_forbidden_message
5735 = "types may not be defined in a new-type-id";
5736 /* Parse the type-specifier-seq. */
5737 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5738 &type_specifier_seq);
5739 /* Restore the old message. */
5740 parser->type_definition_forbidden_message = saved_message;
5741 /* Parse the new-declarator. */
5742 new_declarator = cp_parser_new_declarator_opt (parser);
5744 /* Determine the number of elements in the last array dimension, if
5747 /* Skip down to the last array dimension. */
5748 declarator = new_declarator;
5749 outer_declarator = NULL;
5750 while (declarator && (declarator->kind == cdk_pointer
5751 || declarator->kind == cdk_ptrmem))
5753 outer_declarator = declarator;
5754 declarator = declarator->declarator;
5757 && declarator->kind == cdk_array
5758 && declarator->declarator
5759 && declarator->declarator->kind == cdk_array)
5761 outer_declarator = declarator;
5762 declarator = declarator->declarator;
5765 if (declarator && declarator->kind == cdk_array)
5767 *nelts = declarator->u.array.bounds;
5768 if (*nelts == error_mark_node)
5769 *nelts = integer_one_node;
5771 if (outer_declarator)
5772 outer_declarator->declarator = declarator->declarator;
5774 new_declarator = NULL;
5777 type = groktypename (&type_specifier_seq, new_declarator);
5781 /* Parse an (optional) new-declarator.
5784 ptr-operator new-declarator [opt]
5785 direct-new-declarator
5787 Returns the declarator. */
5789 static cp_declarator *
5790 cp_parser_new_declarator_opt (cp_parser* parser)
5792 enum tree_code code;
5794 cp_cv_quals cv_quals;
5796 /* We don't know if there's a ptr-operator next, or not. */
5797 cp_parser_parse_tentatively (parser);
5798 /* Look for a ptr-operator. */
5799 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5800 /* If that worked, look for more new-declarators. */
5801 if (cp_parser_parse_definitely (parser))
5803 cp_declarator *declarator;
5805 /* Parse another optional declarator. */
5806 declarator = cp_parser_new_declarator_opt (parser);
5808 return cp_parser_make_indirect_declarator
5809 (code, type, cv_quals, declarator);
5812 /* If the next token is a `[', there is a direct-new-declarator. */
5813 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5814 return cp_parser_direct_new_declarator (parser);
5819 /* Parse a direct-new-declarator.
5821 direct-new-declarator:
5823 direct-new-declarator [constant-expression]
5827 static cp_declarator *
5828 cp_parser_direct_new_declarator (cp_parser* parser)
5830 cp_declarator *declarator = NULL;
5836 /* Look for the opening `['. */
5837 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5838 /* The first expression is not required to be constant. */
5841 cp_token *token = cp_lexer_peek_token (parser->lexer);
5842 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5843 /* The standard requires that the expression have integral
5844 type. DR 74 adds enumeration types. We believe that the
5845 real intent is that these expressions be handled like the
5846 expression in a `switch' condition, which also allows
5847 classes with a single conversion to integral or
5848 enumeration type. */
5849 if (!processing_template_decl)
5852 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5857 error ("%Hexpression in new-declarator must have integral "
5858 "or enumeration type", &token->location);
5859 expression = error_mark_node;
5863 /* But all the other expressions must be. */
5866 = cp_parser_constant_expression (parser,
5867 /*allow_non_constant=*/false,
5869 /* Look for the closing `]'. */
5870 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5872 /* Add this bound to the declarator. */
5873 declarator = make_array_declarator (declarator, expression);
5875 /* If the next token is not a `[', then there are no more
5877 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5884 /* Parse a new-initializer.
5887 ( expression-list [opt] )
5890 Returns a representation of the expression-list. If there is no
5891 expression-list, VOID_ZERO_NODE is returned. */
5894 cp_parser_new_initializer (cp_parser* parser)
5896 tree expression_list;
5898 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5900 bool expr_non_constant_p;
5901 maybe_warn_cpp0x ("extended initializer lists");
5902 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5903 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5904 expression_list = build_tree_list (NULL_TREE, expression_list);
5907 expression_list = (cp_parser_parenthesized_expression_list
5908 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5909 /*non_constant_p=*/NULL));
5910 if (!expression_list)
5911 expression_list = void_zero_node;
5913 return expression_list;
5916 /* Parse a delete-expression.
5919 :: [opt] delete cast-expression
5920 :: [opt] delete [ ] cast-expression
5922 Returns a representation of the expression. */
5925 cp_parser_delete_expression (cp_parser* parser)
5927 bool global_scope_p;
5931 /* Look for the optional `::' operator. */
5933 = (cp_parser_global_scope_opt (parser,
5934 /*current_scope_valid_p=*/false)
5936 /* Look for the `delete' keyword. */
5937 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5938 /* See if the array syntax is in use. */
5939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5941 /* Consume the `[' token. */
5942 cp_lexer_consume_token (parser->lexer);
5943 /* Look for the `]' token. */
5944 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5945 /* Remember that this is the `[]' construct. */
5951 /* Parse the cast-expression. */
5952 expression = cp_parser_simple_cast_expression (parser);
5954 /* A delete-expression may not appear in an integral constant
5956 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5957 return error_mark_node;
5959 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5962 /* Returns true if TOKEN may start a cast-expression and false
5966 cp_parser_token_starts_cast_expression (cp_token *token)
5968 switch (token->type)
5974 case CPP_CLOSE_SQUARE:
5975 case CPP_CLOSE_PAREN:
5976 case CPP_CLOSE_BRACE:
5980 case CPP_DEREF_STAR:
5988 case CPP_GREATER_EQ:
6008 /* '[' may start a primary-expression in obj-c++. */
6009 case CPP_OPEN_SQUARE:
6010 return c_dialect_objc ();
6017 /* Parse a cast-expression.
6021 ( type-id ) cast-expression
6023 ADDRESS_P is true iff the unary-expression is appearing as the
6024 operand of the `&' operator. CAST_P is true if this expression is
6025 the target of a cast.
6027 Returns a representation of the expression. */
6030 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6033 /* If it's a `(', then we might be looking at a cast. */
6034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6036 tree type = NULL_TREE;
6037 tree expr = NULL_TREE;
6038 bool compound_literal_p;
6039 const char *saved_message;
6041 /* There's no way to know yet whether or not this is a cast.
6042 For example, `(int (3))' is a unary-expression, while `(int)
6043 3' is a cast. So, we resort to parsing tentatively. */
6044 cp_parser_parse_tentatively (parser);
6045 /* Types may not be defined in a cast. */
6046 saved_message = parser->type_definition_forbidden_message;
6047 parser->type_definition_forbidden_message
6048 = "types may not be defined in casts";
6049 /* Consume the `('. */
6050 cp_lexer_consume_token (parser->lexer);
6051 /* A very tricky bit is that `(struct S) { 3 }' is a
6052 compound-literal (which we permit in C++ as an extension).
6053 But, that construct is not a cast-expression -- it is a
6054 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6055 is legal; if the compound-literal were a cast-expression,
6056 you'd need an extra set of parentheses.) But, if we parse
6057 the type-id, and it happens to be a class-specifier, then we
6058 will commit to the parse at that point, because we cannot
6059 undo the action that is done when creating a new class. So,
6060 then we cannot back up and do a postfix-expression.
6062 Therefore, we scan ahead to the closing `)', and check to see
6063 if the token after the `)' is a `{'. If so, we are not
6064 looking at a cast-expression.
6066 Save tokens so that we can put them back. */
6067 cp_lexer_save_tokens (parser->lexer);
6068 /* Skip tokens until the next token is a closing parenthesis.
6069 If we find the closing `)', and the next token is a `{', then
6070 we are looking at a compound-literal. */
6072 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6073 /*consume_paren=*/true)
6074 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6075 /* Roll back the tokens we skipped. */
6076 cp_lexer_rollback_tokens (parser->lexer);
6077 /* If we were looking at a compound-literal, simulate an error
6078 so that the call to cp_parser_parse_definitely below will
6080 if (compound_literal_p)
6081 cp_parser_simulate_error (parser);
6084 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6085 parser->in_type_id_in_expr_p = true;
6086 /* Look for the type-id. */
6087 type = cp_parser_type_id (parser);
6088 /* Look for the closing `)'. */
6089 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6090 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6093 /* Restore the saved message. */
6094 parser->type_definition_forbidden_message = saved_message;
6096 /* At this point this can only be either a cast or a
6097 parenthesized ctor such as `(T ())' that looks like a cast to
6098 function returning T. */
6099 if (!cp_parser_error_occurred (parser)
6100 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6103 cp_parser_parse_definitely (parser);
6104 expr = cp_parser_cast_expression (parser,
6105 /*address_p=*/false,
6106 /*cast_p=*/true, pidk);
6108 /* Warn about old-style casts, if so requested. */
6109 if (warn_old_style_cast
6110 && !in_system_header
6111 && !VOID_TYPE_P (type)
6112 && current_lang_name != lang_name_c)
6113 warning (OPT_Wold_style_cast, "use of old-style cast");
6115 /* Only type conversions to integral or enumeration types
6116 can be used in constant-expressions. */
6117 if (!cast_valid_in_integral_constant_expression_p (type)
6118 && (cp_parser_non_integral_constant_expression
6120 "a cast to a type other than an integral or "
6121 "enumeration type")))
6122 return error_mark_node;
6124 /* Perform the cast. */
6125 expr = build_c_cast (type, expr);
6129 cp_parser_abort_tentative_parse (parser);
6132 /* If we get here, then it's not a cast, so it must be a
6133 unary-expression. */
6134 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6137 /* Parse a binary expression of the general form:
6141 pm-expression .* cast-expression
6142 pm-expression ->* cast-expression
6144 multiplicative-expression:
6146 multiplicative-expression * pm-expression
6147 multiplicative-expression / pm-expression
6148 multiplicative-expression % pm-expression
6150 additive-expression:
6151 multiplicative-expression
6152 additive-expression + multiplicative-expression
6153 additive-expression - multiplicative-expression
6157 shift-expression << additive-expression
6158 shift-expression >> additive-expression
6160 relational-expression:
6162 relational-expression < shift-expression
6163 relational-expression > shift-expression
6164 relational-expression <= shift-expression
6165 relational-expression >= shift-expression
6169 relational-expression:
6170 relational-expression <? shift-expression
6171 relational-expression >? shift-expression
6173 equality-expression:
6174 relational-expression
6175 equality-expression == relational-expression
6176 equality-expression != relational-expression
6180 and-expression & equality-expression
6182 exclusive-or-expression:
6184 exclusive-or-expression ^ and-expression
6186 inclusive-or-expression:
6187 exclusive-or-expression
6188 inclusive-or-expression | exclusive-or-expression
6190 logical-and-expression:
6191 inclusive-or-expression
6192 logical-and-expression && inclusive-or-expression
6194 logical-or-expression:
6195 logical-and-expression
6196 logical-or-expression || logical-and-expression
6198 All these are implemented with a single function like:
6201 simple-cast-expression
6202 binary-expression <token> binary-expression
6204 CAST_P is true if this expression is the target of a cast.
6206 The binops_by_token map is used to get the tree codes for each <token> type.
6207 binary-expressions are associated according to a precedence table. */
6209 #define TOKEN_PRECEDENCE(token) \
6210 (((token->type == CPP_GREATER \
6211 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6212 && !parser->greater_than_is_operator_p) \
6213 ? PREC_NOT_OPERATOR \
6214 : binops_by_token[token->type].prec)
6217 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6218 enum cp_parser_prec prec,
6221 cp_parser_expression_stack stack;
6222 cp_parser_expression_stack_entry *sp = &stack[0];
6225 enum tree_code tree_type, lhs_type, rhs_type;
6226 enum cp_parser_prec new_prec, lookahead_prec;
6229 /* Parse the first expression. */
6230 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6231 lhs_type = ERROR_MARK;
6235 /* Get an operator token. */
6236 token = cp_lexer_peek_token (parser->lexer);
6238 if (warn_cxx0x_compat
6239 && token->type == CPP_RSHIFT
6240 && !parser->greater_than_is_operator_p)
6242 warning (OPT_Wc__0x_compat,
6243 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6245 warning (OPT_Wc__0x_compat,
6246 "suggest parentheses around %<>>%> expression");
6249 new_prec = TOKEN_PRECEDENCE (token);
6251 /* Popping an entry off the stack means we completed a subexpression:
6252 - either we found a token which is not an operator (`>' where it is not
6253 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6254 will happen repeatedly;
6255 - or, we found an operator which has lower priority. This is the case
6256 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6258 if (new_prec <= prec)
6267 tree_type = binops_by_token[token->type].tree_type;
6269 /* We used the operator token. */
6270 cp_lexer_consume_token (parser->lexer);
6272 /* Extract another operand. It may be the RHS of this expression
6273 or the LHS of a new, higher priority expression. */
6274 rhs = cp_parser_simple_cast_expression (parser);
6275 rhs_type = ERROR_MARK;
6277 /* Get another operator token. Look up its precedence to avoid
6278 building a useless (immediately popped) stack entry for common
6279 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6280 token = cp_lexer_peek_token (parser->lexer);
6281 lookahead_prec = TOKEN_PRECEDENCE (token);
6282 if (lookahead_prec > new_prec)
6284 /* ... and prepare to parse the RHS of the new, higher priority
6285 expression. Since precedence levels on the stack are
6286 monotonically increasing, we do not have to care about
6289 sp->tree_type = tree_type;
6291 sp->lhs_type = lhs_type;
6294 lhs_type = rhs_type;
6296 new_prec = lookahead_prec;
6300 /* If the stack is not empty, we have parsed into LHS the right side
6301 (`4' in the example above) of an expression we had suspended.
6302 We can use the information on the stack to recover the LHS (`3')
6303 from the stack together with the tree code (`MULT_EXPR'), and
6304 the precedence of the higher level subexpression
6305 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6306 which will be used to actually build the additive expression. */
6309 tree_type = sp->tree_type;
6311 rhs_type = lhs_type;
6313 lhs_type = sp->lhs_type;
6316 overloaded_p = false;
6317 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6318 ERROR_MARK for everything that is not a binary expression.
6319 This makes warn_about_parentheses miss some warnings that
6320 involve unary operators. For unary expressions we should
6321 pass the correct tree_code unless the unary expression was
6322 surrounded by parentheses.
6324 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6325 &overloaded_p, tf_warning_or_error);
6326 lhs_type = tree_type;
6328 /* If the binary operator required the use of an overloaded operator,
6329 then this expression cannot be an integral constant-expression.
6330 An overloaded operator can be used even if both operands are
6331 otherwise permissible in an integral constant-expression if at
6332 least one of the operands is of enumeration type. */
6335 && (cp_parser_non_integral_constant_expression
6336 (parser, "calls to overloaded operators")))
6337 return error_mark_node;
6344 /* Parse the `? expression : assignment-expression' part of a
6345 conditional-expression. The LOGICAL_OR_EXPR is the
6346 logical-or-expression that started the conditional-expression.
6347 Returns a representation of the entire conditional-expression.
6349 This routine is used by cp_parser_assignment_expression.
6351 ? expression : assignment-expression
6355 ? : assignment-expression */
6358 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6361 tree assignment_expr;
6363 /* Consume the `?' token. */
6364 cp_lexer_consume_token (parser->lexer);
6365 if (cp_parser_allow_gnu_extensions_p (parser)
6366 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6367 /* Implicit true clause. */
6370 /* Parse the expression. */
6371 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6373 /* The next token should be a `:'. */
6374 cp_parser_require (parser, CPP_COLON, "%<:%>");
6375 /* Parse the assignment-expression. */
6376 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6378 /* Build the conditional-expression. */
6379 return build_x_conditional_expr (logical_or_expr,
6382 tf_warning_or_error);
6385 /* Parse an assignment-expression.
6387 assignment-expression:
6388 conditional-expression
6389 logical-or-expression assignment-operator assignment_expression
6392 CAST_P is true if this expression is the target of a cast.
6394 Returns a representation for the expression. */
6397 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6402 /* If the next token is the `throw' keyword, then we're looking at
6403 a throw-expression. */
6404 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6405 expr = cp_parser_throw_expression (parser);
6406 /* Otherwise, it must be that we are looking at a
6407 logical-or-expression. */
6410 /* Parse the binary expressions (logical-or-expression). */
6411 expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR, pidk);
6412 /* If the next token is a `?' then we're actually looking at a
6413 conditional-expression. */
6414 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6415 return cp_parser_question_colon_clause (parser, expr);
6418 enum tree_code assignment_operator;
6420 /* If it's an assignment-operator, we're using the second
6423 = cp_parser_assignment_operator_opt (parser);
6424 if (assignment_operator != ERROR_MARK)
6426 bool non_constant_p;
6428 /* Parse the right-hand side of the assignment. */
6429 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6431 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6432 maybe_warn_cpp0x ("extended initializer lists");
6434 /* An assignment may not appear in a
6435 constant-expression. */
6436 if (cp_parser_non_integral_constant_expression (parser,
6438 return error_mark_node;
6439 /* Build the assignment expression. */
6440 expr = build_x_modify_expr (expr,
6441 assignment_operator,
6443 tf_warning_or_error);
6451 /* Parse an (optional) assignment-operator.
6453 assignment-operator: one of
6454 = *= /= %= += -= >>= <<= &= ^= |=
6458 assignment-operator: one of
6461 If the next token is an assignment operator, the corresponding tree
6462 code is returned, and the token is consumed. For example, for
6463 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6464 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6465 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6466 operator, ERROR_MARK is returned. */
6468 static enum tree_code
6469 cp_parser_assignment_operator_opt (cp_parser* parser)
6474 /* Peek at the next token. */
6475 token = cp_lexer_peek_token (parser->lexer);
6477 switch (token->type)
6488 op = TRUNC_DIV_EXPR;
6492 op = TRUNC_MOD_EXPR;
6524 /* Nothing else is an assignment operator. */
6528 /* If it was an assignment operator, consume it. */
6529 if (op != ERROR_MARK)
6530 cp_lexer_consume_token (parser->lexer);
6535 /* Parse an expression.
6538 assignment-expression
6539 expression , assignment-expression
6541 CAST_P is true if this expression is the target of a cast.
6543 Returns a representation of the expression. */
6546 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6548 tree expression = NULL_TREE;
6552 tree assignment_expression;
6554 /* Parse the next assignment-expression. */
6555 assignment_expression
6556 = cp_parser_assignment_expression (parser, cast_p, pidk);
6557 /* If this is the first assignment-expression, we can just
6560 expression = assignment_expression;
6562 expression = build_x_compound_expr (expression,
6563 assignment_expression,
6564 tf_warning_or_error);
6565 /* If the next token is not a comma, then we are done with the
6567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6569 /* Consume the `,'. */
6570 cp_lexer_consume_token (parser->lexer);
6571 /* A comma operator cannot appear in a constant-expression. */
6572 if (cp_parser_non_integral_constant_expression (parser,
6573 "a comma operator"))
6574 expression = error_mark_node;
6580 /* Parse a constant-expression.
6582 constant-expression:
6583 conditional-expression
6585 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6586 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6587 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6588 is false, NON_CONSTANT_P should be NULL. */
6591 cp_parser_constant_expression (cp_parser* parser,
6592 bool allow_non_constant_p,
6593 bool *non_constant_p)
6595 bool saved_integral_constant_expression_p;
6596 bool saved_allow_non_integral_constant_expression_p;
6597 bool saved_non_integral_constant_expression_p;
6600 /* It might seem that we could simply parse the
6601 conditional-expression, and then check to see if it were
6602 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6603 one that the compiler can figure out is constant, possibly after
6604 doing some simplifications or optimizations. The standard has a
6605 precise definition of constant-expression, and we must honor
6606 that, even though it is somewhat more restrictive.
6612 is not a legal declaration, because `(2, 3)' is not a
6613 constant-expression. The `,' operator is forbidden in a
6614 constant-expression. However, GCC's constant-folding machinery
6615 will fold this operation to an INTEGER_CST for `3'. */
6617 /* Save the old settings. */
6618 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6619 saved_allow_non_integral_constant_expression_p
6620 = parser->allow_non_integral_constant_expression_p;
6621 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6622 /* We are now parsing a constant-expression. */
6623 parser->integral_constant_expression_p = true;
6624 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6625 parser->non_integral_constant_expression_p = false;
6626 /* Although the grammar says "conditional-expression", we parse an
6627 "assignment-expression", which also permits "throw-expression"
6628 and the use of assignment operators. In the case that
6629 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6630 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6631 actually essential that we look for an assignment-expression.
6632 For example, cp_parser_initializer_clauses uses this function to
6633 determine whether a particular assignment-expression is in fact
6635 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6636 /* Restore the old settings. */
6637 parser->integral_constant_expression_p
6638 = saved_integral_constant_expression_p;
6639 parser->allow_non_integral_constant_expression_p
6640 = saved_allow_non_integral_constant_expression_p;
6641 if (allow_non_constant_p)
6642 *non_constant_p = parser->non_integral_constant_expression_p;
6643 else if (parser->non_integral_constant_expression_p)
6644 expression = error_mark_node;
6645 parser->non_integral_constant_expression_p
6646 = saved_non_integral_constant_expression_p;
6651 /* Parse __builtin_offsetof.
6653 offsetof-expression:
6654 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6656 offsetof-member-designator:
6658 | offsetof-member-designator "." id-expression
6659 | offsetof-member-designator "[" expression "]"
6660 | offsetof-member-designator "->" id-expression */
6663 cp_parser_builtin_offsetof (cp_parser *parser)
6665 int save_ice_p, save_non_ice_p;
6670 /* We're about to accept non-integral-constant things, but will
6671 definitely yield an integral constant expression. Save and
6672 restore these values around our local parsing. */
6673 save_ice_p = parser->integral_constant_expression_p;
6674 save_non_ice_p = parser->non_integral_constant_expression_p;
6676 /* Consume the "__builtin_offsetof" token. */
6677 cp_lexer_consume_token (parser->lexer);
6678 /* Consume the opening `('. */
6679 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6680 /* Parse the type-id. */
6681 type = cp_parser_type_id (parser);
6682 /* Look for the `,'. */
6683 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6684 token = cp_lexer_peek_token (parser->lexer);
6686 /* Build the (type *)null that begins the traditional offsetof macro. */
6687 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6688 tf_warning_or_error);
6690 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6691 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6692 true, &dummy, token->location);
6695 token = cp_lexer_peek_token (parser->lexer);
6696 switch (token->type)
6698 case CPP_OPEN_SQUARE:
6699 /* offsetof-member-designator "[" expression "]" */
6700 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6704 /* offsetof-member-designator "->" identifier */
6705 expr = grok_array_decl (expr, integer_zero_node);
6709 /* offsetof-member-designator "." identifier */
6710 cp_lexer_consume_token (parser->lexer);
6711 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6716 case CPP_CLOSE_PAREN:
6717 /* Consume the ")" token. */
6718 cp_lexer_consume_token (parser->lexer);
6722 /* Error. We know the following require will fail, but
6723 that gives the proper error message. */
6724 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6725 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6726 expr = error_mark_node;
6732 /* If we're processing a template, we can't finish the semantics yet.
6733 Otherwise we can fold the entire expression now. */
6734 if (processing_template_decl)
6735 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6737 expr = finish_offsetof (expr);
6740 parser->integral_constant_expression_p = save_ice_p;
6741 parser->non_integral_constant_expression_p = save_non_ice_p;
6746 /* Parse a trait expression. */
6749 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6752 tree type1, type2 = NULL_TREE;
6753 bool binary = false;
6754 cp_decl_specifier_seq decl_specs;
6758 case RID_HAS_NOTHROW_ASSIGN:
6759 kind = CPTK_HAS_NOTHROW_ASSIGN;
6761 case RID_HAS_NOTHROW_CONSTRUCTOR:
6762 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6764 case RID_HAS_NOTHROW_COPY:
6765 kind = CPTK_HAS_NOTHROW_COPY;
6767 case RID_HAS_TRIVIAL_ASSIGN:
6768 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6770 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6771 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6773 case RID_HAS_TRIVIAL_COPY:
6774 kind = CPTK_HAS_TRIVIAL_COPY;
6776 case RID_HAS_TRIVIAL_DESTRUCTOR:
6777 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6779 case RID_HAS_VIRTUAL_DESTRUCTOR:
6780 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6782 case RID_IS_ABSTRACT:
6783 kind = CPTK_IS_ABSTRACT;
6785 case RID_IS_BASE_OF:
6786 kind = CPTK_IS_BASE_OF;
6790 kind = CPTK_IS_CLASS;
6792 case RID_IS_CONVERTIBLE_TO:
6793 kind = CPTK_IS_CONVERTIBLE_TO;
6797 kind = CPTK_IS_EMPTY;
6800 kind = CPTK_IS_ENUM;
6805 case RID_IS_POLYMORPHIC:
6806 kind = CPTK_IS_POLYMORPHIC;
6809 kind = CPTK_IS_UNION;
6815 /* Consume the token. */
6816 cp_lexer_consume_token (parser->lexer);
6818 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6820 type1 = cp_parser_type_id (parser);
6822 if (type1 == error_mark_node)
6823 return error_mark_node;
6825 /* Build a trivial decl-specifier-seq. */
6826 clear_decl_specs (&decl_specs);
6827 decl_specs.type = type1;
6829 /* Call grokdeclarator to figure out what type this is. */
6830 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6831 /*initialized=*/0, /*attrlist=*/NULL);
6835 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6837 type2 = cp_parser_type_id (parser);
6839 if (type2 == error_mark_node)
6840 return error_mark_node;
6842 /* Build a trivial decl-specifier-seq. */
6843 clear_decl_specs (&decl_specs);
6844 decl_specs.type = type2;
6846 /* Call grokdeclarator to figure out what type this is. */
6847 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6848 /*initialized=*/0, /*attrlist=*/NULL);
6851 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6853 /* Complete the trait expression, which may mean either processing
6854 the trait expr now or saving it for template instantiation. */
6855 return finish_trait_expr (kind, type1, type2);
6858 /* Statements [gram.stmt.stmt] */
6860 /* Parse a statement.
6864 expression-statement
6869 declaration-statement
6872 IN_COMPOUND is true when the statement is nested inside a
6873 cp_parser_compound_statement; this matters for certain pragmas.
6875 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6876 is a (possibly labeled) if statement which is not enclosed in braces
6877 and has an else clause. This is used to implement -Wparentheses. */
6880 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6881 bool in_compound, bool *if_p)
6885 location_t statement_location;
6890 /* There is no statement yet. */
6891 statement = NULL_TREE;
6892 /* Peek at the next token. */
6893 token = cp_lexer_peek_token (parser->lexer);
6894 /* Remember the location of the first token in the statement. */
6895 statement_location = token->location;
6896 /* If this is a keyword, then that will often determine what kind of
6897 statement we have. */
6898 if (token->type == CPP_KEYWORD)
6900 enum rid keyword = token->keyword;
6906 /* Looks like a labeled-statement with a case label.
6907 Parse the label, and then use tail recursion to parse
6909 cp_parser_label_for_labeled_statement (parser);
6914 statement = cp_parser_selection_statement (parser, if_p);
6920 statement = cp_parser_iteration_statement (parser);
6927 statement = cp_parser_jump_statement (parser);
6930 /* Objective-C++ exception-handling constructs. */
6933 case RID_AT_FINALLY:
6934 case RID_AT_SYNCHRONIZED:
6936 statement = cp_parser_objc_statement (parser);
6940 statement = cp_parser_try_block (parser);
6944 /* This must be a namespace alias definition. */
6945 cp_parser_declaration_statement (parser);
6949 /* It might be a keyword like `int' that can start a
6950 declaration-statement. */
6954 else if (token->type == CPP_NAME)
6956 /* If the next token is a `:', then we are looking at a
6957 labeled-statement. */
6958 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6959 if (token->type == CPP_COLON)
6961 /* Looks like a labeled-statement with an ordinary label.
6962 Parse the label, and then use tail recursion to parse
6964 cp_parser_label_for_labeled_statement (parser);
6968 /* Anything that starts with a `{' must be a compound-statement. */
6969 else if (token->type == CPP_OPEN_BRACE)
6970 statement = cp_parser_compound_statement (parser, NULL, false);
6971 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6972 a statement all its own. */
6973 else if (token->type == CPP_PRAGMA)
6975 /* Only certain OpenMP pragmas are attached to statements, and thus
6976 are considered statements themselves. All others are not. In
6977 the context of a compound, accept the pragma as a "statement" and
6978 return so that we can check for a close brace. Otherwise we
6979 require a real statement and must go back and read one. */
6981 cp_parser_pragma (parser, pragma_compound);
6982 else if (!cp_parser_pragma (parser, pragma_stmt))
6986 else if (token->type == CPP_EOF)
6988 cp_parser_error (parser, "expected statement");
6992 /* Everything else must be a declaration-statement or an
6993 expression-statement. Try for the declaration-statement
6994 first, unless we are looking at a `;', in which case we know that
6995 we have an expression-statement. */
6998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7000 cp_parser_parse_tentatively (parser);
7001 /* Try to parse the declaration-statement. */
7002 cp_parser_declaration_statement (parser);
7003 /* If that worked, we're done. */
7004 if (cp_parser_parse_definitely (parser))
7007 /* Look for an expression-statement instead. */
7008 statement = cp_parser_expression_statement (parser, in_statement_expr);
7011 /* Set the line number for the statement. */
7012 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7013 SET_EXPR_LOCATION (statement, statement_location);
7016 /* Parse the label for a labeled-statement, i.e.
7019 case constant-expression :
7023 case constant-expression ... constant-expression : statement
7025 When a label is parsed without errors, the label is added to the
7026 parse tree by the finish_* functions, so this function doesn't
7027 have to return the label. */
7030 cp_parser_label_for_labeled_statement (cp_parser* parser)
7034 /* The next token should be an identifier. */
7035 token = cp_lexer_peek_token (parser->lexer);
7036 if (token->type != CPP_NAME
7037 && token->type != CPP_KEYWORD)
7039 cp_parser_error (parser, "expected labeled-statement");
7043 switch (token->keyword)
7050 /* Consume the `case' token. */
7051 cp_lexer_consume_token (parser->lexer);
7052 /* Parse the constant-expression. */
7053 expr = cp_parser_constant_expression (parser,
7054 /*allow_non_constant_p=*/false,
7057 ellipsis = cp_lexer_peek_token (parser->lexer);
7058 if (ellipsis->type == CPP_ELLIPSIS)
7060 /* Consume the `...' token. */
7061 cp_lexer_consume_token (parser->lexer);
7063 cp_parser_constant_expression (parser,
7064 /*allow_non_constant_p=*/false,
7066 /* We don't need to emit warnings here, as the common code
7067 will do this for us. */
7070 expr_hi = NULL_TREE;
7072 if (parser->in_switch_statement_p)
7073 finish_case_label (expr, expr_hi);
7075 error ("%Hcase label %qE not within a switch statement",
7076 &token->location, expr);
7081 /* Consume the `default' token. */
7082 cp_lexer_consume_token (parser->lexer);
7084 if (parser->in_switch_statement_p)
7085 finish_case_label (NULL_TREE, NULL_TREE);
7087 error ("%Hcase label not within a switch statement", &token->location);
7091 /* Anything else must be an ordinary label. */
7092 finish_label_stmt (cp_parser_identifier (parser));
7096 /* Require the `:' token. */
7097 cp_parser_require (parser, CPP_COLON, "%<:%>");
7100 /* Parse an expression-statement.
7102 expression-statement:
7105 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7106 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7107 indicates whether this expression-statement is part of an
7108 expression statement. */
7111 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7113 tree statement = NULL_TREE;
7115 /* If the next token is a ';', then there is no expression
7117 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7118 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7120 /* Consume the final `;'. */
7121 cp_parser_consume_semicolon_at_end_of_statement (parser);
7123 if (in_statement_expr
7124 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7125 /* This is the final expression statement of a statement
7127 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7129 statement = finish_expr_stmt (statement);
7136 /* Parse a compound-statement.
7139 { statement-seq [opt] }
7144 { label-declaration-seq [opt] statement-seq [opt] }
7146 label-declaration-seq:
7148 label-declaration-seq label-declaration
7150 Returns a tree representing the statement. */
7153 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7158 /* Consume the `{'. */
7159 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7160 return error_mark_node;
7161 /* Begin the compound-statement. */
7162 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7163 /* If the next keyword is `__label__' we have a label declaration. */
7164 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7165 cp_parser_label_declaration (parser);
7166 /* Parse an (optional) statement-seq. */
7167 cp_parser_statement_seq_opt (parser, in_statement_expr);
7168 /* Finish the compound-statement. */
7169 finish_compound_stmt (compound_stmt);
7170 /* Consume the `}'. */
7171 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7173 return compound_stmt;
7176 /* Parse an (optional) statement-seq.
7180 statement-seq [opt] statement */
7183 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7185 /* Scan statements until there aren't any more. */
7188 cp_token *token = cp_lexer_peek_token (parser->lexer);
7190 /* If we're looking at a `}', then we've run out of statements. */
7191 if (token->type == CPP_CLOSE_BRACE
7192 || token->type == CPP_EOF
7193 || token->type == CPP_PRAGMA_EOL)
7196 /* If we are in a compound statement and find 'else' then
7197 something went wrong. */
7198 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7200 if (parser->in_statement & IN_IF_STMT)
7204 token = cp_lexer_consume_token (parser->lexer);
7205 error ("%H%<else%> without a previous %<if%>", &token->location);
7209 /* Parse the statement. */
7210 cp_parser_statement (parser, in_statement_expr, true, NULL);
7214 /* Parse a selection-statement.
7216 selection-statement:
7217 if ( condition ) statement
7218 if ( condition ) statement else statement
7219 switch ( condition ) statement
7221 Returns the new IF_STMT or SWITCH_STMT.
7223 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7224 is a (possibly labeled) if statement which is not enclosed in
7225 braces and has an else clause. This is used to implement
7229 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7237 /* Peek at the next token. */
7238 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7240 /* See what kind of keyword it is. */
7241 keyword = token->keyword;
7250 /* Look for the `('. */
7251 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7253 cp_parser_skip_to_end_of_statement (parser);
7254 return error_mark_node;
7257 /* Begin the selection-statement. */
7258 if (keyword == RID_IF)
7259 statement = begin_if_stmt ();
7261 statement = begin_switch_stmt ();
7263 /* Parse the condition. */
7264 condition = cp_parser_condition (parser);
7265 /* Look for the `)'. */
7266 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7267 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7268 /*consume_paren=*/true);
7270 if (keyword == RID_IF)
7273 unsigned char in_statement;
7275 /* Add the condition. */
7276 finish_if_stmt_cond (condition, statement);
7278 /* Parse the then-clause. */
7279 in_statement = parser->in_statement;
7280 parser->in_statement |= IN_IF_STMT;
7281 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7283 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7284 add_stmt (build_empty_stmt ());
7285 cp_lexer_consume_token (parser->lexer);
7286 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7287 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7288 "empty body in an %<if%> statement");
7292 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7293 parser->in_statement = in_statement;
7295 finish_then_clause (statement);
7297 /* If the next token is `else', parse the else-clause. */
7298 if (cp_lexer_next_token_is_keyword (parser->lexer,
7301 /* Consume the `else' keyword. */
7302 cp_lexer_consume_token (parser->lexer);
7303 begin_else_clause (statement);
7304 /* Parse the else-clause. */
7305 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7307 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7308 OPT_Wempty_body, "suggest braces around "
7309 "empty body in an %<else%> statement");
7310 add_stmt (build_empty_stmt ());
7311 cp_lexer_consume_token (parser->lexer);
7314 cp_parser_implicitly_scoped_statement (parser, NULL);
7316 finish_else_clause (statement);
7318 /* If we are currently parsing a then-clause, then
7319 IF_P will not be NULL. We set it to true to
7320 indicate that this if statement has an else clause.
7321 This may trigger the Wparentheses warning below
7322 when we get back up to the parent if statement. */
7328 /* This if statement does not have an else clause. If
7329 NESTED_IF is true, then the then-clause is an if
7330 statement which does have an else clause. We warn
7331 about the potential ambiguity. */
7333 warning (OPT_Wparentheses,
7334 ("%Hsuggest explicit braces "
7335 "to avoid ambiguous %<else%>"),
7336 EXPR_LOCUS (statement));
7339 /* Now we're all done with the if-statement. */
7340 finish_if_stmt (statement);
7344 bool in_switch_statement_p;
7345 unsigned char in_statement;
7347 /* Add the condition. */
7348 finish_switch_cond (condition, statement);
7350 /* Parse the body of the switch-statement. */
7351 in_switch_statement_p = parser->in_switch_statement_p;
7352 in_statement = parser->in_statement;
7353 parser->in_switch_statement_p = true;
7354 parser->in_statement |= IN_SWITCH_STMT;
7355 cp_parser_implicitly_scoped_statement (parser, NULL);
7356 parser->in_switch_statement_p = in_switch_statement_p;
7357 parser->in_statement = in_statement;
7359 /* Now we're all done with the switch-statement. */
7360 finish_switch_stmt (statement);
7368 cp_parser_error (parser, "expected selection-statement");
7369 return error_mark_node;
7373 /* Parse a condition.
7377 type-specifier-seq declarator = initializer-clause
7378 type-specifier-seq declarator braced-init-list
7383 type-specifier-seq declarator asm-specification [opt]
7384 attributes [opt] = assignment-expression
7386 Returns the expression that should be tested. */
7389 cp_parser_condition (cp_parser* parser)
7391 cp_decl_specifier_seq type_specifiers;
7392 const char *saved_message;
7394 /* Try the declaration first. */
7395 cp_parser_parse_tentatively (parser);
7396 /* New types are not allowed in the type-specifier-seq for a
7398 saved_message = parser->type_definition_forbidden_message;
7399 parser->type_definition_forbidden_message
7400 = "types may not be defined in conditions";
7401 /* Parse the type-specifier-seq. */
7402 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7404 /* Restore the saved message. */
7405 parser->type_definition_forbidden_message = saved_message;
7406 /* If all is well, we might be looking at a declaration. */
7407 if (!cp_parser_error_occurred (parser))
7410 tree asm_specification;
7412 cp_declarator *declarator;
7413 tree initializer = NULL_TREE;
7415 /* Parse the declarator. */
7416 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7417 /*ctor_dtor_or_conv_p=*/NULL,
7418 /*parenthesized_p=*/NULL,
7419 /*member_p=*/false);
7420 /* Parse the attributes. */
7421 attributes = cp_parser_attributes_opt (parser);
7422 /* Parse the asm-specification. */
7423 asm_specification = cp_parser_asm_specification_opt (parser);
7424 /* If the next token is not an `=' or '{', then we might still be
7425 looking at an expression. For example:
7429 looks like a decl-specifier-seq and a declarator -- but then
7430 there is no `=', so this is an expression. */
7431 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7432 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7433 cp_parser_simulate_error (parser);
7435 /* If we did see an `=' or '{', then we are looking at a declaration
7437 if (cp_parser_parse_definitely (parser))
7440 bool non_constant_p;
7441 bool flags = LOOKUP_ONLYCONVERTING;
7443 /* Create the declaration. */
7444 decl = start_decl (declarator, &type_specifiers,
7445 /*initialized_p=*/true,
7446 attributes, /*prefix_attributes=*/NULL_TREE,
7449 /* Parse the initializer. */
7450 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7452 initializer = cp_parser_braced_list (parser, &non_constant_p);
7453 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7458 /* Consume the `='. */
7459 cp_parser_require (parser, CPP_EQ, "%<=%>");
7460 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7462 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7463 maybe_warn_cpp0x ("extended initializer lists");
7465 if (!non_constant_p)
7466 initializer = fold_non_dependent_expr (initializer);
7468 /* Process the initializer. */
7469 cp_finish_decl (decl,
7470 initializer, !non_constant_p,
7475 pop_scope (pushed_scope);
7477 return convert_from_reference (decl);
7480 /* If we didn't even get past the declarator successfully, we are
7481 definitely not looking at a declaration. */
7483 cp_parser_abort_tentative_parse (parser);
7485 /* Otherwise, we are looking at an expression. */
7486 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7489 /* Parse an iteration-statement.
7491 iteration-statement:
7492 while ( condition ) statement
7493 do statement while ( expression ) ;
7494 for ( for-init-statement condition [opt] ; expression [opt] )
7497 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7500 cp_parser_iteration_statement (cp_parser* parser)
7505 unsigned char in_statement;
7507 /* Peek at the next token. */
7508 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7510 return error_mark_node;
7512 /* Remember whether or not we are already within an iteration
7514 in_statement = parser->in_statement;
7516 /* See what kind of keyword it is. */
7517 keyword = token->keyword;
7524 /* Begin the while-statement. */
7525 statement = begin_while_stmt ();
7526 /* Look for the `('. */
7527 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7528 /* Parse the condition. */
7529 condition = cp_parser_condition (parser);
7530 finish_while_stmt_cond (condition, statement);
7531 /* Look for the `)'. */
7532 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7533 /* Parse the dependent statement. */
7534 parser->in_statement = IN_ITERATION_STMT;
7535 cp_parser_already_scoped_statement (parser);
7536 parser->in_statement = in_statement;
7537 /* We're done with the while-statement. */
7538 finish_while_stmt (statement);
7546 /* Begin the do-statement. */
7547 statement = begin_do_stmt ();
7548 /* Parse the body of the do-statement. */
7549 parser->in_statement = IN_ITERATION_STMT;
7550 cp_parser_implicitly_scoped_statement (parser, NULL);
7551 parser->in_statement = in_statement;
7552 finish_do_body (statement);
7553 /* Look for the `while' keyword. */
7554 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7555 /* Look for the `('. */
7556 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7557 /* Parse the expression. */
7558 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7559 /* We're done with the do-statement. */
7560 finish_do_stmt (expression, statement);
7561 /* Look for the `)'. */
7562 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7563 /* Look for the `;'. */
7564 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7570 tree condition = NULL_TREE;
7571 tree expression = NULL_TREE;
7573 /* Begin the for-statement. */
7574 statement = begin_for_stmt ();
7575 /* Look for the `('. */
7576 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7577 /* Parse the initialization. */
7578 cp_parser_for_init_statement (parser);
7579 finish_for_init_stmt (statement);
7581 /* If there's a condition, process it. */
7582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7583 condition = cp_parser_condition (parser);
7584 finish_for_cond (condition, statement);
7585 /* Look for the `;'. */
7586 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7588 /* If there's an expression, process it. */
7589 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7590 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7591 finish_for_expr (expression, statement);
7592 /* Look for the `)'. */
7593 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7595 /* Parse the body of the for-statement. */
7596 parser->in_statement = IN_ITERATION_STMT;
7597 cp_parser_already_scoped_statement (parser);
7598 parser->in_statement = in_statement;
7600 /* We're done with the for-statement. */
7601 finish_for_stmt (statement);
7606 cp_parser_error (parser, "expected iteration-statement");
7607 statement = error_mark_node;
7614 /* Parse a for-init-statement.
7617 expression-statement
7618 simple-declaration */
7621 cp_parser_for_init_statement (cp_parser* parser)
7623 /* If the next token is a `;', then we have an empty
7624 expression-statement. Grammatically, this is also a
7625 simple-declaration, but an invalid one, because it does not
7626 declare anything. Therefore, if we did not handle this case
7627 specially, we would issue an error message about an invalid
7629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7631 /* We're going to speculatively look for a declaration, falling back
7632 to an expression, if necessary. */
7633 cp_parser_parse_tentatively (parser);
7634 /* Parse the declaration. */
7635 cp_parser_simple_declaration (parser,
7636 /*function_definition_allowed_p=*/false);
7637 /* If the tentative parse failed, then we shall need to look for an
7638 expression-statement. */
7639 if (cp_parser_parse_definitely (parser))
7643 cp_parser_expression_statement (parser, false);
7646 /* Parse a jump-statement.
7651 return expression [opt] ;
7652 return braced-init-list ;
7660 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7663 cp_parser_jump_statement (cp_parser* parser)
7665 tree statement = error_mark_node;
7668 unsigned char in_statement;
7670 /* Peek at the next token. */
7671 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7673 return error_mark_node;
7675 /* See what kind of keyword it is. */
7676 keyword = token->keyword;
7680 in_statement = parser->in_statement & ~IN_IF_STMT;
7681 switch (in_statement)
7684 error ("%Hbreak statement not within loop or switch", &token->location);
7687 gcc_assert ((in_statement & IN_SWITCH_STMT)
7688 || in_statement == IN_ITERATION_STMT);
7689 statement = finish_break_stmt ();
7692 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7695 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7698 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7702 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7705 error ("%Hcontinue statement not within a loop", &token->location);
7707 case IN_ITERATION_STMT:
7709 statement = finish_continue_stmt ();
7712 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7717 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7723 bool expr_non_constant_p;
7725 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7727 maybe_warn_cpp0x ("extended initializer lists");
7728 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7730 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7731 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7733 /* If the next token is a `;', then there is no
7736 /* Build the return-statement. */
7737 statement = finish_return_stmt (expr);
7738 /* Look for the final `;'. */
7739 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7744 /* Create the goto-statement. */
7745 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7747 /* Issue a warning about this use of a GNU extension. */
7748 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7749 /* Consume the '*' token. */
7750 cp_lexer_consume_token (parser->lexer);
7751 /* Parse the dependent expression. */
7752 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7755 finish_goto_stmt (cp_parser_identifier (parser));
7756 /* Look for the final `;'. */
7757 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7761 cp_parser_error (parser, "expected jump-statement");
7768 /* Parse a declaration-statement.
7770 declaration-statement:
7771 block-declaration */
7774 cp_parser_declaration_statement (cp_parser* parser)
7778 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7779 p = obstack_alloc (&declarator_obstack, 0);
7781 /* Parse the block-declaration. */
7782 cp_parser_block_declaration (parser, /*statement_p=*/true);
7784 /* Free any declarators allocated. */
7785 obstack_free (&declarator_obstack, p);
7787 /* Finish off the statement. */
7791 /* Some dependent statements (like `if (cond) statement'), are
7792 implicitly in their own scope. In other words, if the statement is
7793 a single statement (as opposed to a compound-statement), it is
7794 none-the-less treated as if it were enclosed in braces. Any
7795 declarations appearing in the dependent statement are out of scope
7796 after control passes that point. This function parses a statement,
7797 but ensures that is in its own scope, even if it is not a
7800 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7801 is a (possibly labeled) if statement which is not enclosed in
7802 braces and has an else clause. This is used to implement
7805 Returns the new statement. */
7808 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7815 /* Mark if () ; with a special NOP_EXPR. */
7816 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7818 cp_lexer_consume_token (parser->lexer);
7819 statement = add_stmt (build_empty_stmt ());
7821 /* if a compound is opened, we simply parse the statement directly. */
7822 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7823 statement = cp_parser_compound_statement (parser, NULL, false);
7824 /* If the token is not a `{', then we must take special action. */
7827 /* Create a compound-statement. */
7828 statement = begin_compound_stmt (0);
7829 /* Parse the dependent-statement. */
7830 cp_parser_statement (parser, NULL_TREE, false, if_p);
7831 /* Finish the dummy compound-statement. */
7832 finish_compound_stmt (statement);
7835 /* Return the statement. */
7839 /* For some dependent statements (like `while (cond) statement'), we
7840 have already created a scope. Therefore, even if the dependent
7841 statement is a compound-statement, we do not want to create another
7845 cp_parser_already_scoped_statement (cp_parser* parser)
7847 /* If the token is a `{', then we must take special action. */
7848 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7849 cp_parser_statement (parser, NULL_TREE, false, NULL);
7852 /* Avoid calling cp_parser_compound_statement, so that we
7853 don't create a new scope. Do everything else by hand. */
7854 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7855 /* If the next keyword is `__label__' we have a label declaration. */
7856 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7857 cp_parser_label_declaration (parser);
7858 /* Parse an (optional) statement-seq. */
7859 cp_parser_statement_seq_opt (parser, NULL_TREE);
7860 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7864 /* Declarations [gram.dcl.dcl] */
7866 /* Parse an optional declaration-sequence.
7870 declaration-seq declaration */
7873 cp_parser_declaration_seq_opt (cp_parser* parser)
7879 token = cp_lexer_peek_token (parser->lexer);
7881 if (token->type == CPP_CLOSE_BRACE
7882 || token->type == CPP_EOF
7883 || token->type == CPP_PRAGMA_EOL)
7886 if (token->type == CPP_SEMICOLON)
7888 /* A declaration consisting of a single semicolon is
7889 invalid. Allow it unless we're being pedantic. */
7890 cp_lexer_consume_token (parser->lexer);
7891 if (!in_system_header)
7892 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7896 /* If we're entering or exiting a region that's implicitly
7897 extern "C", modify the lang context appropriately. */
7898 if (!parser->implicit_extern_c && token->implicit_extern_c)
7900 push_lang_context (lang_name_c);
7901 parser->implicit_extern_c = true;
7903 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7905 pop_lang_context ();
7906 parser->implicit_extern_c = false;
7909 if (token->type == CPP_PRAGMA)
7911 /* A top-level declaration can consist solely of a #pragma.
7912 A nested declaration cannot, so this is done here and not
7913 in cp_parser_declaration. (A #pragma at block scope is
7914 handled in cp_parser_statement.) */
7915 cp_parser_pragma (parser, pragma_external);
7919 /* Parse the declaration itself. */
7920 cp_parser_declaration (parser);
7924 /* Parse a declaration.
7929 template-declaration
7930 explicit-instantiation
7931 explicit-specialization
7932 linkage-specification
7933 namespace-definition
7938 __extension__ declaration */
7941 cp_parser_declaration (cp_parser* parser)
7948 /* Check for the `__extension__' keyword. */
7949 if (cp_parser_extension_opt (parser, &saved_pedantic))
7951 /* Parse the qualified declaration. */
7952 cp_parser_declaration (parser);
7953 /* Restore the PEDANTIC flag. */
7954 pedantic = saved_pedantic;
7959 /* Try to figure out what kind of declaration is present. */
7960 token1 = *cp_lexer_peek_token (parser->lexer);
7962 if (token1.type != CPP_EOF)
7963 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7966 token2.type = CPP_EOF;
7967 token2.keyword = RID_MAX;
7970 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7971 p = obstack_alloc (&declarator_obstack, 0);
7973 /* If the next token is `extern' and the following token is a string
7974 literal, then we have a linkage specification. */
7975 if (token1.keyword == RID_EXTERN
7976 && cp_parser_is_string_literal (&token2))
7977 cp_parser_linkage_specification (parser);
7978 /* If the next token is `template', then we have either a template
7979 declaration, an explicit instantiation, or an explicit
7981 else if (token1.keyword == RID_TEMPLATE)
7983 /* `template <>' indicates a template specialization. */
7984 if (token2.type == CPP_LESS
7985 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7986 cp_parser_explicit_specialization (parser);
7987 /* `template <' indicates a template declaration. */
7988 else if (token2.type == CPP_LESS)
7989 cp_parser_template_declaration (parser, /*member_p=*/false);
7990 /* Anything else must be an explicit instantiation. */
7992 cp_parser_explicit_instantiation (parser);
7994 /* If the next token is `export', then we have a template
7996 else if (token1.keyword == RID_EXPORT)
7997 cp_parser_template_declaration (parser, /*member_p=*/false);
7998 /* If the next token is `extern', 'static' or 'inline' and the one
7999 after that is `template', we have a GNU extended explicit
8000 instantiation directive. */
8001 else if (cp_parser_allow_gnu_extensions_p (parser)
8002 && (token1.keyword == RID_EXTERN
8003 || token1.keyword == RID_STATIC
8004 || token1.keyword == RID_INLINE)
8005 && token2.keyword == RID_TEMPLATE)
8006 cp_parser_explicit_instantiation (parser);
8007 /* If the next token is `namespace', check for a named or unnamed
8008 namespace definition. */
8009 else if (token1.keyword == RID_NAMESPACE
8010 && (/* A named namespace definition. */
8011 (token2.type == CPP_NAME
8012 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8014 /* An unnamed namespace definition. */
8015 || token2.type == CPP_OPEN_BRACE
8016 || token2.keyword == RID_ATTRIBUTE))
8017 cp_parser_namespace_definition (parser);
8018 /* An inline (associated) namespace definition. */
8019 else if (token1.keyword == RID_INLINE
8020 && token2.keyword == RID_NAMESPACE)
8021 cp_parser_namespace_definition (parser);
8022 /* Objective-C++ declaration/definition. */
8023 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8024 cp_parser_objc_declaration (parser);
8025 /* We must have either a block declaration or a function
8028 /* Try to parse a block-declaration, or a function-definition. */
8029 cp_parser_block_declaration (parser, /*statement_p=*/false);
8031 /* Free any declarators allocated. */
8032 obstack_free (&declarator_obstack, p);
8035 /* Parse a block-declaration.
8040 namespace-alias-definition
8047 __extension__ block-declaration
8052 static_assert-declaration
8054 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8055 part of a declaration-statement. */
8058 cp_parser_block_declaration (cp_parser *parser,
8064 /* Check for the `__extension__' keyword. */
8065 if (cp_parser_extension_opt (parser, &saved_pedantic))
8067 /* Parse the qualified declaration. */
8068 cp_parser_block_declaration (parser, statement_p);
8069 /* Restore the PEDANTIC flag. */
8070 pedantic = saved_pedantic;
8075 /* Peek at the next token to figure out which kind of declaration is
8077 token1 = cp_lexer_peek_token (parser->lexer);
8079 /* If the next keyword is `asm', we have an asm-definition. */
8080 if (token1->keyword == RID_ASM)
8083 cp_parser_commit_to_tentative_parse (parser);
8084 cp_parser_asm_definition (parser);
8086 /* If the next keyword is `namespace', we have a
8087 namespace-alias-definition. */
8088 else if (token1->keyword == RID_NAMESPACE)
8089 cp_parser_namespace_alias_definition (parser);
8090 /* If the next keyword is `using', we have either a
8091 using-declaration or a using-directive. */
8092 else if (token1->keyword == RID_USING)
8097 cp_parser_commit_to_tentative_parse (parser);
8098 /* If the token after `using' is `namespace', then we have a
8100 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8101 if (token2->keyword == RID_NAMESPACE)
8102 cp_parser_using_directive (parser);
8103 /* Otherwise, it's a using-declaration. */
8105 cp_parser_using_declaration (parser,
8106 /*access_declaration_p=*/false);
8108 /* If the next keyword is `__label__' we have a misplaced label
8110 else if (token1->keyword == RID_LABEL)
8112 cp_lexer_consume_token (parser->lexer);
8113 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8114 cp_parser_skip_to_end_of_statement (parser);
8115 /* If the next token is now a `;', consume it. */
8116 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8117 cp_lexer_consume_token (parser->lexer);
8119 /* If the next token is `static_assert' we have a static assertion. */
8120 else if (token1->keyword == RID_STATIC_ASSERT)
8121 cp_parser_static_assert (parser, /*member_p=*/false);
8122 /* Anything else must be a simple-declaration. */
8124 cp_parser_simple_declaration (parser, !statement_p);
8127 /* Parse a simple-declaration.
8130 decl-specifier-seq [opt] init-declarator-list [opt] ;
8132 init-declarator-list:
8134 init-declarator-list , init-declarator
8136 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8137 function-definition as a simple-declaration. */
8140 cp_parser_simple_declaration (cp_parser* parser,
8141 bool function_definition_allowed_p)
8143 cp_decl_specifier_seq decl_specifiers;
8144 int declares_class_or_enum;
8145 bool saw_declarator;
8147 /* Defer access checks until we know what is being declared; the
8148 checks for names appearing in the decl-specifier-seq should be
8149 done as if we were in the scope of the thing being declared. */
8150 push_deferring_access_checks (dk_deferred);
8152 /* Parse the decl-specifier-seq. We have to keep track of whether
8153 or not the decl-specifier-seq declares a named class or
8154 enumeration type, since that is the only case in which the
8155 init-declarator-list is allowed to be empty.
8159 In a simple-declaration, the optional init-declarator-list can be
8160 omitted only when declaring a class or enumeration, that is when
8161 the decl-specifier-seq contains either a class-specifier, an
8162 elaborated-type-specifier, or an enum-specifier. */
8163 cp_parser_decl_specifier_seq (parser,
8164 CP_PARSER_FLAGS_OPTIONAL,
8166 &declares_class_or_enum);
8167 /* We no longer need to defer access checks. */
8168 stop_deferring_access_checks ();
8170 /* In a block scope, a valid declaration must always have a
8171 decl-specifier-seq. By not trying to parse declarators, we can
8172 resolve the declaration/expression ambiguity more quickly. */
8173 if (!function_definition_allowed_p
8174 && !decl_specifiers.any_specifiers_p)
8176 cp_parser_error (parser, "expected declaration");
8180 /* If the next two tokens are both identifiers, the code is
8181 erroneous. The usual cause of this situation is code like:
8185 where "T" should name a type -- but does not. */
8186 if (!decl_specifiers.type
8187 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8189 /* If parsing tentatively, we should commit; we really are
8190 looking at a declaration. */
8191 cp_parser_commit_to_tentative_parse (parser);
8196 /* If we have seen at least one decl-specifier, and the next token
8197 is not a parenthesis, then we must be looking at a declaration.
8198 (After "int (" we might be looking at a functional cast.) */
8199 if (decl_specifiers.any_specifiers_p
8200 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8201 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8202 && !cp_parser_error_occurred (parser))
8203 cp_parser_commit_to_tentative_parse (parser);
8205 /* Keep going until we hit the `;' at the end of the simple
8207 saw_declarator = false;
8208 while (cp_lexer_next_token_is_not (parser->lexer,
8212 bool function_definition_p;
8217 /* If we are processing next declarator, coma is expected */
8218 token = cp_lexer_peek_token (parser->lexer);
8219 gcc_assert (token->type == CPP_COMMA);
8220 cp_lexer_consume_token (parser->lexer);
8223 saw_declarator = true;
8225 /* Parse the init-declarator. */
8226 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8228 function_definition_allowed_p,
8230 declares_class_or_enum,
8231 &function_definition_p);
8232 /* If an error occurred while parsing tentatively, exit quickly.
8233 (That usually happens when in the body of a function; each
8234 statement is treated as a declaration-statement until proven
8236 if (cp_parser_error_occurred (parser))
8238 /* Handle function definitions specially. */
8239 if (function_definition_p)
8241 /* If the next token is a `,', then we are probably
8242 processing something like:
8246 which is erroneous. */
8247 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8249 cp_token *token = cp_lexer_peek_token (parser->lexer);
8250 error ("%Hmixing declarations and function-definitions is forbidden",
8253 /* Otherwise, we're done with the list of declarators. */
8256 pop_deferring_access_checks ();
8260 /* The next token should be either a `,' or a `;'. */
8261 token = cp_lexer_peek_token (parser->lexer);
8262 /* If it's a `,', there are more declarators to come. */
8263 if (token->type == CPP_COMMA)
8264 /* will be consumed next time around */;
8265 /* If it's a `;', we are done. */
8266 else if (token->type == CPP_SEMICOLON)
8268 /* Anything else is an error. */
8271 /* If we have already issued an error message we don't need
8272 to issue another one. */
8273 if (decl != error_mark_node
8274 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8275 cp_parser_error (parser, "expected %<,%> or %<;%>");
8276 /* Skip tokens until we reach the end of the statement. */
8277 cp_parser_skip_to_end_of_statement (parser);
8278 /* If the next token is now a `;', consume it. */
8279 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8280 cp_lexer_consume_token (parser->lexer);
8283 /* After the first time around, a function-definition is not
8284 allowed -- even if it was OK at first. For example:
8289 function_definition_allowed_p = false;
8292 /* Issue an error message if no declarators are present, and the
8293 decl-specifier-seq does not itself declare a class or
8295 if (!saw_declarator)
8297 if (cp_parser_declares_only_class_p (parser))
8298 shadow_tag (&decl_specifiers);
8299 /* Perform any deferred access checks. */
8300 perform_deferred_access_checks ();
8303 /* Consume the `;'. */
8304 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8307 pop_deferring_access_checks ();
8310 /* Parse a decl-specifier-seq.
8313 decl-specifier-seq [opt] decl-specifier
8316 storage-class-specifier
8327 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8329 The parser flags FLAGS is used to control type-specifier parsing.
8331 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8334 1: one of the decl-specifiers is an elaborated-type-specifier
8335 (i.e., a type declaration)
8336 2: one of the decl-specifiers is an enum-specifier or a
8337 class-specifier (i.e., a type definition)
8342 cp_parser_decl_specifier_seq (cp_parser* parser,
8343 cp_parser_flags flags,
8344 cp_decl_specifier_seq *decl_specs,
8345 int* declares_class_or_enum)
8347 bool constructor_possible_p = !parser->in_declarator_p;
8348 cp_token *start_token = NULL;
8350 /* Clear DECL_SPECS. */
8351 clear_decl_specs (decl_specs);
8353 /* Assume no class or enumeration type is declared. */
8354 *declares_class_or_enum = 0;
8356 /* Keep reading specifiers until there are no more to read. */
8360 bool found_decl_spec;
8363 /* Peek at the next token. */
8364 token = cp_lexer_peek_token (parser->lexer);
8366 /* Save the first token of the decl spec list for error
8369 start_token = token;
8370 /* Handle attributes. */
8371 if (token->keyword == RID_ATTRIBUTE)
8373 /* Parse the attributes. */
8374 decl_specs->attributes
8375 = chainon (decl_specs->attributes,
8376 cp_parser_attributes_opt (parser));
8379 /* Assume we will find a decl-specifier keyword. */
8380 found_decl_spec = true;
8381 /* If the next token is an appropriate keyword, we can simply
8382 add it to the list. */
8383 switch (token->keyword)
8388 if (!at_class_scope_p ())
8390 error ("%H%<friend%> used outside of class", &token->location);
8391 cp_lexer_purge_token (parser->lexer);
8395 ++decl_specs->specs[(int) ds_friend];
8396 /* Consume the token. */
8397 cp_lexer_consume_token (parser->lexer);
8401 /* function-specifier:
8408 cp_parser_function_specifier_opt (parser, decl_specs);
8414 ++decl_specs->specs[(int) ds_typedef];
8415 /* Consume the token. */
8416 cp_lexer_consume_token (parser->lexer);
8417 /* A constructor declarator cannot appear in a typedef. */
8418 constructor_possible_p = false;
8419 /* The "typedef" keyword can only occur in a declaration; we
8420 may as well commit at this point. */
8421 cp_parser_commit_to_tentative_parse (parser);
8423 if (decl_specs->storage_class != sc_none)
8424 decl_specs->conflicting_specifiers_p = true;
8427 /* storage-class-specifier:
8437 if (cxx_dialect == cxx98)
8439 /* Consume the token. */
8440 cp_lexer_consume_token (parser->lexer);
8442 /* Complain about `auto' as a storage specifier, if
8443 we're complaining about C++0x compatibility. */
8446 "%H%<auto%> will change meaning in C++0x; please remove it",
8449 /* Set the storage class anyway. */
8450 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8454 /* C++0x auto type-specifier. */
8455 found_decl_spec = false;
8462 /* Consume the token. */
8463 cp_lexer_consume_token (parser->lexer);
8464 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8468 /* Consume the token. */
8469 cp_lexer_consume_token (parser->lexer);
8470 ++decl_specs->specs[(int) ds_thread];
8474 /* We did not yet find a decl-specifier yet. */
8475 found_decl_spec = false;
8479 /* Constructors are a special case. The `S' in `S()' is not a
8480 decl-specifier; it is the beginning of the declarator. */
8483 && constructor_possible_p
8484 && (cp_parser_constructor_declarator_p
8485 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8487 /* If we don't have a DECL_SPEC yet, then we must be looking at
8488 a type-specifier. */
8489 if (!found_decl_spec && !constructor_p)
8491 int decl_spec_declares_class_or_enum;
8492 bool is_cv_qualifier;
8496 = cp_parser_type_specifier (parser, flags,
8498 /*is_declaration=*/true,
8499 &decl_spec_declares_class_or_enum,
8501 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8503 /* If this type-specifier referenced a user-defined type
8504 (a typedef, class-name, etc.), then we can't allow any
8505 more such type-specifiers henceforth.
8509 The longest sequence of decl-specifiers that could
8510 possibly be a type name is taken as the
8511 decl-specifier-seq of a declaration. The sequence shall
8512 be self-consistent as described below.
8516 As a general rule, at most one type-specifier is allowed
8517 in the complete decl-specifier-seq of a declaration. The
8518 only exceptions are the following:
8520 -- const or volatile can be combined with any other
8523 -- signed or unsigned can be combined with char, long,
8531 void g (const int Pc);
8533 Here, Pc is *not* part of the decl-specifier seq; it's
8534 the declarator. Therefore, once we see a type-specifier
8535 (other than a cv-qualifier), we forbid any additional
8536 user-defined types. We *do* still allow things like `int
8537 int' to be considered a decl-specifier-seq, and issue the
8538 error message later. */
8539 if (type_spec && !is_cv_qualifier)
8540 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8541 /* A constructor declarator cannot follow a type-specifier. */
8544 constructor_possible_p = false;
8545 found_decl_spec = true;
8549 /* If we still do not have a DECL_SPEC, then there are no more
8551 if (!found_decl_spec)
8554 decl_specs->any_specifiers_p = true;
8555 /* After we see one decl-specifier, further decl-specifiers are
8557 flags |= CP_PARSER_FLAGS_OPTIONAL;
8560 cp_parser_check_decl_spec (decl_specs, start_token->location);
8562 /* Don't allow a friend specifier with a class definition. */
8563 if (decl_specs->specs[(int) ds_friend] != 0
8564 && (*declares_class_or_enum & 2))
8565 error ("%Hclass definition may not be declared a friend",
8566 &start_token->location);
8569 /* Parse an (optional) storage-class-specifier.
8571 storage-class-specifier:
8580 storage-class-specifier:
8583 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8586 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8588 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8591 if (cxx_dialect != cxx98)
8593 /* Fall through for C++98. */
8600 /* Consume the token. */
8601 return cp_lexer_consume_token (parser->lexer)->u.value;
8608 /* Parse an (optional) function-specifier.
8615 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8616 Updates DECL_SPECS, if it is non-NULL. */
8619 cp_parser_function_specifier_opt (cp_parser* parser,
8620 cp_decl_specifier_seq *decl_specs)
8622 cp_token *token = cp_lexer_peek_token (parser->lexer);
8623 switch (token->keyword)
8627 ++decl_specs->specs[(int) ds_inline];
8631 /* 14.5.2.3 [temp.mem]
8633 A member function template shall not be virtual. */
8634 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8635 error ("%Htemplates may not be %<virtual%>", &token->location);
8636 else if (decl_specs)
8637 ++decl_specs->specs[(int) ds_virtual];
8642 ++decl_specs->specs[(int) ds_explicit];
8649 /* Consume the token. */
8650 return cp_lexer_consume_token (parser->lexer)->u.value;
8653 /* Parse a linkage-specification.
8655 linkage-specification:
8656 extern string-literal { declaration-seq [opt] }
8657 extern string-literal declaration */
8660 cp_parser_linkage_specification (cp_parser* parser)
8664 /* Look for the `extern' keyword. */
8665 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8667 /* Look for the string-literal. */
8668 linkage = cp_parser_string_literal (parser, false, false);
8670 /* Transform the literal into an identifier. If the literal is a
8671 wide-character string, or contains embedded NULs, then we can't
8672 handle it as the user wants. */
8673 if (strlen (TREE_STRING_POINTER (linkage))
8674 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8676 cp_parser_error (parser, "invalid linkage-specification");
8677 /* Assume C++ linkage. */
8678 linkage = lang_name_cplusplus;
8681 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8683 /* We're now using the new linkage. */
8684 push_lang_context (linkage);
8686 /* If the next token is a `{', then we're using the first
8688 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8690 /* Consume the `{' token. */
8691 cp_lexer_consume_token (parser->lexer);
8692 /* Parse the declarations. */
8693 cp_parser_declaration_seq_opt (parser);
8694 /* Look for the closing `}'. */
8695 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8697 /* Otherwise, there's just one declaration. */
8700 bool saved_in_unbraced_linkage_specification_p;
8702 saved_in_unbraced_linkage_specification_p
8703 = parser->in_unbraced_linkage_specification_p;
8704 parser->in_unbraced_linkage_specification_p = true;
8705 cp_parser_declaration (parser);
8706 parser->in_unbraced_linkage_specification_p
8707 = saved_in_unbraced_linkage_specification_p;
8710 /* We're done with the linkage-specification. */
8711 pop_lang_context ();
8714 /* Parse a static_assert-declaration.
8716 static_assert-declaration:
8717 static_assert ( constant-expression , string-literal ) ;
8719 If MEMBER_P, this static_assert is a class member. */
8722 cp_parser_static_assert(cp_parser *parser, bool member_p)
8727 location_t saved_loc;
8729 /* Peek at the `static_assert' token so we can keep track of exactly
8730 where the static assertion started. */
8731 token = cp_lexer_peek_token (parser->lexer);
8732 saved_loc = token->location;
8734 /* Look for the `static_assert' keyword. */
8735 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8736 "%<static_assert%>"))
8739 /* We know we are in a static assertion; commit to any tentative
8741 if (cp_parser_parsing_tentatively (parser))
8742 cp_parser_commit_to_tentative_parse (parser);
8744 /* Parse the `(' starting the static assertion condition. */
8745 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8747 /* Parse the constant-expression. */
8749 cp_parser_constant_expression (parser,
8750 /*allow_non_constant_p=*/false,
8751 /*non_constant_p=*/NULL);
8753 /* Parse the separating `,'. */
8754 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8756 /* Parse the string-literal message. */
8757 message = cp_parser_string_literal (parser,
8758 /*translate=*/false,
8761 /* A `)' completes the static assertion. */
8762 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8763 cp_parser_skip_to_closing_parenthesis (parser,
8764 /*recovering=*/true,
8766 /*consume_paren=*/true);
8768 /* A semicolon terminates the declaration. */
8769 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8771 /* Complete the static assertion, which may mean either processing
8772 the static assert now or saving it for template instantiation. */
8773 finish_static_assert (condition, message, saved_loc, member_p);
8776 /* Parse a `decltype' type. Returns the type.
8778 simple-type-specifier:
8779 decltype ( expression ) */
8782 cp_parser_decltype (cp_parser *parser)
8785 bool id_expression_or_member_access_p = false;
8786 const char *saved_message;
8787 bool saved_integral_constant_expression_p;
8788 bool saved_non_integral_constant_expression_p;
8789 cp_token *id_expr_start_token;
8791 /* Look for the `decltype' token. */
8792 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8793 return error_mark_node;
8795 /* Types cannot be defined in a `decltype' expression. Save away the
8797 saved_message = parser->type_definition_forbidden_message;
8799 /* And create the new one. */
8800 parser->type_definition_forbidden_message
8801 = "types may not be defined in %<decltype%> expressions";
8803 /* The restrictions on constant-expressions do not apply inside
8804 decltype expressions. */
8805 saved_integral_constant_expression_p
8806 = parser->integral_constant_expression_p;
8807 saved_non_integral_constant_expression_p
8808 = parser->non_integral_constant_expression_p;
8809 parser->integral_constant_expression_p = false;
8811 /* Do not actually evaluate the expression. */
8814 /* Parse the opening `('. */
8815 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8816 return error_mark_node;
8818 /* First, try parsing an id-expression. */
8819 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8820 cp_parser_parse_tentatively (parser);
8821 expr = cp_parser_id_expression (parser,
8822 /*template_keyword_p=*/false,
8823 /*check_dependency_p=*/true,
8824 /*template_p=*/NULL,
8825 /*declarator_p=*/false,
8826 /*optional_p=*/false);
8828 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8830 bool non_integral_constant_expression_p = false;
8831 tree id_expression = expr;
8833 const char *error_msg;
8835 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8836 /* Lookup the name we got back from the id-expression. */
8837 expr = cp_parser_lookup_name (parser, expr,
8839 /*is_template=*/false,
8840 /*is_namespace=*/false,
8841 /*check_dependency=*/true,
8842 /*ambiguous_decls=*/NULL,
8843 id_expr_start_token->location);
8846 && expr != error_mark_node
8847 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8848 && TREE_CODE (expr) != TYPE_DECL
8849 && (TREE_CODE (expr) != BIT_NOT_EXPR
8850 || !TYPE_P (TREE_OPERAND (expr, 0)))
8851 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8853 /* Complete lookup of the id-expression. */
8854 expr = (finish_id_expression
8855 (id_expression, expr, parser->scope, &idk,
8856 /*integral_constant_expression_p=*/false,
8857 /*allow_non_integral_constant_expression_p=*/true,
8858 &non_integral_constant_expression_p,
8859 /*template_p=*/false,
8861 /*address_p=*/false,
8862 /*template_arg_p=*/false,
8864 id_expr_start_token->location));
8866 if (expr == error_mark_node)
8867 /* We found an id-expression, but it was something that we
8868 should not have found. This is an error, not something
8869 we can recover from, so note that we found an
8870 id-expression and we'll recover as gracefully as
8872 id_expression_or_member_access_p = true;
8876 && expr != error_mark_node
8877 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8878 /* We have an id-expression. */
8879 id_expression_or_member_access_p = true;
8882 if (!id_expression_or_member_access_p)
8884 /* Abort the id-expression parse. */
8885 cp_parser_abort_tentative_parse (parser);
8887 /* Parsing tentatively, again. */
8888 cp_parser_parse_tentatively (parser);
8890 /* Parse a class member access. */
8891 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8893 /*member_access_only_p=*/true, NULL);
8896 && expr != error_mark_node
8897 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8898 /* We have an id-expression. */
8899 id_expression_or_member_access_p = true;
8902 if (id_expression_or_member_access_p)
8903 /* We have parsed the complete id-expression or member access. */
8904 cp_parser_parse_definitely (parser);
8907 /* Abort our attempt to parse an id-expression or member access
8909 cp_parser_abort_tentative_parse (parser);
8911 /* Parse a full expression. */
8912 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8915 /* Go back to evaluating expressions. */
8918 /* Restore the old message and the integral constant expression
8920 parser->type_definition_forbidden_message = saved_message;
8921 parser->integral_constant_expression_p
8922 = saved_integral_constant_expression_p;
8923 parser->non_integral_constant_expression_p
8924 = saved_non_integral_constant_expression_p;
8926 if (expr == error_mark_node)
8928 /* Skip everything up to the closing `)'. */
8929 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8930 /*consume_paren=*/true);
8931 return error_mark_node;
8934 /* Parse to the closing `)'. */
8935 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8937 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8938 /*consume_paren=*/true);
8939 return error_mark_node;
8942 return finish_decltype_type (expr, id_expression_or_member_access_p);
8945 /* Special member functions [gram.special] */
8947 /* Parse a conversion-function-id.
8949 conversion-function-id:
8950 operator conversion-type-id
8952 Returns an IDENTIFIER_NODE representing the operator. */
8955 cp_parser_conversion_function_id (cp_parser* parser)
8959 tree saved_qualifying_scope;
8960 tree saved_object_scope;
8961 tree pushed_scope = NULL_TREE;
8963 /* Look for the `operator' token. */
8964 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8965 return error_mark_node;
8966 /* When we parse the conversion-type-id, the current scope will be
8967 reset. However, we need that information in able to look up the
8968 conversion function later, so we save it here. */
8969 saved_scope = parser->scope;
8970 saved_qualifying_scope = parser->qualifying_scope;
8971 saved_object_scope = parser->object_scope;
8972 /* We must enter the scope of the class so that the names of
8973 entities declared within the class are available in the
8974 conversion-type-id. For example, consider:
8981 S::operator I() { ... }
8983 In order to see that `I' is a type-name in the definition, we
8984 must be in the scope of `S'. */
8986 pushed_scope = push_scope (saved_scope);
8987 /* Parse the conversion-type-id. */
8988 type = cp_parser_conversion_type_id (parser);
8989 /* Leave the scope of the class, if any. */
8991 pop_scope (pushed_scope);
8992 /* Restore the saved scope. */
8993 parser->scope = saved_scope;
8994 parser->qualifying_scope = saved_qualifying_scope;
8995 parser->object_scope = saved_object_scope;
8996 /* If the TYPE is invalid, indicate failure. */
8997 if (type == error_mark_node)
8998 return error_mark_node;
8999 return mangle_conv_op_name_for_type (type);
9002 /* Parse a conversion-type-id:
9005 type-specifier-seq conversion-declarator [opt]
9007 Returns the TYPE specified. */
9010 cp_parser_conversion_type_id (cp_parser* parser)
9013 cp_decl_specifier_seq type_specifiers;
9014 cp_declarator *declarator;
9015 tree type_specified;
9017 /* Parse the attributes. */
9018 attributes = cp_parser_attributes_opt (parser);
9019 /* Parse the type-specifiers. */
9020 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9022 /* If that didn't work, stop. */
9023 if (type_specifiers.type == error_mark_node)
9024 return error_mark_node;
9025 /* Parse the conversion-declarator. */
9026 declarator = cp_parser_conversion_declarator_opt (parser);
9028 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9029 /*initialized=*/0, &attributes);
9031 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9033 /* Don't give this error when parsing tentatively. This happens to
9034 work because we always parse this definitively once. */
9035 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9036 && type_uses_auto (type_specified))
9038 error ("invalid use of %<auto%> in conversion operator");
9039 return error_mark_node;
9042 return type_specified;
9045 /* Parse an (optional) conversion-declarator.
9047 conversion-declarator:
9048 ptr-operator conversion-declarator [opt]
9052 static cp_declarator *
9053 cp_parser_conversion_declarator_opt (cp_parser* parser)
9055 enum tree_code code;
9057 cp_cv_quals cv_quals;
9059 /* We don't know if there's a ptr-operator next, or not. */
9060 cp_parser_parse_tentatively (parser);
9061 /* Try the ptr-operator. */
9062 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9063 /* If it worked, look for more conversion-declarators. */
9064 if (cp_parser_parse_definitely (parser))
9066 cp_declarator *declarator;
9068 /* Parse another optional declarator. */
9069 declarator = cp_parser_conversion_declarator_opt (parser);
9071 return cp_parser_make_indirect_declarator
9072 (code, class_type, cv_quals, declarator);
9078 /* Parse an (optional) ctor-initializer.
9081 : mem-initializer-list
9083 Returns TRUE iff the ctor-initializer was actually present. */
9086 cp_parser_ctor_initializer_opt (cp_parser* parser)
9088 /* If the next token is not a `:', then there is no
9089 ctor-initializer. */
9090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9092 /* Do default initialization of any bases and members. */
9093 if (DECL_CONSTRUCTOR_P (current_function_decl))
9094 finish_mem_initializers (NULL_TREE);
9099 /* Consume the `:' token. */
9100 cp_lexer_consume_token (parser->lexer);
9101 /* And the mem-initializer-list. */
9102 cp_parser_mem_initializer_list (parser);
9107 /* Parse a mem-initializer-list.
9109 mem-initializer-list:
9110 mem-initializer ... [opt]
9111 mem-initializer ... [opt] , mem-initializer-list */
9114 cp_parser_mem_initializer_list (cp_parser* parser)
9116 tree mem_initializer_list = NULL_TREE;
9117 cp_token *token = cp_lexer_peek_token (parser->lexer);
9119 /* Let the semantic analysis code know that we are starting the
9120 mem-initializer-list. */
9121 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9122 error ("%Honly constructors take base initializers",
9125 /* Loop through the list. */
9128 tree mem_initializer;
9130 token = cp_lexer_peek_token (parser->lexer);
9131 /* Parse the mem-initializer. */
9132 mem_initializer = cp_parser_mem_initializer (parser);
9133 /* If the next token is a `...', we're expanding member initializers. */
9134 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9136 /* Consume the `...'. */
9137 cp_lexer_consume_token (parser->lexer);
9139 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9140 can be expanded but members cannot. */
9141 if (mem_initializer != error_mark_node
9142 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9144 error ("%Hcannot expand initializer for member %<%D%>",
9145 &token->location, TREE_PURPOSE (mem_initializer));
9146 mem_initializer = error_mark_node;
9149 /* Construct the pack expansion type. */
9150 if (mem_initializer != error_mark_node)
9151 mem_initializer = make_pack_expansion (mem_initializer);
9153 /* Add it to the list, unless it was erroneous. */
9154 if (mem_initializer != error_mark_node)
9156 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9157 mem_initializer_list = mem_initializer;
9159 /* If the next token is not a `,', we're done. */
9160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9162 /* Consume the `,' token. */
9163 cp_lexer_consume_token (parser->lexer);
9166 /* Perform semantic analysis. */
9167 if (DECL_CONSTRUCTOR_P (current_function_decl))
9168 finish_mem_initializers (mem_initializer_list);
9171 /* Parse a mem-initializer.
9174 mem-initializer-id ( expression-list [opt] )
9175 mem-initializer-id braced-init-list
9180 ( expression-list [opt] )
9182 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9183 class) or FIELD_DECL (for a non-static data member) to initialize;
9184 the TREE_VALUE is the expression-list. An empty initialization
9185 list is represented by void_list_node. */
9188 cp_parser_mem_initializer (cp_parser* parser)
9190 tree mem_initializer_id;
9191 tree expression_list;
9193 cp_token *token = cp_lexer_peek_token (parser->lexer);
9195 /* Find out what is being initialized. */
9196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9198 permerror (token->location,
9199 "anachronistic old-style base class initializer");
9200 mem_initializer_id = NULL_TREE;
9204 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9205 if (mem_initializer_id == error_mark_node)
9206 return mem_initializer_id;
9208 member = expand_member_init (mem_initializer_id);
9209 if (member && !DECL_P (member))
9210 in_base_initializer = 1;
9212 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9214 bool expr_non_constant_p;
9215 maybe_warn_cpp0x ("extended initializer lists");
9216 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9217 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9218 expression_list = build_tree_list (NULL_TREE, expression_list);
9222 = cp_parser_parenthesized_expression_list (parser, false,
9224 /*allow_expansion_p=*/true,
9225 /*non_constant_p=*/NULL);
9226 if (expression_list == error_mark_node)
9227 return error_mark_node;
9228 if (!expression_list)
9229 expression_list = void_type_node;
9231 in_base_initializer = 0;
9233 return member ? build_tree_list (member, expression_list) : error_mark_node;
9236 /* Parse a mem-initializer-id.
9239 :: [opt] nested-name-specifier [opt] class-name
9242 Returns a TYPE indicating the class to be initializer for the first
9243 production. Returns an IDENTIFIER_NODE indicating the data member
9244 to be initialized for the second production. */
9247 cp_parser_mem_initializer_id (cp_parser* parser)
9249 bool global_scope_p;
9250 bool nested_name_specifier_p;
9251 bool template_p = false;
9254 cp_token *token = cp_lexer_peek_token (parser->lexer);
9256 /* `typename' is not allowed in this context ([temp.res]). */
9257 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9259 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9260 "member initializer is implicitly a type)",
9262 cp_lexer_consume_token (parser->lexer);
9264 /* Look for the optional `::' operator. */
9266 = (cp_parser_global_scope_opt (parser,
9267 /*current_scope_valid_p=*/false)
9269 /* Look for the optional nested-name-specifier. The simplest way to
9274 The keyword `typename' is not permitted in a base-specifier or
9275 mem-initializer; in these contexts a qualified name that
9276 depends on a template-parameter is implicitly assumed to be a
9279 is to assume that we have seen the `typename' keyword at this
9281 nested_name_specifier_p
9282 = (cp_parser_nested_name_specifier_opt (parser,
9283 /*typename_keyword_p=*/true,
9284 /*check_dependency_p=*/true,
9286 /*is_declaration=*/true)
9288 if (nested_name_specifier_p)
9289 template_p = cp_parser_optional_template_keyword (parser);
9290 /* If there is a `::' operator or a nested-name-specifier, then we
9291 are definitely looking for a class-name. */
9292 if (global_scope_p || nested_name_specifier_p)
9293 return cp_parser_class_name (parser,
9294 /*typename_keyword_p=*/true,
9295 /*template_keyword_p=*/template_p,
9297 /*check_dependency_p=*/true,
9298 /*class_head_p=*/false,
9299 /*is_declaration=*/true);
9300 /* Otherwise, we could also be looking for an ordinary identifier. */
9301 cp_parser_parse_tentatively (parser);
9302 /* Try a class-name. */
9303 id = cp_parser_class_name (parser,
9304 /*typename_keyword_p=*/true,
9305 /*template_keyword_p=*/false,
9307 /*check_dependency_p=*/true,
9308 /*class_head_p=*/false,
9309 /*is_declaration=*/true);
9310 /* If we found one, we're done. */
9311 if (cp_parser_parse_definitely (parser))
9313 /* Otherwise, look for an ordinary identifier. */
9314 return cp_parser_identifier (parser);
9317 /* Overloading [gram.over] */
9319 /* Parse an operator-function-id.
9321 operator-function-id:
9324 Returns an IDENTIFIER_NODE for the operator which is a
9325 human-readable spelling of the identifier, e.g., `operator +'. */
9328 cp_parser_operator_function_id (cp_parser* parser)
9330 /* Look for the `operator' keyword. */
9331 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9332 return error_mark_node;
9333 /* And then the name of the operator itself. */
9334 return cp_parser_operator (parser);
9337 /* Parse an operator.
9340 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9341 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9342 || ++ -- , ->* -> () []
9349 Returns an IDENTIFIER_NODE for the operator which is a
9350 human-readable spelling of the identifier, e.g., `operator +'. */
9353 cp_parser_operator (cp_parser* parser)
9355 tree id = NULL_TREE;
9358 /* Peek at the next token. */
9359 token = cp_lexer_peek_token (parser->lexer);
9360 /* Figure out which operator we have. */
9361 switch (token->type)
9367 /* The keyword should be either `new' or `delete'. */
9368 if (token->keyword == RID_NEW)
9370 else if (token->keyword == RID_DELETE)
9375 /* Consume the `new' or `delete' token. */
9376 cp_lexer_consume_token (parser->lexer);
9378 /* Peek at the next token. */
9379 token = cp_lexer_peek_token (parser->lexer);
9380 /* If it's a `[' token then this is the array variant of the
9382 if (token->type == CPP_OPEN_SQUARE)
9384 /* Consume the `[' token. */
9385 cp_lexer_consume_token (parser->lexer);
9386 /* Look for the `]' token. */
9387 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9388 id = ansi_opname (op == NEW_EXPR
9389 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9391 /* Otherwise, we have the non-array variant. */
9393 id = ansi_opname (op);
9399 id = ansi_opname (PLUS_EXPR);
9403 id = ansi_opname (MINUS_EXPR);
9407 id = ansi_opname (MULT_EXPR);
9411 id = ansi_opname (TRUNC_DIV_EXPR);
9415 id = ansi_opname (TRUNC_MOD_EXPR);
9419 id = ansi_opname (BIT_XOR_EXPR);
9423 id = ansi_opname (BIT_AND_EXPR);
9427 id = ansi_opname (BIT_IOR_EXPR);
9431 id = ansi_opname (BIT_NOT_EXPR);
9435 id = ansi_opname (TRUTH_NOT_EXPR);
9439 id = ansi_assopname (NOP_EXPR);
9443 id = ansi_opname (LT_EXPR);
9447 id = ansi_opname (GT_EXPR);
9451 id = ansi_assopname (PLUS_EXPR);
9455 id = ansi_assopname (MINUS_EXPR);
9459 id = ansi_assopname (MULT_EXPR);
9463 id = ansi_assopname (TRUNC_DIV_EXPR);
9467 id = ansi_assopname (TRUNC_MOD_EXPR);
9471 id = ansi_assopname (BIT_XOR_EXPR);
9475 id = ansi_assopname (BIT_AND_EXPR);
9479 id = ansi_assopname (BIT_IOR_EXPR);
9483 id = ansi_opname (LSHIFT_EXPR);
9487 id = ansi_opname (RSHIFT_EXPR);
9491 id = ansi_assopname (LSHIFT_EXPR);
9495 id = ansi_assopname (RSHIFT_EXPR);
9499 id = ansi_opname (EQ_EXPR);
9503 id = ansi_opname (NE_EXPR);
9507 id = ansi_opname (LE_EXPR);
9510 case CPP_GREATER_EQ:
9511 id = ansi_opname (GE_EXPR);
9515 id = ansi_opname (TRUTH_ANDIF_EXPR);
9519 id = ansi_opname (TRUTH_ORIF_EXPR);
9523 id = ansi_opname (POSTINCREMENT_EXPR);
9526 case CPP_MINUS_MINUS:
9527 id = ansi_opname (PREDECREMENT_EXPR);
9531 id = ansi_opname (COMPOUND_EXPR);
9534 case CPP_DEREF_STAR:
9535 id = ansi_opname (MEMBER_REF);
9539 id = ansi_opname (COMPONENT_REF);
9542 case CPP_OPEN_PAREN:
9543 /* Consume the `('. */
9544 cp_lexer_consume_token (parser->lexer);
9545 /* Look for the matching `)'. */
9546 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9547 return ansi_opname (CALL_EXPR);
9549 case CPP_OPEN_SQUARE:
9550 /* Consume the `['. */
9551 cp_lexer_consume_token (parser->lexer);
9552 /* Look for the matching `]'. */
9553 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9554 return ansi_opname (ARRAY_REF);
9557 /* Anything else is an error. */
9561 /* If we have selected an identifier, we need to consume the
9564 cp_lexer_consume_token (parser->lexer);
9565 /* Otherwise, no valid operator name was present. */
9568 cp_parser_error (parser, "expected operator");
9569 id = error_mark_node;
9575 /* Parse a template-declaration.
9577 template-declaration:
9578 export [opt] template < template-parameter-list > declaration
9580 If MEMBER_P is TRUE, this template-declaration occurs within a
9583 The grammar rule given by the standard isn't correct. What
9586 template-declaration:
9587 export [opt] template-parameter-list-seq
9588 decl-specifier-seq [opt] init-declarator [opt] ;
9589 export [opt] template-parameter-list-seq
9592 template-parameter-list-seq:
9593 template-parameter-list-seq [opt]
9594 template < template-parameter-list > */
9597 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9599 /* Check for `export'. */
9600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9602 /* Consume the `export' token. */
9603 cp_lexer_consume_token (parser->lexer);
9604 /* Warn that we do not support `export'. */
9605 warning (0, "keyword %<export%> not implemented, and will be ignored");
9608 cp_parser_template_declaration_after_export (parser, member_p);
9611 /* Parse a template-parameter-list.
9613 template-parameter-list:
9615 template-parameter-list , template-parameter
9617 Returns a TREE_LIST. Each node represents a template parameter.
9618 The nodes are connected via their TREE_CHAINs. */
9621 cp_parser_template_parameter_list (cp_parser* parser)
9623 tree parameter_list = NULL_TREE;
9625 begin_template_parm_list ();
9630 bool is_parameter_pack;
9632 /* Parse the template-parameter. */
9633 parameter = cp_parser_template_parameter (parser,
9635 &is_parameter_pack);
9636 /* Add it to the list. */
9637 if (parameter != error_mark_node)
9638 parameter_list = process_template_parm (parameter_list,
9644 tree err_parm = build_tree_list (parameter, parameter);
9645 TREE_VALUE (err_parm) = error_mark_node;
9646 parameter_list = chainon (parameter_list, err_parm);
9649 /* If the next token is not a `,', we're done. */
9650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9652 /* Otherwise, consume the `,' token. */
9653 cp_lexer_consume_token (parser->lexer);
9656 return end_template_parm_list (parameter_list);
9659 /* Parse a template-parameter.
9663 parameter-declaration
9665 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9666 the parameter. The TREE_PURPOSE is the default value, if any.
9667 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9668 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9669 set to true iff this parameter is a parameter pack. */
9672 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9673 bool *is_parameter_pack)
9676 cp_parameter_declarator *parameter_declarator;
9677 cp_declarator *id_declarator;
9680 /* Assume it is a type parameter or a template parameter. */
9681 *is_non_type = false;
9682 /* Assume it not a parameter pack. */
9683 *is_parameter_pack = false;
9684 /* Peek at the next token. */
9685 token = cp_lexer_peek_token (parser->lexer);
9686 /* If it is `class' or `template', we have a type-parameter. */
9687 if (token->keyword == RID_TEMPLATE)
9688 return cp_parser_type_parameter (parser, is_parameter_pack);
9689 /* If it is `class' or `typename' we do not know yet whether it is a
9690 type parameter or a non-type parameter. Consider:
9692 template <typename T, typename T::X X> ...
9696 template <class C, class D*> ...
9698 Here, the first parameter is a type parameter, and the second is
9699 a non-type parameter. We can tell by looking at the token after
9700 the identifier -- if it is a `,', `=', or `>' then we have a type
9702 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9704 /* Peek at the token after `class' or `typename'. */
9705 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9706 /* If it's an ellipsis, we have a template type parameter
9708 if (token->type == CPP_ELLIPSIS)
9709 return cp_parser_type_parameter (parser, is_parameter_pack);
9710 /* If it's an identifier, skip it. */
9711 if (token->type == CPP_NAME)
9712 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9713 /* Now, see if the token looks like the end of a template
9715 if (token->type == CPP_COMMA
9716 || token->type == CPP_EQ
9717 || token->type == CPP_GREATER)
9718 return cp_parser_type_parameter (parser, is_parameter_pack);
9721 /* Otherwise, it is a non-type parameter.
9725 When parsing a default template-argument for a non-type
9726 template-parameter, the first non-nested `>' is taken as the end
9727 of the template parameter-list rather than a greater-than
9729 *is_non_type = true;
9730 parameter_declarator
9731 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9732 /*parenthesized_p=*/NULL);
9734 /* If the parameter declaration is marked as a parameter pack, set
9735 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9736 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9738 if (parameter_declarator
9739 && parameter_declarator->declarator
9740 && parameter_declarator->declarator->parameter_pack_p)
9742 *is_parameter_pack = true;
9743 parameter_declarator->declarator->parameter_pack_p = false;
9746 /* If the next token is an ellipsis, and we don't already have it
9747 marked as a parameter pack, then we have a parameter pack (that
9748 has no declarator). */
9749 if (!*is_parameter_pack
9750 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9751 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9753 /* Consume the `...'. */
9754 cp_lexer_consume_token (parser->lexer);
9755 maybe_warn_variadic_templates ();
9757 *is_parameter_pack = true;
9759 /* We might end up with a pack expansion as the type of the non-type
9760 template parameter, in which case this is a non-type template
9762 else if (parameter_declarator
9763 && parameter_declarator->decl_specifiers.type
9764 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9766 *is_parameter_pack = true;
9767 parameter_declarator->decl_specifiers.type =
9768 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9771 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9773 /* Parameter packs cannot have default arguments. However, a
9774 user may try to do so, so we'll parse them and give an
9775 appropriate diagnostic here. */
9777 /* Consume the `='. */
9778 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9779 cp_lexer_consume_token (parser->lexer);
9781 /* Find the name of the parameter pack. */
9782 id_declarator = parameter_declarator->declarator;
9783 while (id_declarator && id_declarator->kind != cdk_id)
9784 id_declarator = id_declarator->declarator;
9786 if (id_declarator && id_declarator->kind == cdk_id)
9787 error ("%Htemplate parameter pack %qD cannot have a default argument",
9788 &start_token->location, id_declarator->u.id.unqualified_name);
9790 error ("%Htemplate parameter pack cannot have a default argument",
9791 &start_token->location);
9793 /* Parse the default argument, but throw away the result. */
9794 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9797 parm = grokdeclarator (parameter_declarator->declarator,
9798 ¶meter_declarator->decl_specifiers,
9799 PARM, /*initialized=*/0,
9801 if (parm == error_mark_node)
9802 return error_mark_node;
9804 return build_tree_list (parameter_declarator->default_argument, parm);
9807 /* Parse a type-parameter.
9810 class identifier [opt]
9811 class identifier [opt] = type-id
9812 typename identifier [opt]
9813 typename identifier [opt] = type-id
9814 template < template-parameter-list > class identifier [opt]
9815 template < template-parameter-list > class identifier [opt]
9818 GNU Extension (variadic templates):
9821 class ... identifier [opt]
9822 typename ... identifier [opt]
9824 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9825 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9826 the declaration of the parameter.
9828 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9831 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9836 /* Look for a keyword to tell us what kind of parameter this is. */
9837 token = cp_parser_require (parser, CPP_KEYWORD,
9838 "%<class%>, %<typename%>, or %<template%>");
9840 return error_mark_node;
9842 switch (token->keyword)
9848 tree default_argument;
9850 /* If the next token is an ellipsis, we have a template
9852 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9854 /* Consume the `...' token. */
9855 cp_lexer_consume_token (parser->lexer);
9856 maybe_warn_variadic_templates ();
9858 *is_parameter_pack = true;
9861 /* If the next token is an identifier, then it names the
9863 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9864 identifier = cp_parser_identifier (parser);
9866 identifier = NULL_TREE;
9868 /* Create the parameter. */
9869 parameter = finish_template_type_parm (class_type_node, identifier);
9871 /* If the next token is an `=', we have a default argument. */
9872 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9874 /* Consume the `=' token. */
9875 cp_lexer_consume_token (parser->lexer);
9876 /* Parse the default-argument. */
9877 push_deferring_access_checks (dk_no_deferred);
9878 default_argument = cp_parser_type_id (parser);
9880 /* Template parameter packs cannot have default
9882 if (*is_parameter_pack)
9885 error ("%Htemplate parameter pack %qD cannot have a "
9886 "default argument", &token->location, identifier);
9888 error ("%Htemplate parameter packs cannot have "
9889 "default arguments", &token->location);
9890 default_argument = NULL_TREE;
9892 pop_deferring_access_checks ();
9895 default_argument = NULL_TREE;
9897 /* Create the combined representation of the parameter and the
9898 default argument. */
9899 parameter = build_tree_list (default_argument, parameter);
9905 tree parameter_list;
9907 tree default_argument;
9909 /* Look for the `<'. */
9910 cp_parser_require (parser, CPP_LESS, "%<<%>");
9911 /* Parse the template-parameter-list. */
9912 parameter_list = cp_parser_template_parameter_list (parser);
9913 /* Look for the `>'. */
9914 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9915 /* Look for the `class' keyword. */
9916 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9917 /* If the next token is an ellipsis, we have a template
9919 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9921 /* Consume the `...' token. */
9922 cp_lexer_consume_token (parser->lexer);
9923 maybe_warn_variadic_templates ();
9925 *is_parameter_pack = true;
9927 /* If the next token is an `=', then there is a
9928 default-argument. If the next token is a `>', we are at
9929 the end of the parameter-list. If the next token is a `,',
9930 then we are at the end of this parameter. */
9931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9932 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9933 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9935 identifier = cp_parser_identifier (parser);
9936 /* Treat invalid names as if the parameter were nameless. */
9937 if (identifier == error_mark_node)
9938 identifier = NULL_TREE;
9941 identifier = NULL_TREE;
9943 /* Create the template parameter. */
9944 parameter = finish_template_template_parm (class_type_node,
9947 /* If the next token is an `=', then there is a
9948 default-argument. */
9949 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9953 /* Consume the `='. */
9954 cp_lexer_consume_token (parser->lexer);
9955 /* Parse the id-expression. */
9956 push_deferring_access_checks (dk_no_deferred);
9957 /* save token before parsing the id-expression, for error
9959 token = cp_lexer_peek_token (parser->lexer);
9961 = cp_parser_id_expression (parser,
9962 /*template_keyword_p=*/false,
9963 /*check_dependency_p=*/true,
9964 /*template_p=*/&is_template,
9965 /*declarator_p=*/false,
9966 /*optional_p=*/false);
9967 if (TREE_CODE (default_argument) == TYPE_DECL)
9968 /* If the id-expression was a template-id that refers to
9969 a template-class, we already have the declaration here,
9970 so no further lookup is needed. */
9973 /* Look up the name. */
9975 = cp_parser_lookup_name (parser, default_argument,
9977 /*is_template=*/is_template,
9978 /*is_namespace=*/false,
9979 /*check_dependency=*/true,
9980 /*ambiguous_decls=*/NULL,
9982 /* See if the default argument is valid. */
9984 = check_template_template_default_arg (default_argument);
9986 /* Template parameter packs cannot have default
9988 if (*is_parameter_pack)
9991 error ("%Htemplate parameter pack %qD cannot "
9992 "have a default argument",
9993 &token->location, identifier);
9995 error ("%Htemplate parameter packs cannot "
9996 "have default arguments",
9998 default_argument = NULL_TREE;
10000 pop_deferring_access_checks ();
10003 default_argument = NULL_TREE;
10005 /* Create the combined representation of the parameter and the
10006 default argument. */
10007 parameter = build_tree_list (default_argument, parameter);
10012 gcc_unreachable ();
10019 /* Parse a template-id.
10022 template-name < template-argument-list [opt] >
10024 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10025 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10026 returned. Otherwise, if the template-name names a function, or set
10027 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10028 names a class, returns a TYPE_DECL for the specialization.
10030 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10031 uninstantiated templates. */
10034 cp_parser_template_id (cp_parser *parser,
10035 bool template_keyword_p,
10036 bool check_dependency_p,
10037 bool is_declaration)
10043 cp_token_position start_of_id = 0;
10044 deferred_access_check *chk;
10045 VEC (deferred_access_check,gc) *access_check;
10046 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10047 bool is_identifier;
10049 /* If the next token corresponds to a template-id, there is no need
10051 next_token = cp_lexer_peek_token (parser->lexer);
10052 if (next_token->type == CPP_TEMPLATE_ID)
10054 struct tree_check *check_value;
10056 /* Get the stored value. */
10057 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10058 /* Perform any access checks that were deferred. */
10059 access_check = check_value->checks;
10063 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10066 perform_or_defer_access_check (chk->binfo,
10071 /* Return the stored value. */
10072 return check_value->value;
10075 /* Avoid performing name lookup if there is no possibility of
10076 finding a template-id. */
10077 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10078 || (next_token->type == CPP_NAME
10079 && !cp_parser_nth_token_starts_template_argument_list_p
10082 cp_parser_error (parser, "expected template-id");
10083 return error_mark_node;
10086 /* Remember where the template-id starts. */
10087 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10088 start_of_id = cp_lexer_token_position (parser->lexer, false);
10090 push_deferring_access_checks (dk_deferred);
10092 /* Parse the template-name. */
10093 is_identifier = false;
10094 token = cp_lexer_peek_token (parser->lexer);
10095 templ = cp_parser_template_name (parser, template_keyword_p,
10096 check_dependency_p,
10099 if (templ == error_mark_node || is_identifier)
10101 pop_deferring_access_checks ();
10105 /* If we find the sequence `[:' after a template-name, it's probably
10106 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10107 parse correctly the argument list. */
10108 next_token = cp_lexer_peek_token (parser->lexer);
10109 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10110 if (next_token->type == CPP_OPEN_SQUARE
10111 && next_token->flags & DIGRAPH
10112 && next_token_2->type == CPP_COLON
10113 && !(next_token_2->flags & PREV_WHITE))
10115 cp_parser_parse_tentatively (parser);
10116 /* Change `:' into `::'. */
10117 next_token_2->type = CPP_SCOPE;
10118 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10120 cp_lexer_consume_token (parser->lexer);
10122 /* Parse the arguments. */
10123 arguments = cp_parser_enclosed_template_argument_list (parser);
10124 if (!cp_parser_parse_definitely (parser))
10126 /* If we couldn't parse an argument list, then we revert our changes
10127 and return simply an error. Maybe this is not a template-id
10129 next_token_2->type = CPP_COLON;
10130 cp_parser_error (parser, "expected %<<%>");
10131 pop_deferring_access_checks ();
10132 return error_mark_node;
10134 /* Otherwise, emit an error about the invalid digraph, but continue
10135 parsing because we got our argument list. */
10136 if (permerror (next_token->location,
10137 "%<<::%> cannot begin a template-argument list"))
10139 static bool hint = false;
10140 inform (next_token->location,
10141 "%<<:%> is an alternate spelling for %<[%>."
10142 " Insert whitespace between %<<%> and %<::%>");
10143 if (!hint && !flag_permissive)
10145 inform (next_token->location, "(if you use %<-fpermissive%>"
10146 " G++ will accept your code)");
10153 /* Look for the `<' that starts the template-argument-list. */
10154 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10156 pop_deferring_access_checks ();
10157 return error_mark_node;
10159 /* Parse the arguments. */
10160 arguments = cp_parser_enclosed_template_argument_list (parser);
10163 /* Build a representation of the specialization. */
10164 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10165 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10166 else if (DECL_CLASS_TEMPLATE_P (templ)
10167 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10169 bool entering_scope;
10170 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10171 template (rather than some instantiation thereof) only if
10172 is not nested within some other construct. For example, in
10173 "template <typename T> void f(T) { A<T>::", A<T> is just an
10174 instantiation of A. */
10175 entering_scope = (template_parm_scope_p ()
10176 && cp_lexer_next_token_is (parser->lexer,
10179 = finish_template_type (templ, arguments, entering_scope);
10183 /* If it's not a class-template or a template-template, it should be
10184 a function-template. */
10185 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10186 || TREE_CODE (templ) == OVERLOAD
10187 || BASELINK_P (templ)));
10189 template_id = lookup_template_function (templ, arguments);
10192 /* If parsing tentatively, replace the sequence of tokens that makes
10193 up the template-id with a CPP_TEMPLATE_ID token. That way,
10194 should we re-parse the token stream, we will not have to repeat
10195 the effort required to do the parse, nor will we issue duplicate
10196 error messages about problems during instantiation of the
10200 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10202 /* Reset the contents of the START_OF_ID token. */
10203 token->type = CPP_TEMPLATE_ID;
10204 /* Retrieve any deferred checks. Do not pop this access checks yet
10205 so the memory will not be reclaimed during token replacing below. */
10206 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10207 token->u.tree_check_value->value = template_id;
10208 token->u.tree_check_value->checks = get_deferred_access_checks ();
10209 token->keyword = RID_MAX;
10211 /* Purge all subsequent tokens. */
10212 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10214 /* ??? Can we actually assume that, if template_id ==
10215 error_mark_node, we will have issued a diagnostic to the
10216 user, as opposed to simply marking the tentative parse as
10218 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10219 error ("%Hparse error in template argument list",
10223 pop_deferring_access_checks ();
10224 return template_id;
10227 /* Parse a template-name.
10232 The standard should actually say:
10236 operator-function-id
10238 A defect report has been filed about this issue.
10240 A conversion-function-id cannot be a template name because they cannot
10241 be part of a template-id. In fact, looking at this code:
10243 a.operator K<int>()
10245 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10246 It is impossible to call a templated conversion-function-id with an
10247 explicit argument list, since the only allowed template parameter is
10248 the type to which it is converting.
10250 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10251 `template' keyword, in a construction like:
10255 In that case `f' is taken to be a template-name, even though there
10256 is no way of knowing for sure.
10258 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10259 name refers to a set of overloaded functions, at least one of which
10260 is a template, or an IDENTIFIER_NODE with the name of the template,
10261 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10262 names are looked up inside uninstantiated templates. */
10265 cp_parser_template_name (cp_parser* parser,
10266 bool template_keyword_p,
10267 bool check_dependency_p,
10268 bool is_declaration,
10269 bool *is_identifier)
10274 cp_token *token = cp_lexer_peek_token (parser->lexer);
10276 /* If the next token is `operator', then we have either an
10277 operator-function-id or a conversion-function-id. */
10278 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10280 /* We don't know whether we're looking at an
10281 operator-function-id or a conversion-function-id. */
10282 cp_parser_parse_tentatively (parser);
10283 /* Try an operator-function-id. */
10284 identifier = cp_parser_operator_function_id (parser);
10285 /* If that didn't work, try a conversion-function-id. */
10286 if (!cp_parser_parse_definitely (parser))
10288 cp_parser_error (parser, "expected template-name");
10289 return error_mark_node;
10292 /* Look for the identifier. */
10294 identifier = cp_parser_identifier (parser);
10296 /* If we didn't find an identifier, we don't have a template-id. */
10297 if (identifier == error_mark_node)
10298 return error_mark_node;
10300 /* If the name immediately followed the `template' keyword, then it
10301 is a template-name. However, if the next token is not `<', then
10302 we do not treat it as a template-name, since it is not being used
10303 as part of a template-id. This enables us to handle constructs
10306 template <typename T> struct S { S(); };
10307 template <typename T> S<T>::S();
10309 correctly. We would treat `S' as a template -- if it were `S<T>'
10310 -- but we do not if there is no `<'. */
10312 if (processing_template_decl
10313 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10315 /* In a declaration, in a dependent context, we pretend that the
10316 "template" keyword was present in order to improve error
10317 recovery. For example, given:
10319 template <typename T> void f(T::X<int>);
10321 we want to treat "X<int>" as a template-id. */
10323 && !template_keyword_p
10324 && parser->scope && TYPE_P (parser->scope)
10325 && check_dependency_p
10326 && dependent_scope_p (parser->scope)
10327 /* Do not do this for dtors (or ctors), since they never
10328 need the template keyword before their name. */
10329 && !constructor_name_p (identifier, parser->scope))
10331 cp_token_position start = 0;
10333 /* Explain what went wrong. */
10334 error ("%Hnon-template %qD used as template",
10335 &token->location, identifier);
10336 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10337 parser->scope, identifier);
10338 /* If parsing tentatively, find the location of the "<" token. */
10339 if (cp_parser_simulate_error (parser))
10340 start = cp_lexer_token_position (parser->lexer, true);
10341 /* Parse the template arguments so that we can issue error
10342 messages about them. */
10343 cp_lexer_consume_token (parser->lexer);
10344 cp_parser_enclosed_template_argument_list (parser);
10345 /* Skip tokens until we find a good place from which to
10346 continue parsing. */
10347 cp_parser_skip_to_closing_parenthesis (parser,
10348 /*recovering=*/true,
10350 /*consume_paren=*/false);
10351 /* If parsing tentatively, permanently remove the
10352 template argument list. That will prevent duplicate
10353 error messages from being issued about the missing
10354 "template" keyword. */
10356 cp_lexer_purge_tokens_after (parser->lexer, start);
10358 *is_identifier = true;
10362 /* If the "template" keyword is present, then there is generally
10363 no point in doing name-lookup, so we just return IDENTIFIER.
10364 But, if the qualifying scope is non-dependent then we can
10365 (and must) do name-lookup normally. */
10366 if (template_keyword_p
10368 || (TYPE_P (parser->scope)
10369 && dependent_type_p (parser->scope))))
10373 /* Look up the name. */
10374 decl = cp_parser_lookup_name (parser, identifier,
10376 /*is_template=*/false,
10377 /*is_namespace=*/false,
10378 check_dependency_p,
10379 /*ambiguous_decls=*/NULL,
10381 decl = maybe_get_template_decl_from_type_decl (decl);
10383 /* If DECL is a template, then the name was a template-name. */
10384 if (TREE_CODE (decl) == TEMPLATE_DECL)
10388 tree fn = NULL_TREE;
10390 /* The standard does not explicitly indicate whether a name that
10391 names a set of overloaded declarations, some of which are
10392 templates, is a template-name. However, such a name should
10393 be a template-name; otherwise, there is no way to form a
10394 template-id for the overloaded templates. */
10395 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10396 if (TREE_CODE (fns) == OVERLOAD)
10397 for (fn = fns; fn; fn = OVL_NEXT (fn))
10398 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10403 /* The name does not name a template. */
10404 cp_parser_error (parser, "expected template-name");
10405 return error_mark_node;
10409 /* If DECL is dependent, and refers to a function, then just return
10410 its name; we will look it up again during template instantiation. */
10411 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10413 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10414 if (TYPE_P (scope) && dependent_type_p (scope))
10421 /* Parse a template-argument-list.
10423 template-argument-list:
10424 template-argument ... [opt]
10425 template-argument-list , template-argument ... [opt]
10427 Returns a TREE_VEC containing the arguments. */
10430 cp_parser_template_argument_list (cp_parser* parser)
10432 tree fixed_args[10];
10433 unsigned n_args = 0;
10434 unsigned alloced = 10;
10435 tree *arg_ary = fixed_args;
10437 bool saved_in_template_argument_list_p;
10439 bool saved_non_ice_p;
10441 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10442 parser->in_template_argument_list_p = true;
10443 /* Even if the template-id appears in an integral
10444 constant-expression, the contents of the argument list do
10446 saved_ice_p = parser->integral_constant_expression_p;
10447 parser->integral_constant_expression_p = false;
10448 saved_non_ice_p = parser->non_integral_constant_expression_p;
10449 parser->non_integral_constant_expression_p = false;
10450 /* Parse the arguments. */
10456 /* Consume the comma. */
10457 cp_lexer_consume_token (parser->lexer);
10459 /* Parse the template-argument. */
10460 argument = cp_parser_template_argument (parser);
10462 /* If the next token is an ellipsis, we're expanding a template
10464 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10466 /* Consume the `...' token. */
10467 cp_lexer_consume_token (parser->lexer);
10469 /* Make the argument into a TYPE_PACK_EXPANSION or
10470 EXPR_PACK_EXPANSION. */
10471 argument = make_pack_expansion (argument);
10474 if (n_args == alloced)
10478 if (arg_ary == fixed_args)
10480 arg_ary = XNEWVEC (tree, alloced);
10481 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10484 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10486 arg_ary[n_args++] = argument;
10488 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10490 vec = make_tree_vec (n_args);
10493 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10495 if (arg_ary != fixed_args)
10497 parser->non_integral_constant_expression_p = saved_non_ice_p;
10498 parser->integral_constant_expression_p = saved_ice_p;
10499 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10503 /* Parse a template-argument.
10506 assignment-expression
10510 The representation is that of an assignment-expression, type-id, or
10511 id-expression -- except that the qualified id-expression is
10512 evaluated, so that the value returned is either a DECL or an
10515 Although the standard says "assignment-expression", it forbids
10516 throw-expressions or assignments in the template argument.
10517 Therefore, we use "conditional-expression" instead. */
10520 cp_parser_template_argument (cp_parser* parser)
10525 bool maybe_type_id = false;
10526 cp_token *token = NULL, *argument_start_token = NULL;
10529 /* There's really no way to know what we're looking at, so we just
10530 try each alternative in order.
10534 In a template-argument, an ambiguity between a type-id and an
10535 expression is resolved to a type-id, regardless of the form of
10536 the corresponding template-parameter.
10538 Therefore, we try a type-id first. */
10539 cp_parser_parse_tentatively (parser);
10540 argument = cp_parser_type_id (parser);
10541 /* If there was no error parsing the type-id but the next token is a
10542 '>>', our behavior depends on which dialect of C++ we're
10543 parsing. In C++98, we probably found a typo for '> >'. But there
10544 are type-id which are also valid expressions. For instance:
10546 struct X { int operator >> (int); };
10547 template <int V> struct Foo {};
10550 Here 'X()' is a valid type-id of a function type, but the user just
10551 wanted to write the expression "X() >> 5". Thus, we remember that we
10552 found a valid type-id, but we still try to parse the argument as an
10553 expression to see what happens.
10555 In C++0x, the '>>' will be considered two separate '>'
10557 if (!cp_parser_error_occurred (parser)
10558 && cxx_dialect == cxx98
10559 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10561 maybe_type_id = true;
10562 cp_parser_abort_tentative_parse (parser);
10566 /* If the next token isn't a `,' or a `>', then this argument wasn't
10567 really finished. This means that the argument is not a valid
10569 if (!cp_parser_next_token_ends_template_argument_p (parser))
10570 cp_parser_error (parser, "expected template-argument");
10571 /* If that worked, we're done. */
10572 if (cp_parser_parse_definitely (parser))
10575 /* We're still not sure what the argument will be. */
10576 cp_parser_parse_tentatively (parser);
10577 /* Try a template. */
10578 argument_start_token = cp_lexer_peek_token (parser->lexer);
10579 argument = cp_parser_id_expression (parser,
10580 /*template_keyword_p=*/false,
10581 /*check_dependency_p=*/true,
10583 /*declarator_p=*/false,
10584 /*optional_p=*/false);
10585 /* If the next token isn't a `,' or a `>', then this argument wasn't
10586 really finished. */
10587 if (!cp_parser_next_token_ends_template_argument_p (parser))
10588 cp_parser_error (parser, "expected template-argument");
10589 if (!cp_parser_error_occurred (parser))
10591 /* Figure out what is being referred to. If the id-expression
10592 was for a class template specialization, then we will have a
10593 TYPE_DECL at this point. There is no need to do name lookup
10594 at this point in that case. */
10595 if (TREE_CODE (argument) != TYPE_DECL)
10596 argument = cp_parser_lookup_name (parser, argument,
10598 /*is_template=*/template_p,
10599 /*is_namespace=*/false,
10600 /*check_dependency=*/true,
10601 /*ambiguous_decls=*/NULL,
10602 argument_start_token->location);
10603 if (TREE_CODE (argument) != TEMPLATE_DECL
10604 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10605 cp_parser_error (parser, "expected template-name");
10607 if (cp_parser_parse_definitely (parser))
10609 /* It must be a non-type argument. There permitted cases are given
10610 in [temp.arg.nontype]:
10612 -- an integral constant-expression of integral or enumeration
10615 -- the name of a non-type template-parameter; or
10617 -- the name of an object or function with external linkage...
10619 -- the address of an object or function with external linkage...
10621 -- a pointer to member... */
10622 /* Look for a non-type template parameter. */
10623 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10625 cp_parser_parse_tentatively (parser);
10626 argument = cp_parser_primary_expression (parser,
10627 /*address_p=*/false,
10629 /*template_arg_p=*/true,
10631 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10632 || !cp_parser_next_token_ends_template_argument_p (parser))
10633 cp_parser_simulate_error (parser);
10634 if (cp_parser_parse_definitely (parser))
10638 /* If the next token is "&", the argument must be the address of an
10639 object or function with external linkage. */
10640 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10642 cp_lexer_consume_token (parser->lexer);
10643 /* See if we might have an id-expression. */
10644 token = cp_lexer_peek_token (parser->lexer);
10645 if (token->type == CPP_NAME
10646 || token->keyword == RID_OPERATOR
10647 || token->type == CPP_SCOPE
10648 || token->type == CPP_TEMPLATE_ID
10649 || token->type == CPP_NESTED_NAME_SPECIFIER)
10651 cp_parser_parse_tentatively (parser);
10652 argument = cp_parser_primary_expression (parser,
10655 /*template_arg_p=*/true,
10657 if (cp_parser_error_occurred (parser)
10658 || !cp_parser_next_token_ends_template_argument_p (parser))
10659 cp_parser_abort_tentative_parse (parser);
10662 if (TREE_CODE (argument) == INDIRECT_REF)
10664 gcc_assert (REFERENCE_REF_P (argument));
10665 argument = TREE_OPERAND (argument, 0);
10668 if (TREE_CODE (argument) == VAR_DECL)
10670 /* A variable without external linkage might still be a
10671 valid constant-expression, so no error is issued here
10672 if the external-linkage check fails. */
10673 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10674 cp_parser_simulate_error (parser);
10676 else if (is_overloaded_fn (argument))
10677 /* All overloaded functions are allowed; if the external
10678 linkage test does not pass, an error will be issued
10682 && (TREE_CODE (argument) == OFFSET_REF
10683 || TREE_CODE (argument) == SCOPE_REF))
10684 /* A pointer-to-member. */
10686 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10689 cp_parser_simulate_error (parser);
10691 if (cp_parser_parse_definitely (parser))
10694 argument = build_x_unary_op (ADDR_EXPR, argument,
10695 tf_warning_or_error);
10700 /* If the argument started with "&", there are no other valid
10701 alternatives at this point. */
10704 cp_parser_error (parser, "invalid non-type template argument");
10705 return error_mark_node;
10708 /* If the argument wasn't successfully parsed as a type-id followed
10709 by '>>', the argument can only be a constant expression now.
10710 Otherwise, we try parsing the constant-expression tentatively,
10711 because the argument could really be a type-id. */
10713 cp_parser_parse_tentatively (parser);
10714 argument = cp_parser_constant_expression (parser,
10715 /*allow_non_constant_p=*/false,
10716 /*non_constant_p=*/NULL);
10717 argument = fold_non_dependent_expr (argument);
10718 if (!maybe_type_id)
10720 if (!cp_parser_next_token_ends_template_argument_p (parser))
10721 cp_parser_error (parser, "expected template-argument");
10722 if (cp_parser_parse_definitely (parser))
10724 /* We did our best to parse the argument as a non type-id, but that
10725 was the only alternative that matched (albeit with a '>' after
10726 it). We can assume it's just a typo from the user, and a
10727 diagnostic will then be issued. */
10728 return cp_parser_type_id (parser);
10731 /* Parse an explicit-instantiation.
10733 explicit-instantiation:
10734 template declaration
10736 Although the standard says `declaration', what it really means is:
10738 explicit-instantiation:
10739 template decl-specifier-seq [opt] declarator [opt] ;
10741 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10742 supposed to be allowed. A defect report has been filed about this
10747 explicit-instantiation:
10748 storage-class-specifier template
10749 decl-specifier-seq [opt] declarator [opt] ;
10750 function-specifier template
10751 decl-specifier-seq [opt] declarator [opt] ; */
10754 cp_parser_explicit_instantiation (cp_parser* parser)
10756 int declares_class_or_enum;
10757 cp_decl_specifier_seq decl_specifiers;
10758 tree extension_specifier = NULL_TREE;
10761 /* Look for an (optional) storage-class-specifier or
10762 function-specifier. */
10763 if (cp_parser_allow_gnu_extensions_p (parser))
10765 extension_specifier
10766 = cp_parser_storage_class_specifier_opt (parser);
10767 if (!extension_specifier)
10768 extension_specifier
10769 = cp_parser_function_specifier_opt (parser,
10770 /*decl_specs=*/NULL);
10773 /* Look for the `template' keyword. */
10774 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10775 /* Let the front end know that we are processing an explicit
10777 begin_explicit_instantiation ();
10778 /* [temp.explicit] says that we are supposed to ignore access
10779 control while processing explicit instantiation directives. */
10780 push_deferring_access_checks (dk_no_check);
10781 /* Parse a decl-specifier-seq. */
10782 token = cp_lexer_peek_token (parser->lexer);
10783 cp_parser_decl_specifier_seq (parser,
10784 CP_PARSER_FLAGS_OPTIONAL,
10786 &declares_class_or_enum);
10787 /* If there was exactly one decl-specifier, and it declared a class,
10788 and there's no declarator, then we have an explicit type
10790 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10794 type = check_tag_decl (&decl_specifiers);
10795 /* Turn access control back on for names used during
10796 template instantiation. */
10797 pop_deferring_access_checks ();
10799 do_type_instantiation (type, extension_specifier,
10800 /*complain=*/tf_error);
10804 cp_declarator *declarator;
10807 /* Parse the declarator. */
10809 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10810 /*ctor_dtor_or_conv_p=*/NULL,
10811 /*parenthesized_p=*/NULL,
10812 /*member_p=*/false);
10813 if (declares_class_or_enum & 2)
10814 cp_parser_check_for_definition_in_return_type (declarator,
10815 decl_specifiers.type,
10816 decl_specifiers.type_location);
10817 if (declarator != cp_error_declarator)
10819 decl = grokdeclarator (declarator, &decl_specifiers,
10820 NORMAL, 0, &decl_specifiers.attributes);
10821 /* Turn access control back on for names used during
10822 template instantiation. */
10823 pop_deferring_access_checks ();
10824 /* Do the explicit instantiation. */
10825 do_decl_instantiation (decl, extension_specifier);
10829 pop_deferring_access_checks ();
10830 /* Skip the body of the explicit instantiation. */
10831 cp_parser_skip_to_end_of_statement (parser);
10834 /* We're done with the instantiation. */
10835 end_explicit_instantiation ();
10837 cp_parser_consume_semicolon_at_end_of_statement (parser);
10840 /* Parse an explicit-specialization.
10842 explicit-specialization:
10843 template < > declaration
10845 Although the standard says `declaration', what it really means is:
10847 explicit-specialization:
10848 template <> decl-specifier [opt] init-declarator [opt] ;
10849 template <> function-definition
10850 template <> explicit-specialization
10851 template <> template-declaration */
10854 cp_parser_explicit_specialization (cp_parser* parser)
10856 bool need_lang_pop;
10857 cp_token *token = cp_lexer_peek_token (parser->lexer);
10859 /* Look for the `template' keyword. */
10860 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10861 /* Look for the `<'. */
10862 cp_parser_require (parser, CPP_LESS, "%<<%>");
10863 /* Look for the `>'. */
10864 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10865 /* We have processed another parameter list. */
10866 ++parser->num_template_parameter_lists;
10869 A template ... explicit specialization ... shall not have C
10871 if (current_lang_name == lang_name_c)
10873 error ("%Htemplate specialization with C linkage", &token->location);
10874 /* Give it C++ linkage to avoid confusing other parts of the
10876 push_lang_context (lang_name_cplusplus);
10877 need_lang_pop = true;
10880 need_lang_pop = false;
10881 /* Let the front end know that we are beginning a specialization. */
10882 if (!begin_specialization ())
10884 end_specialization ();
10885 cp_parser_skip_to_end_of_block_or_statement (parser);
10889 /* If the next keyword is `template', we need to figure out whether
10890 or not we're looking a template-declaration. */
10891 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10893 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10894 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10895 cp_parser_template_declaration_after_export (parser,
10896 /*member_p=*/false);
10898 cp_parser_explicit_specialization (parser);
10901 /* Parse the dependent declaration. */
10902 cp_parser_single_declaration (parser,
10904 /*member_p=*/false,
10905 /*explicit_specialization_p=*/true,
10906 /*friend_p=*/NULL);
10907 /* We're done with the specialization. */
10908 end_specialization ();
10909 /* For the erroneous case of a template with C linkage, we pushed an
10910 implicit C++ linkage scope; exit that scope now. */
10912 pop_lang_context ();
10913 /* We're done with this parameter list. */
10914 --parser->num_template_parameter_lists;
10917 /* Parse a type-specifier.
10920 simple-type-specifier
10923 elaborated-type-specifier
10931 Returns a representation of the type-specifier. For a
10932 class-specifier, enum-specifier, or elaborated-type-specifier, a
10933 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10935 The parser flags FLAGS is used to control type-specifier parsing.
10937 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10938 in a decl-specifier-seq.
10940 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10941 class-specifier, enum-specifier, or elaborated-type-specifier, then
10942 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10943 if a type is declared; 2 if it is defined. Otherwise, it is set to
10946 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10947 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10948 is set to FALSE. */
10951 cp_parser_type_specifier (cp_parser* parser,
10952 cp_parser_flags flags,
10953 cp_decl_specifier_seq *decl_specs,
10954 bool is_declaration,
10955 int* declares_class_or_enum,
10956 bool* is_cv_qualifier)
10958 tree type_spec = NULL_TREE;
10961 cp_decl_spec ds = ds_last;
10963 /* Assume this type-specifier does not declare a new type. */
10964 if (declares_class_or_enum)
10965 *declares_class_or_enum = 0;
10966 /* And that it does not specify a cv-qualifier. */
10967 if (is_cv_qualifier)
10968 *is_cv_qualifier = false;
10969 /* Peek at the next token. */
10970 token = cp_lexer_peek_token (parser->lexer);
10972 /* If we're looking at a keyword, we can use that to guide the
10973 production we choose. */
10974 keyword = token->keyword;
10978 /* Look for the enum-specifier. */
10979 type_spec = cp_parser_enum_specifier (parser);
10980 /* If that worked, we're done. */
10983 if (declares_class_or_enum)
10984 *declares_class_or_enum = 2;
10986 cp_parser_set_decl_spec_type (decl_specs,
10989 /*user_defined_p=*/true);
10993 goto elaborated_type_specifier;
10995 /* Any of these indicate either a class-specifier, or an
10996 elaborated-type-specifier. */
11000 /* Parse tentatively so that we can back up if we don't find a
11001 class-specifier. */
11002 cp_parser_parse_tentatively (parser);
11003 /* Look for the class-specifier. */
11004 type_spec = cp_parser_class_specifier (parser);
11005 /* If that worked, we're done. */
11006 if (cp_parser_parse_definitely (parser))
11008 if (declares_class_or_enum)
11009 *declares_class_or_enum = 2;
11011 cp_parser_set_decl_spec_type (decl_specs,
11014 /*user_defined_p=*/true);
11018 /* Fall through. */
11019 elaborated_type_specifier:
11020 /* We're declaring (not defining) a class or enum. */
11021 if (declares_class_or_enum)
11022 *declares_class_or_enum = 1;
11024 /* Fall through. */
11026 /* Look for an elaborated-type-specifier. */
11028 = (cp_parser_elaborated_type_specifier
11030 decl_specs && decl_specs->specs[(int) ds_friend],
11033 cp_parser_set_decl_spec_type (decl_specs,
11036 /*user_defined_p=*/true);
11041 if (is_cv_qualifier)
11042 *is_cv_qualifier = true;
11047 if (is_cv_qualifier)
11048 *is_cv_qualifier = true;
11053 if (is_cv_qualifier)
11054 *is_cv_qualifier = true;
11058 /* The `__complex__' keyword is a GNU extension. */
11066 /* Handle simple keywords. */
11071 ++decl_specs->specs[(int)ds];
11072 decl_specs->any_specifiers_p = true;
11074 return cp_lexer_consume_token (parser->lexer)->u.value;
11077 /* If we do not already have a type-specifier, assume we are looking
11078 at a simple-type-specifier. */
11079 type_spec = cp_parser_simple_type_specifier (parser,
11083 /* If we didn't find a type-specifier, and a type-specifier was not
11084 optional in this context, issue an error message. */
11085 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11087 cp_parser_error (parser, "expected type specifier");
11088 return error_mark_node;
11094 /* Parse a simple-type-specifier.
11096 simple-type-specifier:
11097 :: [opt] nested-name-specifier [opt] type-name
11098 :: [opt] nested-name-specifier template template-id
11113 simple-type-specifier:
11115 decltype ( expression )
11121 simple-type-specifier:
11122 __typeof__ unary-expression
11123 __typeof__ ( type-id )
11125 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11126 appropriately updated. */
11129 cp_parser_simple_type_specifier (cp_parser* parser,
11130 cp_decl_specifier_seq *decl_specs,
11131 cp_parser_flags flags)
11133 tree type = NULL_TREE;
11136 /* Peek at the next token. */
11137 token = cp_lexer_peek_token (parser->lexer);
11139 /* If we're looking at a keyword, things are easy. */
11140 switch (token->keyword)
11144 decl_specs->explicit_char_p = true;
11145 type = char_type_node;
11148 type = char16_type_node;
11151 type = char32_type_node;
11154 type = wchar_type_node;
11157 type = boolean_type_node;
11161 ++decl_specs->specs[(int) ds_short];
11162 type = short_integer_type_node;
11166 decl_specs->explicit_int_p = true;
11167 type = integer_type_node;
11171 ++decl_specs->specs[(int) ds_long];
11172 type = long_integer_type_node;
11176 ++decl_specs->specs[(int) ds_signed];
11177 type = integer_type_node;
11181 ++decl_specs->specs[(int) ds_unsigned];
11182 type = unsigned_type_node;
11185 type = float_type_node;
11188 type = double_type_node;
11191 type = void_type_node;
11195 maybe_warn_cpp0x ("C++0x auto");
11196 type = make_auto ();
11200 /* Parse the `decltype' type. */
11201 type = cp_parser_decltype (parser);
11204 cp_parser_set_decl_spec_type (decl_specs, type,
11206 /*user_defined_p=*/true);
11211 /* Consume the `typeof' token. */
11212 cp_lexer_consume_token (parser->lexer);
11213 /* Parse the operand to `typeof'. */
11214 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11215 /* If it is not already a TYPE, take its type. */
11216 if (!TYPE_P (type))
11217 type = finish_typeof (type);
11220 cp_parser_set_decl_spec_type (decl_specs, type,
11222 /*user_defined_p=*/true);
11230 /* If the type-specifier was for a built-in type, we're done. */
11235 /* Record the type. */
11237 && (token->keyword != RID_SIGNED
11238 && token->keyword != RID_UNSIGNED
11239 && token->keyword != RID_SHORT
11240 && token->keyword != RID_LONG))
11241 cp_parser_set_decl_spec_type (decl_specs,
11244 /*user_defined=*/false);
11246 decl_specs->any_specifiers_p = true;
11248 /* Consume the token. */
11249 id = cp_lexer_consume_token (parser->lexer)->u.value;
11251 /* There is no valid C++ program where a non-template type is
11252 followed by a "<". That usually indicates that the user thought
11253 that the type was a template. */
11254 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11256 return TYPE_NAME (type);
11259 /* The type-specifier must be a user-defined type. */
11260 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11265 /* Don't gobble tokens or issue error messages if this is an
11266 optional type-specifier. */
11267 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11268 cp_parser_parse_tentatively (parser);
11270 /* Look for the optional `::' operator. */
11272 = (cp_parser_global_scope_opt (parser,
11273 /*current_scope_valid_p=*/false)
11275 /* Look for the nested-name specifier. */
11277 = (cp_parser_nested_name_specifier_opt (parser,
11278 /*typename_keyword_p=*/false,
11279 /*check_dependency_p=*/true,
11281 /*is_declaration=*/false)
11283 token = cp_lexer_peek_token (parser->lexer);
11284 /* If we have seen a nested-name-specifier, and the next token
11285 is `template', then we are using the template-id production. */
11287 && cp_parser_optional_template_keyword (parser))
11289 /* Look for the template-id. */
11290 type = cp_parser_template_id (parser,
11291 /*template_keyword_p=*/true,
11292 /*check_dependency_p=*/true,
11293 /*is_declaration=*/false);
11294 /* If the template-id did not name a type, we are out of
11296 if (TREE_CODE (type) != TYPE_DECL)
11298 cp_parser_error (parser, "expected template-id for type");
11302 /* Otherwise, look for a type-name. */
11304 type = cp_parser_type_name (parser);
11305 /* Keep track of all name-lookups performed in class scopes. */
11309 && TREE_CODE (type) == TYPE_DECL
11310 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11311 maybe_note_name_used_in_class (DECL_NAME (type), type);
11312 /* If it didn't work out, we don't have a TYPE. */
11313 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11314 && !cp_parser_parse_definitely (parser))
11316 if (type && decl_specs)
11317 cp_parser_set_decl_spec_type (decl_specs, type,
11319 /*user_defined=*/true);
11322 /* If we didn't get a type-name, issue an error message. */
11323 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11325 cp_parser_error (parser, "expected type-name");
11326 return error_mark_node;
11329 /* There is no valid C++ program where a non-template type is
11330 followed by a "<". That usually indicates that the user thought
11331 that the type was a template. */
11332 if (type && type != error_mark_node)
11334 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11335 If it is, then the '<'...'>' enclose protocol names rather than
11336 template arguments, and so everything is fine. */
11337 if (c_dialect_objc ()
11338 && (objc_is_id (type) || objc_is_class_name (type)))
11340 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11341 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11343 /* Clobber the "unqualified" type previously entered into
11344 DECL_SPECS with the new, improved protocol-qualified version. */
11346 decl_specs->type = qual_type;
11351 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11358 /* Parse a type-name.
11371 Returns a TYPE_DECL for the type. */
11374 cp_parser_type_name (cp_parser* parser)
11378 /* We can't know yet whether it is a class-name or not. */
11379 cp_parser_parse_tentatively (parser);
11380 /* Try a class-name. */
11381 type_decl = cp_parser_class_name (parser,
11382 /*typename_keyword_p=*/false,
11383 /*template_keyword_p=*/false,
11385 /*check_dependency_p=*/true,
11386 /*class_head_p=*/false,
11387 /*is_declaration=*/false);
11388 /* If it's not a class-name, keep looking. */
11389 if (!cp_parser_parse_definitely (parser))
11391 /* It must be a typedef-name or an enum-name. */
11392 return cp_parser_nonclass_name (parser);
11398 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11406 Returns a TYPE_DECL for the type. */
11409 cp_parser_nonclass_name (cp_parser* parser)
11414 cp_token *token = cp_lexer_peek_token (parser->lexer);
11415 identifier = cp_parser_identifier (parser);
11416 if (identifier == error_mark_node)
11417 return error_mark_node;
11419 /* Look up the type-name. */
11420 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11422 if (TREE_CODE (type_decl) != TYPE_DECL
11423 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11425 /* See if this is an Objective-C type. */
11426 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11427 tree type = objc_get_protocol_qualified_type (identifier, protos);
11429 type_decl = TYPE_NAME (type);
11432 /* Issue an error if we did not find a type-name. */
11433 if (TREE_CODE (type_decl) != TYPE_DECL)
11435 if (!cp_parser_simulate_error (parser))
11436 cp_parser_name_lookup_error (parser, identifier, type_decl,
11437 "is not a type", token->location);
11438 return error_mark_node;
11440 /* Remember that the name was used in the definition of the
11441 current class so that we can check later to see if the
11442 meaning would have been different after the class was
11443 entirely defined. */
11444 else if (type_decl != error_mark_node
11446 maybe_note_name_used_in_class (identifier, type_decl);
11451 /* Parse an elaborated-type-specifier. Note that the grammar given
11452 here incorporates the resolution to DR68.
11454 elaborated-type-specifier:
11455 class-key :: [opt] nested-name-specifier [opt] identifier
11456 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11457 enum-key :: [opt] nested-name-specifier [opt] identifier
11458 typename :: [opt] nested-name-specifier identifier
11459 typename :: [opt] nested-name-specifier template [opt]
11464 elaborated-type-specifier:
11465 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11466 class-key attributes :: [opt] nested-name-specifier [opt]
11467 template [opt] template-id
11468 enum attributes :: [opt] nested-name-specifier [opt] identifier
11470 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11471 declared `friend'. If IS_DECLARATION is TRUE, then this
11472 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11473 something is being declared.
11475 Returns the TYPE specified. */
11478 cp_parser_elaborated_type_specifier (cp_parser* parser,
11480 bool is_declaration)
11482 enum tag_types tag_type;
11484 tree type = NULL_TREE;
11485 tree attributes = NULL_TREE;
11486 cp_token *token = NULL;
11488 /* See if we're looking at the `enum' keyword. */
11489 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11491 /* Consume the `enum' token. */
11492 cp_lexer_consume_token (parser->lexer);
11493 /* Remember that it's an enumeration type. */
11494 tag_type = enum_type;
11495 /* Parse the optional `struct' or `class' key (for C++0x scoped
11497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11498 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11500 if (cxx_dialect == cxx98)
11501 maybe_warn_cpp0x ("scoped enums");
11503 /* Consume the `struct' or `class'. */
11504 cp_lexer_consume_token (parser->lexer);
11506 /* Parse the attributes. */
11507 attributes = cp_parser_attributes_opt (parser);
11509 /* Or, it might be `typename'. */
11510 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11513 /* Consume the `typename' token. */
11514 cp_lexer_consume_token (parser->lexer);
11515 /* Remember that it's a `typename' type. */
11516 tag_type = typename_type;
11517 /* The `typename' keyword is only allowed in templates. */
11518 if (!processing_template_decl)
11519 permerror (input_location, "using %<typename%> outside of template");
11521 /* Otherwise it must be a class-key. */
11524 tag_type = cp_parser_class_key (parser);
11525 if (tag_type == none_type)
11526 return error_mark_node;
11527 /* Parse the attributes. */
11528 attributes = cp_parser_attributes_opt (parser);
11531 /* Look for the `::' operator. */
11532 cp_parser_global_scope_opt (parser,
11533 /*current_scope_valid_p=*/false);
11534 /* Look for the nested-name-specifier. */
11535 if (tag_type == typename_type)
11537 if (!cp_parser_nested_name_specifier (parser,
11538 /*typename_keyword_p=*/true,
11539 /*check_dependency_p=*/true,
11542 return error_mark_node;
11545 /* Even though `typename' is not present, the proposed resolution
11546 to Core Issue 180 says that in `class A<T>::B', `B' should be
11547 considered a type-name, even if `A<T>' is dependent. */
11548 cp_parser_nested_name_specifier_opt (parser,
11549 /*typename_keyword_p=*/true,
11550 /*check_dependency_p=*/true,
11553 /* For everything but enumeration types, consider a template-id.
11554 For an enumeration type, consider only a plain identifier. */
11555 if (tag_type != enum_type)
11557 bool template_p = false;
11560 /* Allow the `template' keyword. */
11561 template_p = cp_parser_optional_template_keyword (parser);
11562 /* If we didn't see `template', we don't know if there's a
11563 template-id or not. */
11565 cp_parser_parse_tentatively (parser);
11566 /* Parse the template-id. */
11567 token = cp_lexer_peek_token (parser->lexer);
11568 decl = cp_parser_template_id (parser, template_p,
11569 /*check_dependency_p=*/true,
11571 /* If we didn't find a template-id, look for an ordinary
11573 if (!template_p && !cp_parser_parse_definitely (parser))
11575 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11576 in effect, then we must assume that, upon instantiation, the
11577 template will correspond to a class. */
11578 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11579 && tag_type == typename_type)
11580 type = make_typename_type (parser->scope, decl,
11582 /*complain=*/tf_error);
11584 type = TREE_TYPE (decl);
11589 token = cp_lexer_peek_token (parser->lexer);
11590 identifier = cp_parser_identifier (parser);
11592 if (identifier == error_mark_node)
11594 parser->scope = NULL_TREE;
11595 return error_mark_node;
11598 /* For a `typename', we needn't call xref_tag. */
11599 if (tag_type == typename_type
11600 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11601 return cp_parser_make_typename_type (parser, parser->scope,
11604 /* Look up a qualified name in the usual way. */
11608 tree ambiguous_decls;
11610 decl = cp_parser_lookup_name (parser, identifier,
11612 /*is_template=*/false,
11613 /*is_namespace=*/false,
11614 /*check_dependency=*/true,
11618 /* If the lookup was ambiguous, an error will already have been
11620 if (ambiguous_decls)
11621 return error_mark_node;
11623 /* If we are parsing friend declaration, DECL may be a
11624 TEMPLATE_DECL tree node here. However, we need to check
11625 whether this TEMPLATE_DECL results in valid code. Consider
11626 the following example:
11629 template <class T> class C {};
11632 template <class T> friend class N::C; // #1, valid code
11634 template <class T> class Y {
11635 friend class N::C; // #2, invalid code
11638 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11639 name lookup of `N::C'. We see that friend declaration must
11640 be template for the code to be valid. Note that
11641 processing_template_decl does not work here since it is
11642 always 1 for the above two cases. */
11644 decl = (cp_parser_maybe_treat_template_as_class
11645 (decl, /*tag_name_p=*/is_friend
11646 && parser->num_template_parameter_lists));
11648 if (TREE_CODE (decl) != TYPE_DECL)
11650 cp_parser_diagnose_invalid_type_name (parser,
11654 return error_mark_node;
11657 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11659 bool allow_template = (parser->num_template_parameter_lists
11660 || DECL_SELF_REFERENCE_P (decl));
11661 type = check_elaborated_type_specifier (tag_type, decl,
11664 if (type == error_mark_node)
11665 return error_mark_node;
11668 /* Forward declarations of nested types, such as
11673 are invalid unless all components preceding the final '::'
11674 are complete. If all enclosing types are complete, these
11675 declarations become merely pointless.
11677 Invalid forward declarations of nested types are errors
11678 caught elsewhere in parsing. Those that are pointless arrive
11681 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11682 && !is_friend && !processing_explicit_instantiation)
11683 warning (0, "declaration %qD does not declare anything", decl);
11685 type = TREE_TYPE (decl);
11689 /* An elaborated-type-specifier sometimes introduces a new type and
11690 sometimes names an existing type. Normally, the rule is that it
11691 introduces a new type only if there is not an existing type of
11692 the same name already in scope. For example, given:
11695 void f() { struct S s; }
11697 the `struct S' in the body of `f' is the same `struct S' as in
11698 the global scope; the existing definition is used. However, if
11699 there were no global declaration, this would introduce a new
11700 local class named `S'.
11702 An exception to this rule applies to the following code:
11704 namespace N { struct S; }
11706 Here, the elaborated-type-specifier names a new type
11707 unconditionally; even if there is already an `S' in the
11708 containing scope this declaration names a new type.
11709 This exception only applies if the elaborated-type-specifier
11710 forms the complete declaration:
11714 A declaration consisting solely of `class-key identifier ;' is
11715 either a redeclaration of the name in the current scope or a
11716 forward declaration of the identifier as a class name. It
11717 introduces the name into the current scope.
11719 We are in this situation precisely when the next token is a `;'.
11721 An exception to the exception is that a `friend' declaration does
11722 *not* name a new type; i.e., given:
11724 struct S { friend struct T; };
11726 `T' is not a new type in the scope of `S'.
11728 Also, `new struct S' or `sizeof (struct S)' never results in the
11729 definition of a new type; a new type can only be declared in a
11730 declaration context. */
11736 /* Friends have special name lookup rules. */
11737 ts = ts_within_enclosing_non_class;
11738 else if (is_declaration
11739 && cp_lexer_next_token_is (parser->lexer,
11741 /* This is a `class-key identifier ;' */
11747 (parser->num_template_parameter_lists
11748 && (cp_parser_next_token_starts_class_definition_p (parser)
11749 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11750 /* An unqualified name was used to reference this type, so
11751 there were no qualifying templates. */
11752 if (!cp_parser_check_template_parameters (parser,
11753 /*num_templates=*/0,
11755 return error_mark_node;
11756 type = xref_tag (tag_type, identifier, ts, template_p);
11760 if (type == error_mark_node)
11761 return error_mark_node;
11763 /* Allow attributes on forward declarations of classes. */
11766 if (TREE_CODE (type) == TYPENAME_TYPE)
11767 warning (OPT_Wattributes,
11768 "attributes ignored on uninstantiated type");
11769 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11770 && ! processing_explicit_instantiation)
11771 warning (OPT_Wattributes,
11772 "attributes ignored on template instantiation");
11773 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11774 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11776 warning (OPT_Wattributes,
11777 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11780 if (tag_type != enum_type)
11781 cp_parser_check_class_key (tag_type, type);
11783 /* A "<" cannot follow an elaborated type specifier. If that
11784 happens, the user was probably trying to form a template-id. */
11785 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11790 /* Parse an enum-specifier.
11793 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11798 enum struct [C++0x]
11801 : type-specifier-seq
11804 enum-key attributes[opt] identifier [opt] enum-base [opt]
11805 { enumerator-list [opt] }attributes[opt]
11807 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11808 if the token stream isn't an enum-specifier after all. */
11811 cp_parser_enum_specifier (cp_parser* parser)
11816 bool scoped_enum_p = false;
11817 bool has_underlying_type = false;
11818 tree underlying_type = NULL_TREE;
11820 /* Parse tentatively so that we can back up if we don't find a
11822 cp_parser_parse_tentatively (parser);
11824 /* Caller guarantees that the current token is 'enum', an identifier
11825 possibly follows, and the token after that is an opening brace.
11826 If we don't have an identifier, fabricate an anonymous name for
11827 the enumeration being defined. */
11828 cp_lexer_consume_token (parser->lexer);
11830 /* Parse the "class" or "struct", which indicates a scoped
11831 enumeration type in C++0x. */
11832 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11833 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11835 if (cxx_dialect == cxx98)
11836 maybe_warn_cpp0x ("scoped enums");
11838 /* Consume the `struct' or `class' token. */
11839 cp_lexer_consume_token (parser->lexer);
11841 scoped_enum_p = true;
11844 attributes = cp_parser_attributes_opt (parser);
11846 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11847 identifier = cp_parser_identifier (parser);
11849 identifier = make_anon_name ();
11851 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11852 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11854 cp_decl_specifier_seq type_specifiers;
11856 /* At this point this is surely not elaborated type specifier. */
11857 if (!cp_parser_parse_definitely (parser))
11860 if (cxx_dialect == cxx98)
11861 maybe_warn_cpp0x ("scoped enums");
11863 /* Consume the `:'. */
11864 cp_lexer_consume_token (parser->lexer);
11866 has_underlying_type = true;
11868 /* Parse the type-specifier-seq. */
11869 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11872 /* If that didn't work, stop. */
11873 if (type_specifiers.type != error_mark_node)
11875 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11876 /*initialized=*/0, NULL);
11877 if (underlying_type == error_mark_node)
11878 underlying_type = NULL_TREE;
11882 /* Look for the `{' but don't consume it yet. */
11883 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11885 cp_parser_error (parser, "expected %<{%>");
11886 if (has_underlying_type)
11890 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11893 /* Issue an error message if type-definitions are forbidden here. */
11894 if (!cp_parser_check_type_definition (parser))
11895 type = error_mark_node;
11897 /* Create the new type. We do this before consuming the opening
11898 brace so the enum will be recorded as being on the line of its
11899 tag (or the 'enum' keyword, if there is no tag). */
11900 type = start_enum (identifier, underlying_type, scoped_enum_p);
11902 /* Consume the opening brace. */
11903 cp_lexer_consume_token (parser->lexer);
11905 if (type == error_mark_node)
11907 cp_parser_skip_to_end_of_block_or_statement (parser);
11908 return error_mark_node;
11911 /* If the next token is not '}', then there are some enumerators. */
11912 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11913 cp_parser_enumerator_list (parser, type);
11915 /* Consume the final '}'. */
11916 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11918 /* Look for trailing attributes to apply to this enumeration, and
11919 apply them if appropriate. */
11920 if (cp_parser_allow_gnu_extensions_p (parser))
11922 tree trailing_attr = cp_parser_attributes_opt (parser);
11923 trailing_attr = chainon (trailing_attr, attributes);
11924 cplus_decl_attributes (&type,
11926 (int) ATTR_FLAG_TYPE_IN_PLACE);
11929 /* Finish up the enumeration. */
11930 finish_enum (type);
11935 /* Parse an enumerator-list. The enumerators all have the indicated
11939 enumerator-definition
11940 enumerator-list , enumerator-definition */
11943 cp_parser_enumerator_list (cp_parser* parser, tree type)
11947 /* Parse an enumerator-definition. */
11948 cp_parser_enumerator_definition (parser, type);
11950 /* If the next token is not a ',', we've reached the end of
11952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11954 /* Otherwise, consume the `,' and keep going. */
11955 cp_lexer_consume_token (parser->lexer);
11956 /* If the next token is a `}', there is a trailing comma. */
11957 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11959 if (!in_system_header)
11960 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11966 /* Parse an enumerator-definition. The enumerator has the indicated
11969 enumerator-definition:
11971 enumerator = constant-expression
11977 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11982 /* Look for the identifier. */
11983 identifier = cp_parser_identifier (parser);
11984 if (identifier == error_mark_node)
11987 /* If the next token is an '=', then there is an explicit value. */
11988 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11990 /* Consume the `=' token. */
11991 cp_lexer_consume_token (parser->lexer);
11992 /* Parse the value. */
11993 value = cp_parser_constant_expression (parser,
11994 /*allow_non_constant_p=*/false,
12000 /* Create the enumerator. */
12001 build_enumerator (identifier, value, type);
12004 /* Parse a namespace-name.
12007 original-namespace-name
12010 Returns the NAMESPACE_DECL for the namespace. */
12013 cp_parser_namespace_name (cp_parser* parser)
12016 tree namespace_decl;
12018 cp_token *token = cp_lexer_peek_token (parser->lexer);
12020 /* Get the name of the namespace. */
12021 identifier = cp_parser_identifier (parser);
12022 if (identifier == error_mark_node)
12023 return error_mark_node;
12025 /* Look up the identifier in the currently active scope. Look only
12026 for namespaces, due to:
12028 [basic.lookup.udir]
12030 When looking up a namespace-name in a using-directive or alias
12031 definition, only namespace names are considered.
12035 [basic.lookup.qual]
12037 During the lookup of a name preceding the :: scope resolution
12038 operator, object, function, and enumerator names are ignored.
12040 (Note that cp_parser_qualifying_entity only calls this
12041 function if the token after the name is the scope resolution
12043 namespace_decl = cp_parser_lookup_name (parser, identifier,
12045 /*is_template=*/false,
12046 /*is_namespace=*/true,
12047 /*check_dependency=*/true,
12048 /*ambiguous_decls=*/NULL,
12050 /* If it's not a namespace, issue an error. */
12051 if (namespace_decl == error_mark_node
12052 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12054 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12055 error ("%H%qD is not a namespace-name", &token->location, identifier);
12056 cp_parser_error (parser, "expected namespace-name");
12057 namespace_decl = error_mark_node;
12060 return namespace_decl;
12063 /* Parse a namespace-definition.
12065 namespace-definition:
12066 named-namespace-definition
12067 unnamed-namespace-definition
12069 named-namespace-definition:
12070 original-namespace-definition
12071 extension-namespace-definition
12073 original-namespace-definition:
12074 namespace identifier { namespace-body }
12076 extension-namespace-definition:
12077 namespace original-namespace-name { namespace-body }
12079 unnamed-namespace-definition:
12080 namespace { namespace-body } */
12083 cp_parser_namespace_definition (cp_parser* parser)
12085 tree identifier, attribs;
12086 bool has_visibility;
12089 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12092 cp_lexer_consume_token (parser->lexer);
12097 /* Look for the `namespace' keyword. */
12098 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12100 /* Get the name of the namespace. We do not attempt to distinguish
12101 between an original-namespace-definition and an
12102 extension-namespace-definition at this point. The semantic
12103 analysis routines are responsible for that. */
12104 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12105 identifier = cp_parser_identifier (parser);
12107 identifier = NULL_TREE;
12109 /* Parse any specified attributes. */
12110 attribs = cp_parser_attributes_opt (parser);
12112 /* Look for the `{' to start the namespace. */
12113 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12114 /* Start the namespace. */
12115 push_namespace (identifier);
12117 /* "inline namespace" is equivalent to a stub namespace definition
12118 followed by a strong using directive. */
12121 tree name_space = current_namespace;
12122 /* Set up namespace association. */
12123 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12124 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12125 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12126 /* Import the contents of the inline namespace. */
12128 do_using_directive (name_space);
12129 push_namespace (identifier);
12132 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12134 /* Parse the body of the namespace. */
12135 cp_parser_namespace_body (parser);
12137 #ifdef HANDLE_PRAGMA_VISIBILITY
12138 if (has_visibility)
12142 /* Finish the namespace. */
12144 /* Look for the final `}'. */
12145 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12148 /* Parse a namespace-body.
12151 declaration-seq [opt] */
12154 cp_parser_namespace_body (cp_parser* parser)
12156 cp_parser_declaration_seq_opt (parser);
12159 /* Parse a namespace-alias-definition.
12161 namespace-alias-definition:
12162 namespace identifier = qualified-namespace-specifier ; */
12165 cp_parser_namespace_alias_definition (cp_parser* parser)
12168 tree namespace_specifier;
12170 cp_token *token = cp_lexer_peek_token (parser->lexer);
12172 /* Look for the `namespace' keyword. */
12173 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12174 /* Look for the identifier. */
12175 identifier = cp_parser_identifier (parser);
12176 if (identifier == error_mark_node)
12178 /* Look for the `=' token. */
12179 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12180 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12182 error ("%H%<namespace%> definition is not allowed here", &token->location);
12183 /* Skip the definition. */
12184 cp_lexer_consume_token (parser->lexer);
12185 if (cp_parser_skip_to_closing_brace (parser))
12186 cp_lexer_consume_token (parser->lexer);
12189 cp_parser_require (parser, CPP_EQ, "%<=%>");
12190 /* Look for the qualified-namespace-specifier. */
12191 namespace_specifier
12192 = cp_parser_qualified_namespace_specifier (parser);
12193 /* Look for the `;' token. */
12194 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12196 /* Register the alias in the symbol table. */
12197 do_namespace_alias (identifier, namespace_specifier);
12200 /* Parse a qualified-namespace-specifier.
12202 qualified-namespace-specifier:
12203 :: [opt] nested-name-specifier [opt] namespace-name
12205 Returns a NAMESPACE_DECL corresponding to the specified
12209 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12211 /* Look for the optional `::'. */
12212 cp_parser_global_scope_opt (parser,
12213 /*current_scope_valid_p=*/false);
12215 /* Look for the optional nested-name-specifier. */
12216 cp_parser_nested_name_specifier_opt (parser,
12217 /*typename_keyword_p=*/false,
12218 /*check_dependency_p=*/true,
12220 /*is_declaration=*/true);
12222 return cp_parser_namespace_name (parser);
12225 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12226 access declaration.
12229 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12230 using :: unqualified-id ;
12232 access-declaration:
12238 cp_parser_using_declaration (cp_parser* parser,
12239 bool access_declaration_p)
12242 bool typename_p = false;
12243 bool global_scope_p;
12248 if (access_declaration_p)
12249 cp_parser_parse_tentatively (parser);
12252 /* Look for the `using' keyword. */
12253 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12255 /* Peek at the next token. */
12256 token = cp_lexer_peek_token (parser->lexer);
12257 /* See if it's `typename'. */
12258 if (token->keyword == RID_TYPENAME)
12260 /* Remember that we've seen it. */
12262 /* Consume the `typename' token. */
12263 cp_lexer_consume_token (parser->lexer);
12267 /* Look for the optional global scope qualification. */
12269 = (cp_parser_global_scope_opt (parser,
12270 /*current_scope_valid_p=*/false)
12273 /* If we saw `typename', or didn't see `::', then there must be a
12274 nested-name-specifier present. */
12275 if (typename_p || !global_scope_p)
12276 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12277 /*check_dependency_p=*/true,
12279 /*is_declaration=*/true);
12280 /* Otherwise, we could be in either of the two productions. In that
12281 case, treat the nested-name-specifier as optional. */
12283 qscope = cp_parser_nested_name_specifier_opt (parser,
12284 /*typename_keyword_p=*/false,
12285 /*check_dependency_p=*/true,
12287 /*is_declaration=*/true);
12289 qscope = global_namespace;
12291 if (access_declaration_p && cp_parser_error_occurred (parser))
12292 /* Something has already gone wrong; there's no need to parse
12293 further. Since an error has occurred, the return value of
12294 cp_parser_parse_definitely will be false, as required. */
12295 return cp_parser_parse_definitely (parser);
12297 token = cp_lexer_peek_token (parser->lexer);
12298 /* Parse the unqualified-id. */
12299 identifier = cp_parser_unqualified_id (parser,
12300 /*template_keyword_p=*/false,
12301 /*check_dependency_p=*/true,
12302 /*declarator_p=*/true,
12303 /*optional_p=*/false);
12305 if (access_declaration_p)
12307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12308 cp_parser_simulate_error (parser);
12309 if (!cp_parser_parse_definitely (parser))
12313 /* The function we call to handle a using-declaration is different
12314 depending on what scope we are in. */
12315 if (qscope == error_mark_node || identifier == error_mark_node)
12317 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12318 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12319 /* [namespace.udecl]
12321 A using declaration shall not name a template-id. */
12322 error ("%Ha template-id may not appear in a using-declaration",
12326 if (at_class_scope_p ())
12328 /* Create the USING_DECL. */
12329 decl = do_class_using_decl (parser->scope, identifier);
12331 if (check_for_bare_parameter_packs (decl))
12334 /* Add it to the list of members in this class. */
12335 finish_member_declaration (decl);
12339 decl = cp_parser_lookup_name_simple (parser,
12342 if (decl == error_mark_node)
12343 cp_parser_name_lookup_error (parser, identifier,
12346 else if (check_for_bare_parameter_packs (decl))
12348 else if (!at_namespace_scope_p ())
12349 do_local_using_decl (decl, qscope, identifier);
12351 do_toplevel_using_decl (decl, qscope, identifier);
12355 /* Look for the final `;'. */
12356 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12361 /* Parse a using-directive.
12364 using namespace :: [opt] nested-name-specifier [opt]
12365 namespace-name ; */
12368 cp_parser_using_directive (cp_parser* parser)
12370 tree namespace_decl;
12373 /* Look for the `using' keyword. */
12374 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12375 /* And the `namespace' keyword. */
12376 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12377 /* Look for the optional `::' operator. */
12378 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12379 /* And the optional nested-name-specifier. */
12380 cp_parser_nested_name_specifier_opt (parser,
12381 /*typename_keyword_p=*/false,
12382 /*check_dependency_p=*/true,
12384 /*is_declaration=*/true);
12385 /* Get the namespace being used. */
12386 namespace_decl = cp_parser_namespace_name (parser);
12387 /* And any specified attributes. */
12388 attribs = cp_parser_attributes_opt (parser);
12389 /* Update the symbol table. */
12390 parse_using_directive (namespace_decl, attribs);
12391 /* Look for the final `;'. */
12392 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12395 /* Parse an asm-definition.
12398 asm ( string-literal ) ;
12403 asm volatile [opt] ( string-literal ) ;
12404 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12405 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12406 : asm-operand-list [opt] ) ;
12407 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12408 : asm-operand-list [opt]
12409 : asm-operand-list [opt] ) ; */
12412 cp_parser_asm_definition (cp_parser* parser)
12415 tree outputs = NULL_TREE;
12416 tree inputs = NULL_TREE;
12417 tree clobbers = NULL_TREE;
12419 bool volatile_p = false;
12420 bool extended_p = false;
12421 bool invalid_inputs_p = false;
12422 bool invalid_outputs_p = false;
12424 /* Look for the `asm' keyword. */
12425 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12426 /* See if the next token is `volatile'. */
12427 if (cp_parser_allow_gnu_extensions_p (parser)
12428 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12430 /* Remember that we saw the `volatile' keyword. */
12432 /* Consume the token. */
12433 cp_lexer_consume_token (parser->lexer);
12435 /* Look for the opening `('. */
12436 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12438 /* Look for the string. */
12439 string = cp_parser_string_literal (parser, false, false);
12440 if (string == error_mark_node)
12442 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12443 /*consume_paren=*/true);
12447 /* If we're allowing GNU extensions, check for the extended assembly
12448 syntax. Unfortunately, the `:' tokens need not be separated by
12449 a space in C, and so, for compatibility, we tolerate that here
12450 too. Doing that means that we have to treat the `::' operator as
12452 if (cp_parser_allow_gnu_extensions_p (parser)
12453 && parser->in_function_body
12454 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12455 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12457 bool inputs_p = false;
12458 bool clobbers_p = false;
12460 /* The extended syntax was used. */
12463 /* Look for outputs. */
12464 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12466 /* Consume the `:'. */
12467 cp_lexer_consume_token (parser->lexer);
12468 /* Parse the output-operands. */
12469 if (cp_lexer_next_token_is_not (parser->lexer,
12471 && cp_lexer_next_token_is_not (parser->lexer,
12473 && cp_lexer_next_token_is_not (parser->lexer,
12475 outputs = cp_parser_asm_operand_list (parser);
12477 if (outputs == error_mark_node)
12478 invalid_outputs_p = true;
12480 /* If the next token is `::', there are no outputs, and the
12481 next token is the beginning of the inputs. */
12482 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12483 /* The inputs are coming next. */
12486 /* Look for inputs. */
12488 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12490 /* Consume the `:' or `::'. */
12491 cp_lexer_consume_token (parser->lexer);
12492 /* Parse the output-operands. */
12493 if (cp_lexer_next_token_is_not (parser->lexer,
12495 && cp_lexer_next_token_is_not (parser->lexer,
12497 inputs = cp_parser_asm_operand_list (parser);
12499 if (inputs == error_mark_node)
12500 invalid_inputs_p = true;
12502 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12503 /* The clobbers are coming next. */
12506 /* Look for clobbers. */
12508 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12510 /* Consume the `:' or `::'. */
12511 cp_lexer_consume_token (parser->lexer);
12512 /* Parse the clobbers. */
12513 if (cp_lexer_next_token_is_not (parser->lexer,
12515 clobbers = cp_parser_asm_clobber_list (parser);
12518 /* Look for the closing `)'. */
12519 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12520 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12521 /*consume_paren=*/true);
12522 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12524 if (!invalid_inputs_p && !invalid_outputs_p)
12526 /* Create the ASM_EXPR. */
12527 if (parser->in_function_body)
12529 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12531 /* If the extended syntax was not used, mark the ASM_EXPR. */
12534 tree temp = asm_stmt;
12535 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12536 temp = TREE_OPERAND (temp, 0);
12538 ASM_INPUT_P (temp) = 1;
12542 cgraph_add_asm_node (string);
12546 /* Declarators [gram.dcl.decl] */
12548 /* Parse an init-declarator.
12551 declarator initializer [opt]
12556 declarator asm-specification [opt] attributes [opt] initializer [opt]
12558 function-definition:
12559 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12561 decl-specifier-seq [opt] declarator function-try-block
12565 function-definition:
12566 __extension__ function-definition
12568 The DECL_SPECIFIERS apply to this declarator. Returns a
12569 representation of the entity declared. If MEMBER_P is TRUE, then
12570 this declarator appears in a class scope. The new DECL created by
12571 this declarator is returned.
12573 The CHECKS are access checks that should be performed once we know
12574 what entity is being declared (and, therefore, what classes have
12577 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12578 for a function-definition here as well. If the declarator is a
12579 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12580 be TRUE upon return. By that point, the function-definition will
12581 have been completely parsed.
12583 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12587 cp_parser_init_declarator (cp_parser* parser,
12588 cp_decl_specifier_seq *decl_specifiers,
12589 VEC (deferred_access_check,gc)* checks,
12590 bool function_definition_allowed_p,
12592 int declares_class_or_enum,
12593 bool* function_definition_p)
12595 cp_token *token = NULL, *asm_spec_start_token = NULL,
12596 *attributes_start_token = NULL;
12597 cp_declarator *declarator;
12598 tree prefix_attributes;
12600 tree asm_specification;
12602 tree decl = NULL_TREE;
12604 int is_initialized;
12605 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12606 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12608 enum cpp_ttype initialization_kind;
12609 bool is_direct_init = false;
12610 bool is_non_constant_init;
12611 int ctor_dtor_or_conv_p;
12613 tree pushed_scope = NULL;
12615 /* Gather the attributes that were provided with the
12616 decl-specifiers. */
12617 prefix_attributes = decl_specifiers->attributes;
12619 /* Assume that this is not the declarator for a function
12621 if (function_definition_p)
12622 *function_definition_p = false;
12624 /* Defer access checks while parsing the declarator; we cannot know
12625 what names are accessible until we know what is being
12627 resume_deferring_access_checks ();
12629 /* Parse the declarator. */
12630 token = cp_lexer_peek_token (parser->lexer);
12632 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12633 &ctor_dtor_or_conv_p,
12634 /*parenthesized_p=*/NULL,
12635 /*member_p=*/false);
12636 /* Gather up the deferred checks. */
12637 stop_deferring_access_checks ();
12639 /* If the DECLARATOR was erroneous, there's no need to go
12641 if (declarator == cp_error_declarator)
12642 return error_mark_node;
12644 /* Check that the number of template-parameter-lists is OK. */
12645 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12647 return error_mark_node;
12649 if (declares_class_or_enum & 2)
12650 cp_parser_check_for_definition_in_return_type (declarator,
12651 decl_specifiers->type,
12652 decl_specifiers->type_location);
12654 /* Figure out what scope the entity declared by the DECLARATOR is
12655 located in. `grokdeclarator' sometimes changes the scope, so
12656 we compute it now. */
12657 scope = get_scope_of_declarator (declarator);
12659 /* If we're allowing GNU extensions, look for an asm-specification
12661 if (cp_parser_allow_gnu_extensions_p (parser))
12663 /* Look for an asm-specification. */
12664 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12665 asm_specification = cp_parser_asm_specification_opt (parser);
12666 /* And attributes. */
12667 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12668 attributes = cp_parser_attributes_opt (parser);
12672 asm_specification = NULL_TREE;
12673 attributes = NULL_TREE;
12676 /* Peek at the next token. */
12677 token = cp_lexer_peek_token (parser->lexer);
12678 /* Check to see if the token indicates the start of a
12679 function-definition. */
12680 if (function_declarator_p (declarator)
12681 && cp_parser_token_starts_function_definition_p (token))
12683 if (!function_definition_allowed_p)
12685 /* If a function-definition should not appear here, issue an
12687 cp_parser_error (parser,
12688 "a function-definition is not allowed here");
12689 return error_mark_node;
12693 location_t func_brace_location
12694 = cp_lexer_peek_token (parser->lexer)->location;
12696 /* Neither attributes nor an asm-specification are allowed
12697 on a function-definition. */
12698 if (asm_specification)
12699 error ("%Han asm-specification is not allowed "
12700 "on a function-definition",
12701 &asm_spec_start_token->location);
12703 error ("%Hattributes are not allowed on a function-definition",
12704 &attributes_start_token->location);
12705 /* This is a function-definition. */
12706 *function_definition_p = true;
12708 /* Parse the function definition. */
12710 decl = cp_parser_save_member_function_body (parser,
12713 prefix_attributes);
12716 = (cp_parser_function_definition_from_specifiers_and_declarator
12717 (parser, decl_specifiers, prefix_attributes, declarator));
12719 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12721 /* This is where the prologue starts... */
12722 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12723 = func_brace_location;
12732 Only in function declarations for constructors, destructors, and
12733 type conversions can the decl-specifier-seq be omitted.
12735 We explicitly postpone this check past the point where we handle
12736 function-definitions because we tolerate function-definitions
12737 that are missing their return types in some modes. */
12738 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12740 cp_parser_error (parser,
12741 "expected constructor, destructor, or type conversion");
12742 return error_mark_node;
12745 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12746 if (token->type == CPP_EQ
12747 || token->type == CPP_OPEN_PAREN
12748 || token->type == CPP_OPEN_BRACE)
12750 is_initialized = SD_INITIALIZED;
12751 initialization_kind = token->type;
12753 if (token->type == CPP_EQ
12754 && function_declarator_p (declarator))
12756 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12757 if (t2->keyword == RID_DEFAULT)
12758 is_initialized = SD_DEFAULTED;
12759 else if (t2->keyword == RID_DELETE)
12760 is_initialized = SD_DELETED;
12765 /* If the init-declarator isn't initialized and isn't followed by a
12766 `,' or `;', it's not a valid init-declarator. */
12767 if (token->type != CPP_COMMA
12768 && token->type != CPP_SEMICOLON)
12770 cp_parser_error (parser, "expected initializer");
12771 return error_mark_node;
12773 is_initialized = SD_UNINITIALIZED;
12774 initialization_kind = CPP_EOF;
12777 /* Because start_decl has side-effects, we should only call it if we
12778 know we're going ahead. By this point, we know that we cannot
12779 possibly be looking at any other construct. */
12780 cp_parser_commit_to_tentative_parse (parser);
12782 /* If the decl specifiers were bad, issue an error now that we're
12783 sure this was intended to be a declarator. Then continue
12784 declaring the variable(s), as int, to try to cut down on further
12786 if (decl_specifiers->any_specifiers_p
12787 && decl_specifiers->type == error_mark_node)
12789 cp_parser_error (parser, "invalid type in declaration");
12790 decl_specifiers->type = integer_type_node;
12793 /* Check to see whether or not this declaration is a friend. */
12794 friend_p = cp_parser_friend_p (decl_specifiers);
12796 /* Enter the newly declared entry in the symbol table. If we're
12797 processing a declaration in a class-specifier, we wait until
12798 after processing the initializer. */
12801 if (parser->in_unbraced_linkage_specification_p)
12802 decl_specifiers->storage_class = sc_extern;
12803 decl = start_decl (declarator, decl_specifiers,
12804 is_initialized, attributes, prefix_attributes,
12808 /* Enter the SCOPE. That way unqualified names appearing in the
12809 initializer will be looked up in SCOPE. */
12810 pushed_scope = push_scope (scope);
12812 /* Perform deferred access control checks, now that we know in which
12813 SCOPE the declared entity resides. */
12814 if (!member_p && decl)
12816 tree saved_current_function_decl = NULL_TREE;
12818 /* If the entity being declared is a function, pretend that we
12819 are in its scope. If it is a `friend', it may have access to
12820 things that would not otherwise be accessible. */
12821 if (TREE_CODE (decl) == FUNCTION_DECL)
12823 saved_current_function_decl = current_function_decl;
12824 current_function_decl = decl;
12827 /* Perform access checks for template parameters. */
12828 cp_parser_perform_template_parameter_access_checks (checks);
12830 /* Perform the access control checks for the declarator and the
12831 decl-specifiers. */
12832 perform_deferred_access_checks ();
12834 /* Restore the saved value. */
12835 if (TREE_CODE (decl) == FUNCTION_DECL)
12836 current_function_decl = saved_current_function_decl;
12839 /* Parse the initializer. */
12840 initializer = NULL_TREE;
12841 is_direct_init = false;
12842 is_non_constant_init = true;
12843 if (is_initialized)
12845 if (function_declarator_p (declarator))
12847 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12848 if (initialization_kind == CPP_EQ)
12849 initializer = cp_parser_pure_specifier (parser);
12852 /* If the declaration was erroneous, we don't really
12853 know what the user intended, so just silently
12854 consume the initializer. */
12855 if (decl != error_mark_node)
12856 error ("%Hinitializer provided for function",
12857 &initializer_start_token->location);
12858 cp_parser_skip_to_closing_parenthesis (parser,
12859 /*recovering=*/true,
12860 /*or_comma=*/false,
12861 /*consume_paren=*/true);
12865 initializer = cp_parser_initializer (parser,
12867 &is_non_constant_init);
12870 /* The old parser allows attributes to appear after a parenthesized
12871 initializer. Mark Mitchell proposed removing this functionality
12872 on the GCC mailing lists on 2002-08-13. This parser accepts the
12873 attributes -- but ignores them. */
12874 if (cp_parser_allow_gnu_extensions_p (parser)
12875 && initialization_kind == CPP_OPEN_PAREN)
12876 if (cp_parser_attributes_opt (parser))
12877 warning (OPT_Wattributes,
12878 "attributes after parenthesized initializer ignored");
12880 /* For an in-class declaration, use `grokfield' to create the
12886 pop_scope (pushed_scope);
12887 pushed_scope = false;
12889 decl = grokfield (declarator, decl_specifiers,
12890 initializer, !is_non_constant_init,
12891 /*asmspec=*/NULL_TREE,
12892 prefix_attributes);
12893 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12894 cp_parser_save_default_args (parser, decl);
12897 /* Finish processing the declaration. But, skip friend
12899 if (!friend_p && decl && decl != error_mark_node)
12901 cp_finish_decl (decl,
12902 initializer, !is_non_constant_init,
12904 /* If the initializer is in parentheses, then this is
12905 a direct-initialization, which means that an
12906 `explicit' constructor is OK. Otherwise, an
12907 `explicit' constructor cannot be used. */
12908 ((is_direct_init || !is_initialized)
12909 ? 0 : LOOKUP_ONLYCONVERTING));
12911 else if ((cxx_dialect != cxx98) && friend_p
12912 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12913 /* Core issue #226 (C++0x only): A default template-argument
12914 shall not be specified in a friend class template
12916 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12917 /*is_partial=*/0, /*is_friend_decl=*/1);
12919 if (!friend_p && pushed_scope)
12920 pop_scope (pushed_scope);
12925 /* Parse a declarator.
12929 ptr-operator declarator
12931 abstract-declarator:
12932 ptr-operator abstract-declarator [opt]
12933 direct-abstract-declarator
12938 attributes [opt] direct-declarator
12939 attributes [opt] ptr-operator declarator
12941 abstract-declarator:
12942 attributes [opt] ptr-operator abstract-declarator [opt]
12943 attributes [opt] direct-abstract-declarator
12945 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12946 detect constructor, destructor or conversion operators. It is set
12947 to -1 if the declarator is a name, and +1 if it is a
12948 function. Otherwise it is set to zero. Usually you just want to
12949 test for >0, but internally the negative value is used.
12951 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12952 a decl-specifier-seq unless it declares a constructor, destructor,
12953 or conversion. It might seem that we could check this condition in
12954 semantic analysis, rather than parsing, but that makes it difficult
12955 to handle something like `f()'. We want to notice that there are
12956 no decl-specifiers, and therefore realize that this is an
12957 expression, not a declaration.)
12959 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12960 the declarator is a direct-declarator of the form "(...)".
12962 MEMBER_P is true iff this declarator is a member-declarator. */
12964 static cp_declarator *
12965 cp_parser_declarator (cp_parser* parser,
12966 cp_parser_declarator_kind dcl_kind,
12967 int* ctor_dtor_or_conv_p,
12968 bool* parenthesized_p,
12972 cp_declarator *declarator;
12973 enum tree_code code;
12974 cp_cv_quals cv_quals;
12976 tree attributes = NULL_TREE;
12978 /* Assume this is not a constructor, destructor, or type-conversion
12980 if (ctor_dtor_or_conv_p)
12981 *ctor_dtor_or_conv_p = 0;
12983 if (cp_parser_allow_gnu_extensions_p (parser))
12984 attributes = cp_parser_attributes_opt (parser);
12986 /* Peek at the next token. */
12987 token = cp_lexer_peek_token (parser->lexer);
12989 /* Check for the ptr-operator production. */
12990 cp_parser_parse_tentatively (parser);
12991 /* Parse the ptr-operator. */
12992 code = cp_parser_ptr_operator (parser,
12995 /* If that worked, then we have a ptr-operator. */
12996 if (cp_parser_parse_definitely (parser))
12998 /* If a ptr-operator was found, then this declarator was not
13000 if (parenthesized_p)
13001 *parenthesized_p = true;
13002 /* The dependent declarator is optional if we are parsing an
13003 abstract-declarator. */
13004 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13005 cp_parser_parse_tentatively (parser);
13007 /* Parse the dependent declarator. */
13008 declarator = cp_parser_declarator (parser, dcl_kind,
13009 /*ctor_dtor_or_conv_p=*/NULL,
13010 /*parenthesized_p=*/NULL,
13011 /*member_p=*/false);
13013 /* If we are parsing an abstract-declarator, we must handle the
13014 case where the dependent declarator is absent. */
13015 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13016 && !cp_parser_parse_definitely (parser))
13019 declarator = cp_parser_make_indirect_declarator
13020 (code, class_type, cv_quals, declarator);
13022 /* Everything else is a direct-declarator. */
13025 if (parenthesized_p)
13026 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13028 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13029 ctor_dtor_or_conv_p,
13033 if (attributes && declarator && declarator != cp_error_declarator)
13034 declarator->attributes = attributes;
13039 /* Parse a direct-declarator or direct-abstract-declarator.
13043 direct-declarator ( parameter-declaration-clause )
13044 cv-qualifier-seq [opt]
13045 exception-specification [opt]
13046 direct-declarator [ constant-expression [opt] ]
13049 direct-abstract-declarator:
13050 direct-abstract-declarator [opt]
13051 ( parameter-declaration-clause )
13052 cv-qualifier-seq [opt]
13053 exception-specification [opt]
13054 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13055 ( abstract-declarator )
13057 Returns a representation of the declarator. DCL_KIND is
13058 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13059 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13060 we are parsing a direct-declarator. It is
13061 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13062 of ambiguity we prefer an abstract declarator, as per
13063 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13064 cp_parser_declarator. */
13066 static cp_declarator *
13067 cp_parser_direct_declarator (cp_parser* parser,
13068 cp_parser_declarator_kind dcl_kind,
13069 int* ctor_dtor_or_conv_p,
13073 cp_declarator *declarator = NULL;
13074 tree scope = NULL_TREE;
13075 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13076 bool saved_in_declarator_p = parser->in_declarator_p;
13078 tree pushed_scope = NULL_TREE;
13082 /* Peek at the next token. */
13083 token = cp_lexer_peek_token (parser->lexer);
13084 if (token->type == CPP_OPEN_PAREN)
13086 /* This is either a parameter-declaration-clause, or a
13087 parenthesized declarator. When we know we are parsing a
13088 named declarator, it must be a parenthesized declarator
13089 if FIRST is true. For instance, `(int)' is a
13090 parameter-declaration-clause, with an omitted
13091 direct-abstract-declarator. But `((*))', is a
13092 parenthesized abstract declarator. Finally, when T is a
13093 template parameter `(T)' is a
13094 parameter-declaration-clause, and not a parenthesized
13097 We first try and parse a parameter-declaration-clause,
13098 and then try a nested declarator (if FIRST is true).
13100 It is not an error for it not to be a
13101 parameter-declaration-clause, even when FIRST is
13107 The first is the declaration of a function while the
13108 second is the definition of a variable, including its
13111 Having seen only the parenthesis, we cannot know which of
13112 these two alternatives should be selected. Even more
13113 complex are examples like:
13118 The former is a function-declaration; the latter is a
13119 variable initialization.
13121 Thus again, we try a parameter-declaration-clause, and if
13122 that fails, we back out and return. */
13124 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13127 unsigned saved_num_template_parameter_lists;
13128 bool is_declarator = false;
13131 /* In a member-declarator, the only valid interpretation
13132 of a parenthesis is the start of a
13133 parameter-declaration-clause. (It is invalid to
13134 initialize a static data member with a parenthesized
13135 initializer; only the "=" form of initialization is
13138 cp_parser_parse_tentatively (parser);
13140 /* Consume the `('. */
13141 cp_lexer_consume_token (parser->lexer);
13144 /* If this is going to be an abstract declarator, we're
13145 in a declarator and we can't have default args. */
13146 parser->default_arg_ok_p = false;
13147 parser->in_declarator_p = true;
13150 /* Inside the function parameter list, surrounding
13151 template-parameter-lists do not apply. */
13152 saved_num_template_parameter_lists
13153 = parser->num_template_parameter_lists;
13154 parser->num_template_parameter_lists = 0;
13156 begin_scope (sk_function_parms, NULL_TREE);
13158 /* Parse the parameter-declaration-clause. */
13159 params = cp_parser_parameter_declaration_clause (parser);
13161 parser->num_template_parameter_lists
13162 = saved_num_template_parameter_lists;
13164 /* If all went well, parse the cv-qualifier-seq and the
13165 exception-specification. */
13166 if (member_p || cp_parser_parse_definitely (parser))
13168 cp_cv_quals cv_quals;
13169 tree exception_specification;
13172 is_declarator = true;
13174 if (ctor_dtor_or_conv_p)
13175 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13177 /* Consume the `)'. */
13178 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13180 /* Parse the cv-qualifier-seq. */
13181 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13182 /* And the exception-specification. */
13183 exception_specification
13184 = cp_parser_exception_specification_opt (parser);
13187 = cp_parser_late_return_type_opt (parser);
13189 /* Create the function-declarator. */
13190 declarator = make_call_declarator (declarator,
13193 exception_specification,
13195 /* Any subsequent parameter lists are to do with
13196 return type, so are not those of the declared
13198 parser->default_arg_ok_p = false;
13201 /* Remove the function parms from scope. */
13202 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13203 pop_binding (DECL_NAME (t), t);
13207 /* Repeat the main loop. */
13211 /* If this is the first, we can try a parenthesized
13215 bool saved_in_type_id_in_expr_p;
13217 parser->default_arg_ok_p = saved_default_arg_ok_p;
13218 parser->in_declarator_p = saved_in_declarator_p;
13220 /* Consume the `('. */
13221 cp_lexer_consume_token (parser->lexer);
13222 /* Parse the nested declarator. */
13223 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13224 parser->in_type_id_in_expr_p = true;
13226 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13227 /*parenthesized_p=*/NULL,
13229 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13231 /* Expect a `)'. */
13232 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13233 declarator = cp_error_declarator;
13234 if (declarator == cp_error_declarator)
13237 goto handle_declarator;
13239 /* Otherwise, we must be done. */
13243 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13244 && token->type == CPP_OPEN_SQUARE)
13246 /* Parse an array-declarator. */
13249 if (ctor_dtor_or_conv_p)
13250 *ctor_dtor_or_conv_p = 0;
13253 parser->default_arg_ok_p = false;
13254 parser->in_declarator_p = true;
13255 /* Consume the `['. */
13256 cp_lexer_consume_token (parser->lexer);
13257 /* Peek at the next token. */
13258 token = cp_lexer_peek_token (parser->lexer);
13259 /* If the next token is `]', then there is no
13260 constant-expression. */
13261 if (token->type != CPP_CLOSE_SQUARE)
13263 bool non_constant_p;
13266 = cp_parser_constant_expression (parser,
13267 /*allow_non_constant=*/true,
13269 if (!non_constant_p)
13270 bounds = fold_non_dependent_expr (bounds);
13271 /* Normally, the array bound must be an integral constant
13272 expression. However, as an extension, we allow VLAs
13273 in function scopes. */
13274 else if (!parser->in_function_body)
13276 error ("%Harray bound is not an integer constant",
13278 bounds = error_mark_node;
13282 bounds = NULL_TREE;
13283 /* Look for the closing `]'. */
13284 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13286 declarator = cp_error_declarator;
13290 declarator = make_array_declarator (declarator, bounds);
13292 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13294 tree qualifying_scope;
13295 tree unqualified_name;
13296 special_function_kind sfk;
13298 bool pack_expansion_p = false;
13299 cp_token *declarator_id_start_token;
13301 /* Parse a declarator-id */
13302 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13305 cp_parser_parse_tentatively (parser);
13307 /* If we see an ellipsis, we should be looking at a
13309 if (token->type == CPP_ELLIPSIS)
13311 /* Consume the `...' */
13312 cp_lexer_consume_token (parser->lexer);
13314 pack_expansion_p = true;
13318 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13320 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13321 qualifying_scope = parser->scope;
13326 if (!unqualified_name && pack_expansion_p)
13328 /* Check whether an error occurred. */
13329 okay = !cp_parser_error_occurred (parser);
13331 /* We already consumed the ellipsis to mark a
13332 parameter pack, but we have no way to report it,
13333 so abort the tentative parse. We will be exiting
13334 immediately anyway. */
13335 cp_parser_abort_tentative_parse (parser);
13338 okay = cp_parser_parse_definitely (parser);
13341 unqualified_name = error_mark_node;
13342 else if (unqualified_name
13343 && (qualifying_scope
13344 || (TREE_CODE (unqualified_name)
13345 != IDENTIFIER_NODE)))
13347 cp_parser_error (parser, "expected unqualified-id");
13348 unqualified_name = error_mark_node;
13352 if (!unqualified_name)
13354 if (unqualified_name == error_mark_node)
13356 declarator = cp_error_declarator;
13357 pack_expansion_p = false;
13358 declarator->parameter_pack_p = false;
13362 if (qualifying_scope && at_namespace_scope_p ()
13363 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13365 /* In the declaration of a member of a template class
13366 outside of the class itself, the SCOPE will sometimes
13367 be a TYPENAME_TYPE. For example, given:
13369 template <typename T>
13370 int S<T>::R::i = 3;
13372 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13373 this context, we must resolve S<T>::R to an ordinary
13374 type, rather than a typename type.
13376 The reason we normally avoid resolving TYPENAME_TYPEs
13377 is that a specialization of `S' might render
13378 `S<T>::R' not a type. However, if `S' is
13379 specialized, then this `i' will not be used, so there
13380 is no harm in resolving the types here. */
13383 /* Resolve the TYPENAME_TYPE. */
13384 type = resolve_typename_type (qualifying_scope,
13385 /*only_current_p=*/false);
13386 /* If that failed, the declarator is invalid. */
13387 if (TREE_CODE (type) == TYPENAME_TYPE)
13388 error ("%H%<%T::%E%> is not a type",
13389 &declarator_id_start_token->location,
13390 TYPE_CONTEXT (qualifying_scope),
13391 TYPE_IDENTIFIER (qualifying_scope));
13392 qualifying_scope = type;
13397 if (unqualified_name)
13401 if (qualifying_scope
13402 && CLASS_TYPE_P (qualifying_scope))
13403 class_type = qualifying_scope;
13405 class_type = current_class_type;
13407 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13409 tree name_type = TREE_TYPE (unqualified_name);
13410 if (class_type && same_type_p (name_type, class_type))
13412 if (qualifying_scope
13413 && CLASSTYPE_USE_TEMPLATE (name_type))
13415 error ("%Hinvalid use of constructor as a template",
13416 &declarator_id_start_token->location);
13417 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13418 "name the constructor in a qualified name",
13420 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13421 class_type, name_type);
13422 declarator = cp_error_declarator;
13426 unqualified_name = constructor_name (class_type);
13430 /* We do not attempt to print the declarator
13431 here because we do not have enough
13432 information about its original syntactic
13434 cp_parser_error (parser, "invalid declarator");
13435 declarator = cp_error_declarator;
13442 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13443 sfk = sfk_destructor;
13444 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13445 sfk = sfk_conversion;
13446 else if (/* There's no way to declare a constructor
13447 for an anonymous type, even if the type
13448 got a name for linkage purposes. */
13449 !TYPE_WAS_ANONYMOUS (class_type)
13450 && constructor_name_p (unqualified_name,
13453 unqualified_name = constructor_name (class_type);
13454 sfk = sfk_constructor;
13457 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13458 *ctor_dtor_or_conv_p = -1;
13461 declarator = make_id_declarator (qualifying_scope,
13464 declarator->id_loc = token->location;
13465 declarator->parameter_pack_p = pack_expansion_p;
13467 if (pack_expansion_p)
13468 maybe_warn_variadic_templates ();
13470 handle_declarator:;
13471 scope = get_scope_of_declarator (declarator);
13473 /* Any names that appear after the declarator-id for a
13474 member are looked up in the containing scope. */
13475 pushed_scope = push_scope (scope);
13476 parser->in_declarator_p = true;
13477 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13478 || (declarator && declarator->kind == cdk_id))
13479 /* Default args are only allowed on function
13481 parser->default_arg_ok_p = saved_default_arg_ok_p;
13483 parser->default_arg_ok_p = false;
13492 /* For an abstract declarator, we might wind up with nothing at this
13493 point. That's an error; the declarator is not optional. */
13495 cp_parser_error (parser, "expected declarator");
13497 /* If we entered a scope, we must exit it now. */
13499 pop_scope (pushed_scope);
13501 parser->default_arg_ok_p = saved_default_arg_ok_p;
13502 parser->in_declarator_p = saved_in_declarator_p;
13507 /* Parse a ptr-operator.
13510 * cv-qualifier-seq [opt]
13512 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13517 & cv-qualifier-seq [opt]
13519 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13520 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13521 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13522 filled in with the TYPE containing the member. *CV_QUALS is
13523 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13524 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13525 Note that the tree codes returned by this function have nothing
13526 to do with the types of trees that will be eventually be created
13527 to represent the pointer or reference type being parsed. They are
13528 just constants with suggestive names. */
13529 static enum tree_code
13530 cp_parser_ptr_operator (cp_parser* parser,
13532 cp_cv_quals *cv_quals)
13534 enum tree_code code = ERROR_MARK;
13537 /* Assume that it's not a pointer-to-member. */
13539 /* And that there are no cv-qualifiers. */
13540 *cv_quals = TYPE_UNQUALIFIED;
13542 /* Peek at the next token. */
13543 token = cp_lexer_peek_token (parser->lexer);
13545 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13546 if (token->type == CPP_MULT)
13547 code = INDIRECT_REF;
13548 else if (token->type == CPP_AND)
13550 else if ((cxx_dialect != cxx98) &&
13551 token->type == CPP_AND_AND) /* C++0x only */
13552 code = NON_LVALUE_EXPR;
13554 if (code != ERROR_MARK)
13556 /* Consume the `*', `&' or `&&'. */
13557 cp_lexer_consume_token (parser->lexer);
13559 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13560 `&', if we are allowing GNU extensions. (The only qualifier
13561 that can legally appear after `&' is `restrict', but that is
13562 enforced during semantic analysis. */
13563 if (code == INDIRECT_REF
13564 || cp_parser_allow_gnu_extensions_p (parser))
13565 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13569 /* Try the pointer-to-member case. */
13570 cp_parser_parse_tentatively (parser);
13571 /* Look for the optional `::' operator. */
13572 cp_parser_global_scope_opt (parser,
13573 /*current_scope_valid_p=*/false);
13574 /* Look for the nested-name specifier. */
13575 token = cp_lexer_peek_token (parser->lexer);
13576 cp_parser_nested_name_specifier (parser,
13577 /*typename_keyword_p=*/false,
13578 /*check_dependency_p=*/true,
13580 /*is_declaration=*/false);
13581 /* If we found it, and the next token is a `*', then we are
13582 indeed looking at a pointer-to-member operator. */
13583 if (!cp_parser_error_occurred (parser)
13584 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13586 /* Indicate that the `*' operator was used. */
13587 code = INDIRECT_REF;
13589 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13590 error ("%H%qD is a namespace", &token->location, parser->scope);
13593 /* The type of which the member is a member is given by the
13595 *type = parser->scope;
13596 /* The next name will not be qualified. */
13597 parser->scope = NULL_TREE;
13598 parser->qualifying_scope = NULL_TREE;
13599 parser->object_scope = NULL_TREE;
13600 /* Look for the optional cv-qualifier-seq. */
13601 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13604 /* If that didn't work we don't have a ptr-operator. */
13605 if (!cp_parser_parse_definitely (parser))
13606 cp_parser_error (parser, "expected ptr-operator");
13612 /* Parse an (optional) cv-qualifier-seq.
13615 cv-qualifier cv-qualifier-seq [opt]
13626 Returns a bitmask representing the cv-qualifiers. */
13629 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13631 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13636 cp_cv_quals cv_qualifier;
13638 /* Peek at the next token. */
13639 token = cp_lexer_peek_token (parser->lexer);
13640 /* See if it's a cv-qualifier. */
13641 switch (token->keyword)
13644 cv_qualifier = TYPE_QUAL_CONST;
13648 cv_qualifier = TYPE_QUAL_VOLATILE;
13652 cv_qualifier = TYPE_QUAL_RESTRICT;
13656 cv_qualifier = TYPE_UNQUALIFIED;
13663 if (cv_quals & cv_qualifier)
13665 error ("%Hduplicate cv-qualifier", &token->location);
13666 cp_lexer_purge_token (parser->lexer);
13670 cp_lexer_consume_token (parser->lexer);
13671 cv_quals |= cv_qualifier;
13678 /* Parse a late-specified return type, if any. This is not a separate
13679 non-terminal, but part of a function declarator, which looks like
13683 Returns the type indicated by the type-id. */
13686 cp_parser_late_return_type_opt (cp_parser* parser)
13690 /* Peek at the next token. */
13691 token = cp_lexer_peek_token (parser->lexer);
13692 /* A late-specified return type is indicated by an initial '->'. */
13693 if (token->type != CPP_DEREF)
13696 /* Consume the ->. */
13697 cp_lexer_consume_token (parser->lexer);
13699 return cp_parser_type_id (parser);
13702 /* Parse a declarator-id.
13706 :: [opt] nested-name-specifier [opt] type-name
13708 In the `id-expression' case, the value returned is as for
13709 cp_parser_id_expression if the id-expression was an unqualified-id.
13710 If the id-expression was a qualified-id, then a SCOPE_REF is
13711 returned. The first operand is the scope (either a NAMESPACE_DECL
13712 or TREE_TYPE), but the second is still just a representation of an
13716 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13719 /* The expression must be an id-expression. Assume that qualified
13720 names are the names of types so that:
13723 int S<T>::R::i = 3;
13725 will work; we must treat `S<T>::R' as the name of a type.
13726 Similarly, assume that qualified names are templates, where
13730 int S<T>::R<T>::i = 3;
13733 id = cp_parser_id_expression (parser,
13734 /*template_keyword_p=*/false,
13735 /*check_dependency_p=*/false,
13736 /*template_p=*/NULL,
13737 /*declarator_p=*/true,
13739 if (id && BASELINK_P (id))
13740 id = BASELINK_FUNCTIONS (id);
13744 /* Parse a type-id.
13747 type-specifier-seq abstract-declarator [opt]
13749 Returns the TYPE specified. */
13752 cp_parser_type_id (cp_parser* parser)
13754 cp_decl_specifier_seq type_specifier_seq;
13755 cp_declarator *abstract_declarator;
13757 /* Parse the type-specifier-seq. */
13758 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13759 &type_specifier_seq);
13760 if (type_specifier_seq.type == error_mark_node)
13761 return error_mark_node;
13763 /* There might or might not be an abstract declarator. */
13764 cp_parser_parse_tentatively (parser);
13765 /* Look for the declarator. */
13766 abstract_declarator
13767 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13768 /*parenthesized_p=*/NULL,
13769 /*member_p=*/false);
13770 /* Check to see if there really was a declarator. */
13771 if (!cp_parser_parse_definitely (parser))
13772 abstract_declarator = NULL;
13774 if (type_specifier_seq.type
13775 && type_uses_auto (type_specifier_seq.type))
13777 error ("invalid use of %<auto%>");
13778 return error_mark_node;
13781 return groktypename (&type_specifier_seq, abstract_declarator);
13784 /* Parse a type-specifier-seq.
13786 type-specifier-seq:
13787 type-specifier type-specifier-seq [opt]
13791 type-specifier-seq:
13792 attributes type-specifier-seq [opt]
13794 If IS_CONDITION is true, we are at the start of a "condition",
13795 e.g., we've just seen "if (".
13797 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13800 cp_parser_type_specifier_seq (cp_parser* parser,
13802 cp_decl_specifier_seq *type_specifier_seq)
13804 bool seen_type_specifier = false;
13805 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13806 cp_token *start_token = NULL;
13808 /* Clear the TYPE_SPECIFIER_SEQ. */
13809 clear_decl_specs (type_specifier_seq);
13811 /* Parse the type-specifiers and attributes. */
13814 tree type_specifier;
13815 bool is_cv_qualifier;
13817 /* Check for attributes first. */
13818 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13820 type_specifier_seq->attributes =
13821 chainon (type_specifier_seq->attributes,
13822 cp_parser_attributes_opt (parser));
13826 /* record the token of the beginning of the type specifier seq,
13827 for error reporting purposes*/
13829 start_token = cp_lexer_peek_token (parser->lexer);
13831 /* Look for the type-specifier. */
13832 type_specifier = cp_parser_type_specifier (parser,
13834 type_specifier_seq,
13835 /*is_declaration=*/false,
13838 if (!type_specifier)
13840 /* If the first type-specifier could not be found, this is not a
13841 type-specifier-seq at all. */
13842 if (!seen_type_specifier)
13844 cp_parser_error (parser, "expected type-specifier");
13845 type_specifier_seq->type = error_mark_node;
13848 /* If subsequent type-specifiers could not be found, the
13849 type-specifier-seq is complete. */
13853 seen_type_specifier = true;
13854 /* The standard says that a condition can be:
13856 type-specifier-seq declarator = assignment-expression
13863 we should treat the "S" as a declarator, not as a
13864 type-specifier. The standard doesn't say that explicitly for
13865 type-specifier-seq, but it does say that for
13866 decl-specifier-seq in an ordinary declaration. Perhaps it
13867 would be clearer just to allow a decl-specifier-seq here, and
13868 then add a semantic restriction that if any decl-specifiers
13869 that are not type-specifiers appear, the program is invalid. */
13870 if (is_condition && !is_cv_qualifier)
13871 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13874 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13877 /* Parse a parameter-declaration-clause.
13879 parameter-declaration-clause:
13880 parameter-declaration-list [opt] ... [opt]
13881 parameter-declaration-list , ...
13883 Returns a representation for the parameter declarations. A return
13884 value of NULL indicates a parameter-declaration-clause consisting
13885 only of an ellipsis. */
13888 cp_parser_parameter_declaration_clause (cp_parser* parser)
13895 /* Peek at the next token. */
13896 token = cp_lexer_peek_token (parser->lexer);
13897 /* Check for trivial parameter-declaration-clauses. */
13898 if (token->type == CPP_ELLIPSIS)
13900 /* Consume the `...' token. */
13901 cp_lexer_consume_token (parser->lexer);
13904 else if (token->type == CPP_CLOSE_PAREN)
13905 /* There are no parameters. */
13907 #ifndef NO_IMPLICIT_EXTERN_C
13908 if (in_system_header && current_class_type == NULL
13909 && current_lang_name == lang_name_c)
13913 return void_list_node;
13915 /* Check for `(void)', too, which is a special case. */
13916 else if (token->keyword == RID_VOID
13917 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13918 == CPP_CLOSE_PAREN))
13920 /* Consume the `void' token. */
13921 cp_lexer_consume_token (parser->lexer);
13922 /* There are no parameters. */
13923 return void_list_node;
13926 /* Parse the parameter-declaration-list. */
13927 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13928 /* If a parse error occurred while parsing the
13929 parameter-declaration-list, then the entire
13930 parameter-declaration-clause is erroneous. */
13934 /* Peek at the next token. */
13935 token = cp_lexer_peek_token (parser->lexer);
13936 /* If it's a `,', the clause should terminate with an ellipsis. */
13937 if (token->type == CPP_COMMA)
13939 /* Consume the `,'. */
13940 cp_lexer_consume_token (parser->lexer);
13941 /* Expect an ellipsis. */
13943 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13945 /* It might also be `...' if the optional trailing `,' was
13947 else if (token->type == CPP_ELLIPSIS)
13949 /* Consume the `...' token. */
13950 cp_lexer_consume_token (parser->lexer);
13951 /* And remember that we saw it. */
13955 ellipsis_p = false;
13957 /* Finish the parameter list. */
13959 parameters = chainon (parameters, void_list_node);
13964 /* Parse a parameter-declaration-list.
13966 parameter-declaration-list:
13967 parameter-declaration
13968 parameter-declaration-list , parameter-declaration
13970 Returns a representation of the parameter-declaration-list, as for
13971 cp_parser_parameter_declaration_clause. However, the
13972 `void_list_node' is never appended to the list. Upon return,
13973 *IS_ERROR will be true iff an error occurred. */
13976 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13978 tree parameters = NULL_TREE;
13979 tree *tail = ¶meters;
13980 bool saved_in_unbraced_linkage_specification_p;
13982 /* Assume all will go well. */
13984 /* The special considerations that apply to a function within an
13985 unbraced linkage specifications do not apply to the parameters
13986 to the function. */
13987 saved_in_unbraced_linkage_specification_p
13988 = parser->in_unbraced_linkage_specification_p;
13989 parser->in_unbraced_linkage_specification_p = false;
13991 /* Look for more parameters. */
13994 cp_parameter_declarator *parameter;
13995 tree decl = error_mark_node;
13996 bool parenthesized_p;
13997 /* Parse the parameter. */
13999 = cp_parser_parameter_declaration (parser,
14000 /*template_parm_p=*/false,
14003 /* We don't know yet if the enclosing context is deprecated, so wait
14004 and warn in grokparms if appropriate. */
14005 deprecated_state = DEPRECATED_SUPPRESS;
14008 decl = grokdeclarator (parameter->declarator,
14009 ¶meter->decl_specifiers,
14011 parameter->default_argument != NULL_TREE,
14012 ¶meter->decl_specifiers.attributes);
14014 deprecated_state = DEPRECATED_NORMAL;
14016 /* If a parse error occurred parsing the parameter declaration,
14017 then the entire parameter-declaration-list is erroneous. */
14018 if (decl == error_mark_node)
14021 parameters = error_mark_node;
14025 if (parameter->decl_specifiers.attributes)
14026 cplus_decl_attributes (&decl,
14027 parameter->decl_specifiers.attributes,
14029 if (DECL_NAME (decl))
14030 decl = pushdecl (decl);
14032 /* Add the new parameter to the list. */
14033 *tail = build_tree_list (parameter->default_argument, decl);
14034 tail = &TREE_CHAIN (*tail);
14036 /* Peek at the next token. */
14037 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14038 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14039 /* These are for Objective-C++ */
14040 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14041 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14042 /* The parameter-declaration-list is complete. */
14044 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14048 /* Peek at the next token. */
14049 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14050 /* If it's an ellipsis, then the list is complete. */
14051 if (token->type == CPP_ELLIPSIS)
14053 /* Otherwise, there must be more parameters. Consume the
14055 cp_lexer_consume_token (parser->lexer);
14056 /* When parsing something like:
14058 int i(float f, double d)
14060 we can tell after seeing the declaration for "f" that we
14061 are not looking at an initialization of a variable "i",
14062 but rather at the declaration of a function "i".
14064 Due to the fact that the parsing of template arguments
14065 (as specified to a template-id) requires backtracking we
14066 cannot use this technique when inside a template argument
14068 if (!parser->in_template_argument_list_p
14069 && !parser->in_type_id_in_expr_p
14070 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14071 /* However, a parameter-declaration of the form
14072 "foat(f)" (which is a valid declaration of a
14073 parameter "f") can also be interpreted as an
14074 expression (the conversion of "f" to "float"). */
14075 && !parenthesized_p)
14076 cp_parser_commit_to_tentative_parse (parser);
14080 cp_parser_error (parser, "expected %<,%> or %<...%>");
14081 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14082 cp_parser_skip_to_closing_parenthesis (parser,
14083 /*recovering=*/true,
14084 /*or_comma=*/false,
14085 /*consume_paren=*/false);
14090 parser->in_unbraced_linkage_specification_p
14091 = saved_in_unbraced_linkage_specification_p;
14096 /* Parse a parameter declaration.
14098 parameter-declaration:
14099 decl-specifier-seq ... [opt] declarator
14100 decl-specifier-seq declarator = assignment-expression
14101 decl-specifier-seq ... [opt] abstract-declarator [opt]
14102 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14104 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14105 declares a template parameter. (In that case, a non-nested `>'
14106 token encountered during the parsing of the assignment-expression
14107 is not interpreted as a greater-than operator.)
14109 Returns a representation of the parameter, or NULL if an error
14110 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14111 true iff the declarator is of the form "(p)". */
14113 static cp_parameter_declarator *
14114 cp_parser_parameter_declaration (cp_parser *parser,
14115 bool template_parm_p,
14116 bool *parenthesized_p)
14118 int declares_class_or_enum;
14119 bool greater_than_is_operator_p;
14120 cp_decl_specifier_seq decl_specifiers;
14121 cp_declarator *declarator;
14122 tree default_argument;
14123 cp_token *token = NULL, *declarator_token_start = NULL;
14124 const char *saved_message;
14126 /* In a template parameter, `>' is not an operator.
14130 When parsing a default template-argument for a non-type
14131 template-parameter, the first non-nested `>' is taken as the end
14132 of the template parameter-list rather than a greater-than
14134 greater_than_is_operator_p = !template_parm_p;
14136 /* Type definitions may not appear in parameter types. */
14137 saved_message = parser->type_definition_forbidden_message;
14138 parser->type_definition_forbidden_message
14139 = "types may not be defined in parameter types";
14141 /* Parse the declaration-specifiers. */
14142 cp_parser_decl_specifier_seq (parser,
14143 CP_PARSER_FLAGS_NONE,
14145 &declares_class_or_enum);
14146 /* If an error occurred, there's no reason to attempt to parse the
14147 rest of the declaration. */
14148 if (cp_parser_error_occurred (parser))
14150 parser->type_definition_forbidden_message = saved_message;
14154 /* Peek at the next token. */
14155 token = cp_lexer_peek_token (parser->lexer);
14157 /* If the next token is a `)', `,', `=', `>', or `...', then there
14158 is no declarator. However, when variadic templates are enabled,
14159 there may be a declarator following `...'. */
14160 if (token->type == CPP_CLOSE_PAREN
14161 || token->type == CPP_COMMA
14162 || token->type == CPP_EQ
14163 || token->type == CPP_GREATER)
14166 if (parenthesized_p)
14167 *parenthesized_p = false;
14169 /* Otherwise, there should be a declarator. */
14172 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14173 parser->default_arg_ok_p = false;
14175 /* After seeing a decl-specifier-seq, if the next token is not a
14176 "(", there is no possibility that the code is a valid
14177 expression. Therefore, if parsing tentatively, we commit at
14179 if (!parser->in_template_argument_list_p
14180 /* In an expression context, having seen:
14184 we cannot be sure whether we are looking at a
14185 function-type (taking a "char" as a parameter) or a cast
14186 of some object of type "char" to "int". */
14187 && !parser->in_type_id_in_expr_p
14188 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14189 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14190 cp_parser_commit_to_tentative_parse (parser);
14191 /* Parse the declarator. */
14192 declarator_token_start = token;
14193 declarator = cp_parser_declarator (parser,
14194 CP_PARSER_DECLARATOR_EITHER,
14195 /*ctor_dtor_or_conv_p=*/NULL,
14197 /*member_p=*/false);
14198 parser->default_arg_ok_p = saved_default_arg_ok_p;
14199 /* After the declarator, allow more attributes. */
14200 decl_specifiers.attributes
14201 = chainon (decl_specifiers.attributes,
14202 cp_parser_attributes_opt (parser));
14205 /* If the next token is an ellipsis, and we have not seen a
14206 declarator name, and the type of the declarator contains parameter
14207 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14208 a parameter pack expansion expression. Otherwise, leave the
14209 ellipsis for a C-style variadic function. */
14210 token = cp_lexer_peek_token (parser->lexer);
14211 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14213 tree type = decl_specifiers.type;
14215 if (type && DECL_P (type))
14216 type = TREE_TYPE (type);
14219 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14220 && declarator_can_be_parameter_pack (declarator)
14221 && (!declarator || !declarator->parameter_pack_p)
14222 && uses_parameter_packs (type))
14224 /* Consume the `...'. */
14225 cp_lexer_consume_token (parser->lexer);
14226 maybe_warn_variadic_templates ();
14228 /* Build a pack expansion type */
14230 declarator->parameter_pack_p = true;
14232 decl_specifiers.type = make_pack_expansion (type);
14236 /* The restriction on defining new types applies only to the type
14237 of the parameter, not to the default argument. */
14238 parser->type_definition_forbidden_message = saved_message;
14240 /* If the next token is `=', then process a default argument. */
14241 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14243 /* Consume the `='. */
14244 cp_lexer_consume_token (parser->lexer);
14246 /* If we are defining a class, then the tokens that make up the
14247 default argument must be saved and processed later. */
14248 if (!template_parm_p && at_class_scope_p ()
14249 && TYPE_BEING_DEFINED (current_class_type))
14251 unsigned depth = 0;
14252 int maybe_template_id = 0;
14253 cp_token *first_token;
14256 /* Add tokens until we have processed the entire default
14257 argument. We add the range [first_token, token). */
14258 first_token = cp_lexer_peek_token (parser->lexer);
14263 /* Peek at the next token. */
14264 token = cp_lexer_peek_token (parser->lexer);
14265 /* What we do depends on what token we have. */
14266 switch (token->type)
14268 /* In valid code, a default argument must be
14269 immediately followed by a `,' `)', or `...'. */
14271 if (depth == 0 && maybe_template_id)
14273 /* If we've seen a '<', we might be in a
14274 template-argument-list. Until Core issue 325 is
14275 resolved, we don't know how this situation ought
14276 to be handled, so try to DTRT. We check whether
14277 what comes after the comma is a valid parameter
14278 declaration list. If it is, then the comma ends
14279 the default argument; otherwise the default
14280 argument continues. */
14281 bool error = false;
14283 /* Set ITALP so cp_parser_parameter_declaration_list
14284 doesn't decide to commit to this parse. */
14285 bool saved_italp = parser->in_template_argument_list_p;
14286 parser->in_template_argument_list_p = true;
14288 cp_parser_parse_tentatively (parser);
14289 cp_lexer_consume_token (parser->lexer);
14290 cp_parser_parameter_declaration_list (parser, &error);
14291 if (!cp_parser_error_occurred (parser) && !error)
14293 cp_parser_abort_tentative_parse (parser);
14295 parser->in_template_argument_list_p = saved_italp;
14298 case CPP_CLOSE_PAREN:
14300 /* If we run into a non-nested `;', `}', or `]',
14301 then the code is invalid -- but the default
14302 argument is certainly over. */
14303 case CPP_SEMICOLON:
14304 case CPP_CLOSE_BRACE:
14305 case CPP_CLOSE_SQUARE:
14308 /* Update DEPTH, if necessary. */
14309 else if (token->type == CPP_CLOSE_PAREN
14310 || token->type == CPP_CLOSE_BRACE
14311 || token->type == CPP_CLOSE_SQUARE)
14315 case CPP_OPEN_PAREN:
14316 case CPP_OPEN_SQUARE:
14317 case CPP_OPEN_BRACE:
14323 /* This might be the comparison operator, or it might
14324 start a template argument list. */
14325 ++maybe_template_id;
14329 if (cxx_dialect == cxx98)
14331 /* Fall through for C++0x, which treats the `>>'
14332 operator like two `>' tokens in certain
14338 /* This might be an operator, or it might close a
14339 template argument list. But if a previous '<'
14340 started a template argument list, this will have
14341 closed it, so we can't be in one anymore. */
14342 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14343 if (maybe_template_id < 0)
14344 maybe_template_id = 0;
14348 /* If we run out of tokens, issue an error message. */
14350 case CPP_PRAGMA_EOL:
14351 error ("%Hfile ends in default argument", &token->location);
14357 /* In these cases, we should look for template-ids.
14358 For example, if the default argument is
14359 `X<int, double>()', we need to do name lookup to
14360 figure out whether or not `X' is a template; if
14361 so, the `,' does not end the default argument.
14363 That is not yet done. */
14370 /* If we've reached the end, stop. */
14374 /* Add the token to the token block. */
14375 token = cp_lexer_consume_token (parser->lexer);
14378 /* Create a DEFAULT_ARG to represent the unparsed default
14380 default_argument = make_node (DEFAULT_ARG);
14381 DEFARG_TOKENS (default_argument)
14382 = cp_token_cache_new (first_token, token);
14383 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14385 /* Outside of a class definition, we can just parse the
14386 assignment-expression. */
14389 token = cp_lexer_peek_token (parser->lexer);
14391 = cp_parser_default_argument (parser, template_parm_p);
14394 if (!parser->default_arg_ok_p)
14396 if (flag_permissive)
14397 warning (0, "deprecated use of default argument for parameter of non-function");
14400 error ("%Hdefault arguments are only "
14401 "permitted for function parameters",
14403 default_argument = NULL_TREE;
14406 else if ((declarator && declarator->parameter_pack_p)
14407 || (decl_specifiers.type
14408 && PACK_EXPANSION_P (decl_specifiers.type)))
14410 const char* kind = template_parm_p? "template " : "";
14412 /* Find the name of the parameter pack. */
14413 cp_declarator *id_declarator = declarator;
14414 while (id_declarator && id_declarator->kind != cdk_id)
14415 id_declarator = id_declarator->declarator;
14417 if (id_declarator && id_declarator->kind == cdk_id)
14418 error ("%H%sparameter pack %qD cannot have a default argument",
14419 &declarator_token_start->location,
14420 kind, id_declarator->u.id.unqualified_name);
14422 error ("%H%sparameter pack cannot have a default argument",
14423 &declarator_token_start->location, kind);
14425 default_argument = NULL_TREE;
14429 default_argument = NULL_TREE;
14431 return make_parameter_declarator (&decl_specifiers,
14436 /* Parse a default argument and return it.
14438 TEMPLATE_PARM_P is true if this is a default argument for a
14439 non-type template parameter. */
14441 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14443 tree default_argument = NULL_TREE;
14444 bool saved_greater_than_is_operator_p;
14445 bool saved_local_variables_forbidden_p;
14447 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14449 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14450 parser->greater_than_is_operator_p = !template_parm_p;
14451 /* Local variable names (and the `this' keyword) may not
14452 appear in a default argument. */
14453 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14454 parser->local_variables_forbidden_p = true;
14455 /* The default argument expression may cause implicitly
14456 defined member functions to be synthesized, which will
14457 result in garbage collection. We must treat this
14458 situation as if we were within the body of function so as
14459 to avoid collecting live data on the stack. */
14461 /* Parse the assignment-expression. */
14462 if (template_parm_p)
14463 push_deferring_access_checks (dk_no_deferred);
14465 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14466 if (template_parm_p)
14467 pop_deferring_access_checks ();
14468 /* Restore saved state. */
14470 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14471 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14473 return default_argument;
14476 /* Parse a function-body.
14479 compound_statement */
14482 cp_parser_function_body (cp_parser *parser)
14484 cp_parser_compound_statement (parser, NULL, false);
14487 /* Parse a ctor-initializer-opt followed by a function-body. Return
14488 true if a ctor-initializer was present. */
14491 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14494 bool ctor_initializer_p;
14496 /* Begin the function body. */
14497 body = begin_function_body ();
14498 /* Parse the optional ctor-initializer. */
14499 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14500 /* Parse the function-body. */
14501 cp_parser_function_body (parser);
14502 /* Finish the function body. */
14503 finish_function_body (body);
14505 return ctor_initializer_p;
14508 /* Parse an initializer.
14511 = initializer-clause
14512 ( expression-list )
14514 Returns an expression representing the initializer. If no
14515 initializer is present, NULL_TREE is returned.
14517 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14518 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14519 set to TRUE if there is no initializer present. If there is an
14520 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14521 is set to true; otherwise it is set to false. */
14524 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14525 bool* non_constant_p)
14530 /* Peek at the next token. */
14531 token = cp_lexer_peek_token (parser->lexer);
14533 /* Let our caller know whether or not this initializer was
14535 *is_direct_init = (token->type != CPP_EQ);
14536 /* Assume that the initializer is constant. */
14537 *non_constant_p = false;
14539 if (token->type == CPP_EQ)
14541 /* Consume the `='. */
14542 cp_lexer_consume_token (parser->lexer);
14543 /* Parse the initializer-clause. */
14544 init = cp_parser_initializer_clause (parser, non_constant_p);
14546 else if (token->type == CPP_OPEN_PAREN)
14547 init = cp_parser_parenthesized_expression_list (parser, false,
14549 /*allow_expansion_p=*/true,
14551 else if (token->type == CPP_OPEN_BRACE)
14553 maybe_warn_cpp0x ("extended initializer lists");
14554 init = cp_parser_braced_list (parser, non_constant_p);
14555 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14559 /* Anything else is an error. */
14560 cp_parser_error (parser, "expected initializer");
14561 init = error_mark_node;
14567 /* Parse an initializer-clause.
14569 initializer-clause:
14570 assignment-expression
14573 Returns an expression representing the initializer.
14575 If the `assignment-expression' production is used the value
14576 returned is simply a representation for the expression.
14578 Otherwise, calls cp_parser_braced_list. */
14581 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14585 /* Assume the expression is constant. */
14586 *non_constant_p = false;
14588 /* If it is not a `{', then we are looking at an
14589 assignment-expression. */
14590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14593 = cp_parser_constant_expression (parser,
14594 /*allow_non_constant_p=*/true,
14596 if (!*non_constant_p)
14597 initializer = fold_non_dependent_expr (initializer);
14600 initializer = cp_parser_braced_list (parser, non_constant_p);
14602 return initializer;
14605 /* Parse a brace-enclosed initializer list.
14608 { initializer-list , [opt] }
14611 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14612 the elements of the initializer-list (or NULL, if the last
14613 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14614 NULL_TREE. There is no way to detect whether or not the optional
14615 trailing `,' was provided. NON_CONSTANT_P is as for
14616 cp_parser_initializer. */
14619 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14623 /* Consume the `{' token. */
14624 cp_lexer_consume_token (parser->lexer);
14625 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14626 initializer = make_node (CONSTRUCTOR);
14627 /* If it's not a `}', then there is a non-trivial initializer. */
14628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14630 /* Parse the initializer list. */
14631 CONSTRUCTOR_ELTS (initializer)
14632 = cp_parser_initializer_list (parser, non_constant_p);
14633 /* A trailing `,' token is allowed. */
14634 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14635 cp_lexer_consume_token (parser->lexer);
14637 /* Now, there should be a trailing `}'. */
14638 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14639 TREE_TYPE (initializer) = init_list_type_node;
14640 return initializer;
14643 /* Parse an initializer-list.
14646 initializer-clause ... [opt]
14647 initializer-list , initializer-clause ... [opt]
14652 identifier : initializer-clause
14653 initializer-list, identifier : initializer-clause
14655 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14656 for the initializer. If the INDEX of the elt is non-NULL, it is the
14657 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14658 as for cp_parser_initializer. */
14660 static VEC(constructor_elt,gc) *
14661 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14663 VEC(constructor_elt,gc) *v = NULL;
14665 /* Assume all of the expressions are constant. */
14666 *non_constant_p = false;
14668 /* Parse the rest of the list. */
14674 bool clause_non_constant_p;
14676 /* If the next token is an identifier and the following one is a
14677 colon, we are looking at the GNU designated-initializer
14679 if (cp_parser_allow_gnu_extensions_p (parser)
14680 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14681 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14683 /* Warn the user that they are using an extension. */
14684 pedwarn (input_location, OPT_pedantic,
14685 "ISO C++ does not allow designated initializers");
14686 /* Consume the identifier. */
14687 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14688 /* Consume the `:'. */
14689 cp_lexer_consume_token (parser->lexer);
14692 identifier = NULL_TREE;
14694 /* Parse the initializer. */
14695 initializer = cp_parser_initializer_clause (parser,
14696 &clause_non_constant_p);
14697 /* If any clause is non-constant, so is the entire initializer. */
14698 if (clause_non_constant_p)
14699 *non_constant_p = true;
14701 /* If we have an ellipsis, this is an initializer pack
14703 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14705 /* Consume the `...'. */
14706 cp_lexer_consume_token (parser->lexer);
14708 /* Turn the initializer into an initializer expansion. */
14709 initializer = make_pack_expansion (initializer);
14712 /* Add it to the vector. */
14713 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14715 /* If the next token is not a comma, we have reached the end of
14717 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14720 /* Peek at the next token. */
14721 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14722 /* If the next token is a `}', then we're still done. An
14723 initializer-clause can have a trailing `,' after the
14724 initializer-list and before the closing `}'. */
14725 if (token->type == CPP_CLOSE_BRACE)
14728 /* Consume the `,' token. */
14729 cp_lexer_consume_token (parser->lexer);
14735 /* Classes [gram.class] */
14737 /* Parse a class-name.
14743 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14744 to indicate that names looked up in dependent types should be
14745 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14746 keyword has been used to indicate that the name that appears next
14747 is a template. TAG_TYPE indicates the explicit tag given before
14748 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14749 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14750 is the class being defined in a class-head.
14752 Returns the TYPE_DECL representing the class. */
14755 cp_parser_class_name (cp_parser *parser,
14756 bool typename_keyword_p,
14757 bool template_keyword_p,
14758 enum tag_types tag_type,
14759 bool check_dependency_p,
14761 bool is_declaration)
14767 tree identifier = NULL_TREE;
14769 /* All class-names start with an identifier. */
14770 token = cp_lexer_peek_token (parser->lexer);
14771 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14773 cp_parser_error (parser, "expected class-name");
14774 return error_mark_node;
14777 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14778 to a template-id, so we save it here. */
14779 scope = parser->scope;
14780 if (scope == error_mark_node)
14781 return error_mark_node;
14783 /* Any name names a type if we're following the `typename' keyword
14784 in a qualified name where the enclosing scope is type-dependent. */
14785 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14786 && dependent_type_p (scope));
14787 /* Handle the common case (an identifier, but not a template-id)
14789 if (token->type == CPP_NAME
14790 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14792 cp_token *identifier_token;
14795 /* Look for the identifier. */
14796 identifier_token = cp_lexer_peek_token (parser->lexer);
14797 ambiguous_p = identifier_token->ambiguous_p;
14798 identifier = cp_parser_identifier (parser);
14799 /* If the next token isn't an identifier, we are certainly not
14800 looking at a class-name. */
14801 if (identifier == error_mark_node)
14802 decl = error_mark_node;
14803 /* If we know this is a type-name, there's no need to look it
14805 else if (typename_p)
14809 tree ambiguous_decls;
14810 /* If we already know that this lookup is ambiguous, then
14811 we've already issued an error message; there's no reason
14815 cp_parser_simulate_error (parser);
14816 return error_mark_node;
14818 /* If the next token is a `::', then the name must be a type
14821 [basic.lookup.qual]
14823 During the lookup for a name preceding the :: scope
14824 resolution operator, object, function, and enumerator
14825 names are ignored. */
14826 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14827 tag_type = typename_type;
14828 /* Look up the name. */
14829 decl = cp_parser_lookup_name (parser, identifier,
14831 /*is_template=*/false,
14832 /*is_namespace=*/false,
14833 check_dependency_p,
14835 identifier_token->location);
14836 if (ambiguous_decls)
14838 error ("%Hreference to %qD is ambiguous",
14839 &identifier_token->location, identifier);
14840 print_candidates (ambiguous_decls);
14841 if (cp_parser_parsing_tentatively (parser))
14843 identifier_token->ambiguous_p = true;
14844 cp_parser_simulate_error (parser);
14846 return error_mark_node;
14852 /* Try a template-id. */
14853 decl = cp_parser_template_id (parser, template_keyword_p,
14854 check_dependency_p,
14856 if (decl == error_mark_node)
14857 return error_mark_node;
14860 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14862 /* If this is a typename, create a TYPENAME_TYPE. */
14863 if (typename_p && decl != error_mark_node)
14865 decl = make_typename_type (scope, decl, typename_type,
14866 /*complain=*/tf_error);
14867 if (decl != error_mark_node)
14868 decl = TYPE_NAME (decl);
14871 /* Check to see that it is really the name of a class. */
14872 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14873 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14874 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14875 /* Situations like this:
14877 template <typename T> struct A {
14878 typename T::template X<int>::I i;
14881 are problematic. Is `T::template X<int>' a class-name? The
14882 standard does not seem to be definitive, but there is no other
14883 valid interpretation of the following `::'. Therefore, those
14884 names are considered class-names. */
14886 decl = make_typename_type (scope, decl, tag_type, tf_error);
14887 if (decl != error_mark_node)
14888 decl = TYPE_NAME (decl);
14890 else if (TREE_CODE (decl) != TYPE_DECL
14891 || TREE_TYPE (decl) == error_mark_node
14892 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14893 decl = error_mark_node;
14895 if (decl == error_mark_node)
14896 cp_parser_error (parser, "expected class-name");
14897 else if (identifier && !parser->scope)
14898 maybe_note_name_used_in_class (identifier, decl);
14903 /* Parse a class-specifier.
14906 class-head { member-specification [opt] }
14908 Returns the TREE_TYPE representing the class. */
14911 cp_parser_class_specifier (cp_parser* parser)
14915 tree attributes = NULL_TREE;
14916 int has_trailing_semicolon;
14917 bool nested_name_specifier_p;
14918 unsigned saved_num_template_parameter_lists;
14919 bool saved_in_function_body;
14920 bool saved_in_unbraced_linkage_specification_p;
14921 tree old_scope = NULL_TREE;
14922 tree scope = NULL_TREE;
14925 push_deferring_access_checks (dk_no_deferred);
14927 /* Parse the class-head. */
14928 type = cp_parser_class_head (parser,
14929 &nested_name_specifier_p,
14932 /* If the class-head was a semantic disaster, skip the entire body
14936 cp_parser_skip_to_end_of_block_or_statement (parser);
14937 pop_deferring_access_checks ();
14938 return error_mark_node;
14941 /* Look for the `{'. */
14942 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14944 pop_deferring_access_checks ();
14945 return error_mark_node;
14948 /* Process the base classes. If they're invalid, skip the
14949 entire class body. */
14950 if (!xref_basetypes (type, bases))
14952 /* Consuming the closing brace yields better error messages
14954 if (cp_parser_skip_to_closing_brace (parser))
14955 cp_lexer_consume_token (parser->lexer);
14956 pop_deferring_access_checks ();
14957 return error_mark_node;
14960 /* Issue an error message if type-definitions are forbidden here. */
14961 cp_parser_check_type_definition (parser);
14962 /* Remember that we are defining one more class. */
14963 ++parser->num_classes_being_defined;
14964 /* Inside the class, surrounding template-parameter-lists do not
14966 saved_num_template_parameter_lists
14967 = parser->num_template_parameter_lists;
14968 parser->num_template_parameter_lists = 0;
14969 /* We are not in a function body. */
14970 saved_in_function_body = parser->in_function_body;
14971 parser->in_function_body = false;
14972 /* We are not immediately inside an extern "lang" block. */
14973 saved_in_unbraced_linkage_specification_p
14974 = parser->in_unbraced_linkage_specification_p;
14975 parser->in_unbraced_linkage_specification_p = false;
14977 /* Start the class. */
14978 if (nested_name_specifier_p)
14980 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14981 old_scope = push_inner_scope (scope);
14983 type = begin_class_definition (type, attributes);
14985 if (type == error_mark_node)
14986 /* If the type is erroneous, skip the entire body of the class. */
14987 cp_parser_skip_to_closing_brace (parser);
14989 /* Parse the member-specification. */
14990 cp_parser_member_specification_opt (parser);
14992 /* Look for the trailing `}'. */
14993 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14994 /* We get better error messages by noticing a common problem: a
14995 missing trailing `;'. */
14996 token = cp_lexer_peek_token (parser->lexer);
14997 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14998 /* Look for trailing attributes to apply to this class. */
14999 if (cp_parser_allow_gnu_extensions_p (parser))
15000 attributes = cp_parser_attributes_opt (parser);
15001 if (type != error_mark_node)
15002 type = finish_struct (type, attributes);
15003 if (nested_name_specifier_p)
15004 pop_inner_scope (old_scope, scope);
15005 /* If this class is not itself within the scope of another class,
15006 then we need to parse the bodies of all of the queued function
15007 definitions. Note that the queued functions defined in a class
15008 are not always processed immediately following the
15009 class-specifier for that class. Consider:
15012 struct B { void f() { sizeof (A); } };
15015 If `f' were processed before the processing of `A' were
15016 completed, there would be no way to compute the size of `A'.
15017 Note that the nesting we are interested in here is lexical --
15018 not the semantic nesting given by TYPE_CONTEXT. In particular,
15021 struct A { struct B; };
15022 struct A::B { void f() { } };
15024 there is no need to delay the parsing of `A::B::f'. */
15025 if (--parser->num_classes_being_defined == 0)
15029 tree class_type = NULL_TREE;
15030 tree pushed_scope = NULL_TREE;
15032 /* In a first pass, parse default arguments to the functions.
15033 Then, in a second pass, parse the bodies of the functions.
15034 This two-phased approach handles cases like:
15042 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15043 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15044 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15045 TREE_PURPOSE (parser->unparsed_functions_queues)
15046 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15048 fn = TREE_VALUE (queue_entry);
15049 /* If there are default arguments that have not yet been processed,
15050 take care of them now. */
15051 if (class_type != TREE_PURPOSE (queue_entry))
15054 pop_scope (pushed_scope);
15055 class_type = TREE_PURPOSE (queue_entry);
15056 pushed_scope = push_scope (class_type);
15058 /* Make sure that any template parameters are in scope. */
15059 maybe_begin_member_template_processing (fn);
15060 /* Parse the default argument expressions. */
15061 cp_parser_late_parsing_default_args (parser, fn);
15062 /* Remove any template parameters from the symbol table. */
15063 maybe_end_member_template_processing ();
15066 pop_scope (pushed_scope);
15067 /* Now parse the body of the functions. */
15068 for (TREE_VALUE (parser->unparsed_functions_queues)
15069 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15070 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15071 TREE_VALUE (parser->unparsed_functions_queues)
15072 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15074 /* Figure out which function we need to process. */
15075 fn = TREE_VALUE (queue_entry);
15076 /* Parse the function. */
15077 cp_parser_late_parsing_for_member (parser, fn);
15081 /* Put back any saved access checks. */
15082 pop_deferring_access_checks ();
15084 /* Restore saved state. */
15085 parser->in_function_body = saved_in_function_body;
15086 parser->num_template_parameter_lists
15087 = saved_num_template_parameter_lists;
15088 parser->in_unbraced_linkage_specification_p
15089 = saved_in_unbraced_linkage_specification_p;
15094 /* Parse a class-head.
15097 class-key identifier [opt] base-clause [opt]
15098 class-key nested-name-specifier identifier base-clause [opt]
15099 class-key nested-name-specifier [opt] template-id
15103 class-key attributes identifier [opt] base-clause [opt]
15104 class-key attributes nested-name-specifier identifier base-clause [opt]
15105 class-key attributes nested-name-specifier [opt] template-id
15108 Upon return BASES is initialized to the list of base classes (or
15109 NULL, if there are none) in the same form returned by
15110 cp_parser_base_clause.
15112 Returns the TYPE of the indicated class. Sets
15113 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15114 involving a nested-name-specifier was used, and FALSE otherwise.
15116 Returns error_mark_node if this is not a class-head.
15118 Returns NULL_TREE if the class-head is syntactically valid, but
15119 semantically invalid in a way that means we should skip the entire
15120 body of the class. */
15123 cp_parser_class_head (cp_parser* parser,
15124 bool* nested_name_specifier_p,
15125 tree *attributes_p,
15128 tree nested_name_specifier;
15129 enum tag_types class_key;
15130 tree id = NULL_TREE;
15131 tree type = NULL_TREE;
15133 bool template_id_p = false;
15134 bool qualified_p = false;
15135 bool invalid_nested_name_p = false;
15136 bool invalid_explicit_specialization_p = false;
15137 tree pushed_scope = NULL_TREE;
15138 unsigned num_templates;
15139 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15140 /* Assume no nested-name-specifier will be present. */
15141 *nested_name_specifier_p = false;
15142 /* Assume no template parameter lists will be used in defining the
15146 *bases = NULL_TREE;
15148 /* Look for the class-key. */
15149 class_key = cp_parser_class_key (parser);
15150 if (class_key == none_type)
15151 return error_mark_node;
15153 /* Parse the attributes. */
15154 attributes = cp_parser_attributes_opt (parser);
15156 /* If the next token is `::', that is invalid -- but sometimes
15157 people do try to write:
15161 Handle this gracefully by accepting the extra qualifier, and then
15162 issuing an error about it later if this really is a
15163 class-head. If it turns out just to be an elaborated type
15164 specifier, remain silent. */
15165 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15166 qualified_p = true;
15168 push_deferring_access_checks (dk_no_check);
15170 /* Determine the name of the class. Begin by looking for an
15171 optional nested-name-specifier. */
15172 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15173 nested_name_specifier
15174 = cp_parser_nested_name_specifier_opt (parser,
15175 /*typename_keyword_p=*/false,
15176 /*check_dependency_p=*/false,
15178 /*is_declaration=*/false);
15179 /* If there was a nested-name-specifier, then there *must* be an
15181 if (nested_name_specifier)
15183 type_start_token = cp_lexer_peek_token (parser->lexer);
15184 /* Although the grammar says `identifier', it really means
15185 `class-name' or `template-name'. You are only allowed to
15186 define a class that has already been declared with this
15189 The proposed resolution for Core Issue 180 says that wherever
15190 you see `class T::X' you should treat `X' as a type-name.
15192 It is OK to define an inaccessible class; for example:
15194 class A { class B; };
15197 We do not know if we will see a class-name, or a
15198 template-name. We look for a class-name first, in case the
15199 class-name is a template-id; if we looked for the
15200 template-name first we would stop after the template-name. */
15201 cp_parser_parse_tentatively (parser);
15202 type = cp_parser_class_name (parser,
15203 /*typename_keyword_p=*/false,
15204 /*template_keyword_p=*/false,
15206 /*check_dependency_p=*/false,
15207 /*class_head_p=*/true,
15208 /*is_declaration=*/false);
15209 /* If that didn't work, ignore the nested-name-specifier. */
15210 if (!cp_parser_parse_definitely (parser))
15212 invalid_nested_name_p = true;
15213 type_start_token = cp_lexer_peek_token (parser->lexer);
15214 id = cp_parser_identifier (parser);
15215 if (id == error_mark_node)
15218 /* If we could not find a corresponding TYPE, treat this
15219 declaration like an unqualified declaration. */
15220 if (type == error_mark_node)
15221 nested_name_specifier = NULL_TREE;
15222 /* Otherwise, count the number of templates used in TYPE and its
15223 containing scopes. */
15228 for (scope = TREE_TYPE (type);
15229 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15230 scope = (TYPE_P (scope)
15231 ? TYPE_CONTEXT (scope)
15232 : DECL_CONTEXT (scope)))
15234 && CLASS_TYPE_P (scope)
15235 && CLASSTYPE_TEMPLATE_INFO (scope)
15236 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15237 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15241 /* Otherwise, the identifier is optional. */
15244 /* We don't know whether what comes next is a template-id,
15245 an identifier, or nothing at all. */
15246 cp_parser_parse_tentatively (parser);
15247 /* Check for a template-id. */
15248 type_start_token = cp_lexer_peek_token (parser->lexer);
15249 id = cp_parser_template_id (parser,
15250 /*template_keyword_p=*/false,
15251 /*check_dependency_p=*/true,
15252 /*is_declaration=*/true);
15253 /* If that didn't work, it could still be an identifier. */
15254 if (!cp_parser_parse_definitely (parser))
15256 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15258 type_start_token = cp_lexer_peek_token (parser->lexer);
15259 id = cp_parser_identifier (parser);
15266 template_id_p = true;
15271 pop_deferring_access_checks ();
15274 cp_parser_check_for_invalid_template_id (parser, id,
15275 type_start_token->location);
15277 /* If it's not a `:' or a `{' then we can't really be looking at a
15278 class-head, since a class-head only appears as part of a
15279 class-specifier. We have to detect this situation before calling
15280 xref_tag, since that has irreversible side-effects. */
15281 if (!cp_parser_next_token_starts_class_definition_p (parser))
15283 cp_parser_error (parser, "expected %<{%> or %<:%>");
15284 return error_mark_node;
15287 /* At this point, we're going ahead with the class-specifier, even
15288 if some other problem occurs. */
15289 cp_parser_commit_to_tentative_parse (parser);
15290 /* Issue the error about the overly-qualified name now. */
15293 cp_parser_error (parser,
15294 "global qualification of class name is invalid");
15295 return error_mark_node;
15297 else if (invalid_nested_name_p)
15299 cp_parser_error (parser,
15300 "qualified name does not name a class");
15301 return error_mark_node;
15303 else if (nested_name_specifier)
15307 /* Reject typedef-names in class heads. */
15308 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15310 error ("%Hinvalid class name in declaration of %qD",
15311 &type_start_token->location, type);
15316 /* Figure out in what scope the declaration is being placed. */
15317 scope = current_scope ();
15318 /* If that scope does not contain the scope in which the
15319 class was originally declared, the program is invalid. */
15320 if (scope && !is_ancestor (scope, nested_name_specifier))
15322 if (at_namespace_scope_p ())
15323 error ("%Hdeclaration of %qD in namespace %qD which does not "
15325 &type_start_token->location,
15326 type, scope, nested_name_specifier);
15328 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15329 &type_start_token->location,
15330 type, scope, nested_name_specifier);
15336 A declarator-id shall not be qualified except for the
15337 definition of a ... nested class outside of its class
15338 ... [or] the definition or explicit instantiation of a
15339 class member of a namespace outside of its namespace. */
15340 if (scope == nested_name_specifier)
15342 permerror (input_location, "%Hextra qualification not allowed",
15343 &nested_name_specifier_token_start->location);
15344 nested_name_specifier = NULL_TREE;
15348 /* An explicit-specialization must be preceded by "template <>". If
15349 it is not, try to recover gracefully. */
15350 if (at_namespace_scope_p ()
15351 && parser->num_template_parameter_lists == 0
15354 error ("%Han explicit specialization must be preceded by %<template <>%>",
15355 &type_start_token->location);
15356 invalid_explicit_specialization_p = true;
15357 /* Take the same action that would have been taken by
15358 cp_parser_explicit_specialization. */
15359 ++parser->num_template_parameter_lists;
15360 begin_specialization ();
15362 /* There must be no "return" statements between this point and the
15363 end of this function; set "type "to the correct return value and
15364 use "goto done;" to return. */
15365 /* Make sure that the right number of template parameters were
15367 if (!cp_parser_check_template_parameters (parser, num_templates,
15368 type_start_token->location))
15370 /* If something went wrong, there is no point in even trying to
15371 process the class-definition. */
15376 /* Look up the type. */
15379 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15380 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15381 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15383 error ("%Hfunction template %qD redeclared as a class template",
15384 &type_start_token->location, id);
15385 type = error_mark_node;
15389 type = TREE_TYPE (id);
15390 type = maybe_process_partial_specialization (type);
15392 if (nested_name_specifier)
15393 pushed_scope = push_scope (nested_name_specifier);
15395 else if (nested_name_specifier)
15401 template <typename T> struct S { struct T };
15402 template <typename T> struct S<T>::T { };
15404 we will get a TYPENAME_TYPE when processing the definition of
15405 `S::T'. We need to resolve it to the actual type before we
15406 try to define it. */
15407 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15409 class_type = resolve_typename_type (TREE_TYPE (type),
15410 /*only_current_p=*/false);
15411 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15412 type = TYPE_NAME (class_type);
15415 cp_parser_error (parser, "could not resolve typename type");
15416 type = error_mark_node;
15420 if (maybe_process_partial_specialization (TREE_TYPE (type))
15421 == error_mark_node)
15427 class_type = current_class_type;
15428 /* Enter the scope indicated by the nested-name-specifier. */
15429 pushed_scope = push_scope (nested_name_specifier);
15430 /* Get the canonical version of this type. */
15431 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15432 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15433 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15435 type = push_template_decl (type);
15436 if (type == error_mark_node)
15443 type = TREE_TYPE (type);
15444 *nested_name_specifier_p = true;
15446 else /* The name is not a nested name. */
15448 /* If the class was unnamed, create a dummy name. */
15450 id = make_anon_name ();
15451 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15452 parser->num_template_parameter_lists);
15455 /* Indicate whether this class was declared as a `class' or as a
15457 if (TREE_CODE (type) == RECORD_TYPE)
15458 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15459 cp_parser_check_class_key (class_key, type);
15461 /* If this type was already complete, and we see another definition,
15462 that's an error. */
15463 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15465 error ("%Hredefinition of %q#T",
15466 &type_start_token->location, type);
15467 error ("%Hprevious definition of %q+#T",
15468 &type_start_token->location, type);
15472 else if (type == error_mark_node)
15475 /* We will have entered the scope containing the class; the names of
15476 base classes should be looked up in that context. For example:
15478 struct A { struct B {}; struct C; };
15479 struct A::C : B {};
15483 /* Get the list of base-classes, if there is one. */
15484 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15485 *bases = cp_parser_base_clause (parser);
15488 /* Leave the scope given by the nested-name-specifier. We will
15489 enter the class scope itself while processing the members. */
15491 pop_scope (pushed_scope);
15493 if (invalid_explicit_specialization_p)
15495 end_specialization ();
15496 --parser->num_template_parameter_lists;
15498 *attributes_p = attributes;
15502 /* Parse a class-key.
15509 Returns the kind of class-key specified, or none_type to indicate
15512 static enum tag_types
15513 cp_parser_class_key (cp_parser* parser)
15516 enum tag_types tag_type;
15518 /* Look for the class-key. */
15519 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15523 /* Check to see if the TOKEN is a class-key. */
15524 tag_type = cp_parser_token_is_class_key (token);
15526 cp_parser_error (parser, "expected class-key");
15530 /* Parse an (optional) member-specification.
15532 member-specification:
15533 member-declaration member-specification [opt]
15534 access-specifier : member-specification [opt] */
15537 cp_parser_member_specification_opt (cp_parser* parser)
15544 /* Peek at the next token. */
15545 token = cp_lexer_peek_token (parser->lexer);
15546 /* If it's a `}', or EOF then we've seen all the members. */
15547 if (token->type == CPP_CLOSE_BRACE
15548 || token->type == CPP_EOF
15549 || token->type == CPP_PRAGMA_EOL)
15552 /* See if this token is a keyword. */
15553 keyword = token->keyword;
15557 case RID_PROTECTED:
15559 /* Consume the access-specifier. */
15560 cp_lexer_consume_token (parser->lexer);
15561 /* Remember which access-specifier is active. */
15562 current_access_specifier = token->u.value;
15563 /* Look for the `:'. */
15564 cp_parser_require (parser, CPP_COLON, "%<:%>");
15568 /* Accept #pragmas at class scope. */
15569 if (token->type == CPP_PRAGMA)
15571 cp_parser_pragma (parser, pragma_external);
15575 /* Otherwise, the next construction must be a
15576 member-declaration. */
15577 cp_parser_member_declaration (parser);
15582 /* Parse a member-declaration.
15584 member-declaration:
15585 decl-specifier-seq [opt] member-declarator-list [opt] ;
15586 function-definition ; [opt]
15587 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15589 template-declaration
15591 member-declarator-list:
15593 member-declarator-list , member-declarator
15596 declarator pure-specifier [opt]
15597 declarator constant-initializer [opt]
15598 identifier [opt] : constant-expression
15602 member-declaration:
15603 __extension__ member-declaration
15606 declarator attributes [opt] pure-specifier [opt]
15607 declarator attributes [opt] constant-initializer [opt]
15608 identifier [opt] attributes [opt] : constant-expression
15612 member-declaration:
15613 static_assert-declaration */
15616 cp_parser_member_declaration (cp_parser* parser)
15618 cp_decl_specifier_seq decl_specifiers;
15619 tree prefix_attributes;
15621 int declares_class_or_enum;
15623 cp_token *token = NULL;
15624 cp_token *decl_spec_token_start = NULL;
15625 cp_token *initializer_token_start = NULL;
15626 int saved_pedantic;
15628 /* Check for the `__extension__' keyword. */
15629 if (cp_parser_extension_opt (parser, &saved_pedantic))
15632 cp_parser_member_declaration (parser);
15633 /* Restore the old value of the PEDANTIC flag. */
15634 pedantic = saved_pedantic;
15639 /* Check for a template-declaration. */
15640 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15642 /* An explicit specialization here is an error condition, and we
15643 expect the specialization handler to detect and report this. */
15644 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15645 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15646 cp_parser_explicit_specialization (parser);
15648 cp_parser_template_declaration (parser, /*member_p=*/true);
15653 /* Check for a using-declaration. */
15654 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15656 /* Parse the using-declaration. */
15657 cp_parser_using_declaration (parser,
15658 /*access_declaration_p=*/false);
15662 /* Check for @defs. */
15663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15666 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15667 ivar = ivar_chains;
15671 ivar = TREE_CHAIN (member);
15672 TREE_CHAIN (member) = NULL_TREE;
15673 finish_member_declaration (member);
15678 /* If the next token is `static_assert' we have a static assertion. */
15679 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15681 cp_parser_static_assert (parser, /*member_p=*/true);
15685 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15688 /* Parse the decl-specifier-seq. */
15689 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15690 cp_parser_decl_specifier_seq (parser,
15691 CP_PARSER_FLAGS_OPTIONAL,
15693 &declares_class_or_enum);
15694 prefix_attributes = decl_specifiers.attributes;
15695 decl_specifiers.attributes = NULL_TREE;
15696 /* Check for an invalid type-name. */
15697 if (!decl_specifiers.type
15698 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15700 /* If there is no declarator, then the decl-specifier-seq should
15702 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15704 /* If there was no decl-specifier-seq, and the next token is a
15705 `;', then we have something like:
15711 Each member-declaration shall declare at least one member
15712 name of the class. */
15713 if (!decl_specifiers.any_specifiers_p)
15715 cp_token *token = cp_lexer_peek_token (parser->lexer);
15716 if (!in_system_header_at (token->location))
15717 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15723 /* See if this declaration is a friend. */
15724 friend_p = cp_parser_friend_p (&decl_specifiers);
15725 /* If there were decl-specifiers, check to see if there was
15726 a class-declaration. */
15727 type = check_tag_decl (&decl_specifiers);
15728 /* Nested classes have already been added to the class, but
15729 a `friend' needs to be explicitly registered. */
15732 /* If the `friend' keyword was present, the friend must
15733 be introduced with a class-key. */
15734 if (!declares_class_or_enum)
15735 error ("%Ha class-key must be used when declaring a friend",
15736 &decl_spec_token_start->location);
15739 template <typename T> struct A {
15740 friend struct A<T>::B;
15743 A<T>::B will be represented by a TYPENAME_TYPE, and
15744 therefore not recognized by check_tag_decl. */
15746 && decl_specifiers.type
15747 && TYPE_P (decl_specifiers.type))
15748 type = decl_specifiers.type;
15749 if (!type || !TYPE_P (type))
15750 error ("%Hfriend declaration does not name a class or "
15751 "function", &decl_spec_token_start->location);
15753 make_friend_class (current_class_type, type,
15754 /*complain=*/true);
15756 /* If there is no TYPE, an error message will already have
15758 else if (!type || type == error_mark_node)
15760 /* An anonymous aggregate has to be handled specially; such
15761 a declaration really declares a data member (with a
15762 particular type), as opposed to a nested class. */
15763 else if (ANON_AGGR_TYPE_P (type))
15765 /* Remove constructors and such from TYPE, now that we
15766 know it is an anonymous aggregate. */
15767 fixup_anonymous_aggr (type);
15768 /* And make the corresponding data member. */
15769 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15770 /* Add it to the class. */
15771 finish_member_declaration (decl);
15774 cp_parser_check_access_in_redeclaration
15776 decl_spec_token_start->location);
15781 /* See if these declarations will be friends. */
15782 friend_p = cp_parser_friend_p (&decl_specifiers);
15784 /* Keep going until we hit the `;' at the end of the
15786 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15788 tree attributes = NULL_TREE;
15789 tree first_attribute;
15791 /* Peek at the next token. */
15792 token = cp_lexer_peek_token (parser->lexer);
15794 /* Check for a bitfield declaration. */
15795 if (token->type == CPP_COLON
15796 || (token->type == CPP_NAME
15797 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15803 /* Get the name of the bitfield. Note that we cannot just
15804 check TOKEN here because it may have been invalidated by
15805 the call to cp_lexer_peek_nth_token above. */
15806 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15807 identifier = cp_parser_identifier (parser);
15809 identifier = NULL_TREE;
15811 /* Consume the `:' token. */
15812 cp_lexer_consume_token (parser->lexer);
15813 /* Get the width of the bitfield. */
15815 = cp_parser_constant_expression (parser,
15816 /*allow_non_constant=*/false,
15819 /* Look for attributes that apply to the bitfield. */
15820 attributes = cp_parser_attributes_opt (parser);
15821 /* Remember which attributes are prefix attributes and
15823 first_attribute = attributes;
15824 /* Combine the attributes. */
15825 attributes = chainon (prefix_attributes, attributes);
15827 /* Create the bitfield declaration. */
15828 decl = grokbitfield (identifier
15829 ? make_id_declarator (NULL_TREE,
15839 cp_declarator *declarator;
15841 tree asm_specification;
15842 int ctor_dtor_or_conv_p;
15844 /* Parse the declarator. */
15846 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15847 &ctor_dtor_or_conv_p,
15848 /*parenthesized_p=*/NULL,
15849 /*member_p=*/true);
15851 /* If something went wrong parsing the declarator, make sure
15852 that we at least consume some tokens. */
15853 if (declarator == cp_error_declarator)
15855 /* Skip to the end of the statement. */
15856 cp_parser_skip_to_end_of_statement (parser);
15857 /* If the next token is not a semicolon, that is
15858 probably because we just skipped over the body of
15859 a function. So, we consume a semicolon if
15860 present, but do not issue an error message if it
15862 if (cp_lexer_next_token_is (parser->lexer,
15864 cp_lexer_consume_token (parser->lexer);
15868 if (declares_class_or_enum & 2)
15869 cp_parser_check_for_definition_in_return_type
15870 (declarator, decl_specifiers.type,
15871 decl_specifiers.type_location);
15873 /* Look for an asm-specification. */
15874 asm_specification = cp_parser_asm_specification_opt (parser);
15875 /* Look for attributes that apply to the declaration. */
15876 attributes = cp_parser_attributes_opt (parser);
15877 /* Remember which attributes are prefix attributes and
15879 first_attribute = attributes;
15880 /* Combine the attributes. */
15881 attributes = chainon (prefix_attributes, attributes);
15883 /* If it's an `=', then we have a constant-initializer or a
15884 pure-specifier. It is not correct to parse the
15885 initializer before registering the member declaration
15886 since the member declaration should be in scope while
15887 its initializer is processed. However, the rest of the
15888 front end does not yet provide an interface that allows
15889 us to handle this correctly. */
15890 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15894 A pure-specifier shall be used only in the declaration of
15895 a virtual function.
15897 A member-declarator can contain a constant-initializer
15898 only if it declares a static member of integral or
15901 Therefore, if the DECLARATOR is for a function, we look
15902 for a pure-specifier; otherwise, we look for a
15903 constant-initializer. When we call `grokfield', it will
15904 perform more stringent semantics checks. */
15905 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15906 if (function_declarator_p (declarator))
15907 initializer = cp_parser_pure_specifier (parser);
15909 /* Parse the initializer. */
15910 initializer = cp_parser_constant_initializer (parser);
15912 /* Otherwise, there is no initializer. */
15914 initializer = NULL_TREE;
15916 /* See if we are probably looking at a function
15917 definition. We are certainly not looking at a
15918 member-declarator. Calling `grokfield' has
15919 side-effects, so we must not do it unless we are sure
15920 that we are looking at a member-declarator. */
15921 if (cp_parser_token_starts_function_definition_p
15922 (cp_lexer_peek_token (parser->lexer)))
15924 /* The grammar does not allow a pure-specifier to be
15925 used when a member function is defined. (It is
15926 possible that this fact is an oversight in the
15927 standard, since a pure function may be defined
15928 outside of the class-specifier. */
15930 error ("%Hpure-specifier on function-definition",
15931 &initializer_token_start->location);
15932 decl = cp_parser_save_member_function_body (parser,
15936 /* If the member was not a friend, declare it here. */
15938 finish_member_declaration (decl);
15939 /* Peek at the next token. */
15940 token = cp_lexer_peek_token (parser->lexer);
15941 /* If the next token is a semicolon, consume it. */
15942 if (token->type == CPP_SEMICOLON)
15943 cp_lexer_consume_token (parser->lexer);
15947 if (declarator->kind == cdk_function)
15948 declarator->id_loc = token->location;
15949 /* Create the declaration. */
15950 decl = grokfield (declarator, &decl_specifiers,
15951 initializer, /*init_const_expr_p=*/true,
15956 /* Reset PREFIX_ATTRIBUTES. */
15957 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15958 attributes = TREE_CHAIN (attributes);
15960 TREE_CHAIN (attributes) = NULL_TREE;
15962 /* If there is any qualification still in effect, clear it
15963 now; we will be starting fresh with the next declarator. */
15964 parser->scope = NULL_TREE;
15965 parser->qualifying_scope = NULL_TREE;
15966 parser->object_scope = NULL_TREE;
15967 /* If it's a `,', then there are more declarators. */
15968 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15969 cp_lexer_consume_token (parser->lexer);
15970 /* If the next token isn't a `;', then we have a parse error. */
15971 else if (cp_lexer_next_token_is_not (parser->lexer,
15974 cp_parser_error (parser, "expected %<;%>");
15975 /* Skip tokens until we find a `;'. */
15976 cp_parser_skip_to_end_of_statement (parser);
15983 /* Add DECL to the list of members. */
15985 finish_member_declaration (decl);
15987 if (TREE_CODE (decl) == FUNCTION_DECL)
15988 cp_parser_save_default_args (parser, decl);
15993 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15996 /* Parse a pure-specifier.
16001 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16002 Otherwise, ERROR_MARK_NODE is returned. */
16005 cp_parser_pure_specifier (cp_parser* parser)
16009 /* Look for the `=' token. */
16010 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16011 return error_mark_node;
16012 /* Look for the `0' token. */
16013 token = cp_lexer_peek_token (parser->lexer);
16015 if (token->type == CPP_EOF
16016 || token->type == CPP_PRAGMA_EOL)
16017 return error_mark_node;
16019 cp_lexer_consume_token (parser->lexer);
16021 /* Accept = default or = delete in c++0x mode. */
16022 if (token->keyword == RID_DEFAULT
16023 || token->keyword == RID_DELETE)
16025 maybe_warn_cpp0x ("defaulted and deleted functions");
16026 return token->u.value;
16029 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16030 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16032 cp_parser_error (parser,
16033 "invalid pure specifier (only %<= 0%> is allowed)");
16034 cp_parser_skip_to_end_of_statement (parser);
16035 return error_mark_node;
16037 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16039 error ("%Htemplates may not be %<virtual%>", &token->location);
16040 return error_mark_node;
16043 return integer_zero_node;
16046 /* Parse a constant-initializer.
16048 constant-initializer:
16049 = constant-expression
16051 Returns a representation of the constant-expression. */
16054 cp_parser_constant_initializer (cp_parser* parser)
16056 /* Look for the `=' token. */
16057 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16058 return error_mark_node;
16060 /* It is invalid to write:
16062 struct S { static const int i = { 7 }; };
16065 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16067 cp_parser_error (parser,
16068 "a brace-enclosed initializer is not allowed here");
16069 /* Consume the opening brace. */
16070 cp_lexer_consume_token (parser->lexer);
16071 /* Skip the initializer. */
16072 cp_parser_skip_to_closing_brace (parser);
16073 /* Look for the trailing `}'. */
16074 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16076 return error_mark_node;
16079 return cp_parser_constant_expression (parser,
16080 /*allow_non_constant=*/false,
16084 /* Derived classes [gram.class.derived] */
16086 /* Parse a base-clause.
16089 : base-specifier-list
16091 base-specifier-list:
16092 base-specifier ... [opt]
16093 base-specifier-list , base-specifier ... [opt]
16095 Returns a TREE_LIST representing the base-classes, in the order in
16096 which they were declared. The representation of each node is as
16097 described by cp_parser_base_specifier.
16099 In the case that no bases are specified, this function will return
16100 NULL_TREE, not ERROR_MARK_NODE. */
16103 cp_parser_base_clause (cp_parser* parser)
16105 tree bases = NULL_TREE;
16107 /* Look for the `:' that begins the list. */
16108 cp_parser_require (parser, CPP_COLON, "%<:%>");
16110 /* Scan the base-specifier-list. */
16115 bool pack_expansion_p = false;
16117 /* Look for the base-specifier. */
16118 base = cp_parser_base_specifier (parser);
16119 /* Look for the (optional) ellipsis. */
16120 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16122 /* Consume the `...'. */
16123 cp_lexer_consume_token (parser->lexer);
16125 pack_expansion_p = true;
16128 /* Add BASE to the front of the list. */
16129 if (base != error_mark_node)
16131 if (pack_expansion_p)
16132 /* Make this a pack expansion type. */
16133 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16136 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16138 TREE_CHAIN (base) = bases;
16142 /* Peek at the next token. */
16143 token = cp_lexer_peek_token (parser->lexer);
16144 /* If it's not a comma, then the list is complete. */
16145 if (token->type != CPP_COMMA)
16147 /* Consume the `,'. */
16148 cp_lexer_consume_token (parser->lexer);
16151 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16152 base class had a qualified name. However, the next name that
16153 appears is certainly not qualified. */
16154 parser->scope = NULL_TREE;
16155 parser->qualifying_scope = NULL_TREE;
16156 parser->object_scope = NULL_TREE;
16158 return nreverse (bases);
16161 /* Parse a base-specifier.
16164 :: [opt] nested-name-specifier [opt] class-name
16165 virtual access-specifier [opt] :: [opt] nested-name-specifier
16167 access-specifier virtual [opt] :: [opt] nested-name-specifier
16170 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16171 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16172 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16173 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16176 cp_parser_base_specifier (cp_parser* parser)
16180 bool virtual_p = false;
16181 bool duplicate_virtual_error_issued_p = false;
16182 bool duplicate_access_error_issued_p = false;
16183 bool class_scope_p, template_p;
16184 tree access = access_default_node;
16187 /* Process the optional `virtual' and `access-specifier'. */
16190 /* Peek at the next token. */
16191 token = cp_lexer_peek_token (parser->lexer);
16192 /* Process `virtual'. */
16193 switch (token->keyword)
16196 /* If `virtual' appears more than once, issue an error. */
16197 if (virtual_p && !duplicate_virtual_error_issued_p)
16199 cp_parser_error (parser,
16200 "%<virtual%> specified more than once in base-specified");
16201 duplicate_virtual_error_issued_p = true;
16206 /* Consume the `virtual' token. */
16207 cp_lexer_consume_token (parser->lexer);
16212 case RID_PROTECTED:
16214 /* If more than one access specifier appears, issue an
16216 if (access != access_default_node
16217 && !duplicate_access_error_issued_p)
16219 cp_parser_error (parser,
16220 "more than one access specifier in base-specified");
16221 duplicate_access_error_issued_p = true;
16224 access = ridpointers[(int) token->keyword];
16226 /* Consume the access-specifier. */
16227 cp_lexer_consume_token (parser->lexer);
16236 /* It is not uncommon to see programs mechanically, erroneously, use
16237 the 'typename' keyword to denote (dependent) qualified types
16238 as base classes. */
16239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16241 token = cp_lexer_peek_token (parser->lexer);
16242 if (!processing_template_decl)
16243 error ("%Hkeyword %<typename%> not allowed outside of templates",
16246 error ("%Hkeyword %<typename%> not allowed in this context "
16247 "(the base class is implicitly a type)",
16249 cp_lexer_consume_token (parser->lexer);
16252 /* Look for the optional `::' operator. */
16253 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16254 /* Look for the nested-name-specifier. The simplest way to
16259 The keyword `typename' is not permitted in a base-specifier or
16260 mem-initializer; in these contexts a qualified name that
16261 depends on a template-parameter is implicitly assumed to be a
16264 is to pretend that we have seen the `typename' keyword at this
16266 cp_parser_nested_name_specifier_opt (parser,
16267 /*typename_keyword_p=*/true,
16268 /*check_dependency_p=*/true,
16270 /*is_declaration=*/true);
16271 /* If the base class is given by a qualified name, assume that names
16272 we see are type names or templates, as appropriate. */
16273 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16274 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16276 /* Finally, look for the class-name. */
16277 type = cp_parser_class_name (parser,
16281 /*check_dependency_p=*/true,
16282 /*class_head_p=*/false,
16283 /*is_declaration=*/true);
16285 if (type == error_mark_node)
16286 return error_mark_node;
16288 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16291 /* Exception handling [gram.exception] */
16293 /* Parse an (optional) exception-specification.
16295 exception-specification:
16296 throw ( type-id-list [opt] )
16298 Returns a TREE_LIST representing the exception-specification. The
16299 TREE_VALUE of each node is a type. */
16302 cp_parser_exception_specification_opt (cp_parser* parser)
16307 /* Peek at the next token. */
16308 token = cp_lexer_peek_token (parser->lexer);
16309 /* If it's not `throw', then there's no exception-specification. */
16310 if (!cp_parser_is_keyword (token, RID_THROW))
16313 /* Consume the `throw'. */
16314 cp_lexer_consume_token (parser->lexer);
16316 /* Look for the `('. */
16317 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16319 /* Peek at the next token. */
16320 token = cp_lexer_peek_token (parser->lexer);
16321 /* If it's not a `)', then there is a type-id-list. */
16322 if (token->type != CPP_CLOSE_PAREN)
16324 const char *saved_message;
16326 /* Types may not be defined in an exception-specification. */
16327 saved_message = parser->type_definition_forbidden_message;
16328 parser->type_definition_forbidden_message
16329 = "types may not be defined in an exception-specification";
16330 /* Parse the type-id-list. */
16331 type_id_list = cp_parser_type_id_list (parser);
16332 /* Restore the saved message. */
16333 parser->type_definition_forbidden_message = saved_message;
16336 type_id_list = empty_except_spec;
16338 /* Look for the `)'. */
16339 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16341 return type_id_list;
16344 /* Parse an (optional) type-id-list.
16348 type-id-list , type-id ... [opt]
16350 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16351 in the order that the types were presented. */
16354 cp_parser_type_id_list (cp_parser* parser)
16356 tree types = NULL_TREE;
16363 /* Get the next type-id. */
16364 type = cp_parser_type_id (parser);
16365 /* Parse the optional ellipsis. */
16366 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16368 /* Consume the `...'. */
16369 cp_lexer_consume_token (parser->lexer);
16371 /* Turn the type into a pack expansion expression. */
16372 type = make_pack_expansion (type);
16374 /* Add it to the list. */
16375 types = add_exception_specifier (types, type, /*complain=*/1);
16376 /* Peek at the next token. */
16377 token = cp_lexer_peek_token (parser->lexer);
16378 /* If it is not a `,', we are done. */
16379 if (token->type != CPP_COMMA)
16381 /* Consume the `,'. */
16382 cp_lexer_consume_token (parser->lexer);
16385 return nreverse (types);
16388 /* Parse a try-block.
16391 try compound-statement handler-seq */
16394 cp_parser_try_block (cp_parser* parser)
16398 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16399 try_block = begin_try_block ();
16400 cp_parser_compound_statement (parser, NULL, true);
16401 finish_try_block (try_block);
16402 cp_parser_handler_seq (parser);
16403 finish_handler_sequence (try_block);
16408 /* Parse a function-try-block.
16410 function-try-block:
16411 try ctor-initializer [opt] function-body handler-seq */
16414 cp_parser_function_try_block (cp_parser* parser)
16416 tree compound_stmt;
16418 bool ctor_initializer_p;
16420 /* Look for the `try' keyword. */
16421 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16423 /* Let the rest of the front end know where we are. */
16424 try_block = begin_function_try_block (&compound_stmt);
16425 /* Parse the function-body. */
16427 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16428 /* We're done with the `try' part. */
16429 finish_function_try_block (try_block);
16430 /* Parse the handlers. */
16431 cp_parser_handler_seq (parser);
16432 /* We're done with the handlers. */
16433 finish_function_handler_sequence (try_block, compound_stmt);
16435 return ctor_initializer_p;
16438 /* Parse a handler-seq.
16441 handler handler-seq [opt] */
16444 cp_parser_handler_seq (cp_parser* parser)
16450 /* Parse the handler. */
16451 cp_parser_handler (parser);
16452 /* Peek at the next token. */
16453 token = cp_lexer_peek_token (parser->lexer);
16454 /* If it's not `catch' then there are no more handlers. */
16455 if (!cp_parser_is_keyword (token, RID_CATCH))
16460 /* Parse a handler.
16463 catch ( exception-declaration ) compound-statement */
16466 cp_parser_handler (cp_parser* parser)
16471 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16472 handler = begin_handler ();
16473 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16474 declaration = cp_parser_exception_declaration (parser);
16475 finish_handler_parms (declaration, handler);
16476 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16477 cp_parser_compound_statement (parser, NULL, false);
16478 finish_handler (handler);
16481 /* Parse an exception-declaration.
16483 exception-declaration:
16484 type-specifier-seq declarator
16485 type-specifier-seq abstract-declarator
16489 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16490 ellipsis variant is used. */
16493 cp_parser_exception_declaration (cp_parser* parser)
16495 cp_decl_specifier_seq type_specifiers;
16496 cp_declarator *declarator;
16497 const char *saved_message;
16499 /* If it's an ellipsis, it's easy to handle. */
16500 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16502 /* Consume the `...' token. */
16503 cp_lexer_consume_token (parser->lexer);
16507 /* Types may not be defined in exception-declarations. */
16508 saved_message = parser->type_definition_forbidden_message;
16509 parser->type_definition_forbidden_message
16510 = "types may not be defined in exception-declarations";
16512 /* Parse the type-specifier-seq. */
16513 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16515 /* If it's a `)', then there is no declarator. */
16516 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16519 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16520 /*ctor_dtor_or_conv_p=*/NULL,
16521 /*parenthesized_p=*/NULL,
16522 /*member_p=*/false);
16524 /* Restore the saved message. */
16525 parser->type_definition_forbidden_message = saved_message;
16527 if (!type_specifiers.any_specifiers_p)
16528 return error_mark_node;
16530 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16533 /* Parse a throw-expression.
16536 throw assignment-expression [opt]
16538 Returns a THROW_EXPR representing the throw-expression. */
16541 cp_parser_throw_expression (cp_parser* parser)
16546 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16547 token = cp_lexer_peek_token (parser->lexer);
16548 /* Figure out whether or not there is an assignment-expression
16549 following the "throw" keyword. */
16550 if (token->type == CPP_COMMA
16551 || token->type == CPP_SEMICOLON
16552 || token->type == CPP_CLOSE_PAREN
16553 || token->type == CPP_CLOSE_SQUARE
16554 || token->type == CPP_CLOSE_BRACE
16555 || token->type == CPP_COLON)
16556 expression = NULL_TREE;
16558 expression = cp_parser_assignment_expression (parser,
16559 /*cast_p=*/false, NULL);
16561 return build_throw (expression);
16564 /* GNU Extensions */
16566 /* Parse an (optional) asm-specification.
16569 asm ( string-literal )
16571 If the asm-specification is present, returns a STRING_CST
16572 corresponding to the string-literal. Otherwise, returns
16576 cp_parser_asm_specification_opt (cp_parser* parser)
16579 tree asm_specification;
16581 /* Peek at the next token. */
16582 token = cp_lexer_peek_token (parser->lexer);
16583 /* If the next token isn't the `asm' keyword, then there's no
16584 asm-specification. */
16585 if (!cp_parser_is_keyword (token, RID_ASM))
16588 /* Consume the `asm' token. */
16589 cp_lexer_consume_token (parser->lexer);
16590 /* Look for the `('. */
16591 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16593 /* Look for the string-literal. */
16594 asm_specification = cp_parser_string_literal (parser, false, false);
16596 /* Look for the `)'. */
16597 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16599 return asm_specification;
16602 /* Parse an asm-operand-list.
16606 asm-operand-list , asm-operand
16609 string-literal ( expression )
16610 [ string-literal ] string-literal ( expression )
16612 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16613 each node is the expression. The TREE_PURPOSE is itself a
16614 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16615 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16616 is a STRING_CST for the string literal before the parenthesis. Returns
16617 ERROR_MARK_NODE if any of the operands are invalid. */
16620 cp_parser_asm_operand_list (cp_parser* parser)
16622 tree asm_operands = NULL_TREE;
16623 bool invalid_operands = false;
16627 tree string_literal;
16631 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16633 /* Consume the `[' token. */
16634 cp_lexer_consume_token (parser->lexer);
16635 /* Read the operand name. */
16636 name = cp_parser_identifier (parser);
16637 if (name != error_mark_node)
16638 name = build_string (IDENTIFIER_LENGTH (name),
16639 IDENTIFIER_POINTER (name));
16640 /* Look for the closing `]'. */
16641 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16645 /* Look for the string-literal. */
16646 string_literal = cp_parser_string_literal (parser, false, false);
16648 /* Look for the `('. */
16649 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16650 /* Parse the expression. */
16651 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16652 /* Look for the `)'. */
16653 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16655 if (name == error_mark_node
16656 || string_literal == error_mark_node
16657 || expression == error_mark_node)
16658 invalid_operands = true;
16660 /* Add this operand to the list. */
16661 asm_operands = tree_cons (build_tree_list (name, string_literal),
16664 /* If the next token is not a `,', there are no more
16666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16668 /* Consume the `,'. */
16669 cp_lexer_consume_token (parser->lexer);
16672 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16675 /* Parse an asm-clobber-list.
16679 asm-clobber-list , string-literal
16681 Returns a TREE_LIST, indicating the clobbers in the order that they
16682 appeared. The TREE_VALUE of each node is a STRING_CST. */
16685 cp_parser_asm_clobber_list (cp_parser* parser)
16687 tree clobbers = NULL_TREE;
16691 tree string_literal;
16693 /* Look for the string literal. */
16694 string_literal = cp_parser_string_literal (parser, false, false);
16695 /* Add it to the list. */
16696 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16697 /* If the next token is not a `,', then the list is
16699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16701 /* Consume the `,' token. */
16702 cp_lexer_consume_token (parser->lexer);
16708 /* Parse an (optional) series of attributes.
16711 attributes attribute
16714 __attribute__ (( attribute-list [opt] ))
16716 The return value is as for cp_parser_attribute_list. */
16719 cp_parser_attributes_opt (cp_parser* parser)
16721 tree attributes = NULL_TREE;
16726 tree attribute_list;
16728 /* Peek at the next token. */
16729 token = cp_lexer_peek_token (parser->lexer);
16730 /* If it's not `__attribute__', then we're done. */
16731 if (token->keyword != RID_ATTRIBUTE)
16734 /* Consume the `__attribute__' keyword. */
16735 cp_lexer_consume_token (parser->lexer);
16736 /* Look for the two `(' tokens. */
16737 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16738 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16740 /* Peek at the next token. */
16741 token = cp_lexer_peek_token (parser->lexer);
16742 if (token->type != CPP_CLOSE_PAREN)
16743 /* Parse the attribute-list. */
16744 attribute_list = cp_parser_attribute_list (parser);
16746 /* If the next token is a `)', then there is no attribute
16748 attribute_list = NULL;
16750 /* Look for the two `)' tokens. */
16751 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16752 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16754 /* Add these new attributes to the list. */
16755 attributes = chainon (attributes, attribute_list);
16761 /* Parse an attribute-list.
16765 attribute-list , attribute
16769 identifier ( identifier )
16770 identifier ( identifier , expression-list )
16771 identifier ( expression-list )
16773 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16774 to an attribute. The TREE_PURPOSE of each node is the identifier
16775 indicating which attribute is in use. The TREE_VALUE represents
16776 the arguments, if any. */
16779 cp_parser_attribute_list (cp_parser* parser)
16781 tree attribute_list = NULL_TREE;
16782 bool save_translate_strings_p = parser->translate_strings_p;
16784 parser->translate_strings_p = false;
16791 /* Look for the identifier. We also allow keywords here; for
16792 example `__attribute__ ((const))' is legal. */
16793 token = cp_lexer_peek_token (parser->lexer);
16794 if (token->type == CPP_NAME
16795 || token->type == CPP_KEYWORD)
16797 tree arguments = NULL_TREE;
16799 /* Consume the token. */
16800 token = cp_lexer_consume_token (parser->lexer);
16802 /* Save away the identifier that indicates which attribute
16804 identifier = token->u.value;
16805 attribute = build_tree_list (identifier, NULL_TREE);
16807 /* Peek at the next token. */
16808 token = cp_lexer_peek_token (parser->lexer);
16809 /* If it's an `(', then parse the attribute arguments. */
16810 if (token->type == CPP_OPEN_PAREN)
16812 arguments = cp_parser_parenthesized_expression_list
16813 (parser, true, /*cast_p=*/false,
16814 /*allow_expansion_p=*/false,
16815 /*non_constant_p=*/NULL);
16816 /* Save the arguments away. */
16817 TREE_VALUE (attribute) = arguments;
16820 if (arguments != error_mark_node)
16822 /* Add this attribute to the list. */
16823 TREE_CHAIN (attribute) = attribute_list;
16824 attribute_list = attribute;
16827 token = cp_lexer_peek_token (parser->lexer);
16829 /* Now, look for more attributes. If the next token isn't a
16830 `,', we're done. */
16831 if (token->type != CPP_COMMA)
16834 /* Consume the comma and keep going. */
16835 cp_lexer_consume_token (parser->lexer);
16837 parser->translate_strings_p = save_translate_strings_p;
16839 /* We built up the list in reverse order. */
16840 return nreverse (attribute_list);
16843 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16844 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16845 current value of the PEDANTIC flag, regardless of whether or not
16846 the `__extension__' keyword is present. The caller is responsible
16847 for restoring the value of the PEDANTIC flag. */
16850 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16852 /* Save the old value of the PEDANTIC flag. */
16853 *saved_pedantic = pedantic;
16855 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16857 /* Consume the `__extension__' token. */
16858 cp_lexer_consume_token (parser->lexer);
16859 /* We're not being pedantic while the `__extension__' keyword is
16869 /* Parse a label declaration.
16872 __label__ label-declarator-seq ;
16874 label-declarator-seq:
16875 identifier , label-declarator-seq
16879 cp_parser_label_declaration (cp_parser* parser)
16881 /* Look for the `__label__' keyword. */
16882 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16888 /* Look for an identifier. */
16889 identifier = cp_parser_identifier (parser);
16890 /* If we failed, stop. */
16891 if (identifier == error_mark_node)
16893 /* Declare it as a label. */
16894 finish_label_decl (identifier);
16895 /* If the next token is a `;', stop. */
16896 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16898 /* Look for the `,' separating the label declarations. */
16899 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16902 /* Look for the final `;'. */
16903 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16906 /* Support Functions */
16908 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16909 NAME should have one of the representations used for an
16910 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16911 is returned. If PARSER->SCOPE is a dependent type, then a
16912 SCOPE_REF is returned.
16914 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16915 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16916 was formed. Abstractly, such entities should not be passed to this
16917 function, because they do not need to be looked up, but it is
16918 simpler to check for this special case here, rather than at the
16921 In cases not explicitly covered above, this function returns a
16922 DECL, OVERLOAD, or baselink representing the result of the lookup.
16923 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16926 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16927 (e.g., "struct") that was used. In that case bindings that do not
16928 refer to types are ignored.
16930 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16933 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16936 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16939 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16940 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16941 NULL_TREE otherwise. */
16944 cp_parser_lookup_name (cp_parser *parser, tree name,
16945 enum tag_types tag_type,
16948 bool check_dependency,
16949 tree *ambiguous_decls,
16950 location_t name_location)
16954 tree object_type = parser->context->object_type;
16956 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16957 flags |= LOOKUP_COMPLAIN;
16959 /* Assume that the lookup will be unambiguous. */
16960 if (ambiguous_decls)
16961 *ambiguous_decls = NULL_TREE;
16963 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16964 no longer valid. Note that if we are parsing tentatively, and
16965 the parse fails, OBJECT_TYPE will be automatically restored. */
16966 parser->context->object_type = NULL_TREE;
16968 if (name == error_mark_node)
16969 return error_mark_node;
16971 /* A template-id has already been resolved; there is no lookup to
16973 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16975 if (BASELINK_P (name))
16977 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16978 == TEMPLATE_ID_EXPR);
16982 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16983 it should already have been checked to make sure that the name
16984 used matches the type being destroyed. */
16985 if (TREE_CODE (name) == BIT_NOT_EXPR)
16989 /* Figure out to which type this destructor applies. */
16991 type = parser->scope;
16992 else if (object_type)
16993 type = object_type;
16995 type = current_class_type;
16996 /* If that's not a class type, there is no destructor. */
16997 if (!type || !CLASS_TYPE_P (type))
16998 return error_mark_node;
16999 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17000 lazily_declare_fn (sfk_destructor, type);
17001 if (!CLASSTYPE_DESTRUCTORS (type))
17002 return error_mark_node;
17003 /* If it was a class type, return the destructor. */
17004 return CLASSTYPE_DESTRUCTORS (type);
17007 /* By this point, the NAME should be an ordinary identifier. If
17008 the id-expression was a qualified name, the qualifying scope is
17009 stored in PARSER->SCOPE at this point. */
17010 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17012 /* Perform the lookup. */
17017 if (parser->scope == error_mark_node)
17018 return error_mark_node;
17020 /* If the SCOPE is dependent, the lookup must be deferred until
17021 the template is instantiated -- unless we are explicitly
17022 looking up names in uninstantiated templates. Even then, we
17023 cannot look up the name if the scope is not a class type; it
17024 might, for example, be a template type parameter. */
17025 dependent_p = (TYPE_P (parser->scope)
17026 && dependent_scope_p (parser->scope));
17027 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17029 /* Defer lookup. */
17030 decl = error_mark_node;
17033 tree pushed_scope = NULL_TREE;
17035 /* If PARSER->SCOPE is a dependent type, then it must be a
17036 class type, and we must not be checking dependencies;
17037 otherwise, we would have processed this lookup above. So
17038 that PARSER->SCOPE is not considered a dependent base by
17039 lookup_member, we must enter the scope here. */
17041 pushed_scope = push_scope (parser->scope);
17042 /* If the PARSER->SCOPE is a template specialization, it
17043 may be instantiated during name lookup. In that case,
17044 errors may be issued. Even if we rollback the current
17045 tentative parse, those errors are valid. */
17046 decl = lookup_qualified_name (parser->scope, name,
17047 tag_type != none_type,
17048 /*complain=*/true);
17050 /* If we have a single function from a using decl, pull it out. */
17051 if (TREE_CODE (decl) == OVERLOAD
17052 && !really_overloaded_fn (decl))
17053 decl = OVL_FUNCTION (decl);
17056 pop_scope (pushed_scope);
17059 /* If the scope is a dependent type and either we deferred lookup or
17060 we did lookup but didn't find the name, rememeber the name. */
17061 if (decl == error_mark_node && TYPE_P (parser->scope)
17062 && dependent_type_p (parser->scope))
17068 /* The resolution to Core Issue 180 says that `struct
17069 A::B' should be considered a type-name, even if `A'
17071 type = make_typename_type (parser->scope, name, tag_type,
17072 /*complain=*/tf_error);
17073 decl = TYPE_NAME (type);
17075 else if (is_template
17076 && (cp_parser_next_token_ends_template_argument_p (parser)
17077 || cp_lexer_next_token_is (parser->lexer,
17079 decl = make_unbound_class_template (parser->scope,
17081 /*complain=*/tf_error);
17083 decl = build_qualified_name (/*type=*/NULL_TREE,
17084 parser->scope, name,
17087 parser->qualifying_scope = parser->scope;
17088 parser->object_scope = NULL_TREE;
17090 else if (object_type)
17092 tree object_decl = NULL_TREE;
17093 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17094 OBJECT_TYPE is not a class. */
17095 if (CLASS_TYPE_P (object_type))
17096 /* If the OBJECT_TYPE is a template specialization, it may
17097 be instantiated during name lookup. In that case, errors
17098 may be issued. Even if we rollback the current tentative
17099 parse, those errors are valid. */
17100 object_decl = lookup_member (object_type,
17103 tag_type != none_type);
17104 /* Look it up in the enclosing context, too. */
17105 decl = lookup_name_real (name, tag_type != none_type,
17107 /*block_p=*/true, is_namespace, flags);
17108 parser->object_scope = object_type;
17109 parser->qualifying_scope = NULL_TREE;
17111 decl = object_decl;
17115 decl = lookup_name_real (name, tag_type != none_type,
17117 /*block_p=*/true, is_namespace, flags);
17118 parser->qualifying_scope = NULL_TREE;
17119 parser->object_scope = NULL_TREE;
17122 /* If the lookup failed, let our caller know. */
17123 if (!decl || decl == error_mark_node)
17124 return error_mark_node;
17126 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17127 if (TREE_CODE (decl) == TREE_LIST)
17129 if (ambiguous_decls)
17130 *ambiguous_decls = decl;
17131 /* The error message we have to print is too complicated for
17132 cp_parser_error, so we incorporate its actions directly. */
17133 if (!cp_parser_simulate_error (parser))
17135 error ("%Hreference to %qD is ambiguous",
17136 &name_location, name);
17137 print_candidates (decl);
17139 return error_mark_node;
17142 gcc_assert (DECL_P (decl)
17143 || TREE_CODE (decl) == OVERLOAD
17144 || TREE_CODE (decl) == SCOPE_REF
17145 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17146 || BASELINK_P (decl));
17148 /* If we have resolved the name of a member declaration, check to
17149 see if the declaration is accessible. When the name resolves to
17150 set of overloaded functions, accessibility is checked when
17151 overload resolution is done.
17153 During an explicit instantiation, access is not checked at all,
17154 as per [temp.explicit]. */
17156 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17161 /* Like cp_parser_lookup_name, but for use in the typical case where
17162 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17163 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17166 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17168 return cp_parser_lookup_name (parser, name,
17170 /*is_template=*/false,
17171 /*is_namespace=*/false,
17172 /*check_dependency=*/true,
17173 /*ambiguous_decls=*/NULL,
17177 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17178 the current context, return the TYPE_DECL. If TAG_NAME_P is
17179 true, the DECL indicates the class being defined in a class-head,
17180 or declared in an elaborated-type-specifier.
17182 Otherwise, return DECL. */
17185 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17187 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17188 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17191 template <typename T> struct B;
17194 template <typename T> struct A::B {};
17196 Similarly, in an elaborated-type-specifier:
17198 namespace N { struct X{}; }
17201 template <typename T> friend struct N::X;
17204 However, if the DECL refers to a class type, and we are in
17205 the scope of the class, then the name lookup automatically
17206 finds the TYPE_DECL created by build_self_reference rather
17207 than a TEMPLATE_DECL. For example, in:
17209 template <class T> struct S {
17213 there is no need to handle such case. */
17215 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17216 return DECL_TEMPLATE_RESULT (decl);
17221 /* If too many, or too few, template-parameter lists apply to the
17222 declarator, issue an error message. Returns TRUE if all went well,
17223 and FALSE otherwise. */
17226 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17227 cp_declarator *declarator,
17228 location_t declarator_location)
17230 unsigned num_templates;
17232 /* We haven't seen any classes that involve template parameters yet. */
17235 switch (declarator->kind)
17238 if (declarator->u.id.qualifying_scope)
17243 scope = declarator->u.id.qualifying_scope;
17244 member = declarator->u.id.unqualified_name;
17246 while (scope && CLASS_TYPE_P (scope))
17248 /* You're supposed to have one `template <...>'
17249 for every template class, but you don't need one
17250 for a full specialization. For example:
17252 template <class T> struct S{};
17253 template <> struct S<int> { void f(); };
17254 void S<int>::f () {}
17256 is correct; there shouldn't be a `template <>' for
17257 the definition of `S<int>::f'. */
17258 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17259 /* If SCOPE does not have template information of any
17260 kind, then it is not a template, nor is it nested
17261 within a template. */
17263 if (explicit_class_specialization_p (scope))
17265 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17268 scope = TYPE_CONTEXT (scope);
17271 else if (TREE_CODE (declarator->u.id.unqualified_name)
17272 == TEMPLATE_ID_EXPR)
17273 /* If the DECLARATOR has the form `X<y>' then it uses one
17274 additional level of template parameters. */
17277 return cp_parser_check_template_parameters (parser,
17279 declarator_location);
17284 case cdk_reference:
17286 return (cp_parser_check_declarator_template_parameters
17287 (parser, declarator->declarator, declarator_location));
17293 gcc_unreachable ();
17298 /* NUM_TEMPLATES were used in the current declaration. If that is
17299 invalid, return FALSE and issue an error messages. Otherwise,
17303 cp_parser_check_template_parameters (cp_parser* parser,
17304 unsigned num_templates,
17305 location_t location)
17307 /* If there are more template classes than parameter lists, we have
17310 template <class T> void S<T>::R<T>::f (); */
17311 if (parser->num_template_parameter_lists < num_templates)
17313 error ("%Htoo few template-parameter-lists", &location);
17316 /* If there are the same number of template classes and parameter
17317 lists, that's OK. */
17318 if (parser->num_template_parameter_lists == num_templates)
17320 /* If there are more, but only one more, then we are referring to a
17321 member template. That's OK too. */
17322 if (parser->num_template_parameter_lists == num_templates + 1)
17324 /* Otherwise, there are too many template parameter lists. We have
17327 template <class T> template <class U> void S::f(); */
17328 error ("%Htoo many template-parameter-lists", &location);
17332 /* Parse an optional `::' token indicating that the following name is
17333 from the global namespace. If so, PARSER->SCOPE is set to the
17334 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17335 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17336 Returns the new value of PARSER->SCOPE, if the `::' token is
17337 present, and NULL_TREE otherwise. */
17340 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17344 /* Peek at the next token. */
17345 token = cp_lexer_peek_token (parser->lexer);
17346 /* If we're looking at a `::' token then we're starting from the
17347 global namespace, not our current location. */
17348 if (token->type == CPP_SCOPE)
17350 /* Consume the `::' token. */
17351 cp_lexer_consume_token (parser->lexer);
17352 /* Set the SCOPE so that we know where to start the lookup. */
17353 parser->scope = global_namespace;
17354 parser->qualifying_scope = global_namespace;
17355 parser->object_scope = NULL_TREE;
17357 return parser->scope;
17359 else if (!current_scope_valid_p)
17361 parser->scope = NULL_TREE;
17362 parser->qualifying_scope = NULL_TREE;
17363 parser->object_scope = NULL_TREE;
17369 /* Returns TRUE if the upcoming token sequence is the start of a
17370 constructor declarator. If FRIEND_P is true, the declarator is
17371 preceded by the `friend' specifier. */
17374 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17376 bool constructor_p;
17377 tree type_decl = NULL_TREE;
17378 bool nested_name_p;
17379 cp_token *next_token;
17381 /* The common case is that this is not a constructor declarator, so
17382 try to avoid doing lots of work if at all possible. It's not
17383 valid declare a constructor at function scope. */
17384 if (parser->in_function_body)
17386 /* And only certain tokens can begin a constructor declarator. */
17387 next_token = cp_lexer_peek_token (parser->lexer);
17388 if (next_token->type != CPP_NAME
17389 && next_token->type != CPP_SCOPE
17390 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17391 && next_token->type != CPP_TEMPLATE_ID)
17394 /* Parse tentatively; we are going to roll back all of the tokens
17396 cp_parser_parse_tentatively (parser);
17397 /* Assume that we are looking at a constructor declarator. */
17398 constructor_p = true;
17400 /* Look for the optional `::' operator. */
17401 cp_parser_global_scope_opt (parser,
17402 /*current_scope_valid_p=*/false);
17403 /* Look for the nested-name-specifier. */
17405 = (cp_parser_nested_name_specifier_opt (parser,
17406 /*typename_keyword_p=*/false,
17407 /*check_dependency_p=*/false,
17409 /*is_declaration=*/false)
17411 /* Outside of a class-specifier, there must be a
17412 nested-name-specifier. */
17413 if (!nested_name_p &&
17414 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17416 constructor_p = false;
17417 /* If we still think that this might be a constructor-declarator,
17418 look for a class-name. */
17423 template <typename T> struct S { S(); };
17424 template <typename T> S<T>::S ();
17426 we must recognize that the nested `S' names a class.
17429 template <typename T> S<T>::S<T> ();
17431 we must recognize that the nested `S' names a template. */
17432 type_decl = cp_parser_class_name (parser,
17433 /*typename_keyword_p=*/false,
17434 /*template_keyword_p=*/false,
17436 /*check_dependency_p=*/false,
17437 /*class_head_p=*/false,
17438 /*is_declaration=*/false);
17439 /* If there was no class-name, then this is not a constructor. */
17440 constructor_p = !cp_parser_error_occurred (parser);
17443 /* If we're still considering a constructor, we have to see a `(',
17444 to begin the parameter-declaration-clause, followed by either a
17445 `)', an `...', or a decl-specifier. We need to check for a
17446 type-specifier to avoid being fooled into thinking that:
17450 is a constructor. (It is actually a function named `f' that
17451 takes one parameter (of type `int') and returns a value of type
17454 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17457 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17458 /* A parameter declaration begins with a decl-specifier,
17459 which is either the "attribute" keyword, a storage class
17460 specifier, or (usually) a type-specifier. */
17461 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17464 tree pushed_scope = NULL_TREE;
17465 unsigned saved_num_template_parameter_lists;
17467 /* Names appearing in the type-specifier should be looked up
17468 in the scope of the class. */
17469 if (current_class_type)
17473 type = TREE_TYPE (type_decl);
17474 if (TREE_CODE (type) == TYPENAME_TYPE)
17476 type = resolve_typename_type (type,
17477 /*only_current_p=*/false);
17478 if (TREE_CODE (type) == TYPENAME_TYPE)
17480 cp_parser_abort_tentative_parse (parser);
17484 pushed_scope = push_scope (type);
17487 /* Inside the constructor parameter list, surrounding
17488 template-parameter-lists do not apply. */
17489 saved_num_template_parameter_lists
17490 = parser->num_template_parameter_lists;
17491 parser->num_template_parameter_lists = 0;
17493 /* Look for the type-specifier. */
17494 cp_parser_type_specifier (parser,
17495 CP_PARSER_FLAGS_NONE,
17496 /*decl_specs=*/NULL,
17497 /*is_declarator=*/true,
17498 /*declares_class_or_enum=*/NULL,
17499 /*is_cv_qualifier=*/NULL);
17501 parser->num_template_parameter_lists
17502 = saved_num_template_parameter_lists;
17504 /* Leave the scope of the class. */
17506 pop_scope (pushed_scope);
17508 constructor_p = !cp_parser_error_occurred (parser);
17512 constructor_p = false;
17513 /* We did not really want to consume any tokens. */
17514 cp_parser_abort_tentative_parse (parser);
17516 return constructor_p;
17519 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17520 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17521 they must be performed once we are in the scope of the function.
17523 Returns the function defined. */
17526 cp_parser_function_definition_from_specifiers_and_declarator
17527 (cp_parser* parser,
17528 cp_decl_specifier_seq *decl_specifiers,
17530 const cp_declarator *declarator)
17535 /* Begin the function-definition. */
17536 success_p = start_function (decl_specifiers, declarator, attributes);
17538 /* The things we're about to see are not directly qualified by any
17539 template headers we've seen thus far. */
17540 reset_specialization ();
17542 /* If there were names looked up in the decl-specifier-seq that we
17543 did not check, check them now. We must wait until we are in the
17544 scope of the function to perform the checks, since the function
17545 might be a friend. */
17546 perform_deferred_access_checks ();
17550 /* Skip the entire function. */
17551 cp_parser_skip_to_end_of_block_or_statement (parser);
17552 fn = error_mark_node;
17554 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17556 /* Seen already, skip it. An error message has already been output. */
17557 cp_parser_skip_to_end_of_block_or_statement (parser);
17558 fn = current_function_decl;
17559 current_function_decl = NULL_TREE;
17560 /* If this is a function from a class, pop the nested class. */
17561 if (current_class_name)
17562 pop_nested_class ();
17565 fn = cp_parser_function_definition_after_declarator (parser,
17566 /*inline_p=*/false);
17571 /* Parse the part of a function-definition that follows the
17572 declarator. INLINE_P is TRUE iff this function is an inline
17573 function defined with a class-specifier.
17575 Returns the function defined. */
17578 cp_parser_function_definition_after_declarator (cp_parser* parser,
17582 bool ctor_initializer_p = false;
17583 bool saved_in_unbraced_linkage_specification_p;
17584 bool saved_in_function_body;
17585 unsigned saved_num_template_parameter_lists;
17588 saved_in_function_body = parser->in_function_body;
17589 parser->in_function_body = true;
17590 /* If the next token is `return', then the code may be trying to
17591 make use of the "named return value" extension that G++ used to
17593 token = cp_lexer_peek_token (parser->lexer);
17594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17596 /* Consume the `return' keyword. */
17597 cp_lexer_consume_token (parser->lexer);
17598 /* Look for the identifier that indicates what value is to be
17600 cp_parser_identifier (parser);
17601 /* Issue an error message. */
17602 error ("%Hnamed return values are no longer supported",
17604 /* Skip tokens until we reach the start of the function body. */
17607 cp_token *token = cp_lexer_peek_token (parser->lexer);
17608 if (token->type == CPP_OPEN_BRACE
17609 || token->type == CPP_EOF
17610 || token->type == CPP_PRAGMA_EOL)
17612 cp_lexer_consume_token (parser->lexer);
17615 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17616 anything declared inside `f'. */
17617 saved_in_unbraced_linkage_specification_p
17618 = parser->in_unbraced_linkage_specification_p;
17619 parser->in_unbraced_linkage_specification_p = false;
17620 /* Inside the function, surrounding template-parameter-lists do not
17622 saved_num_template_parameter_lists
17623 = parser->num_template_parameter_lists;
17624 parser->num_template_parameter_lists = 0;
17625 /* If the next token is `try', then we are looking at a
17626 function-try-block. */
17627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17628 ctor_initializer_p = cp_parser_function_try_block (parser);
17629 /* A function-try-block includes the function-body, so we only do
17630 this next part if we're not processing a function-try-block. */
17633 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17635 /* Finish the function. */
17636 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17637 (inline_p ? 2 : 0));
17638 /* Generate code for it, if necessary. */
17639 expand_or_defer_fn (fn);
17640 /* Restore the saved values. */
17641 parser->in_unbraced_linkage_specification_p
17642 = saved_in_unbraced_linkage_specification_p;
17643 parser->num_template_parameter_lists
17644 = saved_num_template_parameter_lists;
17645 parser->in_function_body = saved_in_function_body;
17650 /* Parse a template-declaration, assuming that the `export' (and
17651 `extern') keywords, if present, has already been scanned. MEMBER_P
17652 is as for cp_parser_template_declaration. */
17655 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17657 tree decl = NULL_TREE;
17658 VEC (deferred_access_check,gc) *checks;
17659 tree parameter_list;
17660 bool friend_p = false;
17661 bool need_lang_pop;
17664 /* Look for the `template' keyword. */
17665 token = cp_lexer_peek_token (parser->lexer);
17666 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17670 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17672 if (at_class_scope_p () && current_function_decl)
17674 /* 14.5.2.2 [temp.mem]
17676 A local class shall not have member templates. */
17677 error ("%Hinvalid declaration of member template in local class",
17679 cp_parser_skip_to_end_of_block_or_statement (parser);
17684 A template ... shall not have C linkage. */
17685 if (current_lang_name == lang_name_c)
17687 error ("%Htemplate with C linkage", &token->location);
17688 /* Give it C++ linkage to avoid confusing other parts of the
17690 push_lang_context (lang_name_cplusplus);
17691 need_lang_pop = true;
17694 need_lang_pop = false;
17696 /* We cannot perform access checks on the template parameter
17697 declarations until we know what is being declared, just as we
17698 cannot check the decl-specifier list. */
17699 push_deferring_access_checks (dk_deferred);
17701 /* If the next token is `>', then we have an invalid
17702 specialization. Rather than complain about an invalid template
17703 parameter, issue an error message here. */
17704 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17706 cp_parser_error (parser, "invalid explicit specialization");
17707 begin_specialization ();
17708 parameter_list = NULL_TREE;
17711 /* Parse the template parameters. */
17712 parameter_list = cp_parser_template_parameter_list (parser);
17714 /* Get the deferred access checks from the parameter list. These
17715 will be checked once we know what is being declared, as for a
17716 member template the checks must be performed in the scope of the
17717 class containing the member. */
17718 checks = get_deferred_access_checks ();
17720 /* Look for the `>'. */
17721 cp_parser_skip_to_end_of_template_parameter_list (parser);
17722 /* We just processed one more parameter list. */
17723 ++parser->num_template_parameter_lists;
17724 /* If the next token is `template', there are more template
17726 if (cp_lexer_next_token_is_keyword (parser->lexer,
17728 cp_parser_template_declaration_after_export (parser, member_p);
17731 /* There are no access checks when parsing a template, as we do not
17732 know if a specialization will be a friend. */
17733 push_deferring_access_checks (dk_no_check);
17734 token = cp_lexer_peek_token (parser->lexer);
17735 decl = cp_parser_single_declaration (parser,
17738 /*explicit_specialization_p=*/false,
17740 pop_deferring_access_checks ();
17742 /* If this is a member template declaration, let the front
17744 if (member_p && !friend_p && decl)
17746 if (TREE_CODE (decl) == TYPE_DECL)
17747 cp_parser_check_access_in_redeclaration (decl, token->location);
17749 decl = finish_member_template_decl (decl);
17751 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17752 make_friend_class (current_class_type, TREE_TYPE (decl),
17753 /*complain=*/true);
17755 /* We are done with the current parameter list. */
17756 --parser->num_template_parameter_lists;
17758 pop_deferring_access_checks ();
17761 finish_template_decl (parameter_list);
17763 /* Register member declarations. */
17764 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17765 finish_member_declaration (decl);
17766 /* For the erroneous case of a template with C linkage, we pushed an
17767 implicit C++ linkage scope; exit that scope now. */
17769 pop_lang_context ();
17770 /* If DECL is a function template, we must return to parse it later.
17771 (Even though there is no definition, there might be default
17772 arguments that need handling.) */
17773 if (member_p && decl
17774 && (TREE_CODE (decl) == FUNCTION_DECL
17775 || DECL_FUNCTION_TEMPLATE_P (decl)))
17776 TREE_VALUE (parser->unparsed_functions_queues)
17777 = tree_cons (NULL_TREE, decl,
17778 TREE_VALUE (parser->unparsed_functions_queues));
17781 /* Perform the deferred access checks from a template-parameter-list.
17782 CHECKS is a TREE_LIST of access checks, as returned by
17783 get_deferred_access_checks. */
17786 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17788 ++processing_template_parmlist;
17789 perform_access_checks (checks);
17790 --processing_template_parmlist;
17793 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17794 `function-definition' sequence. MEMBER_P is true, this declaration
17795 appears in a class scope.
17797 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17798 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17801 cp_parser_single_declaration (cp_parser* parser,
17802 VEC (deferred_access_check,gc)* checks,
17804 bool explicit_specialization_p,
17807 int declares_class_or_enum;
17808 tree decl = NULL_TREE;
17809 cp_decl_specifier_seq decl_specifiers;
17810 bool function_definition_p = false;
17811 cp_token *decl_spec_token_start;
17813 /* This function is only used when processing a template
17815 gcc_assert (innermost_scope_kind () == sk_template_parms
17816 || innermost_scope_kind () == sk_template_spec);
17818 /* Defer access checks until we know what is being declared. */
17819 push_deferring_access_checks (dk_deferred);
17821 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17823 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17824 cp_parser_decl_specifier_seq (parser,
17825 CP_PARSER_FLAGS_OPTIONAL,
17827 &declares_class_or_enum);
17829 *friend_p = cp_parser_friend_p (&decl_specifiers);
17831 /* There are no template typedefs. */
17832 if (decl_specifiers.specs[(int) ds_typedef])
17834 error ("%Htemplate declaration of %qs",
17835 &decl_spec_token_start->location, "typedef");
17836 decl = error_mark_node;
17839 /* Gather up the access checks that occurred the
17840 decl-specifier-seq. */
17841 stop_deferring_access_checks ();
17843 /* Check for the declaration of a template class. */
17844 if (declares_class_or_enum)
17846 if (cp_parser_declares_only_class_p (parser))
17848 decl = shadow_tag (&decl_specifiers);
17853 friend template <typename T> struct A<T>::B;
17856 A<T>::B will be represented by a TYPENAME_TYPE, and
17857 therefore not recognized by shadow_tag. */
17858 if (friend_p && *friend_p
17860 && decl_specifiers.type
17861 && TYPE_P (decl_specifiers.type))
17862 decl = decl_specifiers.type;
17864 if (decl && decl != error_mark_node)
17865 decl = TYPE_NAME (decl);
17867 decl = error_mark_node;
17869 /* Perform access checks for template parameters. */
17870 cp_parser_perform_template_parameter_access_checks (checks);
17873 /* If it's not a template class, try for a template function. If
17874 the next token is a `;', then this declaration does not declare
17875 anything. But, if there were errors in the decl-specifiers, then
17876 the error might well have come from an attempted class-specifier.
17877 In that case, there's no need to warn about a missing declarator. */
17879 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17880 || decl_specifiers.type != error_mark_node))
17882 decl = cp_parser_init_declarator (parser,
17885 /*function_definition_allowed_p=*/true,
17887 declares_class_or_enum,
17888 &function_definition_p);
17890 /* 7.1.1-1 [dcl.stc]
17892 A storage-class-specifier shall not be specified in an explicit
17893 specialization... */
17895 && explicit_specialization_p
17896 && decl_specifiers.storage_class != sc_none)
17898 error ("%Hexplicit template specialization cannot have a storage class",
17899 &decl_spec_token_start->location);
17900 decl = error_mark_node;
17904 pop_deferring_access_checks ();
17906 /* Clear any current qualification; whatever comes next is the start
17907 of something new. */
17908 parser->scope = NULL_TREE;
17909 parser->qualifying_scope = NULL_TREE;
17910 parser->object_scope = NULL_TREE;
17911 /* Look for a trailing `;' after the declaration. */
17912 if (!function_definition_p
17913 && (decl == error_mark_node
17914 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17915 cp_parser_skip_to_end_of_block_or_statement (parser);
17920 /* Parse a cast-expression that is not the operand of a unary "&". */
17923 cp_parser_simple_cast_expression (cp_parser *parser)
17925 return cp_parser_cast_expression (parser, /*address_p=*/false,
17926 /*cast_p=*/false, NULL);
17929 /* Parse a functional cast to TYPE. Returns an expression
17930 representing the cast. */
17933 cp_parser_functional_cast (cp_parser* parser, tree type)
17935 tree expression_list;
17939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17941 maybe_warn_cpp0x ("extended initializer lists");
17942 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17943 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17944 if (TREE_CODE (type) == TYPE_DECL)
17945 type = TREE_TYPE (type);
17946 return finish_compound_literal (type, expression_list);
17950 = cp_parser_parenthesized_expression_list (parser, false,
17952 /*allow_expansion_p=*/true,
17953 /*non_constant_p=*/NULL);
17955 cast = build_functional_cast (type, expression_list,
17956 tf_warning_or_error);
17957 /* [expr.const]/1: In an integral constant expression "only type
17958 conversions to integral or enumeration type can be used". */
17959 if (TREE_CODE (type) == TYPE_DECL)
17960 type = TREE_TYPE (type);
17961 if (cast != error_mark_node
17962 && !cast_valid_in_integral_constant_expression_p (type)
17963 && (cp_parser_non_integral_constant_expression
17964 (parser, "a call to a constructor")))
17965 return error_mark_node;
17969 /* Save the tokens that make up the body of a member function defined
17970 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17971 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17972 specifiers applied to the declaration. Returns the FUNCTION_DECL
17973 for the member function. */
17976 cp_parser_save_member_function_body (cp_parser* parser,
17977 cp_decl_specifier_seq *decl_specifiers,
17978 cp_declarator *declarator,
17985 /* Create the function-declaration. */
17986 fn = start_method (decl_specifiers, declarator, attributes);
17987 /* If something went badly wrong, bail out now. */
17988 if (fn == error_mark_node)
17990 /* If there's a function-body, skip it. */
17991 if (cp_parser_token_starts_function_definition_p
17992 (cp_lexer_peek_token (parser->lexer)))
17993 cp_parser_skip_to_end_of_block_or_statement (parser);
17994 return error_mark_node;
17997 /* Remember it, if there default args to post process. */
17998 cp_parser_save_default_args (parser, fn);
18000 /* Save away the tokens that make up the body of the
18002 first = parser->lexer->next_token;
18003 /* We can have braced-init-list mem-initializers before the fn body. */
18004 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18006 cp_lexer_consume_token (parser->lexer);
18007 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18008 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18010 /* cache_group will stop after an un-nested { } pair, too. */
18011 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18014 /* variadic mem-inits have ... after the ')'. */
18015 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18016 cp_lexer_consume_token (parser->lexer);
18019 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18020 /* Handle function try blocks. */
18021 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18022 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18023 last = parser->lexer->next_token;
18025 /* Save away the inline definition; we will process it when the
18026 class is complete. */
18027 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18028 DECL_PENDING_INLINE_P (fn) = 1;
18030 /* We need to know that this was defined in the class, so that
18031 friend templates are handled correctly. */
18032 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18034 /* We're done with the inline definition. */
18035 finish_method (fn);
18037 /* Add FN to the queue of functions to be parsed later. */
18038 TREE_VALUE (parser->unparsed_functions_queues)
18039 = tree_cons (NULL_TREE, fn,
18040 TREE_VALUE (parser->unparsed_functions_queues));
18045 /* Parse a template-argument-list, as well as the trailing ">" (but
18046 not the opening ">"). See cp_parser_template_argument_list for the
18050 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18054 tree saved_qualifying_scope;
18055 tree saved_object_scope;
18056 bool saved_greater_than_is_operator_p;
18057 bool saved_skip_evaluation;
18061 When parsing a template-id, the first non-nested `>' is taken as
18062 the end of the template-argument-list rather than a greater-than
18064 saved_greater_than_is_operator_p
18065 = parser->greater_than_is_operator_p;
18066 parser->greater_than_is_operator_p = false;
18067 /* Parsing the argument list may modify SCOPE, so we save it
18069 saved_scope = parser->scope;
18070 saved_qualifying_scope = parser->qualifying_scope;
18071 saved_object_scope = parser->object_scope;
18072 /* We need to evaluate the template arguments, even though this
18073 template-id may be nested within a "sizeof". */
18074 saved_skip_evaluation = skip_evaluation;
18075 skip_evaluation = false;
18076 /* Parse the template-argument-list itself. */
18077 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18078 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18079 arguments = NULL_TREE;
18081 arguments = cp_parser_template_argument_list (parser);
18082 /* Look for the `>' that ends the template-argument-list. If we find
18083 a '>>' instead, it's probably just a typo. */
18084 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18086 if (cxx_dialect != cxx98)
18088 /* In C++0x, a `>>' in a template argument list or cast
18089 expression is considered to be two separate `>'
18090 tokens. So, change the current token to a `>', but don't
18091 consume it: it will be consumed later when the outer
18092 template argument list (or cast expression) is parsed.
18093 Note that this replacement of `>' for `>>' is necessary
18094 even if we are parsing tentatively: in the tentative
18095 case, after calling
18096 cp_parser_enclosed_template_argument_list we will always
18097 throw away all of the template arguments and the first
18098 closing `>', either because the template argument list
18099 was erroneous or because we are replacing those tokens
18100 with a CPP_TEMPLATE_ID token. The second `>' (which will
18101 not have been thrown away) is needed either to close an
18102 outer template argument list or to complete a new-style
18104 cp_token *token = cp_lexer_peek_token (parser->lexer);
18105 token->type = CPP_GREATER;
18107 else if (!saved_greater_than_is_operator_p)
18109 /* If we're in a nested template argument list, the '>>' has
18110 to be a typo for '> >'. We emit the error message, but we
18111 continue parsing and we push a '>' as next token, so that
18112 the argument list will be parsed correctly. Note that the
18113 global source location is still on the token before the
18114 '>>', so we need to say explicitly where we want it. */
18115 cp_token *token = cp_lexer_peek_token (parser->lexer);
18116 error ("%H%<>>%> should be %<> >%> "
18117 "within a nested template argument list",
18120 token->type = CPP_GREATER;
18124 /* If this is not a nested template argument list, the '>>'
18125 is a typo for '>'. Emit an error message and continue.
18126 Same deal about the token location, but here we can get it
18127 right by consuming the '>>' before issuing the diagnostic. */
18128 cp_token *token = cp_lexer_consume_token (parser->lexer);
18129 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18130 "a template argument list", &token->location);
18134 cp_parser_skip_to_end_of_template_parameter_list (parser);
18135 /* The `>' token might be a greater-than operator again now. */
18136 parser->greater_than_is_operator_p
18137 = saved_greater_than_is_operator_p;
18138 /* Restore the SAVED_SCOPE. */
18139 parser->scope = saved_scope;
18140 parser->qualifying_scope = saved_qualifying_scope;
18141 parser->object_scope = saved_object_scope;
18142 skip_evaluation = saved_skip_evaluation;
18147 /* MEMBER_FUNCTION is a member function, or a friend. If default
18148 arguments, or the body of the function have not yet been parsed,
18152 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18154 /* If this member is a template, get the underlying
18156 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18157 member_function = DECL_TEMPLATE_RESULT (member_function);
18159 /* There should not be any class definitions in progress at this
18160 point; the bodies of members are only parsed outside of all class
18162 gcc_assert (parser->num_classes_being_defined == 0);
18163 /* While we're parsing the member functions we might encounter more
18164 classes. We want to handle them right away, but we don't want
18165 them getting mixed up with functions that are currently in the
18167 parser->unparsed_functions_queues
18168 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18170 /* Make sure that any template parameters are in scope. */
18171 maybe_begin_member_template_processing (member_function);
18173 /* If the body of the function has not yet been parsed, parse it
18175 if (DECL_PENDING_INLINE_P (member_function))
18177 tree function_scope;
18178 cp_token_cache *tokens;
18180 /* The function is no longer pending; we are processing it. */
18181 tokens = DECL_PENDING_INLINE_INFO (member_function);
18182 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18183 DECL_PENDING_INLINE_P (member_function) = 0;
18185 /* If this is a local class, enter the scope of the containing
18187 function_scope = current_function_decl;
18188 if (function_scope)
18189 push_function_context ();
18191 /* Push the body of the function onto the lexer stack. */
18192 cp_parser_push_lexer_for_tokens (parser, tokens);
18194 /* Let the front end know that we going to be defining this
18196 start_preparsed_function (member_function, NULL_TREE,
18197 SF_PRE_PARSED | SF_INCLASS_INLINE);
18199 /* Don't do access checking if it is a templated function. */
18200 if (processing_template_decl)
18201 push_deferring_access_checks (dk_no_check);
18203 /* Now, parse the body of the function. */
18204 cp_parser_function_definition_after_declarator (parser,
18205 /*inline_p=*/true);
18207 if (processing_template_decl)
18208 pop_deferring_access_checks ();
18210 /* Leave the scope of the containing function. */
18211 if (function_scope)
18212 pop_function_context ();
18213 cp_parser_pop_lexer (parser);
18216 /* Remove any template parameters from the symbol table. */
18217 maybe_end_member_template_processing ();
18219 /* Restore the queue. */
18220 parser->unparsed_functions_queues
18221 = TREE_CHAIN (parser->unparsed_functions_queues);
18224 /* If DECL contains any default args, remember it on the unparsed
18225 functions queue. */
18228 cp_parser_save_default_args (cp_parser* parser, tree decl)
18232 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18234 probe = TREE_CHAIN (probe))
18235 if (TREE_PURPOSE (probe))
18237 TREE_PURPOSE (parser->unparsed_functions_queues)
18238 = tree_cons (current_class_type, decl,
18239 TREE_PURPOSE (parser->unparsed_functions_queues));
18244 /* FN is a FUNCTION_DECL which may contains a parameter with an
18245 unparsed DEFAULT_ARG. Parse the default args now. This function
18246 assumes that the current scope is the scope in which the default
18247 argument should be processed. */
18250 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18252 bool saved_local_variables_forbidden_p;
18255 /* While we're parsing the default args, we might (due to the
18256 statement expression extension) encounter more classes. We want
18257 to handle them right away, but we don't want them getting mixed
18258 up with default args that are currently in the queue. */
18259 parser->unparsed_functions_queues
18260 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18262 /* Local variable names (and the `this' keyword) may not appear
18263 in a default argument. */
18264 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18265 parser->local_variables_forbidden_p = true;
18267 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18269 parm = TREE_CHAIN (parm))
18271 cp_token_cache *tokens;
18272 tree default_arg = TREE_PURPOSE (parm);
18274 VEC(tree,gc) *insts;
18281 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18282 /* This can happen for a friend declaration for a function
18283 already declared with default arguments. */
18286 /* Push the saved tokens for the default argument onto the parser's
18288 tokens = DEFARG_TOKENS (default_arg);
18289 cp_parser_push_lexer_for_tokens (parser, tokens);
18291 /* Parse the assignment-expression. */
18292 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18294 if (!processing_template_decl)
18295 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18297 TREE_PURPOSE (parm) = parsed_arg;
18299 /* Update any instantiations we've already created. */
18300 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18301 VEC_iterate (tree, insts, ix, copy); ix++)
18302 TREE_PURPOSE (copy) = parsed_arg;
18304 /* If the token stream has not been completely used up, then
18305 there was extra junk after the end of the default
18307 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18308 cp_parser_error (parser, "expected %<,%>");
18310 /* Revert to the main lexer. */
18311 cp_parser_pop_lexer (parser);
18314 /* Make sure no default arg is missing. */
18315 check_default_args (fn);
18317 /* Restore the state of local_variables_forbidden_p. */
18318 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18320 /* Restore the queue. */
18321 parser->unparsed_functions_queues
18322 = TREE_CHAIN (parser->unparsed_functions_queues);
18325 /* Parse the operand of `sizeof' (or a similar operator). Returns
18326 either a TYPE or an expression, depending on the form of the
18327 input. The KEYWORD indicates which kind of expression we have
18331 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18333 tree expr = NULL_TREE;
18334 const char *saved_message;
18336 bool saved_integral_constant_expression_p;
18337 bool saved_non_integral_constant_expression_p;
18338 bool pack_expansion_p = false;
18340 /* Types cannot be defined in a `sizeof' expression. Save away the
18342 saved_message = parser->type_definition_forbidden_message;
18343 /* And create the new one. */
18344 tmp = concat ("types may not be defined in %<",
18345 IDENTIFIER_POINTER (ridpointers[keyword]),
18346 "%> expressions", NULL);
18347 parser->type_definition_forbidden_message = tmp;
18349 /* The restrictions on constant-expressions do not apply inside
18350 sizeof expressions. */
18351 saved_integral_constant_expression_p
18352 = parser->integral_constant_expression_p;
18353 saved_non_integral_constant_expression_p
18354 = parser->non_integral_constant_expression_p;
18355 parser->integral_constant_expression_p = false;
18357 /* If it's a `...', then we are computing the length of a parameter
18359 if (keyword == RID_SIZEOF
18360 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18362 /* Consume the `...'. */
18363 cp_lexer_consume_token (parser->lexer);
18364 maybe_warn_variadic_templates ();
18366 /* Note that this is an expansion. */
18367 pack_expansion_p = true;
18370 /* Do not actually evaluate the expression. */
18372 /* If it's a `(', then we might be looking at the type-id
18374 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18377 bool saved_in_type_id_in_expr_p;
18379 /* We can't be sure yet whether we're looking at a type-id or an
18381 cp_parser_parse_tentatively (parser);
18382 /* Consume the `('. */
18383 cp_lexer_consume_token (parser->lexer);
18384 /* Parse the type-id. */
18385 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18386 parser->in_type_id_in_expr_p = true;
18387 type = cp_parser_type_id (parser);
18388 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18389 /* Now, look for the trailing `)'. */
18390 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18391 /* If all went well, then we're done. */
18392 if (cp_parser_parse_definitely (parser))
18394 cp_decl_specifier_seq decl_specs;
18396 /* Build a trivial decl-specifier-seq. */
18397 clear_decl_specs (&decl_specs);
18398 decl_specs.type = type;
18400 /* Call grokdeclarator to figure out what type this is. */
18401 expr = grokdeclarator (NULL,
18405 /*attrlist=*/NULL);
18409 /* If the type-id production did not work out, then we must be
18410 looking at the unary-expression production. */
18412 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18413 /*cast_p=*/false, NULL);
18415 if (pack_expansion_p)
18416 /* Build a pack expansion. */
18417 expr = make_pack_expansion (expr);
18419 /* Go back to evaluating expressions. */
18422 /* Free the message we created. */
18424 /* And restore the old one. */
18425 parser->type_definition_forbidden_message = saved_message;
18426 parser->integral_constant_expression_p
18427 = saved_integral_constant_expression_p;
18428 parser->non_integral_constant_expression_p
18429 = saved_non_integral_constant_expression_p;
18434 /* If the current declaration has no declarator, return true. */
18437 cp_parser_declares_only_class_p (cp_parser *parser)
18439 /* If the next token is a `;' or a `,' then there is no
18441 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18442 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18445 /* Update the DECL_SPECS to reflect the storage class indicated by
18449 cp_parser_set_storage_class (cp_parser *parser,
18450 cp_decl_specifier_seq *decl_specs,
18452 location_t location)
18454 cp_storage_class storage_class;
18456 if (parser->in_unbraced_linkage_specification_p)
18458 error ("%Hinvalid use of %qD in linkage specification",
18459 &location, ridpointers[keyword]);
18462 else if (decl_specs->storage_class != sc_none)
18464 decl_specs->conflicting_specifiers_p = true;
18468 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18469 && decl_specs->specs[(int) ds_thread])
18471 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18472 decl_specs->specs[(int) ds_thread] = 0;
18478 storage_class = sc_auto;
18481 storage_class = sc_register;
18484 storage_class = sc_static;
18487 storage_class = sc_extern;
18490 storage_class = sc_mutable;
18493 gcc_unreachable ();
18495 decl_specs->storage_class = storage_class;
18497 /* A storage class specifier cannot be applied alongside a typedef
18498 specifier. If there is a typedef specifier present then set
18499 conflicting_specifiers_p which will trigger an error later
18500 on in grokdeclarator. */
18501 if (decl_specs->specs[(int)ds_typedef])
18502 decl_specs->conflicting_specifiers_p = true;
18505 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18506 is true, the type is a user-defined type; otherwise it is a
18507 built-in type specified by a keyword. */
18510 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18512 location_t location,
18513 bool user_defined_p)
18515 decl_specs->any_specifiers_p = true;
18517 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18518 (with, for example, in "typedef int wchar_t;") we remember that
18519 this is what happened. In system headers, we ignore these
18520 declarations so that G++ can work with system headers that are not
18522 if (decl_specs->specs[(int) ds_typedef]
18524 && (type_spec == boolean_type_node
18525 || type_spec == char16_type_node
18526 || type_spec == char32_type_node
18527 || type_spec == wchar_type_node)
18528 && (decl_specs->type
18529 || decl_specs->specs[(int) ds_long]
18530 || decl_specs->specs[(int) ds_short]
18531 || decl_specs->specs[(int) ds_unsigned]
18532 || decl_specs->specs[(int) ds_signed]))
18534 decl_specs->redefined_builtin_type = type_spec;
18535 if (!decl_specs->type)
18537 decl_specs->type = type_spec;
18538 decl_specs->user_defined_type_p = false;
18539 decl_specs->type_location = location;
18542 else if (decl_specs->type)
18543 decl_specs->multiple_types_p = true;
18546 decl_specs->type = type_spec;
18547 decl_specs->user_defined_type_p = user_defined_p;
18548 decl_specs->redefined_builtin_type = NULL_TREE;
18549 decl_specs->type_location = location;
18553 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18554 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18557 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18559 return decl_specifiers->specs[(int) ds_friend] != 0;
18562 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18563 issue an error message indicating that TOKEN_DESC was expected.
18565 Returns the token consumed, if the token had the appropriate type.
18566 Otherwise, returns NULL. */
18569 cp_parser_require (cp_parser* parser,
18570 enum cpp_ttype type,
18571 const char* token_desc)
18573 if (cp_lexer_next_token_is (parser->lexer, type))
18574 return cp_lexer_consume_token (parser->lexer);
18577 /* Output the MESSAGE -- unless we're parsing tentatively. */
18578 if (!cp_parser_simulate_error (parser))
18580 char *message = concat ("expected ", token_desc, NULL);
18581 cp_parser_error (parser, message);
18588 /* An error message is produced if the next token is not '>'.
18589 All further tokens are skipped until the desired token is
18590 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18593 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18595 /* Current level of '< ... >'. */
18596 unsigned level = 0;
18597 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18598 unsigned nesting_depth = 0;
18600 /* Are we ready, yet? If not, issue error message. */
18601 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18604 /* Skip tokens until the desired token is found. */
18607 /* Peek at the next token. */
18608 switch (cp_lexer_peek_token (parser->lexer)->type)
18611 if (!nesting_depth)
18616 if (cxx_dialect == cxx98)
18617 /* C++0x views the `>>' operator as two `>' tokens, but
18620 else if (!nesting_depth && level-- == 0)
18622 /* We've hit a `>>' where the first `>' closes the
18623 template argument list, and the second `>' is
18624 spurious. Just consume the `>>' and stop; we've
18625 already produced at least one error. */
18626 cp_lexer_consume_token (parser->lexer);
18629 /* Fall through for C++0x, so we handle the second `>' in
18633 if (!nesting_depth && level-- == 0)
18635 /* We've reached the token we want, consume it and stop. */
18636 cp_lexer_consume_token (parser->lexer);
18641 case CPP_OPEN_PAREN:
18642 case CPP_OPEN_SQUARE:
18646 case CPP_CLOSE_PAREN:
18647 case CPP_CLOSE_SQUARE:
18648 if (nesting_depth-- == 0)
18653 case CPP_PRAGMA_EOL:
18654 case CPP_SEMICOLON:
18655 case CPP_OPEN_BRACE:
18656 case CPP_CLOSE_BRACE:
18657 /* The '>' was probably forgotten, don't look further. */
18664 /* Consume this token. */
18665 cp_lexer_consume_token (parser->lexer);
18669 /* If the next token is the indicated keyword, consume it. Otherwise,
18670 issue an error message indicating that TOKEN_DESC was expected.
18672 Returns the token consumed, if the token had the appropriate type.
18673 Otherwise, returns NULL. */
18676 cp_parser_require_keyword (cp_parser* parser,
18678 const char* token_desc)
18680 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18682 if (token && token->keyword != keyword)
18684 dyn_string_t error_msg;
18686 /* Format the error message. */
18687 error_msg = dyn_string_new (0);
18688 dyn_string_append_cstr (error_msg, "expected ");
18689 dyn_string_append_cstr (error_msg, token_desc);
18690 cp_parser_error (parser, error_msg->s);
18691 dyn_string_delete (error_msg);
18698 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18699 function-definition. */
18702 cp_parser_token_starts_function_definition_p (cp_token* token)
18704 return (/* An ordinary function-body begins with an `{'. */
18705 token->type == CPP_OPEN_BRACE
18706 /* A ctor-initializer begins with a `:'. */
18707 || token->type == CPP_COLON
18708 /* A function-try-block begins with `try'. */
18709 || token->keyword == RID_TRY
18710 /* The named return value extension begins with `return'. */
18711 || token->keyword == RID_RETURN);
18714 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18718 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18722 token = cp_lexer_peek_token (parser->lexer);
18723 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18726 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18727 C++0x) ending a template-argument. */
18730 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18734 token = cp_lexer_peek_token (parser->lexer);
18735 return (token->type == CPP_COMMA
18736 || token->type == CPP_GREATER
18737 || token->type == CPP_ELLIPSIS
18738 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18741 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18742 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18745 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18750 token = cp_lexer_peek_nth_token (parser->lexer, n);
18751 if (token->type == CPP_LESS)
18753 /* Check for the sequence `<::' in the original code. It would be lexed as
18754 `[:', where `[' is a digraph, and there is no whitespace before
18756 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18759 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18760 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18766 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18767 or none_type otherwise. */
18769 static enum tag_types
18770 cp_parser_token_is_class_key (cp_token* token)
18772 switch (token->keyword)
18777 return record_type;
18786 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18789 cp_parser_check_class_key (enum tag_types class_key, tree type)
18791 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18792 permerror (input_location, "%qs tag used in naming %q#T",
18793 class_key == union_type ? "union"
18794 : class_key == record_type ? "struct" : "class",
18798 /* Issue an error message if DECL is redeclared with different
18799 access than its original declaration [class.access.spec/3].
18800 This applies to nested classes and nested class templates.
18804 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18806 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18809 if ((TREE_PRIVATE (decl)
18810 != (current_access_specifier == access_private_node))
18811 || (TREE_PROTECTED (decl)
18812 != (current_access_specifier == access_protected_node)))
18813 error ("%H%qD redeclared with different access", &location, decl);
18816 /* Look for the `template' keyword, as a syntactic disambiguator.
18817 Return TRUE iff it is present, in which case it will be
18821 cp_parser_optional_template_keyword (cp_parser *parser)
18823 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18825 /* The `template' keyword can only be used within templates;
18826 outside templates the parser can always figure out what is a
18827 template and what is not. */
18828 if (!processing_template_decl)
18830 cp_token *token = cp_lexer_peek_token (parser->lexer);
18831 error ("%H%<template%> (as a disambiguator) is only allowed "
18832 "within templates", &token->location);
18833 /* If this part of the token stream is rescanned, the same
18834 error message would be generated. So, we purge the token
18835 from the stream. */
18836 cp_lexer_purge_token (parser->lexer);
18841 /* Consume the `template' keyword. */
18842 cp_lexer_consume_token (parser->lexer);
18850 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18851 set PARSER->SCOPE, and perform other related actions. */
18854 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18857 struct tree_check *check_value;
18858 deferred_access_check *chk;
18859 VEC (deferred_access_check,gc) *checks;
18861 /* Get the stored value. */
18862 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18863 /* Perform any access checks that were deferred. */
18864 checks = check_value->checks;
18868 VEC_iterate (deferred_access_check, checks, i, chk) ;
18871 perform_or_defer_access_check (chk->binfo,
18876 /* Set the scope from the stored value. */
18877 parser->scope = check_value->value;
18878 parser->qualifying_scope = check_value->qualifying_scope;
18879 parser->object_scope = NULL_TREE;
18882 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18883 encounter the end of a block before what we were looking for. */
18886 cp_parser_cache_group (cp_parser *parser,
18887 enum cpp_ttype end,
18892 cp_token *token = cp_lexer_peek_token (parser->lexer);
18894 /* Abort a parenthesized expression if we encounter a semicolon. */
18895 if ((end == CPP_CLOSE_PAREN || depth == 0)
18896 && token->type == CPP_SEMICOLON)
18898 /* If we've reached the end of the file, stop. */
18899 if (token->type == CPP_EOF
18900 || (end != CPP_PRAGMA_EOL
18901 && token->type == CPP_PRAGMA_EOL))
18903 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18904 /* We've hit the end of an enclosing block, so there's been some
18905 kind of syntax error. */
18908 /* Consume the token. */
18909 cp_lexer_consume_token (parser->lexer);
18910 /* See if it starts a new group. */
18911 if (token->type == CPP_OPEN_BRACE)
18913 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18914 /* In theory this should probably check end == '}', but
18915 cp_parser_save_member_function_body needs it to exit
18916 after either '}' or ')' when called with ')'. */
18920 else if (token->type == CPP_OPEN_PAREN)
18922 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18923 if (depth == 0 && end == CPP_CLOSE_PAREN)
18926 else if (token->type == CPP_PRAGMA)
18927 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18928 else if (token->type == end)
18933 /* Begin parsing tentatively. We always save tokens while parsing
18934 tentatively so that if the tentative parsing fails we can restore the
18938 cp_parser_parse_tentatively (cp_parser* parser)
18940 /* Enter a new parsing context. */
18941 parser->context = cp_parser_context_new (parser->context);
18942 /* Begin saving tokens. */
18943 cp_lexer_save_tokens (parser->lexer);
18944 /* In order to avoid repetitive access control error messages,
18945 access checks are queued up until we are no longer parsing
18947 push_deferring_access_checks (dk_deferred);
18950 /* Commit to the currently active tentative parse. */
18953 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18955 cp_parser_context *context;
18958 /* Mark all of the levels as committed. */
18959 lexer = parser->lexer;
18960 for (context = parser->context; context->next; context = context->next)
18962 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18964 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18965 while (!cp_lexer_saving_tokens (lexer))
18966 lexer = lexer->next;
18967 cp_lexer_commit_tokens (lexer);
18971 /* Abort the currently active tentative parse. All consumed tokens
18972 will be rolled back, and no diagnostics will be issued. */
18975 cp_parser_abort_tentative_parse (cp_parser* parser)
18977 cp_parser_simulate_error (parser);
18978 /* Now, pretend that we want to see if the construct was
18979 successfully parsed. */
18980 cp_parser_parse_definitely (parser);
18983 /* Stop parsing tentatively. If a parse error has occurred, restore the
18984 token stream. Otherwise, commit to the tokens we have consumed.
18985 Returns true if no error occurred; false otherwise. */
18988 cp_parser_parse_definitely (cp_parser* parser)
18990 bool error_occurred;
18991 cp_parser_context *context;
18993 /* Remember whether or not an error occurred, since we are about to
18994 destroy that information. */
18995 error_occurred = cp_parser_error_occurred (parser);
18996 /* Remove the topmost context from the stack. */
18997 context = parser->context;
18998 parser->context = context->next;
18999 /* If no parse errors occurred, commit to the tentative parse. */
19000 if (!error_occurred)
19002 /* Commit to the tokens read tentatively, unless that was
19004 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19005 cp_lexer_commit_tokens (parser->lexer);
19007 pop_to_parent_deferring_access_checks ();
19009 /* Otherwise, if errors occurred, roll back our state so that things
19010 are just as they were before we began the tentative parse. */
19013 cp_lexer_rollback_tokens (parser->lexer);
19014 pop_deferring_access_checks ();
19016 /* Add the context to the front of the free list. */
19017 context->next = cp_parser_context_free_list;
19018 cp_parser_context_free_list = context;
19020 return !error_occurred;
19023 /* Returns true if we are parsing tentatively and are not committed to
19024 this tentative parse. */
19027 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19029 return (cp_parser_parsing_tentatively (parser)
19030 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19033 /* Returns nonzero iff an error has occurred during the most recent
19034 tentative parse. */
19037 cp_parser_error_occurred (cp_parser* parser)
19039 return (cp_parser_parsing_tentatively (parser)
19040 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19043 /* Returns nonzero if GNU extensions are allowed. */
19046 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19048 return parser->allow_gnu_extensions_p;
19051 /* Objective-C++ Productions */
19054 /* Parse an Objective-C expression, which feeds into a primary-expression
19058 objc-message-expression
19059 objc-string-literal
19060 objc-encode-expression
19061 objc-protocol-expression
19062 objc-selector-expression
19064 Returns a tree representation of the expression. */
19067 cp_parser_objc_expression (cp_parser* parser)
19069 /* Try to figure out what kind of declaration is present. */
19070 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19074 case CPP_OPEN_SQUARE:
19075 return cp_parser_objc_message_expression (parser);
19077 case CPP_OBJC_STRING:
19078 kwd = cp_lexer_consume_token (parser->lexer);
19079 return objc_build_string_object (kwd->u.value);
19082 switch (kwd->keyword)
19084 case RID_AT_ENCODE:
19085 return cp_parser_objc_encode_expression (parser);
19087 case RID_AT_PROTOCOL:
19088 return cp_parser_objc_protocol_expression (parser);
19090 case RID_AT_SELECTOR:
19091 return cp_parser_objc_selector_expression (parser);
19097 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19098 &kwd->location, kwd->u.value);
19099 cp_parser_skip_to_end_of_block_or_statement (parser);
19102 return error_mark_node;
19105 /* Parse an Objective-C message expression.
19107 objc-message-expression:
19108 [ objc-message-receiver objc-message-args ]
19110 Returns a representation of an Objective-C message. */
19113 cp_parser_objc_message_expression (cp_parser* parser)
19115 tree receiver, messageargs;
19117 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19118 receiver = cp_parser_objc_message_receiver (parser);
19119 messageargs = cp_parser_objc_message_args (parser);
19120 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19122 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19125 /* Parse an objc-message-receiver.
19127 objc-message-receiver:
19129 simple-type-specifier
19131 Returns a representation of the type or expression. */
19134 cp_parser_objc_message_receiver (cp_parser* parser)
19138 /* An Objective-C message receiver may be either (1) a type
19139 or (2) an expression. */
19140 cp_parser_parse_tentatively (parser);
19141 rcv = cp_parser_expression (parser, false, NULL);
19143 if (cp_parser_parse_definitely (parser))
19146 rcv = cp_parser_simple_type_specifier (parser,
19147 /*decl_specs=*/NULL,
19148 CP_PARSER_FLAGS_NONE);
19150 return objc_get_class_reference (rcv);
19153 /* Parse the arguments and selectors comprising an Objective-C message.
19158 objc-selector-args , objc-comma-args
19160 objc-selector-args:
19161 objc-selector [opt] : assignment-expression
19162 objc-selector-args objc-selector [opt] : assignment-expression
19165 assignment-expression
19166 objc-comma-args , assignment-expression
19168 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19169 selector arguments and TREE_VALUE containing a list of comma
19173 cp_parser_objc_message_args (cp_parser* parser)
19175 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19176 bool maybe_unary_selector_p = true;
19177 cp_token *token = cp_lexer_peek_token (parser->lexer);
19179 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19181 tree selector = NULL_TREE, arg;
19183 if (token->type != CPP_COLON)
19184 selector = cp_parser_objc_selector (parser);
19186 /* Detect if we have a unary selector. */
19187 if (maybe_unary_selector_p
19188 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19189 return build_tree_list (selector, NULL_TREE);
19191 maybe_unary_selector_p = false;
19192 cp_parser_require (parser, CPP_COLON, "%<:%>");
19193 arg = cp_parser_assignment_expression (parser, false, NULL);
19196 = chainon (sel_args,
19197 build_tree_list (selector, arg));
19199 token = cp_lexer_peek_token (parser->lexer);
19202 /* Handle non-selector arguments, if any. */
19203 while (token->type == CPP_COMMA)
19207 cp_lexer_consume_token (parser->lexer);
19208 arg = cp_parser_assignment_expression (parser, false, NULL);
19211 = chainon (addl_args,
19212 build_tree_list (NULL_TREE, arg));
19214 token = cp_lexer_peek_token (parser->lexer);
19217 return build_tree_list (sel_args, addl_args);
19220 /* Parse an Objective-C encode expression.
19222 objc-encode-expression:
19223 @encode objc-typename
19225 Returns an encoded representation of the type argument. */
19228 cp_parser_objc_encode_expression (cp_parser* parser)
19233 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19234 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19235 token = cp_lexer_peek_token (parser->lexer);
19236 type = complete_type (cp_parser_type_id (parser));
19237 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19241 error ("%H%<@encode%> must specify a type as an argument",
19243 return error_mark_node;
19246 return objc_build_encode_expr (type);
19249 /* Parse an Objective-C @defs expression. */
19252 cp_parser_objc_defs_expression (cp_parser *parser)
19256 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19257 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19258 name = cp_parser_identifier (parser);
19259 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19261 return objc_get_class_ivars (name);
19264 /* Parse an Objective-C protocol expression.
19266 objc-protocol-expression:
19267 @protocol ( identifier )
19269 Returns a representation of the protocol expression. */
19272 cp_parser_objc_protocol_expression (cp_parser* parser)
19276 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19277 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19278 proto = cp_parser_identifier (parser);
19279 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19281 return objc_build_protocol_expr (proto);
19284 /* Parse an Objective-C selector expression.
19286 objc-selector-expression:
19287 @selector ( objc-method-signature )
19289 objc-method-signature:
19295 objc-selector-seq objc-selector :
19297 Returns a representation of the method selector. */
19300 cp_parser_objc_selector_expression (cp_parser* parser)
19302 tree sel_seq = NULL_TREE;
19303 bool maybe_unary_selector_p = true;
19306 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19307 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19308 token = cp_lexer_peek_token (parser->lexer);
19310 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19311 || token->type == CPP_SCOPE)
19313 tree selector = NULL_TREE;
19315 if (token->type != CPP_COLON
19316 || token->type == CPP_SCOPE)
19317 selector = cp_parser_objc_selector (parser);
19319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19320 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19322 /* Detect if we have a unary selector. */
19323 if (maybe_unary_selector_p)
19325 sel_seq = selector;
19326 goto finish_selector;
19330 cp_parser_error (parser, "expected %<:%>");
19333 maybe_unary_selector_p = false;
19334 token = cp_lexer_consume_token (parser->lexer);
19336 if (token->type == CPP_SCOPE)
19339 = chainon (sel_seq,
19340 build_tree_list (selector, NULL_TREE));
19342 = chainon (sel_seq,
19343 build_tree_list (NULL_TREE, NULL_TREE));
19347 = chainon (sel_seq,
19348 build_tree_list (selector, NULL_TREE));
19350 token = cp_lexer_peek_token (parser->lexer);
19354 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19356 return objc_build_selector_expr (sel_seq);
19359 /* Parse a list of identifiers.
19361 objc-identifier-list:
19363 objc-identifier-list , identifier
19365 Returns a TREE_LIST of identifier nodes. */
19368 cp_parser_objc_identifier_list (cp_parser* parser)
19370 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19371 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19373 while (sep->type == CPP_COMMA)
19375 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19376 list = chainon (list,
19377 build_tree_list (NULL_TREE,
19378 cp_parser_identifier (parser)));
19379 sep = cp_lexer_peek_token (parser->lexer);
19385 /* Parse an Objective-C alias declaration.
19387 objc-alias-declaration:
19388 @compatibility_alias identifier identifier ;
19390 This function registers the alias mapping with the Objective-C front end.
19391 It returns nothing. */
19394 cp_parser_objc_alias_declaration (cp_parser* parser)
19398 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19399 alias = cp_parser_identifier (parser);
19400 orig = cp_parser_identifier (parser);
19401 objc_declare_alias (alias, orig);
19402 cp_parser_consume_semicolon_at_end_of_statement (parser);
19405 /* Parse an Objective-C class forward-declaration.
19407 objc-class-declaration:
19408 @class objc-identifier-list ;
19410 The function registers the forward declarations with the Objective-C
19411 front end. It returns nothing. */
19414 cp_parser_objc_class_declaration (cp_parser* parser)
19416 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19417 objc_declare_class (cp_parser_objc_identifier_list (parser));
19418 cp_parser_consume_semicolon_at_end_of_statement (parser);
19421 /* Parse a list of Objective-C protocol references.
19423 objc-protocol-refs-opt:
19424 objc-protocol-refs [opt]
19426 objc-protocol-refs:
19427 < objc-identifier-list >
19429 Returns a TREE_LIST of identifiers, if any. */
19432 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19434 tree protorefs = NULL_TREE;
19436 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19438 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19439 protorefs = cp_parser_objc_identifier_list (parser);
19440 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19446 /* Parse a Objective-C visibility specification. */
19449 cp_parser_objc_visibility_spec (cp_parser* parser)
19451 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19453 switch (vis->keyword)
19455 case RID_AT_PRIVATE:
19456 objc_set_visibility (2);
19458 case RID_AT_PROTECTED:
19459 objc_set_visibility (0);
19461 case RID_AT_PUBLIC:
19462 objc_set_visibility (1);
19468 /* Eat '@private'/'@protected'/'@public'. */
19469 cp_lexer_consume_token (parser->lexer);
19472 /* Parse an Objective-C method type. */
19475 cp_parser_objc_method_type (cp_parser* parser)
19477 objc_set_method_type
19478 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19483 /* Parse an Objective-C protocol qualifier. */
19486 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19488 tree quals = NULL_TREE, node;
19489 cp_token *token = cp_lexer_peek_token (parser->lexer);
19491 node = token->u.value;
19493 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19494 && (node == ridpointers [(int) RID_IN]
19495 || node == ridpointers [(int) RID_OUT]
19496 || node == ridpointers [(int) RID_INOUT]
19497 || node == ridpointers [(int) RID_BYCOPY]
19498 || node == ridpointers [(int) RID_BYREF]
19499 || node == ridpointers [(int) RID_ONEWAY]))
19501 quals = tree_cons (NULL_TREE, node, quals);
19502 cp_lexer_consume_token (parser->lexer);
19503 token = cp_lexer_peek_token (parser->lexer);
19504 node = token->u.value;
19510 /* Parse an Objective-C typename. */
19513 cp_parser_objc_typename (cp_parser* parser)
19515 tree type_name = NULL_TREE;
19517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19519 tree proto_quals, cp_type = NULL_TREE;
19521 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19522 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19524 /* An ObjC type name may consist of just protocol qualifiers, in which
19525 case the type shall default to 'id'. */
19526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19527 cp_type = cp_parser_type_id (parser);
19529 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19530 type_name = build_tree_list (proto_quals, cp_type);
19536 /* Check to see if TYPE refers to an Objective-C selector name. */
19539 cp_parser_objc_selector_p (enum cpp_ttype type)
19541 return (type == CPP_NAME || type == CPP_KEYWORD
19542 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19543 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19544 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19545 || type == CPP_XOR || type == CPP_XOR_EQ);
19548 /* Parse an Objective-C selector. */
19551 cp_parser_objc_selector (cp_parser* parser)
19553 cp_token *token = cp_lexer_consume_token (parser->lexer);
19555 if (!cp_parser_objc_selector_p (token->type))
19557 error ("%Hinvalid Objective-C++ selector name", &token->location);
19558 return error_mark_node;
19561 /* C++ operator names are allowed to appear in ObjC selectors. */
19562 switch (token->type)
19564 case CPP_AND_AND: return get_identifier ("and");
19565 case CPP_AND_EQ: return get_identifier ("and_eq");
19566 case CPP_AND: return get_identifier ("bitand");
19567 case CPP_OR: return get_identifier ("bitor");
19568 case CPP_COMPL: return get_identifier ("compl");
19569 case CPP_NOT: return get_identifier ("not");
19570 case CPP_NOT_EQ: return get_identifier ("not_eq");
19571 case CPP_OR_OR: return get_identifier ("or");
19572 case CPP_OR_EQ: return get_identifier ("or_eq");
19573 case CPP_XOR: return get_identifier ("xor");
19574 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19575 default: return token->u.value;
19579 /* Parse an Objective-C params list. */
19582 cp_parser_objc_method_keyword_params (cp_parser* parser)
19584 tree params = NULL_TREE;
19585 bool maybe_unary_selector_p = true;
19586 cp_token *token = cp_lexer_peek_token (parser->lexer);
19588 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19590 tree selector = NULL_TREE, type_name, identifier;
19592 if (token->type != CPP_COLON)
19593 selector = cp_parser_objc_selector (parser);
19595 /* Detect if we have a unary selector. */
19596 if (maybe_unary_selector_p
19597 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19600 maybe_unary_selector_p = false;
19601 cp_parser_require (parser, CPP_COLON, "%<:%>");
19602 type_name = cp_parser_objc_typename (parser);
19603 identifier = cp_parser_identifier (parser);
19607 objc_build_keyword_decl (selector,
19611 token = cp_lexer_peek_token (parser->lexer);
19617 /* Parse the non-keyword Objective-C params. */
19620 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19622 tree params = make_node (TREE_LIST);
19623 cp_token *token = cp_lexer_peek_token (parser->lexer);
19624 *ellipsisp = false; /* Initially, assume no ellipsis. */
19626 while (token->type == CPP_COMMA)
19628 cp_parameter_declarator *parmdecl;
19631 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19632 token = cp_lexer_peek_token (parser->lexer);
19634 if (token->type == CPP_ELLIPSIS)
19636 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19641 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19642 parm = grokdeclarator (parmdecl->declarator,
19643 &parmdecl->decl_specifiers,
19644 PARM, /*initialized=*/0,
19645 /*attrlist=*/NULL);
19647 chainon (params, build_tree_list (NULL_TREE, parm));
19648 token = cp_lexer_peek_token (parser->lexer);
19654 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19657 cp_parser_objc_interstitial_code (cp_parser* parser)
19659 cp_token *token = cp_lexer_peek_token (parser->lexer);
19661 /* If the next token is `extern' and the following token is a string
19662 literal, then we have a linkage specification. */
19663 if (token->keyword == RID_EXTERN
19664 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19665 cp_parser_linkage_specification (parser);
19666 /* Handle #pragma, if any. */
19667 else if (token->type == CPP_PRAGMA)
19668 cp_parser_pragma (parser, pragma_external);
19669 /* Allow stray semicolons. */
19670 else if (token->type == CPP_SEMICOLON)
19671 cp_lexer_consume_token (parser->lexer);
19672 /* Finally, try to parse a block-declaration, or a function-definition. */
19674 cp_parser_block_declaration (parser, /*statement_p=*/false);
19677 /* Parse a method signature. */
19680 cp_parser_objc_method_signature (cp_parser* parser)
19682 tree rettype, kwdparms, optparms;
19683 bool ellipsis = false;
19685 cp_parser_objc_method_type (parser);
19686 rettype = cp_parser_objc_typename (parser);
19687 kwdparms = cp_parser_objc_method_keyword_params (parser);
19688 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19690 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19693 /* Pars an Objective-C method prototype list. */
19696 cp_parser_objc_method_prototype_list (cp_parser* parser)
19698 cp_token *token = cp_lexer_peek_token (parser->lexer);
19700 while (token->keyword != RID_AT_END)
19702 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19704 objc_add_method_declaration
19705 (cp_parser_objc_method_signature (parser));
19706 cp_parser_consume_semicolon_at_end_of_statement (parser);
19709 /* Allow for interspersed non-ObjC++ code. */
19710 cp_parser_objc_interstitial_code (parser);
19712 token = cp_lexer_peek_token (parser->lexer);
19715 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19716 objc_finish_interface ();
19719 /* Parse an Objective-C method definition list. */
19722 cp_parser_objc_method_definition_list (cp_parser* parser)
19724 cp_token *token = cp_lexer_peek_token (parser->lexer);
19726 while (token->keyword != RID_AT_END)
19730 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19732 push_deferring_access_checks (dk_deferred);
19733 objc_start_method_definition
19734 (cp_parser_objc_method_signature (parser));
19736 /* For historical reasons, we accept an optional semicolon. */
19737 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19738 cp_lexer_consume_token (parser->lexer);
19740 perform_deferred_access_checks ();
19741 stop_deferring_access_checks ();
19742 meth = cp_parser_function_definition_after_declarator (parser,
19744 pop_deferring_access_checks ();
19745 objc_finish_method_definition (meth);
19748 /* Allow for interspersed non-ObjC++ code. */
19749 cp_parser_objc_interstitial_code (parser);
19751 token = cp_lexer_peek_token (parser->lexer);
19754 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19755 objc_finish_implementation ();
19758 /* Parse Objective-C ivars. */
19761 cp_parser_objc_class_ivars (cp_parser* parser)
19763 cp_token *token = cp_lexer_peek_token (parser->lexer);
19765 if (token->type != CPP_OPEN_BRACE)
19766 return; /* No ivars specified. */
19768 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19769 token = cp_lexer_peek_token (parser->lexer);
19771 while (token->type != CPP_CLOSE_BRACE)
19773 cp_decl_specifier_seq declspecs;
19774 int decl_class_or_enum_p;
19775 tree prefix_attributes;
19777 cp_parser_objc_visibility_spec (parser);
19779 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19782 cp_parser_decl_specifier_seq (parser,
19783 CP_PARSER_FLAGS_OPTIONAL,
19785 &decl_class_or_enum_p);
19786 prefix_attributes = declspecs.attributes;
19787 declspecs.attributes = NULL_TREE;
19789 /* Keep going until we hit the `;' at the end of the
19791 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19793 tree width = NULL_TREE, attributes, first_attribute, decl;
19794 cp_declarator *declarator = NULL;
19795 int ctor_dtor_or_conv_p;
19797 /* Check for a (possibly unnamed) bitfield declaration. */
19798 token = cp_lexer_peek_token (parser->lexer);
19799 if (token->type == CPP_COLON)
19802 if (token->type == CPP_NAME
19803 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19806 /* Get the name of the bitfield. */
19807 declarator = make_id_declarator (NULL_TREE,
19808 cp_parser_identifier (parser),
19812 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19813 /* Get the width of the bitfield. */
19815 = cp_parser_constant_expression (parser,
19816 /*allow_non_constant=*/false,
19821 /* Parse the declarator. */
19823 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19824 &ctor_dtor_or_conv_p,
19825 /*parenthesized_p=*/NULL,
19826 /*member_p=*/false);
19829 /* Look for attributes that apply to the ivar. */
19830 attributes = cp_parser_attributes_opt (parser);
19831 /* Remember which attributes are prefix attributes and
19833 first_attribute = attributes;
19834 /* Combine the attributes. */
19835 attributes = chainon (prefix_attributes, attributes);
19838 /* Create the bitfield declaration. */
19839 decl = grokbitfield (declarator, &declspecs,
19843 decl = grokfield (declarator, &declspecs,
19844 NULL_TREE, /*init_const_expr_p=*/false,
19845 NULL_TREE, attributes);
19847 /* Add the instance variable. */
19848 objc_add_instance_variable (decl);
19850 /* Reset PREFIX_ATTRIBUTES. */
19851 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19852 attributes = TREE_CHAIN (attributes);
19854 TREE_CHAIN (attributes) = NULL_TREE;
19856 token = cp_lexer_peek_token (parser->lexer);
19858 if (token->type == CPP_COMMA)
19860 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19866 cp_parser_consume_semicolon_at_end_of_statement (parser);
19867 token = cp_lexer_peek_token (parser->lexer);
19870 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19871 /* For historical reasons, we accept an optional semicolon. */
19872 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19873 cp_lexer_consume_token (parser->lexer);
19876 /* Parse an Objective-C protocol declaration. */
19879 cp_parser_objc_protocol_declaration (cp_parser* parser)
19881 tree proto, protorefs;
19884 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19885 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19887 tok = cp_lexer_peek_token (parser->lexer);
19888 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19892 /* See if we have a forward declaration or a definition. */
19893 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19895 /* Try a forward declaration first. */
19896 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19898 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19900 cp_parser_consume_semicolon_at_end_of_statement (parser);
19903 /* Ok, we got a full-fledged definition (or at least should). */
19906 proto = cp_parser_identifier (parser);
19907 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19908 objc_start_protocol (proto, protorefs);
19909 cp_parser_objc_method_prototype_list (parser);
19913 /* Parse an Objective-C superclass or category. */
19916 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19919 cp_token *next = cp_lexer_peek_token (parser->lexer);
19921 *super = *categ = NULL_TREE;
19922 if (next->type == CPP_COLON)
19924 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19925 *super = cp_parser_identifier (parser);
19927 else if (next->type == CPP_OPEN_PAREN)
19929 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19930 *categ = cp_parser_identifier (parser);
19931 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19935 /* Parse an Objective-C class interface. */
19938 cp_parser_objc_class_interface (cp_parser* parser)
19940 tree name, super, categ, protos;
19942 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19943 name = cp_parser_identifier (parser);
19944 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19945 protos = cp_parser_objc_protocol_refs_opt (parser);
19947 /* We have either a class or a category on our hands. */
19949 objc_start_category_interface (name, categ, protos);
19952 objc_start_class_interface (name, super, protos);
19953 /* Handle instance variable declarations, if any. */
19954 cp_parser_objc_class_ivars (parser);
19955 objc_continue_interface ();
19958 cp_parser_objc_method_prototype_list (parser);
19961 /* Parse an Objective-C class implementation. */
19964 cp_parser_objc_class_implementation (cp_parser* parser)
19966 tree name, super, categ;
19968 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19969 name = cp_parser_identifier (parser);
19970 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19972 /* We have either a class or a category on our hands. */
19974 objc_start_category_implementation (name, categ);
19977 objc_start_class_implementation (name, super);
19978 /* Handle instance variable declarations, if any. */
19979 cp_parser_objc_class_ivars (parser);
19980 objc_continue_implementation ();
19983 cp_parser_objc_method_definition_list (parser);
19986 /* Consume the @end token and finish off the implementation. */
19989 cp_parser_objc_end_implementation (cp_parser* parser)
19991 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19992 objc_finish_implementation ();
19995 /* Parse an Objective-C declaration. */
19998 cp_parser_objc_declaration (cp_parser* parser)
20000 /* Try to figure out what kind of declaration is present. */
20001 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20003 switch (kwd->keyword)
20006 cp_parser_objc_alias_declaration (parser);
20009 cp_parser_objc_class_declaration (parser);
20011 case RID_AT_PROTOCOL:
20012 cp_parser_objc_protocol_declaration (parser);
20014 case RID_AT_INTERFACE:
20015 cp_parser_objc_class_interface (parser);
20017 case RID_AT_IMPLEMENTATION:
20018 cp_parser_objc_class_implementation (parser);
20021 cp_parser_objc_end_implementation (parser);
20024 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20025 &kwd->location, kwd->u.value);
20026 cp_parser_skip_to_end_of_block_or_statement (parser);
20030 /* Parse an Objective-C try-catch-finally statement.
20032 objc-try-catch-finally-stmt:
20033 @try compound-statement objc-catch-clause-seq [opt]
20034 objc-finally-clause [opt]
20036 objc-catch-clause-seq:
20037 objc-catch-clause objc-catch-clause-seq [opt]
20040 @catch ( exception-declaration ) compound-statement
20042 objc-finally-clause
20043 @finally compound-statement
20045 Returns NULL_TREE. */
20048 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20049 location_t location;
20052 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20053 location = cp_lexer_peek_token (parser->lexer)->location;
20054 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20055 node, lest it get absorbed into the surrounding block. */
20056 stmt = push_stmt_list ();
20057 cp_parser_compound_statement (parser, NULL, false);
20058 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20060 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20062 cp_parameter_declarator *parmdecl;
20065 cp_lexer_consume_token (parser->lexer);
20066 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20067 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20068 parm = grokdeclarator (parmdecl->declarator,
20069 &parmdecl->decl_specifiers,
20070 PARM, /*initialized=*/0,
20071 /*attrlist=*/NULL);
20072 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20073 objc_begin_catch_clause (parm);
20074 cp_parser_compound_statement (parser, NULL, false);
20075 objc_finish_catch_clause ();
20078 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20080 cp_lexer_consume_token (parser->lexer);
20081 location = cp_lexer_peek_token (parser->lexer)->location;
20082 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20083 node, lest it get absorbed into the surrounding block. */
20084 stmt = push_stmt_list ();
20085 cp_parser_compound_statement (parser, NULL, false);
20086 objc_build_finally_clause (location, pop_stmt_list (stmt));
20089 return objc_finish_try_stmt ();
20092 /* Parse an Objective-C synchronized statement.
20094 objc-synchronized-stmt:
20095 @synchronized ( expression ) compound-statement
20097 Returns NULL_TREE. */
20100 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20101 location_t location;
20104 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20106 location = cp_lexer_peek_token (parser->lexer)->location;
20107 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20108 lock = cp_parser_expression (parser, false, NULL);
20109 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20111 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20112 node, lest it get absorbed into the surrounding block. */
20113 stmt = push_stmt_list ();
20114 cp_parser_compound_statement (parser, NULL, false);
20116 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20119 /* Parse an Objective-C throw statement.
20122 @throw assignment-expression [opt] ;
20124 Returns a constructed '@throw' statement. */
20127 cp_parser_objc_throw_statement (cp_parser *parser) {
20128 tree expr = NULL_TREE;
20130 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20133 expr = cp_parser_assignment_expression (parser, false, NULL);
20135 cp_parser_consume_semicolon_at_end_of_statement (parser);
20137 return objc_build_throw_stmt (expr);
20140 /* Parse an Objective-C statement. */
20143 cp_parser_objc_statement (cp_parser * parser) {
20144 /* Try to figure out what kind of declaration is present. */
20145 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20147 switch (kwd->keyword)
20150 return cp_parser_objc_try_catch_finally_statement (parser);
20151 case RID_AT_SYNCHRONIZED:
20152 return cp_parser_objc_synchronized_statement (parser);
20154 return cp_parser_objc_throw_statement (parser);
20156 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20157 &kwd->location, kwd->u.value);
20158 cp_parser_skip_to_end_of_block_or_statement (parser);
20161 return error_mark_node;
20164 /* OpenMP 2.5 parsing routines. */
20166 /* Returns name of the next clause.
20167 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20168 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20169 returned and the token is consumed. */
20171 static pragma_omp_clause
20172 cp_parser_omp_clause_name (cp_parser *parser)
20174 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20176 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20177 result = PRAGMA_OMP_CLAUSE_IF;
20178 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20179 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20180 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20181 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20182 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20184 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20185 const char *p = IDENTIFIER_POINTER (id);
20190 if (!strcmp ("collapse", p))
20191 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20192 else if (!strcmp ("copyin", p))
20193 result = PRAGMA_OMP_CLAUSE_COPYIN;
20194 else if (!strcmp ("copyprivate", p))
20195 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20198 if (!strcmp ("firstprivate", p))
20199 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20202 if (!strcmp ("lastprivate", p))
20203 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20206 if (!strcmp ("nowait", p))
20207 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20208 else if (!strcmp ("num_threads", p))
20209 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20212 if (!strcmp ("ordered", p))
20213 result = PRAGMA_OMP_CLAUSE_ORDERED;
20216 if (!strcmp ("reduction", p))
20217 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20220 if (!strcmp ("schedule", p))
20221 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20222 else if (!strcmp ("shared", p))
20223 result = PRAGMA_OMP_CLAUSE_SHARED;
20226 if (!strcmp ("untied", p))
20227 result = PRAGMA_OMP_CLAUSE_UNTIED;
20232 if (result != PRAGMA_OMP_CLAUSE_NONE)
20233 cp_lexer_consume_token (parser->lexer);
20238 /* Validate that a clause of the given type does not already exist. */
20241 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20242 const char *name, location_t location)
20246 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20247 if (OMP_CLAUSE_CODE (c) == code)
20249 error ("%Htoo many %qs clauses", &location, name);
20257 variable-list , identifier
20259 In addition, we match a closing parenthesis. An opening parenthesis
20260 will have been consumed by the caller.
20262 If KIND is nonzero, create the appropriate node and install the decl
20263 in OMP_CLAUSE_DECL and add the node to the head of the list.
20265 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20266 return the list created. */
20269 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20277 token = cp_lexer_peek_token (parser->lexer);
20278 name = cp_parser_id_expression (parser, /*template_p=*/false,
20279 /*check_dependency_p=*/true,
20280 /*template_p=*/NULL,
20281 /*declarator_p=*/false,
20282 /*optional_p=*/false);
20283 if (name == error_mark_node)
20286 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20287 if (decl == error_mark_node)
20288 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20289 else if (kind != 0)
20291 tree u = build_omp_clause (kind);
20292 OMP_CLAUSE_DECL (u) = decl;
20293 OMP_CLAUSE_CHAIN (u) = list;
20297 list = tree_cons (decl, NULL_TREE, list);
20300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20302 cp_lexer_consume_token (parser->lexer);
20305 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20309 /* Try to resync to an unnested comma. Copied from
20310 cp_parser_parenthesized_expression_list. */
20312 ending = cp_parser_skip_to_closing_parenthesis (parser,
20313 /*recovering=*/true,
20315 /*consume_paren=*/true);
20323 /* Similarly, but expect leading and trailing parenthesis. This is a very
20324 common case for omp clauses. */
20327 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20329 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20330 return cp_parser_omp_var_list_no_open (parser, kind, list);
20335 collapse ( constant-expression ) */
20338 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20344 loc = cp_lexer_peek_token (parser->lexer)->location;
20345 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20348 num = cp_parser_constant_expression (parser, false, NULL);
20350 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20351 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20352 /*or_comma=*/false,
20353 /*consume_paren=*/true);
20355 if (num == error_mark_node)
20357 num = fold_non_dependent_expr (num);
20358 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20359 || !host_integerp (num, 0)
20360 || (n = tree_low_cst (num, 0)) <= 0
20363 error ("%Hcollapse argument needs positive constant integer expression",
20368 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20369 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20370 OMP_CLAUSE_CHAIN (c) = list;
20371 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20377 default ( shared | none ) */
20380 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20382 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20385 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20387 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20389 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20390 const char *p = IDENTIFIER_POINTER (id);
20395 if (strcmp ("none", p) != 0)
20397 kind = OMP_CLAUSE_DEFAULT_NONE;
20401 if (strcmp ("shared", p) != 0)
20403 kind = OMP_CLAUSE_DEFAULT_SHARED;
20410 cp_lexer_consume_token (parser->lexer);
20415 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20418 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20419 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20420 /*or_comma=*/false,
20421 /*consume_paren=*/true);
20423 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20426 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20427 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20428 OMP_CLAUSE_CHAIN (c) = list;
20429 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20435 if ( expression ) */
20438 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20442 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20445 t = cp_parser_condition (parser);
20447 if (t == error_mark_node
20448 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20449 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20450 /*or_comma=*/false,
20451 /*consume_paren=*/true);
20453 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20455 c = build_omp_clause (OMP_CLAUSE_IF);
20456 OMP_CLAUSE_IF_EXPR (c) = t;
20457 OMP_CLAUSE_CHAIN (c) = list;
20466 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20467 tree list, location_t location)
20471 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20473 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20474 OMP_CLAUSE_CHAIN (c) = list;
20479 num_threads ( expression ) */
20482 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20483 location_t location)
20487 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20490 t = cp_parser_expression (parser, false, NULL);
20492 if (t == error_mark_node
20493 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20494 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20495 /*or_comma=*/false,
20496 /*consume_paren=*/true);
20498 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20499 "num_threads", location);
20501 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20502 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20503 OMP_CLAUSE_CHAIN (c) = list;
20512 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20513 tree list, location_t location)
20517 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20518 "ordered", location);
20520 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20521 OMP_CLAUSE_CHAIN (c) = list;
20526 reduction ( reduction-operator : variable-list )
20528 reduction-operator:
20529 One of: + * - & ^ | && || */
20532 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20534 enum tree_code code;
20537 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20540 switch (cp_lexer_peek_token (parser->lexer)->type)
20552 code = BIT_AND_EXPR;
20555 code = BIT_XOR_EXPR;
20558 code = BIT_IOR_EXPR;
20561 code = TRUTH_ANDIF_EXPR;
20564 code = TRUTH_ORIF_EXPR;
20567 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20568 "%<|%>, %<&&%>, or %<||%>");
20570 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20571 /*or_comma=*/false,
20572 /*consume_paren=*/true);
20575 cp_lexer_consume_token (parser->lexer);
20577 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20580 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20581 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20582 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20588 schedule ( schedule-kind )
20589 schedule ( schedule-kind , expression )
20592 static | dynamic | guided | runtime | auto */
20595 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20599 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20602 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20604 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20606 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20607 const char *p = IDENTIFIER_POINTER (id);
20612 if (strcmp ("dynamic", p) != 0)
20614 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20618 if (strcmp ("guided", p) != 0)
20620 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20624 if (strcmp ("runtime", p) != 0)
20626 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20633 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20634 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20635 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20636 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20639 cp_lexer_consume_token (parser->lexer);
20641 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20644 cp_lexer_consume_token (parser->lexer);
20646 token = cp_lexer_peek_token (parser->lexer);
20647 t = cp_parser_assignment_expression (parser, false, NULL);
20649 if (t == error_mark_node)
20651 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20652 error ("%Hschedule %<runtime%> does not take "
20653 "a %<chunk_size%> parameter", &token->location);
20654 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20655 error ("%Hschedule %<auto%> does not take "
20656 "a %<chunk_size%> parameter", &token->location);
20658 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20660 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20663 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20666 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20667 OMP_CLAUSE_CHAIN (c) = list;
20671 cp_parser_error (parser, "invalid schedule kind");
20673 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20674 /*or_comma=*/false,
20675 /*consume_paren=*/true);
20683 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20684 tree list, location_t location)
20688 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20690 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20691 OMP_CLAUSE_CHAIN (c) = list;
20695 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20696 is a bitmask in MASK. Return the list of clauses found; the result
20697 of clause default goes in *pdefault. */
20700 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20701 const char *where, cp_token *pragma_tok)
20703 tree clauses = NULL;
20705 cp_token *token = NULL;
20707 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20709 pragma_omp_clause c_kind;
20710 const char *c_name;
20711 tree prev = clauses;
20713 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20714 cp_lexer_consume_token (parser->lexer);
20716 token = cp_lexer_peek_token (parser->lexer);
20717 c_kind = cp_parser_omp_clause_name (parser);
20722 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20723 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20725 c_name = "collapse";
20727 case PRAGMA_OMP_CLAUSE_COPYIN:
20728 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20731 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20732 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20734 c_name = "copyprivate";
20736 case PRAGMA_OMP_CLAUSE_DEFAULT:
20737 clauses = cp_parser_omp_clause_default (parser, clauses,
20739 c_name = "default";
20741 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20742 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20744 c_name = "firstprivate";
20746 case PRAGMA_OMP_CLAUSE_IF:
20747 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20750 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20751 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20753 c_name = "lastprivate";
20755 case PRAGMA_OMP_CLAUSE_NOWAIT:
20756 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20759 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20760 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20762 c_name = "num_threads";
20764 case PRAGMA_OMP_CLAUSE_ORDERED:
20765 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20767 c_name = "ordered";
20769 case PRAGMA_OMP_CLAUSE_PRIVATE:
20770 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20772 c_name = "private";
20774 case PRAGMA_OMP_CLAUSE_REDUCTION:
20775 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20776 c_name = "reduction";
20778 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20779 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20781 c_name = "schedule";
20783 case PRAGMA_OMP_CLAUSE_SHARED:
20784 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20788 case PRAGMA_OMP_CLAUSE_UNTIED:
20789 clauses = cp_parser_omp_clause_untied (parser, clauses,
20794 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20798 if (((mask >> c_kind) & 1) == 0)
20800 /* Remove the invalid clause(s) from the list to avoid
20801 confusing the rest of the compiler. */
20803 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20807 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20808 return finish_omp_clauses (clauses);
20815 In practice, we're also interested in adding the statement to an
20816 outer node. So it is convenient if we work around the fact that
20817 cp_parser_statement calls add_stmt. */
20820 cp_parser_begin_omp_structured_block (cp_parser *parser)
20822 unsigned save = parser->in_statement;
20824 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20825 This preserves the "not within loop or switch" style error messages
20826 for nonsense cases like
20832 if (parser->in_statement)
20833 parser->in_statement = IN_OMP_BLOCK;
20839 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20841 parser->in_statement = save;
20845 cp_parser_omp_structured_block (cp_parser *parser)
20847 tree stmt = begin_omp_structured_block ();
20848 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20850 cp_parser_statement (parser, NULL_TREE, false, NULL);
20852 cp_parser_end_omp_structured_block (parser, save);
20853 return finish_omp_structured_block (stmt);
20857 # pragma omp atomic new-line
20861 x binop= expr | x++ | ++x | x-- | --x
20863 +, *, -, /, &, ^, |, <<, >>
20865 where x is an lvalue expression with scalar type. */
20868 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20871 enum tree_code code;
20873 cp_parser_require_pragma_eol (parser, pragma_tok);
20875 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20876 /*cast_p=*/false, NULL);
20877 switch (TREE_CODE (lhs))
20882 case PREINCREMENT_EXPR:
20883 case POSTINCREMENT_EXPR:
20884 lhs = TREE_OPERAND (lhs, 0);
20886 rhs = integer_one_node;
20889 case PREDECREMENT_EXPR:
20890 case POSTDECREMENT_EXPR:
20891 lhs = TREE_OPERAND (lhs, 0);
20893 rhs = integer_one_node;
20897 switch (cp_lexer_peek_token (parser->lexer)->type)
20903 code = TRUNC_DIV_EXPR;
20911 case CPP_LSHIFT_EQ:
20912 code = LSHIFT_EXPR;
20914 case CPP_RSHIFT_EQ:
20915 code = RSHIFT_EXPR;
20918 code = BIT_AND_EXPR;
20921 code = BIT_IOR_EXPR;
20924 code = BIT_XOR_EXPR;
20927 cp_parser_error (parser,
20928 "invalid operator for %<#pragma omp atomic%>");
20931 cp_lexer_consume_token (parser->lexer);
20933 rhs = cp_parser_expression (parser, false, NULL);
20934 if (rhs == error_mark_node)
20938 finish_omp_atomic (code, lhs, rhs);
20939 cp_parser_consume_semicolon_at_end_of_statement (parser);
20943 cp_parser_skip_to_end_of_block_or_statement (parser);
20948 # pragma omp barrier new-line */
20951 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20953 cp_parser_require_pragma_eol (parser, pragma_tok);
20954 finish_omp_barrier ();
20958 # pragma omp critical [(name)] new-line
20959 structured-block */
20962 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20964 tree stmt, name = NULL;
20966 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20968 cp_lexer_consume_token (parser->lexer);
20970 name = cp_parser_identifier (parser);
20972 if (name == error_mark_node
20973 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20974 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20975 /*or_comma=*/false,
20976 /*consume_paren=*/true);
20977 if (name == error_mark_node)
20980 cp_parser_require_pragma_eol (parser, pragma_tok);
20982 stmt = cp_parser_omp_structured_block (parser);
20983 return c_finish_omp_critical (stmt, name);
20987 # pragma omp flush flush-vars[opt] new-line
20990 ( variable-list ) */
20993 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20996 (void) cp_parser_omp_var_list (parser, 0, NULL);
20997 cp_parser_require_pragma_eol (parser, pragma_tok);
20999 finish_omp_flush ();
21002 /* Helper function, to parse omp for increment expression. */
21005 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21007 tree lhs = cp_parser_cast_expression (parser, false, false, NULL), rhs;
21013 cp_parser_skip_to_end_of_statement (parser);
21014 return error_mark_node;
21017 token = cp_lexer_peek_token (parser->lexer);
21018 op = binops_by_token [token->type].tree_type;
21027 cp_parser_skip_to_end_of_statement (parser);
21028 return error_mark_node;
21031 cp_lexer_consume_token (parser->lexer);
21032 rhs = cp_parser_binary_expression (parser, false,
21033 PREC_RELATIONAL_EXPRESSION, NULL);
21034 if (rhs == error_mark_node
21035 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21037 cp_parser_skip_to_end_of_statement (parser);
21038 return error_mark_node;
21041 return build2 (op, boolean_type_node, lhs, rhs);
21044 /* Helper function, to parse omp for increment expression. */
21047 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21049 cp_token *token = cp_lexer_peek_token (parser->lexer);
21055 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21057 op = (token->type == CPP_PLUS_PLUS
21058 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21059 cp_lexer_consume_token (parser->lexer);
21060 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21062 return error_mark_node;
21063 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21066 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21068 return error_mark_node;
21070 token = cp_lexer_peek_token (parser->lexer);
21071 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21073 op = (token->type == CPP_PLUS_PLUS
21074 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21075 cp_lexer_consume_token (parser->lexer);
21076 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21079 op = cp_parser_assignment_operator_opt (parser);
21080 if (op == ERROR_MARK)
21081 return error_mark_node;
21083 if (op != NOP_EXPR)
21085 rhs = cp_parser_assignment_expression (parser, false, NULL);
21086 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21087 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21090 lhs = cp_parser_binary_expression (parser, false,
21091 PREC_ADDITIVE_EXPRESSION, NULL);
21092 token = cp_lexer_peek_token (parser->lexer);
21093 decl_first = lhs == decl;
21096 if (token->type != CPP_PLUS
21097 && token->type != CPP_MINUS)
21098 return error_mark_node;
21102 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21103 cp_lexer_consume_token (parser->lexer);
21104 rhs = cp_parser_binary_expression (parser, false,
21105 PREC_ADDITIVE_EXPRESSION, NULL);
21106 token = cp_lexer_peek_token (parser->lexer);
21107 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21109 if (lhs == NULL_TREE)
21111 if (op == PLUS_EXPR)
21114 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21117 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21118 NULL, tf_warning_or_error);
21121 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21125 if (rhs != decl || op == MINUS_EXPR)
21126 return error_mark_node;
21127 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21130 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21132 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21135 /* Parse the restricted form of the for statement allowed by OpenMP. */
21138 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21140 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21141 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21142 tree this_pre_body, cl;
21143 location_t loc_first;
21144 bool collapse_err = false;
21145 int i, collapse = 1, nbraces = 0;
21147 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21148 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21149 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21151 gcc_assert (collapse >= 1);
21153 declv = make_tree_vec (collapse);
21154 initv = make_tree_vec (collapse);
21155 condv = make_tree_vec (collapse);
21156 incrv = make_tree_vec (collapse);
21158 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21160 for (i = 0; i < collapse; i++)
21162 int bracecount = 0;
21163 bool add_private_clause = false;
21166 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21168 cp_parser_error (parser, "for statement expected");
21171 loc = cp_lexer_consume_token (parser->lexer)->location;
21173 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21176 init = decl = real_decl = NULL;
21177 this_pre_body = push_stmt_list ();
21178 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21180 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21184 integer-type var = lb
21185 random-access-iterator-type var = lb
21186 pointer-type var = lb
21188 cp_decl_specifier_seq type_specifiers;
21190 /* First, try to parse as an initialized declaration. See
21191 cp_parser_condition, from whence the bulk of this is copied. */
21193 cp_parser_parse_tentatively (parser);
21194 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21196 if (cp_parser_parse_definitely (parser))
21198 /* If parsing a type specifier seq succeeded, then this
21199 MUST be a initialized declaration. */
21200 tree asm_specification, attributes;
21201 cp_declarator *declarator;
21203 declarator = cp_parser_declarator (parser,
21204 CP_PARSER_DECLARATOR_NAMED,
21205 /*ctor_dtor_or_conv_p=*/NULL,
21206 /*parenthesized_p=*/NULL,
21207 /*member_p=*/false);
21208 attributes = cp_parser_attributes_opt (parser);
21209 asm_specification = cp_parser_asm_specification_opt (parser);
21211 if (declarator == cp_error_declarator)
21212 cp_parser_skip_to_end_of_statement (parser);
21216 tree pushed_scope, auto_node;
21218 decl = start_decl (declarator, &type_specifiers,
21219 SD_INITIALIZED, attributes,
21220 /*prefix_attributes=*/NULL_TREE,
21223 auto_node = type_uses_auto (TREE_TYPE (decl));
21224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21226 if (cp_lexer_next_token_is (parser->lexer,
21228 error ("parenthesized initialization is not allowed in "
21229 "OpenMP %<for%> loop");
21231 /* Trigger an error. */
21232 cp_parser_require (parser, CPP_EQ, "%<=%>");
21234 init = error_mark_node;
21235 cp_parser_skip_to_end_of_statement (parser);
21237 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21238 || type_dependent_expression_p (decl)
21241 bool is_direct_init, is_non_constant_init;
21243 init = cp_parser_initializer (parser,
21245 &is_non_constant_init);
21247 if (auto_node && describable_type (init))
21250 = do_auto_deduction (TREE_TYPE (decl), init,
21253 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21254 && !type_dependent_expression_p (decl))
21258 cp_finish_decl (decl, init, !is_non_constant_init,
21260 LOOKUP_ONLYCONVERTING);
21261 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21264 = tree_cons (NULL, this_pre_body, for_block);
21268 init = pop_stmt_list (this_pre_body);
21269 this_pre_body = NULL_TREE;
21274 cp_lexer_consume_token (parser->lexer);
21275 init = cp_parser_assignment_expression (parser, false, NULL);
21278 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21279 init = error_mark_node;
21281 cp_finish_decl (decl, NULL_TREE,
21282 /*init_const_expr_p=*/false,
21284 LOOKUP_ONLYCONVERTING);
21288 pop_scope (pushed_scope);
21294 /* If parsing a type specifier sequence failed, then
21295 this MUST be a simple expression. */
21296 cp_parser_parse_tentatively (parser);
21297 decl = cp_parser_primary_expression (parser, false, false,
21299 if (!cp_parser_error_occurred (parser)
21302 && CLASS_TYPE_P (TREE_TYPE (decl)))
21306 cp_parser_parse_definitely (parser);
21307 cp_parser_require (parser, CPP_EQ, "%<=%>");
21308 rhs = cp_parser_assignment_expression (parser, false, NULL);
21309 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21311 tf_warning_or_error));
21312 add_private_clause = true;
21317 cp_parser_abort_tentative_parse (parser);
21318 init = cp_parser_expression (parser, false, NULL);
21321 if (TREE_CODE (init) == MODIFY_EXPR
21322 || TREE_CODE (init) == MODOP_EXPR)
21323 real_decl = TREE_OPERAND (init, 0);
21328 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21331 this_pre_body = pop_stmt_list (this_pre_body);
21335 pre_body = push_stmt_list ();
21337 add_stmt (this_pre_body);
21338 pre_body = pop_stmt_list (pre_body);
21341 pre_body = this_pre_body;
21346 if (par_clauses != NULL && real_decl != NULL_TREE)
21349 for (c = par_clauses; *c ; )
21350 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21351 && OMP_CLAUSE_DECL (*c) == real_decl)
21353 error ("%Hiteration variable %qD should not be firstprivate",
21355 *c = OMP_CLAUSE_CHAIN (*c);
21357 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21358 && OMP_CLAUSE_DECL (*c) == real_decl)
21360 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21361 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21362 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21363 OMP_CLAUSE_DECL (l) = real_decl;
21364 OMP_CLAUSE_CHAIN (l) = clauses;
21365 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21367 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21368 CP_OMP_CLAUSE_INFO (*c) = NULL;
21369 add_private_clause = false;
21373 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21374 && OMP_CLAUSE_DECL (*c) == real_decl)
21375 add_private_clause = false;
21376 c = &OMP_CLAUSE_CHAIN (*c);
21380 if (add_private_clause)
21383 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21385 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21386 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21387 && OMP_CLAUSE_DECL (c) == decl)
21389 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21390 && OMP_CLAUSE_DECL (c) == decl)
21391 error ("%Hiteration variable %qD should not be firstprivate",
21393 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21394 && OMP_CLAUSE_DECL (c) == decl)
21395 error ("%Hiteration variable %qD should not be reduction",
21400 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21401 OMP_CLAUSE_DECL (c) = decl;
21402 c = finish_omp_clauses (c);
21405 OMP_CLAUSE_CHAIN (c) = clauses;
21412 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21414 /* If decl is an iterator, preserve LHS and RHS of the relational
21415 expr until finish_omp_for. */
21417 && (type_dependent_expression_p (decl)
21418 || CLASS_TYPE_P (TREE_TYPE (decl))))
21419 cond = cp_parser_omp_for_cond (parser, decl);
21421 cond = cp_parser_condition (parser);
21423 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21426 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21428 /* If decl is an iterator, preserve the operator on decl
21429 until finish_omp_for. */
21431 && (type_dependent_expression_p (decl)
21432 || CLASS_TYPE_P (TREE_TYPE (decl))))
21433 incr = cp_parser_omp_for_incr (parser, decl);
21435 incr = cp_parser_expression (parser, false, NULL);
21438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21439 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21440 /*or_comma=*/false,
21441 /*consume_paren=*/true);
21443 TREE_VEC_ELT (declv, i) = decl;
21444 TREE_VEC_ELT (initv, i) = init;
21445 TREE_VEC_ELT (condv, i) = cond;
21446 TREE_VEC_ELT (incrv, i) = incr;
21448 if (i == collapse - 1)
21451 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21452 in between the collapsed for loops to be still considered perfectly
21453 nested. Hopefully the final version clarifies this.
21454 For now handle (multiple) {'s and empty statements. */
21455 cp_parser_parse_tentatively (parser);
21458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21460 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21462 cp_lexer_consume_token (parser->lexer);
21465 else if (bracecount
21466 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21467 cp_lexer_consume_token (parser->lexer);
21470 loc = cp_lexer_peek_token (parser->lexer)->location;
21471 error ("%Hnot enough collapsed for loops", &loc);
21472 collapse_err = true;
21473 cp_parser_abort_tentative_parse (parser);
21482 cp_parser_parse_definitely (parser);
21483 nbraces += bracecount;
21487 /* Note that we saved the original contents of this flag when we entered
21488 the structured block, and so we don't need to re-save it here. */
21489 parser->in_statement = IN_OMP_FOR;
21491 /* Note that the grammar doesn't call for a structured block here,
21492 though the loop as a whole is a structured block. */
21493 body = push_stmt_list ();
21494 cp_parser_statement (parser, NULL_TREE, false, NULL);
21495 body = pop_stmt_list (body);
21497 if (declv == NULL_TREE)
21500 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21501 pre_body, clauses);
21505 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21507 cp_lexer_consume_token (parser->lexer);
21510 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21511 cp_lexer_consume_token (parser->lexer);
21516 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21517 error ("%Hcollapsed loops not perfectly nested", &loc);
21519 collapse_err = true;
21520 cp_parser_statement_seq_opt (parser, NULL);
21521 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21527 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21528 for_block = TREE_CHAIN (for_block);
21535 #pragma omp for for-clause[optseq] new-line
21538 #define OMP_FOR_CLAUSE_MASK \
21539 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21540 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21541 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21542 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21543 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21544 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21545 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21546 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21549 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21551 tree clauses, sb, ret;
21554 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21555 "#pragma omp for", pragma_tok);
21557 sb = begin_omp_structured_block ();
21558 save = cp_parser_begin_omp_structured_block (parser);
21560 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21562 cp_parser_end_omp_structured_block (parser, save);
21563 add_stmt (finish_omp_structured_block (sb));
21569 # pragma omp master new-line
21570 structured-block */
21573 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21575 cp_parser_require_pragma_eol (parser, pragma_tok);
21576 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21580 # pragma omp ordered new-line
21581 structured-block */
21584 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21586 cp_parser_require_pragma_eol (parser, pragma_tok);
21587 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21593 { section-sequence }
21596 section-directive[opt] structured-block
21597 section-sequence section-directive structured-block */
21600 cp_parser_omp_sections_scope (cp_parser *parser)
21602 tree stmt, substmt;
21603 bool error_suppress = false;
21606 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21609 stmt = push_stmt_list ();
21611 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21615 substmt = begin_omp_structured_block ();
21616 save = cp_parser_begin_omp_structured_block (parser);
21620 cp_parser_statement (parser, NULL_TREE, false, NULL);
21622 tok = cp_lexer_peek_token (parser->lexer);
21623 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21625 if (tok->type == CPP_CLOSE_BRACE)
21627 if (tok->type == CPP_EOF)
21631 cp_parser_end_omp_structured_block (parser, save);
21632 substmt = finish_omp_structured_block (substmt);
21633 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21634 add_stmt (substmt);
21639 tok = cp_lexer_peek_token (parser->lexer);
21640 if (tok->type == CPP_CLOSE_BRACE)
21642 if (tok->type == CPP_EOF)
21645 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21647 cp_lexer_consume_token (parser->lexer);
21648 cp_parser_require_pragma_eol (parser, tok);
21649 error_suppress = false;
21651 else if (!error_suppress)
21653 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21654 error_suppress = true;
21657 substmt = cp_parser_omp_structured_block (parser);
21658 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21659 add_stmt (substmt);
21661 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21663 substmt = pop_stmt_list (stmt);
21665 stmt = make_node (OMP_SECTIONS);
21666 TREE_TYPE (stmt) = void_type_node;
21667 OMP_SECTIONS_BODY (stmt) = substmt;
21674 # pragma omp sections sections-clause[optseq] newline
21677 #define OMP_SECTIONS_CLAUSE_MASK \
21678 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21679 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21680 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21681 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21682 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21685 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21689 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21690 "#pragma omp sections", pragma_tok);
21692 ret = cp_parser_omp_sections_scope (parser);
21694 OMP_SECTIONS_CLAUSES (ret) = clauses;
21700 # pragma parallel parallel-clause new-line
21701 # pragma parallel for parallel-for-clause new-line
21702 # pragma parallel sections parallel-sections-clause new-line */
21704 #define OMP_PARALLEL_CLAUSE_MASK \
21705 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21706 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21707 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21708 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21709 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21710 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21711 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21712 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21715 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21717 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21718 const char *p_name = "#pragma omp parallel";
21719 tree stmt, clauses, par_clause, ws_clause, block;
21720 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21723 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21725 cp_lexer_consume_token (parser->lexer);
21726 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21727 p_name = "#pragma omp parallel for";
21728 mask |= OMP_FOR_CLAUSE_MASK;
21729 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21731 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21733 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21734 const char *p = IDENTIFIER_POINTER (id);
21735 if (strcmp (p, "sections") == 0)
21737 cp_lexer_consume_token (parser->lexer);
21738 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21739 p_name = "#pragma omp parallel sections";
21740 mask |= OMP_SECTIONS_CLAUSE_MASK;
21741 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21745 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21746 block = begin_omp_parallel ();
21747 save = cp_parser_begin_omp_structured_block (parser);
21751 case PRAGMA_OMP_PARALLEL:
21752 cp_parser_statement (parser, NULL_TREE, false, NULL);
21753 par_clause = clauses;
21756 case PRAGMA_OMP_PARALLEL_FOR:
21757 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21758 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21761 case PRAGMA_OMP_PARALLEL_SECTIONS:
21762 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21763 stmt = cp_parser_omp_sections_scope (parser);
21765 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21769 gcc_unreachable ();
21772 cp_parser_end_omp_structured_block (parser, save);
21773 stmt = finish_omp_parallel (par_clause, block);
21774 if (p_kind != PRAGMA_OMP_PARALLEL)
21775 OMP_PARALLEL_COMBINED (stmt) = 1;
21780 # pragma omp single single-clause[optseq] new-line
21781 structured-block */
21783 #define OMP_SINGLE_CLAUSE_MASK \
21784 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21785 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21786 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21787 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21790 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21792 tree stmt = make_node (OMP_SINGLE);
21793 TREE_TYPE (stmt) = void_type_node;
21795 OMP_SINGLE_CLAUSES (stmt)
21796 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21797 "#pragma omp single", pragma_tok);
21798 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21800 return add_stmt (stmt);
21804 # pragma omp task task-clause[optseq] new-line
21805 structured-block */
21807 #define OMP_TASK_CLAUSE_MASK \
21808 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21809 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21810 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21811 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21812 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21813 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21816 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21818 tree clauses, block;
21821 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21822 "#pragma omp task", pragma_tok);
21823 block = begin_omp_task ();
21824 save = cp_parser_begin_omp_structured_block (parser);
21825 cp_parser_statement (parser, NULL_TREE, false, NULL);
21826 cp_parser_end_omp_structured_block (parser, save);
21827 return finish_omp_task (clauses, block);
21831 # pragma omp taskwait new-line */
21834 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21836 cp_parser_require_pragma_eol (parser, pragma_tok);
21837 finish_omp_taskwait ();
21841 # pragma omp threadprivate (variable-list) */
21844 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21848 vars = cp_parser_omp_var_list (parser, 0, NULL);
21849 cp_parser_require_pragma_eol (parser, pragma_tok);
21851 finish_omp_threadprivate (vars);
21854 /* Main entry point to OpenMP statement pragmas. */
21857 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21861 switch (pragma_tok->pragma_kind)
21863 case PRAGMA_OMP_ATOMIC:
21864 cp_parser_omp_atomic (parser, pragma_tok);
21866 case PRAGMA_OMP_CRITICAL:
21867 stmt = cp_parser_omp_critical (parser, pragma_tok);
21869 case PRAGMA_OMP_FOR:
21870 stmt = cp_parser_omp_for (parser, pragma_tok);
21872 case PRAGMA_OMP_MASTER:
21873 stmt = cp_parser_omp_master (parser, pragma_tok);
21875 case PRAGMA_OMP_ORDERED:
21876 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21878 case PRAGMA_OMP_PARALLEL:
21879 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21881 case PRAGMA_OMP_SECTIONS:
21882 stmt = cp_parser_omp_sections (parser, pragma_tok);
21884 case PRAGMA_OMP_SINGLE:
21885 stmt = cp_parser_omp_single (parser, pragma_tok);
21887 case PRAGMA_OMP_TASK:
21888 stmt = cp_parser_omp_task (parser, pragma_tok);
21891 gcc_unreachable ();
21895 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21900 static GTY (()) cp_parser *the_parser;
21903 /* Special handling for the first token or line in the file. The first
21904 thing in the file might be #pragma GCC pch_preprocess, which loads a
21905 PCH file, which is a GC collection point. So we need to handle this
21906 first pragma without benefit of an existing lexer structure.
21908 Always returns one token to the caller in *FIRST_TOKEN. This is
21909 either the true first token of the file, or the first token after
21910 the initial pragma. */
21913 cp_parser_initial_pragma (cp_token *first_token)
21917 cp_lexer_get_preprocessor_token (NULL, first_token);
21918 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21921 cp_lexer_get_preprocessor_token (NULL, first_token);
21922 if (first_token->type == CPP_STRING)
21924 name = first_token->u.value;
21926 cp_lexer_get_preprocessor_token (NULL, first_token);
21927 if (first_token->type != CPP_PRAGMA_EOL)
21928 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21929 &first_token->location);
21932 error ("%Hexpected string literal", &first_token->location);
21934 /* Skip to the end of the pragma. */
21935 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21936 cp_lexer_get_preprocessor_token (NULL, first_token);
21938 /* Now actually load the PCH file. */
21940 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21942 /* Read one more token to return to our caller. We have to do this
21943 after reading the PCH file in, since its pointers have to be
21945 cp_lexer_get_preprocessor_token (NULL, first_token);
21948 /* Normal parsing of a pragma token. Here we can (and must) use the
21952 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21954 cp_token *pragma_tok;
21957 pragma_tok = cp_lexer_consume_token (parser->lexer);
21958 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21959 parser->lexer->in_pragma = true;
21961 id = pragma_tok->pragma_kind;
21964 case PRAGMA_GCC_PCH_PREPROCESS:
21965 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21966 &pragma_tok->location);
21969 case PRAGMA_OMP_BARRIER:
21972 case pragma_compound:
21973 cp_parser_omp_barrier (parser, pragma_tok);
21976 error ("%H%<#pragma omp barrier%> may only be "
21977 "used in compound statements", &pragma_tok->location);
21984 case PRAGMA_OMP_FLUSH:
21987 case pragma_compound:
21988 cp_parser_omp_flush (parser, pragma_tok);
21991 error ("%H%<#pragma omp flush%> may only be "
21992 "used in compound statements", &pragma_tok->location);
21999 case PRAGMA_OMP_TASKWAIT:
22002 case pragma_compound:
22003 cp_parser_omp_taskwait (parser, pragma_tok);
22006 error ("%H%<#pragma omp taskwait%> may only be "
22007 "used in compound statements",
22008 &pragma_tok->location);
22015 case PRAGMA_OMP_THREADPRIVATE:
22016 cp_parser_omp_threadprivate (parser, pragma_tok);
22019 case PRAGMA_OMP_ATOMIC:
22020 case PRAGMA_OMP_CRITICAL:
22021 case PRAGMA_OMP_FOR:
22022 case PRAGMA_OMP_MASTER:
22023 case PRAGMA_OMP_ORDERED:
22024 case PRAGMA_OMP_PARALLEL:
22025 case PRAGMA_OMP_SECTIONS:
22026 case PRAGMA_OMP_SINGLE:
22027 case PRAGMA_OMP_TASK:
22028 if (context == pragma_external)
22030 cp_parser_omp_construct (parser, pragma_tok);
22033 case PRAGMA_OMP_SECTION:
22034 error ("%H%<#pragma omp section%> may only be used in "
22035 "%<#pragma omp sections%> construct", &pragma_tok->location);
22039 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22040 c_invoke_pragma_handler (id);
22044 cp_parser_error (parser, "expected declaration specifiers");
22048 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22052 /* The interface the pragma parsers have to the lexer. */
22055 pragma_lex (tree *value)
22058 enum cpp_ttype ret;
22060 tok = cp_lexer_peek_token (the_parser->lexer);
22063 *value = tok->u.value;
22065 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22067 else if (ret == CPP_STRING)
22068 *value = cp_parser_string_literal (the_parser, false, false);
22071 cp_lexer_consume_token (the_parser->lexer);
22072 if (ret == CPP_KEYWORD)
22080 /* External interface. */
22082 /* Parse one entire translation unit. */
22085 c_parse_file (void)
22087 bool error_occurred;
22088 static bool already_called = false;
22090 if (already_called)
22092 sorry ("inter-module optimizations not implemented for C++");
22095 already_called = true;
22097 the_parser = cp_parser_new ();
22098 push_deferring_access_checks (flag_access_control
22099 ? dk_no_deferred : dk_no_check);
22100 error_occurred = cp_parser_translation_unit (the_parser);
22104 #include "gt-cp-parser.h"