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 if (cxx_dialect == cxx98)
8329 /* Consume the token. */
8330 cp_lexer_consume_token (parser->lexer);
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 /* C++0x auto type-specifier. */
8345 found_decl_spec = false;
8352 /* Consume the token. */
8353 cp_lexer_consume_token (parser->lexer);
8354 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8358 /* Consume the token. */
8359 cp_lexer_consume_token (parser->lexer);
8360 ++decl_specs->specs[(int) ds_thread];
8364 /* We did not yet find a decl-specifier yet. */
8365 found_decl_spec = false;
8369 /* Constructors are a special case. The `S' in `S()' is not a
8370 decl-specifier; it is the beginning of the declarator. */
8373 && constructor_possible_p
8374 && (cp_parser_constructor_declarator_p
8375 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8377 /* If we don't have a DECL_SPEC yet, then we must be looking at
8378 a type-specifier. */
8379 if (!found_decl_spec && !constructor_p)
8381 int decl_spec_declares_class_or_enum;
8382 bool is_cv_qualifier;
8386 = cp_parser_type_specifier (parser, flags,
8388 /*is_declaration=*/true,
8389 &decl_spec_declares_class_or_enum,
8391 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8393 /* If this type-specifier referenced a user-defined type
8394 (a typedef, class-name, etc.), then we can't allow any
8395 more such type-specifiers henceforth.
8399 The longest sequence of decl-specifiers that could
8400 possibly be a type name is taken as the
8401 decl-specifier-seq of a declaration. The sequence shall
8402 be self-consistent as described below.
8406 As a general rule, at most one type-specifier is allowed
8407 in the complete decl-specifier-seq of a declaration. The
8408 only exceptions are the following:
8410 -- const or volatile can be combined with any other
8413 -- signed or unsigned can be combined with char, long,
8421 void g (const int Pc);
8423 Here, Pc is *not* part of the decl-specifier seq; it's
8424 the declarator. Therefore, once we see a type-specifier
8425 (other than a cv-qualifier), we forbid any additional
8426 user-defined types. We *do* still allow things like `int
8427 int' to be considered a decl-specifier-seq, and issue the
8428 error message later. */
8429 if (type_spec && !is_cv_qualifier)
8430 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8431 /* A constructor declarator cannot follow a type-specifier. */
8434 constructor_possible_p = false;
8435 found_decl_spec = true;
8439 /* If we still do not have a DECL_SPEC, then there are no more
8441 if (!found_decl_spec)
8444 decl_specs->any_specifiers_p = true;
8445 /* After we see one decl-specifier, further decl-specifiers are
8447 flags |= CP_PARSER_FLAGS_OPTIONAL;
8450 cp_parser_check_decl_spec (decl_specs, start_token->location);
8452 /* Don't allow a friend specifier with a class definition. */
8453 if (decl_specs->specs[(int) ds_friend] != 0
8454 && (*declares_class_or_enum & 2))
8455 error ("%Hclass definition may not be declared a friend",
8456 &start_token->location);
8459 /* Parse an (optional) storage-class-specifier.
8461 storage-class-specifier:
8470 storage-class-specifier:
8473 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8476 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8478 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8481 if (cxx_dialect != cxx98)
8483 /* Fall through for C++98. */
8490 /* Consume the token. */
8491 return cp_lexer_consume_token (parser->lexer)->u.value;
8498 /* Parse an (optional) function-specifier.
8505 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8506 Updates DECL_SPECS, if it is non-NULL. */
8509 cp_parser_function_specifier_opt (cp_parser* parser,
8510 cp_decl_specifier_seq *decl_specs)
8512 cp_token *token = cp_lexer_peek_token (parser->lexer);
8513 switch (token->keyword)
8517 ++decl_specs->specs[(int) ds_inline];
8521 /* 14.5.2.3 [temp.mem]
8523 A member function template shall not be virtual. */
8524 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8525 error ("%Htemplates may not be %<virtual%>", &token->location);
8526 else if (decl_specs)
8527 ++decl_specs->specs[(int) ds_virtual];
8532 ++decl_specs->specs[(int) ds_explicit];
8539 /* Consume the token. */
8540 return cp_lexer_consume_token (parser->lexer)->u.value;
8543 /* Parse a linkage-specification.
8545 linkage-specification:
8546 extern string-literal { declaration-seq [opt] }
8547 extern string-literal declaration */
8550 cp_parser_linkage_specification (cp_parser* parser)
8554 /* Look for the `extern' keyword. */
8555 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8557 /* Look for the string-literal. */
8558 linkage = cp_parser_string_literal (parser, false, false);
8560 /* Transform the literal into an identifier. If the literal is a
8561 wide-character string, or contains embedded NULs, then we can't
8562 handle it as the user wants. */
8563 if (strlen (TREE_STRING_POINTER (linkage))
8564 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8566 cp_parser_error (parser, "invalid linkage-specification");
8567 /* Assume C++ linkage. */
8568 linkage = lang_name_cplusplus;
8571 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8573 /* We're now using the new linkage. */
8574 push_lang_context (linkage);
8576 /* If the next token is a `{', then we're using the first
8578 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8580 /* Consume the `{' token. */
8581 cp_lexer_consume_token (parser->lexer);
8582 /* Parse the declarations. */
8583 cp_parser_declaration_seq_opt (parser);
8584 /* Look for the closing `}'. */
8585 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8587 /* Otherwise, there's just one declaration. */
8590 bool saved_in_unbraced_linkage_specification_p;
8592 saved_in_unbraced_linkage_specification_p
8593 = parser->in_unbraced_linkage_specification_p;
8594 parser->in_unbraced_linkage_specification_p = true;
8595 cp_parser_declaration (parser);
8596 parser->in_unbraced_linkage_specification_p
8597 = saved_in_unbraced_linkage_specification_p;
8600 /* We're done with the linkage-specification. */
8601 pop_lang_context ();
8604 /* Parse a static_assert-declaration.
8606 static_assert-declaration:
8607 static_assert ( constant-expression , string-literal ) ;
8609 If MEMBER_P, this static_assert is a class member. */
8612 cp_parser_static_assert(cp_parser *parser, bool member_p)
8617 location_t saved_loc;
8619 /* Peek at the `static_assert' token so we can keep track of exactly
8620 where the static assertion started. */
8621 token = cp_lexer_peek_token (parser->lexer);
8622 saved_loc = token->location;
8624 /* Look for the `static_assert' keyword. */
8625 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8626 "%<static_assert%>"))
8629 /* We know we are in a static assertion; commit to any tentative
8631 if (cp_parser_parsing_tentatively (parser))
8632 cp_parser_commit_to_tentative_parse (parser);
8634 /* Parse the `(' starting the static assertion condition. */
8635 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8637 /* Parse the constant-expression. */
8639 cp_parser_constant_expression (parser,
8640 /*allow_non_constant_p=*/false,
8641 /*non_constant_p=*/NULL);
8643 /* Parse the separating `,'. */
8644 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8646 /* Parse the string-literal message. */
8647 message = cp_parser_string_literal (parser,
8648 /*translate=*/false,
8651 /* A `)' completes the static assertion. */
8652 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8653 cp_parser_skip_to_closing_parenthesis (parser,
8654 /*recovering=*/true,
8656 /*consume_paren=*/true);
8658 /* A semicolon terminates the declaration. */
8659 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8661 /* Complete the static assertion, which may mean either processing
8662 the static assert now or saving it for template instantiation. */
8663 finish_static_assert (condition, message, saved_loc, member_p);
8666 /* Parse a `decltype' type. Returns the type.
8668 simple-type-specifier:
8669 decltype ( expression ) */
8672 cp_parser_decltype (cp_parser *parser)
8675 bool id_expression_or_member_access_p = false;
8676 const char *saved_message;
8677 bool saved_integral_constant_expression_p;
8678 bool saved_non_integral_constant_expression_p;
8679 cp_token *id_expr_start_token;
8681 /* Look for the `decltype' token. */
8682 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8683 return error_mark_node;
8685 /* Types cannot be defined in a `decltype' expression. Save away the
8687 saved_message = parser->type_definition_forbidden_message;
8689 /* And create the new one. */
8690 parser->type_definition_forbidden_message
8691 = "types may not be defined in %<decltype%> expressions";
8693 /* The restrictions on constant-expressions do not apply inside
8694 decltype expressions. */
8695 saved_integral_constant_expression_p
8696 = parser->integral_constant_expression_p;
8697 saved_non_integral_constant_expression_p
8698 = parser->non_integral_constant_expression_p;
8699 parser->integral_constant_expression_p = false;
8701 /* Do not actually evaluate the expression. */
8704 /* Parse the opening `('. */
8705 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8706 return error_mark_node;
8708 /* First, try parsing an id-expression. */
8709 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8710 cp_parser_parse_tentatively (parser);
8711 expr = cp_parser_id_expression (parser,
8712 /*template_keyword_p=*/false,
8713 /*check_dependency_p=*/true,
8714 /*template_p=*/NULL,
8715 /*declarator_p=*/false,
8716 /*optional_p=*/false);
8718 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8720 bool non_integral_constant_expression_p = false;
8721 tree id_expression = expr;
8723 const char *error_msg;
8725 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8726 /* Lookup the name we got back from the id-expression. */
8727 expr = cp_parser_lookup_name (parser, expr,
8729 /*is_template=*/false,
8730 /*is_namespace=*/false,
8731 /*check_dependency=*/true,
8732 /*ambiguous_decls=*/NULL,
8733 id_expr_start_token->location);
8736 && expr != error_mark_node
8737 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8738 && TREE_CODE (expr) != TYPE_DECL
8739 && (TREE_CODE (expr) != BIT_NOT_EXPR
8740 || !TYPE_P (TREE_OPERAND (expr, 0)))
8741 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8743 /* Complete lookup of the id-expression. */
8744 expr = (finish_id_expression
8745 (id_expression, expr, parser->scope, &idk,
8746 /*integral_constant_expression_p=*/false,
8747 /*allow_non_integral_constant_expression_p=*/true,
8748 &non_integral_constant_expression_p,
8749 /*template_p=*/false,
8751 /*address_p=*/false,
8752 /*template_arg_p=*/false,
8754 id_expr_start_token->location));
8756 if (expr == error_mark_node)
8757 /* We found an id-expression, but it was something that we
8758 should not have found. This is an error, not something
8759 we can recover from, so note that we found an
8760 id-expression and we'll recover as gracefully as
8762 id_expression_or_member_access_p = true;
8766 && expr != error_mark_node
8767 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8768 /* We have an id-expression. */
8769 id_expression_or_member_access_p = true;
8772 if (!id_expression_or_member_access_p)
8774 /* Abort the id-expression parse. */
8775 cp_parser_abort_tentative_parse (parser);
8777 /* Parsing tentatively, again. */
8778 cp_parser_parse_tentatively (parser);
8780 /* Parse a class member access. */
8781 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8783 /*member_access_only_p=*/true);
8786 && expr != error_mark_node
8787 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8788 /* We have an id-expression. */
8789 id_expression_or_member_access_p = true;
8792 if (id_expression_or_member_access_p)
8793 /* We have parsed the complete id-expression or member access. */
8794 cp_parser_parse_definitely (parser);
8797 /* Abort our attempt to parse an id-expression or member access
8799 cp_parser_abort_tentative_parse (parser);
8801 /* Parse a full expression. */
8802 expr = cp_parser_expression (parser, /*cast_p=*/false);
8805 /* Go back to evaluating expressions. */
8808 /* Restore the old message and the integral constant expression
8810 parser->type_definition_forbidden_message = saved_message;
8811 parser->integral_constant_expression_p
8812 = saved_integral_constant_expression_p;
8813 parser->non_integral_constant_expression_p
8814 = saved_non_integral_constant_expression_p;
8816 if (expr == error_mark_node)
8818 /* Skip everything up to the closing `)'. */
8819 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8820 /*consume_paren=*/true);
8821 return error_mark_node;
8824 /* Parse to the closing `)'. */
8825 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8827 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8828 /*consume_paren=*/true);
8829 return error_mark_node;
8832 return finish_decltype_type (expr, id_expression_or_member_access_p);
8835 /* Special member functions [gram.special] */
8837 /* Parse a conversion-function-id.
8839 conversion-function-id:
8840 operator conversion-type-id
8842 Returns an IDENTIFIER_NODE representing the operator. */
8845 cp_parser_conversion_function_id (cp_parser* parser)
8849 tree saved_qualifying_scope;
8850 tree saved_object_scope;
8851 tree pushed_scope = NULL_TREE;
8853 /* Look for the `operator' token. */
8854 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8855 return error_mark_node;
8856 /* When we parse the conversion-type-id, the current scope will be
8857 reset. However, we need that information in able to look up the
8858 conversion function later, so we save it here. */
8859 saved_scope = parser->scope;
8860 saved_qualifying_scope = parser->qualifying_scope;
8861 saved_object_scope = parser->object_scope;
8862 /* We must enter the scope of the class so that the names of
8863 entities declared within the class are available in the
8864 conversion-type-id. For example, consider:
8871 S::operator I() { ... }
8873 In order to see that `I' is a type-name in the definition, we
8874 must be in the scope of `S'. */
8876 pushed_scope = push_scope (saved_scope);
8877 /* Parse the conversion-type-id. */
8878 type = cp_parser_conversion_type_id (parser);
8879 /* Leave the scope of the class, if any. */
8881 pop_scope (pushed_scope);
8882 /* Restore the saved scope. */
8883 parser->scope = saved_scope;
8884 parser->qualifying_scope = saved_qualifying_scope;
8885 parser->object_scope = saved_object_scope;
8886 /* If the TYPE is invalid, indicate failure. */
8887 if (type == error_mark_node)
8888 return error_mark_node;
8889 return mangle_conv_op_name_for_type (type);
8892 /* Parse a conversion-type-id:
8895 type-specifier-seq conversion-declarator [opt]
8897 Returns the TYPE specified. */
8900 cp_parser_conversion_type_id (cp_parser* parser)
8903 cp_decl_specifier_seq type_specifiers;
8904 cp_declarator *declarator;
8905 tree type_specified;
8907 /* Parse the attributes. */
8908 attributes = cp_parser_attributes_opt (parser);
8909 /* Parse the type-specifiers. */
8910 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8912 /* If that didn't work, stop. */
8913 if (type_specifiers.type == error_mark_node)
8914 return error_mark_node;
8915 /* Parse the conversion-declarator. */
8916 declarator = cp_parser_conversion_declarator_opt (parser);
8918 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8919 /*initialized=*/0, &attributes);
8921 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8922 return type_specified;
8925 /* Parse an (optional) conversion-declarator.
8927 conversion-declarator:
8928 ptr-operator conversion-declarator [opt]
8932 static cp_declarator *
8933 cp_parser_conversion_declarator_opt (cp_parser* parser)
8935 enum tree_code code;
8937 cp_cv_quals cv_quals;
8939 /* We don't know if there's a ptr-operator next, or not. */
8940 cp_parser_parse_tentatively (parser);
8941 /* Try the ptr-operator. */
8942 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8943 /* If it worked, look for more conversion-declarators. */
8944 if (cp_parser_parse_definitely (parser))
8946 cp_declarator *declarator;
8948 /* Parse another optional declarator. */
8949 declarator = cp_parser_conversion_declarator_opt (parser);
8951 return cp_parser_make_indirect_declarator
8952 (code, class_type, cv_quals, declarator);
8958 /* Parse an (optional) ctor-initializer.
8961 : mem-initializer-list
8963 Returns TRUE iff the ctor-initializer was actually present. */
8966 cp_parser_ctor_initializer_opt (cp_parser* parser)
8968 /* If the next token is not a `:', then there is no
8969 ctor-initializer. */
8970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8972 /* Do default initialization of any bases and members. */
8973 if (DECL_CONSTRUCTOR_P (current_function_decl))
8974 finish_mem_initializers (NULL_TREE);
8979 /* Consume the `:' token. */
8980 cp_lexer_consume_token (parser->lexer);
8981 /* And the mem-initializer-list. */
8982 cp_parser_mem_initializer_list (parser);
8987 /* Parse a mem-initializer-list.
8989 mem-initializer-list:
8990 mem-initializer ... [opt]
8991 mem-initializer ... [opt] , mem-initializer-list */
8994 cp_parser_mem_initializer_list (cp_parser* parser)
8996 tree mem_initializer_list = NULL_TREE;
8997 cp_token *token = cp_lexer_peek_token (parser->lexer);
8999 /* Let the semantic analysis code know that we are starting the
9000 mem-initializer-list. */
9001 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9002 error ("%Honly constructors take base initializers",
9005 /* Loop through the list. */
9008 tree mem_initializer;
9010 token = cp_lexer_peek_token (parser->lexer);
9011 /* Parse the mem-initializer. */
9012 mem_initializer = cp_parser_mem_initializer (parser);
9013 /* If the next token is a `...', we're expanding member initializers. */
9014 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9016 /* Consume the `...'. */
9017 cp_lexer_consume_token (parser->lexer);
9019 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9020 can be expanded but members cannot. */
9021 if (mem_initializer != error_mark_node
9022 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9024 error ("%Hcannot expand initializer for member %<%D%>",
9025 &token->location, TREE_PURPOSE (mem_initializer));
9026 mem_initializer = error_mark_node;
9029 /* Construct the pack expansion type. */
9030 if (mem_initializer != error_mark_node)
9031 mem_initializer = make_pack_expansion (mem_initializer);
9033 /* Add it to the list, unless it was erroneous. */
9034 if (mem_initializer != error_mark_node)
9036 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9037 mem_initializer_list = mem_initializer;
9039 /* If the next token is not a `,', we're done. */
9040 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9042 /* Consume the `,' token. */
9043 cp_lexer_consume_token (parser->lexer);
9046 /* Perform semantic analysis. */
9047 if (DECL_CONSTRUCTOR_P (current_function_decl))
9048 finish_mem_initializers (mem_initializer_list);
9051 /* Parse a mem-initializer.
9054 mem-initializer-id ( expression-list [opt] )
9055 mem-initializer-id braced-init-list
9060 ( expression-list [opt] )
9062 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9063 class) or FIELD_DECL (for a non-static data member) to initialize;
9064 the TREE_VALUE is the expression-list. An empty initialization
9065 list is represented by void_list_node. */
9068 cp_parser_mem_initializer (cp_parser* parser)
9070 tree mem_initializer_id;
9071 tree expression_list;
9073 cp_token *token = cp_lexer_peek_token (parser->lexer);
9075 /* Find out what is being initialized. */
9076 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9078 permerror (token->location,
9079 "anachronistic old-style base class initializer");
9080 mem_initializer_id = NULL_TREE;
9083 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9084 member = expand_member_init (mem_initializer_id);
9085 if (member && !DECL_P (member))
9086 in_base_initializer = 1;
9088 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9090 bool expr_non_constant_p;
9091 maybe_warn_cpp0x ("extended initializer lists");
9092 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9093 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9094 expression_list = build_tree_list (NULL_TREE, expression_list);
9098 = cp_parser_parenthesized_expression_list (parser, false,
9100 /*allow_expansion_p=*/true,
9101 /*non_constant_p=*/NULL);
9102 if (expression_list == error_mark_node)
9103 return error_mark_node;
9104 if (!expression_list)
9105 expression_list = void_type_node;
9107 in_base_initializer = 0;
9109 return member ? build_tree_list (member, expression_list) : error_mark_node;
9112 /* Parse a mem-initializer-id.
9115 :: [opt] nested-name-specifier [opt] class-name
9118 Returns a TYPE indicating the class to be initializer for the first
9119 production. Returns an IDENTIFIER_NODE indicating the data member
9120 to be initialized for the second production. */
9123 cp_parser_mem_initializer_id (cp_parser* parser)
9125 bool global_scope_p;
9126 bool nested_name_specifier_p;
9127 bool template_p = false;
9130 cp_token *token = cp_lexer_peek_token (parser->lexer);
9132 /* `typename' is not allowed in this context ([temp.res]). */
9133 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9135 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9136 "member initializer is implicitly a type)",
9138 cp_lexer_consume_token (parser->lexer);
9140 /* Look for the optional `::' operator. */
9142 = (cp_parser_global_scope_opt (parser,
9143 /*current_scope_valid_p=*/false)
9145 /* Look for the optional nested-name-specifier. The simplest way to
9150 The keyword `typename' is not permitted in a base-specifier or
9151 mem-initializer; in these contexts a qualified name that
9152 depends on a template-parameter is implicitly assumed to be a
9155 is to assume that we have seen the `typename' keyword at this
9157 nested_name_specifier_p
9158 = (cp_parser_nested_name_specifier_opt (parser,
9159 /*typename_keyword_p=*/true,
9160 /*check_dependency_p=*/true,
9162 /*is_declaration=*/true)
9164 if (nested_name_specifier_p)
9165 template_p = cp_parser_optional_template_keyword (parser);
9166 /* If there is a `::' operator or a nested-name-specifier, then we
9167 are definitely looking for a class-name. */
9168 if (global_scope_p || nested_name_specifier_p)
9169 return cp_parser_class_name (parser,
9170 /*typename_keyword_p=*/true,
9171 /*template_keyword_p=*/template_p,
9173 /*check_dependency_p=*/true,
9174 /*class_head_p=*/false,
9175 /*is_declaration=*/true);
9176 /* Otherwise, we could also be looking for an ordinary identifier. */
9177 cp_parser_parse_tentatively (parser);
9178 /* Try a class-name. */
9179 id = cp_parser_class_name (parser,
9180 /*typename_keyword_p=*/true,
9181 /*template_keyword_p=*/false,
9183 /*check_dependency_p=*/true,
9184 /*class_head_p=*/false,
9185 /*is_declaration=*/true);
9186 /* If we found one, we're done. */
9187 if (cp_parser_parse_definitely (parser))
9189 /* Otherwise, look for an ordinary identifier. */
9190 return cp_parser_identifier (parser);
9193 /* Overloading [gram.over] */
9195 /* Parse an operator-function-id.
9197 operator-function-id:
9200 Returns an IDENTIFIER_NODE for the operator which is a
9201 human-readable spelling of the identifier, e.g., `operator +'. */
9204 cp_parser_operator_function_id (cp_parser* parser)
9206 /* Look for the `operator' keyword. */
9207 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9208 return error_mark_node;
9209 /* And then the name of the operator itself. */
9210 return cp_parser_operator (parser);
9213 /* Parse an operator.
9216 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9217 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9218 || ++ -- , ->* -> () []
9225 Returns an IDENTIFIER_NODE for the operator which is a
9226 human-readable spelling of the identifier, e.g., `operator +'. */
9229 cp_parser_operator (cp_parser* parser)
9231 tree id = NULL_TREE;
9234 /* Peek at the next token. */
9235 token = cp_lexer_peek_token (parser->lexer);
9236 /* Figure out which operator we have. */
9237 switch (token->type)
9243 /* The keyword should be either `new' or `delete'. */
9244 if (token->keyword == RID_NEW)
9246 else if (token->keyword == RID_DELETE)
9251 /* Consume the `new' or `delete' token. */
9252 cp_lexer_consume_token (parser->lexer);
9254 /* Peek at the next token. */
9255 token = cp_lexer_peek_token (parser->lexer);
9256 /* If it's a `[' token then this is the array variant of the
9258 if (token->type == CPP_OPEN_SQUARE)
9260 /* Consume the `[' token. */
9261 cp_lexer_consume_token (parser->lexer);
9262 /* Look for the `]' token. */
9263 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9264 id = ansi_opname (op == NEW_EXPR
9265 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9267 /* Otherwise, we have the non-array variant. */
9269 id = ansi_opname (op);
9275 id = ansi_opname (PLUS_EXPR);
9279 id = ansi_opname (MINUS_EXPR);
9283 id = ansi_opname (MULT_EXPR);
9287 id = ansi_opname (TRUNC_DIV_EXPR);
9291 id = ansi_opname (TRUNC_MOD_EXPR);
9295 id = ansi_opname (BIT_XOR_EXPR);
9299 id = ansi_opname (BIT_AND_EXPR);
9303 id = ansi_opname (BIT_IOR_EXPR);
9307 id = ansi_opname (BIT_NOT_EXPR);
9311 id = ansi_opname (TRUTH_NOT_EXPR);
9315 id = ansi_assopname (NOP_EXPR);
9319 id = ansi_opname (LT_EXPR);
9323 id = ansi_opname (GT_EXPR);
9327 id = ansi_assopname (PLUS_EXPR);
9331 id = ansi_assopname (MINUS_EXPR);
9335 id = ansi_assopname (MULT_EXPR);
9339 id = ansi_assopname (TRUNC_DIV_EXPR);
9343 id = ansi_assopname (TRUNC_MOD_EXPR);
9347 id = ansi_assopname (BIT_XOR_EXPR);
9351 id = ansi_assopname (BIT_AND_EXPR);
9355 id = ansi_assopname (BIT_IOR_EXPR);
9359 id = ansi_opname (LSHIFT_EXPR);
9363 id = ansi_opname (RSHIFT_EXPR);
9367 id = ansi_assopname (LSHIFT_EXPR);
9371 id = ansi_assopname (RSHIFT_EXPR);
9375 id = ansi_opname (EQ_EXPR);
9379 id = ansi_opname (NE_EXPR);
9383 id = ansi_opname (LE_EXPR);
9386 case CPP_GREATER_EQ:
9387 id = ansi_opname (GE_EXPR);
9391 id = ansi_opname (TRUTH_ANDIF_EXPR);
9395 id = ansi_opname (TRUTH_ORIF_EXPR);
9399 id = ansi_opname (POSTINCREMENT_EXPR);
9402 case CPP_MINUS_MINUS:
9403 id = ansi_opname (PREDECREMENT_EXPR);
9407 id = ansi_opname (COMPOUND_EXPR);
9410 case CPP_DEREF_STAR:
9411 id = ansi_opname (MEMBER_REF);
9415 id = ansi_opname (COMPONENT_REF);
9418 case CPP_OPEN_PAREN:
9419 /* Consume the `('. */
9420 cp_lexer_consume_token (parser->lexer);
9421 /* Look for the matching `)'. */
9422 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9423 return ansi_opname (CALL_EXPR);
9425 case CPP_OPEN_SQUARE:
9426 /* Consume the `['. */
9427 cp_lexer_consume_token (parser->lexer);
9428 /* Look for the matching `]'. */
9429 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9430 return ansi_opname (ARRAY_REF);
9433 /* Anything else is an error. */
9437 /* If we have selected an identifier, we need to consume the
9440 cp_lexer_consume_token (parser->lexer);
9441 /* Otherwise, no valid operator name was present. */
9444 cp_parser_error (parser, "expected operator");
9445 id = error_mark_node;
9451 /* Parse a template-declaration.
9453 template-declaration:
9454 export [opt] template < template-parameter-list > declaration
9456 If MEMBER_P is TRUE, this template-declaration occurs within a
9459 The grammar rule given by the standard isn't correct. What
9462 template-declaration:
9463 export [opt] template-parameter-list-seq
9464 decl-specifier-seq [opt] init-declarator [opt] ;
9465 export [opt] template-parameter-list-seq
9468 template-parameter-list-seq:
9469 template-parameter-list-seq [opt]
9470 template < template-parameter-list > */
9473 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9475 /* Check for `export'. */
9476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9478 /* Consume the `export' token. */
9479 cp_lexer_consume_token (parser->lexer);
9480 /* Warn that we do not support `export'. */
9481 warning (0, "keyword %<export%> not implemented, and will be ignored");
9484 cp_parser_template_declaration_after_export (parser, member_p);
9487 /* Parse a template-parameter-list.
9489 template-parameter-list:
9491 template-parameter-list , template-parameter
9493 Returns a TREE_LIST. Each node represents a template parameter.
9494 The nodes are connected via their TREE_CHAINs. */
9497 cp_parser_template_parameter_list (cp_parser* parser)
9499 tree parameter_list = NULL_TREE;
9501 begin_template_parm_list ();
9506 bool is_parameter_pack;
9508 /* Parse the template-parameter. */
9509 parameter = cp_parser_template_parameter (parser,
9511 &is_parameter_pack);
9512 /* Add it to the list. */
9513 if (parameter != error_mark_node)
9514 parameter_list = process_template_parm (parameter_list,
9520 tree err_parm = build_tree_list (parameter, parameter);
9521 TREE_VALUE (err_parm) = error_mark_node;
9522 parameter_list = chainon (parameter_list, err_parm);
9525 /* If the next token is not a `,', we're done. */
9526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9528 /* Otherwise, consume the `,' token. */
9529 cp_lexer_consume_token (parser->lexer);
9532 return end_template_parm_list (parameter_list);
9535 /* Parse a template-parameter.
9539 parameter-declaration
9541 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9542 the parameter. The TREE_PURPOSE is the default value, if any.
9543 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9544 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9545 set to true iff this parameter is a parameter pack. */
9548 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9549 bool *is_parameter_pack)
9552 cp_parameter_declarator *parameter_declarator;
9553 cp_declarator *id_declarator;
9556 /* Assume it is a type parameter or a template parameter. */
9557 *is_non_type = false;
9558 /* Assume it not a parameter pack. */
9559 *is_parameter_pack = false;
9560 /* Peek at the next token. */
9561 token = cp_lexer_peek_token (parser->lexer);
9562 /* If it is `class' or `template', we have a type-parameter. */
9563 if (token->keyword == RID_TEMPLATE)
9564 return cp_parser_type_parameter (parser, is_parameter_pack);
9565 /* If it is `class' or `typename' we do not know yet whether it is a
9566 type parameter or a non-type parameter. Consider:
9568 template <typename T, typename T::X X> ...
9572 template <class C, class D*> ...
9574 Here, the first parameter is a type parameter, and the second is
9575 a non-type parameter. We can tell by looking at the token after
9576 the identifier -- if it is a `,', `=', or `>' then we have a type
9578 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9580 /* Peek at the token after `class' or `typename'. */
9581 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9582 /* If it's an ellipsis, we have a template type parameter
9584 if (token->type == CPP_ELLIPSIS)
9585 return cp_parser_type_parameter (parser, is_parameter_pack);
9586 /* If it's an identifier, skip it. */
9587 if (token->type == CPP_NAME)
9588 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9589 /* Now, see if the token looks like the end of a template
9591 if (token->type == CPP_COMMA
9592 || token->type == CPP_EQ
9593 || token->type == CPP_GREATER)
9594 return cp_parser_type_parameter (parser, is_parameter_pack);
9597 /* Otherwise, it is a non-type parameter.
9601 When parsing a default template-argument for a non-type
9602 template-parameter, the first non-nested `>' is taken as the end
9603 of the template parameter-list rather than a greater-than
9605 *is_non_type = true;
9606 parameter_declarator
9607 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9608 /*parenthesized_p=*/NULL);
9610 /* If the parameter declaration is marked as a parameter pack, set
9611 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9612 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9614 if (parameter_declarator
9615 && parameter_declarator->declarator
9616 && parameter_declarator->declarator->parameter_pack_p)
9618 *is_parameter_pack = true;
9619 parameter_declarator->declarator->parameter_pack_p = false;
9622 /* If the next token is an ellipsis, and we don't already have it
9623 marked as a parameter pack, then we have a parameter pack (that
9624 has no declarator). */
9625 if (!*is_parameter_pack
9626 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9627 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9629 /* Consume the `...'. */
9630 cp_lexer_consume_token (parser->lexer);
9631 maybe_warn_variadic_templates ();
9633 *is_parameter_pack = true;
9635 /* We might end up with a pack expansion as the type of the non-type
9636 template parameter, in which case this is a non-type template
9638 else if (parameter_declarator
9639 && parameter_declarator->decl_specifiers.type
9640 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9642 *is_parameter_pack = true;
9643 parameter_declarator->decl_specifiers.type =
9644 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9647 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9649 /* Parameter packs cannot have default arguments. However, a
9650 user may try to do so, so we'll parse them and give an
9651 appropriate diagnostic here. */
9653 /* Consume the `='. */
9654 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9655 cp_lexer_consume_token (parser->lexer);
9657 /* Find the name of the parameter pack. */
9658 id_declarator = parameter_declarator->declarator;
9659 while (id_declarator && id_declarator->kind != cdk_id)
9660 id_declarator = id_declarator->declarator;
9662 if (id_declarator && id_declarator->kind == cdk_id)
9663 error ("%Htemplate parameter pack %qD cannot have a default argument",
9664 &start_token->location, id_declarator->u.id.unqualified_name);
9666 error ("%Htemplate parameter pack cannot have a default argument",
9667 &start_token->location);
9669 /* Parse the default argument, but throw away the result. */
9670 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9673 parm = grokdeclarator (parameter_declarator->declarator,
9674 ¶meter_declarator->decl_specifiers,
9675 PARM, /*initialized=*/0,
9677 if (parm == error_mark_node)
9678 return error_mark_node;
9680 return build_tree_list (parameter_declarator->default_argument, parm);
9683 /* Parse a type-parameter.
9686 class identifier [opt]
9687 class identifier [opt] = type-id
9688 typename identifier [opt]
9689 typename identifier [opt] = type-id
9690 template < template-parameter-list > class identifier [opt]
9691 template < template-parameter-list > class identifier [opt]
9694 GNU Extension (variadic templates):
9697 class ... identifier [opt]
9698 typename ... identifier [opt]
9700 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9701 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9702 the declaration of the parameter.
9704 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9707 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9712 /* Look for a keyword to tell us what kind of parameter this is. */
9713 token = cp_parser_require (parser, CPP_KEYWORD,
9714 "%<class%>, %<typename%>, or %<template%>");
9716 return error_mark_node;
9718 switch (token->keyword)
9724 tree default_argument;
9726 /* If the next token is an ellipsis, we have a template
9728 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9730 /* Consume the `...' token. */
9731 cp_lexer_consume_token (parser->lexer);
9732 maybe_warn_variadic_templates ();
9734 *is_parameter_pack = true;
9737 /* If the next token is an identifier, then it names the
9739 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9740 identifier = cp_parser_identifier (parser);
9742 identifier = NULL_TREE;
9744 /* Create the parameter. */
9745 parameter = finish_template_type_parm (class_type_node, identifier);
9747 /* If the next token is an `=', we have a default argument. */
9748 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9750 /* Consume the `=' token. */
9751 cp_lexer_consume_token (parser->lexer);
9752 /* Parse the default-argument. */
9753 push_deferring_access_checks (dk_no_deferred);
9754 default_argument = cp_parser_type_id (parser);
9756 /* Template parameter packs cannot have default
9758 if (*is_parameter_pack)
9761 error ("%Htemplate parameter pack %qD cannot have a "
9762 "default argument", &token->location, identifier);
9764 error ("%Htemplate parameter packs cannot have "
9765 "default arguments", &token->location);
9766 default_argument = NULL_TREE;
9768 pop_deferring_access_checks ();
9771 default_argument = NULL_TREE;
9773 /* Create the combined representation of the parameter and the
9774 default argument. */
9775 parameter = build_tree_list (default_argument, parameter);
9781 tree parameter_list;
9783 tree default_argument;
9785 /* Look for the `<'. */
9786 cp_parser_require (parser, CPP_LESS, "%<<%>");
9787 /* Parse the template-parameter-list. */
9788 parameter_list = cp_parser_template_parameter_list (parser);
9789 /* Look for the `>'. */
9790 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9791 /* Look for the `class' keyword. */
9792 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9793 /* If the next token is an ellipsis, we have a template
9795 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9797 /* Consume the `...' token. */
9798 cp_lexer_consume_token (parser->lexer);
9799 maybe_warn_variadic_templates ();
9801 *is_parameter_pack = true;
9803 /* If the next token is an `=', then there is a
9804 default-argument. If the next token is a `>', we are at
9805 the end of the parameter-list. If the next token is a `,',
9806 then we are at the end of this parameter. */
9807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9808 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9809 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9811 identifier = cp_parser_identifier (parser);
9812 /* Treat invalid names as if the parameter were nameless. */
9813 if (identifier == error_mark_node)
9814 identifier = NULL_TREE;
9817 identifier = NULL_TREE;
9819 /* Create the template parameter. */
9820 parameter = finish_template_template_parm (class_type_node,
9823 /* If the next token is an `=', then there is a
9824 default-argument. */
9825 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9829 /* Consume the `='. */
9830 cp_lexer_consume_token (parser->lexer);
9831 /* Parse the id-expression. */
9832 push_deferring_access_checks (dk_no_deferred);
9833 /* save token before parsing the id-expression, for error
9835 token = cp_lexer_peek_token (parser->lexer);
9837 = cp_parser_id_expression (parser,
9838 /*template_keyword_p=*/false,
9839 /*check_dependency_p=*/true,
9840 /*template_p=*/&is_template,
9841 /*declarator_p=*/false,
9842 /*optional_p=*/false);
9843 if (TREE_CODE (default_argument) == TYPE_DECL)
9844 /* If the id-expression was a template-id that refers to
9845 a template-class, we already have the declaration here,
9846 so no further lookup is needed. */
9849 /* Look up the name. */
9851 = cp_parser_lookup_name (parser, default_argument,
9853 /*is_template=*/is_template,
9854 /*is_namespace=*/false,
9855 /*check_dependency=*/true,
9856 /*ambiguous_decls=*/NULL,
9858 /* See if the default argument is valid. */
9860 = check_template_template_default_arg (default_argument);
9862 /* Template parameter packs cannot have default
9864 if (*is_parameter_pack)
9867 error ("%Htemplate parameter pack %qD cannot "
9868 "have a default argument",
9869 &token->location, identifier);
9871 error ("%Htemplate parameter packs cannot "
9872 "have default arguments",
9874 default_argument = NULL_TREE;
9876 pop_deferring_access_checks ();
9879 default_argument = NULL_TREE;
9881 /* Create the combined representation of the parameter and the
9882 default argument. */
9883 parameter = build_tree_list (default_argument, parameter);
9895 /* Parse a template-id.
9898 template-name < template-argument-list [opt] >
9900 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9901 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9902 returned. Otherwise, if the template-name names a function, or set
9903 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9904 names a class, returns a TYPE_DECL for the specialization.
9906 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9907 uninstantiated templates. */
9910 cp_parser_template_id (cp_parser *parser,
9911 bool template_keyword_p,
9912 bool check_dependency_p,
9913 bool is_declaration)
9919 cp_token_position start_of_id = 0;
9920 deferred_access_check *chk;
9921 VEC (deferred_access_check,gc) *access_check;
9922 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9925 /* If the next token corresponds to a template-id, there is no need
9927 next_token = cp_lexer_peek_token (parser->lexer);
9928 if (next_token->type == CPP_TEMPLATE_ID)
9930 struct tree_check *check_value;
9932 /* Get the stored value. */
9933 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9934 /* Perform any access checks that were deferred. */
9935 access_check = check_value->checks;
9939 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9942 perform_or_defer_access_check (chk->binfo,
9947 /* Return the stored value. */
9948 return check_value->value;
9951 /* Avoid performing name lookup if there is no possibility of
9952 finding a template-id. */
9953 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9954 || (next_token->type == CPP_NAME
9955 && !cp_parser_nth_token_starts_template_argument_list_p
9958 cp_parser_error (parser, "expected template-id");
9959 return error_mark_node;
9962 /* Remember where the template-id starts. */
9963 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9964 start_of_id = cp_lexer_token_position (parser->lexer, false);
9966 push_deferring_access_checks (dk_deferred);
9968 /* Parse the template-name. */
9969 is_identifier = false;
9970 token = cp_lexer_peek_token (parser->lexer);
9971 templ = cp_parser_template_name (parser, template_keyword_p,
9975 if (templ == error_mark_node || is_identifier)
9977 pop_deferring_access_checks ();
9981 /* If we find the sequence `[:' after a template-name, it's probably
9982 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9983 parse correctly the argument list. */
9984 next_token = cp_lexer_peek_token (parser->lexer);
9985 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9986 if (next_token->type == CPP_OPEN_SQUARE
9987 && next_token->flags & DIGRAPH
9988 && next_token_2->type == CPP_COLON
9989 && !(next_token_2->flags & PREV_WHITE))
9991 cp_parser_parse_tentatively (parser);
9992 /* Change `:' into `::'. */
9993 next_token_2->type = CPP_SCOPE;
9994 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9996 cp_lexer_consume_token (parser->lexer);
9998 /* Parse the arguments. */
9999 arguments = cp_parser_enclosed_template_argument_list (parser);
10000 if (!cp_parser_parse_definitely (parser))
10002 /* If we couldn't parse an argument list, then we revert our changes
10003 and return simply an error. Maybe this is not a template-id
10005 next_token_2->type = CPP_COLON;
10006 cp_parser_error (parser, "expected %<<%>");
10007 pop_deferring_access_checks ();
10008 return error_mark_node;
10010 /* Otherwise, emit an error about the invalid digraph, but continue
10011 parsing because we got our argument list. */
10012 if (permerror (next_token->location,
10013 "%<<::%> cannot begin a template-argument list"))
10015 static bool hint = false;
10016 inform (next_token->location,
10017 "%<<:%> is an alternate spelling for %<[%>."
10018 " Insert whitespace between %<<%> and %<::%>");
10019 if (!hint && !flag_permissive)
10021 inform (next_token->location, "(if you use %<-fpermissive%>"
10022 " G++ will accept your code)");
10029 /* Look for the `<' that starts the template-argument-list. */
10030 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10032 pop_deferring_access_checks ();
10033 return error_mark_node;
10035 /* Parse the arguments. */
10036 arguments = cp_parser_enclosed_template_argument_list (parser);
10039 /* Build a representation of the specialization. */
10040 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10041 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10042 else if (DECL_CLASS_TEMPLATE_P (templ)
10043 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10045 bool entering_scope;
10046 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10047 template (rather than some instantiation thereof) only if
10048 is not nested within some other construct. For example, in
10049 "template <typename T> void f(T) { A<T>::", A<T> is just an
10050 instantiation of A. */
10051 entering_scope = (template_parm_scope_p ()
10052 && cp_lexer_next_token_is (parser->lexer,
10055 = finish_template_type (templ, arguments, entering_scope);
10059 /* If it's not a class-template or a template-template, it should be
10060 a function-template. */
10061 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10062 || TREE_CODE (templ) == OVERLOAD
10063 || BASELINK_P (templ)));
10065 template_id = lookup_template_function (templ, arguments);
10068 /* If parsing tentatively, replace the sequence of tokens that makes
10069 up the template-id with a CPP_TEMPLATE_ID token. That way,
10070 should we re-parse the token stream, we will not have to repeat
10071 the effort required to do the parse, nor will we issue duplicate
10072 error messages about problems during instantiation of the
10076 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10078 /* Reset the contents of the START_OF_ID token. */
10079 token->type = CPP_TEMPLATE_ID;
10080 /* Retrieve any deferred checks. Do not pop this access checks yet
10081 so the memory will not be reclaimed during token replacing below. */
10082 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10083 token->u.tree_check_value->value = template_id;
10084 token->u.tree_check_value->checks = get_deferred_access_checks ();
10085 token->keyword = RID_MAX;
10087 /* Purge all subsequent tokens. */
10088 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10090 /* ??? Can we actually assume that, if template_id ==
10091 error_mark_node, we will have issued a diagnostic to the
10092 user, as opposed to simply marking the tentative parse as
10094 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10095 error ("%Hparse error in template argument list",
10099 pop_deferring_access_checks ();
10100 return template_id;
10103 /* Parse a template-name.
10108 The standard should actually say:
10112 operator-function-id
10114 A defect report has been filed about this issue.
10116 A conversion-function-id cannot be a template name because they cannot
10117 be part of a template-id. In fact, looking at this code:
10119 a.operator K<int>()
10121 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10122 It is impossible to call a templated conversion-function-id with an
10123 explicit argument list, since the only allowed template parameter is
10124 the type to which it is converting.
10126 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10127 `template' keyword, in a construction like:
10131 In that case `f' is taken to be a template-name, even though there
10132 is no way of knowing for sure.
10134 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10135 name refers to a set of overloaded functions, at least one of which
10136 is a template, or an IDENTIFIER_NODE with the name of the template,
10137 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10138 names are looked up inside uninstantiated templates. */
10141 cp_parser_template_name (cp_parser* parser,
10142 bool template_keyword_p,
10143 bool check_dependency_p,
10144 bool is_declaration,
10145 bool *is_identifier)
10150 cp_token *token = cp_lexer_peek_token (parser->lexer);
10152 /* If the next token is `operator', then we have either an
10153 operator-function-id or a conversion-function-id. */
10154 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10156 /* We don't know whether we're looking at an
10157 operator-function-id or a conversion-function-id. */
10158 cp_parser_parse_tentatively (parser);
10159 /* Try an operator-function-id. */
10160 identifier = cp_parser_operator_function_id (parser);
10161 /* If that didn't work, try a conversion-function-id. */
10162 if (!cp_parser_parse_definitely (parser))
10164 cp_parser_error (parser, "expected template-name");
10165 return error_mark_node;
10168 /* Look for the identifier. */
10170 identifier = cp_parser_identifier (parser);
10172 /* If we didn't find an identifier, we don't have a template-id. */
10173 if (identifier == error_mark_node)
10174 return error_mark_node;
10176 /* If the name immediately followed the `template' keyword, then it
10177 is a template-name. However, if the next token is not `<', then
10178 we do not treat it as a template-name, since it is not being used
10179 as part of a template-id. This enables us to handle constructs
10182 template <typename T> struct S { S(); };
10183 template <typename T> S<T>::S();
10185 correctly. We would treat `S' as a template -- if it were `S<T>'
10186 -- but we do not if there is no `<'. */
10188 if (processing_template_decl
10189 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10191 /* In a declaration, in a dependent context, we pretend that the
10192 "template" keyword was present in order to improve error
10193 recovery. For example, given:
10195 template <typename T> void f(T::X<int>);
10197 we want to treat "X<int>" as a template-id. */
10199 && !template_keyword_p
10200 && parser->scope && TYPE_P (parser->scope)
10201 && check_dependency_p
10202 && dependent_type_p (parser->scope)
10203 /* Do not do this for dtors (or ctors), since they never
10204 need the template keyword before their name. */
10205 && !constructor_name_p (identifier, parser->scope))
10207 cp_token_position start = 0;
10209 /* Explain what went wrong. */
10210 error ("%Hnon-template %qD used as template",
10211 &token->location, identifier);
10212 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10213 parser->scope, identifier);
10214 /* If parsing tentatively, find the location of the "<" token. */
10215 if (cp_parser_simulate_error (parser))
10216 start = cp_lexer_token_position (parser->lexer, true);
10217 /* Parse the template arguments so that we can issue error
10218 messages about them. */
10219 cp_lexer_consume_token (parser->lexer);
10220 cp_parser_enclosed_template_argument_list (parser);
10221 /* Skip tokens until we find a good place from which to
10222 continue parsing. */
10223 cp_parser_skip_to_closing_parenthesis (parser,
10224 /*recovering=*/true,
10226 /*consume_paren=*/false);
10227 /* If parsing tentatively, permanently remove the
10228 template argument list. That will prevent duplicate
10229 error messages from being issued about the missing
10230 "template" keyword. */
10232 cp_lexer_purge_tokens_after (parser->lexer, start);
10234 *is_identifier = true;
10238 /* If the "template" keyword is present, then there is generally
10239 no point in doing name-lookup, so we just return IDENTIFIER.
10240 But, if the qualifying scope is non-dependent then we can
10241 (and must) do name-lookup normally. */
10242 if (template_keyword_p
10244 || (TYPE_P (parser->scope)
10245 && dependent_type_p (parser->scope))))
10249 /* Look up the name. */
10250 decl = cp_parser_lookup_name (parser, identifier,
10252 /*is_template=*/false,
10253 /*is_namespace=*/false,
10254 check_dependency_p,
10255 /*ambiguous_decls=*/NULL,
10257 decl = maybe_get_template_decl_from_type_decl (decl);
10259 /* If DECL is a template, then the name was a template-name. */
10260 if (TREE_CODE (decl) == TEMPLATE_DECL)
10264 tree fn = NULL_TREE;
10266 /* The standard does not explicitly indicate whether a name that
10267 names a set of overloaded declarations, some of which are
10268 templates, is a template-name. However, such a name should
10269 be a template-name; otherwise, there is no way to form a
10270 template-id for the overloaded templates. */
10271 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10272 if (TREE_CODE (fns) == OVERLOAD)
10273 for (fn = fns; fn; fn = OVL_NEXT (fn))
10274 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10279 /* The name does not name a template. */
10280 cp_parser_error (parser, "expected template-name");
10281 return error_mark_node;
10285 /* If DECL is dependent, and refers to a function, then just return
10286 its name; we will look it up again during template instantiation. */
10287 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10289 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10290 if (TYPE_P (scope) && dependent_type_p (scope))
10297 /* Parse a template-argument-list.
10299 template-argument-list:
10300 template-argument ... [opt]
10301 template-argument-list , template-argument ... [opt]
10303 Returns a TREE_VEC containing the arguments. */
10306 cp_parser_template_argument_list (cp_parser* parser)
10308 tree fixed_args[10];
10309 unsigned n_args = 0;
10310 unsigned alloced = 10;
10311 tree *arg_ary = fixed_args;
10313 bool saved_in_template_argument_list_p;
10315 bool saved_non_ice_p;
10317 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10318 parser->in_template_argument_list_p = true;
10319 /* Even if the template-id appears in an integral
10320 constant-expression, the contents of the argument list do
10322 saved_ice_p = parser->integral_constant_expression_p;
10323 parser->integral_constant_expression_p = false;
10324 saved_non_ice_p = parser->non_integral_constant_expression_p;
10325 parser->non_integral_constant_expression_p = false;
10326 /* Parse the arguments. */
10332 /* Consume the comma. */
10333 cp_lexer_consume_token (parser->lexer);
10335 /* Parse the template-argument. */
10336 argument = cp_parser_template_argument (parser);
10338 /* If the next token is an ellipsis, we're expanding a template
10340 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10342 /* Consume the `...' token. */
10343 cp_lexer_consume_token (parser->lexer);
10345 /* Make the argument into a TYPE_PACK_EXPANSION or
10346 EXPR_PACK_EXPANSION. */
10347 argument = make_pack_expansion (argument);
10350 if (n_args == alloced)
10354 if (arg_ary == fixed_args)
10356 arg_ary = XNEWVEC (tree, alloced);
10357 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10360 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10362 arg_ary[n_args++] = argument;
10364 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10366 vec = make_tree_vec (n_args);
10369 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10371 if (arg_ary != fixed_args)
10373 parser->non_integral_constant_expression_p = saved_non_ice_p;
10374 parser->integral_constant_expression_p = saved_ice_p;
10375 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10379 /* Parse a template-argument.
10382 assignment-expression
10386 The representation is that of an assignment-expression, type-id, or
10387 id-expression -- except that the qualified id-expression is
10388 evaluated, so that the value returned is either a DECL or an
10391 Although the standard says "assignment-expression", it forbids
10392 throw-expressions or assignments in the template argument.
10393 Therefore, we use "conditional-expression" instead. */
10396 cp_parser_template_argument (cp_parser* parser)
10401 bool maybe_type_id = false;
10402 cp_token *token = NULL, *argument_start_token = NULL;
10405 /* There's really no way to know what we're looking at, so we just
10406 try each alternative in order.
10410 In a template-argument, an ambiguity between a type-id and an
10411 expression is resolved to a type-id, regardless of the form of
10412 the corresponding template-parameter.
10414 Therefore, we try a type-id first. */
10415 cp_parser_parse_tentatively (parser);
10416 argument = cp_parser_type_id (parser);
10417 /* If there was no error parsing the type-id but the next token is a
10418 '>>', our behavior depends on which dialect of C++ we're
10419 parsing. In C++98, we probably found a typo for '> >'. But there
10420 are type-id which are also valid expressions. For instance:
10422 struct X { int operator >> (int); };
10423 template <int V> struct Foo {};
10426 Here 'X()' is a valid type-id of a function type, but the user just
10427 wanted to write the expression "X() >> 5". Thus, we remember that we
10428 found a valid type-id, but we still try to parse the argument as an
10429 expression to see what happens.
10431 In C++0x, the '>>' will be considered two separate '>'
10433 if (!cp_parser_error_occurred (parser)
10434 && cxx_dialect == cxx98
10435 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10437 maybe_type_id = true;
10438 cp_parser_abort_tentative_parse (parser);
10442 /* If the next token isn't a `,' or a `>', then this argument wasn't
10443 really finished. This means that the argument is not a valid
10445 if (!cp_parser_next_token_ends_template_argument_p (parser))
10446 cp_parser_error (parser, "expected template-argument");
10447 /* If that worked, we're done. */
10448 if (cp_parser_parse_definitely (parser))
10451 /* We're still not sure what the argument will be. */
10452 cp_parser_parse_tentatively (parser);
10453 /* Try a template. */
10454 argument_start_token = cp_lexer_peek_token (parser->lexer);
10455 argument = cp_parser_id_expression (parser,
10456 /*template_keyword_p=*/false,
10457 /*check_dependency_p=*/true,
10459 /*declarator_p=*/false,
10460 /*optional_p=*/false);
10461 /* If the next token isn't a `,' or a `>', then this argument wasn't
10462 really finished. */
10463 if (!cp_parser_next_token_ends_template_argument_p (parser))
10464 cp_parser_error (parser, "expected template-argument");
10465 if (!cp_parser_error_occurred (parser))
10467 /* Figure out what is being referred to. If the id-expression
10468 was for a class template specialization, then we will have a
10469 TYPE_DECL at this point. There is no need to do name lookup
10470 at this point in that case. */
10471 if (TREE_CODE (argument) != TYPE_DECL)
10472 argument = cp_parser_lookup_name (parser, argument,
10474 /*is_template=*/template_p,
10475 /*is_namespace=*/false,
10476 /*check_dependency=*/true,
10477 /*ambiguous_decls=*/NULL,
10478 argument_start_token->location);
10479 if (TREE_CODE (argument) != TEMPLATE_DECL
10480 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10481 cp_parser_error (parser, "expected template-name");
10483 if (cp_parser_parse_definitely (parser))
10485 /* It must be a non-type argument. There permitted cases are given
10486 in [temp.arg.nontype]:
10488 -- an integral constant-expression of integral or enumeration
10491 -- the name of a non-type template-parameter; or
10493 -- the name of an object or function with external linkage...
10495 -- the address of an object or function with external linkage...
10497 -- a pointer to member... */
10498 /* Look for a non-type template parameter. */
10499 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10501 cp_parser_parse_tentatively (parser);
10502 argument = cp_parser_primary_expression (parser,
10503 /*address_p=*/false,
10505 /*template_arg_p=*/true,
10507 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10508 || !cp_parser_next_token_ends_template_argument_p (parser))
10509 cp_parser_simulate_error (parser);
10510 if (cp_parser_parse_definitely (parser))
10514 /* If the next token is "&", the argument must be the address of an
10515 object or function with external linkage. */
10516 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10518 cp_lexer_consume_token (parser->lexer);
10519 /* See if we might have an id-expression. */
10520 token = cp_lexer_peek_token (parser->lexer);
10521 if (token->type == CPP_NAME
10522 || token->keyword == RID_OPERATOR
10523 || token->type == CPP_SCOPE
10524 || token->type == CPP_TEMPLATE_ID
10525 || token->type == CPP_NESTED_NAME_SPECIFIER)
10527 cp_parser_parse_tentatively (parser);
10528 argument = cp_parser_primary_expression (parser,
10531 /*template_arg_p=*/true,
10533 if (cp_parser_error_occurred (parser)
10534 || !cp_parser_next_token_ends_template_argument_p (parser))
10535 cp_parser_abort_tentative_parse (parser);
10538 if (TREE_CODE (argument) == INDIRECT_REF)
10540 gcc_assert (REFERENCE_REF_P (argument));
10541 argument = TREE_OPERAND (argument, 0);
10544 if (TREE_CODE (argument) == VAR_DECL)
10546 /* A variable without external linkage might still be a
10547 valid constant-expression, so no error is issued here
10548 if the external-linkage check fails. */
10549 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10550 cp_parser_simulate_error (parser);
10552 else if (is_overloaded_fn (argument))
10553 /* All overloaded functions are allowed; if the external
10554 linkage test does not pass, an error will be issued
10558 && (TREE_CODE (argument) == OFFSET_REF
10559 || TREE_CODE (argument) == SCOPE_REF))
10560 /* A pointer-to-member. */
10562 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10565 cp_parser_simulate_error (parser);
10567 if (cp_parser_parse_definitely (parser))
10570 argument = build_x_unary_op (ADDR_EXPR, argument,
10571 tf_warning_or_error);
10576 /* If the argument started with "&", there are no other valid
10577 alternatives at this point. */
10580 cp_parser_error (parser, "invalid non-type template argument");
10581 return error_mark_node;
10584 /* If the argument wasn't successfully parsed as a type-id followed
10585 by '>>', the argument can only be a constant expression now.
10586 Otherwise, we try parsing the constant-expression tentatively,
10587 because the argument could really be a type-id. */
10589 cp_parser_parse_tentatively (parser);
10590 argument = cp_parser_constant_expression (parser,
10591 /*allow_non_constant_p=*/false,
10592 /*non_constant_p=*/NULL);
10593 argument = fold_non_dependent_expr (argument);
10594 if (!maybe_type_id)
10596 if (!cp_parser_next_token_ends_template_argument_p (parser))
10597 cp_parser_error (parser, "expected template-argument");
10598 if (cp_parser_parse_definitely (parser))
10600 /* We did our best to parse the argument as a non type-id, but that
10601 was the only alternative that matched (albeit with a '>' after
10602 it). We can assume it's just a typo from the user, and a
10603 diagnostic will then be issued. */
10604 return cp_parser_type_id (parser);
10607 /* Parse an explicit-instantiation.
10609 explicit-instantiation:
10610 template declaration
10612 Although the standard says `declaration', what it really means is:
10614 explicit-instantiation:
10615 template decl-specifier-seq [opt] declarator [opt] ;
10617 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10618 supposed to be allowed. A defect report has been filed about this
10623 explicit-instantiation:
10624 storage-class-specifier template
10625 decl-specifier-seq [opt] declarator [opt] ;
10626 function-specifier template
10627 decl-specifier-seq [opt] declarator [opt] ; */
10630 cp_parser_explicit_instantiation (cp_parser* parser)
10632 int declares_class_or_enum;
10633 cp_decl_specifier_seq decl_specifiers;
10634 tree extension_specifier = NULL_TREE;
10637 /* Look for an (optional) storage-class-specifier or
10638 function-specifier. */
10639 if (cp_parser_allow_gnu_extensions_p (parser))
10641 extension_specifier
10642 = cp_parser_storage_class_specifier_opt (parser);
10643 if (!extension_specifier)
10644 extension_specifier
10645 = cp_parser_function_specifier_opt (parser,
10646 /*decl_specs=*/NULL);
10649 /* Look for the `template' keyword. */
10650 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10651 /* Let the front end know that we are processing an explicit
10653 begin_explicit_instantiation ();
10654 /* [temp.explicit] says that we are supposed to ignore access
10655 control while processing explicit instantiation directives. */
10656 push_deferring_access_checks (dk_no_check);
10657 /* Parse a decl-specifier-seq. */
10658 token = cp_lexer_peek_token (parser->lexer);
10659 cp_parser_decl_specifier_seq (parser,
10660 CP_PARSER_FLAGS_OPTIONAL,
10662 &declares_class_or_enum);
10663 /* If there was exactly one decl-specifier, and it declared a class,
10664 and there's no declarator, then we have an explicit type
10666 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10670 type = check_tag_decl (&decl_specifiers);
10671 /* Turn access control back on for names used during
10672 template instantiation. */
10673 pop_deferring_access_checks ();
10675 do_type_instantiation (type, extension_specifier,
10676 /*complain=*/tf_error);
10680 cp_declarator *declarator;
10683 /* Parse the declarator. */
10685 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10686 /*ctor_dtor_or_conv_p=*/NULL,
10687 /*parenthesized_p=*/NULL,
10688 /*member_p=*/false);
10689 if (declares_class_or_enum & 2)
10690 cp_parser_check_for_definition_in_return_type (declarator,
10691 decl_specifiers.type,
10692 decl_specifiers.type_location);
10693 if (declarator != cp_error_declarator)
10695 decl = grokdeclarator (declarator, &decl_specifiers,
10696 NORMAL, 0, &decl_specifiers.attributes);
10697 /* Turn access control back on for names used during
10698 template instantiation. */
10699 pop_deferring_access_checks ();
10700 /* Do the explicit instantiation. */
10701 do_decl_instantiation (decl, extension_specifier);
10705 pop_deferring_access_checks ();
10706 /* Skip the body of the explicit instantiation. */
10707 cp_parser_skip_to_end_of_statement (parser);
10710 /* We're done with the instantiation. */
10711 end_explicit_instantiation ();
10713 cp_parser_consume_semicolon_at_end_of_statement (parser);
10716 /* Parse an explicit-specialization.
10718 explicit-specialization:
10719 template < > declaration
10721 Although the standard says `declaration', what it really means is:
10723 explicit-specialization:
10724 template <> decl-specifier [opt] init-declarator [opt] ;
10725 template <> function-definition
10726 template <> explicit-specialization
10727 template <> template-declaration */
10730 cp_parser_explicit_specialization (cp_parser* parser)
10732 bool need_lang_pop;
10733 cp_token *token = cp_lexer_peek_token (parser->lexer);
10735 /* Look for the `template' keyword. */
10736 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10737 /* Look for the `<'. */
10738 cp_parser_require (parser, CPP_LESS, "%<<%>");
10739 /* Look for the `>'. */
10740 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10741 /* We have processed another parameter list. */
10742 ++parser->num_template_parameter_lists;
10745 A template ... explicit specialization ... shall not have C
10747 if (current_lang_name == lang_name_c)
10749 error ("%Htemplate specialization with C linkage", &token->location);
10750 /* Give it C++ linkage to avoid confusing other parts of the
10752 push_lang_context (lang_name_cplusplus);
10753 need_lang_pop = true;
10756 need_lang_pop = false;
10757 /* Let the front end know that we are beginning a specialization. */
10758 if (!begin_specialization ())
10760 end_specialization ();
10761 cp_parser_skip_to_end_of_block_or_statement (parser);
10765 /* If the next keyword is `template', we need to figure out whether
10766 or not we're looking a template-declaration. */
10767 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10769 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10770 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10771 cp_parser_template_declaration_after_export (parser,
10772 /*member_p=*/false);
10774 cp_parser_explicit_specialization (parser);
10777 /* Parse the dependent declaration. */
10778 cp_parser_single_declaration (parser,
10780 /*member_p=*/false,
10781 /*explicit_specialization_p=*/true,
10782 /*friend_p=*/NULL);
10783 /* We're done with the specialization. */
10784 end_specialization ();
10785 /* For the erroneous case of a template with C linkage, we pushed an
10786 implicit C++ linkage scope; exit that scope now. */
10788 pop_lang_context ();
10789 /* We're done with this parameter list. */
10790 --parser->num_template_parameter_lists;
10793 /* Parse a type-specifier.
10796 simple-type-specifier
10799 elaborated-type-specifier
10807 Returns a representation of the type-specifier. For a
10808 class-specifier, enum-specifier, or elaborated-type-specifier, a
10809 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10811 The parser flags FLAGS is used to control type-specifier parsing.
10813 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10814 in a decl-specifier-seq.
10816 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10817 class-specifier, enum-specifier, or elaborated-type-specifier, then
10818 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10819 if a type is declared; 2 if it is defined. Otherwise, it is set to
10822 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10823 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10824 is set to FALSE. */
10827 cp_parser_type_specifier (cp_parser* parser,
10828 cp_parser_flags flags,
10829 cp_decl_specifier_seq *decl_specs,
10830 bool is_declaration,
10831 int* declares_class_or_enum,
10832 bool* is_cv_qualifier)
10834 tree type_spec = NULL_TREE;
10837 cp_decl_spec ds = ds_last;
10839 /* Assume this type-specifier does not declare a new type. */
10840 if (declares_class_or_enum)
10841 *declares_class_or_enum = 0;
10842 /* And that it does not specify a cv-qualifier. */
10843 if (is_cv_qualifier)
10844 *is_cv_qualifier = false;
10845 /* Peek at the next token. */
10846 token = cp_lexer_peek_token (parser->lexer);
10848 /* If we're looking at a keyword, we can use that to guide the
10849 production we choose. */
10850 keyword = token->keyword;
10854 /* Look for the enum-specifier. */
10855 type_spec = cp_parser_enum_specifier (parser);
10856 /* If that worked, we're done. */
10859 if (declares_class_or_enum)
10860 *declares_class_or_enum = 2;
10862 cp_parser_set_decl_spec_type (decl_specs,
10865 /*user_defined_p=*/true);
10869 goto elaborated_type_specifier;
10871 /* Any of these indicate either a class-specifier, or an
10872 elaborated-type-specifier. */
10876 /* Parse tentatively so that we can back up if we don't find a
10877 class-specifier. */
10878 cp_parser_parse_tentatively (parser);
10879 /* Look for the class-specifier. */
10880 type_spec = cp_parser_class_specifier (parser);
10881 /* If that worked, we're done. */
10882 if (cp_parser_parse_definitely (parser))
10884 if (declares_class_or_enum)
10885 *declares_class_or_enum = 2;
10887 cp_parser_set_decl_spec_type (decl_specs,
10890 /*user_defined_p=*/true);
10894 /* Fall through. */
10895 elaborated_type_specifier:
10896 /* We're declaring (not defining) a class or enum. */
10897 if (declares_class_or_enum)
10898 *declares_class_or_enum = 1;
10900 /* Fall through. */
10902 /* Look for an elaborated-type-specifier. */
10904 = (cp_parser_elaborated_type_specifier
10906 decl_specs && decl_specs->specs[(int) ds_friend],
10909 cp_parser_set_decl_spec_type (decl_specs,
10912 /*user_defined_p=*/true);
10917 if (is_cv_qualifier)
10918 *is_cv_qualifier = true;
10923 if (is_cv_qualifier)
10924 *is_cv_qualifier = true;
10929 if (is_cv_qualifier)
10930 *is_cv_qualifier = true;
10934 /* The `__complex__' keyword is a GNU extension. */
10942 /* Handle simple keywords. */
10947 ++decl_specs->specs[(int)ds];
10948 decl_specs->any_specifiers_p = true;
10950 return cp_lexer_consume_token (parser->lexer)->u.value;
10953 /* If we do not already have a type-specifier, assume we are looking
10954 at a simple-type-specifier. */
10955 type_spec = cp_parser_simple_type_specifier (parser,
10959 /* If we didn't find a type-specifier, and a type-specifier was not
10960 optional in this context, issue an error message. */
10961 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10963 cp_parser_error (parser, "expected type specifier");
10964 return error_mark_node;
10970 /* Parse a simple-type-specifier.
10972 simple-type-specifier:
10973 :: [opt] nested-name-specifier [opt] type-name
10974 :: [opt] nested-name-specifier template template-id
10989 simple-type-specifier:
10991 decltype ( expression )
10997 simple-type-specifier:
10998 __typeof__ unary-expression
10999 __typeof__ ( type-id )
11001 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11002 appropriately updated. */
11005 cp_parser_simple_type_specifier (cp_parser* parser,
11006 cp_decl_specifier_seq *decl_specs,
11007 cp_parser_flags flags)
11009 tree type = NULL_TREE;
11012 /* Peek at the next token. */
11013 token = cp_lexer_peek_token (parser->lexer);
11015 /* If we're looking at a keyword, things are easy. */
11016 switch (token->keyword)
11020 decl_specs->explicit_char_p = true;
11021 type = char_type_node;
11024 type = char16_type_node;
11027 type = char32_type_node;
11030 type = wchar_type_node;
11033 type = boolean_type_node;
11037 ++decl_specs->specs[(int) ds_short];
11038 type = short_integer_type_node;
11042 decl_specs->explicit_int_p = true;
11043 type = integer_type_node;
11047 ++decl_specs->specs[(int) ds_long];
11048 type = long_integer_type_node;
11052 ++decl_specs->specs[(int) ds_signed];
11053 type = integer_type_node;
11057 ++decl_specs->specs[(int) ds_unsigned];
11058 type = unsigned_type_node;
11061 type = float_type_node;
11064 type = double_type_node;
11067 type = void_type_node;
11071 maybe_warn_cpp0x ("C++0x auto");
11072 type = make_auto ();
11076 /* Parse the `decltype' type. */
11077 type = cp_parser_decltype (parser);
11080 cp_parser_set_decl_spec_type (decl_specs, type,
11082 /*user_defined_p=*/true);
11087 /* Consume the `typeof' token. */
11088 cp_lexer_consume_token (parser->lexer);
11089 /* Parse the operand to `typeof'. */
11090 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11091 /* If it is not already a TYPE, take its type. */
11092 if (!TYPE_P (type))
11093 type = finish_typeof (type);
11096 cp_parser_set_decl_spec_type (decl_specs, type,
11098 /*user_defined_p=*/true);
11106 /* If the type-specifier was for a built-in type, we're done. */
11111 /* Record the type. */
11113 && (token->keyword != RID_SIGNED
11114 && token->keyword != RID_UNSIGNED
11115 && token->keyword != RID_SHORT
11116 && token->keyword != RID_LONG))
11117 cp_parser_set_decl_spec_type (decl_specs,
11120 /*user_defined=*/false);
11122 decl_specs->any_specifiers_p = true;
11124 /* Consume the token. */
11125 id = cp_lexer_consume_token (parser->lexer)->u.value;
11127 /* There is no valid C++ program where a non-template type is
11128 followed by a "<". That usually indicates that the user thought
11129 that the type was a template. */
11130 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11132 return TYPE_NAME (type);
11135 /* The type-specifier must be a user-defined type. */
11136 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11141 /* Don't gobble tokens or issue error messages if this is an
11142 optional type-specifier. */
11143 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11144 cp_parser_parse_tentatively (parser);
11146 /* Look for the optional `::' operator. */
11148 = (cp_parser_global_scope_opt (parser,
11149 /*current_scope_valid_p=*/false)
11151 /* Look for the nested-name specifier. */
11153 = (cp_parser_nested_name_specifier_opt (parser,
11154 /*typename_keyword_p=*/false,
11155 /*check_dependency_p=*/true,
11157 /*is_declaration=*/false)
11159 token = cp_lexer_peek_token (parser->lexer);
11160 /* If we have seen a nested-name-specifier, and the next token
11161 is `template', then we are using the template-id production. */
11163 && cp_parser_optional_template_keyword (parser))
11165 /* Look for the template-id. */
11166 type = cp_parser_template_id (parser,
11167 /*template_keyword_p=*/true,
11168 /*check_dependency_p=*/true,
11169 /*is_declaration=*/false);
11170 /* If the template-id did not name a type, we are out of
11172 if (TREE_CODE (type) != TYPE_DECL)
11174 cp_parser_error (parser, "expected template-id for type");
11178 /* Otherwise, look for a type-name. */
11180 type = cp_parser_type_name (parser);
11181 /* Keep track of all name-lookups performed in class scopes. */
11185 && TREE_CODE (type) == TYPE_DECL
11186 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11187 maybe_note_name_used_in_class (DECL_NAME (type), type);
11188 /* If it didn't work out, we don't have a TYPE. */
11189 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11190 && !cp_parser_parse_definitely (parser))
11192 if (type && decl_specs)
11193 cp_parser_set_decl_spec_type (decl_specs, type,
11195 /*user_defined=*/true);
11198 /* If we didn't get a type-name, issue an error message. */
11199 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11201 cp_parser_error (parser, "expected type-name");
11202 return error_mark_node;
11205 /* There is no valid C++ program where a non-template type is
11206 followed by a "<". That usually indicates that the user thought
11207 that the type was a template. */
11208 if (type && type != error_mark_node)
11210 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11211 If it is, then the '<'...'>' enclose protocol names rather than
11212 template arguments, and so everything is fine. */
11213 if (c_dialect_objc ()
11214 && (objc_is_id (type) || objc_is_class_name (type)))
11216 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11217 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11219 /* Clobber the "unqualified" type previously entered into
11220 DECL_SPECS with the new, improved protocol-qualified version. */
11222 decl_specs->type = qual_type;
11227 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11234 /* Parse a type-name.
11247 Returns a TYPE_DECL for the type. */
11250 cp_parser_type_name (cp_parser* parser)
11254 /* We can't know yet whether it is a class-name or not. */
11255 cp_parser_parse_tentatively (parser);
11256 /* Try a class-name. */
11257 type_decl = cp_parser_class_name (parser,
11258 /*typename_keyword_p=*/false,
11259 /*template_keyword_p=*/false,
11261 /*check_dependency_p=*/true,
11262 /*class_head_p=*/false,
11263 /*is_declaration=*/false);
11264 /* If it's not a class-name, keep looking. */
11265 if (!cp_parser_parse_definitely (parser))
11267 /* It must be a typedef-name or an enum-name. */
11268 return cp_parser_nonclass_name (parser);
11274 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11282 Returns a TYPE_DECL for the type. */
11285 cp_parser_nonclass_name (cp_parser* parser)
11290 cp_token *token = cp_lexer_peek_token (parser->lexer);
11291 identifier = cp_parser_identifier (parser);
11292 if (identifier == error_mark_node)
11293 return error_mark_node;
11295 /* Look up the type-name. */
11296 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11298 if (TREE_CODE (type_decl) != TYPE_DECL
11299 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11301 /* See if this is an Objective-C type. */
11302 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11303 tree type = objc_get_protocol_qualified_type (identifier, protos);
11305 type_decl = TYPE_NAME (type);
11308 /* Issue an error if we did not find a type-name. */
11309 if (TREE_CODE (type_decl) != TYPE_DECL)
11311 if (!cp_parser_simulate_error (parser))
11312 cp_parser_name_lookup_error (parser, identifier, type_decl,
11313 "is not a type", token->location);
11314 return error_mark_node;
11316 /* Remember that the name was used in the definition of the
11317 current class so that we can check later to see if the
11318 meaning would have been different after the class was
11319 entirely defined. */
11320 else if (type_decl != error_mark_node
11322 maybe_note_name_used_in_class (identifier, type_decl);
11327 /* Parse an elaborated-type-specifier. Note that the grammar given
11328 here incorporates the resolution to DR68.
11330 elaborated-type-specifier:
11331 class-key :: [opt] nested-name-specifier [opt] identifier
11332 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11333 enum-key :: [opt] nested-name-specifier [opt] identifier
11334 typename :: [opt] nested-name-specifier identifier
11335 typename :: [opt] nested-name-specifier template [opt]
11340 elaborated-type-specifier:
11341 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11342 class-key attributes :: [opt] nested-name-specifier [opt]
11343 template [opt] template-id
11344 enum attributes :: [opt] nested-name-specifier [opt] identifier
11346 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11347 declared `friend'. If IS_DECLARATION is TRUE, then this
11348 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11349 something is being declared.
11351 Returns the TYPE specified. */
11354 cp_parser_elaborated_type_specifier (cp_parser* parser,
11356 bool is_declaration)
11358 enum tag_types tag_type;
11360 tree type = NULL_TREE;
11361 tree attributes = NULL_TREE;
11362 cp_token *token = NULL;
11364 /* See if we're looking at the `enum' keyword. */
11365 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11367 /* Consume the `enum' token. */
11368 cp_lexer_consume_token (parser->lexer);
11369 /* Remember that it's an enumeration type. */
11370 tag_type = enum_type;
11371 /* Parse the optional `struct' or `class' key (for C++0x scoped
11373 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11374 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11376 if (cxx_dialect == cxx98)
11377 maybe_warn_cpp0x ("scoped enums");
11379 /* Consume the `struct' or `class'. */
11380 cp_lexer_consume_token (parser->lexer);
11382 /* Parse the attributes. */
11383 attributes = cp_parser_attributes_opt (parser);
11385 /* Or, it might be `typename'. */
11386 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11389 /* Consume the `typename' token. */
11390 cp_lexer_consume_token (parser->lexer);
11391 /* Remember that it's a `typename' type. */
11392 tag_type = typename_type;
11393 /* The `typename' keyword is only allowed in templates. */
11394 if (!processing_template_decl)
11395 permerror (input_location, "using %<typename%> outside of template");
11397 /* Otherwise it must be a class-key. */
11400 tag_type = cp_parser_class_key (parser);
11401 if (tag_type == none_type)
11402 return error_mark_node;
11403 /* Parse the attributes. */
11404 attributes = cp_parser_attributes_opt (parser);
11407 /* Look for the `::' operator. */
11408 cp_parser_global_scope_opt (parser,
11409 /*current_scope_valid_p=*/false);
11410 /* Look for the nested-name-specifier. */
11411 if (tag_type == typename_type)
11413 if (!cp_parser_nested_name_specifier (parser,
11414 /*typename_keyword_p=*/true,
11415 /*check_dependency_p=*/true,
11418 return error_mark_node;
11421 /* Even though `typename' is not present, the proposed resolution
11422 to Core Issue 180 says that in `class A<T>::B', `B' should be
11423 considered a type-name, even if `A<T>' is dependent. */
11424 cp_parser_nested_name_specifier_opt (parser,
11425 /*typename_keyword_p=*/true,
11426 /*check_dependency_p=*/true,
11429 /* For everything but enumeration types, consider a template-id.
11430 For an enumeration type, consider only a plain identifier. */
11431 if (tag_type != enum_type)
11433 bool template_p = false;
11436 /* Allow the `template' keyword. */
11437 template_p = cp_parser_optional_template_keyword (parser);
11438 /* If we didn't see `template', we don't know if there's a
11439 template-id or not. */
11441 cp_parser_parse_tentatively (parser);
11442 /* Parse the template-id. */
11443 token = cp_lexer_peek_token (parser->lexer);
11444 decl = cp_parser_template_id (parser, template_p,
11445 /*check_dependency_p=*/true,
11447 /* If we didn't find a template-id, look for an ordinary
11449 if (!template_p && !cp_parser_parse_definitely (parser))
11451 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11452 in effect, then we must assume that, upon instantiation, the
11453 template will correspond to a class. */
11454 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11455 && tag_type == typename_type)
11456 type = make_typename_type (parser->scope, decl,
11458 /*complain=*/tf_error);
11460 type = TREE_TYPE (decl);
11465 token = cp_lexer_peek_token (parser->lexer);
11466 identifier = cp_parser_identifier (parser);
11468 if (identifier == error_mark_node)
11470 parser->scope = NULL_TREE;
11471 return error_mark_node;
11474 /* For a `typename', we needn't call xref_tag. */
11475 if (tag_type == typename_type
11476 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11477 return cp_parser_make_typename_type (parser, parser->scope,
11480 /* Look up a qualified name in the usual way. */
11484 tree ambiguous_decls;
11486 decl = cp_parser_lookup_name (parser, identifier,
11488 /*is_template=*/false,
11489 /*is_namespace=*/false,
11490 /*check_dependency=*/true,
11494 /* If the lookup was ambiguous, an error will already have been
11496 if (ambiguous_decls)
11497 return error_mark_node;
11499 /* If we are parsing friend declaration, DECL may be a
11500 TEMPLATE_DECL tree node here. However, we need to check
11501 whether this TEMPLATE_DECL results in valid code. Consider
11502 the following example:
11505 template <class T> class C {};
11508 template <class T> friend class N::C; // #1, valid code
11510 template <class T> class Y {
11511 friend class N::C; // #2, invalid code
11514 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11515 name lookup of `N::C'. We see that friend declaration must
11516 be template for the code to be valid. Note that
11517 processing_template_decl does not work here since it is
11518 always 1 for the above two cases. */
11520 decl = (cp_parser_maybe_treat_template_as_class
11521 (decl, /*tag_name_p=*/is_friend
11522 && parser->num_template_parameter_lists));
11524 if (TREE_CODE (decl) != TYPE_DECL)
11526 cp_parser_diagnose_invalid_type_name (parser,
11530 return error_mark_node;
11533 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11535 bool allow_template = (parser->num_template_parameter_lists
11536 || DECL_SELF_REFERENCE_P (decl));
11537 type = check_elaborated_type_specifier (tag_type, decl,
11540 if (type == error_mark_node)
11541 return error_mark_node;
11544 /* Forward declarations of nested types, such as
11549 are invalid unless all components preceding the final '::'
11550 are complete. If all enclosing types are complete, these
11551 declarations become merely pointless.
11553 Invalid forward declarations of nested types are errors
11554 caught elsewhere in parsing. Those that are pointless arrive
11557 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11558 && !is_friend && !processing_explicit_instantiation)
11559 warning (0, "declaration %qD does not declare anything", decl);
11561 type = TREE_TYPE (decl);
11565 /* An elaborated-type-specifier sometimes introduces a new type and
11566 sometimes names an existing type. Normally, the rule is that it
11567 introduces a new type only if there is not an existing type of
11568 the same name already in scope. For example, given:
11571 void f() { struct S s; }
11573 the `struct S' in the body of `f' is the same `struct S' as in
11574 the global scope; the existing definition is used. However, if
11575 there were no global declaration, this would introduce a new
11576 local class named `S'.
11578 An exception to this rule applies to the following code:
11580 namespace N { struct S; }
11582 Here, the elaborated-type-specifier names a new type
11583 unconditionally; even if there is already an `S' in the
11584 containing scope this declaration names a new type.
11585 This exception only applies if the elaborated-type-specifier
11586 forms the complete declaration:
11590 A declaration consisting solely of `class-key identifier ;' is
11591 either a redeclaration of the name in the current scope or a
11592 forward declaration of the identifier as a class name. It
11593 introduces the name into the current scope.
11595 We are in this situation precisely when the next token is a `;'.
11597 An exception to the exception is that a `friend' declaration does
11598 *not* name a new type; i.e., given:
11600 struct S { friend struct T; };
11602 `T' is not a new type in the scope of `S'.
11604 Also, `new struct S' or `sizeof (struct S)' never results in the
11605 definition of a new type; a new type can only be declared in a
11606 declaration context. */
11612 /* Friends have special name lookup rules. */
11613 ts = ts_within_enclosing_non_class;
11614 else if (is_declaration
11615 && cp_lexer_next_token_is (parser->lexer,
11617 /* This is a `class-key identifier ;' */
11623 (parser->num_template_parameter_lists
11624 && (cp_parser_next_token_starts_class_definition_p (parser)
11625 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11626 /* An unqualified name was used to reference this type, so
11627 there were no qualifying templates. */
11628 if (!cp_parser_check_template_parameters (parser,
11629 /*num_templates=*/0,
11631 return error_mark_node;
11632 type = xref_tag (tag_type, identifier, ts, template_p);
11636 if (type == error_mark_node)
11637 return error_mark_node;
11639 /* Allow attributes on forward declarations of classes. */
11642 if (TREE_CODE (type) == TYPENAME_TYPE)
11643 warning (OPT_Wattributes,
11644 "attributes ignored on uninstantiated type");
11645 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11646 && ! processing_explicit_instantiation)
11647 warning (OPT_Wattributes,
11648 "attributes ignored on template instantiation");
11649 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11650 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11652 warning (OPT_Wattributes,
11653 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11656 if (tag_type != enum_type)
11657 cp_parser_check_class_key (tag_type, type);
11659 /* A "<" cannot follow an elaborated type specifier. If that
11660 happens, the user was probably trying to form a template-id. */
11661 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11666 /* Parse an enum-specifier.
11669 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11674 enum struct [C++0x]
11677 : type-specifier-seq
11680 enum-key attributes[opt] identifier [opt] enum-base [opt]
11681 { enumerator-list [opt] }attributes[opt]
11683 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11684 if the token stream isn't an enum-specifier after all. */
11687 cp_parser_enum_specifier (cp_parser* parser)
11692 bool scoped_enum_p = false;
11693 tree underlying_type = NULL_TREE;
11695 /* Parse tentatively so that we can back up if we don't find a
11697 cp_parser_parse_tentatively (parser);
11699 /* Caller guarantees that the current token is 'enum', an identifier
11700 possibly follows, and the token after that is an opening brace.
11701 If we don't have an identifier, fabricate an anonymous name for
11702 the enumeration being defined. */
11703 cp_lexer_consume_token (parser->lexer);
11705 /* Parse the "class" or "struct", which indicates a scoped
11706 enumeration type in C++0x. */
11707 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11708 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11710 if (cxx_dialect == cxx98)
11711 maybe_warn_cpp0x ("scoped enums");
11713 /* Consume the `struct' or `class' token. */
11714 cp_lexer_consume_token (parser->lexer);
11716 scoped_enum_p = true;
11719 attributes = cp_parser_attributes_opt (parser);
11721 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11722 identifier = cp_parser_identifier (parser);
11724 identifier = make_anon_name ();
11726 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11727 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11729 cp_decl_specifier_seq type_specifiers;
11731 if (cxx_dialect == cxx98)
11732 maybe_warn_cpp0x ("scoped enums");
11734 /* Consume the `:'. */
11735 cp_lexer_consume_token (parser->lexer);
11737 /* Parse the type-specifier-seq. */
11738 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11740 if (type_specifiers.type == error_mark_node)
11741 return error_mark_node;
11743 /* If that didn't work, stop. */
11744 if (type_specifiers.type != error_mark_node)
11746 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11747 /*initialized=*/0, NULL);
11748 if (underlying_type == error_mark_node)
11749 underlying_type = NULL_TREE;
11752 cp_parser_error (parser, "expected underlying type of enumeration");
11755 /* Look for the `{' but don't consume it yet. */
11756 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11757 cp_parser_simulate_error (parser);
11759 if (!cp_parser_parse_definitely (parser))
11762 /* Issue an error message if type-definitions are forbidden here. */
11763 if (!cp_parser_check_type_definition (parser))
11764 type = error_mark_node;
11766 /* Create the new type. We do this before consuming the opening
11767 brace so the enum will be recorded as being on the line of its
11768 tag (or the 'enum' keyword, if there is no tag). */
11769 type = start_enum (identifier, underlying_type, scoped_enum_p);
11771 /* Consume the opening brace. */
11772 cp_lexer_consume_token (parser->lexer);
11774 if (type == error_mark_node)
11776 cp_parser_skip_to_end_of_block_or_statement (parser);
11777 return error_mark_node;
11780 /* If the next token is not '}', then there are some enumerators. */
11781 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11782 cp_parser_enumerator_list (parser, type);
11784 /* Consume the final '}'. */
11785 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11787 /* Look for trailing attributes to apply to this enumeration, and
11788 apply them if appropriate. */
11789 if (cp_parser_allow_gnu_extensions_p (parser))
11791 tree trailing_attr = cp_parser_attributes_opt (parser);
11792 cplus_decl_attributes (&type,
11794 (int) ATTR_FLAG_TYPE_IN_PLACE);
11797 /* Finish up the enumeration. */
11798 finish_enum (type);
11803 /* Parse an enumerator-list. The enumerators all have the indicated
11807 enumerator-definition
11808 enumerator-list , enumerator-definition */
11811 cp_parser_enumerator_list (cp_parser* parser, tree type)
11815 /* Parse an enumerator-definition. */
11816 cp_parser_enumerator_definition (parser, type);
11818 /* If the next token is not a ',', we've reached the end of
11820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11822 /* Otherwise, consume the `,' and keep going. */
11823 cp_lexer_consume_token (parser->lexer);
11824 /* If the next token is a `}', there is a trailing comma. */
11825 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11827 if (!in_system_header)
11828 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11834 /* Parse an enumerator-definition. The enumerator has the indicated
11837 enumerator-definition:
11839 enumerator = constant-expression
11845 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11850 /* Look for the identifier. */
11851 identifier = cp_parser_identifier (parser);
11852 if (identifier == error_mark_node)
11855 /* If the next token is an '=', then there is an explicit value. */
11856 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11858 /* Consume the `=' token. */
11859 cp_lexer_consume_token (parser->lexer);
11860 /* Parse the value. */
11861 value = cp_parser_constant_expression (parser,
11862 /*allow_non_constant_p=*/false,
11868 /* Create the enumerator. */
11869 build_enumerator (identifier, value, type);
11872 /* Parse a namespace-name.
11875 original-namespace-name
11878 Returns the NAMESPACE_DECL for the namespace. */
11881 cp_parser_namespace_name (cp_parser* parser)
11884 tree namespace_decl;
11886 cp_token *token = cp_lexer_peek_token (parser->lexer);
11888 /* Get the name of the namespace. */
11889 identifier = cp_parser_identifier (parser);
11890 if (identifier == error_mark_node)
11891 return error_mark_node;
11893 /* Look up the identifier in the currently active scope. Look only
11894 for namespaces, due to:
11896 [basic.lookup.udir]
11898 When looking up a namespace-name in a using-directive or alias
11899 definition, only namespace names are considered.
11903 [basic.lookup.qual]
11905 During the lookup of a name preceding the :: scope resolution
11906 operator, object, function, and enumerator names are ignored.
11908 (Note that cp_parser_qualifying_entity only calls this
11909 function if the token after the name is the scope resolution
11911 namespace_decl = cp_parser_lookup_name (parser, identifier,
11913 /*is_template=*/false,
11914 /*is_namespace=*/true,
11915 /*check_dependency=*/true,
11916 /*ambiguous_decls=*/NULL,
11918 /* If it's not a namespace, issue an error. */
11919 if (namespace_decl == error_mark_node
11920 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11922 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11923 error ("%H%qD is not a namespace-name", &token->location, identifier);
11924 cp_parser_error (parser, "expected namespace-name");
11925 namespace_decl = error_mark_node;
11928 return namespace_decl;
11931 /* Parse a namespace-definition.
11933 namespace-definition:
11934 named-namespace-definition
11935 unnamed-namespace-definition
11937 named-namespace-definition:
11938 original-namespace-definition
11939 extension-namespace-definition
11941 original-namespace-definition:
11942 namespace identifier { namespace-body }
11944 extension-namespace-definition:
11945 namespace original-namespace-name { namespace-body }
11947 unnamed-namespace-definition:
11948 namespace { namespace-body } */
11951 cp_parser_namespace_definition (cp_parser* parser)
11953 tree identifier, attribs;
11954 bool has_visibility;
11957 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11960 cp_lexer_consume_token (parser->lexer);
11965 /* Look for the `namespace' keyword. */
11966 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11968 /* Get the name of the namespace. We do not attempt to distinguish
11969 between an original-namespace-definition and an
11970 extension-namespace-definition at this point. The semantic
11971 analysis routines are responsible for that. */
11972 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11973 identifier = cp_parser_identifier (parser);
11975 identifier = NULL_TREE;
11977 /* Parse any specified attributes. */
11978 attribs = cp_parser_attributes_opt (parser);
11980 /* Look for the `{' to start the namespace. */
11981 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11982 /* Start the namespace. */
11983 push_namespace (identifier);
11985 /* "inline namespace" is equivalent to a stub namespace definition
11986 followed by a strong using directive. */
11989 tree name_space = current_namespace;
11990 /* Set up namespace association. */
11991 DECL_NAMESPACE_ASSOCIATIONS (name_space)
11992 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
11993 DECL_NAMESPACE_ASSOCIATIONS (name_space));
11994 /* Import the contents of the inline namespace. */
11996 do_using_directive (name_space);
11997 push_namespace (identifier);
12000 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12002 /* Parse the body of the namespace. */
12003 cp_parser_namespace_body (parser);
12005 #ifdef HANDLE_PRAGMA_VISIBILITY
12006 if (has_visibility)
12010 /* Finish the namespace. */
12012 /* Look for the final `}'. */
12013 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12016 /* Parse a namespace-body.
12019 declaration-seq [opt] */
12022 cp_parser_namespace_body (cp_parser* parser)
12024 cp_parser_declaration_seq_opt (parser);
12027 /* Parse a namespace-alias-definition.
12029 namespace-alias-definition:
12030 namespace identifier = qualified-namespace-specifier ; */
12033 cp_parser_namespace_alias_definition (cp_parser* parser)
12036 tree namespace_specifier;
12038 cp_token *token = cp_lexer_peek_token (parser->lexer);
12040 /* Look for the `namespace' keyword. */
12041 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12042 /* Look for the identifier. */
12043 identifier = cp_parser_identifier (parser);
12044 if (identifier == error_mark_node)
12046 /* Look for the `=' token. */
12047 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12048 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12050 error ("%H%<namespace%> definition is not allowed here", &token->location);
12051 /* Skip the definition. */
12052 cp_lexer_consume_token (parser->lexer);
12053 if (cp_parser_skip_to_closing_brace (parser))
12054 cp_lexer_consume_token (parser->lexer);
12057 cp_parser_require (parser, CPP_EQ, "%<=%>");
12058 /* Look for the qualified-namespace-specifier. */
12059 namespace_specifier
12060 = cp_parser_qualified_namespace_specifier (parser);
12061 /* Look for the `;' token. */
12062 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12064 /* Register the alias in the symbol table. */
12065 do_namespace_alias (identifier, namespace_specifier);
12068 /* Parse a qualified-namespace-specifier.
12070 qualified-namespace-specifier:
12071 :: [opt] nested-name-specifier [opt] namespace-name
12073 Returns a NAMESPACE_DECL corresponding to the specified
12077 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12079 /* Look for the optional `::'. */
12080 cp_parser_global_scope_opt (parser,
12081 /*current_scope_valid_p=*/false);
12083 /* Look for the optional nested-name-specifier. */
12084 cp_parser_nested_name_specifier_opt (parser,
12085 /*typename_keyword_p=*/false,
12086 /*check_dependency_p=*/true,
12088 /*is_declaration=*/true);
12090 return cp_parser_namespace_name (parser);
12093 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12094 access declaration.
12097 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12098 using :: unqualified-id ;
12100 access-declaration:
12106 cp_parser_using_declaration (cp_parser* parser,
12107 bool access_declaration_p)
12110 bool typename_p = false;
12111 bool global_scope_p;
12116 if (access_declaration_p)
12117 cp_parser_parse_tentatively (parser);
12120 /* Look for the `using' keyword. */
12121 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12123 /* Peek at the next token. */
12124 token = cp_lexer_peek_token (parser->lexer);
12125 /* See if it's `typename'. */
12126 if (token->keyword == RID_TYPENAME)
12128 /* Remember that we've seen it. */
12130 /* Consume the `typename' token. */
12131 cp_lexer_consume_token (parser->lexer);
12135 /* Look for the optional global scope qualification. */
12137 = (cp_parser_global_scope_opt (parser,
12138 /*current_scope_valid_p=*/false)
12141 /* If we saw `typename', or didn't see `::', then there must be a
12142 nested-name-specifier present. */
12143 if (typename_p || !global_scope_p)
12144 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12145 /*check_dependency_p=*/true,
12147 /*is_declaration=*/true);
12148 /* Otherwise, we could be in either of the two productions. In that
12149 case, treat the nested-name-specifier as optional. */
12151 qscope = cp_parser_nested_name_specifier_opt (parser,
12152 /*typename_keyword_p=*/false,
12153 /*check_dependency_p=*/true,
12155 /*is_declaration=*/true);
12157 qscope = global_namespace;
12159 if (access_declaration_p && cp_parser_error_occurred (parser))
12160 /* Something has already gone wrong; there's no need to parse
12161 further. Since an error has occurred, the return value of
12162 cp_parser_parse_definitely will be false, as required. */
12163 return cp_parser_parse_definitely (parser);
12165 token = cp_lexer_peek_token (parser->lexer);
12166 /* Parse the unqualified-id. */
12167 identifier = cp_parser_unqualified_id (parser,
12168 /*template_keyword_p=*/false,
12169 /*check_dependency_p=*/true,
12170 /*declarator_p=*/true,
12171 /*optional_p=*/false);
12173 if (access_declaration_p)
12175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12176 cp_parser_simulate_error (parser);
12177 if (!cp_parser_parse_definitely (parser))
12181 /* The function we call to handle a using-declaration is different
12182 depending on what scope we are in. */
12183 if (qscope == error_mark_node || identifier == error_mark_node)
12185 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12186 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12187 /* [namespace.udecl]
12189 A using declaration shall not name a template-id. */
12190 error ("%Ha template-id may not appear in a using-declaration",
12194 if (at_class_scope_p ())
12196 /* Create the USING_DECL. */
12197 decl = do_class_using_decl (parser->scope, identifier);
12199 if (check_for_bare_parameter_packs (decl))
12202 /* Add it to the list of members in this class. */
12203 finish_member_declaration (decl);
12207 decl = cp_parser_lookup_name_simple (parser,
12210 if (decl == error_mark_node)
12211 cp_parser_name_lookup_error (parser, identifier,
12214 else if (check_for_bare_parameter_packs (decl))
12216 else if (!at_namespace_scope_p ())
12217 do_local_using_decl (decl, qscope, identifier);
12219 do_toplevel_using_decl (decl, qscope, identifier);
12223 /* Look for the final `;'. */
12224 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12229 /* Parse a using-directive.
12232 using namespace :: [opt] nested-name-specifier [opt]
12233 namespace-name ; */
12236 cp_parser_using_directive (cp_parser* parser)
12238 tree namespace_decl;
12241 /* Look for the `using' keyword. */
12242 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12243 /* And the `namespace' keyword. */
12244 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12245 /* Look for the optional `::' operator. */
12246 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12247 /* And the optional nested-name-specifier. */
12248 cp_parser_nested_name_specifier_opt (parser,
12249 /*typename_keyword_p=*/false,
12250 /*check_dependency_p=*/true,
12252 /*is_declaration=*/true);
12253 /* Get the namespace being used. */
12254 namespace_decl = cp_parser_namespace_name (parser);
12255 /* And any specified attributes. */
12256 attribs = cp_parser_attributes_opt (parser);
12257 /* Update the symbol table. */
12258 parse_using_directive (namespace_decl, attribs);
12259 /* Look for the final `;'. */
12260 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12263 /* Parse an asm-definition.
12266 asm ( string-literal ) ;
12271 asm volatile [opt] ( string-literal ) ;
12272 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12273 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12274 : asm-operand-list [opt] ) ;
12275 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12276 : asm-operand-list [opt]
12277 : asm-operand-list [opt] ) ; */
12280 cp_parser_asm_definition (cp_parser* parser)
12283 tree outputs = NULL_TREE;
12284 tree inputs = NULL_TREE;
12285 tree clobbers = NULL_TREE;
12287 bool volatile_p = false;
12288 bool extended_p = false;
12289 bool invalid_inputs_p = false;
12290 bool invalid_outputs_p = false;
12292 /* Look for the `asm' keyword. */
12293 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12294 /* See if the next token is `volatile'. */
12295 if (cp_parser_allow_gnu_extensions_p (parser)
12296 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12298 /* Remember that we saw the `volatile' keyword. */
12300 /* Consume the token. */
12301 cp_lexer_consume_token (parser->lexer);
12303 /* Look for the opening `('. */
12304 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12306 /* Look for the string. */
12307 string = cp_parser_string_literal (parser, false, false);
12308 if (string == error_mark_node)
12310 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12311 /*consume_paren=*/true);
12315 /* If we're allowing GNU extensions, check for the extended assembly
12316 syntax. Unfortunately, the `:' tokens need not be separated by
12317 a space in C, and so, for compatibility, we tolerate that here
12318 too. Doing that means that we have to treat the `::' operator as
12320 if (cp_parser_allow_gnu_extensions_p (parser)
12321 && parser->in_function_body
12322 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12323 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12325 bool inputs_p = false;
12326 bool clobbers_p = false;
12328 /* The extended syntax was used. */
12331 /* Look for outputs. */
12332 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12334 /* Consume the `:'. */
12335 cp_lexer_consume_token (parser->lexer);
12336 /* Parse the output-operands. */
12337 if (cp_lexer_next_token_is_not (parser->lexer,
12339 && cp_lexer_next_token_is_not (parser->lexer,
12341 && cp_lexer_next_token_is_not (parser->lexer,
12343 outputs = cp_parser_asm_operand_list (parser);
12345 if (outputs == error_mark_node)
12346 invalid_outputs_p = true;
12348 /* If the next token is `::', there are no outputs, and the
12349 next token is the beginning of the inputs. */
12350 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12351 /* The inputs are coming next. */
12354 /* Look for inputs. */
12356 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12358 /* Consume the `:' or `::'. */
12359 cp_lexer_consume_token (parser->lexer);
12360 /* Parse the output-operands. */
12361 if (cp_lexer_next_token_is_not (parser->lexer,
12363 && cp_lexer_next_token_is_not (parser->lexer,
12365 inputs = cp_parser_asm_operand_list (parser);
12367 if (inputs == error_mark_node)
12368 invalid_inputs_p = true;
12370 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12371 /* The clobbers are coming next. */
12374 /* Look for clobbers. */
12376 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12378 /* Consume the `:' or `::'. */
12379 cp_lexer_consume_token (parser->lexer);
12380 /* Parse the clobbers. */
12381 if (cp_lexer_next_token_is_not (parser->lexer,
12383 clobbers = cp_parser_asm_clobber_list (parser);
12386 /* Look for the closing `)'. */
12387 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12388 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12389 /*consume_paren=*/true);
12390 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12392 if (!invalid_inputs_p && !invalid_outputs_p)
12394 /* Create the ASM_EXPR. */
12395 if (parser->in_function_body)
12397 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12399 /* If the extended syntax was not used, mark the ASM_EXPR. */
12402 tree temp = asm_stmt;
12403 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12404 temp = TREE_OPERAND (temp, 0);
12406 ASM_INPUT_P (temp) = 1;
12410 cgraph_add_asm_node (string);
12414 /* Declarators [gram.dcl.decl] */
12416 /* Parse an init-declarator.
12419 declarator initializer [opt]
12424 declarator asm-specification [opt] attributes [opt] initializer [opt]
12426 function-definition:
12427 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12429 decl-specifier-seq [opt] declarator function-try-block
12433 function-definition:
12434 __extension__ function-definition
12436 The DECL_SPECIFIERS apply to this declarator. Returns a
12437 representation of the entity declared. If MEMBER_P is TRUE, then
12438 this declarator appears in a class scope. The new DECL created by
12439 this declarator is returned.
12441 The CHECKS are access checks that should be performed once we know
12442 what entity is being declared (and, therefore, what classes have
12445 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12446 for a function-definition here as well. If the declarator is a
12447 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12448 be TRUE upon return. By that point, the function-definition will
12449 have been completely parsed.
12451 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12455 cp_parser_init_declarator (cp_parser* parser,
12456 cp_decl_specifier_seq *decl_specifiers,
12457 VEC (deferred_access_check,gc)* checks,
12458 bool function_definition_allowed_p,
12460 int declares_class_or_enum,
12461 bool* function_definition_p)
12463 cp_token *token = NULL, *asm_spec_start_token = NULL,
12464 *attributes_start_token = NULL;
12465 cp_declarator *declarator;
12466 tree prefix_attributes;
12468 tree asm_specification;
12470 tree decl = NULL_TREE;
12472 int is_initialized;
12473 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12474 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12476 enum cpp_ttype initialization_kind;
12477 bool is_direct_init = false;
12478 bool is_non_constant_init;
12479 int ctor_dtor_or_conv_p;
12481 tree pushed_scope = NULL;
12483 /* Gather the attributes that were provided with the
12484 decl-specifiers. */
12485 prefix_attributes = decl_specifiers->attributes;
12487 /* Assume that this is not the declarator for a function
12489 if (function_definition_p)
12490 *function_definition_p = false;
12492 /* Defer access checks while parsing the declarator; we cannot know
12493 what names are accessible until we know what is being
12495 resume_deferring_access_checks ();
12497 /* Parse the declarator. */
12498 token = cp_lexer_peek_token (parser->lexer);
12500 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12501 &ctor_dtor_or_conv_p,
12502 /*parenthesized_p=*/NULL,
12503 /*member_p=*/false);
12504 /* Gather up the deferred checks. */
12505 stop_deferring_access_checks ();
12507 /* If the DECLARATOR was erroneous, there's no need to go
12509 if (declarator == cp_error_declarator)
12510 return error_mark_node;
12512 /* Check that the number of template-parameter-lists is OK. */
12513 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12515 return error_mark_node;
12517 if (declares_class_or_enum & 2)
12518 cp_parser_check_for_definition_in_return_type (declarator,
12519 decl_specifiers->type,
12520 decl_specifiers->type_location);
12522 /* Figure out what scope the entity declared by the DECLARATOR is
12523 located in. `grokdeclarator' sometimes changes the scope, so
12524 we compute it now. */
12525 scope = get_scope_of_declarator (declarator);
12527 /* If we're allowing GNU extensions, look for an asm-specification
12529 if (cp_parser_allow_gnu_extensions_p (parser))
12531 /* Look for an asm-specification. */
12532 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12533 asm_specification = cp_parser_asm_specification_opt (parser);
12534 /* And attributes. */
12535 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12536 attributes = cp_parser_attributes_opt (parser);
12540 asm_specification = NULL_TREE;
12541 attributes = NULL_TREE;
12544 /* Peek at the next token. */
12545 token = cp_lexer_peek_token (parser->lexer);
12546 /* Check to see if the token indicates the start of a
12547 function-definition. */
12548 if (function_declarator_p (declarator)
12549 && cp_parser_token_starts_function_definition_p (token))
12551 if (!function_definition_allowed_p)
12553 /* If a function-definition should not appear here, issue an
12555 cp_parser_error (parser,
12556 "a function-definition is not allowed here");
12557 return error_mark_node;
12561 /* Neither attributes nor an asm-specification are allowed
12562 on a function-definition. */
12563 if (asm_specification)
12564 error ("%Han asm-specification is not allowed "
12565 "on a function-definition",
12566 &asm_spec_start_token->location);
12568 error ("%Hattributes are not allowed on a function-definition",
12569 &attributes_start_token->location);
12570 /* This is a function-definition. */
12571 *function_definition_p = true;
12573 /* Parse the function definition. */
12575 decl = cp_parser_save_member_function_body (parser,
12578 prefix_attributes);
12581 = (cp_parser_function_definition_from_specifiers_and_declarator
12582 (parser, decl_specifiers, prefix_attributes, declarator));
12590 Only in function declarations for constructors, destructors, and
12591 type conversions can the decl-specifier-seq be omitted.
12593 We explicitly postpone this check past the point where we handle
12594 function-definitions because we tolerate function-definitions
12595 that are missing their return types in some modes. */
12596 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12598 cp_parser_error (parser,
12599 "expected constructor, destructor, or type conversion");
12600 return error_mark_node;
12603 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12604 if (token->type == CPP_EQ
12605 || token->type == CPP_OPEN_PAREN
12606 || token->type == CPP_OPEN_BRACE)
12608 is_initialized = 1;
12609 initialization_kind = token->type;
12611 if (token->type == CPP_EQ
12612 && function_declarator_p (declarator))
12614 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12615 if (t2->keyword == RID_DEFAULT)
12616 is_initialized = 2;
12617 else if (t2->keyword == RID_DELETE)
12618 is_initialized = 3;
12623 /* If the init-declarator isn't initialized and isn't followed by a
12624 `,' or `;', it's not a valid init-declarator. */
12625 if (token->type != CPP_COMMA
12626 && token->type != CPP_SEMICOLON)
12628 cp_parser_error (parser, "expected initializer");
12629 return error_mark_node;
12631 is_initialized = 0;
12632 initialization_kind = CPP_EOF;
12635 /* Because start_decl has side-effects, we should only call it if we
12636 know we're going ahead. By this point, we know that we cannot
12637 possibly be looking at any other construct. */
12638 cp_parser_commit_to_tentative_parse (parser);
12640 /* If the decl specifiers were bad, issue an error now that we're
12641 sure this was intended to be a declarator. Then continue
12642 declaring the variable(s), as int, to try to cut down on further
12644 if (decl_specifiers->any_specifiers_p
12645 && decl_specifiers->type == error_mark_node)
12647 cp_parser_error (parser, "invalid type in declaration");
12648 decl_specifiers->type = integer_type_node;
12651 /* Check to see whether or not this declaration is a friend. */
12652 friend_p = cp_parser_friend_p (decl_specifiers);
12654 /* Enter the newly declared entry in the symbol table. If we're
12655 processing a declaration in a class-specifier, we wait until
12656 after processing the initializer. */
12659 if (parser->in_unbraced_linkage_specification_p)
12660 decl_specifiers->storage_class = sc_extern;
12661 decl = start_decl (declarator, decl_specifiers,
12662 is_initialized, attributes, prefix_attributes,
12666 /* Enter the SCOPE. That way unqualified names appearing in the
12667 initializer will be looked up in SCOPE. */
12668 pushed_scope = push_scope (scope);
12670 /* Perform deferred access control checks, now that we know in which
12671 SCOPE the declared entity resides. */
12672 if (!member_p && decl)
12674 tree saved_current_function_decl = NULL_TREE;
12676 /* If the entity being declared is a function, pretend that we
12677 are in its scope. If it is a `friend', it may have access to
12678 things that would not otherwise be accessible. */
12679 if (TREE_CODE (decl) == FUNCTION_DECL)
12681 saved_current_function_decl = current_function_decl;
12682 current_function_decl = decl;
12685 /* Perform access checks for template parameters. */
12686 cp_parser_perform_template_parameter_access_checks (checks);
12688 /* Perform the access control checks for the declarator and the
12689 decl-specifiers. */
12690 perform_deferred_access_checks ();
12692 /* Restore the saved value. */
12693 if (TREE_CODE (decl) == FUNCTION_DECL)
12694 current_function_decl = saved_current_function_decl;
12697 /* Parse the initializer. */
12698 initializer = NULL_TREE;
12699 is_direct_init = false;
12700 is_non_constant_init = true;
12701 if (is_initialized)
12703 if (function_declarator_p (declarator))
12705 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12706 if (initialization_kind == CPP_EQ)
12707 initializer = cp_parser_pure_specifier (parser);
12710 /* If the declaration was erroneous, we don't really
12711 know what the user intended, so just silently
12712 consume the initializer. */
12713 if (decl != error_mark_node)
12714 error ("%Hinitializer provided for function",
12715 &initializer_start_token->location);
12716 cp_parser_skip_to_closing_parenthesis (parser,
12717 /*recovering=*/true,
12718 /*or_comma=*/false,
12719 /*consume_paren=*/true);
12723 initializer = cp_parser_initializer (parser,
12725 &is_non_constant_init);
12728 /* The old parser allows attributes to appear after a parenthesized
12729 initializer. Mark Mitchell proposed removing this functionality
12730 on the GCC mailing lists on 2002-08-13. This parser accepts the
12731 attributes -- but ignores them. */
12732 if (cp_parser_allow_gnu_extensions_p (parser)
12733 && initialization_kind == CPP_OPEN_PAREN)
12734 if (cp_parser_attributes_opt (parser))
12735 warning (OPT_Wattributes,
12736 "attributes after parenthesized initializer ignored");
12738 /* For an in-class declaration, use `grokfield' to create the
12744 pop_scope (pushed_scope);
12745 pushed_scope = false;
12747 decl = grokfield (declarator, decl_specifiers,
12748 initializer, !is_non_constant_init,
12749 /*asmspec=*/NULL_TREE,
12750 prefix_attributes);
12751 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12752 cp_parser_save_default_args (parser, decl);
12755 /* Finish processing the declaration. But, skip friend
12757 if (!friend_p && decl && decl != error_mark_node)
12759 cp_finish_decl (decl,
12760 initializer, !is_non_constant_init,
12762 /* If the initializer is in parentheses, then this is
12763 a direct-initialization, which means that an
12764 `explicit' constructor is OK. Otherwise, an
12765 `explicit' constructor cannot be used. */
12766 ((is_direct_init || !is_initialized)
12767 ? 0 : LOOKUP_ONLYCONVERTING));
12769 else if ((cxx_dialect != cxx98) && friend_p
12770 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12771 /* Core issue #226 (C++0x only): A default template-argument
12772 shall not be specified in a friend class template
12774 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12775 /*is_partial=*/0, /*is_friend_decl=*/1);
12777 if (!friend_p && pushed_scope)
12778 pop_scope (pushed_scope);
12783 /* Parse a declarator.
12787 ptr-operator declarator
12789 abstract-declarator:
12790 ptr-operator abstract-declarator [opt]
12791 direct-abstract-declarator
12796 attributes [opt] direct-declarator
12797 attributes [opt] ptr-operator declarator
12799 abstract-declarator:
12800 attributes [opt] ptr-operator abstract-declarator [opt]
12801 attributes [opt] direct-abstract-declarator
12803 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12804 detect constructor, destructor or conversion operators. It is set
12805 to -1 if the declarator is a name, and +1 if it is a
12806 function. Otherwise it is set to zero. Usually you just want to
12807 test for >0, but internally the negative value is used.
12809 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12810 a decl-specifier-seq unless it declares a constructor, destructor,
12811 or conversion. It might seem that we could check this condition in
12812 semantic analysis, rather than parsing, but that makes it difficult
12813 to handle something like `f()'. We want to notice that there are
12814 no decl-specifiers, and therefore realize that this is an
12815 expression, not a declaration.)
12817 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12818 the declarator is a direct-declarator of the form "(...)".
12820 MEMBER_P is true iff this declarator is a member-declarator. */
12822 static cp_declarator *
12823 cp_parser_declarator (cp_parser* parser,
12824 cp_parser_declarator_kind dcl_kind,
12825 int* ctor_dtor_or_conv_p,
12826 bool* parenthesized_p,
12830 cp_declarator *declarator;
12831 enum tree_code code;
12832 cp_cv_quals cv_quals;
12834 tree attributes = NULL_TREE;
12836 /* Assume this is not a constructor, destructor, or type-conversion
12838 if (ctor_dtor_or_conv_p)
12839 *ctor_dtor_or_conv_p = 0;
12841 if (cp_parser_allow_gnu_extensions_p (parser))
12842 attributes = cp_parser_attributes_opt (parser);
12844 /* Peek at the next token. */
12845 token = cp_lexer_peek_token (parser->lexer);
12847 /* Check for the ptr-operator production. */
12848 cp_parser_parse_tentatively (parser);
12849 /* Parse the ptr-operator. */
12850 code = cp_parser_ptr_operator (parser,
12853 /* If that worked, then we have a ptr-operator. */
12854 if (cp_parser_parse_definitely (parser))
12856 /* If a ptr-operator was found, then this declarator was not
12858 if (parenthesized_p)
12859 *parenthesized_p = true;
12860 /* The dependent declarator is optional if we are parsing an
12861 abstract-declarator. */
12862 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12863 cp_parser_parse_tentatively (parser);
12865 /* Parse the dependent declarator. */
12866 declarator = cp_parser_declarator (parser, dcl_kind,
12867 /*ctor_dtor_or_conv_p=*/NULL,
12868 /*parenthesized_p=*/NULL,
12869 /*member_p=*/false);
12871 /* If we are parsing an abstract-declarator, we must handle the
12872 case where the dependent declarator is absent. */
12873 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12874 && !cp_parser_parse_definitely (parser))
12877 declarator = cp_parser_make_indirect_declarator
12878 (code, class_type, cv_quals, declarator);
12880 /* Everything else is a direct-declarator. */
12883 if (parenthesized_p)
12884 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12886 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12887 ctor_dtor_or_conv_p,
12891 if (attributes && declarator && declarator != cp_error_declarator)
12892 declarator->attributes = attributes;
12897 /* Parse a direct-declarator or direct-abstract-declarator.
12901 direct-declarator ( parameter-declaration-clause )
12902 cv-qualifier-seq [opt]
12903 exception-specification [opt]
12904 direct-declarator [ constant-expression [opt] ]
12907 direct-abstract-declarator:
12908 direct-abstract-declarator [opt]
12909 ( parameter-declaration-clause )
12910 cv-qualifier-seq [opt]
12911 exception-specification [opt]
12912 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12913 ( abstract-declarator )
12915 Returns a representation of the declarator. DCL_KIND is
12916 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12917 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12918 we are parsing a direct-declarator. It is
12919 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12920 of ambiguity we prefer an abstract declarator, as per
12921 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12922 cp_parser_declarator. */
12924 static cp_declarator *
12925 cp_parser_direct_declarator (cp_parser* parser,
12926 cp_parser_declarator_kind dcl_kind,
12927 int* ctor_dtor_or_conv_p,
12931 cp_declarator *declarator = NULL;
12932 tree scope = NULL_TREE;
12933 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12934 bool saved_in_declarator_p = parser->in_declarator_p;
12936 tree pushed_scope = NULL_TREE;
12940 /* Peek at the next token. */
12941 token = cp_lexer_peek_token (parser->lexer);
12942 if (token->type == CPP_OPEN_PAREN)
12944 /* This is either a parameter-declaration-clause, or a
12945 parenthesized declarator. When we know we are parsing a
12946 named declarator, it must be a parenthesized declarator
12947 if FIRST is true. For instance, `(int)' is a
12948 parameter-declaration-clause, with an omitted
12949 direct-abstract-declarator. But `((*))', is a
12950 parenthesized abstract declarator. Finally, when T is a
12951 template parameter `(T)' is a
12952 parameter-declaration-clause, and not a parenthesized
12955 We first try and parse a parameter-declaration-clause,
12956 and then try a nested declarator (if FIRST is true).
12958 It is not an error for it not to be a
12959 parameter-declaration-clause, even when FIRST is
12965 The first is the declaration of a function while the
12966 second is the definition of a variable, including its
12969 Having seen only the parenthesis, we cannot know which of
12970 these two alternatives should be selected. Even more
12971 complex are examples like:
12976 The former is a function-declaration; the latter is a
12977 variable initialization.
12979 Thus again, we try a parameter-declaration-clause, and if
12980 that fails, we back out and return. */
12982 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12984 cp_parameter_declarator *params;
12985 unsigned saved_num_template_parameter_lists;
12987 /* In a member-declarator, the only valid interpretation
12988 of a parenthesis is the start of a
12989 parameter-declaration-clause. (It is invalid to
12990 initialize a static data member with a parenthesized
12991 initializer; only the "=" form of initialization is
12994 cp_parser_parse_tentatively (parser);
12996 /* Consume the `('. */
12997 cp_lexer_consume_token (parser->lexer);
13000 /* If this is going to be an abstract declarator, we're
13001 in a declarator and we can't have default args. */
13002 parser->default_arg_ok_p = false;
13003 parser->in_declarator_p = true;
13006 /* Inside the function parameter list, surrounding
13007 template-parameter-lists do not apply. */
13008 saved_num_template_parameter_lists
13009 = parser->num_template_parameter_lists;
13010 parser->num_template_parameter_lists = 0;
13012 /* Parse the parameter-declaration-clause. */
13013 params = cp_parser_parameter_declaration_clause (parser);
13015 parser->num_template_parameter_lists
13016 = saved_num_template_parameter_lists;
13018 /* If all went well, parse the cv-qualifier-seq and the
13019 exception-specification. */
13020 if (member_p || cp_parser_parse_definitely (parser))
13022 cp_cv_quals cv_quals;
13023 tree exception_specification;
13025 if (ctor_dtor_or_conv_p)
13026 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13028 /* Consume the `)'. */
13029 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13031 /* Parse the cv-qualifier-seq. */
13032 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13033 /* And the exception-specification. */
13034 exception_specification
13035 = cp_parser_exception_specification_opt (parser);
13037 /* Create the function-declarator. */
13038 declarator = make_call_declarator (declarator,
13041 exception_specification);
13042 /* Any subsequent parameter lists are to do with
13043 return type, so are not those of the declared
13045 parser->default_arg_ok_p = false;
13047 /* Repeat the main loop. */
13052 /* If this is the first, we can try a parenthesized
13056 bool saved_in_type_id_in_expr_p;
13058 parser->default_arg_ok_p = saved_default_arg_ok_p;
13059 parser->in_declarator_p = saved_in_declarator_p;
13061 /* Consume the `('. */
13062 cp_lexer_consume_token (parser->lexer);
13063 /* Parse the nested declarator. */
13064 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13065 parser->in_type_id_in_expr_p = true;
13067 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13068 /*parenthesized_p=*/NULL,
13070 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13072 /* Expect a `)'. */
13073 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13074 declarator = cp_error_declarator;
13075 if (declarator == cp_error_declarator)
13078 goto handle_declarator;
13080 /* Otherwise, we must be done. */
13084 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13085 && token->type == CPP_OPEN_SQUARE)
13087 /* Parse an array-declarator. */
13090 if (ctor_dtor_or_conv_p)
13091 *ctor_dtor_or_conv_p = 0;
13094 parser->default_arg_ok_p = false;
13095 parser->in_declarator_p = true;
13096 /* Consume the `['. */
13097 cp_lexer_consume_token (parser->lexer);
13098 /* Peek at the next token. */
13099 token = cp_lexer_peek_token (parser->lexer);
13100 /* If the next token is `]', then there is no
13101 constant-expression. */
13102 if (token->type != CPP_CLOSE_SQUARE)
13104 bool non_constant_p;
13107 = cp_parser_constant_expression (parser,
13108 /*allow_non_constant=*/true,
13110 if (!non_constant_p)
13111 bounds = fold_non_dependent_expr (bounds);
13112 /* Normally, the array bound must be an integral constant
13113 expression. However, as an extension, we allow VLAs
13114 in function scopes. */
13115 else if (!parser->in_function_body)
13117 error ("%Harray bound is not an integer constant",
13119 bounds = error_mark_node;
13123 bounds = NULL_TREE;
13124 /* Look for the closing `]'. */
13125 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13127 declarator = cp_error_declarator;
13131 declarator = make_array_declarator (declarator, bounds);
13133 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13135 tree qualifying_scope;
13136 tree unqualified_name;
13137 special_function_kind sfk;
13139 bool pack_expansion_p = false;
13140 cp_token *declarator_id_start_token;
13142 /* Parse a declarator-id */
13143 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13146 cp_parser_parse_tentatively (parser);
13148 /* If we see an ellipsis, we should be looking at a
13150 if (token->type == CPP_ELLIPSIS)
13152 /* Consume the `...' */
13153 cp_lexer_consume_token (parser->lexer);
13155 pack_expansion_p = true;
13159 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13161 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13162 qualifying_scope = parser->scope;
13167 if (!unqualified_name && pack_expansion_p)
13169 /* Check whether an error occurred. */
13170 okay = !cp_parser_error_occurred (parser);
13172 /* We already consumed the ellipsis to mark a
13173 parameter pack, but we have no way to report it,
13174 so abort the tentative parse. We will be exiting
13175 immediately anyway. */
13176 cp_parser_abort_tentative_parse (parser);
13179 okay = cp_parser_parse_definitely (parser);
13182 unqualified_name = error_mark_node;
13183 else if (unqualified_name
13184 && (qualifying_scope
13185 || (TREE_CODE (unqualified_name)
13186 != IDENTIFIER_NODE)))
13188 cp_parser_error (parser, "expected unqualified-id");
13189 unqualified_name = error_mark_node;
13193 if (!unqualified_name)
13195 if (unqualified_name == error_mark_node)
13197 declarator = cp_error_declarator;
13198 pack_expansion_p = false;
13199 declarator->parameter_pack_p = false;
13203 if (qualifying_scope && at_namespace_scope_p ()
13204 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13206 /* In the declaration of a member of a template class
13207 outside of the class itself, the SCOPE will sometimes
13208 be a TYPENAME_TYPE. For example, given:
13210 template <typename T>
13211 int S<T>::R::i = 3;
13213 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13214 this context, we must resolve S<T>::R to an ordinary
13215 type, rather than a typename type.
13217 The reason we normally avoid resolving TYPENAME_TYPEs
13218 is that a specialization of `S' might render
13219 `S<T>::R' not a type. However, if `S' is
13220 specialized, then this `i' will not be used, so there
13221 is no harm in resolving the types here. */
13224 /* Resolve the TYPENAME_TYPE. */
13225 type = resolve_typename_type (qualifying_scope,
13226 /*only_current_p=*/false);
13227 /* If that failed, the declarator is invalid. */
13228 if (TREE_CODE (type) == TYPENAME_TYPE)
13229 error ("%H%<%T::%E%> is not a type",
13230 &declarator_id_start_token->location,
13231 TYPE_CONTEXT (qualifying_scope),
13232 TYPE_IDENTIFIER (qualifying_scope));
13233 qualifying_scope = type;
13238 if (unqualified_name)
13242 if (qualifying_scope
13243 && CLASS_TYPE_P (qualifying_scope))
13244 class_type = qualifying_scope;
13246 class_type = current_class_type;
13248 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13250 tree name_type = TREE_TYPE (unqualified_name);
13251 if (class_type && same_type_p (name_type, class_type))
13253 if (qualifying_scope
13254 && CLASSTYPE_USE_TEMPLATE (name_type))
13256 error ("%Hinvalid use of constructor as a template",
13257 &declarator_id_start_token->location);
13258 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13259 "name the constructor in a qualified name",
13261 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13262 class_type, name_type);
13263 declarator = cp_error_declarator;
13267 unqualified_name = constructor_name (class_type);
13271 /* We do not attempt to print the declarator
13272 here because we do not have enough
13273 information about its original syntactic
13275 cp_parser_error (parser, "invalid declarator");
13276 declarator = cp_error_declarator;
13283 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13284 sfk = sfk_destructor;
13285 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13286 sfk = sfk_conversion;
13287 else if (/* There's no way to declare a constructor
13288 for an anonymous type, even if the type
13289 got a name for linkage purposes. */
13290 !TYPE_WAS_ANONYMOUS (class_type)
13291 && constructor_name_p (unqualified_name,
13294 unqualified_name = constructor_name (class_type);
13295 sfk = sfk_constructor;
13298 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13299 *ctor_dtor_or_conv_p = -1;
13302 declarator = make_id_declarator (qualifying_scope,
13305 declarator->id_loc = token->location;
13306 declarator->parameter_pack_p = pack_expansion_p;
13308 if (pack_expansion_p)
13309 maybe_warn_variadic_templates ();
13311 handle_declarator:;
13312 scope = get_scope_of_declarator (declarator);
13314 /* Any names that appear after the declarator-id for a
13315 member are looked up in the containing scope. */
13316 pushed_scope = push_scope (scope);
13317 parser->in_declarator_p = true;
13318 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13319 || (declarator && declarator->kind == cdk_id))
13320 /* Default args are only allowed on function
13322 parser->default_arg_ok_p = saved_default_arg_ok_p;
13324 parser->default_arg_ok_p = false;
13333 /* For an abstract declarator, we might wind up with nothing at this
13334 point. That's an error; the declarator is not optional. */
13336 cp_parser_error (parser, "expected declarator");
13338 /* If we entered a scope, we must exit it now. */
13340 pop_scope (pushed_scope);
13342 parser->default_arg_ok_p = saved_default_arg_ok_p;
13343 parser->in_declarator_p = saved_in_declarator_p;
13348 /* Parse a ptr-operator.
13351 * cv-qualifier-seq [opt]
13353 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13358 & cv-qualifier-seq [opt]
13360 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13361 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13362 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13363 filled in with the TYPE containing the member. *CV_QUALS is
13364 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13365 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13366 Note that the tree codes returned by this function have nothing
13367 to do with the types of trees that will be eventually be created
13368 to represent the pointer or reference type being parsed. They are
13369 just constants with suggestive names. */
13370 static enum tree_code
13371 cp_parser_ptr_operator (cp_parser* parser,
13373 cp_cv_quals *cv_quals)
13375 enum tree_code code = ERROR_MARK;
13378 /* Assume that it's not a pointer-to-member. */
13380 /* And that there are no cv-qualifiers. */
13381 *cv_quals = TYPE_UNQUALIFIED;
13383 /* Peek at the next token. */
13384 token = cp_lexer_peek_token (parser->lexer);
13386 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13387 if (token->type == CPP_MULT)
13388 code = INDIRECT_REF;
13389 else if (token->type == CPP_AND)
13391 else if ((cxx_dialect != cxx98) &&
13392 token->type == CPP_AND_AND) /* C++0x only */
13393 code = NON_LVALUE_EXPR;
13395 if (code != ERROR_MARK)
13397 /* Consume the `*', `&' or `&&'. */
13398 cp_lexer_consume_token (parser->lexer);
13400 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13401 `&', if we are allowing GNU extensions. (The only qualifier
13402 that can legally appear after `&' is `restrict', but that is
13403 enforced during semantic analysis. */
13404 if (code == INDIRECT_REF
13405 || cp_parser_allow_gnu_extensions_p (parser))
13406 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13410 /* Try the pointer-to-member case. */
13411 cp_parser_parse_tentatively (parser);
13412 /* Look for the optional `::' operator. */
13413 cp_parser_global_scope_opt (parser,
13414 /*current_scope_valid_p=*/false);
13415 /* Look for the nested-name specifier. */
13416 token = cp_lexer_peek_token (parser->lexer);
13417 cp_parser_nested_name_specifier (parser,
13418 /*typename_keyword_p=*/false,
13419 /*check_dependency_p=*/true,
13421 /*is_declaration=*/false);
13422 /* If we found it, and the next token is a `*', then we are
13423 indeed looking at a pointer-to-member operator. */
13424 if (!cp_parser_error_occurred (parser)
13425 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13427 /* Indicate that the `*' operator was used. */
13428 code = INDIRECT_REF;
13430 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13431 error ("%H%qD is a namespace", &token->location, parser->scope);
13434 /* The type of which the member is a member is given by the
13436 *type = parser->scope;
13437 /* The next name will not be qualified. */
13438 parser->scope = NULL_TREE;
13439 parser->qualifying_scope = NULL_TREE;
13440 parser->object_scope = NULL_TREE;
13441 /* Look for the optional cv-qualifier-seq. */
13442 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13445 /* If that didn't work we don't have a ptr-operator. */
13446 if (!cp_parser_parse_definitely (parser))
13447 cp_parser_error (parser, "expected ptr-operator");
13453 /* Parse an (optional) cv-qualifier-seq.
13456 cv-qualifier cv-qualifier-seq [opt]
13467 Returns a bitmask representing the cv-qualifiers. */
13470 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13472 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13477 cp_cv_quals cv_qualifier;
13479 /* Peek at the next token. */
13480 token = cp_lexer_peek_token (parser->lexer);
13481 /* See if it's a cv-qualifier. */
13482 switch (token->keyword)
13485 cv_qualifier = TYPE_QUAL_CONST;
13489 cv_qualifier = TYPE_QUAL_VOLATILE;
13493 cv_qualifier = TYPE_QUAL_RESTRICT;
13497 cv_qualifier = TYPE_UNQUALIFIED;
13504 if (cv_quals & cv_qualifier)
13506 error ("%Hduplicate cv-qualifier", &token->location);
13507 cp_lexer_purge_token (parser->lexer);
13511 cp_lexer_consume_token (parser->lexer);
13512 cv_quals |= cv_qualifier;
13519 /* Parse a declarator-id.
13523 :: [opt] nested-name-specifier [opt] type-name
13525 In the `id-expression' case, the value returned is as for
13526 cp_parser_id_expression if the id-expression was an unqualified-id.
13527 If the id-expression was a qualified-id, then a SCOPE_REF is
13528 returned. The first operand is the scope (either a NAMESPACE_DECL
13529 or TREE_TYPE), but the second is still just a representation of an
13533 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13536 /* The expression must be an id-expression. Assume that qualified
13537 names are the names of types so that:
13540 int S<T>::R::i = 3;
13542 will work; we must treat `S<T>::R' as the name of a type.
13543 Similarly, assume that qualified names are templates, where
13547 int S<T>::R<T>::i = 3;
13550 id = cp_parser_id_expression (parser,
13551 /*template_keyword_p=*/false,
13552 /*check_dependency_p=*/false,
13553 /*template_p=*/NULL,
13554 /*declarator_p=*/true,
13556 if (id && BASELINK_P (id))
13557 id = BASELINK_FUNCTIONS (id);
13561 /* Parse a type-id.
13564 type-specifier-seq abstract-declarator [opt]
13566 Returns the TYPE specified. */
13569 cp_parser_type_id (cp_parser* parser)
13571 cp_decl_specifier_seq type_specifier_seq;
13572 cp_declarator *abstract_declarator;
13574 /* Parse the type-specifier-seq. */
13575 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13576 &type_specifier_seq);
13577 if (type_specifier_seq.type == error_mark_node)
13578 return error_mark_node;
13580 /* There might or might not be an abstract declarator. */
13581 cp_parser_parse_tentatively (parser);
13582 /* Look for the declarator. */
13583 abstract_declarator
13584 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13585 /*parenthesized_p=*/NULL,
13586 /*member_p=*/false);
13587 /* Check to see if there really was a declarator. */
13588 if (!cp_parser_parse_definitely (parser))
13589 abstract_declarator = NULL;
13591 return groktypename (&type_specifier_seq, abstract_declarator);
13594 /* Parse a type-specifier-seq.
13596 type-specifier-seq:
13597 type-specifier type-specifier-seq [opt]
13601 type-specifier-seq:
13602 attributes type-specifier-seq [opt]
13604 If IS_CONDITION is true, we are at the start of a "condition",
13605 e.g., we've just seen "if (".
13607 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13610 cp_parser_type_specifier_seq (cp_parser* parser,
13612 cp_decl_specifier_seq *type_specifier_seq)
13614 bool seen_type_specifier = false;
13615 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13616 cp_token *start_token = NULL;
13618 /* Clear the TYPE_SPECIFIER_SEQ. */
13619 clear_decl_specs (type_specifier_seq);
13621 /* Parse the type-specifiers and attributes. */
13624 tree type_specifier;
13625 bool is_cv_qualifier;
13627 /* Check for attributes first. */
13628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13630 type_specifier_seq->attributes =
13631 chainon (type_specifier_seq->attributes,
13632 cp_parser_attributes_opt (parser));
13636 /* record the token of the beginning of the type specifier seq,
13637 for error reporting purposes*/
13639 start_token = cp_lexer_peek_token (parser->lexer);
13641 /* Look for the type-specifier. */
13642 type_specifier = cp_parser_type_specifier (parser,
13644 type_specifier_seq,
13645 /*is_declaration=*/false,
13648 if (!type_specifier)
13650 /* If the first type-specifier could not be found, this is not a
13651 type-specifier-seq at all. */
13652 if (!seen_type_specifier)
13654 cp_parser_error (parser, "expected type-specifier");
13655 type_specifier_seq->type = error_mark_node;
13658 /* If subsequent type-specifiers could not be found, the
13659 type-specifier-seq is complete. */
13663 seen_type_specifier = true;
13664 /* The standard says that a condition can be:
13666 type-specifier-seq declarator = assignment-expression
13673 we should treat the "S" as a declarator, not as a
13674 type-specifier. The standard doesn't say that explicitly for
13675 type-specifier-seq, but it does say that for
13676 decl-specifier-seq in an ordinary declaration. Perhaps it
13677 would be clearer just to allow a decl-specifier-seq here, and
13678 then add a semantic restriction that if any decl-specifiers
13679 that are not type-specifiers appear, the program is invalid. */
13680 if (is_condition && !is_cv_qualifier)
13681 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13684 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13687 /* Parse a parameter-declaration-clause.
13689 parameter-declaration-clause:
13690 parameter-declaration-list [opt] ... [opt]
13691 parameter-declaration-list , ...
13693 Returns a representation for the parameter declarations. A return
13694 value of NULL indicates a parameter-declaration-clause consisting
13695 only of an ellipsis. */
13697 static cp_parameter_declarator *
13698 cp_parser_parameter_declaration_clause (cp_parser* parser)
13700 cp_parameter_declarator *parameters;
13705 /* Peek at the next token. */
13706 token = cp_lexer_peek_token (parser->lexer);
13707 /* Check for trivial parameter-declaration-clauses. */
13708 if (token->type == CPP_ELLIPSIS)
13710 /* Consume the `...' token. */
13711 cp_lexer_consume_token (parser->lexer);
13714 else if (token->type == CPP_CLOSE_PAREN)
13715 /* There are no parameters. */
13717 #ifndef NO_IMPLICIT_EXTERN_C
13718 if (in_system_header && current_class_type == NULL
13719 && current_lang_name == lang_name_c)
13723 return no_parameters;
13725 /* Check for `(void)', too, which is a special case. */
13726 else if (token->keyword == RID_VOID
13727 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13728 == CPP_CLOSE_PAREN))
13730 /* Consume the `void' token. */
13731 cp_lexer_consume_token (parser->lexer);
13732 /* There are no parameters. */
13733 return no_parameters;
13736 /* Parse the parameter-declaration-list. */
13737 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13738 /* If a parse error occurred while parsing the
13739 parameter-declaration-list, then the entire
13740 parameter-declaration-clause is erroneous. */
13744 /* Peek at the next token. */
13745 token = cp_lexer_peek_token (parser->lexer);
13746 /* If it's a `,', the clause should terminate with an ellipsis. */
13747 if (token->type == CPP_COMMA)
13749 /* Consume the `,'. */
13750 cp_lexer_consume_token (parser->lexer);
13751 /* Expect an ellipsis. */
13753 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13755 /* It might also be `...' if the optional trailing `,' was
13757 else if (token->type == CPP_ELLIPSIS)
13759 /* Consume the `...' token. */
13760 cp_lexer_consume_token (parser->lexer);
13761 /* And remember that we saw it. */
13765 ellipsis_p = false;
13767 /* Finish the parameter list. */
13768 if (parameters && ellipsis_p)
13769 parameters->ellipsis_p = true;
13774 /* Parse a parameter-declaration-list.
13776 parameter-declaration-list:
13777 parameter-declaration
13778 parameter-declaration-list , parameter-declaration
13780 Returns a representation of the parameter-declaration-list, as for
13781 cp_parser_parameter_declaration_clause. However, the
13782 `void_list_node' is never appended to the list. Upon return,
13783 *IS_ERROR will be true iff an error occurred. */
13785 static cp_parameter_declarator *
13786 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13788 cp_parameter_declarator *parameters = NULL;
13789 cp_parameter_declarator **tail = ¶meters;
13790 bool saved_in_unbraced_linkage_specification_p;
13792 /* Assume all will go well. */
13794 /* The special considerations that apply to a function within an
13795 unbraced linkage specifications do not apply to the parameters
13796 to the function. */
13797 saved_in_unbraced_linkage_specification_p
13798 = parser->in_unbraced_linkage_specification_p;
13799 parser->in_unbraced_linkage_specification_p = false;
13801 /* Look for more parameters. */
13804 cp_parameter_declarator *parameter;
13805 bool parenthesized_p;
13806 /* Parse the parameter. */
13808 = cp_parser_parameter_declaration (parser,
13809 /*template_parm_p=*/false,
13812 /* If a parse error occurred parsing the parameter declaration,
13813 then the entire parameter-declaration-list is erroneous. */
13820 /* Add the new parameter to the list. */
13822 tail = ¶meter->next;
13824 /* Peek at the next token. */
13825 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13826 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13827 /* These are for Objective-C++ */
13828 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13829 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13830 /* The parameter-declaration-list is complete. */
13832 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13836 /* Peek at the next token. */
13837 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13838 /* If it's an ellipsis, then the list is complete. */
13839 if (token->type == CPP_ELLIPSIS)
13841 /* Otherwise, there must be more parameters. Consume the
13843 cp_lexer_consume_token (parser->lexer);
13844 /* When parsing something like:
13846 int i(float f, double d)
13848 we can tell after seeing the declaration for "f" that we
13849 are not looking at an initialization of a variable "i",
13850 but rather at the declaration of a function "i".
13852 Due to the fact that the parsing of template arguments
13853 (as specified to a template-id) requires backtracking we
13854 cannot use this technique when inside a template argument
13856 if (!parser->in_template_argument_list_p
13857 && !parser->in_type_id_in_expr_p
13858 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13859 /* However, a parameter-declaration of the form
13860 "foat(f)" (which is a valid declaration of a
13861 parameter "f") can also be interpreted as an
13862 expression (the conversion of "f" to "float"). */
13863 && !parenthesized_p)
13864 cp_parser_commit_to_tentative_parse (parser);
13868 cp_parser_error (parser, "expected %<,%> or %<...%>");
13869 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13870 cp_parser_skip_to_closing_parenthesis (parser,
13871 /*recovering=*/true,
13872 /*or_comma=*/false,
13873 /*consume_paren=*/false);
13878 parser->in_unbraced_linkage_specification_p
13879 = saved_in_unbraced_linkage_specification_p;
13884 /* Parse a parameter declaration.
13886 parameter-declaration:
13887 decl-specifier-seq ... [opt] declarator
13888 decl-specifier-seq declarator = assignment-expression
13889 decl-specifier-seq ... [opt] abstract-declarator [opt]
13890 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13892 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13893 declares a template parameter. (In that case, a non-nested `>'
13894 token encountered during the parsing of the assignment-expression
13895 is not interpreted as a greater-than operator.)
13897 Returns a representation of the parameter, or NULL if an error
13898 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13899 true iff the declarator is of the form "(p)". */
13901 static cp_parameter_declarator *
13902 cp_parser_parameter_declaration (cp_parser *parser,
13903 bool template_parm_p,
13904 bool *parenthesized_p)
13906 int declares_class_or_enum;
13907 bool greater_than_is_operator_p;
13908 cp_decl_specifier_seq decl_specifiers;
13909 cp_declarator *declarator;
13910 tree default_argument;
13911 cp_token *token = NULL, *declarator_token_start = NULL;
13912 const char *saved_message;
13914 /* In a template parameter, `>' is not an operator.
13918 When parsing a default template-argument for a non-type
13919 template-parameter, the first non-nested `>' is taken as the end
13920 of the template parameter-list rather than a greater-than
13922 greater_than_is_operator_p = !template_parm_p;
13924 /* Type definitions may not appear in parameter types. */
13925 saved_message = parser->type_definition_forbidden_message;
13926 parser->type_definition_forbidden_message
13927 = "types may not be defined in parameter types";
13929 /* Parse the declaration-specifiers. */
13930 cp_parser_decl_specifier_seq (parser,
13931 CP_PARSER_FLAGS_NONE,
13933 &declares_class_or_enum);
13934 /* If an error occurred, there's no reason to attempt to parse the
13935 rest of the declaration. */
13936 if (cp_parser_error_occurred (parser))
13938 parser->type_definition_forbidden_message = saved_message;
13942 /* Peek at the next token. */
13943 token = cp_lexer_peek_token (parser->lexer);
13945 /* If the next token is a `)', `,', `=', `>', or `...', then there
13946 is no declarator. However, when variadic templates are enabled,
13947 there may be a declarator following `...'. */
13948 if (token->type == CPP_CLOSE_PAREN
13949 || token->type == CPP_COMMA
13950 || token->type == CPP_EQ
13951 || token->type == CPP_GREATER)
13954 if (parenthesized_p)
13955 *parenthesized_p = false;
13957 /* Otherwise, there should be a declarator. */
13960 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13961 parser->default_arg_ok_p = false;
13963 /* After seeing a decl-specifier-seq, if the next token is not a
13964 "(", there is no possibility that the code is a valid
13965 expression. Therefore, if parsing tentatively, we commit at
13967 if (!parser->in_template_argument_list_p
13968 /* In an expression context, having seen:
13972 we cannot be sure whether we are looking at a
13973 function-type (taking a "char" as a parameter) or a cast
13974 of some object of type "char" to "int". */
13975 && !parser->in_type_id_in_expr_p
13976 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13977 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13978 cp_parser_commit_to_tentative_parse (parser);
13979 /* Parse the declarator. */
13980 declarator_token_start = token;
13981 declarator = cp_parser_declarator (parser,
13982 CP_PARSER_DECLARATOR_EITHER,
13983 /*ctor_dtor_or_conv_p=*/NULL,
13985 /*member_p=*/false);
13986 parser->default_arg_ok_p = saved_default_arg_ok_p;
13987 /* After the declarator, allow more attributes. */
13988 decl_specifiers.attributes
13989 = chainon (decl_specifiers.attributes,
13990 cp_parser_attributes_opt (parser));
13993 /* If the next token is an ellipsis, and we have not seen a
13994 declarator name, and the type of the declarator contains parameter
13995 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13996 a parameter pack expansion expression. Otherwise, leave the
13997 ellipsis for a C-style variadic function. */
13998 token = cp_lexer_peek_token (parser->lexer);
13999 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14001 tree type = decl_specifiers.type;
14003 if (type && DECL_P (type))
14004 type = TREE_TYPE (type);
14007 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14008 && declarator_can_be_parameter_pack (declarator)
14009 && (!declarator || !declarator->parameter_pack_p)
14010 && uses_parameter_packs (type))
14012 /* Consume the `...'. */
14013 cp_lexer_consume_token (parser->lexer);
14014 maybe_warn_variadic_templates ();
14016 /* Build a pack expansion type */
14018 declarator->parameter_pack_p = true;
14020 decl_specifiers.type = make_pack_expansion (type);
14024 /* The restriction on defining new types applies only to the type
14025 of the parameter, not to the default argument. */
14026 parser->type_definition_forbidden_message = saved_message;
14028 /* If the next token is `=', then process a default argument. */
14029 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14031 /* Consume the `='. */
14032 cp_lexer_consume_token (parser->lexer);
14034 /* If we are defining a class, then the tokens that make up the
14035 default argument must be saved and processed later. */
14036 if (!template_parm_p && at_class_scope_p ()
14037 && TYPE_BEING_DEFINED (current_class_type))
14039 unsigned depth = 0;
14040 int maybe_template_id = 0;
14041 cp_token *first_token;
14044 /* Add tokens until we have processed the entire default
14045 argument. We add the range [first_token, token). */
14046 first_token = cp_lexer_peek_token (parser->lexer);
14051 /* Peek at the next token. */
14052 token = cp_lexer_peek_token (parser->lexer);
14053 /* What we do depends on what token we have. */
14054 switch (token->type)
14056 /* In valid code, a default argument must be
14057 immediately followed by a `,' `)', or `...'. */
14059 if (depth == 0 && maybe_template_id)
14061 /* If we've seen a '<', we might be in a
14062 template-argument-list. Until Core issue 325 is
14063 resolved, we don't know how this situation ought
14064 to be handled, so try to DTRT. We check whether
14065 what comes after the comma is a valid parameter
14066 declaration list. If it is, then the comma ends
14067 the default argument; otherwise the default
14068 argument continues. */
14069 bool error = false;
14071 /* Set ITALP so cp_parser_parameter_declaration_list
14072 doesn't decide to commit to this parse. */
14073 bool saved_italp = parser->in_template_argument_list_p;
14074 parser->in_template_argument_list_p = true;
14076 cp_parser_parse_tentatively (parser);
14077 cp_lexer_consume_token (parser->lexer);
14078 cp_parser_parameter_declaration_list (parser, &error);
14079 if (!cp_parser_error_occurred (parser) && !error)
14081 cp_parser_abort_tentative_parse (parser);
14083 parser->in_template_argument_list_p = saved_italp;
14086 case CPP_CLOSE_PAREN:
14088 /* If we run into a non-nested `;', `}', or `]',
14089 then the code is invalid -- but the default
14090 argument is certainly over. */
14091 case CPP_SEMICOLON:
14092 case CPP_CLOSE_BRACE:
14093 case CPP_CLOSE_SQUARE:
14096 /* Update DEPTH, if necessary. */
14097 else if (token->type == CPP_CLOSE_PAREN
14098 || token->type == CPP_CLOSE_BRACE
14099 || token->type == CPP_CLOSE_SQUARE)
14103 case CPP_OPEN_PAREN:
14104 case CPP_OPEN_SQUARE:
14105 case CPP_OPEN_BRACE:
14111 /* This might be the comparison operator, or it might
14112 start a template argument list. */
14113 ++maybe_template_id;
14117 if (cxx_dialect == cxx98)
14119 /* Fall through for C++0x, which treats the `>>'
14120 operator like two `>' tokens in certain
14126 /* This might be an operator, or it might close a
14127 template argument list. But if a previous '<'
14128 started a template argument list, this will have
14129 closed it, so we can't be in one anymore. */
14130 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14131 if (maybe_template_id < 0)
14132 maybe_template_id = 0;
14136 /* If we run out of tokens, issue an error message. */
14138 case CPP_PRAGMA_EOL:
14139 error ("%Hfile ends in default argument", &token->location);
14145 /* In these cases, we should look for template-ids.
14146 For example, if the default argument is
14147 `X<int, double>()', we need to do name lookup to
14148 figure out whether or not `X' is a template; if
14149 so, the `,' does not end the default argument.
14151 That is not yet done. */
14158 /* If we've reached the end, stop. */
14162 /* Add the token to the token block. */
14163 token = cp_lexer_consume_token (parser->lexer);
14166 /* Create a DEFAULT_ARG to represent the unparsed default
14168 default_argument = make_node (DEFAULT_ARG);
14169 DEFARG_TOKENS (default_argument)
14170 = cp_token_cache_new (first_token, token);
14171 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14173 /* Outside of a class definition, we can just parse the
14174 assignment-expression. */
14177 token = cp_lexer_peek_token (parser->lexer);
14179 = cp_parser_default_argument (parser, template_parm_p);
14182 if (!parser->default_arg_ok_p)
14184 if (flag_permissive)
14185 warning (0, "deprecated use of default argument for parameter of non-function");
14188 error ("%Hdefault arguments are only "
14189 "permitted for function parameters",
14191 default_argument = NULL_TREE;
14194 else if ((declarator && declarator->parameter_pack_p)
14195 || (decl_specifiers.type
14196 && PACK_EXPANSION_P (decl_specifiers.type)))
14198 const char* kind = template_parm_p? "template " : "";
14200 /* Find the name of the parameter pack. */
14201 cp_declarator *id_declarator = declarator;
14202 while (id_declarator && id_declarator->kind != cdk_id)
14203 id_declarator = id_declarator->declarator;
14205 if (id_declarator && id_declarator->kind == cdk_id)
14206 error ("%H%sparameter pack %qD cannot have a default argument",
14207 &declarator_token_start->location,
14208 kind, id_declarator->u.id.unqualified_name);
14210 error ("%H%sparameter pack cannot have a default argument",
14211 &declarator_token_start->location, kind);
14213 default_argument = NULL_TREE;
14217 default_argument = NULL_TREE;
14219 return make_parameter_declarator (&decl_specifiers,
14224 /* Parse a default argument and return it.
14226 TEMPLATE_PARM_P is true if this is a default argument for a
14227 non-type template parameter. */
14229 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14231 tree default_argument = NULL_TREE;
14232 bool saved_greater_than_is_operator_p;
14233 bool saved_local_variables_forbidden_p;
14235 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14237 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14238 parser->greater_than_is_operator_p = !template_parm_p;
14239 /* Local variable names (and the `this' keyword) may not
14240 appear in a default argument. */
14241 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14242 parser->local_variables_forbidden_p = true;
14243 /* The default argument expression may cause implicitly
14244 defined member functions to be synthesized, which will
14245 result in garbage collection. We must treat this
14246 situation as if we were within the body of function so as
14247 to avoid collecting live data on the stack. */
14249 /* Parse the assignment-expression. */
14250 if (template_parm_p)
14251 push_deferring_access_checks (dk_no_deferred);
14253 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14254 if (template_parm_p)
14255 pop_deferring_access_checks ();
14256 /* Restore saved state. */
14258 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14259 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14261 return default_argument;
14264 /* Parse a function-body.
14267 compound_statement */
14270 cp_parser_function_body (cp_parser *parser)
14272 cp_parser_compound_statement (parser, NULL, false);
14275 /* Parse a ctor-initializer-opt followed by a function-body. Return
14276 true if a ctor-initializer was present. */
14279 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14282 bool ctor_initializer_p;
14284 /* Begin the function body. */
14285 body = begin_function_body ();
14286 /* Parse the optional ctor-initializer. */
14287 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14288 /* Parse the function-body. */
14289 cp_parser_function_body (parser);
14290 /* Finish the function body. */
14291 finish_function_body (body);
14293 return ctor_initializer_p;
14296 /* Parse an initializer.
14299 = initializer-clause
14300 ( expression-list )
14302 Returns an expression representing the initializer. If no
14303 initializer is present, NULL_TREE is returned.
14305 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14306 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14307 set to TRUE if there is no initializer present. If there is an
14308 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14309 is set to true; otherwise it is set to false. */
14312 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14313 bool* non_constant_p)
14318 /* Peek at the next token. */
14319 token = cp_lexer_peek_token (parser->lexer);
14321 /* Let our caller know whether or not this initializer was
14323 *is_direct_init = (token->type != CPP_EQ);
14324 /* Assume that the initializer is constant. */
14325 *non_constant_p = false;
14327 if (token->type == CPP_EQ)
14329 /* Consume the `='. */
14330 cp_lexer_consume_token (parser->lexer);
14331 /* Parse the initializer-clause. */
14332 init = cp_parser_initializer_clause (parser, non_constant_p);
14334 else if (token->type == CPP_OPEN_PAREN)
14335 init = cp_parser_parenthesized_expression_list (parser, false,
14337 /*allow_expansion_p=*/true,
14339 else if (token->type == CPP_OPEN_BRACE)
14341 maybe_warn_cpp0x ("extended initializer lists");
14342 init = cp_parser_braced_list (parser, non_constant_p);
14343 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14347 /* Anything else is an error. */
14348 cp_parser_error (parser, "expected initializer");
14349 init = error_mark_node;
14355 /* Parse an initializer-clause.
14357 initializer-clause:
14358 assignment-expression
14361 Returns an expression representing the initializer.
14363 If the `assignment-expression' production is used the value
14364 returned is simply a representation for the expression.
14366 Otherwise, calls cp_parser_braced_list. */
14369 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14373 /* Assume the expression is constant. */
14374 *non_constant_p = false;
14376 /* If it is not a `{', then we are looking at an
14377 assignment-expression. */
14378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14381 = cp_parser_constant_expression (parser,
14382 /*allow_non_constant_p=*/true,
14384 if (!*non_constant_p)
14385 initializer = fold_non_dependent_expr (initializer);
14388 initializer = cp_parser_braced_list (parser, non_constant_p);
14390 return initializer;
14393 /* Parse a brace-enclosed initializer list.
14396 { initializer-list , [opt] }
14399 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14400 the elements of the initializer-list (or NULL, if the last
14401 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14402 NULL_TREE. There is no way to detect whether or not the optional
14403 trailing `,' was provided. NON_CONSTANT_P is as for
14404 cp_parser_initializer. */
14407 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14411 /* Consume the `{' token. */
14412 cp_lexer_consume_token (parser->lexer);
14413 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14414 initializer = make_node (CONSTRUCTOR);
14415 /* If it's not a `}', then there is a non-trivial initializer. */
14416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14418 /* Parse the initializer list. */
14419 CONSTRUCTOR_ELTS (initializer)
14420 = cp_parser_initializer_list (parser, non_constant_p);
14421 /* A trailing `,' token is allowed. */
14422 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14423 cp_lexer_consume_token (parser->lexer);
14425 /* Now, there should be a trailing `}'. */
14426 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14427 TREE_TYPE (initializer) = init_list_type_node;
14428 return initializer;
14431 /* Parse an initializer-list.
14434 initializer-clause ... [opt]
14435 initializer-list , initializer-clause ... [opt]
14440 identifier : initializer-clause
14441 initializer-list, identifier : initializer-clause
14443 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14444 for the initializer. If the INDEX of the elt is non-NULL, it is the
14445 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14446 as for cp_parser_initializer. */
14448 static VEC(constructor_elt,gc) *
14449 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14451 VEC(constructor_elt,gc) *v = NULL;
14453 /* Assume all of the expressions are constant. */
14454 *non_constant_p = false;
14456 /* Parse the rest of the list. */
14462 bool clause_non_constant_p;
14464 /* If the next token is an identifier and the following one is a
14465 colon, we are looking at the GNU designated-initializer
14467 if (cp_parser_allow_gnu_extensions_p (parser)
14468 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14469 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14471 /* Warn the user that they are using an extension. */
14472 pedwarn (input_location, OPT_pedantic,
14473 "ISO C++ does not allow designated initializers");
14474 /* Consume the identifier. */
14475 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14476 /* Consume the `:'. */
14477 cp_lexer_consume_token (parser->lexer);
14480 identifier = NULL_TREE;
14482 /* Parse the initializer. */
14483 initializer = cp_parser_initializer_clause (parser,
14484 &clause_non_constant_p);
14485 /* If any clause is non-constant, so is the entire initializer. */
14486 if (clause_non_constant_p)
14487 *non_constant_p = true;
14489 /* If we have an ellipsis, this is an initializer pack
14491 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14493 /* Consume the `...'. */
14494 cp_lexer_consume_token (parser->lexer);
14496 /* Turn the initializer into an initializer expansion. */
14497 initializer = make_pack_expansion (initializer);
14500 /* Add it to the vector. */
14501 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14503 /* If the next token is not a comma, we have reached the end of
14505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14508 /* Peek at the next token. */
14509 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14510 /* If the next token is a `}', then we're still done. An
14511 initializer-clause can have a trailing `,' after the
14512 initializer-list and before the closing `}'. */
14513 if (token->type == CPP_CLOSE_BRACE)
14516 /* Consume the `,' token. */
14517 cp_lexer_consume_token (parser->lexer);
14523 /* Classes [gram.class] */
14525 /* Parse a class-name.
14531 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14532 to indicate that names looked up in dependent types should be
14533 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14534 keyword has been used to indicate that the name that appears next
14535 is a template. TAG_TYPE indicates the explicit tag given before
14536 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14537 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14538 is the class being defined in a class-head.
14540 Returns the TYPE_DECL representing the class. */
14543 cp_parser_class_name (cp_parser *parser,
14544 bool typename_keyword_p,
14545 bool template_keyword_p,
14546 enum tag_types tag_type,
14547 bool check_dependency_p,
14549 bool is_declaration)
14556 /* All class-names start with an identifier. */
14557 token = cp_lexer_peek_token (parser->lexer);
14558 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14560 cp_parser_error (parser, "expected class-name");
14561 return error_mark_node;
14564 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14565 to a template-id, so we save it here. */
14566 scope = parser->scope;
14567 if (scope == error_mark_node)
14568 return error_mark_node;
14570 /* Any name names a type if we're following the `typename' keyword
14571 in a qualified name where the enclosing scope is type-dependent. */
14572 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14573 && dependent_type_p (scope));
14574 /* Handle the common case (an identifier, but not a template-id)
14576 if (token->type == CPP_NAME
14577 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14579 cp_token *identifier_token;
14583 /* Look for the identifier. */
14584 identifier_token = cp_lexer_peek_token (parser->lexer);
14585 ambiguous_p = identifier_token->ambiguous_p;
14586 identifier = cp_parser_identifier (parser);
14587 /* If the next token isn't an identifier, we are certainly not
14588 looking at a class-name. */
14589 if (identifier == error_mark_node)
14590 decl = error_mark_node;
14591 /* If we know this is a type-name, there's no need to look it
14593 else if (typename_p)
14597 tree ambiguous_decls;
14598 /* If we already know that this lookup is ambiguous, then
14599 we've already issued an error message; there's no reason
14603 cp_parser_simulate_error (parser);
14604 return error_mark_node;
14606 /* If the next token is a `::', then the name must be a type
14609 [basic.lookup.qual]
14611 During the lookup for a name preceding the :: scope
14612 resolution operator, object, function, and enumerator
14613 names are ignored. */
14614 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14615 tag_type = typename_type;
14616 /* Look up the name. */
14617 decl = cp_parser_lookup_name (parser, identifier,
14619 /*is_template=*/false,
14620 /*is_namespace=*/false,
14621 check_dependency_p,
14623 identifier_token->location);
14624 if (ambiguous_decls)
14626 error ("%Hreference to %qD is ambiguous",
14627 &identifier_token->location, identifier);
14628 print_candidates (ambiguous_decls);
14629 if (cp_parser_parsing_tentatively (parser))
14631 identifier_token->ambiguous_p = true;
14632 cp_parser_simulate_error (parser);
14634 return error_mark_node;
14640 /* Try a template-id. */
14641 decl = cp_parser_template_id (parser, template_keyword_p,
14642 check_dependency_p,
14644 if (decl == error_mark_node)
14645 return error_mark_node;
14648 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14650 /* If this is a typename, create a TYPENAME_TYPE. */
14651 if (typename_p && decl != error_mark_node)
14653 decl = make_typename_type (scope, decl, typename_type,
14654 /*complain=*/tf_error);
14655 if (decl != error_mark_node)
14656 decl = TYPE_NAME (decl);
14659 /* Check to see that it is really the name of a class. */
14660 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14661 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14662 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14663 /* Situations like this:
14665 template <typename T> struct A {
14666 typename T::template X<int>::I i;
14669 are problematic. Is `T::template X<int>' a class-name? The
14670 standard does not seem to be definitive, but there is no other
14671 valid interpretation of the following `::'. Therefore, those
14672 names are considered class-names. */
14674 decl = make_typename_type (scope, decl, tag_type, tf_error);
14675 if (decl != error_mark_node)
14676 decl = TYPE_NAME (decl);
14678 else if (TREE_CODE (decl) != TYPE_DECL
14679 || TREE_TYPE (decl) == error_mark_node
14680 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14681 decl = error_mark_node;
14683 if (decl == error_mark_node)
14684 cp_parser_error (parser, "expected class-name");
14689 /* Parse a class-specifier.
14692 class-head { member-specification [opt] }
14694 Returns the TREE_TYPE representing the class. */
14697 cp_parser_class_specifier (cp_parser* parser)
14701 tree attributes = NULL_TREE;
14702 int has_trailing_semicolon;
14703 bool nested_name_specifier_p;
14704 unsigned saved_num_template_parameter_lists;
14705 bool saved_in_function_body;
14706 tree old_scope = NULL_TREE;
14707 tree scope = NULL_TREE;
14710 push_deferring_access_checks (dk_no_deferred);
14712 /* Parse the class-head. */
14713 type = cp_parser_class_head (parser,
14714 &nested_name_specifier_p,
14717 /* If the class-head was a semantic disaster, skip the entire body
14721 cp_parser_skip_to_end_of_block_or_statement (parser);
14722 pop_deferring_access_checks ();
14723 return error_mark_node;
14726 /* Look for the `{'. */
14727 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14729 pop_deferring_access_checks ();
14730 return error_mark_node;
14733 /* Process the base classes. If they're invalid, skip the
14734 entire class body. */
14735 if (!xref_basetypes (type, bases))
14737 /* Consuming the closing brace yields better error messages
14739 if (cp_parser_skip_to_closing_brace (parser))
14740 cp_lexer_consume_token (parser->lexer);
14741 pop_deferring_access_checks ();
14742 return error_mark_node;
14745 /* Issue an error message if type-definitions are forbidden here. */
14746 cp_parser_check_type_definition (parser);
14747 /* Remember that we are defining one more class. */
14748 ++parser->num_classes_being_defined;
14749 /* Inside the class, surrounding template-parameter-lists do not
14751 saved_num_template_parameter_lists
14752 = parser->num_template_parameter_lists;
14753 parser->num_template_parameter_lists = 0;
14754 /* We are not in a function body. */
14755 saved_in_function_body = parser->in_function_body;
14756 parser->in_function_body = false;
14758 /* Start the class. */
14759 if (nested_name_specifier_p)
14761 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14762 old_scope = push_inner_scope (scope);
14764 type = begin_class_definition (type, attributes);
14766 if (type == error_mark_node)
14767 /* If the type is erroneous, skip the entire body of the class. */
14768 cp_parser_skip_to_closing_brace (parser);
14770 /* Parse the member-specification. */
14771 cp_parser_member_specification_opt (parser);
14773 /* Look for the trailing `}'. */
14774 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14775 /* We get better error messages by noticing a common problem: a
14776 missing trailing `;'. */
14777 token = cp_lexer_peek_token (parser->lexer);
14778 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14779 /* Look for trailing attributes to apply to this class. */
14780 if (cp_parser_allow_gnu_extensions_p (parser))
14781 attributes = cp_parser_attributes_opt (parser);
14782 if (type != error_mark_node)
14783 type = finish_struct (type, attributes);
14784 if (nested_name_specifier_p)
14785 pop_inner_scope (old_scope, scope);
14786 /* If this class is not itself within the scope of another class,
14787 then we need to parse the bodies of all of the queued function
14788 definitions. Note that the queued functions defined in a class
14789 are not always processed immediately following the
14790 class-specifier for that class. Consider:
14793 struct B { void f() { sizeof (A); } };
14796 If `f' were processed before the processing of `A' were
14797 completed, there would be no way to compute the size of `A'.
14798 Note that the nesting we are interested in here is lexical --
14799 not the semantic nesting given by TYPE_CONTEXT. In particular,
14802 struct A { struct B; };
14803 struct A::B { void f() { } };
14805 there is no need to delay the parsing of `A::B::f'. */
14806 if (--parser->num_classes_being_defined == 0)
14810 tree class_type = NULL_TREE;
14811 tree pushed_scope = NULL_TREE;
14813 /* In a first pass, parse default arguments to the functions.
14814 Then, in a second pass, parse the bodies of the functions.
14815 This two-phased approach handles cases like:
14823 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14824 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14825 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14826 TREE_PURPOSE (parser->unparsed_functions_queues)
14827 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14829 fn = TREE_VALUE (queue_entry);
14830 /* If there are default arguments that have not yet been processed,
14831 take care of them now. */
14832 if (class_type != TREE_PURPOSE (queue_entry))
14835 pop_scope (pushed_scope);
14836 class_type = TREE_PURPOSE (queue_entry);
14837 pushed_scope = push_scope (class_type);
14839 /* Make sure that any template parameters are in scope. */
14840 maybe_begin_member_template_processing (fn);
14841 /* Parse the default argument expressions. */
14842 cp_parser_late_parsing_default_args (parser, fn);
14843 /* Remove any template parameters from the symbol table. */
14844 maybe_end_member_template_processing ();
14847 pop_scope (pushed_scope);
14848 /* Now parse the body of the functions. */
14849 for (TREE_VALUE (parser->unparsed_functions_queues)
14850 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14851 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14852 TREE_VALUE (parser->unparsed_functions_queues)
14853 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14855 /* Figure out which function we need to process. */
14856 fn = TREE_VALUE (queue_entry);
14857 /* Parse the function. */
14858 cp_parser_late_parsing_for_member (parser, fn);
14862 /* Put back any saved access checks. */
14863 pop_deferring_access_checks ();
14865 /* Restore saved state. */
14866 parser->in_function_body = saved_in_function_body;
14867 parser->num_template_parameter_lists
14868 = saved_num_template_parameter_lists;
14873 /* Parse a class-head.
14876 class-key identifier [opt] base-clause [opt]
14877 class-key nested-name-specifier identifier base-clause [opt]
14878 class-key nested-name-specifier [opt] template-id
14882 class-key attributes identifier [opt] base-clause [opt]
14883 class-key attributes nested-name-specifier identifier base-clause [opt]
14884 class-key attributes nested-name-specifier [opt] template-id
14887 Upon return BASES is initialized to the list of base classes (or
14888 NULL, if there are none) in the same form returned by
14889 cp_parser_base_clause.
14891 Returns the TYPE of the indicated class. Sets
14892 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14893 involving a nested-name-specifier was used, and FALSE otherwise.
14895 Returns error_mark_node if this is not a class-head.
14897 Returns NULL_TREE if the class-head is syntactically valid, but
14898 semantically invalid in a way that means we should skip the entire
14899 body of the class. */
14902 cp_parser_class_head (cp_parser* parser,
14903 bool* nested_name_specifier_p,
14904 tree *attributes_p,
14907 tree nested_name_specifier;
14908 enum tag_types class_key;
14909 tree id = NULL_TREE;
14910 tree type = NULL_TREE;
14912 bool template_id_p = false;
14913 bool qualified_p = false;
14914 bool invalid_nested_name_p = false;
14915 bool invalid_explicit_specialization_p = false;
14916 tree pushed_scope = NULL_TREE;
14917 unsigned num_templates;
14918 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14919 /* Assume no nested-name-specifier will be present. */
14920 *nested_name_specifier_p = false;
14921 /* Assume no template parameter lists will be used in defining the
14925 *bases = NULL_TREE;
14927 /* Look for the class-key. */
14928 class_key = cp_parser_class_key (parser);
14929 if (class_key == none_type)
14930 return error_mark_node;
14932 /* Parse the attributes. */
14933 attributes = cp_parser_attributes_opt (parser);
14935 /* If the next token is `::', that is invalid -- but sometimes
14936 people do try to write:
14940 Handle this gracefully by accepting the extra qualifier, and then
14941 issuing an error about it later if this really is a
14942 class-head. If it turns out just to be an elaborated type
14943 specifier, remain silent. */
14944 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14945 qualified_p = true;
14947 push_deferring_access_checks (dk_no_check);
14949 /* Determine the name of the class. Begin by looking for an
14950 optional nested-name-specifier. */
14951 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
14952 nested_name_specifier
14953 = cp_parser_nested_name_specifier_opt (parser,
14954 /*typename_keyword_p=*/false,
14955 /*check_dependency_p=*/false,
14957 /*is_declaration=*/false);
14958 /* If there was a nested-name-specifier, then there *must* be an
14960 if (nested_name_specifier)
14962 type_start_token = cp_lexer_peek_token (parser->lexer);
14963 /* Although the grammar says `identifier', it really means
14964 `class-name' or `template-name'. You are only allowed to
14965 define a class that has already been declared with this
14968 The proposed resolution for Core Issue 180 says that wherever
14969 you see `class T::X' you should treat `X' as a type-name.
14971 It is OK to define an inaccessible class; for example:
14973 class A { class B; };
14976 We do not know if we will see a class-name, or a
14977 template-name. We look for a class-name first, in case the
14978 class-name is a template-id; if we looked for the
14979 template-name first we would stop after the template-name. */
14980 cp_parser_parse_tentatively (parser);
14981 type = cp_parser_class_name (parser,
14982 /*typename_keyword_p=*/false,
14983 /*template_keyword_p=*/false,
14985 /*check_dependency_p=*/false,
14986 /*class_head_p=*/true,
14987 /*is_declaration=*/false);
14988 /* If that didn't work, ignore the nested-name-specifier. */
14989 if (!cp_parser_parse_definitely (parser))
14991 invalid_nested_name_p = true;
14992 type_start_token = cp_lexer_peek_token (parser->lexer);
14993 id = cp_parser_identifier (parser);
14994 if (id == error_mark_node)
14997 /* If we could not find a corresponding TYPE, treat this
14998 declaration like an unqualified declaration. */
14999 if (type == error_mark_node)
15000 nested_name_specifier = NULL_TREE;
15001 /* Otherwise, count the number of templates used in TYPE and its
15002 containing scopes. */
15007 for (scope = TREE_TYPE (type);
15008 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15009 scope = (TYPE_P (scope)
15010 ? TYPE_CONTEXT (scope)
15011 : DECL_CONTEXT (scope)))
15013 && CLASS_TYPE_P (scope)
15014 && CLASSTYPE_TEMPLATE_INFO (scope)
15015 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15016 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15020 /* Otherwise, the identifier is optional. */
15023 /* We don't know whether what comes next is a template-id,
15024 an identifier, or nothing at all. */
15025 cp_parser_parse_tentatively (parser);
15026 /* Check for a template-id. */
15027 type_start_token = cp_lexer_peek_token (parser->lexer);
15028 id = cp_parser_template_id (parser,
15029 /*template_keyword_p=*/false,
15030 /*check_dependency_p=*/true,
15031 /*is_declaration=*/true);
15032 /* If that didn't work, it could still be an identifier. */
15033 if (!cp_parser_parse_definitely (parser))
15035 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15037 type_start_token = cp_lexer_peek_token (parser->lexer);
15038 id = cp_parser_identifier (parser);
15045 template_id_p = true;
15050 pop_deferring_access_checks ();
15053 cp_parser_check_for_invalid_template_id (parser, id,
15054 type_start_token->location);
15056 /* If it's not a `:' or a `{' then we can't really be looking at a
15057 class-head, since a class-head only appears as part of a
15058 class-specifier. We have to detect this situation before calling
15059 xref_tag, since that has irreversible side-effects. */
15060 if (!cp_parser_next_token_starts_class_definition_p (parser))
15062 cp_parser_error (parser, "expected %<{%> or %<:%>");
15063 return error_mark_node;
15066 /* At this point, we're going ahead with the class-specifier, even
15067 if some other problem occurs. */
15068 cp_parser_commit_to_tentative_parse (parser);
15069 /* Issue the error about the overly-qualified name now. */
15072 cp_parser_error (parser,
15073 "global qualification of class name is invalid");
15074 return error_mark_node;
15076 else if (invalid_nested_name_p)
15078 cp_parser_error (parser,
15079 "qualified name does not name a class");
15080 return error_mark_node;
15082 else if (nested_name_specifier)
15086 /* Reject typedef-names in class heads. */
15087 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15089 error ("%Hinvalid class name in declaration of %qD",
15090 &type_start_token->location, type);
15095 /* Figure out in what scope the declaration is being placed. */
15096 scope = current_scope ();
15097 /* If that scope does not contain the scope in which the
15098 class was originally declared, the program is invalid. */
15099 if (scope && !is_ancestor (scope, nested_name_specifier))
15101 if (at_namespace_scope_p ())
15102 error ("%Hdeclaration of %qD in namespace %qD which does not "
15104 &type_start_token->location,
15105 type, scope, nested_name_specifier);
15107 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15108 &type_start_token->location,
15109 type, scope, nested_name_specifier);
15115 A declarator-id shall not be qualified except for the
15116 definition of a ... nested class outside of its class
15117 ... [or] the definition or explicit instantiation of a
15118 class member of a namespace outside of its namespace. */
15119 if (scope == nested_name_specifier)
15121 permerror (input_location, "%Hextra qualification not allowed",
15122 &nested_name_specifier_token_start->location);
15123 nested_name_specifier = NULL_TREE;
15127 /* An explicit-specialization must be preceded by "template <>". If
15128 it is not, try to recover gracefully. */
15129 if (at_namespace_scope_p ()
15130 && parser->num_template_parameter_lists == 0
15133 error ("%Han explicit specialization must be preceded by %<template <>%>",
15134 &type_start_token->location);
15135 invalid_explicit_specialization_p = true;
15136 /* Take the same action that would have been taken by
15137 cp_parser_explicit_specialization. */
15138 ++parser->num_template_parameter_lists;
15139 begin_specialization ();
15141 /* There must be no "return" statements between this point and the
15142 end of this function; set "type "to the correct return value and
15143 use "goto done;" to return. */
15144 /* Make sure that the right number of template parameters were
15146 if (!cp_parser_check_template_parameters (parser, num_templates,
15147 type_start_token->location))
15149 /* If something went wrong, there is no point in even trying to
15150 process the class-definition. */
15155 /* Look up the type. */
15158 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15159 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15160 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15162 error ("%Hfunction template %qD redeclared as a class template",
15163 &type_start_token->location, id);
15164 type = error_mark_node;
15168 type = TREE_TYPE (id);
15169 type = maybe_process_partial_specialization (type);
15171 if (nested_name_specifier)
15172 pushed_scope = push_scope (nested_name_specifier);
15174 else if (nested_name_specifier)
15180 template <typename T> struct S { struct T };
15181 template <typename T> struct S<T>::T { };
15183 we will get a TYPENAME_TYPE when processing the definition of
15184 `S::T'. We need to resolve it to the actual type before we
15185 try to define it. */
15186 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15188 class_type = resolve_typename_type (TREE_TYPE (type),
15189 /*only_current_p=*/false);
15190 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15191 type = TYPE_NAME (class_type);
15194 cp_parser_error (parser, "could not resolve typename type");
15195 type = error_mark_node;
15199 if (maybe_process_partial_specialization (TREE_TYPE (type))
15200 == error_mark_node)
15206 class_type = current_class_type;
15207 /* Enter the scope indicated by the nested-name-specifier. */
15208 pushed_scope = push_scope (nested_name_specifier);
15209 /* Get the canonical version of this type. */
15210 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15211 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15212 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15214 type = push_template_decl (type);
15215 if (type == error_mark_node)
15222 type = TREE_TYPE (type);
15223 *nested_name_specifier_p = true;
15225 else /* The name is not a nested name. */
15227 /* If the class was unnamed, create a dummy name. */
15229 id = make_anon_name ();
15230 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15231 parser->num_template_parameter_lists);
15234 /* Indicate whether this class was declared as a `class' or as a
15236 if (TREE_CODE (type) == RECORD_TYPE)
15237 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15238 cp_parser_check_class_key (class_key, type);
15240 /* If this type was already complete, and we see another definition,
15241 that's an error. */
15242 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15244 error ("%Hredefinition of %q#T",
15245 &type_start_token->location, type);
15246 error ("%Hprevious definition of %q+#T",
15247 &type_start_token->location, type);
15251 else if (type == error_mark_node)
15254 /* We will have entered the scope containing the class; the names of
15255 base classes should be looked up in that context. For example:
15257 struct A { struct B {}; struct C; };
15258 struct A::C : B {};
15262 /* Get the list of base-classes, if there is one. */
15263 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15264 *bases = cp_parser_base_clause (parser);
15267 /* Leave the scope given by the nested-name-specifier. We will
15268 enter the class scope itself while processing the members. */
15270 pop_scope (pushed_scope);
15272 if (invalid_explicit_specialization_p)
15274 end_specialization ();
15275 --parser->num_template_parameter_lists;
15277 *attributes_p = attributes;
15281 /* Parse a class-key.
15288 Returns the kind of class-key specified, or none_type to indicate
15291 static enum tag_types
15292 cp_parser_class_key (cp_parser* parser)
15295 enum tag_types tag_type;
15297 /* Look for the class-key. */
15298 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15302 /* Check to see if the TOKEN is a class-key. */
15303 tag_type = cp_parser_token_is_class_key (token);
15305 cp_parser_error (parser, "expected class-key");
15309 /* Parse an (optional) member-specification.
15311 member-specification:
15312 member-declaration member-specification [opt]
15313 access-specifier : member-specification [opt] */
15316 cp_parser_member_specification_opt (cp_parser* parser)
15323 /* Peek at the next token. */
15324 token = cp_lexer_peek_token (parser->lexer);
15325 /* If it's a `}', or EOF then we've seen all the members. */
15326 if (token->type == CPP_CLOSE_BRACE
15327 || token->type == CPP_EOF
15328 || token->type == CPP_PRAGMA_EOL)
15331 /* See if this token is a keyword. */
15332 keyword = token->keyword;
15336 case RID_PROTECTED:
15338 /* Consume the access-specifier. */
15339 cp_lexer_consume_token (parser->lexer);
15340 /* Remember which access-specifier is active. */
15341 current_access_specifier = token->u.value;
15342 /* Look for the `:'. */
15343 cp_parser_require (parser, CPP_COLON, "%<:%>");
15347 /* Accept #pragmas at class scope. */
15348 if (token->type == CPP_PRAGMA)
15350 cp_parser_pragma (parser, pragma_external);
15354 /* Otherwise, the next construction must be a
15355 member-declaration. */
15356 cp_parser_member_declaration (parser);
15361 /* Parse a member-declaration.
15363 member-declaration:
15364 decl-specifier-seq [opt] member-declarator-list [opt] ;
15365 function-definition ; [opt]
15366 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15368 template-declaration
15370 member-declarator-list:
15372 member-declarator-list , member-declarator
15375 declarator pure-specifier [opt]
15376 declarator constant-initializer [opt]
15377 identifier [opt] : constant-expression
15381 member-declaration:
15382 __extension__ member-declaration
15385 declarator attributes [opt] pure-specifier [opt]
15386 declarator attributes [opt] constant-initializer [opt]
15387 identifier [opt] attributes [opt] : constant-expression
15391 member-declaration:
15392 static_assert-declaration */
15395 cp_parser_member_declaration (cp_parser* parser)
15397 cp_decl_specifier_seq decl_specifiers;
15398 tree prefix_attributes;
15400 int declares_class_or_enum;
15402 cp_token *token = NULL;
15403 cp_token *decl_spec_token_start = NULL;
15404 cp_token *initializer_token_start = NULL;
15405 int saved_pedantic;
15407 /* Check for the `__extension__' keyword. */
15408 if (cp_parser_extension_opt (parser, &saved_pedantic))
15411 cp_parser_member_declaration (parser);
15412 /* Restore the old value of the PEDANTIC flag. */
15413 pedantic = saved_pedantic;
15418 /* Check for a template-declaration. */
15419 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15421 /* An explicit specialization here is an error condition, and we
15422 expect the specialization handler to detect and report this. */
15423 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15424 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15425 cp_parser_explicit_specialization (parser);
15427 cp_parser_template_declaration (parser, /*member_p=*/true);
15432 /* Check for a using-declaration. */
15433 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15435 /* Parse the using-declaration. */
15436 cp_parser_using_declaration (parser,
15437 /*access_declaration_p=*/false);
15441 /* Check for @defs. */
15442 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15445 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15446 ivar = ivar_chains;
15450 ivar = TREE_CHAIN (member);
15451 TREE_CHAIN (member) = NULL_TREE;
15452 finish_member_declaration (member);
15457 /* If the next token is `static_assert' we have a static assertion. */
15458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15460 cp_parser_static_assert (parser, /*member_p=*/true);
15464 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15467 /* Parse the decl-specifier-seq. */
15468 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15469 cp_parser_decl_specifier_seq (parser,
15470 CP_PARSER_FLAGS_OPTIONAL,
15472 &declares_class_or_enum);
15473 prefix_attributes = decl_specifiers.attributes;
15474 decl_specifiers.attributes = NULL_TREE;
15475 /* Check for an invalid type-name. */
15476 if (!decl_specifiers.type
15477 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15479 /* If there is no declarator, then the decl-specifier-seq should
15481 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15483 /* If there was no decl-specifier-seq, and the next token is a
15484 `;', then we have something like:
15490 Each member-declaration shall declare at least one member
15491 name of the class. */
15492 if (!decl_specifiers.any_specifiers_p)
15494 cp_token *token = cp_lexer_peek_token (parser->lexer);
15495 if (!in_system_header_at (token->location))
15496 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15502 /* See if this declaration is a friend. */
15503 friend_p = cp_parser_friend_p (&decl_specifiers);
15504 /* If there were decl-specifiers, check to see if there was
15505 a class-declaration. */
15506 type = check_tag_decl (&decl_specifiers);
15507 /* Nested classes have already been added to the class, but
15508 a `friend' needs to be explicitly registered. */
15511 /* If the `friend' keyword was present, the friend must
15512 be introduced with a class-key. */
15513 if (!declares_class_or_enum)
15514 error ("%Ha class-key must be used when declaring a friend",
15515 &decl_spec_token_start->location);
15518 template <typename T> struct A {
15519 friend struct A<T>::B;
15522 A<T>::B will be represented by a TYPENAME_TYPE, and
15523 therefore not recognized by check_tag_decl. */
15525 && decl_specifiers.type
15526 && TYPE_P (decl_specifiers.type))
15527 type = decl_specifiers.type;
15528 if (!type || !TYPE_P (type))
15529 error ("%Hfriend declaration does not name a class or "
15530 "function", &decl_spec_token_start->location);
15532 make_friend_class (current_class_type, type,
15533 /*complain=*/true);
15535 /* If there is no TYPE, an error message will already have
15537 else if (!type || type == error_mark_node)
15539 /* An anonymous aggregate has to be handled specially; such
15540 a declaration really declares a data member (with a
15541 particular type), as opposed to a nested class. */
15542 else if (ANON_AGGR_TYPE_P (type))
15544 /* Remove constructors and such from TYPE, now that we
15545 know it is an anonymous aggregate. */
15546 fixup_anonymous_aggr (type);
15547 /* And make the corresponding data member. */
15548 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15549 /* Add it to the class. */
15550 finish_member_declaration (decl);
15553 cp_parser_check_access_in_redeclaration
15555 decl_spec_token_start->location);
15560 /* See if these declarations will be friends. */
15561 friend_p = cp_parser_friend_p (&decl_specifiers);
15563 /* Keep going until we hit the `;' at the end of the
15565 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15567 tree attributes = NULL_TREE;
15568 tree first_attribute;
15570 /* Peek at the next token. */
15571 token = cp_lexer_peek_token (parser->lexer);
15573 /* Check for a bitfield declaration. */
15574 if (token->type == CPP_COLON
15575 || (token->type == CPP_NAME
15576 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15582 /* Get the name of the bitfield. Note that we cannot just
15583 check TOKEN here because it may have been invalidated by
15584 the call to cp_lexer_peek_nth_token above. */
15585 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15586 identifier = cp_parser_identifier (parser);
15588 identifier = NULL_TREE;
15590 /* Consume the `:' token. */
15591 cp_lexer_consume_token (parser->lexer);
15592 /* Get the width of the bitfield. */
15594 = cp_parser_constant_expression (parser,
15595 /*allow_non_constant=*/false,
15598 /* Look for attributes that apply to the bitfield. */
15599 attributes = cp_parser_attributes_opt (parser);
15600 /* Remember which attributes are prefix attributes and
15602 first_attribute = attributes;
15603 /* Combine the attributes. */
15604 attributes = chainon (prefix_attributes, attributes);
15606 /* Create the bitfield declaration. */
15607 decl = grokbitfield (identifier
15608 ? make_id_declarator (NULL_TREE,
15618 cp_declarator *declarator;
15620 tree asm_specification;
15621 int ctor_dtor_or_conv_p;
15623 /* Parse the declarator. */
15625 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15626 &ctor_dtor_or_conv_p,
15627 /*parenthesized_p=*/NULL,
15628 /*member_p=*/true);
15630 /* If something went wrong parsing the declarator, make sure
15631 that we at least consume some tokens. */
15632 if (declarator == cp_error_declarator)
15634 /* Skip to the end of the statement. */
15635 cp_parser_skip_to_end_of_statement (parser);
15636 /* If the next token is not a semicolon, that is
15637 probably because we just skipped over the body of
15638 a function. So, we consume a semicolon if
15639 present, but do not issue an error message if it
15641 if (cp_lexer_next_token_is (parser->lexer,
15643 cp_lexer_consume_token (parser->lexer);
15647 if (declares_class_or_enum & 2)
15648 cp_parser_check_for_definition_in_return_type
15649 (declarator, decl_specifiers.type,
15650 decl_specifiers.type_location);
15652 /* Look for an asm-specification. */
15653 asm_specification = cp_parser_asm_specification_opt (parser);
15654 /* Look for attributes that apply to the declaration. */
15655 attributes = cp_parser_attributes_opt (parser);
15656 /* Remember which attributes are prefix attributes and
15658 first_attribute = attributes;
15659 /* Combine the attributes. */
15660 attributes = chainon (prefix_attributes, attributes);
15662 /* If it's an `=', then we have a constant-initializer or a
15663 pure-specifier. It is not correct to parse the
15664 initializer before registering the member declaration
15665 since the member declaration should be in scope while
15666 its initializer is processed. However, the rest of the
15667 front end does not yet provide an interface that allows
15668 us to handle this correctly. */
15669 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15673 A pure-specifier shall be used only in the declaration of
15674 a virtual function.
15676 A member-declarator can contain a constant-initializer
15677 only if it declares a static member of integral or
15680 Therefore, if the DECLARATOR is for a function, we look
15681 for a pure-specifier; otherwise, we look for a
15682 constant-initializer. When we call `grokfield', it will
15683 perform more stringent semantics checks. */
15684 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15685 if (function_declarator_p (declarator))
15686 initializer = cp_parser_pure_specifier (parser);
15688 /* Parse the initializer. */
15689 initializer = cp_parser_constant_initializer (parser);
15691 /* Otherwise, there is no initializer. */
15693 initializer = NULL_TREE;
15695 /* See if we are probably looking at a function
15696 definition. We are certainly not looking at a
15697 member-declarator. Calling `grokfield' has
15698 side-effects, so we must not do it unless we are sure
15699 that we are looking at a member-declarator. */
15700 if (cp_parser_token_starts_function_definition_p
15701 (cp_lexer_peek_token (parser->lexer)))
15703 /* The grammar does not allow a pure-specifier to be
15704 used when a member function is defined. (It is
15705 possible that this fact is an oversight in the
15706 standard, since a pure function may be defined
15707 outside of the class-specifier. */
15709 error ("%Hpure-specifier on function-definition",
15710 &initializer_token_start->location);
15711 decl = cp_parser_save_member_function_body (parser,
15715 /* If the member was not a friend, declare it here. */
15717 finish_member_declaration (decl);
15718 /* Peek at the next token. */
15719 token = cp_lexer_peek_token (parser->lexer);
15720 /* If the next token is a semicolon, consume it. */
15721 if (token->type == CPP_SEMICOLON)
15722 cp_lexer_consume_token (parser->lexer);
15726 /* Create the declaration. */
15727 decl = grokfield (declarator, &decl_specifiers,
15728 initializer, /*init_const_expr_p=*/true,
15733 /* Reset PREFIX_ATTRIBUTES. */
15734 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15735 attributes = TREE_CHAIN (attributes);
15737 TREE_CHAIN (attributes) = NULL_TREE;
15739 /* If there is any qualification still in effect, clear it
15740 now; we will be starting fresh with the next declarator. */
15741 parser->scope = NULL_TREE;
15742 parser->qualifying_scope = NULL_TREE;
15743 parser->object_scope = NULL_TREE;
15744 /* If it's a `,', then there are more declarators. */
15745 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15746 cp_lexer_consume_token (parser->lexer);
15747 /* If the next token isn't a `;', then we have a parse error. */
15748 else if (cp_lexer_next_token_is_not (parser->lexer,
15751 cp_parser_error (parser, "expected %<;%>");
15752 /* Skip tokens until we find a `;'. */
15753 cp_parser_skip_to_end_of_statement (parser);
15760 /* Add DECL to the list of members. */
15762 finish_member_declaration (decl);
15764 if (TREE_CODE (decl) == FUNCTION_DECL)
15765 cp_parser_save_default_args (parser, decl);
15770 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15773 /* Parse a pure-specifier.
15778 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15779 Otherwise, ERROR_MARK_NODE is returned. */
15782 cp_parser_pure_specifier (cp_parser* parser)
15786 /* Look for the `=' token. */
15787 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15788 return error_mark_node;
15789 /* Look for the `0' token. */
15790 token = cp_lexer_consume_token (parser->lexer);
15792 /* Accept = default or = delete in c++0x mode. */
15793 if (token->keyword == RID_DEFAULT
15794 || token->keyword == RID_DELETE)
15796 maybe_warn_cpp0x ("defaulted and deleted functions");
15797 return token->u.value;
15800 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15801 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15803 cp_parser_error (parser,
15804 "invalid pure specifier (only %<= 0%> is allowed)");
15805 cp_parser_skip_to_end_of_statement (parser);
15806 return error_mark_node;
15808 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15810 error ("%Htemplates may not be %<virtual%>", &token->location);
15811 return error_mark_node;
15814 return integer_zero_node;
15817 /* Parse a constant-initializer.
15819 constant-initializer:
15820 = constant-expression
15822 Returns a representation of the constant-expression. */
15825 cp_parser_constant_initializer (cp_parser* parser)
15827 /* Look for the `=' token. */
15828 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15829 return error_mark_node;
15831 /* It is invalid to write:
15833 struct S { static const int i = { 7 }; };
15836 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15838 cp_parser_error (parser,
15839 "a brace-enclosed initializer is not allowed here");
15840 /* Consume the opening brace. */
15841 cp_lexer_consume_token (parser->lexer);
15842 /* Skip the initializer. */
15843 cp_parser_skip_to_closing_brace (parser);
15844 /* Look for the trailing `}'. */
15845 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15847 return error_mark_node;
15850 return cp_parser_constant_expression (parser,
15851 /*allow_non_constant=*/false,
15855 /* Derived classes [gram.class.derived] */
15857 /* Parse a base-clause.
15860 : base-specifier-list
15862 base-specifier-list:
15863 base-specifier ... [opt]
15864 base-specifier-list , base-specifier ... [opt]
15866 Returns a TREE_LIST representing the base-classes, in the order in
15867 which they were declared. The representation of each node is as
15868 described by cp_parser_base_specifier.
15870 In the case that no bases are specified, this function will return
15871 NULL_TREE, not ERROR_MARK_NODE. */
15874 cp_parser_base_clause (cp_parser* parser)
15876 tree bases = NULL_TREE;
15878 /* Look for the `:' that begins the list. */
15879 cp_parser_require (parser, CPP_COLON, "%<:%>");
15881 /* Scan the base-specifier-list. */
15886 bool pack_expansion_p = false;
15888 /* Look for the base-specifier. */
15889 base = cp_parser_base_specifier (parser);
15890 /* Look for the (optional) ellipsis. */
15891 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15893 /* Consume the `...'. */
15894 cp_lexer_consume_token (parser->lexer);
15896 pack_expansion_p = true;
15899 /* Add BASE to the front of the list. */
15900 if (base != error_mark_node)
15902 if (pack_expansion_p)
15903 /* Make this a pack expansion type. */
15904 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15907 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15909 TREE_CHAIN (base) = bases;
15913 /* Peek at the next token. */
15914 token = cp_lexer_peek_token (parser->lexer);
15915 /* If it's not a comma, then the list is complete. */
15916 if (token->type != CPP_COMMA)
15918 /* Consume the `,'. */
15919 cp_lexer_consume_token (parser->lexer);
15922 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15923 base class had a qualified name. However, the next name that
15924 appears is certainly not qualified. */
15925 parser->scope = NULL_TREE;
15926 parser->qualifying_scope = NULL_TREE;
15927 parser->object_scope = NULL_TREE;
15929 return nreverse (bases);
15932 /* Parse a base-specifier.
15935 :: [opt] nested-name-specifier [opt] class-name
15936 virtual access-specifier [opt] :: [opt] nested-name-specifier
15938 access-specifier virtual [opt] :: [opt] nested-name-specifier
15941 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15942 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15943 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15944 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15947 cp_parser_base_specifier (cp_parser* parser)
15951 bool virtual_p = false;
15952 bool duplicate_virtual_error_issued_p = false;
15953 bool duplicate_access_error_issued_p = false;
15954 bool class_scope_p, template_p;
15955 tree access = access_default_node;
15958 /* Process the optional `virtual' and `access-specifier'. */
15961 /* Peek at the next token. */
15962 token = cp_lexer_peek_token (parser->lexer);
15963 /* Process `virtual'. */
15964 switch (token->keyword)
15967 /* If `virtual' appears more than once, issue an error. */
15968 if (virtual_p && !duplicate_virtual_error_issued_p)
15970 cp_parser_error (parser,
15971 "%<virtual%> specified more than once in base-specified");
15972 duplicate_virtual_error_issued_p = true;
15977 /* Consume the `virtual' token. */
15978 cp_lexer_consume_token (parser->lexer);
15983 case RID_PROTECTED:
15985 /* If more than one access specifier appears, issue an
15987 if (access != access_default_node
15988 && !duplicate_access_error_issued_p)
15990 cp_parser_error (parser,
15991 "more than one access specifier in base-specified");
15992 duplicate_access_error_issued_p = true;
15995 access = ridpointers[(int) token->keyword];
15997 /* Consume the access-specifier. */
15998 cp_lexer_consume_token (parser->lexer);
16007 /* It is not uncommon to see programs mechanically, erroneously, use
16008 the 'typename' keyword to denote (dependent) qualified types
16009 as base classes. */
16010 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16012 token = cp_lexer_peek_token (parser->lexer);
16013 if (!processing_template_decl)
16014 error ("%Hkeyword %<typename%> not allowed outside of templates",
16017 error ("%Hkeyword %<typename%> not allowed in this context "
16018 "(the base class is implicitly a type)",
16020 cp_lexer_consume_token (parser->lexer);
16023 /* Look for the optional `::' operator. */
16024 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16025 /* Look for the nested-name-specifier. The simplest way to
16030 The keyword `typename' is not permitted in a base-specifier or
16031 mem-initializer; in these contexts a qualified name that
16032 depends on a template-parameter is implicitly assumed to be a
16035 is to pretend that we have seen the `typename' keyword at this
16037 cp_parser_nested_name_specifier_opt (parser,
16038 /*typename_keyword_p=*/true,
16039 /*check_dependency_p=*/true,
16041 /*is_declaration=*/true);
16042 /* If the base class is given by a qualified name, assume that names
16043 we see are type names or templates, as appropriate. */
16044 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16045 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16047 /* Finally, look for the class-name. */
16048 type = cp_parser_class_name (parser,
16052 /*check_dependency_p=*/true,
16053 /*class_head_p=*/false,
16054 /*is_declaration=*/true);
16056 if (type == error_mark_node)
16057 return error_mark_node;
16059 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16062 /* Exception handling [gram.exception] */
16064 /* Parse an (optional) exception-specification.
16066 exception-specification:
16067 throw ( type-id-list [opt] )
16069 Returns a TREE_LIST representing the exception-specification. The
16070 TREE_VALUE of each node is a type. */
16073 cp_parser_exception_specification_opt (cp_parser* parser)
16078 /* Peek at the next token. */
16079 token = cp_lexer_peek_token (parser->lexer);
16080 /* If it's not `throw', then there's no exception-specification. */
16081 if (!cp_parser_is_keyword (token, RID_THROW))
16084 /* Consume the `throw'. */
16085 cp_lexer_consume_token (parser->lexer);
16087 /* Look for the `('. */
16088 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16090 /* Peek at the next token. */
16091 token = cp_lexer_peek_token (parser->lexer);
16092 /* If it's not a `)', then there is a type-id-list. */
16093 if (token->type != CPP_CLOSE_PAREN)
16095 const char *saved_message;
16097 /* Types may not be defined in an exception-specification. */
16098 saved_message = parser->type_definition_forbidden_message;
16099 parser->type_definition_forbidden_message
16100 = "types may not be defined in an exception-specification";
16101 /* Parse the type-id-list. */
16102 type_id_list = cp_parser_type_id_list (parser);
16103 /* Restore the saved message. */
16104 parser->type_definition_forbidden_message = saved_message;
16107 type_id_list = empty_except_spec;
16109 /* Look for the `)'. */
16110 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16112 return type_id_list;
16115 /* Parse an (optional) type-id-list.
16119 type-id-list , type-id ... [opt]
16121 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16122 in the order that the types were presented. */
16125 cp_parser_type_id_list (cp_parser* parser)
16127 tree types = NULL_TREE;
16134 /* Get the next type-id. */
16135 type = cp_parser_type_id (parser);
16136 /* Parse the optional ellipsis. */
16137 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16139 /* Consume the `...'. */
16140 cp_lexer_consume_token (parser->lexer);
16142 /* Turn the type into a pack expansion expression. */
16143 type = make_pack_expansion (type);
16145 /* Add it to the list. */
16146 types = add_exception_specifier (types, type, /*complain=*/1);
16147 /* Peek at the next token. */
16148 token = cp_lexer_peek_token (parser->lexer);
16149 /* If it is not a `,', we are done. */
16150 if (token->type != CPP_COMMA)
16152 /* Consume the `,'. */
16153 cp_lexer_consume_token (parser->lexer);
16156 return nreverse (types);
16159 /* Parse a try-block.
16162 try compound-statement handler-seq */
16165 cp_parser_try_block (cp_parser* parser)
16169 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16170 try_block = begin_try_block ();
16171 cp_parser_compound_statement (parser, NULL, true);
16172 finish_try_block (try_block);
16173 cp_parser_handler_seq (parser);
16174 finish_handler_sequence (try_block);
16179 /* Parse a function-try-block.
16181 function-try-block:
16182 try ctor-initializer [opt] function-body handler-seq */
16185 cp_parser_function_try_block (cp_parser* parser)
16187 tree compound_stmt;
16189 bool ctor_initializer_p;
16191 /* Look for the `try' keyword. */
16192 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16194 /* Let the rest of the front end know where we are. */
16195 try_block = begin_function_try_block (&compound_stmt);
16196 /* Parse the function-body. */
16198 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16199 /* We're done with the `try' part. */
16200 finish_function_try_block (try_block);
16201 /* Parse the handlers. */
16202 cp_parser_handler_seq (parser);
16203 /* We're done with the handlers. */
16204 finish_function_handler_sequence (try_block, compound_stmt);
16206 return ctor_initializer_p;
16209 /* Parse a handler-seq.
16212 handler handler-seq [opt] */
16215 cp_parser_handler_seq (cp_parser* parser)
16221 /* Parse the handler. */
16222 cp_parser_handler (parser);
16223 /* Peek at the next token. */
16224 token = cp_lexer_peek_token (parser->lexer);
16225 /* If it's not `catch' then there are no more handlers. */
16226 if (!cp_parser_is_keyword (token, RID_CATCH))
16231 /* Parse a handler.
16234 catch ( exception-declaration ) compound-statement */
16237 cp_parser_handler (cp_parser* parser)
16242 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16243 handler = begin_handler ();
16244 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16245 declaration = cp_parser_exception_declaration (parser);
16246 finish_handler_parms (declaration, handler);
16247 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16248 cp_parser_compound_statement (parser, NULL, false);
16249 finish_handler (handler);
16252 /* Parse an exception-declaration.
16254 exception-declaration:
16255 type-specifier-seq declarator
16256 type-specifier-seq abstract-declarator
16260 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16261 ellipsis variant is used. */
16264 cp_parser_exception_declaration (cp_parser* parser)
16266 cp_decl_specifier_seq type_specifiers;
16267 cp_declarator *declarator;
16268 const char *saved_message;
16270 /* If it's an ellipsis, it's easy to handle. */
16271 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16273 /* Consume the `...' token. */
16274 cp_lexer_consume_token (parser->lexer);
16278 /* Types may not be defined in exception-declarations. */
16279 saved_message = parser->type_definition_forbidden_message;
16280 parser->type_definition_forbidden_message
16281 = "types may not be defined in exception-declarations";
16283 /* Parse the type-specifier-seq. */
16284 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16286 /* If it's a `)', then there is no declarator. */
16287 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16290 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16291 /*ctor_dtor_or_conv_p=*/NULL,
16292 /*parenthesized_p=*/NULL,
16293 /*member_p=*/false);
16295 /* Restore the saved message. */
16296 parser->type_definition_forbidden_message = saved_message;
16298 if (!type_specifiers.any_specifiers_p)
16299 return error_mark_node;
16301 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16304 /* Parse a throw-expression.
16307 throw assignment-expression [opt]
16309 Returns a THROW_EXPR representing the throw-expression. */
16312 cp_parser_throw_expression (cp_parser* parser)
16317 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16318 token = cp_lexer_peek_token (parser->lexer);
16319 /* Figure out whether or not there is an assignment-expression
16320 following the "throw" keyword. */
16321 if (token->type == CPP_COMMA
16322 || token->type == CPP_SEMICOLON
16323 || token->type == CPP_CLOSE_PAREN
16324 || token->type == CPP_CLOSE_SQUARE
16325 || token->type == CPP_CLOSE_BRACE
16326 || token->type == CPP_COLON)
16327 expression = NULL_TREE;
16329 expression = cp_parser_assignment_expression (parser,
16332 return build_throw (expression);
16335 /* GNU Extensions */
16337 /* Parse an (optional) asm-specification.
16340 asm ( string-literal )
16342 If the asm-specification is present, returns a STRING_CST
16343 corresponding to the string-literal. Otherwise, returns
16347 cp_parser_asm_specification_opt (cp_parser* parser)
16350 tree asm_specification;
16352 /* Peek at the next token. */
16353 token = cp_lexer_peek_token (parser->lexer);
16354 /* If the next token isn't the `asm' keyword, then there's no
16355 asm-specification. */
16356 if (!cp_parser_is_keyword (token, RID_ASM))
16359 /* Consume the `asm' token. */
16360 cp_lexer_consume_token (parser->lexer);
16361 /* Look for the `('. */
16362 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16364 /* Look for the string-literal. */
16365 asm_specification = cp_parser_string_literal (parser, false, false);
16367 /* Look for the `)'. */
16368 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16370 return asm_specification;
16373 /* Parse an asm-operand-list.
16377 asm-operand-list , asm-operand
16380 string-literal ( expression )
16381 [ string-literal ] string-literal ( expression )
16383 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16384 each node is the expression. The TREE_PURPOSE is itself a
16385 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16386 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16387 is a STRING_CST for the string literal before the parenthesis. Returns
16388 ERROR_MARK_NODE if any of the operands are invalid. */
16391 cp_parser_asm_operand_list (cp_parser* parser)
16393 tree asm_operands = NULL_TREE;
16394 bool invalid_operands = false;
16398 tree string_literal;
16402 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16404 /* Consume the `[' token. */
16405 cp_lexer_consume_token (parser->lexer);
16406 /* Read the operand name. */
16407 name = cp_parser_identifier (parser);
16408 if (name != error_mark_node)
16409 name = build_string (IDENTIFIER_LENGTH (name),
16410 IDENTIFIER_POINTER (name));
16411 /* Look for the closing `]'. */
16412 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16416 /* Look for the string-literal. */
16417 string_literal = cp_parser_string_literal (parser, false, false);
16419 /* Look for the `('. */
16420 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16421 /* Parse the expression. */
16422 expression = cp_parser_expression (parser, /*cast_p=*/false);
16423 /* Look for the `)'. */
16424 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16426 if (name == error_mark_node
16427 || string_literal == error_mark_node
16428 || expression == error_mark_node)
16429 invalid_operands = true;
16431 /* Add this operand to the list. */
16432 asm_operands = tree_cons (build_tree_list (name, string_literal),
16435 /* If the next token is not a `,', there are no more
16437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16439 /* Consume the `,'. */
16440 cp_lexer_consume_token (parser->lexer);
16443 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16446 /* Parse an asm-clobber-list.
16450 asm-clobber-list , string-literal
16452 Returns a TREE_LIST, indicating the clobbers in the order that they
16453 appeared. The TREE_VALUE of each node is a STRING_CST. */
16456 cp_parser_asm_clobber_list (cp_parser* parser)
16458 tree clobbers = NULL_TREE;
16462 tree string_literal;
16464 /* Look for the string literal. */
16465 string_literal = cp_parser_string_literal (parser, false, false);
16466 /* Add it to the list. */
16467 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16468 /* If the next token is not a `,', then the list is
16470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16472 /* Consume the `,' token. */
16473 cp_lexer_consume_token (parser->lexer);
16479 /* Parse an (optional) series of attributes.
16482 attributes attribute
16485 __attribute__ (( attribute-list [opt] ))
16487 The return value is as for cp_parser_attribute_list. */
16490 cp_parser_attributes_opt (cp_parser* parser)
16492 tree attributes = NULL_TREE;
16497 tree attribute_list;
16499 /* Peek at the next token. */
16500 token = cp_lexer_peek_token (parser->lexer);
16501 /* If it's not `__attribute__', then we're done. */
16502 if (token->keyword != RID_ATTRIBUTE)
16505 /* Consume the `__attribute__' keyword. */
16506 cp_lexer_consume_token (parser->lexer);
16507 /* Look for the two `(' tokens. */
16508 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16509 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16511 /* Peek at the next token. */
16512 token = cp_lexer_peek_token (parser->lexer);
16513 if (token->type != CPP_CLOSE_PAREN)
16514 /* Parse the attribute-list. */
16515 attribute_list = cp_parser_attribute_list (parser);
16517 /* If the next token is a `)', then there is no attribute
16519 attribute_list = NULL;
16521 /* Look for the two `)' tokens. */
16522 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16523 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16525 /* Add these new attributes to the list. */
16526 attributes = chainon (attributes, attribute_list);
16532 /* Parse an attribute-list.
16536 attribute-list , attribute
16540 identifier ( identifier )
16541 identifier ( identifier , expression-list )
16542 identifier ( expression-list )
16544 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16545 to an attribute. The TREE_PURPOSE of each node is the identifier
16546 indicating which attribute is in use. The TREE_VALUE represents
16547 the arguments, if any. */
16550 cp_parser_attribute_list (cp_parser* parser)
16552 tree attribute_list = NULL_TREE;
16553 bool save_translate_strings_p = parser->translate_strings_p;
16555 parser->translate_strings_p = false;
16562 /* Look for the identifier. We also allow keywords here; for
16563 example `__attribute__ ((const))' is legal. */
16564 token = cp_lexer_peek_token (parser->lexer);
16565 if (token->type == CPP_NAME
16566 || token->type == CPP_KEYWORD)
16568 tree arguments = NULL_TREE;
16570 /* Consume the token. */
16571 token = cp_lexer_consume_token (parser->lexer);
16573 /* Save away the identifier that indicates which attribute
16575 identifier = token->u.value;
16576 attribute = build_tree_list (identifier, NULL_TREE);
16578 /* Peek at the next token. */
16579 token = cp_lexer_peek_token (parser->lexer);
16580 /* If it's an `(', then parse the attribute arguments. */
16581 if (token->type == CPP_OPEN_PAREN)
16583 arguments = cp_parser_parenthesized_expression_list
16584 (parser, true, /*cast_p=*/false,
16585 /*allow_expansion_p=*/false,
16586 /*non_constant_p=*/NULL);
16587 /* Save the arguments away. */
16588 TREE_VALUE (attribute) = arguments;
16591 if (arguments != error_mark_node)
16593 /* Add this attribute to the list. */
16594 TREE_CHAIN (attribute) = attribute_list;
16595 attribute_list = attribute;
16598 token = cp_lexer_peek_token (parser->lexer);
16600 /* Now, look for more attributes. If the next token isn't a
16601 `,', we're done. */
16602 if (token->type != CPP_COMMA)
16605 /* Consume the comma and keep going. */
16606 cp_lexer_consume_token (parser->lexer);
16608 parser->translate_strings_p = save_translate_strings_p;
16610 /* We built up the list in reverse order. */
16611 return nreverse (attribute_list);
16614 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16615 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16616 current value of the PEDANTIC flag, regardless of whether or not
16617 the `__extension__' keyword is present. The caller is responsible
16618 for restoring the value of the PEDANTIC flag. */
16621 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16623 /* Save the old value of the PEDANTIC flag. */
16624 *saved_pedantic = pedantic;
16626 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16628 /* Consume the `__extension__' token. */
16629 cp_lexer_consume_token (parser->lexer);
16630 /* We're not being pedantic while the `__extension__' keyword is
16640 /* Parse a label declaration.
16643 __label__ label-declarator-seq ;
16645 label-declarator-seq:
16646 identifier , label-declarator-seq
16650 cp_parser_label_declaration (cp_parser* parser)
16652 /* Look for the `__label__' keyword. */
16653 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16659 /* Look for an identifier. */
16660 identifier = cp_parser_identifier (parser);
16661 /* If we failed, stop. */
16662 if (identifier == error_mark_node)
16664 /* Declare it as a label. */
16665 finish_label_decl (identifier);
16666 /* If the next token is a `;', stop. */
16667 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16669 /* Look for the `,' separating the label declarations. */
16670 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16673 /* Look for the final `;'. */
16674 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16677 /* Support Functions */
16679 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16680 NAME should have one of the representations used for an
16681 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16682 is returned. If PARSER->SCOPE is a dependent type, then a
16683 SCOPE_REF is returned.
16685 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16686 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16687 was formed. Abstractly, such entities should not be passed to this
16688 function, because they do not need to be looked up, but it is
16689 simpler to check for this special case here, rather than at the
16692 In cases not explicitly covered above, this function returns a
16693 DECL, OVERLOAD, or baselink representing the result of the lookup.
16694 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16697 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16698 (e.g., "struct") that was used. In that case bindings that do not
16699 refer to types are ignored.
16701 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16704 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16707 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16710 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16711 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16712 NULL_TREE otherwise. */
16715 cp_parser_lookup_name (cp_parser *parser, tree name,
16716 enum tag_types tag_type,
16719 bool check_dependency,
16720 tree *ambiguous_decls,
16721 location_t name_location)
16725 tree object_type = parser->context->object_type;
16727 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16728 flags |= LOOKUP_COMPLAIN;
16730 /* Assume that the lookup will be unambiguous. */
16731 if (ambiguous_decls)
16732 *ambiguous_decls = NULL_TREE;
16734 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16735 no longer valid. Note that if we are parsing tentatively, and
16736 the parse fails, OBJECT_TYPE will be automatically restored. */
16737 parser->context->object_type = NULL_TREE;
16739 if (name == error_mark_node)
16740 return error_mark_node;
16742 /* A template-id has already been resolved; there is no lookup to
16744 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16746 if (BASELINK_P (name))
16748 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16749 == TEMPLATE_ID_EXPR);
16753 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16754 it should already have been checked to make sure that the name
16755 used matches the type being destroyed. */
16756 if (TREE_CODE (name) == BIT_NOT_EXPR)
16760 /* Figure out to which type this destructor applies. */
16762 type = parser->scope;
16763 else if (object_type)
16764 type = object_type;
16766 type = current_class_type;
16767 /* If that's not a class type, there is no destructor. */
16768 if (!type || !CLASS_TYPE_P (type))
16769 return error_mark_node;
16770 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16771 lazily_declare_fn (sfk_destructor, type);
16772 if (!CLASSTYPE_DESTRUCTORS (type))
16773 return error_mark_node;
16774 /* If it was a class type, return the destructor. */
16775 return CLASSTYPE_DESTRUCTORS (type);
16778 /* By this point, the NAME should be an ordinary identifier. If
16779 the id-expression was a qualified name, the qualifying scope is
16780 stored in PARSER->SCOPE at this point. */
16781 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16783 /* Perform the lookup. */
16788 if (parser->scope == error_mark_node)
16789 return error_mark_node;
16791 /* If the SCOPE is dependent, the lookup must be deferred until
16792 the template is instantiated -- unless we are explicitly
16793 looking up names in uninstantiated templates. Even then, we
16794 cannot look up the name if the scope is not a class type; it
16795 might, for example, be a template type parameter. */
16796 dependent_p = (TYPE_P (parser->scope)
16797 && !(parser->in_declarator_p
16798 && currently_open_class (parser->scope))
16799 && dependent_type_p (parser->scope));
16800 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16807 /* The resolution to Core Issue 180 says that `struct
16808 A::B' should be considered a type-name, even if `A'
16810 type = make_typename_type (parser->scope, name, tag_type,
16811 /*complain=*/tf_error);
16812 decl = TYPE_NAME (type);
16814 else if (is_template
16815 && (cp_parser_next_token_ends_template_argument_p (parser)
16816 || cp_lexer_next_token_is (parser->lexer,
16818 decl = make_unbound_class_template (parser->scope,
16820 /*complain=*/tf_error);
16822 decl = build_qualified_name (/*type=*/NULL_TREE,
16823 parser->scope, name,
16828 tree pushed_scope = NULL_TREE;
16830 /* If PARSER->SCOPE is a dependent type, then it must be a
16831 class type, and we must not be checking dependencies;
16832 otherwise, we would have processed this lookup above. So
16833 that PARSER->SCOPE is not considered a dependent base by
16834 lookup_member, we must enter the scope here. */
16836 pushed_scope = push_scope (parser->scope);
16837 /* If the PARSER->SCOPE is a template specialization, it
16838 may be instantiated during name lookup. In that case,
16839 errors may be issued. Even if we rollback the current
16840 tentative parse, those errors are valid. */
16841 decl = lookup_qualified_name (parser->scope, name,
16842 tag_type != none_type,
16843 /*complain=*/true);
16845 /* If we have a single function from a using decl, pull it out. */
16847 && TREE_CODE (decl) == OVERLOAD
16848 && !really_overloaded_fn (decl))
16849 decl = OVL_FUNCTION (decl);
16852 pop_scope (pushed_scope);
16854 parser->qualifying_scope = parser->scope;
16855 parser->object_scope = NULL_TREE;
16857 else if (object_type)
16859 tree object_decl = NULL_TREE;
16860 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16861 OBJECT_TYPE is not a class. */
16862 if (CLASS_TYPE_P (object_type))
16863 /* If the OBJECT_TYPE is a template specialization, it may
16864 be instantiated during name lookup. In that case, errors
16865 may be issued. Even if we rollback the current tentative
16866 parse, those errors are valid. */
16867 object_decl = lookup_member (object_type,
16870 tag_type != none_type);
16871 /* Look it up in the enclosing context, too. */
16872 decl = lookup_name_real (name, tag_type != none_type,
16874 /*block_p=*/true, is_namespace, flags);
16875 parser->object_scope = object_type;
16876 parser->qualifying_scope = NULL_TREE;
16878 decl = object_decl;
16882 decl = lookup_name_real (name, tag_type != none_type,
16884 /*block_p=*/true, is_namespace, flags);
16885 parser->qualifying_scope = NULL_TREE;
16886 parser->object_scope = NULL_TREE;
16889 /* If the lookup failed, let our caller know. */
16890 if (!decl || decl == error_mark_node)
16891 return error_mark_node;
16893 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16894 if (TREE_CODE (decl) == TREE_LIST)
16896 if (ambiguous_decls)
16897 *ambiguous_decls = decl;
16898 /* The error message we have to print is too complicated for
16899 cp_parser_error, so we incorporate its actions directly. */
16900 if (!cp_parser_simulate_error (parser))
16902 error ("%Hreference to %qD is ambiguous",
16903 &name_location, name);
16904 print_candidates (decl);
16906 return error_mark_node;
16909 gcc_assert (DECL_P (decl)
16910 || TREE_CODE (decl) == OVERLOAD
16911 || TREE_CODE (decl) == SCOPE_REF
16912 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16913 || BASELINK_P (decl));
16915 /* If we have resolved the name of a member declaration, check to
16916 see if the declaration is accessible. When the name resolves to
16917 set of overloaded functions, accessibility is checked when
16918 overload resolution is done.
16920 During an explicit instantiation, access is not checked at all,
16921 as per [temp.explicit]. */
16923 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16928 /* Like cp_parser_lookup_name, but for use in the typical case where
16929 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16930 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16933 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
16935 return cp_parser_lookup_name (parser, name,
16937 /*is_template=*/false,
16938 /*is_namespace=*/false,
16939 /*check_dependency=*/true,
16940 /*ambiguous_decls=*/NULL,
16944 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16945 the current context, return the TYPE_DECL. If TAG_NAME_P is
16946 true, the DECL indicates the class being defined in a class-head,
16947 or declared in an elaborated-type-specifier.
16949 Otherwise, return DECL. */
16952 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16954 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16955 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16958 template <typename T> struct B;
16961 template <typename T> struct A::B {};
16963 Similarly, in an elaborated-type-specifier:
16965 namespace N { struct X{}; }
16968 template <typename T> friend struct N::X;
16971 However, if the DECL refers to a class type, and we are in
16972 the scope of the class, then the name lookup automatically
16973 finds the TYPE_DECL created by build_self_reference rather
16974 than a TEMPLATE_DECL. For example, in:
16976 template <class T> struct S {
16980 there is no need to handle such case. */
16982 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16983 return DECL_TEMPLATE_RESULT (decl);
16988 /* If too many, or too few, template-parameter lists apply to the
16989 declarator, issue an error message. Returns TRUE if all went well,
16990 and FALSE otherwise. */
16993 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16994 cp_declarator *declarator,
16995 location_t declarator_location)
16997 unsigned num_templates;
16999 /* We haven't seen any classes that involve template parameters yet. */
17002 switch (declarator->kind)
17005 if (declarator->u.id.qualifying_scope)
17010 scope = declarator->u.id.qualifying_scope;
17011 member = declarator->u.id.unqualified_name;
17013 while (scope && CLASS_TYPE_P (scope))
17015 /* You're supposed to have one `template <...>'
17016 for every template class, but you don't need one
17017 for a full specialization. For example:
17019 template <class T> struct S{};
17020 template <> struct S<int> { void f(); };
17021 void S<int>::f () {}
17023 is correct; there shouldn't be a `template <>' for
17024 the definition of `S<int>::f'. */
17025 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17026 /* If SCOPE does not have template information of any
17027 kind, then it is not a template, nor is it nested
17028 within a template. */
17030 if (explicit_class_specialization_p (scope))
17032 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17035 scope = TYPE_CONTEXT (scope);
17038 else if (TREE_CODE (declarator->u.id.unqualified_name)
17039 == TEMPLATE_ID_EXPR)
17040 /* If the DECLARATOR has the form `X<y>' then it uses one
17041 additional level of template parameters. */
17044 return cp_parser_check_template_parameters (parser,
17046 declarator_location);
17051 case cdk_reference:
17053 return (cp_parser_check_declarator_template_parameters
17054 (parser, declarator->declarator, declarator_location));
17060 gcc_unreachable ();
17065 /* NUM_TEMPLATES were used in the current declaration. If that is
17066 invalid, return FALSE and issue an error messages. Otherwise,
17070 cp_parser_check_template_parameters (cp_parser* parser,
17071 unsigned num_templates,
17072 location_t location)
17074 /* If there are more template classes than parameter lists, we have
17077 template <class T> void S<T>::R<T>::f (); */
17078 if (parser->num_template_parameter_lists < num_templates)
17080 error ("%Htoo few template-parameter-lists", &location);
17083 /* If there are the same number of template classes and parameter
17084 lists, that's OK. */
17085 if (parser->num_template_parameter_lists == num_templates)
17087 /* If there are more, but only one more, then we are referring to a
17088 member template. That's OK too. */
17089 if (parser->num_template_parameter_lists == num_templates + 1)
17091 /* Otherwise, there are too many template parameter lists. We have
17094 template <class T> template <class U> void S::f(); */
17095 error ("%Htoo many template-parameter-lists", &location);
17099 /* Parse an optional `::' token indicating that the following name is
17100 from the global namespace. If so, PARSER->SCOPE is set to the
17101 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17102 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17103 Returns the new value of PARSER->SCOPE, if the `::' token is
17104 present, and NULL_TREE otherwise. */
17107 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17111 /* Peek at the next token. */
17112 token = cp_lexer_peek_token (parser->lexer);
17113 /* If we're looking at a `::' token then we're starting from the
17114 global namespace, not our current location. */
17115 if (token->type == CPP_SCOPE)
17117 /* Consume the `::' token. */
17118 cp_lexer_consume_token (parser->lexer);
17119 /* Set the SCOPE so that we know where to start the lookup. */
17120 parser->scope = global_namespace;
17121 parser->qualifying_scope = global_namespace;
17122 parser->object_scope = NULL_TREE;
17124 return parser->scope;
17126 else if (!current_scope_valid_p)
17128 parser->scope = NULL_TREE;
17129 parser->qualifying_scope = NULL_TREE;
17130 parser->object_scope = NULL_TREE;
17136 /* Returns TRUE if the upcoming token sequence is the start of a
17137 constructor declarator. If FRIEND_P is true, the declarator is
17138 preceded by the `friend' specifier. */
17141 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17143 bool constructor_p;
17144 tree type_decl = NULL_TREE;
17145 bool nested_name_p;
17146 cp_token *next_token;
17148 /* The common case is that this is not a constructor declarator, so
17149 try to avoid doing lots of work if at all possible. It's not
17150 valid declare a constructor at function scope. */
17151 if (parser->in_function_body)
17153 /* And only certain tokens can begin a constructor declarator. */
17154 next_token = cp_lexer_peek_token (parser->lexer);
17155 if (next_token->type != CPP_NAME
17156 && next_token->type != CPP_SCOPE
17157 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17158 && next_token->type != CPP_TEMPLATE_ID)
17161 /* Parse tentatively; we are going to roll back all of the tokens
17163 cp_parser_parse_tentatively (parser);
17164 /* Assume that we are looking at a constructor declarator. */
17165 constructor_p = true;
17167 /* Look for the optional `::' operator. */
17168 cp_parser_global_scope_opt (parser,
17169 /*current_scope_valid_p=*/false);
17170 /* Look for the nested-name-specifier. */
17172 = (cp_parser_nested_name_specifier_opt (parser,
17173 /*typename_keyword_p=*/false,
17174 /*check_dependency_p=*/false,
17176 /*is_declaration=*/false)
17178 /* Outside of a class-specifier, there must be a
17179 nested-name-specifier. */
17180 if (!nested_name_p &&
17181 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17183 constructor_p = false;
17184 /* If we still think that this might be a constructor-declarator,
17185 look for a class-name. */
17190 template <typename T> struct S { S(); };
17191 template <typename T> S<T>::S ();
17193 we must recognize that the nested `S' names a class.
17196 template <typename T> S<T>::S<T> ();
17198 we must recognize that the nested `S' names a template. */
17199 type_decl = cp_parser_class_name (parser,
17200 /*typename_keyword_p=*/false,
17201 /*template_keyword_p=*/false,
17203 /*check_dependency_p=*/false,
17204 /*class_head_p=*/false,
17205 /*is_declaration=*/false);
17206 /* If there was no class-name, then this is not a constructor. */
17207 constructor_p = !cp_parser_error_occurred (parser);
17210 /* If we're still considering a constructor, we have to see a `(',
17211 to begin the parameter-declaration-clause, followed by either a
17212 `)', an `...', or a decl-specifier. We need to check for a
17213 type-specifier to avoid being fooled into thinking that:
17217 is a constructor. (It is actually a function named `f' that
17218 takes one parameter (of type `int') and returns a value of type
17221 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17224 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17225 /* A parameter declaration begins with a decl-specifier,
17226 which is either the "attribute" keyword, a storage class
17227 specifier, or (usually) a type-specifier. */
17228 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17231 tree pushed_scope = NULL_TREE;
17232 unsigned saved_num_template_parameter_lists;
17234 /* Names appearing in the type-specifier should be looked up
17235 in the scope of the class. */
17236 if (current_class_type)
17240 type = TREE_TYPE (type_decl);
17241 if (TREE_CODE (type) == TYPENAME_TYPE)
17243 type = resolve_typename_type (type,
17244 /*only_current_p=*/false);
17245 if (TREE_CODE (type) == TYPENAME_TYPE)
17247 cp_parser_abort_tentative_parse (parser);
17251 pushed_scope = push_scope (type);
17254 /* Inside the constructor parameter list, surrounding
17255 template-parameter-lists do not apply. */
17256 saved_num_template_parameter_lists
17257 = parser->num_template_parameter_lists;
17258 parser->num_template_parameter_lists = 0;
17260 /* Look for the type-specifier. */
17261 cp_parser_type_specifier (parser,
17262 CP_PARSER_FLAGS_NONE,
17263 /*decl_specs=*/NULL,
17264 /*is_declarator=*/true,
17265 /*declares_class_or_enum=*/NULL,
17266 /*is_cv_qualifier=*/NULL);
17268 parser->num_template_parameter_lists
17269 = saved_num_template_parameter_lists;
17271 /* Leave the scope of the class. */
17273 pop_scope (pushed_scope);
17275 constructor_p = !cp_parser_error_occurred (parser);
17279 constructor_p = false;
17280 /* We did not really want to consume any tokens. */
17281 cp_parser_abort_tentative_parse (parser);
17283 return constructor_p;
17286 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17287 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17288 they must be performed once we are in the scope of the function.
17290 Returns the function defined. */
17293 cp_parser_function_definition_from_specifiers_and_declarator
17294 (cp_parser* parser,
17295 cp_decl_specifier_seq *decl_specifiers,
17297 const cp_declarator *declarator)
17302 /* Begin the function-definition. */
17303 success_p = start_function (decl_specifiers, declarator, attributes);
17305 /* The things we're about to see are not directly qualified by any
17306 template headers we've seen thus far. */
17307 reset_specialization ();
17309 /* If there were names looked up in the decl-specifier-seq that we
17310 did not check, check them now. We must wait until we are in the
17311 scope of the function to perform the checks, since the function
17312 might be a friend. */
17313 perform_deferred_access_checks ();
17317 /* Skip the entire function. */
17318 cp_parser_skip_to_end_of_block_or_statement (parser);
17319 fn = error_mark_node;
17321 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17323 /* Seen already, skip it. An error message has already been output. */
17324 cp_parser_skip_to_end_of_block_or_statement (parser);
17325 fn = current_function_decl;
17326 current_function_decl = NULL_TREE;
17327 /* If this is a function from a class, pop the nested class. */
17328 if (current_class_name)
17329 pop_nested_class ();
17332 fn = cp_parser_function_definition_after_declarator (parser,
17333 /*inline_p=*/false);
17338 /* Parse the part of a function-definition that follows the
17339 declarator. INLINE_P is TRUE iff this function is an inline
17340 function defined with a class-specifier.
17342 Returns the function defined. */
17345 cp_parser_function_definition_after_declarator (cp_parser* parser,
17349 bool ctor_initializer_p = false;
17350 bool saved_in_unbraced_linkage_specification_p;
17351 bool saved_in_function_body;
17352 unsigned saved_num_template_parameter_lists;
17355 saved_in_function_body = parser->in_function_body;
17356 parser->in_function_body = true;
17357 /* If the next token is `return', then the code may be trying to
17358 make use of the "named return value" extension that G++ used to
17360 token = cp_lexer_peek_token (parser->lexer);
17361 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17363 /* Consume the `return' keyword. */
17364 cp_lexer_consume_token (parser->lexer);
17365 /* Look for the identifier that indicates what value is to be
17367 cp_parser_identifier (parser);
17368 /* Issue an error message. */
17369 error ("%Hnamed return values are no longer supported",
17371 /* Skip tokens until we reach the start of the function body. */
17374 cp_token *token = cp_lexer_peek_token (parser->lexer);
17375 if (token->type == CPP_OPEN_BRACE
17376 || token->type == CPP_EOF
17377 || token->type == CPP_PRAGMA_EOL)
17379 cp_lexer_consume_token (parser->lexer);
17382 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17383 anything declared inside `f'. */
17384 saved_in_unbraced_linkage_specification_p
17385 = parser->in_unbraced_linkage_specification_p;
17386 parser->in_unbraced_linkage_specification_p = false;
17387 /* Inside the function, surrounding template-parameter-lists do not
17389 saved_num_template_parameter_lists
17390 = parser->num_template_parameter_lists;
17391 parser->num_template_parameter_lists = 0;
17392 /* If the next token is `try', then we are looking at a
17393 function-try-block. */
17394 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17395 ctor_initializer_p = cp_parser_function_try_block (parser);
17396 /* A function-try-block includes the function-body, so we only do
17397 this next part if we're not processing a function-try-block. */
17400 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17402 /* Finish the function. */
17403 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17404 (inline_p ? 2 : 0));
17405 /* Generate code for it, if necessary. */
17406 expand_or_defer_fn (fn);
17407 /* Restore the saved values. */
17408 parser->in_unbraced_linkage_specification_p
17409 = saved_in_unbraced_linkage_specification_p;
17410 parser->num_template_parameter_lists
17411 = saved_num_template_parameter_lists;
17412 parser->in_function_body = saved_in_function_body;
17417 /* Parse a template-declaration, assuming that the `export' (and
17418 `extern') keywords, if present, has already been scanned. MEMBER_P
17419 is as for cp_parser_template_declaration. */
17422 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17424 tree decl = NULL_TREE;
17425 VEC (deferred_access_check,gc) *checks;
17426 tree parameter_list;
17427 bool friend_p = false;
17428 bool need_lang_pop;
17431 /* Look for the `template' keyword. */
17432 token = cp_lexer_peek_token (parser->lexer);
17433 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17437 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17439 if (at_class_scope_p () && current_function_decl)
17441 /* 14.5.2.2 [temp.mem]
17443 A local class shall not have member templates. */
17444 error ("%Hinvalid declaration of member template in local class",
17446 cp_parser_skip_to_end_of_block_or_statement (parser);
17451 A template ... shall not have C linkage. */
17452 if (current_lang_name == lang_name_c)
17454 error ("%Htemplate with C linkage", &token->location);
17455 /* Give it C++ linkage to avoid confusing other parts of the
17457 push_lang_context (lang_name_cplusplus);
17458 need_lang_pop = true;
17461 need_lang_pop = false;
17463 /* We cannot perform access checks on the template parameter
17464 declarations until we know what is being declared, just as we
17465 cannot check the decl-specifier list. */
17466 push_deferring_access_checks (dk_deferred);
17468 /* If the next token is `>', then we have an invalid
17469 specialization. Rather than complain about an invalid template
17470 parameter, issue an error message here. */
17471 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17473 cp_parser_error (parser, "invalid explicit specialization");
17474 begin_specialization ();
17475 parameter_list = NULL_TREE;
17478 /* Parse the template parameters. */
17479 parameter_list = cp_parser_template_parameter_list (parser);
17481 /* Get the deferred access checks from the parameter list. These
17482 will be checked once we know what is being declared, as for a
17483 member template the checks must be performed in the scope of the
17484 class containing the member. */
17485 checks = get_deferred_access_checks ();
17487 /* Look for the `>'. */
17488 cp_parser_skip_to_end_of_template_parameter_list (parser);
17489 /* We just processed one more parameter list. */
17490 ++parser->num_template_parameter_lists;
17491 /* If the next token is `template', there are more template
17493 if (cp_lexer_next_token_is_keyword (parser->lexer,
17495 cp_parser_template_declaration_after_export (parser, member_p);
17498 /* There are no access checks when parsing a template, as we do not
17499 know if a specialization will be a friend. */
17500 push_deferring_access_checks (dk_no_check);
17501 token = cp_lexer_peek_token (parser->lexer);
17502 decl = cp_parser_single_declaration (parser,
17505 /*explicit_specialization_p=*/false,
17507 pop_deferring_access_checks ();
17509 /* If this is a member template declaration, let the front
17511 if (member_p && !friend_p && decl)
17513 if (TREE_CODE (decl) == TYPE_DECL)
17514 cp_parser_check_access_in_redeclaration (decl, token->location);
17516 decl = finish_member_template_decl (decl);
17518 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17519 make_friend_class (current_class_type, TREE_TYPE (decl),
17520 /*complain=*/true);
17522 /* We are done with the current parameter list. */
17523 --parser->num_template_parameter_lists;
17525 pop_deferring_access_checks ();
17528 finish_template_decl (parameter_list);
17530 /* Register member declarations. */
17531 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17532 finish_member_declaration (decl);
17533 /* For the erroneous case of a template with C linkage, we pushed an
17534 implicit C++ linkage scope; exit that scope now. */
17536 pop_lang_context ();
17537 /* If DECL is a function template, we must return to parse it later.
17538 (Even though there is no definition, there might be default
17539 arguments that need handling.) */
17540 if (member_p && decl
17541 && (TREE_CODE (decl) == FUNCTION_DECL
17542 || DECL_FUNCTION_TEMPLATE_P (decl)))
17543 TREE_VALUE (parser->unparsed_functions_queues)
17544 = tree_cons (NULL_TREE, decl,
17545 TREE_VALUE (parser->unparsed_functions_queues));
17548 /* Perform the deferred access checks from a template-parameter-list.
17549 CHECKS is a TREE_LIST of access checks, as returned by
17550 get_deferred_access_checks. */
17553 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17555 ++processing_template_parmlist;
17556 perform_access_checks (checks);
17557 --processing_template_parmlist;
17560 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17561 `function-definition' sequence. MEMBER_P is true, this declaration
17562 appears in a class scope.
17564 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17565 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17568 cp_parser_single_declaration (cp_parser* parser,
17569 VEC (deferred_access_check,gc)* checks,
17571 bool explicit_specialization_p,
17574 int declares_class_or_enum;
17575 tree decl = NULL_TREE;
17576 cp_decl_specifier_seq decl_specifiers;
17577 bool function_definition_p = false;
17578 cp_token *decl_spec_token_start;
17580 /* This function is only used when processing a template
17582 gcc_assert (innermost_scope_kind () == sk_template_parms
17583 || innermost_scope_kind () == sk_template_spec);
17585 /* Defer access checks until we know what is being declared. */
17586 push_deferring_access_checks (dk_deferred);
17588 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17590 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17591 cp_parser_decl_specifier_seq (parser,
17592 CP_PARSER_FLAGS_OPTIONAL,
17594 &declares_class_or_enum);
17596 *friend_p = cp_parser_friend_p (&decl_specifiers);
17598 /* There are no template typedefs. */
17599 if (decl_specifiers.specs[(int) ds_typedef])
17601 error ("%Htemplate declaration of %qs",
17602 &decl_spec_token_start->location, "typedef");
17603 decl = error_mark_node;
17606 /* Gather up the access checks that occurred the
17607 decl-specifier-seq. */
17608 stop_deferring_access_checks ();
17610 /* Check for the declaration of a template class. */
17611 if (declares_class_or_enum)
17613 if (cp_parser_declares_only_class_p (parser))
17615 decl = shadow_tag (&decl_specifiers);
17620 friend template <typename T> struct A<T>::B;
17623 A<T>::B will be represented by a TYPENAME_TYPE, and
17624 therefore not recognized by shadow_tag. */
17625 if (friend_p && *friend_p
17627 && decl_specifiers.type
17628 && TYPE_P (decl_specifiers.type))
17629 decl = decl_specifiers.type;
17631 if (decl && decl != error_mark_node)
17632 decl = TYPE_NAME (decl);
17634 decl = error_mark_node;
17636 /* Perform access checks for template parameters. */
17637 cp_parser_perform_template_parameter_access_checks (checks);
17640 /* If it's not a template class, try for a template function. If
17641 the next token is a `;', then this declaration does not declare
17642 anything. But, if there were errors in the decl-specifiers, then
17643 the error might well have come from an attempted class-specifier.
17644 In that case, there's no need to warn about a missing declarator. */
17646 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17647 || decl_specifiers.type != error_mark_node))
17649 decl = cp_parser_init_declarator (parser,
17652 /*function_definition_allowed_p=*/true,
17654 declares_class_or_enum,
17655 &function_definition_p);
17657 /* 7.1.1-1 [dcl.stc]
17659 A storage-class-specifier shall not be specified in an explicit
17660 specialization... */
17662 && explicit_specialization_p
17663 && decl_specifiers.storage_class != sc_none)
17665 error ("%Hexplicit template specialization cannot have a storage class",
17666 &decl_spec_token_start->location);
17667 decl = error_mark_node;
17671 pop_deferring_access_checks ();
17673 /* Clear any current qualification; whatever comes next is the start
17674 of something new. */
17675 parser->scope = NULL_TREE;
17676 parser->qualifying_scope = NULL_TREE;
17677 parser->object_scope = NULL_TREE;
17678 /* Look for a trailing `;' after the declaration. */
17679 if (!function_definition_p
17680 && (decl == error_mark_node
17681 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17682 cp_parser_skip_to_end_of_block_or_statement (parser);
17687 /* Parse a cast-expression that is not the operand of a unary "&". */
17690 cp_parser_simple_cast_expression (cp_parser *parser)
17692 return cp_parser_cast_expression (parser, /*address_p=*/false,
17696 /* Parse a functional cast to TYPE. Returns an expression
17697 representing the cast. */
17700 cp_parser_functional_cast (cp_parser* parser, tree type)
17702 tree expression_list;
17706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17708 maybe_warn_cpp0x ("extended initializer lists");
17709 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17710 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17711 if (TREE_CODE (type) == TYPE_DECL)
17712 type = TREE_TYPE (type);
17713 return finish_compound_literal (type, expression_list);
17717 = cp_parser_parenthesized_expression_list (parser, false,
17719 /*allow_expansion_p=*/true,
17720 /*non_constant_p=*/NULL);
17722 cast = build_functional_cast (type, expression_list,
17723 tf_warning_or_error);
17724 /* [expr.const]/1: In an integral constant expression "only type
17725 conversions to integral or enumeration type can be used". */
17726 if (TREE_CODE (type) == TYPE_DECL)
17727 type = TREE_TYPE (type);
17728 if (cast != error_mark_node
17729 && !cast_valid_in_integral_constant_expression_p (type)
17730 && (cp_parser_non_integral_constant_expression
17731 (parser, "a call to a constructor")))
17732 return error_mark_node;
17736 /* Save the tokens that make up the body of a member function defined
17737 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17738 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17739 specifiers applied to the declaration. Returns the FUNCTION_DECL
17740 for the member function. */
17743 cp_parser_save_member_function_body (cp_parser* parser,
17744 cp_decl_specifier_seq *decl_specifiers,
17745 cp_declarator *declarator,
17752 /* Create the function-declaration. */
17753 fn = start_method (decl_specifiers, declarator, attributes);
17754 /* If something went badly wrong, bail out now. */
17755 if (fn == error_mark_node)
17757 /* If there's a function-body, skip it. */
17758 if (cp_parser_token_starts_function_definition_p
17759 (cp_lexer_peek_token (parser->lexer)))
17760 cp_parser_skip_to_end_of_block_or_statement (parser);
17761 return error_mark_node;
17764 /* Remember it, if there default args to post process. */
17765 cp_parser_save_default_args (parser, fn);
17767 /* Save away the tokens that make up the body of the
17769 first = parser->lexer->next_token;
17770 /* We can have braced-init-list mem-initializers before the fn body. */
17771 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17773 cp_lexer_consume_token (parser->lexer);
17774 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17775 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17777 /* cache_group will stop after an un-nested { } pair, too. */
17778 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17781 /* variadic mem-inits have ... after the ')'. */
17782 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17783 cp_lexer_consume_token (parser->lexer);
17786 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17787 /* Handle function try blocks. */
17788 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17789 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17790 last = parser->lexer->next_token;
17792 /* Save away the inline definition; we will process it when the
17793 class is complete. */
17794 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17795 DECL_PENDING_INLINE_P (fn) = 1;
17797 /* We need to know that this was defined in the class, so that
17798 friend templates are handled correctly. */
17799 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17801 /* We're done with the inline definition. */
17802 finish_method (fn);
17804 /* Add FN to the queue of functions to be parsed later. */
17805 TREE_VALUE (parser->unparsed_functions_queues)
17806 = tree_cons (NULL_TREE, fn,
17807 TREE_VALUE (parser->unparsed_functions_queues));
17812 /* Parse a template-argument-list, as well as the trailing ">" (but
17813 not the opening ">"). See cp_parser_template_argument_list for the
17817 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17821 tree saved_qualifying_scope;
17822 tree saved_object_scope;
17823 bool saved_greater_than_is_operator_p;
17824 bool saved_skip_evaluation;
17828 When parsing a template-id, the first non-nested `>' is taken as
17829 the end of the template-argument-list rather than a greater-than
17831 saved_greater_than_is_operator_p
17832 = parser->greater_than_is_operator_p;
17833 parser->greater_than_is_operator_p = false;
17834 /* Parsing the argument list may modify SCOPE, so we save it
17836 saved_scope = parser->scope;
17837 saved_qualifying_scope = parser->qualifying_scope;
17838 saved_object_scope = parser->object_scope;
17839 /* We need to evaluate the template arguments, even though this
17840 template-id may be nested within a "sizeof". */
17841 saved_skip_evaluation = skip_evaluation;
17842 skip_evaluation = false;
17843 /* Parse the template-argument-list itself. */
17844 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17845 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17846 arguments = NULL_TREE;
17848 arguments = cp_parser_template_argument_list (parser);
17849 /* Look for the `>' that ends the template-argument-list. If we find
17850 a '>>' instead, it's probably just a typo. */
17851 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17853 if (cxx_dialect != cxx98)
17855 /* In C++0x, a `>>' in a template argument list or cast
17856 expression is considered to be two separate `>'
17857 tokens. So, change the current token to a `>', but don't
17858 consume it: it will be consumed later when the outer
17859 template argument list (or cast expression) is parsed.
17860 Note that this replacement of `>' for `>>' is necessary
17861 even if we are parsing tentatively: in the tentative
17862 case, after calling
17863 cp_parser_enclosed_template_argument_list we will always
17864 throw away all of the template arguments and the first
17865 closing `>', either because the template argument list
17866 was erroneous or because we are replacing those tokens
17867 with a CPP_TEMPLATE_ID token. The second `>' (which will
17868 not have been thrown away) is needed either to close an
17869 outer template argument list or to complete a new-style
17871 cp_token *token = cp_lexer_peek_token (parser->lexer);
17872 token->type = CPP_GREATER;
17874 else if (!saved_greater_than_is_operator_p)
17876 /* If we're in a nested template argument list, the '>>' has
17877 to be a typo for '> >'. We emit the error message, but we
17878 continue parsing and we push a '>' as next token, so that
17879 the argument list will be parsed correctly. Note that the
17880 global source location is still on the token before the
17881 '>>', so we need to say explicitly where we want it. */
17882 cp_token *token = cp_lexer_peek_token (parser->lexer);
17883 error ("%H%<>>%> should be %<> >%> "
17884 "within a nested template argument list",
17887 token->type = CPP_GREATER;
17891 /* If this is not a nested template argument list, the '>>'
17892 is a typo for '>'. Emit an error message and continue.
17893 Same deal about the token location, but here we can get it
17894 right by consuming the '>>' before issuing the diagnostic. */
17895 cp_token *token = cp_lexer_consume_token (parser->lexer);
17896 error ("%Hspurious %<>>%>, use %<>%> to terminate "
17897 "a template argument list", &token->location);
17901 cp_parser_skip_to_end_of_template_parameter_list (parser);
17902 /* The `>' token might be a greater-than operator again now. */
17903 parser->greater_than_is_operator_p
17904 = saved_greater_than_is_operator_p;
17905 /* Restore the SAVED_SCOPE. */
17906 parser->scope = saved_scope;
17907 parser->qualifying_scope = saved_qualifying_scope;
17908 parser->object_scope = saved_object_scope;
17909 skip_evaluation = saved_skip_evaluation;
17914 /* MEMBER_FUNCTION is a member function, or a friend. If default
17915 arguments, or the body of the function have not yet been parsed,
17919 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17921 /* If this member is a template, get the underlying
17923 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17924 member_function = DECL_TEMPLATE_RESULT (member_function);
17926 /* There should not be any class definitions in progress at this
17927 point; the bodies of members are only parsed outside of all class
17929 gcc_assert (parser->num_classes_being_defined == 0);
17930 /* While we're parsing the member functions we might encounter more
17931 classes. We want to handle them right away, but we don't want
17932 them getting mixed up with functions that are currently in the
17934 parser->unparsed_functions_queues
17935 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17937 /* Make sure that any template parameters are in scope. */
17938 maybe_begin_member_template_processing (member_function);
17940 /* If the body of the function has not yet been parsed, parse it
17942 if (DECL_PENDING_INLINE_P (member_function))
17944 tree function_scope;
17945 cp_token_cache *tokens;
17947 /* The function is no longer pending; we are processing it. */
17948 tokens = DECL_PENDING_INLINE_INFO (member_function);
17949 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17950 DECL_PENDING_INLINE_P (member_function) = 0;
17952 /* If this is a local class, enter the scope of the containing
17954 function_scope = current_function_decl;
17955 if (function_scope)
17956 push_function_context ();
17958 /* Push the body of the function onto the lexer stack. */
17959 cp_parser_push_lexer_for_tokens (parser, tokens);
17961 /* Let the front end know that we going to be defining this
17963 start_preparsed_function (member_function, NULL_TREE,
17964 SF_PRE_PARSED | SF_INCLASS_INLINE);
17966 /* Don't do access checking if it is a templated function. */
17967 if (processing_template_decl)
17968 push_deferring_access_checks (dk_no_check);
17970 /* Now, parse the body of the function. */
17971 cp_parser_function_definition_after_declarator (parser,
17972 /*inline_p=*/true);
17974 if (processing_template_decl)
17975 pop_deferring_access_checks ();
17977 /* Leave the scope of the containing function. */
17978 if (function_scope)
17979 pop_function_context ();
17980 cp_parser_pop_lexer (parser);
17983 /* Remove any template parameters from the symbol table. */
17984 maybe_end_member_template_processing ();
17986 /* Restore the queue. */
17987 parser->unparsed_functions_queues
17988 = TREE_CHAIN (parser->unparsed_functions_queues);
17991 /* If DECL contains any default args, remember it on the unparsed
17992 functions queue. */
17995 cp_parser_save_default_args (cp_parser* parser, tree decl)
17999 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18001 probe = TREE_CHAIN (probe))
18002 if (TREE_PURPOSE (probe))
18004 TREE_PURPOSE (parser->unparsed_functions_queues)
18005 = tree_cons (current_class_type, decl,
18006 TREE_PURPOSE (parser->unparsed_functions_queues));
18011 /* FN is a FUNCTION_DECL which may contains a parameter with an
18012 unparsed DEFAULT_ARG. Parse the default args now. This function
18013 assumes that the current scope is the scope in which the default
18014 argument should be processed. */
18017 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18019 bool saved_local_variables_forbidden_p;
18022 /* While we're parsing the default args, we might (due to the
18023 statement expression extension) encounter more classes. We want
18024 to handle them right away, but we don't want them getting mixed
18025 up with default args that are currently in the queue. */
18026 parser->unparsed_functions_queues
18027 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18029 /* Local variable names (and the `this' keyword) may not appear
18030 in a default argument. */
18031 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18032 parser->local_variables_forbidden_p = true;
18034 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18036 parm = TREE_CHAIN (parm))
18038 cp_token_cache *tokens;
18039 tree default_arg = TREE_PURPOSE (parm);
18041 VEC(tree,gc) *insts;
18048 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18049 /* This can happen for a friend declaration for a function
18050 already declared with default arguments. */
18053 /* Push the saved tokens for the default argument onto the parser's
18055 tokens = DEFARG_TOKENS (default_arg);
18056 cp_parser_push_lexer_for_tokens (parser, tokens);
18058 /* Parse the assignment-expression. */
18059 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
18061 if (!processing_template_decl)
18062 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18064 TREE_PURPOSE (parm) = parsed_arg;
18066 /* Update any instantiations we've already created. */
18067 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18068 VEC_iterate (tree, insts, ix, copy); ix++)
18069 TREE_PURPOSE (copy) = parsed_arg;
18071 /* If the token stream has not been completely used up, then
18072 there was extra junk after the end of the default
18074 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18075 cp_parser_error (parser, "expected %<,%>");
18077 /* Revert to the main lexer. */
18078 cp_parser_pop_lexer (parser);
18081 /* Make sure no default arg is missing. */
18082 check_default_args (fn);
18084 /* Restore the state of local_variables_forbidden_p. */
18085 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18087 /* Restore the queue. */
18088 parser->unparsed_functions_queues
18089 = TREE_CHAIN (parser->unparsed_functions_queues);
18092 /* Parse the operand of `sizeof' (or a similar operator). Returns
18093 either a TYPE or an expression, depending on the form of the
18094 input. The KEYWORD indicates which kind of expression we have
18098 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18100 tree expr = NULL_TREE;
18101 const char *saved_message;
18103 bool saved_integral_constant_expression_p;
18104 bool saved_non_integral_constant_expression_p;
18105 bool pack_expansion_p = false;
18107 /* Types cannot be defined in a `sizeof' expression. Save away the
18109 saved_message = parser->type_definition_forbidden_message;
18110 /* And create the new one. */
18111 tmp = concat ("types may not be defined in %<",
18112 IDENTIFIER_POINTER (ridpointers[keyword]),
18113 "%> expressions", NULL);
18114 parser->type_definition_forbidden_message = tmp;
18116 /* The restrictions on constant-expressions do not apply inside
18117 sizeof expressions. */
18118 saved_integral_constant_expression_p
18119 = parser->integral_constant_expression_p;
18120 saved_non_integral_constant_expression_p
18121 = parser->non_integral_constant_expression_p;
18122 parser->integral_constant_expression_p = false;
18124 /* If it's a `...', then we are computing the length of a parameter
18126 if (keyword == RID_SIZEOF
18127 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18129 /* Consume the `...'. */
18130 cp_lexer_consume_token (parser->lexer);
18131 maybe_warn_variadic_templates ();
18133 /* Note that this is an expansion. */
18134 pack_expansion_p = true;
18137 /* Do not actually evaluate the expression. */
18139 /* If it's a `(', then we might be looking at the type-id
18141 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18144 bool saved_in_type_id_in_expr_p;
18146 /* We can't be sure yet whether we're looking at a type-id or an
18148 cp_parser_parse_tentatively (parser);
18149 /* Consume the `('. */
18150 cp_lexer_consume_token (parser->lexer);
18151 /* Parse the type-id. */
18152 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18153 parser->in_type_id_in_expr_p = true;
18154 type = cp_parser_type_id (parser);
18155 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18156 /* Now, look for the trailing `)'. */
18157 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18158 /* If all went well, then we're done. */
18159 if (cp_parser_parse_definitely (parser))
18161 cp_decl_specifier_seq decl_specs;
18163 /* Build a trivial decl-specifier-seq. */
18164 clear_decl_specs (&decl_specs);
18165 decl_specs.type = type;
18167 /* Call grokdeclarator to figure out what type this is. */
18168 expr = grokdeclarator (NULL,
18172 /*attrlist=*/NULL);
18176 /* If the type-id production did not work out, then we must be
18177 looking at the unary-expression production. */
18179 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18182 if (pack_expansion_p)
18183 /* Build a pack expansion. */
18184 expr = make_pack_expansion (expr);
18186 /* Go back to evaluating expressions. */
18189 /* Free the message we created. */
18191 /* And restore the old one. */
18192 parser->type_definition_forbidden_message = saved_message;
18193 parser->integral_constant_expression_p
18194 = saved_integral_constant_expression_p;
18195 parser->non_integral_constant_expression_p
18196 = saved_non_integral_constant_expression_p;
18201 /* If the current declaration has no declarator, return true. */
18204 cp_parser_declares_only_class_p (cp_parser *parser)
18206 /* If the next token is a `;' or a `,' then there is no
18208 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18209 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18212 /* Update the DECL_SPECS to reflect the storage class indicated by
18216 cp_parser_set_storage_class (cp_parser *parser,
18217 cp_decl_specifier_seq *decl_specs,
18219 location_t location)
18221 cp_storage_class storage_class;
18223 if (parser->in_unbraced_linkage_specification_p)
18225 error ("%Hinvalid use of %qD in linkage specification",
18226 &location, ridpointers[keyword]);
18229 else if (decl_specs->storage_class != sc_none)
18231 decl_specs->conflicting_specifiers_p = true;
18235 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18236 && decl_specs->specs[(int) ds_thread])
18238 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18239 decl_specs->specs[(int) ds_thread] = 0;
18245 storage_class = sc_auto;
18248 storage_class = sc_register;
18251 storage_class = sc_static;
18254 storage_class = sc_extern;
18257 storage_class = sc_mutable;
18260 gcc_unreachable ();
18262 decl_specs->storage_class = storage_class;
18264 /* A storage class specifier cannot be applied alongside a typedef
18265 specifier. If there is a typedef specifier present then set
18266 conflicting_specifiers_p which will trigger an error later
18267 on in grokdeclarator. */
18268 if (decl_specs->specs[(int)ds_typedef])
18269 decl_specs->conflicting_specifiers_p = true;
18272 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18273 is true, the type is a user-defined type; otherwise it is a
18274 built-in type specified by a keyword. */
18277 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18279 location_t location,
18280 bool user_defined_p)
18282 decl_specs->any_specifiers_p = true;
18284 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18285 (with, for example, in "typedef int wchar_t;") we remember that
18286 this is what happened. In system headers, we ignore these
18287 declarations so that G++ can work with system headers that are not
18289 if (decl_specs->specs[(int) ds_typedef]
18291 && (type_spec == boolean_type_node
18292 || type_spec == char16_type_node
18293 || type_spec == char32_type_node
18294 || type_spec == wchar_type_node)
18295 && (decl_specs->type
18296 || decl_specs->specs[(int) ds_long]
18297 || decl_specs->specs[(int) ds_short]
18298 || decl_specs->specs[(int) ds_unsigned]
18299 || decl_specs->specs[(int) ds_signed]))
18301 decl_specs->redefined_builtin_type = type_spec;
18302 if (!decl_specs->type)
18304 decl_specs->type = type_spec;
18305 decl_specs->user_defined_type_p = false;
18306 decl_specs->type_location = location;
18309 else if (decl_specs->type)
18310 decl_specs->multiple_types_p = true;
18313 decl_specs->type = type_spec;
18314 decl_specs->user_defined_type_p = user_defined_p;
18315 decl_specs->redefined_builtin_type = NULL_TREE;
18316 decl_specs->type_location = location;
18320 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18321 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18324 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18326 return decl_specifiers->specs[(int) ds_friend] != 0;
18329 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18330 issue an error message indicating that TOKEN_DESC was expected.
18332 Returns the token consumed, if the token had the appropriate type.
18333 Otherwise, returns NULL. */
18336 cp_parser_require (cp_parser* parser,
18337 enum cpp_ttype type,
18338 const char* token_desc)
18340 if (cp_lexer_next_token_is (parser->lexer, type))
18341 return cp_lexer_consume_token (parser->lexer);
18344 /* Output the MESSAGE -- unless we're parsing tentatively. */
18345 if (!cp_parser_simulate_error (parser))
18347 char *message = concat ("expected ", token_desc, NULL);
18348 cp_parser_error (parser, message);
18355 /* An error message is produced if the next token is not '>'.
18356 All further tokens are skipped until the desired token is
18357 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18360 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18362 /* Current level of '< ... >'. */
18363 unsigned level = 0;
18364 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18365 unsigned nesting_depth = 0;
18367 /* Are we ready, yet? If not, issue error message. */
18368 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18371 /* Skip tokens until the desired token is found. */
18374 /* Peek at the next token. */
18375 switch (cp_lexer_peek_token (parser->lexer)->type)
18378 if (!nesting_depth)
18383 if (cxx_dialect == cxx98)
18384 /* C++0x views the `>>' operator as two `>' tokens, but
18387 else if (!nesting_depth && level-- == 0)
18389 /* We've hit a `>>' where the first `>' closes the
18390 template argument list, and the second `>' is
18391 spurious. Just consume the `>>' and stop; we've
18392 already produced at least one error. */
18393 cp_lexer_consume_token (parser->lexer);
18396 /* Fall through for C++0x, so we handle the second `>' in
18400 if (!nesting_depth && level-- == 0)
18402 /* We've reached the token we want, consume it and stop. */
18403 cp_lexer_consume_token (parser->lexer);
18408 case CPP_OPEN_PAREN:
18409 case CPP_OPEN_SQUARE:
18413 case CPP_CLOSE_PAREN:
18414 case CPP_CLOSE_SQUARE:
18415 if (nesting_depth-- == 0)
18420 case CPP_PRAGMA_EOL:
18421 case CPP_SEMICOLON:
18422 case CPP_OPEN_BRACE:
18423 case CPP_CLOSE_BRACE:
18424 /* The '>' was probably forgotten, don't look further. */
18431 /* Consume this token. */
18432 cp_lexer_consume_token (parser->lexer);
18436 /* If the next token is the indicated keyword, consume it. Otherwise,
18437 issue an error message indicating that TOKEN_DESC was expected.
18439 Returns the token consumed, if the token had the appropriate type.
18440 Otherwise, returns NULL. */
18443 cp_parser_require_keyword (cp_parser* parser,
18445 const char* token_desc)
18447 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18449 if (token && token->keyword != keyword)
18451 dyn_string_t error_msg;
18453 /* Format the error message. */
18454 error_msg = dyn_string_new (0);
18455 dyn_string_append_cstr (error_msg, "expected ");
18456 dyn_string_append_cstr (error_msg, token_desc);
18457 cp_parser_error (parser, error_msg->s);
18458 dyn_string_delete (error_msg);
18465 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18466 function-definition. */
18469 cp_parser_token_starts_function_definition_p (cp_token* token)
18471 return (/* An ordinary function-body begins with an `{'. */
18472 token->type == CPP_OPEN_BRACE
18473 /* A ctor-initializer begins with a `:'. */
18474 || token->type == CPP_COLON
18475 /* A function-try-block begins with `try'. */
18476 || token->keyword == RID_TRY
18477 /* The named return value extension begins with `return'. */
18478 || token->keyword == RID_RETURN);
18481 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18485 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18489 token = cp_lexer_peek_token (parser->lexer);
18490 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18493 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18494 C++0x) ending a template-argument. */
18497 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18501 token = cp_lexer_peek_token (parser->lexer);
18502 return (token->type == CPP_COMMA
18503 || token->type == CPP_GREATER
18504 || token->type == CPP_ELLIPSIS
18505 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18508 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18509 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18512 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18517 token = cp_lexer_peek_nth_token (parser->lexer, n);
18518 if (token->type == CPP_LESS)
18520 /* Check for the sequence `<::' in the original code. It would be lexed as
18521 `[:', where `[' is a digraph, and there is no whitespace before
18523 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18526 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18527 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18533 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18534 or none_type otherwise. */
18536 static enum tag_types
18537 cp_parser_token_is_class_key (cp_token* token)
18539 switch (token->keyword)
18544 return record_type;
18553 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18556 cp_parser_check_class_key (enum tag_types class_key, tree type)
18558 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18559 permerror (input_location, "%qs tag used in naming %q#T",
18560 class_key == union_type ? "union"
18561 : class_key == record_type ? "struct" : "class",
18565 /* Issue an error message if DECL is redeclared with different
18566 access than its original declaration [class.access.spec/3].
18567 This applies to nested classes and nested class templates.
18571 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18573 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18576 if ((TREE_PRIVATE (decl)
18577 != (current_access_specifier == access_private_node))
18578 || (TREE_PROTECTED (decl)
18579 != (current_access_specifier == access_protected_node)))
18580 error ("%H%qD redeclared with different access", &location, decl);
18583 /* Look for the `template' keyword, as a syntactic disambiguator.
18584 Return TRUE iff it is present, in which case it will be
18588 cp_parser_optional_template_keyword (cp_parser *parser)
18590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18592 /* The `template' keyword can only be used within templates;
18593 outside templates the parser can always figure out what is a
18594 template and what is not. */
18595 if (!processing_template_decl)
18597 cp_token *token = cp_lexer_peek_token (parser->lexer);
18598 error ("%H%<template%> (as a disambiguator) is only allowed "
18599 "within templates", &token->location);
18600 /* If this part of the token stream is rescanned, the same
18601 error message would be generated. So, we purge the token
18602 from the stream. */
18603 cp_lexer_purge_token (parser->lexer);
18608 /* Consume the `template' keyword. */
18609 cp_lexer_consume_token (parser->lexer);
18617 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18618 set PARSER->SCOPE, and perform other related actions. */
18621 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18624 struct tree_check *check_value;
18625 deferred_access_check *chk;
18626 VEC (deferred_access_check,gc) *checks;
18628 /* Get the stored value. */
18629 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18630 /* Perform any access checks that were deferred. */
18631 checks = check_value->checks;
18635 VEC_iterate (deferred_access_check, checks, i, chk) ;
18638 perform_or_defer_access_check (chk->binfo,
18643 /* Set the scope from the stored value. */
18644 parser->scope = check_value->value;
18645 parser->qualifying_scope = check_value->qualifying_scope;
18646 parser->object_scope = NULL_TREE;
18649 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18650 encounter the end of a block before what we were looking for. */
18653 cp_parser_cache_group (cp_parser *parser,
18654 enum cpp_ttype end,
18659 cp_token *token = cp_lexer_peek_token (parser->lexer);
18661 /* Abort a parenthesized expression if we encounter a semicolon. */
18662 if ((end == CPP_CLOSE_PAREN || depth == 0)
18663 && token->type == CPP_SEMICOLON)
18665 /* If we've reached the end of the file, stop. */
18666 if (token->type == CPP_EOF
18667 || (end != CPP_PRAGMA_EOL
18668 && token->type == CPP_PRAGMA_EOL))
18670 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18671 /* We've hit the end of an enclosing block, so there's been some
18672 kind of syntax error. */
18675 /* Consume the token. */
18676 cp_lexer_consume_token (parser->lexer);
18677 /* See if it starts a new group. */
18678 if (token->type == CPP_OPEN_BRACE)
18680 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18681 /* In theory this should probably check end == '}', but
18682 cp_parser_save_member_function_body needs it to exit
18683 after either '}' or ')' when called with ')'. */
18687 else if (token->type == CPP_OPEN_PAREN)
18689 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18690 if (depth == 0 && end == CPP_CLOSE_PAREN)
18693 else if (token->type == CPP_PRAGMA)
18694 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18695 else if (token->type == end)
18700 /* Begin parsing tentatively. We always save tokens while parsing
18701 tentatively so that if the tentative parsing fails we can restore the
18705 cp_parser_parse_tentatively (cp_parser* parser)
18707 /* Enter a new parsing context. */
18708 parser->context = cp_parser_context_new (parser->context);
18709 /* Begin saving tokens. */
18710 cp_lexer_save_tokens (parser->lexer);
18711 /* In order to avoid repetitive access control error messages,
18712 access checks are queued up until we are no longer parsing
18714 push_deferring_access_checks (dk_deferred);
18717 /* Commit to the currently active tentative parse. */
18720 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18722 cp_parser_context *context;
18725 /* Mark all of the levels as committed. */
18726 lexer = parser->lexer;
18727 for (context = parser->context; context->next; context = context->next)
18729 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18731 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18732 while (!cp_lexer_saving_tokens (lexer))
18733 lexer = lexer->next;
18734 cp_lexer_commit_tokens (lexer);
18738 /* Abort the currently active tentative parse. All consumed tokens
18739 will be rolled back, and no diagnostics will be issued. */
18742 cp_parser_abort_tentative_parse (cp_parser* parser)
18744 cp_parser_simulate_error (parser);
18745 /* Now, pretend that we want to see if the construct was
18746 successfully parsed. */
18747 cp_parser_parse_definitely (parser);
18750 /* Stop parsing tentatively. If a parse error has occurred, restore the
18751 token stream. Otherwise, commit to the tokens we have consumed.
18752 Returns true if no error occurred; false otherwise. */
18755 cp_parser_parse_definitely (cp_parser* parser)
18757 bool error_occurred;
18758 cp_parser_context *context;
18760 /* Remember whether or not an error occurred, since we are about to
18761 destroy that information. */
18762 error_occurred = cp_parser_error_occurred (parser);
18763 /* Remove the topmost context from the stack. */
18764 context = parser->context;
18765 parser->context = context->next;
18766 /* If no parse errors occurred, commit to the tentative parse. */
18767 if (!error_occurred)
18769 /* Commit to the tokens read tentatively, unless that was
18771 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18772 cp_lexer_commit_tokens (parser->lexer);
18774 pop_to_parent_deferring_access_checks ();
18776 /* Otherwise, if errors occurred, roll back our state so that things
18777 are just as they were before we began the tentative parse. */
18780 cp_lexer_rollback_tokens (parser->lexer);
18781 pop_deferring_access_checks ();
18783 /* Add the context to the front of the free list. */
18784 context->next = cp_parser_context_free_list;
18785 cp_parser_context_free_list = context;
18787 return !error_occurred;
18790 /* Returns true if we are parsing tentatively and are not committed to
18791 this tentative parse. */
18794 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18796 return (cp_parser_parsing_tentatively (parser)
18797 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18800 /* Returns nonzero iff an error has occurred during the most recent
18801 tentative parse. */
18804 cp_parser_error_occurred (cp_parser* parser)
18806 return (cp_parser_parsing_tentatively (parser)
18807 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18810 /* Returns nonzero if GNU extensions are allowed. */
18813 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18815 return parser->allow_gnu_extensions_p;
18818 /* Objective-C++ Productions */
18821 /* Parse an Objective-C expression, which feeds into a primary-expression
18825 objc-message-expression
18826 objc-string-literal
18827 objc-encode-expression
18828 objc-protocol-expression
18829 objc-selector-expression
18831 Returns a tree representation of the expression. */
18834 cp_parser_objc_expression (cp_parser* parser)
18836 /* Try to figure out what kind of declaration is present. */
18837 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18841 case CPP_OPEN_SQUARE:
18842 return cp_parser_objc_message_expression (parser);
18844 case CPP_OBJC_STRING:
18845 kwd = cp_lexer_consume_token (parser->lexer);
18846 return objc_build_string_object (kwd->u.value);
18849 switch (kwd->keyword)
18851 case RID_AT_ENCODE:
18852 return cp_parser_objc_encode_expression (parser);
18854 case RID_AT_PROTOCOL:
18855 return cp_parser_objc_protocol_expression (parser);
18857 case RID_AT_SELECTOR:
18858 return cp_parser_objc_selector_expression (parser);
18864 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18865 &kwd->location, kwd->u.value);
18866 cp_parser_skip_to_end_of_block_or_statement (parser);
18869 return error_mark_node;
18872 /* Parse an Objective-C message expression.
18874 objc-message-expression:
18875 [ objc-message-receiver objc-message-args ]
18877 Returns a representation of an Objective-C message. */
18880 cp_parser_objc_message_expression (cp_parser* parser)
18882 tree receiver, messageargs;
18884 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18885 receiver = cp_parser_objc_message_receiver (parser);
18886 messageargs = cp_parser_objc_message_args (parser);
18887 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18889 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18892 /* Parse an objc-message-receiver.
18894 objc-message-receiver:
18896 simple-type-specifier
18898 Returns a representation of the type or expression. */
18901 cp_parser_objc_message_receiver (cp_parser* parser)
18905 /* An Objective-C message receiver may be either (1) a type
18906 or (2) an expression. */
18907 cp_parser_parse_tentatively (parser);
18908 rcv = cp_parser_expression (parser, false);
18910 if (cp_parser_parse_definitely (parser))
18913 rcv = cp_parser_simple_type_specifier (parser,
18914 /*decl_specs=*/NULL,
18915 CP_PARSER_FLAGS_NONE);
18917 return objc_get_class_reference (rcv);
18920 /* Parse the arguments and selectors comprising an Objective-C message.
18925 objc-selector-args , objc-comma-args
18927 objc-selector-args:
18928 objc-selector [opt] : assignment-expression
18929 objc-selector-args objc-selector [opt] : assignment-expression
18932 assignment-expression
18933 objc-comma-args , assignment-expression
18935 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18936 selector arguments and TREE_VALUE containing a list of comma
18940 cp_parser_objc_message_args (cp_parser* parser)
18942 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18943 bool maybe_unary_selector_p = true;
18944 cp_token *token = cp_lexer_peek_token (parser->lexer);
18946 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18948 tree selector = NULL_TREE, arg;
18950 if (token->type != CPP_COLON)
18951 selector = cp_parser_objc_selector (parser);
18953 /* Detect if we have a unary selector. */
18954 if (maybe_unary_selector_p
18955 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18956 return build_tree_list (selector, NULL_TREE);
18958 maybe_unary_selector_p = false;
18959 cp_parser_require (parser, CPP_COLON, "%<:%>");
18960 arg = cp_parser_assignment_expression (parser, false);
18963 = chainon (sel_args,
18964 build_tree_list (selector, arg));
18966 token = cp_lexer_peek_token (parser->lexer);
18969 /* Handle non-selector arguments, if any. */
18970 while (token->type == CPP_COMMA)
18974 cp_lexer_consume_token (parser->lexer);
18975 arg = cp_parser_assignment_expression (parser, false);
18978 = chainon (addl_args,
18979 build_tree_list (NULL_TREE, arg));
18981 token = cp_lexer_peek_token (parser->lexer);
18984 return build_tree_list (sel_args, addl_args);
18987 /* Parse an Objective-C encode expression.
18989 objc-encode-expression:
18990 @encode objc-typename
18992 Returns an encoded representation of the type argument. */
18995 cp_parser_objc_encode_expression (cp_parser* parser)
19000 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19001 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19002 token = cp_lexer_peek_token (parser->lexer);
19003 type = complete_type (cp_parser_type_id (parser));
19004 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19008 error ("%H%<@encode%> must specify a type as an argument",
19010 return error_mark_node;
19013 return objc_build_encode_expr (type);
19016 /* Parse an Objective-C @defs expression. */
19019 cp_parser_objc_defs_expression (cp_parser *parser)
19023 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19024 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19025 name = cp_parser_identifier (parser);
19026 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19028 return objc_get_class_ivars (name);
19031 /* Parse an Objective-C protocol expression.
19033 objc-protocol-expression:
19034 @protocol ( identifier )
19036 Returns a representation of the protocol expression. */
19039 cp_parser_objc_protocol_expression (cp_parser* parser)
19043 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19044 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19045 proto = cp_parser_identifier (parser);
19046 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19048 return objc_build_protocol_expr (proto);
19051 /* Parse an Objective-C selector expression.
19053 objc-selector-expression:
19054 @selector ( objc-method-signature )
19056 objc-method-signature:
19062 objc-selector-seq objc-selector :
19064 Returns a representation of the method selector. */
19067 cp_parser_objc_selector_expression (cp_parser* parser)
19069 tree sel_seq = NULL_TREE;
19070 bool maybe_unary_selector_p = true;
19073 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19074 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19075 token = cp_lexer_peek_token (parser->lexer);
19077 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19078 || token->type == CPP_SCOPE)
19080 tree selector = NULL_TREE;
19082 if (token->type != CPP_COLON
19083 || token->type == CPP_SCOPE)
19084 selector = cp_parser_objc_selector (parser);
19086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19087 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19089 /* Detect if we have a unary selector. */
19090 if (maybe_unary_selector_p)
19092 sel_seq = selector;
19093 goto finish_selector;
19097 cp_parser_error (parser, "expected %<:%>");
19100 maybe_unary_selector_p = false;
19101 token = cp_lexer_consume_token (parser->lexer);
19103 if (token->type == CPP_SCOPE)
19106 = chainon (sel_seq,
19107 build_tree_list (selector, NULL_TREE));
19109 = chainon (sel_seq,
19110 build_tree_list (NULL_TREE, NULL_TREE));
19114 = chainon (sel_seq,
19115 build_tree_list (selector, NULL_TREE));
19117 token = cp_lexer_peek_token (parser->lexer);
19121 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19123 return objc_build_selector_expr (sel_seq);
19126 /* Parse a list of identifiers.
19128 objc-identifier-list:
19130 objc-identifier-list , identifier
19132 Returns a TREE_LIST of identifier nodes. */
19135 cp_parser_objc_identifier_list (cp_parser* parser)
19137 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19138 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19140 while (sep->type == CPP_COMMA)
19142 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19143 list = chainon (list,
19144 build_tree_list (NULL_TREE,
19145 cp_parser_identifier (parser)));
19146 sep = cp_lexer_peek_token (parser->lexer);
19152 /* Parse an Objective-C alias declaration.
19154 objc-alias-declaration:
19155 @compatibility_alias identifier identifier ;
19157 This function registers the alias mapping with the Objective-C front end.
19158 It returns nothing. */
19161 cp_parser_objc_alias_declaration (cp_parser* parser)
19165 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19166 alias = cp_parser_identifier (parser);
19167 orig = cp_parser_identifier (parser);
19168 objc_declare_alias (alias, orig);
19169 cp_parser_consume_semicolon_at_end_of_statement (parser);
19172 /* Parse an Objective-C class forward-declaration.
19174 objc-class-declaration:
19175 @class objc-identifier-list ;
19177 The function registers the forward declarations with the Objective-C
19178 front end. It returns nothing. */
19181 cp_parser_objc_class_declaration (cp_parser* parser)
19183 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19184 objc_declare_class (cp_parser_objc_identifier_list (parser));
19185 cp_parser_consume_semicolon_at_end_of_statement (parser);
19188 /* Parse a list of Objective-C protocol references.
19190 objc-protocol-refs-opt:
19191 objc-protocol-refs [opt]
19193 objc-protocol-refs:
19194 < objc-identifier-list >
19196 Returns a TREE_LIST of identifiers, if any. */
19199 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19201 tree protorefs = NULL_TREE;
19203 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19205 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19206 protorefs = cp_parser_objc_identifier_list (parser);
19207 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19213 /* Parse a Objective-C visibility specification. */
19216 cp_parser_objc_visibility_spec (cp_parser* parser)
19218 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19220 switch (vis->keyword)
19222 case RID_AT_PRIVATE:
19223 objc_set_visibility (2);
19225 case RID_AT_PROTECTED:
19226 objc_set_visibility (0);
19228 case RID_AT_PUBLIC:
19229 objc_set_visibility (1);
19235 /* Eat '@private'/'@protected'/'@public'. */
19236 cp_lexer_consume_token (parser->lexer);
19239 /* Parse an Objective-C method type. */
19242 cp_parser_objc_method_type (cp_parser* parser)
19244 objc_set_method_type
19245 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19250 /* Parse an Objective-C protocol qualifier. */
19253 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19255 tree quals = NULL_TREE, node;
19256 cp_token *token = cp_lexer_peek_token (parser->lexer);
19258 node = token->u.value;
19260 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19261 && (node == ridpointers [(int) RID_IN]
19262 || node == ridpointers [(int) RID_OUT]
19263 || node == ridpointers [(int) RID_INOUT]
19264 || node == ridpointers [(int) RID_BYCOPY]
19265 || node == ridpointers [(int) RID_BYREF]
19266 || node == ridpointers [(int) RID_ONEWAY]))
19268 quals = tree_cons (NULL_TREE, node, quals);
19269 cp_lexer_consume_token (parser->lexer);
19270 token = cp_lexer_peek_token (parser->lexer);
19271 node = token->u.value;
19277 /* Parse an Objective-C typename. */
19280 cp_parser_objc_typename (cp_parser* parser)
19282 tree type_name = NULL_TREE;
19284 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19286 tree proto_quals, cp_type = NULL_TREE;
19288 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19289 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19291 /* An ObjC type name may consist of just protocol qualifiers, in which
19292 case the type shall default to 'id'. */
19293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19294 cp_type = cp_parser_type_id (parser);
19296 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19297 type_name = build_tree_list (proto_quals, cp_type);
19303 /* Check to see if TYPE refers to an Objective-C selector name. */
19306 cp_parser_objc_selector_p (enum cpp_ttype type)
19308 return (type == CPP_NAME || type == CPP_KEYWORD
19309 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19310 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19311 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19312 || type == CPP_XOR || type == CPP_XOR_EQ);
19315 /* Parse an Objective-C selector. */
19318 cp_parser_objc_selector (cp_parser* parser)
19320 cp_token *token = cp_lexer_consume_token (parser->lexer);
19322 if (!cp_parser_objc_selector_p (token->type))
19324 error ("%Hinvalid Objective-C++ selector name", &token->location);
19325 return error_mark_node;
19328 /* C++ operator names are allowed to appear in ObjC selectors. */
19329 switch (token->type)
19331 case CPP_AND_AND: return get_identifier ("and");
19332 case CPP_AND_EQ: return get_identifier ("and_eq");
19333 case CPP_AND: return get_identifier ("bitand");
19334 case CPP_OR: return get_identifier ("bitor");
19335 case CPP_COMPL: return get_identifier ("compl");
19336 case CPP_NOT: return get_identifier ("not");
19337 case CPP_NOT_EQ: return get_identifier ("not_eq");
19338 case CPP_OR_OR: return get_identifier ("or");
19339 case CPP_OR_EQ: return get_identifier ("or_eq");
19340 case CPP_XOR: return get_identifier ("xor");
19341 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19342 default: return token->u.value;
19346 /* Parse an Objective-C params list. */
19349 cp_parser_objc_method_keyword_params (cp_parser* parser)
19351 tree params = NULL_TREE;
19352 bool maybe_unary_selector_p = true;
19353 cp_token *token = cp_lexer_peek_token (parser->lexer);
19355 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19357 tree selector = NULL_TREE, type_name, identifier;
19359 if (token->type != CPP_COLON)
19360 selector = cp_parser_objc_selector (parser);
19362 /* Detect if we have a unary selector. */
19363 if (maybe_unary_selector_p
19364 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19367 maybe_unary_selector_p = false;
19368 cp_parser_require (parser, CPP_COLON, "%<:%>");
19369 type_name = cp_parser_objc_typename (parser);
19370 identifier = cp_parser_identifier (parser);
19374 objc_build_keyword_decl (selector,
19378 token = cp_lexer_peek_token (parser->lexer);
19384 /* Parse the non-keyword Objective-C params. */
19387 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19389 tree params = make_node (TREE_LIST);
19390 cp_token *token = cp_lexer_peek_token (parser->lexer);
19391 *ellipsisp = false; /* Initially, assume no ellipsis. */
19393 while (token->type == CPP_COMMA)
19395 cp_parameter_declarator *parmdecl;
19398 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19399 token = cp_lexer_peek_token (parser->lexer);
19401 if (token->type == CPP_ELLIPSIS)
19403 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19408 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19409 parm = grokdeclarator (parmdecl->declarator,
19410 &parmdecl->decl_specifiers,
19411 PARM, /*initialized=*/0,
19412 /*attrlist=*/NULL);
19414 chainon (params, build_tree_list (NULL_TREE, parm));
19415 token = cp_lexer_peek_token (parser->lexer);
19421 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19424 cp_parser_objc_interstitial_code (cp_parser* parser)
19426 cp_token *token = cp_lexer_peek_token (parser->lexer);
19428 /* If the next token is `extern' and the following token is a string
19429 literal, then we have a linkage specification. */
19430 if (token->keyword == RID_EXTERN
19431 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19432 cp_parser_linkage_specification (parser);
19433 /* Handle #pragma, if any. */
19434 else if (token->type == CPP_PRAGMA)
19435 cp_parser_pragma (parser, pragma_external);
19436 /* Allow stray semicolons. */
19437 else if (token->type == CPP_SEMICOLON)
19438 cp_lexer_consume_token (parser->lexer);
19439 /* Finally, try to parse a block-declaration, or a function-definition. */
19441 cp_parser_block_declaration (parser, /*statement_p=*/false);
19444 /* Parse a method signature. */
19447 cp_parser_objc_method_signature (cp_parser* parser)
19449 tree rettype, kwdparms, optparms;
19450 bool ellipsis = false;
19452 cp_parser_objc_method_type (parser);
19453 rettype = cp_parser_objc_typename (parser);
19454 kwdparms = cp_parser_objc_method_keyword_params (parser);
19455 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19457 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19460 /* Pars an Objective-C method prototype list. */
19463 cp_parser_objc_method_prototype_list (cp_parser* parser)
19465 cp_token *token = cp_lexer_peek_token (parser->lexer);
19467 while (token->keyword != RID_AT_END)
19469 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19471 objc_add_method_declaration
19472 (cp_parser_objc_method_signature (parser));
19473 cp_parser_consume_semicolon_at_end_of_statement (parser);
19476 /* Allow for interspersed non-ObjC++ code. */
19477 cp_parser_objc_interstitial_code (parser);
19479 token = cp_lexer_peek_token (parser->lexer);
19482 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19483 objc_finish_interface ();
19486 /* Parse an Objective-C method definition list. */
19489 cp_parser_objc_method_definition_list (cp_parser* parser)
19491 cp_token *token = cp_lexer_peek_token (parser->lexer);
19493 while (token->keyword != RID_AT_END)
19497 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19499 push_deferring_access_checks (dk_deferred);
19500 objc_start_method_definition
19501 (cp_parser_objc_method_signature (parser));
19503 /* For historical reasons, we accept an optional semicolon. */
19504 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19505 cp_lexer_consume_token (parser->lexer);
19507 perform_deferred_access_checks ();
19508 stop_deferring_access_checks ();
19509 meth = cp_parser_function_definition_after_declarator (parser,
19511 pop_deferring_access_checks ();
19512 objc_finish_method_definition (meth);
19515 /* Allow for interspersed non-ObjC++ code. */
19516 cp_parser_objc_interstitial_code (parser);
19518 token = cp_lexer_peek_token (parser->lexer);
19521 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19522 objc_finish_implementation ();
19525 /* Parse Objective-C ivars. */
19528 cp_parser_objc_class_ivars (cp_parser* parser)
19530 cp_token *token = cp_lexer_peek_token (parser->lexer);
19532 if (token->type != CPP_OPEN_BRACE)
19533 return; /* No ivars specified. */
19535 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19536 token = cp_lexer_peek_token (parser->lexer);
19538 while (token->type != CPP_CLOSE_BRACE)
19540 cp_decl_specifier_seq declspecs;
19541 int decl_class_or_enum_p;
19542 tree prefix_attributes;
19544 cp_parser_objc_visibility_spec (parser);
19546 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19549 cp_parser_decl_specifier_seq (parser,
19550 CP_PARSER_FLAGS_OPTIONAL,
19552 &decl_class_or_enum_p);
19553 prefix_attributes = declspecs.attributes;
19554 declspecs.attributes = NULL_TREE;
19556 /* Keep going until we hit the `;' at the end of the
19558 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19560 tree width = NULL_TREE, attributes, first_attribute, decl;
19561 cp_declarator *declarator = NULL;
19562 int ctor_dtor_or_conv_p;
19564 /* Check for a (possibly unnamed) bitfield declaration. */
19565 token = cp_lexer_peek_token (parser->lexer);
19566 if (token->type == CPP_COLON)
19569 if (token->type == CPP_NAME
19570 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19573 /* Get the name of the bitfield. */
19574 declarator = make_id_declarator (NULL_TREE,
19575 cp_parser_identifier (parser),
19579 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19580 /* Get the width of the bitfield. */
19582 = cp_parser_constant_expression (parser,
19583 /*allow_non_constant=*/false,
19588 /* Parse the declarator. */
19590 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19591 &ctor_dtor_or_conv_p,
19592 /*parenthesized_p=*/NULL,
19593 /*member_p=*/false);
19596 /* Look for attributes that apply to the ivar. */
19597 attributes = cp_parser_attributes_opt (parser);
19598 /* Remember which attributes are prefix attributes and
19600 first_attribute = attributes;
19601 /* Combine the attributes. */
19602 attributes = chainon (prefix_attributes, attributes);
19605 /* Create the bitfield declaration. */
19606 decl = grokbitfield (declarator, &declspecs,
19610 decl = grokfield (declarator, &declspecs,
19611 NULL_TREE, /*init_const_expr_p=*/false,
19612 NULL_TREE, attributes);
19614 /* Add the instance variable. */
19615 objc_add_instance_variable (decl);
19617 /* Reset PREFIX_ATTRIBUTES. */
19618 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19619 attributes = TREE_CHAIN (attributes);
19621 TREE_CHAIN (attributes) = NULL_TREE;
19623 token = cp_lexer_peek_token (parser->lexer);
19625 if (token->type == CPP_COMMA)
19627 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19633 cp_parser_consume_semicolon_at_end_of_statement (parser);
19634 token = cp_lexer_peek_token (parser->lexer);
19637 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19638 /* For historical reasons, we accept an optional semicolon. */
19639 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19640 cp_lexer_consume_token (parser->lexer);
19643 /* Parse an Objective-C protocol declaration. */
19646 cp_parser_objc_protocol_declaration (cp_parser* parser)
19648 tree proto, protorefs;
19651 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19654 tok = cp_lexer_peek_token (parser->lexer);
19655 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19659 /* See if we have a forward declaration or a definition. */
19660 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19662 /* Try a forward declaration first. */
19663 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19665 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19667 cp_parser_consume_semicolon_at_end_of_statement (parser);
19670 /* Ok, we got a full-fledged definition (or at least should). */
19673 proto = cp_parser_identifier (parser);
19674 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19675 objc_start_protocol (proto, protorefs);
19676 cp_parser_objc_method_prototype_list (parser);
19680 /* Parse an Objective-C superclass or category. */
19683 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19686 cp_token *next = cp_lexer_peek_token (parser->lexer);
19688 *super = *categ = NULL_TREE;
19689 if (next->type == CPP_COLON)
19691 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19692 *super = cp_parser_identifier (parser);
19694 else if (next->type == CPP_OPEN_PAREN)
19696 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19697 *categ = cp_parser_identifier (parser);
19698 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19702 /* Parse an Objective-C class interface. */
19705 cp_parser_objc_class_interface (cp_parser* parser)
19707 tree name, super, categ, protos;
19709 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19710 name = cp_parser_identifier (parser);
19711 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19712 protos = cp_parser_objc_protocol_refs_opt (parser);
19714 /* We have either a class or a category on our hands. */
19716 objc_start_category_interface (name, categ, protos);
19719 objc_start_class_interface (name, super, protos);
19720 /* Handle instance variable declarations, if any. */
19721 cp_parser_objc_class_ivars (parser);
19722 objc_continue_interface ();
19725 cp_parser_objc_method_prototype_list (parser);
19728 /* Parse an Objective-C class implementation. */
19731 cp_parser_objc_class_implementation (cp_parser* parser)
19733 tree name, super, categ;
19735 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19736 name = cp_parser_identifier (parser);
19737 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19739 /* We have either a class or a category on our hands. */
19741 objc_start_category_implementation (name, categ);
19744 objc_start_class_implementation (name, super);
19745 /* Handle instance variable declarations, if any. */
19746 cp_parser_objc_class_ivars (parser);
19747 objc_continue_implementation ();
19750 cp_parser_objc_method_definition_list (parser);
19753 /* Consume the @end token and finish off the implementation. */
19756 cp_parser_objc_end_implementation (cp_parser* parser)
19758 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19759 objc_finish_implementation ();
19762 /* Parse an Objective-C declaration. */
19765 cp_parser_objc_declaration (cp_parser* parser)
19767 /* Try to figure out what kind of declaration is present. */
19768 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19770 switch (kwd->keyword)
19773 cp_parser_objc_alias_declaration (parser);
19776 cp_parser_objc_class_declaration (parser);
19778 case RID_AT_PROTOCOL:
19779 cp_parser_objc_protocol_declaration (parser);
19781 case RID_AT_INTERFACE:
19782 cp_parser_objc_class_interface (parser);
19784 case RID_AT_IMPLEMENTATION:
19785 cp_parser_objc_class_implementation (parser);
19788 cp_parser_objc_end_implementation (parser);
19791 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19792 &kwd->location, kwd->u.value);
19793 cp_parser_skip_to_end_of_block_or_statement (parser);
19797 /* Parse an Objective-C try-catch-finally statement.
19799 objc-try-catch-finally-stmt:
19800 @try compound-statement objc-catch-clause-seq [opt]
19801 objc-finally-clause [opt]
19803 objc-catch-clause-seq:
19804 objc-catch-clause objc-catch-clause-seq [opt]
19807 @catch ( exception-declaration ) compound-statement
19809 objc-finally-clause
19810 @finally compound-statement
19812 Returns NULL_TREE. */
19815 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19816 location_t location;
19819 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19820 location = cp_lexer_peek_token (parser->lexer)->location;
19821 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19822 node, lest it get absorbed into the surrounding block. */
19823 stmt = push_stmt_list ();
19824 cp_parser_compound_statement (parser, NULL, false);
19825 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19827 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19829 cp_parameter_declarator *parmdecl;
19832 cp_lexer_consume_token (parser->lexer);
19833 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19834 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19835 parm = grokdeclarator (parmdecl->declarator,
19836 &parmdecl->decl_specifiers,
19837 PARM, /*initialized=*/0,
19838 /*attrlist=*/NULL);
19839 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19840 objc_begin_catch_clause (parm);
19841 cp_parser_compound_statement (parser, NULL, false);
19842 objc_finish_catch_clause ();
19845 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19847 cp_lexer_consume_token (parser->lexer);
19848 location = cp_lexer_peek_token (parser->lexer)->location;
19849 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19850 node, lest it get absorbed into the surrounding block. */
19851 stmt = push_stmt_list ();
19852 cp_parser_compound_statement (parser, NULL, false);
19853 objc_build_finally_clause (location, pop_stmt_list (stmt));
19856 return objc_finish_try_stmt ();
19859 /* Parse an Objective-C synchronized statement.
19861 objc-synchronized-stmt:
19862 @synchronized ( expression ) compound-statement
19864 Returns NULL_TREE. */
19867 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19868 location_t location;
19871 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19873 location = cp_lexer_peek_token (parser->lexer)->location;
19874 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19875 lock = cp_parser_expression (parser, false);
19876 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19878 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19879 node, lest it get absorbed into the surrounding block. */
19880 stmt = push_stmt_list ();
19881 cp_parser_compound_statement (parser, NULL, false);
19883 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19886 /* Parse an Objective-C throw statement.
19889 @throw assignment-expression [opt] ;
19891 Returns a constructed '@throw' statement. */
19894 cp_parser_objc_throw_statement (cp_parser *parser) {
19895 tree expr = NULL_TREE;
19897 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19900 expr = cp_parser_assignment_expression (parser, false);
19902 cp_parser_consume_semicolon_at_end_of_statement (parser);
19904 return objc_build_throw_stmt (expr);
19907 /* Parse an Objective-C statement. */
19910 cp_parser_objc_statement (cp_parser * parser) {
19911 /* Try to figure out what kind of declaration is present. */
19912 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19914 switch (kwd->keyword)
19917 return cp_parser_objc_try_catch_finally_statement (parser);
19918 case RID_AT_SYNCHRONIZED:
19919 return cp_parser_objc_synchronized_statement (parser);
19921 return cp_parser_objc_throw_statement (parser);
19923 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19924 &kwd->location, kwd->u.value);
19925 cp_parser_skip_to_end_of_block_or_statement (parser);
19928 return error_mark_node;
19931 /* OpenMP 2.5 parsing routines. */
19933 /* Returns name of the next clause.
19934 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19935 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19936 returned and the token is consumed. */
19938 static pragma_omp_clause
19939 cp_parser_omp_clause_name (cp_parser *parser)
19941 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19943 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19944 result = PRAGMA_OMP_CLAUSE_IF;
19945 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19946 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19947 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19948 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19949 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19951 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19952 const char *p = IDENTIFIER_POINTER (id);
19957 if (!strcmp ("collapse", p))
19958 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19959 else if (!strcmp ("copyin", p))
19960 result = PRAGMA_OMP_CLAUSE_COPYIN;
19961 else if (!strcmp ("copyprivate", p))
19962 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19965 if (!strcmp ("firstprivate", p))
19966 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19969 if (!strcmp ("lastprivate", p))
19970 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19973 if (!strcmp ("nowait", p))
19974 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19975 else if (!strcmp ("num_threads", p))
19976 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19979 if (!strcmp ("ordered", p))
19980 result = PRAGMA_OMP_CLAUSE_ORDERED;
19983 if (!strcmp ("reduction", p))
19984 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19987 if (!strcmp ("schedule", p))
19988 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19989 else if (!strcmp ("shared", p))
19990 result = PRAGMA_OMP_CLAUSE_SHARED;
19993 if (!strcmp ("untied", p))
19994 result = PRAGMA_OMP_CLAUSE_UNTIED;
19999 if (result != PRAGMA_OMP_CLAUSE_NONE)
20000 cp_lexer_consume_token (parser->lexer);
20005 /* Validate that a clause of the given type does not already exist. */
20008 check_no_duplicate_clause (tree clauses, enum tree_code code,
20009 const char *name, location_t location)
20013 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20014 if (OMP_CLAUSE_CODE (c) == code)
20016 error ("%Htoo many %qs clauses", &location, name);
20024 variable-list , identifier
20026 In addition, we match a closing parenthesis. An opening parenthesis
20027 will have been consumed by the caller.
20029 If KIND is nonzero, create the appropriate node and install the decl
20030 in OMP_CLAUSE_DECL and add the node to the head of the list.
20032 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20033 return the list created. */
20036 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20044 token = cp_lexer_peek_token (parser->lexer);
20045 name = cp_parser_id_expression (parser, /*template_p=*/false,
20046 /*check_dependency_p=*/true,
20047 /*template_p=*/NULL,
20048 /*declarator_p=*/false,
20049 /*optional_p=*/false);
20050 if (name == error_mark_node)
20053 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20054 if (decl == error_mark_node)
20055 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20056 else if (kind != 0)
20058 tree u = build_omp_clause (kind);
20059 OMP_CLAUSE_DECL (u) = decl;
20060 OMP_CLAUSE_CHAIN (u) = list;
20064 list = tree_cons (decl, NULL_TREE, list);
20067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20069 cp_lexer_consume_token (parser->lexer);
20072 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20076 /* Try to resync to an unnested comma. Copied from
20077 cp_parser_parenthesized_expression_list. */
20079 ending = cp_parser_skip_to_closing_parenthesis (parser,
20080 /*recovering=*/true,
20082 /*consume_paren=*/true);
20090 /* Similarly, but expect leading and trailing parenthesis. This is a very
20091 common case for omp clauses. */
20094 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20096 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20097 return cp_parser_omp_var_list_no_open (parser, kind, list);
20102 collapse ( constant-expression ) */
20105 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20111 loc = cp_lexer_peek_token (parser->lexer)->location;
20112 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20115 num = cp_parser_constant_expression (parser, false, NULL);
20117 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20118 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20119 /*or_comma=*/false,
20120 /*consume_paren=*/true);
20122 if (num == error_mark_node)
20124 num = fold_non_dependent_expr (num);
20125 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20126 || !host_integerp (num, 0)
20127 || (n = tree_low_cst (num, 0)) <= 0
20130 error ("%Hcollapse argument needs positive constant integer expression",
20135 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20136 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20137 OMP_CLAUSE_CHAIN (c) = list;
20138 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20144 default ( shared | none ) */
20147 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20149 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20152 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20154 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20156 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20157 const char *p = IDENTIFIER_POINTER (id);
20162 if (strcmp ("none", p) != 0)
20164 kind = OMP_CLAUSE_DEFAULT_NONE;
20168 if (strcmp ("shared", p) != 0)
20170 kind = OMP_CLAUSE_DEFAULT_SHARED;
20177 cp_lexer_consume_token (parser->lexer);
20182 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20185 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20186 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20187 /*or_comma=*/false,
20188 /*consume_paren=*/true);
20190 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20193 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20194 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20195 OMP_CLAUSE_CHAIN (c) = list;
20196 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20202 if ( expression ) */
20205 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20209 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20212 t = cp_parser_condition (parser);
20214 if (t == error_mark_node
20215 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20216 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20217 /*or_comma=*/false,
20218 /*consume_paren=*/true);
20220 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20222 c = build_omp_clause (OMP_CLAUSE_IF);
20223 OMP_CLAUSE_IF_EXPR (c) = t;
20224 OMP_CLAUSE_CHAIN (c) = list;
20233 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20234 tree list, location_t location)
20238 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20240 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20241 OMP_CLAUSE_CHAIN (c) = list;
20246 num_threads ( expression ) */
20249 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20250 location_t location)
20254 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20257 t = cp_parser_expression (parser, false);
20259 if (t == error_mark_node
20260 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20261 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20262 /*or_comma=*/false,
20263 /*consume_paren=*/true);
20265 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20266 "num_threads", location);
20268 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20269 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20270 OMP_CLAUSE_CHAIN (c) = list;
20279 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20280 tree list, location_t location)
20284 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20285 "ordered", location);
20287 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20288 OMP_CLAUSE_CHAIN (c) = list;
20293 reduction ( reduction-operator : variable-list )
20295 reduction-operator:
20296 One of: + * - & ^ | && || */
20299 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20301 enum tree_code code;
20304 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20307 switch (cp_lexer_peek_token (parser->lexer)->type)
20319 code = BIT_AND_EXPR;
20322 code = BIT_XOR_EXPR;
20325 code = BIT_IOR_EXPR;
20328 code = TRUTH_ANDIF_EXPR;
20331 code = TRUTH_ORIF_EXPR;
20334 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20335 "%<|%>, %<&&%>, or %<||%>");
20337 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20338 /*or_comma=*/false,
20339 /*consume_paren=*/true);
20342 cp_lexer_consume_token (parser->lexer);
20344 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20347 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20348 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20349 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20355 schedule ( schedule-kind )
20356 schedule ( schedule-kind , expression )
20359 static | dynamic | guided | runtime | auto */
20362 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20366 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20369 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20371 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20373 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20374 const char *p = IDENTIFIER_POINTER (id);
20379 if (strcmp ("dynamic", p) != 0)
20381 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20385 if (strcmp ("guided", p) != 0)
20387 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20391 if (strcmp ("runtime", p) != 0)
20393 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20400 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20401 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20402 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20403 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20406 cp_lexer_consume_token (parser->lexer);
20408 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20411 cp_lexer_consume_token (parser->lexer);
20413 token = cp_lexer_peek_token (parser->lexer);
20414 t = cp_parser_assignment_expression (parser, false);
20416 if (t == error_mark_node)
20418 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20419 error ("%Hschedule %<runtime%> does not take "
20420 "a %<chunk_size%> parameter", &token->location);
20421 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20422 error ("%Hschedule %<auto%> does not take "
20423 "a %<chunk_size%> parameter", &token->location);
20425 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20427 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20430 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20433 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20434 OMP_CLAUSE_CHAIN (c) = list;
20438 cp_parser_error (parser, "invalid schedule kind");
20440 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20441 /*or_comma=*/false,
20442 /*consume_paren=*/true);
20450 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20451 tree list, location_t location)
20455 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20457 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20458 OMP_CLAUSE_CHAIN (c) = list;
20462 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20463 is a bitmask in MASK. Return the list of clauses found; the result
20464 of clause default goes in *pdefault. */
20467 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20468 const char *where, cp_token *pragma_tok)
20470 tree clauses = NULL;
20472 cp_token *token = NULL;
20474 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20476 pragma_omp_clause c_kind;
20477 const char *c_name;
20478 tree prev = clauses;
20480 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20481 cp_lexer_consume_token (parser->lexer);
20483 token = cp_lexer_peek_token (parser->lexer);
20484 c_kind = cp_parser_omp_clause_name (parser);
20489 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20490 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20492 c_name = "collapse";
20494 case PRAGMA_OMP_CLAUSE_COPYIN:
20495 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20498 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20499 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20501 c_name = "copyprivate";
20503 case PRAGMA_OMP_CLAUSE_DEFAULT:
20504 clauses = cp_parser_omp_clause_default (parser, clauses,
20506 c_name = "default";
20508 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20509 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20511 c_name = "firstprivate";
20513 case PRAGMA_OMP_CLAUSE_IF:
20514 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20517 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20518 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20520 c_name = "lastprivate";
20522 case PRAGMA_OMP_CLAUSE_NOWAIT:
20523 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20526 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20527 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20529 c_name = "num_threads";
20531 case PRAGMA_OMP_CLAUSE_ORDERED:
20532 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20534 c_name = "ordered";
20536 case PRAGMA_OMP_CLAUSE_PRIVATE:
20537 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20539 c_name = "private";
20541 case PRAGMA_OMP_CLAUSE_REDUCTION:
20542 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20543 c_name = "reduction";
20545 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20546 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20548 c_name = "schedule";
20550 case PRAGMA_OMP_CLAUSE_SHARED:
20551 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20555 case PRAGMA_OMP_CLAUSE_UNTIED:
20556 clauses = cp_parser_omp_clause_untied (parser, clauses,
20561 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20565 if (((mask >> c_kind) & 1) == 0)
20567 /* Remove the invalid clause(s) from the list to avoid
20568 confusing the rest of the compiler. */
20570 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20574 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20575 return finish_omp_clauses (clauses);
20582 In practice, we're also interested in adding the statement to an
20583 outer node. So it is convenient if we work around the fact that
20584 cp_parser_statement calls add_stmt. */
20587 cp_parser_begin_omp_structured_block (cp_parser *parser)
20589 unsigned save = parser->in_statement;
20591 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20592 This preserves the "not within loop or switch" style error messages
20593 for nonsense cases like
20599 if (parser->in_statement)
20600 parser->in_statement = IN_OMP_BLOCK;
20606 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20608 parser->in_statement = save;
20612 cp_parser_omp_structured_block (cp_parser *parser)
20614 tree stmt = begin_omp_structured_block ();
20615 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20617 cp_parser_statement (parser, NULL_TREE, false, NULL);
20619 cp_parser_end_omp_structured_block (parser, save);
20620 return finish_omp_structured_block (stmt);
20624 # pragma omp atomic new-line
20628 x binop= expr | x++ | ++x | x-- | --x
20630 +, *, -, /, &, ^, |, <<, >>
20632 where x is an lvalue expression with scalar type. */
20635 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20638 enum tree_code code;
20640 cp_parser_require_pragma_eol (parser, pragma_tok);
20642 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20644 switch (TREE_CODE (lhs))
20649 case PREINCREMENT_EXPR:
20650 case POSTINCREMENT_EXPR:
20651 lhs = TREE_OPERAND (lhs, 0);
20653 rhs = integer_one_node;
20656 case PREDECREMENT_EXPR:
20657 case POSTDECREMENT_EXPR:
20658 lhs = TREE_OPERAND (lhs, 0);
20660 rhs = integer_one_node;
20664 switch (cp_lexer_peek_token (parser->lexer)->type)
20670 code = TRUNC_DIV_EXPR;
20678 case CPP_LSHIFT_EQ:
20679 code = LSHIFT_EXPR;
20681 case CPP_RSHIFT_EQ:
20682 code = RSHIFT_EXPR;
20685 code = BIT_AND_EXPR;
20688 code = BIT_IOR_EXPR;
20691 code = BIT_XOR_EXPR;
20694 cp_parser_error (parser,
20695 "invalid operator for %<#pragma omp atomic%>");
20698 cp_lexer_consume_token (parser->lexer);
20700 rhs = cp_parser_expression (parser, false);
20701 if (rhs == error_mark_node)
20705 finish_omp_atomic (code, lhs, rhs);
20706 cp_parser_consume_semicolon_at_end_of_statement (parser);
20710 cp_parser_skip_to_end_of_block_or_statement (parser);
20715 # pragma omp barrier new-line */
20718 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20720 cp_parser_require_pragma_eol (parser, pragma_tok);
20721 finish_omp_barrier ();
20725 # pragma omp critical [(name)] new-line
20726 structured-block */
20729 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20731 tree stmt, name = NULL;
20733 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20735 cp_lexer_consume_token (parser->lexer);
20737 name = cp_parser_identifier (parser);
20739 if (name == error_mark_node
20740 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20741 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20742 /*or_comma=*/false,
20743 /*consume_paren=*/true);
20744 if (name == error_mark_node)
20747 cp_parser_require_pragma_eol (parser, pragma_tok);
20749 stmt = cp_parser_omp_structured_block (parser);
20750 return c_finish_omp_critical (stmt, name);
20754 # pragma omp flush flush-vars[opt] new-line
20757 ( variable-list ) */
20760 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20762 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20763 (void) cp_parser_omp_var_list (parser, 0, NULL);
20764 cp_parser_require_pragma_eol (parser, pragma_tok);
20766 finish_omp_flush ();
20769 /* Helper function, to parse omp for increment expression. */
20772 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20774 tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20780 cp_parser_skip_to_end_of_statement (parser);
20781 return error_mark_node;
20784 token = cp_lexer_peek_token (parser->lexer);
20785 op = binops_by_token [token->type].tree_type;
20794 cp_parser_skip_to_end_of_statement (parser);
20795 return error_mark_node;
20798 cp_lexer_consume_token (parser->lexer);
20799 rhs = cp_parser_binary_expression (parser, false,
20800 PREC_RELATIONAL_EXPRESSION);
20801 if (rhs == error_mark_node
20802 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20804 cp_parser_skip_to_end_of_statement (parser);
20805 return error_mark_node;
20808 return build2 (op, boolean_type_node, lhs, rhs);
20811 /* Helper function, to parse omp for increment expression. */
20814 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20816 cp_token *token = cp_lexer_peek_token (parser->lexer);
20822 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20824 op = (token->type == CPP_PLUS_PLUS
20825 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20826 cp_lexer_consume_token (parser->lexer);
20827 lhs = cp_parser_cast_expression (parser, false, false);
20829 return error_mark_node;
20830 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20833 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20835 return error_mark_node;
20837 token = cp_lexer_peek_token (parser->lexer);
20838 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20840 op = (token->type == CPP_PLUS_PLUS
20841 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20842 cp_lexer_consume_token (parser->lexer);
20843 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20846 op = cp_parser_assignment_operator_opt (parser);
20847 if (op == ERROR_MARK)
20848 return error_mark_node;
20850 if (op != NOP_EXPR)
20852 rhs = cp_parser_assignment_expression (parser, false);
20853 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20854 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20857 lhs = cp_parser_binary_expression (parser, false,
20858 PREC_ADDITIVE_EXPRESSION);
20859 token = cp_lexer_peek_token (parser->lexer);
20860 decl_first = lhs == decl;
20863 if (token->type != CPP_PLUS
20864 && token->type != CPP_MINUS)
20865 return error_mark_node;
20869 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20870 cp_lexer_consume_token (parser->lexer);
20871 rhs = cp_parser_binary_expression (parser, false,
20872 PREC_ADDITIVE_EXPRESSION);
20873 token = cp_lexer_peek_token (parser->lexer);
20874 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20876 if (lhs == NULL_TREE)
20878 if (op == PLUS_EXPR)
20881 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20884 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20885 NULL, tf_warning_or_error);
20888 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20892 if (rhs != decl || op == MINUS_EXPR)
20893 return error_mark_node;
20894 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20897 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20899 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20902 /* Parse the restricted form of the for statement allowed by OpenMP. */
20905 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20907 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20908 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20909 tree this_pre_body, cl;
20910 location_t loc_first;
20911 bool collapse_err = false;
20912 int i, collapse = 1, nbraces = 0;
20914 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20915 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20916 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20918 gcc_assert (collapse >= 1);
20920 declv = make_tree_vec (collapse);
20921 initv = make_tree_vec (collapse);
20922 condv = make_tree_vec (collapse);
20923 incrv = make_tree_vec (collapse);
20925 loc_first = cp_lexer_peek_token (parser->lexer)->location;
20927 for (i = 0; i < collapse; i++)
20929 int bracecount = 0;
20930 bool add_private_clause = false;
20933 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20935 cp_parser_error (parser, "for statement expected");
20938 loc = cp_lexer_consume_token (parser->lexer)->location;
20940 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20943 init = decl = real_decl = NULL;
20944 this_pre_body = push_stmt_list ();
20945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20947 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
20951 integer-type var = lb
20952 random-access-iterator-type var = lb
20953 pointer-type var = lb
20955 cp_decl_specifier_seq type_specifiers;
20957 /* First, try to parse as an initialized declaration. See
20958 cp_parser_condition, from whence the bulk of this is copied. */
20960 cp_parser_parse_tentatively (parser);
20961 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20963 if (cp_parser_parse_definitely (parser))
20965 /* If parsing a type specifier seq succeeded, then this
20966 MUST be a initialized declaration. */
20967 tree asm_specification, attributes;
20968 cp_declarator *declarator;
20970 declarator = cp_parser_declarator (parser,
20971 CP_PARSER_DECLARATOR_NAMED,
20972 /*ctor_dtor_or_conv_p=*/NULL,
20973 /*parenthesized_p=*/NULL,
20974 /*member_p=*/false);
20975 attributes = cp_parser_attributes_opt (parser);
20976 asm_specification = cp_parser_asm_specification_opt (parser);
20978 if (declarator == cp_error_declarator)
20979 cp_parser_skip_to_end_of_statement (parser);
20985 decl = start_decl (declarator, &type_specifiers,
20986 /*initialized_p=*/false, attributes,
20987 /*prefix_attributes=*/NULL_TREE,
20990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
20992 if (cp_lexer_next_token_is (parser->lexer,
20994 error ("parenthesized initialization is not allowed in "
20995 "OpenMP %<for%> loop");
20997 /* Trigger an error. */
20998 cp_parser_require (parser, CPP_EQ, "%<=%>");
21000 init = error_mark_node;
21001 cp_parser_skip_to_end_of_statement (parser);
21003 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21004 || type_dependent_expression_p (decl))
21006 bool is_direct_init, is_non_constant_init;
21008 init = cp_parser_initializer (parser,
21010 &is_non_constant_init);
21012 cp_finish_decl (decl, init, !is_non_constant_init,
21014 LOOKUP_ONLYCONVERTING);
21015 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21018 = tree_cons (NULL, this_pre_body, for_block);
21022 init = pop_stmt_list (this_pre_body);
21023 this_pre_body = NULL_TREE;
21028 cp_lexer_consume_token (parser->lexer);
21029 init = cp_parser_assignment_expression (parser, false);
21031 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21032 init = error_mark_node;
21034 cp_finish_decl (decl, NULL_TREE,
21035 /*init_const_expr_p=*/false,
21037 LOOKUP_ONLYCONVERTING);
21041 pop_scope (pushed_scope);
21047 /* If parsing a type specifier sequence failed, then
21048 this MUST be a simple expression. */
21049 cp_parser_parse_tentatively (parser);
21050 decl = cp_parser_primary_expression (parser, false, false,
21052 if (!cp_parser_error_occurred (parser)
21055 && CLASS_TYPE_P (TREE_TYPE (decl)))
21059 cp_parser_parse_definitely (parser);
21060 cp_parser_require (parser, CPP_EQ, "%<=%>");
21061 rhs = cp_parser_assignment_expression (parser, false);
21062 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21064 tf_warning_or_error));
21065 add_private_clause = true;
21070 cp_parser_abort_tentative_parse (parser);
21071 init = cp_parser_expression (parser, false);
21074 if (TREE_CODE (init) == MODIFY_EXPR
21075 || TREE_CODE (init) == MODOP_EXPR)
21076 real_decl = TREE_OPERAND (init, 0);
21081 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21084 this_pre_body = pop_stmt_list (this_pre_body);
21088 pre_body = push_stmt_list ();
21090 add_stmt (this_pre_body);
21091 pre_body = pop_stmt_list (pre_body);
21094 pre_body = this_pre_body;
21099 if (par_clauses != NULL && real_decl != NULL_TREE)
21102 for (c = par_clauses; *c ; )
21103 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21104 && OMP_CLAUSE_DECL (*c) == real_decl)
21106 error ("%Hiteration variable %qD should not be firstprivate",
21108 *c = OMP_CLAUSE_CHAIN (*c);
21110 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21111 && OMP_CLAUSE_DECL (*c) == real_decl)
21113 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21114 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21115 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21116 OMP_CLAUSE_DECL (l) = real_decl;
21117 OMP_CLAUSE_CHAIN (l) = clauses;
21118 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21120 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21121 CP_OMP_CLAUSE_INFO (*c) = NULL;
21122 add_private_clause = false;
21126 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21127 && OMP_CLAUSE_DECL (*c) == real_decl)
21128 add_private_clause = false;
21129 c = &OMP_CLAUSE_CHAIN (*c);
21133 if (add_private_clause)
21136 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21138 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21139 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21140 && OMP_CLAUSE_DECL (c) == decl)
21142 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21143 && OMP_CLAUSE_DECL (c) == decl)
21144 error ("%Hiteration variable %qD should not be firstprivate",
21146 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21147 && OMP_CLAUSE_DECL (c) == decl)
21148 error ("%Hiteration variable %qD should not be reduction",
21153 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21154 OMP_CLAUSE_DECL (c) = decl;
21155 c = finish_omp_clauses (c);
21158 OMP_CLAUSE_CHAIN (c) = clauses;
21165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21167 /* If decl is an iterator, preserve LHS and RHS of the relational
21168 expr until finish_omp_for. */
21170 && (type_dependent_expression_p (decl)
21171 || CLASS_TYPE_P (TREE_TYPE (decl))))
21172 cond = cp_parser_omp_for_cond (parser, decl);
21174 cond = cp_parser_condition (parser);
21176 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21179 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21181 /* If decl is an iterator, preserve the operator on decl
21182 until finish_omp_for. */
21184 && (type_dependent_expression_p (decl)
21185 || CLASS_TYPE_P (TREE_TYPE (decl))))
21186 incr = cp_parser_omp_for_incr (parser, decl);
21188 incr = cp_parser_expression (parser, false);
21191 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21192 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21193 /*or_comma=*/false,
21194 /*consume_paren=*/true);
21196 TREE_VEC_ELT (declv, i) = decl;
21197 TREE_VEC_ELT (initv, i) = init;
21198 TREE_VEC_ELT (condv, i) = cond;
21199 TREE_VEC_ELT (incrv, i) = incr;
21201 if (i == collapse - 1)
21204 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21205 in between the collapsed for loops to be still considered perfectly
21206 nested. Hopefully the final version clarifies this.
21207 For now handle (multiple) {'s and empty statements. */
21208 cp_parser_parse_tentatively (parser);
21211 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21213 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21215 cp_lexer_consume_token (parser->lexer);
21218 else if (bracecount
21219 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21220 cp_lexer_consume_token (parser->lexer);
21223 loc = cp_lexer_peek_token (parser->lexer)->location;
21224 error ("%Hnot enough collapsed for loops", &loc);
21225 collapse_err = true;
21226 cp_parser_abort_tentative_parse (parser);
21235 cp_parser_parse_definitely (parser);
21236 nbraces += bracecount;
21240 /* Note that we saved the original contents of this flag when we entered
21241 the structured block, and so we don't need to re-save it here. */
21242 parser->in_statement = IN_OMP_FOR;
21244 /* Note that the grammar doesn't call for a structured block here,
21245 though the loop as a whole is a structured block. */
21246 body = push_stmt_list ();
21247 cp_parser_statement (parser, NULL_TREE, false, NULL);
21248 body = pop_stmt_list (body);
21250 if (declv == NULL_TREE)
21253 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21254 pre_body, clauses);
21258 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21260 cp_lexer_consume_token (parser->lexer);
21263 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21264 cp_lexer_consume_token (parser->lexer);
21269 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21270 error ("%Hcollapsed loops not perfectly nested", &loc);
21272 collapse_err = true;
21273 cp_parser_statement_seq_opt (parser, NULL);
21274 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21280 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21281 for_block = TREE_CHAIN (for_block);
21288 #pragma omp for for-clause[optseq] new-line
21291 #define OMP_FOR_CLAUSE_MASK \
21292 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21293 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21294 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21295 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21296 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21297 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21298 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21299 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21302 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21304 tree clauses, sb, ret;
21307 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21308 "#pragma omp for", pragma_tok);
21310 sb = begin_omp_structured_block ();
21311 save = cp_parser_begin_omp_structured_block (parser);
21313 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21315 cp_parser_end_omp_structured_block (parser, save);
21316 add_stmt (finish_omp_structured_block (sb));
21322 # pragma omp master new-line
21323 structured-block */
21326 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21328 cp_parser_require_pragma_eol (parser, pragma_tok);
21329 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21333 # pragma omp ordered new-line
21334 structured-block */
21337 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21339 cp_parser_require_pragma_eol (parser, pragma_tok);
21340 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21346 { section-sequence }
21349 section-directive[opt] structured-block
21350 section-sequence section-directive structured-block */
21353 cp_parser_omp_sections_scope (cp_parser *parser)
21355 tree stmt, substmt;
21356 bool error_suppress = false;
21359 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21362 stmt = push_stmt_list ();
21364 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21368 substmt = begin_omp_structured_block ();
21369 save = cp_parser_begin_omp_structured_block (parser);
21373 cp_parser_statement (parser, NULL_TREE, false, NULL);
21375 tok = cp_lexer_peek_token (parser->lexer);
21376 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21378 if (tok->type == CPP_CLOSE_BRACE)
21380 if (tok->type == CPP_EOF)
21384 cp_parser_end_omp_structured_block (parser, save);
21385 substmt = finish_omp_structured_block (substmt);
21386 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21387 add_stmt (substmt);
21392 tok = cp_lexer_peek_token (parser->lexer);
21393 if (tok->type == CPP_CLOSE_BRACE)
21395 if (tok->type == CPP_EOF)
21398 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21400 cp_lexer_consume_token (parser->lexer);
21401 cp_parser_require_pragma_eol (parser, tok);
21402 error_suppress = false;
21404 else if (!error_suppress)
21406 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21407 error_suppress = true;
21410 substmt = cp_parser_omp_structured_block (parser);
21411 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21412 add_stmt (substmt);
21414 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21416 substmt = pop_stmt_list (stmt);
21418 stmt = make_node (OMP_SECTIONS);
21419 TREE_TYPE (stmt) = void_type_node;
21420 OMP_SECTIONS_BODY (stmt) = substmt;
21427 # pragma omp sections sections-clause[optseq] newline
21430 #define OMP_SECTIONS_CLAUSE_MASK \
21431 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21432 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21433 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21434 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21435 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21438 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21442 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21443 "#pragma omp sections", pragma_tok);
21445 ret = cp_parser_omp_sections_scope (parser);
21447 OMP_SECTIONS_CLAUSES (ret) = clauses;
21453 # pragma parallel parallel-clause new-line
21454 # pragma parallel for parallel-for-clause new-line
21455 # pragma parallel sections parallel-sections-clause new-line */
21457 #define OMP_PARALLEL_CLAUSE_MASK \
21458 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21459 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21460 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21461 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21462 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21463 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21464 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21465 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21468 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21470 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21471 const char *p_name = "#pragma omp parallel";
21472 tree stmt, clauses, par_clause, ws_clause, block;
21473 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21478 cp_lexer_consume_token (parser->lexer);
21479 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21480 p_name = "#pragma omp parallel for";
21481 mask |= OMP_FOR_CLAUSE_MASK;
21482 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21484 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21486 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21487 const char *p = IDENTIFIER_POINTER (id);
21488 if (strcmp (p, "sections") == 0)
21490 cp_lexer_consume_token (parser->lexer);
21491 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21492 p_name = "#pragma omp parallel sections";
21493 mask |= OMP_SECTIONS_CLAUSE_MASK;
21494 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21498 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21499 block = begin_omp_parallel ();
21500 save = cp_parser_begin_omp_structured_block (parser);
21504 case PRAGMA_OMP_PARALLEL:
21505 cp_parser_statement (parser, NULL_TREE, false, NULL);
21506 par_clause = clauses;
21509 case PRAGMA_OMP_PARALLEL_FOR:
21510 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21511 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21514 case PRAGMA_OMP_PARALLEL_SECTIONS:
21515 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21516 stmt = cp_parser_omp_sections_scope (parser);
21518 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21522 gcc_unreachable ();
21525 cp_parser_end_omp_structured_block (parser, save);
21526 stmt = finish_omp_parallel (par_clause, block);
21527 if (p_kind != PRAGMA_OMP_PARALLEL)
21528 OMP_PARALLEL_COMBINED (stmt) = 1;
21533 # pragma omp single single-clause[optseq] new-line
21534 structured-block */
21536 #define OMP_SINGLE_CLAUSE_MASK \
21537 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21538 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21539 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21540 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21543 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21545 tree stmt = make_node (OMP_SINGLE);
21546 TREE_TYPE (stmt) = void_type_node;
21548 OMP_SINGLE_CLAUSES (stmt)
21549 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21550 "#pragma omp single", pragma_tok);
21551 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21553 return add_stmt (stmt);
21557 # pragma omp task task-clause[optseq] new-line
21558 structured-block */
21560 #define OMP_TASK_CLAUSE_MASK \
21561 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21562 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21563 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21564 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21565 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21566 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21569 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21571 tree clauses, block;
21574 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21575 "#pragma omp task", pragma_tok);
21576 block = begin_omp_task ();
21577 save = cp_parser_begin_omp_structured_block (parser);
21578 cp_parser_statement (parser, NULL_TREE, false, NULL);
21579 cp_parser_end_omp_structured_block (parser, save);
21580 return finish_omp_task (clauses, block);
21584 # pragma omp taskwait new-line */
21587 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21589 cp_parser_require_pragma_eol (parser, pragma_tok);
21590 finish_omp_taskwait ();
21594 # pragma omp threadprivate (variable-list) */
21597 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21601 vars = cp_parser_omp_var_list (parser, 0, NULL);
21602 cp_parser_require_pragma_eol (parser, pragma_tok);
21604 finish_omp_threadprivate (vars);
21607 /* Main entry point to OpenMP statement pragmas. */
21610 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21614 switch (pragma_tok->pragma_kind)
21616 case PRAGMA_OMP_ATOMIC:
21617 cp_parser_omp_atomic (parser, pragma_tok);
21619 case PRAGMA_OMP_CRITICAL:
21620 stmt = cp_parser_omp_critical (parser, pragma_tok);
21622 case PRAGMA_OMP_FOR:
21623 stmt = cp_parser_omp_for (parser, pragma_tok);
21625 case PRAGMA_OMP_MASTER:
21626 stmt = cp_parser_omp_master (parser, pragma_tok);
21628 case PRAGMA_OMP_ORDERED:
21629 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21631 case PRAGMA_OMP_PARALLEL:
21632 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21634 case PRAGMA_OMP_SECTIONS:
21635 stmt = cp_parser_omp_sections (parser, pragma_tok);
21637 case PRAGMA_OMP_SINGLE:
21638 stmt = cp_parser_omp_single (parser, pragma_tok);
21640 case PRAGMA_OMP_TASK:
21641 stmt = cp_parser_omp_task (parser, pragma_tok);
21644 gcc_unreachable ();
21648 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21653 static GTY (()) cp_parser *the_parser;
21656 /* Special handling for the first token or line in the file. The first
21657 thing in the file might be #pragma GCC pch_preprocess, which loads a
21658 PCH file, which is a GC collection point. So we need to handle this
21659 first pragma without benefit of an existing lexer structure.
21661 Always returns one token to the caller in *FIRST_TOKEN. This is
21662 either the true first token of the file, or the first token after
21663 the initial pragma. */
21666 cp_parser_initial_pragma (cp_token *first_token)
21670 cp_lexer_get_preprocessor_token (NULL, first_token);
21671 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21674 cp_lexer_get_preprocessor_token (NULL, first_token);
21675 if (first_token->type == CPP_STRING)
21677 name = first_token->u.value;
21679 cp_lexer_get_preprocessor_token (NULL, first_token);
21680 if (first_token->type != CPP_PRAGMA_EOL)
21681 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21682 &first_token->location);
21685 error ("%Hexpected string literal", &first_token->location);
21687 /* Skip to the end of the pragma. */
21688 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21689 cp_lexer_get_preprocessor_token (NULL, first_token);
21691 /* Now actually load the PCH file. */
21693 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21695 /* Read one more token to return to our caller. We have to do this
21696 after reading the PCH file in, since its pointers have to be
21698 cp_lexer_get_preprocessor_token (NULL, first_token);
21701 /* Normal parsing of a pragma token. Here we can (and must) use the
21705 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21707 cp_token *pragma_tok;
21710 pragma_tok = cp_lexer_consume_token (parser->lexer);
21711 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21712 parser->lexer->in_pragma = true;
21714 id = pragma_tok->pragma_kind;
21717 case PRAGMA_GCC_PCH_PREPROCESS:
21718 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21719 &pragma_tok->location);
21722 case PRAGMA_OMP_BARRIER:
21725 case pragma_compound:
21726 cp_parser_omp_barrier (parser, pragma_tok);
21729 error ("%H%<#pragma omp barrier%> may only be "
21730 "used in compound statements", &pragma_tok->location);
21737 case PRAGMA_OMP_FLUSH:
21740 case pragma_compound:
21741 cp_parser_omp_flush (parser, pragma_tok);
21744 error ("%H%<#pragma omp flush%> may only be "
21745 "used in compound statements", &pragma_tok->location);
21752 case PRAGMA_OMP_TASKWAIT:
21755 case pragma_compound:
21756 cp_parser_omp_taskwait (parser, pragma_tok);
21759 error ("%H%<#pragma omp taskwait%> may only be "
21760 "used in compound statements",
21761 &pragma_tok->location);
21768 case PRAGMA_OMP_THREADPRIVATE:
21769 cp_parser_omp_threadprivate (parser, pragma_tok);
21772 case PRAGMA_OMP_ATOMIC:
21773 case PRAGMA_OMP_CRITICAL:
21774 case PRAGMA_OMP_FOR:
21775 case PRAGMA_OMP_MASTER:
21776 case PRAGMA_OMP_ORDERED:
21777 case PRAGMA_OMP_PARALLEL:
21778 case PRAGMA_OMP_SECTIONS:
21779 case PRAGMA_OMP_SINGLE:
21780 case PRAGMA_OMP_TASK:
21781 if (context == pragma_external)
21783 cp_parser_omp_construct (parser, pragma_tok);
21786 case PRAGMA_OMP_SECTION:
21787 error ("%H%<#pragma omp section%> may only be used in "
21788 "%<#pragma omp sections%> construct", &pragma_tok->location);
21792 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21793 c_invoke_pragma_handler (id);
21797 cp_parser_error (parser, "expected declaration specifiers");
21801 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21805 /* The interface the pragma parsers have to the lexer. */
21808 pragma_lex (tree *value)
21811 enum cpp_ttype ret;
21813 tok = cp_lexer_peek_token (the_parser->lexer);
21816 *value = tok->u.value;
21818 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21820 else if (ret == CPP_STRING)
21821 *value = cp_parser_string_literal (the_parser, false, false);
21824 cp_lexer_consume_token (the_parser->lexer);
21825 if (ret == CPP_KEYWORD)
21833 /* External interface. */
21835 /* Parse one entire translation unit. */
21838 c_parse_file (void)
21840 bool error_occurred;
21841 static bool already_called = false;
21843 if (already_called)
21845 sorry ("inter-module optimizations not implemented for C++");
21848 already_called = true;
21850 the_parser = cp_parser_new ();
21851 push_deferring_access_checks (flag_access_control
21852 ? dk_no_deferred : dk_no_check);
21853 error_occurred = cp_parser_translation_unit (the_parser);
21857 #include "gt-cp-parser.h"