2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 /* The location at which this token was found. */
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
102 /* The cp_lexer structure represents the C++ lexer. It is responsible
103 for managing the token stream from the preprocessor and supplying
104 it to the parser. Tokens are never added to the cp_lexer after
107 typedef struct cp_lexer GTY (())
109 /* The memory allocated for the buffer. NULL if this lexer does not
110 own the token buffer. */
111 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112 /* If the lexer owns the buffer, this is the number of tokens in the
114 size_t buffer_length;
116 /* A pointer just past the last available token. The tokens
117 in this lexer are [buffer, last_token). */
118 cp_token_position GTY ((skip)) last_token;
120 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
121 no more available tokens. */
122 cp_token_position GTY ((skip)) next_token;
124 /* A stack indicating positions at which cp_lexer_save_tokens was
125 called. The top entry is the most recent position at which we
126 began saving tokens. If the stack is non-empty, we are saving
128 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
130 /* The next lexer in a linked list of lexers. */
131 struct cp_lexer *next;
133 /* True if we should output debugging information. */
136 /* True if we're in the context of parsing a pragma, and should not
137 increment past the end-of-line marker. */
141 /* cp_token_cache is a range of tokens. There is no need to represent
142 allocate heap memory for it, since tokens are never removed from the
143 lexer's array. There is also no need for the GC to walk through
144 a cp_token_cache, since everything in here is referenced through
147 typedef struct cp_token_cache GTY(())
149 /* The beginning of the token range. */
150 cp_token * GTY((skip)) first;
152 /* Points immediately after the last token in the range. */
153 cp_token * GTY ((skip)) last;
158 static cp_lexer *cp_lexer_new_main
160 static cp_lexer *cp_lexer_new_from_tokens
161 (cp_token_cache *tokens);
162 static void cp_lexer_destroy
164 static int cp_lexer_saving_tokens
166 static cp_token_position cp_lexer_token_position
168 static cp_token *cp_lexer_token_at
169 (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171 (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
174 static cp_token *cp_lexer_peek_nth_token
175 (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177 (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181 (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
184 static void cp_lexer_purge_token
186 static void cp_lexer_purge_tokens_after
187 (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
190 static void cp_lexer_commit_tokens
192 static void cp_lexer_rollback_tokens
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196 (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
199 static void cp_lexer_start_debugging
200 (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205 about passing NULL to functions that require non-NULL arguments
206 (fputs, fprintf). It will never be used, so all we need is a value
207 of the right type that's guaranteed not to be NULL. */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
213 static cp_token_cache *cp_token_cache_new
214 (cp_token *, cp_token *);
216 static void cp_parser_initial_pragma
219 /* Manifest constants. */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
223 /* A token type for keywords, as opposed to ordinary identifiers. */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
226 /* A token type for template-ids. If a template-id is processed while
227 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228 the value of the CPP_TEMPLATE_ID is whatever was returned by
229 cp_parser_template_id. */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
232 /* A token type for nested-name-specifiers. If a
233 nested-name-specifier is processed while parsing tentatively, it is
234 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236 cp_parser_nested_name_specifier_opt. */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
239 /* A token type for tokens that are not tokens at all; these are used
240 to represent slots in the array where there used to be a token
241 that has now been deleted. */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
244 /* The number of token types, including C++-specific ones. */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written. */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
313 cpp_get_options (parse_in)->client_diagnostic = true;
314 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
348 /* Frees all resources associated with LEXER. */
351 cp_lexer_destroy (cp_lexer *lexer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
364 cp_lexer_debugging_p (cp_lexer *lexer)
366 return lexer->debugging_p;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
376 return lexer->next_token - previous_p;
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
385 /* nonzero if we are presently saving tokens. */
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
401 static int is_extern_c = 0;
403 /* Get a new token from the preprocessor. */
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
420 if (C_IS_RESERVED_WORD (token->u.value))
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
426 /* Update the value. Some keywords are mapped to particular
427 entities, rather than simply having the value of the
428 corresponding IDENTIFIER_NODE. For example, `__const' is
429 mapped to `const'. */
430 token->u.value = ridpointers[token->keyword];
434 if (warn_cxx0x_compat
435 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
438 /* Warn about the C++0x keyword (but still treat it as
440 warning (OPT_Wc__0x_compat,
441 "identifier %<%s%> will become a keyword in C++0x",
442 IDENTIFIER_POINTER (token->u.value));
444 /* Clear out the C_RID_CODE so we don't warn about this
445 particular identifier-turned-keyword again. */
446 C_SET_RID_CODE (token->u.value, RID_MAX);
449 token->ambiguous_p = false;
450 token->keyword = RID_MAX;
453 /* Handle Objective-C++ keywords. */
454 else if (token->type == CPP_AT_NAME)
456 token->type = CPP_KEYWORD;
457 switch (C_RID_CODE (token->u.value))
459 /* Map 'class' to '@class', 'private' to '@private', etc. */
460 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464 case RID_THROW: token->keyword = RID_AT_THROW; break;
465 case RID_TRY: token->keyword = RID_AT_TRY; break;
466 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467 default: token->keyword = C_RID_CODE (token->u.value);
470 else if (token->type == CPP_PRAGMA)
472 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
473 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474 token->u.value = NULL_TREE;
478 /* Update the globals input_location and the input file stack from TOKEN. */
480 cp_lexer_set_source_position_from_token (cp_token *token)
482 if (token->type != CPP_EOF)
484 input_location = token->location;
488 /* Return a pointer to the next token in the token stream, but do not
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
494 if (cp_lexer_debugging_p (lexer))
496 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498 putc ('\n', cp_lexer_debug_stream);
500 return lexer->next_token;
503 /* Return true if the next token has the indicated TYPE. */
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
508 return cp_lexer_peek_token (lexer)->type == type;
511 /* Return true if the next token does not have the indicated TYPE. */
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
516 return !cp_lexer_next_token_is (lexer, type);
519 /* Return true if the next token is the indicated KEYWORD. */
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
524 return cp_lexer_peek_token (lexer)->keyword == keyword;
527 /* Return true if the next token is not the indicated KEYWORD. */
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
532 return cp_lexer_peek_token (lexer)->keyword != keyword;
535 /* Return true if the next token is a keyword for a decl-specifier. */
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
542 token = cp_lexer_peek_token (lexer);
543 switch (token->keyword)
545 /* auto specifier: storage-class-specifier in C++,
546 simple-type-specifier in C++0x. */
548 /* Storage classes. */
554 /* Elaborated type specifiers. */
560 /* Simple type specifiers. */
574 /* GNU extensions. */
577 /* C++0x extensions. */
586 /* Return a pointer to the Nth token in the token stream. If N is 1,
587 then this is precisely equivalent to cp_lexer_peek_token (except
588 that it is not inline). One would like to disallow that case, but
589 there is one case (cp_parser_nth_token_starts_template_id) where
590 the caller passes a variable for N and it might be 1. */
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
597 /* N is 1-based, not zero-based. */
600 if (cp_lexer_debugging_p (lexer))
601 fprintf (cp_lexer_debug_stream,
602 "cp_lexer: peeking ahead %ld at token: ", (long)n);
605 token = lexer->next_token;
606 gcc_assert (!n || token != &eof_token);
610 if (token == lexer->last_token)
616 if (token->type != CPP_PURGED)
620 if (cp_lexer_debugging_p (lexer))
622 cp_lexer_print_token (cp_lexer_debug_stream, token);
623 putc ('\n', cp_lexer_debug_stream);
629 /* Return the next token, and advance the lexer's next_token pointer
630 to point to the next non-purged token. */
633 cp_lexer_consume_token (cp_lexer* lexer)
635 cp_token *token = lexer->next_token;
637 gcc_assert (token != &eof_token);
638 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
643 if (lexer->next_token == lexer->last_token)
645 lexer->next_token = &eof_token;
650 while (lexer->next_token->type == CPP_PURGED);
652 cp_lexer_set_source_position_from_token (token);
654 /* Provide debugging output. */
655 if (cp_lexer_debugging_p (lexer))
657 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658 cp_lexer_print_token (cp_lexer_debug_stream, token);
659 putc ('\n', cp_lexer_debug_stream);
665 /* Permanently remove the next token from the token stream, and
666 advance the next_token pointer to refer to the next non-purged
670 cp_lexer_purge_token (cp_lexer *lexer)
672 cp_token *tok = lexer->next_token;
674 gcc_assert (tok != &eof_token);
675 tok->type = CPP_PURGED;
676 tok->location = UNKNOWN_LOCATION;
677 tok->u.value = NULL_TREE;
678 tok->keyword = RID_MAX;
683 if (tok == lexer->last_token)
689 while (tok->type == CPP_PURGED);
690 lexer->next_token = tok;
693 /* Permanently remove all tokens after TOK, up to, but not
694 including, the token that will be returned next by
695 cp_lexer_peek_token. */
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
700 cp_token *peek = lexer->next_token;
702 if (peek == &eof_token)
703 peek = lexer->last_token;
705 gcc_assert (tok < peek);
707 for ( tok += 1; tok != peek; tok += 1)
709 tok->type = CPP_PURGED;
710 tok->location = UNKNOWN_LOCATION;
711 tok->u.value = NULL_TREE;
712 tok->keyword = RID_MAX;
716 /* Begin saving tokens. All tokens consumed after this point will be
720 cp_lexer_save_tokens (cp_lexer* lexer)
722 /* Provide debugging output. */
723 if (cp_lexer_debugging_p (lexer))
724 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
726 VEC_safe_push (cp_token_position, heap,
727 lexer->saved_tokens, lexer->next_token);
730 /* Commit to the portion of the token stream most recently saved. */
733 cp_lexer_commit_tokens (cp_lexer* lexer)
735 /* Provide debugging output. */
736 if (cp_lexer_debugging_p (lexer))
737 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
739 VEC_pop (cp_token_position, lexer->saved_tokens);
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743 to the token stream. Stop saving tokens. */
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
748 /* Provide debugging output. */
749 if (cp_lexer_debugging_p (lexer))
750 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
752 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
755 /* Print a representation of the TOKEN on the STREAM. */
757 #ifdef ENABLE_CHECKING
760 cp_lexer_print_token (FILE * stream, cp_token *token)
762 /* We don't use cpp_type2name here because the parser defines
763 a few tokens of its own. */
764 static const char *const token_names[] = {
765 /* cpplib-defined token types */
771 /* C++ parser token types - see "Manifest constants", above. */
774 "NESTED_NAME_SPECIFIER",
778 /* If we have a name for the token, print it out. Otherwise, we
779 simply give the numeric code. */
780 gcc_assert (token->type < ARRAY_SIZE(token_names));
781 fputs (token_names[token->type], stream);
783 /* For some tokens, print the associated data. */
787 /* Some keywords have a value that is not an IDENTIFIER_NODE.
788 For example, `struct' is mapped to an INTEGER_CST. */
789 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791 /* else fall through */
793 fputs (IDENTIFIER_POINTER (token->u.value), stream);
800 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
808 /* Start emitting debugging information. */
811 cp_lexer_start_debugging (cp_lexer* lexer)
813 lexer->debugging_p = true;
816 /* Stop emitting debugging information. */
819 cp_lexer_stop_debugging (cp_lexer* lexer)
821 lexer->debugging_p = false;
824 #endif /* ENABLE_CHECKING */
826 /* Create a new cp_token_cache, representing a range of tokens. */
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
831 cp_token_cache *cache = GGC_NEW (cp_token_cache);
832 cache->first = first;
838 /* Decl-specifiers. */
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
845 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
850 /* Nothing other than the parser should be creating declarators;
851 declarators are a semi-syntactic representation of C++ entities.
852 Other parts of the front end that need to create entities (like
853 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
855 static cp_declarator *make_call_declarator
856 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
857 static cp_declarator *make_array_declarator
858 (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860 (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862 (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864 (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866 (cp_cv_quals, tree, cp_declarator *);
868 /* An erroneous declarator. */
869 static cp_declarator *cp_error_declarator;
871 /* The obstack on which declarators and related data structures are
873 static struct obstack declarator_obstack;
875 /* Alloc BYTES from the declarator memory pool. */
878 alloc_declarator (size_t bytes)
880 return obstack_alloc (&declarator_obstack, bytes);
883 /* Allocate a declarator of the indicated KIND. Clear fields that are
884 common to all declarators. */
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
889 cp_declarator *declarator;
891 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892 declarator->kind = kind;
893 declarator->attributes = NULL_TREE;
894 declarator->declarator = NULL;
895 declarator->parameter_pack_p = false;
900 /* Make a declarator for a generalized identifier. If
901 QUALIFYING_SCOPE is non-NULL, the identifier is
902 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903 UNQUALIFIED_NAME. SFK indicates the kind of special function this
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908 special_function_kind sfk)
910 cp_declarator *declarator;
912 /* It is valid to write:
914 class C { void f(); };
918 The standard is not clear about whether `typedef const C D' is
919 legal; as of 2002-09-15 the committee is considering that
920 question. EDG 3.0 allows that syntax. Therefore, we do as
922 if (qualifying_scope && TYPE_P (qualifying_scope))
923 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
925 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
929 declarator = make_declarator (cdk_id);
930 declarator->u.id.qualifying_scope = qualifying_scope;
931 declarator->u.id.unqualified_name = unqualified_name;
932 declarator->u.id.sfk = sfk;
937 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
938 of modifiers such as const or volatile to apply to the pointer
939 type, represented as identifiers. */
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
944 cp_declarator *declarator;
946 declarator = make_declarator (cdk_pointer);
947 declarator->declarator = target;
948 declarator->u.pointer.qualifiers = cv_qualifiers;
949 declarator->u.pointer.class_type = NULL_TREE;
952 declarator->parameter_pack_p = target->parameter_pack_p;
953 target->parameter_pack_p = false;
956 declarator->parameter_pack_p = false;
961 /* Like make_pointer_declarator -- but for references. */
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
967 cp_declarator *declarator;
969 declarator = make_declarator (cdk_reference);
970 declarator->declarator = target;
971 declarator->u.reference.qualifiers = cv_qualifiers;
972 declarator->u.reference.rvalue_ref = rvalue_ref;
975 declarator->parameter_pack_p = target->parameter_pack_p;
976 target->parameter_pack_p = false;
979 declarator->parameter_pack_p = false;
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985 member of CLASS_TYPE. */
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989 cp_declarator *pointee)
991 cp_declarator *declarator;
993 declarator = make_declarator (cdk_ptrmem);
994 declarator->declarator = pointee;
995 declarator->u.pointer.qualifiers = cv_qualifiers;
996 declarator->u.pointer.class_type = class_type;
1000 declarator->parameter_pack_p = pointee->parameter_pack_p;
1001 pointee->parameter_pack_p = false;
1004 declarator->parameter_pack_p = false;
1009 /* Make a declarator for the function given by TARGET, with the
1010 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1011 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1012 indicates what exceptions can be thrown. */
1015 make_call_declarator (cp_declarator *target,
1016 cp_parameter_declarator *parms,
1017 cp_cv_quals cv_qualifiers,
1018 tree exception_specification)
1020 cp_declarator *declarator;
1022 declarator = make_declarator (cdk_function);
1023 declarator->declarator = target;
1024 declarator->u.function.parameters = parms;
1025 declarator->u.function.qualifiers = cv_qualifiers;
1026 declarator->u.function.exception_specification = exception_specification;
1029 declarator->parameter_pack_p = target->parameter_pack_p;
1030 target->parameter_pack_p = false;
1033 declarator->parameter_pack_p = false;
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039 defined by ELEMENT. */
1042 make_array_declarator (cp_declarator *element, tree bounds)
1044 cp_declarator *declarator;
1046 declarator = make_declarator (cdk_array);
1047 declarator->declarator = element;
1048 declarator->u.array.bounds = bounds;
1051 declarator->parameter_pack_p = element->parameter_pack_p;
1052 element->parameter_pack_p = false;
1055 declarator->parameter_pack_p = false;
1060 /* Determine whether the declarator we've seen so far can be a
1061 parameter pack, when followed by an ellipsis. */
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1065 /* Search for a declarator name, or any other declarator that goes
1066 after the point where the ellipsis could appear in a parameter
1067 pack. If we find any of these, then this declarator can not be
1068 made into a parameter pack. */
1070 while (declarator && !found)
1072 switch ((int)declarator->kind)
1083 declarator = declarator->declarator;
1091 cp_parameter_declarator *no_parameters;
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094 DECLARATOR and DEFAULT_ARGUMENT. */
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098 cp_declarator *declarator,
1099 tree default_argument)
1101 cp_parameter_declarator *parameter;
1103 parameter = ((cp_parameter_declarator *)
1104 alloc_declarator (sizeof (cp_parameter_declarator)));
1105 parameter->next = NULL;
1106 if (decl_specifiers)
1107 parameter->decl_specifiers = *decl_specifiers;
1109 clear_decl_specs (¶meter->decl_specifiers);
1110 parameter->declarator = declarator;
1111 parameter->default_argument = default_argument;
1112 parameter->ellipsis_p = false;
1117 /* Returns true iff DECLARATOR is a declaration for a function. */
1120 function_declarator_p (const cp_declarator *declarator)
1124 if (declarator->kind == cdk_function
1125 && declarator->declarator->kind == cdk_id)
1127 if (declarator->kind == cdk_id
1128 || declarator->kind == cdk_error)
1130 declarator = declarator->declarator;
1140 A cp_parser parses the token stream as specified by the C++
1141 grammar. Its job is purely parsing, not semantic analysis. For
1142 example, the parser breaks the token stream into declarators,
1143 expressions, statements, and other similar syntactic constructs.
1144 It does not check that the types of the expressions on either side
1145 of an assignment-statement are compatible, or that a function is
1146 not declared with a parameter of type `void'.
1148 The parser invokes routines elsewhere in the compiler to perform
1149 semantic analysis and to build up the abstract syntax tree for the
1152 The parser (and the template instantiation code, which is, in a
1153 way, a close relative of parsing) are the only parts of the
1154 compiler that should be calling push_scope and pop_scope, or
1155 related functions. The parser (and template instantiation code)
1156 keeps track of what scope is presently active; everything else
1157 should simply honor that. (The code that generates static
1158 initializers may also need to set the scope, in order to check
1159 access control correctly when emitting the initializers.)
1164 The parser is of the standard recursive-descent variety. Upcoming
1165 tokens in the token stream are examined in order to determine which
1166 production to use when parsing a non-terminal. Some C++ constructs
1167 require arbitrary look ahead to disambiguate. For example, it is
1168 impossible, in the general case, to tell whether a statement is an
1169 expression or declaration without scanning the entire statement.
1170 Therefore, the parser is capable of "parsing tentatively." When the
1171 parser is not sure what construct comes next, it enters this mode.
1172 Then, while we attempt to parse the construct, the parser queues up
1173 error messages, rather than issuing them immediately, and saves the
1174 tokens it consumes. If the construct is parsed successfully, the
1175 parser "commits", i.e., it issues any queued error messages and
1176 the tokens that were being preserved are permanently discarded.
1177 If, however, the construct is not parsed successfully, the parser
1178 rolls back its state completely so that it can resume parsing using
1179 a different alternative.
1184 The performance of the parser could probably be improved substantially.
1185 We could often eliminate the need to parse tentatively by looking ahead
1186 a little bit. In some places, this approach might not entirely eliminate
1187 the need to parse tentatively, but it might still speed up the average
1190 /* Flags that are passed to some parsing functions. These values can
1191 be bitwise-ored together. */
1193 typedef enum cp_parser_flags
1196 CP_PARSER_FLAGS_NONE = 0x0,
1197 /* The construct is optional. If it is not present, then no error
1198 should be issued. */
1199 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200 /* When parsing a type-specifier, do not allow user-defined types. */
1201 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 /* The different kinds of declarators we want to parse. */
1206 typedef enum cp_parser_declarator_kind
1208 /* We want an abstract declarator. */
1209 CP_PARSER_DECLARATOR_ABSTRACT,
1210 /* We want a named declarator. */
1211 CP_PARSER_DECLARATOR_NAMED,
1212 /* We don't mind, but the name must be an unqualified-id. */
1213 CP_PARSER_DECLARATOR_EITHER
1214 } cp_parser_declarator_kind;
1216 /* The precedence values used to parse binary expressions. The minimum value
1217 of PREC must be 1, because zero is reserved to quickly discriminate
1218 binary operators from other tokens. */
1223 PREC_LOGICAL_OR_EXPRESSION,
1224 PREC_LOGICAL_AND_EXPRESSION,
1225 PREC_INCLUSIVE_OR_EXPRESSION,
1226 PREC_EXCLUSIVE_OR_EXPRESSION,
1227 PREC_AND_EXPRESSION,
1228 PREC_EQUALITY_EXPRESSION,
1229 PREC_RELATIONAL_EXPRESSION,
1230 PREC_SHIFT_EXPRESSION,
1231 PREC_ADDITIVE_EXPRESSION,
1232 PREC_MULTIPLICATIVE_EXPRESSION,
1234 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1237 /* A mapping from a token type to a corresponding tree node type, with a
1238 precedence value. */
1240 typedef struct cp_parser_binary_operations_map_node
1242 /* The token type. */
1243 enum cpp_ttype token_type;
1244 /* The corresponding tree code. */
1245 enum tree_code tree_type;
1246 /* The precedence of this operator. */
1247 enum cp_parser_prec prec;
1248 } cp_parser_binary_operations_map_node;
1250 /* The status of a tentative parse. */
1252 typedef enum cp_parser_status_kind
1254 /* No errors have occurred. */
1255 CP_PARSER_STATUS_KIND_NO_ERROR,
1256 /* An error has occurred. */
1257 CP_PARSER_STATUS_KIND_ERROR,
1258 /* We are committed to this tentative parse, whether or not an error
1260 CP_PARSER_STATUS_KIND_COMMITTED
1261 } cp_parser_status_kind;
1263 typedef struct cp_parser_expression_stack_entry
1265 /* Left hand side of the binary operation we are currently
1268 /* Original tree code for left hand side, if it was a binary
1269 expression itself (used for -Wparentheses). */
1270 enum tree_code lhs_type;
1271 /* Tree code for the binary operation we are parsing. */
1272 enum tree_code tree_type;
1273 /* Precedence of the binary operation we are parsing. */
1275 } cp_parser_expression_stack_entry;
1277 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1278 entries because precedence levels on the stack are monotonically
1280 typedef struct cp_parser_expression_stack_entry
1281 cp_parser_expression_stack[NUM_PREC_VALUES];
1283 /* Context that is saved and restored when parsing tentatively. */
1284 typedef struct cp_parser_context GTY (())
1286 /* If this is a tentative parsing context, the status of the
1288 enum cp_parser_status_kind status;
1289 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1290 that are looked up in this context must be looked up both in the
1291 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292 the context of the containing expression. */
1295 /* The next parsing context in the stack. */
1296 struct cp_parser_context *next;
1297 } cp_parser_context;
1301 /* Constructors and destructors. */
1303 static cp_parser_context *cp_parser_context_new
1304 (cp_parser_context *);
1306 /* Class variables. */
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311 Transformed into an associative array (binops_by_token) by
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1318 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1338 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1340 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1342 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1344 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 /* The same as binops, but initialized by cp_parser_new so that
1348 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1352 /* Constructors and destructors. */
1354 /* Construct a new context. The context below this one on the stack
1355 is given by NEXT. */
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1360 cp_parser_context *context;
1362 /* Allocate the storage. */
1363 if (cp_parser_context_free_list != NULL)
1365 /* Pull the first entry from the free list. */
1366 context = cp_parser_context_free_list;
1367 cp_parser_context_free_list = context->next;
1368 memset (context, 0, sizeof (*context));
1371 context = GGC_CNEW (cp_parser_context);
1373 /* No errors have occurred yet in this context. */
1374 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375 /* If this is not the bottommost context, copy information that we
1376 need from the previous context. */
1379 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380 expression, then we are parsing one in this context, too. */
1381 context->object_type = next->object_type;
1382 /* Thread the stack. */
1383 context->next = next;
1389 /* The cp_parser structure represents the C++ parser. */
1391 typedef struct cp_parser GTY(())
1393 /* The lexer from which we are obtaining tokens. */
1396 /* The scope in which names should be looked up. If NULL_TREE, then
1397 we look up names in the scope that is currently open in the
1398 source program. If non-NULL, this is either a TYPE or
1399 NAMESPACE_DECL for the scope in which we should look. It can
1400 also be ERROR_MARK, when we've parsed a bogus scope.
1402 This value is not cleared automatically after a name is looked
1403 up, so we must be careful to clear it before starting a new look
1404 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1405 will look up `Z' in the scope of `X', rather than the current
1406 scope.) Unfortunately, it is difficult to tell when name lookup
1407 is complete, because we sometimes peek at a token, look it up,
1408 and then decide not to consume it. */
1411 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1412 last lookup took place. OBJECT_SCOPE is used if an expression
1413 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1414 respectively. QUALIFYING_SCOPE is used for an expression of the
1415 form "X::Y"; it refers to X. */
1417 tree qualifying_scope;
1419 /* A stack of parsing contexts. All but the bottom entry on the
1420 stack will be tentative contexts.
1422 We parse tentatively in order to determine which construct is in
1423 use in some situations. For example, in order to determine
1424 whether a statement is an expression-statement or a
1425 declaration-statement we parse it tentatively as a
1426 declaration-statement. If that fails, we then reparse the same
1427 token stream as an expression-statement. */
1428 cp_parser_context *context;
1430 /* True if we are parsing GNU C++. If this flag is not set, then
1431 GNU extensions are not recognized. */
1432 bool allow_gnu_extensions_p;
1434 /* TRUE if the `>' token should be interpreted as the greater-than
1435 operator. FALSE if it is the end of a template-id or
1436 template-parameter-list. In C++0x mode, this flag also applies to
1437 `>>' tokens, which are viewed as two consecutive `>' tokens when
1438 this flag is FALSE. */
1439 bool greater_than_is_operator_p;
1441 /* TRUE if default arguments are allowed within a parameter list
1442 that starts at this point. FALSE if only a gnu extension makes
1443 them permissible. */
1444 bool default_arg_ok_p;
1446 /* TRUE if we are parsing an integral constant-expression. See
1447 [expr.const] for a precise definition. */
1448 bool integral_constant_expression_p;
1450 /* TRUE if we are parsing an integral constant-expression -- but a
1451 non-constant expression should be permitted as well. This flag
1452 is used when parsing an array bound so that GNU variable-length
1453 arrays are tolerated. */
1454 bool allow_non_integral_constant_expression_p;
1456 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1457 been seen that makes the expression non-constant. */
1458 bool non_integral_constant_expression_p;
1460 /* TRUE if local variable names and `this' are forbidden in the
1462 bool local_variables_forbidden_p;
1464 /* TRUE if the declaration we are parsing is part of a
1465 linkage-specification of the form `extern string-literal
1467 bool in_unbraced_linkage_specification_p;
1469 /* TRUE if we are presently parsing a declarator, after the
1470 direct-declarator. */
1471 bool in_declarator_p;
1473 /* TRUE if we are presently parsing a template-argument-list. */
1474 bool in_template_argument_list_p;
1476 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1477 to IN_OMP_BLOCK if parsing OpenMP structured block and
1478 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1479 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1480 iteration-statement, OpenMP block or loop within that switch. */
1481 #define IN_SWITCH_STMT 1
1482 #define IN_ITERATION_STMT 2
1483 #define IN_OMP_BLOCK 4
1484 #define IN_OMP_FOR 8
1485 #define IN_IF_STMT 16
1486 unsigned char in_statement;
1488 /* TRUE if we are presently parsing the body of a switch statement.
1489 Note that this doesn't quite overlap with in_statement above.
1490 The difference relates to giving the right sets of error messages:
1491 "case not in switch" vs "break statement used with OpenMP...". */
1492 bool in_switch_statement_p;
1494 /* TRUE if we are parsing a type-id in an expression context. In
1495 such a situation, both "type (expr)" and "type (type)" are valid
1497 bool in_type_id_in_expr_p;
1499 /* TRUE if we are currently in a header file where declarations are
1500 implicitly extern "C". */
1501 bool implicit_extern_c;
1503 /* TRUE if strings in expressions should be translated to the execution
1505 bool translate_strings_p;
1507 /* TRUE if we are presently parsing the body of a function, but not
1509 bool in_function_body;
1511 /* If non-NULL, then we are parsing a construct where new type
1512 definitions are not permitted. The string stored here will be
1513 issued as an error message if a type is defined. */
1514 const char *type_definition_forbidden_message;
1516 /* A list of lists. The outer list is a stack, used for member
1517 functions of local classes. At each level there are two sub-list,
1518 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1519 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1520 TREE_VALUE's. The functions are chained in reverse declaration
1523 The TREE_PURPOSE sublist contains those functions with default
1524 arguments that need post processing, and the TREE_VALUE sublist
1525 contains those functions with definitions that need post
1528 These lists can only be processed once the outermost class being
1529 defined is complete. */
1530 tree unparsed_functions_queues;
1532 /* The number of classes whose definitions are currently in
1534 unsigned num_classes_being_defined;
1536 /* The number of template parameter lists that apply directly to the
1537 current declaration. */
1538 unsigned num_template_parameter_lists;
1543 /* Constructors and destructors. */
1545 static cp_parser *cp_parser_new
1548 /* Routines to parse various constructs.
1550 Those that return `tree' will return the error_mark_node (rather
1551 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1552 Sometimes, they will return an ordinary node if error-recovery was
1553 attempted, even though a parse error occurred. So, to check
1554 whether or not a parse error occurred, you should always use
1555 cp_parser_error_occurred. If the construct is optional (indicated
1556 either by an `_opt' in the name of the function that does the
1557 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1558 the construct is not present. */
1560 /* Lexical conventions [gram.lex] */
1562 static tree cp_parser_identifier
1564 static tree cp_parser_string_literal
1565 (cp_parser *, bool, bool);
1567 /* Basic concepts [gram.basic] */
1569 static bool cp_parser_translation_unit
1572 /* Expressions [gram.expr] */
1574 static tree cp_parser_primary_expression
1575 (cp_parser *, bool, bool, bool, cp_id_kind *);
1576 static tree cp_parser_id_expression
1577 (cp_parser *, bool, bool, bool *, bool, bool);
1578 static tree cp_parser_unqualified_id
1579 (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier_opt
1581 (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_qualifying_entity
1585 (cp_parser *, bool, bool, bool, bool, bool);
1586 static tree cp_parser_postfix_expression
1587 (cp_parser *, bool, bool, bool);
1588 static tree cp_parser_postfix_open_square_expression
1589 (cp_parser *, tree, bool);
1590 static tree cp_parser_postfix_dot_deref_expression
1591 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1592 static tree cp_parser_parenthesized_expression_list
1593 (cp_parser *, bool, bool, bool, bool *);
1594 static void cp_parser_pseudo_destructor_name
1595 (cp_parser *, tree *, tree *);
1596 static tree cp_parser_unary_expression
1597 (cp_parser *, bool, bool);
1598 static enum tree_code cp_parser_unary_operator
1600 static tree cp_parser_new_expression
1602 static tree cp_parser_new_placement
1604 static tree cp_parser_new_type_id
1605 (cp_parser *, tree *);
1606 static cp_declarator *cp_parser_new_declarator_opt
1608 static cp_declarator *cp_parser_direct_new_declarator
1610 static tree cp_parser_new_initializer
1612 static tree cp_parser_delete_expression
1614 static tree cp_parser_cast_expression
1615 (cp_parser *, bool, bool);
1616 static tree cp_parser_binary_expression
1617 (cp_parser *, bool, enum cp_parser_prec);
1618 static tree cp_parser_question_colon_clause
1619 (cp_parser *, tree);
1620 static tree cp_parser_assignment_expression
1621 (cp_parser *, bool);
1622 static enum tree_code cp_parser_assignment_operator_opt
1624 static tree cp_parser_expression
1625 (cp_parser *, bool);
1626 static tree cp_parser_constant_expression
1627 (cp_parser *, bool, bool *);
1628 static tree cp_parser_builtin_offsetof
1631 /* Statements [gram.stmt.stmt] */
1633 static void cp_parser_statement
1634 (cp_parser *, tree, bool, bool *);
1635 static void cp_parser_label_for_labeled_statement
1637 static tree cp_parser_expression_statement
1638 (cp_parser *, tree);
1639 static tree cp_parser_compound_statement
1640 (cp_parser *, tree, bool);
1641 static void cp_parser_statement_seq_opt
1642 (cp_parser *, tree);
1643 static tree cp_parser_selection_statement
1644 (cp_parser *, bool *);
1645 static tree cp_parser_condition
1647 static tree cp_parser_iteration_statement
1649 static void cp_parser_for_init_statement
1651 static tree cp_parser_jump_statement
1653 static void cp_parser_declaration_statement
1656 static tree cp_parser_implicitly_scoped_statement
1657 (cp_parser *, bool *);
1658 static void cp_parser_already_scoped_statement
1661 /* Declarations [gram.dcl.dcl] */
1663 static void cp_parser_declaration_seq_opt
1665 static void cp_parser_declaration
1667 static void cp_parser_block_declaration
1668 (cp_parser *, bool);
1669 static void cp_parser_simple_declaration
1670 (cp_parser *, bool);
1671 static void cp_parser_decl_specifier_seq
1672 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1673 static tree cp_parser_storage_class_specifier_opt
1675 static tree cp_parser_function_specifier_opt
1676 (cp_parser *, cp_decl_specifier_seq *);
1677 static tree cp_parser_type_specifier
1678 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1680 static tree cp_parser_simple_type_specifier
1681 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1682 static tree cp_parser_type_name
1684 static tree cp_parser_nonclass_name
1685 (cp_parser* parser);
1686 static tree cp_parser_elaborated_type_specifier
1687 (cp_parser *, bool, bool);
1688 static tree cp_parser_enum_specifier
1690 static void cp_parser_enumerator_list
1691 (cp_parser *, tree);
1692 static void cp_parser_enumerator_definition
1693 (cp_parser *, tree);
1694 static tree cp_parser_namespace_name
1696 static void cp_parser_namespace_definition
1698 static void cp_parser_namespace_body
1700 static tree cp_parser_qualified_namespace_specifier
1702 static void cp_parser_namespace_alias_definition
1704 static bool cp_parser_using_declaration
1705 (cp_parser *, bool);
1706 static void cp_parser_using_directive
1708 static void cp_parser_asm_definition
1710 static void cp_parser_linkage_specification
1712 static void cp_parser_static_assert
1713 (cp_parser *, bool);
1714 static tree cp_parser_decltype
1717 /* Declarators [gram.dcl.decl] */
1719 static tree cp_parser_init_declarator
1720 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1721 static cp_declarator *cp_parser_declarator
1722 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1723 static cp_declarator *cp_parser_direct_declarator
1724 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1725 static enum tree_code cp_parser_ptr_operator
1726 (cp_parser *, tree *, cp_cv_quals *);
1727 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1729 static tree cp_parser_declarator_id
1730 (cp_parser *, bool);
1731 static tree cp_parser_type_id
1733 static void cp_parser_type_specifier_seq
1734 (cp_parser *, bool, cp_decl_specifier_seq *);
1735 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1737 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1738 (cp_parser *, bool *);
1739 static cp_parameter_declarator *cp_parser_parameter_declaration
1740 (cp_parser *, bool, bool *);
1741 static tree cp_parser_default_argument
1742 (cp_parser *, bool);
1743 static void cp_parser_function_body
1745 static tree cp_parser_initializer
1746 (cp_parser *, bool *, bool *);
1747 static tree cp_parser_initializer_clause
1748 (cp_parser *, bool *);
1749 static tree cp_parser_braced_list
1750 (cp_parser*, bool*);
1751 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1752 (cp_parser *, bool *);
1754 static bool cp_parser_ctor_initializer_opt_and_function_body
1757 /* Classes [gram.class] */
1759 static tree cp_parser_class_name
1760 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1761 static tree cp_parser_class_specifier
1763 static tree cp_parser_class_head
1764 (cp_parser *, bool *, tree *, tree *);
1765 static enum tag_types cp_parser_class_key
1767 static void cp_parser_member_specification_opt
1769 static void cp_parser_member_declaration
1771 static tree cp_parser_pure_specifier
1773 static tree cp_parser_constant_initializer
1776 /* Derived classes [gram.class.derived] */
1778 static tree cp_parser_base_clause
1780 static tree cp_parser_base_specifier
1783 /* Special member functions [gram.special] */
1785 static tree cp_parser_conversion_function_id
1787 static tree cp_parser_conversion_type_id
1789 static cp_declarator *cp_parser_conversion_declarator_opt
1791 static bool cp_parser_ctor_initializer_opt
1793 static void cp_parser_mem_initializer_list
1795 static tree cp_parser_mem_initializer
1797 static tree cp_parser_mem_initializer_id
1800 /* Overloading [gram.over] */
1802 static tree cp_parser_operator_function_id
1804 static tree cp_parser_operator
1807 /* Templates [gram.temp] */
1809 static void cp_parser_template_declaration
1810 (cp_parser *, bool);
1811 static tree cp_parser_template_parameter_list
1813 static tree cp_parser_template_parameter
1814 (cp_parser *, bool *, bool *);
1815 static tree cp_parser_type_parameter
1816 (cp_parser *, bool *);
1817 static tree cp_parser_template_id
1818 (cp_parser *, bool, bool, bool);
1819 static tree cp_parser_template_name
1820 (cp_parser *, bool, bool, bool, bool *);
1821 static tree cp_parser_template_argument_list
1823 static tree cp_parser_template_argument
1825 static void cp_parser_explicit_instantiation
1827 static void cp_parser_explicit_specialization
1830 /* Exception handling [gram.exception] */
1832 static tree cp_parser_try_block
1834 static bool cp_parser_function_try_block
1836 static void cp_parser_handler_seq
1838 static void cp_parser_handler
1840 static tree cp_parser_exception_declaration
1842 static tree cp_parser_throw_expression
1844 static tree cp_parser_exception_specification_opt
1846 static tree cp_parser_type_id_list
1849 /* GNU Extensions */
1851 static tree cp_parser_asm_specification_opt
1853 static tree cp_parser_asm_operand_list
1855 static tree cp_parser_asm_clobber_list
1857 static tree cp_parser_attributes_opt
1859 static tree cp_parser_attribute_list
1861 static bool cp_parser_extension_opt
1862 (cp_parser *, int *);
1863 static void cp_parser_label_declaration
1866 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1867 static bool cp_parser_pragma
1868 (cp_parser *, enum pragma_context);
1870 /* Objective-C++ Productions */
1872 static tree cp_parser_objc_message_receiver
1874 static tree cp_parser_objc_message_args
1876 static tree cp_parser_objc_message_expression
1878 static tree cp_parser_objc_encode_expression
1880 static tree cp_parser_objc_defs_expression
1882 static tree cp_parser_objc_protocol_expression
1884 static tree cp_parser_objc_selector_expression
1886 static tree cp_parser_objc_expression
1888 static bool cp_parser_objc_selector_p
1890 static tree cp_parser_objc_selector
1892 static tree cp_parser_objc_protocol_refs_opt
1894 static void cp_parser_objc_declaration
1896 static tree cp_parser_objc_statement
1899 /* Utility Routines */
1901 static tree cp_parser_lookup_name
1902 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1903 static tree cp_parser_lookup_name_simple
1904 (cp_parser *, tree, location_t);
1905 static tree cp_parser_maybe_treat_template_as_class
1907 static bool cp_parser_check_declarator_template_parameters
1908 (cp_parser *, cp_declarator *, location_t);
1909 static bool cp_parser_check_template_parameters
1910 (cp_parser *, unsigned, location_t);
1911 static tree cp_parser_simple_cast_expression
1913 static tree cp_parser_global_scope_opt
1914 (cp_parser *, bool);
1915 static bool cp_parser_constructor_declarator_p
1916 (cp_parser *, bool);
1917 static tree cp_parser_function_definition_from_specifiers_and_declarator
1918 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1919 static tree cp_parser_function_definition_after_declarator
1920 (cp_parser *, bool);
1921 static void cp_parser_template_declaration_after_export
1922 (cp_parser *, bool);
1923 static void cp_parser_perform_template_parameter_access_checks
1924 (VEC (deferred_access_check,gc)*);
1925 static tree cp_parser_single_declaration
1926 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1927 static tree cp_parser_functional_cast
1928 (cp_parser *, tree);
1929 static tree cp_parser_save_member_function_body
1930 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1931 static tree cp_parser_enclosed_template_argument_list
1933 static void cp_parser_save_default_args
1934 (cp_parser *, tree);
1935 static void cp_parser_late_parsing_for_member
1936 (cp_parser *, tree);
1937 static void cp_parser_late_parsing_default_args
1938 (cp_parser *, tree);
1939 static tree cp_parser_sizeof_operand
1940 (cp_parser *, enum rid);
1941 static tree cp_parser_trait_expr
1942 (cp_parser *, enum rid);
1943 static bool cp_parser_declares_only_class_p
1945 static void cp_parser_set_storage_class
1946 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1947 static void cp_parser_set_decl_spec_type
1948 (cp_decl_specifier_seq *, tree, location_t, bool);
1949 static bool cp_parser_friend_p
1950 (const cp_decl_specifier_seq *);
1951 static cp_token *cp_parser_require
1952 (cp_parser *, enum cpp_ttype, const char *);
1953 static cp_token *cp_parser_require_keyword
1954 (cp_parser *, enum rid, const char *);
1955 static bool cp_parser_token_starts_function_definition_p
1957 static bool cp_parser_next_token_starts_class_definition_p
1959 static bool cp_parser_next_token_ends_template_argument_p
1961 static bool cp_parser_nth_token_starts_template_argument_list_p
1962 (cp_parser *, size_t);
1963 static enum tag_types cp_parser_token_is_class_key
1965 static void cp_parser_check_class_key
1966 (enum tag_types, tree type);
1967 static void cp_parser_check_access_in_redeclaration
1968 (tree type, location_t location);
1969 static bool cp_parser_optional_template_keyword
1971 static void cp_parser_pre_parsed_nested_name_specifier
1973 static bool cp_parser_cache_group
1974 (cp_parser *, enum cpp_ttype, unsigned);
1975 static void cp_parser_parse_tentatively
1977 static void cp_parser_commit_to_tentative_parse
1979 static void cp_parser_abort_tentative_parse
1981 static bool cp_parser_parse_definitely
1983 static inline bool cp_parser_parsing_tentatively
1985 static bool cp_parser_uncommitted_to_tentative_parse_p
1987 static void cp_parser_error
1988 (cp_parser *, const char *);
1989 static void cp_parser_name_lookup_error
1990 (cp_parser *, tree, tree, const char *, location_t);
1991 static bool cp_parser_simulate_error
1993 static bool cp_parser_check_type_definition
1995 static void cp_parser_check_for_definition_in_return_type
1996 (cp_declarator *, tree, location_t type_location);
1997 static void cp_parser_check_for_invalid_template_id
1998 (cp_parser *, tree, location_t location);
1999 static bool cp_parser_non_integral_constant_expression
2000 (cp_parser *, const char *);
2001 static void cp_parser_diagnose_invalid_type_name
2002 (cp_parser *, tree, tree, location_t);
2003 static bool cp_parser_parse_and_diagnose_invalid_type_name
2005 static int cp_parser_skip_to_closing_parenthesis
2006 (cp_parser *, bool, bool, bool);
2007 static void cp_parser_skip_to_end_of_statement
2009 static void cp_parser_consume_semicolon_at_end_of_statement
2011 static void cp_parser_skip_to_end_of_block_or_statement
2013 static bool cp_parser_skip_to_closing_brace
2015 static void cp_parser_skip_to_end_of_template_parameter_list
2017 static void cp_parser_skip_to_pragma_eol
2018 (cp_parser*, cp_token *);
2019 static bool cp_parser_error_occurred
2021 static bool cp_parser_allow_gnu_extensions_p
2023 static bool cp_parser_is_string_literal
2025 static bool cp_parser_is_keyword
2026 (cp_token *, enum rid);
2027 static tree cp_parser_make_typename_type
2028 (cp_parser *, tree, tree, location_t location);
2029 static cp_declarator * cp_parser_make_indirect_declarator
2030 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2032 /* Returns nonzero if we are parsing tentatively. */
2035 cp_parser_parsing_tentatively (cp_parser* parser)
2037 return parser->context->next != NULL;
2040 /* Returns nonzero if TOKEN is a string literal. */
2043 cp_parser_is_string_literal (cp_token* token)
2045 return (token->type == CPP_STRING ||
2046 token->type == CPP_STRING16 ||
2047 token->type == CPP_STRING32 ||
2048 token->type == CPP_WSTRING);
2051 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2054 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2056 return token->keyword == keyword;
2059 /* If not parsing tentatively, issue a diagnostic of the form
2060 FILE:LINE: MESSAGE before TOKEN
2061 where TOKEN is the next token in the input stream. MESSAGE
2062 (specified by the caller) is usually of the form "expected
2066 cp_parser_error (cp_parser* parser, const char* message)
2068 if (!cp_parser_simulate_error (parser))
2070 cp_token *token = cp_lexer_peek_token (parser->lexer);
2071 /* This diagnostic makes more sense if it is tagged to the line
2072 of the token we just peeked at. */
2073 cp_lexer_set_source_position_from_token (token);
2075 if (token->type == CPP_PRAGMA)
2077 error ("%H%<#pragma%> is not allowed here", &token->location);
2078 cp_parser_skip_to_pragma_eol (parser, token);
2082 c_parse_error (message,
2083 /* Because c_parser_error does not understand
2084 CPP_KEYWORD, keywords are treated like
2086 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2091 /* Issue an error about name-lookup failing. NAME is the
2092 IDENTIFIER_NODE DECL is the result of
2093 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2094 the thing that we hoped to find. */
2097 cp_parser_name_lookup_error (cp_parser* parser,
2100 const char* desired,
2101 location_t location)
2103 /* If name lookup completely failed, tell the user that NAME was not
2105 if (decl == error_mark_node)
2107 if (parser->scope && parser->scope != global_namespace)
2108 error ("%H%<%E::%E%> has not been declared",
2109 &location, parser->scope, name);
2110 else if (parser->scope == global_namespace)
2111 error ("%H%<::%E%> has not been declared", &location, name);
2112 else if (parser->object_scope
2113 && !CLASS_TYPE_P (parser->object_scope))
2114 error ("%Hrequest for member %qE in non-class type %qT",
2115 &location, name, parser->object_scope);
2116 else if (parser->object_scope)
2117 error ("%H%<%T::%E%> has not been declared",
2118 &location, parser->object_scope, name);
2120 error ("%H%qE has not been declared", &location, name);
2122 else if (parser->scope && parser->scope != global_namespace)
2123 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2124 else if (parser->scope == global_namespace)
2125 error ("%H%<::%E%> %s", &location, name, desired);
2127 error ("%H%qE %s", &location, name, desired);
2130 /* If we are parsing tentatively, remember that an error has occurred
2131 during this tentative parse. Returns true if the error was
2132 simulated; false if a message should be issued by the caller. */
2135 cp_parser_simulate_error (cp_parser* parser)
2137 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2139 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2145 /* Check for repeated decl-specifiers. */
2148 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2149 location_t location)
2153 for (ds = ds_first; ds != ds_last; ++ds)
2155 unsigned count = decl_specs->specs[(int)ds];
2158 /* The "long" specifier is a special case because of "long long". */
2162 error ("%H%<long long long%> is too long for GCC", &location);
2163 else if (pedantic && !in_system_header && warn_long_long
2164 && cxx_dialect == cxx98)
2165 pedwarn (location, OPT_Wlong_long,
2166 "ISO C++ 1998 does not support %<long long%>");
2170 static const char *const decl_spec_names[] = {
2186 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2191 /* This function is called when a type is defined. If type
2192 definitions are forbidden at this point, an error message is
2196 cp_parser_check_type_definition (cp_parser* parser)
2198 /* If types are forbidden here, issue a message. */
2199 if (parser->type_definition_forbidden_message)
2201 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2202 in the message need to be interpreted. */
2203 error (parser->type_definition_forbidden_message);
2209 /* This function is called when the DECLARATOR is processed. The TYPE
2210 was a type defined in the decl-specifiers. If it is invalid to
2211 define a type in the decl-specifiers for DECLARATOR, an error is
2212 issued. TYPE_LOCATION is the location of TYPE and is used
2213 for error reporting. */
2216 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2217 tree type, location_t type_location)
2219 /* [dcl.fct] forbids type definitions in return types.
2220 Unfortunately, it's not easy to know whether or not we are
2221 processing a return type until after the fact. */
2223 && (declarator->kind == cdk_pointer
2224 || declarator->kind == cdk_reference
2225 || declarator->kind == cdk_ptrmem))
2226 declarator = declarator->declarator;
2228 && declarator->kind == cdk_function)
2230 error ("%Hnew types may not be defined in a return type", &type_location);
2231 inform (type_location,
2232 "(perhaps a semicolon is missing after the definition of %qT)",
2237 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2238 "<" in any valid C++ program. If the next token is indeed "<",
2239 issue a message warning the user about what appears to be an
2240 invalid attempt to form a template-id. LOCATION is the location
2241 of the type-specifier (TYPE) */
2244 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2245 tree type, location_t location)
2247 cp_token_position start = 0;
2249 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2252 error ("%H%qT is not a template", &location, type);
2253 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2254 error ("%H%qE is not a template", &location, type);
2256 error ("%Hinvalid template-id", &location);
2257 /* Remember the location of the invalid "<". */
2258 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2259 start = cp_lexer_token_position (parser->lexer, true);
2260 /* Consume the "<". */
2261 cp_lexer_consume_token (parser->lexer);
2262 /* Parse the template arguments. */
2263 cp_parser_enclosed_template_argument_list (parser);
2264 /* Permanently remove the invalid template arguments so that
2265 this error message is not issued again. */
2267 cp_lexer_purge_tokens_after (parser->lexer, start);
2271 /* If parsing an integral constant-expression, issue an error message
2272 about the fact that THING appeared and return true. Otherwise,
2273 return false. In either case, set
2274 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2277 cp_parser_non_integral_constant_expression (cp_parser *parser,
2280 parser->non_integral_constant_expression_p = true;
2281 if (parser->integral_constant_expression_p)
2283 if (!parser->allow_non_integral_constant_expression_p)
2285 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2286 in the message need to be interpreted. */
2287 char *message = concat (thing,
2288 " cannot appear in a constant-expression",
2298 /* Emit a diagnostic for an invalid type name. SCOPE is the
2299 qualifying scope (or NULL, if none) for ID. This function commits
2300 to the current active tentative parse, if any. (Otherwise, the
2301 problematic construct might be encountered again later, resulting
2302 in duplicate error messages.) LOCATION is the location of ID. */
2305 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2306 tree scope, tree id,
2307 location_t location)
2309 tree decl, old_scope;
2310 /* Try to lookup the identifier. */
2311 old_scope = parser->scope;
2312 parser->scope = scope;
2313 decl = cp_parser_lookup_name_simple (parser, id, location);
2314 parser->scope = old_scope;
2315 /* If the lookup found a template-name, it means that the user forgot
2316 to specify an argument list. Emit a useful error message. */
2317 if (TREE_CODE (decl) == TEMPLATE_DECL)
2318 error ("%Hinvalid use of template-name %qE without an argument list",
2320 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2321 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2322 else if (TREE_CODE (decl) == TYPE_DECL)
2323 /* Something like 'unsigned A a;' */
2324 error ("%Hinvalid combination of multiple type-specifiers",
2326 else if (!parser->scope)
2328 /* Issue an error message. */
2329 error ("%H%qE does not name a type", &location, id);
2330 /* If we're in a template class, it's possible that the user was
2331 referring to a type from a base class. For example:
2333 template <typename T> struct A { typedef T X; };
2334 template <typename T> struct B : public A<T> { X x; };
2336 The user should have said "typename A<T>::X". */
2337 if (processing_template_decl && current_class_type
2338 && TYPE_BINFO (current_class_type))
2342 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2346 tree base_type = BINFO_TYPE (b);
2347 if (CLASS_TYPE_P (base_type)
2348 && dependent_type_p (base_type))
2351 /* Go from a particular instantiation of the
2352 template (which will have an empty TYPE_FIELDs),
2353 to the main version. */
2354 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2355 for (field = TYPE_FIELDS (base_type);
2357 field = TREE_CHAIN (field))
2358 if (TREE_CODE (field) == TYPE_DECL
2359 && DECL_NAME (field) == id)
2362 "(perhaps %<typename %T::%E%> was intended)",
2363 BINFO_TYPE (b), id);
2372 /* Here we diagnose qualified-ids where the scope is actually correct,
2373 but the identifier does not resolve to a valid type name. */
2374 else if (parser->scope != error_mark_node)
2376 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2377 error ("%H%qE in namespace %qE does not name a type",
2378 &location, id, parser->scope);
2379 else if (TYPE_P (parser->scope))
2380 error ("%H%qE in class %qT does not name a type",
2381 &location, id, parser->scope);
2385 cp_parser_commit_to_tentative_parse (parser);
2388 /* Check for a common situation where a type-name should be present,
2389 but is not, and issue a sensible error message. Returns true if an
2390 invalid type-name was detected.
2392 The situation handled by this function are variable declarations of the
2393 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2394 Usually, `ID' should name a type, but if we got here it means that it
2395 does not. We try to emit the best possible error message depending on
2396 how exactly the id-expression looks like. */
2399 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2402 cp_token *token = cp_lexer_peek_token (parser->lexer);
2404 cp_parser_parse_tentatively (parser);
2405 id = cp_parser_id_expression (parser,
2406 /*template_keyword_p=*/false,
2407 /*check_dependency_p=*/true,
2408 /*template_p=*/NULL,
2409 /*declarator_p=*/true,
2410 /*optional_p=*/false);
2411 /* After the id-expression, there should be a plain identifier,
2412 otherwise this is not a simple variable declaration. Also, if
2413 the scope is dependent, we cannot do much. */
2414 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2415 || (parser->scope && TYPE_P (parser->scope)
2416 && dependent_type_p (parser->scope))
2417 || TREE_CODE (id) == TYPE_DECL)
2419 cp_parser_abort_tentative_parse (parser);
2422 if (!cp_parser_parse_definitely (parser))
2425 /* Emit a diagnostic for the invalid type. */
2426 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2427 id, token->location);
2428 /* Skip to the end of the declaration; there's no point in
2429 trying to process it. */
2430 cp_parser_skip_to_end_of_block_or_statement (parser);
2434 /* Consume tokens up to, and including, the next non-nested closing `)'.
2435 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2436 are doing error recovery. Returns -1 if OR_COMMA is true and we
2437 found an unnested comma. */
2440 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445 unsigned paren_depth = 0;
2446 unsigned brace_depth = 0;
2448 if (recovering && !or_comma
2449 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454 cp_token * token = cp_lexer_peek_token (parser->lexer);
2456 switch (token->type)
2459 case CPP_PRAGMA_EOL:
2460 /* If we've run out of tokens, then there is no closing `)'. */
2464 /* This matches the processing in skip_to_end_of_statement. */
2469 case CPP_OPEN_BRACE:
2472 case CPP_CLOSE_BRACE:
2478 if (recovering && or_comma && !brace_depth && !paren_depth)
2482 case CPP_OPEN_PAREN:
2487 case CPP_CLOSE_PAREN:
2488 if (!brace_depth && !paren_depth--)
2491 cp_lexer_consume_token (parser->lexer);
2500 /* Consume the token. */
2501 cp_lexer_consume_token (parser->lexer);
2505 /* Consume tokens until we reach the end of the current statement.
2506 Normally, that will be just before consuming a `;'. However, if a
2507 non-nested `}' comes first, then we stop before consuming that. */
2510 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2512 unsigned nesting_depth = 0;
2516 cp_token *token = cp_lexer_peek_token (parser->lexer);
2518 switch (token->type)
2521 case CPP_PRAGMA_EOL:
2522 /* If we've run out of tokens, stop. */
2526 /* If the next token is a `;', we have reached the end of the
2532 case CPP_CLOSE_BRACE:
2533 /* If this is a non-nested '}', stop before consuming it.
2534 That way, when confronted with something like:
2538 we stop before consuming the closing '}', even though we
2539 have not yet reached a `;'. */
2540 if (nesting_depth == 0)
2543 /* If it is the closing '}' for a block that we have
2544 scanned, stop -- but only after consuming the token.
2550 we will stop after the body of the erroneously declared
2551 function, but before consuming the following `typedef'
2553 if (--nesting_depth == 0)
2555 cp_lexer_consume_token (parser->lexer);
2559 case CPP_OPEN_BRACE:
2567 /* Consume the token. */
2568 cp_lexer_consume_token (parser->lexer);
2572 /* This function is called at the end of a statement or declaration.
2573 If the next token is a semicolon, it is consumed; otherwise, error
2574 recovery is attempted. */
2577 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2579 /* Look for the trailing `;'. */
2580 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2582 /* If there is additional (erroneous) input, skip to the end of
2584 cp_parser_skip_to_end_of_statement (parser);
2585 /* If the next token is now a `;', consume it. */
2586 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2587 cp_lexer_consume_token (parser->lexer);
2591 /* Skip tokens until we have consumed an entire block, or until we
2592 have consumed a non-nested `;'. */
2595 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2597 int nesting_depth = 0;
2599 while (nesting_depth >= 0)
2601 cp_token *token = cp_lexer_peek_token (parser->lexer);
2603 switch (token->type)
2606 case CPP_PRAGMA_EOL:
2607 /* If we've run out of tokens, stop. */
2611 /* Stop if this is an unnested ';'. */
2616 case CPP_CLOSE_BRACE:
2617 /* Stop if this is an unnested '}', or closes the outermost
2624 case CPP_OPEN_BRACE:
2633 /* Consume the token. */
2634 cp_lexer_consume_token (parser->lexer);
2638 /* Skip tokens until a non-nested closing curly brace is the next
2639 token, or there are no more tokens. Return true in the first case,
2643 cp_parser_skip_to_closing_brace (cp_parser *parser)
2645 unsigned nesting_depth = 0;
2649 cp_token *token = cp_lexer_peek_token (parser->lexer);
2651 switch (token->type)
2654 case CPP_PRAGMA_EOL:
2655 /* If we've run out of tokens, stop. */
2658 case CPP_CLOSE_BRACE:
2659 /* If the next token is a non-nested `}', then we have reached
2660 the end of the current block. */
2661 if (nesting_depth-- == 0)
2665 case CPP_OPEN_BRACE:
2666 /* If it the next token is a `{', then we are entering a new
2667 block. Consume the entire block. */
2675 /* Consume the token. */
2676 cp_lexer_consume_token (parser->lexer);
2680 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2681 parameter is the PRAGMA token, allowing us to purge the entire pragma
2685 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2689 parser->lexer->in_pragma = false;
2692 token = cp_lexer_consume_token (parser->lexer);
2693 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2695 /* Ensure that the pragma is not parsed again. */
2696 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2699 /* Require pragma end of line, resyncing with it as necessary. The
2700 arguments are as for cp_parser_skip_to_pragma_eol. */
2703 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2705 parser->lexer->in_pragma = false;
2706 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2707 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2710 /* This is a simple wrapper around make_typename_type. When the id is
2711 an unresolved identifier node, we can provide a superior diagnostic
2712 using cp_parser_diagnose_invalid_type_name. */
2715 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2716 tree id, location_t id_location)
2719 if (TREE_CODE (id) == IDENTIFIER_NODE)
2721 result = make_typename_type (scope, id, typename_type,
2722 /*complain=*/tf_none);
2723 if (result == error_mark_node)
2724 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2727 return make_typename_type (scope, id, typename_type, tf_error);
2730 /* This is a wrapper around the
2731 make_{pointer,ptrmem,reference}_declarator functions that decides
2732 which one to call based on the CODE and CLASS_TYPE arguments. The
2733 CODE argument should be one of the values returned by
2734 cp_parser_ptr_operator. */
2735 static cp_declarator *
2736 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2737 cp_cv_quals cv_qualifiers,
2738 cp_declarator *target)
2740 if (code == ERROR_MARK)
2741 return cp_error_declarator;
2743 if (code == INDIRECT_REF)
2744 if (class_type == NULL_TREE)
2745 return make_pointer_declarator (cv_qualifiers, target);
2747 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2748 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2749 return make_reference_declarator (cv_qualifiers, target, false);
2750 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2751 return make_reference_declarator (cv_qualifiers, target, true);
2755 /* Create a new C++ parser. */
2758 cp_parser_new (void)
2764 /* cp_lexer_new_main is called before calling ggc_alloc because
2765 cp_lexer_new_main might load a PCH file. */
2766 lexer = cp_lexer_new_main ();
2768 /* Initialize the binops_by_token so that we can get the tree
2769 directly from the token. */
2770 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2771 binops_by_token[binops[i].token_type] = binops[i];
2773 parser = GGC_CNEW (cp_parser);
2774 parser->lexer = lexer;
2775 parser->context = cp_parser_context_new (NULL);
2777 /* For now, we always accept GNU extensions. */
2778 parser->allow_gnu_extensions_p = 1;
2780 /* The `>' token is a greater-than operator, not the end of a
2782 parser->greater_than_is_operator_p = true;
2784 parser->default_arg_ok_p = true;
2786 /* We are not parsing a constant-expression. */
2787 parser->integral_constant_expression_p = false;
2788 parser->allow_non_integral_constant_expression_p = false;
2789 parser->non_integral_constant_expression_p = false;
2791 /* Local variable names are not forbidden. */
2792 parser->local_variables_forbidden_p = false;
2794 /* We are not processing an `extern "C"' declaration. */
2795 parser->in_unbraced_linkage_specification_p = false;
2797 /* We are not processing a declarator. */
2798 parser->in_declarator_p = false;
2800 /* We are not processing a template-argument-list. */
2801 parser->in_template_argument_list_p = false;
2803 /* We are not in an iteration statement. */
2804 parser->in_statement = 0;
2806 /* We are not in a switch statement. */
2807 parser->in_switch_statement_p = false;
2809 /* We are not parsing a type-id inside an expression. */
2810 parser->in_type_id_in_expr_p = false;
2812 /* Declarations aren't implicitly extern "C". */
2813 parser->implicit_extern_c = false;
2815 /* String literals should be translated to the execution character set. */
2816 parser->translate_strings_p = true;
2818 /* We are not parsing a function body. */
2819 parser->in_function_body = false;
2821 /* The unparsed function queue is empty. */
2822 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2824 /* There are no classes being defined. */
2825 parser->num_classes_being_defined = 0;
2827 /* No template parameters apply. */
2828 parser->num_template_parameter_lists = 0;
2833 /* Create a cp_lexer structure which will emit the tokens in CACHE
2834 and push it onto the parser's lexer stack. This is used for delayed
2835 parsing of in-class method bodies and default arguments, and should
2836 not be confused with tentative parsing. */
2838 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2840 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2841 lexer->next = parser->lexer;
2842 parser->lexer = lexer;
2844 /* Move the current source position to that of the first token in the
2846 cp_lexer_set_source_position_from_token (lexer->next_token);
2849 /* Pop the top lexer off the parser stack. This is never used for the
2850 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2852 cp_parser_pop_lexer (cp_parser *parser)
2854 cp_lexer *lexer = parser->lexer;
2855 parser->lexer = lexer->next;
2856 cp_lexer_destroy (lexer);
2858 /* Put the current source position back where it was before this
2859 lexer was pushed. */
2860 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2863 /* Lexical conventions [gram.lex] */
2865 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2869 cp_parser_identifier (cp_parser* parser)
2873 /* Look for the identifier. */
2874 token = cp_parser_require (parser, CPP_NAME, "identifier");
2875 /* Return the value. */
2876 return token ? token->u.value : error_mark_node;
2879 /* Parse a sequence of adjacent string constants. Returns a
2880 TREE_STRING representing the combined, nul-terminated string
2881 constant. If TRANSLATE is true, translate the string to the
2882 execution character set. If WIDE_OK is true, a wide string is
2885 C++98 [lex.string] says that if a narrow string literal token is
2886 adjacent to a wide string literal token, the behavior is undefined.
2887 However, C99 6.4.5p4 says that this results in a wide string literal.
2888 We follow C99 here, for consistency with the C front end.
2890 This code is largely lifted from lex_string() in c-lex.c.
2892 FUTURE: ObjC++ will need to handle @-strings here. */
2894 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2898 struct obstack str_ob;
2899 cpp_string str, istr, *strs;
2901 enum cpp_ttype type;
2903 tok = cp_lexer_peek_token (parser->lexer);
2904 if (!cp_parser_is_string_literal (tok))
2906 cp_parser_error (parser, "expected string-literal");
2907 return error_mark_node;
2912 /* Try to avoid the overhead of creating and destroying an obstack
2913 for the common case of just one string. */
2914 if (!cp_parser_is_string_literal
2915 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2917 cp_lexer_consume_token (parser->lexer);
2919 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2920 str.len = TREE_STRING_LENGTH (tok->u.value);
2927 gcc_obstack_init (&str_ob);
2932 cp_lexer_consume_token (parser->lexer);
2934 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2935 str.len = TREE_STRING_LENGTH (tok->u.value);
2937 if (type != tok->type)
2939 if (type == CPP_STRING)
2941 else if (tok->type != CPP_STRING)
2942 error ("%Hunsupported non-standard concatenation "
2943 "of string literals", &tok->location);
2946 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2948 tok = cp_lexer_peek_token (parser->lexer);
2950 while (cp_parser_is_string_literal (tok));
2952 strs = (cpp_string *) obstack_finish (&str_ob);
2955 if (type != CPP_STRING && !wide_ok)
2957 cp_parser_error (parser, "a wide string is invalid in this context");
2961 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2962 (parse_in, strs, count, &istr, type))
2964 value = build_string (istr.len, (const char *)istr.text);
2965 free (CONST_CAST (unsigned char *, istr.text));
2971 TREE_TYPE (value) = char_array_type_node;
2974 TREE_TYPE (value) = char16_array_type_node;
2977 TREE_TYPE (value) = char32_array_type_node;
2980 TREE_TYPE (value) = wchar_array_type_node;
2984 value = fix_string_type (value);
2987 /* cpp_interpret_string has issued an error. */
2988 value = error_mark_node;
2991 obstack_free (&str_ob, 0);
2997 /* Basic concepts [gram.basic] */
2999 /* Parse a translation-unit.
3002 declaration-seq [opt]
3004 Returns TRUE if all went well. */
3007 cp_parser_translation_unit (cp_parser* parser)
3009 /* The address of the first non-permanent object on the declarator
3011 static void *declarator_obstack_base;
3015 /* Create the declarator obstack, if necessary. */
3016 if (!cp_error_declarator)
3018 gcc_obstack_init (&declarator_obstack);
3019 /* Create the error declarator. */
3020 cp_error_declarator = make_declarator (cdk_error);
3021 /* Create the empty parameter list. */
3022 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3023 /* Remember where the base of the declarator obstack lies. */
3024 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3027 cp_parser_declaration_seq_opt (parser);
3029 /* If there are no tokens left then all went well. */
3030 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3032 /* Get rid of the token array; we don't need it any more. */
3033 cp_lexer_destroy (parser->lexer);
3034 parser->lexer = NULL;
3036 /* This file might have been a context that's implicitly extern
3037 "C". If so, pop the lang context. (Only relevant for PCH.) */
3038 if (parser->implicit_extern_c)
3040 pop_lang_context ();
3041 parser->implicit_extern_c = false;
3045 finish_translation_unit ();
3051 cp_parser_error (parser, "expected declaration");
3055 /* Make sure the declarator obstack was fully cleaned up. */
3056 gcc_assert (obstack_next_free (&declarator_obstack)
3057 == declarator_obstack_base);
3059 /* All went well. */
3063 /* Expressions [gram.expr] */
3065 /* Parse a primary-expression.
3076 ( compound-statement )
3077 __builtin_va_arg ( assignment-expression , type-id )
3078 __builtin_offsetof ( type-id , offsetof-expression )
3081 __has_nothrow_assign ( type-id )
3082 __has_nothrow_constructor ( type-id )
3083 __has_nothrow_copy ( type-id )
3084 __has_trivial_assign ( type-id )
3085 __has_trivial_constructor ( type-id )
3086 __has_trivial_copy ( type-id )
3087 __has_trivial_destructor ( type-id )
3088 __has_virtual_destructor ( type-id )
3089 __is_abstract ( type-id )
3090 __is_base_of ( type-id , type-id )
3091 __is_class ( type-id )
3092 __is_convertible_to ( type-id , type-id )
3093 __is_empty ( type-id )
3094 __is_enum ( type-id )
3095 __is_pod ( type-id )
3096 __is_polymorphic ( type-id )
3097 __is_union ( type-id )
3099 Objective-C++ Extension:
3107 ADDRESS_P is true iff this expression was immediately preceded by
3108 "&" and therefore might denote a pointer-to-member. CAST_P is true
3109 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3110 true iff this expression is a template argument.
3112 Returns a representation of the expression. Upon return, *IDK
3113 indicates what kind of id-expression (if any) was present. */
3116 cp_parser_primary_expression (cp_parser *parser,
3119 bool template_arg_p,
3122 cp_token *token = NULL;
3124 /* Assume the primary expression is not an id-expression. */
3125 *idk = CP_ID_KIND_NONE;
3127 /* Peek at the next token. */
3128 token = cp_lexer_peek_token (parser->lexer);
3129 switch (token->type)
3142 token = cp_lexer_consume_token (parser->lexer);
3143 /* Floating-point literals are only allowed in an integral
3144 constant expression if they are cast to an integral or
3145 enumeration type. */
3146 if (TREE_CODE (token->u.value) == REAL_CST
3147 && parser->integral_constant_expression_p
3150 /* CAST_P will be set even in invalid code like "int(2.7 +
3151 ...)". Therefore, we have to check that the next token
3152 is sure to end the cast. */
3155 cp_token *next_token;
3157 next_token = cp_lexer_peek_token (parser->lexer);
3158 if (/* The comma at the end of an
3159 enumerator-definition. */
3160 next_token->type != CPP_COMMA
3161 /* The curly brace at the end of an enum-specifier. */
3162 && next_token->type != CPP_CLOSE_BRACE
3163 /* The end of a statement. */
3164 && next_token->type != CPP_SEMICOLON
3165 /* The end of the cast-expression. */
3166 && next_token->type != CPP_CLOSE_PAREN
3167 /* The end of an array bound. */
3168 && next_token->type != CPP_CLOSE_SQUARE
3169 /* The closing ">" in a template-argument-list. */
3170 && (next_token->type != CPP_GREATER
3171 || parser->greater_than_is_operator_p)
3172 /* C++0x only: A ">>" treated like two ">" tokens,
3173 in a template-argument-list. */
3174 && (next_token->type != CPP_RSHIFT
3175 || (cxx_dialect == cxx98)
3176 || parser->greater_than_is_operator_p))
3180 /* If we are within a cast, then the constraint that the
3181 cast is to an integral or enumeration type will be
3182 checked at that point. If we are not within a cast, then
3183 this code is invalid. */
3185 cp_parser_non_integral_constant_expression
3186 (parser, "floating-point literal");
3188 return token->u.value;
3194 /* ??? Should wide strings be allowed when parser->translate_strings_p
3195 is false (i.e. in attributes)? If not, we can kill the third
3196 argument to cp_parser_string_literal. */
3197 return cp_parser_string_literal (parser,
3198 parser->translate_strings_p,
3201 case CPP_OPEN_PAREN:
3204 bool saved_greater_than_is_operator_p;
3206 /* Consume the `('. */
3207 cp_lexer_consume_token (parser->lexer);
3208 /* Within a parenthesized expression, a `>' token is always
3209 the greater-than operator. */
3210 saved_greater_than_is_operator_p
3211 = parser->greater_than_is_operator_p;
3212 parser->greater_than_is_operator_p = true;
3213 /* If we see `( { ' then we are looking at the beginning of
3214 a GNU statement-expression. */
3215 if (cp_parser_allow_gnu_extensions_p (parser)
3216 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3218 /* Statement-expressions are not allowed by the standard. */
3219 pedwarn (token->location, OPT_pedantic,
3220 "ISO C++ forbids braced-groups within expressions");
3222 /* And they're not allowed outside of a function-body; you
3223 cannot, for example, write:
3225 int i = ({ int j = 3; j + 1; });
3227 at class or namespace scope. */
3228 if (!parser->in_function_body
3229 || parser->in_template_argument_list_p)
3231 error ("%Hstatement-expressions are not allowed outside "
3232 "functions nor in template-argument lists",
3234 cp_parser_skip_to_end_of_block_or_statement (parser);
3235 expr = error_mark_node;
3239 /* Start the statement-expression. */
3240 expr = begin_stmt_expr ();
3241 /* Parse the compound-statement. */
3242 cp_parser_compound_statement (parser, expr, false);
3244 expr = finish_stmt_expr (expr, false);
3249 /* Parse the parenthesized expression. */
3250 expr = cp_parser_expression (parser, cast_p);
3251 /* Let the front end know that this expression was
3252 enclosed in parentheses. This matters in case, for
3253 example, the expression is of the form `A::B', since
3254 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3256 finish_parenthesized_expr (expr);
3258 /* The `>' token might be the end of a template-id or
3259 template-parameter-list now. */
3260 parser->greater_than_is_operator_p
3261 = saved_greater_than_is_operator_p;
3262 /* Consume the `)'. */
3263 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3264 cp_parser_skip_to_end_of_statement (parser);
3270 switch (token->keyword)
3272 /* These two are the boolean literals. */
3274 cp_lexer_consume_token (parser->lexer);
3275 return boolean_true_node;
3277 cp_lexer_consume_token (parser->lexer);
3278 return boolean_false_node;
3280 /* The `__null' literal. */
3282 cp_lexer_consume_token (parser->lexer);
3285 /* Recognize the `this' keyword. */
3287 cp_lexer_consume_token (parser->lexer);
3288 if (parser->local_variables_forbidden_p)
3290 error ("%H%<this%> may not be used in this context",
3292 return error_mark_node;
3294 /* Pointers cannot appear in constant-expressions. */
3295 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3296 return error_mark_node;
3297 return finish_this_expr ();
3299 /* The `operator' keyword can be the beginning of an
3304 case RID_FUNCTION_NAME:
3305 case RID_PRETTY_FUNCTION_NAME:
3306 case RID_C99_FUNCTION_NAME:
3307 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3308 __func__ are the names of variables -- but they are
3309 treated specially. Therefore, they are handled here,
3310 rather than relying on the generic id-expression logic
3311 below. Grammatically, these names are id-expressions.
3313 Consume the token. */
3314 token = cp_lexer_consume_token (parser->lexer);
3315 /* Look up the name. */
3316 return finish_fname (token->u.value);
3323 /* The `__builtin_va_arg' construct is used to handle
3324 `va_arg'. Consume the `__builtin_va_arg' token. */
3325 cp_lexer_consume_token (parser->lexer);
3326 /* Look for the opening `('. */
3327 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3328 /* Now, parse the assignment-expression. */
3329 expression = cp_parser_assignment_expression (parser,
3331 /* Look for the `,'. */
3332 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3333 /* Parse the type-id. */
3334 type = cp_parser_type_id (parser);
3335 /* Look for the closing `)'. */
3336 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3337 /* Using `va_arg' in a constant-expression is not
3339 if (cp_parser_non_integral_constant_expression (parser,
3341 return error_mark_node;
3342 return build_x_va_arg (expression, type);
3346 return cp_parser_builtin_offsetof (parser);
3348 case RID_HAS_NOTHROW_ASSIGN:
3349 case RID_HAS_NOTHROW_CONSTRUCTOR:
3350 case RID_HAS_NOTHROW_COPY:
3351 case RID_HAS_TRIVIAL_ASSIGN:
3352 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3353 case RID_HAS_TRIVIAL_COPY:
3354 case RID_HAS_TRIVIAL_DESTRUCTOR:
3355 case RID_HAS_VIRTUAL_DESTRUCTOR:
3356 case RID_IS_ABSTRACT:
3357 case RID_IS_BASE_OF:
3359 case RID_IS_CONVERTIBLE_TO:
3363 case RID_IS_POLYMORPHIC:
3365 return cp_parser_trait_expr (parser, token->keyword);
3367 /* Objective-C++ expressions. */
3369 case RID_AT_PROTOCOL:
3370 case RID_AT_SELECTOR:
3371 return cp_parser_objc_expression (parser);
3374 cp_parser_error (parser, "expected primary-expression");
3375 return error_mark_node;
3378 /* An id-expression can start with either an identifier, a
3379 `::' as the beginning of a qualified-id, or the "operator"
3383 case CPP_TEMPLATE_ID:
3384 case CPP_NESTED_NAME_SPECIFIER:
3388 const char *error_msg;
3391 cp_token *id_expr_token;
3394 /* Parse the id-expression. */
3396 = cp_parser_id_expression (parser,
3397 /*template_keyword_p=*/false,
3398 /*check_dependency_p=*/true,
3400 /*declarator_p=*/false,
3401 /*optional_p=*/false);
3402 if (id_expression == error_mark_node)
3403 return error_mark_node;
3404 id_expr_token = token;
3405 token = cp_lexer_peek_token (parser->lexer);
3406 done = (token->type != CPP_OPEN_SQUARE
3407 && token->type != CPP_OPEN_PAREN
3408 && token->type != CPP_DOT
3409 && token->type != CPP_DEREF
3410 && token->type != CPP_PLUS_PLUS
3411 && token->type != CPP_MINUS_MINUS);
3412 /* If we have a template-id, then no further lookup is
3413 required. If the template-id was for a template-class, we
3414 will sometimes have a TYPE_DECL at this point. */
3415 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3416 || TREE_CODE (id_expression) == TYPE_DECL)
3417 decl = id_expression;
3418 /* Look up the name. */
3421 tree ambiguous_decls;
3423 decl = cp_parser_lookup_name (parser, id_expression,
3426 /*is_namespace=*/false,
3427 /*check_dependency=*/true,
3429 id_expr_token->location);
3430 /* If the lookup was ambiguous, an error will already have
3432 if (ambiguous_decls)
3433 return error_mark_node;
3435 /* In Objective-C++, an instance variable (ivar) may be preferred
3436 to whatever cp_parser_lookup_name() found. */
3437 decl = objc_lookup_ivar (decl, id_expression);
3439 /* If name lookup gives us a SCOPE_REF, then the
3440 qualifying scope was dependent. */
3441 if (TREE_CODE (decl) == SCOPE_REF)
3443 /* At this point, we do not know if DECL is a valid
3444 integral constant expression. We assume that it is
3445 in fact such an expression, so that code like:
3447 template <int N> struct A {
3451 is accepted. At template-instantiation time, we
3452 will check that B<N>::i is actually a constant. */
3455 /* Check to see if DECL is a local variable in a context
3456 where that is forbidden. */
3457 if (parser->local_variables_forbidden_p
3458 && local_variable_p (decl))
3460 /* It might be that we only found DECL because we are
3461 trying to be generous with pre-ISO scoping rules.
3462 For example, consider:
3466 for (int i = 0; i < 10; ++i) {}
3467 extern void f(int j = i);
3470 Here, name look up will originally find the out
3471 of scope `i'. We need to issue a warning message,
3472 but then use the global `i'. */
3473 decl = check_for_out_of_scope_variable (decl);
3474 if (local_variable_p (decl))
3476 error ("%Hlocal variable %qD may not appear in this context",
3477 &id_expr_token->location, decl);
3478 return error_mark_node;
3483 decl = (finish_id_expression
3484 (id_expression, decl, parser->scope,
3486 parser->integral_constant_expression_p,
3487 parser->allow_non_integral_constant_expression_p,
3488 &parser->non_integral_constant_expression_p,
3489 template_p, done, address_p,
3492 id_expr_token->location));
3494 cp_parser_error (parser, error_msg);
3498 /* Anything else is an error. */
3500 /* ...unless we have an Objective-C++ message or string literal,
3502 if (c_dialect_objc ()
3503 && (token->type == CPP_OPEN_SQUARE
3504 || token->type == CPP_OBJC_STRING))
3505 return cp_parser_objc_expression (parser);
3507 cp_parser_error (parser, "expected primary-expression");
3508 return error_mark_node;
3512 /* Parse an id-expression.
3519 :: [opt] nested-name-specifier template [opt] unqualified-id
3521 :: operator-function-id
3524 Return a representation of the unqualified portion of the
3525 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3526 a `::' or nested-name-specifier.
3528 Often, if the id-expression was a qualified-id, the caller will
3529 want to make a SCOPE_REF to represent the qualified-id. This
3530 function does not do this in order to avoid wastefully creating
3531 SCOPE_REFs when they are not required.
3533 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3536 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3537 uninstantiated templates.
3539 If *TEMPLATE_P is non-NULL, it is set to true iff the
3540 `template' keyword is used to explicitly indicate that the entity
3541 named is a template.
3543 If DECLARATOR_P is true, the id-expression is appearing as part of
3544 a declarator, rather than as part of an expression. */
3547 cp_parser_id_expression (cp_parser *parser,
3548 bool template_keyword_p,
3549 bool check_dependency_p,
3554 bool global_scope_p;
3555 bool nested_name_specifier_p;
3557 /* Assume the `template' keyword was not used. */
3559 *template_p = template_keyword_p;
3561 /* Look for the optional `::' operator. */
3563 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3565 /* Look for the optional nested-name-specifier. */
3566 nested_name_specifier_p
3567 = (cp_parser_nested_name_specifier_opt (parser,
3568 /*typename_keyword_p=*/false,
3573 /* If there is a nested-name-specifier, then we are looking at
3574 the first qualified-id production. */
3575 if (nested_name_specifier_p)
3578 tree saved_object_scope;
3579 tree saved_qualifying_scope;
3580 tree unqualified_id;
3583 /* See if the next token is the `template' keyword. */
3585 template_p = &is_template;
3586 *template_p = cp_parser_optional_template_keyword (parser);
3587 /* Name lookup we do during the processing of the
3588 unqualified-id might obliterate SCOPE. */
3589 saved_scope = parser->scope;
3590 saved_object_scope = parser->object_scope;
3591 saved_qualifying_scope = parser->qualifying_scope;
3592 /* Process the final unqualified-id. */
3593 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3596 /*optional_p=*/false);
3597 /* Restore the SAVED_SCOPE for our caller. */
3598 parser->scope = saved_scope;
3599 parser->object_scope = saved_object_scope;
3600 parser->qualifying_scope = saved_qualifying_scope;
3602 return unqualified_id;
3604 /* Otherwise, if we are in global scope, then we are looking at one
3605 of the other qualified-id productions. */
3606 else if (global_scope_p)
3611 /* Peek at the next token. */
3612 token = cp_lexer_peek_token (parser->lexer);
3614 /* If it's an identifier, and the next token is not a "<", then
3615 we can avoid the template-id case. This is an optimization
3616 for this common case. */
3617 if (token->type == CPP_NAME
3618 && !cp_parser_nth_token_starts_template_argument_list_p
3620 return cp_parser_identifier (parser);
3622 cp_parser_parse_tentatively (parser);
3623 /* Try a template-id. */
3624 id = cp_parser_template_id (parser,
3625 /*template_keyword_p=*/false,
3626 /*check_dependency_p=*/true,
3628 /* If that worked, we're done. */
3629 if (cp_parser_parse_definitely (parser))
3632 /* Peek at the next token. (Changes in the token buffer may
3633 have invalidated the pointer obtained above.) */
3634 token = cp_lexer_peek_token (parser->lexer);
3636 switch (token->type)
3639 return cp_parser_identifier (parser);
3642 if (token->keyword == RID_OPERATOR)
3643 return cp_parser_operator_function_id (parser);
3647 cp_parser_error (parser, "expected id-expression");
3648 return error_mark_node;
3652 return cp_parser_unqualified_id (parser, template_keyword_p,
3653 /*check_dependency_p=*/true,
3658 /* Parse an unqualified-id.
3662 operator-function-id
3663 conversion-function-id
3667 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3668 keyword, in a construct like `A::template ...'.
3670 Returns a representation of unqualified-id. For the `identifier'
3671 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3672 production a BIT_NOT_EXPR is returned; the operand of the
3673 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3674 other productions, see the documentation accompanying the
3675 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3676 names are looked up in uninstantiated templates. If DECLARATOR_P
3677 is true, the unqualified-id is appearing as part of a declarator,
3678 rather than as part of an expression. */
3681 cp_parser_unqualified_id (cp_parser* parser,
3682 bool template_keyword_p,
3683 bool check_dependency_p,
3689 /* Peek at the next token. */
3690 token = cp_lexer_peek_token (parser->lexer);
3692 switch (token->type)
3698 /* We don't know yet whether or not this will be a
3700 cp_parser_parse_tentatively (parser);
3701 /* Try a template-id. */
3702 id = cp_parser_template_id (parser, template_keyword_p,
3705 /* If it worked, we're done. */
3706 if (cp_parser_parse_definitely (parser))
3708 /* Otherwise, it's an ordinary identifier. */
3709 return cp_parser_identifier (parser);
3712 case CPP_TEMPLATE_ID:
3713 return cp_parser_template_id (parser, template_keyword_p,
3720 tree qualifying_scope;
3725 /* Consume the `~' token. */
3726 cp_lexer_consume_token (parser->lexer);
3727 /* Parse the class-name. The standard, as written, seems to
3730 template <typename T> struct S { ~S (); };
3731 template <typename T> S<T>::~S() {}
3733 is invalid, since `~' must be followed by a class-name, but
3734 `S<T>' is dependent, and so not known to be a class.
3735 That's not right; we need to look in uninstantiated
3736 templates. A further complication arises from:
3738 template <typename T> void f(T t) {
3742 Here, it is not possible to look up `T' in the scope of `T'
3743 itself. We must look in both the current scope, and the
3744 scope of the containing complete expression.
3746 Yet another issue is:
3755 The standard does not seem to say that the `S' in `~S'
3756 should refer to the type `S' and not the data member
3759 /* DR 244 says that we look up the name after the "~" in the
3760 same scope as we looked up the qualifying name. That idea
3761 isn't fully worked out; it's more complicated than that. */
3762 scope = parser->scope;
3763 object_scope = parser->object_scope;
3764 qualifying_scope = parser->qualifying_scope;
3766 /* Check for invalid scopes. */
3767 if (scope == error_mark_node)
3769 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3770 cp_lexer_consume_token (parser->lexer);
3771 return error_mark_node;
3773 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3775 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3776 error ("%Hscope %qT before %<~%> is not a class-name",
3777 &token->location, scope);
3778 cp_parser_simulate_error (parser);
3779 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3780 cp_lexer_consume_token (parser->lexer);
3781 return error_mark_node;
3783 gcc_assert (!scope || TYPE_P (scope));
3785 /* If the name is of the form "X::~X" it's OK. */
3786 token = cp_lexer_peek_token (parser->lexer);
3788 && token->type == CPP_NAME
3789 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3791 && constructor_name_p (token->u.value, scope))
3793 cp_lexer_consume_token (parser->lexer);
3794 return build_nt (BIT_NOT_EXPR, scope);
3797 /* If there was an explicit qualification (S::~T), first look
3798 in the scope given by the qualification (i.e., S). */
3800 type_decl = NULL_TREE;
3803 cp_parser_parse_tentatively (parser);
3804 type_decl = cp_parser_class_name (parser,
3805 /*typename_keyword_p=*/false,
3806 /*template_keyword_p=*/false,
3808 /*check_dependency=*/false,
3809 /*class_head_p=*/false,
3811 if (cp_parser_parse_definitely (parser))
3814 /* In "N::S::~S", look in "N" as well. */
3815 if (!done && scope && qualifying_scope)
3817 cp_parser_parse_tentatively (parser);
3818 parser->scope = qualifying_scope;
3819 parser->object_scope = NULL_TREE;
3820 parser->qualifying_scope = NULL_TREE;
3822 = cp_parser_class_name (parser,
3823 /*typename_keyword_p=*/false,
3824 /*template_keyword_p=*/false,
3826 /*check_dependency=*/false,
3827 /*class_head_p=*/false,
3829 if (cp_parser_parse_definitely (parser))
3832 /* In "p->S::~T", look in the scope given by "*p" as well. */
3833 else if (!done && object_scope)
3835 cp_parser_parse_tentatively (parser);
3836 parser->scope = object_scope;
3837 parser->object_scope = NULL_TREE;
3838 parser->qualifying_scope = NULL_TREE;
3840 = cp_parser_class_name (parser,
3841 /*typename_keyword_p=*/false,
3842 /*template_keyword_p=*/false,
3844 /*check_dependency=*/false,
3845 /*class_head_p=*/false,
3847 if (cp_parser_parse_definitely (parser))
3850 /* Look in the surrounding context. */
3853 parser->scope = NULL_TREE;
3854 parser->object_scope = NULL_TREE;
3855 parser->qualifying_scope = NULL_TREE;
3857 = cp_parser_class_name (parser,
3858 /*typename_keyword_p=*/false,
3859 /*template_keyword_p=*/false,
3861 /*check_dependency=*/false,
3862 /*class_head_p=*/false,
3865 /* If an error occurred, assume that the name of the
3866 destructor is the same as the name of the qualifying
3867 class. That allows us to keep parsing after running
3868 into ill-formed destructor names. */
3869 if (type_decl == error_mark_node && scope)
3870 return build_nt (BIT_NOT_EXPR, scope);
3871 else if (type_decl == error_mark_node)
3872 return error_mark_node;
3874 /* Check that destructor name and scope match. */
3875 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3877 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3878 error ("%Hdeclaration of %<~%T%> as member of %qT",
3879 &token->location, type_decl, scope);
3880 cp_parser_simulate_error (parser);
3881 return error_mark_node;
3886 A typedef-name that names a class shall not be used as the
3887 identifier in the declarator for a destructor declaration. */
3889 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3890 && !DECL_SELF_REFERENCE_P (type_decl)
3891 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3892 error ("%Htypedef-name %qD used as destructor declarator",
3893 &token->location, type_decl);
3895 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3899 if (token->keyword == RID_OPERATOR)
3903 /* This could be a template-id, so we try that first. */
3904 cp_parser_parse_tentatively (parser);
3905 /* Try a template-id. */
3906 id = cp_parser_template_id (parser, template_keyword_p,
3907 /*check_dependency_p=*/true,
3909 /* If that worked, we're done. */
3910 if (cp_parser_parse_definitely (parser))
3912 /* We still don't know whether we're looking at an
3913 operator-function-id or a conversion-function-id. */
3914 cp_parser_parse_tentatively (parser);
3915 /* Try an operator-function-id. */
3916 id = cp_parser_operator_function_id (parser);
3917 /* If that didn't work, try a conversion-function-id. */
3918 if (!cp_parser_parse_definitely (parser))
3919 id = cp_parser_conversion_function_id (parser);
3928 cp_parser_error (parser, "expected unqualified-id");
3929 return error_mark_node;
3933 /* Parse an (optional) nested-name-specifier.
3935 nested-name-specifier: [C++98]
3936 class-or-namespace-name :: nested-name-specifier [opt]
3937 class-or-namespace-name :: template nested-name-specifier [opt]
3939 nested-name-specifier: [C++0x]
3942 nested-name-specifier identifier ::
3943 nested-name-specifier template [opt] simple-template-id ::
3945 PARSER->SCOPE should be set appropriately before this function is
3946 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3947 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3950 Sets PARSER->SCOPE to the class (TYPE) or namespace
3951 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3952 it unchanged if there is no nested-name-specifier. Returns the new
3953 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3955 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3956 part of a declaration and/or decl-specifier. */
3959 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3960 bool typename_keyword_p,
3961 bool check_dependency_p,
3963 bool is_declaration)
3965 bool success = false;
3966 cp_token_position start = 0;
3969 /* Remember where the nested-name-specifier starts. */
3970 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3972 start = cp_lexer_token_position (parser->lexer, false);
3973 push_deferring_access_checks (dk_deferred);
3980 tree saved_qualifying_scope;
3981 bool template_keyword_p;
3983 /* Spot cases that cannot be the beginning of a
3984 nested-name-specifier. */
3985 token = cp_lexer_peek_token (parser->lexer);
3987 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3988 the already parsed nested-name-specifier. */
3989 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3991 /* Grab the nested-name-specifier and continue the loop. */
3992 cp_parser_pre_parsed_nested_name_specifier (parser);
3993 /* If we originally encountered this nested-name-specifier
3994 with IS_DECLARATION set to false, we will not have
3995 resolved TYPENAME_TYPEs, so we must do so here. */
3997 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3999 new_scope = resolve_typename_type (parser->scope,
4000 /*only_current_p=*/false);
4001 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4002 parser->scope = new_scope;
4008 /* Spot cases that cannot be the beginning of a
4009 nested-name-specifier. On the second and subsequent times
4010 through the loop, we look for the `template' keyword. */
4011 if (success && token->keyword == RID_TEMPLATE)
4013 /* A template-id can start a nested-name-specifier. */
4014 else if (token->type == CPP_TEMPLATE_ID)
4018 /* If the next token is not an identifier, then it is
4019 definitely not a type-name or namespace-name. */
4020 if (token->type != CPP_NAME)
4022 /* If the following token is neither a `<' (to begin a
4023 template-id), nor a `::', then we are not looking at a
4024 nested-name-specifier. */
4025 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4026 if (token->type != CPP_SCOPE
4027 && !cp_parser_nth_token_starts_template_argument_list_p
4032 /* The nested-name-specifier is optional, so we parse
4034 cp_parser_parse_tentatively (parser);
4036 /* Look for the optional `template' keyword, if this isn't the
4037 first time through the loop. */
4039 template_keyword_p = cp_parser_optional_template_keyword (parser);
4041 template_keyword_p = false;
4043 /* Save the old scope since the name lookup we are about to do
4044 might destroy it. */
4045 old_scope = parser->scope;
4046 saved_qualifying_scope = parser->qualifying_scope;
4047 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4048 look up names in "X<T>::I" in order to determine that "Y" is
4049 a template. So, if we have a typename at this point, we make
4050 an effort to look through it. */
4052 && !typename_keyword_p
4054 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4055 parser->scope = resolve_typename_type (parser->scope,
4056 /*only_current_p=*/false);
4057 /* Parse the qualifying entity. */
4059 = cp_parser_qualifying_entity (parser,
4065 /* Look for the `::' token. */
4066 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4068 /* If we found what we wanted, we keep going; otherwise, we're
4070 if (!cp_parser_parse_definitely (parser))
4072 bool error_p = false;
4074 /* Restore the OLD_SCOPE since it was valid before the
4075 failed attempt at finding the last
4076 class-or-namespace-name. */
4077 parser->scope = old_scope;
4078 parser->qualifying_scope = saved_qualifying_scope;
4079 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4081 /* If the next token is an identifier, and the one after
4082 that is a `::', then any valid interpretation would have
4083 found a class-or-namespace-name. */
4084 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4085 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4087 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4090 token = cp_lexer_consume_token (parser->lexer);
4093 if (!token->ambiguous_p)
4096 tree ambiguous_decls;
4098 decl = cp_parser_lookup_name (parser, token->u.value,
4100 /*is_template=*/false,
4101 /*is_namespace=*/false,
4102 /*check_dependency=*/true,
4105 if (TREE_CODE (decl) == TEMPLATE_DECL)
4106 error ("%H%qD used without template parameters",
4107 &token->location, decl);
4108 else if (ambiguous_decls)
4110 error ("%Hreference to %qD is ambiguous",
4111 &token->location, token->u.value);
4112 print_candidates (ambiguous_decls);
4113 decl = error_mark_node;
4117 const char* msg = "is not a class or namespace";
4118 if (cxx_dialect != cxx98)
4119 msg = "is not a class, namespace, or enumeration";
4120 cp_parser_name_lookup_error
4121 (parser, token->u.value, decl, msg,
4125 parser->scope = error_mark_node;
4127 /* Treat this as a successful nested-name-specifier
4132 If the name found is not a class-name (clause
4133 _class_) or namespace-name (_namespace.def_), the
4134 program is ill-formed. */
4137 cp_lexer_consume_token (parser->lexer);
4141 /* We've found one valid nested-name-specifier. */
4143 /* Name lookup always gives us a DECL. */
4144 if (TREE_CODE (new_scope) == TYPE_DECL)
4145 new_scope = TREE_TYPE (new_scope);
4146 /* Uses of "template" must be followed by actual templates. */
4147 if (template_keyword_p
4148 && !(CLASS_TYPE_P (new_scope)
4149 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4150 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4151 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4152 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4153 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4154 == TEMPLATE_ID_EXPR)))
4155 permerror (input_location, TYPE_P (new_scope)
4156 ? "%qT is not a template"
4157 : "%qD is not a template",
4159 /* If it is a class scope, try to complete it; we are about to
4160 be looking up names inside the class. */
4161 if (TYPE_P (new_scope)
4162 /* Since checking types for dependency can be expensive,
4163 avoid doing it if the type is already complete. */
4164 && !COMPLETE_TYPE_P (new_scope)
4165 /* Do not try to complete dependent types. */
4166 && !dependent_type_p (new_scope))
4168 new_scope = complete_type (new_scope);
4169 /* If it is a typedef to current class, use the current
4170 class instead, as the typedef won't have any names inside
4172 if (!COMPLETE_TYPE_P (new_scope)
4173 && currently_open_class (new_scope))
4174 new_scope = TYPE_MAIN_VARIANT (new_scope);
4176 /* Make sure we look in the right scope the next time through
4178 parser->scope = new_scope;
4181 /* If parsing tentatively, replace the sequence of tokens that makes
4182 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4183 token. That way, should we re-parse the token stream, we will
4184 not have to repeat the effort required to do the parse, nor will
4185 we issue duplicate error messages. */
4186 if (success && start)
4190 token = cp_lexer_token_at (parser->lexer, start);
4191 /* Reset the contents of the START token. */
4192 token->type = CPP_NESTED_NAME_SPECIFIER;
4193 /* Retrieve any deferred checks. Do not pop this access checks yet
4194 so the memory will not be reclaimed during token replacing below. */
4195 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4196 token->u.tree_check_value->value = parser->scope;
4197 token->u.tree_check_value->checks = get_deferred_access_checks ();
4198 token->u.tree_check_value->qualifying_scope =
4199 parser->qualifying_scope;
4200 token->keyword = RID_MAX;
4202 /* Purge all subsequent tokens. */
4203 cp_lexer_purge_tokens_after (parser->lexer, start);
4207 pop_to_parent_deferring_access_checks ();
4209 return success ? parser->scope : NULL_TREE;
4212 /* Parse a nested-name-specifier. See
4213 cp_parser_nested_name_specifier_opt for details. This function
4214 behaves identically, except that it will an issue an error if no
4215 nested-name-specifier is present. */
4218 cp_parser_nested_name_specifier (cp_parser *parser,
4219 bool typename_keyword_p,
4220 bool check_dependency_p,
4222 bool is_declaration)
4226 /* Look for the nested-name-specifier. */
4227 scope = cp_parser_nested_name_specifier_opt (parser,
4232 /* If it was not present, issue an error message. */
4235 cp_parser_error (parser, "expected nested-name-specifier");
4236 parser->scope = NULL_TREE;
4242 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4243 this is either a class-name or a namespace-name (which corresponds
4244 to the class-or-namespace-name production in the grammar). For
4245 C++0x, it can also be a type-name that refers to an enumeration
4248 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4249 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4250 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4251 TYPE_P is TRUE iff the next name should be taken as a class-name,
4252 even the same name is declared to be another entity in the same
4255 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4256 specified by the class-or-namespace-name. If neither is found the
4257 ERROR_MARK_NODE is returned. */
4260 cp_parser_qualifying_entity (cp_parser *parser,
4261 bool typename_keyword_p,
4262 bool template_keyword_p,
4263 bool check_dependency_p,
4265 bool is_declaration)
4268 tree saved_qualifying_scope;
4269 tree saved_object_scope;
4272 bool successful_parse_p;
4274 /* Before we try to parse the class-name, we must save away the
4275 current PARSER->SCOPE since cp_parser_class_name will destroy
4277 saved_scope = parser->scope;
4278 saved_qualifying_scope = parser->qualifying_scope;
4279 saved_object_scope = parser->object_scope;
4280 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4281 there is no need to look for a namespace-name. */
4282 only_class_p = template_keyword_p
4283 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4285 cp_parser_parse_tentatively (parser);
4286 scope = cp_parser_class_name (parser,
4289 type_p ? class_type : none_type,
4291 /*class_head_p=*/false,
4293 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4294 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4296 && cxx_dialect != cxx98
4297 && !successful_parse_p)
4299 /* Restore the saved scope. */
4300 parser->scope = saved_scope;
4301 parser->qualifying_scope = saved_qualifying_scope;
4302 parser->object_scope = saved_object_scope;
4304 /* Parse tentatively. */
4305 cp_parser_parse_tentatively (parser);
4307 /* Parse a typedef-name or enum-name. */
4308 scope = cp_parser_nonclass_name (parser);
4309 successful_parse_p = cp_parser_parse_definitely (parser);
4311 /* If that didn't work, try for a namespace-name. */
4312 if (!only_class_p && !successful_parse_p)
4314 /* Restore the saved scope. */
4315 parser->scope = saved_scope;
4316 parser->qualifying_scope = saved_qualifying_scope;
4317 parser->object_scope = saved_object_scope;
4318 /* If we are not looking at an identifier followed by the scope
4319 resolution operator, then this is not part of a
4320 nested-name-specifier. (Note that this function is only used
4321 to parse the components of a nested-name-specifier.) */
4322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4323 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4324 return error_mark_node;
4325 scope = cp_parser_namespace_name (parser);
4331 /* Parse a postfix-expression.
4335 postfix-expression [ expression ]
4336 postfix-expression ( expression-list [opt] )
4337 simple-type-specifier ( expression-list [opt] )
4338 typename :: [opt] nested-name-specifier identifier
4339 ( expression-list [opt] )
4340 typename :: [opt] nested-name-specifier template [opt] template-id
4341 ( expression-list [opt] )
4342 postfix-expression . template [opt] id-expression
4343 postfix-expression -> template [opt] id-expression
4344 postfix-expression . pseudo-destructor-name
4345 postfix-expression -> pseudo-destructor-name
4346 postfix-expression ++
4347 postfix-expression --
4348 dynamic_cast < type-id > ( expression )
4349 static_cast < type-id > ( expression )
4350 reinterpret_cast < type-id > ( expression )
4351 const_cast < type-id > ( expression )
4352 typeid ( expression )
4358 ( type-id ) { initializer-list , [opt] }
4360 This extension is a GNU version of the C99 compound-literal
4361 construct. (The C99 grammar uses `type-name' instead of `type-id',
4362 but they are essentially the same concept.)
4364 If ADDRESS_P is true, the postfix expression is the operand of the
4365 `&' operator. CAST_P is true if this expression is the target of a
4368 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4369 class member access expressions [expr.ref].
4371 Returns a representation of the expression. */
4374 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4375 bool member_access_only_p)
4379 cp_id_kind idk = CP_ID_KIND_NONE;
4380 tree postfix_expression = NULL_TREE;
4381 bool is_member_access = false;
4383 /* Peek at the next token. */
4384 token = cp_lexer_peek_token (parser->lexer);
4385 /* Some of the productions are determined by keywords. */
4386 keyword = token->keyword;
4396 const char *saved_message;
4398 /* All of these can be handled in the same way from the point
4399 of view of parsing. Begin by consuming the token
4400 identifying the cast. */
4401 cp_lexer_consume_token (parser->lexer);
4403 /* New types cannot be defined in the cast. */
4404 saved_message = parser->type_definition_forbidden_message;
4405 parser->type_definition_forbidden_message
4406 = "types may not be defined in casts";
4408 /* Look for the opening `<'. */
4409 cp_parser_require (parser, CPP_LESS, "%<<%>");
4410 /* Parse the type to which we are casting. */
4411 type = cp_parser_type_id (parser);
4412 /* Look for the closing `>'. */
4413 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4414 /* Restore the old message. */
4415 parser->type_definition_forbidden_message = saved_message;
4417 /* And the expression which is being cast. */
4418 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4419 expression = cp_parser_expression (parser, /*cast_p=*/true);
4420 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4422 /* Only type conversions to integral or enumeration types
4423 can be used in constant-expressions. */
4424 if (!cast_valid_in_integral_constant_expression_p (type)
4425 && (cp_parser_non_integral_constant_expression
4427 "a cast to a type other than an integral or "
4428 "enumeration type")))
4429 return error_mark_node;
4435 = build_dynamic_cast (type, expression, tf_warning_or_error);
4439 = build_static_cast (type, expression, tf_warning_or_error);
4443 = build_reinterpret_cast (type, expression,
4444 tf_warning_or_error);
4448 = build_const_cast (type, expression, tf_warning_or_error);
4459 const char *saved_message;
4460 bool saved_in_type_id_in_expr_p;
4462 /* Consume the `typeid' token. */
4463 cp_lexer_consume_token (parser->lexer);
4464 /* Look for the `(' token. */
4465 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4466 /* Types cannot be defined in a `typeid' expression. */
4467 saved_message = parser->type_definition_forbidden_message;
4468 parser->type_definition_forbidden_message
4469 = "types may not be defined in a %<typeid%> expression";
4470 /* We can't be sure yet whether we're looking at a type-id or an
4472 cp_parser_parse_tentatively (parser);
4473 /* Try a type-id first. */
4474 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4475 parser->in_type_id_in_expr_p = true;
4476 type = cp_parser_type_id (parser);
4477 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4478 /* Look for the `)' token. Otherwise, we can't be sure that
4479 we're not looking at an expression: consider `typeid (int
4480 (3))', for example. */
4481 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4482 /* If all went well, simply lookup the type-id. */
4483 if (cp_parser_parse_definitely (parser))
4484 postfix_expression = get_typeid (type);
4485 /* Otherwise, fall back to the expression variant. */
4490 /* Look for an expression. */
4491 expression = cp_parser_expression (parser, /*cast_p=*/false);
4492 /* Compute its typeid. */
4493 postfix_expression = build_typeid (expression);
4494 /* Look for the `)' token. */
4495 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4497 /* Restore the saved message. */
4498 parser->type_definition_forbidden_message = saved_message;
4499 /* `typeid' may not appear in an integral constant expression. */
4500 if (cp_parser_non_integral_constant_expression(parser,
4501 "%<typeid%> operator"))
4502 return error_mark_node;
4509 /* The syntax permitted here is the same permitted for an
4510 elaborated-type-specifier. */
4511 type = cp_parser_elaborated_type_specifier (parser,
4512 /*is_friend=*/false,
4513 /*is_declaration=*/false);
4514 postfix_expression = cp_parser_functional_cast (parser, type);
4522 /* If the next thing is a simple-type-specifier, we may be
4523 looking at a functional cast. We could also be looking at
4524 an id-expression. So, we try the functional cast, and if
4525 that doesn't work we fall back to the primary-expression. */
4526 cp_parser_parse_tentatively (parser);
4527 /* Look for the simple-type-specifier. */
4528 type = cp_parser_simple_type_specifier (parser,
4529 /*decl_specs=*/NULL,
4530 CP_PARSER_FLAGS_NONE);
4531 /* Parse the cast itself. */
4532 if (!cp_parser_error_occurred (parser))
4534 = cp_parser_functional_cast (parser, type);
4535 /* If that worked, we're done. */
4536 if (cp_parser_parse_definitely (parser))
4539 /* If the functional-cast didn't work out, try a
4540 compound-literal. */
4541 if (cp_parser_allow_gnu_extensions_p (parser)
4542 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4544 VEC(constructor_elt,gc) *initializer_list = NULL;
4545 bool saved_in_type_id_in_expr_p;
4547 cp_parser_parse_tentatively (parser);
4548 /* Consume the `('. */
4549 cp_lexer_consume_token (parser->lexer);
4550 /* Parse the type. */
4551 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4552 parser->in_type_id_in_expr_p = true;
4553 type = cp_parser_type_id (parser);
4554 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4555 /* Look for the `)'. */
4556 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4557 /* Look for the `{'. */
4558 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4559 /* If things aren't going well, there's no need to
4561 if (!cp_parser_error_occurred (parser))
4563 bool non_constant_p;
4564 /* Parse the initializer-list. */
4566 = cp_parser_initializer_list (parser, &non_constant_p);
4567 /* Allow a trailing `,'. */
4568 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4569 cp_lexer_consume_token (parser->lexer);
4570 /* Look for the final `}'. */
4571 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4573 /* If that worked, we're definitely looking at a
4574 compound-literal expression. */
4575 if (cp_parser_parse_definitely (parser))
4577 /* Warn the user that a compound literal is not
4578 allowed in standard C++. */
4579 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4580 /* For simplicity, we disallow compound literals in
4581 constant-expressions. We could
4582 allow compound literals of integer type, whose
4583 initializer was a constant, in constant
4584 expressions. Permitting that usage, as a further
4585 extension, would not change the meaning of any
4586 currently accepted programs. (Of course, as
4587 compound literals are not part of ISO C++, the
4588 standard has nothing to say.) */
4589 if (cp_parser_non_integral_constant_expression
4590 (parser, "non-constant compound literals"))
4592 postfix_expression = error_mark_node;
4595 /* Form the representation of the compound-literal. */
4597 = (finish_compound_literal
4598 (type, build_constructor (init_list_type_node,
4599 initializer_list)));
4604 /* It must be a primary-expression. */
4606 = cp_parser_primary_expression (parser, address_p, cast_p,
4607 /*template_arg_p=*/false,
4613 /* Keep looping until the postfix-expression is complete. */
4616 if (idk == CP_ID_KIND_UNQUALIFIED
4617 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4618 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4619 /* It is not a Koenig lookup function call. */
4621 = unqualified_name_lookup_error (postfix_expression);
4623 /* Peek at the next token. */
4624 token = cp_lexer_peek_token (parser->lexer);
4626 switch (token->type)
4628 case CPP_OPEN_SQUARE:
4630 = cp_parser_postfix_open_square_expression (parser,
4633 idk = CP_ID_KIND_NONE;
4634 is_member_access = false;
4637 case CPP_OPEN_PAREN:
4638 /* postfix-expression ( expression-list [opt] ) */
4641 bool is_builtin_constant_p;
4642 bool saved_integral_constant_expression_p = false;
4643 bool saved_non_integral_constant_expression_p = false;
4646 is_member_access = false;
4648 is_builtin_constant_p
4649 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4650 if (is_builtin_constant_p)
4652 /* The whole point of __builtin_constant_p is to allow
4653 non-constant expressions to appear as arguments. */
4654 saved_integral_constant_expression_p
4655 = parser->integral_constant_expression_p;
4656 saved_non_integral_constant_expression_p
4657 = parser->non_integral_constant_expression_p;
4658 parser->integral_constant_expression_p = false;
4660 args = (cp_parser_parenthesized_expression_list
4661 (parser, /*is_attribute_list=*/false,
4662 /*cast_p=*/false, /*allow_expansion_p=*/true,
4663 /*non_constant_p=*/NULL));
4664 if (is_builtin_constant_p)
4666 parser->integral_constant_expression_p
4667 = saved_integral_constant_expression_p;
4668 parser->non_integral_constant_expression_p
4669 = saved_non_integral_constant_expression_p;
4672 if (args == error_mark_node)
4674 postfix_expression = error_mark_node;
4678 /* Function calls are not permitted in
4679 constant-expressions. */
4680 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4681 && cp_parser_non_integral_constant_expression (parser,
4684 postfix_expression = error_mark_node;
4689 if (idk == CP_ID_KIND_UNQUALIFIED)
4691 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4697 = perform_koenig_lookup (postfix_expression, args);
4701 = unqualified_fn_lookup_error (postfix_expression);
4703 /* We do not perform argument-dependent lookup if
4704 normal lookup finds a non-function, in accordance
4705 with the expected resolution of DR 218. */
4706 else if (args && is_overloaded_fn (postfix_expression))
4708 tree fn = get_first_fn (postfix_expression);
4710 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4711 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4713 /* Only do argument dependent lookup if regular
4714 lookup does not find a set of member functions.
4715 [basic.lookup.koenig]/2a */
4716 if (!DECL_FUNCTION_MEMBER_P (fn))
4720 = perform_koenig_lookup (postfix_expression, args);
4725 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4727 tree instance = TREE_OPERAND (postfix_expression, 0);
4728 tree fn = TREE_OPERAND (postfix_expression, 1);
4730 if (processing_template_decl
4731 && (type_dependent_expression_p (instance)
4732 || (!BASELINK_P (fn)
4733 && TREE_CODE (fn) != FIELD_DECL)
4734 || type_dependent_expression_p (fn)
4735 || any_type_dependent_arguments_p (args)))
4738 = build_nt_call_list (postfix_expression, args);
4742 if (BASELINK_P (fn))
4744 = (build_new_method_call
4745 (instance, fn, args, NULL_TREE,
4746 (idk == CP_ID_KIND_QUALIFIED
4747 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4749 tf_warning_or_error));
4752 = finish_call_expr (postfix_expression, args,
4753 /*disallow_virtual=*/false,
4755 tf_warning_or_error);
4757 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4758 || TREE_CODE (postfix_expression) == MEMBER_REF
4759 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4760 postfix_expression = (build_offset_ref_call_from_tree
4761 (postfix_expression, args));
4762 else if (idk == CP_ID_KIND_QUALIFIED)
4763 /* A call to a static class member, or a namespace-scope
4766 = finish_call_expr (postfix_expression, args,
4767 /*disallow_virtual=*/true,
4769 tf_warning_or_error);
4771 /* All other function calls. */
4773 = finish_call_expr (postfix_expression, args,
4774 /*disallow_virtual=*/false,
4776 tf_warning_or_error);
4778 if (warn_disallowed_functions)
4779 warn_if_disallowed_function_p (postfix_expression);
4781 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4782 idk = CP_ID_KIND_NONE;
4788 /* postfix-expression . template [opt] id-expression
4789 postfix-expression . pseudo-destructor-name
4790 postfix-expression -> template [opt] id-expression
4791 postfix-expression -> pseudo-destructor-name */
4793 /* Consume the `.' or `->' operator. */
4794 cp_lexer_consume_token (parser->lexer);
4797 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4802 is_member_access = true;
4806 /* postfix-expression ++ */
4807 /* Consume the `++' token. */
4808 cp_lexer_consume_token (parser->lexer);
4809 /* Generate a representation for the complete expression. */
4811 = finish_increment_expr (postfix_expression,
4812 POSTINCREMENT_EXPR);
4813 /* Increments may not appear in constant-expressions. */
4814 if (cp_parser_non_integral_constant_expression (parser,
4816 postfix_expression = error_mark_node;
4817 idk = CP_ID_KIND_NONE;
4818 is_member_access = false;
4821 case CPP_MINUS_MINUS:
4822 /* postfix-expression -- */
4823 /* Consume the `--' token. */
4824 cp_lexer_consume_token (parser->lexer);
4825 /* Generate a representation for the complete expression. */
4827 = finish_increment_expr (postfix_expression,
4828 POSTDECREMENT_EXPR);
4829 /* Decrements may not appear in constant-expressions. */
4830 if (cp_parser_non_integral_constant_expression (parser,
4832 postfix_expression = error_mark_node;
4833 idk = CP_ID_KIND_NONE;
4834 is_member_access = false;
4838 if (member_access_only_p)
4839 return is_member_access? postfix_expression : error_mark_node;
4841 return postfix_expression;
4845 /* We should never get here. */
4847 return error_mark_node;
4850 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4851 by cp_parser_builtin_offsetof. We're looking for
4853 postfix-expression [ expression ]
4855 FOR_OFFSETOF is set if we're being called in that context, which
4856 changes how we deal with integer constant expressions. */
4859 cp_parser_postfix_open_square_expression (cp_parser *parser,
4860 tree postfix_expression,
4865 /* Consume the `[' token. */
4866 cp_lexer_consume_token (parser->lexer);
4868 /* Parse the index expression. */
4869 /* ??? For offsetof, there is a question of what to allow here. If
4870 offsetof is not being used in an integral constant expression context,
4871 then we *could* get the right answer by computing the value at runtime.
4872 If we are in an integral constant expression context, then we might
4873 could accept any constant expression; hard to say without analysis.
4874 Rather than open the barn door too wide right away, allow only integer
4875 constant expressions here. */
4877 index = cp_parser_constant_expression (parser, false, NULL);
4879 index = cp_parser_expression (parser, /*cast_p=*/false);
4881 /* Look for the closing `]'. */
4882 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4884 /* Build the ARRAY_REF. */
4885 postfix_expression = grok_array_decl (postfix_expression, index);
4887 /* When not doing offsetof, array references are not permitted in
4888 constant-expressions. */
4890 && (cp_parser_non_integral_constant_expression
4891 (parser, "an array reference")))
4892 postfix_expression = error_mark_node;
4894 return postfix_expression;
4897 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4898 by cp_parser_builtin_offsetof. We're looking for
4900 postfix-expression . template [opt] id-expression
4901 postfix-expression . pseudo-destructor-name
4902 postfix-expression -> template [opt] id-expression
4903 postfix-expression -> pseudo-destructor-name
4905 FOR_OFFSETOF is set if we're being called in that context. That sorta
4906 limits what of the above we'll actually accept, but nevermind.
4907 TOKEN_TYPE is the "." or "->" token, which will already have been
4908 removed from the stream. */
4911 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4912 enum cpp_ttype token_type,
4913 tree postfix_expression,
4914 bool for_offsetof, cp_id_kind *idk,
4915 location_t location)
4919 bool pseudo_destructor_p;
4920 tree scope = NULL_TREE;
4922 /* If this is a `->' operator, dereference the pointer. */
4923 if (token_type == CPP_DEREF)
4924 postfix_expression = build_x_arrow (postfix_expression);
4925 /* Check to see whether or not the expression is type-dependent. */
4926 dependent_p = type_dependent_expression_p (postfix_expression);
4927 /* The identifier following the `->' or `.' is not qualified. */
4928 parser->scope = NULL_TREE;
4929 parser->qualifying_scope = NULL_TREE;
4930 parser->object_scope = NULL_TREE;
4931 *idk = CP_ID_KIND_NONE;
4932 /* Enter the scope corresponding to the type of the object
4933 given by the POSTFIX_EXPRESSION. */
4934 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4936 scope = TREE_TYPE (postfix_expression);
4937 /* According to the standard, no expression should ever have
4938 reference type. Unfortunately, we do not currently match
4939 the standard in this respect in that our internal representation
4940 of an expression may have reference type even when the standard
4941 says it does not. Therefore, we have to manually obtain the
4942 underlying type here. */
4943 scope = non_reference (scope);
4944 /* The type of the POSTFIX_EXPRESSION must be complete. */
4945 if (scope == unknown_type_node)
4947 error ("%H%qE does not have class type", &location, postfix_expression);
4951 scope = complete_type_or_else (scope, NULL_TREE);
4952 /* Let the name lookup machinery know that we are processing a
4953 class member access expression. */
4954 parser->context->object_type = scope;
4955 /* If something went wrong, we want to be able to discern that case,
4956 as opposed to the case where there was no SCOPE due to the type
4957 of expression being dependent. */
4959 scope = error_mark_node;
4960 /* If the SCOPE was erroneous, make the various semantic analysis
4961 functions exit quickly -- and without issuing additional error
4963 if (scope == error_mark_node)
4964 postfix_expression = error_mark_node;
4967 /* Assume this expression is not a pseudo-destructor access. */
4968 pseudo_destructor_p = false;
4970 /* If the SCOPE is a scalar type, then, if this is a valid program,
4971 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4972 is type dependent, it can be pseudo-destructor-name or something else.
4973 Try to parse it as pseudo-destructor-name first. */
4974 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4979 cp_parser_parse_tentatively (parser);
4980 /* Parse the pseudo-destructor-name. */
4982 cp_parser_pseudo_destructor_name (parser, &s, &type);
4984 && (cp_parser_error_occurred (parser)
4985 || TREE_CODE (type) != TYPE_DECL
4986 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4987 cp_parser_abort_tentative_parse (parser);
4988 else if (cp_parser_parse_definitely (parser))
4990 pseudo_destructor_p = true;
4992 = finish_pseudo_destructor_expr (postfix_expression,
4993 s, TREE_TYPE (type));
4997 if (!pseudo_destructor_p)
4999 /* If the SCOPE is not a scalar type, we are looking at an
5000 ordinary class member access expression, rather than a
5001 pseudo-destructor-name. */
5003 cp_token *token = cp_lexer_peek_token (parser->lexer);
5004 /* Parse the id-expression. */
5005 name = (cp_parser_id_expression
5007 cp_parser_optional_template_keyword (parser),
5008 /*check_dependency_p=*/true,
5010 /*declarator_p=*/false,
5011 /*optional_p=*/false));
5012 /* In general, build a SCOPE_REF if the member name is qualified.
5013 However, if the name was not dependent and has already been
5014 resolved; there is no need to build the SCOPE_REF. For example;
5016 struct X { void f(); };
5017 template <typename T> void f(T* t) { t->X::f(); }
5019 Even though "t" is dependent, "X::f" is not and has been resolved
5020 to a BASELINK; there is no need to include scope information. */
5022 /* But we do need to remember that there was an explicit scope for
5023 virtual function calls. */
5025 *idk = CP_ID_KIND_QUALIFIED;
5027 /* If the name is a template-id that names a type, we will get a
5028 TYPE_DECL here. That is invalid code. */
5029 if (TREE_CODE (name) == TYPE_DECL)
5031 error ("%Hinvalid use of %qD", &token->location, name);
5032 postfix_expression = error_mark_node;
5036 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5038 name = build_qualified_name (/*type=*/NULL_TREE,
5042 parser->scope = NULL_TREE;
5043 parser->qualifying_scope = NULL_TREE;
5044 parser->object_scope = NULL_TREE;
5046 if (scope && name && BASELINK_P (name))
5047 adjust_result_of_qualified_name_lookup
5048 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5050 = finish_class_member_access_expr (postfix_expression, name,
5052 tf_warning_or_error);
5056 /* We no longer need to look up names in the scope of the object on
5057 the left-hand side of the `.' or `->' operator. */
5058 parser->context->object_type = NULL_TREE;
5060 /* Outside of offsetof, these operators may not appear in
5061 constant-expressions. */
5063 && (cp_parser_non_integral_constant_expression
5064 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5065 postfix_expression = error_mark_node;
5067 return postfix_expression;
5070 /* Parse a parenthesized expression-list.
5073 assignment-expression
5074 expression-list, assignment-expression
5079 identifier, expression-list
5081 CAST_P is true if this expression is the target of a cast.
5083 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5086 Returns a TREE_LIST. The TREE_VALUE of each node is a
5087 representation of an assignment-expression. Note that a TREE_LIST
5088 is returned even if there is only a single expression in the list.
5089 error_mark_node is returned if the ( and or ) are
5090 missing. NULL_TREE is returned on no expressions. The parentheses
5091 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5092 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5093 indicates whether or not all of the expressions in the list were
5097 cp_parser_parenthesized_expression_list (cp_parser* parser,
5098 bool is_attribute_list,
5100 bool allow_expansion_p,
5101 bool *non_constant_p)
5103 tree expression_list = NULL_TREE;
5104 bool fold_expr_p = is_attribute_list;
5105 tree identifier = NULL_TREE;
5106 bool saved_greater_than_is_operator_p;
5108 /* Assume all the expressions will be constant. */
5110 *non_constant_p = false;
5112 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5113 return error_mark_node;
5115 /* Within a parenthesized expression, a `>' token is always
5116 the greater-than operator. */
5117 saved_greater_than_is_operator_p
5118 = parser->greater_than_is_operator_p;
5119 parser->greater_than_is_operator_p = true;
5121 /* Consume expressions until there are no more. */
5122 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5127 /* At the beginning of attribute lists, check to see if the
5128 next token is an identifier. */
5129 if (is_attribute_list
5130 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5134 /* Consume the identifier. */
5135 token = cp_lexer_consume_token (parser->lexer);
5136 /* Save the identifier. */
5137 identifier = token->u.value;
5141 bool expr_non_constant_p;
5143 /* Parse the next assignment-expression. */
5144 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5146 /* A braced-init-list. */
5147 maybe_warn_cpp0x ("extended initializer lists");
5148 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5149 if (non_constant_p && expr_non_constant_p)
5150 *non_constant_p = true;
5152 else if (non_constant_p)
5154 expr = (cp_parser_constant_expression
5155 (parser, /*allow_non_constant_p=*/true,
5156 &expr_non_constant_p));
5157 if (expr_non_constant_p)
5158 *non_constant_p = true;
5161 expr = cp_parser_assignment_expression (parser, cast_p);
5164 expr = fold_non_dependent_expr (expr);
5166 /* If we have an ellipsis, then this is an expression
5168 if (allow_expansion_p
5169 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5171 /* Consume the `...'. */
5172 cp_lexer_consume_token (parser->lexer);
5174 /* Build the argument pack. */
5175 expr = make_pack_expansion (expr);
5178 /* Add it to the list. We add error_mark_node
5179 expressions to the list, so that we can still tell if
5180 the correct form for a parenthesized expression-list
5181 is found. That gives better errors. */
5182 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5184 if (expr == error_mark_node)
5188 /* After the first item, attribute lists look the same as
5189 expression lists. */
5190 is_attribute_list = false;
5193 /* If the next token isn't a `,', then we are done. */
5194 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5197 /* Otherwise, consume the `,' and keep going. */
5198 cp_lexer_consume_token (parser->lexer);
5201 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5206 /* We try and resync to an unnested comma, as that will give the
5207 user better diagnostics. */
5208 ending = cp_parser_skip_to_closing_parenthesis (parser,
5209 /*recovering=*/true,
5211 /*consume_paren=*/true);
5216 parser->greater_than_is_operator_p
5217 = saved_greater_than_is_operator_p;
5218 return error_mark_node;
5222 parser->greater_than_is_operator_p
5223 = saved_greater_than_is_operator_p;
5225 /* We built up the list in reverse order so we must reverse it now. */
5226 expression_list = nreverse (expression_list);
5228 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5230 return expression_list;
5233 /* Parse a pseudo-destructor-name.
5235 pseudo-destructor-name:
5236 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5237 :: [opt] nested-name-specifier template template-id :: ~ type-name
5238 :: [opt] nested-name-specifier [opt] ~ type-name
5240 If either of the first two productions is used, sets *SCOPE to the
5241 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5242 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5243 or ERROR_MARK_NODE if the parse fails. */
5246 cp_parser_pseudo_destructor_name (cp_parser* parser,
5250 bool nested_name_specifier_p;
5252 /* Assume that things will not work out. */
5253 *type = error_mark_node;
5255 /* Look for the optional `::' operator. */
5256 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5257 /* Look for the optional nested-name-specifier. */
5258 nested_name_specifier_p
5259 = (cp_parser_nested_name_specifier_opt (parser,
5260 /*typename_keyword_p=*/false,
5261 /*check_dependency_p=*/true,
5263 /*is_declaration=*/true)
5265 /* Now, if we saw a nested-name-specifier, we might be doing the
5266 second production. */
5267 if (nested_name_specifier_p
5268 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5270 /* Consume the `template' keyword. */
5271 cp_lexer_consume_token (parser->lexer);
5272 /* Parse the template-id. */
5273 cp_parser_template_id (parser,
5274 /*template_keyword_p=*/true,
5275 /*check_dependency_p=*/false,
5276 /*is_declaration=*/true);
5277 /* Look for the `::' token. */
5278 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5280 /* If the next token is not a `~', then there might be some
5281 additional qualification. */
5282 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5284 /* At this point, we're looking for "type-name :: ~". The type-name
5285 must not be a class-name, since this is a pseudo-destructor. So,
5286 it must be either an enum-name, or a typedef-name -- both of which
5287 are just identifiers. So, we peek ahead to check that the "::"
5288 and "~" tokens are present; if they are not, then we can avoid
5289 calling type_name. */
5290 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5291 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5292 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5294 cp_parser_error (parser, "non-scalar type");
5298 /* Look for the type-name. */
5299 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5300 if (*scope == error_mark_node)
5303 /* Look for the `::' token. */
5304 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5309 /* Look for the `~'. */
5310 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5311 /* Look for the type-name again. We are not responsible for
5312 checking that it matches the first type-name. */
5313 *type = cp_parser_nonclass_name (parser);
5316 /* Parse a unary-expression.
5322 unary-operator cast-expression
5323 sizeof unary-expression
5331 __extension__ cast-expression
5332 __alignof__ unary-expression
5333 __alignof__ ( type-id )
5334 __real__ cast-expression
5335 __imag__ cast-expression
5338 ADDRESS_P is true iff the unary-expression is appearing as the
5339 operand of the `&' operator. CAST_P is true if this expression is
5340 the target of a cast.
5342 Returns a representation of the expression. */
5345 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5348 enum tree_code unary_operator;
5350 /* Peek at the next token. */
5351 token = cp_lexer_peek_token (parser->lexer);
5352 /* Some keywords give away the kind of expression. */
5353 if (token->type == CPP_KEYWORD)
5355 enum rid keyword = token->keyword;
5365 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5366 /* Consume the token. */
5367 cp_lexer_consume_token (parser->lexer);
5368 /* Parse the operand. */
5369 operand = cp_parser_sizeof_operand (parser, keyword);
5371 if (TYPE_P (operand))
5372 return cxx_sizeof_or_alignof_type (operand, op, true);
5374 return cxx_sizeof_or_alignof_expr (operand, op, true);
5378 return cp_parser_new_expression (parser);
5381 return cp_parser_delete_expression (parser);
5385 /* The saved value of the PEDANTIC flag. */
5389 /* Save away the PEDANTIC flag. */
5390 cp_parser_extension_opt (parser, &saved_pedantic);
5391 /* Parse the cast-expression. */
5392 expr = cp_parser_simple_cast_expression (parser);
5393 /* Restore the PEDANTIC flag. */
5394 pedantic = saved_pedantic;
5404 /* Consume the `__real__' or `__imag__' token. */
5405 cp_lexer_consume_token (parser->lexer);
5406 /* Parse the cast-expression. */
5407 expression = cp_parser_simple_cast_expression (parser);
5408 /* Create the complete representation. */
5409 return build_x_unary_op ((keyword == RID_REALPART
5410 ? REALPART_EXPR : IMAGPART_EXPR),
5412 tf_warning_or_error);
5421 /* Look for the `:: new' and `:: delete', which also signal the
5422 beginning of a new-expression, or delete-expression,
5423 respectively. If the next token is `::', then it might be one of
5425 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5429 /* See if the token after the `::' is one of the keywords in
5430 which we're interested. */
5431 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5432 /* If it's `new', we have a new-expression. */
5433 if (keyword == RID_NEW)
5434 return cp_parser_new_expression (parser);
5435 /* Similarly, for `delete'. */
5436 else if (keyword == RID_DELETE)
5437 return cp_parser_delete_expression (parser);
5440 /* Look for a unary operator. */
5441 unary_operator = cp_parser_unary_operator (token);
5442 /* The `++' and `--' operators can be handled similarly, even though
5443 they are not technically unary-operators in the grammar. */
5444 if (unary_operator == ERROR_MARK)
5446 if (token->type == CPP_PLUS_PLUS)
5447 unary_operator = PREINCREMENT_EXPR;
5448 else if (token->type == CPP_MINUS_MINUS)
5449 unary_operator = PREDECREMENT_EXPR;
5450 /* Handle the GNU address-of-label extension. */
5451 else if (cp_parser_allow_gnu_extensions_p (parser)
5452 && token->type == CPP_AND_AND)
5457 /* Consume the '&&' token. */
5458 cp_lexer_consume_token (parser->lexer);
5459 /* Look for the identifier. */
5460 identifier = cp_parser_identifier (parser);
5461 /* Create an expression representing the address. */
5462 expression = finish_label_address_expr (identifier);
5463 if (cp_parser_non_integral_constant_expression (parser,
5464 "the address of a label"))
5465 expression = error_mark_node;
5469 if (unary_operator != ERROR_MARK)
5471 tree cast_expression;
5472 tree expression = error_mark_node;
5473 const char *non_constant_p = NULL;
5475 /* Consume the operator token. */
5476 token = cp_lexer_consume_token (parser->lexer);
5477 /* Parse the cast-expression. */
5479 = cp_parser_cast_expression (parser,
5480 unary_operator == ADDR_EXPR,
5482 /* Now, build an appropriate representation. */
5483 switch (unary_operator)
5486 non_constant_p = "%<*%>";
5487 expression = build_x_indirect_ref (cast_expression, "unary *",
5488 tf_warning_or_error);
5492 non_constant_p = "%<&%>";
5495 expression = build_x_unary_op (unary_operator, cast_expression,
5496 tf_warning_or_error);
5499 case PREINCREMENT_EXPR:
5500 case PREDECREMENT_EXPR:
5501 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5502 ? "%<++%>" : "%<--%>");
5504 case UNARY_PLUS_EXPR:
5506 case TRUTH_NOT_EXPR:
5507 expression = finish_unary_op_expr (unary_operator, cast_expression);
5515 && cp_parser_non_integral_constant_expression (parser,
5517 expression = error_mark_node;
5522 return cp_parser_postfix_expression (parser, address_p, cast_p,
5523 /*member_access_only_p=*/false);
5526 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5527 unary-operator, the corresponding tree code is returned. */
5529 static enum tree_code
5530 cp_parser_unary_operator (cp_token* token)
5532 switch (token->type)
5535 return INDIRECT_REF;
5541 return UNARY_PLUS_EXPR;
5547 return TRUTH_NOT_EXPR;
5550 return BIT_NOT_EXPR;
5557 /* Parse a new-expression.
5560 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5561 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5563 Returns a representation of the expression. */
5566 cp_parser_new_expression (cp_parser* parser)
5568 bool global_scope_p;
5574 /* Look for the optional `::' operator. */
5576 = (cp_parser_global_scope_opt (parser,
5577 /*current_scope_valid_p=*/false)
5579 /* Look for the `new' operator. */
5580 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5581 /* There's no easy way to tell a new-placement from the
5582 `( type-id )' construct. */
5583 cp_parser_parse_tentatively (parser);
5584 /* Look for a new-placement. */
5585 placement = cp_parser_new_placement (parser);
5586 /* If that didn't work out, there's no new-placement. */
5587 if (!cp_parser_parse_definitely (parser))
5588 placement = NULL_TREE;
5590 /* If the next token is a `(', then we have a parenthesized
5592 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5595 /* Consume the `('. */
5596 cp_lexer_consume_token (parser->lexer);
5597 /* Parse the type-id. */
5598 type = cp_parser_type_id (parser);
5599 /* Look for the closing `)'. */
5600 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5601 token = cp_lexer_peek_token (parser->lexer);
5602 /* There should not be a direct-new-declarator in this production,
5603 but GCC used to allowed this, so we check and emit a sensible error
5604 message for this case. */
5605 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5607 error ("%Harray bound forbidden after parenthesized type-id",
5609 inform (token->location,
5610 "try removing the parentheses around the type-id");
5611 cp_parser_direct_new_declarator (parser);
5615 /* Otherwise, there must be a new-type-id. */
5617 type = cp_parser_new_type_id (parser, &nelts);
5619 /* If the next token is a `(' or '{', then we have a new-initializer. */
5620 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5621 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5622 initializer = cp_parser_new_initializer (parser);
5624 initializer = NULL_TREE;
5626 /* A new-expression may not appear in an integral constant
5628 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5629 return error_mark_node;
5631 /* Create a representation of the new-expression. */
5632 return build_new (placement, type, nelts, initializer, global_scope_p,
5633 tf_warning_or_error);
5636 /* Parse a new-placement.
5641 Returns the same representation as for an expression-list. */
5644 cp_parser_new_placement (cp_parser* parser)
5646 tree expression_list;
5648 /* Parse the expression-list. */
5649 expression_list = (cp_parser_parenthesized_expression_list
5650 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5651 /*non_constant_p=*/NULL));
5653 return expression_list;
5656 /* Parse a new-type-id.
5659 type-specifier-seq new-declarator [opt]
5661 Returns the TYPE allocated. If the new-type-id indicates an array
5662 type, *NELTS is set to the number of elements in the last array
5663 bound; the TYPE will not include the last array bound. */
5666 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5668 cp_decl_specifier_seq type_specifier_seq;
5669 cp_declarator *new_declarator;
5670 cp_declarator *declarator;
5671 cp_declarator *outer_declarator;
5672 const char *saved_message;
5675 /* The type-specifier sequence must not contain type definitions.
5676 (It cannot contain declarations of new types either, but if they
5677 are not definitions we will catch that because they are not
5679 saved_message = parser->type_definition_forbidden_message;
5680 parser->type_definition_forbidden_message
5681 = "types may not be defined in a new-type-id";
5682 /* Parse the type-specifier-seq. */
5683 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5684 &type_specifier_seq);
5685 /* Restore the old message. */
5686 parser->type_definition_forbidden_message = saved_message;
5687 /* Parse the new-declarator. */
5688 new_declarator = cp_parser_new_declarator_opt (parser);
5690 /* Determine the number of elements in the last array dimension, if
5693 /* Skip down to the last array dimension. */
5694 declarator = new_declarator;
5695 outer_declarator = NULL;
5696 while (declarator && (declarator->kind == cdk_pointer
5697 || declarator->kind == cdk_ptrmem))
5699 outer_declarator = declarator;
5700 declarator = declarator->declarator;
5703 && declarator->kind == cdk_array
5704 && declarator->declarator
5705 && declarator->declarator->kind == cdk_array)
5707 outer_declarator = declarator;
5708 declarator = declarator->declarator;
5711 if (declarator && declarator->kind == cdk_array)
5713 *nelts = declarator->u.array.bounds;
5714 if (*nelts == error_mark_node)
5715 *nelts = integer_one_node;
5717 if (outer_declarator)
5718 outer_declarator->declarator = declarator->declarator;
5720 new_declarator = NULL;
5723 type = groktypename (&type_specifier_seq, new_declarator);
5727 /* Parse an (optional) new-declarator.
5730 ptr-operator new-declarator [opt]
5731 direct-new-declarator
5733 Returns the declarator. */
5735 static cp_declarator *
5736 cp_parser_new_declarator_opt (cp_parser* parser)
5738 enum tree_code code;
5740 cp_cv_quals cv_quals;
5742 /* We don't know if there's a ptr-operator next, or not. */
5743 cp_parser_parse_tentatively (parser);
5744 /* Look for a ptr-operator. */
5745 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5746 /* If that worked, look for more new-declarators. */
5747 if (cp_parser_parse_definitely (parser))
5749 cp_declarator *declarator;
5751 /* Parse another optional declarator. */
5752 declarator = cp_parser_new_declarator_opt (parser);
5754 return cp_parser_make_indirect_declarator
5755 (code, type, cv_quals, declarator);
5758 /* If the next token is a `[', there is a direct-new-declarator. */
5759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5760 return cp_parser_direct_new_declarator (parser);
5765 /* Parse a direct-new-declarator.
5767 direct-new-declarator:
5769 direct-new-declarator [constant-expression]
5773 static cp_declarator *
5774 cp_parser_direct_new_declarator (cp_parser* parser)
5776 cp_declarator *declarator = NULL;
5782 /* Look for the opening `['. */
5783 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5784 /* The first expression is not required to be constant. */
5787 cp_token *token = cp_lexer_peek_token (parser->lexer);
5788 expression = cp_parser_expression (parser, /*cast_p=*/false);
5789 /* The standard requires that the expression have integral
5790 type. DR 74 adds enumeration types. We believe that the
5791 real intent is that these expressions be handled like the
5792 expression in a `switch' condition, which also allows
5793 classes with a single conversion to integral or
5794 enumeration type. */
5795 if (!processing_template_decl)
5798 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5803 error ("%Hexpression in new-declarator must have integral "
5804 "or enumeration type", &token->location);
5805 expression = error_mark_node;
5809 /* But all the other expressions must be. */
5812 = cp_parser_constant_expression (parser,
5813 /*allow_non_constant=*/false,
5815 /* Look for the closing `]'. */
5816 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5818 /* Add this bound to the declarator. */
5819 declarator = make_array_declarator (declarator, expression);
5821 /* If the next token is not a `[', then there are no more
5823 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5830 /* Parse a new-initializer.
5833 ( expression-list [opt] )
5836 Returns a representation of the expression-list. If there is no
5837 expression-list, VOID_ZERO_NODE is returned. */
5840 cp_parser_new_initializer (cp_parser* parser)
5842 tree expression_list;
5844 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5846 bool expr_non_constant_p;
5847 maybe_warn_cpp0x ("extended initializer lists");
5848 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5849 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5850 expression_list = build_tree_list (NULL_TREE, expression_list);
5853 expression_list = (cp_parser_parenthesized_expression_list
5854 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5855 /*non_constant_p=*/NULL));
5856 if (!expression_list)
5857 expression_list = void_zero_node;
5859 return expression_list;
5862 /* Parse a delete-expression.
5865 :: [opt] delete cast-expression
5866 :: [opt] delete [ ] cast-expression
5868 Returns a representation of the expression. */
5871 cp_parser_delete_expression (cp_parser* parser)
5873 bool global_scope_p;
5877 /* Look for the optional `::' operator. */
5879 = (cp_parser_global_scope_opt (parser,
5880 /*current_scope_valid_p=*/false)
5882 /* Look for the `delete' keyword. */
5883 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5884 /* See if the array syntax is in use. */
5885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5887 /* Consume the `[' token. */
5888 cp_lexer_consume_token (parser->lexer);
5889 /* Look for the `]' token. */
5890 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5891 /* Remember that this is the `[]' construct. */
5897 /* Parse the cast-expression. */
5898 expression = cp_parser_simple_cast_expression (parser);
5900 /* A delete-expression may not appear in an integral constant
5902 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5903 return error_mark_node;
5905 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5908 /* Parse a cast-expression.
5912 ( type-id ) cast-expression
5914 ADDRESS_P is true iff the unary-expression is appearing as the
5915 operand of the `&' operator. CAST_P is true if this expression is
5916 the target of a cast.
5918 Returns a representation of the expression. */
5921 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5923 /* If it's a `(', then we might be looking at a cast. */
5924 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5926 tree type = NULL_TREE;
5927 tree expr = NULL_TREE;
5928 bool compound_literal_p;
5929 const char *saved_message;
5931 /* There's no way to know yet whether or not this is a cast.
5932 For example, `(int (3))' is a unary-expression, while `(int)
5933 3' is a cast. So, we resort to parsing tentatively. */
5934 cp_parser_parse_tentatively (parser);
5935 /* Types may not be defined in a cast. */
5936 saved_message = parser->type_definition_forbidden_message;
5937 parser->type_definition_forbidden_message
5938 = "types may not be defined in casts";
5939 /* Consume the `('. */
5940 cp_lexer_consume_token (parser->lexer);
5941 /* A very tricky bit is that `(struct S) { 3 }' is a
5942 compound-literal (which we permit in C++ as an extension).
5943 But, that construct is not a cast-expression -- it is a
5944 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5945 is legal; if the compound-literal were a cast-expression,
5946 you'd need an extra set of parentheses.) But, if we parse
5947 the type-id, and it happens to be a class-specifier, then we
5948 will commit to the parse at that point, because we cannot
5949 undo the action that is done when creating a new class. So,
5950 then we cannot back up and do a postfix-expression.
5952 Therefore, we scan ahead to the closing `)', and check to see
5953 if the token after the `)' is a `{'. If so, we are not
5954 looking at a cast-expression.
5956 Save tokens so that we can put them back. */
5957 cp_lexer_save_tokens (parser->lexer);
5958 /* Skip tokens until the next token is a closing parenthesis.
5959 If we find the closing `)', and the next token is a `{', then
5960 we are looking at a compound-literal. */
5962 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5963 /*consume_paren=*/true)
5964 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5965 /* Roll back the tokens we skipped. */
5966 cp_lexer_rollback_tokens (parser->lexer);
5967 /* If we were looking at a compound-literal, simulate an error
5968 so that the call to cp_parser_parse_definitely below will
5970 if (compound_literal_p)
5971 cp_parser_simulate_error (parser);
5974 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5975 parser->in_type_id_in_expr_p = true;
5976 /* Look for the type-id. */
5977 type = cp_parser_type_id (parser);
5978 /* Look for the closing `)'. */
5979 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5980 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5983 /* Restore the saved message. */
5984 parser->type_definition_forbidden_message = saved_message;
5986 /* If ok so far, parse the dependent expression. We cannot be
5987 sure it is a cast. Consider `(T ())'. It is a parenthesized
5988 ctor of T, but looks like a cast to function returning T
5989 without a dependent expression. */
5990 if (!cp_parser_error_occurred (parser))
5991 expr = cp_parser_cast_expression (parser,
5992 /*address_p=*/false,
5995 if (cp_parser_parse_definitely (parser))
5997 /* Warn about old-style casts, if so requested. */
5998 if (warn_old_style_cast
5999 && !in_system_header
6000 && !VOID_TYPE_P (type)
6001 && current_lang_name != lang_name_c)
6002 warning (OPT_Wold_style_cast, "use of old-style cast");
6004 /* Only type conversions to integral or enumeration types
6005 can be used in constant-expressions. */
6006 if (!cast_valid_in_integral_constant_expression_p (type)
6007 && (cp_parser_non_integral_constant_expression
6009 "a cast to a type other than an integral or "
6010 "enumeration type")))
6011 return error_mark_node;
6013 /* Perform the cast. */
6014 expr = build_c_cast (type, expr);
6019 /* If we get here, then it's not a cast, so it must be a
6020 unary-expression. */
6021 return cp_parser_unary_expression (parser, address_p, cast_p);
6024 /* Parse a binary expression of the general form:
6028 pm-expression .* cast-expression
6029 pm-expression ->* cast-expression
6031 multiplicative-expression:
6033 multiplicative-expression * pm-expression
6034 multiplicative-expression / pm-expression
6035 multiplicative-expression % pm-expression
6037 additive-expression:
6038 multiplicative-expression
6039 additive-expression + multiplicative-expression
6040 additive-expression - multiplicative-expression
6044 shift-expression << additive-expression
6045 shift-expression >> additive-expression
6047 relational-expression:
6049 relational-expression < shift-expression
6050 relational-expression > shift-expression
6051 relational-expression <= shift-expression
6052 relational-expression >= shift-expression
6056 relational-expression:
6057 relational-expression <? shift-expression
6058 relational-expression >? shift-expression
6060 equality-expression:
6061 relational-expression
6062 equality-expression == relational-expression
6063 equality-expression != relational-expression
6067 and-expression & equality-expression
6069 exclusive-or-expression:
6071 exclusive-or-expression ^ and-expression
6073 inclusive-or-expression:
6074 exclusive-or-expression
6075 inclusive-or-expression | exclusive-or-expression
6077 logical-and-expression:
6078 inclusive-or-expression
6079 logical-and-expression && inclusive-or-expression
6081 logical-or-expression:
6082 logical-and-expression
6083 logical-or-expression || logical-and-expression
6085 All these are implemented with a single function like:
6088 simple-cast-expression
6089 binary-expression <token> binary-expression
6091 CAST_P is true if this expression is the target of a cast.
6093 The binops_by_token map is used to get the tree codes for each <token> type.
6094 binary-expressions are associated according to a precedence table. */
6096 #define TOKEN_PRECEDENCE(token) \
6097 (((token->type == CPP_GREATER \
6098 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6099 && !parser->greater_than_is_operator_p) \
6100 ? PREC_NOT_OPERATOR \
6101 : binops_by_token[token->type].prec)
6104 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6105 enum cp_parser_prec prec)
6107 cp_parser_expression_stack stack;
6108 cp_parser_expression_stack_entry *sp = &stack[0];
6111 enum tree_code tree_type, lhs_type, rhs_type;
6112 enum cp_parser_prec new_prec, lookahead_prec;
6115 /* Parse the first expression. */
6116 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6117 lhs_type = ERROR_MARK;
6121 /* Get an operator token. */
6122 token = cp_lexer_peek_token (parser->lexer);
6124 if (warn_cxx0x_compat
6125 && token->type == CPP_RSHIFT
6126 && !parser->greater_than_is_operator_p)
6128 warning (OPT_Wc__0x_compat,
6129 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6131 warning (OPT_Wc__0x_compat,
6132 "suggest parentheses around %<>>%> expression");
6135 new_prec = TOKEN_PRECEDENCE (token);
6137 /* Popping an entry off the stack means we completed a subexpression:
6138 - either we found a token which is not an operator (`>' where it is not
6139 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6140 will happen repeatedly;
6141 - or, we found an operator which has lower priority. This is the case
6142 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6144 if (new_prec <= prec)
6153 tree_type = binops_by_token[token->type].tree_type;
6155 /* We used the operator token. */
6156 cp_lexer_consume_token (parser->lexer);
6158 /* Extract another operand. It may be the RHS of this expression
6159 or the LHS of a new, higher priority expression. */
6160 rhs = cp_parser_simple_cast_expression (parser);
6161 rhs_type = ERROR_MARK;
6163 /* Get another operator token. Look up its precedence to avoid
6164 building a useless (immediately popped) stack entry for common
6165 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6166 token = cp_lexer_peek_token (parser->lexer);
6167 lookahead_prec = TOKEN_PRECEDENCE (token);
6168 if (lookahead_prec > new_prec)
6170 /* ... and prepare to parse the RHS of the new, higher priority
6171 expression. Since precedence levels on the stack are
6172 monotonically increasing, we do not have to care about
6175 sp->tree_type = tree_type;
6177 sp->lhs_type = lhs_type;
6180 lhs_type = rhs_type;
6182 new_prec = lookahead_prec;
6186 /* If the stack is not empty, we have parsed into LHS the right side
6187 (`4' in the example above) of an expression we had suspended.
6188 We can use the information on the stack to recover the LHS (`3')
6189 from the stack together with the tree code (`MULT_EXPR'), and
6190 the precedence of the higher level subexpression
6191 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6192 which will be used to actually build the additive expression. */
6195 tree_type = sp->tree_type;
6197 rhs_type = lhs_type;
6199 lhs_type = sp->lhs_type;
6202 overloaded_p = false;
6203 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6204 &overloaded_p, tf_warning_or_error);
6205 lhs_type = tree_type;
6207 /* If the binary operator required the use of an overloaded operator,
6208 then this expression cannot be an integral constant-expression.
6209 An overloaded operator can be used even if both operands are
6210 otherwise permissible in an integral constant-expression if at
6211 least one of the operands is of enumeration type. */
6214 && (cp_parser_non_integral_constant_expression
6215 (parser, "calls to overloaded operators")))
6216 return error_mark_node;
6223 /* Parse the `? expression : assignment-expression' part of a
6224 conditional-expression. The LOGICAL_OR_EXPR is the
6225 logical-or-expression that started the conditional-expression.
6226 Returns a representation of the entire conditional-expression.
6228 This routine is used by cp_parser_assignment_expression.
6230 ? expression : assignment-expression
6234 ? : assignment-expression */
6237 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6240 tree assignment_expr;
6242 /* Consume the `?' token. */
6243 cp_lexer_consume_token (parser->lexer);
6244 if (cp_parser_allow_gnu_extensions_p (parser)
6245 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6246 /* Implicit true clause. */
6249 /* Parse the expression. */
6250 expr = cp_parser_expression (parser, /*cast_p=*/false);
6252 /* The next token should be a `:'. */
6253 cp_parser_require (parser, CPP_COLON, "%<:%>");
6254 /* Parse the assignment-expression. */
6255 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6257 /* Build the conditional-expression. */
6258 return build_x_conditional_expr (logical_or_expr,
6261 tf_warning_or_error);
6264 /* Parse an assignment-expression.
6266 assignment-expression:
6267 conditional-expression
6268 logical-or-expression assignment-operator assignment_expression
6271 CAST_P is true if this expression is the target of a cast.
6273 Returns a representation for the expression. */
6276 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6280 /* If the next token is the `throw' keyword, then we're looking at
6281 a throw-expression. */
6282 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6283 expr = cp_parser_throw_expression (parser);
6284 /* Otherwise, it must be that we are looking at a
6285 logical-or-expression. */
6288 /* Parse the binary expressions (logical-or-expression). */
6289 expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6290 /* If the next token is a `?' then we're actually looking at a
6291 conditional-expression. */
6292 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6293 return cp_parser_question_colon_clause (parser, expr);
6296 enum tree_code assignment_operator;
6298 /* If it's an assignment-operator, we're using the second
6301 = cp_parser_assignment_operator_opt (parser);
6302 if (assignment_operator != ERROR_MARK)
6304 bool non_constant_p;
6306 /* Parse the right-hand side of the assignment. */
6307 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6309 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6310 maybe_warn_cpp0x ("extended initializer lists");
6312 /* An assignment may not appear in a
6313 constant-expression. */
6314 if (cp_parser_non_integral_constant_expression (parser,
6316 return error_mark_node;
6317 /* Build the assignment expression. */
6318 expr = build_x_modify_expr (expr,
6319 assignment_operator,
6321 tf_warning_or_error);
6329 /* Parse an (optional) assignment-operator.
6331 assignment-operator: one of
6332 = *= /= %= += -= >>= <<= &= ^= |=
6336 assignment-operator: one of
6339 If the next token is an assignment operator, the corresponding tree
6340 code is returned, and the token is consumed. For example, for
6341 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6342 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6343 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6344 operator, ERROR_MARK is returned. */
6346 static enum tree_code
6347 cp_parser_assignment_operator_opt (cp_parser* parser)
6352 /* Peek at the next token. */
6353 token = cp_lexer_peek_token (parser->lexer);
6355 switch (token->type)
6366 op = TRUNC_DIV_EXPR;
6370 op = TRUNC_MOD_EXPR;
6402 /* Nothing else is an assignment operator. */
6406 /* If it was an assignment operator, consume it. */
6407 if (op != ERROR_MARK)
6408 cp_lexer_consume_token (parser->lexer);
6413 /* Parse an expression.
6416 assignment-expression
6417 expression , assignment-expression
6419 CAST_P is true if this expression is the target of a cast.
6421 Returns a representation of the expression. */
6424 cp_parser_expression (cp_parser* parser, bool cast_p)
6426 tree expression = NULL_TREE;
6430 tree assignment_expression;
6432 /* Parse the next assignment-expression. */
6433 assignment_expression
6434 = cp_parser_assignment_expression (parser, cast_p);
6435 /* If this is the first assignment-expression, we can just
6438 expression = assignment_expression;
6440 expression = build_x_compound_expr (expression,
6441 assignment_expression,
6442 tf_warning_or_error);
6443 /* If the next token is not a comma, then we are done with the
6445 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6447 /* Consume the `,'. */
6448 cp_lexer_consume_token (parser->lexer);
6449 /* A comma operator cannot appear in a constant-expression. */
6450 if (cp_parser_non_integral_constant_expression (parser,
6451 "a comma operator"))
6452 expression = error_mark_node;
6458 /* Parse a constant-expression.
6460 constant-expression:
6461 conditional-expression
6463 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6464 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6465 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6466 is false, NON_CONSTANT_P should be NULL. */
6469 cp_parser_constant_expression (cp_parser* parser,
6470 bool allow_non_constant_p,
6471 bool *non_constant_p)
6473 bool saved_integral_constant_expression_p;
6474 bool saved_allow_non_integral_constant_expression_p;
6475 bool saved_non_integral_constant_expression_p;
6478 /* It might seem that we could simply parse the
6479 conditional-expression, and then check to see if it were
6480 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6481 one that the compiler can figure out is constant, possibly after
6482 doing some simplifications or optimizations. The standard has a
6483 precise definition of constant-expression, and we must honor
6484 that, even though it is somewhat more restrictive.
6490 is not a legal declaration, because `(2, 3)' is not a
6491 constant-expression. The `,' operator is forbidden in a
6492 constant-expression. However, GCC's constant-folding machinery
6493 will fold this operation to an INTEGER_CST for `3'. */
6495 /* Save the old settings. */
6496 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6497 saved_allow_non_integral_constant_expression_p
6498 = parser->allow_non_integral_constant_expression_p;
6499 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6500 /* We are now parsing a constant-expression. */
6501 parser->integral_constant_expression_p = true;
6502 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6503 parser->non_integral_constant_expression_p = false;
6504 /* Although the grammar says "conditional-expression", we parse an
6505 "assignment-expression", which also permits "throw-expression"
6506 and the use of assignment operators. In the case that
6507 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6508 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6509 actually essential that we look for an assignment-expression.
6510 For example, cp_parser_initializer_clauses uses this function to
6511 determine whether a particular assignment-expression is in fact
6513 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6514 /* Restore the old settings. */
6515 parser->integral_constant_expression_p
6516 = saved_integral_constant_expression_p;
6517 parser->allow_non_integral_constant_expression_p
6518 = saved_allow_non_integral_constant_expression_p;
6519 if (allow_non_constant_p)
6520 *non_constant_p = parser->non_integral_constant_expression_p;
6521 else if (parser->non_integral_constant_expression_p)
6522 expression = error_mark_node;
6523 parser->non_integral_constant_expression_p
6524 = saved_non_integral_constant_expression_p;
6529 /* Parse __builtin_offsetof.
6531 offsetof-expression:
6532 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6534 offsetof-member-designator:
6536 | offsetof-member-designator "." id-expression
6537 | offsetof-member-designator "[" expression "]" */
6540 cp_parser_builtin_offsetof (cp_parser *parser)
6542 int save_ice_p, save_non_ice_p;
6547 /* We're about to accept non-integral-constant things, but will
6548 definitely yield an integral constant expression. Save and
6549 restore these values around our local parsing. */
6550 save_ice_p = parser->integral_constant_expression_p;
6551 save_non_ice_p = parser->non_integral_constant_expression_p;
6553 /* Consume the "__builtin_offsetof" token. */
6554 cp_lexer_consume_token (parser->lexer);
6555 /* Consume the opening `('. */
6556 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6557 /* Parse the type-id. */
6558 type = cp_parser_type_id (parser);
6559 /* Look for the `,'. */
6560 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6561 token = cp_lexer_peek_token (parser->lexer);
6563 /* Build the (type *)null that begins the traditional offsetof macro. */
6564 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6565 tf_warning_or_error);
6567 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6568 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6569 true, &dummy, token->location);
6572 token = cp_lexer_peek_token (parser->lexer);
6573 switch (token->type)
6575 case CPP_OPEN_SQUARE:
6576 /* offsetof-member-designator "[" expression "]" */
6577 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6581 /* offsetof-member-designator "." identifier */
6582 cp_lexer_consume_token (parser->lexer);
6583 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6588 case CPP_CLOSE_PAREN:
6589 /* Consume the ")" token. */
6590 cp_lexer_consume_token (parser->lexer);
6594 /* Error. We know the following require will fail, but
6595 that gives the proper error message. */
6596 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6597 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6598 expr = error_mark_node;
6604 /* If we're processing a template, we can't finish the semantics yet.
6605 Otherwise we can fold the entire expression now. */
6606 if (processing_template_decl)
6607 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6609 expr = finish_offsetof (expr);
6612 parser->integral_constant_expression_p = save_ice_p;
6613 parser->non_integral_constant_expression_p = save_non_ice_p;
6618 /* Parse a trait expression. */
6621 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6624 tree type1, type2 = NULL_TREE;
6625 bool binary = false;
6626 cp_decl_specifier_seq decl_specs;
6630 case RID_HAS_NOTHROW_ASSIGN:
6631 kind = CPTK_HAS_NOTHROW_ASSIGN;
6633 case RID_HAS_NOTHROW_CONSTRUCTOR:
6634 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6636 case RID_HAS_NOTHROW_COPY:
6637 kind = CPTK_HAS_NOTHROW_COPY;
6639 case RID_HAS_TRIVIAL_ASSIGN:
6640 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6642 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6643 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6645 case RID_HAS_TRIVIAL_COPY:
6646 kind = CPTK_HAS_TRIVIAL_COPY;
6648 case RID_HAS_TRIVIAL_DESTRUCTOR:
6649 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6651 case RID_HAS_VIRTUAL_DESTRUCTOR:
6652 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6654 case RID_IS_ABSTRACT:
6655 kind = CPTK_IS_ABSTRACT;
6657 case RID_IS_BASE_OF:
6658 kind = CPTK_IS_BASE_OF;
6662 kind = CPTK_IS_CLASS;
6664 case RID_IS_CONVERTIBLE_TO:
6665 kind = CPTK_IS_CONVERTIBLE_TO;
6669 kind = CPTK_IS_EMPTY;
6672 kind = CPTK_IS_ENUM;
6677 case RID_IS_POLYMORPHIC:
6678 kind = CPTK_IS_POLYMORPHIC;
6681 kind = CPTK_IS_UNION;
6687 /* Consume the token. */
6688 cp_lexer_consume_token (parser->lexer);
6690 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6692 type1 = cp_parser_type_id (parser);
6694 if (type1 == error_mark_node)
6695 return error_mark_node;
6697 /* Build a trivial decl-specifier-seq. */
6698 clear_decl_specs (&decl_specs);
6699 decl_specs.type = type1;
6701 /* Call grokdeclarator to figure out what type this is. */
6702 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6703 /*initialized=*/0, /*attrlist=*/NULL);
6707 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6709 type2 = cp_parser_type_id (parser);
6711 if (type2 == error_mark_node)
6712 return error_mark_node;
6714 /* Build a trivial decl-specifier-seq. */
6715 clear_decl_specs (&decl_specs);
6716 decl_specs.type = type2;
6718 /* Call grokdeclarator to figure out what type this is. */
6719 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6720 /*initialized=*/0, /*attrlist=*/NULL);
6723 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6725 /* Complete the trait expression, which may mean either processing
6726 the trait expr now or saving it for template instantiation. */
6727 return finish_trait_expr (kind, type1, type2);
6730 /* Statements [gram.stmt.stmt] */
6732 /* Parse a statement.
6736 expression-statement
6741 declaration-statement
6744 IN_COMPOUND is true when the statement is nested inside a
6745 cp_parser_compound_statement; this matters for certain pragmas.
6747 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6748 is a (possibly labeled) if statement which is not enclosed in braces
6749 and has an else clause. This is used to implement -Wparentheses. */
6752 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6753 bool in_compound, bool *if_p)
6757 location_t statement_location;
6762 /* There is no statement yet. */
6763 statement = NULL_TREE;
6764 /* Peek at the next token. */
6765 token = cp_lexer_peek_token (parser->lexer);
6766 /* Remember the location of the first token in the statement. */
6767 statement_location = token->location;
6768 /* If this is a keyword, then that will often determine what kind of
6769 statement we have. */
6770 if (token->type == CPP_KEYWORD)
6772 enum rid keyword = token->keyword;
6778 /* Looks like a labeled-statement with a case label.
6779 Parse the label, and then use tail recursion to parse
6781 cp_parser_label_for_labeled_statement (parser);
6786 statement = cp_parser_selection_statement (parser, if_p);
6792 statement = cp_parser_iteration_statement (parser);
6799 statement = cp_parser_jump_statement (parser);
6802 /* Objective-C++ exception-handling constructs. */
6805 case RID_AT_FINALLY:
6806 case RID_AT_SYNCHRONIZED:
6808 statement = cp_parser_objc_statement (parser);
6812 statement = cp_parser_try_block (parser);
6816 /* This must be a namespace alias definition. */
6817 cp_parser_declaration_statement (parser);
6821 /* It might be a keyword like `int' that can start a
6822 declaration-statement. */
6826 else if (token->type == CPP_NAME)
6828 /* If the next token is a `:', then we are looking at a
6829 labeled-statement. */
6830 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6831 if (token->type == CPP_COLON)
6833 /* Looks like a labeled-statement with an ordinary label.
6834 Parse the label, and then use tail recursion to parse
6836 cp_parser_label_for_labeled_statement (parser);
6840 /* Anything that starts with a `{' must be a compound-statement. */
6841 else if (token->type == CPP_OPEN_BRACE)
6842 statement = cp_parser_compound_statement (parser, NULL, false);
6843 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6844 a statement all its own. */
6845 else if (token->type == CPP_PRAGMA)
6847 /* Only certain OpenMP pragmas are attached to statements, and thus
6848 are considered statements themselves. All others are not. In
6849 the context of a compound, accept the pragma as a "statement" and
6850 return so that we can check for a close brace. Otherwise we
6851 require a real statement and must go back and read one. */
6853 cp_parser_pragma (parser, pragma_compound);
6854 else if (!cp_parser_pragma (parser, pragma_stmt))
6858 else if (token->type == CPP_EOF)
6860 cp_parser_error (parser, "expected statement");
6864 /* Everything else must be a declaration-statement or an
6865 expression-statement. Try for the declaration-statement
6866 first, unless we are looking at a `;', in which case we know that
6867 we have an expression-statement. */
6870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6872 cp_parser_parse_tentatively (parser);
6873 /* Try to parse the declaration-statement. */
6874 cp_parser_declaration_statement (parser);
6875 /* If that worked, we're done. */
6876 if (cp_parser_parse_definitely (parser))
6879 /* Look for an expression-statement instead. */
6880 statement = cp_parser_expression_statement (parser, in_statement_expr);
6883 /* Set the line number for the statement. */
6884 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6885 SET_EXPR_LOCATION (statement, statement_location);
6888 /* Parse the label for a labeled-statement, i.e.
6891 case constant-expression :
6895 case constant-expression ... constant-expression : statement
6897 When a label is parsed without errors, the label is added to the
6898 parse tree by the finish_* functions, so this function doesn't
6899 have to return the label. */
6902 cp_parser_label_for_labeled_statement (cp_parser* parser)
6906 /* The next token should be an identifier. */
6907 token = cp_lexer_peek_token (parser->lexer);
6908 if (token->type != CPP_NAME
6909 && token->type != CPP_KEYWORD)
6911 cp_parser_error (parser, "expected labeled-statement");
6915 switch (token->keyword)
6922 /* Consume the `case' token. */
6923 cp_lexer_consume_token (parser->lexer);
6924 /* Parse the constant-expression. */
6925 expr = cp_parser_constant_expression (parser,
6926 /*allow_non_constant_p=*/false,
6929 ellipsis = cp_lexer_peek_token (parser->lexer);
6930 if (ellipsis->type == CPP_ELLIPSIS)
6932 /* Consume the `...' token. */
6933 cp_lexer_consume_token (parser->lexer);
6935 cp_parser_constant_expression (parser,
6936 /*allow_non_constant_p=*/false,
6938 /* We don't need to emit warnings here, as the common code
6939 will do this for us. */
6942 expr_hi = NULL_TREE;
6944 if (parser->in_switch_statement_p)
6945 finish_case_label (expr, expr_hi);
6947 error ("%Hcase label %qE not within a switch statement",
6948 &token->location, expr);
6953 /* Consume the `default' token. */
6954 cp_lexer_consume_token (parser->lexer);
6956 if (parser->in_switch_statement_p)
6957 finish_case_label (NULL_TREE, NULL_TREE);
6959 error ("%Hcase label not within a switch statement", &token->location);
6963 /* Anything else must be an ordinary label. */
6964 finish_label_stmt (cp_parser_identifier (parser));
6968 /* Require the `:' token. */
6969 cp_parser_require (parser, CPP_COLON, "%<:%>");
6972 /* Parse an expression-statement.
6974 expression-statement:
6977 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6978 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6979 indicates whether this expression-statement is part of an
6980 expression statement. */
6983 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6985 tree statement = NULL_TREE;
6987 /* If the next token is a ';', then there is no expression
6989 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6990 statement = cp_parser_expression (parser, /*cast_p=*/false);
6992 /* Consume the final `;'. */
6993 cp_parser_consume_semicolon_at_end_of_statement (parser);
6995 if (in_statement_expr
6996 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6997 /* This is the final expression statement of a statement
6999 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7001 statement = finish_expr_stmt (statement);
7008 /* Parse a compound-statement.
7011 { statement-seq [opt] }
7016 { label-declaration-seq [opt] statement-seq [opt] }
7018 label-declaration-seq:
7020 label-declaration-seq label-declaration
7022 Returns a tree representing the statement. */
7025 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7030 /* Consume the `{'. */
7031 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7032 return error_mark_node;
7033 /* Begin the compound-statement. */
7034 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7035 /* If the next keyword is `__label__' we have a label declaration. */
7036 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7037 cp_parser_label_declaration (parser);
7038 /* Parse an (optional) statement-seq. */
7039 cp_parser_statement_seq_opt (parser, in_statement_expr);
7040 /* Finish the compound-statement. */
7041 finish_compound_stmt (compound_stmt);
7042 /* Consume the `}'. */
7043 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7045 return compound_stmt;
7048 /* Parse an (optional) statement-seq.
7052 statement-seq [opt] statement */
7055 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7057 /* Scan statements until there aren't any more. */
7060 cp_token *token = cp_lexer_peek_token (parser->lexer);
7062 /* If we're looking at a `}', then we've run out of statements. */
7063 if (token->type == CPP_CLOSE_BRACE
7064 || token->type == CPP_EOF
7065 || token->type == CPP_PRAGMA_EOL)
7068 /* If we are in a compound statement and find 'else' then
7069 something went wrong. */
7070 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7072 if (parser->in_statement & IN_IF_STMT)
7076 token = cp_lexer_consume_token (parser->lexer);
7077 error ("%H%<else%> without a previous %<if%>", &token->location);
7081 /* Parse the statement. */
7082 cp_parser_statement (parser, in_statement_expr, true, NULL);
7086 /* Parse a selection-statement.
7088 selection-statement:
7089 if ( condition ) statement
7090 if ( condition ) statement else statement
7091 switch ( condition ) statement
7093 Returns the new IF_STMT or SWITCH_STMT.
7095 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7096 is a (possibly labeled) if statement which is not enclosed in
7097 braces and has an else clause. This is used to implement
7101 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7109 /* Peek at the next token. */
7110 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7112 /* See what kind of keyword it is. */
7113 keyword = token->keyword;
7122 /* Look for the `('. */
7123 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7125 cp_parser_skip_to_end_of_statement (parser);
7126 return error_mark_node;
7129 /* Begin the selection-statement. */
7130 if (keyword == RID_IF)
7131 statement = begin_if_stmt ();
7133 statement = begin_switch_stmt ();
7135 /* Parse the condition. */
7136 condition = cp_parser_condition (parser);
7137 /* Look for the `)'. */
7138 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7139 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7140 /*consume_paren=*/true);
7142 if (keyword == RID_IF)
7145 unsigned char in_statement;
7147 /* Add the condition. */
7148 finish_if_stmt_cond (condition, statement);
7150 /* Parse the then-clause. */
7151 in_statement = parser->in_statement;
7152 parser->in_statement |= IN_IF_STMT;
7153 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7154 parser->in_statement = in_statement;
7156 finish_then_clause (statement);
7158 /* If the next token is `else', parse the else-clause. */
7159 if (cp_lexer_next_token_is_keyword (parser->lexer,
7162 /* Consume the `else' keyword. */
7163 cp_lexer_consume_token (parser->lexer);
7164 begin_else_clause (statement);
7165 /* Parse the else-clause. */
7166 cp_parser_implicitly_scoped_statement (parser, NULL);
7167 finish_else_clause (statement);
7169 /* If we are currently parsing a then-clause, then
7170 IF_P will not be NULL. We set it to true to
7171 indicate that this if statement has an else clause.
7172 This may trigger the Wparentheses warning below
7173 when we get back up to the parent if statement. */
7179 /* This if statement does not have an else clause. If
7180 NESTED_IF is true, then the then-clause is an if
7181 statement which does have an else clause. We warn
7182 about the potential ambiguity. */
7184 warning (OPT_Wparentheses,
7185 ("%Hsuggest explicit braces "
7186 "to avoid ambiguous %<else%>"),
7187 EXPR_LOCUS (statement));
7190 /* Now we're all done with the if-statement. */
7191 finish_if_stmt (statement);
7195 bool in_switch_statement_p;
7196 unsigned char in_statement;
7198 /* Add the condition. */
7199 finish_switch_cond (condition, statement);
7201 /* Parse the body of the switch-statement. */
7202 in_switch_statement_p = parser->in_switch_statement_p;
7203 in_statement = parser->in_statement;
7204 parser->in_switch_statement_p = true;
7205 parser->in_statement |= IN_SWITCH_STMT;
7206 cp_parser_implicitly_scoped_statement (parser, NULL);
7207 parser->in_switch_statement_p = in_switch_statement_p;
7208 parser->in_statement = in_statement;
7210 /* Now we're all done with the switch-statement. */
7211 finish_switch_stmt (statement);
7219 cp_parser_error (parser, "expected selection-statement");
7220 return error_mark_node;
7224 /* Parse a condition.
7228 type-specifier-seq declarator = initializer-clause
7229 type-specifier-seq declarator braced-init-list
7234 type-specifier-seq declarator asm-specification [opt]
7235 attributes [opt] = assignment-expression
7237 Returns the expression that should be tested. */
7240 cp_parser_condition (cp_parser* parser)
7242 cp_decl_specifier_seq type_specifiers;
7243 const char *saved_message;
7245 /* Try the declaration first. */
7246 cp_parser_parse_tentatively (parser);
7247 /* New types are not allowed in the type-specifier-seq for a
7249 saved_message = parser->type_definition_forbidden_message;
7250 parser->type_definition_forbidden_message
7251 = "types may not be defined in conditions";
7252 /* Parse the type-specifier-seq. */
7253 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7255 /* Restore the saved message. */
7256 parser->type_definition_forbidden_message = saved_message;
7257 /* If all is well, we might be looking at a declaration. */
7258 if (!cp_parser_error_occurred (parser))
7261 tree asm_specification;
7263 cp_declarator *declarator;
7264 tree initializer = NULL_TREE;
7266 /* Parse the declarator. */
7267 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7268 /*ctor_dtor_or_conv_p=*/NULL,
7269 /*parenthesized_p=*/NULL,
7270 /*member_p=*/false);
7271 /* Parse the attributes. */
7272 attributes = cp_parser_attributes_opt (parser);
7273 /* Parse the asm-specification. */
7274 asm_specification = cp_parser_asm_specification_opt (parser);
7275 /* If the next token is not an `=' or '{', then we might still be
7276 looking at an expression. For example:
7280 looks like a decl-specifier-seq and a declarator -- but then
7281 there is no `=', so this is an expression. */
7282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7283 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7284 cp_parser_simulate_error (parser);
7286 /* If we did see an `=' or '{', then we are looking at a declaration
7288 if (cp_parser_parse_definitely (parser))
7291 bool non_constant_p;
7292 bool flags = LOOKUP_ONLYCONVERTING;
7294 /* Create the declaration. */
7295 decl = start_decl (declarator, &type_specifiers,
7296 /*initialized_p=*/true,
7297 attributes, /*prefix_attributes=*/NULL_TREE,
7300 /* Parse the initializer. */
7301 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7303 initializer = cp_parser_braced_list (parser, &non_constant_p);
7304 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7309 /* Consume the `='. */
7310 cp_lexer_consume_token (parser->lexer);
7311 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7313 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7314 maybe_warn_cpp0x ("extended initializer lists");
7316 if (!non_constant_p)
7317 initializer = fold_non_dependent_expr (initializer);
7319 /* Process the initializer. */
7320 cp_finish_decl (decl,
7321 initializer, !non_constant_p,
7326 pop_scope (pushed_scope);
7328 return convert_from_reference (decl);
7331 /* If we didn't even get past the declarator successfully, we are
7332 definitely not looking at a declaration. */
7334 cp_parser_abort_tentative_parse (parser);
7336 /* Otherwise, we are looking at an expression. */
7337 return cp_parser_expression (parser, /*cast_p=*/false);
7340 /* We check for a ) immediately followed by ; with no whitespacing
7341 between. This is used to issue a warning for:
7349 as the semicolon is probably extraneous.
7351 On parse errors, the next token might not be a ), so do nothing in
7355 check_empty_body (cp_parser* parser, const char* type)
7358 cp_token *close_paren;
7359 expanded_location close_loc;
7360 expanded_location semi_loc;
7362 close_paren = cp_lexer_peek_token (parser->lexer);
7363 if (close_paren->type != CPP_CLOSE_PAREN)
7366 close_loc = expand_location (close_paren->location);
7367 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7369 if (token->type != CPP_SEMICOLON
7370 || (token->flags & PREV_WHITE))
7373 semi_loc = expand_location (token->location);
7374 if (close_loc.line == semi_loc.line
7375 && close_loc.column+1 == semi_loc.column)
7376 warning (OPT_Wempty_body,
7377 "suggest a space before %<;%> or explicit braces around empty "
7378 "body in %<%s%> statement",
7382 /* Parse an iteration-statement.
7384 iteration-statement:
7385 while ( condition ) statement
7386 do statement while ( expression ) ;
7387 for ( for-init-statement condition [opt] ; expression [opt] )
7390 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7393 cp_parser_iteration_statement (cp_parser* parser)
7398 unsigned char in_statement;
7400 /* Peek at the next token. */
7401 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7403 return error_mark_node;
7405 /* Remember whether or not we are already within an iteration
7407 in_statement = parser->in_statement;
7409 /* See what kind of keyword it is. */
7410 keyword = token->keyword;
7417 /* Begin the while-statement. */
7418 statement = begin_while_stmt ();
7419 /* Look for the `('. */
7420 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7421 /* Parse the condition. */
7422 condition = cp_parser_condition (parser);
7423 finish_while_stmt_cond (condition, statement);
7424 check_empty_body (parser, "while");
7425 /* Look for the `)'. */
7426 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7427 /* Parse the dependent statement. */
7428 parser->in_statement = IN_ITERATION_STMT;
7429 cp_parser_already_scoped_statement (parser);
7430 parser->in_statement = in_statement;
7431 /* We're done with the while-statement. */
7432 finish_while_stmt (statement);
7440 /* Begin the do-statement. */
7441 statement = begin_do_stmt ();
7442 /* Parse the body of the do-statement. */
7443 parser->in_statement = IN_ITERATION_STMT;
7444 cp_parser_implicitly_scoped_statement (parser, NULL);
7445 parser->in_statement = in_statement;
7446 finish_do_body (statement);
7447 /* Look for the `while' keyword. */
7448 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7449 /* Look for the `('. */
7450 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7451 /* Parse the expression. */
7452 expression = cp_parser_expression (parser, /*cast_p=*/false);
7453 /* We're done with the do-statement. */
7454 finish_do_stmt (expression, statement);
7455 /* Look for the `)'. */
7456 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7457 /* Look for the `;'. */
7458 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7464 tree condition = NULL_TREE;
7465 tree expression = NULL_TREE;
7467 /* Begin the for-statement. */
7468 statement = begin_for_stmt ();
7469 /* Look for the `('. */
7470 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7471 /* Parse the initialization. */
7472 cp_parser_for_init_statement (parser);
7473 finish_for_init_stmt (statement);
7475 /* If there's a condition, process it. */
7476 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7477 condition = cp_parser_condition (parser);
7478 finish_for_cond (condition, statement);
7479 /* Look for the `;'. */
7480 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7482 /* If there's an expression, process it. */
7483 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7484 expression = cp_parser_expression (parser, /*cast_p=*/false);
7485 finish_for_expr (expression, statement);
7486 check_empty_body (parser, "for");
7487 /* Look for the `)'. */
7488 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7490 /* Parse the body of the for-statement. */
7491 parser->in_statement = IN_ITERATION_STMT;
7492 cp_parser_already_scoped_statement (parser);
7493 parser->in_statement = in_statement;
7495 /* We're done with the for-statement. */
7496 finish_for_stmt (statement);
7501 cp_parser_error (parser, "expected iteration-statement");
7502 statement = error_mark_node;
7509 /* Parse a for-init-statement.
7512 expression-statement
7513 simple-declaration */
7516 cp_parser_for_init_statement (cp_parser* parser)
7518 /* If the next token is a `;', then we have an empty
7519 expression-statement. Grammatically, this is also a
7520 simple-declaration, but an invalid one, because it does not
7521 declare anything. Therefore, if we did not handle this case
7522 specially, we would issue an error message about an invalid
7524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7526 /* We're going to speculatively look for a declaration, falling back
7527 to an expression, if necessary. */
7528 cp_parser_parse_tentatively (parser);
7529 /* Parse the declaration. */
7530 cp_parser_simple_declaration (parser,
7531 /*function_definition_allowed_p=*/false);
7532 /* If the tentative parse failed, then we shall need to look for an
7533 expression-statement. */
7534 if (cp_parser_parse_definitely (parser))
7538 cp_parser_expression_statement (parser, false);
7541 /* Parse a jump-statement.
7546 return expression [opt] ;
7547 return braced-init-list ;
7555 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7558 cp_parser_jump_statement (cp_parser* parser)
7560 tree statement = error_mark_node;
7563 unsigned char in_statement;
7565 /* Peek at the next token. */
7566 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7568 return error_mark_node;
7570 /* See what kind of keyword it is. */
7571 keyword = token->keyword;
7575 in_statement = parser->in_statement & ~IN_IF_STMT;
7576 switch (in_statement)
7579 error ("%Hbreak statement not within loop or switch", &token->location);
7582 gcc_assert ((in_statement & IN_SWITCH_STMT)
7583 || in_statement == IN_ITERATION_STMT);
7584 statement = finish_break_stmt ();
7587 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7590 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7593 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7597 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7600 error ("%Hcontinue statement not within a loop", &token->location);
7602 case IN_ITERATION_STMT:
7604 statement = finish_continue_stmt ();
7607 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7612 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7618 bool expr_non_constant_p;
7620 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7622 maybe_warn_cpp0x ("extended initializer lists");
7623 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7625 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7626 expr = cp_parser_expression (parser, /*cast_p=*/false);
7628 /* If the next token is a `;', then there is no
7631 /* Build the return-statement. */
7632 statement = finish_return_stmt (expr);
7633 /* Look for the final `;'. */
7634 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7639 /* Create the goto-statement. */
7640 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7642 /* Issue a warning about this use of a GNU extension. */
7643 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7644 /* Consume the '*' token. */
7645 cp_lexer_consume_token (parser->lexer);
7646 /* Parse the dependent expression. */
7647 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7650 finish_goto_stmt (cp_parser_identifier (parser));
7651 /* Look for the final `;'. */
7652 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7656 cp_parser_error (parser, "expected jump-statement");
7663 /* Parse a declaration-statement.
7665 declaration-statement:
7666 block-declaration */
7669 cp_parser_declaration_statement (cp_parser* parser)
7673 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7674 p = obstack_alloc (&declarator_obstack, 0);
7676 /* Parse the block-declaration. */
7677 cp_parser_block_declaration (parser, /*statement_p=*/true);
7679 /* Free any declarators allocated. */
7680 obstack_free (&declarator_obstack, p);
7682 /* Finish off the statement. */
7686 /* Some dependent statements (like `if (cond) statement'), are
7687 implicitly in their own scope. In other words, if the statement is
7688 a single statement (as opposed to a compound-statement), it is
7689 none-the-less treated as if it were enclosed in braces. Any
7690 declarations appearing in the dependent statement are out of scope
7691 after control passes that point. This function parses a statement,
7692 but ensures that is in its own scope, even if it is not a
7695 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7696 is a (possibly labeled) if statement which is not enclosed in
7697 braces and has an else clause. This is used to implement
7700 Returns the new statement. */
7703 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7710 /* Mark if () ; with a special NOP_EXPR. */
7711 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7713 cp_lexer_consume_token (parser->lexer);
7714 statement = add_stmt (build_empty_stmt ());
7716 /* if a compound is opened, we simply parse the statement directly. */
7717 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7718 statement = cp_parser_compound_statement (parser, NULL, false);
7719 /* If the token is not a `{', then we must take special action. */
7722 /* Create a compound-statement. */
7723 statement = begin_compound_stmt (0);
7724 /* Parse the dependent-statement. */
7725 cp_parser_statement (parser, NULL_TREE, false, if_p);
7726 /* Finish the dummy compound-statement. */
7727 finish_compound_stmt (statement);
7730 /* Return the statement. */
7734 /* For some dependent statements (like `while (cond) statement'), we
7735 have already created a scope. Therefore, even if the dependent
7736 statement is a compound-statement, we do not want to create another
7740 cp_parser_already_scoped_statement (cp_parser* parser)
7742 /* If the token is a `{', then we must take special action. */
7743 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7744 cp_parser_statement (parser, NULL_TREE, false, NULL);
7747 /* Avoid calling cp_parser_compound_statement, so that we
7748 don't create a new scope. Do everything else by hand. */
7749 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7750 cp_parser_statement_seq_opt (parser, NULL_TREE);
7751 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7755 /* Declarations [gram.dcl.dcl] */
7757 /* Parse an optional declaration-sequence.
7761 declaration-seq declaration */
7764 cp_parser_declaration_seq_opt (cp_parser* parser)
7770 token = cp_lexer_peek_token (parser->lexer);
7772 if (token->type == CPP_CLOSE_BRACE
7773 || token->type == CPP_EOF
7774 || token->type == CPP_PRAGMA_EOL)
7777 if (token->type == CPP_SEMICOLON)
7779 /* A declaration consisting of a single semicolon is
7780 invalid. Allow it unless we're being pedantic. */
7781 cp_lexer_consume_token (parser->lexer);
7782 if (!in_system_header)
7783 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7787 /* If we're entering or exiting a region that's implicitly
7788 extern "C", modify the lang context appropriately. */
7789 if (!parser->implicit_extern_c && token->implicit_extern_c)
7791 push_lang_context (lang_name_c);
7792 parser->implicit_extern_c = true;
7794 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7796 pop_lang_context ();
7797 parser->implicit_extern_c = false;
7800 if (token->type == CPP_PRAGMA)
7802 /* A top-level declaration can consist solely of a #pragma.
7803 A nested declaration cannot, so this is done here and not
7804 in cp_parser_declaration. (A #pragma at block scope is
7805 handled in cp_parser_statement.) */
7806 cp_parser_pragma (parser, pragma_external);
7810 /* Parse the declaration itself. */
7811 cp_parser_declaration (parser);
7815 /* Parse a declaration.
7820 template-declaration
7821 explicit-instantiation
7822 explicit-specialization
7823 linkage-specification
7824 namespace-definition
7829 __extension__ declaration */
7832 cp_parser_declaration (cp_parser* parser)
7839 /* Check for the `__extension__' keyword. */
7840 if (cp_parser_extension_opt (parser, &saved_pedantic))
7842 /* Parse the qualified declaration. */
7843 cp_parser_declaration (parser);
7844 /* Restore the PEDANTIC flag. */
7845 pedantic = saved_pedantic;
7850 /* Try to figure out what kind of declaration is present. */
7851 token1 = *cp_lexer_peek_token (parser->lexer);
7853 if (token1.type != CPP_EOF)
7854 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7857 token2.type = CPP_EOF;
7858 token2.keyword = RID_MAX;
7861 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7862 p = obstack_alloc (&declarator_obstack, 0);
7864 /* If the next token is `extern' and the following token is a string
7865 literal, then we have a linkage specification. */
7866 if (token1.keyword == RID_EXTERN
7867 && cp_parser_is_string_literal (&token2))
7868 cp_parser_linkage_specification (parser);
7869 /* If the next token is `template', then we have either a template
7870 declaration, an explicit instantiation, or an explicit
7872 else if (token1.keyword == RID_TEMPLATE)
7874 /* `template <>' indicates a template specialization. */
7875 if (token2.type == CPP_LESS
7876 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7877 cp_parser_explicit_specialization (parser);
7878 /* `template <' indicates a template declaration. */
7879 else if (token2.type == CPP_LESS)
7880 cp_parser_template_declaration (parser, /*member_p=*/false);
7881 /* Anything else must be an explicit instantiation. */
7883 cp_parser_explicit_instantiation (parser);
7885 /* If the next token is `export', then we have a template
7887 else if (token1.keyword == RID_EXPORT)
7888 cp_parser_template_declaration (parser, /*member_p=*/false);
7889 /* If the next token is `extern', 'static' or 'inline' and the one
7890 after that is `template', we have a GNU extended explicit
7891 instantiation directive. */
7892 else if (cp_parser_allow_gnu_extensions_p (parser)
7893 && (token1.keyword == RID_EXTERN
7894 || token1.keyword == RID_STATIC
7895 || token1.keyword == RID_INLINE)
7896 && token2.keyword == RID_TEMPLATE)
7897 cp_parser_explicit_instantiation (parser);
7898 /* If the next token is `namespace', check for a named or unnamed
7899 namespace definition. */
7900 else if (token1.keyword == RID_NAMESPACE
7901 && (/* A named namespace definition. */
7902 (token2.type == CPP_NAME
7903 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7905 /* An unnamed namespace definition. */
7906 || token2.type == CPP_OPEN_BRACE
7907 || token2.keyword == RID_ATTRIBUTE))
7908 cp_parser_namespace_definition (parser);
7909 /* An inline (associated) namespace definition. */
7910 else if (token1.keyword == RID_INLINE
7911 && token2.keyword == RID_NAMESPACE)
7912 cp_parser_namespace_definition (parser);
7913 /* Objective-C++ declaration/definition. */
7914 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7915 cp_parser_objc_declaration (parser);
7916 /* We must have either a block declaration or a function
7919 /* Try to parse a block-declaration, or a function-definition. */
7920 cp_parser_block_declaration (parser, /*statement_p=*/false);
7922 /* Free any declarators allocated. */
7923 obstack_free (&declarator_obstack, p);
7926 /* Parse a block-declaration.
7931 namespace-alias-definition
7938 __extension__ block-declaration
7943 static_assert-declaration
7945 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7946 part of a declaration-statement. */
7949 cp_parser_block_declaration (cp_parser *parser,
7955 /* Check for the `__extension__' keyword. */
7956 if (cp_parser_extension_opt (parser, &saved_pedantic))
7958 /* Parse the qualified declaration. */
7959 cp_parser_block_declaration (parser, statement_p);
7960 /* Restore the PEDANTIC flag. */
7961 pedantic = saved_pedantic;
7966 /* Peek at the next token to figure out which kind of declaration is
7968 token1 = cp_lexer_peek_token (parser->lexer);
7970 /* If the next keyword is `asm', we have an asm-definition. */
7971 if (token1->keyword == RID_ASM)
7974 cp_parser_commit_to_tentative_parse (parser);
7975 cp_parser_asm_definition (parser);
7977 /* If the next keyword is `namespace', we have a
7978 namespace-alias-definition. */
7979 else if (token1->keyword == RID_NAMESPACE)
7980 cp_parser_namespace_alias_definition (parser);
7981 /* If the next keyword is `using', we have either a
7982 using-declaration or a using-directive. */
7983 else if (token1->keyword == RID_USING)
7988 cp_parser_commit_to_tentative_parse (parser);
7989 /* If the token after `using' is `namespace', then we have a
7991 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7992 if (token2->keyword == RID_NAMESPACE)
7993 cp_parser_using_directive (parser);
7994 /* Otherwise, it's a using-declaration. */
7996 cp_parser_using_declaration (parser,
7997 /*access_declaration_p=*/false);
7999 /* If the next keyword is `__label__' we have a misplaced label
8001 else if (token1->keyword == RID_LABEL)
8003 cp_lexer_consume_token (parser->lexer);
8004 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8005 cp_parser_skip_to_end_of_statement (parser);
8006 /* If the next token is now a `;', consume it. */
8007 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8008 cp_lexer_consume_token (parser->lexer);
8010 /* If the next token is `static_assert' we have a static assertion. */
8011 else if (token1->keyword == RID_STATIC_ASSERT)
8012 cp_parser_static_assert (parser, /*member_p=*/false);
8013 /* Anything else must be a simple-declaration. */
8015 cp_parser_simple_declaration (parser, !statement_p);
8018 /* Parse a simple-declaration.
8021 decl-specifier-seq [opt] init-declarator-list [opt] ;
8023 init-declarator-list:
8025 init-declarator-list , init-declarator
8027 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8028 function-definition as a simple-declaration. */
8031 cp_parser_simple_declaration (cp_parser* parser,
8032 bool function_definition_allowed_p)
8034 cp_decl_specifier_seq decl_specifiers;
8035 int declares_class_or_enum;
8036 bool saw_declarator;
8038 /* Defer access checks until we know what is being declared; the
8039 checks for names appearing in the decl-specifier-seq should be
8040 done as if we were in the scope of the thing being declared. */
8041 push_deferring_access_checks (dk_deferred);
8043 /* Parse the decl-specifier-seq. We have to keep track of whether
8044 or not the decl-specifier-seq declares a named class or
8045 enumeration type, since that is the only case in which the
8046 init-declarator-list is allowed to be empty.
8050 In a simple-declaration, the optional init-declarator-list can be
8051 omitted only when declaring a class or enumeration, that is when
8052 the decl-specifier-seq contains either a class-specifier, an
8053 elaborated-type-specifier, or an enum-specifier. */
8054 cp_parser_decl_specifier_seq (parser,
8055 CP_PARSER_FLAGS_OPTIONAL,
8057 &declares_class_or_enum);
8058 /* We no longer need to defer access checks. */
8059 stop_deferring_access_checks ();
8061 /* In a block scope, a valid declaration must always have a
8062 decl-specifier-seq. By not trying to parse declarators, we can
8063 resolve the declaration/expression ambiguity more quickly. */
8064 if (!function_definition_allowed_p
8065 && !decl_specifiers.any_specifiers_p)
8067 cp_parser_error (parser, "expected declaration");
8071 /* If the next two tokens are both identifiers, the code is
8072 erroneous. The usual cause of this situation is code like:
8076 where "T" should name a type -- but does not. */
8077 if (!decl_specifiers.type
8078 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8080 /* If parsing tentatively, we should commit; we really are
8081 looking at a declaration. */
8082 cp_parser_commit_to_tentative_parse (parser);
8087 /* If we have seen at least one decl-specifier, and the next token
8088 is not a parenthesis, then we must be looking at a declaration.
8089 (After "int (" we might be looking at a functional cast.) */
8090 if (decl_specifiers.any_specifiers_p
8091 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8092 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8093 cp_parser_commit_to_tentative_parse (parser);
8095 /* Keep going until we hit the `;' at the end of the simple
8097 saw_declarator = false;
8098 while (cp_lexer_next_token_is_not (parser->lexer,
8102 bool function_definition_p;
8107 /* If we are processing next declarator, coma is expected */
8108 token = cp_lexer_peek_token (parser->lexer);
8109 gcc_assert (token->type == CPP_COMMA);
8110 cp_lexer_consume_token (parser->lexer);
8113 saw_declarator = true;
8115 /* Parse the init-declarator. */
8116 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8118 function_definition_allowed_p,
8120 declares_class_or_enum,
8121 &function_definition_p);
8122 /* If an error occurred while parsing tentatively, exit quickly.
8123 (That usually happens when in the body of a function; each
8124 statement is treated as a declaration-statement until proven
8126 if (cp_parser_error_occurred (parser))
8128 /* Handle function definitions specially. */
8129 if (function_definition_p)
8131 /* If the next token is a `,', then we are probably
8132 processing something like:
8136 which is erroneous. */
8137 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8139 cp_token *token = cp_lexer_peek_token (parser->lexer);
8140 error ("%Hmixing declarations and function-definitions is forbidden",
8143 /* Otherwise, we're done with the list of declarators. */
8146 pop_deferring_access_checks ();
8150 /* The next token should be either a `,' or a `;'. */
8151 token = cp_lexer_peek_token (parser->lexer);
8152 /* If it's a `,', there are more declarators to come. */
8153 if (token->type == CPP_COMMA)
8154 /* will be consumed next time around */;
8155 /* If it's a `;', we are done. */
8156 else if (token->type == CPP_SEMICOLON)
8158 /* Anything else is an error. */
8161 /* If we have already issued an error message we don't need
8162 to issue another one. */
8163 if (decl != error_mark_node
8164 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8165 cp_parser_error (parser, "expected %<,%> or %<;%>");
8166 /* Skip tokens until we reach the end of the statement. */
8167 cp_parser_skip_to_end_of_statement (parser);
8168 /* If the next token is now a `;', consume it. */
8169 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8170 cp_lexer_consume_token (parser->lexer);
8173 /* After the first time around, a function-definition is not
8174 allowed -- even if it was OK at first. For example:
8179 function_definition_allowed_p = false;
8182 /* Issue an error message if no declarators are present, and the
8183 decl-specifier-seq does not itself declare a class or
8185 if (!saw_declarator)
8187 if (cp_parser_declares_only_class_p (parser))
8188 shadow_tag (&decl_specifiers);
8189 /* Perform any deferred access checks. */
8190 perform_deferred_access_checks ();
8193 /* Consume the `;'. */
8194 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8197 pop_deferring_access_checks ();
8200 /* Parse a decl-specifier-seq.
8203 decl-specifier-seq [opt] decl-specifier
8206 storage-class-specifier
8217 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8219 The parser flags FLAGS is used to control type-specifier parsing.
8221 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8224 1: one of the decl-specifiers is an elaborated-type-specifier
8225 (i.e., a type declaration)
8226 2: one of the decl-specifiers is an enum-specifier or a
8227 class-specifier (i.e., a type definition)
8232 cp_parser_decl_specifier_seq (cp_parser* parser,
8233 cp_parser_flags flags,
8234 cp_decl_specifier_seq *decl_specs,
8235 int* declares_class_or_enum)
8237 bool constructor_possible_p = !parser->in_declarator_p;
8238 cp_token *start_token = NULL;
8240 /* Clear DECL_SPECS. */
8241 clear_decl_specs (decl_specs);
8243 /* Assume no class or enumeration type is declared. */
8244 *declares_class_or_enum = 0;
8246 /* Keep reading specifiers until there are no more to read. */
8250 bool found_decl_spec;
8253 /* Peek at the next token. */
8254 token = cp_lexer_peek_token (parser->lexer);
8256 /* Save the first token of the decl spec list for error
8259 start_token = token;
8260 /* Handle attributes. */
8261 if (token->keyword == RID_ATTRIBUTE)
8263 /* Parse the attributes. */
8264 decl_specs->attributes
8265 = chainon (decl_specs->attributes,
8266 cp_parser_attributes_opt (parser));
8269 /* Assume we will find a decl-specifier keyword. */
8270 found_decl_spec = true;
8271 /* If the next token is an appropriate keyword, we can simply
8272 add it to the list. */
8273 switch (token->keyword)
8278 if (!at_class_scope_p ())
8280 error ("%H%<friend%> used outside of class", &token->location);
8281 cp_lexer_purge_token (parser->lexer);
8285 ++decl_specs->specs[(int) ds_friend];
8286 /* Consume the token. */
8287 cp_lexer_consume_token (parser->lexer);
8291 /* function-specifier:
8298 cp_parser_function_specifier_opt (parser, decl_specs);
8304 ++decl_specs->specs[(int) ds_typedef];
8305 /* Consume the token. */
8306 cp_lexer_consume_token (parser->lexer);
8307 /* A constructor declarator cannot appear in a typedef. */
8308 constructor_possible_p = false;
8309 /* The "typedef" keyword can only occur in a declaration; we
8310 may as well commit at this point. */
8311 cp_parser_commit_to_tentative_parse (parser);
8313 if (decl_specs->storage_class != sc_none)
8314 decl_specs->conflicting_specifiers_p = true;
8317 /* storage-class-specifier:
8327 /* Consume the token. */
8328 cp_lexer_consume_token (parser->lexer);
8330 if (cxx_dialect == cxx98)
8332 /* Complain about `auto' as a storage specifier, if
8333 we're complaining about C++0x compatibility. */
8336 "%H%<auto%> will change meaning in C++0x; please remove it",
8339 /* Set the storage class anyway. */
8340 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8344 /* We do not yet support the use of `auto' as a
8346 error ("%HC++0x %<auto%> specifier not supported", &token->location);
8353 /* Consume the token. */
8354 cp_lexer_consume_token (parser->lexer);
8355 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8359 /* Consume the token. */
8360 cp_lexer_consume_token (parser->lexer);
8361 ++decl_specs->specs[(int) ds_thread];
8365 /* We did not yet find a decl-specifier yet. */
8366 found_decl_spec = false;
8370 /* Constructors are a special case. The `S' in `S()' is not a
8371 decl-specifier; it is the beginning of the declarator. */
8374 && constructor_possible_p
8375 && (cp_parser_constructor_declarator_p
8376 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8378 /* If we don't have a DECL_SPEC yet, then we must be looking at
8379 a type-specifier. */
8380 if (!found_decl_spec && !constructor_p)
8382 int decl_spec_declares_class_or_enum;
8383 bool is_cv_qualifier;
8387 = cp_parser_type_specifier (parser, flags,
8389 /*is_declaration=*/true,
8390 &decl_spec_declares_class_or_enum,
8392 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8394 /* If this type-specifier referenced a user-defined type
8395 (a typedef, class-name, etc.), then we can't allow any
8396 more such type-specifiers henceforth.
8400 The longest sequence of decl-specifiers that could
8401 possibly be a type name is taken as the
8402 decl-specifier-seq of a declaration. The sequence shall
8403 be self-consistent as described below.
8407 As a general rule, at most one type-specifier is allowed
8408 in the complete decl-specifier-seq of a declaration. The
8409 only exceptions are the following:
8411 -- const or volatile can be combined with any other
8414 -- signed or unsigned can be combined with char, long,
8422 void g (const int Pc);
8424 Here, Pc is *not* part of the decl-specifier seq; it's
8425 the declarator. Therefore, once we see a type-specifier
8426 (other than a cv-qualifier), we forbid any additional
8427 user-defined types. We *do* still allow things like `int
8428 int' to be considered a decl-specifier-seq, and issue the
8429 error message later. */
8430 if (type_spec && !is_cv_qualifier)
8431 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8432 /* A constructor declarator cannot follow a type-specifier. */
8435 constructor_possible_p = false;
8436 found_decl_spec = true;
8440 /* If we still do not have a DECL_SPEC, then there are no more
8442 if (!found_decl_spec)
8445 decl_specs->any_specifiers_p = true;
8446 /* After we see one decl-specifier, further decl-specifiers are
8448 flags |= CP_PARSER_FLAGS_OPTIONAL;
8451 cp_parser_check_decl_spec (decl_specs, start_token->location);
8453 /* Don't allow a friend specifier with a class definition. */
8454 if (decl_specs->specs[(int) ds_friend] != 0
8455 && (*declares_class_or_enum & 2))
8456 error ("%Hclass definition may not be declared a friend",
8457 &start_token->location);
8460 /* Parse an (optional) storage-class-specifier.
8462 storage-class-specifier:
8471 storage-class-specifier:
8474 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8477 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8479 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8482 if (cxx_dialect != cxx98)
8484 /* Fall through for C++98. */
8491 /* Consume the token. */
8492 return cp_lexer_consume_token (parser->lexer)->u.value;
8499 /* Parse an (optional) function-specifier.
8506 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8507 Updates DECL_SPECS, if it is non-NULL. */
8510 cp_parser_function_specifier_opt (cp_parser* parser,
8511 cp_decl_specifier_seq *decl_specs)
8513 cp_token *token = cp_lexer_peek_token (parser->lexer);
8514 switch (token->keyword)
8518 ++decl_specs->specs[(int) ds_inline];
8522 /* 14.5.2.3 [temp.mem]
8524 A member function template shall not be virtual. */
8525 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8526 error ("%Htemplates may not be %<virtual%>", &token->location);
8527 else if (decl_specs)
8528 ++decl_specs->specs[(int) ds_virtual];
8533 ++decl_specs->specs[(int) ds_explicit];
8540 /* Consume the token. */
8541 return cp_lexer_consume_token (parser->lexer)->u.value;
8544 /* Parse a linkage-specification.
8546 linkage-specification:
8547 extern string-literal { declaration-seq [opt] }
8548 extern string-literal declaration */
8551 cp_parser_linkage_specification (cp_parser* parser)
8555 /* Look for the `extern' keyword. */
8556 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8558 /* Look for the string-literal. */
8559 linkage = cp_parser_string_literal (parser, false, false);
8561 /* Transform the literal into an identifier. If the literal is a
8562 wide-character string, or contains embedded NULs, then we can't
8563 handle it as the user wants. */
8564 if (strlen (TREE_STRING_POINTER (linkage))
8565 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8567 cp_parser_error (parser, "invalid linkage-specification");
8568 /* Assume C++ linkage. */
8569 linkage = lang_name_cplusplus;
8572 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8574 /* We're now using the new linkage. */
8575 push_lang_context (linkage);
8577 /* If the next token is a `{', then we're using the first
8579 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8581 /* Consume the `{' token. */
8582 cp_lexer_consume_token (parser->lexer);
8583 /* Parse the declarations. */
8584 cp_parser_declaration_seq_opt (parser);
8585 /* Look for the closing `}'. */
8586 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8588 /* Otherwise, there's just one declaration. */
8591 bool saved_in_unbraced_linkage_specification_p;
8593 saved_in_unbraced_linkage_specification_p
8594 = parser->in_unbraced_linkage_specification_p;
8595 parser->in_unbraced_linkage_specification_p = true;
8596 cp_parser_declaration (parser);
8597 parser->in_unbraced_linkage_specification_p
8598 = saved_in_unbraced_linkage_specification_p;
8601 /* We're done with the linkage-specification. */
8602 pop_lang_context ();
8605 /* Parse a static_assert-declaration.
8607 static_assert-declaration:
8608 static_assert ( constant-expression , string-literal ) ;
8610 If MEMBER_P, this static_assert is a class member. */
8613 cp_parser_static_assert(cp_parser *parser, bool member_p)
8618 location_t saved_loc;
8620 /* Peek at the `static_assert' token so we can keep track of exactly
8621 where the static assertion started. */
8622 token = cp_lexer_peek_token (parser->lexer);
8623 saved_loc = token->location;
8625 /* Look for the `static_assert' keyword. */
8626 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8627 "%<static_assert%>"))
8630 /* We know we are in a static assertion; commit to any tentative
8632 if (cp_parser_parsing_tentatively (parser))
8633 cp_parser_commit_to_tentative_parse (parser);
8635 /* Parse the `(' starting the static assertion condition. */
8636 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8638 /* Parse the constant-expression. */
8640 cp_parser_constant_expression (parser,
8641 /*allow_non_constant_p=*/false,
8642 /*non_constant_p=*/NULL);
8644 /* Parse the separating `,'. */
8645 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8647 /* Parse the string-literal message. */
8648 message = cp_parser_string_literal (parser,
8649 /*translate=*/false,
8652 /* A `)' completes the static assertion. */
8653 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8654 cp_parser_skip_to_closing_parenthesis (parser,
8655 /*recovering=*/true,
8657 /*consume_paren=*/true);
8659 /* A semicolon terminates the declaration. */
8660 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8662 /* Complete the static assertion, which may mean either processing
8663 the static assert now or saving it for template instantiation. */
8664 finish_static_assert (condition, message, saved_loc, member_p);
8667 /* Parse a `decltype' type. Returns the type.
8669 simple-type-specifier:
8670 decltype ( expression ) */
8673 cp_parser_decltype (cp_parser *parser)
8676 bool id_expression_or_member_access_p = false;
8677 const char *saved_message;
8678 bool saved_integral_constant_expression_p;
8679 bool saved_non_integral_constant_expression_p;
8680 cp_token *id_expr_start_token;
8682 /* Look for the `decltype' token. */
8683 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8684 return error_mark_node;
8686 /* Types cannot be defined in a `decltype' expression. Save away the
8688 saved_message = parser->type_definition_forbidden_message;
8690 /* And create the new one. */
8691 parser->type_definition_forbidden_message
8692 = "types may not be defined in %<decltype%> expressions";
8694 /* The restrictions on constant-expressions do not apply inside
8695 decltype expressions. */
8696 saved_integral_constant_expression_p
8697 = parser->integral_constant_expression_p;
8698 saved_non_integral_constant_expression_p
8699 = parser->non_integral_constant_expression_p;
8700 parser->integral_constant_expression_p = false;
8702 /* Do not actually evaluate the expression. */
8705 /* Parse the opening `('. */
8706 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8707 return error_mark_node;
8709 /* First, try parsing an id-expression. */
8710 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8711 cp_parser_parse_tentatively (parser);
8712 expr = cp_parser_id_expression (parser,
8713 /*template_keyword_p=*/false,
8714 /*check_dependency_p=*/true,
8715 /*template_p=*/NULL,
8716 /*declarator_p=*/false,
8717 /*optional_p=*/false);
8719 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8721 bool non_integral_constant_expression_p = false;
8722 tree id_expression = expr;
8724 const char *error_msg;
8726 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8727 /* Lookup the name we got back from the id-expression. */
8728 expr = cp_parser_lookup_name (parser, expr,
8730 /*is_template=*/false,
8731 /*is_namespace=*/false,
8732 /*check_dependency=*/true,
8733 /*ambiguous_decls=*/NULL,
8734 id_expr_start_token->location);
8737 && expr != error_mark_node
8738 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8739 && TREE_CODE (expr) != TYPE_DECL
8740 && (TREE_CODE (expr) != BIT_NOT_EXPR
8741 || !TYPE_P (TREE_OPERAND (expr, 0)))
8742 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8744 /* Complete lookup of the id-expression. */
8745 expr = (finish_id_expression
8746 (id_expression, expr, parser->scope, &idk,
8747 /*integral_constant_expression_p=*/false,
8748 /*allow_non_integral_constant_expression_p=*/true,
8749 &non_integral_constant_expression_p,
8750 /*template_p=*/false,
8752 /*address_p=*/false,
8753 /*template_arg_p=*/false,
8755 id_expr_start_token->location));
8757 if (expr == error_mark_node)
8758 /* We found an id-expression, but it was something that we
8759 should not have found. This is an error, not something
8760 we can recover from, so note that we found an
8761 id-expression and we'll recover as gracefully as
8763 id_expression_or_member_access_p = true;
8767 && expr != error_mark_node
8768 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8769 /* We have an id-expression. */
8770 id_expression_or_member_access_p = true;
8773 if (!id_expression_or_member_access_p)
8775 /* Abort the id-expression parse. */
8776 cp_parser_abort_tentative_parse (parser);
8778 /* Parsing tentatively, again. */
8779 cp_parser_parse_tentatively (parser);
8781 /* Parse a class member access. */
8782 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8784 /*member_access_only_p=*/true);
8787 && expr != error_mark_node
8788 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8789 /* We have an id-expression. */
8790 id_expression_or_member_access_p = true;
8793 if (id_expression_or_member_access_p)
8794 /* We have parsed the complete id-expression or member access. */
8795 cp_parser_parse_definitely (parser);
8798 /* Abort our attempt to parse an id-expression or member access
8800 cp_parser_abort_tentative_parse (parser);
8802 /* Parse a full expression. */
8803 expr = cp_parser_expression (parser, /*cast_p=*/false);
8806 /* Go back to evaluating expressions. */
8809 /* Restore the old message and the integral constant expression
8811 parser->type_definition_forbidden_message = saved_message;
8812 parser->integral_constant_expression_p
8813 = saved_integral_constant_expression_p;
8814 parser->non_integral_constant_expression_p
8815 = saved_non_integral_constant_expression_p;
8817 if (expr == error_mark_node)
8819 /* Skip everything up to the closing `)'. */
8820 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8821 /*consume_paren=*/true);
8822 return error_mark_node;
8825 /* Parse to the closing `)'. */
8826 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8828 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8829 /*consume_paren=*/true);
8830 return error_mark_node;
8833 return finish_decltype_type (expr, id_expression_or_member_access_p);
8836 /* Special member functions [gram.special] */
8838 /* Parse a conversion-function-id.
8840 conversion-function-id:
8841 operator conversion-type-id
8843 Returns an IDENTIFIER_NODE representing the operator. */
8846 cp_parser_conversion_function_id (cp_parser* parser)
8850 tree saved_qualifying_scope;
8851 tree saved_object_scope;
8852 tree pushed_scope = NULL_TREE;
8854 /* Look for the `operator' token. */
8855 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8856 return error_mark_node;
8857 /* When we parse the conversion-type-id, the current scope will be
8858 reset. However, we need that information in able to look up the
8859 conversion function later, so we save it here. */
8860 saved_scope = parser->scope;
8861 saved_qualifying_scope = parser->qualifying_scope;
8862 saved_object_scope = parser->object_scope;
8863 /* We must enter the scope of the class so that the names of
8864 entities declared within the class are available in the
8865 conversion-type-id. For example, consider:
8872 S::operator I() { ... }
8874 In order to see that `I' is a type-name in the definition, we
8875 must be in the scope of `S'. */
8877 pushed_scope = push_scope (saved_scope);
8878 /* Parse the conversion-type-id. */
8879 type = cp_parser_conversion_type_id (parser);
8880 /* Leave the scope of the class, if any. */
8882 pop_scope (pushed_scope);
8883 /* Restore the saved scope. */
8884 parser->scope = saved_scope;
8885 parser->qualifying_scope = saved_qualifying_scope;
8886 parser->object_scope = saved_object_scope;
8887 /* If the TYPE is invalid, indicate failure. */
8888 if (type == error_mark_node)
8889 return error_mark_node;
8890 return mangle_conv_op_name_for_type (type);
8893 /* Parse a conversion-type-id:
8896 type-specifier-seq conversion-declarator [opt]
8898 Returns the TYPE specified. */
8901 cp_parser_conversion_type_id (cp_parser* parser)
8904 cp_decl_specifier_seq type_specifiers;
8905 cp_declarator *declarator;
8906 tree type_specified;
8908 /* Parse the attributes. */
8909 attributes = cp_parser_attributes_opt (parser);
8910 /* Parse the type-specifiers. */
8911 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8913 /* If that didn't work, stop. */
8914 if (type_specifiers.type == error_mark_node)
8915 return error_mark_node;
8916 /* Parse the conversion-declarator. */
8917 declarator = cp_parser_conversion_declarator_opt (parser);
8919 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8920 /*initialized=*/0, &attributes);
8922 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8923 return type_specified;
8926 /* Parse an (optional) conversion-declarator.
8928 conversion-declarator:
8929 ptr-operator conversion-declarator [opt]
8933 static cp_declarator *
8934 cp_parser_conversion_declarator_opt (cp_parser* parser)
8936 enum tree_code code;
8938 cp_cv_quals cv_quals;
8940 /* We don't know if there's a ptr-operator next, or not. */
8941 cp_parser_parse_tentatively (parser);
8942 /* Try the ptr-operator. */
8943 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8944 /* If it worked, look for more conversion-declarators. */
8945 if (cp_parser_parse_definitely (parser))
8947 cp_declarator *declarator;
8949 /* Parse another optional declarator. */
8950 declarator = cp_parser_conversion_declarator_opt (parser);
8952 return cp_parser_make_indirect_declarator
8953 (code, class_type, cv_quals, declarator);
8959 /* Parse an (optional) ctor-initializer.
8962 : mem-initializer-list
8964 Returns TRUE iff the ctor-initializer was actually present. */
8967 cp_parser_ctor_initializer_opt (cp_parser* parser)
8969 /* If the next token is not a `:', then there is no
8970 ctor-initializer. */
8971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8973 /* Do default initialization of any bases and members. */
8974 if (DECL_CONSTRUCTOR_P (current_function_decl))
8975 finish_mem_initializers (NULL_TREE);
8980 /* Consume the `:' token. */
8981 cp_lexer_consume_token (parser->lexer);
8982 /* And the mem-initializer-list. */
8983 cp_parser_mem_initializer_list (parser);
8988 /* Parse a mem-initializer-list.
8990 mem-initializer-list:
8991 mem-initializer ... [opt]
8992 mem-initializer ... [opt] , mem-initializer-list */
8995 cp_parser_mem_initializer_list (cp_parser* parser)
8997 tree mem_initializer_list = NULL_TREE;
8998 cp_token *token = cp_lexer_peek_token (parser->lexer);
9000 /* Let the semantic analysis code know that we are starting the
9001 mem-initializer-list. */
9002 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9003 error ("%Honly constructors take base initializers",
9006 /* Loop through the list. */
9009 tree mem_initializer;
9011 token = cp_lexer_peek_token (parser->lexer);
9012 /* Parse the mem-initializer. */
9013 mem_initializer = cp_parser_mem_initializer (parser);
9014 /* If the next token is a `...', we're expanding member initializers. */
9015 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9017 /* Consume the `...'. */
9018 cp_lexer_consume_token (parser->lexer);
9020 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9021 can be expanded but members cannot. */
9022 if (mem_initializer != error_mark_node
9023 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9025 error ("%Hcannot expand initializer for member %<%D%>",
9026 &token->location, TREE_PURPOSE (mem_initializer));
9027 mem_initializer = error_mark_node;
9030 /* Construct the pack expansion type. */
9031 if (mem_initializer != error_mark_node)
9032 mem_initializer = make_pack_expansion (mem_initializer);
9034 /* Add it to the list, unless it was erroneous. */
9035 if (mem_initializer != error_mark_node)
9037 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9038 mem_initializer_list = mem_initializer;
9040 /* If the next token is not a `,', we're done. */
9041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9043 /* Consume the `,' token. */
9044 cp_lexer_consume_token (parser->lexer);
9047 /* Perform semantic analysis. */
9048 if (DECL_CONSTRUCTOR_P (current_function_decl))
9049 finish_mem_initializers (mem_initializer_list);
9052 /* Parse a mem-initializer.
9055 mem-initializer-id ( expression-list [opt] )
9056 mem-initializer-id braced-init-list
9061 ( expression-list [opt] )
9063 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9064 class) or FIELD_DECL (for a non-static data member) to initialize;
9065 the TREE_VALUE is the expression-list. An empty initialization
9066 list is represented by void_list_node. */
9069 cp_parser_mem_initializer (cp_parser* parser)
9071 tree mem_initializer_id;
9072 tree expression_list;
9074 cp_token *token = cp_lexer_peek_token (parser->lexer);
9076 /* Find out what is being initialized. */
9077 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9079 permerror (token->location,
9080 "anachronistic old-style base class initializer");
9081 mem_initializer_id = NULL_TREE;
9084 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9085 member = expand_member_init (mem_initializer_id);
9086 if (member && !DECL_P (member))
9087 in_base_initializer = 1;
9089 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9091 bool expr_non_constant_p;
9092 maybe_warn_cpp0x ("extended initializer lists");
9093 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9094 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9095 expression_list = build_tree_list (NULL_TREE, expression_list);
9099 = cp_parser_parenthesized_expression_list (parser, false,
9101 /*allow_expansion_p=*/true,
9102 /*non_constant_p=*/NULL);
9103 if (expression_list == error_mark_node)
9104 return error_mark_node;
9105 if (!expression_list)
9106 expression_list = void_type_node;
9108 in_base_initializer = 0;
9110 return member ? build_tree_list (member, expression_list) : error_mark_node;
9113 /* Parse a mem-initializer-id.
9116 :: [opt] nested-name-specifier [opt] class-name
9119 Returns a TYPE indicating the class to be initializer for the first
9120 production. Returns an IDENTIFIER_NODE indicating the data member
9121 to be initialized for the second production. */
9124 cp_parser_mem_initializer_id (cp_parser* parser)
9126 bool global_scope_p;
9127 bool nested_name_specifier_p;
9128 bool template_p = false;
9131 cp_token *token = cp_lexer_peek_token (parser->lexer);
9133 /* `typename' is not allowed in this context ([temp.res]). */
9134 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9136 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9137 "member initializer is implicitly a type)",
9139 cp_lexer_consume_token (parser->lexer);
9141 /* Look for the optional `::' operator. */
9143 = (cp_parser_global_scope_opt (parser,
9144 /*current_scope_valid_p=*/false)
9146 /* Look for the optional nested-name-specifier. The simplest way to
9151 The keyword `typename' is not permitted in a base-specifier or
9152 mem-initializer; in these contexts a qualified name that
9153 depends on a template-parameter is implicitly assumed to be a
9156 is to assume that we have seen the `typename' keyword at this
9158 nested_name_specifier_p
9159 = (cp_parser_nested_name_specifier_opt (parser,
9160 /*typename_keyword_p=*/true,
9161 /*check_dependency_p=*/true,
9163 /*is_declaration=*/true)
9165 if (nested_name_specifier_p)
9166 template_p = cp_parser_optional_template_keyword (parser);
9167 /* If there is a `::' operator or a nested-name-specifier, then we
9168 are definitely looking for a class-name. */
9169 if (global_scope_p || nested_name_specifier_p)
9170 return cp_parser_class_name (parser,
9171 /*typename_keyword_p=*/true,
9172 /*template_keyword_p=*/template_p,
9174 /*check_dependency_p=*/true,
9175 /*class_head_p=*/false,
9176 /*is_declaration=*/true);
9177 /* Otherwise, we could also be looking for an ordinary identifier. */
9178 cp_parser_parse_tentatively (parser);
9179 /* Try a class-name. */
9180 id = cp_parser_class_name (parser,
9181 /*typename_keyword_p=*/true,
9182 /*template_keyword_p=*/false,
9184 /*check_dependency_p=*/true,
9185 /*class_head_p=*/false,
9186 /*is_declaration=*/true);
9187 /* If we found one, we're done. */
9188 if (cp_parser_parse_definitely (parser))
9190 /* Otherwise, look for an ordinary identifier. */
9191 return cp_parser_identifier (parser);
9194 /* Overloading [gram.over] */
9196 /* Parse an operator-function-id.
9198 operator-function-id:
9201 Returns an IDENTIFIER_NODE for the operator which is a
9202 human-readable spelling of the identifier, e.g., `operator +'. */
9205 cp_parser_operator_function_id (cp_parser* parser)
9207 /* Look for the `operator' keyword. */
9208 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9209 return error_mark_node;
9210 /* And then the name of the operator itself. */
9211 return cp_parser_operator (parser);
9214 /* Parse an operator.
9217 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9218 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9219 || ++ -- , ->* -> () []
9226 Returns an IDENTIFIER_NODE for the operator which is a
9227 human-readable spelling of the identifier, e.g., `operator +'. */
9230 cp_parser_operator (cp_parser* parser)
9232 tree id = NULL_TREE;
9235 /* Peek at the next token. */
9236 token = cp_lexer_peek_token (parser->lexer);
9237 /* Figure out which operator we have. */
9238 switch (token->type)
9244 /* The keyword should be either `new' or `delete'. */
9245 if (token->keyword == RID_NEW)
9247 else if (token->keyword == RID_DELETE)
9252 /* Consume the `new' or `delete' token. */
9253 cp_lexer_consume_token (parser->lexer);
9255 /* Peek at the next token. */
9256 token = cp_lexer_peek_token (parser->lexer);
9257 /* If it's a `[' token then this is the array variant of the
9259 if (token->type == CPP_OPEN_SQUARE)
9261 /* Consume the `[' token. */
9262 cp_lexer_consume_token (parser->lexer);
9263 /* Look for the `]' token. */
9264 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9265 id = ansi_opname (op == NEW_EXPR
9266 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9268 /* Otherwise, we have the non-array variant. */
9270 id = ansi_opname (op);
9276 id = ansi_opname (PLUS_EXPR);
9280 id = ansi_opname (MINUS_EXPR);
9284 id = ansi_opname (MULT_EXPR);
9288 id = ansi_opname (TRUNC_DIV_EXPR);
9292 id = ansi_opname (TRUNC_MOD_EXPR);
9296 id = ansi_opname (BIT_XOR_EXPR);
9300 id = ansi_opname (BIT_AND_EXPR);
9304 id = ansi_opname (BIT_IOR_EXPR);
9308 id = ansi_opname (BIT_NOT_EXPR);
9312 id = ansi_opname (TRUTH_NOT_EXPR);
9316 id = ansi_assopname (NOP_EXPR);
9320 id = ansi_opname (LT_EXPR);
9324 id = ansi_opname (GT_EXPR);
9328 id = ansi_assopname (PLUS_EXPR);
9332 id = ansi_assopname (MINUS_EXPR);
9336 id = ansi_assopname (MULT_EXPR);
9340 id = ansi_assopname (TRUNC_DIV_EXPR);
9344 id = ansi_assopname (TRUNC_MOD_EXPR);
9348 id = ansi_assopname (BIT_XOR_EXPR);
9352 id = ansi_assopname (BIT_AND_EXPR);
9356 id = ansi_assopname (BIT_IOR_EXPR);
9360 id = ansi_opname (LSHIFT_EXPR);
9364 id = ansi_opname (RSHIFT_EXPR);
9368 id = ansi_assopname (LSHIFT_EXPR);
9372 id = ansi_assopname (RSHIFT_EXPR);
9376 id = ansi_opname (EQ_EXPR);
9380 id = ansi_opname (NE_EXPR);
9384 id = ansi_opname (LE_EXPR);
9387 case CPP_GREATER_EQ:
9388 id = ansi_opname (GE_EXPR);
9392 id = ansi_opname (TRUTH_ANDIF_EXPR);
9396 id = ansi_opname (TRUTH_ORIF_EXPR);
9400 id = ansi_opname (POSTINCREMENT_EXPR);
9403 case CPP_MINUS_MINUS:
9404 id = ansi_opname (PREDECREMENT_EXPR);
9408 id = ansi_opname (COMPOUND_EXPR);
9411 case CPP_DEREF_STAR:
9412 id = ansi_opname (MEMBER_REF);
9416 id = ansi_opname (COMPONENT_REF);
9419 case CPP_OPEN_PAREN:
9420 /* Consume the `('. */
9421 cp_lexer_consume_token (parser->lexer);
9422 /* Look for the matching `)'. */
9423 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9424 return ansi_opname (CALL_EXPR);
9426 case CPP_OPEN_SQUARE:
9427 /* Consume the `['. */
9428 cp_lexer_consume_token (parser->lexer);
9429 /* Look for the matching `]'. */
9430 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9431 return ansi_opname (ARRAY_REF);
9434 /* Anything else is an error. */
9438 /* If we have selected an identifier, we need to consume the
9441 cp_lexer_consume_token (parser->lexer);
9442 /* Otherwise, no valid operator name was present. */
9445 cp_parser_error (parser, "expected operator");
9446 id = error_mark_node;
9452 /* Parse a template-declaration.
9454 template-declaration:
9455 export [opt] template < template-parameter-list > declaration
9457 If MEMBER_P is TRUE, this template-declaration occurs within a
9460 The grammar rule given by the standard isn't correct. What
9463 template-declaration:
9464 export [opt] template-parameter-list-seq
9465 decl-specifier-seq [opt] init-declarator [opt] ;
9466 export [opt] template-parameter-list-seq
9469 template-parameter-list-seq:
9470 template-parameter-list-seq [opt]
9471 template < template-parameter-list > */
9474 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9476 /* Check for `export'. */
9477 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9479 /* Consume the `export' token. */
9480 cp_lexer_consume_token (parser->lexer);
9481 /* Warn that we do not support `export'. */
9482 warning (0, "keyword %<export%> not implemented, and will be ignored");
9485 cp_parser_template_declaration_after_export (parser, member_p);
9488 /* Parse a template-parameter-list.
9490 template-parameter-list:
9492 template-parameter-list , template-parameter
9494 Returns a TREE_LIST. Each node represents a template parameter.
9495 The nodes are connected via their TREE_CHAINs. */
9498 cp_parser_template_parameter_list (cp_parser* parser)
9500 tree parameter_list = NULL_TREE;
9502 begin_template_parm_list ();
9507 bool is_parameter_pack;
9509 /* Parse the template-parameter. */
9510 parameter = cp_parser_template_parameter (parser,
9512 &is_parameter_pack);
9513 /* Add it to the list. */
9514 if (parameter != error_mark_node)
9515 parameter_list = process_template_parm (parameter_list,
9521 tree err_parm = build_tree_list (parameter, parameter);
9522 TREE_VALUE (err_parm) = error_mark_node;
9523 parameter_list = chainon (parameter_list, err_parm);
9526 /* If the next token is not a `,', we're done. */
9527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9529 /* Otherwise, consume the `,' token. */
9530 cp_lexer_consume_token (parser->lexer);
9533 return end_template_parm_list (parameter_list);
9536 /* Parse a template-parameter.
9540 parameter-declaration
9542 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9543 the parameter. The TREE_PURPOSE is the default value, if any.
9544 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9545 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9546 set to true iff this parameter is a parameter pack. */
9549 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9550 bool *is_parameter_pack)
9553 cp_parameter_declarator *parameter_declarator;
9554 cp_declarator *id_declarator;
9557 /* Assume it is a type parameter or a template parameter. */
9558 *is_non_type = false;
9559 /* Assume it not a parameter pack. */
9560 *is_parameter_pack = false;
9561 /* Peek at the next token. */
9562 token = cp_lexer_peek_token (parser->lexer);
9563 /* If it is `class' or `template', we have a type-parameter. */
9564 if (token->keyword == RID_TEMPLATE)
9565 return cp_parser_type_parameter (parser, is_parameter_pack);
9566 /* If it is `class' or `typename' we do not know yet whether it is a
9567 type parameter or a non-type parameter. Consider:
9569 template <typename T, typename T::X X> ...
9573 template <class C, class D*> ...
9575 Here, the first parameter is a type parameter, and the second is
9576 a non-type parameter. We can tell by looking at the token after
9577 the identifier -- if it is a `,', `=', or `>' then we have a type
9579 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9581 /* Peek at the token after `class' or `typename'. */
9582 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9583 /* If it's an ellipsis, we have a template type parameter
9585 if (token->type == CPP_ELLIPSIS)
9586 return cp_parser_type_parameter (parser, is_parameter_pack);
9587 /* If it's an identifier, skip it. */
9588 if (token->type == CPP_NAME)
9589 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9590 /* Now, see if the token looks like the end of a template
9592 if (token->type == CPP_COMMA
9593 || token->type == CPP_EQ
9594 || token->type == CPP_GREATER)
9595 return cp_parser_type_parameter (parser, is_parameter_pack);
9598 /* Otherwise, it is a non-type parameter.
9602 When parsing a default template-argument for a non-type
9603 template-parameter, the first non-nested `>' is taken as the end
9604 of the template parameter-list rather than a greater-than
9606 *is_non_type = true;
9607 parameter_declarator
9608 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9609 /*parenthesized_p=*/NULL);
9611 /* If the parameter declaration is marked as a parameter pack, set
9612 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9613 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9615 if (parameter_declarator
9616 && parameter_declarator->declarator
9617 && parameter_declarator->declarator->parameter_pack_p)
9619 *is_parameter_pack = true;
9620 parameter_declarator->declarator->parameter_pack_p = false;
9623 /* If the next token is an ellipsis, and we don't already have it
9624 marked as a parameter pack, then we have a parameter pack (that
9625 has no declarator). */
9626 if (!*is_parameter_pack
9627 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9628 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9630 /* Consume the `...'. */
9631 cp_lexer_consume_token (parser->lexer);
9632 maybe_warn_variadic_templates ();
9634 *is_parameter_pack = true;
9636 /* We might end up with a pack expansion as the type of the non-type
9637 template parameter, in which case this is a non-type template
9639 else if (parameter_declarator
9640 && parameter_declarator->decl_specifiers.type
9641 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9643 *is_parameter_pack = true;
9644 parameter_declarator->decl_specifiers.type =
9645 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9648 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9650 /* Parameter packs cannot have default arguments. However, a
9651 user may try to do so, so we'll parse them and give an
9652 appropriate diagnostic here. */
9654 /* Consume the `='. */
9655 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9656 cp_lexer_consume_token (parser->lexer);
9658 /* Find the name of the parameter pack. */
9659 id_declarator = parameter_declarator->declarator;
9660 while (id_declarator && id_declarator->kind != cdk_id)
9661 id_declarator = id_declarator->declarator;
9663 if (id_declarator && id_declarator->kind == cdk_id)
9664 error ("%Htemplate parameter pack %qD cannot have a default argument",
9665 &start_token->location, id_declarator->u.id.unqualified_name);
9667 error ("%Htemplate parameter pack cannot have a default argument",
9668 &start_token->location);
9670 /* Parse the default argument, but throw away the result. */
9671 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9674 parm = grokdeclarator (parameter_declarator->declarator,
9675 ¶meter_declarator->decl_specifiers,
9676 PARM, /*initialized=*/0,
9678 if (parm == error_mark_node)
9679 return error_mark_node;
9681 return build_tree_list (parameter_declarator->default_argument, parm);
9684 /* Parse a type-parameter.
9687 class identifier [opt]
9688 class identifier [opt] = type-id
9689 typename identifier [opt]
9690 typename identifier [opt] = type-id
9691 template < template-parameter-list > class identifier [opt]
9692 template < template-parameter-list > class identifier [opt]
9695 GNU Extension (variadic templates):
9698 class ... identifier [opt]
9699 typename ... identifier [opt]
9701 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9702 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9703 the declaration of the parameter.
9705 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9708 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9713 /* Look for a keyword to tell us what kind of parameter this is. */
9714 token = cp_parser_require (parser, CPP_KEYWORD,
9715 "%<class%>, %<typename%>, or %<template%>");
9717 return error_mark_node;
9719 switch (token->keyword)
9725 tree default_argument;
9727 /* If the next token is an ellipsis, we have a template
9729 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9731 /* Consume the `...' token. */
9732 cp_lexer_consume_token (parser->lexer);
9733 maybe_warn_variadic_templates ();
9735 *is_parameter_pack = true;
9738 /* If the next token is an identifier, then it names the
9740 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9741 identifier = cp_parser_identifier (parser);
9743 identifier = NULL_TREE;
9745 /* Create the parameter. */
9746 parameter = finish_template_type_parm (class_type_node, identifier);
9748 /* If the next token is an `=', we have a default argument. */
9749 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9751 /* Consume the `=' token. */
9752 cp_lexer_consume_token (parser->lexer);
9753 /* Parse the default-argument. */
9754 push_deferring_access_checks (dk_no_deferred);
9755 default_argument = cp_parser_type_id (parser);
9757 /* Template parameter packs cannot have default
9759 if (*is_parameter_pack)
9762 error ("%Htemplate parameter pack %qD cannot have a "
9763 "default argument", &token->location, identifier);
9765 error ("%Htemplate parameter packs cannot have "
9766 "default arguments", &token->location);
9767 default_argument = NULL_TREE;
9769 pop_deferring_access_checks ();
9772 default_argument = NULL_TREE;
9774 /* Create the combined representation of the parameter and the
9775 default argument. */
9776 parameter = build_tree_list (default_argument, parameter);
9782 tree parameter_list;
9784 tree default_argument;
9786 /* Look for the `<'. */
9787 cp_parser_require (parser, CPP_LESS, "%<<%>");
9788 /* Parse the template-parameter-list. */
9789 parameter_list = cp_parser_template_parameter_list (parser);
9790 /* Look for the `>'. */
9791 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9792 /* Look for the `class' keyword. */
9793 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9794 /* If the next token is an ellipsis, we have a template
9796 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9798 /* Consume the `...' token. */
9799 cp_lexer_consume_token (parser->lexer);
9800 maybe_warn_variadic_templates ();
9802 *is_parameter_pack = true;
9804 /* If the next token is an `=', then there is a
9805 default-argument. If the next token is a `>', we are at
9806 the end of the parameter-list. If the next token is a `,',
9807 then we are at the end of this parameter. */
9808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9809 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9810 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9812 identifier = cp_parser_identifier (parser);
9813 /* Treat invalid names as if the parameter were nameless. */
9814 if (identifier == error_mark_node)
9815 identifier = NULL_TREE;
9818 identifier = NULL_TREE;
9820 /* Create the template parameter. */
9821 parameter = finish_template_template_parm (class_type_node,
9824 /* If the next token is an `=', then there is a
9825 default-argument. */
9826 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9830 /* Consume the `='. */
9831 cp_lexer_consume_token (parser->lexer);
9832 /* Parse the id-expression. */
9833 push_deferring_access_checks (dk_no_deferred);
9834 /* save token before parsing the id-expression, for error
9836 token = cp_lexer_peek_token (parser->lexer);
9838 = cp_parser_id_expression (parser,
9839 /*template_keyword_p=*/false,
9840 /*check_dependency_p=*/true,
9841 /*template_p=*/&is_template,
9842 /*declarator_p=*/false,
9843 /*optional_p=*/false);
9844 if (TREE_CODE (default_argument) == TYPE_DECL)
9845 /* If the id-expression was a template-id that refers to
9846 a template-class, we already have the declaration here,
9847 so no further lookup is needed. */
9850 /* Look up the name. */
9852 = cp_parser_lookup_name (parser, default_argument,
9854 /*is_template=*/is_template,
9855 /*is_namespace=*/false,
9856 /*check_dependency=*/true,
9857 /*ambiguous_decls=*/NULL,
9859 /* See if the default argument is valid. */
9861 = check_template_template_default_arg (default_argument);
9863 /* Template parameter packs cannot have default
9865 if (*is_parameter_pack)
9868 error ("%Htemplate parameter pack %qD cannot "
9869 "have a default argument",
9870 &token->location, identifier);
9872 error ("%Htemplate parameter packs cannot "
9873 "have default arguments",
9875 default_argument = NULL_TREE;
9877 pop_deferring_access_checks ();
9880 default_argument = NULL_TREE;
9882 /* Create the combined representation of the parameter and the
9883 default argument. */
9884 parameter = build_tree_list (default_argument, parameter);
9896 /* Parse a template-id.
9899 template-name < template-argument-list [opt] >
9901 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9902 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9903 returned. Otherwise, if the template-name names a function, or set
9904 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9905 names a class, returns a TYPE_DECL for the specialization.
9907 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9908 uninstantiated templates. */
9911 cp_parser_template_id (cp_parser *parser,
9912 bool template_keyword_p,
9913 bool check_dependency_p,
9914 bool is_declaration)
9920 cp_token_position start_of_id = 0;
9921 deferred_access_check *chk;
9922 VEC (deferred_access_check,gc) *access_check;
9923 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9926 /* If the next token corresponds to a template-id, there is no need
9928 next_token = cp_lexer_peek_token (parser->lexer);
9929 if (next_token->type == CPP_TEMPLATE_ID)
9931 struct tree_check *check_value;
9933 /* Get the stored value. */
9934 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9935 /* Perform any access checks that were deferred. */
9936 access_check = check_value->checks;
9940 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9943 perform_or_defer_access_check (chk->binfo,
9948 /* Return the stored value. */
9949 return check_value->value;
9952 /* Avoid performing name lookup if there is no possibility of
9953 finding a template-id. */
9954 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9955 || (next_token->type == CPP_NAME
9956 && !cp_parser_nth_token_starts_template_argument_list_p
9959 cp_parser_error (parser, "expected template-id");
9960 return error_mark_node;
9963 /* Remember where the template-id starts. */
9964 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9965 start_of_id = cp_lexer_token_position (parser->lexer, false);
9967 push_deferring_access_checks (dk_deferred);
9969 /* Parse the template-name. */
9970 is_identifier = false;
9971 token = cp_lexer_peek_token (parser->lexer);
9972 templ = cp_parser_template_name (parser, template_keyword_p,
9976 if (templ == error_mark_node || is_identifier)
9978 pop_deferring_access_checks ();
9982 /* If we find the sequence `[:' after a template-name, it's probably
9983 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9984 parse correctly the argument list. */
9985 next_token = cp_lexer_peek_token (parser->lexer);
9986 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9987 if (next_token->type == CPP_OPEN_SQUARE
9988 && next_token->flags & DIGRAPH
9989 && next_token_2->type == CPP_COLON
9990 && !(next_token_2->flags & PREV_WHITE))
9992 cp_parser_parse_tentatively (parser);
9993 /* Change `:' into `::'. */
9994 next_token_2->type = CPP_SCOPE;
9995 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9997 cp_lexer_consume_token (parser->lexer);
9999 /* Parse the arguments. */
10000 arguments = cp_parser_enclosed_template_argument_list (parser);
10001 if (!cp_parser_parse_definitely (parser))
10003 /* If we couldn't parse an argument list, then we revert our changes
10004 and return simply an error. Maybe this is not a template-id
10006 next_token_2->type = CPP_COLON;
10007 cp_parser_error (parser, "expected %<<%>");
10008 pop_deferring_access_checks ();
10009 return error_mark_node;
10011 /* Otherwise, emit an error about the invalid digraph, but continue
10012 parsing because we got our argument list. */
10013 if (permerror (next_token->location,
10014 "%<<::%> cannot begin a template-argument list"))
10016 static bool hint = false;
10017 inform (next_token->location,
10018 "%<<:%> is an alternate spelling for %<[%>."
10019 " Insert whitespace between %<<%> and %<::%>");
10020 if (!hint && !flag_permissive)
10022 inform (next_token->location, "(if you use %<-fpermissive%>"
10023 " G++ will accept your code)");
10030 /* Look for the `<' that starts the template-argument-list. */
10031 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10033 pop_deferring_access_checks ();
10034 return error_mark_node;
10036 /* Parse the arguments. */
10037 arguments = cp_parser_enclosed_template_argument_list (parser);
10040 /* Build a representation of the specialization. */
10041 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10042 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10043 else if (DECL_CLASS_TEMPLATE_P (templ)
10044 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10046 bool entering_scope;
10047 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10048 template (rather than some instantiation thereof) only if
10049 is not nested within some other construct. For example, in
10050 "template <typename T> void f(T) { A<T>::", A<T> is just an
10051 instantiation of A. */
10052 entering_scope = (template_parm_scope_p ()
10053 && cp_lexer_next_token_is (parser->lexer,
10056 = finish_template_type (templ, arguments, entering_scope);
10060 /* If it's not a class-template or a template-template, it should be
10061 a function-template. */
10062 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10063 || TREE_CODE (templ) == OVERLOAD
10064 || BASELINK_P (templ)));
10066 template_id = lookup_template_function (templ, arguments);
10069 /* If parsing tentatively, replace the sequence of tokens that makes
10070 up the template-id with a CPP_TEMPLATE_ID token. That way,
10071 should we re-parse the token stream, we will not have to repeat
10072 the effort required to do the parse, nor will we issue duplicate
10073 error messages about problems during instantiation of the
10077 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10079 /* Reset the contents of the START_OF_ID token. */
10080 token->type = CPP_TEMPLATE_ID;
10081 /* Retrieve any deferred checks. Do not pop this access checks yet
10082 so the memory will not be reclaimed during token replacing below. */
10083 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10084 token->u.tree_check_value->value = template_id;
10085 token->u.tree_check_value->checks = get_deferred_access_checks ();
10086 token->keyword = RID_MAX;
10088 /* Purge all subsequent tokens. */
10089 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10091 /* ??? Can we actually assume that, if template_id ==
10092 error_mark_node, we will have issued a diagnostic to the
10093 user, as opposed to simply marking the tentative parse as
10095 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10096 error ("%Hparse error in template argument list",
10100 pop_deferring_access_checks ();
10101 return template_id;
10104 /* Parse a template-name.
10109 The standard should actually say:
10113 operator-function-id
10115 A defect report has been filed about this issue.
10117 A conversion-function-id cannot be a template name because they cannot
10118 be part of a template-id. In fact, looking at this code:
10120 a.operator K<int>()
10122 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10123 It is impossible to call a templated conversion-function-id with an
10124 explicit argument list, since the only allowed template parameter is
10125 the type to which it is converting.
10127 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10128 `template' keyword, in a construction like:
10132 In that case `f' is taken to be a template-name, even though there
10133 is no way of knowing for sure.
10135 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10136 name refers to a set of overloaded functions, at least one of which
10137 is a template, or an IDENTIFIER_NODE with the name of the template,
10138 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10139 names are looked up inside uninstantiated templates. */
10142 cp_parser_template_name (cp_parser* parser,
10143 bool template_keyword_p,
10144 bool check_dependency_p,
10145 bool is_declaration,
10146 bool *is_identifier)
10151 cp_token *token = cp_lexer_peek_token (parser->lexer);
10153 /* If the next token is `operator', then we have either an
10154 operator-function-id or a conversion-function-id. */
10155 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10157 /* We don't know whether we're looking at an
10158 operator-function-id or a conversion-function-id. */
10159 cp_parser_parse_tentatively (parser);
10160 /* Try an operator-function-id. */
10161 identifier = cp_parser_operator_function_id (parser);
10162 /* If that didn't work, try a conversion-function-id. */
10163 if (!cp_parser_parse_definitely (parser))
10165 cp_parser_error (parser, "expected template-name");
10166 return error_mark_node;
10169 /* Look for the identifier. */
10171 identifier = cp_parser_identifier (parser);
10173 /* If we didn't find an identifier, we don't have a template-id. */
10174 if (identifier == error_mark_node)
10175 return error_mark_node;
10177 /* If the name immediately followed the `template' keyword, then it
10178 is a template-name. However, if the next token is not `<', then
10179 we do not treat it as a template-name, since it is not being used
10180 as part of a template-id. This enables us to handle constructs
10183 template <typename T> struct S { S(); };
10184 template <typename T> S<T>::S();
10186 correctly. We would treat `S' as a template -- if it were `S<T>'
10187 -- but we do not if there is no `<'. */
10189 if (processing_template_decl
10190 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10192 /* In a declaration, in a dependent context, we pretend that the
10193 "template" keyword was present in order to improve error
10194 recovery. For example, given:
10196 template <typename T> void f(T::X<int>);
10198 we want to treat "X<int>" as a template-id. */
10200 && !template_keyword_p
10201 && parser->scope && TYPE_P (parser->scope)
10202 && check_dependency_p
10203 && dependent_type_p (parser->scope)
10204 /* Do not do this for dtors (or ctors), since they never
10205 need the template keyword before their name. */
10206 && !constructor_name_p (identifier, parser->scope))
10208 cp_token_position start = 0;
10210 /* Explain what went wrong. */
10211 error ("%Hnon-template %qD used as template",
10212 &token->location, identifier);
10213 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10214 parser->scope, identifier);
10215 /* If parsing tentatively, find the location of the "<" token. */
10216 if (cp_parser_simulate_error (parser))
10217 start = cp_lexer_token_position (parser->lexer, true);
10218 /* Parse the template arguments so that we can issue error
10219 messages about them. */
10220 cp_lexer_consume_token (parser->lexer);
10221 cp_parser_enclosed_template_argument_list (parser);
10222 /* Skip tokens until we find a good place from which to
10223 continue parsing. */
10224 cp_parser_skip_to_closing_parenthesis (parser,
10225 /*recovering=*/true,
10227 /*consume_paren=*/false);
10228 /* If parsing tentatively, permanently remove the
10229 template argument list. That will prevent duplicate
10230 error messages from being issued about the missing
10231 "template" keyword. */
10233 cp_lexer_purge_tokens_after (parser->lexer, start);
10235 *is_identifier = true;
10239 /* If the "template" keyword is present, then there is generally
10240 no point in doing name-lookup, so we just return IDENTIFIER.
10241 But, if the qualifying scope is non-dependent then we can
10242 (and must) do name-lookup normally. */
10243 if (template_keyword_p
10245 || (TYPE_P (parser->scope)
10246 && dependent_type_p (parser->scope))))
10250 /* Look up the name. */
10251 decl = cp_parser_lookup_name (parser, identifier,
10253 /*is_template=*/false,
10254 /*is_namespace=*/false,
10255 check_dependency_p,
10256 /*ambiguous_decls=*/NULL,
10258 decl = maybe_get_template_decl_from_type_decl (decl);
10260 /* If DECL is a template, then the name was a template-name. */
10261 if (TREE_CODE (decl) == TEMPLATE_DECL)
10265 tree fn = NULL_TREE;
10267 /* The standard does not explicitly indicate whether a name that
10268 names a set of overloaded declarations, some of which are
10269 templates, is a template-name. However, such a name should
10270 be a template-name; otherwise, there is no way to form a
10271 template-id for the overloaded templates. */
10272 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10273 if (TREE_CODE (fns) == OVERLOAD)
10274 for (fn = fns; fn; fn = OVL_NEXT (fn))
10275 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10280 /* The name does not name a template. */
10281 cp_parser_error (parser, "expected template-name");
10282 return error_mark_node;
10286 /* If DECL is dependent, and refers to a function, then just return
10287 its name; we will look it up again during template instantiation. */
10288 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10290 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10291 if (TYPE_P (scope) && dependent_type_p (scope))
10298 /* Parse a template-argument-list.
10300 template-argument-list:
10301 template-argument ... [opt]
10302 template-argument-list , template-argument ... [opt]
10304 Returns a TREE_VEC containing the arguments. */
10307 cp_parser_template_argument_list (cp_parser* parser)
10309 tree fixed_args[10];
10310 unsigned n_args = 0;
10311 unsigned alloced = 10;
10312 tree *arg_ary = fixed_args;
10314 bool saved_in_template_argument_list_p;
10316 bool saved_non_ice_p;
10318 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10319 parser->in_template_argument_list_p = true;
10320 /* Even if the template-id appears in an integral
10321 constant-expression, the contents of the argument list do
10323 saved_ice_p = parser->integral_constant_expression_p;
10324 parser->integral_constant_expression_p = false;
10325 saved_non_ice_p = parser->non_integral_constant_expression_p;
10326 parser->non_integral_constant_expression_p = false;
10327 /* Parse the arguments. */
10333 /* Consume the comma. */
10334 cp_lexer_consume_token (parser->lexer);
10336 /* Parse the template-argument. */
10337 argument = cp_parser_template_argument (parser);
10339 /* If the next token is an ellipsis, we're expanding a template
10341 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10343 /* Consume the `...' token. */
10344 cp_lexer_consume_token (parser->lexer);
10346 /* Make the argument into a TYPE_PACK_EXPANSION or
10347 EXPR_PACK_EXPANSION. */
10348 argument = make_pack_expansion (argument);
10351 if (n_args == alloced)
10355 if (arg_ary == fixed_args)
10357 arg_ary = XNEWVEC (tree, alloced);
10358 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10361 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10363 arg_ary[n_args++] = argument;
10365 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10367 vec = make_tree_vec (n_args);
10370 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10372 if (arg_ary != fixed_args)
10374 parser->non_integral_constant_expression_p = saved_non_ice_p;
10375 parser->integral_constant_expression_p = saved_ice_p;
10376 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10380 /* Parse a template-argument.
10383 assignment-expression
10387 The representation is that of an assignment-expression, type-id, or
10388 id-expression -- except that the qualified id-expression is
10389 evaluated, so that the value returned is either a DECL or an
10392 Although the standard says "assignment-expression", it forbids
10393 throw-expressions or assignments in the template argument.
10394 Therefore, we use "conditional-expression" instead. */
10397 cp_parser_template_argument (cp_parser* parser)
10402 bool maybe_type_id = false;
10403 cp_token *token = NULL, *argument_start_token = NULL;
10406 /* There's really no way to know what we're looking at, so we just
10407 try each alternative in order.
10411 In a template-argument, an ambiguity between a type-id and an
10412 expression is resolved to a type-id, regardless of the form of
10413 the corresponding template-parameter.
10415 Therefore, we try a type-id first. */
10416 cp_parser_parse_tentatively (parser);
10417 argument = cp_parser_type_id (parser);
10418 /* If there was no error parsing the type-id but the next token is a
10419 '>>', our behavior depends on which dialect of C++ we're
10420 parsing. In C++98, we probably found a typo for '> >'. But there
10421 are type-id which are also valid expressions. For instance:
10423 struct X { int operator >> (int); };
10424 template <int V> struct Foo {};
10427 Here 'X()' is a valid type-id of a function type, but the user just
10428 wanted to write the expression "X() >> 5". Thus, we remember that we
10429 found a valid type-id, but we still try to parse the argument as an
10430 expression to see what happens.
10432 In C++0x, the '>>' will be considered two separate '>'
10434 if (!cp_parser_error_occurred (parser)
10435 && cxx_dialect == cxx98
10436 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10438 maybe_type_id = true;
10439 cp_parser_abort_tentative_parse (parser);
10443 /* If the next token isn't a `,' or a `>', then this argument wasn't
10444 really finished. This means that the argument is not a valid
10446 if (!cp_parser_next_token_ends_template_argument_p (parser))
10447 cp_parser_error (parser, "expected template-argument");
10448 /* If that worked, we're done. */
10449 if (cp_parser_parse_definitely (parser))
10452 /* We're still not sure what the argument will be. */
10453 cp_parser_parse_tentatively (parser);
10454 /* Try a template. */
10455 argument_start_token = cp_lexer_peek_token (parser->lexer);
10456 argument = cp_parser_id_expression (parser,
10457 /*template_keyword_p=*/false,
10458 /*check_dependency_p=*/true,
10460 /*declarator_p=*/false,
10461 /*optional_p=*/false);
10462 /* If the next token isn't a `,' or a `>', then this argument wasn't
10463 really finished. */
10464 if (!cp_parser_next_token_ends_template_argument_p (parser))
10465 cp_parser_error (parser, "expected template-argument");
10466 if (!cp_parser_error_occurred (parser))
10468 /* Figure out what is being referred to. If the id-expression
10469 was for a class template specialization, then we will have a
10470 TYPE_DECL at this point. There is no need to do name lookup
10471 at this point in that case. */
10472 if (TREE_CODE (argument) != TYPE_DECL)
10473 argument = cp_parser_lookup_name (parser, argument,
10475 /*is_template=*/template_p,
10476 /*is_namespace=*/false,
10477 /*check_dependency=*/true,
10478 /*ambiguous_decls=*/NULL,
10479 argument_start_token->location);
10480 if (TREE_CODE (argument) != TEMPLATE_DECL
10481 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10482 cp_parser_error (parser, "expected template-name");
10484 if (cp_parser_parse_definitely (parser))
10486 /* It must be a non-type argument. There permitted cases are given
10487 in [temp.arg.nontype]:
10489 -- an integral constant-expression of integral or enumeration
10492 -- the name of a non-type template-parameter; or
10494 -- the name of an object or function with external linkage...
10496 -- the address of an object or function with external linkage...
10498 -- a pointer to member... */
10499 /* Look for a non-type template parameter. */
10500 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10502 cp_parser_parse_tentatively (parser);
10503 argument = cp_parser_primary_expression (parser,
10504 /*address_p=*/false,
10506 /*template_arg_p=*/true,
10508 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10509 || !cp_parser_next_token_ends_template_argument_p (parser))
10510 cp_parser_simulate_error (parser);
10511 if (cp_parser_parse_definitely (parser))
10515 /* If the next token is "&", the argument must be the address of an
10516 object or function with external linkage. */
10517 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10519 cp_lexer_consume_token (parser->lexer);
10520 /* See if we might have an id-expression. */
10521 token = cp_lexer_peek_token (parser->lexer);
10522 if (token->type == CPP_NAME
10523 || token->keyword == RID_OPERATOR
10524 || token->type == CPP_SCOPE
10525 || token->type == CPP_TEMPLATE_ID
10526 || token->type == CPP_NESTED_NAME_SPECIFIER)
10528 cp_parser_parse_tentatively (parser);
10529 argument = cp_parser_primary_expression (parser,
10532 /*template_arg_p=*/true,
10534 if (cp_parser_error_occurred (parser)
10535 || !cp_parser_next_token_ends_template_argument_p (parser))
10536 cp_parser_abort_tentative_parse (parser);
10539 if (TREE_CODE (argument) == INDIRECT_REF)
10541 gcc_assert (REFERENCE_REF_P (argument));
10542 argument = TREE_OPERAND (argument, 0);
10545 if (TREE_CODE (argument) == VAR_DECL)
10547 /* A variable without external linkage might still be a
10548 valid constant-expression, so no error is issued here
10549 if the external-linkage check fails. */
10550 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10551 cp_parser_simulate_error (parser);
10553 else if (is_overloaded_fn (argument))
10554 /* All overloaded functions are allowed; if the external
10555 linkage test does not pass, an error will be issued
10559 && (TREE_CODE (argument) == OFFSET_REF
10560 || TREE_CODE (argument) == SCOPE_REF))
10561 /* A pointer-to-member. */
10563 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10566 cp_parser_simulate_error (parser);
10568 if (cp_parser_parse_definitely (parser))
10571 argument = build_x_unary_op (ADDR_EXPR, argument,
10572 tf_warning_or_error);
10577 /* If the argument started with "&", there are no other valid
10578 alternatives at this point. */
10581 cp_parser_error (parser, "invalid non-type template argument");
10582 return error_mark_node;
10585 /* If the argument wasn't successfully parsed as a type-id followed
10586 by '>>', the argument can only be a constant expression now.
10587 Otherwise, we try parsing the constant-expression tentatively,
10588 because the argument could really be a type-id. */
10590 cp_parser_parse_tentatively (parser);
10591 argument = cp_parser_constant_expression (parser,
10592 /*allow_non_constant_p=*/false,
10593 /*non_constant_p=*/NULL);
10594 argument = fold_non_dependent_expr (argument);
10595 if (!maybe_type_id)
10597 if (!cp_parser_next_token_ends_template_argument_p (parser))
10598 cp_parser_error (parser, "expected template-argument");
10599 if (cp_parser_parse_definitely (parser))
10601 /* We did our best to parse the argument as a non type-id, but that
10602 was the only alternative that matched (albeit with a '>' after
10603 it). We can assume it's just a typo from the user, and a
10604 diagnostic will then be issued. */
10605 return cp_parser_type_id (parser);
10608 /* Parse an explicit-instantiation.
10610 explicit-instantiation:
10611 template declaration
10613 Although the standard says `declaration', what it really means is:
10615 explicit-instantiation:
10616 template decl-specifier-seq [opt] declarator [opt] ;
10618 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10619 supposed to be allowed. A defect report has been filed about this
10624 explicit-instantiation:
10625 storage-class-specifier template
10626 decl-specifier-seq [opt] declarator [opt] ;
10627 function-specifier template
10628 decl-specifier-seq [opt] declarator [opt] ; */
10631 cp_parser_explicit_instantiation (cp_parser* parser)
10633 int declares_class_or_enum;
10634 cp_decl_specifier_seq decl_specifiers;
10635 tree extension_specifier = NULL_TREE;
10638 /* Look for an (optional) storage-class-specifier or
10639 function-specifier. */
10640 if (cp_parser_allow_gnu_extensions_p (parser))
10642 extension_specifier
10643 = cp_parser_storage_class_specifier_opt (parser);
10644 if (!extension_specifier)
10645 extension_specifier
10646 = cp_parser_function_specifier_opt (parser,
10647 /*decl_specs=*/NULL);
10650 /* Look for the `template' keyword. */
10651 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10652 /* Let the front end know that we are processing an explicit
10654 begin_explicit_instantiation ();
10655 /* [temp.explicit] says that we are supposed to ignore access
10656 control while processing explicit instantiation directives. */
10657 push_deferring_access_checks (dk_no_check);
10658 /* Parse a decl-specifier-seq. */
10659 token = cp_lexer_peek_token (parser->lexer);
10660 cp_parser_decl_specifier_seq (parser,
10661 CP_PARSER_FLAGS_OPTIONAL,
10663 &declares_class_or_enum);
10664 /* If there was exactly one decl-specifier, and it declared a class,
10665 and there's no declarator, then we have an explicit type
10667 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10671 type = check_tag_decl (&decl_specifiers);
10672 /* Turn access control back on for names used during
10673 template instantiation. */
10674 pop_deferring_access_checks ();
10676 do_type_instantiation (type, extension_specifier,
10677 /*complain=*/tf_error);
10681 cp_declarator *declarator;
10684 /* Parse the declarator. */
10686 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10687 /*ctor_dtor_or_conv_p=*/NULL,
10688 /*parenthesized_p=*/NULL,
10689 /*member_p=*/false);
10690 if (declares_class_or_enum & 2)
10691 cp_parser_check_for_definition_in_return_type (declarator,
10692 decl_specifiers.type,
10693 decl_specifiers.type_location);
10694 if (declarator != cp_error_declarator)
10696 decl = grokdeclarator (declarator, &decl_specifiers,
10697 NORMAL, 0, &decl_specifiers.attributes);
10698 /* Turn access control back on for names used during
10699 template instantiation. */
10700 pop_deferring_access_checks ();
10701 /* Do the explicit instantiation. */
10702 do_decl_instantiation (decl, extension_specifier);
10706 pop_deferring_access_checks ();
10707 /* Skip the body of the explicit instantiation. */
10708 cp_parser_skip_to_end_of_statement (parser);
10711 /* We're done with the instantiation. */
10712 end_explicit_instantiation ();
10714 cp_parser_consume_semicolon_at_end_of_statement (parser);
10717 /* Parse an explicit-specialization.
10719 explicit-specialization:
10720 template < > declaration
10722 Although the standard says `declaration', what it really means is:
10724 explicit-specialization:
10725 template <> decl-specifier [opt] init-declarator [opt] ;
10726 template <> function-definition
10727 template <> explicit-specialization
10728 template <> template-declaration */
10731 cp_parser_explicit_specialization (cp_parser* parser)
10733 bool need_lang_pop;
10734 cp_token *token = cp_lexer_peek_token (parser->lexer);
10736 /* Look for the `template' keyword. */
10737 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10738 /* Look for the `<'. */
10739 cp_parser_require (parser, CPP_LESS, "%<<%>");
10740 /* Look for the `>'. */
10741 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10742 /* We have processed another parameter list. */
10743 ++parser->num_template_parameter_lists;
10746 A template ... explicit specialization ... shall not have C
10748 if (current_lang_name == lang_name_c)
10750 error ("%Htemplate specialization with C linkage", &token->location);
10751 /* Give it C++ linkage to avoid confusing other parts of the
10753 push_lang_context (lang_name_cplusplus);
10754 need_lang_pop = true;
10757 need_lang_pop = false;
10758 /* Let the front end know that we are beginning a specialization. */
10759 if (!begin_specialization ())
10761 end_specialization ();
10762 cp_parser_skip_to_end_of_block_or_statement (parser);
10766 /* If the next keyword is `template', we need to figure out whether
10767 or not we're looking a template-declaration. */
10768 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10770 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10771 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10772 cp_parser_template_declaration_after_export (parser,
10773 /*member_p=*/false);
10775 cp_parser_explicit_specialization (parser);
10778 /* Parse the dependent declaration. */
10779 cp_parser_single_declaration (parser,
10781 /*member_p=*/false,
10782 /*explicit_specialization_p=*/true,
10783 /*friend_p=*/NULL);
10784 /* We're done with the specialization. */
10785 end_specialization ();
10786 /* For the erroneous case of a template with C linkage, we pushed an
10787 implicit C++ linkage scope; exit that scope now. */
10789 pop_lang_context ();
10790 /* We're done with this parameter list. */
10791 --parser->num_template_parameter_lists;
10794 /* Parse a type-specifier.
10797 simple-type-specifier
10800 elaborated-type-specifier
10808 Returns a representation of the type-specifier. For a
10809 class-specifier, enum-specifier, or elaborated-type-specifier, a
10810 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10812 The parser flags FLAGS is used to control type-specifier parsing.
10814 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10815 in a decl-specifier-seq.
10817 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10818 class-specifier, enum-specifier, or elaborated-type-specifier, then
10819 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10820 if a type is declared; 2 if it is defined. Otherwise, it is set to
10823 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10824 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10825 is set to FALSE. */
10828 cp_parser_type_specifier (cp_parser* parser,
10829 cp_parser_flags flags,
10830 cp_decl_specifier_seq *decl_specs,
10831 bool is_declaration,
10832 int* declares_class_or_enum,
10833 bool* is_cv_qualifier)
10835 tree type_spec = NULL_TREE;
10838 cp_decl_spec ds = ds_last;
10840 /* Assume this type-specifier does not declare a new type. */
10841 if (declares_class_or_enum)
10842 *declares_class_or_enum = 0;
10843 /* And that it does not specify a cv-qualifier. */
10844 if (is_cv_qualifier)
10845 *is_cv_qualifier = false;
10846 /* Peek at the next token. */
10847 token = cp_lexer_peek_token (parser->lexer);
10849 /* If we're looking at a keyword, we can use that to guide the
10850 production we choose. */
10851 keyword = token->keyword;
10855 /* Look for the enum-specifier. */
10856 type_spec = cp_parser_enum_specifier (parser);
10857 /* If that worked, we're done. */
10860 if (declares_class_or_enum)
10861 *declares_class_or_enum = 2;
10863 cp_parser_set_decl_spec_type (decl_specs,
10866 /*user_defined_p=*/true);
10870 goto elaborated_type_specifier;
10872 /* Any of these indicate either a class-specifier, or an
10873 elaborated-type-specifier. */
10877 /* Parse tentatively so that we can back up if we don't find a
10878 class-specifier. */
10879 cp_parser_parse_tentatively (parser);
10880 /* Look for the class-specifier. */
10881 type_spec = cp_parser_class_specifier (parser);
10882 /* If that worked, we're done. */
10883 if (cp_parser_parse_definitely (parser))
10885 if (declares_class_or_enum)
10886 *declares_class_or_enum = 2;
10888 cp_parser_set_decl_spec_type (decl_specs,
10891 /*user_defined_p=*/true);
10895 /* Fall through. */
10896 elaborated_type_specifier:
10897 /* We're declaring (not defining) a class or enum. */
10898 if (declares_class_or_enum)
10899 *declares_class_or_enum = 1;
10901 /* Fall through. */
10903 /* Look for an elaborated-type-specifier. */
10905 = (cp_parser_elaborated_type_specifier
10907 decl_specs && decl_specs->specs[(int) ds_friend],
10910 cp_parser_set_decl_spec_type (decl_specs,
10913 /*user_defined_p=*/true);
10918 if (is_cv_qualifier)
10919 *is_cv_qualifier = true;
10924 if (is_cv_qualifier)
10925 *is_cv_qualifier = true;
10930 if (is_cv_qualifier)
10931 *is_cv_qualifier = true;
10935 /* The `__complex__' keyword is a GNU extension. */
10943 /* Handle simple keywords. */
10948 ++decl_specs->specs[(int)ds];
10949 decl_specs->any_specifiers_p = true;
10951 return cp_lexer_consume_token (parser->lexer)->u.value;
10954 /* If we do not already have a type-specifier, assume we are looking
10955 at a simple-type-specifier. */
10956 type_spec = cp_parser_simple_type_specifier (parser,
10960 /* If we didn't find a type-specifier, and a type-specifier was not
10961 optional in this context, issue an error message. */
10962 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10964 cp_parser_error (parser, "expected type specifier");
10965 return error_mark_node;
10971 /* Parse a simple-type-specifier.
10973 simple-type-specifier:
10974 :: [opt] nested-name-specifier [opt] type-name
10975 :: [opt] nested-name-specifier template template-id
10990 simple-type-specifier:
10992 decltype ( expression )
10998 simple-type-specifier:
10999 __typeof__ unary-expression
11000 __typeof__ ( type-id )
11002 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11003 appropriately updated. */
11006 cp_parser_simple_type_specifier (cp_parser* parser,
11007 cp_decl_specifier_seq *decl_specs,
11008 cp_parser_flags flags)
11010 tree type = NULL_TREE;
11013 /* Peek at the next token. */
11014 token = cp_lexer_peek_token (parser->lexer);
11016 /* If we're looking at a keyword, things are easy. */
11017 switch (token->keyword)
11021 decl_specs->explicit_char_p = true;
11022 type = char_type_node;
11025 type = char16_type_node;
11028 type = char32_type_node;
11031 type = wchar_type_node;
11034 type = boolean_type_node;
11038 ++decl_specs->specs[(int) ds_short];
11039 type = short_integer_type_node;
11043 decl_specs->explicit_int_p = true;
11044 type = integer_type_node;
11048 ++decl_specs->specs[(int) ds_long];
11049 type = long_integer_type_node;
11053 ++decl_specs->specs[(int) ds_signed];
11054 type = integer_type_node;
11058 ++decl_specs->specs[(int) ds_unsigned];
11059 type = unsigned_type_node;
11062 type = float_type_node;
11065 type = double_type_node;
11068 type = void_type_node;
11072 if (cxx_dialect != cxx98)
11074 /* Consume the token. */
11075 cp_lexer_consume_token (parser->lexer);
11076 /* We do not yet support the use of `auto' as a
11078 error ("%HC++0x %<auto%> specifier not supported", &token->location);
11083 /* Parse the `decltype' type. */
11084 type = cp_parser_decltype (parser);
11087 cp_parser_set_decl_spec_type (decl_specs, type,
11089 /*user_defined_p=*/true);
11094 /* Consume the `typeof' token. */
11095 cp_lexer_consume_token (parser->lexer);
11096 /* Parse the operand to `typeof'. */
11097 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11098 /* If it is not already a TYPE, take its type. */
11099 if (!TYPE_P (type))
11100 type = finish_typeof (type);
11103 cp_parser_set_decl_spec_type (decl_specs, type,
11105 /*user_defined_p=*/true);
11113 /* If the type-specifier was for a built-in type, we're done. */
11118 /* Record the type. */
11120 && (token->keyword != RID_SIGNED
11121 && token->keyword != RID_UNSIGNED
11122 && token->keyword != RID_SHORT
11123 && token->keyword != RID_LONG))
11124 cp_parser_set_decl_spec_type (decl_specs,
11127 /*user_defined=*/false);
11129 decl_specs->any_specifiers_p = true;
11131 /* Consume the token. */
11132 id = cp_lexer_consume_token (parser->lexer)->u.value;
11134 /* There is no valid C++ program where a non-template type is
11135 followed by a "<". That usually indicates that the user thought
11136 that the type was a template. */
11137 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11139 return TYPE_NAME (type);
11142 /* The type-specifier must be a user-defined type. */
11143 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11148 /* Don't gobble tokens or issue error messages if this is an
11149 optional type-specifier. */
11150 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11151 cp_parser_parse_tentatively (parser);
11153 /* Look for the optional `::' operator. */
11155 = (cp_parser_global_scope_opt (parser,
11156 /*current_scope_valid_p=*/false)
11158 /* Look for the nested-name specifier. */
11160 = (cp_parser_nested_name_specifier_opt (parser,
11161 /*typename_keyword_p=*/false,
11162 /*check_dependency_p=*/true,
11164 /*is_declaration=*/false)
11166 token = cp_lexer_peek_token (parser->lexer);
11167 /* If we have seen a nested-name-specifier, and the next token
11168 is `template', then we are using the template-id production. */
11170 && cp_parser_optional_template_keyword (parser))
11172 /* Look for the template-id. */
11173 type = cp_parser_template_id (parser,
11174 /*template_keyword_p=*/true,
11175 /*check_dependency_p=*/true,
11176 /*is_declaration=*/false);
11177 /* If the template-id did not name a type, we are out of
11179 if (TREE_CODE (type) != TYPE_DECL)
11181 cp_parser_error (parser, "expected template-id for type");
11185 /* Otherwise, look for a type-name. */
11187 type = cp_parser_type_name (parser);
11188 /* Keep track of all name-lookups performed in class scopes. */
11192 && TREE_CODE (type) == TYPE_DECL
11193 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11194 maybe_note_name_used_in_class (DECL_NAME (type), type);
11195 /* If it didn't work out, we don't have a TYPE. */
11196 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11197 && !cp_parser_parse_definitely (parser))
11199 if (type && decl_specs)
11200 cp_parser_set_decl_spec_type (decl_specs, type,
11202 /*user_defined=*/true);
11205 /* If we didn't get a type-name, issue an error message. */
11206 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11208 cp_parser_error (parser, "expected type-name");
11209 return error_mark_node;
11212 /* There is no valid C++ program where a non-template type is
11213 followed by a "<". That usually indicates that the user thought
11214 that the type was a template. */
11215 if (type && type != error_mark_node)
11217 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11218 If it is, then the '<'...'>' enclose protocol names rather than
11219 template arguments, and so everything is fine. */
11220 if (c_dialect_objc ()
11221 && (objc_is_id (type) || objc_is_class_name (type)))
11223 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11224 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11226 /* Clobber the "unqualified" type previously entered into
11227 DECL_SPECS with the new, improved protocol-qualified version. */
11229 decl_specs->type = qual_type;
11234 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11241 /* Parse a type-name.
11254 Returns a TYPE_DECL for the type. */
11257 cp_parser_type_name (cp_parser* parser)
11261 /* We can't know yet whether it is a class-name or not. */
11262 cp_parser_parse_tentatively (parser);
11263 /* Try a class-name. */
11264 type_decl = cp_parser_class_name (parser,
11265 /*typename_keyword_p=*/false,
11266 /*template_keyword_p=*/false,
11268 /*check_dependency_p=*/true,
11269 /*class_head_p=*/false,
11270 /*is_declaration=*/false);
11271 /* If it's not a class-name, keep looking. */
11272 if (!cp_parser_parse_definitely (parser))
11274 /* It must be a typedef-name or an enum-name. */
11275 return cp_parser_nonclass_name (parser);
11281 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11289 Returns a TYPE_DECL for the type. */
11292 cp_parser_nonclass_name (cp_parser* parser)
11297 cp_token *token = cp_lexer_peek_token (parser->lexer);
11298 identifier = cp_parser_identifier (parser);
11299 if (identifier == error_mark_node)
11300 return error_mark_node;
11302 /* Look up the type-name. */
11303 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11305 if (TREE_CODE (type_decl) != TYPE_DECL
11306 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11308 /* See if this is an Objective-C type. */
11309 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11310 tree type = objc_get_protocol_qualified_type (identifier, protos);
11312 type_decl = TYPE_NAME (type);
11315 /* Issue an error if we did not find a type-name. */
11316 if (TREE_CODE (type_decl) != TYPE_DECL)
11318 if (!cp_parser_simulate_error (parser))
11319 cp_parser_name_lookup_error (parser, identifier, type_decl,
11320 "is not a type", token->location);
11321 return error_mark_node;
11323 /* Remember that the name was used in the definition of the
11324 current class so that we can check later to see if the
11325 meaning would have been different after the class was
11326 entirely defined. */
11327 else if (type_decl != error_mark_node
11329 maybe_note_name_used_in_class (identifier, type_decl);
11334 /* Parse an elaborated-type-specifier. Note that the grammar given
11335 here incorporates the resolution to DR68.
11337 elaborated-type-specifier:
11338 class-key :: [opt] nested-name-specifier [opt] identifier
11339 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11340 enum-key :: [opt] nested-name-specifier [opt] identifier
11341 typename :: [opt] nested-name-specifier identifier
11342 typename :: [opt] nested-name-specifier template [opt]
11347 elaborated-type-specifier:
11348 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11349 class-key attributes :: [opt] nested-name-specifier [opt]
11350 template [opt] template-id
11351 enum attributes :: [opt] nested-name-specifier [opt] identifier
11353 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11354 declared `friend'. If IS_DECLARATION is TRUE, then this
11355 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11356 something is being declared.
11358 Returns the TYPE specified. */
11361 cp_parser_elaborated_type_specifier (cp_parser* parser,
11363 bool is_declaration)
11365 enum tag_types tag_type;
11367 tree type = NULL_TREE;
11368 tree attributes = NULL_TREE;
11369 cp_token *token = NULL;
11371 /* See if we're looking at the `enum' keyword. */
11372 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11374 /* Consume the `enum' token. */
11375 cp_lexer_consume_token (parser->lexer);
11376 /* Remember that it's an enumeration type. */
11377 tag_type = enum_type;
11378 /* Parse the optional `struct' or `class' key (for C++0x scoped
11380 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11381 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11383 if (cxx_dialect == cxx98)
11384 maybe_warn_cpp0x ("scoped enums");
11386 /* Consume the `struct' or `class'. */
11387 cp_lexer_consume_token (parser->lexer);
11389 /* Parse the attributes. */
11390 attributes = cp_parser_attributes_opt (parser);
11392 /* Or, it might be `typename'. */
11393 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11396 /* Consume the `typename' token. */
11397 cp_lexer_consume_token (parser->lexer);
11398 /* Remember that it's a `typename' type. */
11399 tag_type = typename_type;
11400 /* The `typename' keyword is only allowed in templates. */
11401 if (!processing_template_decl)
11402 permerror (input_location, "using %<typename%> outside of template");
11404 /* Otherwise it must be a class-key. */
11407 tag_type = cp_parser_class_key (parser);
11408 if (tag_type == none_type)
11409 return error_mark_node;
11410 /* Parse the attributes. */
11411 attributes = cp_parser_attributes_opt (parser);
11414 /* Look for the `::' operator. */
11415 cp_parser_global_scope_opt (parser,
11416 /*current_scope_valid_p=*/false);
11417 /* Look for the nested-name-specifier. */
11418 if (tag_type == typename_type)
11420 if (!cp_parser_nested_name_specifier (parser,
11421 /*typename_keyword_p=*/true,
11422 /*check_dependency_p=*/true,
11425 return error_mark_node;
11428 /* Even though `typename' is not present, the proposed resolution
11429 to Core Issue 180 says that in `class A<T>::B', `B' should be
11430 considered a type-name, even if `A<T>' is dependent. */
11431 cp_parser_nested_name_specifier_opt (parser,
11432 /*typename_keyword_p=*/true,
11433 /*check_dependency_p=*/true,
11436 /* For everything but enumeration types, consider a template-id.
11437 For an enumeration type, consider only a plain identifier. */
11438 if (tag_type != enum_type)
11440 bool template_p = false;
11443 /* Allow the `template' keyword. */
11444 template_p = cp_parser_optional_template_keyword (parser);
11445 /* If we didn't see `template', we don't know if there's a
11446 template-id or not. */
11448 cp_parser_parse_tentatively (parser);
11449 /* Parse the template-id. */
11450 token = cp_lexer_peek_token (parser->lexer);
11451 decl = cp_parser_template_id (parser, template_p,
11452 /*check_dependency_p=*/true,
11454 /* If we didn't find a template-id, look for an ordinary
11456 if (!template_p && !cp_parser_parse_definitely (parser))
11458 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11459 in effect, then we must assume that, upon instantiation, the
11460 template will correspond to a class. */
11461 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11462 && tag_type == typename_type)
11463 type = make_typename_type (parser->scope, decl,
11465 /*complain=*/tf_error);
11467 type = TREE_TYPE (decl);
11472 token = cp_lexer_peek_token (parser->lexer);
11473 identifier = cp_parser_identifier (parser);
11475 if (identifier == error_mark_node)
11477 parser->scope = NULL_TREE;
11478 return error_mark_node;
11481 /* For a `typename', we needn't call xref_tag. */
11482 if (tag_type == typename_type
11483 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11484 return cp_parser_make_typename_type (parser, parser->scope,
11487 /* Look up a qualified name in the usual way. */
11491 tree ambiguous_decls;
11493 decl = cp_parser_lookup_name (parser, identifier,
11495 /*is_template=*/false,
11496 /*is_namespace=*/false,
11497 /*check_dependency=*/true,
11501 /* If the lookup was ambiguous, an error will already have been
11503 if (ambiguous_decls)
11504 return error_mark_node;
11506 /* If we are parsing friend declaration, DECL may be a
11507 TEMPLATE_DECL tree node here. However, we need to check
11508 whether this TEMPLATE_DECL results in valid code. Consider
11509 the following example:
11512 template <class T> class C {};
11515 template <class T> friend class N::C; // #1, valid code
11517 template <class T> class Y {
11518 friend class N::C; // #2, invalid code
11521 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11522 name lookup of `N::C'. We see that friend declaration must
11523 be template for the code to be valid. Note that
11524 processing_template_decl does not work here since it is
11525 always 1 for the above two cases. */
11527 decl = (cp_parser_maybe_treat_template_as_class
11528 (decl, /*tag_name_p=*/is_friend
11529 && parser->num_template_parameter_lists));
11531 if (TREE_CODE (decl) != TYPE_DECL)
11533 cp_parser_diagnose_invalid_type_name (parser,
11537 return error_mark_node;
11540 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11542 bool allow_template = (parser->num_template_parameter_lists
11543 || DECL_SELF_REFERENCE_P (decl));
11544 type = check_elaborated_type_specifier (tag_type, decl,
11547 if (type == error_mark_node)
11548 return error_mark_node;
11551 /* Forward declarations of nested types, such as
11556 are invalid unless all components preceding the final '::'
11557 are complete. If all enclosing types are complete, these
11558 declarations become merely pointless.
11560 Invalid forward declarations of nested types are errors
11561 caught elsewhere in parsing. Those that are pointless arrive
11564 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11565 && !is_friend && !processing_explicit_instantiation)
11566 warning (0, "declaration %qD does not declare anything", decl);
11568 type = TREE_TYPE (decl);
11572 /* An elaborated-type-specifier sometimes introduces a new type and
11573 sometimes names an existing type. Normally, the rule is that it
11574 introduces a new type only if there is not an existing type of
11575 the same name already in scope. For example, given:
11578 void f() { struct S s; }
11580 the `struct S' in the body of `f' is the same `struct S' as in
11581 the global scope; the existing definition is used. However, if
11582 there were no global declaration, this would introduce a new
11583 local class named `S'.
11585 An exception to this rule applies to the following code:
11587 namespace N { struct S; }
11589 Here, the elaborated-type-specifier names a new type
11590 unconditionally; even if there is already an `S' in the
11591 containing scope this declaration names a new type.
11592 This exception only applies if the elaborated-type-specifier
11593 forms the complete declaration:
11597 A declaration consisting solely of `class-key identifier ;' is
11598 either a redeclaration of the name in the current scope or a
11599 forward declaration of the identifier as a class name. It
11600 introduces the name into the current scope.
11602 We are in this situation precisely when the next token is a `;'.
11604 An exception to the exception is that a `friend' declaration does
11605 *not* name a new type; i.e., given:
11607 struct S { friend struct T; };
11609 `T' is not a new type in the scope of `S'.
11611 Also, `new struct S' or `sizeof (struct S)' never results in the
11612 definition of a new type; a new type can only be declared in a
11613 declaration context. */
11619 /* Friends have special name lookup rules. */
11620 ts = ts_within_enclosing_non_class;
11621 else if (is_declaration
11622 && cp_lexer_next_token_is (parser->lexer,
11624 /* This is a `class-key identifier ;' */
11630 (parser->num_template_parameter_lists
11631 && (cp_parser_next_token_starts_class_definition_p (parser)
11632 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11633 /* An unqualified name was used to reference this type, so
11634 there were no qualifying templates. */
11635 if (!cp_parser_check_template_parameters (parser,
11636 /*num_templates=*/0,
11638 return error_mark_node;
11639 type = xref_tag (tag_type, identifier, ts, template_p);
11643 if (type == error_mark_node)
11644 return error_mark_node;
11646 /* Allow attributes on forward declarations of classes. */
11649 if (TREE_CODE (type) == TYPENAME_TYPE)
11650 warning (OPT_Wattributes,
11651 "attributes ignored on uninstantiated type");
11652 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11653 && ! processing_explicit_instantiation)
11654 warning (OPT_Wattributes,
11655 "attributes ignored on template instantiation");
11656 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11657 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11659 warning (OPT_Wattributes,
11660 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11663 if (tag_type != enum_type)
11664 cp_parser_check_class_key (tag_type, type);
11666 /* A "<" cannot follow an elaborated type specifier. If that
11667 happens, the user was probably trying to form a template-id. */
11668 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11673 /* Parse an enum-specifier.
11676 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11681 enum struct [C++0x]
11684 : type-specifier-seq
11687 enum-key attributes[opt] identifier [opt] enum-base [opt]
11688 { enumerator-list [opt] }attributes[opt]
11690 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11691 if the token stream isn't an enum-specifier after all. */
11694 cp_parser_enum_specifier (cp_parser* parser)
11699 bool scoped_enum_p = false;
11700 tree underlying_type = NULL_TREE;
11702 /* Parse tentatively so that we can back up if we don't find a
11704 cp_parser_parse_tentatively (parser);
11706 /* Caller guarantees that the current token is 'enum', an identifier
11707 possibly follows, and the token after that is an opening brace.
11708 If we don't have an identifier, fabricate an anonymous name for
11709 the enumeration being defined. */
11710 cp_lexer_consume_token (parser->lexer);
11712 /* Parse the "class" or "struct", which indicates a scoped
11713 enumeration type in C++0x. */
11714 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11715 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11717 if (cxx_dialect == cxx98)
11718 maybe_warn_cpp0x ("scoped enums");
11720 /* Consume the `struct' or `class' token. */
11721 cp_lexer_consume_token (parser->lexer);
11723 scoped_enum_p = true;
11726 attributes = cp_parser_attributes_opt (parser);
11728 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11729 identifier = cp_parser_identifier (parser);
11731 identifier = make_anon_name ();
11733 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11734 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11736 cp_decl_specifier_seq type_specifiers;
11738 if (cxx_dialect == cxx98)
11739 maybe_warn_cpp0x ("scoped enums");
11741 /* Consume the `:'. */
11742 cp_lexer_consume_token (parser->lexer);
11744 /* Parse the type-specifier-seq. */
11745 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11747 if (type_specifiers.type == error_mark_node)
11748 return error_mark_node;
11750 /* If that didn't work, stop. */
11751 if (type_specifiers.type != error_mark_node)
11753 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11754 /*initialized=*/0, NULL);
11755 if (underlying_type == error_mark_node)
11756 underlying_type = NULL_TREE;
11759 cp_parser_error (parser, "expected underlying type of enumeration");
11762 /* Look for the `{' but don't consume it yet. */
11763 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11764 cp_parser_simulate_error (parser);
11766 if (!cp_parser_parse_definitely (parser))
11769 /* Issue an error message if type-definitions are forbidden here. */
11770 if (!cp_parser_check_type_definition (parser))
11771 type = error_mark_node;
11773 /* Create the new type. We do this before consuming the opening
11774 brace so the enum will be recorded as being on the line of its
11775 tag (or the 'enum' keyword, if there is no tag). */
11776 type = start_enum (identifier, underlying_type, scoped_enum_p);
11778 /* Consume the opening brace. */
11779 cp_lexer_consume_token (parser->lexer);
11781 if (type == error_mark_node)
11783 cp_parser_skip_to_end_of_block_or_statement (parser);
11784 return error_mark_node;
11787 /* If the next token is not '}', then there are some enumerators. */
11788 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11789 cp_parser_enumerator_list (parser, type);
11791 /* Consume the final '}'. */
11792 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11794 /* Look for trailing attributes to apply to this enumeration, and
11795 apply them if appropriate. */
11796 if (cp_parser_allow_gnu_extensions_p (parser))
11798 tree trailing_attr = cp_parser_attributes_opt (parser);
11799 cplus_decl_attributes (&type,
11801 (int) ATTR_FLAG_TYPE_IN_PLACE);
11804 /* Finish up the enumeration. */
11805 finish_enum (type);
11810 /* Parse an enumerator-list. The enumerators all have the indicated
11814 enumerator-definition
11815 enumerator-list , enumerator-definition */
11818 cp_parser_enumerator_list (cp_parser* parser, tree type)
11822 /* Parse an enumerator-definition. */
11823 cp_parser_enumerator_definition (parser, type);
11825 /* If the next token is not a ',', we've reached the end of
11827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11829 /* Otherwise, consume the `,' and keep going. */
11830 cp_lexer_consume_token (parser->lexer);
11831 /* If the next token is a `}', there is a trailing comma. */
11832 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11834 if (!in_system_header)
11835 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11841 /* Parse an enumerator-definition. The enumerator has the indicated
11844 enumerator-definition:
11846 enumerator = constant-expression
11852 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11857 /* Look for the identifier. */
11858 identifier = cp_parser_identifier (parser);
11859 if (identifier == error_mark_node)
11862 /* If the next token is an '=', then there is an explicit value. */
11863 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11865 /* Consume the `=' token. */
11866 cp_lexer_consume_token (parser->lexer);
11867 /* Parse the value. */
11868 value = cp_parser_constant_expression (parser,
11869 /*allow_non_constant_p=*/false,
11875 /* Create the enumerator. */
11876 build_enumerator (identifier, value, type);
11879 /* Parse a namespace-name.
11882 original-namespace-name
11885 Returns the NAMESPACE_DECL for the namespace. */
11888 cp_parser_namespace_name (cp_parser* parser)
11891 tree namespace_decl;
11893 cp_token *token = cp_lexer_peek_token (parser->lexer);
11895 /* Get the name of the namespace. */
11896 identifier = cp_parser_identifier (parser);
11897 if (identifier == error_mark_node)
11898 return error_mark_node;
11900 /* Look up the identifier in the currently active scope. Look only
11901 for namespaces, due to:
11903 [basic.lookup.udir]
11905 When looking up a namespace-name in a using-directive or alias
11906 definition, only namespace names are considered.
11910 [basic.lookup.qual]
11912 During the lookup of a name preceding the :: scope resolution
11913 operator, object, function, and enumerator names are ignored.
11915 (Note that cp_parser_qualifying_entity only calls this
11916 function if the token after the name is the scope resolution
11918 namespace_decl = cp_parser_lookup_name (parser, identifier,
11920 /*is_template=*/false,
11921 /*is_namespace=*/true,
11922 /*check_dependency=*/true,
11923 /*ambiguous_decls=*/NULL,
11925 /* If it's not a namespace, issue an error. */
11926 if (namespace_decl == error_mark_node
11927 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11929 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11930 error ("%H%qD is not a namespace-name", &token->location, identifier);
11931 cp_parser_error (parser, "expected namespace-name");
11932 namespace_decl = error_mark_node;
11935 return namespace_decl;
11938 /* Parse a namespace-definition.
11940 namespace-definition:
11941 named-namespace-definition
11942 unnamed-namespace-definition
11944 named-namespace-definition:
11945 original-namespace-definition
11946 extension-namespace-definition
11948 original-namespace-definition:
11949 namespace identifier { namespace-body }
11951 extension-namespace-definition:
11952 namespace original-namespace-name { namespace-body }
11954 unnamed-namespace-definition:
11955 namespace { namespace-body } */
11958 cp_parser_namespace_definition (cp_parser* parser)
11960 tree identifier, attribs;
11961 bool has_visibility;
11964 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11967 cp_lexer_consume_token (parser->lexer);
11972 /* Look for the `namespace' keyword. */
11973 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11975 /* Get the name of the namespace. We do not attempt to distinguish
11976 between an original-namespace-definition and an
11977 extension-namespace-definition at this point. The semantic
11978 analysis routines are responsible for that. */
11979 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11980 identifier = cp_parser_identifier (parser);
11982 identifier = NULL_TREE;
11984 /* Parse any specified attributes. */
11985 attribs = cp_parser_attributes_opt (parser);
11987 /* Look for the `{' to start the namespace. */
11988 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11989 /* Start the namespace. */
11990 push_namespace (identifier);
11992 /* "inline namespace" is equivalent to a stub namespace definition
11993 followed by a strong using directive. */
11996 tree name_space = current_namespace;
11997 /* Set up namespace association. */
11998 DECL_NAMESPACE_ASSOCIATIONS (name_space)
11999 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12000 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12001 /* Import the contents of the inline namespace. */
12003 do_using_directive (name_space);
12004 push_namespace (identifier);
12007 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12009 /* Parse the body of the namespace. */
12010 cp_parser_namespace_body (parser);
12012 #ifdef HANDLE_PRAGMA_VISIBILITY
12013 if (has_visibility)
12017 /* Finish the namespace. */
12019 /* Look for the final `}'. */
12020 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12023 /* Parse a namespace-body.
12026 declaration-seq [opt] */
12029 cp_parser_namespace_body (cp_parser* parser)
12031 cp_parser_declaration_seq_opt (parser);
12034 /* Parse a namespace-alias-definition.
12036 namespace-alias-definition:
12037 namespace identifier = qualified-namespace-specifier ; */
12040 cp_parser_namespace_alias_definition (cp_parser* parser)
12043 tree namespace_specifier;
12045 cp_token *token = cp_lexer_peek_token (parser->lexer);
12047 /* Look for the `namespace' keyword. */
12048 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12049 /* Look for the identifier. */
12050 identifier = cp_parser_identifier (parser);
12051 if (identifier == error_mark_node)
12053 /* Look for the `=' token. */
12054 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12055 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12057 error ("%H%<namespace%> definition is not allowed here", &token->location);
12058 /* Skip the definition. */
12059 cp_lexer_consume_token (parser->lexer);
12060 if (cp_parser_skip_to_closing_brace (parser))
12061 cp_lexer_consume_token (parser->lexer);
12064 cp_parser_require (parser, CPP_EQ, "%<=%>");
12065 /* Look for the qualified-namespace-specifier. */
12066 namespace_specifier
12067 = cp_parser_qualified_namespace_specifier (parser);
12068 /* Look for the `;' token. */
12069 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12071 /* Register the alias in the symbol table. */
12072 do_namespace_alias (identifier, namespace_specifier);
12075 /* Parse a qualified-namespace-specifier.
12077 qualified-namespace-specifier:
12078 :: [opt] nested-name-specifier [opt] namespace-name
12080 Returns a NAMESPACE_DECL corresponding to the specified
12084 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12086 /* Look for the optional `::'. */
12087 cp_parser_global_scope_opt (parser,
12088 /*current_scope_valid_p=*/false);
12090 /* Look for the optional nested-name-specifier. */
12091 cp_parser_nested_name_specifier_opt (parser,
12092 /*typename_keyword_p=*/false,
12093 /*check_dependency_p=*/true,
12095 /*is_declaration=*/true);
12097 return cp_parser_namespace_name (parser);
12100 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12101 access declaration.
12104 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12105 using :: unqualified-id ;
12107 access-declaration:
12113 cp_parser_using_declaration (cp_parser* parser,
12114 bool access_declaration_p)
12117 bool typename_p = false;
12118 bool global_scope_p;
12123 if (access_declaration_p)
12124 cp_parser_parse_tentatively (parser);
12127 /* Look for the `using' keyword. */
12128 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12130 /* Peek at the next token. */
12131 token = cp_lexer_peek_token (parser->lexer);
12132 /* See if it's `typename'. */
12133 if (token->keyword == RID_TYPENAME)
12135 /* Remember that we've seen it. */
12137 /* Consume the `typename' token. */
12138 cp_lexer_consume_token (parser->lexer);
12142 /* Look for the optional global scope qualification. */
12144 = (cp_parser_global_scope_opt (parser,
12145 /*current_scope_valid_p=*/false)
12148 /* If we saw `typename', or didn't see `::', then there must be a
12149 nested-name-specifier present. */
12150 if (typename_p || !global_scope_p)
12151 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12152 /*check_dependency_p=*/true,
12154 /*is_declaration=*/true);
12155 /* Otherwise, we could be in either of the two productions. In that
12156 case, treat the nested-name-specifier as optional. */
12158 qscope = cp_parser_nested_name_specifier_opt (parser,
12159 /*typename_keyword_p=*/false,
12160 /*check_dependency_p=*/true,
12162 /*is_declaration=*/true);
12164 qscope = global_namespace;
12166 if (access_declaration_p && cp_parser_error_occurred (parser))
12167 /* Something has already gone wrong; there's no need to parse
12168 further. Since an error has occurred, the return value of
12169 cp_parser_parse_definitely will be false, as required. */
12170 return cp_parser_parse_definitely (parser);
12172 token = cp_lexer_peek_token (parser->lexer);
12173 /* Parse the unqualified-id. */
12174 identifier = cp_parser_unqualified_id (parser,
12175 /*template_keyword_p=*/false,
12176 /*check_dependency_p=*/true,
12177 /*declarator_p=*/true,
12178 /*optional_p=*/false);
12180 if (access_declaration_p)
12182 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12183 cp_parser_simulate_error (parser);
12184 if (!cp_parser_parse_definitely (parser))
12188 /* The function we call to handle a using-declaration is different
12189 depending on what scope we are in. */
12190 if (qscope == error_mark_node || identifier == error_mark_node)
12192 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12193 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12194 /* [namespace.udecl]
12196 A using declaration shall not name a template-id. */
12197 error ("%Ha template-id may not appear in a using-declaration",
12201 if (at_class_scope_p ())
12203 /* Create the USING_DECL. */
12204 decl = do_class_using_decl (parser->scope, identifier);
12206 if (check_for_bare_parameter_packs (decl))
12209 /* Add it to the list of members in this class. */
12210 finish_member_declaration (decl);
12214 decl = cp_parser_lookup_name_simple (parser,
12217 if (decl == error_mark_node)
12218 cp_parser_name_lookup_error (parser, identifier,
12221 else if (check_for_bare_parameter_packs (decl))
12223 else if (!at_namespace_scope_p ())
12224 do_local_using_decl (decl, qscope, identifier);
12226 do_toplevel_using_decl (decl, qscope, identifier);
12230 /* Look for the final `;'. */
12231 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12236 /* Parse a using-directive.
12239 using namespace :: [opt] nested-name-specifier [opt]
12240 namespace-name ; */
12243 cp_parser_using_directive (cp_parser* parser)
12245 tree namespace_decl;
12248 /* Look for the `using' keyword. */
12249 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12250 /* And the `namespace' keyword. */
12251 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12252 /* Look for the optional `::' operator. */
12253 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12254 /* And the optional nested-name-specifier. */
12255 cp_parser_nested_name_specifier_opt (parser,
12256 /*typename_keyword_p=*/false,
12257 /*check_dependency_p=*/true,
12259 /*is_declaration=*/true);
12260 /* Get the namespace being used. */
12261 namespace_decl = cp_parser_namespace_name (parser);
12262 /* And any specified attributes. */
12263 attribs = cp_parser_attributes_opt (parser);
12264 /* Update the symbol table. */
12265 parse_using_directive (namespace_decl, attribs);
12266 /* Look for the final `;'. */
12267 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12270 /* Parse an asm-definition.
12273 asm ( string-literal ) ;
12278 asm volatile [opt] ( string-literal ) ;
12279 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12280 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12281 : asm-operand-list [opt] ) ;
12282 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12283 : asm-operand-list [opt]
12284 : asm-operand-list [opt] ) ; */
12287 cp_parser_asm_definition (cp_parser* parser)
12290 tree outputs = NULL_TREE;
12291 tree inputs = NULL_TREE;
12292 tree clobbers = NULL_TREE;
12294 bool volatile_p = false;
12295 bool extended_p = false;
12296 bool invalid_inputs_p = false;
12297 bool invalid_outputs_p = false;
12299 /* Look for the `asm' keyword. */
12300 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12301 /* See if the next token is `volatile'. */
12302 if (cp_parser_allow_gnu_extensions_p (parser)
12303 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12305 /* Remember that we saw the `volatile' keyword. */
12307 /* Consume the token. */
12308 cp_lexer_consume_token (parser->lexer);
12310 /* Look for the opening `('. */
12311 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12313 /* Look for the string. */
12314 string = cp_parser_string_literal (parser, false, false);
12315 if (string == error_mark_node)
12317 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12318 /*consume_paren=*/true);
12322 /* If we're allowing GNU extensions, check for the extended assembly
12323 syntax. Unfortunately, the `:' tokens need not be separated by
12324 a space in C, and so, for compatibility, we tolerate that here
12325 too. Doing that means that we have to treat the `::' operator as
12327 if (cp_parser_allow_gnu_extensions_p (parser)
12328 && parser->in_function_body
12329 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12330 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12332 bool inputs_p = false;
12333 bool clobbers_p = false;
12335 /* The extended syntax was used. */
12338 /* Look for outputs. */
12339 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12341 /* Consume the `:'. */
12342 cp_lexer_consume_token (parser->lexer);
12343 /* Parse the output-operands. */
12344 if (cp_lexer_next_token_is_not (parser->lexer,
12346 && cp_lexer_next_token_is_not (parser->lexer,
12348 && cp_lexer_next_token_is_not (parser->lexer,
12350 outputs = cp_parser_asm_operand_list (parser);
12352 if (outputs == error_mark_node)
12353 invalid_outputs_p = true;
12355 /* If the next token is `::', there are no outputs, and the
12356 next token is the beginning of the inputs. */
12357 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12358 /* The inputs are coming next. */
12361 /* Look for inputs. */
12363 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12365 /* Consume the `:' or `::'. */
12366 cp_lexer_consume_token (parser->lexer);
12367 /* Parse the output-operands. */
12368 if (cp_lexer_next_token_is_not (parser->lexer,
12370 && cp_lexer_next_token_is_not (parser->lexer,
12372 inputs = cp_parser_asm_operand_list (parser);
12374 if (inputs == error_mark_node)
12375 invalid_inputs_p = true;
12377 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12378 /* The clobbers are coming next. */
12381 /* Look for clobbers. */
12383 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12385 /* Consume the `:' or `::'. */
12386 cp_lexer_consume_token (parser->lexer);
12387 /* Parse the clobbers. */
12388 if (cp_lexer_next_token_is_not (parser->lexer,
12390 clobbers = cp_parser_asm_clobber_list (parser);
12393 /* Look for the closing `)'. */
12394 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12395 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12396 /*consume_paren=*/true);
12397 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12399 if (!invalid_inputs_p && !invalid_outputs_p)
12401 /* Create the ASM_EXPR. */
12402 if (parser->in_function_body)
12404 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12406 /* If the extended syntax was not used, mark the ASM_EXPR. */
12409 tree temp = asm_stmt;
12410 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12411 temp = TREE_OPERAND (temp, 0);
12413 ASM_INPUT_P (temp) = 1;
12417 cgraph_add_asm_node (string);
12421 /* Declarators [gram.dcl.decl] */
12423 /* Parse an init-declarator.
12426 declarator initializer [opt]
12431 declarator asm-specification [opt] attributes [opt] initializer [opt]
12433 function-definition:
12434 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12436 decl-specifier-seq [opt] declarator function-try-block
12440 function-definition:
12441 __extension__ function-definition
12443 The DECL_SPECIFIERS apply to this declarator. Returns a
12444 representation of the entity declared. If MEMBER_P is TRUE, then
12445 this declarator appears in a class scope. The new DECL created by
12446 this declarator is returned.
12448 The CHECKS are access checks that should be performed once we know
12449 what entity is being declared (and, therefore, what classes have
12452 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12453 for a function-definition here as well. If the declarator is a
12454 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12455 be TRUE upon return. By that point, the function-definition will
12456 have been completely parsed.
12458 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12462 cp_parser_init_declarator (cp_parser* parser,
12463 cp_decl_specifier_seq *decl_specifiers,
12464 VEC (deferred_access_check,gc)* checks,
12465 bool function_definition_allowed_p,
12467 int declares_class_or_enum,
12468 bool* function_definition_p)
12470 cp_token *token = NULL, *asm_spec_start_token = NULL,
12471 *attributes_start_token = NULL;
12472 cp_declarator *declarator;
12473 tree prefix_attributes;
12475 tree asm_specification;
12477 tree decl = NULL_TREE;
12479 int is_initialized;
12480 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12481 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12483 enum cpp_ttype initialization_kind;
12484 bool is_direct_init = false;
12485 bool is_non_constant_init;
12486 int ctor_dtor_or_conv_p;
12488 tree pushed_scope = NULL;
12490 /* Gather the attributes that were provided with the
12491 decl-specifiers. */
12492 prefix_attributes = decl_specifiers->attributes;
12494 /* Assume that this is not the declarator for a function
12496 if (function_definition_p)
12497 *function_definition_p = false;
12499 /* Defer access checks while parsing the declarator; we cannot know
12500 what names are accessible until we know what is being
12502 resume_deferring_access_checks ();
12504 /* Parse the declarator. */
12505 token = cp_lexer_peek_token (parser->lexer);
12507 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12508 &ctor_dtor_or_conv_p,
12509 /*parenthesized_p=*/NULL,
12510 /*member_p=*/false);
12511 /* Gather up the deferred checks. */
12512 stop_deferring_access_checks ();
12514 /* If the DECLARATOR was erroneous, there's no need to go
12516 if (declarator == cp_error_declarator)
12517 return error_mark_node;
12519 /* Check that the number of template-parameter-lists is OK. */
12520 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12522 return error_mark_node;
12524 if (declares_class_or_enum & 2)
12525 cp_parser_check_for_definition_in_return_type (declarator,
12526 decl_specifiers->type,
12527 decl_specifiers->type_location);
12529 /* Figure out what scope the entity declared by the DECLARATOR is
12530 located in. `grokdeclarator' sometimes changes the scope, so
12531 we compute it now. */
12532 scope = get_scope_of_declarator (declarator);
12534 /* If we're allowing GNU extensions, look for an asm-specification
12536 if (cp_parser_allow_gnu_extensions_p (parser))
12538 /* Look for an asm-specification. */
12539 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12540 asm_specification = cp_parser_asm_specification_opt (parser);
12541 /* And attributes. */
12542 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12543 attributes = cp_parser_attributes_opt (parser);
12547 asm_specification = NULL_TREE;
12548 attributes = NULL_TREE;
12551 /* Peek at the next token. */
12552 token = cp_lexer_peek_token (parser->lexer);
12553 /* Check to see if the token indicates the start of a
12554 function-definition. */
12555 if (function_declarator_p (declarator)
12556 && cp_parser_token_starts_function_definition_p (token))
12558 if (!function_definition_allowed_p)
12560 /* If a function-definition should not appear here, issue an
12562 cp_parser_error (parser,
12563 "a function-definition is not allowed here");
12564 return error_mark_node;
12568 /* Neither attributes nor an asm-specification are allowed
12569 on a function-definition. */
12570 if (asm_specification)
12571 error ("%Han asm-specification is not allowed "
12572 "on a function-definition",
12573 &asm_spec_start_token->location);
12575 error ("%Hattributes are not allowed on a function-definition",
12576 &attributes_start_token->location);
12577 /* This is a function-definition. */
12578 *function_definition_p = true;
12580 /* Parse the function definition. */
12582 decl = cp_parser_save_member_function_body (parser,
12585 prefix_attributes);
12588 = (cp_parser_function_definition_from_specifiers_and_declarator
12589 (parser, decl_specifiers, prefix_attributes, declarator));
12597 Only in function declarations for constructors, destructors, and
12598 type conversions can the decl-specifier-seq be omitted.
12600 We explicitly postpone this check past the point where we handle
12601 function-definitions because we tolerate function-definitions
12602 that are missing their return types in some modes. */
12603 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12605 cp_parser_error (parser,
12606 "expected constructor, destructor, or type conversion");
12607 return error_mark_node;
12610 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12611 if (token->type == CPP_EQ
12612 || token->type == CPP_OPEN_PAREN
12613 || token->type == CPP_OPEN_BRACE)
12615 is_initialized = 1;
12616 initialization_kind = token->type;
12618 if (token->type == CPP_EQ
12619 && function_declarator_p (declarator))
12621 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12622 if (t2->keyword == RID_DEFAULT)
12623 is_initialized = 2;
12624 else if (t2->keyword == RID_DELETE)
12625 is_initialized = 3;
12630 /* If the init-declarator isn't initialized and isn't followed by a
12631 `,' or `;', it's not a valid init-declarator. */
12632 if (token->type != CPP_COMMA
12633 && token->type != CPP_SEMICOLON)
12635 cp_parser_error (parser, "expected initializer");
12636 return error_mark_node;
12638 is_initialized = 0;
12639 initialization_kind = CPP_EOF;
12642 /* Because start_decl has side-effects, we should only call it if we
12643 know we're going ahead. By this point, we know that we cannot
12644 possibly be looking at any other construct. */
12645 cp_parser_commit_to_tentative_parse (parser);
12647 /* If the decl specifiers were bad, issue an error now that we're
12648 sure this was intended to be a declarator. Then continue
12649 declaring the variable(s), as int, to try to cut down on further
12651 if (decl_specifiers->any_specifiers_p
12652 && decl_specifiers->type == error_mark_node)
12654 cp_parser_error (parser, "invalid type in declaration");
12655 decl_specifiers->type = integer_type_node;
12658 /* Check to see whether or not this declaration is a friend. */
12659 friend_p = cp_parser_friend_p (decl_specifiers);
12661 /* Enter the newly declared entry in the symbol table. If we're
12662 processing a declaration in a class-specifier, we wait until
12663 after processing the initializer. */
12666 if (parser->in_unbraced_linkage_specification_p)
12667 decl_specifiers->storage_class = sc_extern;
12668 decl = start_decl (declarator, decl_specifiers,
12669 is_initialized, attributes, prefix_attributes,
12673 /* Enter the SCOPE. That way unqualified names appearing in the
12674 initializer will be looked up in SCOPE. */
12675 pushed_scope = push_scope (scope);
12677 /* Perform deferred access control checks, now that we know in which
12678 SCOPE the declared entity resides. */
12679 if (!member_p && decl)
12681 tree saved_current_function_decl = NULL_TREE;
12683 /* If the entity being declared is a function, pretend that we
12684 are in its scope. If it is a `friend', it may have access to
12685 things that would not otherwise be accessible. */
12686 if (TREE_CODE (decl) == FUNCTION_DECL)
12688 saved_current_function_decl = current_function_decl;
12689 current_function_decl = decl;
12692 /* Perform access checks for template parameters. */
12693 cp_parser_perform_template_parameter_access_checks (checks);
12695 /* Perform the access control checks for the declarator and the
12696 decl-specifiers. */
12697 perform_deferred_access_checks ();
12699 /* Restore the saved value. */
12700 if (TREE_CODE (decl) == FUNCTION_DECL)
12701 current_function_decl = saved_current_function_decl;
12704 /* Parse the initializer. */
12705 initializer = NULL_TREE;
12706 is_direct_init = false;
12707 is_non_constant_init = true;
12708 if (is_initialized)
12710 if (function_declarator_p (declarator))
12712 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12713 if (initialization_kind == CPP_EQ)
12714 initializer = cp_parser_pure_specifier (parser);
12717 /* If the declaration was erroneous, we don't really
12718 know what the user intended, so just silently
12719 consume the initializer. */
12720 if (decl != error_mark_node)
12721 error ("%Hinitializer provided for function",
12722 &initializer_start_token->location);
12723 cp_parser_skip_to_closing_parenthesis (parser,
12724 /*recovering=*/true,
12725 /*or_comma=*/false,
12726 /*consume_paren=*/true);
12730 initializer = cp_parser_initializer (parser,
12732 &is_non_constant_init);
12735 /* The old parser allows attributes to appear after a parenthesized
12736 initializer. Mark Mitchell proposed removing this functionality
12737 on the GCC mailing lists on 2002-08-13. This parser accepts the
12738 attributes -- but ignores them. */
12739 if (cp_parser_allow_gnu_extensions_p (parser)
12740 && initialization_kind == CPP_OPEN_PAREN)
12741 if (cp_parser_attributes_opt (parser))
12742 warning (OPT_Wattributes,
12743 "attributes after parenthesized initializer ignored");
12745 /* For an in-class declaration, use `grokfield' to create the
12751 pop_scope (pushed_scope);
12752 pushed_scope = false;
12754 decl = grokfield (declarator, decl_specifiers,
12755 initializer, !is_non_constant_init,
12756 /*asmspec=*/NULL_TREE,
12757 prefix_attributes);
12758 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12759 cp_parser_save_default_args (parser, decl);
12762 /* Finish processing the declaration. But, skip friend
12764 if (!friend_p && decl && decl != error_mark_node)
12766 cp_finish_decl (decl,
12767 initializer, !is_non_constant_init,
12769 /* If the initializer is in parentheses, then this is
12770 a direct-initialization, which means that an
12771 `explicit' constructor is OK. Otherwise, an
12772 `explicit' constructor cannot be used. */
12773 ((is_direct_init || !is_initialized)
12774 ? 0 : LOOKUP_ONLYCONVERTING));
12776 else if ((cxx_dialect != cxx98) && friend_p
12777 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12778 /* Core issue #226 (C++0x only): A default template-argument
12779 shall not be specified in a friend class template
12781 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12782 /*is_partial=*/0, /*is_friend_decl=*/1);
12784 if (!friend_p && pushed_scope)
12785 pop_scope (pushed_scope);
12790 /* Parse a declarator.
12794 ptr-operator declarator
12796 abstract-declarator:
12797 ptr-operator abstract-declarator [opt]
12798 direct-abstract-declarator
12803 attributes [opt] direct-declarator
12804 attributes [opt] ptr-operator declarator
12806 abstract-declarator:
12807 attributes [opt] ptr-operator abstract-declarator [opt]
12808 attributes [opt] direct-abstract-declarator
12810 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12811 detect constructor, destructor or conversion operators. It is set
12812 to -1 if the declarator is a name, and +1 if it is a
12813 function. Otherwise it is set to zero. Usually you just want to
12814 test for >0, but internally the negative value is used.
12816 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12817 a decl-specifier-seq unless it declares a constructor, destructor,
12818 or conversion. It might seem that we could check this condition in
12819 semantic analysis, rather than parsing, but that makes it difficult
12820 to handle something like `f()'. We want to notice that there are
12821 no decl-specifiers, and therefore realize that this is an
12822 expression, not a declaration.)
12824 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12825 the declarator is a direct-declarator of the form "(...)".
12827 MEMBER_P is true iff this declarator is a member-declarator. */
12829 static cp_declarator *
12830 cp_parser_declarator (cp_parser* parser,
12831 cp_parser_declarator_kind dcl_kind,
12832 int* ctor_dtor_or_conv_p,
12833 bool* parenthesized_p,
12837 cp_declarator *declarator;
12838 enum tree_code code;
12839 cp_cv_quals cv_quals;
12841 tree attributes = NULL_TREE;
12843 /* Assume this is not a constructor, destructor, or type-conversion
12845 if (ctor_dtor_or_conv_p)
12846 *ctor_dtor_or_conv_p = 0;
12848 if (cp_parser_allow_gnu_extensions_p (parser))
12849 attributes = cp_parser_attributes_opt (parser);
12851 /* Peek at the next token. */
12852 token = cp_lexer_peek_token (parser->lexer);
12854 /* Check for the ptr-operator production. */
12855 cp_parser_parse_tentatively (parser);
12856 /* Parse the ptr-operator. */
12857 code = cp_parser_ptr_operator (parser,
12860 /* If that worked, then we have a ptr-operator. */
12861 if (cp_parser_parse_definitely (parser))
12863 /* If a ptr-operator was found, then this declarator was not
12865 if (parenthesized_p)
12866 *parenthesized_p = true;
12867 /* The dependent declarator is optional if we are parsing an
12868 abstract-declarator. */
12869 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12870 cp_parser_parse_tentatively (parser);
12872 /* Parse the dependent declarator. */
12873 declarator = cp_parser_declarator (parser, dcl_kind,
12874 /*ctor_dtor_or_conv_p=*/NULL,
12875 /*parenthesized_p=*/NULL,
12876 /*member_p=*/false);
12878 /* If we are parsing an abstract-declarator, we must handle the
12879 case where the dependent declarator is absent. */
12880 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12881 && !cp_parser_parse_definitely (parser))
12884 declarator = cp_parser_make_indirect_declarator
12885 (code, class_type, cv_quals, declarator);
12887 /* Everything else is a direct-declarator. */
12890 if (parenthesized_p)
12891 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12893 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12894 ctor_dtor_or_conv_p,
12898 if (attributes && declarator && declarator != cp_error_declarator)
12899 declarator->attributes = attributes;
12904 /* Parse a direct-declarator or direct-abstract-declarator.
12908 direct-declarator ( parameter-declaration-clause )
12909 cv-qualifier-seq [opt]
12910 exception-specification [opt]
12911 direct-declarator [ constant-expression [opt] ]
12914 direct-abstract-declarator:
12915 direct-abstract-declarator [opt]
12916 ( parameter-declaration-clause )
12917 cv-qualifier-seq [opt]
12918 exception-specification [opt]
12919 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12920 ( abstract-declarator )
12922 Returns a representation of the declarator. DCL_KIND is
12923 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12924 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12925 we are parsing a direct-declarator. It is
12926 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12927 of ambiguity we prefer an abstract declarator, as per
12928 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12929 cp_parser_declarator. */
12931 static cp_declarator *
12932 cp_parser_direct_declarator (cp_parser* parser,
12933 cp_parser_declarator_kind dcl_kind,
12934 int* ctor_dtor_or_conv_p,
12938 cp_declarator *declarator = NULL;
12939 tree scope = NULL_TREE;
12940 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12941 bool saved_in_declarator_p = parser->in_declarator_p;
12943 tree pushed_scope = NULL_TREE;
12947 /* Peek at the next token. */
12948 token = cp_lexer_peek_token (parser->lexer);
12949 if (token->type == CPP_OPEN_PAREN)
12951 /* This is either a parameter-declaration-clause, or a
12952 parenthesized declarator. When we know we are parsing a
12953 named declarator, it must be a parenthesized declarator
12954 if FIRST is true. For instance, `(int)' is a
12955 parameter-declaration-clause, with an omitted
12956 direct-abstract-declarator. But `((*))', is a
12957 parenthesized abstract declarator. Finally, when T is a
12958 template parameter `(T)' is a
12959 parameter-declaration-clause, and not a parenthesized
12962 We first try and parse a parameter-declaration-clause,
12963 and then try a nested declarator (if FIRST is true).
12965 It is not an error for it not to be a
12966 parameter-declaration-clause, even when FIRST is
12972 The first is the declaration of a function while the
12973 second is the definition of a variable, including its
12976 Having seen only the parenthesis, we cannot know which of
12977 these two alternatives should be selected. Even more
12978 complex are examples like:
12983 The former is a function-declaration; the latter is a
12984 variable initialization.
12986 Thus again, we try a parameter-declaration-clause, and if
12987 that fails, we back out and return. */
12989 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12991 cp_parameter_declarator *params;
12992 unsigned saved_num_template_parameter_lists;
12994 /* In a member-declarator, the only valid interpretation
12995 of a parenthesis is the start of a
12996 parameter-declaration-clause. (It is invalid to
12997 initialize a static data member with a parenthesized
12998 initializer; only the "=" form of initialization is
13001 cp_parser_parse_tentatively (parser);
13003 /* Consume the `('. */
13004 cp_lexer_consume_token (parser->lexer);
13007 /* If this is going to be an abstract declarator, we're
13008 in a declarator and we can't have default args. */
13009 parser->default_arg_ok_p = false;
13010 parser->in_declarator_p = true;
13013 /* Inside the function parameter list, surrounding
13014 template-parameter-lists do not apply. */
13015 saved_num_template_parameter_lists
13016 = parser->num_template_parameter_lists;
13017 parser->num_template_parameter_lists = 0;
13019 /* Parse the parameter-declaration-clause. */
13020 params = cp_parser_parameter_declaration_clause (parser);
13022 parser->num_template_parameter_lists
13023 = saved_num_template_parameter_lists;
13025 /* If all went well, parse the cv-qualifier-seq and the
13026 exception-specification. */
13027 if (member_p || cp_parser_parse_definitely (parser))
13029 cp_cv_quals cv_quals;
13030 tree exception_specification;
13032 if (ctor_dtor_or_conv_p)
13033 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13035 /* Consume the `)'. */
13036 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13038 /* Parse the cv-qualifier-seq. */
13039 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13040 /* And the exception-specification. */
13041 exception_specification
13042 = cp_parser_exception_specification_opt (parser);
13044 /* Create the function-declarator. */
13045 declarator = make_call_declarator (declarator,
13048 exception_specification);
13049 /* Any subsequent parameter lists are to do with
13050 return type, so are not those of the declared
13052 parser->default_arg_ok_p = false;
13054 /* Repeat the main loop. */
13059 /* If this is the first, we can try a parenthesized
13063 bool saved_in_type_id_in_expr_p;
13065 parser->default_arg_ok_p = saved_default_arg_ok_p;
13066 parser->in_declarator_p = saved_in_declarator_p;
13068 /* Consume the `('. */
13069 cp_lexer_consume_token (parser->lexer);
13070 /* Parse the nested declarator. */
13071 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13072 parser->in_type_id_in_expr_p = true;
13074 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13075 /*parenthesized_p=*/NULL,
13077 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13079 /* Expect a `)'. */
13080 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13081 declarator = cp_error_declarator;
13082 if (declarator == cp_error_declarator)
13085 goto handle_declarator;
13087 /* Otherwise, we must be done. */
13091 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13092 && token->type == CPP_OPEN_SQUARE)
13094 /* Parse an array-declarator. */
13097 if (ctor_dtor_or_conv_p)
13098 *ctor_dtor_or_conv_p = 0;
13101 parser->default_arg_ok_p = false;
13102 parser->in_declarator_p = true;
13103 /* Consume the `['. */
13104 cp_lexer_consume_token (parser->lexer);
13105 /* Peek at the next token. */
13106 token = cp_lexer_peek_token (parser->lexer);
13107 /* If the next token is `]', then there is no
13108 constant-expression. */
13109 if (token->type != CPP_CLOSE_SQUARE)
13111 bool non_constant_p;
13114 = cp_parser_constant_expression (parser,
13115 /*allow_non_constant=*/true,
13117 if (!non_constant_p)
13118 bounds = fold_non_dependent_expr (bounds);
13119 /* Normally, the array bound must be an integral constant
13120 expression. However, as an extension, we allow VLAs
13121 in function scopes. */
13122 else if (!parser->in_function_body)
13124 error ("%Harray bound is not an integer constant",
13126 bounds = error_mark_node;
13130 bounds = NULL_TREE;
13131 /* Look for the closing `]'. */
13132 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13134 declarator = cp_error_declarator;
13138 declarator = make_array_declarator (declarator, bounds);
13140 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13142 tree qualifying_scope;
13143 tree unqualified_name;
13144 special_function_kind sfk;
13146 bool pack_expansion_p = false;
13147 cp_token *declarator_id_start_token;
13149 /* Parse a declarator-id */
13150 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13153 cp_parser_parse_tentatively (parser);
13155 /* If we see an ellipsis, we should be looking at a
13157 if (token->type == CPP_ELLIPSIS)
13159 /* Consume the `...' */
13160 cp_lexer_consume_token (parser->lexer);
13162 pack_expansion_p = true;
13166 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13168 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13169 qualifying_scope = parser->scope;
13174 if (!unqualified_name && pack_expansion_p)
13176 /* Check whether an error occurred. */
13177 okay = !cp_parser_error_occurred (parser);
13179 /* We already consumed the ellipsis to mark a
13180 parameter pack, but we have no way to report it,
13181 so abort the tentative parse. We will be exiting
13182 immediately anyway. */
13183 cp_parser_abort_tentative_parse (parser);
13186 okay = cp_parser_parse_definitely (parser);
13189 unqualified_name = error_mark_node;
13190 else if (unqualified_name
13191 && (qualifying_scope
13192 || (TREE_CODE (unqualified_name)
13193 != IDENTIFIER_NODE)))
13195 cp_parser_error (parser, "expected unqualified-id");
13196 unqualified_name = error_mark_node;
13200 if (!unqualified_name)
13202 if (unqualified_name == error_mark_node)
13204 declarator = cp_error_declarator;
13205 pack_expansion_p = false;
13206 declarator->parameter_pack_p = false;
13210 if (qualifying_scope && at_namespace_scope_p ()
13211 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13213 /* In the declaration of a member of a template class
13214 outside of the class itself, the SCOPE will sometimes
13215 be a TYPENAME_TYPE. For example, given:
13217 template <typename T>
13218 int S<T>::R::i = 3;
13220 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13221 this context, we must resolve S<T>::R to an ordinary
13222 type, rather than a typename type.
13224 The reason we normally avoid resolving TYPENAME_TYPEs
13225 is that a specialization of `S' might render
13226 `S<T>::R' not a type. However, if `S' is
13227 specialized, then this `i' will not be used, so there
13228 is no harm in resolving the types here. */
13231 /* Resolve the TYPENAME_TYPE. */
13232 type = resolve_typename_type (qualifying_scope,
13233 /*only_current_p=*/false);
13234 /* If that failed, the declarator is invalid. */
13235 if (TREE_CODE (type) == TYPENAME_TYPE)
13236 error ("%H%<%T::%E%> is not a type",
13237 &declarator_id_start_token->location,
13238 TYPE_CONTEXT (qualifying_scope),
13239 TYPE_IDENTIFIER (qualifying_scope));
13240 qualifying_scope = type;
13245 if (unqualified_name)
13249 if (qualifying_scope
13250 && CLASS_TYPE_P (qualifying_scope))
13251 class_type = qualifying_scope;
13253 class_type = current_class_type;
13255 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13257 tree name_type = TREE_TYPE (unqualified_name);
13258 if (class_type && same_type_p (name_type, class_type))
13260 if (qualifying_scope
13261 && CLASSTYPE_USE_TEMPLATE (name_type))
13263 error ("%Hinvalid use of constructor as a template",
13264 &declarator_id_start_token->location);
13265 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13266 "name the constructor in a qualified name",
13268 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13269 class_type, name_type);
13270 declarator = cp_error_declarator;
13274 unqualified_name = constructor_name (class_type);
13278 /* We do not attempt to print the declarator
13279 here because we do not have enough
13280 information about its original syntactic
13282 cp_parser_error (parser, "invalid declarator");
13283 declarator = cp_error_declarator;
13290 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13291 sfk = sfk_destructor;
13292 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13293 sfk = sfk_conversion;
13294 else if (/* There's no way to declare a constructor
13295 for an anonymous type, even if the type
13296 got a name for linkage purposes. */
13297 !TYPE_WAS_ANONYMOUS (class_type)
13298 && constructor_name_p (unqualified_name,
13301 unqualified_name = constructor_name (class_type);
13302 sfk = sfk_constructor;
13305 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13306 *ctor_dtor_or_conv_p = -1;
13309 declarator = make_id_declarator (qualifying_scope,
13312 declarator->id_loc = token->location;
13313 declarator->parameter_pack_p = pack_expansion_p;
13315 if (pack_expansion_p)
13316 maybe_warn_variadic_templates ();
13318 handle_declarator:;
13319 scope = get_scope_of_declarator (declarator);
13321 /* Any names that appear after the declarator-id for a
13322 member are looked up in the containing scope. */
13323 pushed_scope = push_scope (scope);
13324 parser->in_declarator_p = true;
13325 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13326 || (declarator && declarator->kind == cdk_id))
13327 /* Default args are only allowed on function
13329 parser->default_arg_ok_p = saved_default_arg_ok_p;
13331 parser->default_arg_ok_p = false;
13340 /* For an abstract declarator, we might wind up with nothing at this
13341 point. That's an error; the declarator is not optional. */
13343 cp_parser_error (parser, "expected declarator");
13345 /* If we entered a scope, we must exit it now. */
13347 pop_scope (pushed_scope);
13349 parser->default_arg_ok_p = saved_default_arg_ok_p;
13350 parser->in_declarator_p = saved_in_declarator_p;
13355 /* Parse a ptr-operator.
13358 * cv-qualifier-seq [opt]
13360 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13365 & cv-qualifier-seq [opt]
13367 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13368 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13369 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13370 filled in with the TYPE containing the member. *CV_QUALS is
13371 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13372 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13373 Note that the tree codes returned by this function have nothing
13374 to do with the types of trees that will be eventually be created
13375 to represent the pointer or reference type being parsed. They are
13376 just constants with suggestive names. */
13377 static enum tree_code
13378 cp_parser_ptr_operator (cp_parser* parser,
13380 cp_cv_quals *cv_quals)
13382 enum tree_code code = ERROR_MARK;
13385 /* Assume that it's not a pointer-to-member. */
13387 /* And that there are no cv-qualifiers. */
13388 *cv_quals = TYPE_UNQUALIFIED;
13390 /* Peek at the next token. */
13391 token = cp_lexer_peek_token (parser->lexer);
13393 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13394 if (token->type == CPP_MULT)
13395 code = INDIRECT_REF;
13396 else if (token->type == CPP_AND)
13398 else if ((cxx_dialect != cxx98) &&
13399 token->type == CPP_AND_AND) /* C++0x only */
13400 code = NON_LVALUE_EXPR;
13402 if (code != ERROR_MARK)
13404 /* Consume the `*', `&' or `&&'. */
13405 cp_lexer_consume_token (parser->lexer);
13407 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13408 `&', if we are allowing GNU extensions. (The only qualifier
13409 that can legally appear after `&' is `restrict', but that is
13410 enforced during semantic analysis. */
13411 if (code == INDIRECT_REF
13412 || cp_parser_allow_gnu_extensions_p (parser))
13413 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13417 /* Try the pointer-to-member case. */
13418 cp_parser_parse_tentatively (parser);
13419 /* Look for the optional `::' operator. */
13420 cp_parser_global_scope_opt (parser,
13421 /*current_scope_valid_p=*/false);
13422 /* Look for the nested-name specifier. */
13423 token = cp_lexer_peek_token (parser->lexer);
13424 cp_parser_nested_name_specifier (parser,
13425 /*typename_keyword_p=*/false,
13426 /*check_dependency_p=*/true,
13428 /*is_declaration=*/false);
13429 /* If we found it, and the next token is a `*', then we are
13430 indeed looking at a pointer-to-member operator. */
13431 if (!cp_parser_error_occurred (parser)
13432 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13434 /* Indicate that the `*' operator was used. */
13435 code = INDIRECT_REF;
13437 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13438 error ("%H%qD is a namespace", &token->location, parser->scope);
13441 /* The type of which the member is a member is given by the
13443 *type = parser->scope;
13444 /* The next name will not be qualified. */
13445 parser->scope = NULL_TREE;
13446 parser->qualifying_scope = NULL_TREE;
13447 parser->object_scope = NULL_TREE;
13448 /* Look for the optional cv-qualifier-seq. */
13449 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13452 /* If that didn't work we don't have a ptr-operator. */
13453 if (!cp_parser_parse_definitely (parser))
13454 cp_parser_error (parser, "expected ptr-operator");
13460 /* Parse an (optional) cv-qualifier-seq.
13463 cv-qualifier cv-qualifier-seq [opt]
13474 Returns a bitmask representing the cv-qualifiers. */
13477 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13479 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13484 cp_cv_quals cv_qualifier;
13486 /* Peek at the next token. */
13487 token = cp_lexer_peek_token (parser->lexer);
13488 /* See if it's a cv-qualifier. */
13489 switch (token->keyword)
13492 cv_qualifier = TYPE_QUAL_CONST;
13496 cv_qualifier = TYPE_QUAL_VOLATILE;
13500 cv_qualifier = TYPE_QUAL_RESTRICT;
13504 cv_qualifier = TYPE_UNQUALIFIED;
13511 if (cv_quals & cv_qualifier)
13513 error ("%Hduplicate cv-qualifier", &token->location);
13514 cp_lexer_purge_token (parser->lexer);
13518 cp_lexer_consume_token (parser->lexer);
13519 cv_quals |= cv_qualifier;
13526 /* Parse a declarator-id.
13530 :: [opt] nested-name-specifier [opt] type-name
13532 In the `id-expression' case, the value returned is as for
13533 cp_parser_id_expression if the id-expression was an unqualified-id.
13534 If the id-expression was a qualified-id, then a SCOPE_REF is
13535 returned. The first operand is the scope (either a NAMESPACE_DECL
13536 or TREE_TYPE), but the second is still just a representation of an
13540 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13543 /* The expression must be an id-expression. Assume that qualified
13544 names are the names of types so that:
13547 int S<T>::R::i = 3;
13549 will work; we must treat `S<T>::R' as the name of a type.
13550 Similarly, assume that qualified names are templates, where
13554 int S<T>::R<T>::i = 3;
13557 id = cp_parser_id_expression (parser,
13558 /*template_keyword_p=*/false,
13559 /*check_dependency_p=*/false,
13560 /*template_p=*/NULL,
13561 /*declarator_p=*/true,
13563 if (id && BASELINK_P (id))
13564 id = BASELINK_FUNCTIONS (id);
13568 /* Parse a type-id.
13571 type-specifier-seq abstract-declarator [opt]
13573 Returns the TYPE specified. */
13576 cp_parser_type_id (cp_parser* parser)
13578 cp_decl_specifier_seq type_specifier_seq;
13579 cp_declarator *abstract_declarator;
13581 /* Parse the type-specifier-seq. */
13582 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13583 &type_specifier_seq);
13584 if (type_specifier_seq.type == error_mark_node)
13585 return error_mark_node;
13587 /* There might or might not be an abstract declarator. */
13588 cp_parser_parse_tentatively (parser);
13589 /* Look for the declarator. */
13590 abstract_declarator
13591 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13592 /*parenthesized_p=*/NULL,
13593 /*member_p=*/false);
13594 /* Check to see if there really was a declarator. */
13595 if (!cp_parser_parse_definitely (parser))
13596 abstract_declarator = NULL;
13598 return groktypename (&type_specifier_seq, abstract_declarator);
13601 /* Parse a type-specifier-seq.
13603 type-specifier-seq:
13604 type-specifier type-specifier-seq [opt]
13608 type-specifier-seq:
13609 attributes type-specifier-seq [opt]
13611 If IS_CONDITION is true, we are at the start of a "condition",
13612 e.g., we've just seen "if (".
13614 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13617 cp_parser_type_specifier_seq (cp_parser* parser,
13619 cp_decl_specifier_seq *type_specifier_seq)
13621 bool seen_type_specifier = false;
13622 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13623 cp_token *start_token = NULL;
13625 /* Clear the TYPE_SPECIFIER_SEQ. */
13626 clear_decl_specs (type_specifier_seq);
13628 /* Parse the type-specifiers and attributes. */
13631 tree type_specifier;
13632 bool is_cv_qualifier;
13634 /* Check for attributes first. */
13635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13637 type_specifier_seq->attributes =
13638 chainon (type_specifier_seq->attributes,
13639 cp_parser_attributes_opt (parser));
13643 /* record the token of the beginning of the type specifier seq,
13644 for error reporting purposes*/
13646 start_token = cp_lexer_peek_token (parser->lexer);
13648 /* Look for the type-specifier. */
13649 type_specifier = cp_parser_type_specifier (parser,
13651 type_specifier_seq,
13652 /*is_declaration=*/false,
13655 if (!type_specifier)
13657 /* If the first type-specifier could not be found, this is not a
13658 type-specifier-seq at all. */
13659 if (!seen_type_specifier)
13661 cp_parser_error (parser, "expected type-specifier");
13662 type_specifier_seq->type = error_mark_node;
13665 /* If subsequent type-specifiers could not be found, the
13666 type-specifier-seq is complete. */
13670 seen_type_specifier = true;
13671 /* The standard says that a condition can be:
13673 type-specifier-seq declarator = assignment-expression
13680 we should treat the "S" as a declarator, not as a
13681 type-specifier. The standard doesn't say that explicitly for
13682 type-specifier-seq, but it does say that for
13683 decl-specifier-seq in an ordinary declaration. Perhaps it
13684 would be clearer just to allow a decl-specifier-seq here, and
13685 then add a semantic restriction that if any decl-specifiers
13686 that are not type-specifiers appear, the program is invalid. */
13687 if (is_condition && !is_cv_qualifier)
13688 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13691 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13694 /* Parse a parameter-declaration-clause.
13696 parameter-declaration-clause:
13697 parameter-declaration-list [opt] ... [opt]
13698 parameter-declaration-list , ...
13700 Returns a representation for the parameter declarations. A return
13701 value of NULL indicates a parameter-declaration-clause consisting
13702 only of an ellipsis. */
13704 static cp_parameter_declarator *
13705 cp_parser_parameter_declaration_clause (cp_parser* parser)
13707 cp_parameter_declarator *parameters;
13712 /* Peek at the next token. */
13713 token = cp_lexer_peek_token (parser->lexer);
13714 /* Check for trivial parameter-declaration-clauses. */
13715 if (token->type == CPP_ELLIPSIS)
13717 /* Consume the `...' token. */
13718 cp_lexer_consume_token (parser->lexer);
13721 else if (token->type == CPP_CLOSE_PAREN)
13722 /* There are no parameters. */
13724 #ifndef NO_IMPLICIT_EXTERN_C
13725 if (in_system_header && current_class_type == NULL
13726 && current_lang_name == lang_name_c)
13730 return no_parameters;
13732 /* Check for `(void)', too, which is a special case. */
13733 else if (token->keyword == RID_VOID
13734 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13735 == CPP_CLOSE_PAREN))
13737 /* Consume the `void' token. */
13738 cp_lexer_consume_token (parser->lexer);
13739 /* There are no parameters. */
13740 return no_parameters;
13743 /* Parse the parameter-declaration-list. */
13744 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13745 /* If a parse error occurred while parsing the
13746 parameter-declaration-list, then the entire
13747 parameter-declaration-clause is erroneous. */
13751 /* Peek at the next token. */
13752 token = cp_lexer_peek_token (parser->lexer);
13753 /* If it's a `,', the clause should terminate with an ellipsis. */
13754 if (token->type == CPP_COMMA)
13756 /* Consume the `,'. */
13757 cp_lexer_consume_token (parser->lexer);
13758 /* Expect an ellipsis. */
13760 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13762 /* It might also be `...' if the optional trailing `,' was
13764 else if (token->type == CPP_ELLIPSIS)
13766 /* Consume the `...' token. */
13767 cp_lexer_consume_token (parser->lexer);
13768 /* And remember that we saw it. */
13772 ellipsis_p = false;
13774 /* Finish the parameter list. */
13775 if (parameters && ellipsis_p)
13776 parameters->ellipsis_p = true;
13781 /* Parse a parameter-declaration-list.
13783 parameter-declaration-list:
13784 parameter-declaration
13785 parameter-declaration-list , parameter-declaration
13787 Returns a representation of the parameter-declaration-list, as for
13788 cp_parser_parameter_declaration_clause. However, the
13789 `void_list_node' is never appended to the list. Upon return,
13790 *IS_ERROR will be true iff an error occurred. */
13792 static cp_parameter_declarator *
13793 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13795 cp_parameter_declarator *parameters = NULL;
13796 cp_parameter_declarator **tail = ¶meters;
13797 bool saved_in_unbraced_linkage_specification_p;
13799 /* Assume all will go well. */
13801 /* The special considerations that apply to a function within an
13802 unbraced linkage specifications do not apply to the parameters
13803 to the function. */
13804 saved_in_unbraced_linkage_specification_p
13805 = parser->in_unbraced_linkage_specification_p;
13806 parser->in_unbraced_linkage_specification_p = false;
13808 /* Look for more parameters. */
13811 cp_parameter_declarator *parameter;
13812 bool parenthesized_p;
13813 /* Parse the parameter. */
13815 = cp_parser_parameter_declaration (parser,
13816 /*template_parm_p=*/false,
13819 /* If a parse error occurred parsing the parameter declaration,
13820 then the entire parameter-declaration-list is erroneous. */
13827 /* Add the new parameter to the list. */
13829 tail = ¶meter->next;
13831 /* Peek at the next token. */
13832 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13833 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13834 /* These are for Objective-C++ */
13835 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13836 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13837 /* The parameter-declaration-list is complete. */
13839 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13843 /* Peek at the next token. */
13844 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13845 /* If it's an ellipsis, then the list is complete. */
13846 if (token->type == CPP_ELLIPSIS)
13848 /* Otherwise, there must be more parameters. Consume the
13850 cp_lexer_consume_token (parser->lexer);
13851 /* When parsing something like:
13853 int i(float f, double d)
13855 we can tell after seeing the declaration for "f" that we
13856 are not looking at an initialization of a variable "i",
13857 but rather at the declaration of a function "i".
13859 Due to the fact that the parsing of template arguments
13860 (as specified to a template-id) requires backtracking we
13861 cannot use this technique when inside a template argument
13863 if (!parser->in_template_argument_list_p
13864 && !parser->in_type_id_in_expr_p
13865 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13866 /* However, a parameter-declaration of the form
13867 "foat(f)" (which is a valid declaration of a
13868 parameter "f") can also be interpreted as an
13869 expression (the conversion of "f" to "float"). */
13870 && !parenthesized_p)
13871 cp_parser_commit_to_tentative_parse (parser);
13875 cp_parser_error (parser, "expected %<,%> or %<...%>");
13876 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13877 cp_parser_skip_to_closing_parenthesis (parser,
13878 /*recovering=*/true,
13879 /*or_comma=*/false,
13880 /*consume_paren=*/false);
13885 parser->in_unbraced_linkage_specification_p
13886 = saved_in_unbraced_linkage_specification_p;
13891 /* Parse a parameter declaration.
13893 parameter-declaration:
13894 decl-specifier-seq ... [opt] declarator
13895 decl-specifier-seq declarator = assignment-expression
13896 decl-specifier-seq ... [opt] abstract-declarator [opt]
13897 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13899 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13900 declares a template parameter. (In that case, a non-nested `>'
13901 token encountered during the parsing of the assignment-expression
13902 is not interpreted as a greater-than operator.)
13904 Returns a representation of the parameter, or NULL if an error
13905 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13906 true iff the declarator is of the form "(p)". */
13908 static cp_parameter_declarator *
13909 cp_parser_parameter_declaration (cp_parser *parser,
13910 bool template_parm_p,
13911 bool *parenthesized_p)
13913 int declares_class_or_enum;
13914 bool greater_than_is_operator_p;
13915 cp_decl_specifier_seq decl_specifiers;
13916 cp_declarator *declarator;
13917 tree default_argument;
13918 cp_token *token = NULL, *declarator_token_start = NULL;
13919 const char *saved_message;
13921 /* In a template parameter, `>' is not an operator.
13925 When parsing a default template-argument for a non-type
13926 template-parameter, the first non-nested `>' is taken as the end
13927 of the template parameter-list rather than a greater-than
13929 greater_than_is_operator_p = !template_parm_p;
13931 /* Type definitions may not appear in parameter types. */
13932 saved_message = parser->type_definition_forbidden_message;
13933 parser->type_definition_forbidden_message
13934 = "types may not be defined in parameter types";
13936 /* Parse the declaration-specifiers. */
13937 cp_parser_decl_specifier_seq (parser,
13938 CP_PARSER_FLAGS_NONE,
13940 &declares_class_or_enum);
13941 /* If an error occurred, there's no reason to attempt to parse the
13942 rest of the declaration. */
13943 if (cp_parser_error_occurred (parser))
13945 parser->type_definition_forbidden_message = saved_message;
13949 /* Peek at the next token. */
13950 token = cp_lexer_peek_token (parser->lexer);
13952 /* If the next token is a `)', `,', `=', `>', or `...', then there
13953 is no declarator. However, when variadic templates are enabled,
13954 there may be a declarator following `...'. */
13955 if (token->type == CPP_CLOSE_PAREN
13956 || token->type == CPP_COMMA
13957 || token->type == CPP_EQ
13958 || token->type == CPP_GREATER)
13961 if (parenthesized_p)
13962 *parenthesized_p = false;
13964 /* Otherwise, there should be a declarator. */
13967 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13968 parser->default_arg_ok_p = false;
13970 /* After seeing a decl-specifier-seq, if the next token is not a
13971 "(", there is no possibility that the code is a valid
13972 expression. Therefore, if parsing tentatively, we commit at
13974 if (!parser->in_template_argument_list_p
13975 /* In an expression context, having seen:
13979 we cannot be sure whether we are looking at a
13980 function-type (taking a "char" as a parameter) or a cast
13981 of some object of type "char" to "int". */
13982 && !parser->in_type_id_in_expr_p
13983 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13984 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13985 cp_parser_commit_to_tentative_parse (parser);
13986 /* Parse the declarator. */
13987 declarator_token_start = token;
13988 declarator = cp_parser_declarator (parser,
13989 CP_PARSER_DECLARATOR_EITHER,
13990 /*ctor_dtor_or_conv_p=*/NULL,
13992 /*member_p=*/false);
13993 parser->default_arg_ok_p = saved_default_arg_ok_p;
13994 /* After the declarator, allow more attributes. */
13995 decl_specifiers.attributes
13996 = chainon (decl_specifiers.attributes,
13997 cp_parser_attributes_opt (parser));
14000 /* If the next token is an ellipsis, and we have not seen a
14001 declarator name, and the type of the declarator contains parameter
14002 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14003 a parameter pack expansion expression. Otherwise, leave the
14004 ellipsis for a C-style variadic function. */
14005 token = cp_lexer_peek_token (parser->lexer);
14006 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14008 tree type = decl_specifiers.type;
14010 if (type && DECL_P (type))
14011 type = TREE_TYPE (type);
14014 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14015 && declarator_can_be_parameter_pack (declarator)
14016 && (!declarator || !declarator->parameter_pack_p)
14017 && uses_parameter_packs (type))
14019 /* Consume the `...'. */
14020 cp_lexer_consume_token (parser->lexer);
14021 maybe_warn_variadic_templates ();
14023 /* Build a pack expansion type */
14025 declarator->parameter_pack_p = true;
14027 decl_specifiers.type = make_pack_expansion (type);
14031 /* The restriction on defining new types applies only to the type
14032 of the parameter, not to the default argument. */
14033 parser->type_definition_forbidden_message = saved_message;
14035 /* If the next token is `=', then process a default argument. */
14036 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14038 /* Consume the `='. */
14039 cp_lexer_consume_token (parser->lexer);
14041 /* If we are defining a class, then the tokens that make up the
14042 default argument must be saved and processed later. */
14043 if (!template_parm_p && at_class_scope_p ()
14044 && TYPE_BEING_DEFINED (current_class_type))
14046 unsigned depth = 0;
14047 int maybe_template_id = 0;
14048 cp_token *first_token;
14051 /* Add tokens until we have processed the entire default
14052 argument. We add the range [first_token, token). */
14053 first_token = cp_lexer_peek_token (parser->lexer);
14058 /* Peek at the next token. */
14059 token = cp_lexer_peek_token (parser->lexer);
14060 /* What we do depends on what token we have. */
14061 switch (token->type)
14063 /* In valid code, a default argument must be
14064 immediately followed by a `,' `)', or `...'. */
14066 if (depth == 0 && maybe_template_id)
14068 /* If we've seen a '<', we might be in a
14069 template-argument-list. Until Core issue 325 is
14070 resolved, we don't know how this situation ought
14071 to be handled, so try to DTRT. We check whether
14072 what comes after the comma is a valid parameter
14073 declaration list. If it is, then the comma ends
14074 the default argument; otherwise the default
14075 argument continues. */
14076 bool error = false;
14078 /* Set ITALP so cp_parser_parameter_declaration_list
14079 doesn't decide to commit to this parse. */
14080 bool saved_italp = parser->in_template_argument_list_p;
14081 parser->in_template_argument_list_p = true;
14083 cp_parser_parse_tentatively (parser);
14084 cp_lexer_consume_token (parser->lexer);
14085 cp_parser_parameter_declaration_list (parser, &error);
14086 if (!cp_parser_error_occurred (parser) && !error)
14088 cp_parser_abort_tentative_parse (parser);
14090 parser->in_template_argument_list_p = saved_italp;
14093 case CPP_CLOSE_PAREN:
14095 /* If we run into a non-nested `;', `}', or `]',
14096 then the code is invalid -- but the default
14097 argument is certainly over. */
14098 case CPP_SEMICOLON:
14099 case CPP_CLOSE_BRACE:
14100 case CPP_CLOSE_SQUARE:
14103 /* Update DEPTH, if necessary. */
14104 else if (token->type == CPP_CLOSE_PAREN
14105 || token->type == CPP_CLOSE_BRACE
14106 || token->type == CPP_CLOSE_SQUARE)
14110 case CPP_OPEN_PAREN:
14111 case CPP_OPEN_SQUARE:
14112 case CPP_OPEN_BRACE:
14118 /* This might be the comparison operator, or it might
14119 start a template argument list. */
14120 ++maybe_template_id;
14124 if (cxx_dialect == cxx98)
14126 /* Fall through for C++0x, which treats the `>>'
14127 operator like two `>' tokens in certain
14133 /* This might be an operator, or it might close a
14134 template argument list. But if a previous '<'
14135 started a template argument list, this will have
14136 closed it, so we can't be in one anymore. */
14137 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14138 if (maybe_template_id < 0)
14139 maybe_template_id = 0;
14143 /* If we run out of tokens, issue an error message. */
14145 case CPP_PRAGMA_EOL:
14146 error ("%Hfile ends in default argument", &token->location);
14152 /* In these cases, we should look for template-ids.
14153 For example, if the default argument is
14154 `X<int, double>()', we need to do name lookup to
14155 figure out whether or not `X' is a template; if
14156 so, the `,' does not end the default argument.
14158 That is not yet done. */
14165 /* If we've reached the end, stop. */
14169 /* Add the token to the token block. */
14170 token = cp_lexer_consume_token (parser->lexer);
14173 /* Create a DEFAULT_ARG to represent the unparsed default
14175 default_argument = make_node (DEFAULT_ARG);
14176 DEFARG_TOKENS (default_argument)
14177 = cp_token_cache_new (first_token, token);
14178 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14180 /* Outside of a class definition, we can just parse the
14181 assignment-expression. */
14184 token = cp_lexer_peek_token (parser->lexer);
14186 = cp_parser_default_argument (parser, template_parm_p);
14189 if (!parser->default_arg_ok_p)
14191 if (flag_permissive)
14192 warning (0, "deprecated use of default argument for parameter of non-function");
14195 error ("%Hdefault arguments are only "
14196 "permitted for function parameters",
14198 default_argument = NULL_TREE;
14201 else if ((declarator && declarator->parameter_pack_p)
14202 || (decl_specifiers.type
14203 && PACK_EXPANSION_P (decl_specifiers.type)))
14205 const char* kind = template_parm_p? "template " : "";
14207 /* Find the name of the parameter pack. */
14208 cp_declarator *id_declarator = declarator;
14209 while (id_declarator && id_declarator->kind != cdk_id)
14210 id_declarator = id_declarator->declarator;
14212 if (id_declarator && id_declarator->kind == cdk_id)
14213 error ("%H%sparameter pack %qD cannot have a default argument",
14214 &declarator_token_start->location,
14215 kind, id_declarator->u.id.unqualified_name);
14217 error ("%H%sparameter pack cannot have a default argument",
14218 &declarator_token_start->location, kind);
14220 default_argument = NULL_TREE;
14224 default_argument = NULL_TREE;
14226 return make_parameter_declarator (&decl_specifiers,
14231 /* Parse a default argument and return it.
14233 TEMPLATE_PARM_P is true if this is a default argument for a
14234 non-type template parameter. */
14236 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14238 tree default_argument = NULL_TREE;
14239 bool saved_greater_than_is_operator_p;
14240 bool saved_local_variables_forbidden_p;
14242 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14244 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14245 parser->greater_than_is_operator_p = !template_parm_p;
14246 /* Local variable names (and the `this' keyword) may not
14247 appear in a default argument. */
14248 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14249 parser->local_variables_forbidden_p = true;
14250 /* The default argument expression may cause implicitly
14251 defined member functions to be synthesized, which will
14252 result in garbage collection. We must treat this
14253 situation as if we were within the body of function so as
14254 to avoid collecting live data on the stack. */
14256 /* Parse the assignment-expression. */
14257 if (template_parm_p)
14258 push_deferring_access_checks (dk_no_deferred);
14260 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14261 if (template_parm_p)
14262 pop_deferring_access_checks ();
14263 /* Restore saved state. */
14265 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14266 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14268 return default_argument;
14271 /* Parse a function-body.
14274 compound_statement */
14277 cp_parser_function_body (cp_parser *parser)
14279 cp_parser_compound_statement (parser, NULL, false);
14282 /* Parse a ctor-initializer-opt followed by a function-body. Return
14283 true if a ctor-initializer was present. */
14286 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14289 bool ctor_initializer_p;
14291 /* Begin the function body. */
14292 body = begin_function_body ();
14293 /* Parse the optional ctor-initializer. */
14294 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14295 /* Parse the function-body. */
14296 cp_parser_function_body (parser);
14297 /* Finish the function body. */
14298 finish_function_body (body);
14300 return ctor_initializer_p;
14303 /* Parse an initializer.
14306 = initializer-clause
14307 ( expression-list )
14309 Returns an expression representing the initializer. If no
14310 initializer is present, NULL_TREE is returned.
14312 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14313 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14314 set to TRUE if there is no initializer present. If there is an
14315 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14316 is set to true; otherwise it is set to false. */
14319 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14320 bool* non_constant_p)
14325 /* Peek at the next token. */
14326 token = cp_lexer_peek_token (parser->lexer);
14328 /* Let our caller know whether or not this initializer was
14330 *is_direct_init = (token->type != CPP_EQ);
14331 /* Assume that the initializer is constant. */
14332 *non_constant_p = false;
14334 if (token->type == CPP_EQ)
14336 /* Consume the `='. */
14337 cp_lexer_consume_token (parser->lexer);
14338 /* Parse the initializer-clause. */
14339 init = cp_parser_initializer_clause (parser, non_constant_p);
14341 else if (token->type == CPP_OPEN_PAREN)
14342 init = cp_parser_parenthesized_expression_list (parser, false,
14344 /*allow_expansion_p=*/true,
14346 else if (token->type == CPP_OPEN_BRACE)
14348 maybe_warn_cpp0x ("extended initializer lists");
14349 init = cp_parser_braced_list (parser, non_constant_p);
14350 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14354 /* Anything else is an error. */
14355 cp_parser_error (parser, "expected initializer");
14356 init = error_mark_node;
14362 /* Parse an initializer-clause.
14364 initializer-clause:
14365 assignment-expression
14368 Returns an expression representing the initializer.
14370 If the `assignment-expression' production is used the value
14371 returned is simply a representation for the expression.
14373 Otherwise, calls cp_parser_braced_list. */
14376 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14380 /* Assume the expression is constant. */
14381 *non_constant_p = false;
14383 /* If it is not a `{', then we are looking at an
14384 assignment-expression. */
14385 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14388 = cp_parser_constant_expression (parser,
14389 /*allow_non_constant_p=*/true,
14391 if (!*non_constant_p)
14392 initializer = fold_non_dependent_expr (initializer);
14395 initializer = cp_parser_braced_list (parser, non_constant_p);
14397 return initializer;
14400 /* Parse a brace-enclosed initializer list.
14403 { initializer-list , [opt] }
14406 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14407 the elements of the initializer-list (or NULL, if the last
14408 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14409 NULL_TREE. There is no way to detect whether or not the optional
14410 trailing `,' was provided. NON_CONSTANT_P is as for
14411 cp_parser_initializer. */
14414 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14418 /* Consume the `{' token. */
14419 cp_lexer_consume_token (parser->lexer);
14420 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14421 initializer = make_node (CONSTRUCTOR);
14422 /* If it's not a `}', then there is a non-trivial initializer. */
14423 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14425 /* Parse the initializer list. */
14426 CONSTRUCTOR_ELTS (initializer)
14427 = cp_parser_initializer_list (parser, non_constant_p);
14428 /* A trailing `,' token is allowed. */
14429 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14430 cp_lexer_consume_token (parser->lexer);
14432 /* Now, there should be a trailing `}'. */
14433 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14434 TREE_TYPE (initializer) = init_list_type_node;
14435 return initializer;
14438 /* Parse an initializer-list.
14441 initializer-clause ... [opt]
14442 initializer-list , initializer-clause ... [opt]
14447 identifier : initializer-clause
14448 initializer-list, identifier : initializer-clause
14450 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14451 for the initializer. If the INDEX of the elt is non-NULL, it is the
14452 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14453 as for cp_parser_initializer. */
14455 static VEC(constructor_elt,gc) *
14456 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14458 VEC(constructor_elt,gc) *v = NULL;
14460 /* Assume all of the expressions are constant. */
14461 *non_constant_p = false;
14463 /* Parse the rest of the list. */
14469 bool clause_non_constant_p;
14471 /* If the next token is an identifier and the following one is a
14472 colon, we are looking at the GNU designated-initializer
14474 if (cp_parser_allow_gnu_extensions_p (parser)
14475 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14476 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14478 /* Warn the user that they are using an extension. */
14479 pedwarn (input_location, OPT_pedantic,
14480 "ISO C++ does not allow designated initializers");
14481 /* Consume the identifier. */
14482 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14483 /* Consume the `:'. */
14484 cp_lexer_consume_token (parser->lexer);
14487 identifier = NULL_TREE;
14489 /* Parse the initializer. */
14490 initializer = cp_parser_initializer_clause (parser,
14491 &clause_non_constant_p);
14492 /* If any clause is non-constant, so is the entire initializer. */
14493 if (clause_non_constant_p)
14494 *non_constant_p = true;
14496 /* If we have an ellipsis, this is an initializer pack
14498 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14500 /* Consume the `...'. */
14501 cp_lexer_consume_token (parser->lexer);
14503 /* Turn the initializer into an initializer expansion. */
14504 initializer = make_pack_expansion (initializer);
14507 /* Add it to the vector. */
14508 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14510 /* If the next token is not a comma, we have reached the end of
14512 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14515 /* Peek at the next token. */
14516 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14517 /* If the next token is a `}', then we're still done. An
14518 initializer-clause can have a trailing `,' after the
14519 initializer-list and before the closing `}'. */
14520 if (token->type == CPP_CLOSE_BRACE)
14523 /* Consume the `,' token. */
14524 cp_lexer_consume_token (parser->lexer);
14530 /* Classes [gram.class] */
14532 /* Parse a class-name.
14538 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14539 to indicate that names looked up in dependent types should be
14540 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14541 keyword has been used to indicate that the name that appears next
14542 is a template. TAG_TYPE indicates the explicit tag given before
14543 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14544 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14545 is the class being defined in a class-head.
14547 Returns the TYPE_DECL representing the class. */
14550 cp_parser_class_name (cp_parser *parser,
14551 bool typename_keyword_p,
14552 bool template_keyword_p,
14553 enum tag_types tag_type,
14554 bool check_dependency_p,
14556 bool is_declaration)
14563 /* All class-names start with an identifier. */
14564 token = cp_lexer_peek_token (parser->lexer);
14565 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14567 cp_parser_error (parser, "expected class-name");
14568 return error_mark_node;
14571 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14572 to a template-id, so we save it here. */
14573 scope = parser->scope;
14574 if (scope == error_mark_node)
14575 return error_mark_node;
14577 /* Any name names a type if we're following the `typename' keyword
14578 in a qualified name where the enclosing scope is type-dependent. */
14579 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14580 && dependent_type_p (scope));
14581 /* Handle the common case (an identifier, but not a template-id)
14583 if (token->type == CPP_NAME
14584 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14586 cp_token *identifier_token;
14590 /* Look for the identifier. */
14591 identifier_token = cp_lexer_peek_token (parser->lexer);
14592 ambiguous_p = identifier_token->ambiguous_p;
14593 identifier = cp_parser_identifier (parser);
14594 /* If the next token isn't an identifier, we are certainly not
14595 looking at a class-name. */
14596 if (identifier == error_mark_node)
14597 decl = error_mark_node;
14598 /* If we know this is a type-name, there's no need to look it
14600 else if (typename_p)
14604 tree ambiguous_decls;
14605 /* If we already know that this lookup is ambiguous, then
14606 we've already issued an error message; there's no reason
14610 cp_parser_simulate_error (parser);
14611 return error_mark_node;
14613 /* If the next token is a `::', then the name must be a type
14616 [basic.lookup.qual]
14618 During the lookup for a name preceding the :: scope
14619 resolution operator, object, function, and enumerator
14620 names are ignored. */
14621 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14622 tag_type = typename_type;
14623 /* Look up the name. */
14624 decl = cp_parser_lookup_name (parser, identifier,
14626 /*is_template=*/false,
14627 /*is_namespace=*/false,
14628 check_dependency_p,
14630 identifier_token->location);
14631 if (ambiguous_decls)
14633 error ("%Hreference to %qD is ambiguous",
14634 &identifier_token->location, identifier);
14635 print_candidates (ambiguous_decls);
14636 if (cp_parser_parsing_tentatively (parser))
14638 identifier_token->ambiguous_p = true;
14639 cp_parser_simulate_error (parser);
14641 return error_mark_node;
14647 /* Try a template-id. */
14648 decl = cp_parser_template_id (parser, template_keyword_p,
14649 check_dependency_p,
14651 if (decl == error_mark_node)
14652 return error_mark_node;
14655 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14657 /* If this is a typename, create a TYPENAME_TYPE. */
14658 if (typename_p && decl != error_mark_node)
14660 decl = make_typename_type (scope, decl, typename_type,
14661 /*complain=*/tf_error);
14662 if (decl != error_mark_node)
14663 decl = TYPE_NAME (decl);
14666 /* Check to see that it is really the name of a class. */
14667 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14668 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14669 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14670 /* Situations like this:
14672 template <typename T> struct A {
14673 typename T::template X<int>::I i;
14676 are problematic. Is `T::template X<int>' a class-name? The
14677 standard does not seem to be definitive, but there is no other
14678 valid interpretation of the following `::'. Therefore, those
14679 names are considered class-names. */
14681 decl = make_typename_type (scope, decl, tag_type, tf_error);
14682 if (decl != error_mark_node)
14683 decl = TYPE_NAME (decl);
14685 else if (TREE_CODE (decl) != TYPE_DECL
14686 || TREE_TYPE (decl) == error_mark_node
14687 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14688 decl = error_mark_node;
14690 if (decl == error_mark_node)
14691 cp_parser_error (parser, "expected class-name");
14696 /* Parse a class-specifier.
14699 class-head { member-specification [opt] }
14701 Returns the TREE_TYPE representing the class. */
14704 cp_parser_class_specifier (cp_parser* parser)
14708 tree attributes = NULL_TREE;
14709 int has_trailing_semicolon;
14710 bool nested_name_specifier_p;
14711 unsigned saved_num_template_parameter_lists;
14712 bool saved_in_function_body;
14713 tree old_scope = NULL_TREE;
14714 tree scope = NULL_TREE;
14717 push_deferring_access_checks (dk_no_deferred);
14719 /* Parse the class-head. */
14720 type = cp_parser_class_head (parser,
14721 &nested_name_specifier_p,
14724 /* If the class-head was a semantic disaster, skip the entire body
14728 cp_parser_skip_to_end_of_block_or_statement (parser);
14729 pop_deferring_access_checks ();
14730 return error_mark_node;
14733 /* Look for the `{'. */
14734 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14736 pop_deferring_access_checks ();
14737 return error_mark_node;
14740 /* Process the base classes. If they're invalid, skip the
14741 entire class body. */
14742 if (!xref_basetypes (type, bases))
14744 /* Consuming the closing brace yields better error messages
14746 if (cp_parser_skip_to_closing_brace (parser))
14747 cp_lexer_consume_token (parser->lexer);
14748 pop_deferring_access_checks ();
14749 return error_mark_node;
14752 /* Issue an error message if type-definitions are forbidden here. */
14753 cp_parser_check_type_definition (parser);
14754 /* Remember that we are defining one more class. */
14755 ++parser->num_classes_being_defined;
14756 /* Inside the class, surrounding template-parameter-lists do not
14758 saved_num_template_parameter_lists
14759 = parser->num_template_parameter_lists;
14760 parser->num_template_parameter_lists = 0;
14761 /* We are not in a function body. */
14762 saved_in_function_body = parser->in_function_body;
14763 parser->in_function_body = false;
14765 /* Start the class. */
14766 if (nested_name_specifier_p)
14768 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14769 old_scope = push_inner_scope (scope);
14771 type = begin_class_definition (type, attributes);
14773 if (type == error_mark_node)
14774 /* If the type is erroneous, skip the entire body of the class. */
14775 cp_parser_skip_to_closing_brace (parser);
14777 /* Parse the member-specification. */
14778 cp_parser_member_specification_opt (parser);
14780 /* Look for the trailing `}'. */
14781 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14782 /* We get better error messages by noticing a common problem: a
14783 missing trailing `;'. */
14784 token = cp_lexer_peek_token (parser->lexer);
14785 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14786 /* Look for trailing attributes to apply to this class. */
14787 if (cp_parser_allow_gnu_extensions_p (parser))
14788 attributes = cp_parser_attributes_opt (parser);
14789 if (type != error_mark_node)
14790 type = finish_struct (type, attributes);
14791 if (nested_name_specifier_p)
14792 pop_inner_scope (old_scope, scope);
14793 /* If this class is not itself within the scope of another class,
14794 then we need to parse the bodies of all of the queued function
14795 definitions. Note that the queued functions defined in a class
14796 are not always processed immediately following the
14797 class-specifier for that class. Consider:
14800 struct B { void f() { sizeof (A); } };
14803 If `f' were processed before the processing of `A' were
14804 completed, there would be no way to compute the size of `A'.
14805 Note that the nesting we are interested in here is lexical --
14806 not the semantic nesting given by TYPE_CONTEXT. In particular,
14809 struct A { struct B; };
14810 struct A::B { void f() { } };
14812 there is no need to delay the parsing of `A::B::f'. */
14813 if (--parser->num_classes_being_defined == 0)
14817 tree class_type = NULL_TREE;
14818 tree pushed_scope = NULL_TREE;
14820 /* In a first pass, parse default arguments to the functions.
14821 Then, in a second pass, parse the bodies of the functions.
14822 This two-phased approach handles cases like:
14830 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14831 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14832 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14833 TREE_PURPOSE (parser->unparsed_functions_queues)
14834 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14836 fn = TREE_VALUE (queue_entry);
14837 /* If there are default arguments that have not yet been processed,
14838 take care of them now. */
14839 if (class_type != TREE_PURPOSE (queue_entry))
14842 pop_scope (pushed_scope);
14843 class_type = TREE_PURPOSE (queue_entry);
14844 pushed_scope = push_scope (class_type);
14846 /* Make sure that any template parameters are in scope. */
14847 maybe_begin_member_template_processing (fn);
14848 /* Parse the default argument expressions. */
14849 cp_parser_late_parsing_default_args (parser, fn);
14850 /* Remove any template parameters from the symbol table. */
14851 maybe_end_member_template_processing ();
14854 pop_scope (pushed_scope);
14855 /* Now parse the body of the functions. */
14856 for (TREE_VALUE (parser->unparsed_functions_queues)
14857 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14858 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14859 TREE_VALUE (parser->unparsed_functions_queues)
14860 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14862 /* Figure out which function we need to process. */
14863 fn = TREE_VALUE (queue_entry);
14864 /* Parse the function. */
14865 cp_parser_late_parsing_for_member (parser, fn);
14869 /* Put back any saved access checks. */
14870 pop_deferring_access_checks ();
14872 /* Restore saved state. */
14873 parser->in_function_body = saved_in_function_body;
14874 parser->num_template_parameter_lists
14875 = saved_num_template_parameter_lists;
14880 /* Parse a class-head.
14883 class-key identifier [opt] base-clause [opt]
14884 class-key nested-name-specifier identifier base-clause [opt]
14885 class-key nested-name-specifier [opt] template-id
14889 class-key attributes identifier [opt] base-clause [opt]
14890 class-key attributes nested-name-specifier identifier base-clause [opt]
14891 class-key attributes nested-name-specifier [opt] template-id
14894 Upon return BASES is initialized to the list of base classes (or
14895 NULL, if there are none) in the same form returned by
14896 cp_parser_base_clause.
14898 Returns the TYPE of the indicated class. Sets
14899 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14900 involving a nested-name-specifier was used, and FALSE otherwise.
14902 Returns error_mark_node if this is not a class-head.
14904 Returns NULL_TREE if the class-head is syntactically valid, but
14905 semantically invalid in a way that means we should skip the entire
14906 body of the class. */
14909 cp_parser_class_head (cp_parser* parser,
14910 bool* nested_name_specifier_p,
14911 tree *attributes_p,
14914 tree nested_name_specifier;
14915 enum tag_types class_key;
14916 tree id = NULL_TREE;
14917 tree type = NULL_TREE;
14919 bool template_id_p = false;
14920 bool qualified_p = false;
14921 bool invalid_nested_name_p = false;
14922 bool invalid_explicit_specialization_p = false;
14923 tree pushed_scope = NULL_TREE;
14924 unsigned num_templates;
14925 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14926 /* Assume no nested-name-specifier will be present. */
14927 *nested_name_specifier_p = false;
14928 /* Assume no template parameter lists will be used in defining the
14932 *bases = NULL_TREE;
14934 /* Look for the class-key. */
14935 class_key = cp_parser_class_key (parser);
14936 if (class_key == none_type)
14937 return error_mark_node;
14939 /* Parse the attributes. */
14940 attributes = cp_parser_attributes_opt (parser);
14942 /* If the next token is `::', that is invalid -- but sometimes
14943 people do try to write:
14947 Handle this gracefully by accepting the extra qualifier, and then
14948 issuing an error about it later if this really is a
14949 class-head. If it turns out just to be an elaborated type
14950 specifier, remain silent. */
14951 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14952 qualified_p = true;
14954 push_deferring_access_checks (dk_no_check);
14956 /* Determine the name of the class. Begin by looking for an
14957 optional nested-name-specifier. */
14958 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
14959 nested_name_specifier
14960 = cp_parser_nested_name_specifier_opt (parser,
14961 /*typename_keyword_p=*/false,
14962 /*check_dependency_p=*/false,
14964 /*is_declaration=*/false);
14965 /* If there was a nested-name-specifier, then there *must* be an
14967 if (nested_name_specifier)
14969 type_start_token = cp_lexer_peek_token (parser->lexer);
14970 /* Although the grammar says `identifier', it really means
14971 `class-name' or `template-name'. You are only allowed to
14972 define a class that has already been declared with this
14975 The proposed resolution for Core Issue 180 says that wherever
14976 you see `class T::X' you should treat `X' as a type-name.
14978 It is OK to define an inaccessible class; for example:
14980 class A { class B; };
14983 We do not know if we will see a class-name, or a
14984 template-name. We look for a class-name first, in case the
14985 class-name is a template-id; if we looked for the
14986 template-name first we would stop after the template-name. */
14987 cp_parser_parse_tentatively (parser);
14988 type = cp_parser_class_name (parser,
14989 /*typename_keyword_p=*/false,
14990 /*template_keyword_p=*/false,
14992 /*check_dependency_p=*/false,
14993 /*class_head_p=*/true,
14994 /*is_declaration=*/false);
14995 /* If that didn't work, ignore the nested-name-specifier. */
14996 if (!cp_parser_parse_definitely (parser))
14998 invalid_nested_name_p = true;
14999 type_start_token = cp_lexer_peek_token (parser->lexer);
15000 id = cp_parser_identifier (parser);
15001 if (id == error_mark_node)
15004 /* If we could not find a corresponding TYPE, treat this
15005 declaration like an unqualified declaration. */
15006 if (type == error_mark_node)
15007 nested_name_specifier = NULL_TREE;
15008 /* Otherwise, count the number of templates used in TYPE and its
15009 containing scopes. */
15014 for (scope = TREE_TYPE (type);
15015 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15016 scope = (TYPE_P (scope)
15017 ? TYPE_CONTEXT (scope)
15018 : DECL_CONTEXT (scope)))
15020 && CLASS_TYPE_P (scope)
15021 && CLASSTYPE_TEMPLATE_INFO (scope)
15022 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15023 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15027 /* Otherwise, the identifier is optional. */
15030 /* We don't know whether what comes next is a template-id,
15031 an identifier, or nothing at all. */
15032 cp_parser_parse_tentatively (parser);
15033 /* Check for a template-id. */
15034 type_start_token = cp_lexer_peek_token (parser->lexer);
15035 id = cp_parser_template_id (parser,
15036 /*template_keyword_p=*/false,
15037 /*check_dependency_p=*/true,
15038 /*is_declaration=*/true);
15039 /* If that didn't work, it could still be an identifier. */
15040 if (!cp_parser_parse_definitely (parser))
15042 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15044 type_start_token = cp_lexer_peek_token (parser->lexer);
15045 id = cp_parser_identifier (parser);
15052 template_id_p = true;
15057 pop_deferring_access_checks ();
15060 cp_parser_check_for_invalid_template_id (parser, id,
15061 type_start_token->location);
15063 /* If it's not a `:' or a `{' then we can't really be looking at a
15064 class-head, since a class-head only appears as part of a
15065 class-specifier. We have to detect this situation before calling
15066 xref_tag, since that has irreversible side-effects. */
15067 if (!cp_parser_next_token_starts_class_definition_p (parser))
15069 cp_parser_error (parser, "expected %<{%> or %<:%>");
15070 return error_mark_node;
15073 /* At this point, we're going ahead with the class-specifier, even
15074 if some other problem occurs. */
15075 cp_parser_commit_to_tentative_parse (parser);
15076 /* Issue the error about the overly-qualified name now. */
15079 cp_parser_error (parser,
15080 "global qualification of class name is invalid");
15081 return error_mark_node;
15083 else if (invalid_nested_name_p)
15085 cp_parser_error (parser,
15086 "qualified name does not name a class");
15087 return error_mark_node;
15089 else if (nested_name_specifier)
15093 /* Reject typedef-names in class heads. */
15094 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15096 error ("%Hinvalid class name in declaration of %qD",
15097 &type_start_token->location, type);
15102 /* Figure out in what scope the declaration is being placed. */
15103 scope = current_scope ();
15104 /* If that scope does not contain the scope in which the
15105 class was originally declared, the program is invalid. */
15106 if (scope && !is_ancestor (scope, nested_name_specifier))
15108 if (at_namespace_scope_p ())
15109 error ("%Hdeclaration of %qD in namespace %qD which does not "
15111 &type_start_token->location,
15112 type, scope, nested_name_specifier);
15114 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15115 &type_start_token->location,
15116 type, scope, nested_name_specifier);
15122 A declarator-id shall not be qualified except for the
15123 definition of a ... nested class outside of its class
15124 ... [or] the definition or explicit instantiation of a
15125 class member of a namespace outside of its namespace. */
15126 if (scope == nested_name_specifier)
15128 permerror (input_location, "%Hextra qualification not allowed",
15129 &nested_name_specifier_token_start->location);
15130 nested_name_specifier = NULL_TREE;
15134 /* An explicit-specialization must be preceded by "template <>". If
15135 it is not, try to recover gracefully. */
15136 if (at_namespace_scope_p ()
15137 && parser->num_template_parameter_lists == 0
15140 error ("%Han explicit specialization must be preceded by %<template <>%>",
15141 &type_start_token->location);
15142 invalid_explicit_specialization_p = true;
15143 /* Take the same action that would have been taken by
15144 cp_parser_explicit_specialization. */
15145 ++parser->num_template_parameter_lists;
15146 begin_specialization ();
15148 /* There must be no "return" statements between this point and the
15149 end of this function; set "type "to the correct return value and
15150 use "goto done;" to return. */
15151 /* Make sure that the right number of template parameters were
15153 if (!cp_parser_check_template_parameters (parser, num_templates,
15154 type_start_token->location))
15156 /* If something went wrong, there is no point in even trying to
15157 process the class-definition. */
15162 /* Look up the type. */
15165 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15166 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15167 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15169 error ("%Hfunction template %qD redeclared as a class template",
15170 &type_start_token->location, id);
15171 type = error_mark_node;
15175 type = TREE_TYPE (id);
15176 type = maybe_process_partial_specialization (type);
15178 if (nested_name_specifier)
15179 pushed_scope = push_scope (nested_name_specifier);
15181 else if (nested_name_specifier)
15187 template <typename T> struct S { struct T };
15188 template <typename T> struct S<T>::T { };
15190 we will get a TYPENAME_TYPE when processing the definition of
15191 `S::T'. We need to resolve it to the actual type before we
15192 try to define it. */
15193 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15195 class_type = resolve_typename_type (TREE_TYPE (type),
15196 /*only_current_p=*/false);
15197 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15198 type = TYPE_NAME (class_type);
15201 cp_parser_error (parser, "could not resolve typename type");
15202 type = error_mark_node;
15206 if (maybe_process_partial_specialization (TREE_TYPE (type))
15207 == error_mark_node)
15213 class_type = current_class_type;
15214 /* Enter the scope indicated by the nested-name-specifier. */
15215 pushed_scope = push_scope (nested_name_specifier);
15216 /* Get the canonical version of this type. */
15217 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15218 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15219 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15221 type = push_template_decl (type);
15222 if (type == error_mark_node)
15229 type = TREE_TYPE (type);
15230 *nested_name_specifier_p = true;
15232 else /* The name is not a nested name. */
15234 /* If the class was unnamed, create a dummy name. */
15236 id = make_anon_name ();
15237 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15238 parser->num_template_parameter_lists);
15241 /* Indicate whether this class was declared as a `class' or as a
15243 if (TREE_CODE (type) == RECORD_TYPE)
15244 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15245 cp_parser_check_class_key (class_key, type);
15247 /* If this type was already complete, and we see another definition,
15248 that's an error. */
15249 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15251 error ("%Hredefinition of %q#T",
15252 &type_start_token->location, type);
15253 error ("%Hprevious definition of %q+#T",
15254 &type_start_token->location, type);
15258 else if (type == error_mark_node)
15261 /* We will have entered the scope containing the class; the names of
15262 base classes should be looked up in that context. For example:
15264 struct A { struct B {}; struct C; };
15265 struct A::C : B {};
15269 /* Get the list of base-classes, if there is one. */
15270 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15271 *bases = cp_parser_base_clause (parser);
15274 /* Leave the scope given by the nested-name-specifier. We will
15275 enter the class scope itself while processing the members. */
15277 pop_scope (pushed_scope);
15279 if (invalid_explicit_specialization_p)
15281 end_specialization ();
15282 --parser->num_template_parameter_lists;
15284 *attributes_p = attributes;
15288 /* Parse a class-key.
15295 Returns the kind of class-key specified, or none_type to indicate
15298 static enum tag_types
15299 cp_parser_class_key (cp_parser* parser)
15302 enum tag_types tag_type;
15304 /* Look for the class-key. */
15305 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15309 /* Check to see if the TOKEN is a class-key. */
15310 tag_type = cp_parser_token_is_class_key (token);
15312 cp_parser_error (parser, "expected class-key");
15316 /* Parse an (optional) member-specification.
15318 member-specification:
15319 member-declaration member-specification [opt]
15320 access-specifier : member-specification [opt] */
15323 cp_parser_member_specification_opt (cp_parser* parser)
15330 /* Peek at the next token. */
15331 token = cp_lexer_peek_token (parser->lexer);
15332 /* If it's a `}', or EOF then we've seen all the members. */
15333 if (token->type == CPP_CLOSE_BRACE
15334 || token->type == CPP_EOF
15335 || token->type == CPP_PRAGMA_EOL)
15338 /* See if this token is a keyword. */
15339 keyword = token->keyword;
15343 case RID_PROTECTED:
15345 /* Consume the access-specifier. */
15346 cp_lexer_consume_token (parser->lexer);
15347 /* Remember which access-specifier is active. */
15348 current_access_specifier = token->u.value;
15349 /* Look for the `:'. */
15350 cp_parser_require (parser, CPP_COLON, "%<:%>");
15354 /* Accept #pragmas at class scope. */
15355 if (token->type == CPP_PRAGMA)
15357 cp_parser_pragma (parser, pragma_external);
15361 /* Otherwise, the next construction must be a
15362 member-declaration. */
15363 cp_parser_member_declaration (parser);
15368 /* Parse a member-declaration.
15370 member-declaration:
15371 decl-specifier-seq [opt] member-declarator-list [opt] ;
15372 function-definition ; [opt]
15373 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15375 template-declaration
15377 member-declarator-list:
15379 member-declarator-list , member-declarator
15382 declarator pure-specifier [opt]
15383 declarator constant-initializer [opt]
15384 identifier [opt] : constant-expression
15388 member-declaration:
15389 __extension__ member-declaration
15392 declarator attributes [opt] pure-specifier [opt]
15393 declarator attributes [opt] constant-initializer [opt]
15394 identifier [opt] attributes [opt] : constant-expression
15398 member-declaration:
15399 static_assert-declaration */
15402 cp_parser_member_declaration (cp_parser* parser)
15404 cp_decl_specifier_seq decl_specifiers;
15405 tree prefix_attributes;
15407 int declares_class_or_enum;
15409 cp_token *token = NULL;
15410 cp_token *decl_spec_token_start = NULL;
15411 cp_token *initializer_token_start = NULL;
15412 int saved_pedantic;
15414 /* Check for the `__extension__' keyword. */
15415 if (cp_parser_extension_opt (parser, &saved_pedantic))
15418 cp_parser_member_declaration (parser);
15419 /* Restore the old value of the PEDANTIC flag. */
15420 pedantic = saved_pedantic;
15425 /* Check for a template-declaration. */
15426 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15428 /* An explicit specialization here is an error condition, and we
15429 expect the specialization handler to detect and report this. */
15430 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15431 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15432 cp_parser_explicit_specialization (parser);
15434 cp_parser_template_declaration (parser, /*member_p=*/true);
15439 /* Check for a using-declaration. */
15440 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15442 /* Parse the using-declaration. */
15443 cp_parser_using_declaration (parser,
15444 /*access_declaration_p=*/false);
15448 /* Check for @defs. */
15449 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15452 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15453 ivar = ivar_chains;
15457 ivar = TREE_CHAIN (member);
15458 TREE_CHAIN (member) = NULL_TREE;
15459 finish_member_declaration (member);
15464 /* If the next token is `static_assert' we have a static assertion. */
15465 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15467 cp_parser_static_assert (parser, /*member_p=*/true);
15471 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15474 /* Parse the decl-specifier-seq. */
15475 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15476 cp_parser_decl_specifier_seq (parser,
15477 CP_PARSER_FLAGS_OPTIONAL,
15479 &declares_class_or_enum);
15480 prefix_attributes = decl_specifiers.attributes;
15481 decl_specifiers.attributes = NULL_TREE;
15482 /* Check for an invalid type-name. */
15483 if (!decl_specifiers.type
15484 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15486 /* If there is no declarator, then the decl-specifier-seq should
15488 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15490 /* If there was no decl-specifier-seq, and the next token is a
15491 `;', then we have something like:
15497 Each member-declaration shall declare at least one member
15498 name of the class. */
15499 if (!decl_specifiers.any_specifiers_p)
15501 cp_token *token = cp_lexer_peek_token (parser->lexer);
15502 if (!in_system_header_at (token->location))
15503 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15509 /* See if this declaration is a friend. */
15510 friend_p = cp_parser_friend_p (&decl_specifiers);
15511 /* If there were decl-specifiers, check to see if there was
15512 a class-declaration. */
15513 type = check_tag_decl (&decl_specifiers);
15514 /* Nested classes have already been added to the class, but
15515 a `friend' needs to be explicitly registered. */
15518 /* If the `friend' keyword was present, the friend must
15519 be introduced with a class-key. */
15520 if (!declares_class_or_enum)
15521 error ("%Ha class-key must be used when declaring a friend",
15522 &decl_spec_token_start->location);
15525 template <typename T> struct A {
15526 friend struct A<T>::B;
15529 A<T>::B will be represented by a TYPENAME_TYPE, and
15530 therefore not recognized by check_tag_decl. */
15532 && decl_specifiers.type
15533 && TYPE_P (decl_specifiers.type))
15534 type = decl_specifiers.type;
15535 if (!type || !TYPE_P (type))
15536 error ("%Hfriend declaration does not name a class or "
15537 "function", &decl_spec_token_start->location);
15539 make_friend_class (current_class_type, type,
15540 /*complain=*/true);
15542 /* If there is no TYPE, an error message will already have
15544 else if (!type || type == error_mark_node)
15546 /* An anonymous aggregate has to be handled specially; such
15547 a declaration really declares a data member (with a
15548 particular type), as opposed to a nested class. */
15549 else if (ANON_AGGR_TYPE_P (type))
15551 /* Remove constructors and such from TYPE, now that we
15552 know it is an anonymous aggregate. */
15553 fixup_anonymous_aggr (type);
15554 /* And make the corresponding data member. */
15555 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15556 /* Add it to the class. */
15557 finish_member_declaration (decl);
15560 cp_parser_check_access_in_redeclaration
15562 decl_spec_token_start->location);
15567 /* See if these declarations will be friends. */
15568 friend_p = cp_parser_friend_p (&decl_specifiers);
15570 /* Keep going until we hit the `;' at the end of the
15572 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15574 tree attributes = NULL_TREE;
15575 tree first_attribute;
15577 /* Peek at the next token. */
15578 token = cp_lexer_peek_token (parser->lexer);
15580 /* Check for a bitfield declaration. */
15581 if (token->type == CPP_COLON
15582 || (token->type == CPP_NAME
15583 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15589 /* Get the name of the bitfield. Note that we cannot just
15590 check TOKEN here because it may have been invalidated by
15591 the call to cp_lexer_peek_nth_token above. */
15592 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15593 identifier = cp_parser_identifier (parser);
15595 identifier = NULL_TREE;
15597 /* Consume the `:' token. */
15598 cp_lexer_consume_token (parser->lexer);
15599 /* Get the width of the bitfield. */
15601 = cp_parser_constant_expression (parser,
15602 /*allow_non_constant=*/false,
15605 /* Look for attributes that apply to the bitfield. */
15606 attributes = cp_parser_attributes_opt (parser);
15607 /* Remember which attributes are prefix attributes and
15609 first_attribute = attributes;
15610 /* Combine the attributes. */
15611 attributes = chainon (prefix_attributes, attributes);
15613 /* Create the bitfield declaration. */
15614 decl = grokbitfield (identifier
15615 ? make_id_declarator (NULL_TREE,
15625 cp_declarator *declarator;
15627 tree asm_specification;
15628 int ctor_dtor_or_conv_p;
15630 /* Parse the declarator. */
15632 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15633 &ctor_dtor_or_conv_p,
15634 /*parenthesized_p=*/NULL,
15635 /*member_p=*/true);
15637 /* If something went wrong parsing the declarator, make sure
15638 that we at least consume some tokens. */
15639 if (declarator == cp_error_declarator)
15641 /* Skip to the end of the statement. */
15642 cp_parser_skip_to_end_of_statement (parser);
15643 /* If the next token is not a semicolon, that is
15644 probably because we just skipped over the body of
15645 a function. So, we consume a semicolon if
15646 present, but do not issue an error message if it
15648 if (cp_lexer_next_token_is (parser->lexer,
15650 cp_lexer_consume_token (parser->lexer);
15654 if (declares_class_or_enum & 2)
15655 cp_parser_check_for_definition_in_return_type
15656 (declarator, decl_specifiers.type,
15657 decl_specifiers.type_location);
15659 /* Look for an asm-specification. */
15660 asm_specification = cp_parser_asm_specification_opt (parser);
15661 /* Look for attributes that apply to the declaration. */
15662 attributes = cp_parser_attributes_opt (parser);
15663 /* Remember which attributes are prefix attributes and
15665 first_attribute = attributes;
15666 /* Combine the attributes. */
15667 attributes = chainon (prefix_attributes, attributes);
15669 /* If it's an `=', then we have a constant-initializer or a
15670 pure-specifier. It is not correct to parse the
15671 initializer before registering the member declaration
15672 since the member declaration should be in scope while
15673 its initializer is processed. However, the rest of the
15674 front end does not yet provide an interface that allows
15675 us to handle this correctly. */
15676 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15680 A pure-specifier shall be used only in the declaration of
15681 a virtual function.
15683 A member-declarator can contain a constant-initializer
15684 only if it declares a static member of integral or
15687 Therefore, if the DECLARATOR is for a function, we look
15688 for a pure-specifier; otherwise, we look for a
15689 constant-initializer. When we call `grokfield', it will
15690 perform more stringent semantics checks. */
15691 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15692 if (function_declarator_p (declarator))
15693 initializer = cp_parser_pure_specifier (parser);
15695 /* Parse the initializer. */
15696 initializer = cp_parser_constant_initializer (parser);
15698 /* Otherwise, there is no initializer. */
15700 initializer = NULL_TREE;
15702 /* See if we are probably looking at a function
15703 definition. We are certainly not looking at a
15704 member-declarator. Calling `grokfield' has
15705 side-effects, so we must not do it unless we are sure
15706 that we are looking at a member-declarator. */
15707 if (cp_parser_token_starts_function_definition_p
15708 (cp_lexer_peek_token (parser->lexer)))
15710 /* The grammar does not allow a pure-specifier to be
15711 used when a member function is defined. (It is
15712 possible that this fact is an oversight in the
15713 standard, since a pure function may be defined
15714 outside of the class-specifier. */
15716 error ("%Hpure-specifier on function-definition",
15717 &initializer_token_start->location);
15718 decl = cp_parser_save_member_function_body (parser,
15722 /* If the member was not a friend, declare it here. */
15724 finish_member_declaration (decl);
15725 /* Peek at the next token. */
15726 token = cp_lexer_peek_token (parser->lexer);
15727 /* If the next token is a semicolon, consume it. */
15728 if (token->type == CPP_SEMICOLON)
15729 cp_lexer_consume_token (parser->lexer);
15733 /* Create the declaration. */
15734 decl = grokfield (declarator, &decl_specifiers,
15735 initializer, /*init_const_expr_p=*/true,
15740 /* Reset PREFIX_ATTRIBUTES. */
15741 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15742 attributes = TREE_CHAIN (attributes);
15744 TREE_CHAIN (attributes) = NULL_TREE;
15746 /* If there is any qualification still in effect, clear it
15747 now; we will be starting fresh with the next declarator. */
15748 parser->scope = NULL_TREE;
15749 parser->qualifying_scope = NULL_TREE;
15750 parser->object_scope = NULL_TREE;
15751 /* If it's a `,', then there are more declarators. */
15752 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15753 cp_lexer_consume_token (parser->lexer);
15754 /* If the next token isn't a `;', then we have a parse error. */
15755 else if (cp_lexer_next_token_is_not (parser->lexer,
15758 cp_parser_error (parser, "expected %<;%>");
15759 /* Skip tokens until we find a `;'. */
15760 cp_parser_skip_to_end_of_statement (parser);
15767 /* Add DECL to the list of members. */
15769 finish_member_declaration (decl);
15771 if (TREE_CODE (decl) == FUNCTION_DECL)
15772 cp_parser_save_default_args (parser, decl);
15777 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15780 /* Parse a pure-specifier.
15785 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15786 Otherwise, ERROR_MARK_NODE is returned. */
15789 cp_parser_pure_specifier (cp_parser* parser)
15793 /* Look for the `=' token. */
15794 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15795 return error_mark_node;
15796 /* Look for the `0' token. */
15797 token = cp_lexer_consume_token (parser->lexer);
15799 /* Accept = default or = delete in c++0x mode. */
15800 if (token->keyword == RID_DEFAULT
15801 || token->keyword == RID_DELETE)
15803 maybe_warn_cpp0x ("defaulted and deleted functions");
15804 return token->u.value;
15807 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15808 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15810 cp_parser_error (parser,
15811 "invalid pure specifier (only %<= 0%> is allowed)");
15812 cp_parser_skip_to_end_of_statement (parser);
15813 return error_mark_node;
15815 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15817 error ("%Htemplates may not be %<virtual%>", &token->location);
15818 return error_mark_node;
15821 return integer_zero_node;
15824 /* Parse a constant-initializer.
15826 constant-initializer:
15827 = constant-expression
15829 Returns a representation of the constant-expression. */
15832 cp_parser_constant_initializer (cp_parser* parser)
15834 /* Look for the `=' token. */
15835 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15836 return error_mark_node;
15838 /* It is invalid to write:
15840 struct S { static const int i = { 7 }; };
15843 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15845 cp_parser_error (parser,
15846 "a brace-enclosed initializer is not allowed here");
15847 /* Consume the opening brace. */
15848 cp_lexer_consume_token (parser->lexer);
15849 /* Skip the initializer. */
15850 cp_parser_skip_to_closing_brace (parser);
15851 /* Look for the trailing `}'. */
15852 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15854 return error_mark_node;
15857 return cp_parser_constant_expression (parser,
15858 /*allow_non_constant=*/false,
15862 /* Derived classes [gram.class.derived] */
15864 /* Parse a base-clause.
15867 : base-specifier-list
15869 base-specifier-list:
15870 base-specifier ... [opt]
15871 base-specifier-list , base-specifier ... [opt]
15873 Returns a TREE_LIST representing the base-classes, in the order in
15874 which they were declared. The representation of each node is as
15875 described by cp_parser_base_specifier.
15877 In the case that no bases are specified, this function will return
15878 NULL_TREE, not ERROR_MARK_NODE. */
15881 cp_parser_base_clause (cp_parser* parser)
15883 tree bases = NULL_TREE;
15885 /* Look for the `:' that begins the list. */
15886 cp_parser_require (parser, CPP_COLON, "%<:%>");
15888 /* Scan the base-specifier-list. */
15893 bool pack_expansion_p = false;
15895 /* Look for the base-specifier. */
15896 base = cp_parser_base_specifier (parser);
15897 /* Look for the (optional) ellipsis. */
15898 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15900 /* Consume the `...'. */
15901 cp_lexer_consume_token (parser->lexer);
15903 pack_expansion_p = true;
15906 /* Add BASE to the front of the list. */
15907 if (base != error_mark_node)
15909 if (pack_expansion_p)
15910 /* Make this a pack expansion type. */
15911 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15914 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15916 TREE_CHAIN (base) = bases;
15920 /* Peek at the next token. */
15921 token = cp_lexer_peek_token (parser->lexer);
15922 /* If it's not a comma, then the list is complete. */
15923 if (token->type != CPP_COMMA)
15925 /* Consume the `,'. */
15926 cp_lexer_consume_token (parser->lexer);
15929 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15930 base class had a qualified name. However, the next name that
15931 appears is certainly not qualified. */
15932 parser->scope = NULL_TREE;
15933 parser->qualifying_scope = NULL_TREE;
15934 parser->object_scope = NULL_TREE;
15936 return nreverse (bases);
15939 /* Parse a base-specifier.
15942 :: [opt] nested-name-specifier [opt] class-name
15943 virtual access-specifier [opt] :: [opt] nested-name-specifier
15945 access-specifier virtual [opt] :: [opt] nested-name-specifier
15948 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15949 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15950 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15951 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15954 cp_parser_base_specifier (cp_parser* parser)
15958 bool virtual_p = false;
15959 bool duplicate_virtual_error_issued_p = false;
15960 bool duplicate_access_error_issued_p = false;
15961 bool class_scope_p, template_p;
15962 tree access = access_default_node;
15965 /* Process the optional `virtual' and `access-specifier'. */
15968 /* Peek at the next token. */
15969 token = cp_lexer_peek_token (parser->lexer);
15970 /* Process `virtual'. */
15971 switch (token->keyword)
15974 /* If `virtual' appears more than once, issue an error. */
15975 if (virtual_p && !duplicate_virtual_error_issued_p)
15977 cp_parser_error (parser,
15978 "%<virtual%> specified more than once in base-specified");
15979 duplicate_virtual_error_issued_p = true;
15984 /* Consume the `virtual' token. */
15985 cp_lexer_consume_token (parser->lexer);
15990 case RID_PROTECTED:
15992 /* If more than one access specifier appears, issue an
15994 if (access != access_default_node
15995 && !duplicate_access_error_issued_p)
15997 cp_parser_error (parser,
15998 "more than one access specifier in base-specified");
15999 duplicate_access_error_issued_p = true;
16002 access = ridpointers[(int) token->keyword];
16004 /* Consume the access-specifier. */
16005 cp_lexer_consume_token (parser->lexer);
16014 /* It is not uncommon to see programs mechanically, erroneously, use
16015 the 'typename' keyword to denote (dependent) qualified types
16016 as base classes. */
16017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16019 token = cp_lexer_peek_token (parser->lexer);
16020 if (!processing_template_decl)
16021 error ("%Hkeyword %<typename%> not allowed outside of templates",
16024 error ("%Hkeyword %<typename%> not allowed in this context "
16025 "(the base class is implicitly a type)",
16027 cp_lexer_consume_token (parser->lexer);
16030 /* Look for the optional `::' operator. */
16031 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16032 /* Look for the nested-name-specifier. The simplest way to
16037 The keyword `typename' is not permitted in a base-specifier or
16038 mem-initializer; in these contexts a qualified name that
16039 depends on a template-parameter is implicitly assumed to be a
16042 is to pretend that we have seen the `typename' keyword at this
16044 cp_parser_nested_name_specifier_opt (parser,
16045 /*typename_keyword_p=*/true,
16046 /*check_dependency_p=*/true,
16048 /*is_declaration=*/true);
16049 /* If the base class is given by a qualified name, assume that names
16050 we see are type names or templates, as appropriate. */
16051 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16052 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16054 /* Finally, look for the class-name. */
16055 type = cp_parser_class_name (parser,
16059 /*check_dependency_p=*/true,
16060 /*class_head_p=*/false,
16061 /*is_declaration=*/true);
16063 if (type == error_mark_node)
16064 return error_mark_node;
16066 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16069 /* Exception handling [gram.exception] */
16071 /* Parse an (optional) exception-specification.
16073 exception-specification:
16074 throw ( type-id-list [opt] )
16076 Returns a TREE_LIST representing the exception-specification. The
16077 TREE_VALUE of each node is a type. */
16080 cp_parser_exception_specification_opt (cp_parser* parser)
16085 /* Peek at the next token. */
16086 token = cp_lexer_peek_token (parser->lexer);
16087 /* If it's not `throw', then there's no exception-specification. */
16088 if (!cp_parser_is_keyword (token, RID_THROW))
16091 /* Consume the `throw'. */
16092 cp_lexer_consume_token (parser->lexer);
16094 /* Look for the `('. */
16095 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16097 /* Peek at the next token. */
16098 token = cp_lexer_peek_token (parser->lexer);
16099 /* If it's not a `)', then there is a type-id-list. */
16100 if (token->type != CPP_CLOSE_PAREN)
16102 const char *saved_message;
16104 /* Types may not be defined in an exception-specification. */
16105 saved_message = parser->type_definition_forbidden_message;
16106 parser->type_definition_forbidden_message
16107 = "types may not be defined in an exception-specification";
16108 /* Parse the type-id-list. */
16109 type_id_list = cp_parser_type_id_list (parser);
16110 /* Restore the saved message. */
16111 parser->type_definition_forbidden_message = saved_message;
16114 type_id_list = empty_except_spec;
16116 /* Look for the `)'. */
16117 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16119 return type_id_list;
16122 /* Parse an (optional) type-id-list.
16126 type-id-list , type-id ... [opt]
16128 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16129 in the order that the types were presented. */
16132 cp_parser_type_id_list (cp_parser* parser)
16134 tree types = NULL_TREE;
16141 /* Get the next type-id. */
16142 type = cp_parser_type_id (parser);
16143 /* Parse the optional ellipsis. */
16144 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16146 /* Consume the `...'. */
16147 cp_lexer_consume_token (parser->lexer);
16149 /* Turn the type into a pack expansion expression. */
16150 type = make_pack_expansion (type);
16152 /* Add it to the list. */
16153 types = add_exception_specifier (types, type, /*complain=*/1);
16154 /* Peek at the next token. */
16155 token = cp_lexer_peek_token (parser->lexer);
16156 /* If it is not a `,', we are done. */
16157 if (token->type != CPP_COMMA)
16159 /* Consume the `,'. */
16160 cp_lexer_consume_token (parser->lexer);
16163 return nreverse (types);
16166 /* Parse a try-block.
16169 try compound-statement handler-seq */
16172 cp_parser_try_block (cp_parser* parser)
16176 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16177 try_block = begin_try_block ();
16178 cp_parser_compound_statement (parser, NULL, true);
16179 finish_try_block (try_block);
16180 cp_parser_handler_seq (parser);
16181 finish_handler_sequence (try_block);
16186 /* Parse a function-try-block.
16188 function-try-block:
16189 try ctor-initializer [opt] function-body handler-seq */
16192 cp_parser_function_try_block (cp_parser* parser)
16194 tree compound_stmt;
16196 bool ctor_initializer_p;
16198 /* Look for the `try' keyword. */
16199 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16201 /* Let the rest of the front end know where we are. */
16202 try_block = begin_function_try_block (&compound_stmt);
16203 /* Parse the function-body. */
16205 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16206 /* We're done with the `try' part. */
16207 finish_function_try_block (try_block);
16208 /* Parse the handlers. */
16209 cp_parser_handler_seq (parser);
16210 /* We're done with the handlers. */
16211 finish_function_handler_sequence (try_block, compound_stmt);
16213 return ctor_initializer_p;
16216 /* Parse a handler-seq.
16219 handler handler-seq [opt] */
16222 cp_parser_handler_seq (cp_parser* parser)
16228 /* Parse the handler. */
16229 cp_parser_handler (parser);
16230 /* Peek at the next token. */
16231 token = cp_lexer_peek_token (parser->lexer);
16232 /* If it's not `catch' then there are no more handlers. */
16233 if (!cp_parser_is_keyword (token, RID_CATCH))
16238 /* Parse a handler.
16241 catch ( exception-declaration ) compound-statement */
16244 cp_parser_handler (cp_parser* parser)
16249 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16250 handler = begin_handler ();
16251 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16252 declaration = cp_parser_exception_declaration (parser);
16253 finish_handler_parms (declaration, handler);
16254 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16255 cp_parser_compound_statement (parser, NULL, false);
16256 finish_handler (handler);
16259 /* Parse an exception-declaration.
16261 exception-declaration:
16262 type-specifier-seq declarator
16263 type-specifier-seq abstract-declarator
16267 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16268 ellipsis variant is used. */
16271 cp_parser_exception_declaration (cp_parser* parser)
16273 cp_decl_specifier_seq type_specifiers;
16274 cp_declarator *declarator;
16275 const char *saved_message;
16277 /* If it's an ellipsis, it's easy to handle. */
16278 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16280 /* Consume the `...' token. */
16281 cp_lexer_consume_token (parser->lexer);
16285 /* Types may not be defined in exception-declarations. */
16286 saved_message = parser->type_definition_forbidden_message;
16287 parser->type_definition_forbidden_message
16288 = "types may not be defined in exception-declarations";
16290 /* Parse the type-specifier-seq. */
16291 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16293 /* If it's a `)', then there is no declarator. */
16294 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16297 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16298 /*ctor_dtor_or_conv_p=*/NULL,
16299 /*parenthesized_p=*/NULL,
16300 /*member_p=*/false);
16302 /* Restore the saved message. */
16303 parser->type_definition_forbidden_message = saved_message;
16305 if (!type_specifiers.any_specifiers_p)
16306 return error_mark_node;
16308 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16311 /* Parse a throw-expression.
16314 throw assignment-expression [opt]
16316 Returns a THROW_EXPR representing the throw-expression. */
16319 cp_parser_throw_expression (cp_parser* parser)
16324 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16325 token = cp_lexer_peek_token (parser->lexer);
16326 /* Figure out whether or not there is an assignment-expression
16327 following the "throw" keyword. */
16328 if (token->type == CPP_COMMA
16329 || token->type == CPP_SEMICOLON
16330 || token->type == CPP_CLOSE_PAREN
16331 || token->type == CPP_CLOSE_SQUARE
16332 || token->type == CPP_CLOSE_BRACE
16333 || token->type == CPP_COLON)
16334 expression = NULL_TREE;
16336 expression = cp_parser_assignment_expression (parser,
16339 return build_throw (expression);
16342 /* GNU Extensions */
16344 /* Parse an (optional) asm-specification.
16347 asm ( string-literal )
16349 If the asm-specification is present, returns a STRING_CST
16350 corresponding to the string-literal. Otherwise, returns
16354 cp_parser_asm_specification_opt (cp_parser* parser)
16357 tree asm_specification;
16359 /* Peek at the next token. */
16360 token = cp_lexer_peek_token (parser->lexer);
16361 /* If the next token isn't the `asm' keyword, then there's no
16362 asm-specification. */
16363 if (!cp_parser_is_keyword (token, RID_ASM))
16366 /* Consume the `asm' token. */
16367 cp_lexer_consume_token (parser->lexer);
16368 /* Look for the `('. */
16369 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16371 /* Look for the string-literal. */
16372 asm_specification = cp_parser_string_literal (parser, false, false);
16374 /* Look for the `)'. */
16375 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16377 return asm_specification;
16380 /* Parse an asm-operand-list.
16384 asm-operand-list , asm-operand
16387 string-literal ( expression )
16388 [ string-literal ] string-literal ( expression )
16390 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16391 each node is the expression. The TREE_PURPOSE is itself a
16392 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16393 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16394 is a STRING_CST for the string literal before the parenthesis. Returns
16395 ERROR_MARK_NODE if any of the operands are invalid. */
16398 cp_parser_asm_operand_list (cp_parser* parser)
16400 tree asm_operands = NULL_TREE;
16401 bool invalid_operands = false;
16405 tree string_literal;
16409 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16411 /* Consume the `[' token. */
16412 cp_lexer_consume_token (parser->lexer);
16413 /* Read the operand name. */
16414 name = cp_parser_identifier (parser);
16415 if (name != error_mark_node)
16416 name = build_string (IDENTIFIER_LENGTH (name),
16417 IDENTIFIER_POINTER (name));
16418 /* Look for the closing `]'. */
16419 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16423 /* Look for the string-literal. */
16424 string_literal = cp_parser_string_literal (parser, false, false);
16426 /* Look for the `('. */
16427 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16428 /* Parse the expression. */
16429 expression = cp_parser_expression (parser, /*cast_p=*/false);
16430 /* Look for the `)'. */
16431 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16433 if (name == error_mark_node
16434 || string_literal == error_mark_node
16435 || expression == error_mark_node)
16436 invalid_operands = true;
16438 /* Add this operand to the list. */
16439 asm_operands = tree_cons (build_tree_list (name, string_literal),
16442 /* If the next token is not a `,', there are no more
16444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16446 /* Consume the `,'. */
16447 cp_lexer_consume_token (parser->lexer);
16450 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16453 /* Parse an asm-clobber-list.
16457 asm-clobber-list , string-literal
16459 Returns a TREE_LIST, indicating the clobbers in the order that they
16460 appeared. The TREE_VALUE of each node is a STRING_CST. */
16463 cp_parser_asm_clobber_list (cp_parser* parser)
16465 tree clobbers = NULL_TREE;
16469 tree string_literal;
16471 /* Look for the string literal. */
16472 string_literal = cp_parser_string_literal (parser, false, false);
16473 /* Add it to the list. */
16474 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16475 /* If the next token is not a `,', then the list is
16477 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16479 /* Consume the `,' token. */
16480 cp_lexer_consume_token (parser->lexer);
16486 /* Parse an (optional) series of attributes.
16489 attributes attribute
16492 __attribute__ (( attribute-list [opt] ))
16494 The return value is as for cp_parser_attribute_list. */
16497 cp_parser_attributes_opt (cp_parser* parser)
16499 tree attributes = NULL_TREE;
16504 tree attribute_list;
16506 /* Peek at the next token. */
16507 token = cp_lexer_peek_token (parser->lexer);
16508 /* If it's not `__attribute__', then we're done. */
16509 if (token->keyword != RID_ATTRIBUTE)
16512 /* Consume the `__attribute__' keyword. */
16513 cp_lexer_consume_token (parser->lexer);
16514 /* Look for the two `(' tokens. */
16515 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16516 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16518 /* Peek at the next token. */
16519 token = cp_lexer_peek_token (parser->lexer);
16520 if (token->type != CPP_CLOSE_PAREN)
16521 /* Parse the attribute-list. */
16522 attribute_list = cp_parser_attribute_list (parser);
16524 /* If the next token is a `)', then there is no attribute
16526 attribute_list = NULL;
16528 /* Look for the two `)' tokens. */
16529 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16530 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16532 /* Add these new attributes to the list. */
16533 attributes = chainon (attributes, attribute_list);
16539 /* Parse an attribute-list.
16543 attribute-list , attribute
16547 identifier ( identifier )
16548 identifier ( identifier , expression-list )
16549 identifier ( expression-list )
16551 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16552 to an attribute. The TREE_PURPOSE of each node is the identifier
16553 indicating which attribute is in use. The TREE_VALUE represents
16554 the arguments, if any. */
16557 cp_parser_attribute_list (cp_parser* parser)
16559 tree attribute_list = NULL_TREE;
16560 bool save_translate_strings_p = parser->translate_strings_p;
16562 parser->translate_strings_p = false;
16569 /* Look for the identifier. We also allow keywords here; for
16570 example `__attribute__ ((const))' is legal. */
16571 token = cp_lexer_peek_token (parser->lexer);
16572 if (token->type == CPP_NAME
16573 || token->type == CPP_KEYWORD)
16575 tree arguments = NULL_TREE;
16577 /* Consume the token. */
16578 token = cp_lexer_consume_token (parser->lexer);
16580 /* Save away the identifier that indicates which attribute
16582 identifier = token->u.value;
16583 attribute = build_tree_list (identifier, NULL_TREE);
16585 /* Peek at the next token. */
16586 token = cp_lexer_peek_token (parser->lexer);
16587 /* If it's an `(', then parse the attribute arguments. */
16588 if (token->type == CPP_OPEN_PAREN)
16590 arguments = cp_parser_parenthesized_expression_list
16591 (parser, true, /*cast_p=*/false,
16592 /*allow_expansion_p=*/false,
16593 /*non_constant_p=*/NULL);
16594 /* Save the arguments away. */
16595 TREE_VALUE (attribute) = arguments;
16598 if (arguments != error_mark_node)
16600 /* Add this attribute to the list. */
16601 TREE_CHAIN (attribute) = attribute_list;
16602 attribute_list = attribute;
16605 token = cp_lexer_peek_token (parser->lexer);
16607 /* Now, look for more attributes. If the next token isn't a
16608 `,', we're done. */
16609 if (token->type != CPP_COMMA)
16612 /* Consume the comma and keep going. */
16613 cp_lexer_consume_token (parser->lexer);
16615 parser->translate_strings_p = save_translate_strings_p;
16617 /* We built up the list in reverse order. */
16618 return nreverse (attribute_list);
16621 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16622 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16623 current value of the PEDANTIC flag, regardless of whether or not
16624 the `__extension__' keyword is present. The caller is responsible
16625 for restoring the value of the PEDANTIC flag. */
16628 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16630 /* Save the old value of the PEDANTIC flag. */
16631 *saved_pedantic = pedantic;
16633 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16635 /* Consume the `__extension__' token. */
16636 cp_lexer_consume_token (parser->lexer);
16637 /* We're not being pedantic while the `__extension__' keyword is
16647 /* Parse a label declaration.
16650 __label__ label-declarator-seq ;
16652 label-declarator-seq:
16653 identifier , label-declarator-seq
16657 cp_parser_label_declaration (cp_parser* parser)
16659 /* Look for the `__label__' keyword. */
16660 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16666 /* Look for an identifier. */
16667 identifier = cp_parser_identifier (parser);
16668 /* If we failed, stop. */
16669 if (identifier == error_mark_node)
16671 /* Declare it as a label. */
16672 finish_label_decl (identifier);
16673 /* If the next token is a `;', stop. */
16674 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16676 /* Look for the `,' separating the label declarations. */
16677 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16680 /* Look for the final `;'. */
16681 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16684 /* Support Functions */
16686 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16687 NAME should have one of the representations used for an
16688 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16689 is returned. If PARSER->SCOPE is a dependent type, then a
16690 SCOPE_REF is returned.
16692 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16693 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16694 was formed. Abstractly, such entities should not be passed to this
16695 function, because they do not need to be looked up, but it is
16696 simpler to check for this special case here, rather than at the
16699 In cases not explicitly covered above, this function returns a
16700 DECL, OVERLOAD, or baselink representing the result of the lookup.
16701 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16704 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16705 (e.g., "struct") that was used. In that case bindings that do not
16706 refer to types are ignored.
16708 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16711 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16714 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16717 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16718 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16719 NULL_TREE otherwise. */
16722 cp_parser_lookup_name (cp_parser *parser, tree name,
16723 enum tag_types tag_type,
16726 bool check_dependency,
16727 tree *ambiguous_decls,
16728 location_t name_location)
16732 tree object_type = parser->context->object_type;
16734 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16735 flags |= LOOKUP_COMPLAIN;
16737 /* Assume that the lookup will be unambiguous. */
16738 if (ambiguous_decls)
16739 *ambiguous_decls = NULL_TREE;
16741 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16742 no longer valid. Note that if we are parsing tentatively, and
16743 the parse fails, OBJECT_TYPE will be automatically restored. */
16744 parser->context->object_type = NULL_TREE;
16746 if (name == error_mark_node)
16747 return error_mark_node;
16749 /* A template-id has already been resolved; there is no lookup to
16751 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16753 if (BASELINK_P (name))
16755 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16756 == TEMPLATE_ID_EXPR);
16760 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16761 it should already have been checked to make sure that the name
16762 used matches the type being destroyed. */
16763 if (TREE_CODE (name) == BIT_NOT_EXPR)
16767 /* Figure out to which type this destructor applies. */
16769 type = parser->scope;
16770 else if (object_type)
16771 type = object_type;
16773 type = current_class_type;
16774 /* If that's not a class type, there is no destructor. */
16775 if (!type || !CLASS_TYPE_P (type))
16776 return error_mark_node;
16777 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16778 lazily_declare_fn (sfk_destructor, type);
16779 if (!CLASSTYPE_DESTRUCTORS (type))
16780 return error_mark_node;
16781 /* If it was a class type, return the destructor. */
16782 return CLASSTYPE_DESTRUCTORS (type);
16785 /* By this point, the NAME should be an ordinary identifier. If
16786 the id-expression was a qualified name, the qualifying scope is
16787 stored in PARSER->SCOPE at this point. */
16788 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16790 /* Perform the lookup. */
16795 if (parser->scope == error_mark_node)
16796 return error_mark_node;
16798 /* If the SCOPE is dependent, the lookup must be deferred until
16799 the template is instantiated -- unless we are explicitly
16800 looking up names in uninstantiated templates. Even then, we
16801 cannot look up the name if the scope is not a class type; it
16802 might, for example, be a template type parameter. */
16803 dependent_p = (TYPE_P (parser->scope)
16804 && !(parser->in_declarator_p
16805 && currently_open_class (parser->scope))
16806 && dependent_type_p (parser->scope));
16807 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16814 /* The resolution to Core Issue 180 says that `struct
16815 A::B' should be considered a type-name, even if `A'
16817 type = make_typename_type (parser->scope, name, tag_type,
16818 /*complain=*/tf_error);
16819 decl = TYPE_NAME (type);
16821 else if (is_template
16822 && (cp_parser_next_token_ends_template_argument_p (parser)
16823 || cp_lexer_next_token_is (parser->lexer,
16825 decl = make_unbound_class_template (parser->scope,
16827 /*complain=*/tf_error);
16829 decl = build_qualified_name (/*type=*/NULL_TREE,
16830 parser->scope, name,
16835 tree pushed_scope = NULL_TREE;
16837 /* If PARSER->SCOPE is a dependent type, then it must be a
16838 class type, and we must not be checking dependencies;
16839 otherwise, we would have processed this lookup above. So
16840 that PARSER->SCOPE is not considered a dependent base by
16841 lookup_member, we must enter the scope here. */
16843 pushed_scope = push_scope (parser->scope);
16844 /* If the PARSER->SCOPE is a template specialization, it
16845 may be instantiated during name lookup. In that case,
16846 errors may be issued. Even if we rollback the current
16847 tentative parse, those errors are valid. */
16848 decl = lookup_qualified_name (parser->scope, name,
16849 tag_type != none_type,
16850 /*complain=*/true);
16852 /* If we have a single function from a using decl, pull it out. */
16854 && TREE_CODE (decl) == OVERLOAD
16855 && !really_overloaded_fn (decl))
16856 decl = OVL_FUNCTION (decl);
16859 pop_scope (pushed_scope);
16861 parser->qualifying_scope = parser->scope;
16862 parser->object_scope = NULL_TREE;
16864 else if (object_type)
16866 tree object_decl = NULL_TREE;
16867 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16868 OBJECT_TYPE is not a class. */
16869 if (CLASS_TYPE_P (object_type))
16870 /* If the OBJECT_TYPE is a template specialization, it may
16871 be instantiated during name lookup. In that case, errors
16872 may be issued. Even if we rollback the current tentative
16873 parse, those errors are valid. */
16874 object_decl = lookup_member (object_type,
16877 tag_type != none_type);
16878 /* Look it up in the enclosing context, too. */
16879 decl = lookup_name_real (name, tag_type != none_type,
16881 /*block_p=*/true, is_namespace, flags);
16882 parser->object_scope = object_type;
16883 parser->qualifying_scope = NULL_TREE;
16885 decl = object_decl;
16889 decl = lookup_name_real (name, tag_type != none_type,
16891 /*block_p=*/true, is_namespace, flags);
16892 parser->qualifying_scope = NULL_TREE;
16893 parser->object_scope = NULL_TREE;
16896 /* If the lookup failed, let our caller know. */
16897 if (!decl || decl == error_mark_node)
16898 return error_mark_node;
16900 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16901 if (TREE_CODE (decl) == TREE_LIST)
16903 if (ambiguous_decls)
16904 *ambiguous_decls = decl;
16905 /* The error message we have to print is too complicated for
16906 cp_parser_error, so we incorporate its actions directly. */
16907 if (!cp_parser_simulate_error (parser))
16909 error ("%Hreference to %qD is ambiguous",
16910 &name_location, name);
16911 print_candidates (decl);
16913 return error_mark_node;
16916 gcc_assert (DECL_P (decl)
16917 || TREE_CODE (decl) == OVERLOAD
16918 || TREE_CODE (decl) == SCOPE_REF
16919 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16920 || BASELINK_P (decl));
16922 /* If we have resolved the name of a member declaration, check to
16923 see if the declaration is accessible. When the name resolves to
16924 set of overloaded functions, accessibility is checked when
16925 overload resolution is done.
16927 During an explicit instantiation, access is not checked at all,
16928 as per [temp.explicit]. */
16930 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16935 /* Like cp_parser_lookup_name, but for use in the typical case where
16936 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16937 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16940 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
16942 return cp_parser_lookup_name (parser, name,
16944 /*is_template=*/false,
16945 /*is_namespace=*/false,
16946 /*check_dependency=*/true,
16947 /*ambiguous_decls=*/NULL,
16951 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16952 the current context, return the TYPE_DECL. If TAG_NAME_P is
16953 true, the DECL indicates the class being defined in a class-head,
16954 or declared in an elaborated-type-specifier.
16956 Otherwise, return DECL. */
16959 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16961 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16962 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16965 template <typename T> struct B;
16968 template <typename T> struct A::B {};
16970 Similarly, in an elaborated-type-specifier:
16972 namespace N { struct X{}; }
16975 template <typename T> friend struct N::X;
16978 However, if the DECL refers to a class type, and we are in
16979 the scope of the class, then the name lookup automatically
16980 finds the TYPE_DECL created by build_self_reference rather
16981 than a TEMPLATE_DECL. For example, in:
16983 template <class T> struct S {
16987 there is no need to handle such case. */
16989 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16990 return DECL_TEMPLATE_RESULT (decl);
16995 /* If too many, or too few, template-parameter lists apply to the
16996 declarator, issue an error message. Returns TRUE if all went well,
16997 and FALSE otherwise. */
17000 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17001 cp_declarator *declarator,
17002 location_t declarator_location)
17004 unsigned num_templates;
17006 /* We haven't seen any classes that involve template parameters yet. */
17009 switch (declarator->kind)
17012 if (declarator->u.id.qualifying_scope)
17017 scope = declarator->u.id.qualifying_scope;
17018 member = declarator->u.id.unqualified_name;
17020 while (scope && CLASS_TYPE_P (scope))
17022 /* You're supposed to have one `template <...>'
17023 for every template class, but you don't need one
17024 for a full specialization. For example:
17026 template <class T> struct S{};
17027 template <> struct S<int> { void f(); };
17028 void S<int>::f () {}
17030 is correct; there shouldn't be a `template <>' for
17031 the definition of `S<int>::f'. */
17032 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17033 /* If SCOPE does not have template information of any
17034 kind, then it is not a template, nor is it nested
17035 within a template. */
17037 if (explicit_class_specialization_p (scope))
17039 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17042 scope = TYPE_CONTEXT (scope);
17045 else if (TREE_CODE (declarator->u.id.unqualified_name)
17046 == TEMPLATE_ID_EXPR)
17047 /* If the DECLARATOR has the form `X<y>' then it uses one
17048 additional level of template parameters. */
17051 return cp_parser_check_template_parameters (parser,
17053 declarator_location);
17058 case cdk_reference:
17060 return (cp_parser_check_declarator_template_parameters
17061 (parser, declarator->declarator, declarator_location));
17067 gcc_unreachable ();
17072 /* NUM_TEMPLATES were used in the current declaration. If that is
17073 invalid, return FALSE and issue an error messages. Otherwise,
17077 cp_parser_check_template_parameters (cp_parser* parser,
17078 unsigned num_templates,
17079 location_t location)
17081 /* If there are more template classes than parameter lists, we have
17084 template <class T> void S<T>::R<T>::f (); */
17085 if (parser->num_template_parameter_lists < num_templates)
17087 error ("%Htoo few template-parameter-lists", &location);
17090 /* If there are the same number of template classes and parameter
17091 lists, that's OK. */
17092 if (parser->num_template_parameter_lists == num_templates)
17094 /* If there are more, but only one more, then we are referring to a
17095 member template. That's OK too. */
17096 if (parser->num_template_parameter_lists == num_templates + 1)
17098 /* Otherwise, there are too many template parameter lists. We have
17101 template <class T> template <class U> void S::f(); */
17102 error ("%Htoo many template-parameter-lists", &location);
17106 /* Parse an optional `::' token indicating that the following name is
17107 from the global namespace. If so, PARSER->SCOPE is set to the
17108 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17109 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17110 Returns the new value of PARSER->SCOPE, if the `::' token is
17111 present, and NULL_TREE otherwise. */
17114 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17118 /* Peek at the next token. */
17119 token = cp_lexer_peek_token (parser->lexer);
17120 /* If we're looking at a `::' token then we're starting from the
17121 global namespace, not our current location. */
17122 if (token->type == CPP_SCOPE)
17124 /* Consume the `::' token. */
17125 cp_lexer_consume_token (parser->lexer);
17126 /* Set the SCOPE so that we know where to start the lookup. */
17127 parser->scope = global_namespace;
17128 parser->qualifying_scope = global_namespace;
17129 parser->object_scope = NULL_TREE;
17131 return parser->scope;
17133 else if (!current_scope_valid_p)
17135 parser->scope = NULL_TREE;
17136 parser->qualifying_scope = NULL_TREE;
17137 parser->object_scope = NULL_TREE;
17143 /* Returns TRUE if the upcoming token sequence is the start of a
17144 constructor declarator. If FRIEND_P is true, the declarator is
17145 preceded by the `friend' specifier. */
17148 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17150 bool constructor_p;
17151 tree type_decl = NULL_TREE;
17152 bool nested_name_p;
17153 cp_token *next_token;
17155 /* The common case is that this is not a constructor declarator, so
17156 try to avoid doing lots of work if at all possible. It's not
17157 valid declare a constructor at function scope. */
17158 if (parser->in_function_body)
17160 /* And only certain tokens can begin a constructor declarator. */
17161 next_token = cp_lexer_peek_token (parser->lexer);
17162 if (next_token->type != CPP_NAME
17163 && next_token->type != CPP_SCOPE
17164 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17165 && next_token->type != CPP_TEMPLATE_ID)
17168 /* Parse tentatively; we are going to roll back all of the tokens
17170 cp_parser_parse_tentatively (parser);
17171 /* Assume that we are looking at a constructor declarator. */
17172 constructor_p = true;
17174 /* Look for the optional `::' operator. */
17175 cp_parser_global_scope_opt (parser,
17176 /*current_scope_valid_p=*/false);
17177 /* Look for the nested-name-specifier. */
17179 = (cp_parser_nested_name_specifier_opt (parser,
17180 /*typename_keyword_p=*/false,
17181 /*check_dependency_p=*/false,
17183 /*is_declaration=*/false)
17185 /* Outside of a class-specifier, there must be a
17186 nested-name-specifier. */
17187 if (!nested_name_p &&
17188 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17190 constructor_p = false;
17191 /* If we still think that this might be a constructor-declarator,
17192 look for a class-name. */
17197 template <typename T> struct S { S(); };
17198 template <typename T> S<T>::S ();
17200 we must recognize that the nested `S' names a class.
17203 template <typename T> S<T>::S<T> ();
17205 we must recognize that the nested `S' names a template. */
17206 type_decl = cp_parser_class_name (parser,
17207 /*typename_keyword_p=*/false,
17208 /*template_keyword_p=*/false,
17210 /*check_dependency_p=*/false,
17211 /*class_head_p=*/false,
17212 /*is_declaration=*/false);
17213 /* If there was no class-name, then this is not a constructor. */
17214 constructor_p = !cp_parser_error_occurred (parser);
17217 /* If we're still considering a constructor, we have to see a `(',
17218 to begin the parameter-declaration-clause, followed by either a
17219 `)', an `...', or a decl-specifier. We need to check for a
17220 type-specifier to avoid being fooled into thinking that:
17224 is a constructor. (It is actually a function named `f' that
17225 takes one parameter (of type `int') and returns a value of type
17228 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17230 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17231 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17232 /* A parameter declaration begins with a decl-specifier,
17233 which is either the "attribute" keyword, a storage class
17234 specifier, or (usually) a type-specifier. */
17235 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17238 tree pushed_scope = NULL_TREE;
17239 unsigned saved_num_template_parameter_lists;
17241 /* Names appearing in the type-specifier should be looked up
17242 in the scope of the class. */
17243 if (current_class_type)
17247 type = TREE_TYPE (type_decl);
17248 if (TREE_CODE (type) == TYPENAME_TYPE)
17250 type = resolve_typename_type (type,
17251 /*only_current_p=*/false);
17252 if (TREE_CODE (type) == TYPENAME_TYPE)
17254 cp_parser_abort_tentative_parse (parser);
17258 pushed_scope = push_scope (type);
17261 /* Inside the constructor parameter list, surrounding
17262 template-parameter-lists do not apply. */
17263 saved_num_template_parameter_lists
17264 = parser->num_template_parameter_lists;
17265 parser->num_template_parameter_lists = 0;
17267 /* Look for the type-specifier. */
17268 cp_parser_type_specifier (parser,
17269 CP_PARSER_FLAGS_NONE,
17270 /*decl_specs=*/NULL,
17271 /*is_declarator=*/true,
17272 /*declares_class_or_enum=*/NULL,
17273 /*is_cv_qualifier=*/NULL);
17275 parser->num_template_parameter_lists
17276 = saved_num_template_parameter_lists;
17278 /* Leave the scope of the class. */
17280 pop_scope (pushed_scope);
17282 constructor_p = !cp_parser_error_occurred (parser);
17286 constructor_p = false;
17287 /* We did not really want to consume any tokens. */
17288 cp_parser_abort_tentative_parse (parser);
17290 return constructor_p;
17293 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17294 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17295 they must be performed once we are in the scope of the function.
17297 Returns the function defined. */
17300 cp_parser_function_definition_from_specifiers_and_declarator
17301 (cp_parser* parser,
17302 cp_decl_specifier_seq *decl_specifiers,
17304 const cp_declarator *declarator)
17309 /* Begin the function-definition. */
17310 success_p = start_function (decl_specifiers, declarator, attributes);
17312 /* The things we're about to see are not directly qualified by any
17313 template headers we've seen thus far. */
17314 reset_specialization ();
17316 /* If there were names looked up in the decl-specifier-seq that we
17317 did not check, check them now. We must wait until we are in the
17318 scope of the function to perform the checks, since the function
17319 might be a friend. */
17320 perform_deferred_access_checks ();
17324 /* Skip the entire function. */
17325 cp_parser_skip_to_end_of_block_or_statement (parser);
17326 fn = error_mark_node;
17328 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17330 /* Seen already, skip it. An error message has already been output. */
17331 cp_parser_skip_to_end_of_block_or_statement (parser);
17332 fn = current_function_decl;
17333 current_function_decl = NULL_TREE;
17334 /* If this is a function from a class, pop the nested class. */
17335 if (current_class_name)
17336 pop_nested_class ();
17339 fn = cp_parser_function_definition_after_declarator (parser,
17340 /*inline_p=*/false);
17345 /* Parse the part of a function-definition that follows the
17346 declarator. INLINE_P is TRUE iff this function is an inline
17347 function defined with a class-specifier.
17349 Returns the function defined. */
17352 cp_parser_function_definition_after_declarator (cp_parser* parser,
17356 bool ctor_initializer_p = false;
17357 bool saved_in_unbraced_linkage_specification_p;
17358 bool saved_in_function_body;
17359 unsigned saved_num_template_parameter_lists;
17362 saved_in_function_body = parser->in_function_body;
17363 parser->in_function_body = true;
17364 /* If the next token is `return', then the code may be trying to
17365 make use of the "named return value" extension that G++ used to
17367 token = cp_lexer_peek_token (parser->lexer);
17368 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17370 /* Consume the `return' keyword. */
17371 cp_lexer_consume_token (parser->lexer);
17372 /* Look for the identifier that indicates what value is to be
17374 cp_parser_identifier (parser);
17375 /* Issue an error message. */
17376 error ("%Hnamed return values are no longer supported",
17378 /* Skip tokens until we reach the start of the function body. */
17381 cp_token *token = cp_lexer_peek_token (parser->lexer);
17382 if (token->type == CPP_OPEN_BRACE
17383 || token->type == CPP_EOF
17384 || token->type == CPP_PRAGMA_EOL)
17386 cp_lexer_consume_token (parser->lexer);
17389 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17390 anything declared inside `f'. */
17391 saved_in_unbraced_linkage_specification_p
17392 = parser->in_unbraced_linkage_specification_p;
17393 parser->in_unbraced_linkage_specification_p = false;
17394 /* Inside the function, surrounding template-parameter-lists do not
17396 saved_num_template_parameter_lists
17397 = parser->num_template_parameter_lists;
17398 parser->num_template_parameter_lists = 0;
17399 /* If the next token is `try', then we are looking at a
17400 function-try-block. */
17401 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17402 ctor_initializer_p = cp_parser_function_try_block (parser);
17403 /* A function-try-block includes the function-body, so we only do
17404 this next part if we're not processing a function-try-block. */
17407 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17409 /* Finish the function. */
17410 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17411 (inline_p ? 2 : 0));
17412 /* Generate code for it, if necessary. */
17413 expand_or_defer_fn (fn);
17414 /* Restore the saved values. */
17415 parser->in_unbraced_linkage_specification_p
17416 = saved_in_unbraced_linkage_specification_p;
17417 parser->num_template_parameter_lists
17418 = saved_num_template_parameter_lists;
17419 parser->in_function_body = saved_in_function_body;
17424 /* Parse a template-declaration, assuming that the `export' (and
17425 `extern') keywords, if present, has already been scanned. MEMBER_P
17426 is as for cp_parser_template_declaration. */
17429 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17431 tree decl = NULL_TREE;
17432 VEC (deferred_access_check,gc) *checks;
17433 tree parameter_list;
17434 bool friend_p = false;
17435 bool need_lang_pop;
17438 /* Look for the `template' keyword. */
17439 token = cp_lexer_peek_token (parser->lexer);
17440 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17444 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17446 if (at_class_scope_p () && current_function_decl)
17448 /* 14.5.2.2 [temp.mem]
17450 A local class shall not have member templates. */
17451 error ("%Hinvalid declaration of member template in local class",
17453 cp_parser_skip_to_end_of_block_or_statement (parser);
17458 A template ... shall not have C linkage. */
17459 if (current_lang_name == lang_name_c)
17461 error ("%Htemplate with C linkage", &token->location);
17462 /* Give it C++ linkage to avoid confusing other parts of the
17464 push_lang_context (lang_name_cplusplus);
17465 need_lang_pop = true;
17468 need_lang_pop = false;
17470 /* We cannot perform access checks on the template parameter
17471 declarations until we know what is being declared, just as we
17472 cannot check the decl-specifier list. */
17473 push_deferring_access_checks (dk_deferred);
17475 /* If the next token is `>', then we have an invalid
17476 specialization. Rather than complain about an invalid template
17477 parameter, issue an error message here. */
17478 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17480 cp_parser_error (parser, "invalid explicit specialization");
17481 begin_specialization ();
17482 parameter_list = NULL_TREE;
17485 /* Parse the template parameters. */
17486 parameter_list = cp_parser_template_parameter_list (parser);
17488 /* Get the deferred access checks from the parameter list. These
17489 will be checked once we know what is being declared, as for a
17490 member template the checks must be performed in the scope of the
17491 class containing the member. */
17492 checks = get_deferred_access_checks ();
17494 /* Look for the `>'. */
17495 cp_parser_skip_to_end_of_template_parameter_list (parser);
17496 /* We just processed one more parameter list. */
17497 ++parser->num_template_parameter_lists;
17498 /* If the next token is `template', there are more template
17500 if (cp_lexer_next_token_is_keyword (parser->lexer,
17502 cp_parser_template_declaration_after_export (parser, member_p);
17505 /* There are no access checks when parsing a template, as we do not
17506 know if a specialization will be a friend. */
17507 push_deferring_access_checks (dk_no_check);
17508 token = cp_lexer_peek_token (parser->lexer);
17509 decl = cp_parser_single_declaration (parser,
17512 /*explicit_specialization_p=*/false,
17514 pop_deferring_access_checks ();
17516 /* If this is a member template declaration, let the front
17518 if (member_p && !friend_p && decl)
17520 if (TREE_CODE (decl) == TYPE_DECL)
17521 cp_parser_check_access_in_redeclaration (decl, token->location);
17523 decl = finish_member_template_decl (decl);
17525 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17526 make_friend_class (current_class_type, TREE_TYPE (decl),
17527 /*complain=*/true);
17529 /* We are done with the current parameter list. */
17530 --parser->num_template_parameter_lists;
17532 pop_deferring_access_checks ();
17535 finish_template_decl (parameter_list);
17537 /* Register member declarations. */
17538 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17539 finish_member_declaration (decl);
17540 /* For the erroneous case of a template with C linkage, we pushed an
17541 implicit C++ linkage scope; exit that scope now. */
17543 pop_lang_context ();
17544 /* If DECL is a function template, we must return to parse it later.
17545 (Even though there is no definition, there might be default
17546 arguments that need handling.) */
17547 if (member_p && decl
17548 && (TREE_CODE (decl) == FUNCTION_DECL
17549 || DECL_FUNCTION_TEMPLATE_P (decl)))
17550 TREE_VALUE (parser->unparsed_functions_queues)
17551 = tree_cons (NULL_TREE, decl,
17552 TREE_VALUE (parser->unparsed_functions_queues));
17555 /* Perform the deferred access checks from a template-parameter-list.
17556 CHECKS is a TREE_LIST of access checks, as returned by
17557 get_deferred_access_checks. */
17560 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17562 ++processing_template_parmlist;
17563 perform_access_checks (checks);
17564 --processing_template_parmlist;
17567 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17568 `function-definition' sequence. MEMBER_P is true, this declaration
17569 appears in a class scope.
17571 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17572 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17575 cp_parser_single_declaration (cp_parser* parser,
17576 VEC (deferred_access_check,gc)* checks,
17578 bool explicit_specialization_p,
17581 int declares_class_or_enum;
17582 tree decl = NULL_TREE;
17583 cp_decl_specifier_seq decl_specifiers;
17584 bool function_definition_p = false;
17585 cp_token *decl_spec_token_start;
17587 /* This function is only used when processing a template
17589 gcc_assert (innermost_scope_kind () == sk_template_parms
17590 || innermost_scope_kind () == sk_template_spec);
17592 /* Defer access checks until we know what is being declared. */
17593 push_deferring_access_checks (dk_deferred);
17595 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17597 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17598 cp_parser_decl_specifier_seq (parser,
17599 CP_PARSER_FLAGS_OPTIONAL,
17601 &declares_class_or_enum);
17603 *friend_p = cp_parser_friend_p (&decl_specifiers);
17605 /* There are no template typedefs. */
17606 if (decl_specifiers.specs[(int) ds_typedef])
17608 error ("%Htemplate declaration of %qs",
17609 &decl_spec_token_start->location, "typedef");
17610 decl = error_mark_node;
17613 /* Gather up the access checks that occurred the
17614 decl-specifier-seq. */
17615 stop_deferring_access_checks ();
17617 /* Check for the declaration of a template class. */
17618 if (declares_class_or_enum)
17620 if (cp_parser_declares_only_class_p (parser))
17622 decl = shadow_tag (&decl_specifiers);
17627 friend template <typename T> struct A<T>::B;
17630 A<T>::B will be represented by a TYPENAME_TYPE, and
17631 therefore not recognized by shadow_tag. */
17632 if (friend_p && *friend_p
17634 && decl_specifiers.type
17635 && TYPE_P (decl_specifiers.type))
17636 decl = decl_specifiers.type;
17638 if (decl && decl != error_mark_node)
17639 decl = TYPE_NAME (decl);
17641 decl = error_mark_node;
17643 /* Perform access checks for template parameters. */
17644 cp_parser_perform_template_parameter_access_checks (checks);
17647 /* If it's not a template class, try for a template function. If
17648 the next token is a `;', then this declaration does not declare
17649 anything. But, if there were errors in the decl-specifiers, then
17650 the error might well have come from an attempted class-specifier.
17651 In that case, there's no need to warn about a missing declarator. */
17653 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17654 || decl_specifiers.type != error_mark_node))
17656 decl = cp_parser_init_declarator (parser,
17659 /*function_definition_allowed_p=*/true,
17661 declares_class_or_enum,
17662 &function_definition_p);
17664 /* 7.1.1-1 [dcl.stc]
17666 A storage-class-specifier shall not be specified in an explicit
17667 specialization... */
17669 && explicit_specialization_p
17670 && decl_specifiers.storage_class != sc_none)
17672 error ("%Hexplicit template specialization cannot have a storage class",
17673 &decl_spec_token_start->location);
17674 decl = error_mark_node;
17678 pop_deferring_access_checks ();
17680 /* Clear any current qualification; whatever comes next is the start
17681 of something new. */
17682 parser->scope = NULL_TREE;
17683 parser->qualifying_scope = NULL_TREE;
17684 parser->object_scope = NULL_TREE;
17685 /* Look for a trailing `;' after the declaration. */
17686 if (!function_definition_p
17687 && (decl == error_mark_node
17688 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17689 cp_parser_skip_to_end_of_block_or_statement (parser);
17694 /* Parse a cast-expression that is not the operand of a unary "&". */
17697 cp_parser_simple_cast_expression (cp_parser *parser)
17699 return cp_parser_cast_expression (parser, /*address_p=*/false,
17703 /* Parse a functional cast to TYPE. Returns an expression
17704 representing the cast. */
17707 cp_parser_functional_cast (cp_parser* parser, tree type)
17709 tree expression_list;
17713 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17715 maybe_warn_cpp0x ("extended initializer lists");
17716 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17717 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17718 if (TREE_CODE (type) == TYPE_DECL)
17719 type = TREE_TYPE (type);
17720 return finish_compound_literal (type, expression_list);
17724 = cp_parser_parenthesized_expression_list (parser, false,
17726 /*allow_expansion_p=*/true,
17727 /*non_constant_p=*/NULL);
17729 cast = build_functional_cast (type, expression_list,
17730 tf_warning_or_error);
17731 /* [expr.const]/1: In an integral constant expression "only type
17732 conversions to integral or enumeration type can be used". */
17733 if (TREE_CODE (type) == TYPE_DECL)
17734 type = TREE_TYPE (type);
17735 if (cast != error_mark_node
17736 && !cast_valid_in_integral_constant_expression_p (type)
17737 && (cp_parser_non_integral_constant_expression
17738 (parser, "a call to a constructor")))
17739 return error_mark_node;
17743 /* Save the tokens that make up the body of a member function defined
17744 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17745 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17746 specifiers applied to the declaration. Returns the FUNCTION_DECL
17747 for the member function. */
17750 cp_parser_save_member_function_body (cp_parser* parser,
17751 cp_decl_specifier_seq *decl_specifiers,
17752 cp_declarator *declarator,
17759 /* Create the function-declaration. */
17760 fn = start_method (decl_specifiers, declarator, attributes);
17761 /* If something went badly wrong, bail out now. */
17762 if (fn == error_mark_node)
17764 /* If there's a function-body, skip it. */
17765 if (cp_parser_token_starts_function_definition_p
17766 (cp_lexer_peek_token (parser->lexer)))
17767 cp_parser_skip_to_end_of_block_or_statement (parser);
17768 return error_mark_node;
17771 /* Remember it, if there default args to post process. */
17772 cp_parser_save_default_args (parser, fn);
17774 /* Save away the tokens that make up the body of the
17776 first = parser->lexer->next_token;
17777 /* We can have braced-init-list mem-initializers before the fn body. */
17778 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17780 cp_lexer_consume_token (parser->lexer);
17781 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17782 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17784 /* cache_group will stop after an un-nested { } pair, too. */
17785 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17788 /* variadic mem-inits have ... after the ')'. */
17789 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17790 cp_lexer_consume_token (parser->lexer);
17793 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17794 /* Handle function try blocks. */
17795 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17796 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17797 last = parser->lexer->next_token;
17799 /* Save away the inline definition; we will process it when the
17800 class is complete. */
17801 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17802 DECL_PENDING_INLINE_P (fn) = 1;
17804 /* We need to know that this was defined in the class, so that
17805 friend templates are handled correctly. */
17806 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17808 /* We're done with the inline definition. */
17809 finish_method (fn);
17811 /* Add FN to the queue of functions to be parsed later. */
17812 TREE_VALUE (parser->unparsed_functions_queues)
17813 = tree_cons (NULL_TREE, fn,
17814 TREE_VALUE (parser->unparsed_functions_queues));
17819 /* Parse a template-argument-list, as well as the trailing ">" (but
17820 not the opening ">"). See cp_parser_template_argument_list for the
17824 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17828 tree saved_qualifying_scope;
17829 tree saved_object_scope;
17830 bool saved_greater_than_is_operator_p;
17831 bool saved_skip_evaluation;
17835 When parsing a template-id, the first non-nested `>' is taken as
17836 the end of the template-argument-list rather than a greater-than
17838 saved_greater_than_is_operator_p
17839 = parser->greater_than_is_operator_p;
17840 parser->greater_than_is_operator_p = false;
17841 /* Parsing the argument list may modify SCOPE, so we save it
17843 saved_scope = parser->scope;
17844 saved_qualifying_scope = parser->qualifying_scope;
17845 saved_object_scope = parser->object_scope;
17846 /* We need to evaluate the template arguments, even though this
17847 template-id may be nested within a "sizeof". */
17848 saved_skip_evaluation = skip_evaluation;
17849 skip_evaluation = false;
17850 /* Parse the template-argument-list itself. */
17851 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17852 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17853 arguments = NULL_TREE;
17855 arguments = cp_parser_template_argument_list (parser);
17856 /* Look for the `>' that ends the template-argument-list. If we find
17857 a '>>' instead, it's probably just a typo. */
17858 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17860 if (cxx_dialect != cxx98)
17862 /* In C++0x, a `>>' in a template argument list or cast
17863 expression is considered to be two separate `>'
17864 tokens. So, change the current token to a `>', but don't
17865 consume it: it will be consumed later when the outer
17866 template argument list (or cast expression) is parsed.
17867 Note that this replacement of `>' for `>>' is necessary
17868 even if we are parsing tentatively: in the tentative
17869 case, after calling
17870 cp_parser_enclosed_template_argument_list we will always
17871 throw away all of the template arguments and the first
17872 closing `>', either because the template argument list
17873 was erroneous or because we are replacing those tokens
17874 with a CPP_TEMPLATE_ID token. The second `>' (which will
17875 not have been thrown away) is needed either to close an
17876 outer template argument list or to complete a new-style
17878 cp_token *token = cp_lexer_peek_token (parser->lexer);
17879 token->type = CPP_GREATER;
17881 else if (!saved_greater_than_is_operator_p)
17883 /* If we're in a nested template argument list, the '>>' has
17884 to be a typo for '> >'. We emit the error message, but we
17885 continue parsing and we push a '>' as next token, so that
17886 the argument list will be parsed correctly. Note that the
17887 global source location is still on the token before the
17888 '>>', so we need to say explicitly where we want it. */
17889 cp_token *token = cp_lexer_peek_token (parser->lexer);
17890 error ("%H%<>>%> should be %<> >%> "
17891 "within a nested template argument list",
17894 token->type = CPP_GREATER;
17898 /* If this is not a nested template argument list, the '>>'
17899 is a typo for '>'. Emit an error message and continue.
17900 Same deal about the token location, but here we can get it
17901 right by consuming the '>>' before issuing the diagnostic. */
17902 cp_token *token = cp_lexer_consume_token (parser->lexer);
17903 error ("%Hspurious %<>>%>, use %<>%> to terminate "
17904 "a template argument list", &token->location);
17908 cp_parser_skip_to_end_of_template_parameter_list (parser);
17909 /* The `>' token might be a greater-than operator again now. */
17910 parser->greater_than_is_operator_p
17911 = saved_greater_than_is_operator_p;
17912 /* Restore the SAVED_SCOPE. */
17913 parser->scope = saved_scope;
17914 parser->qualifying_scope = saved_qualifying_scope;
17915 parser->object_scope = saved_object_scope;
17916 skip_evaluation = saved_skip_evaluation;
17921 /* MEMBER_FUNCTION is a member function, or a friend. If default
17922 arguments, or the body of the function have not yet been parsed,
17926 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17928 /* If this member is a template, get the underlying
17930 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17931 member_function = DECL_TEMPLATE_RESULT (member_function);
17933 /* There should not be any class definitions in progress at this
17934 point; the bodies of members are only parsed outside of all class
17936 gcc_assert (parser->num_classes_being_defined == 0);
17937 /* While we're parsing the member functions we might encounter more
17938 classes. We want to handle them right away, but we don't want
17939 them getting mixed up with functions that are currently in the
17941 parser->unparsed_functions_queues
17942 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17944 /* Make sure that any template parameters are in scope. */
17945 maybe_begin_member_template_processing (member_function);
17947 /* If the body of the function has not yet been parsed, parse it
17949 if (DECL_PENDING_INLINE_P (member_function))
17951 tree function_scope;
17952 cp_token_cache *tokens;
17954 /* The function is no longer pending; we are processing it. */
17955 tokens = DECL_PENDING_INLINE_INFO (member_function);
17956 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17957 DECL_PENDING_INLINE_P (member_function) = 0;
17959 /* If this is a local class, enter the scope of the containing
17961 function_scope = current_function_decl;
17962 if (function_scope)
17963 push_function_context ();
17965 /* Push the body of the function onto the lexer stack. */
17966 cp_parser_push_lexer_for_tokens (parser, tokens);
17968 /* Let the front end know that we going to be defining this
17970 start_preparsed_function (member_function, NULL_TREE,
17971 SF_PRE_PARSED | SF_INCLASS_INLINE);
17973 /* Don't do access checking if it is a templated function. */
17974 if (processing_template_decl)
17975 push_deferring_access_checks (dk_no_check);
17977 /* Now, parse the body of the function. */
17978 cp_parser_function_definition_after_declarator (parser,
17979 /*inline_p=*/true);
17981 if (processing_template_decl)
17982 pop_deferring_access_checks ();
17984 /* Leave the scope of the containing function. */
17985 if (function_scope)
17986 pop_function_context ();
17987 cp_parser_pop_lexer (parser);
17990 /* Remove any template parameters from the symbol table. */
17991 maybe_end_member_template_processing ();
17993 /* Restore the queue. */
17994 parser->unparsed_functions_queues
17995 = TREE_CHAIN (parser->unparsed_functions_queues);
17998 /* If DECL contains any default args, remember it on the unparsed
17999 functions queue. */
18002 cp_parser_save_default_args (cp_parser* parser, tree decl)
18006 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18008 probe = TREE_CHAIN (probe))
18009 if (TREE_PURPOSE (probe))
18011 TREE_PURPOSE (parser->unparsed_functions_queues)
18012 = tree_cons (current_class_type, decl,
18013 TREE_PURPOSE (parser->unparsed_functions_queues));
18018 /* FN is a FUNCTION_DECL which may contains a parameter with an
18019 unparsed DEFAULT_ARG. Parse the default args now. This function
18020 assumes that the current scope is the scope in which the default
18021 argument should be processed. */
18024 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18026 bool saved_local_variables_forbidden_p;
18029 /* While we're parsing the default args, we might (due to the
18030 statement expression extension) encounter more classes. We want
18031 to handle them right away, but we don't want them getting mixed
18032 up with default args that are currently in the queue. */
18033 parser->unparsed_functions_queues
18034 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18036 /* Local variable names (and the `this' keyword) may not appear
18037 in a default argument. */
18038 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18039 parser->local_variables_forbidden_p = true;
18041 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18043 parm = TREE_CHAIN (parm))
18045 cp_token_cache *tokens;
18046 tree default_arg = TREE_PURPOSE (parm);
18048 VEC(tree,gc) *insts;
18055 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18056 /* This can happen for a friend declaration for a function
18057 already declared with default arguments. */
18060 /* Push the saved tokens for the default argument onto the parser's
18062 tokens = DEFARG_TOKENS (default_arg);
18063 cp_parser_push_lexer_for_tokens (parser, tokens);
18065 /* Parse the assignment-expression. */
18066 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
18068 if (!processing_template_decl)
18069 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18071 TREE_PURPOSE (parm) = parsed_arg;
18073 /* Update any instantiations we've already created. */
18074 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18075 VEC_iterate (tree, insts, ix, copy); ix++)
18076 TREE_PURPOSE (copy) = parsed_arg;
18078 /* If the token stream has not been completely used up, then
18079 there was extra junk after the end of the default
18081 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18082 cp_parser_error (parser, "expected %<,%>");
18084 /* Revert to the main lexer. */
18085 cp_parser_pop_lexer (parser);
18088 /* Make sure no default arg is missing. */
18089 check_default_args (fn);
18091 /* Restore the state of local_variables_forbidden_p. */
18092 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18094 /* Restore the queue. */
18095 parser->unparsed_functions_queues
18096 = TREE_CHAIN (parser->unparsed_functions_queues);
18099 /* Parse the operand of `sizeof' (or a similar operator). Returns
18100 either a TYPE or an expression, depending on the form of the
18101 input. The KEYWORD indicates which kind of expression we have
18105 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18107 tree expr = NULL_TREE;
18108 const char *saved_message;
18110 bool saved_integral_constant_expression_p;
18111 bool saved_non_integral_constant_expression_p;
18112 bool pack_expansion_p = false;
18114 /* Types cannot be defined in a `sizeof' expression. Save away the
18116 saved_message = parser->type_definition_forbidden_message;
18117 /* And create the new one. */
18118 tmp = concat ("types may not be defined in %<",
18119 IDENTIFIER_POINTER (ridpointers[keyword]),
18120 "%> expressions", NULL);
18121 parser->type_definition_forbidden_message = tmp;
18123 /* The restrictions on constant-expressions do not apply inside
18124 sizeof expressions. */
18125 saved_integral_constant_expression_p
18126 = parser->integral_constant_expression_p;
18127 saved_non_integral_constant_expression_p
18128 = parser->non_integral_constant_expression_p;
18129 parser->integral_constant_expression_p = false;
18131 /* If it's a `...', then we are computing the length of a parameter
18133 if (keyword == RID_SIZEOF
18134 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18136 /* Consume the `...'. */
18137 cp_lexer_consume_token (parser->lexer);
18138 maybe_warn_variadic_templates ();
18140 /* Note that this is an expansion. */
18141 pack_expansion_p = true;
18144 /* Do not actually evaluate the expression. */
18146 /* If it's a `(', then we might be looking at the type-id
18148 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18151 bool saved_in_type_id_in_expr_p;
18153 /* We can't be sure yet whether we're looking at a type-id or an
18155 cp_parser_parse_tentatively (parser);
18156 /* Consume the `('. */
18157 cp_lexer_consume_token (parser->lexer);
18158 /* Parse the type-id. */
18159 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18160 parser->in_type_id_in_expr_p = true;
18161 type = cp_parser_type_id (parser);
18162 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18163 /* Now, look for the trailing `)'. */
18164 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18165 /* If all went well, then we're done. */
18166 if (cp_parser_parse_definitely (parser))
18168 cp_decl_specifier_seq decl_specs;
18170 /* Build a trivial decl-specifier-seq. */
18171 clear_decl_specs (&decl_specs);
18172 decl_specs.type = type;
18174 /* Call grokdeclarator to figure out what type this is. */
18175 expr = grokdeclarator (NULL,
18179 /*attrlist=*/NULL);
18183 /* If the type-id production did not work out, then we must be
18184 looking at the unary-expression production. */
18186 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18189 if (pack_expansion_p)
18190 /* Build a pack expansion. */
18191 expr = make_pack_expansion (expr);
18193 /* Go back to evaluating expressions. */
18196 /* Free the message we created. */
18198 /* And restore the old one. */
18199 parser->type_definition_forbidden_message = saved_message;
18200 parser->integral_constant_expression_p
18201 = saved_integral_constant_expression_p;
18202 parser->non_integral_constant_expression_p
18203 = saved_non_integral_constant_expression_p;
18208 /* If the current declaration has no declarator, return true. */
18211 cp_parser_declares_only_class_p (cp_parser *parser)
18213 /* If the next token is a `;' or a `,' then there is no
18215 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18216 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18219 /* Update the DECL_SPECS to reflect the storage class indicated by
18223 cp_parser_set_storage_class (cp_parser *parser,
18224 cp_decl_specifier_seq *decl_specs,
18226 location_t location)
18228 cp_storage_class storage_class;
18230 if (parser->in_unbraced_linkage_specification_p)
18232 error ("%Hinvalid use of %qD in linkage specification",
18233 &location, ridpointers[keyword]);
18236 else if (decl_specs->storage_class != sc_none)
18238 decl_specs->conflicting_specifiers_p = true;
18242 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18243 && decl_specs->specs[(int) ds_thread])
18245 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18246 decl_specs->specs[(int) ds_thread] = 0;
18252 storage_class = sc_auto;
18255 storage_class = sc_register;
18258 storage_class = sc_static;
18261 storage_class = sc_extern;
18264 storage_class = sc_mutable;
18267 gcc_unreachable ();
18269 decl_specs->storage_class = storage_class;
18271 /* A storage class specifier cannot be applied alongside a typedef
18272 specifier. If there is a typedef specifier present then set
18273 conflicting_specifiers_p which will trigger an error later
18274 on in grokdeclarator. */
18275 if (decl_specs->specs[(int)ds_typedef])
18276 decl_specs->conflicting_specifiers_p = true;
18279 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18280 is true, the type is a user-defined type; otherwise it is a
18281 built-in type specified by a keyword. */
18284 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18286 location_t location,
18287 bool user_defined_p)
18289 decl_specs->any_specifiers_p = true;
18291 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18292 (with, for example, in "typedef int wchar_t;") we remember that
18293 this is what happened. In system headers, we ignore these
18294 declarations so that G++ can work with system headers that are not
18296 if (decl_specs->specs[(int) ds_typedef]
18298 && (type_spec == boolean_type_node
18299 || type_spec == char16_type_node
18300 || type_spec == char32_type_node
18301 || type_spec == wchar_type_node)
18302 && (decl_specs->type
18303 || decl_specs->specs[(int) ds_long]
18304 || decl_specs->specs[(int) ds_short]
18305 || decl_specs->specs[(int) ds_unsigned]
18306 || decl_specs->specs[(int) ds_signed]))
18308 decl_specs->redefined_builtin_type = type_spec;
18309 if (!decl_specs->type)
18311 decl_specs->type = type_spec;
18312 decl_specs->user_defined_type_p = false;
18313 decl_specs->type_location = location;
18316 else if (decl_specs->type)
18317 decl_specs->multiple_types_p = true;
18320 decl_specs->type = type_spec;
18321 decl_specs->user_defined_type_p = user_defined_p;
18322 decl_specs->redefined_builtin_type = NULL_TREE;
18323 decl_specs->type_location = location;
18327 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18328 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18331 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18333 return decl_specifiers->specs[(int) ds_friend] != 0;
18336 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18337 issue an error message indicating that TOKEN_DESC was expected.
18339 Returns the token consumed, if the token had the appropriate type.
18340 Otherwise, returns NULL. */
18343 cp_parser_require (cp_parser* parser,
18344 enum cpp_ttype type,
18345 const char* token_desc)
18347 if (cp_lexer_next_token_is (parser->lexer, type))
18348 return cp_lexer_consume_token (parser->lexer);
18351 /* Output the MESSAGE -- unless we're parsing tentatively. */
18352 if (!cp_parser_simulate_error (parser))
18354 char *message = concat ("expected ", token_desc, NULL);
18355 cp_parser_error (parser, message);
18362 /* An error message is produced if the next token is not '>'.
18363 All further tokens are skipped until the desired token is
18364 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18367 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18369 /* Current level of '< ... >'. */
18370 unsigned level = 0;
18371 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18372 unsigned nesting_depth = 0;
18374 /* Are we ready, yet? If not, issue error message. */
18375 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18378 /* Skip tokens until the desired token is found. */
18381 /* Peek at the next token. */
18382 switch (cp_lexer_peek_token (parser->lexer)->type)
18385 if (!nesting_depth)
18390 if (cxx_dialect == cxx98)
18391 /* C++0x views the `>>' operator as two `>' tokens, but
18394 else if (!nesting_depth && level-- == 0)
18396 /* We've hit a `>>' where the first `>' closes the
18397 template argument list, and the second `>' is
18398 spurious. Just consume the `>>' and stop; we've
18399 already produced at least one error. */
18400 cp_lexer_consume_token (parser->lexer);
18403 /* Fall through for C++0x, so we handle the second `>' in
18407 if (!nesting_depth && level-- == 0)
18409 /* We've reached the token we want, consume it and stop. */
18410 cp_lexer_consume_token (parser->lexer);
18415 case CPP_OPEN_PAREN:
18416 case CPP_OPEN_SQUARE:
18420 case CPP_CLOSE_PAREN:
18421 case CPP_CLOSE_SQUARE:
18422 if (nesting_depth-- == 0)
18427 case CPP_PRAGMA_EOL:
18428 case CPP_SEMICOLON:
18429 case CPP_OPEN_BRACE:
18430 case CPP_CLOSE_BRACE:
18431 /* The '>' was probably forgotten, don't look further. */
18438 /* Consume this token. */
18439 cp_lexer_consume_token (parser->lexer);
18443 /* If the next token is the indicated keyword, consume it. Otherwise,
18444 issue an error message indicating that TOKEN_DESC was expected.
18446 Returns the token consumed, if the token had the appropriate type.
18447 Otherwise, returns NULL. */
18450 cp_parser_require_keyword (cp_parser* parser,
18452 const char* token_desc)
18454 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18456 if (token && token->keyword != keyword)
18458 dyn_string_t error_msg;
18460 /* Format the error message. */
18461 error_msg = dyn_string_new (0);
18462 dyn_string_append_cstr (error_msg, "expected ");
18463 dyn_string_append_cstr (error_msg, token_desc);
18464 cp_parser_error (parser, error_msg->s);
18465 dyn_string_delete (error_msg);
18472 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18473 function-definition. */
18476 cp_parser_token_starts_function_definition_p (cp_token* token)
18478 return (/* An ordinary function-body begins with an `{'. */
18479 token->type == CPP_OPEN_BRACE
18480 /* A ctor-initializer begins with a `:'. */
18481 || token->type == CPP_COLON
18482 /* A function-try-block begins with `try'. */
18483 || token->keyword == RID_TRY
18484 /* The named return value extension begins with `return'. */
18485 || token->keyword == RID_RETURN);
18488 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18492 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18496 token = cp_lexer_peek_token (parser->lexer);
18497 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18500 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18501 C++0x) ending a template-argument. */
18504 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18508 token = cp_lexer_peek_token (parser->lexer);
18509 return (token->type == CPP_COMMA
18510 || token->type == CPP_GREATER
18511 || token->type == CPP_ELLIPSIS
18512 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18515 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18516 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18519 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18524 token = cp_lexer_peek_nth_token (parser->lexer, n);
18525 if (token->type == CPP_LESS)
18527 /* Check for the sequence `<::' in the original code. It would be lexed as
18528 `[:', where `[' is a digraph, and there is no whitespace before
18530 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18533 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18534 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18540 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18541 or none_type otherwise. */
18543 static enum tag_types
18544 cp_parser_token_is_class_key (cp_token* token)
18546 switch (token->keyword)
18551 return record_type;
18560 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18563 cp_parser_check_class_key (enum tag_types class_key, tree type)
18565 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18566 permerror (input_location, "%qs tag used in naming %q#T",
18567 class_key == union_type ? "union"
18568 : class_key == record_type ? "struct" : "class",
18572 /* Issue an error message if DECL is redeclared with different
18573 access than its original declaration [class.access.spec/3].
18574 This applies to nested classes and nested class templates.
18578 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18580 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18583 if ((TREE_PRIVATE (decl)
18584 != (current_access_specifier == access_private_node))
18585 || (TREE_PROTECTED (decl)
18586 != (current_access_specifier == access_protected_node)))
18587 error ("%H%qD redeclared with different access", &location, decl);
18590 /* Look for the `template' keyword, as a syntactic disambiguator.
18591 Return TRUE iff it is present, in which case it will be
18595 cp_parser_optional_template_keyword (cp_parser *parser)
18597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18599 /* The `template' keyword can only be used within templates;
18600 outside templates the parser can always figure out what is a
18601 template and what is not. */
18602 if (!processing_template_decl)
18604 cp_token *token = cp_lexer_peek_token (parser->lexer);
18605 error ("%H%<template%> (as a disambiguator) is only allowed "
18606 "within templates", &token->location);
18607 /* If this part of the token stream is rescanned, the same
18608 error message would be generated. So, we purge the token
18609 from the stream. */
18610 cp_lexer_purge_token (parser->lexer);
18615 /* Consume the `template' keyword. */
18616 cp_lexer_consume_token (parser->lexer);
18624 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18625 set PARSER->SCOPE, and perform other related actions. */
18628 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18631 struct tree_check *check_value;
18632 deferred_access_check *chk;
18633 VEC (deferred_access_check,gc) *checks;
18635 /* Get the stored value. */
18636 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18637 /* Perform any access checks that were deferred. */
18638 checks = check_value->checks;
18642 VEC_iterate (deferred_access_check, checks, i, chk) ;
18645 perform_or_defer_access_check (chk->binfo,
18650 /* Set the scope from the stored value. */
18651 parser->scope = check_value->value;
18652 parser->qualifying_scope = check_value->qualifying_scope;
18653 parser->object_scope = NULL_TREE;
18656 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18657 encounter the end of a block before what we were looking for. */
18660 cp_parser_cache_group (cp_parser *parser,
18661 enum cpp_ttype end,
18666 cp_token *token = cp_lexer_peek_token (parser->lexer);
18668 /* Abort a parenthesized expression if we encounter a semicolon. */
18669 if ((end == CPP_CLOSE_PAREN || depth == 0)
18670 && token->type == CPP_SEMICOLON)
18672 /* If we've reached the end of the file, stop. */
18673 if (token->type == CPP_EOF
18674 || (end != CPP_PRAGMA_EOL
18675 && token->type == CPP_PRAGMA_EOL))
18677 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18678 /* We've hit the end of an enclosing block, so there's been some
18679 kind of syntax error. */
18682 /* Consume the token. */
18683 cp_lexer_consume_token (parser->lexer);
18684 /* See if it starts a new group. */
18685 if (token->type == CPP_OPEN_BRACE)
18687 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18688 /* In theory this should probably check end == '}', but
18689 cp_parser_save_member_function_body needs it to exit
18690 after either '}' or ')' when called with ')'. */
18694 else if (token->type == CPP_OPEN_PAREN)
18696 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18697 if (depth == 0 && end == CPP_CLOSE_PAREN)
18700 else if (token->type == CPP_PRAGMA)
18701 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18702 else if (token->type == end)
18707 /* Begin parsing tentatively. We always save tokens while parsing
18708 tentatively so that if the tentative parsing fails we can restore the
18712 cp_parser_parse_tentatively (cp_parser* parser)
18714 /* Enter a new parsing context. */
18715 parser->context = cp_parser_context_new (parser->context);
18716 /* Begin saving tokens. */
18717 cp_lexer_save_tokens (parser->lexer);
18718 /* In order to avoid repetitive access control error messages,
18719 access checks are queued up until we are no longer parsing
18721 push_deferring_access_checks (dk_deferred);
18724 /* Commit to the currently active tentative parse. */
18727 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18729 cp_parser_context *context;
18732 /* Mark all of the levels as committed. */
18733 lexer = parser->lexer;
18734 for (context = parser->context; context->next; context = context->next)
18736 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18738 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18739 while (!cp_lexer_saving_tokens (lexer))
18740 lexer = lexer->next;
18741 cp_lexer_commit_tokens (lexer);
18745 /* Abort the currently active tentative parse. All consumed tokens
18746 will be rolled back, and no diagnostics will be issued. */
18749 cp_parser_abort_tentative_parse (cp_parser* parser)
18751 cp_parser_simulate_error (parser);
18752 /* Now, pretend that we want to see if the construct was
18753 successfully parsed. */
18754 cp_parser_parse_definitely (parser);
18757 /* Stop parsing tentatively. If a parse error has occurred, restore the
18758 token stream. Otherwise, commit to the tokens we have consumed.
18759 Returns true if no error occurred; false otherwise. */
18762 cp_parser_parse_definitely (cp_parser* parser)
18764 bool error_occurred;
18765 cp_parser_context *context;
18767 /* Remember whether or not an error occurred, since we are about to
18768 destroy that information. */
18769 error_occurred = cp_parser_error_occurred (parser);
18770 /* Remove the topmost context from the stack. */
18771 context = parser->context;
18772 parser->context = context->next;
18773 /* If no parse errors occurred, commit to the tentative parse. */
18774 if (!error_occurred)
18776 /* Commit to the tokens read tentatively, unless that was
18778 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18779 cp_lexer_commit_tokens (parser->lexer);
18781 pop_to_parent_deferring_access_checks ();
18783 /* Otherwise, if errors occurred, roll back our state so that things
18784 are just as they were before we began the tentative parse. */
18787 cp_lexer_rollback_tokens (parser->lexer);
18788 pop_deferring_access_checks ();
18790 /* Add the context to the front of the free list. */
18791 context->next = cp_parser_context_free_list;
18792 cp_parser_context_free_list = context;
18794 return !error_occurred;
18797 /* Returns true if we are parsing tentatively and are not committed to
18798 this tentative parse. */
18801 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18803 return (cp_parser_parsing_tentatively (parser)
18804 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18807 /* Returns nonzero iff an error has occurred during the most recent
18808 tentative parse. */
18811 cp_parser_error_occurred (cp_parser* parser)
18813 return (cp_parser_parsing_tentatively (parser)
18814 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18817 /* Returns nonzero if GNU extensions are allowed. */
18820 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18822 return parser->allow_gnu_extensions_p;
18825 /* Objective-C++ Productions */
18828 /* Parse an Objective-C expression, which feeds into a primary-expression
18832 objc-message-expression
18833 objc-string-literal
18834 objc-encode-expression
18835 objc-protocol-expression
18836 objc-selector-expression
18838 Returns a tree representation of the expression. */
18841 cp_parser_objc_expression (cp_parser* parser)
18843 /* Try to figure out what kind of declaration is present. */
18844 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18848 case CPP_OPEN_SQUARE:
18849 return cp_parser_objc_message_expression (parser);
18851 case CPP_OBJC_STRING:
18852 kwd = cp_lexer_consume_token (parser->lexer);
18853 return objc_build_string_object (kwd->u.value);
18856 switch (kwd->keyword)
18858 case RID_AT_ENCODE:
18859 return cp_parser_objc_encode_expression (parser);
18861 case RID_AT_PROTOCOL:
18862 return cp_parser_objc_protocol_expression (parser);
18864 case RID_AT_SELECTOR:
18865 return cp_parser_objc_selector_expression (parser);
18871 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18872 &kwd->location, kwd->u.value);
18873 cp_parser_skip_to_end_of_block_or_statement (parser);
18876 return error_mark_node;
18879 /* Parse an Objective-C message expression.
18881 objc-message-expression:
18882 [ objc-message-receiver objc-message-args ]
18884 Returns a representation of an Objective-C message. */
18887 cp_parser_objc_message_expression (cp_parser* parser)
18889 tree receiver, messageargs;
18891 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18892 receiver = cp_parser_objc_message_receiver (parser);
18893 messageargs = cp_parser_objc_message_args (parser);
18894 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18896 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18899 /* Parse an objc-message-receiver.
18901 objc-message-receiver:
18903 simple-type-specifier
18905 Returns a representation of the type or expression. */
18908 cp_parser_objc_message_receiver (cp_parser* parser)
18912 /* An Objective-C message receiver may be either (1) a type
18913 or (2) an expression. */
18914 cp_parser_parse_tentatively (parser);
18915 rcv = cp_parser_expression (parser, false);
18917 if (cp_parser_parse_definitely (parser))
18920 rcv = cp_parser_simple_type_specifier (parser,
18921 /*decl_specs=*/NULL,
18922 CP_PARSER_FLAGS_NONE);
18924 return objc_get_class_reference (rcv);
18927 /* Parse the arguments and selectors comprising an Objective-C message.
18932 objc-selector-args , objc-comma-args
18934 objc-selector-args:
18935 objc-selector [opt] : assignment-expression
18936 objc-selector-args objc-selector [opt] : assignment-expression
18939 assignment-expression
18940 objc-comma-args , assignment-expression
18942 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18943 selector arguments and TREE_VALUE containing a list of comma
18947 cp_parser_objc_message_args (cp_parser* parser)
18949 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18950 bool maybe_unary_selector_p = true;
18951 cp_token *token = cp_lexer_peek_token (parser->lexer);
18953 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18955 tree selector = NULL_TREE, arg;
18957 if (token->type != CPP_COLON)
18958 selector = cp_parser_objc_selector (parser);
18960 /* Detect if we have a unary selector. */
18961 if (maybe_unary_selector_p
18962 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18963 return build_tree_list (selector, NULL_TREE);
18965 maybe_unary_selector_p = false;
18966 cp_parser_require (parser, CPP_COLON, "%<:%>");
18967 arg = cp_parser_assignment_expression (parser, false);
18970 = chainon (sel_args,
18971 build_tree_list (selector, arg));
18973 token = cp_lexer_peek_token (parser->lexer);
18976 /* Handle non-selector arguments, if any. */
18977 while (token->type == CPP_COMMA)
18981 cp_lexer_consume_token (parser->lexer);
18982 arg = cp_parser_assignment_expression (parser, false);
18985 = chainon (addl_args,
18986 build_tree_list (NULL_TREE, arg));
18988 token = cp_lexer_peek_token (parser->lexer);
18991 return build_tree_list (sel_args, addl_args);
18994 /* Parse an Objective-C encode expression.
18996 objc-encode-expression:
18997 @encode objc-typename
18999 Returns an encoded representation of the type argument. */
19002 cp_parser_objc_encode_expression (cp_parser* parser)
19007 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19008 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19009 token = cp_lexer_peek_token (parser->lexer);
19010 type = complete_type (cp_parser_type_id (parser));
19011 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19015 error ("%H%<@encode%> must specify a type as an argument",
19017 return error_mark_node;
19020 return objc_build_encode_expr (type);
19023 /* Parse an Objective-C @defs expression. */
19026 cp_parser_objc_defs_expression (cp_parser *parser)
19030 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19031 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19032 name = cp_parser_identifier (parser);
19033 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19035 return objc_get_class_ivars (name);
19038 /* Parse an Objective-C protocol expression.
19040 objc-protocol-expression:
19041 @protocol ( identifier )
19043 Returns a representation of the protocol expression. */
19046 cp_parser_objc_protocol_expression (cp_parser* parser)
19050 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19051 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19052 proto = cp_parser_identifier (parser);
19053 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19055 return objc_build_protocol_expr (proto);
19058 /* Parse an Objective-C selector expression.
19060 objc-selector-expression:
19061 @selector ( objc-method-signature )
19063 objc-method-signature:
19069 objc-selector-seq objc-selector :
19071 Returns a representation of the method selector. */
19074 cp_parser_objc_selector_expression (cp_parser* parser)
19076 tree sel_seq = NULL_TREE;
19077 bool maybe_unary_selector_p = true;
19080 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19081 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19082 token = cp_lexer_peek_token (parser->lexer);
19084 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19085 || token->type == CPP_SCOPE)
19087 tree selector = NULL_TREE;
19089 if (token->type != CPP_COLON
19090 || token->type == CPP_SCOPE)
19091 selector = cp_parser_objc_selector (parser);
19093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19094 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19096 /* Detect if we have a unary selector. */
19097 if (maybe_unary_selector_p)
19099 sel_seq = selector;
19100 goto finish_selector;
19104 cp_parser_error (parser, "expected %<:%>");
19107 maybe_unary_selector_p = false;
19108 token = cp_lexer_consume_token (parser->lexer);
19110 if (token->type == CPP_SCOPE)
19113 = chainon (sel_seq,
19114 build_tree_list (selector, NULL_TREE));
19116 = chainon (sel_seq,
19117 build_tree_list (NULL_TREE, NULL_TREE));
19121 = chainon (sel_seq,
19122 build_tree_list (selector, NULL_TREE));
19124 token = cp_lexer_peek_token (parser->lexer);
19128 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19130 return objc_build_selector_expr (sel_seq);
19133 /* Parse a list of identifiers.
19135 objc-identifier-list:
19137 objc-identifier-list , identifier
19139 Returns a TREE_LIST of identifier nodes. */
19142 cp_parser_objc_identifier_list (cp_parser* parser)
19144 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19145 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19147 while (sep->type == CPP_COMMA)
19149 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19150 list = chainon (list,
19151 build_tree_list (NULL_TREE,
19152 cp_parser_identifier (parser)));
19153 sep = cp_lexer_peek_token (parser->lexer);
19159 /* Parse an Objective-C alias declaration.
19161 objc-alias-declaration:
19162 @compatibility_alias identifier identifier ;
19164 This function registers the alias mapping with the Objective-C front end.
19165 It returns nothing. */
19168 cp_parser_objc_alias_declaration (cp_parser* parser)
19172 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19173 alias = cp_parser_identifier (parser);
19174 orig = cp_parser_identifier (parser);
19175 objc_declare_alias (alias, orig);
19176 cp_parser_consume_semicolon_at_end_of_statement (parser);
19179 /* Parse an Objective-C class forward-declaration.
19181 objc-class-declaration:
19182 @class objc-identifier-list ;
19184 The function registers the forward declarations with the Objective-C
19185 front end. It returns nothing. */
19188 cp_parser_objc_class_declaration (cp_parser* parser)
19190 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19191 objc_declare_class (cp_parser_objc_identifier_list (parser));
19192 cp_parser_consume_semicolon_at_end_of_statement (parser);
19195 /* Parse a list of Objective-C protocol references.
19197 objc-protocol-refs-opt:
19198 objc-protocol-refs [opt]
19200 objc-protocol-refs:
19201 < objc-identifier-list >
19203 Returns a TREE_LIST of identifiers, if any. */
19206 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19208 tree protorefs = NULL_TREE;
19210 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19212 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19213 protorefs = cp_parser_objc_identifier_list (parser);
19214 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19220 /* Parse a Objective-C visibility specification. */
19223 cp_parser_objc_visibility_spec (cp_parser* parser)
19225 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19227 switch (vis->keyword)
19229 case RID_AT_PRIVATE:
19230 objc_set_visibility (2);
19232 case RID_AT_PROTECTED:
19233 objc_set_visibility (0);
19235 case RID_AT_PUBLIC:
19236 objc_set_visibility (1);
19242 /* Eat '@private'/'@protected'/'@public'. */
19243 cp_lexer_consume_token (parser->lexer);
19246 /* Parse an Objective-C method type. */
19249 cp_parser_objc_method_type (cp_parser* parser)
19251 objc_set_method_type
19252 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19257 /* Parse an Objective-C protocol qualifier. */
19260 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19262 tree quals = NULL_TREE, node;
19263 cp_token *token = cp_lexer_peek_token (parser->lexer);
19265 node = token->u.value;
19267 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19268 && (node == ridpointers [(int) RID_IN]
19269 || node == ridpointers [(int) RID_OUT]
19270 || node == ridpointers [(int) RID_INOUT]
19271 || node == ridpointers [(int) RID_BYCOPY]
19272 || node == ridpointers [(int) RID_BYREF]
19273 || node == ridpointers [(int) RID_ONEWAY]))
19275 quals = tree_cons (NULL_TREE, node, quals);
19276 cp_lexer_consume_token (parser->lexer);
19277 token = cp_lexer_peek_token (parser->lexer);
19278 node = token->u.value;
19284 /* Parse an Objective-C typename. */
19287 cp_parser_objc_typename (cp_parser* parser)
19289 tree type_name = NULL_TREE;
19291 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19293 tree proto_quals, cp_type = NULL_TREE;
19295 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19296 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19298 /* An ObjC type name may consist of just protocol qualifiers, in which
19299 case the type shall default to 'id'. */
19300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19301 cp_type = cp_parser_type_id (parser);
19303 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19304 type_name = build_tree_list (proto_quals, cp_type);
19310 /* Check to see if TYPE refers to an Objective-C selector name. */
19313 cp_parser_objc_selector_p (enum cpp_ttype type)
19315 return (type == CPP_NAME || type == CPP_KEYWORD
19316 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19317 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19318 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19319 || type == CPP_XOR || type == CPP_XOR_EQ);
19322 /* Parse an Objective-C selector. */
19325 cp_parser_objc_selector (cp_parser* parser)
19327 cp_token *token = cp_lexer_consume_token (parser->lexer);
19329 if (!cp_parser_objc_selector_p (token->type))
19331 error ("%Hinvalid Objective-C++ selector name", &token->location);
19332 return error_mark_node;
19335 /* C++ operator names are allowed to appear in ObjC selectors. */
19336 switch (token->type)
19338 case CPP_AND_AND: return get_identifier ("and");
19339 case CPP_AND_EQ: return get_identifier ("and_eq");
19340 case CPP_AND: return get_identifier ("bitand");
19341 case CPP_OR: return get_identifier ("bitor");
19342 case CPP_COMPL: return get_identifier ("compl");
19343 case CPP_NOT: return get_identifier ("not");
19344 case CPP_NOT_EQ: return get_identifier ("not_eq");
19345 case CPP_OR_OR: return get_identifier ("or");
19346 case CPP_OR_EQ: return get_identifier ("or_eq");
19347 case CPP_XOR: return get_identifier ("xor");
19348 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19349 default: return token->u.value;
19353 /* Parse an Objective-C params list. */
19356 cp_parser_objc_method_keyword_params (cp_parser* parser)
19358 tree params = NULL_TREE;
19359 bool maybe_unary_selector_p = true;
19360 cp_token *token = cp_lexer_peek_token (parser->lexer);
19362 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19364 tree selector = NULL_TREE, type_name, identifier;
19366 if (token->type != CPP_COLON)
19367 selector = cp_parser_objc_selector (parser);
19369 /* Detect if we have a unary selector. */
19370 if (maybe_unary_selector_p
19371 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19374 maybe_unary_selector_p = false;
19375 cp_parser_require (parser, CPP_COLON, "%<:%>");
19376 type_name = cp_parser_objc_typename (parser);
19377 identifier = cp_parser_identifier (parser);
19381 objc_build_keyword_decl (selector,
19385 token = cp_lexer_peek_token (parser->lexer);
19391 /* Parse the non-keyword Objective-C params. */
19394 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19396 tree params = make_node (TREE_LIST);
19397 cp_token *token = cp_lexer_peek_token (parser->lexer);
19398 *ellipsisp = false; /* Initially, assume no ellipsis. */
19400 while (token->type == CPP_COMMA)
19402 cp_parameter_declarator *parmdecl;
19405 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19406 token = cp_lexer_peek_token (parser->lexer);
19408 if (token->type == CPP_ELLIPSIS)
19410 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19415 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19416 parm = grokdeclarator (parmdecl->declarator,
19417 &parmdecl->decl_specifiers,
19418 PARM, /*initialized=*/0,
19419 /*attrlist=*/NULL);
19421 chainon (params, build_tree_list (NULL_TREE, parm));
19422 token = cp_lexer_peek_token (parser->lexer);
19428 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19431 cp_parser_objc_interstitial_code (cp_parser* parser)
19433 cp_token *token = cp_lexer_peek_token (parser->lexer);
19435 /* If the next token is `extern' and the following token is a string
19436 literal, then we have a linkage specification. */
19437 if (token->keyword == RID_EXTERN
19438 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19439 cp_parser_linkage_specification (parser);
19440 /* Handle #pragma, if any. */
19441 else if (token->type == CPP_PRAGMA)
19442 cp_parser_pragma (parser, pragma_external);
19443 /* Allow stray semicolons. */
19444 else if (token->type == CPP_SEMICOLON)
19445 cp_lexer_consume_token (parser->lexer);
19446 /* Finally, try to parse a block-declaration, or a function-definition. */
19448 cp_parser_block_declaration (parser, /*statement_p=*/false);
19451 /* Parse a method signature. */
19454 cp_parser_objc_method_signature (cp_parser* parser)
19456 tree rettype, kwdparms, optparms;
19457 bool ellipsis = false;
19459 cp_parser_objc_method_type (parser);
19460 rettype = cp_parser_objc_typename (parser);
19461 kwdparms = cp_parser_objc_method_keyword_params (parser);
19462 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19464 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19467 /* Pars an Objective-C method prototype list. */
19470 cp_parser_objc_method_prototype_list (cp_parser* parser)
19472 cp_token *token = cp_lexer_peek_token (parser->lexer);
19474 while (token->keyword != RID_AT_END)
19476 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19478 objc_add_method_declaration
19479 (cp_parser_objc_method_signature (parser));
19480 cp_parser_consume_semicolon_at_end_of_statement (parser);
19483 /* Allow for interspersed non-ObjC++ code. */
19484 cp_parser_objc_interstitial_code (parser);
19486 token = cp_lexer_peek_token (parser->lexer);
19489 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19490 objc_finish_interface ();
19493 /* Parse an Objective-C method definition list. */
19496 cp_parser_objc_method_definition_list (cp_parser* parser)
19498 cp_token *token = cp_lexer_peek_token (parser->lexer);
19500 while (token->keyword != RID_AT_END)
19504 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19506 push_deferring_access_checks (dk_deferred);
19507 objc_start_method_definition
19508 (cp_parser_objc_method_signature (parser));
19510 /* For historical reasons, we accept an optional semicolon. */
19511 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19512 cp_lexer_consume_token (parser->lexer);
19514 perform_deferred_access_checks ();
19515 stop_deferring_access_checks ();
19516 meth = cp_parser_function_definition_after_declarator (parser,
19518 pop_deferring_access_checks ();
19519 objc_finish_method_definition (meth);
19522 /* Allow for interspersed non-ObjC++ code. */
19523 cp_parser_objc_interstitial_code (parser);
19525 token = cp_lexer_peek_token (parser->lexer);
19528 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19529 objc_finish_implementation ();
19532 /* Parse Objective-C ivars. */
19535 cp_parser_objc_class_ivars (cp_parser* parser)
19537 cp_token *token = cp_lexer_peek_token (parser->lexer);
19539 if (token->type != CPP_OPEN_BRACE)
19540 return; /* No ivars specified. */
19542 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19543 token = cp_lexer_peek_token (parser->lexer);
19545 while (token->type != CPP_CLOSE_BRACE)
19547 cp_decl_specifier_seq declspecs;
19548 int decl_class_or_enum_p;
19549 tree prefix_attributes;
19551 cp_parser_objc_visibility_spec (parser);
19553 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19556 cp_parser_decl_specifier_seq (parser,
19557 CP_PARSER_FLAGS_OPTIONAL,
19559 &decl_class_or_enum_p);
19560 prefix_attributes = declspecs.attributes;
19561 declspecs.attributes = NULL_TREE;
19563 /* Keep going until we hit the `;' at the end of the
19565 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19567 tree width = NULL_TREE, attributes, first_attribute, decl;
19568 cp_declarator *declarator = NULL;
19569 int ctor_dtor_or_conv_p;
19571 /* Check for a (possibly unnamed) bitfield declaration. */
19572 token = cp_lexer_peek_token (parser->lexer);
19573 if (token->type == CPP_COLON)
19576 if (token->type == CPP_NAME
19577 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19580 /* Get the name of the bitfield. */
19581 declarator = make_id_declarator (NULL_TREE,
19582 cp_parser_identifier (parser),
19586 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19587 /* Get the width of the bitfield. */
19589 = cp_parser_constant_expression (parser,
19590 /*allow_non_constant=*/false,
19595 /* Parse the declarator. */
19597 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19598 &ctor_dtor_or_conv_p,
19599 /*parenthesized_p=*/NULL,
19600 /*member_p=*/false);
19603 /* Look for attributes that apply to the ivar. */
19604 attributes = cp_parser_attributes_opt (parser);
19605 /* Remember which attributes are prefix attributes and
19607 first_attribute = attributes;
19608 /* Combine the attributes. */
19609 attributes = chainon (prefix_attributes, attributes);
19612 /* Create the bitfield declaration. */
19613 decl = grokbitfield (declarator, &declspecs,
19617 decl = grokfield (declarator, &declspecs,
19618 NULL_TREE, /*init_const_expr_p=*/false,
19619 NULL_TREE, attributes);
19621 /* Add the instance variable. */
19622 objc_add_instance_variable (decl);
19624 /* Reset PREFIX_ATTRIBUTES. */
19625 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19626 attributes = TREE_CHAIN (attributes);
19628 TREE_CHAIN (attributes) = NULL_TREE;
19630 token = cp_lexer_peek_token (parser->lexer);
19632 if (token->type == CPP_COMMA)
19634 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19640 cp_parser_consume_semicolon_at_end_of_statement (parser);
19641 token = cp_lexer_peek_token (parser->lexer);
19644 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19645 /* For historical reasons, we accept an optional semicolon. */
19646 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19647 cp_lexer_consume_token (parser->lexer);
19650 /* Parse an Objective-C protocol declaration. */
19653 cp_parser_objc_protocol_declaration (cp_parser* parser)
19655 tree proto, protorefs;
19658 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19659 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19661 tok = cp_lexer_peek_token (parser->lexer);
19662 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19666 /* See if we have a forward declaration or a definition. */
19667 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19669 /* Try a forward declaration first. */
19670 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19672 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19674 cp_parser_consume_semicolon_at_end_of_statement (parser);
19677 /* Ok, we got a full-fledged definition (or at least should). */
19680 proto = cp_parser_identifier (parser);
19681 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19682 objc_start_protocol (proto, protorefs);
19683 cp_parser_objc_method_prototype_list (parser);
19687 /* Parse an Objective-C superclass or category. */
19690 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19693 cp_token *next = cp_lexer_peek_token (parser->lexer);
19695 *super = *categ = NULL_TREE;
19696 if (next->type == CPP_COLON)
19698 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19699 *super = cp_parser_identifier (parser);
19701 else if (next->type == CPP_OPEN_PAREN)
19703 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19704 *categ = cp_parser_identifier (parser);
19705 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19709 /* Parse an Objective-C class interface. */
19712 cp_parser_objc_class_interface (cp_parser* parser)
19714 tree name, super, categ, protos;
19716 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19717 name = cp_parser_identifier (parser);
19718 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19719 protos = cp_parser_objc_protocol_refs_opt (parser);
19721 /* We have either a class or a category on our hands. */
19723 objc_start_category_interface (name, categ, protos);
19726 objc_start_class_interface (name, super, protos);
19727 /* Handle instance variable declarations, if any. */
19728 cp_parser_objc_class_ivars (parser);
19729 objc_continue_interface ();
19732 cp_parser_objc_method_prototype_list (parser);
19735 /* Parse an Objective-C class implementation. */
19738 cp_parser_objc_class_implementation (cp_parser* parser)
19740 tree name, super, categ;
19742 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19743 name = cp_parser_identifier (parser);
19744 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19746 /* We have either a class or a category on our hands. */
19748 objc_start_category_implementation (name, categ);
19751 objc_start_class_implementation (name, super);
19752 /* Handle instance variable declarations, if any. */
19753 cp_parser_objc_class_ivars (parser);
19754 objc_continue_implementation ();
19757 cp_parser_objc_method_definition_list (parser);
19760 /* Consume the @end token and finish off the implementation. */
19763 cp_parser_objc_end_implementation (cp_parser* parser)
19765 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19766 objc_finish_implementation ();
19769 /* Parse an Objective-C declaration. */
19772 cp_parser_objc_declaration (cp_parser* parser)
19774 /* Try to figure out what kind of declaration is present. */
19775 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19777 switch (kwd->keyword)
19780 cp_parser_objc_alias_declaration (parser);
19783 cp_parser_objc_class_declaration (parser);
19785 case RID_AT_PROTOCOL:
19786 cp_parser_objc_protocol_declaration (parser);
19788 case RID_AT_INTERFACE:
19789 cp_parser_objc_class_interface (parser);
19791 case RID_AT_IMPLEMENTATION:
19792 cp_parser_objc_class_implementation (parser);
19795 cp_parser_objc_end_implementation (parser);
19798 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19799 &kwd->location, kwd->u.value);
19800 cp_parser_skip_to_end_of_block_or_statement (parser);
19804 /* Parse an Objective-C try-catch-finally statement.
19806 objc-try-catch-finally-stmt:
19807 @try compound-statement objc-catch-clause-seq [opt]
19808 objc-finally-clause [opt]
19810 objc-catch-clause-seq:
19811 objc-catch-clause objc-catch-clause-seq [opt]
19814 @catch ( exception-declaration ) compound-statement
19816 objc-finally-clause
19817 @finally compound-statement
19819 Returns NULL_TREE. */
19822 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19823 location_t location;
19826 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19827 location = cp_lexer_peek_token (parser->lexer)->location;
19828 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19829 node, lest it get absorbed into the surrounding block. */
19830 stmt = push_stmt_list ();
19831 cp_parser_compound_statement (parser, NULL, false);
19832 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19834 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19836 cp_parameter_declarator *parmdecl;
19839 cp_lexer_consume_token (parser->lexer);
19840 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19841 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19842 parm = grokdeclarator (parmdecl->declarator,
19843 &parmdecl->decl_specifiers,
19844 PARM, /*initialized=*/0,
19845 /*attrlist=*/NULL);
19846 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19847 objc_begin_catch_clause (parm);
19848 cp_parser_compound_statement (parser, NULL, false);
19849 objc_finish_catch_clause ();
19852 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19854 cp_lexer_consume_token (parser->lexer);
19855 location = cp_lexer_peek_token (parser->lexer)->location;
19856 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19857 node, lest it get absorbed into the surrounding block. */
19858 stmt = push_stmt_list ();
19859 cp_parser_compound_statement (parser, NULL, false);
19860 objc_build_finally_clause (location, pop_stmt_list (stmt));
19863 return objc_finish_try_stmt ();
19866 /* Parse an Objective-C synchronized statement.
19868 objc-synchronized-stmt:
19869 @synchronized ( expression ) compound-statement
19871 Returns NULL_TREE. */
19874 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19875 location_t location;
19878 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19880 location = cp_lexer_peek_token (parser->lexer)->location;
19881 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19882 lock = cp_parser_expression (parser, false);
19883 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19885 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19886 node, lest it get absorbed into the surrounding block. */
19887 stmt = push_stmt_list ();
19888 cp_parser_compound_statement (parser, NULL, false);
19890 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19893 /* Parse an Objective-C throw statement.
19896 @throw assignment-expression [opt] ;
19898 Returns a constructed '@throw' statement. */
19901 cp_parser_objc_throw_statement (cp_parser *parser) {
19902 tree expr = NULL_TREE;
19904 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19906 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19907 expr = cp_parser_assignment_expression (parser, false);
19909 cp_parser_consume_semicolon_at_end_of_statement (parser);
19911 return objc_build_throw_stmt (expr);
19914 /* Parse an Objective-C statement. */
19917 cp_parser_objc_statement (cp_parser * parser) {
19918 /* Try to figure out what kind of declaration is present. */
19919 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19921 switch (kwd->keyword)
19924 return cp_parser_objc_try_catch_finally_statement (parser);
19925 case RID_AT_SYNCHRONIZED:
19926 return cp_parser_objc_synchronized_statement (parser);
19928 return cp_parser_objc_throw_statement (parser);
19930 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19931 &kwd->location, kwd->u.value);
19932 cp_parser_skip_to_end_of_block_or_statement (parser);
19935 return error_mark_node;
19938 /* OpenMP 2.5 parsing routines. */
19940 /* Returns name of the next clause.
19941 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19942 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19943 returned and the token is consumed. */
19945 static pragma_omp_clause
19946 cp_parser_omp_clause_name (cp_parser *parser)
19948 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19950 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19951 result = PRAGMA_OMP_CLAUSE_IF;
19952 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19953 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19954 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19955 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19956 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19958 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19959 const char *p = IDENTIFIER_POINTER (id);
19964 if (!strcmp ("collapse", p))
19965 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19966 else if (!strcmp ("copyin", p))
19967 result = PRAGMA_OMP_CLAUSE_COPYIN;
19968 else if (!strcmp ("copyprivate", p))
19969 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19972 if (!strcmp ("firstprivate", p))
19973 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19976 if (!strcmp ("lastprivate", p))
19977 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19980 if (!strcmp ("nowait", p))
19981 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19982 else if (!strcmp ("num_threads", p))
19983 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19986 if (!strcmp ("ordered", p))
19987 result = PRAGMA_OMP_CLAUSE_ORDERED;
19990 if (!strcmp ("reduction", p))
19991 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19994 if (!strcmp ("schedule", p))
19995 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19996 else if (!strcmp ("shared", p))
19997 result = PRAGMA_OMP_CLAUSE_SHARED;
20000 if (!strcmp ("untied", p))
20001 result = PRAGMA_OMP_CLAUSE_UNTIED;
20006 if (result != PRAGMA_OMP_CLAUSE_NONE)
20007 cp_lexer_consume_token (parser->lexer);
20012 /* Validate that a clause of the given type does not already exist. */
20015 check_no_duplicate_clause (tree clauses, enum tree_code code,
20016 const char *name, location_t location)
20020 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20021 if (OMP_CLAUSE_CODE (c) == code)
20023 error ("%Htoo many %qs clauses", &location, name);
20031 variable-list , identifier
20033 In addition, we match a closing parenthesis. An opening parenthesis
20034 will have been consumed by the caller.
20036 If KIND is nonzero, create the appropriate node and install the decl
20037 in OMP_CLAUSE_DECL and add the node to the head of the list.
20039 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20040 return the list created. */
20043 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20051 token = cp_lexer_peek_token (parser->lexer);
20052 name = cp_parser_id_expression (parser, /*template_p=*/false,
20053 /*check_dependency_p=*/true,
20054 /*template_p=*/NULL,
20055 /*declarator_p=*/false,
20056 /*optional_p=*/false);
20057 if (name == error_mark_node)
20060 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20061 if (decl == error_mark_node)
20062 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20063 else if (kind != 0)
20065 tree u = build_omp_clause (kind);
20066 OMP_CLAUSE_DECL (u) = decl;
20067 OMP_CLAUSE_CHAIN (u) = list;
20071 list = tree_cons (decl, NULL_TREE, list);
20074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20076 cp_lexer_consume_token (parser->lexer);
20079 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20083 /* Try to resync to an unnested comma. Copied from
20084 cp_parser_parenthesized_expression_list. */
20086 ending = cp_parser_skip_to_closing_parenthesis (parser,
20087 /*recovering=*/true,
20089 /*consume_paren=*/true);
20097 /* Similarly, but expect leading and trailing parenthesis. This is a very
20098 common case for omp clauses. */
20101 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20103 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20104 return cp_parser_omp_var_list_no_open (parser, kind, list);
20109 collapse ( constant-expression ) */
20112 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20118 loc = cp_lexer_peek_token (parser->lexer)->location;
20119 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20122 num = cp_parser_constant_expression (parser, false, NULL);
20124 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20125 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20126 /*or_comma=*/false,
20127 /*consume_paren=*/true);
20129 if (num == error_mark_node)
20131 num = fold_non_dependent_expr (num);
20132 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20133 || !host_integerp (num, 0)
20134 || (n = tree_low_cst (num, 0)) <= 0
20137 error ("%Hcollapse argument needs positive constant integer expression",
20142 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20143 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20144 OMP_CLAUSE_CHAIN (c) = list;
20145 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20151 default ( shared | none ) */
20154 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20156 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20159 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20161 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20163 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20164 const char *p = IDENTIFIER_POINTER (id);
20169 if (strcmp ("none", p) != 0)
20171 kind = OMP_CLAUSE_DEFAULT_NONE;
20175 if (strcmp ("shared", p) != 0)
20177 kind = OMP_CLAUSE_DEFAULT_SHARED;
20184 cp_lexer_consume_token (parser->lexer);
20189 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20192 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20193 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20194 /*or_comma=*/false,
20195 /*consume_paren=*/true);
20197 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20200 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20201 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20202 OMP_CLAUSE_CHAIN (c) = list;
20203 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20209 if ( expression ) */
20212 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20216 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20219 t = cp_parser_condition (parser);
20221 if (t == error_mark_node
20222 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20223 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20224 /*or_comma=*/false,
20225 /*consume_paren=*/true);
20227 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20229 c = build_omp_clause (OMP_CLAUSE_IF);
20230 OMP_CLAUSE_IF_EXPR (c) = t;
20231 OMP_CLAUSE_CHAIN (c) = list;
20240 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20241 tree list, location_t location)
20245 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20247 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20248 OMP_CLAUSE_CHAIN (c) = list;
20253 num_threads ( expression ) */
20256 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20257 location_t location)
20261 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20264 t = cp_parser_expression (parser, false);
20266 if (t == error_mark_node
20267 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20268 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20269 /*or_comma=*/false,
20270 /*consume_paren=*/true);
20272 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20273 "num_threads", location);
20275 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20276 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20277 OMP_CLAUSE_CHAIN (c) = list;
20286 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20287 tree list, location_t location)
20291 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20292 "ordered", location);
20294 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20295 OMP_CLAUSE_CHAIN (c) = list;
20300 reduction ( reduction-operator : variable-list )
20302 reduction-operator:
20303 One of: + * - & ^ | && || */
20306 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20308 enum tree_code code;
20311 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20314 switch (cp_lexer_peek_token (parser->lexer)->type)
20326 code = BIT_AND_EXPR;
20329 code = BIT_XOR_EXPR;
20332 code = BIT_IOR_EXPR;
20335 code = TRUTH_ANDIF_EXPR;
20338 code = TRUTH_ORIF_EXPR;
20341 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20342 "%<|%>, %<&&%>, or %<||%>");
20344 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20345 /*or_comma=*/false,
20346 /*consume_paren=*/true);
20349 cp_lexer_consume_token (parser->lexer);
20351 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20354 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20355 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20356 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20362 schedule ( schedule-kind )
20363 schedule ( schedule-kind , expression )
20366 static | dynamic | guided | runtime | auto */
20369 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20373 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20376 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20378 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20380 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20381 const char *p = IDENTIFIER_POINTER (id);
20386 if (strcmp ("dynamic", p) != 0)
20388 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20392 if (strcmp ("guided", p) != 0)
20394 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20398 if (strcmp ("runtime", p) != 0)
20400 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20407 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20408 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20409 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20410 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20413 cp_lexer_consume_token (parser->lexer);
20415 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20418 cp_lexer_consume_token (parser->lexer);
20420 token = cp_lexer_peek_token (parser->lexer);
20421 t = cp_parser_assignment_expression (parser, false);
20423 if (t == error_mark_node)
20425 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20426 error ("%Hschedule %<runtime%> does not take "
20427 "a %<chunk_size%> parameter", &token->location);
20428 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20429 error ("%Hschedule %<auto%> does not take "
20430 "a %<chunk_size%> parameter", &token->location);
20432 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20434 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20437 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20440 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20441 OMP_CLAUSE_CHAIN (c) = list;
20445 cp_parser_error (parser, "invalid schedule kind");
20447 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20448 /*or_comma=*/false,
20449 /*consume_paren=*/true);
20457 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20458 tree list, location_t location)
20462 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20464 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20465 OMP_CLAUSE_CHAIN (c) = list;
20469 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20470 is a bitmask in MASK. Return the list of clauses found; the result
20471 of clause default goes in *pdefault. */
20474 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20475 const char *where, cp_token *pragma_tok)
20477 tree clauses = NULL;
20479 cp_token *token = NULL;
20481 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20483 pragma_omp_clause c_kind;
20484 const char *c_name;
20485 tree prev = clauses;
20487 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20488 cp_lexer_consume_token (parser->lexer);
20490 token = cp_lexer_peek_token (parser->lexer);
20491 c_kind = cp_parser_omp_clause_name (parser);
20496 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20497 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20499 c_name = "collapse";
20501 case PRAGMA_OMP_CLAUSE_COPYIN:
20502 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20505 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20506 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20508 c_name = "copyprivate";
20510 case PRAGMA_OMP_CLAUSE_DEFAULT:
20511 clauses = cp_parser_omp_clause_default (parser, clauses,
20513 c_name = "default";
20515 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20516 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20518 c_name = "firstprivate";
20520 case PRAGMA_OMP_CLAUSE_IF:
20521 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20524 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20525 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20527 c_name = "lastprivate";
20529 case PRAGMA_OMP_CLAUSE_NOWAIT:
20530 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20533 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20534 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20536 c_name = "num_threads";
20538 case PRAGMA_OMP_CLAUSE_ORDERED:
20539 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20541 c_name = "ordered";
20543 case PRAGMA_OMP_CLAUSE_PRIVATE:
20544 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20546 c_name = "private";
20548 case PRAGMA_OMP_CLAUSE_REDUCTION:
20549 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20550 c_name = "reduction";
20552 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20553 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20555 c_name = "schedule";
20557 case PRAGMA_OMP_CLAUSE_SHARED:
20558 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20562 case PRAGMA_OMP_CLAUSE_UNTIED:
20563 clauses = cp_parser_omp_clause_untied (parser, clauses,
20568 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20572 if (((mask >> c_kind) & 1) == 0)
20574 /* Remove the invalid clause(s) from the list to avoid
20575 confusing the rest of the compiler. */
20577 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20581 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20582 return finish_omp_clauses (clauses);
20589 In practice, we're also interested in adding the statement to an
20590 outer node. So it is convenient if we work around the fact that
20591 cp_parser_statement calls add_stmt. */
20594 cp_parser_begin_omp_structured_block (cp_parser *parser)
20596 unsigned save = parser->in_statement;
20598 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20599 This preserves the "not within loop or switch" style error messages
20600 for nonsense cases like
20606 if (parser->in_statement)
20607 parser->in_statement = IN_OMP_BLOCK;
20613 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20615 parser->in_statement = save;
20619 cp_parser_omp_structured_block (cp_parser *parser)
20621 tree stmt = begin_omp_structured_block ();
20622 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20624 cp_parser_statement (parser, NULL_TREE, false, NULL);
20626 cp_parser_end_omp_structured_block (parser, save);
20627 return finish_omp_structured_block (stmt);
20631 # pragma omp atomic new-line
20635 x binop= expr | x++ | ++x | x-- | --x
20637 +, *, -, /, &, ^, |, <<, >>
20639 where x is an lvalue expression with scalar type. */
20642 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20645 enum tree_code code;
20647 cp_parser_require_pragma_eol (parser, pragma_tok);
20649 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20651 switch (TREE_CODE (lhs))
20656 case PREINCREMENT_EXPR:
20657 case POSTINCREMENT_EXPR:
20658 lhs = TREE_OPERAND (lhs, 0);
20660 rhs = integer_one_node;
20663 case PREDECREMENT_EXPR:
20664 case POSTDECREMENT_EXPR:
20665 lhs = TREE_OPERAND (lhs, 0);
20667 rhs = integer_one_node;
20671 switch (cp_lexer_peek_token (parser->lexer)->type)
20677 code = TRUNC_DIV_EXPR;
20685 case CPP_LSHIFT_EQ:
20686 code = LSHIFT_EXPR;
20688 case CPP_RSHIFT_EQ:
20689 code = RSHIFT_EXPR;
20692 code = BIT_AND_EXPR;
20695 code = BIT_IOR_EXPR;
20698 code = BIT_XOR_EXPR;
20701 cp_parser_error (parser,
20702 "invalid operator for %<#pragma omp atomic%>");
20705 cp_lexer_consume_token (parser->lexer);
20707 rhs = cp_parser_expression (parser, false);
20708 if (rhs == error_mark_node)
20712 finish_omp_atomic (code, lhs, rhs);
20713 cp_parser_consume_semicolon_at_end_of_statement (parser);
20717 cp_parser_skip_to_end_of_block_or_statement (parser);
20722 # pragma omp barrier new-line */
20725 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20727 cp_parser_require_pragma_eol (parser, pragma_tok);
20728 finish_omp_barrier ();
20732 # pragma omp critical [(name)] new-line
20733 structured-block */
20736 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20738 tree stmt, name = NULL;
20740 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20742 cp_lexer_consume_token (parser->lexer);
20744 name = cp_parser_identifier (parser);
20746 if (name == error_mark_node
20747 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20748 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20749 /*or_comma=*/false,
20750 /*consume_paren=*/true);
20751 if (name == error_mark_node)
20754 cp_parser_require_pragma_eol (parser, pragma_tok);
20756 stmt = cp_parser_omp_structured_block (parser);
20757 return c_finish_omp_critical (stmt, name);
20761 # pragma omp flush flush-vars[opt] new-line
20764 ( variable-list ) */
20767 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20769 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20770 (void) cp_parser_omp_var_list (parser, 0, NULL);
20771 cp_parser_require_pragma_eol (parser, pragma_tok);
20773 finish_omp_flush ();
20776 /* Helper function, to parse omp for increment expression. */
20779 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20781 tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20787 cp_parser_skip_to_end_of_statement (parser);
20788 return error_mark_node;
20791 token = cp_lexer_peek_token (parser->lexer);
20792 op = binops_by_token [token->type].tree_type;
20801 cp_parser_skip_to_end_of_statement (parser);
20802 return error_mark_node;
20805 cp_lexer_consume_token (parser->lexer);
20806 rhs = cp_parser_binary_expression (parser, false,
20807 PREC_RELATIONAL_EXPRESSION);
20808 if (rhs == error_mark_node
20809 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20811 cp_parser_skip_to_end_of_statement (parser);
20812 return error_mark_node;
20815 return build2 (op, boolean_type_node, lhs, rhs);
20818 /* Helper function, to parse omp for increment expression. */
20821 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20823 cp_token *token = cp_lexer_peek_token (parser->lexer);
20829 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20831 op = (token->type == CPP_PLUS_PLUS
20832 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20833 cp_lexer_consume_token (parser->lexer);
20834 lhs = cp_parser_cast_expression (parser, false, false);
20836 return error_mark_node;
20837 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20840 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20842 return error_mark_node;
20844 token = cp_lexer_peek_token (parser->lexer);
20845 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20847 op = (token->type == CPP_PLUS_PLUS
20848 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20849 cp_lexer_consume_token (parser->lexer);
20850 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20853 op = cp_parser_assignment_operator_opt (parser);
20854 if (op == ERROR_MARK)
20855 return error_mark_node;
20857 if (op != NOP_EXPR)
20859 rhs = cp_parser_assignment_expression (parser, false);
20860 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20861 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20864 lhs = cp_parser_binary_expression (parser, false,
20865 PREC_ADDITIVE_EXPRESSION);
20866 token = cp_lexer_peek_token (parser->lexer);
20867 decl_first = lhs == decl;
20870 if (token->type != CPP_PLUS
20871 && token->type != CPP_MINUS)
20872 return error_mark_node;
20876 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20877 cp_lexer_consume_token (parser->lexer);
20878 rhs = cp_parser_binary_expression (parser, false,
20879 PREC_ADDITIVE_EXPRESSION);
20880 token = cp_lexer_peek_token (parser->lexer);
20881 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20883 if (lhs == NULL_TREE)
20885 if (op == PLUS_EXPR)
20888 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20891 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20892 NULL, tf_warning_or_error);
20895 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20899 if (rhs != decl || op == MINUS_EXPR)
20900 return error_mark_node;
20901 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20904 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20906 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20909 /* Parse the restricted form of the for statement allowed by OpenMP. */
20912 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20914 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20915 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20916 tree this_pre_body, cl;
20917 location_t loc_first;
20918 bool collapse_err = false;
20919 int i, collapse = 1, nbraces = 0;
20921 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20922 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20923 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20925 gcc_assert (collapse >= 1);
20927 declv = make_tree_vec (collapse);
20928 initv = make_tree_vec (collapse);
20929 condv = make_tree_vec (collapse);
20930 incrv = make_tree_vec (collapse);
20932 loc_first = cp_lexer_peek_token (parser->lexer)->location;
20934 for (i = 0; i < collapse; i++)
20936 int bracecount = 0;
20937 bool add_private_clause = false;
20940 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20942 cp_parser_error (parser, "for statement expected");
20945 loc = cp_lexer_consume_token (parser->lexer)->location;
20947 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20950 init = decl = real_decl = NULL;
20951 this_pre_body = push_stmt_list ();
20952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20954 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
20958 integer-type var = lb
20959 random-access-iterator-type var = lb
20960 pointer-type var = lb
20962 cp_decl_specifier_seq type_specifiers;
20964 /* First, try to parse as an initialized declaration. See
20965 cp_parser_condition, from whence the bulk of this is copied. */
20967 cp_parser_parse_tentatively (parser);
20968 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20970 if (cp_parser_parse_definitely (parser))
20972 /* If parsing a type specifier seq succeeded, then this
20973 MUST be a initialized declaration. */
20974 tree asm_specification, attributes;
20975 cp_declarator *declarator;
20977 declarator = cp_parser_declarator (parser,
20978 CP_PARSER_DECLARATOR_NAMED,
20979 /*ctor_dtor_or_conv_p=*/NULL,
20980 /*parenthesized_p=*/NULL,
20981 /*member_p=*/false);
20982 attributes = cp_parser_attributes_opt (parser);
20983 asm_specification = cp_parser_asm_specification_opt (parser);
20985 if (declarator == cp_error_declarator)
20986 cp_parser_skip_to_end_of_statement (parser);
20992 decl = start_decl (declarator, &type_specifiers,
20993 /*initialized_p=*/false, attributes,
20994 /*prefix_attributes=*/NULL_TREE,
20997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
20999 if (cp_lexer_next_token_is (parser->lexer,
21001 error ("parenthesized initialization is not allowed in "
21002 "OpenMP %<for%> loop");
21004 /* Trigger an error. */
21005 cp_parser_require (parser, CPP_EQ, "%<=%>");
21007 init = error_mark_node;
21008 cp_parser_skip_to_end_of_statement (parser);
21010 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21011 || type_dependent_expression_p (decl))
21013 bool is_direct_init, is_non_constant_init;
21015 init = cp_parser_initializer (parser,
21017 &is_non_constant_init);
21019 cp_finish_decl (decl, init, !is_non_constant_init,
21021 LOOKUP_ONLYCONVERTING);
21022 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21025 = tree_cons (NULL, this_pre_body, for_block);
21029 init = pop_stmt_list (this_pre_body);
21030 this_pre_body = NULL_TREE;
21035 cp_lexer_consume_token (parser->lexer);
21036 init = cp_parser_assignment_expression (parser, false);
21038 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21039 init = error_mark_node;
21041 cp_finish_decl (decl, NULL_TREE,
21042 /*init_const_expr_p=*/false,
21044 LOOKUP_ONLYCONVERTING);
21048 pop_scope (pushed_scope);
21054 /* If parsing a type specifier sequence failed, then
21055 this MUST be a simple expression. */
21056 cp_parser_parse_tentatively (parser);
21057 decl = cp_parser_primary_expression (parser, false, false,
21059 if (!cp_parser_error_occurred (parser)
21062 && CLASS_TYPE_P (TREE_TYPE (decl)))
21066 cp_parser_parse_definitely (parser);
21067 cp_parser_require (parser, CPP_EQ, "%<=%>");
21068 rhs = cp_parser_assignment_expression (parser, false);
21069 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21071 tf_warning_or_error));
21072 add_private_clause = true;
21077 cp_parser_abort_tentative_parse (parser);
21078 init = cp_parser_expression (parser, false);
21081 if (TREE_CODE (init) == MODIFY_EXPR
21082 || TREE_CODE (init) == MODOP_EXPR)
21083 real_decl = TREE_OPERAND (init, 0);
21088 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21091 this_pre_body = pop_stmt_list (this_pre_body);
21095 pre_body = push_stmt_list ();
21097 add_stmt (this_pre_body);
21098 pre_body = pop_stmt_list (pre_body);
21101 pre_body = this_pre_body;
21106 if (par_clauses != NULL && real_decl != NULL_TREE)
21109 for (c = par_clauses; *c ; )
21110 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21111 && OMP_CLAUSE_DECL (*c) == real_decl)
21113 error ("%Hiteration variable %qD should not be firstprivate",
21115 *c = OMP_CLAUSE_CHAIN (*c);
21117 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21118 && OMP_CLAUSE_DECL (*c) == real_decl)
21120 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21121 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21122 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21123 OMP_CLAUSE_DECL (l) = real_decl;
21124 OMP_CLAUSE_CHAIN (l) = clauses;
21125 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21127 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21128 CP_OMP_CLAUSE_INFO (*c) = NULL;
21129 add_private_clause = false;
21133 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21134 && OMP_CLAUSE_DECL (*c) == real_decl)
21135 add_private_clause = false;
21136 c = &OMP_CLAUSE_CHAIN (*c);
21140 if (add_private_clause)
21143 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21145 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21146 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21147 && OMP_CLAUSE_DECL (c) == decl)
21149 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21150 && OMP_CLAUSE_DECL (c) == decl)
21151 error ("%Hiteration variable %qD should not be firstprivate",
21153 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21154 && OMP_CLAUSE_DECL (c) == decl)
21155 error ("%Hiteration variable %qD should not be reduction",
21160 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21161 OMP_CLAUSE_DECL (c) = decl;
21162 c = finish_omp_clauses (c);
21165 OMP_CLAUSE_CHAIN (c) = clauses;
21172 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21174 /* If decl is an iterator, preserve LHS and RHS of the relational
21175 expr until finish_omp_for. */
21177 && (type_dependent_expression_p (decl)
21178 || CLASS_TYPE_P (TREE_TYPE (decl))))
21179 cond = cp_parser_omp_for_cond (parser, decl);
21181 cond = cp_parser_condition (parser);
21183 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21188 /* If decl is an iterator, preserve the operator on decl
21189 until finish_omp_for. */
21191 && (type_dependent_expression_p (decl)
21192 || CLASS_TYPE_P (TREE_TYPE (decl))))
21193 incr = cp_parser_omp_for_incr (parser, decl);
21195 incr = cp_parser_expression (parser, false);
21198 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21199 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21200 /*or_comma=*/false,
21201 /*consume_paren=*/true);
21203 TREE_VEC_ELT (declv, i) = decl;
21204 TREE_VEC_ELT (initv, i) = init;
21205 TREE_VEC_ELT (condv, i) = cond;
21206 TREE_VEC_ELT (incrv, i) = incr;
21208 if (i == collapse - 1)
21211 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21212 in between the collapsed for loops to be still considered perfectly
21213 nested. Hopefully the final version clarifies this.
21214 For now handle (multiple) {'s and empty statements. */
21215 cp_parser_parse_tentatively (parser);
21218 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21220 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21222 cp_lexer_consume_token (parser->lexer);
21225 else if (bracecount
21226 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21227 cp_lexer_consume_token (parser->lexer);
21230 loc = cp_lexer_peek_token (parser->lexer)->location;
21231 error ("%Hnot enough collapsed for loops", &loc);
21232 collapse_err = true;
21233 cp_parser_abort_tentative_parse (parser);
21242 cp_parser_parse_definitely (parser);
21243 nbraces += bracecount;
21247 /* Note that we saved the original contents of this flag when we entered
21248 the structured block, and so we don't need to re-save it here. */
21249 parser->in_statement = IN_OMP_FOR;
21251 /* Note that the grammar doesn't call for a structured block here,
21252 though the loop as a whole is a structured block. */
21253 body = push_stmt_list ();
21254 cp_parser_statement (parser, NULL_TREE, false, NULL);
21255 body = pop_stmt_list (body);
21257 if (declv == NULL_TREE)
21260 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21261 pre_body, clauses);
21265 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21267 cp_lexer_consume_token (parser->lexer);
21270 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21271 cp_lexer_consume_token (parser->lexer);
21276 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21277 error ("%Hcollapsed loops not perfectly nested", &loc);
21279 collapse_err = true;
21280 cp_parser_statement_seq_opt (parser, NULL);
21281 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21287 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21288 for_block = TREE_CHAIN (for_block);
21295 #pragma omp for for-clause[optseq] new-line
21298 #define OMP_FOR_CLAUSE_MASK \
21299 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21300 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21301 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21302 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21303 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21304 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21305 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21306 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21309 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21311 tree clauses, sb, ret;
21314 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21315 "#pragma omp for", pragma_tok);
21317 sb = begin_omp_structured_block ();
21318 save = cp_parser_begin_omp_structured_block (parser);
21320 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21322 cp_parser_end_omp_structured_block (parser, save);
21323 add_stmt (finish_omp_structured_block (sb));
21329 # pragma omp master new-line
21330 structured-block */
21333 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21335 cp_parser_require_pragma_eol (parser, pragma_tok);
21336 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21340 # pragma omp ordered new-line
21341 structured-block */
21344 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21346 cp_parser_require_pragma_eol (parser, pragma_tok);
21347 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21353 { section-sequence }
21356 section-directive[opt] structured-block
21357 section-sequence section-directive structured-block */
21360 cp_parser_omp_sections_scope (cp_parser *parser)
21362 tree stmt, substmt;
21363 bool error_suppress = false;
21366 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21369 stmt = push_stmt_list ();
21371 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21375 substmt = begin_omp_structured_block ();
21376 save = cp_parser_begin_omp_structured_block (parser);
21380 cp_parser_statement (parser, NULL_TREE, false, NULL);
21382 tok = cp_lexer_peek_token (parser->lexer);
21383 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21385 if (tok->type == CPP_CLOSE_BRACE)
21387 if (tok->type == CPP_EOF)
21391 cp_parser_end_omp_structured_block (parser, save);
21392 substmt = finish_omp_structured_block (substmt);
21393 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21394 add_stmt (substmt);
21399 tok = cp_lexer_peek_token (parser->lexer);
21400 if (tok->type == CPP_CLOSE_BRACE)
21402 if (tok->type == CPP_EOF)
21405 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21407 cp_lexer_consume_token (parser->lexer);
21408 cp_parser_require_pragma_eol (parser, tok);
21409 error_suppress = false;
21411 else if (!error_suppress)
21413 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21414 error_suppress = true;
21417 substmt = cp_parser_omp_structured_block (parser);
21418 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21419 add_stmt (substmt);
21421 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21423 substmt = pop_stmt_list (stmt);
21425 stmt = make_node (OMP_SECTIONS);
21426 TREE_TYPE (stmt) = void_type_node;
21427 OMP_SECTIONS_BODY (stmt) = substmt;
21434 # pragma omp sections sections-clause[optseq] newline
21437 #define OMP_SECTIONS_CLAUSE_MASK \
21438 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21439 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21440 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21441 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21442 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21445 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21449 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21450 "#pragma omp sections", pragma_tok);
21452 ret = cp_parser_omp_sections_scope (parser);
21454 OMP_SECTIONS_CLAUSES (ret) = clauses;
21460 # pragma parallel parallel-clause new-line
21461 # pragma parallel for parallel-for-clause new-line
21462 # pragma parallel sections parallel-sections-clause new-line */
21464 #define OMP_PARALLEL_CLAUSE_MASK \
21465 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21466 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21467 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21468 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21469 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21470 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21471 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21472 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21475 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21477 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21478 const char *p_name = "#pragma omp parallel";
21479 tree stmt, clauses, par_clause, ws_clause, block;
21480 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21483 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21485 cp_lexer_consume_token (parser->lexer);
21486 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21487 p_name = "#pragma omp parallel for";
21488 mask |= OMP_FOR_CLAUSE_MASK;
21489 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21491 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21493 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21494 const char *p = IDENTIFIER_POINTER (id);
21495 if (strcmp (p, "sections") == 0)
21497 cp_lexer_consume_token (parser->lexer);
21498 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21499 p_name = "#pragma omp parallel sections";
21500 mask |= OMP_SECTIONS_CLAUSE_MASK;
21501 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21505 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21506 block = begin_omp_parallel ();
21507 save = cp_parser_begin_omp_structured_block (parser);
21511 case PRAGMA_OMP_PARALLEL:
21512 cp_parser_statement (parser, NULL_TREE, false, NULL);
21513 par_clause = clauses;
21516 case PRAGMA_OMP_PARALLEL_FOR:
21517 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21518 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21521 case PRAGMA_OMP_PARALLEL_SECTIONS:
21522 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21523 stmt = cp_parser_omp_sections_scope (parser);
21525 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21529 gcc_unreachable ();
21532 cp_parser_end_omp_structured_block (parser, save);
21533 stmt = finish_omp_parallel (par_clause, block);
21534 if (p_kind != PRAGMA_OMP_PARALLEL)
21535 OMP_PARALLEL_COMBINED (stmt) = 1;
21540 # pragma omp single single-clause[optseq] new-line
21541 structured-block */
21543 #define OMP_SINGLE_CLAUSE_MASK \
21544 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21545 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21546 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21547 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21550 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21552 tree stmt = make_node (OMP_SINGLE);
21553 TREE_TYPE (stmt) = void_type_node;
21555 OMP_SINGLE_CLAUSES (stmt)
21556 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21557 "#pragma omp single", pragma_tok);
21558 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21560 return add_stmt (stmt);
21564 # pragma omp task task-clause[optseq] new-line
21565 structured-block */
21567 #define OMP_TASK_CLAUSE_MASK \
21568 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21569 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21570 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21571 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21572 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21573 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21576 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21578 tree clauses, block;
21581 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21582 "#pragma omp task", pragma_tok);
21583 block = begin_omp_task ();
21584 save = cp_parser_begin_omp_structured_block (parser);
21585 cp_parser_statement (parser, NULL_TREE, false, NULL);
21586 cp_parser_end_omp_structured_block (parser, save);
21587 return finish_omp_task (clauses, block);
21591 # pragma omp taskwait new-line */
21594 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21596 cp_parser_require_pragma_eol (parser, pragma_tok);
21597 finish_omp_taskwait ();
21601 # pragma omp threadprivate (variable-list) */
21604 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21608 vars = cp_parser_omp_var_list (parser, 0, NULL);
21609 cp_parser_require_pragma_eol (parser, pragma_tok);
21611 finish_omp_threadprivate (vars);
21614 /* Main entry point to OpenMP statement pragmas. */
21617 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21621 switch (pragma_tok->pragma_kind)
21623 case PRAGMA_OMP_ATOMIC:
21624 cp_parser_omp_atomic (parser, pragma_tok);
21626 case PRAGMA_OMP_CRITICAL:
21627 stmt = cp_parser_omp_critical (parser, pragma_tok);
21629 case PRAGMA_OMP_FOR:
21630 stmt = cp_parser_omp_for (parser, pragma_tok);
21632 case PRAGMA_OMP_MASTER:
21633 stmt = cp_parser_omp_master (parser, pragma_tok);
21635 case PRAGMA_OMP_ORDERED:
21636 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21638 case PRAGMA_OMP_PARALLEL:
21639 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21641 case PRAGMA_OMP_SECTIONS:
21642 stmt = cp_parser_omp_sections (parser, pragma_tok);
21644 case PRAGMA_OMP_SINGLE:
21645 stmt = cp_parser_omp_single (parser, pragma_tok);
21647 case PRAGMA_OMP_TASK:
21648 stmt = cp_parser_omp_task (parser, pragma_tok);
21651 gcc_unreachable ();
21655 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21660 static GTY (()) cp_parser *the_parser;
21663 /* Special handling for the first token or line in the file. The first
21664 thing in the file might be #pragma GCC pch_preprocess, which loads a
21665 PCH file, which is a GC collection point. So we need to handle this
21666 first pragma without benefit of an existing lexer structure.
21668 Always returns one token to the caller in *FIRST_TOKEN. This is
21669 either the true first token of the file, or the first token after
21670 the initial pragma. */
21673 cp_parser_initial_pragma (cp_token *first_token)
21677 cp_lexer_get_preprocessor_token (NULL, first_token);
21678 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21681 cp_lexer_get_preprocessor_token (NULL, first_token);
21682 if (first_token->type == CPP_STRING)
21684 name = first_token->u.value;
21686 cp_lexer_get_preprocessor_token (NULL, first_token);
21687 if (first_token->type != CPP_PRAGMA_EOL)
21688 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21689 &first_token->location);
21692 error ("%Hexpected string literal", &first_token->location);
21694 /* Skip to the end of the pragma. */
21695 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21696 cp_lexer_get_preprocessor_token (NULL, first_token);
21698 /* Now actually load the PCH file. */
21700 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21702 /* Read one more token to return to our caller. We have to do this
21703 after reading the PCH file in, since its pointers have to be
21705 cp_lexer_get_preprocessor_token (NULL, first_token);
21708 /* Normal parsing of a pragma token. Here we can (and must) use the
21712 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21714 cp_token *pragma_tok;
21717 pragma_tok = cp_lexer_consume_token (parser->lexer);
21718 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21719 parser->lexer->in_pragma = true;
21721 id = pragma_tok->pragma_kind;
21724 case PRAGMA_GCC_PCH_PREPROCESS:
21725 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21726 &pragma_tok->location);
21729 case PRAGMA_OMP_BARRIER:
21732 case pragma_compound:
21733 cp_parser_omp_barrier (parser, pragma_tok);
21736 error ("%H%<#pragma omp barrier%> may only be "
21737 "used in compound statements", &pragma_tok->location);
21744 case PRAGMA_OMP_FLUSH:
21747 case pragma_compound:
21748 cp_parser_omp_flush (parser, pragma_tok);
21751 error ("%H%<#pragma omp flush%> may only be "
21752 "used in compound statements", &pragma_tok->location);
21759 case PRAGMA_OMP_TASKWAIT:
21762 case pragma_compound:
21763 cp_parser_omp_taskwait (parser, pragma_tok);
21766 error ("%H%<#pragma omp taskwait%> may only be "
21767 "used in compound statements",
21768 &pragma_tok->location);
21775 case PRAGMA_OMP_THREADPRIVATE:
21776 cp_parser_omp_threadprivate (parser, pragma_tok);
21779 case PRAGMA_OMP_ATOMIC:
21780 case PRAGMA_OMP_CRITICAL:
21781 case PRAGMA_OMP_FOR:
21782 case PRAGMA_OMP_MASTER:
21783 case PRAGMA_OMP_ORDERED:
21784 case PRAGMA_OMP_PARALLEL:
21785 case PRAGMA_OMP_SECTIONS:
21786 case PRAGMA_OMP_SINGLE:
21787 case PRAGMA_OMP_TASK:
21788 if (context == pragma_external)
21790 cp_parser_omp_construct (parser, pragma_tok);
21793 case PRAGMA_OMP_SECTION:
21794 error ("%H%<#pragma omp section%> may only be used in "
21795 "%<#pragma omp sections%> construct", &pragma_tok->location);
21799 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21800 c_invoke_pragma_handler (id);
21804 cp_parser_error (parser, "expected declaration specifiers");
21808 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21812 /* The interface the pragma parsers have to the lexer. */
21815 pragma_lex (tree *value)
21818 enum cpp_ttype ret;
21820 tok = cp_lexer_peek_token (the_parser->lexer);
21823 *value = tok->u.value;
21825 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21827 else if (ret == CPP_STRING)
21828 *value = cp_parser_string_literal (the_parser, false, false);
21831 cp_lexer_consume_token (the_parser->lexer);
21832 if (ret == CPP_KEYWORD)
21840 /* External interface. */
21842 /* Parse one entire translation unit. */
21845 c_parse_file (void)
21847 bool error_occurred;
21848 static bool already_called = false;
21850 if (already_called)
21852 sorry ("inter-module optimizations not implemented for C++");
21855 already_called = true;
21857 the_parser = cp_parser_new ();
21858 push_deferring_access_checks (flag_access_control
21859 ? dk_no_deferred : dk_no_check);
21860 error_occurred = cp_parser_translation_unit (the_parser);
21864 #include "gt-cp-parser.h"