2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
49 typedef struct cp_token GTY (())
51 /* The kind of token. */
52 ENUM_BITFIELD (cpp_ttype) type : 8;
53 /* If this token is a keyword, this value indicates which keyword.
54 Otherwise, this value is RID_MAX. */
55 ENUM_BITFIELD (rid) keyword : 8;
58 /* True if this token is from a system header. */
59 BOOL_BITFIELD in_system_header : 1;
60 /* True if this token is from a context where it is implicitly extern "C" */
61 BOOL_BITFIELD implicit_extern_c : 1;
62 /* True for a CPP_NAME token that is not a keyword (i.e., for which
63 KEYWORD is RID_MAX) iff this name was looked up and found to be
64 ambiguous. An error has already been reported. */
65 BOOL_BITFIELD ambiguous_p : 1;
66 /* The value associated with this token, if any. */
68 /* The location at which this token was found. */
72 /* We use a stack of token pointer for saving token sets. */
73 typedef struct cp_token *cp_token_position;
74 DEF_VEC_P (cp_token_position);
75 DEF_VEC_ALLOC_P (cp_token_position,heap);
77 static const cp_token eof_token =
79 CPP_EOF, RID_MAX, 0, 0, 0, false, NULL_TREE,
80 #if USE_MAPPED_LOCATION
87 /* The cp_lexer structure represents the C++ lexer. It is responsible
88 for managing the token stream from the preprocessor and supplying
89 it to the parser. Tokens are never added to the cp_lexer after
92 typedef struct cp_lexer GTY (())
94 /* The memory allocated for the buffer. NULL if this lexer does not
95 own the token buffer. */
96 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
97 /* If the lexer owns the buffer, this is the number of tokens in the
101 /* A pointer just past the last available token. The tokens
102 in this lexer are [buffer, last_token). */
103 cp_token_position GTY ((skip)) last_token;
105 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
106 no more available tokens. */
107 cp_token_position GTY ((skip)) next_token;
109 /* A stack indicating positions at which cp_lexer_save_tokens was
110 called. The top entry is the most recent position at which we
111 began saving tokens. If the stack is non-empty, we are saving
113 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
115 /* True if we should output debugging information. */
118 /* The next lexer in a linked list of lexers. */
119 struct cp_lexer *next;
122 /* cp_token_cache is a range of tokens. There is no need to represent
123 allocate heap memory for it, since tokens are never removed from the
124 lexer's array. There is also no need for the GC to walk through
125 a cp_token_cache, since everything in here is referenced through
128 typedef struct cp_token_cache GTY(())
130 /* The beginning of the token range. */
131 cp_token * GTY((skip)) first;
133 /* Points immediately after the last token in the range. */
134 cp_token * GTY ((skip)) last;
139 static cp_lexer *cp_lexer_new_main
141 static cp_lexer *cp_lexer_new_from_tokens
142 (cp_token_cache *tokens);
143 static void cp_lexer_destroy
145 static int cp_lexer_saving_tokens
147 static cp_token_position cp_lexer_token_position
149 static cp_token *cp_lexer_token_at
150 (cp_lexer *, cp_token_position);
151 static void cp_lexer_get_preprocessor_token
152 (cp_lexer *, cp_token *);
153 static inline cp_token *cp_lexer_peek_token
155 static cp_token *cp_lexer_peek_nth_token
156 (cp_lexer *, size_t);
157 static inline bool cp_lexer_next_token_is
158 (cp_lexer *, enum cpp_ttype);
159 static bool cp_lexer_next_token_is_not
160 (cp_lexer *, enum cpp_ttype);
161 static bool cp_lexer_next_token_is_keyword
162 (cp_lexer *, enum rid);
163 static cp_token *cp_lexer_consume_token
165 static void cp_lexer_purge_token
167 static void cp_lexer_purge_tokens_after
168 (cp_lexer *, cp_token_position);
169 static void cp_lexer_handle_pragma
171 static void cp_lexer_save_tokens
173 static void cp_lexer_commit_tokens
175 static void cp_lexer_rollback_tokens
177 #ifdef ENABLE_CHECKING
178 static void cp_lexer_print_token
179 (FILE *, cp_token *);
180 static inline bool cp_lexer_debugging_p
182 static void cp_lexer_start_debugging
183 (cp_lexer *) ATTRIBUTE_UNUSED;
184 static void cp_lexer_stop_debugging
185 (cp_lexer *) ATTRIBUTE_UNUSED;
187 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
188 about passing NULL to functions that require non-NULL arguments
189 (fputs, fprintf). It will never be used, so all we need is a value
190 of the right type that's guaranteed not to be NULL. */
191 #define cp_lexer_debug_stream stdout
192 #define cp_lexer_print_token(str, tok) (void) 0
193 #define cp_lexer_debugging_p(lexer) 0
194 #endif /* ENABLE_CHECKING */
196 static cp_token_cache *cp_token_cache_new
197 (cp_token *, cp_token *);
199 /* Manifest constants. */
200 #define CP_LEXER_BUFFER_SIZE 10000
201 #define CP_SAVED_TOKEN_STACK 5
203 /* A token type for keywords, as opposed to ordinary identifiers. */
204 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
206 /* A token type for template-ids. If a template-id is processed while
207 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
208 the value of the CPP_TEMPLATE_ID is whatever was returned by
209 cp_parser_template_id. */
210 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
212 /* A token type for nested-name-specifiers. If a
213 nested-name-specifier is processed while parsing tentatively, it is
214 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
215 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
216 cp_parser_nested_name_specifier_opt. */
217 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
219 /* A token type for tokens that are not tokens at all; these are used
220 to represent slots in the array where there used to be a token
221 that has now been deleted. */
222 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
224 /* The number of token types, including C++-specific ones. */
225 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
229 #ifdef ENABLE_CHECKING
230 /* The stream to which debugging output should be written. */
231 static FILE *cp_lexer_debug_stream;
232 #endif /* ENABLE_CHECKING */
234 /* Create a new main C++ lexer, the lexer that gets tokens from the
238 cp_lexer_new_main (void)
240 cp_token first_token;
247 /* It's possible that lexing the first token will load a PCH file,
248 which is a GC collection point. So we have to grab the first
249 token before allocating any memory. Pragmas must not be deferred
250 as -fpch-preprocess can generate a pragma to load the PCH file in
251 the preprocessed output used by -save-temps. */
252 cp_lexer_get_preprocessor_token (NULL, &first_token);
254 /* Tell cpplib we want CPP_PRAGMA tokens. */
255 cpp_get_options (parse_in)->defer_pragmas = true;
257 /* Tell pragma_lex not to merge string constants. */
258 c_lex_return_raw_strings = true;
260 c_common_no_more_pch ();
262 /* Allocate the memory. */
263 lexer = GGC_CNEW (cp_lexer);
265 #ifdef ENABLE_CHECKING
266 /* Initially we are not debugging. */
267 lexer->debugging_p = false;
268 #endif /* ENABLE_CHECKING */
269 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
270 CP_SAVED_TOKEN_STACK);
272 /* Create the buffer. */
273 alloc = CP_LEXER_BUFFER_SIZE;
274 buffer = GGC_NEWVEC (cp_token, alloc);
276 /* Put the first token in the buffer. */
281 /* Get the remaining tokens from the preprocessor. */
282 while (pos->type != CPP_EOF)
289 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
290 pos = buffer + space;
292 cp_lexer_get_preprocessor_token (lexer, pos);
294 lexer->buffer = buffer;
295 lexer->buffer_length = alloc - space;
296 lexer->last_token = pos;
297 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
299 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
300 direct calls to pragma_lex. Those callers all expect pragma_lex
301 to do string constant concatenation. */
302 c_lex_return_raw_strings = false;
304 /* Subsequent preprocessor diagnostics should use compiler
305 diagnostic functions to get the compiler source location. */
306 cpp_get_options (parse_in)->client_diagnostic = true;
307 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
309 gcc_assert (lexer->next_token->type != CPP_PURGED);
313 /* Create a new lexer whose token stream is primed with the tokens in
314 CACHE. When these tokens are exhausted, no new tokens will be read. */
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
319 cp_token *first = cache->first;
320 cp_token *last = cache->last;
321 cp_lexer *lexer = GGC_CNEW (cp_lexer);
323 /* We do not own the buffer. */
324 lexer->buffer = NULL;
325 lexer->buffer_length = 0;
326 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327 lexer->last_token = last;
329 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330 CP_SAVED_TOKEN_STACK);
332 #ifdef ENABLE_CHECKING
333 /* Initially we are not debugging. */
334 lexer->debugging_p = false;
337 gcc_assert (lexer->next_token->type != CPP_PURGED);
341 /* Frees all resources associated with LEXER. */
344 cp_lexer_destroy (cp_lexer *lexer)
347 ggc_free (lexer->buffer);
348 VEC_free (cp_token_position, heap, lexer->saved_tokens);
352 /* Returns nonzero if debugging information should be output. */
354 #ifdef ENABLE_CHECKING
357 cp_lexer_debugging_p (cp_lexer *lexer)
359 return lexer->debugging_p;
362 #endif /* ENABLE_CHECKING */
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
367 gcc_assert (!previous_p || lexer->next_token != &eof_token);
369 return lexer->next_token - previous_p;
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
378 /* nonzero if we are presently saving tokens. */
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
383 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
386 /* Store the next token from the preprocessor in *TOKEN. Return true
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
393 static int is_extern_c = 0;
395 /* Get a new token from the preprocessor. */
397 = c_lex_with_flags (&token->value, &token->location, &token->flags);
398 token->in_system_header = in_system_header;
400 /* On some systems, some header files are surrounded by an
401 implicit extern "C" block. Set a flag in the token if it
402 comes from such a header. */
403 is_extern_c += pending_lang_change;
404 pending_lang_change = 0;
405 token->implicit_extern_c = is_extern_c > 0;
407 /* Check to see if this token is a keyword. */
408 if (token->type == CPP_NAME)
410 if (C_IS_RESERVED_WORD (token->value))
412 /* Mark this token as a keyword. */
413 token->type = CPP_KEYWORD;
414 /* Record which keyword. */
415 token->keyword = C_RID_CODE (token->value);
416 /* Update the value. Some keywords are mapped to particular
417 entities, rather than simply having the value of the
418 corresponding IDENTIFIER_NODE. For example, `__const' is
419 mapped to `const'. */
420 token->value = ridpointers[token->keyword];
424 token->ambiguous_p = false;
425 token->keyword = RID_MAX;
428 /* Handle Objective-C++ keywords. */
429 else if (token->type == CPP_AT_NAME)
431 token->type = CPP_KEYWORD;
432 switch (C_RID_CODE (token->value))
434 /* Map 'class' to '@class', 'private' to '@private', etc. */
435 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439 case RID_THROW: token->keyword = RID_AT_THROW; break;
440 case RID_TRY: token->keyword = RID_AT_TRY; break;
441 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442 default: token->keyword = C_RID_CODE (token->value);
446 token->keyword = RID_MAX;
449 /* Update the globals input_location and in_system_header from TOKEN. */
451 cp_lexer_set_source_position_from_token (cp_token *token)
453 if (token->type != CPP_EOF)
455 input_location = token->location;
456 in_system_header = token->in_system_header;
460 /* Return a pointer to the next token in the token stream, but do not
463 static inline cp_token *
464 cp_lexer_peek_token (cp_lexer *lexer)
466 if (cp_lexer_debugging_p (lexer))
468 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
469 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
470 putc ('\n', cp_lexer_debug_stream);
472 return lexer->next_token;
475 /* Return true if the next token has the indicated TYPE. */
478 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
480 return cp_lexer_peek_token (lexer)->type == type;
483 /* Return true if the next token does not have the indicated TYPE. */
486 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
488 return !cp_lexer_next_token_is (lexer, type);
491 /* Return true if the next token is the indicated KEYWORD. */
494 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
498 /* Peek at the next token. */
499 token = cp_lexer_peek_token (lexer);
500 /* Check to see if it is the indicated keyword. */
501 return token->keyword == keyword;
504 /* Return a pointer to the Nth token in the token stream. If N is 1,
505 then this is precisely equivalent to cp_lexer_peek_token (except
506 that it is not inline). One would like to disallow that case, but
507 there is one case (cp_parser_nth_token_starts_template_id) where
508 the caller passes a variable for N and it might be 1. */
511 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
515 /* N is 1-based, not zero-based. */
518 if (cp_lexer_debugging_p (lexer))
519 fprintf (cp_lexer_debug_stream,
520 "cp_lexer: peeking ahead %ld at token: ", (long)n);
523 token = lexer->next_token;
524 gcc_assert (!n || token != &eof_token);
528 if (token == lexer->last_token)
530 token = (cp_token *)&eof_token;
534 if (token->type != CPP_PURGED)
538 if (cp_lexer_debugging_p (lexer))
540 cp_lexer_print_token (cp_lexer_debug_stream, token);
541 putc ('\n', cp_lexer_debug_stream);
547 /* Return the next token, and advance the lexer's next_token pointer
548 to point to the next non-purged token. */
551 cp_lexer_consume_token (cp_lexer* lexer)
553 cp_token *token = lexer->next_token;
555 gcc_assert (token != &eof_token);
560 if (lexer->next_token == lexer->last_token)
562 lexer->next_token = (cp_token *)&eof_token;
567 while (lexer->next_token->type == CPP_PURGED);
569 cp_lexer_set_source_position_from_token (token);
571 /* Provide debugging output. */
572 if (cp_lexer_debugging_p (lexer))
574 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
575 cp_lexer_print_token (cp_lexer_debug_stream, token);
576 putc ('\n', cp_lexer_debug_stream);
582 /* Permanently remove the next token from the token stream, and
583 advance the next_token pointer to refer to the next non-purged
587 cp_lexer_purge_token (cp_lexer *lexer)
589 cp_token *tok = lexer->next_token;
591 gcc_assert (tok != &eof_token);
592 tok->type = CPP_PURGED;
593 tok->location = UNKNOWN_LOCATION;
594 tok->value = NULL_TREE;
595 tok->keyword = RID_MAX;
600 if (tok == lexer->last_token)
602 tok = (cp_token *)&eof_token;
606 while (tok->type == CPP_PURGED);
607 lexer->next_token = tok;
610 /* Permanently remove all tokens after TOK, up to, but not
611 including, the token that will be returned next by
612 cp_lexer_peek_token. */
615 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
617 cp_token *peek = lexer->next_token;
619 if (peek == &eof_token)
620 peek = lexer->last_token;
622 gcc_assert (tok < peek);
624 for ( tok += 1; tok != peek; tok += 1)
626 tok->type = CPP_PURGED;
627 tok->location = UNKNOWN_LOCATION;
628 tok->value = NULL_TREE;
629 tok->keyword = RID_MAX;
633 /* Consume and handle a pragma token. */
635 cp_lexer_handle_pragma (cp_lexer *lexer)
638 cp_token *token = cp_lexer_consume_token (lexer);
639 gcc_assert (token->type == CPP_PRAGMA);
640 gcc_assert (token->value);
642 s.len = TREE_STRING_LENGTH (token->value);
643 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
645 cpp_handle_deferred_pragma (parse_in, &s);
647 /* Clearing token->value here means that we will get an ICE if we
648 try to process this #pragma again (which should be impossible). */
652 /* Begin saving tokens. All tokens consumed after this point will be
656 cp_lexer_save_tokens (cp_lexer* lexer)
658 /* Provide debugging output. */
659 if (cp_lexer_debugging_p (lexer))
660 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
662 VEC_safe_push (cp_token_position, heap,
663 lexer->saved_tokens, lexer->next_token);
666 /* Commit to the portion of the token stream most recently saved. */
669 cp_lexer_commit_tokens (cp_lexer* lexer)
671 /* Provide debugging output. */
672 if (cp_lexer_debugging_p (lexer))
673 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
675 VEC_pop (cp_token_position, lexer->saved_tokens);
678 /* Return all tokens saved since the last call to cp_lexer_save_tokens
679 to the token stream. Stop saving tokens. */
682 cp_lexer_rollback_tokens (cp_lexer* lexer)
684 /* Provide debugging output. */
685 if (cp_lexer_debugging_p (lexer))
686 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
688 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
691 /* Print a representation of the TOKEN on the STREAM. */
693 #ifdef ENABLE_CHECKING
696 cp_lexer_print_token (FILE * stream, cp_token *token)
698 /* We don't use cpp_type2name here because the parser defines
699 a few tokens of its own. */
700 static const char *const token_names[] = {
701 /* cpplib-defined token types */
707 /* C++ parser token types - see "Manifest constants", above. */
710 "NESTED_NAME_SPECIFIER",
714 /* If we have a name for the token, print it out. Otherwise, we
715 simply give the numeric code. */
716 gcc_assert (token->type < ARRAY_SIZE(token_names));
717 fputs (token_names[token->type], stream);
719 /* For some tokens, print the associated data. */
723 /* Some keywords have a value that is not an IDENTIFIER_NODE.
724 For example, `struct' is mapped to an INTEGER_CST. */
725 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
727 /* else fall through */
729 fputs (IDENTIFIER_POINTER (token->value), stream);
735 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
743 /* Start emitting debugging information. */
746 cp_lexer_start_debugging (cp_lexer* lexer)
748 lexer->debugging_p = true;
751 /* Stop emitting debugging information. */
754 cp_lexer_stop_debugging (cp_lexer* lexer)
756 lexer->debugging_p = false;
759 #endif /* ENABLE_CHECKING */
761 /* Create a new cp_token_cache, representing a range of tokens. */
763 static cp_token_cache *
764 cp_token_cache_new (cp_token *first, cp_token *last)
766 cp_token_cache *cache = GGC_NEW (cp_token_cache);
767 cache->first = first;
773 /* Decl-specifiers. */
775 static void clear_decl_specs
776 (cp_decl_specifier_seq *);
778 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
781 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
783 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
788 /* Nothing other than the parser should be creating declarators;
789 declarators are a semi-syntactic representation of C++ entities.
790 Other parts of the front end that need to create entities (like
791 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
793 static cp_declarator *make_call_declarator
794 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
795 static cp_declarator *make_array_declarator
796 (cp_declarator *, tree);
797 static cp_declarator *make_pointer_declarator
798 (cp_cv_quals, cp_declarator *);
799 static cp_declarator *make_reference_declarator
800 (cp_cv_quals, cp_declarator *);
801 static cp_parameter_declarator *make_parameter_declarator
802 (cp_decl_specifier_seq *, cp_declarator *, tree);
803 static cp_declarator *make_ptrmem_declarator
804 (cp_cv_quals, tree, cp_declarator *);
806 cp_declarator *cp_error_declarator;
808 /* The obstack on which declarators and related data structures are
810 static struct obstack declarator_obstack;
812 /* Alloc BYTES from the declarator memory pool. */
815 alloc_declarator (size_t bytes)
817 return obstack_alloc (&declarator_obstack, bytes);
820 /* Allocate a declarator of the indicated KIND. Clear fields that are
821 common to all declarators. */
823 static cp_declarator *
824 make_declarator (cp_declarator_kind kind)
826 cp_declarator *declarator;
828 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
829 declarator->kind = kind;
830 declarator->attributes = NULL_TREE;
831 declarator->declarator = NULL;
836 /* Make a declarator for a generalized identifier. If non-NULL, the
837 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
838 just UNQUALIFIED_NAME. */
840 static cp_declarator *
841 make_id_declarator (tree qualifying_scope, tree unqualified_name)
843 cp_declarator *declarator;
845 /* It is valid to write:
847 class C { void f(); };
851 The standard is not clear about whether `typedef const C D' is
852 legal; as of 2002-09-15 the committee is considering that
853 question. EDG 3.0 allows that syntax. Therefore, we do as
855 if (qualifying_scope && TYPE_P (qualifying_scope))
856 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
858 declarator = make_declarator (cdk_id);
859 declarator->u.id.qualifying_scope = qualifying_scope;
860 declarator->u.id.unqualified_name = unqualified_name;
861 declarator->u.id.sfk = sfk_none;
866 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
867 of modifiers such as const or volatile to apply to the pointer
868 type, represented as identifiers. */
871 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
873 cp_declarator *declarator;
875 declarator = make_declarator (cdk_pointer);
876 declarator->declarator = target;
877 declarator->u.pointer.qualifiers = cv_qualifiers;
878 declarator->u.pointer.class_type = NULL_TREE;
883 /* Like make_pointer_declarator -- but for references. */
886 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
888 cp_declarator *declarator;
890 declarator = make_declarator (cdk_reference);
891 declarator->declarator = target;
892 declarator->u.pointer.qualifiers = cv_qualifiers;
893 declarator->u.pointer.class_type = NULL_TREE;
898 /* Like make_pointer_declarator -- but for a pointer to a non-static
899 member of CLASS_TYPE. */
902 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
903 cp_declarator *pointee)
905 cp_declarator *declarator;
907 declarator = make_declarator (cdk_ptrmem);
908 declarator->declarator = pointee;
909 declarator->u.pointer.qualifiers = cv_qualifiers;
910 declarator->u.pointer.class_type = class_type;
915 /* Make a declarator for the function given by TARGET, with the
916 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
917 "const"-qualified member function. The EXCEPTION_SPECIFICATION
918 indicates what exceptions can be thrown. */
921 make_call_declarator (cp_declarator *target,
922 cp_parameter_declarator *parms,
923 cp_cv_quals cv_qualifiers,
924 tree exception_specification)
926 cp_declarator *declarator;
928 declarator = make_declarator (cdk_function);
929 declarator->declarator = target;
930 declarator->u.function.parameters = parms;
931 declarator->u.function.qualifiers = cv_qualifiers;
932 declarator->u.function.exception_specification = exception_specification;
937 /* Make a declarator for an array of BOUNDS elements, each of which is
938 defined by ELEMENT. */
941 make_array_declarator (cp_declarator *element, tree bounds)
943 cp_declarator *declarator;
945 declarator = make_declarator (cdk_array);
946 declarator->declarator = element;
947 declarator->u.array.bounds = bounds;
952 cp_parameter_declarator *no_parameters;
954 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
955 DECLARATOR and DEFAULT_ARGUMENT. */
957 cp_parameter_declarator *
958 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
959 cp_declarator *declarator,
960 tree default_argument)
962 cp_parameter_declarator *parameter;
964 parameter = ((cp_parameter_declarator *)
965 alloc_declarator (sizeof (cp_parameter_declarator)));
966 parameter->next = NULL;
968 parameter->decl_specifiers = *decl_specifiers;
970 clear_decl_specs (¶meter->decl_specifiers);
971 parameter->declarator = declarator;
972 parameter->default_argument = default_argument;
973 parameter->ellipsis_p = false;
983 A cp_parser parses the token stream as specified by the C++
984 grammar. Its job is purely parsing, not semantic analysis. For
985 example, the parser breaks the token stream into declarators,
986 expressions, statements, and other similar syntactic constructs.
987 It does not check that the types of the expressions on either side
988 of an assignment-statement are compatible, or that a function is
989 not declared with a parameter of type `void'.
991 The parser invokes routines elsewhere in the compiler to perform
992 semantic analysis and to build up the abstract syntax tree for the
995 The parser (and the template instantiation code, which is, in a
996 way, a close relative of parsing) are the only parts of the
997 compiler that should be calling push_scope and pop_scope, or
998 related functions. The parser (and template instantiation code)
999 keeps track of what scope is presently active; everything else
1000 should simply honor that. (The code that generates static
1001 initializers may also need to set the scope, in order to check
1002 access control correctly when emitting the initializers.)
1007 The parser is of the standard recursive-descent variety. Upcoming
1008 tokens in the token stream are examined in order to determine which
1009 production to use when parsing a non-terminal. Some C++ constructs
1010 require arbitrary look ahead to disambiguate. For example, it is
1011 impossible, in the general case, to tell whether a statement is an
1012 expression or declaration without scanning the entire statement.
1013 Therefore, the parser is capable of "parsing tentatively." When the
1014 parser is not sure what construct comes next, it enters this mode.
1015 Then, while we attempt to parse the construct, the parser queues up
1016 error messages, rather than issuing them immediately, and saves the
1017 tokens it consumes. If the construct is parsed successfully, the
1018 parser "commits", i.e., it issues any queued error messages and
1019 the tokens that were being preserved are permanently discarded.
1020 If, however, the construct is not parsed successfully, the parser
1021 rolls back its state completely so that it can resume parsing using
1022 a different alternative.
1027 The performance of the parser could probably be improved substantially.
1028 We could often eliminate the need to parse tentatively by looking ahead
1029 a little bit. In some places, this approach might not entirely eliminate
1030 the need to parse tentatively, but it might still speed up the average
1033 /* Flags that are passed to some parsing functions. These values can
1034 be bitwise-ored together. */
1036 typedef enum cp_parser_flags
1039 CP_PARSER_FLAGS_NONE = 0x0,
1040 /* The construct is optional. If it is not present, then no error
1041 should be issued. */
1042 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1043 /* When parsing a type-specifier, do not allow user-defined types. */
1044 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1047 /* The different kinds of declarators we want to parse. */
1049 typedef enum cp_parser_declarator_kind
1051 /* We want an abstract declarator. */
1052 CP_PARSER_DECLARATOR_ABSTRACT,
1053 /* We want a named declarator. */
1054 CP_PARSER_DECLARATOR_NAMED,
1055 /* We don't mind, but the name must be an unqualified-id. */
1056 CP_PARSER_DECLARATOR_EITHER
1057 } cp_parser_declarator_kind;
1059 /* The precedence values used to parse binary expressions. The minimum value
1060 of PREC must be 1, because zero is reserved to quickly discriminate
1061 binary operators from other tokens. */
1066 PREC_LOGICAL_OR_EXPRESSION,
1067 PREC_LOGICAL_AND_EXPRESSION,
1068 PREC_INCLUSIVE_OR_EXPRESSION,
1069 PREC_EXCLUSIVE_OR_EXPRESSION,
1070 PREC_AND_EXPRESSION,
1071 PREC_EQUALITY_EXPRESSION,
1072 PREC_RELATIONAL_EXPRESSION,
1073 PREC_SHIFT_EXPRESSION,
1074 PREC_ADDITIVE_EXPRESSION,
1075 PREC_MULTIPLICATIVE_EXPRESSION,
1077 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1080 /* A mapping from a token type to a corresponding tree node type, with a
1081 precedence value. */
1083 typedef struct cp_parser_binary_operations_map_node
1085 /* The token type. */
1086 enum cpp_ttype token_type;
1087 /* The corresponding tree code. */
1088 enum tree_code tree_type;
1089 /* The precedence of this operator. */
1090 enum cp_parser_prec prec;
1091 } cp_parser_binary_operations_map_node;
1093 /* The status of a tentative parse. */
1095 typedef enum cp_parser_status_kind
1097 /* No errors have occurred. */
1098 CP_PARSER_STATUS_KIND_NO_ERROR,
1099 /* An error has occurred. */
1100 CP_PARSER_STATUS_KIND_ERROR,
1101 /* We are committed to this tentative parse, whether or not an error
1103 CP_PARSER_STATUS_KIND_COMMITTED
1104 } cp_parser_status_kind;
1106 typedef struct cp_parser_expression_stack_entry
1109 enum tree_code tree_type;
1111 } cp_parser_expression_stack_entry;
1113 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1114 entries because precedence levels on the stack are monotonically
1116 typedef struct cp_parser_expression_stack_entry
1117 cp_parser_expression_stack[NUM_PREC_VALUES];
1119 /* Context that is saved and restored when parsing tentatively. */
1120 typedef struct cp_parser_context GTY (())
1122 /* If this is a tentative parsing context, the status of the
1124 enum cp_parser_status_kind status;
1125 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1126 that are looked up in this context must be looked up both in the
1127 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1128 the context of the containing expression. */
1131 /* The next parsing context in the stack. */
1132 struct cp_parser_context *next;
1133 } cp_parser_context;
1137 /* Constructors and destructors. */
1139 static cp_parser_context *cp_parser_context_new
1140 (cp_parser_context *);
1142 /* Class variables. */
1144 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1146 /* The operator-precedence table used by cp_parser_binary_expression.
1147 Transformed into an associative array (binops_by_token) by
1150 static const cp_parser_binary_operations_map_node binops[] = {
1151 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1152 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1154 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1155 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1156 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1158 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1159 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1161 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1162 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1164 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1165 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1166 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1167 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1168 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1169 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1171 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1172 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1174 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1176 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1178 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1180 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1182 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1185 /* The same as binops, but initialized by cp_parser_new so that
1186 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1188 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1190 /* Constructors and destructors. */
1192 /* Construct a new context. The context below this one on the stack
1193 is given by NEXT. */
1195 static cp_parser_context *
1196 cp_parser_context_new (cp_parser_context* next)
1198 cp_parser_context *context;
1200 /* Allocate the storage. */
1201 if (cp_parser_context_free_list != NULL)
1203 /* Pull the first entry from the free list. */
1204 context = cp_parser_context_free_list;
1205 cp_parser_context_free_list = context->next;
1206 memset (context, 0, sizeof (*context));
1209 context = GGC_CNEW (cp_parser_context);
1211 /* No errors have occurred yet in this context. */
1212 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1213 /* If this is not the bottomost context, copy information that we
1214 need from the previous context. */
1217 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1218 expression, then we are parsing one in this context, too. */
1219 context->object_type = next->object_type;
1220 /* Thread the stack. */
1221 context->next = next;
1227 /* The cp_parser structure represents the C++ parser. */
1229 typedef struct cp_parser GTY(())
1231 /* The lexer from which we are obtaining tokens. */
1234 /* The scope in which names should be looked up. If NULL_TREE, then
1235 we look up names in the scope that is currently open in the
1236 source program. If non-NULL, this is either a TYPE or
1237 NAMESPACE_DECL for the scope in which we should look. It can
1238 also be ERROR_MARK, when we've parsed a bogus scope.
1240 This value is not cleared automatically after a name is looked
1241 up, so we must be careful to clear it before starting a new look
1242 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1243 will look up `Z' in the scope of `X', rather than the current
1244 scope.) Unfortunately, it is difficult to tell when name lookup
1245 is complete, because we sometimes peek at a token, look it up,
1246 and then decide not to consume it. */
1249 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1250 last lookup took place. OBJECT_SCOPE is used if an expression
1251 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1252 respectively. QUALIFYING_SCOPE is used for an expression of the
1253 form "X::Y"; it refers to X. */
1255 tree qualifying_scope;
1257 /* A stack of parsing contexts. All but the bottom entry on the
1258 stack will be tentative contexts.
1260 We parse tentatively in order to determine which construct is in
1261 use in some situations. For example, in order to determine
1262 whether a statement is an expression-statement or a
1263 declaration-statement we parse it tentatively as a
1264 declaration-statement. If that fails, we then reparse the same
1265 token stream as an expression-statement. */
1266 cp_parser_context *context;
1268 /* True if we are parsing GNU C++. If this flag is not set, then
1269 GNU extensions are not recognized. */
1270 bool allow_gnu_extensions_p;
1272 /* TRUE if the `>' token should be interpreted as the greater-than
1273 operator. FALSE if it is the end of a template-id or
1274 template-parameter-list. */
1275 bool greater_than_is_operator_p;
1277 /* TRUE if default arguments are allowed within a parameter list
1278 that starts at this point. FALSE if only a gnu extension makes
1279 them permissible. */
1280 bool default_arg_ok_p;
1282 /* TRUE if we are parsing an integral constant-expression. See
1283 [expr.const] for a precise definition. */
1284 bool integral_constant_expression_p;
1286 /* TRUE if we are parsing an integral constant-expression -- but a
1287 non-constant expression should be permitted as well. This flag
1288 is used when parsing an array bound so that GNU variable-length
1289 arrays are tolerated. */
1290 bool allow_non_integral_constant_expression_p;
1292 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1293 been seen that makes the expression non-constant. */
1294 bool non_integral_constant_expression_p;
1296 /* TRUE if local variable names and `this' are forbidden in the
1298 bool local_variables_forbidden_p;
1300 /* TRUE if the declaration we are parsing is part of a
1301 linkage-specification of the form `extern string-literal
1303 bool in_unbraced_linkage_specification_p;
1305 /* TRUE if we are presently parsing a declarator, after the
1306 direct-declarator. */
1307 bool in_declarator_p;
1309 /* TRUE if we are presently parsing a template-argument-list. */
1310 bool in_template_argument_list_p;
1312 /* TRUE if we are presently parsing the body of an
1313 iteration-statement. */
1314 bool in_iteration_statement_p;
1316 /* TRUE if we are presently parsing the body of a switch
1318 bool in_switch_statement_p;
1320 /* TRUE if we are parsing a type-id in an expression context. In
1321 such a situation, both "type (expr)" and "type (type)" are valid
1323 bool in_type_id_in_expr_p;
1325 /* TRUE if we are currently in a header file where declarations are
1326 implicitly extern "C". */
1327 bool implicit_extern_c;
1329 /* TRUE if strings in expressions should be translated to the execution
1331 bool translate_strings_p;
1333 /* If non-NULL, then we are parsing a construct where new type
1334 definitions are not permitted. The string stored here will be
1335 issued as an error message if a type is defined. */
1336 const char *type_definition_forbidden_message;
1338 /* A list of lists. The outer list is a stack, used for member
1339 functions of local classes. At each level there are two sub-list,
1340 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1341 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1342 TREE_VALUE's. The functions are chained in reverse declaration
1345 The TREE_PURPOSE sublist contains those functions with default
1346 arguments that need post processing, and the TREE_VALUE sublist
1347 contains those functions with definitions that need post
1350 These lists can only be processed once the outermost class being
1351 defined is complete. */
1352 tree unparsed_functions_queues;
1354 /* The number of classes whose definitions are currently in
1356 unsigned num_classes_being_defined;
1358 /* The number of template parameter lists that apply directly to the
1359 current declaration. */
1360 unsigned num_template_parameter_lists;
1363 /* The type of a function that parses some kind of expression. */
1364 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1368 /* Constructors and destructors. */
1370 static cp_parser *cp_parser_new
1373 /* Routines to parse various constructs.
1375 Those that return `tree' will return the error_mark_node (rather
1376 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1377 Sometimes, they will return an ordinary node if error-recovery was
1378 attempted, even though a parse error occurred. So, to check
1379 whether or not a parse error occurred, you should always use
1380 cp_parser_error_occurred. If the construct is optional (indicated
1381 either by an `_opt' in the name of the function that does the
1382 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1383 the construct is not present. */
1385 /* Lexical conventions [gram.lex] */
1387 static tree cp_parser_identifier
1389 static tree cp_parser_string_literal
1390 (cp_parser *, bool, bool);
1392 /* Basic concepts [gram.basic] */
1394 static bool cp_parser_translation_unit
1397 /* Expressions [gram.expr] */
1399 static tree cp_parser_primary_expression
1400 (cp_parser *, bool, bool, bool, cp_id_kind *);
1401 static tree cp_parser_id_expression
1402 (cp_parser *, bool, bool, bool *, bool);
1403 static tree cp_parser_unqualified_id
1404 (cp_parser *, bool, bool, bool);
1405 static tree cp_parser_nested_name_specifier_opt
1406 (cp_parser *, bool, bool, bool, bool);
1407 static tree cp_parser_nested_name_specifier
1408 (cp_parser *, bool, bool, bool, bool);
1409 static tree cp_parser_class_or_namespace_name
1410 (cp_parser *, bool, bool, bool, bool, bool);
1411 static tree cp_parser_postfix_expression
1412 (cp_parser *, bool, bool);
1413 static tree cp_parser_postfix_open_square_expression
1414 (cp_parser *, tree, bool);
1415 static tree cp_parser_postfix_dot_deref_expression
1416 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1417 static tree cp_parser_parenthesized_expression_list
1418 (cp_parser *, bool, bool, bool *);
1419 static void cp_parser_pseudo_destructor_name
1420 (cp_parser *, tree *, tree *);
1421 static tree cp_parser_unary_expression
1422 (cp_parser *, bool, bool);
1423 static enum tree_code cp_parser_unary_operator
1425 static tree cp_parser_new_expression
1427 static tree cp_parser_new_placement
1429 static tree cp_parser_new_type_id
1430 (cp_parser *, tree *);
1431 static cp_declarator *cp_parser_new_declarator_opt
1433 static cp_declarator *cp_parser_direct_new_declarator
1435 static tree cp_parser_new_initializer
1437 static tree cp_parser_delete_expression
1439 static tree cp_parser_cast_expression
1440 (cp_parser *, bool, bool);
1441 static tree cp_parser_binary_expression
1442 (cp_parser *, bool);
1443 static tree cp_parser_question_colon_clause
1444 (cp_parser *, tree);
1445 static tree cp_parser_assignment_expression
1446 (cp_parser *, bool);
1447 static enum tree_code cp_parser_assignment_operator_opt
1449 static tree cp_parser_expression
1450 (cp_parser *, bool);
1451 static tree cp_parser_constant_expression
1452 (cp_parser *, bool, bool *);
1453 static tree cp_parser_builtin_offsetof
1456 /* Statements [gram.stmt.stmt] */
1458 static void cp_parser_statement
1459 (cp_parser *, tree);
1460 static tree cp_parser_labeled_statement
1461 (cp_parser *, tree);
1462 static tree cp_parser_expression_statement
1463 (cp_parser *, tree);
1464 static tree cp_parser_compound_statement
1465 (cp_parser *, tree, bool);
1466 static void cp_parser_statement_seq_opt
1467 (cp_parser *, tree);
1468 static tree cp_parser_selection_statement
1470 static tree cp_parser_condition
1472 static tree cp_parser_iteration_statement
1474 static void cp_parser_for_init_statement
1476 static tree cp_parser_jump_statement
1478 static void cp_parser_declaration_statement
1481 static tree cp_parser_implicitly_scoped_statement
1483 static void cp_parser_already_scoped_statement
1486 /* Declarations [gram.dcl.dcl] */
1488 static void cp_parser_declaration_seq_opt
1490 static void cp_parser_declaration
1492 static void cp_parser_block_declaration
1493 (cp_parser *, bool);
1494 static void cp_parser_simple_declaration
1495 (cp_parser *, bool);
1496 static void cp_parser_decl_specifier_seq
1497 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1498 static tree cp_parser_storage_class_specifier_opt
1500 static tree cp_parser_function_specifier_opt
1501 (cp_parser *, cp_decl_specifier_seq *);
1502 static tree cp_parser_type_specifier
1503 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1505 static tree cp_parser_simple_type_specifier
1506 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1507 static tree cp_parser_type_name
1509 static tree cp_parser_elaborated_type_specifier
1510 (cp_parser *, bool, bool);
1511 static tree cp_parser_enum_specifier
1513 static void cp_parser_enumerator_list
1514 (cp_parser *, tree);
1515 static void cp_parser_enumerator_definition
1516 (cp_parser *, tree);
1517 static tree cp_parser_namespace_name
1519 static void cp_parser_namespace_definition
1521 static void cp_parser_namespace_body
1523 static tree cp_parser_qualified_namespace_specifier
1525 static void cp_parser_namespace_alias_definition
1527 static void cp_parser_using_declaration
1529 static void cp_parser_using_directive
1531 static void cp_parser_asm_definition
1533 static void cp_parser_linkage_specification
1536 /* Declarators [gram.dcl.decl] */
1538 static tree cp_parser_init_declarator
1539 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1540 static cp_declarator *cp_parser_declarator
1541 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1542 static cp_declarator *cp_parser_direct_declarator
1543 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1544 static enum tree_code cp_parser_ptr_operator
1545 (cp_parser *, tree *, cp_cv_quals *);
1546 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1548 static tree cp_parser_declarator_id
1550 static tree cp_parser_type_id
1552 static void cp_parser_type_specifier_seq
1553 (cp_parser *, bool, cp_decl_specifier_seq *);
1554 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1556 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1557 (cp_parser *, bool *);
1558 static cp_parameter_declarator *cp_parser_parameter_declaration
1559 (cp_parser *, bool, bool *);
1560 static void cp_parser_function_body
1562 static tree cp_parser_initializer
1563 (cp_parser *, bool *, bool *);
1564 static tree cp_parser_initializer_clause
1565 (cp_parser *, bool *);
1566 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1567 (cp_parser *, bool *);
1569 static bool cp_parser_ctor_initializer_opt_and_function_body
1572 /* Classes [gram.class] */
1574 static tree cp_parser_class_name
1575 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1576 static tree cp_parser_class_specifier
1578 static tree cp_parser_class_head
1579 (cp_parser *, bool *, tree *);
1580 static enum tag_types cp_parser_class_key
1582 static void cp_parser_member_specification_opt
1584 static void cp_parser_member_declaration
1586 static tree cp_parser_pure_specifier
1588 static tree cp_parser_constant_initializer
1591 /* Derived classes [gram.class.derived] */
1593 static tree cp_parser_base_clause
1595 static tree cp_parser_base_specifier
1598 /* Special member functions [gram.special] */
1600 static tree cp_parser_conversion_function_id
1602 static tree cp_parser_conversion_type_id
1604 static cp_declarator *cp_parser_conversion_declarator_opt
1606 static bool cp_parser_ctor_initializer_opt
1608 static void cp_parser_mem_initializer_list
1610 static tree cp_parser_mem_initializer
1612 static tree cp_parser_mem_initializer_id
1615 /* Overloading [gram.over] */
1617 static tree cp_parser_operator_function_id
1619 static tree cp_parser_operator
1622 /* Templates [gram.temp] */
1624 static void cp_parser_template_declaration
1625 (cp_parser *, bool);
1626 static tree cp_parser_template_parameter_list
1628 static tree cp_parser_template_parameter
1629 (cp_parser *, bool *);
1630 static tree cp_parser_type_parameter
1632 static tree cp_parser_template_id
1633 (cp_parser *, bool, bool, bool);
1634 static tree cp_parser_template_name
1635 (cp_parser *, bool, bool, bool, bool *);
1636 static tree cp_parser_template_argument_list
1638 static tree cp_parser_template_argument
1640 static void cp_parser_explicit_instantiation
1642 static void cp_parser_explicit_specialization
1645 /* Exception handling [gram.exception] */
1647 static tree cp_parser_try_block
1649 static bool cp_parser_function_try_block
1651 static void cp_parser_handler_seq
1653 static void cp_parser_handler
1655 static tree cp_parser_exception_declaration
1657 static tree cp_parser_throw_expression
1659 static tree cp_parser_exception_specification_opt
1661 static tree cp_parser_type_id_list
1664 /* GNU Extensions */
1666 static tree cp_parser_asm_specification_opt
1668 static tree cp_parser_asm_operand_list
1670 static tree cp_parser_asm_clobber_list
1672 static tree cp_parser_attributes_opt
1674 static tree cp_parser_attribute_list
1676 static bool cp_parser_extension_opt
1677 (cp_parser *, int *);
1678 static void cp_parser_label_declaration
1681 /* Objective-C++ Productions */
1683 static tree cp_parser_objc_message_receiver
1685 static tree cp_parser_objc_message_args
1687 static tree cp_parser_objc_message_expression
1689 static tree cp_parser_objc_encode_expression
1691 static tree cp_parser_objc_defs_expression
1693 static tree cp_parser_objc_protocol_expression
1695 static tree cp_parser_objc_selector_expression
1697 static tree cp_parser_objc_expression
1699 static bool cp_parser_objc_selector_p
1701 static tree cp_parser_objc_selector
1703 static tree cp_parser_objc_protocol_refs_opt
1705 static void cp_parser_objc_declaration
1707 static tree cp_parser_objc_statement
1710 /* Utility Routines */
1712 static tree cp_parser_lookup_name
1713 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1714 static tree cp_parser_lookup_name_simple
1715 (cp_parser *, tree);
1716 static tree cp_parser_maybe_treat_template_as_class
1718 static bool cp_parser_check_declarator_template_parameters
1719 (cp_parser *, cp_declarator *);
1720 static bool cp_parser_check_template_parameters
1721 (cp_parser *, unsigned);
1722 static tree cp_parser_simple_cast_expression
1724 static tree cp_parser_global_scope_opt
1725 (cp_parser *, bool);
1726 static bool cp_parser_constructor_declarator_p
1727 (cp_parser *, bool);
1728 static tree cp_parser_function_definition_from_specifiers_and_declarator
1729 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1730 static tree cp_parser_function_definition_after_declarator
1731 (cp_parser *, bool);
1732 static void cp_parser_template_declaration_after_export
1733 (cp_parser *, bool);
1734 static tree cp_parser_single_declaration
1735 (cp_parser *, bool, bool *);
1736 static tree cp_parser_functional_cast
1737 (cp_parser *, tree);
1738 static tree cp_parser_save_member_function_body
1739 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1740 static tree cp_parser_enclosed_template_argument_list
1742 static void cp_parser_save_default_args
1743 (cp_parser *, tree);
1744 static void cp_parser_late_parsing_for_member
1745 (cp_parser *, tree);
1746 static void cp_parser_late_parsing_default_args
1747 (cp_parser *, tree);
1748 static tree cp_parser_sizeof_operand
1749 (cp_parser *, enum rid);
1750 static bool cp_parser_declares_only_class_p
1752 static void cp_parser_set_storage_class
1753 (cp_decl_specifier_seq *, cp_storage_class);
1754 static void cp_parser_set_decl_spec_type
1755 (cp_decl_specifier_seq *, tree, bool);
1756 static bool cp_parser_friend_p
1757 (const cp_decl_specifier_seq *);
1758 static cp_token *cp_parser_require
1759 (cp_parser *, enum cpp_ttype, const char *);
1760 static cp_token *cp_parser_require_keyword
1761 (cp_parser *, enum rid, const char *);
1762 static bool cp_parser_token_starts_function_definition_p
1764 static bool cp_parser_next_token_starts_class_definition_p
1766 static bool cp_parser_next_token_ends_template_argument_p
1768 static bool cp_parser_nth_token_starts_template_argument_list_p
1769 (cp_parser *, size_t);
1770 static enum tag_types cp_parser_token_is_class_key
1772 static void cp_parser_check_class_key
1773 (enum tag_types, tree type);
1774 static void cp_parser_check_access_in_redeclaration
1776 static bool cp_parser_optional_template_keyword
1778 static void cp_parser_pre_parsed_nested_name_specifier
1780 static void cp_parser_cache_group
1781 (cp_parser *, enum cpp_ttype, unsigned);
1782 static void cp_parser_parse_tentatively
1784 static void cp_parser_commit_to_tentative_parse
1786 static void cp_parser_abort_tentative_parse
1788 static bool cp_parser_parse_definitely
1790 static inline bool cp_parser_parsing_tentatively
1792 static bool cp_parser_uncommitted_to_tentative_parse_p
1794 static void cp_parser_error
1795 (cp_parser *, const char *);
1796 static void cp_parser_name_lookup_error
1797 (cp_parser *, tree, tree, const char *);
1798 static bool cp_parser_simulate_error
1800 static void cp_parser_check_type_definition
1802 static void cp_parser_check_for_definition_in_return_type
1803 (cp_declarator *, tree);
1804 static void cp_parser_check_for_invalid_template_id
1805 (cp_parser *, tree);
1806 static bool cp_parser_non_integral_constant_expression
1807 (cp_parser *, const char *);
1808 static void cp_parser_diagnose_invalid_type_name
1809 (cp_parser *, tree, tree);
1810 static bool cp_parser_parse_and_diagnose_invalid_type_name
1812 static int cp_parser_skip_to_closing_parenthesis
1813 (cp_parser *, bool, bool, bool);
1814 static void cp_parser_skip_to_end_of_statement
1816 static void cp_parser_consume_semicolon_at_end_of_statement
1818 static void cp_parser_skip_to_end_of_block_or_statement
1820 static void cp_parser_skip_to_closing_brace
1822 static void cp_parser_skip_until_found
1823 (cp_parser *, enum cpp_ttype, const char *);
1824 static bool cp_parser_error_occurred
1826 static bool cp_parser_allow_gnu_extensions_p
1828 static bool cp_parser_is_string_literal
1830 static bool cp_parser_is_keyword
1831 (cp_token *, enum rid);
1832 static tree cp_parser_make_typename_type
1833 (cp_parser *, tree, tree);
1835 /* Returns nonzero if we are parsing tentatively. */
1838 cp_parser_parsing_tentatively (cp_parser* parser)
1840 return parser->context->next != NULL;
1843 /* Returns nonzero if TOKEN is a string literal. */
1846 cp_parser_is_string_literal (cp_token* token)
1848 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1851 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1854 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1856 return token->keyword == keyword;
1859 /* A minimum or maximum operator has been seen. As these are
1860 deprecated, issue a warning. */
1863 cp_parser_warn_min_max (void)
1865 if (warn_deprecated && !in_system_header)
1866 warning (0, "minimum/maximum operators are deprecated");
1869 /* If not parsing tentatively, issue a diagnostic of the form
1870 FILE:LINE: MESSAGE before TOKEN
1871 where TOKEN is the next token in the input stream. MESSAGE
1872 (specified by the caller) is usually of the form "expected
1876 cp_parser_error (cp_parser* parser, const char* message)
1878 if (!cp_parser_simulate_error (parser))
1880 cp_token *token = cp_lexer_peek_token (parser->lexer);
1881 /* This diagnostic makes more sense if it is tagged to the line
1882 of the token we just peeked at. */
1883 cp_lexer_set_source_position_from_token (token);
1884 if (token->type == CPP_PRAGMA)
1886 error ("%<#pragma%> is not allowed here");
1887 cp_lexer_purge_token (parser->lexer);
1890 c_parse_error (message,
1891 /* Because c_parser_error does not understand
1892 CPP_KEYWORD, keywords are treated like
1894 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1899 /* Issue an error about name-lookup failing. NAME is the
1900 IDENTIFIER_NODE DECL is the result of
1901 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1902 the thing that we hoped to find. */
1905 cp_parser_name_lookup_error (cp_parser* parser,
1908 const char* desired)
1910 /* If name lookup completely failed, tell the user that NAME was not
1912 if (decl == error_mark_node)
1914 if (parser->scope && parser->scope != global_namespace)
1915 error ("%<%D::%D%> has not been declared",
1916 parser->scope, name);
1917 else if (parser->scope == global_namespace)
1918 error ("%<::%D%> has not been declared", name);
1919 else if (parser->object_scope
1920 && !CLASS_TYPE_P (parser->object_scope))
1921 error ("request for member %qD in non-class type %qT",
1922 name, parser->object_scope);
1923 else if (parser->object_scope)
1924 error ("%<%T::%D%> has not been declared",
1925 parser->object_scope, name);
1927 error ("%qD has not been declared", name);
1929 else if (parser->scope && parser->scope != global_namespace)
1930 error ("%<%D::%D%> %s", parser->scope, name, desired);
1931 else if (parser->scope == global_namespace)
1932 error ("%<::%D%> %s", name, desired);
1934 error ("%qD %s", name, desired);
1937 /* If we are parsing tentatively, remember that an error has occurred
1938 during this tentative parse. Returns true if the error was
1939 simulated; false if a message should be issued by the caller. */
1942 cp_parser_simulate_error (cp_parser* parser)
1944 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1946 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1952 /* This function is called when a type is defined. If type
1953 definitions are forbidden at this point, an error message is
1957 cp_parser_check_type_definition (cp_parser* parser)
1959 /* If types are forbidden here, issue a message. */
1960 if (parser->type_definition_forbidden_message)
1961 /* Use `%s' to print the string in case there are any escape
1962 characters in the message. */
1963 error ("%s", parser->type_definition_forbidden_message);
1966 /* This function is called when the DECLARATOR is processed. The TYPE
1967 was a type defined in the decl-specifiers. If it is invalid to
1968 define a type in the decl-specifiers for DECLARATOR, an error is
1972 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1975 /* [dcl.fct] forbids type definitions in return types.
1976 Unfortunately, it's not easy to know whether or not we are
1977 processing a return type until after the fact. */
1979 && (declarator->kind == cdk_pointer
1980 || declarator->kind == cdk_reference
1981 || declarator->kind == cdk_ptrmem))
1982 declarator = declarator->declarator;
1984 && declarator->kind == cdk_function)
1986 error ("new types may not be defined in a return type");
1987 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1992 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1993 "<" in any valid C++ program. If the next token is indeed "<",
1994 issue a message warning the user about what appears to be an
1995 invalid attempt to form a template-id. */
1998 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2001 cp_token_position start = 0;
2003 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2006 error ("%qT is not a template", type);
2007 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2008 error ("%qE is not a template", type);
2010 error ("invalid template-id");
2011 /* Remember the location of the invalid "<". */
2012 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2013 start = cp_lexer_token_position (parser->lexer, true);
2014 /* Consume the "<". */
2015 cp_lexer_consume_token (parser->lexer);
2016 /* Parse the template arguments. */
2017 cp_parser_enclosed_template_argument_list (parser);
2018 /* Permanently remove the invalid template arguments so that
2019 this error message is not issued again. */
2021 cp_lexer_purge_tokens_after (parser->lexer, start);
2025 /* If parsing an integral constant-expression, issue an error message
2026 about the fact that THING appeared and return true. Otherwise,
2027 return false. In either case, set
2028 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2031 cp_parser_non_integral_constant_expression (cp_parser *parser,
2034 parser->non_integral_constant_expression_p = true;
2035 if (parser->integral_constant_expression_p)
2037 if (!parser->allow_non_integral_constant_expression_p)
2039 error ("%s cannot appear in a constant-expression", thing);
2046 /* Emit a diagnostic for an invalid type name. SCOPE is the
2047 qualifying scope (or NULL, if none) for ID. This function commits
2048 to the current active tentative parse, if any. (Otherwise, the
2049 problematic construct might be encountered again later, resulting
2050 in duplicate error messages.) */
2053 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2055 tree decl, old_scope;
2056 /* Try to lookup the identifier. */
2057 old_scope = parser->scope;
2058 parser->scope = scope;
2059 decl = cp_parser_lookup_name_simple (parser, id);
2060 parser->scope = old_scope;
2061 /* If the lookup found a template-name, it means that the user forgot
2062 to specify an argument list. Emit a useful error message. */
2063 if (TREE_CODE (decl) == TEMPLATE_DECL)
2064 error ("invalid use of template-name %qE without an argument list",
2066 else if (!parser->scope)
2068 /* Issue an error message. */
2069 error ("%qE does not name a type", id);
2070 /* If we're in a template class, it's possible that the user was
2071 referring to a type from a base class. For example:
2073 template <typename T> struct A { typedef T X; };
2074 template <typename T> struct B : public A<T> { X x; };
2076 The user should have said "typename A<T>::X". */
2077 if (processing_template_decl && current_class_type
2078 && TYPE_BINFO (current_class_type))
2082 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2086 tree base_type = BINFO_TYPE (b);
2087 if (CLASS_TYPE_P (base_type)
2088 && dependent_type_p (base_type))
2091 /* Go from a particular instantiation of the
2092 template (which will have an empty TYPE_FIELDs),
2093 to the main version. */
2094 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2095 for (field = TYPE_FIELDS (base_type);
2097 field = TREE_CHAIN (field))
2098 if (TREE_CODE (field) == TYPE_DECL
2099 && DECL_NAME (field) == id)
2101 inform ("(perhaps %<typename %T::%E%> was intended)",
2102 BINFO_TYPE (b), id);
2111 /* Here we diagnose qualified-ids where the scope is actually correct,
2112 but the identifier does not resolve to a valid type name. */
2113 else if (parser->scope != error_mark_node)
2115 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2116 error ("%qE in namespace %qE does not name a type",
2118 else if (TYPE_P (parser->scope))
2119 error ("%qE in class %qT does not name a type", id, parser->scope);
2123 cp_parser_commit_to_tentative_parse (parser);
2126 /* Check for a common situation where a type-name should be present,
2127 but is not, and issue a sensible error message. Returns true if an
2128 invalid type-name was detected.
2130 The situation handled by this function are variable declarations of the
2131 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2132 Usually, `ID' should name a type, but if we got here it means that it
2133 does not. We try to emit the best possible error message depending on
2134 how exactly the id-expression looks like.
2138 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2142 cp_parser_parse_tentatively (parser);
2143 id = cp_parser_id_expression (parser,
2144 /*template_keyword_p=*/false,
2145 /*check_dependency_p=*/true,
2146 /*template_p=*/NULL,
2147 /*declarator_p=*/true);
2148 /* After the id-expression, there should be a plain identifier,
2149 otherwise this is not a simple variable declaration. Also, if
2150 the scope is dependent, we cannot do much. */
2151 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2152 || (parser->scope && TYPE_P (parser->scope)
2153 && dependent_type_p (parser->scope)))
2155 cp_parser_abort_tentative_parse (parser);
2158 if (!cp_parser_parse_definitely (parser)
2159 || TREE_CODE (id) != IDENTIFIER_NODE)
2162 /* Emit a diagnostic for the invalid type. */
2163 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2164 /* Skip to the end of the declaration; there's no point in
2165 trying to process it. */
2166 cp_parser_skip_to_end_of_block_or_statement (parser);
2170 /* Consume tokens up to, and including, the next non-nested closing `)'.
2171 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2172 are doing error recovery. Returns -1 if OR_COMMA is true and we
2173 found an unnested comma. */
2176 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2181 unsigned paren_depth = 0;
2182 unsigned brace_depth = 0;
2185 if (recovering && !or_comma
2186 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2193 /* If we've run out of tokens, then there is no closing `)'. */
2194 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2200 token = cp_lexer_peek_token (parser->lexer);
2202 /* This matches the processing in skip_to_end_of_statement. */
2203 if (token->type == CPP_SEMICOLON && !brace_depth)
2208 if (token->type == CPP_OPEN_BRACE)
2210 if (token->type == CPP_CLOSE_BRACE)
2218 if (recovering && or_comma && token->type == CPP_COMMA
2219 && !brace_depth && !paren_depth)
2227 /* If it is an `(', we have entered another level of nesting. */
2228 if (token->type == CPP_OPEN_PAREN)
2230 /* If it is a `)', then we might be done. */
2231 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2234 cp_lexer_consume_token (parser->lexer);
2242 /* Consume the token. */
2243 cp_lexer_consume_token (parser->lexer);
2249 /* Consume tokens until we reach the end of the current statement.
2250 Normally, that will be just before consuming a `;'. However, if a
2251 non-nested `}' comes first, then we stop before consuming that. */
2254 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2256 unsigned nesting_depth = 0;
2262 /* Peek at the next token. */
2263 token = cp_lexer_peek_token (parser->lexer);
2264 /* If we've run out of tokens, stop. */
2265 if (token->type == CPP_EOF)
2267 /* If the next token is a `;', we have reached the end of the
2269 if (token->type == CPP_SEMICOLON && !nesting_depth)
2271 /* If the next token is a non-nested `}', then we have reached
2272 the end of the current block. */
2273 if (token->type == CPP_CLOSE_BRACE)
2275 /* If this is a non-nested `}', stop before consuming it.
2276 That way, when confronted with something like:
2280 we stop before consuming the closing `}', even though we
2281 have not yet reached a `;'. */
2282 if (nesting_depth == 0)
2284 /* If it is the closing `}' for a block that we have
2285 scanned, stop -- but only after consuming the token.
2291 we will stop after the body of the erroneously declared
2292 function, but before consuming the following `typedef'
2294 if (--nesting_depth == 0)
2296 cp_lexer_consume_token (parser->lexer);
2300 /* If it the next token is a `{', then we are entering a new
2301 block. Consume the entire block. */
2302 else if (token->type == CPP_OPEN_BRACE)
2304 /* Consume the token. */
2305 cp_lexer_consume_token (parser->lexer);
2309 /* This function is called at the end of a statement or declaration.
2310 If the next token is a semicolon, it is consumed; otherwise, error
2311 recovery is attempted. */
2314 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2316 /* Look for the trailing `;'. */
2317 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2319 /* If there is additional (erroneous) input, skip to the end of
2321 cp_parser_skip_to_end_of_statement (parser);
2322 /* If the next token is now a `;', consume it. */
2323 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2324 cp_lexer_consume_token (parser->lexer);
2328 /* Skip tokens until we have consumed an entire block, or until we
2329 have consumed a non-nested `;'. */
2332 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2334 int nesting_depth = 0;
2336 while (nesting_depth >= 0)
2338 cp_token *token = cp_lexer_peek_token (parser->lexer);
2340 if (token->type == CPP_EOF)
2343 switch (token->type)
2346 /* If we've run out of tokens, stop. */
2351 /* Stop if this is an unnested ';'. */
2356 case CPP_CLOSE_BRACE:
2357 /* Stop if this is an unnested '}', or closes the outermost
2364 case CPP_OPEN_BRACE:
2373 /* Consume the token. */
2374 cp_lexer_consume_token (parser->lexer);
2379 /* Skip tokens until a non-nested closing curly brace is the next
2383 cp_parser_skip_to_closing_brace (cp_parser *parser)
2385 unsigned nesting_depth = 0;
2391 /* Peek at the next token. */
2392 token = cp_lexer_peek_token (parser->lexer);
2393 /* If we've run out of tokens, stop. */
2394 if (token->type == CPP_EOF)
2396 /* If the next token is a non-nested `}', then we have reached
2397 the end of the current block. */
2398 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2400 /* If it the next token is a `{', then we are entering a new
2401 block. Consume the entire block. */
2402 else if (token->type == CPP_OPEN_BRACE)
2404 /* Consume the token. */
2405 cp_lexer_consume_token (parser->lexer);
2409 /* This is a simple wrapper around make_typename_type. When the id is
2410 an unresolved identifier node, we can provide a superior diagnostic
2411 using cp_parser_diagnose_invalid_type_name. */
2414 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2417 if (TREE_CODE (id) == IDENTIFIER_NODE)
2419 result = make_typename_type (scope, id, typename_type,
2420 /*complain=*/tf_none);
2421 if (result == error_mark_node)
2422 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2425 return make_typename_type (scope, id, typename_type, tf_error);
2429 /* Create a new C++ parser. */
2432 cp_parser_new (void)
2438 /* cp_lexer_new_main is called before calling ggc_alloc because
2439 cp_lexer_new_main might load a PCH file. */
2440 lexer = cp_lexer_new_main ();
2442 /* Initialize the binops_by_token so that we can get the tree
2443 directly from the token. */
2444 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2445 binops_by_token[binops[i].token_type] = binops[i];
2447 parser = GGC_CNEW (cp_parser);
2448 parser->lexer = lexer;
2449 parser->context = cp_parser_context_new (NULL);
2451 /* For now, we always accept GNU extensions. */
2452 parser->allow_gnu_extensions_p = 1;
2454 /* The `>' token is a greater-than operator, not the end of a
2456 parser->greater_than_is_operator_p = true;
2458 parser->default_arg_ok_p = true;
2460 /* We are not parsing a constant-expression. */
2461 parser->integral_constant_expression_p = false;
2462 parser->allow_non_integral_constant_expression_p = false;
2463 parser->non_integral_constant_expression_p = false;
2465 /* Local variable names are not forbidden. */
2466 parser->local_variables_forbidden_p = false;
2468 /* We are not processing an `extern "C"' declaration. */
2469 parser->in_unbraced_linkage_specification_p = false;
2471 /* We are not processing a declarator. */
2472 parser->in_declarator_p = false;
2474 /* We are not processing a template-argument-list. */
2475 parser->in_template_argument_list_p = false;
2477 /* We are not in an iteration statement. */
2478 parser->in_iteration_statement_p = false;
2480 /* We are not in a switch statement. */
2481 parser->in_switch_statement_p = false;
2483 /* We are not parsing a type-id inside an expression. */
2484 parser->in_type_id_in_expr_p = false;
2486 /* Declarations aren't implicitly extern "C". */
2487 parser->implicit_extern_c = false;
2489 /* String literals should be translated to the execution character set. */
2490 parser->translate_strings_p = true;
2492 /* The unparsed function queue is empty. */
2493 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2495 /* There are no classes being defined. */
2496 parser->num_classes_being_defined = 0;
2498 /* No template parameters apply. */
2499 parser->num_template_parameter_lists = 0;
2504 /* Create a cp_lexer structure which will emit the tokens in CACHE
2505 and push it onto the parser's lexer stack. This is used for delayed
2506 parsing of in-class method bodies and default arguments, and should
2507 not be confused with tentative parsing. */
2509 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2511 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2512 lexer->next = parser->lexer;
2513 parser->lexer = lexer;
2515 /* Move the current source position to that of the first token in the
2517 cp_lexer_set_source_position_from_token (lexer->next_token);
2520 /* Pop the top lexer off the parser stack. This is never used for the
2521 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2523 cp_parser_pop_lexer (cp_parser *parser)
2525 cp_lexer *lexer = parser->lexer;
2526 parser->lexer = lexer->next;
2527 cp_lexer_destroy (lexer);
2529 /* Put the current source position back where it was before this
2530 lexer was pushed. */
2531 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2534 /* Lexical conventions [gram.lex] */
2536 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2540 cp_parser_identifier (cp_parser* parser)
2544 /* Look for the identifier. */
2545 token = cp_parser_require (parser, CPP_NAME, "identifier");
2546 /* Return the value. */
2547 return token ? token->value : error_mark_node;
2550 /* Parse a sequence of adjacent string constants. Returns a
2551 TREE_STRING representing the combined, nul-terminated string
2552 constant. If TRANSLATE is true, translate the string to the
2553 execution character set. If WIDE_OK is true, a wide string is
2556 C++98 [lex.string] says that if a narrow string literal token is
2557 adjacent to a wide string literal token, the behavior is undefined.
2558 However, C99 6.4.5p4 says that this results in a wide string literal.
2559 We follow C99 here, for consistency with the C front end.
2561 This code is largely lifted from lex_string() in c-lex.c.
2563 FUTURE: ObjC++ will need to handle @-strings here. */
2565 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2570 struct obstack str_ob;
2571 cpp_string str, istr, *strs;
2574 tok = cp_lexer_peek_token (parser->lexer);
2575 if (!cp_parser_is_string_literal (tok))
2577 cp_parser_error (parser, "expected string-literal");
2578 return error_mark_node;
2581 /* Try to avoid the overhead of creating and destroying an obstack
2582 for the common case of just one string. */
2583 if (!cp_parser_is_string_literal
2584 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2586 cp_lexer_consume_token (parser->lexer);
2588 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2589 str.len = TREE_STRING_LENGTH (tok->value);
2591 if (tok->type == CPP_WSTRING)
2598 gcc_obstack_init (&str_ob);
2603 cp_lexer_consume_token (parser->lexer);
2605 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2606 str.len = TREE_STRING_LENGTH (tok->value);
2607 if (tok->type == CPP_WSTRING)
2610 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2612 tok = cp_lexer_peek_token (parser->lexer);
2614 while (cp_parser_is_string_literal (tok));
2616 strs = (cpp_string *) obstack_finish (&str_ob);
2619 if (wide && !wide_ok)
2621 cp_parser_error (parser, "a wide string is invalid in this context");
2625 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2626 (parse_in, strs, count, &istr, wide))
2628 value = build_string (istr.len, (char *)istr.text);
2629 free ((void *)istr.text);
2631 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2632 value = fix_string_type (value);
2635 /* cpp_interpret_string has issued an error. */
2636 value = error_mark_node;
2639 obstack_free (&str_ob, 0);
2645 /* Basic concepts [gram.basic] */
2647 /* Parse a translation-unit.
2650 declaration-seq [opt]
2652 Returns TRUE if all went well. */
2655 cp_parser_translation_unit (cp_parser* parser)
2657 /* The address of the first non-permanent object on the declarator
2659 static void *declarator_obstack_base;
2663 /* Create the declarator obstack, if necessary. */
2664 if (!cp_error_declarator)
2666 gcc_obstack_init (&declarator_obstack);
2667 /* Create the error declarator. */
2668 cp_error_declarator = make_declarator (cdk_error);
2669 /* Create the empty parameter list. */
2670 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2671 /* Remember where the base of the declarator obstack lies. */
2672 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2675 cp_parser_declaration_seq_opt (parser);
2677 /* If there are no tokens left then all went well. */
2678 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2680 /* Get rid of the token array; we don't need it any more. */
2681 cp_lexer_destroy (parser->lexer);
2682 parser->lexer = NULL;
2684 /* This file might have been a context that's implicitly extern
2685 "C". If so, pop the lang context. (Only relevant for PCH.) */
2686 if (parser->implicit_extern_c)
2688 pop_lang_context ();
2689 parser->implicit_extern_c = false;
2693 finish_translation_unit ();
2699 cp_parser_error (parser, "expected declaration");
2703 /* Make sure the declarator obstack was fully cleaned up. */
2704 gcc_assert (obstack_next_free (&declarator_obstack)
2705 == declarator_obstack_base);
2707 /* All went well. */
2711 /* Expressions [gram.expr] */
2713 /* Parse a primary-expression.
2724 ( compound-statement )
2725 __builtin_va_arg ( assignment-expression , type-id )
2727 Objective-C++ Extension:
2735 ADDRESS_P is true iff this expression was immediately preceded by
2736 "&" and therefore might denote a pointer-to-member. CAST_P is true
2737 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2738 true iff this expression is a template argument.
2740 Returns a representation of the expression. Upon return, *IDK
2741 indicates what kind of id-expression (if any) was present. */
2744 cp_parser_primary_expression (cp_parser *parser,
2747 bool template_arg_p,
2752 /* Assume the primary expression is not an id-expression. */
2753 *idk = CP_ID_KIND_NONE;
2755 /* Peek at the next token. */
2756 token = cp_lexer_peek_token (parser->lexer);
2757 switch (token->type)
2768 token = cp_lexer_consume_token (parser->lexer);
2769 /* Floating-point literals are only allowed in an integral
2770 constant expression if they are cast to an integral or
2771 enumeration type. */
2772 if (TREE_CODE (token->value) == REAL_CST
2773 && parser->integral_constant_expression_p
2776 /* CAST_P will be set even in invalid code like "int(2.7 +
2777 ...)". Therefore, we have to check that the next token
2778 is sure to end the cast. */
2781 cp_token *next_token;
2783 next_token = cp_lexer_peek_token (parser->lexer);
2784 if (/* The comma at the end of an
2785 enumerator-definition. */
2786 next_token->type != CPP_COMMA
2787 /* The curly brace at the end of an enum-specifier. */
2788 && next_token->type != CPP_CLOSE_BRACE
2789 /* The end of a statement. */
2790 && next_token->type != CPP_SEMICOLON
2791 /* The end of the cast-expression. */
2792 && next_token->type != CPP_CLOSE_PAREN
2793 /* The end of an array bound. */
2794 && next_token->type != CPP_CLOSE_SQUARE
2795 /* The closing ">" in a template-argument-list. */
2796 && (next_token->type != CPP_GREATER
2797 || parser->greater_than_is_operator_p))
2801 /* If we are within a cast, then the constraint that the
2802 cast is to an integral or enumeration type will be
2803 checked at that point. If we are not within a cast, then
2804 this code is invalid. */
2806 cp_parser_non_integral_constant_expression
2807 (parser, "floating-point literal");
2809 return token->value;
2813 /* ??? Should wide strings be allowed when parser->translate_strings_p
2814 is false (i.e. in attributes)? If not, we can kill the third
2815 argument to cp_parser_string_literal. */
2816 return cp_parser_string_literal (parser,
2817 parser->translate_strings_p,
2820 case CPP_OPEN_PAREN:
2823 bool saved_greater_than_is_operator_p;
2825 /* Consume the `('. */
2826 cp_lexer_consume_token (parser->lexer);
2827 /* Within a parenthesized expression, a `>' token is always
2828 the greater-than operator. */
2829 saved_greater_than_is_operator_p
2830 = parser->greater_than_is_operator_p;
2831 parser->greater_than_is_operator_p = true;
2832 /* If we see `( { ' then we are looking at the beginning of
2833 a GNU statement-expression. */
2834 if (cp_parser_allow_gnu_extensions_p (parser)
2835 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2837 /* Statement-expressions are not allowed by the standard. */
2839 pedwarn ("ISO C++ forbids braced-groups within expressions");
2841 /* And they're not allowed outside of a function-body; you
2842 cannot, for example, write:
2844 int i = ({ int j = 3; j + 1; });
2846 at class or namespace scope. */
2847 if (!at_function_scope_p ())
2848 error ("statement-expressions are allowed only inside functions");
2849 /* Start the statement-expression. */
2850 expr = begin_stmt_expr ();
2851 /* Parse the compound-statement. */
2852 cp_parser_compound_statement (parser, expr, false);
2854 expr = finish_stmt_expr (expr, false);
2858 /* Parse the parenthesized expression. */
2859 expr = cp_parser_expression (parser, cast_p);
2860 /* Let the front end know that this expression was
2861 enclosed in parentheses. This matters in case, for
2862 example, the expression is of the form `A::B', since
2863 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2865 finish_parenthesized_expr (expr);
2867 /* The `>' token might be the end of a template-id or
2868 template-parameter-list now. */
2869 parser->greater_than_is_operator_p
2870 = saved_greater_than_is_operator_p;
2871 /* Consume the `)'. */
2872 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2873 cp_parser_skip_to_end_of_statement (parser);
2879 switch (token->keyword)
2881 /* These two are the boolean literals. */
2883 cp_lexer_consume_token (parser->lexer);
2884 return boolean_true_node;
2886 cp_lexer_consume_token (parser->lexer);
2887 return boolean_false_node;
2889 /* The `__null' literal. */
2891 cp_lexer_consume_token (parser->lexer);
2894 /* Recognize the `this' keyword. */
2896 cp_lexer_consume_token (parser->lexer);
2897 if (parser->local_variables_forbidden_p)
2899 error ("%<this%> may not be used in this context");
2900 return error_mark_node;
2902 /* Pointers cannot appear in constant-expressions. */
2903 if (cp_parser_non_integral_constant_expression (parser,
2905 return error_mark_node;
2906 return finish_this_expr ();
2908 /* The `operator' keyword can be the beginning of an
2913 case RID_FUNCTION_NAME:
2914 case RID_PRETTY_FUNCTION_NAME:
2915 case RID_C99_FUNCTION_NAME:
2916 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2917 __func__ are the names of variables -- but they are
2918 treated specially. Therefore, they are handled here,
2919 rather than relying on the generic id-expression logic
2920 below. Grammatically, these names are id-expressions.
2922 Consume the token. */
2923 token = cp_lexer_consume_token (parser->lexer);
2924 /* Look up the name. */
2925 return finish_fname (token->value);
2932 /* The `__builtin_va_arg' construct is used to handle
2933 `va_arg'. Consume the `__builtin_va_arg' token. */
2934 cp_lexer_consume_token (parser->lexer);
2935 /* Look for the opening `('. */
2936 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2937 /* Now, parse the assignment-expression. */
2938 expression = cp_parser_assignment_expression (parser,
2940 /* Look for the `,'. */
2941 cp_parser_require (parser, CPP_COMMA, "`,'");
2942 /* Parse the type-id. */
2943 type = cp_parser_type_id (parser);
2944 /* Look for the closing `)'. */
2945 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2946 /* Using `va_arg' in a constant-expression is not
2948 if (cp_parser_non_integral_constant_expression (parser,
2950 return error_mark_node;
2951 return build_x_va_arg (expression, type);
2955 return cp_parser_builtin_offsetof (parser);
2957 /* Objective-C++ expressions. */
2959 case RID_AT_PROTOCOL:
2960 case RID_AT_SELECTOR:
2961 return cp_parser_objc_expression (parser);
2964 cp_parser_error (parser, "expected primary-expression");
2965 return error_mark_node;
2968 /* An id-expression can start with either an identifier, a
2969 `::' as the beginning of a qualified-id, or the "operator"
2973 case CPP_TEMPLATE_ID:
2974 case CPP_NESTED_NAME_SPECIFIER:
2978 const char *error_msg;
2983 /* Parse the id-expression. */
2985 = cp_parser_id_expression (parser,
2986 /*template_keyword_p=*/false,
2987 /*check_dependency_p=*/true,
2989 /*declarator_p=*/false);
2990 if (id_expression == error_mark_node)
2991 return error_mark_node;
2992 token = cp_lexer_peek_token (parser->lexer);
2993 done = (token->type != CPP_OPEN_SQUARE
2994 && token->type != CPP_OPEN_PAREN
2995 && token->type != CPP_DOT
2996 && token->type != CPP_DEREF
2997 && token->type != CPP_PLUS_PLUS
2998 && token->type != CPP_MINUS_MINUS);
2999 /* If we have a template-id, then no further lookup is
3000 required. If the template-id was for a template-class, we
3001 will sometimes have a TYPE_DECL at this point. */
3002 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3003 || TREE_CODE (id_expression) == TYPE_DECL)
3004 decl = id_expression;
3005 /* Look up the name. */
3008 tree ambiguous_decls;
3010 decl = cp_parser_lookup_name (parser, id_expression,
3013 /*is_namespace=*/false,
3014 /*check_dependency=*/true,
3016 /* If the lookup was ambiguous, an error will already have
3018 if (ambiguous_decls)
3019 return error_mark_node;
3021 /* In Objective-C++, an instance variable (ivar) may be preferred
3022 to whatever cp_parser_lookup_name() found. */
3023 decl = objc_lookup_ivar (decl, id_expression);
3025 /* If name lookup gives us a SCOPE_REF, then the
3026 qualifying scope was dependent. */
3027 if (TREE_CODE (decl) == SCOPE_REF)
3029 /* Check to see if DECL is a local variable in a context
3030 where that is forbidden. */
3031 if (parser->local_variables_forbidden_p
3032 && local_variable_p (decl))
3034 /* It might be that we only found DECL because we are
3035 trying to be generous with pre-ISO scoping rules.
3036 For example, consider:
3040 for (int i = 0; i < 10; ++i) {}
3041 extern void f(int j = i);
3044 Here, name look up will originally find the out
3045 of scope `i'. We need to issue a warning message,
3046 but then use the global `i'. */
3047 decl = check_for_out_of_scope_variable (decl);
3048 if (local_variable_p (decl))
3050 error ("local variable %qD may not appear in this context",
3052 return error_mark_node;
3057 decl = (finish_id_expression
3058 (id_expression, decl, parser->scope,
3060 parser->integral_constant_expression_p,
3061 parser->allow_non_integral_constant_expression_p,
3062 &parser->non_integral_constant_expression_p,
3063 template_p, done, address_p,
3067 cp_parser_error (parser, error_msg);
3071 /* Anything else is an error. */
3073 /* ...unless we have an Objective-C++ message or string literal, that is. */
3074 if (c_dialect_objc ()
3075 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3076 return cp_parser_objc_expression (parser);
3078 cp_parser_error (parser, "expected primary-expression");
3079 return error_mark_node;
3083 /* Parse an id-expression.
3090 :: [opt] nested-name-specifier template [opt] unqualified-id
3092 :: operator-function-id
3095 Return a representation of the unqualified portion of the
3096 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3097 a `::' or nested-name-specifier.
3099 Often, if the id-expression was a qualified-id, the caller will
3100 want to make a SCOPE_REF to represent the qualified-id. This
3101 function does not do this in order to avoid wastefully creating
3102 SCOPE_REFs when they are not required.
3104 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3107 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3108 uninstantiated templates.
3110 If *TEMPLATE_P is non-NULL, it is set to true iff the
3111 `template' keyword is used to explicitly indicate that the entity
3112 named is a template.
3114 If DECLARATOR_P is true, the id-expression is appearing as part of
3115 a declarator, rather than as part of an expression. */
3118 cp_parser_id_expression (cp_parser *parser,
3119 bool template_keyword_p,
3120 bool check_dependency_p,
3124 bool global_scope_p;
3125 bool nested_name_specifier_p;
3127 /* Assume the `template' keyword was not used. */
3129 *template_p = template_keyword_p;
3131 /* Look for the optional `::' operator. */
3133 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3135 /* Look for the optional nested-name-specifier. */
3136 nested_name_specifier_p
3137 = (cp_parser_nested_name_specifier_opt (parser,
3138 /*typename_keyword_p=*/false,
3143 /* If there is a nested-name-specifier, then we are looking at
3144 the first qualified-id production. */
3145 if (nested_name_specifier_p)
3148 tree saved_object_scope;
3149 tree saved_qualifying_scope;
3150 tree unqualified_id;
3153 /* See if the next token is the `template' keyword. */
3155 template_p = &is_template;
3156 *template_p = cp_parser_optional_template_keyword (parser);
3157 /* Name lookup we do during the processing of the
3158 unqualified-id might obliterate SCOPE. */
3159 saved_scope = parser->scope;
3160 saved_object_scope = parser->object_scope;
3161 saved_qualifying_scope = parser->qualifying_scope;
3162 /* Process the final unqualified-id. */
3163 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3166 /* Restore the SAVED_SCOPE for our caller. */
3167 parser->scope = saved_scope;
3168 parser->object_scope = saved_object_scope;
3169 parser->qualifying_scope = saved_qualifying_scope;
3171 return unqualified_id;
3173 /* Otherwise, if we are in global scope, then we are looking at one
3174 of the other qualified-id productions. */
3175 else if (global_scope_p)
3180 /* Peek at the next token. */
3181 token = cp_lexer_peek_token (parser->lexer);
3183 /* If it's an identifier, and the next token is not a "<", then
3184 we can avoid the template-id case. This is an optimization
3185 for this common case. */
3186 if (token->type == CPP_NAME
3187 && !cp_parser_nth_token_starts_template_argument_list_p
3189 return cp_parser_identifier (parser);
3191 cp_parser_parse_tentatively (parser);
3192 /* Try a template-id. */
3193 id = cp_parser_template_id (parser,
3194 /*template_keyword_p=*/false,
3195 /*check_dependency_p=*/true,
3197 /* If that worked, we're done. */
3198 if (cp_parser_parse_definitely (parser))
3201 /* Peek at the next token. (Changes in the token buffer may
3202 have invalidated the pointer obtained above.) */
3203 token = cp_lexer_peek_token (parser->lexer);
3205 switch (token->type)
3208 return cp_parser_identifier (parser);
3211 if (token->keyword == RID_OPERATOR)
3212 return cp_parser_operator_function_id (parser);
3216 cp_parser_error (parser, "expected id-expression");
3217 return error_mark_node;
3221 return cp_parser_unqualified_id (parser, template_keyword_p,
3222 /*check_dependency_p=*/true,
3226 /* Parse an unqualified-id.
3230 operator-function-id
3231 conversion-function-id
3235 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3236 keyword, in a construct like `A::template ...'.
3238 Returns a representation of unqualified-id. For the `identifier'
3239 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3240 production a BIT_NOT_EXPR is returned; the operand of the
3241 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3242 other productions, see the documentation accompanying the
3243 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3244 names are looked up in uninstantiated templates. If DECLARATOR_P
3245 is true, the unqualified-id is appearing as part of a declarator,
3246 rather than as part of an expression. */
3249 cp_parser_unqualified_id (cp_parser* parser,
3250 bool template_keyword_p,
3251 bool check_dependency_p,
3256 /* Peek at the next token. */
3257 token = cp_lexer_peek_token (parser->lexer);
3259 switch (token->type)
3265 /* We don't know yet whether or not this will be a
3267 cp_parser_parse_tentatively (parser);
3268 /* Try a template-id. */
3269 id = cp_parser_template_id (parser, template_keyword_p,
3272 /* If it worked, we're done. */
3273 if (cp_parser_parse_definitely (parser))
3275 /* Otherwise, it's an ordinary identifier. */
3276 return cp_parser_identifier (parser);
3279 case CPP_TEMPLATE_ID:
3280 return cp_parser_template_id (parser, template_keyword_p,
3287 tree qualifying_scope;
3292 /* Consume the `~' token. */
3293 cp_lexer_consume_token (parser->lexer);
3294 /* Parse the class-name. The standard, as written, seems to
3297 template <typename T> struct S { ~S (); };
3298 template <typename T> S<T>::~S() {}
3300 is invalid, since `~' must be followed by a class-name, but
3301 `S<T>' is dependent, and so not known to be a class.
3302 That's not right; we need to look in uninstantiated
3303 templates. A further complication arises from:
3305 template <typename T> void f(T t) {
3309 Here, it is not possible to look up `T' in the scope of `T'
3310 itself. We must look in both the current scope, and the
3311 scope of the containing complete expression.
3313 Yet another issue is:
3322 The standard does not seem to say that the `S' in `~S'
3323 should refer to the type `S' and not the data member
3326 /* DR 244 says that we look up the name after the "~" in the
3327 same scope as we looked up the qualifying name. That idea
3328 isn't fully worked out; it's more complicated than that. */
3329 scope = parser->scope;
3330 object_scope = parser->object_scope;
3331 qualifying_scope = parser->qualifying_scope;
3333 /* If the name is of the form "X::~X" it's OK. */
3334 if (scope && TYPE_P (scope)
3335 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3336 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3338 && (cp_lexer_peek_token (parser->lexer)->value
3339 == TYPE_IDENTIFIER (scope)))
3341 cp_lexer_consume_token (parser->lexer);
3342 return build_nt (BIT_NOT_EXPR, scope);
3345 /* If there was an explicit qualification (S::~T), first look
3346 in the scope given by the qualification (i.e., S). */
3348 type_decl = NULL_TREE;
3351 cp_parser_parse_tentatively (parser);
3352 type_decl = cp_parser_class_name (parser,
3353 /*typename_keyword_p=*/false,
3354 /*template_keyword_p=*/false,
3356 /*check_dependency=*/false,
3357 /*class_head_p=*/false,
3359 if (cp_parser_parse_definitely (parser))
3362 /* In "N::S::~S", look in "N" as well. */
3363 if (!done && scope && qualifying_scope)
3365 cp_parser_parse_tentatively (parser);
3366 parser->scope = qualifying_scope;
3367 parser->object_scope = NULL_TREE;
3368 parser->qualifying_scope = NULL_TREE;
3370 = cp_parser_class_name (parser,
3371 /*typename_keyword_p=*/false,
3372 /*template_keyword_p=*/false,
3374 /*check_dependency=*/false,
3375 /*class_head_p=*/false,
3377 if (cp_parser_parse_definitely (parser))
3380 /* In "p->S::~T", look in the scope given by "*p" as well. */
3381 else if (!done && object_scope)
3383 cp_parser_parse_tentatively (parser);
3384 parser->scope = object_scope;
3385 parser->object_scope = NULL_TREE;
3386 parser->qualifying_scope = NULL_TREE;
3388 = cp_parser_class_name (parser,
3389 /*typename_keyword_p=*/false,
3390 /*template_keyword_p=*/false,
3392 /*check_dependency=*/false,
3393 /*class_head_p=*/false,
3395 if (cp_parser_parse_definitely (parser))
3398 /* Look in the surrounding context. */
3401 parser->scope = NULL_TREE;
3402 parser->object_scope = NULL_TREE;
3403 parser->qualifying_scope = NULL_TREE;
3405 = cp_parser_class_name (parser,
3406 /*typename_keyword_p=*/false,
3407 /*template_keyword_p=*/false,
3409 /*check_dependency=*/false,
3410 /*class_head_p=*/false,
3413 /* If an error occurred, assume that the name of the
3414 destructor is the same as the name of the qualifying
3415 class. That allows us to keep parsing after running
3416 into ill-formed destructor names. */
3417 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3418 return build_nt (BIT_NOT_EXPR, scope);
3419 else if (type_decl == error_mark_node)
3420 return error_mark_node;
3424 A typedef-name that names a class shall not be used as the
3425 identifier in the declarator for a destructor declaration. */
3427 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3428 && !DECL_SELF_REFERENCE_P (type_decl)
3429 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3430 error ("typedef-name %qD used as destructor declarator",
3433 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3437 if (token->keyword == RID_OPERATOR)
3441 /* This could be a template-id, so we try that first. */
3442 cp_parser_parse_tentatively (parser);
3443 /* Try a template-id. */
3444 id = cp_parser_template_id (parser, template_keyword_p,
3445 /*check_dependency_p=*/true,
3447 /* If that worked, we're done. */
3448 if (cp_parser_parse_definitely (parser))
3450 /* We still don't know whether we're looking at an
3451 operator-function-id or a conversion-function-id. */
3452 cp_parser_parse_tentatively (parser);
3453 /* Try an operator-function-id. */
3454 id = cp_parser_operator_function_id (parser);
3455 /* If that didn't work, try a conversion-function-id. */
3456 if (!cp_parser_parse_definitely (parser))
3457 id = cp_parser_conversion_function_id (parser);
3464 cp_parser_error (parser, "expected unqualified-id");
3465 return error_mark_node;
3469 /* Parse an (optional) nested-name-specifier.
3471 nested-name-specifier:
3472 class-or-namespace-name :: nested-name-specifier [opt]
3473 class-or-namespace-name :: template nested-name-specifier [opt]
3475 PARSER->SCOPE should be set appropriately before this function is
3476 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3477 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3480 Sets PARSER->SCOPE to the class (TYPE) or namespace
3481 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3482 it unchanged if there is no nested-name-specifier. Returns the new
3483 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3485 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3486 part of a declaration and/or decl-specifier. */
3489 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3490 bool typename_keyword_p,
3491 bool check_dependency_p,
3493 bool is_declaration)
3495 bool success = false;
3496 tree access_check = NULL_TREE;
3497 cp_token_position start = 0;
3500 /* If the next token corresponds to a nested name specifier, there
3501 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3502 false, it may have been true before, in which case something
3503 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3504 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3505 CHECK_DEPENDENCY_P is false, we have to fall through into the
3507 if (check_dependency_p
3508 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3510 cp_parser_pre_parsed_nested_name_specifier (parser);
3511 return parser->scope;
3514 /* Remember where the nested-name-specifier starts. */
3515 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3516 start = cp_lexer_token_position (parser->lexer, false);
3518 push_deferring_access_checks (dk_deferred);
3524 tree saved_qualifying_scope;
3525 bool template_keyword_p;
3527 /* Spot cases that cannot be the beginning of a
3528 nested-name-specifier. */
3529 token = cp_lexer_peek_token (parser->lexer);
3531 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3532 the already parsed nested-name-specifier. */
3533 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3535 /* Grab the nested-name-specifier and continue the loop. */
3536 cp_parser_pre_parsed_nested_name_specifier (parser);
3541 /* Spot cases that cannot be the beginning of a
3542 nested-name-specifier. On the second and subsequent times
3543 through the loop, we look for the `template' keyword. */
3544 if (success && token->keyword == RID_TEMPLATE)
3546 /* A template-id can start a nested-name-specifier. */
3547 else if (token->type == CPP_TEMPLATE_ID)
3551 /* If the next token is not an identifier, then it is
3552 definitely not a class-or-namespace-name. */
3553 if (token->type != CPP_NAME)
3555 /* If the following token is neither a `<' (to begin a
3556 template-id), nor a `::', then we are not looking at a
3557 nested-name-specifier. */
3558 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3559 if (token->type != CPP_SCOPE
3560 && !cp_parser_nth_token_starts_template_argument_list_p
3565 /* The nested-name-specifier is optional, so we parse
3567 cp_parser_parse_tentatively (parser);
3569 /* Look for the optional `template' keyword, if this isn't the
3570 first time through the loop. */
3572 template_keyword_p = cp_parser_optional_template_keyword (parser);
3574 template_keyword_p = false;
3576 /* Save the old scope since the name lookup we are about to do
3577 might destroy it. */
3578 old_scope = parser->scope;
3579 saved_qualifying_scope = parser->qualifying_scope;
3580 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3581 look up names in "X<T>::I" in order to determine that "Y" is
3582 a template. So, if we have a typename at this point, we make
3583 an effort to look through it. */
3585 && !typename_keyword_p
3587 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3588 parser->scope = resolve_typename_type (parser->scope,
3589 /*only_current_p=*/false);
3590 /* Parse the qualifying entity. */
3592 = cp_parser_class_or_namespace_name (parser,
3598 /* Look for the `::' token. */
3599 cp_parser_require (parser, CPP_SCOPE, "`::'");
3601 /* If we found what we wanted, we keep going; otherwise, we're
3603 if (!cp_parser_parse_definitely (parser))
3605 bool error_p = false;
3607 /* Restore the OLD_SCOPE since it was valid before the
3608 failed attempt at finding the last
3609 class-or-namespace-name. */
3610 parser->scope = old_scope;
3611 parser->qualifying_scope = saved_qualifying_scope;
3612 /* If the next token is an identifier, and the one after
3613 that is a `::', then any valid interpretation would have
3614 found a class-or-namespace-name. */
3615 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3616 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3618 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3621 token = cp_lexer_consume_token (parser->lexer);
3624 if (!token->ambiguous_p)
3627 tree ambiguous_decls;
3629 decl = cp_parser_lookup_name (parser, token->value,
3631 /*is_template=*/false,
3632 /*is_namespace=*/false,
3633 /*check_dependency=*/true,
3635 if (TREE_CODE (decl) == TEMPLATE_DECL)
3636 error ("%qD used without template parameters", decl);
3637 else if (ambiguous_decls)
3639 error ("reference to %qD is ambiguous",
3641 print_candidates (ambiguous_decls);
3642 decl = error_mark_node;
3645 cp_parser_name_lookup_error
3646 (parser, token->value, decl,
3647 "is not a class or namespace");
3649 parser->scope = error_mark_node;
3651 /* Treat this as a successful nested-name-specifier
3656 If the name found is not a class-name (clause
3657 _class_) or namespace-name (_namespace.def_), the
3658 program is ill-formed. */
3661 cp_lexer_consume_token (parser->lexer);
3665 /* We've found one valid nested-name-specifier. */
3667 /* Name lookup always gives us a DECL. */
3668 if (TREE_CODE (new_scope) == TYPE_DECL)
3669 new_scope = TREE_TYPE (new_scope);
3670 /* Uses of "template" must be followed by actual templates. */
3671 if (template_keyword_p
3672 && !(CLASS_TYPE_P (new_scope)
3673 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3674 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3675 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3676 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3677 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3678 == TEMPLATE_ID_EXPR)))
3679 pedwarn (TYPE_P (new_scope)
3680 ? "%qT is not a template"
3681 : "%qD is not a template",
3683 /* If it is a class scope, try to complete it; we are about to
3684 be looking up names inside the class. */
3685 if (TYPE_P (new_scope)
3686 /* Since checking types for dependency can be expensive,
3687 avoid doing it if the type is already complete. */
3688 && !COMPLETE_TYPE_P (new_scope)
3689 /* Do not try to complete dependent types. */
3690 && !dependent_type_p (new_scope))
3691 new_scope = complete_type (new_scope);
3692 /* Make sure we look in the right scope the next time through
3694 parser->scope = new_scope;
3697 /* Retrieve any deferred checks. Do not pop this access checks yet
3698 so the memory will not be reclaimed during token replacing below. */
3699 access_check = get_deferred_access_checks ();
3701 /* If parsing tentatively, replace the sequence of tokens that makes
3702 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3703 token. That way, should we re-parse the token stream, we will
3704 not have to repeat the effort required to do the parse, nor will
3705 we issue duplicate error messages. */
3706 if (success && start)
3708 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3710 /* Reset the contents of the START token. */
3711 token->type = CPP_NESTED_NAME_SPECIFIER;
3712 token->value = build_tree_list (access_check, parser->scope);
3713 TREE_TYPE (token->value) = parser->qualifying_scope;
3714 token->keyword = RID_MAX;
3716 /* Purge all subsequent tokens. */
3717 cp_lexer_purge_tokens_after (parser->lexer, start);
3720 pop_deferring_access_checks ();
3721 return success ? parser->scope : NULL_TREE;
3724 /* Parse a nested-name-specifier. See
3725 cp_parser_nested_name_specifier_opt for details. This function
3726 behaves identically, except that it will an issue an error if no
3727 nested-name-specifier is present. */
3730 cp_parser_nested_name_specifier (cp_parser *parser,
3731 bool typename_keyword_p,
3732 bool check_dependency_p,
3734 bool is_declaration)
3738 /* Look for the nested-name-specifier. */
3739 scope = cp_parser_nested_name_specifier_opt (parser,
3744 /* If it was not present, issue an error message. */
3747 cp_parser_error (parser, "expected nested-name-specifier");
3748 parser->scope = NULL_TREE;
3754 /* Parse a class-or-namespace-name.
3756 class-or-namespace-name:
3760 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3761 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3762 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3763 TYPE_P is TRUE iff the next name should be taken as a class-name,
3764 even the same name is declared to be another entity in the same
3767 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3768 specified by the class-or-namespace-name. If neither is found the
3769 ERROR_MARK_NODE is returned. */
3772 cp_parser_class_or_namespace_name (cp_parser *parser,
3773 bool typename_keyword_p,
3774 bool template_keyword_p,
3775 bool check_dependency_p,
3777 bool is_declaration)
3780 tree saved_qualifying_scope;
3781 tree saved_object_scope;
3785 /* Before we try to parse the class-name, we must save away the
3786 current PARSER->SCOPE since cp_parser_class_name will destroy
3788 saved_scope = parser->scope;
3789 saved_qualifying_scope = parser->qualifying_scope;
3790 saved_object_scope = parser->object_scope;
3791 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3792 there is no need to look for a namespace-name. */
3793 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3795 cp_parser_parse_tentatively (parser);
3796 scope = cp_parser_class_name (parser,
3799 type_p ? class_type : none_type,
3801 /*class_head_p=*/false,
3803 /* If that didn't work, try for a namespace-name. */
3804 if (!only_class_p && !cp_parser_parse_definitely (parser))
3806 /* Restore the saved scope. */
3807 parser->scope = saved_scope;
3808 parser->qualifying_scope = saved_qualifying_scope;
3809 parser->object_scope = saved_object_scope;
3810 /* If we are not looking at an identifier followed by the scope
3811 resolution operator, then this is not part of a
3812 nested-name-specifier. (Note that this function is only used
3813 to parse the components of a nested-name-specifier.) */
3814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3815 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3816 return error_mark_node;
3817 scope = cp_parser_namespace_name (parser);
3823 /* Parse a postfix-expression.
3827 postfix-expression [ expression ]
3828 postfix-expression ( expression-list [opt] )
3829 simple-type-specifier ( expression-list [opt] )
3830 typename :: [opt] nested-name-specifier identifier
3831 ( expression-list [opt] )
3832 typename :: [opt] nested-name-specifier template [opt] template-id
3833 ( expression-list [opt] )
3834 postfix-expression . template [opt] id-expression
3835 postfix-expression -> template [opt] id-expression
3836 postfix-expression . pseudo-destructor-name
3837 postfix-expression -> pseudo-destructor-name
3838 postfix-expression ++
3839 postfix-expression --
3840 dynamic_cast < type-id > ( expression )
3841 static_cast < type-id > ( expression )
3842 reinterpret_cast < type-id > ( expression )
3843 const_cast < type-id > ( expression )
3844 typeid ( expression )
3850 ( type-id ) { initializer-list , [opt] }
3852 This extension is a GNU version of the C99 compound-literal
3853 construct. (The C99 grammar uses `type-name' instead of `type-id',
3854 but they are essentially the same concept.)
3856 If ADDRESS_P is true, the postfix expression is the operand of the
3857 `&' operator. CAST_P is true if this expression is the target of a
3860 Returns a representation of the expression. */
3863 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3867 cp_id_kind idk = CP_ID_KIND_NONE;
3868 tree postfix_expression = NULL_TREE;
3870 /* Peek at the next token. */
3871 token = cp_lexer_peek_token (parser->lexer);
3872 /* Some of the productions are determined by keywords. */
3873 keyword = token->keyword;
3883 const char *saved_message;
3885 /* All of these can be handled in the same way from the point
3886 of view of parsing. Begin by consuming the token
3887 identifying the cast. */
3888 cp_lexer_consume_token (parser->lexer);
3890 /* New types cannot be defined in the cast. */
3891 saved_message = parser->type_definition_forbidden_message;
3892 parser->type_definition_forbidden_message
3893 = "types may not be defined in casts";
3895 /* Look for the opening `<'. */
3896 cp_parser_require (parser, CPP_LESS, "`<'");
3897 /* Parse the type to which we are casting. */
3898 type = cp_parser_type_id (parser);
3899 /* Look for the closing `>'. */
3900 cp_parser_require (parser, CPP_GREATER, "`>'");
3901 /* Restore the old message. */
3902 parser->type_definition_forbidden_message = saved_message;
3904 /* And the expression which is being cast. */
3905 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3906 expression = cp_parser_expression (parser, /*cast_p=*/true);
3907 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3909 /* Only type conversions to integral or enumeration types
3910 can be used in constant-expressions. */
3911 if (parser->integral_constant_expression_p
3912 && !dependent_type_p (type)
3913 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3914 && (cp_parser_non_integral_constant_expression
3916 "a cast to a type other than an integral or "
3917 "enumeration type")))
3918 return error_mark_node;
3924 = build_dynamic_cast (type, expression);
3928 = build_static_cast (type, expression);
3932 = build_reinterpret_cast (type, expression);
3936 = build_const_cast (type, expression);
3947 const char *saved_message;
3948 bool saved_in_type_id_in_expr_p;
3950 /* Consume the `typeid' token. */
3951 cp_lexer_consume_token (parser->lexer);
3952 /* Look for the `(' token. */
3953 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3954 /* Types cannot be defined in a `typeid' expression. */
3955 saved_message = parser->type_definition_forbidden_message;
3956 parser->type_definition_forbidden_message
3957 = "types may not be defined in a `typeid\' expression";
3958 /* We can't be sure yet whether we're looking at a type-id or an
3960 cp_parser_parse_tentatively (parser);
3961 /* Try a type-id first. */
3962 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3963 parser->in_type_id_in_expr_p = true;
3964 type = cp_parser_type_id (parser);
3965 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3966 /* Look for the `)' token. Otherwise, we can't be sure that
3967 we're not looking at an expression: consider `typeid (int
3968 (3))', for example. */
3969 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3970 /* If all went well, simply lookup the type-id. */
3971 if (cp_parser_parse_definitely (parser))
3972 postfix_expression = get_typeid (type);
3973 /* Otherwise, fall back to the expression variant. */
3978 /* Look for an expression. */
3979 expression = cp_parser_expression (parser, /*cast_p=*/false);
3980 /* Compute its typeid. */
3981 postfix_expression = build_typeid (expression);
3982 /* Look for the `)' token. */
3983 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3985 /* `typeid' may not appear in an integral constant expression. */
3986 if (cp_parser_non_integral_constant_expression(parser,
3987 "`typeid' operator"))
3988 return error_mark_node;
3989 /* Restore the saved message. */
3990 parser->type_definition_forbidden_message = saved_message;
3997 /* The syntax permitted here is the same permitted for an
3998 elaborated-type-specifier. */
3999 type = cp_parser_elaborated_type_specifier (parser,
4000 /*is_friend=*/false,
4001 /*is_declaration=*/false);
4002 postfix_expression = cp_parser_functional_cast (parser, type);
4010 /* If the next thing is a simple-type-specifier, we may be
4011 looking at a functional cast. We could also be looking at
4012 an id-expression. So, we try the functional cast, and if
4013 that doesn't work we fall back to the primary-expression. */
4014 cp_parser_parse_tentatively (parser);
4015 /* Look for the simple-type-specifier. */
4016 type = cp_parser_simple_type_specifier (parser,
4017 /*decl_specs=*/NULL,
4018 CP_PARSER_FLAGS_NONE);
4019 /* Parse the cast itself. */
4020 if (!cp_parser_error_occurred (parser))
4022 = cp_parser_functional_cast (parser, type);
4023 /* If that worked, we're done. */
4024 if (cp_parser_parse_definitely (parser))
4027 /* If the functional-cast didn't work out, try a
4028 compound-literal. */
4029 if (cp_parser_allow_gnu_extensions_p (parser)
4030 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4032 VEC(constructor_elt,gc) *initializer_list = NULL;
4033 bool saved_in_type_id_in_expr_p;
4035 cp_parser_parse_tentatively (parser);
4036 /* Consume the `('. */
4037 cp_lexer_consume_token (parser->lexer);
4038 /* Parse the type. */
4039 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4040 parser->in_type_id_in_expr_p = true;
4041 type = cp_parser_type_id (parser);
4042 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4043 /* Look for the `)'. */
4044 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4045 /* Look for the `{'. */
4046 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4047 /* If things aren't going well, there's no need to
4049 if (!cp_parser_error_occurred (parser))
4051 bool non_constant_p;
4052 /* Parse the initializer-list. */
4054 = cp_parser_initializer_list (parser, &non_constant_p);
4055 /* Allow a trailing `,'. */
4056 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4057 cp_lexer_consume_token (parser->lexer);
4058 /* Look for the final `}'. */
4059 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4061 /* If that worked, we're definitely looking at a
4062 compound-literal expression. */
4063 if (cp_parser_parse_definitely (parser))
4065 /* Warn the user that a compound literal is not
4066 allowed in standard C++. */
4068 pedwarn ("ISO C++ forbids compound-literals");
4069 /* Form the representation of the compound-literal. */
4071 = finish_compound_literal (type, initializer_list);
4076 /* It must be a primary-expression. */
4078 = cp_parser_primary_expression (parser, address_p, cast_p,
4079 /*template_arg_p=*/false,
4085 /* Keep looping until the postfix-expression is complete. */
4088 if (idk == CP_ID_KIND_UNQUALIFIED
4089 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4090 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4091 /* It is not a Koenig lookup function call. */
4093 = unqualified_name_lookup_error (postfix_expression);
4095 /* Peek at the next token. */
4096 token = cp_lexer_peek_token (parser->lexer);
4098 switch (token->type)
4100 case CPP_OPEN_SQUARE:
4102 = cp_parser_postfix_open_square_expression (parser,
4105 idk = CP_ID_KIND_NONE;
4108 case CPP_OPEN_PAREN:
4109 /* postfix-expression ( expression-list [opt] ) */
4112 bool is_builtin_constant_p;
4113 bool saved_integral_constant_expression_p = false;
4114 bool saved_non_integral_constant_expression_p = false;
4117 is_builtin_constant_p
4118 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4119 if (is_builtin_constant_p)
4121 /* The whole point of __builtin_constant_p is to allow
4122 non-constant expressions to appear as arguments. */
4123 saved_integral_constant_expression_p
4124 = parser->integral_constant_expression_p;
4125 saved_non_integral_constant_expression_p
4126 = parser->non_integral_constant_expression_p;
4127 parser->integral_constant_expression_p = false;
4129 args = (cp_parser_parenthesized_expression_list
4130 (parser, /*is_attribute_list=*/false,
4132 /*non_constant_p=*/NULL));
4133 if (is_builtin_constant_p)
4135 parser->integral_constant_expression_p
4136 = saved_integral_constant_expression_p;
4137 parser->non_integral_constant_expression_p
4138 = saved_non_integral_constant_expression_p;
4141 if (args == error_mark_node)
4143 postfix_expression = error_mark_node;
4147 /* Function calls are not permitted in
4148 constant-expressions. */
4149 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4150 && cp_parser_non_integral_constant_expression (parser,
4153 postfix_expression = error_mark_node;
4158 if (idk == CP_ID_KIND_UNQUALIFIED)
4160 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4166 = perform_koenig_lookup (postfix_expression, args);
4170 = unqualified_fn_lookup_error (postfix_expression);
4172 /* We do not perform argument-dependent lookup if
4173 normal lookup finds a non-function, in accordance
4174 with the expected resolution of DR 218. */
4175 else if (args && is_overloaded_fn (postfix_expression))
4177 tree fn = get_first_fn (postfix_expression);
4179 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4180 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4182 /* Only do argument dependent lookup if regular
4183 lookup does not find a set of member functions.
4184 [basic.lookup.koenig]/2a */
4185 if (!DECL_FUNCTION_MEMBER_P (fn))
4189 = perform_koenig_lookup (postfix_expression, args);
4194 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4196 tree instance = TREE_OPERAND (postfix_expression, 0);
4197 tree fn = TREE_OPERAND (postfix_expression, 1);
4199 if (processing_template_decl
4200 && (type_dependent_expression_p (instance)
4201 || (!BASELINK_P (fn)
4202 && TREE_CODE (fn) != FIELD_DECL)
4203 || type_dependent_expression_p (fn)
4204 || any_type_dependent_arguments_p (args)))
4207 = build_min_nt (CALL_EXPR, postfix_expression,
4212 if (BASELINK_P (fn))
4214 = (build_new_method_call
4215 (instance, fn, args, NULL_TREE,
4216 (idk == CP_ID_KIND_QUALIFIED
4217 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4220 = finish_call_expr (postfix_expression, args,
4221 /*disallow_virtual=*/false,
4222 /*koenig_p=*/false);
4224 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4225 || TREE_CODE (postfix_expression) == MEMBER_REF
4226 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4227 postfix_expression = (build_offset_ref_call_from_tree
4228 (postfix_expression, args));
4229 else if (idk == CP_ID_KIND_QUALIFIED)
4230 /* A call to a static class member, or a namespace-scope
4233 = finish_call_expr (postfix_expression, args,
4234 /*disallow_virtual=*/true,
4237 /* All other function calls. */
4239 = finish_call_expr (postfix_expression, args,
4240 /*disallow_virtual=*/false,
4243 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4244 idk = CP_ID_KIND_NONE;
4250 /* postfix-expression . template [opt] id-expression
4251 postfix-expression . pseudo-destructor-name
4252 postfix-expression -> template [opt] id-expression
4253 postfix-expression -> pseudo-destructor-name */
4255 /* Consume the `.' or `->' operator. */
4256 cp_lexer_consume_token (parser->lexer);
4259 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4265 /* postfix-expression ++ */
4266 /* Consume the `++' token. */
4267 cp_lexer_consume_token (parser->lexer);
4268 /* Generate a representation for the complete expression. */
4270 = finish_increment_expr (postfix_expression,
4271 POSTINCREMENT_EXPR);
4272 /* Increments may not appear in constant-expressions. */
4273 if (cp_parser_non_integral_constant_expression (parser,
4275 postfix_expression = error_mark_node;
4276 idk = CP_ID_KIND_NONE;
4279 case CPP_MINUS_MINUS:
4280 /* postfix-expression -- */
4281 /* Consume the `--' token. */
4282 cp_lexer_consume_token (parser->lexer);
4283 /* Generate a representation for the complete expression. */
4285 = finish_increment_expr (postfix_expression,
4286 POSTDECREMENT_EXPR);
4287 /* Decrements may not appear in constant-expressions. */
4288 if (cp_parser_non_integral_constant_expression (parser,
4290 postfix_expression = error_mark_node;
4291 idk = CP_ID_KIND_NONE;
4295 return postfix_expression;
4299 /* We should never get here. */
4301 return error_mark_node;
4304 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4305 by cp_parser_builtin_offsetof. We're looking for
4307 postfix-expression [ expression ]
4309 FOR_OFFSETOF is set if we're being called in that context, which
4310 changes how we deal with integer constant expressions. */
4313 cp_parser_postfix_open_square_expression (cp_parser *parser,
4314 tree postfix_expression,
4319 /* Consume the `[' token. */
4320 cp_lexer_consume_token (parser->lexer);
4322 /* Parse the index expression. */
4323 /* ??? For offsetof, there is a question of what to allow here. If
4324 offsetof is not being used in an integral constant expression context,
4325 then we *could* get the right answer by computing the value at runtime.
4326 If we are in an integral constant expression context, then we might
4327 could accept any constant expression; hard to say without analysis.
4328 Rather than open the barn door too wide right away, allow only integer
4329 constant expressions here. */
4331 index = cp_parser_constant_expression (parser, false, NULL);
4333 index = cp_parser_expression (parser, /*cast_p=*/false);
4335 /* Look for the closing `]'. */
4336 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4338 /* Build the ARRAY_REF. */
4339 postfix_expression = grok_array_decl (postfix_expression, index);
4341 /* When not doing offsetof, array references are not permitted in
4342 constant-expressions. */
4344 && (cp_parser_non_integral_constant_expression
4345 (parser, "an array reference")))
4346 postfix_expression = error_mark_node;
4348 return postfix_expression;
4351 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4352 by cp_parser_builtin_offsetof. We're looking for
4354 postfix-expression . template [opt] id-expression
4355 postfix-expression . pseudo-destructor-name
4356 postfix-expression -> template [opt] id-expression
4357 postfix-expression -> pseudo-destructor-name
4359 FOR_OFFSETOF is set if we're being called in that context. That sorta
4360 limits what of the above we'll actually accept, but nevermind.
4361 TOKEN_TYPE is the "." or "->" token, which will already have been
4362 removed from the stream. */
4365 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4366 enum cpp_ttype token_type,
4367 tree postfix_expression,
4368 bool for_offsetof, cp_id_kind *idk)
4372 bool pseudo_destructor_p;
4373 tree scope = NULL_TREE;
4375 /* If this is a `->' operator, dereference the pointer. */
4376 if (token_type == CPP_DEREF)
4377 postfix_expression = build_x_arrow (postfix_expression);
4378 /* Check to see whether or not the expression is type-dependent. */
4379 dependent_p = type_dependent_expression_p (postfix_expression);
4380 /* The identifier following the `->' or `.' is not qualified. */
4381 parser->scope = NULL_TREE;
4382 parser->qualifying_scope = NULL_TREE;
4383 parser->object_scope = NULL_TREE;
4384 *idk = CP_ID_KIND_NONE;
4385 /* Enter the scope corresponding to the type of the object
4386 given by the POSTFIX_EXPRESSION. */
4387 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4389 scope = TREE_TYPE (postfix_expression);
4390 /* According to the standard, no expression should ever have
4391 reference type. Unfortunately, we do not currently match
4392 the standard in this respect in that our internal representation
4393 of an expression may have reference type even when the standard
4394 says it does not. Therefore, we have to manually obtain the
4395 underlying type here. */
4396 scope = non_reference (scope);
4397 /* The type of the POSTFIX_EXPRESSION must be complete. */
4398 if (scope == unknown_type_node)
4400 error ("%qE does not have class type", postfix_expression);
4404 scope = complete_type_or_else (scope, NULL_TREE);
4405 /* Let the name lookup machinery know that we are processing a
4406 class member access expression. */
4407 parser->context->object_type = scope;
4408 /* If something went wrong, we want to be able to discern that case,
4409 as opposed to the case where there was no SCOPE due to the type
4410 of expression being dependent. */
4412 scope = error_mark_node;
4413 /* If the SCOPE was erroneous, make the various semantic analysis
4414 functions exit quickly -- and without issuing additional error
4416 if (scope == error_mark_node)
4417 postfix_expression = error_mark_node;
4420 /* Assume this expression is not a pseudo-destructor access. */
4421 pseudo_destructor_p = false;
4423 /* If the SCOPE is a scalar type, then, if this is a valid program,
4424 we must be looking at a pseudo-destructor-name. */
4425 if (scope && SCALAR_TYPE_P (scope))
4430 cp_parser_parse_tentatively (parser);
4431 /* Parse the pseudo-destructor-name. */
4433 cp_parser_pseudo_destructor_name (parser, &s, &type);
4434 if (cp_parser_parse_definitely (parser))
4436 pseudo_destructor_p = true;
4438 = finish_pseudo_destructor_expr (postfix_expression,
4439 s, TREE_TYPE (type));
4443 if (!pseudo_destructor_p)
4445 /* If the SCOPE is not a scalar type, we are looking at an
4446 ordinary class member access expression, rather than a
4447 pseudo-destructor-name. */
4449 /* Parse the id-expression. */
4450 name = (cp_parser_id_expression
4452 cp_parser_optional_template_keyword (parser),
4453 /*check_dependency_p=*/true,
4455 /*declarator_p=*/false));
4456 /* In general, build a SCOPE_REF if the member name is qualified.
4457 However, if the name was not dependent and has already been
4458 resolved; there is no need to build the SCOPE_REF. For example;
4460 struct X { void f(); };
4461 template <typename T> void f(T* t) { t->X::f(); }
4463 Even though "t" is dependent, "X::f" is not and has been resolved
4464 to a BASELINK; there is no need to include scope information. */
4466 /* But we do need to remember that there was an explicit scope for
4467 virtual function calls. */
4469 *idk = CP_ID_KIND_QUALIFIED;
4471 /* If the name is a template-id that names a type, we will get a
4472 TYPE_DECL here. That is invalid code. */
4473 if (TREE_CODE (name) == TYPE_DECL)
4475 error ("invalid use of %qD", name);
4476 postfix_expression = error_mark_node;
4480 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4482 name = build_qualified_name (/*type=*/NULL_TREE,
4486 parser->scope = NULL_TREE;
4487 parser->qualifying_scope = NULL_TREE;
4488 parser->object_scope = NULL_TREE;
4490 if (scope && name && BASELINK_P (name))
4491 adjust_result_of_qualified_name_lookup
4492 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4494 = finish_class_member_access_expr (postfix_expression, name,
4499 /* We no longer need to look up names in the scope of the object on
4500 the left-hand side of the `.' or `->' operator. */
4501 parser->context->object_type = NULL_TREE;
4503 /* Outside of offsetof, these operators may not appear in
4504 constant-expressions. */
4506 && (cp_parser_non_integral_constant_expression
4507 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4508 postfix_expression = error_mark_node;
4510 return postfix_expression;
4513 /* Parse a parenthesized expression-list.
4516 assignment-expression
4517 expression-list, assignment-expression
4522 identifier, expression-list
4524 CAST_P is true if this expression is the target of a cast.
4526 Returns a TREE_LIST. The TREE_VALUE of each node is a
4527 representation of an assignment-expression. Note that a TREE_LIST
4528 is returned even if there is only a single expression in the list.
4529 error_mark_node is returned if the ( and or ) are
4530 missing. NULL_TREE is returned on no expressions. The parentheses
4531 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4532 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4533 indicates whether or not all of the expressions in the list were
4537 cp_parser_parenthesized_expression_list (cp_parser* parser,
4538 bool is_attribute_list,
4540 bool *non_constant_p)
4542 tree expression_list = NULL_TREE;
4543 bool fold_expr_p = is_attribute_list;
4544 tree identifier = NULL_TREE;
4546 /* Assume all the expressions will be constant. */
4548 *non_constant_p = false;
4550 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4551 return error_mark_node;
4553 /* Consume expressions until there are no more. */
4554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4559 /* At the beginning of attribute lists, check to see if the
4560 next token is an identifier. */
4561 if (is_attribute_list
4562 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4566 /* Consume the identifier. */
4567 token = cp_lexer_consume_token (parser->lexer);
4568 /* Save the identifier. */
4569 identifier = token->value;
4573 /* Parse the next assignment-expression. */
4576 bool expr_non_constant_p;
4577 expr = (cp_parser_constant_expression
4578 (parser, /*allow_non_constant_p=*/true,
4579 &expr_non_constant_p));
4580 if (expr_non_constant_p)
4581 *non_constant_p = true;
4584 expr = cp_parser_assignment_expression (parser, cast_p);
4587 expr = fold_non_dependent_expr (expr);
4589 /* Add it to the list. We add error_mark_node
4590 expressions to the list, so that we can still tell if
4591 the correct form for a parenthesized expression-list
4592 is found. That gives better errors. */
4593 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4595 if (expr == error_mark_node)
4599 /* After the first item, attribute lists look the same as
4600 expression lists. */
4601 is_attribute_list = false;
4604 /* If the next token isn't a `,', then we are done. */
4605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4608 /* Otherwise, consume the `,' and keep going. */
4609 cp_lexer_consume_token (parser->lexer);
4612 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4617 /* We try and resync to an unnested comma, as that will give the
4618 user better diagnostics. */
4619 ending = cp_parser_skip_to_closing_parenthesis (parser,
4620 /*recovering=*/true,
4622 /*consume_paren=*/true);
4626 return error_mark_node;
4629 /* We built up the list in reverse order so we must reverse it now. */
4630 expression_list = nreverse (expression_list);
4632 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4634 return expression_list;
4637 /* Parse a pseudo-destructor-name.
4639 pseudo-destructor-name:
4640 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4641 :: [opt] nested-name-specifier template template-id :: ~ type-name
4642 :: [opt] nested-name-specifier [opt] ~ type-name
4644 If either of the first two productions is used, sets *SCOPE to the
4645 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4646 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4647 or ERROR_MARK_NODE if the parse fails. */
4650 cp_parser_pseudo_destructor_name (cp_parser* parser,
4654 bool nested_name_specifier_p;
4656 /* Assume that things will not work out. */
4657 *type = error_mark_node;
4659 /* Look for the optional `::' operator. */
4660 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4661 /* Look for the optional nested-name-specifier. */
4662 nested_name_specifier_p
4663 = (cp_parser_nested_name_specifier_opt (parser,
4664 /*typename_keyword_p=*/false,
4665 /*check_dependency_p=*/true,
4667 /*is_declaration=*/true)
4669 /* Now, if we saw a nested-name-specifier, we might be doing the
4670 second production. */
4671 if (nested_name_specifier_p
4672 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4674 /* Consume the `template' keyword. */
4675 cp_lexer_consume_token (parser->lexer);
4676 /* Parse the template-id. */
4677 cp_parser_template_id (parser,
4678 /*template_keyword_p=*/true,
4679 /*check_dependency_p=*/false,
4680 /*is_declaration=*/true);
4681 /* Look for the `::' token. */
4682 cp_parser_require (parser, CPP_SCOPE, "`::'");
4684 /* If the next token is not a `~', then there might be some
4685 additional qualification. */
4686 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4688 /* Look for the type-name. */
4689 *scope = TREE_TYPE (cp_parser_type_name (parser));
4691 if (*scope == error_mark_node)
4694 /* If we don't have ::~, then something has gone wrong. Since
4695 the only caller of this function is looking for something
4696 after `.' or `->' after a scalar type, most likely the
4697 program is trying to get a member of a non-aggregate
4699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4700 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4702 cp_parser_error (parser, "request for member of non-aggregate type");
4706 /* Look for the `::' token. */
4707 cp_parser_require (parser, CPP_SCOPE, "`::'");
4712 /* Look for the `~'. */
4713 cp_parser_require (parser, CPP_COMPL, "`~'");
4714 /* Look for the type-name again. We are not responsible for
4715 checking that it matches the first type-name. */
4716 *type = cp_parser_type_name (parser);
4719 /* Parse a unary-expression.
4725 unary-operator cast-expression
4726 sizeof unary-expression
4734 __extension__ cast-expression
4735 __alignof__ unary-expression
4736 __alignof__ ( type-id )
4737 __real__ cast-expression
4738 __imag__ cast-expression
4741 ADDRESS_P is true iff the unary-expression is appearing as the
4742 operand of the `&' operator. CAST_P is true if this expression is
4743 the target of a cast.
4745 Returns a representation of the expression. */
4748 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4751 enum tree_code unary_operator;
4753 /* Peek at the next token. */
4754 token = cp_lexer_peek_token (parser->lexer);
4755 /* Some keywords give away the kind of expression. */
4756 if (token->type == CPP_KEYWORD)
4758 enum rid keyword = token->keyword;
4768 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4769 /* Consume the token. */
4770 cp_lexer_consume_token (parser->lexer);
4771 /* Parse the operand. */
4772 operand = cp_parser_sizeof_operand (parser, keyword);
4774 if (TYPE_P (operand))
4775 return cxx_sizeof_or_alignof_type (operand, op, true);
4777 return cxx_sizeof_or_alignof_expr (operand, op);
4781 return cp_parser_new_expression (parser);
4784 return cp_parser_delete_expression (parser);
4788 /* The saved value of the PEDANTIC flag. */
4792 /* Save away the PEDANTIC flag. */
4793 cp_parser_extension_opt (parser, &saved_pedantic);
4794 /* Parse the cast-expression. */
4795 expr = cp_parser_simple_cast_expression (parser);
4796 /* Restore the PEDANTIC flag. */
4797 pedantic = saved_pedantic;
4807 /* Consume the `__real__' or `__imag__' token. */
4808 cp_lexer_consume_token (parser->lexer);
4809 /* Parse the cast-expression. */
4810 expression = cp_parser_simple_cast_expression (parser);
4811 /* Create the complete representation. */
4812 return build_x_unary_op ((keyword == RID_REALPART
4813 ? REALPART_EXPR : IMAGPART_EXPR),
4823 /* Look for the `:: new' and `:: delete', which also signal the
4824 beginning of a new-expression, or delete-expression,
4825 respectively. If the next token is `::', then it might be one of
4827 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4831 /* See if the token after the `::' is one of the keywords in
4832 which we're interested. */
4833 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4834 /* If it's `new', we have a new-expression. */
4835 if (keyword == RID_NEW)
4836 return cp_parser_new_expression (parser);
4837 /* Similarly, for `delete'. */
4838 else if (keyword == RID_DELETE)
4839 return cp_parser_delete_expression (parser);
4842 /* Look for a unary operator. */
4843 unary_operator = cp_parser_unary_operator (token);
4844 /* The `++' and `--' operators can be handled similarly, even though
4845 they are not technically unary-operators in the grammar. */
4846 if (unary_operator == ERROR_MARK)
4848 if (token->type == CPP_PLUS_PLUS)
4849 unary_operator = PREINCREMENT_EXPR;
4850 else if (token->type == CPP_MINUS_MINUS)
4851 unary_operator = PREDECREMENT_EXPR;
4852 /* Handle the GNU address-of-label extension. */
4853 else if (cp_parser_allow_gnu_extensions_p (parser)
4854 && token->type == CPP_AND_AND)
4858 /* Consume the '&&' token. */
4859 cp_lexer_consume_token (parser->lexer);
4860 /* Look for the identifier. */
4861 identifier = cp_parser_identifier (parser);
4862 /* Create an expression representing the address. */
4863 return finish_label_address_expr (identifier);
4866 if (unary_operator != ERROR_MARK)
4868 tree cast_expression;
4869 tree expression = error_mark_node;
4870 const char *non_constant_p = NULL;
4872 /* Consume the operator token. */
4873 token = cp_lexer_consume_token (parser->lexer);
4874 /* Parse the cast-expression. */
4876 = cp_parser_cast_expression (parser,
4877 unary_operator == ADDR_EXPR,
4879 /* Now, build an appropriate representation. */
4880 switch (unary_operator)
4883 non_constant_p = "`*'";
4884 expression = build_x_indirect_ref (cast_expression, "unary *");
4888 non_constant_p = "`&'";
4891 expression = build_x_unary_op (unary_operator, cast_expression);
4894 case PREINCREMENT_EXPR:
4895 case PREDECREMENT_EXPR:
4896 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4899 case UNARY_PLUS_EXPR:
4901 case TRUTH_NOT_EXPR:
4902 expression = finish_unary_op_expr (unary_operator, cast_expression);
4910 && cp_parser_non_integral_constant_expression (parser,
4912 expression = error_mark_node;
4917 return cp_parser_postfix_expression (parser, address_p, cast_p);
4920 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4921 unary-operator, the corresponding tree code is returned. */
4923 static enum tree_code
4924 cp_parser_unary_operator (cp_token* token)
4926 switch (token->type)
4929 return INDIRECT_REF;
4935 return UNARY_PLUS_EXPR;
4941 return TRUTH_NOT_EXPR;
4944 return BIT_NOT_EXPR;
4951 /* Parse a new-expression.
4954 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4955 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4957 Returns a representation of the expression. */
4960 cp_parser_new_expression (cp_parser* parser)
4962 bool global_scope_p;
4968 /* Look for the optional `::' operator. */
4970 = (cp_parser_global_scope_opt (parser,
4971 /*current_scope_valid_p=*/false)
4973 /* Look for the `new' operator. */
4974 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4975 /* There's no easy way to tell a new-placement from the
4976 `( type-id )' construct. */
4977 cp_parser_parse_tentatively (parser);
4978 /* Look for a new-placement. */
4979 placement = cp_parser_new_placement (parser);
4980 /* If that didn't work out, there's no new-placement. */
4981 if (!cp_parser_parse_definitely (parser))
4982 placement = NULL_TREE;
4984 /* If the next token is a `(', then we have a parenthesized
4986 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4988 /* Consume the `('. */
4989 cp_lexer_consume_token (parser->lexer);
4990 /* Parse the type-id. */
4991 type = cp_parser_type_id (parser);
4992 /* Look for the closing `)'. */
4993 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4994 /* There should not be a direct-new-declarator in this production,
4995 but GCC used to allowed this, so we check and emit a sensible error
4996 message for this case. */
4997 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4999 error ("array bound forbidden after parenthesized type-id");
5000 inform ("try removing the parentheses around the type-id");
5001 cp_parser_direct_new_declarator (parser);
5005 /* Otherwise, there must be a new-type-id. */
5007 type = cp_parser_new_type_id (parser, &nelts);
5009 /* If the next token is a `(', then we have a new-initializer. */
5010 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5011 initializer = cp_parser_new_initializer (parser);
5013 initializer = NULL_TREE;
5015 /* A new-expression may not appear in an integral constant
5017 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5018 return error_mark_node;
5020 /* Create a representation of the new-expression. */
5021 return build_new (placement, type, nelts, initializer, global_scope_p);
5024 /* Parse a new-placement.
5029 Returns the same representation as for an expression-list. */
5032 cp_parser_new_placement (cp_parser* parser)
5034 tree expression_list;
5036 /* Parse the expression-list. */
5037 expression_list = (cp_parser_parenthesized_expression_list
5038 (parser, false, /*cast_p=*/false,
5039 /*non_constant_p=*/NULL));
5041 return expression_list;
5044 /* Parse a new-type-id.
5047 type-specifier-seq new-declarator [opt]
5049 Returns the TYPE allocated. If the new-type-id indicates an array
5050 type, *NELTS is set to the number of elements in the last array
5051 bound; the TYPE will not include the last array bound. */
5054 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5056 cp_decl_specifier_seq type_specifier_seq;
5057 cp_declarator *new_declarator;
5058 cp_declarator *declarator;
5059 cp_declarator *outer_declarator;
5060 const char *saved_message;
5063 /* The type-specifier sequence must not contain type definitions.
5064 (It cannot contain declarations of new types either, but if they
5065 are not definitions we will catch that because they are not
5067 saved_message = parser->type_definition_forbidden_message;
5068 parser->type_definition_forbidden_message
5069 = "types may not be defined in a new-type-id";
5070 /* Parse the type-specifier-seq. */
5071 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5072 &type_specifier_seq);
5073 /* Restore the old message. */
5074 parser->type_definition_forbidden_message = saved_message;
5075 /* Parse the new-declarator. */
5076 new_declarator = cp_parser_new_declarator_opt (parser);
5078 /* Determine the number of elements in the last array dimension, if
5081 /* Skip down to the last array dimension. */
5082 declarator = new_declarator;
5083 outer_declarator = NULL;
5084 while (declarator && (declarator->kind == cdk_pointer
5085 || declarator->kind == cdk_ptrmem))
5087 outer_declarator = declarator;
5088 declarator = declarator->declarator;
5091 && declarator->kind == cdk_array
5092 && declarator->declarator
5093 && declarator->declarator->kind == cdk_array)
5095 outer_declarator = declarator;
5096 declarator = declarator->declarator;
5099 if (declarator && declarator->kind == cdk_array)
5101 *nelts = declarator->u.array.bounds;
5102 if (*nelts == error_mark_node)
5103 *nelts = integer_one_node;
5105 if (outer_declarator)
5106 outer_declarator->declarator = declarator->declarator;
5108 new_declarator = NULL;
5111 type = groktypename (&type_specifier_seq, new_declarator);
5112 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5114 *nelts = array_type_nelts_top (type);
5115 type = TREE_TYPE (type);
5120 /* Parse an (optional) new-declarator.
5123 ptr-operator new-declarator [opt]
5124 direct-new-declarator
5126 Returns the declarator. */
5128 static cp_declarator *
5129 cp_parser_new_declarator_opt (cp_parser* parser)
5131 enum tree_code code;
5133 cp_cv_quals cv_quals;
5135 /* We don't know if there's a ptr-operator next, or not. */
5136 cp_parser_parse_tentatively (parser);
5137 /* Look for a ptr-operator. */
5138 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5139 /* If that worked, look for more new-declarators. */
5140 if (cp_parser_parse_definitely (parser))
5142 cp_declarator *declarator;
5144 /* Parse another optional declarator. */
5145 declarator = cp_parser_new_declarator_opt (parser);
5147 /* Create the representation of the declarator. */
5149 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5150 else if (code == INDIRECT_REF)
5151 declarator = make_pointer_declarator (cv_quals, declarator);
5153 declarator = make_reference_declarator (cv_quals, declarator);
5158 /* If the next token is a `[', there is a direct-new-declarator. */
5159 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5160 return cp_parser_direct_new_declarator (parser);
5165 /* Parse a direct-new-declarator.
5167 direct-new-declarator:
5169 direct-new-declarator [constant-expression]
5173 static cp_declarator *
5174 cp_parser_direct_new_declarator (cp_parser* parser)
5176 cp_declarator *declarator = NULL;
5182 /* Look for the opening `['. */
5183 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5184 /* The first expression is not required to be constant. */
5187 expression = cp_parser_expression (parser, /*cast_p=*/false);
5188 /* The standard requires that the expression have integral
5189 type. DR 74 adds enumeration types. We believe that the
5190 real intent is that these expressions be handled like the
5191 expression in a `switch' condition, which also allows
5192 classes with a single conversion to integral or
5193 enumeration type. */
5194 if (!processing_template_decl)
5197 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5202 error ("expression in new-declarator must have integral "
5203 "or enumeration type");
5204 expression = error_mark_node;
5208 /* But all the other expressions must be. */
5211 = cp_parser_constant_expression (parser,
5212 /*allow_non_constant=*/false,
5214 /* Look for the closing `]'. */
5215 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5217 /* Add this bound to the declarator. */
5218 declarator = make_array_declarator (declarator, expression);
5220 /* If the next token is not a `[', then there are no more
5222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5229 /* Parse a new-initializer.
5232 ( expression-list [opt] )
5234 Returns a representation of the expression-list. If there is no
5235 expression-list, VOID_ZERO_NODE is returned. */
5238 cp_parser_new_initializer (cp_parser* parser)
5240 tree expression_list;
5242 expression_list = (cp_parser_parenthesized_expression_list
5243 (parser, false, /*cast_p=*/false,
5244 /*non_constant_p=*/NULL));
5245 if (!expression_list)
5246 expression_list = void_zero_node;
5248 return expression_list;
5251 /* Parse a delete-expression.
5254 :: [opt] delete cast-expression
5255 :: [opt] delete [ ] cast-expression
5257 Returns a representation of the expression. */
5260 cp_parser_delete_expression (cp_parser* parser)
5262 bool global_scope_p;
5266 /* Look for the optional `::' operator. */
5268 = (cp_parser_global_scope_opt (parser,
5269 /*current_scope_valid_p=*/false)
5271 /* Look for the `delete' keyword. */
5272 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5273 /* See if the array syntax is in use. */
5274 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5276 /* Consume the `[' token. */
5277 cp_lexer_consume_token (parser->lexer);
5278 /* Look for the `]' token. */
5279 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5280 /* Remember that this is the `[]' construct. */
5286 /* Parse the cast-expression. */
5287 expression = cp_parser_simple_cast_expression (parser);
5289 /* A delete-expression may not appear in an integral constant
5291 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5292 return error_mark_node;
5294 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5297 /* Parse a cast-expression.
5301 ( type-id ) cast-expression
5303 ADDRESS_P is true iff the unary-expression is appearing as the
5304 operand of the `&' operator. CAST_P is true if this expression is
5305 the target of a cast.
5307 Returns a representation of the expression. */
5310 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5312 /* If it's a `(', then we might be looking at a cast. */
5313 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5315 tree type = NULL_TREE;
5316 tree expr = NULL_TREE;
5317 bool compound_literal_p;
5318 const char *saved_message;
5320 /* There's no way to know yet whether or not this is a cast.
5321 For example, `(int (3))' is a unary-expression, while `(int)
5322 3' is a cast. So, we resort to parsing tentatively. */
5323 cp_parser_parse_tentatively (parser);
5324 /* Types may not be defined in a cast. */
5325 saved_message = parser->type_definition_forbidden_message;
5326 parser->type_definition_forbidden_message
5327 = "types may not be defined in casts";
5328 /* Consume the `('. */
5329 cp_lexer_consume_token (parser->lexer);
5330 /* A very tricky bit is that `(struct S) { 3 }' is a
5331 compound-literal (which we permit in C++ as an extension).
5332 But, that construct is not a cast-expression -- it is a
5333 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5334 is legal; if the compound-literal were a cast-expression,
5335 you'd need an extra set of parentheses.) But, if we parse
5336 the type-id, and it happens to be a class-specifier, then we
5337 will commit to the parse at that point, because we cannot
5338 undo the action that is done when creating a new class. So,
5339 then we cannot back up and do a postfix-expression.
5341 Therefore, we scan ahead to the closing `)', and check to see
5342 if the token after the `)' is a `{'. If so, we are not
5343 looking at a cast-expression.
5345 Save tokens so that we can put them back. */
5346 cp_lexer_save_tokens (parser->lexer);
5347 /* Skip tokens until the next token is a closing parenthesis.
5348 If we find the closing `)', and the next token is a `{', then
5349 we are looking at a compound-literal. */
5351 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5352 /*consume_paren=*/true)
5353 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5354 /* Roll back the tokens we skipped. */
5355 cp_lexer_rollback_tokens (parser->lexer);
5356 /* If we were looking at a compound-literal, simulate an error
5357 so that the call to cp_parser_parse_definitely below will
5359 if (compound_literal_p)
5360 cp_parser_simulate_error (parser);
5363 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5364 parser->in_type_id_in_expr_p = true;
5365 /* Look for the type-id. */
5366 type = cp_parser_type_id (parser);
5367 /* Look for the closing `)'. */
5368 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5369 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5372 /* Restore the saved message. */
5373 parser->type_definition_forbidden_message = saved_message;
5375 /* If ok so far, parse the dependent expression. We cannot be
5376 sure it is a cast. Consider `(T ())'. It is a parenthesized
5377 ctor of T, but looks like a cast to function returning T
5378 without a dependent expression. */
5379 if (!cp_parser_error_occurred (parser))
5380 expr = cp_parser_cast_expression (parser,
5381 /*address_p=*/false,
5384 if (cp_parser_parse_definitely (parser))
5386 /* Warn about old-style casts, if so requested. */
5387 if (warn_old_style_cast
5388 && !in_system_header
5389 && !VOID_TYPE_P (type)
5390 && current_lang_name != lang_name_c)
5391 warning (0, "use of old-style cast");
5393 /* Only type conversions to integral or enumeration types
5394 can be used in constant-expressions. */
5395 if (parser->integral_constant_expression_p
5396 && !dependent_type_p (type)
5397 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5398 && (cp_parser_non_integral_constant_expression
5400 "a cast to a type other than an integral or "
5401 "enumeration type")))
5402 return error_mark_node;
5404 /* Perform the cast. */
5405 expr = build_c_cast (type, expr);
5410 /* If we get here, then it's not a cast, so it must be a
5411 unary-expression. */
5412 return cp_parser_unary_expression (parser, address_p, cast_p);
5415 /* Parse a binary expression of the general form:
5419 pm-expression .* cast-expression
5420 pm-expression ->* cast-expression
5422 multiplicative-expression:
5424 multiplicative-expression * pm-expression
5425 multiplicative-expression / pm-expression
5426 multiplicative-expression % pm-expression
5428 additive-expression:
5429 multiplicative-expression
5430 additive-expression + multiplicative-expression
5431 additive-expression - multiplicative-expression
5435 shift-expression << additive-expression
5436 shift-expression >> additive-expression
5438 relational-expression:
5440 relational-expression < shift-expression
5441 relational-expression > shift-expression
5442 relational-expression <= shift-expression
5443 relational-expression >= shift-expression
5447 relational-expression:
5448 relational-expression <? shift-expression
5449 relational-expression >? shift-expression
5451 equality-expression:
5452 relational-expression
5453 equality-expression == relational-expression
5454 equality-expression != relational-expression
5458 and-expression & equality-expression
5460 exclusive-or-expression:
5462 exclusive-or-expression ^ and-expression
5464 inclusive-or-expression:
5465 exclusive-or-expression
5466 inclusive-or-expression | exclusive-or-expression
5468 logical-and-expression:
5469 inclusive-or-expression
5470 logical-and-expression && inclusive-or-expression
5472 logical-or-expression:
5473 logical-and-expression
5474 logical-or-expression || logical-and-expression
5476 All these are implemented with a single function like:
5479 simple-cast-expression
5480 binary-expression <token> binary-expression
5482 CAST_P is true if this expression is the target of a cast.
5484 The binops_by_token map is used to get the tree codes for each <token> type.
5485 binary-expressions are associated according to a precedence table. */
5487 #define TOKEN_PRECEDENCE(token) \
5488 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5489 ? PREC_NOT_OPERATOR \
5490 : binops_by_token[token->type].prec)
5493 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5495 cp_parser_expression_stack stack;
5496 cp_parser_expression_stack_entry *sp = &stack[0];
5499 enum tree_code tree_type;
5500 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5503 /* Parse the first expression. */
5504 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5508 /* Get an operator token. */
5509 token = cp_lexer_peek_token (parser->lexer);
5510 if (token->type == CPP_MIN || token->type == CPP_MAX)
5511 cp_parser_warn_min_max ();
5513 new_prec = TOKEN_PRECEDENCE (token);
5515 /* Popping an entry off the stack means we completed a subexpression:
5516 - either we found a token which is not an operator (`>' where it is not
5517 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5518 will happen repeatedly;
5519 - or, we found an operator which has lower priority. This is the case
5520 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5522 if (new_prec <= prec)
5531 tree_type = binops_by_token[token->type].tree_type;
5533 /* We used the operator token. */
5534 cp_lexer_consume_token (parser->lexer);
5536 /* Extract another operand. It may be the RHS of this expression
5537 or the LHS of a new, higher priority expression. */
5538 rhs = cp_parser_simple_cast_expression (parser);
5540 /* Get another operator token. Look up its precedence to avoid
5541 building a useless (immediately popped) stack entry for common
5542 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5543 token = cp_lexer_peek_token (parser->lexer);
5544 lookahead_prec = TOKEN_PRECEDENCE (token);
5545 if (lookahead_prec > new_prec)
5547 /* ... and prepare to parse the RHS of the new, higher priority
5548 expression. Since precedence levels on the stack are
5549 monotonically increasing, we do not have to care about
5552 sp->tree_type = tree_type;
5557 new_prec = lookahead_prec;
5561 /* If the stack is not empty, we have parsed into LHS the right side
5562 (`4' in the example above) of an expression we had suspended.
5563 We can use the information on the stack to recover the LHS (`3')
5564 from the stack together with the tree code (`MULT_EXPR'), and
5565 the precedence of the higher level subexpression
5566 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5567 which will be used to actually build the additive expression. */
5570 tree_type = sp->tree_type;
5575 overloaded_p = false;
5576 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5578 /* If the binary operator required the use of an overloaded operator,
5579 then this expression cannot be an integral constant-expression.
5580 An overloaded operator can be used even if both operands are
5581 otherwise permissible in an integral constant-expression if at
5582 least one of the operands is of enumeration type. */
5585 && (cp_parser_non_integral_constant_expression
5586 (parser, "calls to overloaded operators")))
5587 return error_mark_node;
5594 /* Parse the `? expression : assignment-expression' part of a
5595 conditional-expression. The LOGICAL_OR_EXPR is the
5596 logical-or-expression that started the conditional-expression.
5597 Returns a representation of the entire conditional-expression.
5599 This routine is used by cp_parser_assignment_expression.
5601 ? expression : assignment-expression
5605 ? : assignment-expression */
5608 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5611 tree assignment_expr;
5613 /* Consume the `?' token. */
5614 cp_lexer_consume_token (parser->lexer);
5615 if (cp_parser_allow_gnu_extensions_p (parser)
5616 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5617 /* Implicit true clause. */
5620 /* Parse the expression. */
5621 expr = cp_parser_expression (parser, /*cast_p=*/false);
5623 /* The next token should be a `:'. */
5624 cp_parser_require (parser, CPP_COLON, "`:'");
5625 /* Parse the assignment-expression. */
5626 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5628 /* Build the conditional-expression. */
5629 return build_x_conditional_expr (logical_or_expr,
5634 /* Parse an assignment-expression.
5636 assignment-expression:
5637 conditional-expression
5638 logical-or-expression assignment-operator assignment_expression
5641 CAST_P is true if this expression is the target of a cast.
5643 Returns a representation for the expression. */
5646 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5650 /* If the next token is the `throw' keyword, then we're looking at
5651 a throw-expression. */
5652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5653 expr = cp_parser_throw_expression (parser);
5654 /* Otherwise, it must be that we are looking at a
5655 logical-or-expression. */
5658 /* Parse the binary expressions (logical-or-expression). */
5659 expr = cp_parser_binary_expression (parser, cast_p);
5660 /* If the next token is a `?' then we're actually looking at a
5661 conditional-expression. */
5662 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5663 return cp_parser_question_colon_clause (parser, expr);
5666 enum tree_code assignment_operator;
5668 /* If it's an assignment-operator, we're using the second
5671 = cp_parser_assignment_operator_opt (parser);
5672 if (assignment_operator != ERROR_MARK)
5676 /* Parse the right-hand side of the assignment. */
5677 rhs = cp_parser_assignment_expression (parser, cast_p);
5678 /* An assignment may not appear in a
5679 constant-expression. */
5680 if (cp_parser_non_integral_constant_expression (parser,
5682 return error_mark_node;
5683 /* Build the assignment expression. */
5684 expr = build_x_modify_expr (expr,
5685 assignment_operator,
5694 /* Parse an (optional) assignment-operator.
5696 assignment-operator: one of
5697 = *= /= %= += -= >>= <<= &= ^= |=
5701 assignment-operator: one of
5704 If the next token is an assignment operator, the corresponding tree
5705 code is returned, and the token is consumed. For example, for
5706 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5707 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5708 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5709 operator, ERROR_MARK is returned. */
5711 static enum tree_code
5712 cp_parser_assignment_operator_opt (cp_parser* parser)
5717 /* Peek at the next toen. */
5718 token = cp_lexer_peek_token (parser->lexer);
5720 switch (token->type)
5731 op = TRUNC_DIV_EXPR;
5735 op = TRUNC_MOD_EXPR;
5768 cp_parser_warn_min_max ();
5773 cp_parser_warn_min_max ();
5777 /* Nothing else is an assignment operator. */
5781 /* If it was an assignment operator, consume it. */
5782 if (op != ERROR_MARK)
5783 cp_lexer_consume_token (parser->lexer);
5788 /* Parse an expression.
5791 assignment-expression
5792 expression , assignment-expression
5794 CAST_P is true if this expression is the target of a cast.
5796 Returns a representation of the expression. */
5799 cp_parser_expression (cp_parser* parser, bool cast_p)
5801 tree expression = NULL_TREE;
5805 tree assignment_expression;
5807 /* Parse the next assignment-expression. */
5808 assignment_expression
5809 = cp_parser_assignment_expression (parser, cast_p);
5810 /* If this is the first assignment-expression, we can just
5813 expression = assignment_expression;
5815 expression = build_x_compound_expr (expression,
5816 assignment_expression);
5817 /* If the next token is not a comma, then we are done with the
5819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5821 /* Consume the `,'. */
5822 cp_lexer_consume_token (parser->lexer);
5823 /* A comma operator cannot appear in a constant-expression. */
5824 if (cp_parser_non_integral_constant_expression (parser,
5825 "a comma operator"))
5826 expression = error_mark_node;
5832 /* Parse a constant-expression.
5834 constant-expression:
5835 conditional-expression
5837 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5838 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5839 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5840 is false, NON_CONSTANT_P should be NULL. */
5843 cp_parser_constant_expression (cp_parser* parser,
5844 bool allow_non_constant_p,
5845 bool *non_constant_p)
5847 bool saved_integral_constant_expression_p;
5848 bool saved_allow_non_integral_constant_expression_p;
5849 bool saved_non_integral_constant_expression_p;
5852 /* It might seem that we could simply parse the
5853 conditional-expression, and then check to see if it were
5854 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5855 one that the compiler can figure out is constant, possibly after
5856 doing some simplifications or optimizations. The standard has a
5857 precise definition of constant-expression, and we must honor
5858 that, even though it is somewhat more restrictive.
5864 is not a legal declaration, because `(2, 3)' is not a
5865 constant-expression. The `,' operator is forbidden in a
5866 constant-expression. However, GCC's constant-folding machinery
5867 will fold this operation to an INTEGER_CST for `3'. */
5869 /* Save the old settings. */
5870 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5871 saved_allow_non_integral_constant_expression_p
5872 = parser->allow_non_integral_constant_expression_p;
5873 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5874 /* We are now parsing a constant-expression. */
5875 parser->integral_constant_expression_p = true;
5876 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5877 parser->non_integral_constant_expression_p = false;
5878 /* Although the grammar says "conditional-expression", we parse an
5879 "assignment-expression", which also permits "throw-expression"
5880 and the use of assignment operators. In the case that
5881 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5882 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5883 actually essential that we look for an assignment-expression.
5884 For example, cp_parser_initializer_clauses uses this function to
5885 determine whether a particular assignment-expression is in fact
5887 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5888 /* Restore the old settings. */
5889 parser->integral_constant_expression_p
5890 = saved_integral_constant_expression_p;
5891 parser->allow_non_integral_constant_expression_p
5892 = saved_allow_non_integral_constant_expression_p;
5893 if (allow_non_constant_p)
5894 *non_constant_p = parser->non_integral_constant_expression_p;
5895 else if (parser->non_integral_constant_expression_p)
5896 expression = error_mark_node;
5897 parser->non_integral_constant_expression_p
5898 = saved_non_integral_constant_expression_p;
5903 /* Parse __builtin_offsetof.
5905 offsetof-expression:
5906 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5908 offsetof-member-designator:
5910 | offsetof-member-designator "." id-expression
5911 | offsetof-member-designator "[" expression "]"
5915 cp_parser_builtin_offsetof (cp_parser *parser)
5917 int save_ice_p, save_non_ice_p;
5921 /* We're about to accept non-integral-constant things, but will
5922 definitely yield an integral constant expression. Save and
5923 restore these values around our local parsing. */
5924 save_ice_p = parser->integral_constant_expression_p;
5925 save_non_ice_p = parser->non_integral_constant_expression_p;
5927 /* Consume the "__builtin_offsetof" token. */
5928 cp_lexer_consume_token (parser->lexer);
5929 /* Consume the opening `('. */
5930 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5931 /* Parse the type-id. */
5932 type = cp_parser_type_id (parser);
5933 /* Look for the `,'. */
5934 cp_parser_require (parser, CPP_COMMA, "`,'");
5936 /* Build the (type *)null that begins the traditional offsetof macro. */
5937 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5939 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5940 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5944 cp_token *token = cp_lexer_peek_token (parser->lexer);
5945 switch (token->type)
5947 case CPP_OPEN_SQUARE:
5948 /* offsetof-member-designator "[" expression "]" */
5949 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5953 /* offsetof-member-designator "." identifier */
5954 cp_lexer_consume_token (parser->lexer);
5955 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5959 case CPP_CLOSE_PAREN:
5960 /* Consume the ")" token. */
5961 cp_lexer_consume_token (parser->lexer);
5965 /* Error. We know the following require will fail, but
5966 that gives the proper error message. */
5967 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5968 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5969 expr = error_mark_node;
5975 /* If we're processing a template, we can't finish the semantics yet.
5976 Otherwise we can fold the entire expression now. */
5977 if (processing_template_decl)
5978 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5980 expr = fold_offsetof (expr);
5983 parser->integral_constant_expression_p = save_ice_p;
5984 parser->non_integral_constant_expression_p = save_non_ice_p;
5989 /* Statements [gram.stmt.stmt] */
5991 /* Parse a statement.
5995 expression-statement
6000 declaration-statement
6004 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6008 location_t statement_location;
6010 /* There is no statement yet. */
6011 statement = NULL_TREE;
6012 /* Peek at the next token. */
6013 token = cp_lexer_peek_token (parser->lexer);
6014 /* Remember the location of the first token in the statement. */
6015 statement_location = token->location;
6016 /* If this is a keyword, then that will often determine what kind of
6017 statement we have. */
6018 if (token->type == CPP_KEYWORD)
6020 enum rid keyword = token->keyword;
6026 statement = cp_parser_labeled_statement (parser,
6032 statement = cp_parser_selection_statement (parser);
6038 statement = cp_parser_iteration_statement (parser);
6045 statement = cp_parser_jump_statement (parser);
6048 /* Objective-C++ exception-handling constructs. */
6051 case RID_AT_FINALLY:
6052 case RID_AT_SYNCHRONIZED:
6054 statement = cp_parser_objc_statement (parser);
6058 statement = cp_parser_try_block (parser);
6062 /* It might be a keyword like `int' that can start a
6063 declaration-statement. */
6067 else if (token->type == CPP_NAME)
6069 /* If the next token is a `:', then we are looking at a
6070 labeled-statement. */
6071 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6072 if (token->type == CPP_COLON)
6073 statement = cp_parser_labeled_statement (parser, in_statement_expr);
6075 /* Anything that starts with a `{' must be a compound-statement. */
6076 else if (token->type == CPP_OPEN_BRACE)
6077 statement = cp_parser_compound_statement (parser, NULL, false);
6078 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6079 a statement all its own. */
6080 else if (token->type == CPP_PRAGMA)
6082 cp_lexer_handle_pragma (parser->lexer);
6085 else if (token->type == CPP_EOF)
6087 cp_parser_error (parser, "expected statement");
6091 /* Everything else must be a declaration-statement or an
6092 expression-statement. Try for the declaration-statement
6093 first, unless we are looking at a `;', in which case we know that
6094 we have an expression-statement. */
6097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6099 cp_parser_parse_tentatively (parser);
6100 /* Try to parse the declaration-statement. */
6101 cp_parser_declaration_statement (parser);
6102 /* If that worked, we're done. */
6103 if (cp_parser_parse_definitely (parser))
6106 /* Look for an expression-statement instead. */
6107 statement = cp_parser_expression_statement (parser, in_statement_expr);
6110 /* Set the line number for the statement. */
6111 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6112 SET_EXPR_LOCATION (statement, statement_location);
6115 /* Parse a labeled-statement.
6118 identifier : statement
6119 case constant-expression : statement
6125 case constant-expression ... constant-expression : statement
6127 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6128 For an ordinary label, returns a LABEL_EXPR. */
6131 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6134 tree statement = error_mark_node;
6136 /* The next token should be an identifier. */
6137 token = cp_lexer_peek_token (parser->lexer);
6138 if (token->type != CPP_NAME
6139 && token->type != CPP_KEYWORD)
6141 cp_parser_error (parser, "expected labeled-statement");
6142 return error_mark_node;
6145 switch (token->keyword)
6152 /* Consume the `case' token. */
6153 cp_lexer_consume_token (parser->lexer);
6154 /* Parse the constant-expression. */
6155 expr = cp_parser_constant_expression (parser,
6156 /*allow_non_constant_p=*/false,
6159 ellipsis = cp_lexer_peek_token (parser->lexer);
6160 if (ellipsis->type == CPP_ELLIPSIS)
6162 /* Consume the `...' token. */
6163 cp_lexer_consume_token (parser->lexer);
6165 cp_parser_constant_expression (parser,
6166 /*allow_non_constant_p=*/false,
6168 /* We don't need to emit warnings here, as the common code
6169 will do this for us. */
6172 expr_hi = NULL_TREE;
6174 if (!parser->in_switch_statement_p)
6175 error ("case label %qE not within a switch statement", expr);
6177 statement = finish_case_label (expr, expr_hi);
6182 /* Consume the `default' token. */
6183 cp_lexer_consume_token (parser->lexer);
6184 if (!parser->in_switch_statement_p)
6185 error ("case label not within a switch statement");
6187 statement = finish_case_label (NULL_TREE, NULL_TREE);
6191 /* Anything else must be an ordinary label. */
6192 statement = finish_label_stmt (cp_parser_identifier (parser));
6196 /* Require the `:' token. */
6197 cp_parser_require (parser, CPP_COLON, "`:'");
6198 /* Parse the labeled statement. */
6199 cp_parser_statement (parser, in_statement_expr);
6201 /* Return the label, in the case of a `case' or `default' label. */
6205 /* Parse an expression-statement.
6207 expression-statement:
6210 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6211 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6212 indicates whether this expression-statement is part of an
6213 expression statement. */
6216 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6218 tree statement = NULL_TREE;
6220 /* If the next token is a ';', then there is no expression
6222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6223 statement = cp_parser_expression (parser, /*cast_p=*/false);
6225 /* Consume the final `;'. */
6226 cp_parser_consume_semicolon_at_end_of_statement (parser);
6228 if (in_statement_expr
6229 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6230 /* This is the final expression statement of a statement
6232 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6234 statement = finish_expr_stmt (statement);
6241 /* Parse a compound-statement.
6244 { statement-seq [opt] }
6246 Returns a tree representing the statement. */
6249 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6254 /* Consume the `{'. */
6255 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6256 return error_mark_node;
6257 /* Begin the compound-statement. */
6258 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6259 /* Parse an (optional) statement-seq. */
6260 cp_parser_statement_seq_opt (parser, in_statement_expr);
6261 /* Finish the compound-statement. */
6262 finish_compound_stmt (compound_stmt);
6263 /* Consume the `}'. */
6264 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6266 return compound_stmt;
6269 /* Parse an (optional) statement-seq.
6273 statement-seq [opt] statement */
6276 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6278 /* Scan statements until there aren't any more. */
6281 /* If we're looking at a `}', then we've run out of statements. */
6282 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6283 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6286 /* Parse the statement. */
6287 cp_parser_statement (parser, in_statement_expr);
6291 /* Parse a selection-statement.
6293 selection-statement:
6294 if ( condition ) statement
6295 if ( condition ) statement else statement
6296 switch ( condition ) statement
6298 Returns the new IF_STMT or SWITCH_STMT. */
6301 cp_parser_selection_statement (cp_parser* parser)
6306 /* Peek at the next token. */
6307 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6309 /* See what kind of keyword it is. */
6310 keyword = token->keyword;
6319 /* Look for the `('. */
6320 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6322 cp_parser_skip_to_end_of_statement (parser);
6323 return error_mark_node;
6326 /* Begin the selection-statement. */
6327 if (keyword == RID_IF)
6328 statement = begin_if_stmt ();
6330 statement = begin_switch_stmt ();
6332 /* Parse the condition. */
6333 condition = cp_parser_condition (parser);
6334 /* Look for the `)'. */
6335 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6336 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6337 /*consume_paren=*/true);
6339 if (keyword == RID_IF)
6341 /* Add the condition. */
6342 finish_if_stmt_cond (condition, statement);
6344 /* Parse the then-clause. */
6345 cp_parser_implicitly_scoped_statement (parser);
6346 finish_then_clause (statement);
6348 /* If the next token is `else', parse the else-clause. */
6349 if (cp_lexer_next_token_is_keyword (parser->lexer,
6352 /* Consume the `else' keyword. */
6353 cp_lexer_consume_token (parser->lexer);
6354 begin_else_clause (statement);
6355 /* Parse the else-clause. */
6356 cp_parser_implicitly_scoped_statement (parser);
6357 finish_else_clause (statement);
6360 /* Now we're all done with the if-statement. */
6361 finish_if_stmt (statement);
6365 bool in_switch_statement_p;
6367 /* Add the condition. */
6368 finish_switch_cond (condition, statement);
6370 /* Parse the body of the switch-statement. */
6371 in_switch_statement_p = parser->in_switch_statement_p;
6372 parser->in_switch_statement_p = true;
6373 cp_parser_implicitly_scoped_statement (parser);
6374 parser->in_switch_statement_p = in_switch_statement_p;
6376 /* Now we're all done with the switch-statement. */
6377 finish_switch_stmt (statement);
6385 cp_parser_error (parser, "expected selection-statement");
6386 return error_mark_node;
6390 /* Parse a condition.
6394 type-specifier-seq declarator = assignment-expression
6399 type-specifier-seq declarator asm-specification [opt]
6400 attributes [opt] = assignment-expression
6402 Returns the expression that should be tested. */
6405 cp_parser_condition (cp_parser* parser)
6407 cp_decl_specifier_seq type_specifiers;
6408 const char *saved_message;
6410 /* Try the declaration first. */
6411 cp_parser_parse_tentatively (parser);
6412 /* New types are not allowed in the type-specifier-seq for a
6414 saved_message = parser->type_definition_forbidden_message;
6415 parser->type_definition_forbidden_message
6416 = "types may not be defined in conditions";
6417 /* Parse the type-specifier-seq. */
6418 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6420 /* Restore the saved message. */
6421 parser->type_definition_forbidden_message = saved_message;
6422 /* If all is well, we might be looking at a declaration. */
6423 if (!cp_parser_error_occurred (parser))
6426 tree asm_specification;
6428 cp_declarator *declarator;
6429 tree initializer = NULL_TREE;
6431 /* Parse the declarator. */
6432 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6433 /*ctor_dtor_or_conv_p=*/NULL,
6434 /*parenthesized_p=*/NULL,
6435 /*member_p=*/false);
6436 /* Parse the attributes. */
6437 attributes = cp_parser_attributes_opt (parser);
6438 /* Parse the asm-specification. */
6439 asm_specification = cp_parser_asm_specification_opt (parser);
6440 /* If the next token is not an `=', then we might still be
6441 looking at an expression. For example:
6445 looks like a decl-specifier-seq and a declarator -- but then
6446 there is no `=', so this is an expression. */
6447 cp_parser_require (parser, CPP_EQ, "`='");
6448 /* If we did see an `=', then we are looking at a declaration
6450 if (cp_parser_parse_definitely (parser))
6454 /* Create the declaration. */
6455 decl = start_decl (declarator, &type_specifiers,
6456 /*initialized_p=*/true,
6457 attributes, /*prefix_attributes=*/NULL_TREE,
6459 /* Parse the assignment-expression. */
6460 initializer = cp_parser_assignment_expression (parser,
6463 /* Process the initializer. */
6464 cp_finish_decl (decl,
6467 LOOKUP_ONLYCONVERTING);
6470 pop_scope (pushed_scope);
6472 return convert_from_reference (decl);
6475 /* If we didn't even get past the declarator successfully, we are
6476 definitely not looking at a declaration. */
6478 cp_parser_abort_tentative_parse (parser);
6480 /* Otherwise, we are looking at an expression. */
6481 return cp_parser_expression (parser, /*cast_p=*/false);
6484 /* Parse an iteration-statement.
6486 iteration-statement:
6487 while ( condition ) statement
6488 do statement while ( expression ) ;
6489 for ( for-init-statement condition [opt] ; expression [opt] )
6492 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6495 cp_parser_iteration_statement (cp_parser* parser)
6500 bool in_iteration_statement_p;
6503 /* Peek at the next token. */
6504 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6506 return error_mark_node;
6508 /* Remember whether or not we are already within an iteration
6510 in_iteration_statement_p = parser->in_iteration_statement_p;
6512 /* See what kind of keyword it is. */
6513 keyword = token->keyword;
6520 /* Begin the while-statement. */
6521 statement = begin_while_stmt ();
6522 /* Look for the `('. */
6523 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6524 /* Parse the condition. */
6525 condition = cp_parser_condition (parser);
6526 finish_while_stmt_cond (condition, statement);
6527 /* Look for the `)'. */
6528 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6529 /* Parse the dependent statement. */
6530 parser->in_iteration_statement_p = true;
6531 cp_parser_already_scoped_statement (parser);
6532 parser->in_iteration_statement_p = in_iteration_statement_p;
6533 /* We're done with the while-statement. */
6534 finish_while_stmt (statement);
6542 /* Begin the do-statement. */
6543 statement = begin_do_stmt ();
6544 /* Parse the body of the do-statement. */
6545 parser->in_iteration_statement_p = true;
6546 cp_parser_implicitly_scoped_statement (parser);
6547 parser->in_iteration_statement_p = in_iteration_statement_p;
6548 finish_do_body (statement);
6549 /* Look for the `while' keyword. */
6550 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6551 /* Look for the `('. */
6552 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6553 /* Parse the expression. */
6554 expression = cp_parser_expression (parser, /*cast_p=*/false);
6555 /* We're done with the do-statement. */
6556 finish_do_stmt (expression, statement);
6557 /* Look for the `)'. */
6558 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6559 /* Look for the `;'. */
6560 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6566 tree condition = NULL_TREE;
6567 tree expression = NULL_TREE;
6569 /* Begin the for-statement. */
6570 statement = begin_for_stmt ();
6571 /* Look for the `('. */
6572 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6573 /* Parse the initialization. */
6574 cp_parser_for_init_statement (parser);
6575 finish_for_init_stmt (statement);
6577 /* If there's a condition, process it. */
6578 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6579 condition = cp_parser_condition (parser);
6580 finish_for_cond (condition, statement);
6581 /* Look for the `;'. */
6582 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6584 /* If there's an expression, process it. */
6585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6586 expression = cp_parser_expression (parser, /*cast_p=*/false);
6587 finish_for_expr (expression, statement);
6588 /* Look for the `)'. */
6589 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6591 /* Parse the body of the for-statement. */
6592 parser->in_iteration_statement_p = true;
6593 cp_parser_already_scoped_statement (parser);
6594 parser->in_iteration_statement_p = in_iteration_statement_p;
6596 /* We're done with the for-statement. */
6597 finish_for_stmt (statement);
6602 cp_parser_error (parser, "expected iteration-statement");
6603 statement = error_mark_node;
6610 /* Parse a for-init-statement.
6613 expression-statement
6614 simple-declaration */
6617 cp_parser_for_init_statement (cp_parser* parser)
6619 /* If the next token is a `;', then we have an empty
6620 expression-statement. Grammatically, this is also a
6621 simple-declaration, but an invalid one, because it does not
6622 declare anything. Therefore, if we did not handle this case
6623 specially, we would issue an error message about an invalid
6625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6627 /* We're going to speculatively look for a declaration, falling back
6628 to an expression, if necessary. */
6629 cp_parser_parse_tentatively (parser);
6630 /* Parse the declaration. */
6631 cp_parser_simple_declaration (parser,
6632 /*function_definition_allowed_p=*/false);
6633 /* If the tentative parse failed, then we shall need to look for an
6634 expression-statement. */
6635 if (cp_parser_parse_definitely (parser))
6639 cp_parser_expression_statement (parser, false);
6642 /* Parse a jump-statement.
6647 return expression [opt] ;
6655 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6658 cp_parser_jump_statement (cp_parser* parser)
6660 tree statement = error_mark_node;
6664 /* Peek at the next token. */
6665 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6667 return error_mark_node;
6669 /* See what kind of keyword it is. */
6670 keyword = token->keyword;
6674 if (!parser->in_switch_statement_p
6675 && !parser->in_iteration_statement_p)
6677 error ("break statement not within loop or switch");
6678 statement = error_mark_node;
6681 statement = finish_break_stmt ();
6682 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6686 if (!parser->in_iteration_statement_p)
6688 error ("continue statement not within a loop");
6689 statement = error_mark_node;
6692 statement = finish_continue_stmt ();
6693 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6700 /* If the next token is a `;', then there is no
6702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6703 expr = cp_parser_expression (parser, /*cast_p=*/false);
6706 /* Build the return-statement. */
6707 statement = finish_return_stmt (expr);
6708 /* Look for the final `;'. */
6709 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6714 /* Create the goto-statement. */
6715 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6717 /* Issue a warning about this use of a GNU extension. */
6719 pedwarn ("ISO C++ forbids computed gotos");
6720 /* Consume the '*' token. */
6721 cp_lexer_consume_token (parser->lexer);
6722 /* Parse the dependent expression. */
6723 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6726 finish_goto_stmt (cp_parser_identifier (parser));
6727 /* Look for the final `;'. */
6728 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6732 cp_parser_error (parser, "expected jump-statement");
6739 /* Parse a declaration-statement.
6741 declaration-statement:
6742 block-declaration */
6745 cp_parser_declaration_statement (cp_parser* parser)
6749 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6750 p = obstack_alloc (&declarator_obstack, 0);
6752 /* Parse the block-declaration. */
6753 cp_parser_block_declaration (parser, /*statement_p=*/true);
6755 /* Free any declarators allocated. */
6756 obstack_free (&declarator_obstack, p);
6758 /* Finish off the statement. */
6762 /* Some dependent statements (like `if (cond) statement'), are
6763 implicitly in their own scope. In other words, if the statement is
6764 a single statement (as opposed to a compound-statement), it is
6765 none-the-less treated as if it were enclosed in braces. Any
6766 declarations appearing in the dependent statement are out of scope
6767 after control passes that point. This function parses a statement,
6768 but ensures that is in its own scope, even if it is not a
6771 Returns the new statement. */
6774 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6778 /* If the token is not a `{', then we must take special action. */
6779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6781 /* Create a compound-statement. */
6782 statement = begin_compound_stmt (0);
6783 /* Parse the dependent-statement. */
6784 cp_parser_statement (parser, false);
6785 /* Finish the dummy compound-statement. */
6786 finish_compound_stmt (statement);
6788 /* Otherwise, we simply parse the statement directly. */
6790 statement = cp_parser_compound_statement (parser, NULL, false);
6792 /* Return the statement. */
6796 /* For some dependent statements (like `while (cond) statement'), we
6797 have already created a scope. Therefore, even if the dependent
6798 statement is a compound-statement, we do not want to create another
6802 cp_parser_already_scoped_statement (cp_parser* parser)
6804 /* If the token is a `{', then we must take special action. */
6805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6806 cp_parser_statement (parser, false);
6809 /* Avoid calling cp_parser_compound_statement, so that we
6810 don't create a new scope. Do everything else by hand. */
6811 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6812 cp_parser_statement_seq_opt (parser, false);
6813 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6817 /* Declarations [gram.dcl.dcl] */
6819 /* Parse an optional declaration-sequence.
6823 declaration-seq declaration */
6826 cp_parser_declaration_seq_opt (cp_parser* parser)
6832 token = cp_lexer_peek_token (parser->lexer);
6834 if (token->type == CPP_CLOSE_BRACE
6835 || token->type == CPP_EOF)
6838 if (token->type == CPP_SEMICOLON)
6840 /* A declaration consisting of a single semicolon is
6841 invalid. Allow it unless we're being pedantic. */
6842 cp_lexer_consume_token (parser->lexer);
6843 if (pedantic && !in_system_header)
6844 pedwarn ("extra %<;%>");
6848 /* If we're entering or exiting a region that's implicitly
6849 extern "C", modify the lang context appropriately. */
6850 if (!parser->implicit_extern_c && token->implicit_extern_c)
6852 push_lang_context (lang_name_c);
6853 parser->implicit_extern_c = true;
6855 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6857 pop_lang_context ();
6858 parser->implicit_extern_c = false;
6861 if (token->type == CPP_PRAGMA)
6863 /* A top-level declaration can consist solely of a #pragma.
6864 A nested declaration cannot, so this is done here and not
6865 in cp_parser_declaration. (A #pragma at block scope is
6866 handled in cp_parser_statement.) */
6867 cp_lexer_handle_pragma (parser->lexer);
6871 /* Parse the declaration itself. */
6872 cp_parser_declaration (parser);
6876 /* Parse a declaration.
6881 template-declaration
6882 explicit-instantiation
6883 explicit-specialization
6884 linkage-specification
6885 namespace-definition
6890 __extension__ declaration */
6893 cp_parser_declaration (cp_parser* parser)
6900 /* Check for the `__extension__' keyword. */
6901 if (cp_parser_extension_opt (parser, &saved_pedantic))
6903 /* Parse the qualified declaration. */
6904 cp_parser_declaration (parser);
6905 /* Restore the PEDANTIC flag. */
6906 pedantic = saved_pedantic;
6911 /* Try to figure out what kind of declaration is present. */
6912 token1 = *cp_lexer_peek_token (parser->lexer);
6914 if (token1.type != CPP_EOF)
6915 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6918 token2.type = CPP_EOF;
6919 token2.keyword = RID_MAX;
6922 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6923 p = obstack_alloc (&declarator_obstack, 0);
6925 /* If the next token is `extern' and the following token is a string
6926 literal, then we have a linkage specification. */
6927 if (token1.keyword == RID_EXTERN
6928 && cp_parser_is_string_literal (&token2))
6929 cp_parser_linkage_specification (parser);
6930 /* If the next token is `template', then we have either a template
6931 declaration, an explicit instantiation, or an explicit
6933 else if (token1.keyword == RID_TEMPLATE)
6935 /* `template <>' indicates a template specialization. */
6936 if (token2.type == CPP_LESS
6937 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6938 cp_parser_explicit_specialization (parser);
6939 /* `template <' indicates a template declaration. */
6940 else if (token2.type == CPP_LESS)
6941 cp_parser_template_declaration (parser, /*member_p=*/false);
6942 /* Anything else must be an explicit instantiation. */
6944 cp_parser_explicit_instantiation (parser);
6946 /* If the next token is `export', then we have a template
6948 else if (token1.keyword == RID_EXPORT)
6949 cp_parser_template_declaration (parser, /*member_p=*/false);
6950 /* If the next token is `extern', 'static' or 'inline' and the one
6951 after that is `template', we have a GNU extended explicit
6952 instantiation directive. */
6953 else if (cp_parser_allow_gnu_extensions_p (parser)
6954 && (token1.keyword == RID_EXTERN
6955 || token1.keyword == RID_STATIC
6956 || token1.keyword == RID_INLINE)
6957 && token2.keyword == RID_TEMPLATE)
6958 cp_parser_explicit_instantiation (parser);
6959 /* If the next token is `namespace', check for a named or unnamed
6960 namespace definition. */
6961 else if (token1.keyword == RID_NAMESPACE
6962 && (/* A named namespace definition. */
6963 (token2.type == CPP_NAME
6964 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6966 /* An unnamed namespace definition. */
6967 || token2.type == CPP_OPEN_BRACE))
6968 cp_parser_namespace_definition (parser);
6969 /* Objective-C++ declaration/definition. */
6970 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6971 cp_parser_objc_declaration (parser);
6972 /* We must have either a block declaration or a function
6975 /* Try to parse a block-declaration, or a function-definition. */
6976 cp_parser_block_declaration (parser, /*statement_p=*/false);
6978 /* Free any declarators allocated. */
6979 obstack_free (&declarator_obstack, p);
6982 /* Parse a block-declaration.
6987 namespace-alias-definition
6994 __extension__ block-declaration
6997 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6998 part of a declaration-statement. */
7001 cp_parser_block_declaration (cp_parser *parser,
7007 /* Check for the `__extension__' keyword. */
7008 if (cp_parser_extension_opt (parser, &saved_pedantic))
7010 /* Parse the qualified declaration. */
7011 cp_parser_block_declaration (parser, statement_p);
7012 /* Restore the PEDANTIC flag. */
7013 pedantic = saved_pedantic;
7018 /* Peek at the next token to figure out which kind of declaration is
7020 token1 = cp_lexer_peek_token (parser->lexer);
7022 /* If the next keyword is `asm', we have an asm-definition. */
7023 if (token1->keyword == RID_ASM)
7026 cp_parser_commit_to_tentative_parse (parser);
7027 cp_parser_asm_definition (parser);
7029 /* If the next keyword is `namespace', we have a
7030 namespace-alias-definition. */
7031 else if (token1->keyword == RID_NAMESPACE)
7032 cp_parser_namespace_alias_definition (parser);
7033 /* If the next keyword is `using', we have either a
7034 using-declaration or a using-directive. */
7035 else if (token1->keyword == RID_USING)
7040 cp_parser_commit_to_tentative_parse (parser);
7041 /* If the token after `using' is `namespace', then we have a
7043 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7044 if (token2->keyword == RID_NAMESPACE)
7045 cp_parser_using_directive (parser);
7046 /* Otherwise, it's a using-declaration. */
7048 cp_parser_using_declaration (parser);
7050 /* If the next keyword is `__label__' we have a label declaration. */
7051 else if (token1->keyword == RID_LABEL)
7054 cp_parser_commit_to_tentative_parse (parser);
7055 cp_parser_label_declaration (parser);
7057 /* Anything else must be a simple-declaration. */
7059 cp_parser_simple_declaration (parser, !statement_p);
7062 /* Parse a simple-declaration.
7065 decl-specifier-seq [opt] init-declarator-list [opt] ;
7067 init-declarator-list:
7069 init-declarator-list , init-declarator
7071 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7072 function-definition as a simple-declaration. */
7075 cp_parser_simple_declaration (cp_parser* parser,
7076 bool function_definition_allowed_p)
7078 cp_decl_specifier_seq decl_specifiers;
7079 int declares_class_or_enum;
7080 bool saw_declarator;
7082 /* Defer access checks until we know what is being declared; the
7083 checks for names appearing in the decl-specifier-seq should be
7084 done as if we were in the scope of the thing being declared. */
7085 push_deferring_access_checks (dk_deferred);
7087 /* Parse the decl-specifier-seq. We have to keep track of whether
7088 or not the decl-specifier-seq declares a named class or
7089 enumeration type, since that is the only case in which the
7090 init-declarator-list is allowed to be empty.
7094 In a simple-declaration, the optional init-declarator-list can be
7095 omitted only when declaring a class or enumeration, that is when
7096 the decl-specifier-seq contains either a class-specifier, an
7097 elaborated-type-specifier, or an enum-specifier. */
7098 cp_parser_decl_specifier_seq (parser,
7099 CP_PARSER_FLAGS_OPTIONAL,
7101 &declares_class_or_enum);
7102 /* We no longer need to defer access checks. */
7103 stop_deferring_access_checks ();
7105 /* In a block scope, a valid declaration must always have a
7106 decl-specifier-seq. By not trying to parse declarators, we can
7107 resolve the declaration/expression ambiguity more quickly. */
7108 if (!function_definition_allowed_p
7109 && !decl_specifiers.any_specifiers_p)
7111 cp_parser_error (parser, "expected declaration");
7115 /* If the next two tokens are both identifiers, the code is
7116 erroneous. The usual cause of this situation is code like:
7120 where "T" should name a type -- but does not. */
7121 if (!decl_specifiers.type
7122 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7124 /* If parsing tentatively, we should commit; we really are
7125 looking at a declaration. */
7126 cp_parser_commit_to_tentative_parse (parser);
7131 /* If we have seen at least one decl-specifier, and the next token
7132 is not a parenthesis, then we must be looking at a declaration.
7133 (After "int (" we might be looking at a functional cast.) */
7134 if (decl_specifiers.any_specifiers_p
7135 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7136 cp_parser_commit_to_tentative_parse (parser);
7138 /* Keep going until we hit the `;' at the end of the simple
7140 saw_declarator = false;
7141 while (cp_lexer_next_token_is_not (parser->lexer,
7145 bool function_definition_p;
7150 /* If we are processing next declarator, coma is expected */
7151 token = cp_lexer_peek_token (parser->lexer);
7152 gcc_assert (token->type == CPP_COMMA);
7153 cp_lexer_consume_token (parser->lexer);
7156 saw_declarator = true;
7158 /* Parse the init-declarator. */
7159 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7160 function_definition_allowed_p,
7162 declares_class_or_enum,
7163 &function_definition_p);
7164 /* If an error occurred while parsing tentatively, exit quickly.
7165 (That usually happens when in the body of a function; each
7166 statement is treated as a declaration-statement until proven
7168 if (cp_parser_error_occurred (parser))
7170 /* Handle function definitions specially. */
7171 if (function_definition_p)
7173 /* If the next token is a `,', then we are probably
7174 processing something like:
7178 which is erroneous. */
7179 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7180 error ("mixing declarations and function-definitions is forbidden");
7181 /* Otherwise, we're done with the list of declarators. */
7184 pop_deferring_access_checks ();
7188 /* The next token should be either a `,' or a `;'. */
7189 token = cp_lexer_peek_token (parser->lexer);
7190 /* If it's a `,', there are more declarators to come. */
7191 if (token->type == CPP_COMMA)
7192 /* will be consumed next time around */;
7193 /* If it's a `;', we are done. */
7194 else if (token->type == CPP_SEMICOLON)
7196 /* Anything else is an error. */
7199 /* If we have already issued an error message we don't need
7200 to issue another one. */
7201 if (decl != error_mark_node
7202 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7203 cp_parser_error (parser, "expected %<,%> or %<;%>");
7204 /* Skip tokens until we reach the end of the statement. */
7205 cp_parser_skip_to_end_of_statement (parser);
7206 /* If the next token is now a `;', consume it. */
7207 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7208 cp_lexer_consume_token (parser->lexer);
7211 /* After the first time around, a function-definition is not
7212 allowed -- even if it was OK at first. For example:
7217 function_definition_allowed_p = false;
7220 /* Issue an error message if no declarators are present, and the
7221 decl-specifier-seq does not itself declare a class or
7223 if (!saw_declarator)
7225 if (cp_parser_declares_only_class_p (parser))
7226 shadow_tag (&decl_specifiers);
7227 /* Perform any deferred access checks. */
7228 perform_deferred_access_checks ();
7231 /* Consume the `;'. */
7232 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7235 pop_deferring_access_checks ();
7238 /* Parse a decl-specifier-seq.
7241 decl-specifier-seq [opt] decl-specifier
7244 storage-class-specifier
7255 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7257 The parser flags FLAGS is used to control type-specifier parsing.
7259 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7262 1: one of the decl-specifiers is an elaborated-type-specifier
7263 (i.e., a type declaration)
7264 2: one of the decl-specifiers is an enum-specifier or a
7265 class-specifier (i.e., a type definition)
7270 cp_parser_decl_specifier_seq (cp_parser* parser,
7271 cp_parser_flags flags,
7272 cp_decl_specifier_seq *decl_specs,
7273 int* declares_class_or_enum)
7275 bool constructor_possible_p = !parser->in_declarator_p;
7277 /* Clear DECL_SPECS. */
7278 clear_decl_specs (decl_specs);
7280 /* Assume no class or enumeration type is declared. */
7281 *declares_class_or_enum = 0;
7283 /* Keep reading specifiers until there are no more to read. */
7287 bool found_decl_spec;
7290 /* Peek at the next token. */
7291 token = cp_lexer_peek_token (parser->lexer);
7292 /* Handle attributes. */
7293 if (token->keyword == RID_ATTRIBUTE)
7295 /* Parse the attributes. */
7296 decl_specs->attributes
7297 = chainon (decl_specs->attributes,
7298 cp_parser_attributes_opt (parser));
7301 /* Assume we will find a decl-specifier keyword. */
7302 found_decl_spec = true;
7303 /* If the next token is an appropriate keyword, we can simply
7304 add it to the list. */
7305 switch (token->keyword)
7310 if (decl_specs->specs[(int) ds_friend]++)
7311 error ("duplicate %<friend%>");
7312 /* Consume the token. */
7313 cp_lexer_consume_token (parser->lexer);
7316 /* function-specifier:
7323 cp_parser_function_specifier_opt (parser, decl_specs);
7329 ++decl_specs->specs[(int) ds_typedef];
7330 /* Consume the token. */
7331 cp_lexer_consume_token (parser->lexer);
7332 /* A constructor declarator cannot appear in a typedef. */
7333 constructor_possible_p = false;
7334 /* The "typedef" keyword can only occur in a declaration; we
7335 may as well commit at this point. */
7336 cp_parser_commit_to_tentative_parse (parser);
7339 /* storage-class-specifier:
7349 /* Consume the token. */
7350 cp_lexer_consume_token (parser->lexer);
7351 cp_parser_set_storage_class (decl_specs, sc_auto);
7354 /* Consume the token. */
7355 cp_lexer_consume_token (parser->lexer);
7356 cp_parser_set_storage_class (decl_specs, sc_register);
7359 /* Consume the token. */
7360 cp_lexer_consume_token (parser->lexer);
7361 if (decl_specs->specs[(int) ds_thread])
7363 error ("%<__thread%> before %<static%>");
7364 decl_specs->specs[(int) ds_thread] = 0;
7366 cp_parser_set_storage_class (decl_specs, sc_static);
7369 /* Consume the token. */
7370 cp_lexer_consume_token (parser->lexer);
7371 if (decl_specs->specs[(int) ds_thread])
7373 error ("%<__thread%> before %<extern%>");
7374 decl_specs->specs[(int) ds_thread] = 0;
7376 cp_parser_set_storage_class (decl_specs, sc_extern);
7379 /* Consume the token. */
7380 cp_lexer_consume_token (parser->lexer);
7381 cp_parser_set_storage_class (decl_specs, sc_mutable);
7384 /* Consume the token. */
7385 cp_lexer_consume_token (parser->lexer);
7386 ++decl_specs->specs[(int) ds_thread];
7390 /* We did not yet find a decl-specifier yet. */
7391 found_decl_spec = false;
7395 /* Constructors are a special case. The `S' in `S()' is not a
7396 decl-specifier; it is the beginning of the declarator. */
7399 && constructor_possible_p
7400 && (cp_parser_constructor_declarator_p
7401 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7403 /* If we don't have a DECL_SPEC yet, then we must be looking at
7404 a type-specifier. */
7405 if (!found_decl_spec && !constructor_p)
7407 int decl_spec_declares_class_or_enum;
7408 bool is_cv_qualifier;
7412 = cp_parser_type_specifier (parser, flags,
7414 /*is_declaration=*/true,
7415 &decl_spec_declares_class_or_enum,
7418 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7420 /* If this type-specifier referenced a user-defined type
7421 (a typedef, class-name, etc.), then we can't allow any
7422 more such type-specifiers henceforth.
7426 The longest sequence of decl-specifiers that could
7427 possibly be a type name is taken as the
7428 decl-specifier-seq of a declaration. The sequence shall
7429 be self-consistent as described below.
7433 As a general rule, at most one type-specifier is allowed
7434 in the complete decl-specifier-seq of a declaration. The
7435 only exceptions are the following:
7437 -- const or volatile can be combined with any other
7440 -- signed or unsigned can be combined with char, long,
7448 void g (const int Pc);
7450 Here, Pc is *not* part of the decl-specifier seq; it's
7451 the declarator. Therefore, once we see a type-specifier
7452 (other than a cv-qualifier), we forbid any additional
7453 user-defined types. We *do* still allow things like `int
7454 int' to be considered a decl-specifier-seq, and issue the
7455 error message later. */
7456 if (type_spec && !is_cv_qualifier)
7457 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7458 /* A constructor declarator cannot follow a type-specifier. */
7461 constructor_possible_p = false;
7462 found_decl_spec = true;
7466 /* If we still do not have a DECL_SPEC, then there are no more
7468 if (!found_decl_spec)
7471 decl_specs->any_specifiers_p = true;
7472 /* After we see one decl-specifier, further decl-specifiers are
7474 flags |= CP_PARSER_FLAGS_OPTIONAL;
7477 /* Don't allow a friend specifier with a class definition. */
7478 if (decl_specs->specs[(int) ds_friend] != 0
7479 && (*declares_class_or_enum & 2))
7480 error ("class definition may not be declared a friend");
7483 /* Parse an (optional) storage-class-specifier.
7485 storage-class-specifier:
7494 storage-class-specifier:
7497 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7500 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7502 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7510 /* Consume the token. */
7511 return cp_lexer_consume_token (parser->lexer)->value;
7518 /* Parse an (optional) function-specifier.
7525 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7526 Updates DECL_SPECS, if it is non-NULL. */
7529 cp_parser_function_specifier_opt (cp_parser* parser,
7530 cp_decl_specifier_seq *decl_specs)
7532 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7536 ++decl_specs->specs[(int) ds_inline];
7541 ++decl_specs->specs[(int) ds_virtual];
7546 ++decl_specs->specs[(int) ds_explicit];
7553 /* Consume the token. */
7554 return cp_lexer_consume_token (parser->lexer)->value;
7557 /* Parse a linkage-specification.
7559 linkage-specification:
7560 extern string-literal { declaration-seq [opt] }
7561 extern string-literal declaration */
7564 cp_parser_linkage_specification (cp_parser* parser)
7568 /* Look for the `extern' keyword. */
7569 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7571 /* Look for the string-literal. */
7572 linkage = cp_parser_string_literal (parser, false, false);
7574 /* Transform the literal into an identifier. If the literal is a
7575 wide-character string, or contains embedded NULs, then we can't
7576 handle it as the user wants. */
7577 if (strlen (TREE_STRING_POINTER (linkage))
7578 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7580 cp_parser_error (parser, "invalid linkage-specification");
7581 /* Assume C++ linkage. */
7582 linkage = lang_name_cplusplus;
7585 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7587 /* We're now using the new linkage. */
7588 push_lang_context (linkage);
7590 /* If the next token is a `{', then we're using the first
7592 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7594 /* Consume the `{' token. */
7595 cp_lexer_consume_token (parser->lexer);
7596 /* Parse the declarations. */
7597 cp_parser_declaration_seq_opt (parser);
7598 /* Look for the closing `}'. */
7599 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7601 /* Otherwise, there's just one declaration. */
7604 bool saved_in_unbraced_linkage_specification_p;
7606 saved_in_unbraced_linkage_specification_p
7607 = parser->in_unbraced_linkage_specification_p;
7608 parser->in_unbraced_linkage_specification_p = true;
7609 have_extern_spec = true;
7610 cp_parser_declaration (parser);
7611 have_extern_spec = false;
7612 parser->in_unbraced_linkage_specification_p
7613 = saved_in_unbraced_linkage_specification_p;
7616 /* We're done with the linkage-specification. */
7617 pop_lang_context ();
7620 /* Special member functions [gram.special] */
7622 /* Parse a conversion-function-id.
7624 conversion-function-id:
7625 operator conversion-type-id
7627 Returns an IDENTIFIER_NODE representing the operator. */
7630 cp_parser_conversion_function_id (cp_parser* parser)
7634 tree saved_qualifying_scope;
7635 tree saved_object_scope;
7636 tree pushed_scope = NULL_TREE;
7638 /* Look for the `operator' token. */
7639 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7640 return error_mark_node;
7641 /* When we parse the conversion-type-id, the current scope will be
7642 reset. However, we need that information in able to look up the
7643 conversion function later, so we save it here. */
7644 saved_scope = parser->scope;
7645 saved_qualifying_scope = parser->qualifying_scope;
7646 saved_object_scope = parser->object_scope;
7647 /* We must enter the scope of the class so that the names of
7648 entities declared within the class are available in the
7649 conversion-type-id. For example, consider:
7656 S::operator I() { ... }
7658 In order to see that `I' is a type-name in the definition, we
7659 must be in the scope of `S'. */
7661 pushed_scope = push_scope (saved_scope);
7662 /* Parse the conversion-type-id. */
7663 type = cp_parser_conversion_type_id (parser);
7664 /* Leave the scope of the class, if any. */
7666 pop_scope (pushed_scope);
7667 /* Restore the saved scope. */
7668 parser->scope = saved_scope;
7669 parser->qualifying_scope = saved_qualifying_scope;
7670 parser->object_scope = saved_object_scope;
7671 /* If the TYPE is invalid, indicate failure. */
7672 if (type == error_mark_node)
7673 return error_mark_node;
7674 return mangle_conv_op_name_for_type (type);
7677 /* Parse a conversion-type-id:
7680 type-specifier-seq conversion-declarator [opt]
7682 Returns the TYPE specified. */
7685 cp_parser_conversion_type_id (cp_parser* parser)
7688 cp_decl_specifier_seq type_specifiers;
7689 cp_declarator *declarator;
7690 tree type_specified;
7692 /* Parse the attributes. */
7693 attributes = cp_parser_attributes_opt (parser);
7694 /* Parse the type-specifiers. */
7695 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7697 /* If that didn't work, stop. */
7698 if (type_specifiers.type == error_mark_node)
7699 return error_mark_node;
7700 /* Parse the conversion-declarator. */
7701 declarator = cp_parser_conversion_declarator_opt (parser);
7703 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7704 /*initialized=*/0, &attributes);
7706 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7707 return type_specified;
7710 /* Parse an (optional) conversion-declarator.
7712 conversion-declarator:
7713 ptr-operator conversion-declarator [opt]
7717 static cp_declarator *
7718 cp_parser_conversion_declarator_opt (cp_parser* parser)
7720 enum tree_code code;
7722 cp_cv_quals cv_quals;
7724 /* We don't know if there's a ptr-operator next, or not. */
7725 cp_parser_parse_tentatively (parser);
7726 /* Try the ptr-operator. */
7727 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7728 /* If it worked, look for more conversion-declarators. */
7729 if (cp_parser_parse_definitely (parser))
7731 cp_declarator *declarator;
7733 /* Parse another optional declarator. */
7734 declarator = cp_parser_conversion_declarator_opt (parser);
7736 /* Create the representation of the declarator. */
7738 declarator = make_ptrmem_declarator (cv_quals, class_type,
7740 else if (code == INDIRECT_REF)
7741 declarator = make_pointer_declarator (cv_quals, declarator);
7743 declarator = make_reference_declarator (cv_quals, declarator);
7751 /* Parse an (optional) ctor-initializer.
7754 : mem-initializer-list
7756 Returns TRUE iff the ctor-initializer was actually present. */
7759 cp_parser_ctor_initializer_opt (cp_parser* parser)
7761 /* If the next token is not a `:', then there is no
7762 ctor-initializer. */
7763 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7765 /* Do default initialization of any bases and members. */
7766 if (DECL_CONSTRUCTOR_P (current_function_decl))
7767 finish_mem_initializers (NULL_TREE);
7772 /* Consume the `:' token. */
7773 cp_lexer_consume_token (parser->lexer);
7774 /* And the mem-initializer-list. */
7775 cp_parser_mem_initializer_list (parser);
7780 /* Parse a mem-initializer-list.
7782 mem-initializer-list:
7784 mem-initializer , mem-initializer-list */
7787 cp_parser_mem_initializer_list (cp_parser* parser)
7789 tree mem_initializer_list = NULL_TREE;
7791 /* Let the semantic analysis code know that we are starting the
7792 mem-initializer-list. */
7793 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7794 error ("only constructors take base initializers");
7796 /* Loop through the list. */
7799 tree mem_initializer;
7801 /* Parse the mem-initializer. */
7802 mem_initializer = cp_parser_mem_initializer (parser);
7803 /* Add it to the list, unless it was erroneous. */
7804 if (mem_initializer)
7806 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7807 mem_initializer_list = mem_initializer;
7809 /* If the next token is not a `,', we're done. */
7810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7812 /* Consume the `,' token. */
7813 cp_lexer_consume_token (parser->lexer);
7816 /* Perform semantic analysis. */
7817 if (DECL_CONSTRUCTOR_P (current_function_decl))
7818 finish_mem_initializers (mem_initializer_list);
7821 /* Parse a mem-initializer.
7824 mem-initializer-id ( expression-list [opt] )
7829 ( expression-list [opt] )
7831 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7832 class) or FIELD_DECL (for a non-static data member) to initialize;
7833 the TREE_VALUE is the expression-list. */
7836 cp_parser_mem_initializer (cp_parser* parser)
7838 tree mem_initializer_id;
7839 tree expression_list;
7842 /* Find out what is being initialized. */
7843 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7845 pedwarn ("anachronistic old-style base class initializer");
7846 mem_initializer_id = NULL_TREE;
7849 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7850 member = expand_member_init (mem_initializer_id);
7851 if (member && !DECL_P (member))
7852 in_base_initializer = 1;
7855 = cp_parser_parenthesized_expression_list (parser, false,
7857 /*non_constant_p=*/NULL);
7858 if (!expression_list)
7859 expression_list = void_type_node;
7861 in_base_initializer = 0;
7863 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7866 /* Parse a mem-initializer-id.
7869 :: [opt] nested-name-specifier [opt] class-name
7872 Returns a TYPE indicating the class to be initializer for the first
7873 production. Returns an IDENTIFIER_NODE indicating the data member
7874 to be initialized for the second production. */
7877 cp_parser_mem_initializer_id (cp_parser* parser)
7879 bool global_scope_p;
7880 bool nested_name_specifier_p;
7881 bool template_p = false;
7884 /* `typename' is not allowed in this context ([temp.res]). */
7885 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7887 error ("keyword %<typename%> not allowed in this context (a qualified "
7888 "member initializer is implicitly a type)");
7889 cp_lexer_consume_token (parser->lexer);
7891 /* Look for the optional `::' operator. */
7893 = (cp_parser_global_scope_opt (parser,
7894 /*current_scope_valid_p=*/false)
7896 /* Look for the optional nested-name-specifier. The simplest way to
7901 The keyword `typename' is not permitted in a base-specifier or
7902 mem-initializer; in these contexts a qualified name that
7903 depends on a template-parameter is implicitly assumed to be a
7906 is to assume that we have seen the `typename' keyword at this
7908 nested_name_specifier_p
7909 = (cp_parser_nested_name_specifier_opt (parser,
7910 /*typename_keyword_p=*/true,
7911 /*check_dependency_p=*/true,
7913 /*is_declaration=*/true)
7915 if (nested_name_specifier_p)
7916 template_p = cp_parser_optional_template_keyword (parser);
7917 /* If there is a `::' operator or a nested-name-specifier, then we
7918 are definitely looking for a class-name. */
7919 if (global_scope_p || nested_name_specifier_p)
7920 return cp_parser_class_name (parser,
7921 /*typename_keyword_p=*/true,
7922 /*template_keyword_p=*/template_p,
7924 /*check_dependency_p=*/true,
7925 /*class_head_p=*/false,
7926 /*is_declaration=*/true);
7927 /* Otherwise, we could also be looking for an ordinary identifier. */
7928 cp_parser_parse_tentatively (parser);
7929 /* Try a class-name. */
7930 id = cp_parser_class_name (parser,
7931 /*typename_keyword_p=*/true,
7932 /*template_keyword_p=*/false,
7934 /*check_dependency_p=*/true,
7935 /*class_head_p=*/false,
7936 /*is_declaration=*/true);
7937 /* If we found one, we're done. */
7938 if (cp_parser_parse_definitely (parser))
7940 /* Otherwise, look for an ordinary identifier. */
7941 return cp_parser_identifier (parser);
7944 /* Overloading [gram.over] */
7946 /* Parse an operator-function-id.
7948 operator-function-id:
7951 Returns an IDENTIFIER_NODE for the operator which is a
7952 human-readable spelling of the identifier, e.g., `operator +'. */
7955 cp_parser_operator_function_id (cp_parser* parser)
7957 /* Look for the `operator' keyword. */
7958 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7959 return error_mark_node;
7960 /* And then the name of the operator itself. */
7961 return cp_parser_operator (parser);
7964 /* Parse an operator.
7967 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7968 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7969 || ++ -- , ->* -> () []
7976 Returns an IDENTIFIER_NODE for the operator which is a
7977 human-readable spelling of the identifier, e.g., `operator +'. */
7980 cp_parser_operator (cp_parser* parser)
7982 tree id = NULL_TREE;
7985 /* Peek at the next token. */
7986 token = cp_lexer_peek_token (parser->lexer);
7987 /* Figure out which operator we have. */
7988 switch (token->type)
7994 /* The keyword should be either `new' or `delete'. */
7995 if (token->keyword == RID_NEW)
7997 else if (token->keyword == RID_DELETE)
8002 /* Consume the `new' or `delete' token. */
8003 cp_lexer_consume_token (parser->lexer);
8005 /* Peek at the next token. */
8006 token = cp_lexer_peek_token (parser->lexer);
8007 /* If it's a `[' token then this is the array variant of the
8009 if (token->type == CPP_OPEN_SQUARE)
8011 /* Consume the `[' token. */
8012 cp_lexer_consume_token (parser->lexer);
8013 /* Look for the `]' token. */
8014 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8015 id = ansi_opname (op == NEW_EXPR
8016 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8018 /* Otherwise, we have the non-array variant. */
8020 id = ansi_opname (op);
8026 id = ansi_opname (PLUS_EXPR);
8030 id = ansi_opname (MINUS_EXPR);
8034 id = ansi_opname (MULT_EXPR);
8038 id = ansi_opname (TRUNC_DIV_EXPR);
8042 id = ansi_opname (TRUNC_MOD_EXPR);
8046 id = ansi_opname (BIT_XOR_EXPR);
8050 id = ansi_opname (BIT_AND_EXPR);
8054 id = ansi_opname (BIT_IOR_EXPR);
8058 id = ansi_opname (BIT_NOT_EXPR);
8062 id = ansi_opname (TRUTH_NOT_EXPR);
8066 id = ansi_assopname (NOP_EXPR);
8070 id = ansi_opname (LT_EXPR);
8074 id = ansi_opname (GT_EXPR);
8078 id = ansi_assopname (PLUS_EXPR);
8082 id = ansi_assopname (MINUS_EXPR);
8086 id = ansi_assopname (MULT_EXPR);
8090 id = ansi_assopname (TRUNC_DIV_EXPR);
8094 id = ansi_assopname (TRUNC_MOD_EXPR);
8098 id = ansi_assopname (BIT_XOR_EXPR);
8102 id = ansi_assopname (BIT_AND_EXPR);
8106 id = ansi_assopname (BIT_IOR_EXPR);
8110 id = ansi_opname (LSHIFT_EXPR);
8114 id = ansi_opname (RSHIFT_EXPR);
8118 id = ansi_assopname (LSHIFT_EXPR);
8122 id = ansi_assopname (RSHIFT_EXPR);
8126 id = ansi_opname (EQ_EXPR);
8130 id = ansi_opname (NE_EXPR);
8134 id = ansi_opname (LE_EXPR);
8137 case CPP_GREATER_EQ:
8138 id = ansi_opname (GE_EXPR);
8142 id = ansi_opname (TRUTH_ANDIF_EXPR);
8146 id = ansi_opname (TRUTH_ORIF_EXPR);
8150 id = ansi_opname (POSTINCREMENT_EXPR);
8153 case CPP_MINUS_MINUS:
8154 id = ansi_opname (PREDECREMENT_EXPR);
8158 id = ansi_opname (COMPOUND_EXPR);
8161 case CPP_DEREF_STAR:
8162 id = ansi_opname (MEMBER_REF);
8166 id = ansi_opname (COMPONENT_REF);
8169 case CPP_OPEN_PAREN:
8170 /* Consume the `('. */
8171 cp_lexer_consume_token (parser->lexer);
8172 /* Look for the matching `)'. */
8173 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8174 return ansi_opname (CALL_EXPR);
8176 case CPP_OPEN_SQUARE:
8177 /* Consume the `['. */
8178 cp_lexer_consume_token (parser->lexer);
8179 /* Look for the matching `]'. */
8180 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8181 return ansi_opname (ARRAY_REF);
8185 id = ansi_opname (MIN_EXPR);
8186 cp_parser_warn_min_max ();
8190 id = ansi_opname (MAX_EXPR);
8191 cp_parser_warn_min_max ();
8195 id = ansi_assopname (MIN_EXPR);
8196 cp_parser_warn_min_max ();
8200 id = ansi_assopname (MAX_EXPR);
8201 cp_parser_warn_min_max ();
8205 /* Anything else is an error. */
8209 /* If we have selected an identifier, we need to consume the
8212 cp_lexer_consume_token (parser->lexer);
8213 /* Otherwise, no valid operator name was present. */
8216 cp_parser_error (parser, "expected operator");
8217 id = error_mark_node;
8223 /* Parse a template-declaration.
8225 template-declaration:
8226 export [opt] template < template-parameter-list > declaration
8228 If MEMBER_P is TRUE, this template-declaration occurs within a
8231 The grammar rule given by the standard isn't correct. What
8234 template-declaration:
8235 export [opt] template-parameter-list-seq
8236 decl-specifier-seq [opt] init-declarator [opt] ;
8237 export [opt] template-parameter-list-seq
8240 template-parameter-list-seq:
8241 template-parameter-list-seq [opt]
8242 template < template-parameter-list > */
8245 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8247 /* Check for `export'. */
8248 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8250 /* Consume the `export' token. */
8251 cp_lexer_consume_token (parser->lexer);
8252 /* Warn that we do not support `export'. */
8253 warning (0, "keyword %<export%> not implemented, and will be ignored");
8256 cp_parser_template_declaration_after_export (parser, member_p);
8259 /* Parse a template-parameter-list.
8261 template-parameter-list:
8263 template-parameter-list , template-parameter
8265 Returns a TREE_LIST. Each node represents a template parameter.
8266 The nodes are connected via their TREE_CHAINs. */
8269 cp_parser_template_parameter_list (cp_parser* parser)
8271 tree parameter_list = NULL_TREE;
8279 /* Parse the template-parameter. */
8280 parameter = cp_parser_template_parameter (parser, &is_non_type);
8281 /* Add it to the list. */
8282 if (parameter != error_mark_node)
8283 parameter_list = process_template_parm (parameter_list,
8286 /* Peek at the next token. */
8287 token = cp_lexer_peek_token (parser->lexer);
8288 /* If it's not a `,', we're done. */
8289 if (token->type != CPP_COMMA)
8291 /* Otherwise, consume the `,' token. */
8292 cp_lexer_consume_token (parser->lexer);
8295 return parameter_list;
8298 /* Parse a template-parameter.
8302 parameter-declaration
8304 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8305 the parameter. The TREE_PURPOSE is the default value, if any.
8306 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8307 iff this parameter is a non-type parameter. */
8310 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8313 cp_parameter_declarator *parameter_declarator;
8316 /* Assume it is a type parameter or a template parameter. */
8317 *is_non_type = false;
8318 /* Peek at the next token. */
8319 token = cp_lexer_peek_token (parser->lexer);
8320 /* If it is `class' or `template', we have a type-parameter. */
8321 if (token->keyword == RID_TEMPLATE)
8322 return cp_parser_type_parameter (parser);
8323 /* If it is `class' or `typename' we do not know yet whether it is a
8324 type parameter or a non-type parameter. Consider:
8326 template <typename T, typename T::X X> ...
8330 template <class C, class D*> ...
8332 Here, the first parameter is a type parameter, and the second is
8333 a non-type parameter. We can tell by looking at the token after
8334 the identifier -- if it is a `,', `=', or `>' then we have a type
8336 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8338 /* Peek at the token after `class' or `typename'. */
8339 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8340 /* If it's an identifier, skip it. */
8341 if (token->type == CPP_NAME)
8342 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8343 /* Now, see if the token looks like the end of a template
8345 if (token->type == CPP_COMMA
8346 || token->type == CPP_EQ
8347 || token->type == CPP_GREATER)
8348 return cp_parser_type_parameter (parser);
8351 /* Otherwise, it is a non-type parameter.
8355 When parsing a default template-argument for a non-type
8356 template-parameter, the first non-nested `>' is taken as the end
8357 of the template parameter-list rather than a greater-than
8359 *is_non_type = true;
8360 parameter_declarator
8361 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8362 /*parenthesized_p=*/NULL);
8363 parm = grokdeclarator (parameter_declarator->declarator,
8364 ¶meter_declarator->decl_specifiers,
8365 PARM, /*initialized=*/0,
8367 if (parm == error_mark_node)
8368 return error_mark_node;
8369 return build_tree_list (parameter_declarator->default_argument, parm);
8372 /* Parse a type-parameter.
8375 class identifier [opt]
8376 class identifier [opt] = type-id
8377 typename identifier [opt]
8378 typename identifier [opt] = type-id
8379 template < template-parameter-list > class identifier [opt]
8380 template < template-parameter-list > class identifier [opt]
8383 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8384 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8385 the declaration of the parameter. */
8388 cp_parser_type_parameter (cp_parser* parser)
8393 /* Look for a keyword to tell us what kind of parameter this is. */
8394 token = cp_parser_require (parser, CPP_KEYWORD,
8395 "`class', `typename', or `template'");
8397 return error_mark_node;
8399 switch (token->keyword)
8405 tree default_argument;
8407 /* If the next token is an identifier, then it names the
8409 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8410 identifier = cp_parser_identifier (parser);
8412 identifier = NULL_TREE;
8414 /* Create the parameter. */
8415 parameter = finish_template_type_parm (class_type_node, identifier);
8417 /* If the next token is an `=', we have a default argument. */
8418 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8420 /* Consume the `=' token. */
8421 cp_lexer_consume_token (parser->lexer);
8422 /* Parse the default-argument. */
8423 default_argument = cp_parser_type_id (parser);
8426 default_argument = NULL_TREE;
8428 /* Create the combined representation of the parameter and the
8429 default argument. */
8430 parameter = build_tree_list (default_argument, parameter);
8436 tree parameter_list;
8438 tree default_argument;
8440 /* Look for the `<'. */
8441 cp_parser_require (parser, CPP_LESS, "`<'");
8442 /* Parse the template-parameter-list. */
8443 begin_template_parm_list ();
8445 = cp_parser_template_parameter_list (parser);
8446 parameter_list = end_template_parm_list (parameter_list);
8447 /* Look for the `>'. */
8448 cp_parser_require (parser, CPP_GREATER, "`>'");
8449 /* Look for the `class' keyword. */
8450 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8451 /* If the next token is an `=', then there is a
8452 default-argument. If the next token is a `>', we are at
8453 the end of the parameter-list. If the next token is a `,',
8454 then we are at the end of this parameter. */
8455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8456 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8457 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8459 identifier = cp_parser_identifier (parser);
8460 /* Treat invalid names as if the parameter were nameless. */
8461 if (identifier == error_mark_node)
8462 identifier = NULL_TREE;
8465 identifier = NULL_TREE;
8467 /* Create the template parameter. */
8468 parameter = finish_template_template_parm (class_type_node,
8471 /* If the next token is an `=', then there is a
8472 default-argument. */
8473 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8477 /* Consume the `='. */
8478 cp_lexer_consume_token (parser->lexer);
8479 /* Parse the id-expression. */
8481 = cp_parser_id_expression (parser,
8482 /*template_keyword_p=*/false,
8483 /*check_dependency_p=*/true,
8484 /*template_p=*/&is_template,
8485 /*declarator_p=*/false);
8486 if (TREE_CODE (default_argument) == TYPE_DECL)
8487 /* If the id-expression was a template-id that refers to
8488 a template-class, we already have the declaration here,
8489 so no further lookup is needed. */
8492 /* Look up the name. */
8494 = cp_parser_lookup_name (parser, default_argument,
8496 /*is_template=*/is_template,
8497 /*is_namespace=*/false,
8498 /*check_dependency=*/true,
8499 /*ambiguous_decls=*/NULL);
8500 /* See if the default argument is valid. */
8502 = check_template_template_default_arg (default_argument);
8505 default_argument = NULL_TREE;
8507 /* Create the combined representation of the parameter and the
8508 default argument. */
8509 parameter = build_tree_list (default_argument, parameter);
8521 /* Parse a template-id.
8524 template-name < template-argument-list [opt] >
8526 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8527 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8528 returned. Otherwise, if the template-name names a function, or set
8529 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8530 names a class, returns a TYPE_DECL for the specialization.
8532 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8533 uninstantiated templates. */
8536 cp_parser_template_id (cp_parser *parser,
8537 bool template_keyword_p,
8538 bool check_dependency_p,
8539 bool is_declaration)
8544 cp_token_position start_of_id = 0;
8545 tree access_check = NULL_TREE;
8546 cp_token *next_token, *next_token_2;
8549 /* If the next token corresponds to a template-id, there is no need
8551 next_token = cp_lexer_peek_token (parser->lexer);
8552 if (next_token->type == CPP_TEMPLATE_ID)
8557 /* Get the stored value. */
8558 value = cp_lexer_consume_token (parser->lexer)->value;
8559 /* Perform any access checks that were deferred. */
8560 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8561 perform_or_defer_access_check (TREE_PURPOSE (check),
8562 TREE_VALUE (check));
8563 /* Return the stored value. */
8564 return TREE_VALUE (value);
8567 /* Avoid performing name lookup if there is no possibility of
8568 finding a template-id. */
8569 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8570 || (next_token->type == CPP_NAME
8571 && !cp_parser_nth_token_starts_template_argument_list_p
8574 cp_parser_error (parser, "expected template-id");
8575 return error_mark_node;
8578 /* Remember where the template-id starts. */
8579 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8580 start_of_id = cp_lexer_token_position (parser->lexer, false);
8582 push_deferring_access_checks (dk_deferred);
8584 /* Parse the template-name. */
8585 is_identifier = false;
8586 template = cp_parser_template_name (parser, template_keyword_p,
8590 if (template == error_mark_node || is_identifier)
8592 pop_deferring_access_checks ();
8596 /* If we find the sequence `[:' after a template-name, it's probably
8597 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8598 parse correctly the argument list. */
8599 next_token = cp_lexer_peek_token (parser->lexer);
8600 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8601 if (next_token->type == CPP_OPEN_SQUARE
8602 && next_token->flags & DIGRAPH
8603 && next_token_2->type == CPP_COLON
8604 && !(next_token_2->flags & PREV_WHITE))
8606 cp_parser_parse_tentatively (parser);
8607 /* Change `:' into `::'. */
8608 next_token_2->type = CPP_SCOPE;
8609 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8611 cp_lexer_consume_token (parser->lexer);
8612 /* Parse the arguments. */
8613 arguments = cp_parser_enclosed_template_argument_list (parser);
8614 if (!cp_parser_parse_definitely (parser))
8616 /* If we couldn't parse an argument list, then we revert our changes
8617 and return simply an error. Maybe this is not a template-id
8619 next_token_2->type = CPP_COLON;
8620 cp_parser_error (parser, "expected %<<%>");
8621 pop_deferring_access_checks ();
8622 return error_mark_node;
8624 /* Otherwise, emit an error about the invalid digraph, but continue
8625 parsing because we got our argument list. */
8626 pedwarn ("%<<::%> cannot begin a template-argument list");
8627 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8628 "between %<<%> and %<::%>");
8629 if (!flag_permissive)
8634 inform ("(if you use -fpermissive G++ will accept your code)");
8641 /* Look for the `<' that starts the template-argument-list. */
8642 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8644 pop_deferring_access_checks ();
8645 return error_mark_node;
8647 /* Parse the arguments. */
8648 arguments = cp_parser_enclosed_template_argument_list (parser);
8651 /* Build a representation of the specialization. */
8652 if (TREE_CODE (template) == IDENTIFIER_NODE)
8653 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8654 else if (DECL_CLASS_TEMPLATE_P (template)
8655 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8657 = finish_template_type (template, arguments,
8658 cp_lexer_next_token_is (parser->lexer,
8662 /* If it's not a class-template or a template-template, it should be
8663 a function-template. */
8664 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8665 || TREE_CODE (template) == OVERLOAD
8666 || BASELINK_P (template)));
8668 template_id = lookup_template_function (template, arguments);
8671 /* Retrieve any deferred checks. Do not pop this access checks yet
8672 so the memory will not be reclaimed during token replacing below. */
8673 access_check = get_deferred_access_checks ();
8675 /* If parsing tentatively, replace the sequence of tokens that makes
8676 up the template-id with a CPP_TEMPLATE_ID token. That way,
8677 should we re-parse the token stream, we will not have to repeat
8678 the effort required to do the parse, nor will we issue duplicate
8679 error messages about problems during instantiation of the
8683 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8685 /* Reset the contents of the START_OF_ID token. */
8686 token->type = CPP_TEMPLATE_ID;
8687 token->value = build_tree_list (access_check, template_id);
8688 token->keyword = RID_MAX;
8690 /* Purge all subsequent tokens. */
8691 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8693 /* ??? Can we actually assume that, if template_id ==
8694 error_mark_node, we will have issued a diagnostic to the
8695 user, as opposed to simply marking the tentative parse as
8697 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8698 error ("parse error in template argument list");
8701 pop_deferring_access_checks ();
8705 /* Parse a template-name.
8710 The standard should actually say:
8714 operator-function-id
8716 A defect report has been filed about this issue.
8718 A conversion-function-id cannot be a template name because they cannot
8719 be part of a template-id. In fact, looking at this code:
8723 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8724 It is impossible to call a templated conversion-function-id with an
8725 explicit argument list, since the only allowed template parameter is
8726 the type to which it is converting.
8728 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8729 `template' keyword, in a construction like:
8733 In that case `f' is taken to be a template-name, even though there
8734 is no way of knowing for sure.
8736 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8737 name refers to a set of overloaded functions, at least one of which
8738 is a template, or an IDENTIFIER_NODE with the name of the template,
8739 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8740 names are looked up inside uninstantiated templates. */
8743 cp_parser_template_name (cp_parser* parser,
8744 bool template_keyword_p,
8745 bool check_dependency_p,
8746 bool is_declaration,
8747 bool *is_identifier)
8753 /* If the next token is `operator', then we have either an
8754 operator-function-id or a conversion-function-id. */
8755 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8757 /* We don't know whether we're looking at an
8758 operator-function-id or a conversion-function-id. */
8759 cp_parser_parse_tentatively (parser);
8760 /* Try an operator-function-id. */
8761 identifier = cp_parser_operator_function_id (parser);
8762 /* If that didn't work, try a conversion-function-id. */
8763 if (!cp_parser_parse_definitely (parser))
8765 cp_parser_error (parser, "expected template-name");
8766 return error_mark_node;
8769 /* Look for the identifier. */
8771 identifier = cp_parser_identifier (parser);
8773 /* If we didn't find an identifier, we don't have a template-id. */
8774 if (identifier == error_mark_node)
8775 return error_mark_node;
8777 /* If the name immediately followed the `template' keyword, then it
8778 is a template-name. However, if the next token is not `<', then
8779 we do not treat it as a template-name, since it is not being used
8780 as part of a template-id. This enables us to handle constructs
8783 template <typename T> struct S { S(); };
8784 template <typename T> S<T>::S();
8786 correctly. We would treat `S' as a template -- if it were `S<T>'
8787 -- but we do not if there is no `<'. */
8789 if (processing_template_decl
8790 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8792 /* In a declaration, in a dependent context, we pretend that the
8793 "template" keyword was present in order to improve error
8794 recovery. For example, given:
8796 template <typename T> void f(T::X<int>);
8798 we want to treat "X<int>" as a template-id. */
8800 && !template_keyword_p
8801 && parser->scope && TYPE_P (parser->scope)
8802 && check_dependency_p
8803 && dependent_type_p (parser->scope)
8804 /* Do not do this for dtors (or ctors), since they never
8805 need the template keyword before their name. */
8806 && !constructor_name_p (identifier, parser->scope))
8808 cp_token_position start = 0;
8810 /* Explain what went wrong. */
8811 error ("non-template %qD used as template", identifier);
8812 inform ("use %<%T::template %D%> to indicate that it is a template",
8813 parser->scope, identifier);
8814 /* If parsing tentatively, find the location of the "<" token. */
8815 if (cp_parser_simulate_error (parser))
8816 start = cp_lexer_token_position (parser->lexer, true);
8817 /* Parse the template arguments so that we can issue error
8818 messages about them. */
8819 cp_lexer_consume_token (parser->lexer);
8820 cp_parser_enclosed_template_argument_list (parser);
8821 /* Skip tokens until we find a good place from which to
8822 continue parsing. */
8823 cp_parser_skip_to_closing_parenthesis (parser,
8824 /*recovering=*/true,
8826 /*consume_paren=*/false);
8827 /* If parsing tentatively, permanently remove the
8828 template argument list. That will prevent duplicate
8829 error messages from being issued about the missing
8830 "template" keyword. */
8832 cp_lexer_purge_tokens_after (parser->lexer, start);
8834 *is_identifier = true;
8838 /* If the "template" keyword is present, then there is generally
8839 no point in doing name-lookup, so we just return IDENTIFIER.
8840 But, if the qualifying scope is non-dependent then we can
8841 (and must) do name-lookup normally. */
8842 if (template_keyword_p
8844 || (TYPE_P (parser->scope)
8845 && dependent_type_p (parser->scope))))
8849 /* Look up the name. */
8850 decl = cp_parser_lookup_name (parser, identifier,
8852 /*is_template=*/false,
8853 /*is_namespace=*/false,
8855 /*ambiguous_decls=*/NULL);
8856 decl = maybe_get_template_decl_from_type_decl (decl);
8858 /* If DECL is a template, then the name was a template-name. */
8859 if (TREE_CODE (decl) == TEMPLATE_DECL)
8863 tree fn = NULL_TREE;
8865 /* The standard does not explicitly indicate whether a name that
8866 names a set of overloaded declarations, some of which are
8867 templates, is a template-name. However, such a name should
8868 be a template-name; otherwise, there is no way to form a
8869 template-id for the overloaded templates. */
8870 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8871 if (TREE_CODE (fns) == OVERLOAD)
8872 for (fn = fns; fn; fn = OVL_NEXT (fn))
8873 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8878 /* The name does not name a template. */
8879 cp_parser_error (parser, "expected template-name");
8880 return error_mark_node;
8884 /* If DECL is dependent, and refers to a function, then just return
8885 its name; we will look it up again during template instantiation. */
8886 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8888 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8889 if (TYPE_P (scope) && dependent_type_p (scope))
8896 /* Parse a template-argument-list.
8898 template-argument-list:
8900 template-argument-list , template-argument
8902 Returns a TREE_VEC containing the arguments. */
8905 cp_parser_template_argument_list (cp_parser* parser)
8907 tree fixed_args[10];
8908 unsigned n_args = 0;
8909 unsigned alloced = 10;
8910 tree *arg_ary = fixed_args;
8912 bool saved_in_template_argument_list_p;
8914 bool saved_non_ice_p;
8916 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8917 parser->in_template_argument_list_p = true;
8918 /* Even if the template-id appears in an integral
8919 constant-expression, the contents of the argument list do
8921 saved_ice_p = parser->integral_constant_expression_p;
8922 parser->integral_constant_expression_p = false;
8923 saved_non_ice_p = parser->non_integral_constant_expression_p;
8924 parser->non_integral_constant_expression_p = false;
8925 /* Parse the arguments. */
8931 /* Consume the comma. */
8932 cp_lexer_consume_token (parser->lexer);
8934 /* Parse the template-argument. */
8935 argument = cp_parser_template_argument (parser);
8936 if (n_args == alloced)
8940 if (arg_ary == fixed_args)
8942 arg_ary = XNEWVEC (tree, alloced);
8943 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8946 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
8948 arg_ary[n_args++] = argument;
8950 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8952 vec = make_tree_vec (n_args);
8955 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8957 if (arg_ary != fixed_args)
8959 parser->non_integral_constant_expression_p = saved_non_ice_p;
8960 parser->integral_constant_expression_p = saved_ice_p;
8961 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8965 /* Parse a template-argument.
8968 assignment-expression
8972 The representation is that of an assignment-expression, type-id, or
8973 id-expression -- except that the qualified id-expression is
8974 evaluated, so that the value returned is either a DECL or an
8977 Although the standard says "assignment-expression", it forbids
8978 throw-expressions or assignments in the template argument.
8979 Therefore, we use "conditional-expression" instead. */
8982 cp_parser_template_argument (cp_parser* parser)
8987 bool maybe_type_id = false;
8991 /* There's really no way to know what we're looking at, so we just
8992 try each alternative in order.
8996 In a template-argument, an ambiguity between a type-id and an
8997 expression is resolved to a type-id, regardless of the form of
8998 the corresponding template-parameter.
9000 Therefore, we try a type-id first. */
9001 cp_parser_parse_tentatively (parser);
9002 argument = cp_parser_type_id (parser);
9003 /* If there was no error parsing the type-id but the next token is a '>>',
9004 we probably found a typo for '> >'. But there are type-id which are
9005 also valid expressions. For instance:
9007 struct X { int operator >> (int); };
9008 template <int V> struct Foo {};
9011 Here 'X()' is a valid type-id of a function type, but the user just
9012 wanted to write the expression "X() >> 5". Thus, we remember that we
9013 found a valid type-id, but we still try to parse the argument as an
9014 expression to see what happens. */
9015 if (!cp_parser_error_occurred (parser)
9016 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9018 maybe_type_id = true;
9019 cp_parser_abort_tentative_parse (parser);
9023 /* If the next token isn't a `,' or a `>', then this argument wasn't
9024 really finished. This means that the argument is not a valid
9026 if (!cp_parser_next_token_ends_template_argument_p (parser))
9027 cp_parser_error (parser, "expected template-argument");
9028 /* If that worked, we're done. */
9029 if (cp_parser_parse_definitely (parser))
9032 /* We're still not sure what the argument will be. */
9033 cp_parser_parse_tentatively (parser);
9034 /* Try a template. */
9035 argument = cp_parser_id_expression (parser,
9036 /*template_keyword_p=*/false,
9037 /*check_dependency_p=*/true,
9039 /*declarator_p=*/false);
9040 /* If the next token isn't a `,' or a `>', then this argument wasn't
9042 if (!cp_parser_next_token_ends_template_argument_p (parser))
9043 cp_parser_error (parser, "expected template-argument");
9044 if (!cp_parser_error_occurred (parser))
9046 /* Figure out what is being referred to. If the id-expression
9047 was for a class template specialization, then we will have a
9048 TYPE_DECL at this point. There is no need to do name lookup
9049 at this point in that case. */
9050 if (TREE_CODE (argument) != TYPE_DECL)
9051 argument = cp_parser_lookup_name (parser, argument,
9053 /*is_template=*/template_p,
9054 /*is_namespace=*/false,
9055 /*check_dependency=*/true,
9056 /*ambiguous_decls=*/NULL);
9057 if (TREE_CODE (argument) != TEMPLATE_DECL
9058 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9059 cp_parser_error (parser, "expected template-name");
9061 if (cp_parser_parse_definitely (parser))
9063 /* It must be a non-type argument. There permitted cases are given
9064 in [temp.arg.nontype]:
9066 -- an integral constant-expression of integral or enumeration
9069 -- the name of a non-type template-parameter; or
9071 -- the name of an object or function with external linkage...
9073 -- the address of an object or function with external linkage...
9075 -- a pointer to member... */
9076 /* Look for a non-type template parameter. */
9077 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9079 cp_parser_parse_tentatively (parser);
9080 argument = cp_parser_primary_expression (parser,
9083 /*template_arg_p=*/true,
9085 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9086 || !cp_parser_next_token_ends_template_argument_p (parser))
9087 cp_parser_simulate_error (parser);
9088 if (cp_parser_parse_definitely (parser))
9092 /* If the next token is "&", the argument must be the address of an
9093 object or function with external linkage. */
9094 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9096 cp_lexer_consume_token (parser->lexer);
9097 /* See if we might have an id-expression. */
9098 token = cp_lexer_peek_token (parser->lexer);
9099 if (token->type == CPP_NAME
9100 || token->keyword == RID_OPERATOR
9101 || token->type == CPP_SCOPE
9102 || token->type == CPP_TEMPLATE_ID
9103 || token->type == CPP_NESTED_NAME_SPECIFIER)
9105 cp_parser_parse_tentatively (parser);
9106 argument = cp_parser_primary_expression (parser,
9109 /*template_arg_p=*/true,
9111 if (cp_parser_error_occurred (parser)
9112 || !cp_parser_next_token_ends_template_argument_p (parser))
9113 cp_parser_abort_tentative_parse (parser);
9116 if (TREE_CODE (argument) == INDIRECT_REF)
9118 gcc_assert (REFERENCE_REF_P (argument));
9119 argument = TREE_OPERAND (argument, 0);
9122 if (TREE_CODE (argument) == BASELINK)
9123 /* We don't need the information about what class was used
9124 to name the overloaded functions. */
9125 argument = BASELINK_FUNCTIONS (argument);
9127 if (TREE_CODE (argument) == VAR_DECL)
9129 /* A variable without external linkage might still be a
9130 valid constant-expression, so no error is issued here
9131 if the external-linkage check fails. */
9132 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9133 cp_parser_simulate_error (parser);
9135 else if (is_overloaded_fn (argument))
9136 /* All overloaded functions are allowed; if the external
9137 linkage test does not pass, an error will be issued
9141 && (TREE_CODE (argument) == OFFSET_REF
9142 || TREE_CODE (argument) == SCOPE_REF))
9143 /* A pointer-to-member. */
9145 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9148 cp_parser_simulate_error (parser);
9150 if (cp_parser_parse_definitely (parser))
9153 argument = build_x_unary_op (ADDR_EXPR, argument);
9158 /* If the argument started with "&", there are no other valid
9159 alternatives at this point. */
9162 cp_parser_error (parser, "invalid non-type template argument");
9163 return error_mark_node;
9166 /* If the argument wasn't successfully parsed as a type-id followed
9167 by '>>', the argument can only be a constant expression now.
9168 Otherwise, we try parsing the constant-expression tentatively,
9169 because the argument could really be a type-id. */
9171 cp_parser_parse_tentatively (parser);
9172 argument = cp_parser_constant_expression (parser,
9173 /*allow_non_constant_p=*/false,
9174 /*non_constant_p=*/NULL);
9175 argument = fold_non_dependent_expr (argument);
9178 if (!cp_parser_next_token_ends_template_argument_p (parser))
9179 cp_parser_error (parser, "expected template-argument");
9180 if (cp_parser_parse_definitely (parser))
9182 /* We did our best to parse the argument as a non type-id, but that
9183 was the only alternative that matched (albeit with a '>' after
9184 it). We can assume it's just a typo from the user, and a
9185 diagnostic will then be issued. */
9186 return cp_parser_type_id (parser);
9189 /* Parse an explicit-instantiation.
9191 explicit-instantiation:
9192 template declaration
9194 Although the standard says `declaration', what it really means is:
9196 explicit-instantiation:
9197 template decl-specifier-seq [opt] declarator [opt] ;
9199 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9200 supposed to be allowed. A defect report has been filed about this
9205 explicit-instantiation:
9206 storage-class-specifier template
9207 decl-specifier-seq [opt] declarator [opt] ;
9208 function-specifier template
9209 decl-specifier-seq [opt] declarator [opt] ; */
9212 cp_parser_explicit_instantiation (cp_parser* parser)
9214 int declares_class_or_enum;
9215 cp_decl_specifier_seq decl_specifiers;
9216 tree extension_specifier = NULL_TREE;
9218 /* Look for an (optional) storage-class-specifier or
9219 function-specifier. */
9220 if (cp_parser_allow_gnu_extensions_p (parser))
9223 = cp_parser_storage_class_specifier_opt (parser);
9224 if (!extension_specifier)
9226 = cp_parser_function_specifier_opt (parser,
9227 /*decl_specs=*/NULL);
9230 /* Look for the `template' keyword. */
9231 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9232 /* Let the front end know that we are processing an explicit
9234 begin_explicit_instantiation ();
9235 /* [temp.explicit] says that we are supposed to ignore access
9236 control while processing explicit instantiation directives. */
9237 push_deferring_access_checks (dk_no_check);
9238 /* Parse a decl-specifier-seq. */
9239 cp_parser_decl_specifier_seq (parser,
9240 CP_PARSER_FLAGS_OPTIONAL,
9242 &declares_class_or_enum);
9243 /* If there was exactly one decl-specifier, and it declared a class,
9244 and there's no declarator, then we have an explicit type
9246 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9250 type = check_tag_decl (&decl_specifiers);
9251 /* Turn access control back on for names used during
9252 template instantiation. */
9253 pop_deferring_access_checks ();
9255 do_type_instantiation (type, extension_specifier,
9256 /*complain=*/tf_error);
9260 cp_declarator *declarator;
9263 /* Parse the declarator. */
9265 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9266 /*ctor_dtor_or_conv_p=*/NULL,
9267 /*parenthesized_p=*/NULL,
9268 /*member_p=*/false);
9269 if (declares_class_or_enum & 2)
9270 cp_parser_check_for_definition_in_return_type (declarator,
9271 decl_specifiers.type);
9272 if (declarator != cp_error_declarator)
9274 decl = grokdeclarator (declarator, &decl_specifiers,
9276 /* Turn access control back on for names used during
9277 template instantiation. */
9278 pop_deferring_access_checks ();
9279 /* Do the explicit instantiation. */
9280 do_decl_instantiation (decl, extension_specifier);
9284 pop_deferring_access_checks ();
9285 /* Skip the body of the explicit instantiation. */
9286 cp_parser_skip_to_end_of_statement (parser);
9289 /* We're done with the instantiation. */
9290 end_explicit_instantiation ();
9292 cp_parser_consume_semicolon_at_end_of_statement (parser);
9295 /* Parse an explicit-specialization.
9297 explicit-specialization:
9298 template < > declaration
9300 Although the standard says `declaration', what it really means is:
9302 explicit-specialization:
9303 template <> decl-specifier [opt] init-declarator [opt] ;
9304 template <> function-definition
9305 template <> explicit-specialization
9306 template <> template-declaration */
9309 cp_parser_explicit_specialization (cp_parser* parser)
9312 /* Look for the `template' keyword. */
9313 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9314 /* Look for the `<'. */
9315 cp_parser_require (parser, CPP_LESS, "`<'");
9316 /* Look for the `>'. */
9317 cp_parser_require (parser, CPP_GREATER, "`>'");
9318 /* We have processed another parameter list. */
9319 ++parser->num_template_parameter_lists;
9322 A template ... explicit specialization ... shall not have C
9324 if (current_lang_name == lang_name_c)
9326 error ("template specialization with C linkage");
9327 /* Give it C++ linkage to avoid confusing other parts of the
9329 push_lang_context (lang_name_cplusplus);
9330 need_lang_pop = true;
9333 need_lang_pop = false;
9334 /* Let the front end know that we are beginning a specialization. */
9335 begin_specialization ();
9336 /* If the next keyword is `template', we need to figure out whether
9337 or not we're looking a template-declaration. */
9338 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9340 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9341 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9342 cp_parser_template_declaration_after_export (parser,
9343 /*member_p=*/false);
9345 cp_parser_explicit_specialization (parser);
9348 /* Parse the dependent declaration. */
9349 cp_parser_single_declaration (parser,
9352 /* We're done with the specialization. */
9353 end_specialization ();
9354 /* For the erroneous case of a template with C linkage, we pushed an
9355 implicit C++ linkage scope; exit that scope now. */
9357 pop_lang_context ();
9358 /* We're done with this parameter list. */
9359 --parser->num_template_parameter_lists;
9362 /* Parse a type-specifier.
9365 simple-type-specifier
9368 elaborated-type-specifier
9376 Returns a representation of the type-specifier. For a
9377 class-specifier, enum-specifier, or elaborated-type-specifier, a
9378 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9380 The parser flags FLAGS is used to control type-specifier parsing.
9382 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9383 in a decl-specifier-seq.
9385 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9386 class-specifier, enum-specifier, or elaborated-type-specifier, then
9387 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9388 if a type is declared; 2 if it is defined. Otherwise, it is set to
9391 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9392 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9396 cp_parser_type_specifier (cp_parser* parser,
9397 cp_parser_flags flags,
9398 cp_decl_specifier_seq *decl_specs,
9399 bool is_declaration,
9400 int* declares_class_or_enum,
9401 bool* is_cv_qualifier)
9403 tree type_spec = NULL_TREE;
9406 cp_decl_spec ds = ds_last;
9408 /* Assume this type-specifier does not declare a new type. */
9409 if (declares_class_or_enum)
9410 *declares_class_or_enum = 0;
9411 /* And that it does not specify a cv-qualifier. */
9412 if (is_cv_qualifier)
9413 *is_cv_qualifier = false;
9414 /* Peek at the next token. */
9415 token = cp_lexer_peek_token (parser->lexer);
9417 /* If we're looking at a keyword, we can use that to guide the
9418 production we choose. */
9419 keyword = token->keyword;
9423 /* 'enum' [identifier] '{' introduces an enum-specifier;
9424 'enum' <anything else> introduces an elaborated-type-specifier. */
9425 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9426 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9427 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9430 if (parser->num_template_parameter_lists)
9432 error ("template declaration of %qs", "enum");
9433 cp_parser_skip_to_end_of_block_or_statement (parser);
9434 type_spec = error_mark_node;
9437 type_spec = cp_parser_enum_specifier (parser);
9439 if (declares_class_or_enum)
9440 *declares_class_or_enum = 2;
9442 cp_parser_set_decl_spec_type (decl_specs,
9444 /*user_defined_p=*/true);
9448 goto elaborated_type_specifier;
9450 /* Any of these indicate either a class-specifier, or an
9451 elaborated-type-specifier. */
9455 /* Parse tentatively so that we can back up if we don't find a
9457 cp_parser_parse_tentatively (parser);
9458 /* Look for the class-specifier. */
9459 type_spec = cp_parser_class_specifier (parser);
9460 /* If that worked, we're done. */
9461 if (cp_parser_parse_definitely (parser))
9463 if (declares_class_or_enum)
9464 *declares_class_or_enum = 2;
9466 cp_parser_set_decl_spec_type (decl_specs,
9468 /*user_defined_p=*/true);
9473 elaborated_type_specifier:
9474 /* We're declaring (not defining) a class or enum. */
9475 if (declares_class_or_enum)
9476 *declares_class_or_enum = 1;
9480 /* Look for an elaborated-type-specifier. */
9482 = (cp_parser_elaborated_type_specifier
9484 decl_specs && decl_specs->specs[(int) ds_friend],
9487 cp_parser_set_decl_spec_type (decl_specs,
9489 /*user_defined_p=*/true);
9494 if (is_cv_qualifier)
9495 *is_cv_qualifier = true;
9500 if (is_cv_qualifier)
9501 *is_cv_qualifier = true;
9506 if (is_cv_qualifier)
9507 *is_cv_qualifier = true;
9511 /* The `__complex__' keyword is a GNU extension. */
9519 /* Handle simple keywords. */
9524 ++decl_specs->specs[(int)ds];
9525 decl_specs->any_specifiers_p = true;
9527 return cp_lexer_consume_token (parser->lexer)->value;
9530 /* If we do not already have a type-specifier, assume we are looking
9531 at a simple-type-specifier. */
9532 type_spec = cp_parser_simple_type_specifier (parser,
9536 /* If we didn't find a type-specifier, and a type-specifier was not
9537 optional in this context, issue an error message. */
9538 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9540 cp_parser_error (parser, "expected type specifier");
9541 return error_mark_node;
9547 /* Parse a simple-type-specifier.
9549 simple-type-specifier:
9550 :: [opt] nested-name-specifier [opt] type-name
9551 :: [opt] nested-name-specifier template template-id
9566 simple-type-specifier:
9567 __typeof__ unary-expression
9568 __typeof__ ( type-id )
9570 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9571 appropriately updated. */
9574 cp_parser_simple_type_specifier (cp_parser* parser,
9575 cp_decl_specifier_seq *decl_specs,
9576 cp_parser_flags flags)
9578 tree type = NULL_TREE;
9581 /* Peek at the next token. */
9582 token = cp_lexer_peek_token (parser->lexer);
9584 /* If we're looking at a keyword, things are easy. */
9585 switch (token->keyword)
9589 decl_specs->explicit_char_p = true;
9590 type = char_type_node;
9593 type = wchar_type_node;
9596 type = boolean_type_node;
9600 ++decl_specs->specs[(int) ds_short];
9601 type = short_integer_type_node;
9605 decl_specs->explicit_int_p = true;
9606 type = integer_type_node;
9610 ++decl_specs->specs[(int) ds_long];
9611 type = long_integer_type_node;
9615 ++decl_specs->specs[(int) ds_signed];
9616 type = integer_type_node;
9620 ++decl_specs->specs[(int) ds_unsigned];
9621 type = unsigned_type_node;
9624 type = float_type_node;
9627 type = double_type_node;
9630 type = void_type_node;
9634 /* Consume the `typeof' token. */
9635 cp_lexer_consume_token (parser->lexer);
9636 /* Parse the operand to `typeof'. */
9637 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9638 /* If it is not already a TYPE, take its type. */
9640 type = finish_typeof (type);
9643 cp_parser_set_decl_spec_type (decl_specs, type,
9644 /*user_defined_p=*/true);
9652 /* If the type-specifier was for a built-in type, we're done. */
9657 /* Record the type. */
9659 && (token->keyword != RID_SIGNED
9660 && token->keyword != RID_UNSIGNED
9661 && token->keyword != RID_SHORT
9662 && token->keyword != RID_LONG))
9663 cp_parser_set_decl_spec_type (decl_specs,
9665 /*user_defined=*/false);
9667 decl_specs->any_specifiers_p = true;
9669 /* Consume the token. */
9670 id = cp_lexer_consume_token (parser->lexer)->value;
9672 /* There is no valid C++ program where a non-template type is
9673 followed by a "<". That usually indicates that the user thought
9674 that the type was a template. */
9675 cp_parser_check_for_invalid_template_id (parser, type);
9677 return TYPE_NAME (type);
9680 /* The type-specifier must be a user-defined type. */
9681 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9686 /* Don't gobble tokens or issue error messages if this is an
9687 optional type-specifier. */
9688 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9689 cp_parser_parse_tentatively (parser);
9691 /* Look for the optional `::' operator. */
9693 = (cp_parser_global_scope_opt (parser,
9694 /*current_scope_valid_p=*/false)
9696 /* Look for the nested-name specifier. */
9698 = (cp_parser_nested_name_specifier_opt (parser,
9699 /*typename_keyword_p=*/false,
9700 /*check_dependency_p=*/true,
9702 /*is_declaration=*/false)
9704 /* If we have seen a nested-name-specifier, and the next token
9705 is `template', then we are using the template-id production. */
9707 && cp_parser_optional_template_keyword (parser))
9709 /* Look for the template-id. */
9710 type = cp_parser_template_id (parser,
9711 /*template_keyword_p=*/true,
9712 /*check_dependency_p=*/true,
9713 /*is_declaration=*/false);
9714 /* If the template-id did not name a type, we are out of
9716 if (TREE_CODE (type) != TYPE_DECL)
9718 cp_parser_error (parser, "expected template-id for type");
9722 /* Otherwise, look for a type-name. */
9724 type = cp_parser_type_name (parser);
9725 /* Keep track of all name-lookups performed in class scopes. */
9729 && TREE_CODE (type) == TYPE_DECL
9730 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9731 maybe_note_name_used_in_class (DECL_NAME (type), type);
9732 /* If it didn't work out, we don't have a TYPE. */
9733 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9734 && !cp_parser_parse_definitely (parser))
9736 if (type && decl_specs)
9737 cp_parser_set_decl_spec_type (decl_specs, type,
9738 /*user_defined=*/true);
9741 /* If we didn't get a type-name, issue an error message. */
9742 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9744 cp_parser_error (parser, "expected type-name");
9745 return error_mark_node;
9748 /* There is no valid C++ program where a non-template type is
9749 followed by a "<". That usually indicates that the user thought
9750 that the type was a template. */
9751 if (type && type != error_mark_node)
9753 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9754 If it is, then the '<'...'>' enclose protocol names rather than
9755 template arguments, and so everything is fine. */
9756 if (c_dialect_objc ()
9757 && (objc_is_id (type) || objc_is_class_name (type)))
9759 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9760 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9762 /* Clobber the "unqualified" type previously entered into
9763 DECL_SPECS with the new, improved protocol-qualified version. */
9765 decl_specs->type = qual_type;
9770 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9776 /* Parse a type-name.
9789 Returns a TYPE_DECL for the type. */
9792 cp_parser_type_name (cp_parser* parser)
9797 /* We can't know yet whether it is a class-name or not. */
9798 cp_parser_parse_tentatively (parser);
9799 /* Try a class-name. */
9800 type_decl = cp_parser_class_name (parser,
9801 /*typename_keyword_p=*/false,
9802 /*template_keyword_p=*/false,
9804 /*check_dependency_p=*/true,
9805 /*class_head_p=*/false,
9806 /*is_declaration=*/false);
9807 /* If it's not a class-name, keep looking. */
9808 if (!cp_parser_parse_definitely (parser))
9810 /* It must be a typedef-name or an enum-name. */
9811 identifier = cp_parser_identifier (parser);
9812 if (identifier == error_mark_node)
9813 return error_mark_node;
9815 /* Look up the type-name. */
9816 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9818 if (TREE_CODE (type_decl) != TYPE_DECL
9819 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9821 /* See if this is an Objective-C type. */
9822 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9823 tree type = objc_get_protocol_qualified_type (identifier, protos);
9825 type_decl = TYPE_NAME (type);
9828 /* Issue an error if we did not find a type-name. */
9829 if (TREE_CODE (type_decl) != TYPE_DECL)
9831 if (!cp_parser_simulate_error (parser))
9832 cp_parser_name_lookup_error (parser, identifier, type_decl,
9834 type_decl = error_mark_node;
9836 /* Remember that the name was used in the definition of the
9837 current class so that we can check later to see if the
9838 meaning would have been different after the class was
9839 entirely defined. */
9840 else if (type_decl != error_mark_node
9842 maybe_note_name_used_in_class (identifier, type_decl);
9849 /* Parse an elaborated-type-specifier. Note that the grammar given
9850 here incorporates the resolution to DR68.
9852 elaborated-type-specifier:
9853 class-key :: [opt] nested-name-specifier [opt] identifier
9854 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9855 enum :: [opt] nested-name-specifier [opt] identifier
9856 typename :: [opt] nested-name-specifier identifier
9857 typename :: [opt] nested-name-specifier template [opt]
9862 elaborated-type-specifier:
9863 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9864 class-key attributes :: [opt] nested-name-specifier [opt]
9865 template [opt] template-id
9866 enum attributes :: [opt] nested-name-specifier [opt] identifier
9868 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9869 declared `friend'. If IS_DECLARATION is TRUE, then this
9870 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9871 something is being declared.
9873 Returns the TYPE specified. */
9876 cp_parser_elaborated_type_specifier (cp_parser* parser,
9878 bool is_declaration)
9880 enum tag_types tag_type;
9882 tree type = NULL_TREE;
9883 tree attributes = NULL_TREE;
9885 /* See if we're looking at the `enum' keyword. */
9886 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9888 /* Consume the `enum' token. */
9889 cp_lexer_consume_token (parser->lexer);
9890 /* Remember that it's an enumeration type. */
9891 tag_type = enum_type;
9892 /* Parse the attributes. */
9893 attributes = cp_parser_attributes_opt (parser);
9895 /* Or, it might be `typename'. */
9896 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9899 /* Consume the `typename' token. */
9900 cp_lexer_consume_token (parser->lexer);
9901 /* Remember that it's a `typename' type. */
9902 tag_type = typename_type;
9903 /* The `typename' keyword is only allowed in templates. */
9904 if (!processing_template_decl)
9905 pedwarn ("using %<typename%> outside of template");
9907 /* Otherwise it must be a class-key. */
9910 tag_type = cp_parser_class_key (parser);
9911 if (tag_type == none_type)
9912 return error_mark_node;
9913 /* Parse the attributes. */
9914 attributes = cp_parser_attributes_opt (parser);
9917 /* Look for the `::' operator. */
9918 cp_parser_global_scope_opt (parser,
9919 /*current_scope_valid_p=*/false);
9920 /* Look for the nested-name-specifier. */
9921 if (tag_type == typename_type)
9923 if (!cp_parser_nested_name_specifier (parser,
9924 /*typename_keyword_p=*/true,
9925 /*check_dependency_p=*/true,
9928 return error_mark_node;
9931 /* Even though `typename' is not present, the proposed resolution
9932 to Core Issue 180 says that in `class A<T>::B', `B' should be
9933 considered a type-name, even if `A<T>' is dependent. */
9934 cp_parser_nested_name_specifier_opt (parser,
9935 /*typename_keyword_p=*/true,
9936 /*check_dependency_p=*/true,
9939 /* For everything but enumeration types, consider a template-id. */
9940 if (tag_type != enum_type)
9942 bool template_p = false;
9945 /* Allow the `template' keyword. */
9946 template_p = cp_parser_optional_template_keyword (parser);
9947 /* If we didn't see `template', we don't know if there's a
9948 template-id or not. */
9950 cp_parser_parse_tentatively (parser);
9951 /* Parse the template-id. */
9952 decl = cp_parser_template_id (parser, template_p,
9953 /*check_dependency_p=*/true,
9955 /* If we didn't find a template-id, look for an ordinary
9957 if (!template_p && !cp_parser_parse_definitely (parser))
9959 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9960 in effect, then we must assume that, upon instantiation, the
9961 template will correspond to a class. */
9962 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9963 && tag_type == typename_type)
9964 type = make_typename_type (parser->scope, decl,
9966 /*complain=*/tf_error);
9968 type = TREE_TYPE (decl);
9971 /* For an enumeration type, consider only a plain identifier. */
9974 identifier = cp_parser_identifier (parser);
9976 if (identifier == error_mark_node)
9978 parser->scope = NULL_TREE;
9979 return error_mark_node;
9982 /* For a `typename', we needn't call xref_tag. */
9983 if (tag_type == typename_type
9984 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9985 return cp_parser_make_typename_type (parser, parser->scope,
9987 /* Look up a qualified name in the usual way. */
9992 decl = cp_parser_lookup_name (parser, identifier,
9994 /*is_template=*/false,
9995 /*is_namespace=*/false,
9996 /*check_dependency=*/true,
9997 /*ambiguous_decls=*/NULL);
9999 /* If we are parsing friend declaration, DECL may be a
10000 TEMPLATE_DECL tree node here. However, we need to check
10001 whether this TEMPLATE_DECL results in valid code. Consider
10002 the following example:
10005 template <class T> class C {};
10008 template <class T> friend class N::C; // #1, valid code
10010 template <class T> class Y {
10011 friend class N::C; // #2, invalid code
10014 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10015 name lookup of `N::C'. We see that friend declaration must
10016 be template for the code to be valid. Note that
10017 processing_template_decl does not work here since it is
10018 always 1 for the above two cases. */
10020 decl = (cp_parser_maybe_treat_template_as_class
10021 (decl, /*tag_name_p=*/is_friend
10022 && parser->num_template_parameter_lists));
10024 if (TREE_CODE (decl) != TYPE_DECL)
10026 cp_parser_diagnose_invalid_type_name (parser,
10029 return error_mark_node;
10032 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10033 check_elaborated_type_specifier
10035 (parser->num_template_parameter_lists
10036 || DECL_SELF_REFERENCE_P (decl)));
10038 type = TREE_TYPE (decl);
10042 /* An elaborated-type-specifier sometimes introduces a new type and
10043 sometimes names an existing type. Normally, the rule is that it
10044 introduces a new type only if there is not an existing type of
10045 the same name already in scope. For example, given:
10048 void f() { struct S s; }
10050 the `struct S' in the body of `f' is the same `struct S' as in
10051 the global scope; the existing definition is used. However, if
10052 there were no global declaration, this would introduce a new
10053 local class named `S'.
10055 An exception to this rule applies to the following code:
10057 namespace N { struct S; }
10059 Here, the elaborated-type-specifier names a new type
10060 unconditionally; even if there is already an `S' in the
10061 containing scope this declaration names a new type.
10062 This exception only applies if the elaborated-type-specifier
10063 forms the complete declaration:
10067 A declaration consisting solely of `class-key identifier ;' is
10068 either a redeclaration of the name in the current scope or a
10069 forward declaration of the identifier as a class name. It
10070 introduces the name into the current scope.
10072 We are in this situation precisely when the next token is a `;'.
10074 An exception to the exception is that a `friend' declaration does
10075 *not* name a new type; i.e., given:
10077 struct S { friend struct T; };
10079 `T' is not a new type in the scope of `S'.
10081 Also, `new struct S' or `sizeof (struct S)' never results in the
10082 definition of a new type; a new type can only be declared in a
10083 declaration context. */
10089 /* Friends have special name lookup rules. */
10090 ts = ts_within_enclosing_non_class;
10091 else if (is_declaration
10092 && cp_lexer_next_token_is (parser->lexer,
10094 /* This is a `class-key identifier ;' */
10099 /* Warn about attributes. They are ignored. */
10101 warning (OPT_Wattributes,
10102 "type attributes are honored only at type definition");
10105 (parser->num_template_parameter_lists
10106 && (cp_parser_next_token_starts_class_definition_p (parser)
10107 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10108 type = xref_tag (tag_type, identifier, ts, template_p);
10111 if (tag_type != enum_type)
10112 cp_parser_check_class_key (tag_type, type);
10114 /* A "<" cannot follow an elaborated type specifier. If that
10115 happens, the user was probably trying to form a template-id. */
10116 cp_parser_check_for_invalid_template_id (parser, type);
10121 /* Parse an enum-specifier.
10124 enum identifier [opt] { enumerator-list [opt] }
10127 enum identifier [opt] { enumerator-list [opt] } attributes
10129 Returns an ENUM_TYPE representing the enumeration. */
10132 cp_parser_enum_specifier (cp_parser* parser)
10137 /* Caller guarantees that the current token is 'enum', an identifier
10138 possibly follows, and the token after that is an opening brace.
10139 If we don't have an identifier, fabricate an anonymous name for
10140 the enumeration being defined. */
10141 cp_lexer_consume_token (parser->lexer);
10143 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10144 identifier = cp_parser_identifier (parser);
10146 identifier = make_anon_name ();
10148 /* Issue an error message if type-definitions are forbidden here. */
10149 cp_parser_check_type_definition (parser);
10151 /* Create the new type. We do this before consuming the opening brace
10152 so the enum will be recorded as being on the line of its tag (or the
10153 'enum' keyword, if there is no tag). */
10154 type = start_enum (identifier);
10156 /* Consume the opening brace. */
10157 cp_lexer_consume_token (parser->lexer);
10159 /* If the next token is not '}', then there are some enumerators. */
10160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10161 cp_parser_enumerator_list (parser, type);
10163 /* Consume the final '}'. */
10164 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10166 /* Look for trailing attributes to apply to this enumeration, and
10167 apply them if appropriate. */
10168 if (cp_parser_allow_gnu_extensions_p (parser))
10170 tree trailing_attr = cp_parser_attributes_opt (parser);
10171 cplus_decl_attributes (&type,
10173 (int) ATTR_FLAG_TYPE_IN_PLACE);
10176 /* Finish up the enumeration. */
10177 finish_enum (type);
10182 /* Parse an enumerator-list. The enumerators all have the indicated
10186 enumerator-definition
10187 enumerator-list , enumerator-definition */
10190 cp_parser_enumerator_list (cp_parser* parser, tree type)
10194 /* Parse an enumerator-definition. */
10195 cp_parser_enumerator_definition (parser, type);
10197 /* If the next token is not a ',', we've reached the end of
10199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10201 /* Otherwise, consume the `,' and keep going. */
10202 cp_lexer_consume_token (parser->lexer);
10203 /* If the next token is a `}', there is a trailing comma. */
10204 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10206 if (pedantic && !in_system_header)
10207 pedwarn ("comma at end of enumerator list");
10213 /* Parse an enumerator-definition. The enumerator has the indicated
10216 enumerator-definition:
10218 enumerator = constant-expression
10224 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10229 /* Look for the identifier. */
10230 identifier = cp_parser_identifier (parser);
10231 if (identifier == error_mark_node)
10234 /* If the next token is an '=', then there is an explicit value. */
10235 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10237 /* Consume the `=' token. */
10238 cp_lexer_consume_token (parser->lexer);
10239 /* Parse the value. */
10240 value = cp_parser_constant_expression (parser,
10241 /*allow_non_constant_p=*/false,
10247 /* Create the enumerator. */
10248 build_enumerator (identifier, value, type);
10251 /* Parse a namespace-name.
10254 original-namespace-name
10257 Returns the NAMESPACE_DECL for the namespace. */
10260 cp_parser_namespace_name (cp_parser* parser)
10263 tree namespace_decl;
10265 /* Get the name of the namespace. */
10266 identifier = cp_parser_identifier (parser);
10267 if (identifier == error_mark_node)
10268 return error_mark_node;
10270 /* Look up the identifier in the currently active scope. Look only
10271 for namespaces, due to:
10273 [basic.lookup.udir]
10275 When looking up a namespace-name in a using-directive or alias
10276 definition, only namespace names are considered.
10280 [basic.lookup.qual]
10282 During the lookup of a name preceding the :: scope resolution
10283 operator, object, function, and enumerator names are ignored.
10285 (Note that cp_parser_class_or_namespace_name only calls this
10286 function if the token after the name is the scope resolution
10288 namespace_decl = cp_parser_lookup_name (parser, identifier,
10290 /*is_template=*/false,
10291 /*is_namespace=*/true,
10292 /*check_dependency=*/true,
10293 /*ambiguous_decls=*/NULL);
10294 /* If it's not a namespace, issue an error. */
10295 if (namespace_decl == error_mark_node
10296 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10298 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10299 error ("%qD is not a namespace-name", identifier);
10300 cp_parser_error (parser, "expected namespace-name");
10301 namespace_decl = error_mark_node;
10304 return namespace_decl;
10307 /* Parse a namespace-definition.
10309 namespace-definition:
10310 named-namespace-definition
10311 unnamed-namespace-definition
10313 named-namespace-definition:
10314 original-namespace-definition
10315 extension-namespace-definition
10317 original-namespace-definition:
10318 namespace identifier { namespace-body }
10320 extension-namespace-definition:
10321 namespace original-namespace-name { namespace-body }
10323 unnamed-namespace-definition:
10324 namespace { namespace-body } */
10327 cp_parser_namespace_definition (cp_parser* parser)
10331 /* Look for the `namespace' keyword. */
10332 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10334 /* Get the name of the namespace. We do not attempt to distinguish
10335 between an original-namespace-definition and an
10336 extension-namespace-definition at this point. The semantic
10337 analysis routines are responsible for that. */
10338 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10339 identifier = cp_parser_identifier (parser);
10341 identifier = NULL_TREE;
10343 /* Look for the `{' to start the namespace. */
10344 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10345 /* Start the namespace. */
10346 push_namespace (identifier);
10347 /* Parse the body of the namespace. */
10348 cp_parser_namespace_body (parser);
10349 /* Finish the namespace. */
10351 /* Look for the final `}'. */
10352 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10355 /* Parse a namespace-body.
10358 declaration-seq [opt] */
10361 cp_parser_namespace_body (cp_parser* parser)
10363 cp_parser_declaration_seq_opt (parser);
10366 /* Parse a namespace-alias-definition.
10368 namespace-alias-definition:
10369 namespace identifier = qualified-namespace-specifier ; */
10372 cp_parser_namespace_alias_definition (cp_parser* parser)
10375 tree namespace_specifier;
10377 /* Look for the `namespace' keyword. */
10378 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10379 /* Look for the identifier. */
10380 identifier = cp_parser_identifier (parser);
10381 if (identifier == error_mark_node)
10383 /* Look for the `=' token. */
10384 cp_parser_require (parser, CPP_EQ, "`='");
10385 /* Look for the qualified-namespace-specifier. */
10386 namespace_specifier
10387 = cp_parser_qualified_namespace_specifier (parser);
10388 /* Look for the `;' token. */
10389 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10391 /* Register the alias in the symbol table. */
10392 do_namespace_alias (identifier, namespace_specifier);
10395 /* Parse a qualified-namespace-specifier.
10397 qualified-namespace-specifier:
10398 :: [opt] nested-name-specifier [opt] namespace-name
10400 Returns a NAMESPACE_DECL corresponding to the specified
10404 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10406 /* Look for the optional `::'. */
10407 cp_parser_global_scope_opt (parser,
10408 /*current_scope_valid_p=*/false);
10410 /* Look for the optional nested-name-specifier. */
10411 cp_parser_nested_name_specifier_opt (parser,
10412 /*typename_keyword_p=*/false,
10413 /*check_dependency_p=*/true,
10415 /*is_declaration=*/true);
10417 return cp_parser_namespace_name (parser);
10420 /* Parse a using-declaration.
10423 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10424 using :: unqualified-id ; */
10427 cp_parser_using_declaration (cp_parser* parser)
10430 bool typename_p = false;
10431 bool global_scope_p;
10436 /* Look for the `using' keyword. */
10437 cp_parser_require_keyword (parser, RID_USING, "`using'");
10439 /* Peek at the next token. */
10440 token = cp_lexer_peek_token (parser->lexer);
10441 /* See if it's `typename'. */
10442 if (token->keyword == RID_TYPENAME)
10444 /* Remember that we've seen it. */
10446 /* Consume the `typename' token. */
10447 cp_lexer_consume_token (parser->lexer);
10450 /* Look for the optional global scope qualification. */
10452 = (cp_parser_global_scope_opt (parser,
10453 /*current_scope_valid_p=*/false)
10456 /* If we saw `typename', or didn't see `::', then there must be a
10457 nested-name-specifier present. */
10458 if (typename_p || !global_scope_p)
10459 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10460 /*check_dependency_p=*/true,
10462 /*is_declaration=*/true);
10463 /* Otherwise, we could be in either of the two productions. In that
10464 case, treat the nested-name-specifier as optional. */
10466 qscope = cp_parser_nested_name_specifier_opt (parser,
10467 /*typename_keyword_p=*/false,
10468 /*check_dependency_p=*/true,
10470 /*is_declaration=*/true);
10472 qscope = global_namespace;
10474 /* Parse the unqualified-id. */
10475 identifier = cp_parser_unqualified_id (parser,
10476 /*template_keyword_p=*/false,
10477 /*check_dependency_p=*/true,
10478 /*declarator_p=*/true);
10480 /* The function we call to handle a using-declaration is different
10481 depending on what scope we are in. */
10482 if (identifier == error_mark_node)
10484 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10485 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10486 /* [namespace.udecl]
10488 A using declaration shall not name a template-id. */
10489 error ("a template-id may not appear in a using-declaration");
10492 if (at_class_scope_p ())
10494 /* Create the USING_DECL. */
10495 decl = do_class_using_decl (parser->scope, identifier);
10496 /* Add it to the list of members in this class. */
10497 finish_member_declaration (decl);
10501 decl = cp_parser_lookup_name_simple (parser, identifier);
10502 if (decl == error_mark_node)
10503 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10504 else if (!at_namespace_scope_p ())
10505 do_local_using_decl (decl, qscope, identifier);
10507 do_toplevel_using_decl (decl, qscope, identifier);
10511 /* Look for the final `;'. */
10512 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10515 /* Parse a using-directive.
10518 using namespace :: [opt] nested-name-specifier [opt]
10519 namespace-name ; */
10522 cp_parser_using_directive (cp_parser* parser)
10524 tree namespace_decl;
10527 /* Look for the `using' keyword. */
10528 cp_parser_require_keyword (parser, RID_USING, "`using'");
10529 /* And the `namespace' keyword. */
10530 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10531 /* Look for the optional `::' operator. */
10532 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10533 /* And the optional nested-name-specifier. */
10534 cp_parser_nested_name_specifier_opt (parser,
10535 /*typename_keyword_p=*/false,
10536 /*check_dependency_p=*/true,
10538 /*is_declaration=*/true);
10539 /* Get the namespace being used. */
10540 namespace_decl = cp_parser_namespace_name (parser);
10541 /* And any specified attributes. */
10542 attribs = cp_parser_attributes_opt (parser);
10543 /* Update the symbol table. */
10544 parse_using_directive (namespace_decl, attribs);
10545 /* Look for the final `;'. */
10546 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10549 /* Parse an asm-definition.
10552 asm ( string-literal ) ;
10557 asm volatile [opt] ( string-literal ) ;
10558 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10559 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10560 : asm-operand-list [opt] ) ;
10561 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10562 : asm-operand-list [opt]
10563 : asm-operand-list [opt] ) ; */
10566 cp_parser_asm_definition (cp_parser* parser)
10569 tree outputs = NULL_TREE;
10570 tree inputs = NULL_TREE;
10571 tree clobbers = NULL_TREE;
10573 bool volatile_p = false;
10574 bool extended_p = false;
10576 /* Look for the `asm' keyword. */
10577 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10578 /* See if the next token is `volatile'. */
10579 if (cp_parser_allow_gnu_extensions_p (parser)
10580 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10582 /* Remember that we saw the `volatile' keyword. */
10584 /* Consume the token. */
10585 cp_lexer_consume_token (parser->lexer);
10587 /* Look for the opening `('. */
10588 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10590 /* Look for the string. */
10591 string = cp_parser_string_literal (parser, false, false);
10592 if (string == error_mark_node)
10594 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10595 /*consume_paren=*/true);
10599 /* If we're allowing GNU extensions, check for the extended assembly
10600 syntax. Unfortunately, the `:' tokens need not be separated by
10601 a space in C, and so, for compatibility, we tolerate that here
10602 too. Doing that means that we have to treat the `::' operator as
10604 if (cp_parser_allow_gnu_extensions_p (parser)
10605 && at_function_scope_p ()
10606 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10607 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10609 bool inputs_p = false;
10610 bool clobbers_p = false;
10612 /* The extended syntax was used. */
10615 /* Look for outputs. */
10616 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10618 /* Consume the `:'. */
10619 cp_lexer_consume_token (parser->lexer);
10620 /* Parse the output-operands. */
10621 if (cp_lexer_next_token_is_not (parser->lexer,
10623 && cp_lexer_next_token_is_not (parser->lexer,
10625 && cp_lexer_next_token_is_not (parser->lexer,
10627 outputs = cp_parser_asm_operand_list (parser);
10629 /* If the next token is `::', there are no outputs, and the
10630 next token is the beginning of the inputs. */
10631 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10632 /* The inputs are coming next. */
10635 /* Look for inputs. */
10637 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10639 /* Consume the `:' or `::'. */
10640 cp_lexer_consume_token (parser->lexer);
10641 /* Parse the output-operands. */
10642 if (cp_lexer_next_token_is_not (parser->lexer,
10644 && cp_lexer_next_token_is_not (parser->lexer,
10646 inputs = cp_parser_asm_operand_list (parser);
10648 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10649 /* The clobbers are coming next. */
10652 /* Look for clobbers. */
10654 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10656 /* Consume the `:' or `::'. */
10657 cp_lexer_consume_token (parser->lexer);
10658 /* Parse the clobbers. */
10659 if (cp_lexer_next_token_is_not (parser->lexer,
10661 clobbers = cp_parser_asm_clobber_list (parser);
10664 /* Look for the closing `)'. */
10665 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10666 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10667 /*consume_paren=*/true);
10668 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10670 /* Create the ASM_EXPR. */
10671 if (at_function_scope_p ())
10673 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10675 /* If the extended syntax was not used, mark the ASM_EXPR. */
10678 tree temp = asm_stmt;
10679 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10680 temp = TREE_OPERAND (temp, 0);
10682 ASM_INPUT_P (temp) = 1;
10686 assemble_asm (string);
10689 /* Declarators [gram.dcl.decl] */
10691 /* Parse an init-declarator.
10694 declarator initializer [opt]
10699 declarator asm-specification [opt] attributes [opt] initializer [opt]
10701 function-definition:
10702 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10704 decl-specifier-seq [opt] declarator function-try-block
10708 function-definition:
10709 __extension__ function-definition
10711 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10712 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10713 then this declarator appears in a class scope. The new DECL created
10714 by this declarator is returned.
10716 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10717 for a function-definition here as well. If the declarator is a
10718 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10719 be TRUE upon return. By that point, the function-definition will
10720 have been completely parsed.
10722 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10726 cp_parser_init_declarator (cp_parser* parser,
10727 cp_decl_specifier_seq *decl_specifiers,
10728 bool function_definition_allowed_p,
10730 int declares_class_or_enum,
10731 bool* function_definition_p)
10734 cp_declarator *declarator;
10735 tree prefix_attributes;
10737 tree asm_specification;
10739 tree decl = NULL_TREE;
10741 bool is_initialized;
10742 bool is_parenthesized_init;
10743 bool is_non_constant_init;
10744 int ctor_dtor_or_conv_p;
10746 tree pushed_scope = NULL;
10748 /* Gather the attributes that were provided with the
10749 decl-specifiers. */
10750 prefix_attributes = decl_specifiers->attributes;
10752 /* Assume that this is not the declarator for a function
10754 if (function_definition_p)
10755 *function_definition_p = false;
10757 /* Defer access checks while parsing the declarator; we cannot know
10758 what names are accessible until we know what is being
10760 resume_deferring_access_checks ();
10762 /* Parse the declarator. */
10764 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10765 &ctor_dtor_or_conv_p,
10766 /*parenthesized_p=*/NULL,
10767 /*member_p=*/false);
10768 /* Gather up the deferred checks. */
10769 stop_deferring_access_checks ();
10771 /* If the DECLARATOR was erroneous, there's no need to go
10773 if (declarator == cp_error_declarator)
10774 return error_mark_node;
10776 if (declares_class_or_enum & 2)
10777 cp_parser_check_for_definition_in_return_type (declarator,
10778 decl_specifiers->type);
10780 /* Figure out what scope the entity declared by the DECLARATOR is
10781 located in. `grokdeclarator' sometimes changes the scope, so
10782 we compute it now. */
10783 scope = get_scope_of_declarator (declarator);
10785 /* If we're allowing GNU extensions, look for an asm-specification
10787 if (cp_parser_allow_gnu_extensions_p (parser))
10789 /* Look for an asm-specification. */
10790 asm_specification = cp_parser_asm_specification_opt (parser);
10791 /* And attributes. */
10792 attributes = cp_parser_attributes_opt (parser);
10796 asm_specification = NULL_TREE;
10797 attributes = NULL_TREE;
10800 /* Peek at the next token. */
10801 token = cp_lexer_peek_token (parser->lexer);
10802 /* Check to see if the token indicates the start of a
10803 function-definition. */
10804 if (cp_parser_token_starts_function_definition_p (token))
10806 if (!function_definition_allowed_p)
10808 /* If a function-definition should not appear here, issue an
10810 cp_parser_error (parser,
10811 "a function-definition is not allowed here");
10812 return error_mark_node;
10816 /* Neither attributes nor an asm-specification are allowed
10817 on a function-definition. */
10818 if (asm_specification)
10819 error ("an asm-specification is not allowed on a function-definition");
10821 error ("attributes are not allowed on a function-definition");
10822 /* This is a function-definition. */
10823 *function_definition_p = true;
10825 /* Parse the function definition. */
10827 decl = cp_parser_save_member_function_body (parser,
10830 prefix_attributes);
10833 = (cp_parser_function_definition_from_specifiers_and_declarator
10834 (parser, decl_specifiers, prefix_attributes, declarator));
10842 Only in function declarations for constructors, destructors, and
10843 type conversions can the decl-specifier-seq be omitted.
10845 We explicitly postpone this check past the point where we handle
10846 function-definitions because we tolerate function-definitions
10847 that are missing their return types in some modes. */
10848 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10850 cp_parser_error (parser,
10851 "expected constructor, destructor, or type conversion");
10852 return error_mark_node;
10855 /* An `=' or an `(' indicates an initializer. */
10856 is_initialized = (token->type == CPP_EQ
10857 || token->type == CPP_OPEN_PAREN);
10858 /* If the init-declarator isn't initialized and isn't followed by a
10859 `,' or `;', it's not a valid init-declarator. */
10860 if (!is_initialized
10861 && token->type != CPP_COMMA
10862 && token->type != CPP_SEMICOLON)
10864 cp_parser_error (parser, "expected initializer");
10865 return error_mark_node;
10868 /* Because start_decl has side-effects, we should only call it if we
10869 know we're going ahead. By this point, we know that we cannot
10870 possibly be looking at any other construct. */
10871 cp_parser_commit_to_tentative_parse (parser);
10873 /* If the decl specifiers were bad, issue an error now that we're
10874 sure this was intended to be a declarator. Then continue
10875 declaring the variable(s), as int, to try to cut down on further
10877 if (decl_specifiers->any_specifiers_p
10878 && decl_specifiers->type == error_mark_node)
10880 cp_parser_error (parser, "invalid type in declaration");
10881 decl_specifiers->type = integer_type_node;
10884 /* Check to see whether or not this declaration is a friend. */
10885 friend_p = cp_parser_friend_p (decl_specifiers);
10887 /* Check that the number of template-parameter-lists is OK. */
10888 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10889 return error_mark_node;
10891 /* Enter the newly declared entry in the symbol table. If we're
10892 processing a declaration in a class-specifier, we wait until
10893 after processing the initializer. */
10896 if (parser->in_unbraced_linkage_specification_p)
10898 decl_specifiers->storage_class = sc_extern;
10899 have_extern_spec = false;
10901 decl = start_decl (declarator, decl_specifiers,
10902 is_initialized, attributes, prefix_attributes,
10906 /* Enter the SCOPE. That way unqualified names appearing in the
10907 initializer will be looked up in SCOPE. */
10908 pushed_scope = push_scope (scope);
10910 /* Perform deferred access control checks, now that we know in which
10911 SCOPE the declared entity resides. */
10912 if (!member_p && decl)
10914 tree saved_current_function_decl = NULL_TREE;
10916 /* If the entity being declared is a function, pretend that we
10917 are in its scope. If it is a `friend', it may have access to
10918 things that would not otherwise be accessible. */
10919 if (TREE_CODE (decl) == FUNCTION_DECL)
10921 saved_current_function_decl = current_function_decl;
10922 current_function_decl = decl;
10925 /* Perform the access control checks for the declarator and the
10926 the decl-specifiers. */
10927 perform_deferred_access_checks ();
10929 /* Restore the saved value. */
10930 if (TREE_CODE (decl) == FUNCTION_DECL)
10931 current_function_decl = saved_current_function_decl;
10934 /* Parse the initializer. */
10935 if (is_initialized)
10936 initializer = cp_parser_initializer (parser,
10937 &is_parenthesized_init,
10938 &is_non_constant_init);
10941 initializer = NULL_TREE;
10942 is_parenthesized_init = false;
10943 is_non_constant_init = true;
10946 /* The old parser allows attributes to appear after a parenthesized
10947 initializer. Mark Mitchell proposed removing this functionality
10948 on the GCC mailing lists on 2002-08-13. This parser accepts the
10949 attributes -- but ignores them. */
10950 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10951 if (cp_parser_attributes_opt (parser))
10952 warning (OPT_Wattributes,
10953 "attributes after parenthesized initializer ignored");
10955 /* For an in-class declaration, use `grokfield' to create the
10961 pop_scope (pushed_scope);
10962 pushed_scope = false;
10964 decl = grokfield (declarator, decl_specifiers,
10965 initializer, /*asmspec=*/NULL_TREE,
10966 prefix_attributes);
10967 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10968 cp_parser_save_default_args (parser, decl);
10971 /* Finish processing the declaration. But, skip friend
10973 if (!friend_p && decl && decl != error_mark_node)
10975 cp_finish_decl (decl,
10978 /* If the initializer is in parentheses, then this is
10979 a direct-initialization, which means that an
10980 `explicit' constructor is OK. Otherwise, an
10981 `explicit' constructor cannot be used. */
10982 ((is_parenthesized_init || !is_initialized)
10983 ? 0 : LOOKUP_ONLYCONVERTING));
10985 if (!friend_p && pushed_scope)
10986 pop_scope (pushed_scope);
10988 /* Remember whether or not variables were initialized by
10989 constant-expressions. */
10990 if (decl && TREE_CODE (decl) == VAR_DECL
10991 && is_initialized && !is_non_constant_init)
10992 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10997 /* Parse a declarator.
11001 ptr-operator declarator
11003 abstract-declarator:
11004 ptr-operator abstract-declarator [opt]
11005 direct-abstract-declarator
11010 attributes [opt] direct-declarator
11011 attributes [opt] ptr-operator declarator
11013 abstract-declarator:
11014 attributes [opt] ptr-operator abstract-declarator [opt]
11015 attributes [opt] direct-abstract-declarator
11017 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11018 detect constructor, destructor or conversion operators. It is set
11019 to -1 if the declarator is a name, and +1 if it is a
11020 function. Otherwise it is set to zero. Usually you just want to
11021 test for >0, but internally the negative value is used.
11023 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11024 a decl-specifier-seq unless it declares a constructor, destructor,
11025 or conversion. It might seem that we could check this condition in
11026 semantic analysis, rather than parsing, but that makes it difficult
11027 to handle something like `f()'. We want to notice that there are
11028 no decl-specifiers, and therefore realize that this is an
11029 expression, not a declaration.)
11031 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11032 the declarator is a direct-declarator of the form "(...)".
11034 MEMBER_P is true iff this declarator is a member-declarator. */
11036 static cp_declarator *
11037 cp_parser_declarator (cp_parser* parser,
11038 cp_parser_declarator_kind dcl_kind,
11039 int* ctor_dtor_or_conv_p,
11040 bool* parenthesized_p,
11044 cp_declarator *declarator;
11045 enum tree_code code;
11046 cp_cv_quals cv_quals;
11048 tree attributes = NULL_TREE;
11050 /* Assume this is not a constructor, destructor, or type-conversion
11052 if (ctor_dtor_or_conv_p)
11053 *ctor_dtor_or_conv_p = 0;
11055 if (cp_parser_allow_gnu_extensions_p (parser))
11056 attributes = cp_parser_attributes_opt (parser);
11058 /* Peek at the next token. */
11059 token = cp_lexer_peek_token (parser->lexer);
11061 /* Check for the ptr-operator production. */
11062 cp_parser_parse_tentatively (parser);
11063 /* Parse the ptr-operator. */
11064 code = cp_parser_ptr_operator (parser,
11067 /* If that worked, then we have a ptr-operator. */
11068 if (cp_parser_parse_definitely (parser))
11070 /* If a ptr-operator was found, then this declarator was not
11072 if (parenthesized_p)
11073 *parenthesized_p = true;
11074 /* The dependent declarator is optional if we are parsing an
11075 abstract-declarator. */
11076 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11077 cp_parser_parse_tentatively (parser);
11079 /* Parse the dependent declarator. */
11080 declarator = cp_parser_declarator (parser, dcl_kind,
11081 /*ctor_dtor_or_conv_p=*/NULL,
11082 /*parenthesized_p=*/NULL,
11083 /*member_p=*/false);
11085 /* If we are parsing an abstract-declarator, we must handle the
11086 case where the dependent declarator is absent. */
11087 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11088 && !cp_parser_parse_definitely (parser))
11091 /* Build the representation of the ptr-operator. */
11093 declarator = make_ptrmem_declarator (cv_quals,
11096 else if (code == INDIRECT_REF)
11097 declarator = make_pointer_declarator (cv_quals, declarator);
11099 declarator = make_reference_declarator (cv_quals, declarator);
11101 /* Everything else is a direct-declarator. */
11104 if (parenthesized_p)
11105 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11107 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11108 ctor_dtor_or_conv_p,
11112 if (attributes && declarator != cp_error_declarator)
11113 declarator->attributes = attributes;
11118 /* Parse a direct-declarator or direct-abstract-declarator.
11122 direct-declarator ( parameter-declaration-clause )
11123 cv-qualifier-seq [opt]
11124 exception-specification [opt]
11125 direct-declarator [ constant-expression [opt] ]
11128 direct-abstract-declarator:
11129 direct-abstract-declarator [opt]
11130 ( parameter-declaration-clause )
11131 cv-qualifier-seq [opt]
11132 exception-specification [opt]
11133 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11134 ( abstract-declarator )
11136 Returns a representation of the declarator. DCL_KIND is
11137 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11138 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11139 we are parsing a direct-declarator. It is
11140 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11141 of ambiguity we prefer an abstract declarator, as per
11142 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11143 cp_parser_declarator. */
11145 static cp_declarator *
11146 cp_parser_direct_declarator (cp_parser* parser,
11147 cp_parser_declarator_kind dcl_kind,
11148 int* ctor_dtor_or_conv_p,
11152 cp_declarator *declarator = NULL;
11153 tree scope = NULL_TREE;
11154 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11155 bool saved_in_declarator_p = parser->in_declarator_p;
11157 tree pushed_scope = NULL_TREE;
11161 /* Peek at the next token. */
11162 token = cp_lexer_peek_token (parser->lexer);
11163 if (token->type == CPP_OPEN_PAREN)
11165 /* This is either a parameter-declaration-clause, or a
11166 parenthesized declarator. When we know we are parsing a
11167 named declarator, it must be a parenthesized declarator
11168 if FIRST is true. For instance, `(int)' is a
11169 parameter-declaration-clause, with an omitted
11170 direct-abstract-declarator. But `((*))', is a
11171 parenthesized abstract declarator. Finally, when T is a
11172 template parameter `(T)' is a
11173 parameter-declaration-clause, and not a parenthesized
11176 We first try and parse a parameter-declaration-clause,
11177 and then try a nested declarator (if FIRST is true).
11179 It is not an error for it not to be a
11180 parameter-declaration-clause, even when FIRST is
11186 The first is the declaration of a function while the
11187 second is a the definition of a variable, including its
11190 Having seen only the parenthesis, we cannot know which of
11191 these two alternatives should be selected. Even more
11192 complex are examples like:
11197 The former is a function-declaration; the latter is a
11198 variable initialization.
11200 Thus again, we try a parameter-declaration-clause, and if
11201 that fails, we back out and return. */
11203 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11205 cp_parameter_declarator *params;
11206 unsigned saved_num_template_parameter_lists;
11208 /* In a member-declarator, the only valid interpretation
11209 of a parenthesis is the start of a
11210 parameter-declaration-clause. (It is invalid to
11211 initialize a static data member with a parenthesized
11212 initializer; only the "=" form of initialization is
11215 cp_parser_parse_tentatively (parser);
11217 /* Consume the `('. */
11218 cp_lexer_consume_token (parser->lexer);
11221 /* If this is going to be an abstract declarator, we're
11222 in a declarator and we can't have default args. */
11223 parser->default_arg_ok_p = false;
11224 parser->in_declarator_p = true;
11227 /* Inside the function parameter list, surrounding
11228 template-parameter-lists do not apply. */
11229 saved_num_template_parameter_lists
11230 = parser->num_template_parameter_lists;
11231 parser->num_template_parameter_lists = 0;
11233 /* Parse the parameter-declaration-clause. */
11234 params = cp_parser_parameter_declaration_clause (parser);
11236 parser->num_template_parameter_lists
11237 = saved_num_template_parameter_lists;
11239 /* If all went well, parse the cv-qualifier-seq and the
11240 exception-specification. */
11241 if (member_p || cp_parser_parse_definitely (parser))
11243 cp_cv_quals cv_quals;
11244 tree exception_specification;
11246 if (ctor_dtor_or_conv_p)
11247 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11249 /* Consume the `)'. */
11250 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11252 /* Parse the cv-qualifier-seq. */
11253 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11254 /* And the exception-specification. */
11255 exception_specification
11256 = cp_parser_exception_specification_opt (parser);
11258 /* Create the function-declarator. */
11259 declarator = make_call_declarator (declarator,
11262 exception_specification);
11263 /* Any subsequent parameter lists are to do with
11264 return type, so are not those of the declared
11266 parser->default_arg_ok_p = false;
11268 /* Repeat the main loop. */
11273 /* If this is the first, we can try a parenthesized
11277 bool saved_in_type_id_in_expr_p;
11279 parser->default_arg_ok_p = saved_default_arg_ok_p;
11280 parser->in_declarator_p = saved_in_declarator_p;
11282 /* Consume the `('. */
11283 cp_lexer_consume_token (parser->lexer);
11284 /* Parse the nested declarator. */
11285 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11286 parser->in_type_id_in_expr_p = true;
11288 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11289 /*parenthesized_p=*/NULL,
11291 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11293 /* Expect a `)'. */
11294 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11295 declarator = cp_error_declarator;
11296 if (declarator == cp_error_declarator)
11299 goto handle_declarator;
11301 /* Otherwise, we must be done. */
11305 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11306 && token->type == CPP_OPEN_SQUARE)
11308 /* Parse an array-declarator. */
11311 if (ctor_dtor_or_conv_p)
11312 *ctor_dtor_or_conv_p = 0;
11315 parser->default_arg_ok_p = false;
11316 parser->in_declarator_p = true;
11317 /* Consume the `['. */
11318 cp_lexer_consume_token (parser->lexer);
11319 /* Peek at the next token. */
11320 token = cp_lexer_peek_token (parser->lexer);
11321 /* If the next token is `]', then there is no
11322 constant-expression. */
11323 if (token->type != CPP_CLOSE_SQUARE)
11325 bool non_constant_p;
11328 = cp_parser_constant_expression (parser,
11329 /*allow_non_constant=*/true,
11331 if (!non_constant_p)
11332 bounds = fold_non_dependent_expr (bounds);
11333 /* Normally, the array bound must be an integral constant
11334 expression. However, as an extension, we allow VLAs
11335 in function scopes. */
11336 else if (!at_function_scope_p ())
11338 error ("array bound is not an integer constant");
11339 bounds = error_mark_node;
11343 bounds = NULL_TREE;
11344 /* Look for the closing `]'. */
11345 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11347 declarator = cp_error_declarator;
11351 declarator = make_array_declarator (declarator, bounds);
11353 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11355 tree qualifying_scope;
11356 tree unqualified_name;
11358 /* Parse a declarator-id */
11359 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11360 cp_parser_parse_tentatively (parser);
11361 unqualified_name = cp_parser_declarator_id (parser);
11362 qualifying_scope = parser->scope;
11363 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11365 if (!cp_parser_parse_definitely (parser))
11366 unqualified_name = error_mark_node;
11367 else if (qualifying_scope
11368 || (TREE_CODE (unqualified_name)
11369 != IDENTIFIER_NODE))
11371 cp_parser_error (parser, "expected unqualified-id");
11372 unqualified_name = error_mark_node;
11376 if (unqualified_name == error_mark_node)
11378 declarator = cp_error_declarator;
11382 if (qualifying_scope && at_namespace_scope_p ()
11383 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11385 /* In the declaration of a member of a template class
11386 outside of the class itself, the SCOPE will sometimes
11387 be a TYPENAME_TYPE. For example, given:
11389 template <typename T>
11390 int S<T>::R::i = 3;
11392 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11393 this context, we must resolve S<T>::R to an ordinary
11394 type, rather than a typename type.
11396 The reason we normally avoid resolving TYPENAME_TYPEs
11397 is that a specialization of `S' might render
11398 `S<T>::R' not a type. However, if `S' is
11399 specialized, then this `i' will not be used, so there
11400 is no harm in resolving the types here. */
11403 /* Resolve the TYPENAME_TYPE. */
11404 type = resolve_typename_type (qualifying_scope,
11405 /*only_current_p=*/false);
11406 /* If that failed, the declarator is invalid. */
11407 if (type == error_mark_node)
11408 error ("%<%T::%D%> is not a type",
11409 TYPE_CONTEXT (qualifying_scope),
11410 TYPE_IDENTIFIER (qualifying_scope));
11411 qualifying_scope = type;
11414 declarator = make_id_declarator (qualifying_scope,
11416 declarator->id_loc = token->location;
11417 if (unqualified_name)
11421 if (qualifying_scope
11422 && CLASS_TYPE_P (qualifying_scope))
11423 class_type = qualifying_scope;
11425 class_type = current_class_type;
11429 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11430 declarator->u.id.sfk = sfk_destructor;
11431 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11432 declarator->u.id.sfk = sfk_conversion;
11433 else if (/* There's no way to declare a constructor
11434 for an anonymous type, even if the type
11435 got a name for linkage purposes. */
11436 !TYPE_WAS_ANONYMOUS (class_type)
11437 && (constructor_name_p (unqualified_name,
11439 || (TREE_CODE (unqualified_name) == TYPE_DECL
11441 (TREE_TYPE (unqualified_name),
11443 declarator->u.id.sfk = sfk_constructor;
11445 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11446 *ctor_dtor_or_conv_p = -1;
11447 if (qualifying_scope
11448 && TREE_CODE (unqualified_name) == TYPE_DECL
11449 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11451 error ("invalid use of constructor as a template");
11452 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11453 "the constructor in a qualified name",
11455 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11456 class_type, class_type);
11461 handle_declarator:;
11462 scope = get_scope_of_declarator (declarator);
11464 /* Any names that appear after the declarator-id for a
11465 member are looked up in the containing scope. */
11466 pushed_scope = push_scope (scope);
11467 parser->in_declarator_p = true;
11468 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11469 || (declarator && declarator->kind == cdk_id))
11470 /* Default args are only allowed on function
11472 parser->default_arg_ok_p = saved_default_arg_ok_p;
11474 parser->default_arg_ok_p = false;
11483 /* For an abstract declarator, we might wind up with nothing at this
11484 point. That's an error; the declarator is not optional. */
11486 cp_parser_error (parser, "expected declarator");
11488 /* If we entered a scope, we must exit it now. */
11490 pop_scope (pushed_scope);
11492 parser->default_arg_ok_p = saved_default_arg_ok_p;
11493 parser->in_declarator_p = saved_in_declarator_p;
11498 /* Parse a ptr-operator.
11501 * cv-qualifier-seq [opt]
11503 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11508 & cv-qualifier-seq [opt]
11510 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11511 Returns ADDR_EXPR if a reference was used. In the case of a
11512 pointer-to-member, *TYPE is filled in with the TYPE containing the
11513 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11514 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11515 ERROR_MARK if an error occurred. */
11517 static enum tree_code
11518 cp_parser_ptr_operator (cp_parser* parser,
11520 cp_cv_quals *cv_quals)
11522 enum tree_code code = ERROR_MARK;
11525 /* Assume that it's not a pointer-to-member. */
11527 /* And that there are no cv-qualifiers. */
11528 *cv_quals = TYPE_UNQUALIFIED;
11530 /* Peek at the next token. */
11531 token = cp_lexer_peek_token (parser->lexer);
11532 /* If it's a `*' or `&' we have a pointer or reference. */
11533 if (token->type == CPP_MULT || token->type == CPP_AND)
11535 /* Remember which ptr-operator we were processing. */
11536 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11538 /* Consume the `*' or `&'. */
11539 cp_lexer_consume_token (parser->lexer);
11541 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11542 `&', if we are allowing GNU extensions. (The only qualifier
11543 that can legally appear after `&' is `restrict', but that is
11544 enforced during semantic analysis. */
11545 if (code == INDIRECT_REF
11546 || cp_parser_allow_gnu_extensions_p (parser))
11547 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11551 /* Try the pointer-to-member case. */
11552 cp_parser_parse_tentatively (parser);
11553 /* Look for the optional `::' operator. */
11554 cp_parser_global_scope_opt (parser,
11555 /*current_scope_valid_p=*/false);
11556 /* Look for the nested-name specifier. */
11557 cp_parser_nested_name_specifier (parser,
11558 /*typename_keyword_p=*/false,
11559 /*check_dependency_p=*/true,
11561 /*is_declaration=*/false);
11562 /* If we found it, and the next token is a `*', then we are
11563 indeed looking at a pointer-to-member operator. */
11564 if (!cp_parser_error_occurred (parser)
11565 && cp_parser_require (parser, CPP_MULT, "`*'"))
11567 /* The type of which the member is a member is given by the
11569 *type = parser->scope;
11570 /* The next name will not be qualified. */
11571 parser->scope = NULL_TREE;
11572 parser->qualifying_scope = NULL_TREE;
11573 parser->object_scope = NULL_TREE;
11574 /* Indicate that the `*' operator was used. */
11575 code = INDIRECT_REF;
11576 /* Look for the optional cv-qualifier-seq. */
11577 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11579 /* If that didn't work we don't have a ptr-operator. */
11580 if (!cp_parser_parse_definitely (parser))
11581 cp_parser_error (parser, "expected ptr-operator");
11587 /* Parse an (optional) cv-qualifier-seq.
11590 cv-qualifier cv-qualifier-seq [opt]
11601 Returns a bitmask representing the cv-qualifiers. */
11604 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11606 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11611 cp_cv_quals cv_qualifier;
11613 /* Peek at the next token. */
11614 token = cp_lexer_peek_token (parser->lexer);
11615 /* See if it's a cv-qualifier. */
11616 switch (token->keyword)
11619 cv_qualifier = TYPE_QUAL_CONST;
11623 cv_qualifier = TYPE_QUAL_VOLATILE;
11627 cv_qualifier = TYPE_QUAL_RESTRICT;
11631 cv_qualifier = TYPE_UNQUALIFIED;
11638 if (cv_quals & cv_qualifier)
11640 error ("duplicate cv-qualifier");
11641 cp_lexer_purge_token (parser->lexer);
11645 cp_lexer_consume_token (parser->lexer);
11646 cv_quals |= cv_qualifier;
11653 /* Parse a declarator-id.
11657 :: [opt] nested-name-specifier [opt] type-name
11659 In the `id-expression' case, the value returned is as for
11660 cp_parser_id_expression if the id-expression was an unqualified-id.
11661 If the id-expression was a qualified-id, then a SCOPE_REF is
11662 returned. The first operand is the scope (either a NAMESPACE_DECL
11663 or TREE_TYPE), but the second is still just a representation of an
11667 cp_parser_declarator_id (cp_parser* parser)
11669 /* The expression must be an id-expression. Assume that qualified
11670 names are the names of types so that:
11673 int S<T>::R::i = 3;
11675 will work; we must treat `S<T>::R' as the name of a type.
11676 Similarly, assume that qualified names are templates, where
11680 int S<T>::R<T>::i = 3;
11683 return cp_parser_id_expression (parser,
11684 /*template_keyword_p=*/false,
11685 /*check_dependency_p=*/false,
11686 /*template_p=*/NULL,
11687 /*declarator_p=*/true);
11690 /* Parse a type-id.
11693 type-specifier-seq abstract-declarator [opt]
11695 Returns the TYPE specified. */
11698 cp_parser_type_id (cp_parser* parser)
11700 cp_decl_specifier_seq type_specifier_seq;
11701 cp_declarator *abstract_declarator;
11703 /* Parse the type-specifier-seq. */
11704 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11705 &type_specifier_seq);
11706 if (type_specifier_seq.type == error_mark_node)
11707 return error_mark_node;
11709 /* There might or might not be an abstract declarator. */
11710 cp_parser_parse_tentatively (parser);
11711 /* Look for the declarator. */
11712 abstract_declarator
11713 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11714 /*parenthesized_p=*/NULL,
11715 /*member_p=*/false);
11716 /* Check to see if there really was a declarator. */
11717 if (!cp_parser_parse_definitely (parser))
11718 abstract_declarator = NULL;
11720 return groktypename (&type_specifier_seq, abstract_declarator);
11723 /* Parse a type-specifier-seq.
11725 type-specifier-seq:
11726 type-specifier type-specifier-seq [opt]
11730 type-specifier-seq:
11731 attributes type-specifier-seq [opt]
11733 If IS_CONDITION is true, we are at the start of a "condition",
11734 e.g., we've just seen "if (".
11736 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11739 cp_parser_type_specifier_seq (cp_parser* parser,
11741 cp_decl_specifier_seq *type_specifier_seq)
11743 bool seen_type_specifier = false;
11744 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11746 /* Clear the TYPE_SPECIFIER_SEQ. */
11747 clear_decl_specs (type_specifier_seq);
11749 /* Parse the type-specifiers and attributes. */
11752 tree type_specifier;
11753 bool is_cv_qualifier;
11755 /* Check for attributes first. */
11756 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11758 type_specifier_seq->attributes =
11759 chainon (type_specifier_seq->attributes,
11760 cp_parser_attributes_opt (parser));
11764 /* Look for the type-specifier. */
11765 type_specifier = cp_parser_type_specifier (parser,
11767 type_specifier_seq,
11768 /*is_declaration=*/false,
11771 if (!type_specifier)
11773 /* If the first type-specifier could not be found, this is not a
11774 type-specifier-seq at all. */
11775 if (!seen_type_specifier)
11777 cp_parser_error (parser, "expected type-specifier");
11778 type_specifier_seq->type = error_mark_node;
11781 /* If subsequent type-specifiers could not be found, the
11782 type-specifier-seq is complete. */
11786 seen_type_specifier = true;
11787 /* The standard says that a condition can be:
11789 type-specifier-seq declarator = assignment-expression
11796 we should treat the "S" as a declarator, not as a
11797 type-specifier. The standard doesn't say that explicitly for
11798 type-specifier-seq, but it does say that for
11799 decl-specifier-seq in an ordinary declaration. Perhaps it
11800 would be clearer just to allow a decl-specifier-seq here, and
11801 then add a semantic restriction that if any decl-specifiers
11802 that are not type-specifiers appear, the program is invalid. */
11803 if (is_condition && !is_cv_qualifier)
11804 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11810 /* Parse a parameter-declaration-clause.
11812 parameter-declaration-clause:
11813 parameter-declaration-list [opt] ... [opt]
11814 parameter-declaration-list , ...
11816 Returns a representation for the parameter declarations. A return
11817 value of NULL indicates a parameter-declaration-clause consisting
11818 only of an ellipsis. */
11820 static cp_parameter_declarator *
11821 cp_parser_parameter_declaration_clause (cp_parser* parser)
11823 cp_parameter_declarator *parameters;
11828 /* Peek at the next token. */
11829 token = cp_lexer_peek_token (parser->lexer);
11830 /* Check for trivial parameter-declaration-clauses. */
11831 if (token->type == CPP_ELLIPSIS)
11833 /* Consume the `...' token. */
11834 cp_lexer_consume_token (parser->lexer);
11837 else if (token->type == CPP_CLOSE_PAREN)
11838 /* There are no parameters. */
11840 #ifndef NO_IMPLICIT_EXTERN_C
11841 if (in_system_header && current_class_type == NULL
11842 && current_lang_name == lang_name_c)
11846 return no_parameters;
11848 /* Check for `(void)', too, which is a special case. */
11849 else if (token->keyword == RID_VOID
11850 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11851 == CPP_CLOSE_PAREN))
11853 /* Consume the `void' token. */
11854 cp_lexer_consume_token (parser->lexer);
11855 /* There are no parameters. */
11856 return no_parameters;
11859 /* Parse the parameter-declaration-list. */
11860 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11861 /* If a parse error occurred while parsing the
11862 parameter-declaration-list, then the entire
11863 parameter-declaration-clause is erroneous. */
11867 /* Peek at the next token. */
11868 token = cp_lexer_peek_token (parser->lexer);
11869 /* If it's a `,', the clause should terminate with an ellipsis. */
11870 if (token->type == CPP_COMMA)
11872 /* Consume the `,'. */
11873 cp_lexer_consume_token (parser->lexer);
11874 /* Expect an ellipsis. */
11876 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11878 /* It might also be `...' if the optional trailing `,' was
11880 else if (token->type == CPP_ELLIPSIS)
11882 /* Consume the `...' token. */
11883 cp_lexer_consume_token (parser->lexer);
11884 /* And remember that we saw it. */
11888 ellipsis_p = false;
11890 /* Finish the parameter list. */
11891 if (parameters && ellipsis_p)
11892 parameters->ellipsis_p = true;
11897 /* Parse a parameter-declaration-list.
11899 parameter-declaration-list:
11900 parameter-declaration
11901 parameter-declaration-list , parameter-declaration
11903 Returns a representation of the parameter-declaration-list, as for
11904 cp_parser_parameter_declaration_clause. However, the
11905 `void_list_node' is never appended to the list. Upon return,
11906 *IS_ERROR will be true iff an error occurred. */
11908 static cp_parameter_declarator *
11909 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11911 cp_parameter_declarator *parameters = NULL;
11912 cp_parameter_declarator **tail = ¶meters;
11914 /* Assume all will go well. */
11917 /* Look for more parameters. */
11920 cp_parameter_declarator *parameter;
11921 bool parenthesized_p;
11922 /* Parse the parameter. */
11924 = cp_parser_parameter_declaration (parser,
11925 /*template_parm_p=*/false,
11928 /* If a parse error occurred parsing the parameter declaration,
11929 then the entire parameter-declaration-list is erroneous. */
11936 /* Add the new parameter to the list. */
11938 tail = ¶meter->next;
11940 /* Peek at the next token. */
11941 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11942 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11943 /* These are for Objective-C++ */
11944 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11945 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11946 /* The parameter-declaration-list is complete. */
11948 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11952 /* Peek at the next token. */
11953 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11954 /* If it's an ellipsis, then the list is complete. */
11955 if (token->type == CPP_ELLIPSIS)
11957 /* Otherwise, there must be more parameters. Consume the
11959 cp_lexer_consume_token (parser->lexer);
11960 /* When parsing something like:
11962 int i(float f, double d)
11964 we can tell after seeing the declaration for "f" that we
11965 are not looking at an initialization of a variable "i",
11966 but rather at the declaration of a function "i".
11968 Due to the fact that the parsing of template arguments
11969 (as specified to a template-id) requires backtracking we
11970 cannot use this technique when inside a template argument
11972 if (!parser->in_template_argument_list_p
11973 && !parser->in_type_id_in_expr_p
11974 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11975 /* However, a parameter-declaration of the form
11976 "foat(f)" (which is a valid declaration of a
11977 parameter "f") can also be interpreted as an
11978 expression (the conversion of "f" to "float"). */
11979 && !parenthesized_p)
11980 cp_parser_commit_to_tentative_parse (parser);
11984 cp_parser_error (parser, "expected %<,%> or %<...%>");
11985 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11986 cp_parser_skip_to_closing_parenthesis (parser,
11987 /*recovering=*/true,
11988 /*or_comma=*/false,
11989 /*consume_paren=*/false);
11997 /* Parse a parameter declaration.
11999 parameter-declaration:
12000 decl-specifier-seq declarator
12001 decl-specifier-seq declarator = assignment-expression
12002 decl-specifier-seq abstract-declarator [opt]
12003 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12005 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12006 declares a template parameter. (In that case, a non-nested `>'
12007 token encountered during the parsing of the assignment-expression
12008 is not interpreted as a greater-than operator.)
12010 Returns a representation of the parameter, or NULL if an error
12011 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12012 true iff the declarator is of the form "(p)". */
12014 static cp_parameter_declarator *
12015 cp_parser_parameter_declaration (cp_parser *parser,
12016 bool template_parm_p,
12017 bool *parenthesized_p)
12019 int declares_class_or_enum;
12020 bool greater_than_is_operator_p;
12021 cp_decl_specifier_seq decl_specifiers;
12022 cp_declarator *declarator;
12023 tree default_argument;
12025 const char *saved_message;
12027 /* In a template parameter, `>' is not an operator.
12031 When parsing a default template-argument for a non-type
12032 template-parameter, the first non-nested `>' is taken as the end
12033 of the template parameter-list rather than a greater-than
12035 greater_than_is_operator_p = !template_parm_p;
12037 /* Type definitions may not appear in parameter types. */
12038 saved_message = parser->type_definition_forbidden_message;
12039 parser->type_definition_forbidden_message
12040 = "types may not be defined in parameter types";
12042 /* Parse the declaration-specifiers. */
12043 cp_parser_decl_specifier_seq (parser,
12044 CP_PARSER_FLAGS_NONE,
12046 &declares_class_or_enum);
12047 /* If an error occurred, there's no reason to attempt to parse the
12048 rest of the declaration. */
12049 if (cp_parser_error_occurred (parser))
12051 parser->type_definition_forbidden_message = saved_message;
12055 /* Peek at the next token. */
12056 token = cp_lexer_peek_token (parser->lexer);
12057 /* If the next token is a `)', `,', `=', `>', or `...', then there
12058 is no declarator. */
12059 if (token->type == CPP_CLOSE_PAREN
12060 || token->type == CPP_COMMA
12061 || token->type == CPP_EQ
12062 || token->type == CPP_ELLIPSIS
12063 || token->type == CPP_GREATER)
12066 if (parenthesized_p)
12067 *parenthesized_p = false;
12069 /* Otherwise, there should be a declarator. */
12072 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12073 parser->default_arg_ok_p = false;
12075 /* After seeing a decl-specifier-seq, if the next token is not a
12076 "(", there is no possibility that the code is a valid
12077 expression. Therefore, if parsing tentatively, we commit at
12079 if (!parser->in_template_argument_list_p
12080 /* In an expression context, having seen:
12084 we cannot be sure whether we are looking at a
12085 function-type (taking a "char" as a parameter) or a cast
12086 of some object of type "char" to "int". */
12087 && !parser->in_type_id_in_expr_p
12088 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12089 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12090 cp_parser_commit_to_tentative_parse (parser);
12091 /* Parse the declarator. */
12092 declarator = cp_parser_declarator (parser,
12093 CP_PARSER_DECLARATOR_EITHER,
12094 /*ctor_dtor_or_conv_p=*/NULL,
12096 /*member_p=*/false);
12097 parser->default_arg_ok_p = saved_default_arg_ok_p;
12098 /* After the declarator, allow more attributes. */
12099 decl_specifiers.attributes
12100 = chainon (decl_specifiers.attributes,
12101 cp_parser_attributes_opt (parser));
12104 /* The restriction on defining new types applies only to the type
12105 of the parameter, not to the default argument. */
12106 parser->type_definition_forbidden_message = saved_message;
12108 /* If the next token is `=', then process a default argument. */
12109 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12111 bool saved_greater_than_is_operator_p;
12112 /* Consume the `='. */
12113 cp_lexer_consume_token (parser->lexer);
12115 /* If we are defining a class, then the tokens that make up the
12116 default argument must be saved and processed later. */
12117 if (!template_parm_p && at_class_scope_p ()
12118 && TYPE_BEING_DEFINED (current_class_type))
12120 unsigned depth = 0;
12121 cp_token *first_token;
12124 /* Add tokens until we have processed the entire default
12125 argument. We add the range [first_token, token). */
12126 first_token = cp_lexer_peek_token (parser->lexer);
12131 /* Peek at the next token. */
12132 token = cp_lexer_peek_token (parser->lexer);
12133 /* What we do depends on what token we have. */
12134 switch (token->type)
12136 /* In valid code, a default argument must be
12137 immediately followed by a `,' `)', or `...'. */
12139 case CPP_CLOSE_PAREN:
12141 /* If we run into a non-nested `;', `}', or `]',
12142 then the code is invalid -- but the default
12143 argument is certainly over. */
12144 case CPP_SEMICOLON:
12145 case CPP_CLOSE_BRACE:
12146 case CPP_CLOSE_SQUARE:
12149 /* Update DEPTH, if necessary. */
12150 else if (token->type == CPP_CLOSE_PAREN
12151 || token->type == CPP_CLOSE_BRACE
12152 || token->type == CPP_CLOSE_SQUARE)
12156 case CPP_OPEN_PAREN:
12157 case CPP_OPEN_SQUARE:
12158 case CPP_OPEN_BRACE:
12163 /* If we see a non-nested `>', and `>' is not an
12164 operator, then it marks the end of the default
12166 if (!depth && !greater_than_is_operator_p)
12170 /* If we run out of tokens, issue an error message. */
12172 error ("file ends in default argument");
12178 /* In these cases, we should look for template-ids.
12179 For example, if the default argument is
12180 `X<int, double>()', we need to do name lookup to
12181 figure out whether or not `X' is a template; if
12182 so, the `,' does not end the default argument.
12184 That is not yet done. */
12191 /* If we've reached the end, stop. */
12195 /* Add the token to the token block. */
12196 token = cp_lexer_consume_token (parser->lexer);
12199 /* Create a DEFAULT_ARG to represented the unparsed default
12201 default_argument = make_node (DEFAULT_ARG);
12202 DEFARG_TOKENS (default_argument)
12203 = cp_token_cache_new (first_token, token);
12204 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12206 /* Outside of a class definition, we can just parse the
12207 assignment-expression. */
12210 bool saved_local_variables_forbidden_p;
12212 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12214 saved_greater_than_is_operator_p
12215 = parser->greater_than_is_operator_p;
12216 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12217 /* Local variable names (and the `this' keyword) may not
12218 appear in a default argument. */
12219 saved_local_variables_forbidden_p
12220 = parser->local_variables_forbidden_p;
12221 parser->local_variables_forbidden_p = true;
12222 /* Parse the assignment-expression. */
12224 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12225 /* Restore saved state. */
12226 parser->greater_than_is_operator_p
12227 = saved_greater_than_is_operator_p;
12228 parser->local_variables_forbidden_p
12229 = saved_local_variables_forbidden_p;
12231 if (!parser->default_arg_ok_p)
12233 if (!flag_pedantic_errors)
12234 warning (0, "deprecated use of default argument for parameter of non-function");
12237 error ("default arguments are only permitted for function parameters");
12238 default_argument = NULL_TREE;
12243 default_argument = NULL_TREE;
12245 return make_parameter_declarator (&decl_specifiers,
12250 /* Parse a function-body.
12253 compound_statement */
12256 cp_parser_function_body (cp_parser *parser)
12258 cp_parser_compound_statement (parser, NULL, false);
12261 /* Parse a ctor-initializer-opt followed by a function-body. Return
12262 true if a ctor-initializer was present. */
12265 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12268 bool ctor_initializer_p;
12270 /* Begin the function body. */
12271 body = begin_function_body ();
12272 /* Parse the optional ctor-initializer. */
12273 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12274 /* Parse the function-body. */
12275 cp_parser_function_body (parser);
12276 /* Finish the function body. */
12277 finish_function_body (body);
12279 return ctor_initializer_p;
12282 /* Parse an initializer.
12285 = initializer-clause
12286 ( expression-list )
12288 Returns an expression representing the initializer. If no
12289 initializer is present, NULL_TREE is returned.
12291 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12292 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12293 set to FALSE if there is no initializer present. If there is an
12294 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12295 is set to true; otherwise it is set to false. */
12298 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12299 bool* non_constant_p)
12304 /* Peek at the next token. */
12305 token = cp_lexer_peek_token (parser->lexer);
12307 /* Let our caller know whether or not this initializer was
12309 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12310 /* Assume that the initializer is constant. */
12311 *non_constant_p = false;
12313 if (token->type == CPP_EQ)
12315 /* Consume the `='. */
12316 cp_lexer_consume_token (parser->lexer);
12317 /* Parse the initializer-clause. */
12318 init = cp_parser_initializer_clause (parser, non_constant_p);
12320 else if (token->type == CPP_OPEN_PAREN)
12321 init = cp_parser_parenthesized_expression_list (parser, false,
12326 /* Anything else is an error. */
12327 cp_parser_error (parser, "expected initializer");
12328 init = error_mark_node;
12334 /* Parse an initializer-clause.
12336 initializer-clause:
12337 assignment-expression
12338 { initializer-list , [opt] }
12341 Returns an expression representing the initializer.
12343 If the `assignment-expression' production is used the value
12344 returned is simply a representation for the expression.
12346 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12347 the elements of the initializer-list (or NULL, if the last
12348 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12349 NULL_TREE. There is no way to detect whether or not the optional
12350 trailing `,' was provided. NON_CONSTANT_P is as for
12351 cp_parser_initializer. */
12354 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12358 /* Assume the expression is constant. */
12359 *non_constant_p = false;
12361 /* If it is not a `{', then we are looking at an
12362 assignment-expression. */
12363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12366 = cp_parser_constant_expression (parser,
12367 /*allow_non_constant_p=*/true,
12369 if (!*non_constant_p)
12370 initializer = fold_non_dependent_expr (initializer);
12374 /* Consume the `{' token. */
12375 cp_lexer_consume_token (parser->lexer);
12376 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12377 initializer = make_node (CONSTRUCTOR);
12378 /* If it's not a `}', then there is a non-trivial initializer. */
12379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12381 /* Parse the initializer list. */
12382 CONSTRUCTOR_ELTS (initializer)
12383 = cp_parser_initializer_list (parser, non_constant_p);
12384 /* A trailing `,' token is allowed. */
12385 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12386 cp_lexer_consume_token (parser->lexer);
12388 /* Now, there should be a trailing `}'. */
12389 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12392 return initializer;
12395 /* Parse an initializer-list.
12399 initializer-list , initializer-clause
12404 identifier : initializer-clause
12405 initializer-list, identifier : initializer-clause
12407 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12408 for the initializer. If the INDEX of the elt is non-NULL, it is the
12409 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12410 as for cp_parser_initializer. */
12412 static VEC(constructor_elt,gc) *
12413 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12415 VEC(constructor_elt,gc) *v = NULL;
12417 /* Assume all of the expressions are constant. */
12418 *non_constant_p = false;
12420 /* Parse the rest of the list. */
12426 bool clause_non_constant_p;
12428 /* If the next token is an identifier and the following one is a
12429 colon, we are looking at the GNU designated-initializer
12431 if (cp_parser_allow_gnu_extensions_p (parser)
12432 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12433 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12435 /* Consume the identifier. */
12436 identifier = cp_lexer_consume_token (parser->lexer)->value;
12437 /* Consume the `:'. */
12438 cp_lexer_consume_token (parser->lexer);
12441 identifier = NULL_TREE;
12443 /* Parse the initializer. */
12444 initializer = cp_parser_initializer_clause (parser,
12445 &clause_non_constant_p);
12446 /* If any clause is non-constant, so is the entire initializer. */
12447 if (clause_non_constant_p)
12448 *non_constant_p = true;
12450 /* Add it to the vector. */
12451 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12453 /* If the next token is not a comma, we have reached the end of
12455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12458 /* Peek at the next token. */
12459 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12460 /* If the next token is a `}', then we're still done. An
12461 initializer-clause can have a trailing `,' after the
12462 initializer-list and before the closing `}'. */
12463 if (token->type == CPP_CLOSE_BRACE)
12466 /* Consume the `,' token. */
12467 cp_lexer_consume_token (parser->lexer);
12473 /* Classes [gram.class] */
12475 /* Parse a class-name.
12481 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12482 to indicate that names looked up in dependent types should be
12483 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12484 keyword has been used to indicate that the name that appears next
12485 is a template. TAG_TYPE indicates the explicit tag given before
12486 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12487 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12488 is the class being defined in a class-head.
12490 Returns the TYPE_DECL representing the class. */
12493 cp_parser_class_name (cp_parser *parser,
12494 bool typename_keyword_p,
12495 bool template_keyword_p,
12496 enum tag_types tag_type,
12497 bool check_dependency_p,
12499 bool is_declaration)
12506 /* All class-names start with an identifier. */
12507 token = cp_lexer_peek_token (parser->lexer);
12508 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12510 cp_parser_error (parser, "expected class-name");
12511 return error_mark_node;
12514 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12515 to a template-id, so we save it here. */
12516 scope = parser->scope;
12517 if (scope == error_mark_node)
12518 return error_mark_node;
12520 /* Any name names a type if we're following the `typename' keyword
12521 in a qualified name where the enclosing scope is type-dependent. */
12522 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12523 && dependent_type_p (scope));
12524 /* Handle the common case (an identifier, but not a template-id)
12526 if (token->type == CPP_NAME
12527 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12529 cp_token *identifier_token;
12533 /* Look for the identifier. */
12534 identifier_token = cp_lexer_peek_token (parser->lexer);
12535 ambiguous_p = identifier_token->ambiguous_p;
12536 identifier = cp_parser_identifier (parser);
12537 /* If the next token isn't an identifier, we are certainly not
12538 looking at a class-name. */
12539 if (identifier == error_mark_node)
12540 decl = error_mark_node;
12541 /* If we know this is a type-name, there's no need to look it
12543 else if (typename_p)
12547 tree ambiguous_decls;
12548 /* If we already know that this lookup is ambiguous, then
12549 we've already issued an error message; there's no reason
12553 cp_parser_simulate_error (parser);
12554 return error_mark_node;
12556 /* If the next token is a `::', then the name must be a type
12559 [basic.lookup.qual]
12561 During the lookup for a name preceding the :: scope
12562 resolution operator, object, function, and enumerator
12563 names are ignored. */
12564 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12565 tag_type = typename_type;
12566 /* Look up the name. */
12567 decl = cp_parser_lookup_name (parser, identifier,
12569 /*is_template=*/false,
12570 /*is_namespace=*/false,
12571 check_dependency_p,
12573 if (ambiguous_decls)
12575 error ("reference to %qD is ambiguous", identifier);
12576 print_candidates (ambiguous_decls);
12577 if (cp_parser_parsing_tentatively (parser))
12579 identifier_token->ambiguous_p = true;
12580 cp_parser_simulate_error (parser);
12582 return error_mark_node;
12588 /* Try a template-id. */
12589 decl = cp_parser_template_id (parser, template_keyword_p,
12590 check_dependency_p,
12592 if (decl == error_mark_node)
12593 return error_mark_node;
12596 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12598 /* If this is a typename, create a TYPENAME_TYPE. */
12599 if (typename_p && decl != error_mark_node)
12601 decl = make_typename_type (scope, decl, typename_type,
12602 /*complain=*/tf_error);
12603 if (decl != error_mark_node)
12604 decl = TYPE_NAME (decl);
12607 /* Check to see that it is really the name of a class. */
12608 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12609 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12610 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12611 /* Situations like this:
12613 template <typename T> struct A {
12614 typename T::template X<int>::I i;
12617 are problematic. Is `T::template X<int>' a class-name? The
12618 standard does not seem to be definitive, but there is no other
12619 valid interpretation of the following `::'. Therefore, those
12620 names are considered class-names. */
12621 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12622 else if (decl == error_mark_node
12623 || TREE_CODE (decl) != TYPE_DECL
12624 || TREE_TYPE (decl) == error_mark_node
12625 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12627 cp_parser_error (parser, "expected class-name");
12628 return error_mark_node;
12634 /* Parse a class-specifier.
12637 class-head { member-specification [opt] }
12639 Returns the TREE_TYPE representing the class. */
12642 cp_parser_class_specifier (cp_parser* parser)
12646 tree attributes = NULL_TREE;
12647 int has_trailing_semicolon;
12648 bool nested_name_specifier_p;
12649 unsigned saved_num_template_parameter_lists;
12650 tree old_scope = NULL_TREE;
12651 tree scope = NULL_TREE;
12653 push_deferring_access_checks (dk_no_deferred);
12655 /* Parse the class-head. */
12656 type = cp_parser_class_head (parser,
12657 &nested_name_specifier_p,
12659 /* If the class-head was a semantic disaster, skip the entire body
12663 cp_parser_skip_to_end_of_block_or_statement (parser);
12664 pop_deferring_access_checks ();
12665 return error_mark_node;
12668 /* Look for the `{'. */
12669 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12671 pop_deferring_access_checks ();
12672 return error_mark_node;
12675 /* Issue an error message if type-definitions are forbidden here. */
12676 cp_parser_check_type_definition (parser);
12677 /* Remember that we are defining one more class. */
12678 ++parser->num_classes_being_defined;
12679 /* Inside the class, surrounding template-parameter-lists do not
12681 saved_num_template_parameter_lists
12682 = parser->num_template_parameter_lists;
12683 parser->num_template_parameter_lists = 0;
12685 /* Start the class. */
12686 if (nested_name_specifier_p)
12688 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12689 old_scope = push_inner_scope (scope);
12691 type = begin_class_definition (type);
12693 if (type == error_mark_node)
12694 /* If the type is erroneous, skip the entire body of the class. */
12695 cp_parser_skip_to_closing_brace (parser);
12697 /* Parse the member-specification. */
12698 cp_parser_member_specification_opt (parser);
12700 /* Look for the trailing `}'. */
12701 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12702 /* We get better error messages by noticing a common problem: a
12703 missing trailing `;'. */
12704 token = cp_lexer_peek_token (parser->lexer);
12705 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12706 /* Look for trailing attributes to apply to this class. */
12707 if (cp_parser_allow_gnu_extensions_p (parser))
12709 tree sub_attr = cp_parser_attributes_opt (parser);
12710 attributes = chainon (attributes, sub_attr);
12712 if (type != error_mark_node)
12713 type = finish_struct (type, attributes);
12714 if (nested_name_specifier_p)
12715 pop_inner_scope (old_scope, scope);
12716 /* If this class is not itself within the scope of another class,
12717 then we need to parse the bodies of all of the queued function
12718 definitions. Note that the queued functions defined in a class
12719 are not always processed immediately following the
12720 class-specifier for that class. Consider:
12723 struct B { void f() { sizeof (A); } };
12726 If `f' were processed before the processing of `A' were
12727 completed, there would be no way to compute the size of `A'.
12728 Note that the nesting we are interested in here is lexical --
12729 not the semantic nesting given by TYPE_CONTEXT. In particular,
12732 struct A { struct B; };
12733 struct A::B { void f() { } };
12735 there is no need to delay the parsing of `A::B::f'. */
12736 if (--parser->num_classes_being_defined == 0)
12740 tree class_type = NULL_TREE;
12741 tree pushed_scope = NULL_TREE;
12743 /* In a first pass, parse default arguments to the functions.
12744 Then, in a second pass, parse the bodies of the functions.
12745 This two-phased approach handles cases like:
12753 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12754 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12755 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12756 TREE_PURPOSE (parser->unparsed_functions_queues)
12757 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12759 fn = TREE_VALUE (queue_entry);
12760 /* If there are default arguments that have not yet been processed,
12761 take care of them now. */
12762 if (class_type != TREE_PURPOSE (queue_entry))
12765 pop_scope (pushed_scope);
12766 class_type = TREE_PURPOSE (queue_entry);
12767 pushed_scope = push_scope (class_type);
12769 /* Make sure that any template parameters are in scope. */
12770 maybe_begin_member_template_processing (fn);
12771 /* Parse the default argument expressions. */
12772 cp_parser_late_parsing_default_args (parser, fn);
12773 /* Remove any template parameters from the symbol table. */
12774 maybe_end_member_template_processing ();
12777 pop_scope (pushed_scope);
12778 /* Now parse the body of the functions. */
12779 for (TREE_VALUE (parser->unparsed_functions_queues)
12780 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12781 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12782 TREE_VALUE (parser->unparsed_functions_queues)
12783 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12785 /* Figure out which function we need to process. */
12786 fn = TREE_VALUE (queue_entry);
12787 /* Parse the function. */
12788 cp_parser_late_parsing_for_member (parser, fn);
12792 /* Put back any saved access checks. */
12793 pop_deferring_access_checks ();
12795 /* Restore the count of active template-parameter-lists. */
12796 parser->num_template_parameter_lists
12797 = saved_num_template_parameter_lists;
12802 /* Parse a class-head.
12805 class-key identifier [opt] base-clause [opt]
12806 class-key nested-name-specifier identifier base-clause [opt]
12807 class-key nested-name-specifier [opt] template-id
12811 class-key attributes identifier [opt] base-clause [opt]
12812 class-key attributes nested-name-specifier identifier base-clause [opt]
12813 class-key attributes nested-name-specifier [opt] template-id
12816 Returns the TYPE of the indicated class. Sets
12817 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12818 involving a nested-name-specifier was used, and FALSE otherwise.
12820 Returns error_mark_node if this is not a class-head.
12822 Returns NULL_TREE if the class-head is syntactically valid, but
12823 semantically invalid in a way that means we should skip the entire
12824 body of the class. */
12827 cp_parser_class_head (cp_parser* parser,
12828 bool* nested_name_specifier_p,
12829 tree *attributes_p)
12831 tree nested_name_specifier;
12832 enum tag_types class_key;
12833 tree id = NULL_TREE;
12834 tree type = NULL_TREE;
12836 bool template_id_p = false;
12837 bool qualified_p = false;
12838 bool invalid_nested_name_p = false;
12839 bool invalid_explicit_specialization_p = false;
12840 tree pushed_scope = NULL_TREE;
12841 unsigned num_templates;
12844 /* Assume no nested-name-specifier will be present. */
12845 *nested_name_specifier_p = false;
12846 /* Assume no template parameter lists will be used in defining the
12850 /* Look for the class-key. */
12851 class_key = cp_parser_class_key (parser);
12852 if (class_key == none_type)
12853 return error_mark_node;
12855 /* Parse the attributes. */
12856 attributes = cp_parser_attributes_opt (parser);
12858 /* If the next token is `::', that is invalid -- but sometimes
12859 people do try to write:
12863 Handle this gracefully by accepting the extra qualifier, and then
12864 issuing an error about it later if this really is a
12865 class-head. If it turns out just to be an elaborated type
12866 specifier, remain silent. */
12867 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12868 qualified_p = true;
12870 push_deferring_access_checks (dk_no_check);
12872 /* Determine the name of the class. Begin by looking for an
12873 optional nested-name-specifier. */
12874 nested_name_specifier
12875 = cp_parser_nested_name_specifier_opt (parser,
12876 /*typename_keyword_p=*/false,
12877 /*check_dependency_p=*/false,
12879 /*is_declaration=*/false);
12880 /* If there was a nested-name-specifier, then there *must* be an
12882 if (nested_name_specifier)
12884 /* Although the grammar says `identifier', it really means
12885 `class-name' or `template-name'. You are only allowed to
12886 define a class that has already been declared with this
12889 The proposed resolution for Core Issue 180 says that whever
12890 you see `class T::X' you should treat `X' as a type-name.
12892 It is OK to define an inaccessible class; for example:
12894 class A { class B; };
12897 We do not know if we will see a class-name, or a
12898 template-name. We look for a class-name first, in case the
12899 class-name is a template-id; if we looked for the
12900 template-name first we would stop after the template-name. */
12901 cp_parser_parse_tentatively (parser);
12902 type = cp_parser_class_name (parser,
12903 /*typename_keyword_p=*/false,
12904 /*template_keyword_p=*/false,
12906 /*check_dependency_p=*/false,
12907 /*class_head_p=*/true,
12908 /*is_declaration=*/false);
12909 /* If that didn't work, ignore the nested-name-specifier. */
12910 if (!cp_parser_parse_definitely (parser))
12912 invalid_nested_name_p = true;
12913 id = cp_parser_identifier (parser);
12914 if (id == error_mark_node)
12917 /* If we could not find a corresponding TYPE, treat this
12918 declaration like an unqualified declaration. */
12919 if (type == error_mark_node)
12920 nested_name_specifier = NULL_TREE;
12921 /* Otherwise, count the number of templates used in TYPE and its
12922 containing scopes. */
12927 for (scope = TREE_TYPE (type);
12928 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12929 scope = (TYPE_P (scope)
12930 ? TYPE_CONTEXT (scope)
12931 : DECL_CONTEXT (scope)))
12933 && CLASS_TYPE_P (scope)
12934 && CLASSTYPE_TEMPLATE_INFO (scope)
12935 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12936 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12940 /* Otherwise, the identifier is optional. */
12943 /* We don't know whether what comes next is a template-id,
12944 an identifier, or nothing at all. */
12945 cp_parser_parse_tentatively (parser);
12946 /* Check for a template-id. */
12947 id = cp_parser_template_id (parser,
12948 /*template_keyword_p=*/false,
12949 /*check_dependency_p=*/true,
12950 /*is_declaration=*/true);
12951 /* If that didn't work, it could still be an identifier. */
12952 if (!cp_parser_parse_definitely (parser))
12954 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12955 id = cp_parser_identifier (parser);
12961 template_id_p = true;
12966 pop_deferring_access_checks ();
12969 cp_parser_check_for_invalid_template_id (parser, id);
12971 /* If it's not a `:' or a `{' then we can't really be looking at a
12972 class-head, since a class-head only appears as part of a
12973 class-specifier. We have to detect this situation before calling
12974 xref_tag, since that has irreversible side-effects. */
12975 if (!cp_parser_next_token_starts_class_definition_p (parser))
12977 cp_parser_error (parser, "expected %<{%> or %<:%>");
12978 return error_mark_node;
12981 /* At this point, we're going ahead with the class-specifier, even
12982 if some other problem occurs. */
12983 cp_parser_commit_to_tentative_parse (parser);
12984 /* Issue the error about the overly-qualified name now. */
12986 cp_parser_error (parser,
12987 "global qualification of class name is invalid");
12988 else if (invalid_nested_name_p)
12989 cp_parser_error (parser,
12990 "qualified name does not name a class");
12991 else if (nested_name_specifier)
12995 /* Reject typedef-names in class heads. */
12996 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12998 error ("invalid class name in declaration of %qD", type);
13003 /* Figure out in what scope the declaration is being placed. */
13004 scope = current_scope ();
13005 /* If that scope does not contain the scope in which the
13006 class was originally declared, the program is invalid. */
13007 if (scope && !is_ancestor (scope, nested_name_specifier))
13009 error ("declaration of %qD in %qD which does not enclose %qD",
13010 type, scope, nested_name_specifier);
13016 A declarator-id shall not be qualified exception of the
13017 definition of a ... nested class outside of its class
13018 ... [or] a the definition or explicit instantiation of a
13019 class member of a namespace outside of its namespace. */
13020 if (scope == nested_name_specifier)
13022 pedwarn ("extra qualification ignored");
13023 nested_name_specifier = NULL_TREE;
13027 /* An explicit-specialization must be preceded by "template <>". If
13028 it is not, try to recover gracefully. */
13029 if (at_namespace_scope_p ()
13030 && parser->num_template_parameter_lists == 0
13033 error ("an explicit specialization must be preceded by %<template <>%>");
13034 invalid_explicit_specialization_p = true;
13035 /* Take the same action that would have been taken by
13036 cp_parser_explicit_specialization. */
13037 ++parser->num_template_parameter_lists;
13038 begin_specialization ();
13040 /* There must be no "return" statements between this point and the
13041 end of this function; set "type "to the correct return value and
13042 use "goto done;" to return. */
13043 /* Make sure that the right number of template parameters were
13045 if (!cp_parser_check_template_parameters (parser, num_templates))
13047 /* If something went wrong, there is no point in even trying to
13048 process the class-definition. */
13053 /* Look up the type. */
13056 type = TREE_TYPE (id);
13057 maybe_process_partial_specialization (type);
13058 if (nested_name_specifier)
13059 pushed_scope = push_scope (nested_name_specifier);
13061 else if (nested_name_specifier)
13067 template <typename T> struct S { struct T };
13068 template <typename T> struct S<T>::T { };
13070 we will get a TYPENAME_TYPE when processing the definition of
13071 `S::T'. We need to resolve it to the actual type before we
13072 try to define it. */
13073 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13075 class_type = resolve_typename_type (TREE_TYPE (type),
13076 /*only_current_p=*/false);
13077 if (class_type != error_mark_node)
13078 type = TYPE_NAME (class_type);
13081 cp_parser_error (parser, "could not resolve typename type");
13082 type = error_mark_node;
13086 maybe_process_partial_specialization (TREE_TYPE (type));
13087 class_type = current_class_type;
13088 /* Enter the scope indicated by the nested-name-specifier. */
13089 pushed_scope = push_scope (nested_name_specifier);
13090 /* Get the canonical version of this type. */
13091 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13092 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13093 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13095 type = push_template_decl (type);
13096 if (type == error_mark_node)
13103 type = TREE_TYPE (type);
13104 *nested_name_specifier_p = true;
13106 else /* The name is not a nested name. */
13108 /* If the class was unnamed, create a dummy name. */
13110 id = make_anon_name ();
13111 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13112 parser->num_template_parameter_lists);
13115 /* Indicate whether this class was declared as a `class' or as a
13117 if (TREE_CODE (type) == RECORD_TYPE)
13118 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13119 cp_parser_check_class_key (class_key, type);
13121 /* If this type was already complete, and we see another definition,
13122 that's an error. */
13123 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13125 error ("redefinition of %q#T", type);
13126 error ("previous definition of %q+#T", type);
13131 /* We will have entered the scope containing the class; the names of
13132 base classes should be looked up in that context. For example:
13134 struct A { struct B {}; struct C; };
13135 struct A::C : B {};
13140 /* Get the list of base-classes, if there is one. */
13141 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13142 bases = cp_parser_base_clause (parser);
13144 /* Process the base classes. */
13145 xref_basetypes (type, bases);
13148 /* Leave the scope given by the nested-name-specifier. We will
13149 enter the class scope itself while processing the members. */
13151 pop_scope (pushed_scope);
13153 if (invalid_explicit_specialization_p)
13155 end_specialization ();
13156 --parser->num_template_parameter_lists;
13158 *attributes_p = attributes;
13162 /* Parse a class-key.
13169 Returns the kind of class-key specified, or none_type to indicate
13172 static enum tag_types
13173 cp_parser_class_key (cp_parser* parser)
13176 enum tag_types tag_type;
13178 /* Look for the class-key. */
13179 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13183 /* Check to see if the TOKEN is a class-key. */
13184 tag_type = cp_parser_token_is_class_key (token);
13186 cp_parser_error (parser, "expected class-key");
13190 /* Parse an (optional) member-specification.
13192 member-specification:
13193 member-declaration member-specification [opt]
13194 access-specifier : member-specification [opt] */
13197 cp_parser_member_specification_opt (cp_parser* parser)
13204 /* Peek at the next token. */
13205 token = cp_lexer_peek_token (parser->lexer);
13206 /* If it's a `}', or EOF then we've seen all the members. */
13207 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13210 /* See if this token is a keyword. */
13211 keyword = token->keyword;
13215 case RID_PROTECTED:
13217 /* Consume the access-specifier. */
13218 cp_lexer_consume_token (parser->lexer);
13219 /* Remember which access-specifier is active. */
13220 current_access_specifier = token->value;
13221 /* Look for the `:'. */
13222 cp_parser_require (parser, CPP_COLON, "`:'");
13226 /* Accept #pragmas at class scope. */
13227 if (token->type == CPP_PRAGMA)
13229 cp_lexer_handle_pragma (parser->lexer);
13233 /* Otherwise, the next construction must be a
13234 member-declaration. */
13235 cp_parser_member_declaration (parser);
13240 /* Parse a member-declaration.
13242 member-declaration:
13243 decl-specifier-seq [opt] member-declarator-list [opt] ;
13244 function-definition ; [opt]
13245 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13247 template-declaration
13249 member-declarator-list:
13251 member-declarator-list , member-declarator
13254 declarator pure-specifier [opt]
13255 declarator constant-initializer [opt]
13256 identifier [opt] : constant-expression
13260 member-declaration:
13261 __extension__ member-declaration
13264 declarator attributes [opt] pure-specifier [opt]
13265 declarator attributes [opt] constant-initializer [opt]
13266 identifier [opt] attributes [opt] : constant-expression */
13269 cp_parser_member_declaration (cp_parser* parser)
13271 cp_decl_specifier_seq decl_specifiers;
13272 tree prefix_attributes;
13274 int declares_class_or_enum;
13277 int saved_pedantic;
13279 /* Check for the `__extension__' keyword. */
13280 if (cp_parser_extension_opt (parser, &saved_pedantic))
13283 cp_parser_member_declaration (parser);
13284 /* Restore the old value of the PEDANTIC flag. */
13285 pedantic = saved_pedantic;
13290 /* Check for a template-declaration. */
13291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13293 /* An explicit specialization here is an error condition, and we
13294 expect the specialization handler to detect and report this. */
13295 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13296 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13297 cp_parser_explicit_specialization (parser);
13299 cp_parser_template_declaration (parser, /*member_p=*/true);
13304 /* Check for a using-declaration. */
13305 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13307 /* Parse the using-declaration. */
13308 cp_parser_using_declaration (parser);
13313 /* Check for @defs. */
13314 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13317 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13318 ivar = ivar_chains;
13322 ivar = TREE_CHAIN (member);
13323 TREE_CHAIN (member) = NULL_TREE;
13324 finish_member_declaration (member);
13329 /* Parse the decl-specifier-seq. */
13330 cp_parser_decl_specifier_seq (parser,
13331 CP_PARSER_FLAGS_OPTIONAL,
13333 &declares_class_or_enum);
13334 prefix_attributes = decl_specifiers.attributes;
13335 decl_specifiers.attributes = NULL_TREE;
13336 /* Check for an invalid type-name. */
13337 if (!decl_specifiers.type
13338 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13340 /* If there is no declarator, then the decl-specifier-seq should
13342 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13344 /* If there was no decl-specifier-seq, and the next token is a
13345 `;', then we have something like:
13351 Each member-declaration shall declare at least one member
13352 name of the class. */
13353 if (!decl_specifiers.any_specifiers_p)
13355 cp_token *token = cp_lexer_peek_token (parser->lexer);
13356 if (pedantic && !token->in_system_header)
13357 pedwarn ("%Hextra %<;%>", &token->location);
13363 /* See if this declaration is a friend. */
13364 friend_p = cp_parser_friend_p (&decl_specifiers);
13365 /* If there were decl-specifiers, check to see if there was
13366 a class-declaration. */
13367 type = check_tag_decl (&decl_specifiers);
13368 /* Nested classes have already been added to the class, but
13369 a `friend' needs to be explicitly registered. */
13372 /* If the `friend' keyword was present, the friend must
13373 be introduced with a class-key. */
13374 if (!declares_class_or_enum)
13375 error ("a class-key must be used when declaring a friend");
13378 template <typename T> struct A {
13379 friend struct A<T>::B;
13382 A<T>::B will be represented by a TYPENAME_TYPE, and
13383 therefore not recognized by check_tag_decl. */
13385 && decl_specifiers.type
13386 && TYPE_P (decl_specifiers.type))
13387 type = decl_specifiers.type;
13388 if (!type || !TYPE_P (type))
13389 error ("friend declaration does not name a class or "
13392 make_friend_class (current_class_type, type,
13393 /*complain=*/true);
13395 /* If there is no TYPE, an error message will already have
13397 else if (!type || type == error_mark_node)
13399 /* An anonymous aggregate has to be handled specially; such
13400 a declaration really declares a data member (with a
13401 particular type), as opposed to a nested class. */
13402 else if (ANON_AGGR_TYPE_P (type))
13404 /* Remove constructors and such from TYPE, now that we
13405 know it is an anonymous aggregate. */
13406 fixup_anonymous_aggr (type);
13407 /* And make the corresponding data member. */
13408 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13409 /* Add it to the class. */
13410 finish_member_declaration (decl);
13413 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13418 /* See if these declarations will be friends. */
13419 friend_p = cp_parser_friend_p (&decl_specifiers);
13421 /* Keep going until we hit the `;' at the end of the
13423 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13425 tree attributes = NULL_TREE;
13426 tree first_attribute;
13428 /* Peek at the next token. */
13429 token = cp_lexer_peek_token (parser->lexer);
13431 /* Check for a bitfield declaration. */
13432 if (token->type == CPP_COLON
13433 || (token->type == CPP_NAME
13434 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13440 /* Get the name of the bitfield. Note that we cannot just
13441 check TOKEN here because it may have been invalidated by
13442 the call to cp_lexer_peek_nth_token above. */
13443 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13444 identifier = cp_parser_identifier (parser);
13446 identifier = NULL_TREE;
13448 /* Consume the `:' token. */
13449 cp_lexer_consume_token (parser->lexer);
13450 /* Get the width of the bitfield. */
13452 = cp_parser_constant_expression (parser,
13453 /*allow_non_constant=*/false,
13456 /* Look for attributes that apply to the bitfield. */
13457 attributes = cp_parser_attributes_opt (parser);
13458 /* Remember which attributes are prefix attributes and
13460 first_attribute = attributes;
13461 /* Combine the attributes. */
13462 attributes = chainon (prefix_attributes, attributes);
13464 /* Create the bitfield declaration. */
13465 decl = grokbitfield (identifier
13466 ? make_id_declarator (NULL_TREE,
13471 /* Apply the attributes. */
13472 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13476 cp_declarator *declarator;
13478 tree asm_specification;
13479 int ctor_dtor_or_conv_p;
13481 /* Parse the declarator. */
13483 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13484 &ctor_dtor_or_conv_p,
13485 /*parenthesized_p=*/NULL,
13486 /*member_p=*/true);
13488 /* If something went wrong parsing the declarator, make sure
13489 that we at least consume some tokens. */
13490 if (declarator == cp_error_declarator)
13492 /* Skip to the end of the statement. */
13493 cp_parser_skip_to_end_of_statement (parser);
13494 /* If the next token is not a semicolon, that is
13495 probably because we just skipped over the body of
13496 a function. So, we consume a semicolon if
13497 present, but do not issue an error message if it
13499 if (cp_lexer_next_token_is (parser->lexer,
13501 cp_lexer_consume_token (parser->lexer);
13505 if (declares_class_or_enum & 2)
13506 cp_parser_check_for_definition_in_return_type
13507 (declarator, decl_specifiers.type);
13509 /* Look for an asm-specification. */
13510 asm_specification = cp_parser_asm_specification_opt (parser);
13511 /* Look for attributes that apply to the declaration. */
13512 attributes = cp_parser_attributes_opt (parser);
13513 /* Remember which attributes are prefix attributes and
13515 first_attribute = attributes;
13516 /* Combine the attributes. */
13517 attributes = chainon (prefix_attributes, attributes);
13519 /* If it's an `=', then we have a constant-initializer or a
13520 pure-specifier. It is not correct to parse the
13521 initializer before registering the member declaration
13522 since the member declaration should be in scope while
13523 its initializer is processed. However, the rest of the
13524 front end does not yet provide an interface that allows
13525 us to handle this correctly. */
13526 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13530 A pure-specifier shall be used only in the declaration of
13531 a virtual function.
13533 A member-declarator can contain a constant-initializer
13534 only if it declares a static member of integral or
13537 Therefore, if the DECLARATOR is for a function, we look
13538 for a pure-specifier; otherwise, we look for a
13539 constant-initializer. When we call `grokfield', it will
13540 perform more stringent semantics checks. */
13541 if (declarator->kind == cdk_function)
13542 initializer = cp_parser_pure_specifier (parser);
13544 /* Parse the initializer. */
13545 initializer = cp_parser_constant_initializer (parser);
13547 /* Otherwise, there is no initializer. */
13549 initializer = NULL_TREE;
13551 /* See if we are probably looking at a function
13552 definition. We are certainly not looking at a
13553 member-declarator. Calling `grokfield' has
13554 side-effects, so we must not do it unless we are sure
13555 that we are looking at a member-declarator. */
13556 if (cp_parser_token_starts_function_definition_p
13557 (cp_lexer_peek_token (parser->lexer)))
13559 /* The grammar does not allow a pure-specifier to be
13560 used when a member function is defined. (It is
13561 possible that this fact is an oversight in the
13562 standard, since a pure function may be defined
13563 outside of the class-specifier. */
13565 error ("pure-specifier on function-definition");
13566 decl = cp_parser_save_member_function_body (parser,
13570 /* If the member was not a friend, declare it here. */
13572 finish_member_declaration (decl);
13573 /* Peek at the next token. */
13574 token = cp_lexer_peek_token (parser->lexer);
13575 /* If the next token is a semicolon, consume it. */
13576 if (token->type == CPP_SEMICOLON)
13577 cp_lexer_consume_token (parser->lexer);
13582 /* Create the declaration. */
13583 decl = grokfield (declarator, &decl_specifiers,
13584 initializer, asm_specification,
13586 /* Any initialization must have been from a
13587 constant-expression. */
13588 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13589 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13593 /* Reset PREFIX_ATTRIBUTES. */
13594 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13595 attributes = TREE_CHAIN (attributes);
13597 TREE_CHAIN (attributes) = NULL_TREE;
13599 /* If there is any qualification still in effect, clear it
13600 now; we will be starting fresh with the next declarator. */
13601 parser->scope = NULL_TREE;
13602 parser->qualifying_scope = NULL_TREE;
13603 parser->object_scope = NULL_TREE;
13604 /* If it's a `,', then there are more declarators. */
13605 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13606 cp_lexer_consume_token (parser->lexer);
13607 /* If the next token isn't a `;', then we have a parse error. */
13608 else if (cp_lexer_next_token_is_not (parser->lexer,
13611 cp_parser_error (parser, "expected %<;%>");
13612 /* Skip tokens until we find a `;'. */
13613 cp_parser_skip_to_end_of_statement (parser);
13620 /* Add DECL to the list of members. */
13622 finish_member_declaration (decl);
13624 if (TREE_CODE (decl) == FUNCTION_DECL)
13625 cp_parser_save_default_args (parser, decl);
13630 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13633 /* Parse a pure-specifier.
13638 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13639 Otherwise, ERROR_MARK_NODE is returned. */
13642 cp_parser_pure_specifier (cp_parser* parser)
13646 /* Look for the `=' token. */
13647 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13648 return error_mark_node;
13649 /* Look for the `0' token. */
13650 token = cp_lexer_consume_token (parser->lexer);
13651 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13652 if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13653 return integer_zero_node;
13655 cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13656 cp_parser_skip_to_end_of_statement (parser);
13657 return error_mark_node;
13660 /* Parse a constant-initializer.
13662 constant-initializer:
13663 = constant-expression
13665 Returns a representation of the constant-expression. */
13668 cp_parser_constant_initializer (cp_parser* parser)
13670 /* Look for the `=' token. */
13671 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13672 return error_mark_node;
13674 /* It is invalid to write:
13676 struct S { static const int i = { 7 }; };
13679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13681 cp_parser_error (parser,
13682 "a brace-enclosed initializer is not allowed here");
13683 /* Consume the opening brace. */
13684 cp_lexer_consume_token (parser->lexer);
13685 /* Skip the initializer. */
13686 cp_parser_skip_to_closing_brace (parser);
13687 /* Look for the trailing `}'. */
13688 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13690 return error_mark_node;
13693 return cp_parser_constant_expression (parser,
13694 /*allow_non_constant=*/false,
13698 /* Derived classes [gram.class.derived] */
13700 /* Parse a base-clause.
13703 : base-specifier-list
13705 base-specifier-list:
13707 base-specifier-list , base-specifier
13709 Returns a TREE_LIST representing the base-classes, in the order in
13710 which they were declared. The representation of each node is as
13711 described by cp_parser_base_specifier.
13713 In the case that no bases are specified, this function will return
13714 NULL_TREE, not ERROR_MARK_NODE. */
13717 cp_parser_base_clause (cp_parser* parser)
13719 tree bases = NULL_TREE;
13721 /* Look for the `:' that begins the list. */
13722 cp_parser_require (parser, CPP_COLON, "`:'");
13724 /* Scan the base-specifier-list. */
13730 /* Look for the base-specifier. */
13731 base = cp_parser_base_specifier (parser);
13732 /* Add BASE to the front of the list. */
13733 if (base != error_mark_node)
13735 TREE_CHAIN (base) = bases;
13738 /* Peek at the next token. */
13739 token = cp_lexer_peek_token (parser->lexer);
13740 /* If it's not a comma, then the list is complete. */
13741 if (token->type != CPP_COMMA)
13743 /* Consume the `,'. */
13744 cp_lexer_consume_token (parser->lexer);
13747 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13748 base class had a qualified name. However, the next name that
13749 appears is certainly not qualified. */
13750 parser->scope = NULL_TREE;
13751 parser->qualifying_scope = NULL_TREE;
13752 parser->object_scope = NULL_TREE;
13754 return nreverse (bases);
13757 /* Parse a base-specifier.
13760 :: [opt] nested-name-specifier [opt] class-name
13761 virtual access-specifier [opt] :: [opt] nested-name-specifier
13763 access-specifier virtual [opt] :: [opt] nested-name-specifier
13766 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13767 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13768 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13769 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13772 cp_parser_base_specifier (cp_parser* parser)
13776 bool virtual_p = false;
13777 bool duplicate_virtual_error_issued_p = false;
13778 bool duplicate_access_error_issued_p = false;
13779 bool class_scope_p, template_p;
13780 tree access = access_default_node;
13783 /* Process the optional `virtual' and `access-specifier'. */
13786 /* Peek at the next token. */
13787 token = cp_lexer_peek_token (parser->lexer);
13788 /* Process `virtual'. */
13789 switch (token->keyword)
13792 /* If `virtual' appears more than once, issue an error. */
13793 if (virtual_p && !duplicate_virtual_error_issued_p)
13795 cp_parser_error (parser,
13796 "%<virtual%> specified more than once in base-specified");
13797 duplicate_virtual_error_issued_p = true;
13802 /* Consume the `virtual' token. */
13803 cp_lexer_consume_token (parser->lexer);
13808 case RID_PROTECTED:
13810 /* If more than one access specifier appears, issue an
13812 if (access != access_default_node
13813 && !duplicate_access_error_issued_p)
13815 cp_parser_error (parser,
13816 "more than one access specifier in base-specified");
13817 duplicate_access_error_issued_p = true;
13820 access = ridpointers[(int) token->keyword];
13822 /* Consume the access-specifier. */
13823 cp_lexer_consume_token (parser->lexer);
13832 /* It is not uncommon to see programs mechanically, erroneously, use
13833 the 'typename' keyword to denote (dependent) qualified types
13834 as base classes. */
13835 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13837 if (!processing_template_decl)
13838 error ("keyword %<typename%> not allowed outside of templates");
13840 error ("keyword %<typename%> not allowed in this context "
13841 "(the base class is implicitly a type)");
13842 cp_lexer_consume_token (parser->lexer);
13845 /* Look for the optional `::' operator. */
13846 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13847 /* Look for the nested-name-specifier. The simplest way to
13852 The keyword `typename' is not permitted in a base-specifier or
13853 mem-initializer; in these contexts a qualified name that
13854 depends on a template-parameter is implicitly assumed to be a
13857 is to pretend that we have seen the `typename' keyword at this
13859 cp_parser_nested_name_specifier_opt (parser,
13860 /*typename_keyword_p=*/true,
13861 /*check_dependency_p=*/true,
13863 /*is_declaration=*/true);
13864 /* If the base class is given by a qualified name, assume that names
13865 we see are type names or templates, as appropriate. */
13866 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13867 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13869 /* Finally, look for the class-name. */
13870 type = cp_parser_class_name (parser,
13874 /*check_dependency_p=*/true,
13875 /*class_head_p=*/false,
13876 /*is_declaration=*/true);
13878 if (type == error_mark_node)
13879 return error_mark_node;
13881 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13884 /* Exception handling [gram.exception] */
13886 /* Parse an (optional) exception-specification.
13888 exception-specification:
13889 throw ( type-id-list [opt] )
13891 Returns a TREE_LIST representing the exception-specification. The
13892 TREE_VALUE of each node is a type. */
13895 cp_parser_exception_specification_opt (cp_parser* parser)
13900 /* Peek at the next token. */
13901 token = cp_lexer_peek_token (parser->lexer);
13902 /* If it's not `throw', then there's no exception-specification. */
13903 if (!cp_parser_is_keyword (token, RID_THROW))
13906 /* Consume the `throw'. */
13907 cp_lexer_consume_token (parser->lexer);
13909 /* Look for the `('. */
13910 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13912 /* Peek at the next token. */
13913 token = cp_lexer_peek_token (parser->lexer);
13914 /* If it's not a `)', then there is a type-id-list. */
13915 if (token->type != CPP_CLOSE_PAREN)
13917 const char *saved_message;
13919 /* Types may not be defined in an exception-specification. */
13920 saved_message = parser->type_definition_forbidden_message;
13921 parser->type_definition_forbidden_message
13922 = "types may not be defined in an exception-specification";
13923 /* Parse the type-id-list. */
13924 type_id_list = cp_parser_type_id_list (parser);
13925 /* Restore the saved message. */
13926 parser->type_definition_forbidden_message = saved_message;
13929 type_id_list = empty_except_spec;
13931 /* Look for the `)'. */
13932 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13934 return type_id_list;
13937 /* Parse an (optional) type-id-list.
13941 type-id-list , type-id
13943 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13944 in the order that the types were presented. */
13947 cp_parser_type_id_list (cp_parser* parser)
13949 tree types = NULL_TREE;
13956 /* Get the next type-id. */
13957 type = cp_parser_type_id (parser);
13958 /* Add it to the list. */
13959 types = add_exception_specifier (types, type, /*complain=*/1);
13960 /* Peek at the next token. */
13961 token = cp_lexer_peek_token (parser->lexer);
13962 /* If it is not a `,', we are done. */
13963 if (token->type != CPP_COMMA)
13965 /* Consume the `,'. */
13966 cp_lexer_consume_token (parser->lexer);
13969 return nreverse (types);
13972 /* Parse a try-block.
13975 try compound-statement handler-seq */
13978 cp_parser_try_block (cp_parser* parser)
13982 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13983 try_block = begin_try_block ();
13984 cp_parser_compound_statement (parser, NULL, true);
13985 finish_try_block (try_block);
13986 cp_parser_handler_seq (parser);
13987 finish_handler_sequence (try_block);
13992 /* Parse a function-try-block.
13994 function-try-block:
13995 try ctor-initializer [opt] function-body handler-seq */
13998 cp_parser_function_try_block (cp_parser* parser)
14001 bool ctor_initializer_p;
14003 /* Look for the `try' keyword. */
14004 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14006 /* Let the rest of the front-end know where we are. */
14007 try_block = begin_function_try_block ();
14008 /* Parse the function-body. */
14010 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14011 /* We're done with the `try' part. */
14012 finish_function_try_block (try_block);
14013 /* Parse the handlers. */
14014 cp_parser_handler_seq (parser);
14015 /* We're done with the handlers. */
14016 finish_function_handler_sequence (try_block);
14018 return ctor_initializer_p;
14021 /* Parse a handler-seq.
14024 handler handler-seq [opt] */
14027 cp_parser_handler_seq (cp_parser* parser)
14033 /* Parse the handler. */
14034 cp_parser_handler (parser);
14035 /* Peek at the next token. */
14036 token = cp_lexer_peek_token (parser->lexer);
14037 /* If it's not `catch' then there are no more handlers. */
14038 if (!cp_parser_is_keyword (token, RID_CATCH))
14043 /* Parse a handler.
14046 catch ( exception-declaration ) compound-statement */
14049 cp_parser_handler (cp_parser* parser)
14054 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14055 handler = begin_handler ();
14056 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14057 declaration = cp_parser_exception_declaration (parser);
14058 finish_handler_parms (declaration, handler);
14059 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14060 cp_parser_compound_statement (parser, NULL, false);
14061 finish_handler (handler);
14064 /* Parse an exception-declaration.
14066 exception-declaration:
14067 type-specifier-seq declarator
14068 type-specifier-seq abstract-declarator
14072 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14073 ellipsis variant is used. */
14076 cp_parser_exception_declaration (cp_parser* parser)
14079 cp_decl_specifier_seq type_specifiers;
14080 cp_declarator *declarator;
14081 const char *saved_message;
14083 /* If it's an ellipsis, it's easy to handle. */
14084 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14086 /* Consume the `...' token. */
14087 cp_lexer_consume_token (parser->lexer);
14091 /* Types may not be defined in exception-declarations. */
14092 saved_message = parser->type_definition_forbidden_message;
14093 parser->type_definition_forbidden_message
14094 = "types may not be defined in exception-declarations";
14096 /* Parse the type-specifier-seq. */
14097 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14099 /* If it's a `)', then there is no declarator. */
14100 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14103 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14104 /*ctor_dtor_or_conv_p=*/NULL,
14105 /*parenthesized_p=*/NULL,
14106 /*member_p=*/false);
14108 /* Restore the saved message. */
14109 parser->type_definition_forbidden_message = saved_message;
14111 if (type_specifiers.any_specifiers_p)
14113 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14114 if (decl == NULL_TREE)
14115 error ("invalid catch parameter");
14123 /* Parse a throw-expression.
14126 throw assignment-expression [opt]
14128 Returns a THROW_EXPR representing the throw-expression. */
14131 cp_parser_throw_expression (cp_parser* parser)
14136 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14137 token = cp_lexer_peek_token (parser->lexer);
14138 /* Figure out whether or not there is an assignment-expression
14139 following the "throw" keyword. */
14140 if (token->type == CPP_COMMA
14141 || token->type == CPP_SEMICOLON
14142 || token->type == CPP_CLOSE_PAREN
14143 || token->type == CPP_CLOSE_SQUARE
14144 || token->type == CPP_CLOSE_BRACE
14145 || token->type == CPP_COLON)
14146 expression = NULL_TREE;
14148 expression = cp_parser_assignment_expression (parser,
14151 return build_throw (expression);
14154 /* GNU Extensions */
14156 /* Parse an (optional) asm-specification.
14159 asm ( string-literal )
14161 If the asm-specification is present, returns a STRING_CST
14162 corresponding to the string-literal. Otherwise, returns
14166 cp_parser_asm_specification_opt (cp_parser* parser)
14169 tree asm_specification;
14171 /* Peek at the next token. */
14172 token = cp_lexer_peek_token (parser->lexer);
14173 /* If the next token isn't the `asm' keyword, then there's no
14174 asm-specification. */
14175 if (!cp_parser_is_keyword (token, RID_ASM))
14178 /* Consume the `asm' token. */
14179 cp_lexer_consume_token (parser->lexer);
14180 /* Look for the `('. */
14181 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14183 /* Look for the string-literal. */
14184 asm_specification = cp_parser_string_literal (parser, false, false);
14186 /* Look for the `)'. */
14187 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14189 return asm_specification;
14192 /* Parse an asm-operand-list.
14196 asm-operand-list , asm-operand
14199 string-literal ( expression )
14200 [ string-literal ] string-literal ( expression )
14202 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14203 each node is the expression. The TREE_PURPOSE is itself a
14204 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14205 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14206 is a STRING_CST for the string literal before the parenthesis. */
14209 cp_parser_asm_operand_list (cp_parser* parser)
14211 tree asm_operands = NULL_TREE;
14215 tree string_literal;
14219 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14221 /* Consume the `[' token. */
14222 cp_lexer_consume_token (parser->lexer);
14223 /* Read the operand name. */
14224 name = cp_parser_identifier (parser);
14225 if (name != error_mark_node)
14226 name = build_string (IDENTIFIER_LENGTH (name),
14227 IDENTIFIER_POINTER (name));
14228 /* Look for the closing `]'. */
14229 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14233 /* Look for the string-literal. */
14234 string_literal = cp_parser_string_literal (parser, false, false);
14236 /* Look for the `('. */
14237 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14238 /* Parse the expression. */
14239 expression = cp_parser_expression (parser, /*cast_p=*/false);
14240 /* Look for the `)'. */
14241 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14243 /* Add this operand to the list. */
14244 asm_operands = tree_cons (build_tree_list (name, string_literal),
14247 /* If the next token is not a `,', there are no more
14249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14251 /* Consume the `,'. */
14252 cp_lexer_consume_token (parser->lexer);
14255 return nreverse (asm_operands);
14258 /* Parse an asm-clobber-list.
14262 asm-clobber-list , string-literal
14264 Returns a TREE_LIST, indicating the clobbers in the order that they
14265 appeared. The TREE_VALUE of each node is a STRING_CST. */
14268 cp_parser_asm_clobber_list (cp_parser* parser)
14270 tree clobbers = NULL_TREE;
14274 tree string_literal;
14276 /* Look for the string literal. */
14277 string_literal = cp_parser_string_literal (parser, false, false);
14278 /* Add it to the list. */
14279 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14280 /* If the next token is not a `,', then the list is
14282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14284 /* Consume the `,' token. */
14285 cp_lexer_consume_token (parser->lexer);
14291 /* Parse an (optional) series of attributes.
14294 attributes attribute
14297 __attribute__ (( attribute-list [opt] ))
14299 The return value is as for cp_parser_attribute_list. */
14302 cp_parser_attributes_opt (cp_parser* parser)
14304 tree attributes = NULL_TREE;
14309 tree attribute_list;
14311 /* Peek at the next token. */
14312 token = cp_lexer_peek_token (parser->lexer);
14313 /* If it's not `__attribute__', then we're done. */
14314 if (token->keyword != RID_ATTRIBUTE)
14317 /* Consume the `__attribute__' keyword. */
14318 cp_lexer_consume_token (parser->lexer);
14319 /* Look for the two `(' tokens. */
14320 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14321 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14323 /* Peek at the next token. */
14324 token = cp_lexer_peek_token (parser->lexer);
14325 if (token->type != CPP_CLOSE_PAREN)
14326 /* Parse the attribute-list. */
14327 attribute_list = cp_parser_attribute_list (parser);
14329 /* If the next token is a `)', then there is no attribute
14331 attribute_list = NULL;
14333 /* Look for the two `)' tokens. */
14334 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14335 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14337 /* Add these new attributes to the list. */
14338 attributes = chainon (attributes, attribute_list);
14344 /* Parse an attribute-list.
14348 attribute-list , attribute
14352 identifier ( identifier )
14353 identifier ( identifier , expression-list )
14354 identifier ( expression-list )
14356 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14357 to an attribute. The TREE_PURPOSE of each node is the identifier
14358 indicating which attribute is in use. The TREE_VALUE represents
14359 the arguments, if any. */
14362 cp_parser_attribute_list (cp_parser* parser)
14364 tree attribute_list = NULL_TREE;
14365 bool save_translate_strings_p = parser->translate_strings_p;
14367 parser->translate_strings_p = false;
14374 /* Look for the identifier. We also allow keywords here; for
14375 example `__attribute__ ((const))' is legal. */
14376 token = cp_lexer_peek_token (parser->lexer);
14377 if (token->type == CPP_NAME
14378 || token->type == CPP_KEYWORD)
14380 /* Consume the token. */
14381 token = cp_lexer_consume_token (parser->lexer);
14383 /* Save away the identifier that indicates which attribute
14385 identifier = token->value;
14386 attribute = build_tree_list (identifier, NULL_TREE);
14388 /* Peek at the next token. */
14389 token = cp_lexer_peek_token (parser->lexer);
14390 /* If it's an `(', then parse the attribute arguments. */
14391 if (token->type == CPP_OPEN_PAREN)
14395 arguments = (cp_parser_parenthesized_expression_list
14396 (parser, true, /*cast_p=*/false,
14397 /*non_constant_p=*/NULL));
14398 /* Save the identifier and arguments away. */
14399 TREE_VALUE (attribute) = arguments;
14402 /* Add this attribute to the list. */
14403 TREE_CHAIN (attribute) = attribute_list;
14404 attribute_list = attribute;
14406 token = cp_lexer_peek_token (parser->lexer);
14408 /* Now, look for more attributes. If the next token isn't a
14409 `,', we're done. */
14410 if (token->type != CPP_COMMA)
14413 /* Consume the comma and keep going. */
14414 cp_lexer_consume_token (parser->lexer);
14416 parser->translate_strings_p = save_translate_strings_p;
14418 /* We built up the list in reverse order. */
14419 return nreverse (attribute_list);
14422 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14423 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14424 current value of the PEDANTIC flag, regardless of whether or not
14425 the `__extension__' keyword is present. The caller is responsible
14426 for restoring the value of the PEDANTIC flag. */
14429 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14431 /* Save the old value of the PEDANTIC flag. */
14432 *saved_pedantic = pedantic;
14434 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14436 /* Consume the `__extension__' token. */
14437 cp_lexer_consume_token (parser->lexer);
14438 /* We're not being pedantic while the `__extension__' keyword is
14448 /* Parse a label declaration.
14451 __label__ label-declarator-seq ;
14453 label-declarator-seq:
14454 identifier , label-declarator-seq
14458 cp_parser_label_declaration (cp_parser* parser)
14460 /* Look for the `__label__' keyword. */
14461 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14467 /* Look for an identifier. */
14468 identifier = cp_parser_identifier (parser);
14469 /* If we failed, stop. */
14470 if (identifier == error_mark_node)
14472 /* Declare it as a label. */
14473 finish_label_decl (identifier);
14474 /* If the next token is a `;', stop. */
14475 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14477 /* Look for the `,' separating the label declarations. */
14478 cp_parser_require (parser, CPP_COMMA, "`,'");
14481 /* Look for the final `;'. */
14482 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14485 /* Support Functions */
14487 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14488 NAME should have one of the representations used for an
14489 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14490 is returned. If PARSER->SCOPE is a dependent type, then a
14491 SCOPE_REF is returned.
14493 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14494 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14495 was formed. Abstractly, such entities should not be passed to this
14496 function, because they do not need to be looked up, but it is
14497 simpler to check for this special case here, rather than at the
14500 In cases not explicitly covered above, this function returns a
14501 DECL, OVERLOAD, or baselink representing the result of the lookup.
14502 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14505 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14506 (e.g., "struct") that was used. In that case bindings that do not
14507 refer to types are ignored.
14509 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14512 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14515 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14518 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14519 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14520 NULL_TREE otherwise. */
14523 cp_parser_lookup_name (cp_parser *parser, tree name,
14524 enum tag_types tag_type,
14527 bool check_dependency,
14528 tree *ambiguous_decls)
14532 tree object_type = parser->context->object_type;
14534 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14535 flags |= LOOKUP_COMPLAIN;
14537 /* Assume that the lookup will be unambiguous. */
14538 if (ambiguous_decls)
14539 *ambiguous_decls = NULL_TREE;
14541 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14542 no longer valid. Note that if we are parsing tentatively, and
14543 the parse fails, OBJECT_TYPE will be automatically restored. */
14544 parser->context->object_type = NULL_TREE;
14546 if (name == error_mark_node)
14547 return error_mark_node;
14549 /* A template-id has already been resolved; there is no lookup to
14551 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14553 if (BASELINK_P (name))
14555 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14556 == TEMPLATE_ID_EXPR);
14560 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14561 it should already have been checked to make sure that the name
14562 used matches the type being destroyed. */
14563 if (TREE_CODE (name) == BIT_NOT_EXPR)
14567 /* Figure out to which type this destructor applies. */
14569 type = parser->scope;
14570 else if (object_type)
14571 type = object_type;
14573 type = current_class_type;
14574 /* If that's not a class type, there is no destructor. */
14575 if (!type || !CLASS_TYPE_P (type))
14576 return error_mark_node;
14577 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14578 lazily_declare_fn (sfk_destructor, type);
14579 if (!CLASSTYPE_DESTRUCTORS (type))
14580 return error_mark_node;
14581 /* If it was a class type, return the destructor. */
14582 return CLASSTYPE_DESTRUCTORS (type);
14585 /* By this point, the NAME should be an ordinary identifier. If
14586 the id-expression was a qualified name, the qualifying scope is
14587 stored in PARSER->SCOPE at this point. */
14588 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14590 /* Perform the lookup. */
14595 if (parser->scope == error_mark_node)
14596 return error_mark_node;
14598 /* If the SCOPE is dependent, the lookup must be deferred until
14599 the template is instantiated -- unless we are explicitly
14600 looking up names in uninstantiated templates. Even then, we
14601 cannot look up the name if the scope is not a class type; it
14602 might, for example, be a template type parameter. */
14603 dependent_p = (TYPE_P (parser->scope)
14604 && !(parser->in_declarator_p
14605 && currently_open_class (parser->scope))
14606 && dependent_type_p (parser->scope));
14607 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14614 /* The resolution to Core Issue 180 says that `struct
14615 A::B' should be considered a type-name, even if `A'
14617 type = make_typename_type (parser->scope, name, tag_type,
14618 /*complain=*/tf_error);
14619 decl = TYPE_NAME (type);
14621 else if (is_template
14622 && (cp_parser_next_token_ends_template_argument_p (parser)
14623 || cp_lexer_next_token_is (parser->lexer,
14625 decl = make_unbound_class_template (parser->scope,
14627 /*complain=*/tf_error);
14629 decl = build_qualified_name (/*type=*/NULL_TREE,
14630 parser->scope, name,
14635 tree pushed_scope = NULL_TREE;
14637 /* If PARSER->SCOPE is a dependent type, then it must be a
14638 class type, and we must not be checking dependencies;
14639 otherwise, we would have processed this lookup above. So
14640 that PARSER->SCOPE is not considered a dependent base by
14641 lookup_member, we must enter the scope here. */
14643 pushed_scope = push_scope (parser->scope);
14644 /* If the PARSER->SCOPE is a template specialization, it
14645 may be instantiated during name lookup. In that case,
14646 errors may be issued. Even if we rollback the current
14647 tentative parse, those errors are valid. */
14648 decl = lookup_qualified_name (parser->scope, name,
14649 tag_type != none_type,
14650 /*complain=*/true);
14652 pop_scope (pushed_scope);
14654 parser->qualifying_scope = parser->scope;
14655 parser->object_scope = NULL_TREE;
14657 else if (object_type)
14659 tree object_decl = NULL_TREE;
14660 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14661 OBJECT_TYPE is not a class. */
14662 if (CLASS_TYPE_P (object_type))
14663 /* If the OBJECT_TYPE is a template specialization, it may
14664 be instantiated during name lookup. In that case, errors
14665 may be issued. Even if we rollback the current tentative
14666 parse, those errors are valid. */
14667 object_decl = lookup_member (object_type,
14670 tag_type != none_type);
14671 /* Look it up in the enclosing context, too. */
14672 decl = lookup_name_real (name, tag_type != none_type,
14674 /*block_p=*/true, is_namespace, flags);
14675 parser->object_scope = object_type;
14676 parser->qualifying_scope = NULL_TREE;
14678 decl = object_decl;
14682 decl = lookup_name_real (name, tag_type != none_type,
14684 /*block_p=*/true, is_namespace, flags);
14685 parser->qualifying_scope = NULL_TREE;
14686 parser->object_scope = NULL_TREE;
14689 /* If the lookup failed, let our caller know. */
14690 if (!decl || decl == error_mark_node)
14691 return error_mark_node;
14693 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14694 if (TREE_CODE (decl) == TREE_LIST)
14696 if (ambiguous_decls)
14697 *ambiguous_decls = decl;
14698 /* The error message we have to print is too complicated for
14699 cp_parser_error, so we incorporate its actions directly. */
14700 if (!cp_parser_simulate_error (parser))
14702 error ("reference to %qD is ambiguous", name);
14703 print_candidates (decl);
14705 return error_mark_node;
14708 gcc_assert (DECL_P (decl)
14709 || TREE_CODE (decl) == OVERLOAD
14710 || TREE_CODE (decl) == SCOPE_REF
14711 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14712 || BASELINK_P (decl));
14714 /* If we have resolved the name of a member declaration, check to
14715 see if the declaration is accessible. When the name resolves to
14716 set of overloaded functions, accessibility is checked when
14717 overload resolution is done.
14719 During an explicit instantiation, access is not checked at all,
14720 as per [temp.explicit]. */
14722 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14727 /* Like cp_parser_lookup_name, but for use in the typical case where
14728 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14729 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14732 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14734 return cp_parser_lookup_name (parser, name,
14736 /*is_template=*/false,
14737 /*is_namespace=*/false,
14738 /*check_dependency=*/true,
14739 /*ambiguous_decls=*/NULL);
14742 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14743 the current context, return the TYPE_DECL. If TAG_NAME_P is
14744 true, the DECL indicates the class being defined in a class-head,
14745 or declared in an elaborated-type-specifier.
14747 Otherwise, return DECL. */
14750 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14752 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14753 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14756 template <typename T> struct B;
14759 template <typename T> struct A::B {};
14761 Similarly, in an elaborated-type-specifier:
14763 namespace N { struct X{}; }
14766 template <typename T> friend struct N::X;
14769 However, if the DECL refers to a class type, and we are in
14770 the scope of the class, then the name lookup automatically
14771 finds the TYPE_DECL created by build_self_reference rather
14772 than a TEMPLATE_DECL. For example, in:
14774 template <class T> struct S {
14778 there is no need to handle such case. */
14780 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14781 return DECL_TEMPLATE_RESULT (decl);
14786 /* If too many, or too few, template-parameter lists apply to the
14787 declarator, issue an error message. Returns TRUE if all went well,
14788 and FALSE otherwise. */
14791 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14792 cp_declarator *declarator)
14794 unsigned num_templates;
14796 /* We haven't seen any classes that involve template parameters yet. */
14799 switch (declarator->kind)
14802 if (declarator->u.id.qualifying_scope)
14807 scope = declarator->u.id.qualifying_scope;
14808 member = declarator->u.id.unqualified_name;
14810 while (scope && CLASS_TYPE_P (scope))
14812 /* You're supposed to have one `template <...>'
14813 for every template class, but you don't need one
14814 for a full specialization. For example:
14816 template <class T> struct S{};
14817 template <> struct S<int> { void f(); };
14818 void S<int>::f () {}
14820 is correct; there shouldn't be a `template <>' for
14821 the definition of `S<int>::f'. */
14822 if (CLASSTYPE_TEMPLATE_INFO (scope)
14823 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14824 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14825 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14828 scope = TYPE_CONTEXT (scope);
14831 else if (TREE_CODE (declarator->u.id.unqualified_name)
14832 == TEMPLATE_ID_EXPR)
14833 /* If the DECLARATOR has the form `X<y>' then it uses one
14834 additional level of template parameters. */
14837 return cp_parser_check_template_parameters (parser,
14843 case cdk_reference:
14845 return (cp_parser_check_declarator_template_parameters
14846 (parser, declarator->declarator));
14852 gcc_unreachable ();
14857 /* NUM_TEMPLATES were used in the current declaration. If that is
14858 invalid, return FALSE and issue an error messages. Otherwise,
14862 cp_parser_check_template_parameters (cp_parser* parser,
14863 unsigned num_templates)
14865 /* If there are more template classes than parameter lists, we have
14868 template <class T> void S<T>::R<T>::f (); */
14869 if (parser->num_template_parameter_lists < num_templates)
14871 error ("too few template-parameter-lists");
14874 /* If there are the same number of template classes and parameter
14875 lists, that's OK. */
14876 if (parser->num_template_parameter_lists == num_templates)
14878 /* If there are more, but only one more, then we are referring to a
14879 member template. That's OK too. */
14880 if (parser->num_template_parameter_lists == num_templates + 1)
14882 /* Otherwise, there are too many template parameter lists. We have
14885 template <class T> template <class U> void S::f(); */
14886 error ("too many template-parameter-lists");
14890 /* Parse an optional `::' token indicating that the following name is
14891 from the global namespace. If so, PARSER->SCOPE is set to the
14892 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14893 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14894 Returns the new value of PARSER->SCOPE, if the `::' token is
14895 present, and NULL_TREE otherwise. */
14898 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14902 /* Peek at the next token. */
14903 token = cp_lexer_peek_token (parser->lexer);
14904 /* If we're looking at a `::' token then we're starting from the
14905 global namespace, not our current location. */
14906 if (token->type == CPP_SCOPE)
14908 /* Consume the `::' token. */
14909 cp_lexer_consume_token (parser->lexer);
14910 /* Set the SCOPE so that we know where to start the lookup. */
14911 parser->scope = global_namespace;
14912 parser->qualifying_scope = global_namespace;
14913 parser->object_scope = NULL_TREE;
14915 return parser->scope;
14917 else if (!current_scope_valid_p)
14919 parser->scope = NULL_TREE;
14920 parser->qualifying_scope = NULL_TREE;
14921 parser->object_scope = NULL_TREE;
14927 /* Returns TRUE if the upcoming token sequence is the start of a
14928 constructor declarator. If FRIEND_P is true, the declarator is
14929 preceded by the `friend' specifier. */
14932 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14934 bool constructor_p;
14935 tree type_decl = NULL_TREE;
14936 bool nested_name_p;
14937 cp_token *next_token;
14939 /* The common case is that this is not a constructor declarator, so
14940 try to avoid doing lots of work if at all possible. It's not
14941 valid declare a constructor at function scope. */
14942 if (at_function_scope_p ())
14944 /* And only certain tokens can begin a constructor declarator. */
14945 next_token = cp_lexer_peek_token (parser->lexer);
14946 if (next_token->type != CPP_NAME
14947 && next_token->type != CPP_SCOPE
14948 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14949 && next_token->type != CPP_TEMPLATE_ID)
14952 /* Parse tentatively; we are going to roll back all of the tokens
14954 cp_parser_parse_tentatively (parser);
14955 /* Assume that we are looking at a constructor declarator. */
14956 constructor_p = true;
14958 /* Look for the optional `::' operator. */
14959 cp_parser_global_scope_opt (parser,
14960 /*current_scope_valid_p=*/false);
14961 /* Look for the nested-name-specifier. */
14963 = (cp_parser_nested_name_specifier_opt (parser,
14964 /*typename_keyword_p=*/false,
14965 /*check_dependency_p=*/false,
14967 /*is_declaration=*/false)
14969 /* Outside of a class-specifier, there must be a
14970 nested-name-specifier. */
14971 if (!nested_name_p &&
14972 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14974 constructor_p = false;
14975 /* If we still think that this might be a constructor-declarator,
14976 look for a class-name. */
14981 template <typename T> struct S { S(); };
14982 template <typename T> S<T>::S ();
14984 we must recognize that the nested `S' names a class.
14987 template <typename T> S<T>::S<T> ();
14989 we must recognize that the nested `S' names a template. */
14990 type_decl = cp_parser_class_name (parser,
14991 /*typename_keyword_p=*/false,
14992 /*template_keyword_p=*/false,
14994 /*check_dependency_p=*/false,
14995 /*class_head_p=*/false,
14996 /*is_declaration=*/false);
14997 /* If there was no class-name, then this is not a constructor. */
14998 constructor_p = !cp_parser_error_occurred (parser);
15001 /* If we're still considering a constructor, we have to see a `(',
15002 to begin the parameter-declaration-clause, followed by either a
15003 `)', an `...', or a decl-specifier. We need to check for a
15004 type-specifier to avoid being fooled into thinking that:
15008 is a constructor. (It is actually a function named `f' that
15009 takes one parameter (of type `int') and returns a value of type
15012 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15014 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15015 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15016 /* A parameter declaration begins with a decl-specifier,
15017 which is either the "attribute" keyword, a storage class
15018 specifier, or (usually) a type-specifier. */
15019 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15020 && !cp_parser_storage_class_specifier_opt (parser))
15023 tree pushed_scope = NULL_TREE;
15024 unsigned saved_num_template_parameter_lists;
15026 /* Names appearing in the type-specifier should be looked up
15027 in the scope of the class. */
15028 if (current_class_type)
15032 type = TREE_TYPE (type_decl);
15033 if (TREE_CODE (type) == TYPENAME_TYPE)
15035 type = resolve_typename_type (type,
15036 /*only_current_p=*/false);
15037 if (type == error_mark_node)
15039 cp_parser_abort_tentative_parse (parser);
15043 pushed_scope = push_scope (type);
15046 /* Inside the constructor parameter list, surrounding
15047 template-parameter-lists do not apply. */
15048 saved_num_template_parameter_lists
15049 = parser->num_template_parameter_lists;
15050 parser->num_template_parameter_lists = 0;
15052 /* Look for the type-specifier. */
15053 cp_parser_type_specifier (parser,
15054 CP_PARSER_FLAGS_NONE,
15055 /*decl_specs=*/NULL,
15056 /*is_declarator=*/true,
15057 /*declares_class_or_enum=*/NULL,
15058 /*is_cv_qualifier=*/NULL);
15060 parser->num_template_parameter_lists
15061 = saved_num_template_parameter_lists;
15063 /* Leave the scope of the class. */
15065 pop_scope (pushed_scope);
15067 constructor_p = !cp_parser_error_occurred (parser);
15071 constructor_p = false;
15072 /* We did not really want to consume any tokens. */
15073 cp_parser_abort_tentative_parse (parser);
15075 return constructor_p;
15078 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15079 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15080 they must be performed once we are in the scope of the function.
15082 Returns the function defined. */
15085 cp_parser_function_definition_from_specifiers_and_declarator
15086 (cp_parser* parser,
15087 cp_decl_specifier_seq *decl_specifiers,
15089 const cp_declarator *declarator)
15094 /* Begin the function-definition. */
15095 success_p = start_function (decl_specifiers, declarator, attributes);
15097 /* The things we're about to see are not directly qualified by any
15098 template headers we've seen thus far. */
15099 reset_specialization ();
15101 /* If there were names looked up in the decl-specifier-seq that we
15102 did not check, check them now. We must wait until we are in the
15103 scope of the function to perform the checks, since the function
15104 might be a friend. */
15105 perform_deferred_access_checks ();
15109 /* Skip the entire function. */
15110 error ("invalid function declaration");
15111 cp_parser_skip_to_end_of_block_or_statement (parser);
15112 fn = error_mark_node;
15115 fn = cp_parser_function_definition_after_declarator (parser,
15116 /*inline_p=*/false);
15121 /* Parse the part of a function-definition that follows the
15122 declarator. INLINE_P is TRUE iff this function is an inline
15123 function defined with a class-specifier.
15125 Returns the function defined. */
15128 cp_parser_function_definition_after_declarator (cp_parser* parser,
15132 bool ctor_initializer_p = false;
15133 bool saved_in_unbraced_linkage_specification_p;
15134 unsigned saved_num_template_parameter_lists;
15136 /* If the next token is `return', then the code may be trying to
15137 make use of the "named return value" extension that G++ used to
15139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15141 /* Consume the `return' keyword. */
15142 cp_lexer_consume_token (parser->lexer);
15143 /* Look for the identifier that indicates what value is to be
15145 cp_parser_identifier (parser);
15146 /* Issue an error message. */
15147 error ("named return values are no longer supported");
15148 /* Skip tokens until we reach the start of the function body. */
15149 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15150 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15151 cp_lexer_consume_token (parser->lexer);
15153 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15154 anything declared inside `f'. */
15155 saved_in_unbraced_linkage_specification_p
15156 = parser->in_unbraced_linkage_specification_p;
15157 parser->in_unbraced_linkage_specification_p = false;
15158 /* Inside the function, surrounding template-parameter-lists do not
15160 saved_num_template_parameter_lists
15161 = parser->num_template_parameter_lists;
15162 parser->num_template_parameter_lists = 0;
15163 /* If the next token is `try', then we are looking at a
15164 function-try-block. */
15165 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15166 ctor_initializer_p = cp_parser_function_try_block (parser);
15167 /* A function-try-block includes the function-body, so we only do
15168 this next part if we're not processing a function-try-block. */
15171 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15173 /* Finish the function. */
15174 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15175 (inline_p ? 2 : 0));
15176 /* Generate code for it, if necessary. */
15177 expand_or_defer_fn (fn);
15178 /* Restore the saved values. */
15179 parser->in_unbraced_linkage_specification_p
15180 = saved_in_unbraced_linkage_specification_p;
15181 parser->num_template_parameter_lists
15182 = saved_num_template_parameter_lists;
15187 /* Parse a template-declaration, assuming that the `export' (and
15188 `extern') keywords, if present, has already been scanned. MEMBER_P
15189 is as for cp_parser_template_declaration. */
15192 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15194 tree decl = NULL_TREE;
15195 tree parameter_list;
15196 bool friend_p = false;
15197 bool need_lang_pop;
15199 /* Look for the `template' keyword. */
15200 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15204 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15208 A template ... shall not have C linkage. */
15209 if (current_lang_name == lang_name_c)
15211 error ("template with C linkage");
15212 /* Give it C++ linkage to avoid confusing other parts of the
15214 push_lang_context (lang_name_cplusplus);
15215 need_lang_pop = true;
15218 need_lang_pop = false;
15219 /* If the next token is `>', then we have an invalid
15220 specialization. Rather than complain about an invalid template
15221 parameter, issue an error message here. */
15222 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15224 cp_parser_error (parser, "invalid explicit specialization");
15225 begin_specialization ();
15226 parameter_list = NULL_TREE;
15230 /* Parse the template parameters. */
15231 begin_template_parm_list ();
15232 parameter_list = cp_parser_template_parameter_list (parser);
15233 parameter_list = end_template_parm_list (parameter_list);
15236 /* Look for the `>'. */
15237 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15238 /* We just processed one more parameter list. */
15239 ++parser->num_template_parameter_lists;
15240 /* If the next token is `template', there are more template
15242 if (cp_lexer_next_token_is_keyword (parser->lexer,
15244 cp_parser_template_declaration_after_export (parser, member_p);
15247 /* There are no access checks when parsing a template, as we do not
15248 know if a specialization will be a friend. */
15249 push_deferring_access_checks (dk_no_check);
15251 decl = cp_parser_single_declaration (parser,
15255 pop_deferring_access_checks ();
15257 /* If this is a member template declaration, let the front
15259 if (member_p && !friend_p && decl)
15261 if (TREE_CODE (decl) == TYPE_DECL)
15262 cp_parser_check_access_in_redeclaration (decl);
15264 decl = finish_member_template_decl (decl);
15266 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15267 make_friend_class (current_class_type, TREE_TYPE (decl),
15268 /*complain=*/true);
15270 /* We are done with the current parameter list. */
15271 --parser->num_template_parameter_lists;
15274 finish_template_decl (parameter_list);
15276 /* Register member declarations. */
15277 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15278 finish_member_declaration (decl);
15279 /* For the erroneous case of a template with C linkage, we pushed an
15280 implicit C++ linkage scope; exit that scope now. */
15282 pop_lang_context ();
15283 /* If DECL is a function template, we must return to parse it later.
15284 (Even though there is no definition, there might be default
15285 arguments that need handling.) */
15286 if (member_p && decl
15287 && (TREE_CODE (decl) == FUNCTION_DECL
15288 || DECL_FUNCTION_TEMPLATE_P (decl)))
15289 TREE_VALUE (parser->unparsed_functions_queues)
15290 = tree_cons (NULL_TREE, decl,
15291 TREE_VALUE (parser->unparsed_functions_queues));
15294 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15295 `function-definition' sequence. MEMBER_P is true, this declaration
15296 appears in a class scope.
15298 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15299 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15302 cp_parser_single_declaration (cp_parser* parser,
15306 int declares_class_or_enum;
15307 tree decl = NULL_TREE;
15308 cp_decl_specifier_seq decl_specifiers;
15309 bool function_definition_p = false;
15311 /* This function is only used when processing a template
15313 gcc_assert (innermost_scope_kind () == sk_template_parms
15314 || innermost_scope_kind () == sk_template_spec);
15316 /* Defer access checks until we know what is being declared. */
15317 push_deferring_access_checks (dk_deferred);
15319 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15321 cp_parser_decl_specifier_seq (parser,
15322 CP_PARSER_FLAGS_OPTIONAL,
15324 &declares_class_or_enum);
15326 *friend_p = cp_parser_friend_p (&decl_specifiers);
15328 /* There are no template typedefs. */
15329 if (decl_specifiers.specs[(int) ds_typedef])
15331 error ("template declaration of %qs", "typedef");
15332 decl = error_mark_node;
15335 /* Gather up the access checks that occurred the
15336 decl-specifier-seq. */
15337 stop_deferring_access_checks ();
15339 /* Check for the declaration of a template class. */
15340 if (declares_class_or_enum)
15342 if (cp_parser_declares_only_class_p (parser))
15344 decl = shadow_tag (&decl_specifiers);
15349 friend template <typename T> struct A<T>::B;
15352 A<T>::B will be represented by a TYPENAME_TYPE, and
15353 therefore not recognized by shadow_tag. */
15354 if (friend_p && *friend_p
15356 && decl_specifiers.type
15357 && TYPE_P (decl_specifiers.type))
15358 decl = decl_specifiers.type;
15360 if (decl && decl != error_mark_node)
15361 decl = TYPE_NAME (decl);
15363 decl = error_mark_node;
15366 /* If it's not a template class, try for a template function. If
15367 the next token is a `;', then this declaration does not declare
15368 anything. But, if there were errors in the decl-specifiers, then
15369 the error might well have come from an attempted class-specifier.
15370 In that case, there's no need to warn about a missing declarator. */
15372 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15373 || decl_specifiers.type != error_mark_node))
15374 decl = cp_parser_init_declarator (parser,
15376 /*function_definition_allowed_p=*/true,
15378 declares_class_or_enum,
15379 &function_definition_p);
15381 pop_deferring_access_checks ();
15383 /* Clear any current qualification; whatever comes next is the start
15384 of something new. */
15385 parser->scope = NULL_TREE;
15386 parser->qualifying_scope = NULL_TREE;
15387 parser->object_scope = NULL_TREE;
15388 /* Look for a trailing `;' after the declaration. */
15389 if (!function_definition_p
15390 && (decl == error_mark_node
15391 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15392 cp_parser_skip_to_end_of_block_or_statement (parser);
15397 /* Parse a cast-expression that is not the operand of a unary "&". */
15400 cp_parser_simple_cast_expression (cp_parser *parser)
15402 return cp_parser_cast_expression (parser, /*address_p=*/false,
15406 /* Parse a functional cast to TYPE. Returns an expression
15407 representing the cast. */
15410 cp_parser_functional_cast (cp_parser* parser, tree type)
15412 tree expression_list;
15416 = cp_parser_parenthesized_expression_list (parser, false,
15418 /*non_constant_p=*/NULL);
15420 cast = build_functional_cast (type, expression_list);
15421 /* [expr.const]/1: In an integral constant expression "only type
15422 conversions to integral or enumeration type can be used". */
15423 if (TREE_CODE (type) == TYPE_DECL)
15424 type = TREE_TYPE (type);
15425 if (cast != error_mark_node && !dependent_type_p (type)
15426 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15428 if (cp_parser_non_integral_constant_expression
15429 (parser, "a call to a constructor"))
15430 return error_mark_node;
15435 /* Save the tokens that make up the body of a member function defined
15436 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15437 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15438 specifiers applied to the declaration. Returns the FUNCTION_DECL
15439 for the member function. */
15442 cp_parser_save_member_function_body (cp_parser* parser,
15443 cp_decl_specifier_seq *decl_specifiers,
15444 cp_declarator *declarator,
15451 /* Create the function-declaration. */
15452 fn = start_method (decl_specifiers, declarator, attributes);
15453 /* If something went badly wrong, bail out now. */
15454 if (fn == error_mark_node)
15456 /* If there's a function-body, skip it. */
15457 if (cp_parser_token_starts_function_definition_p
15458 (cp_lexer_peek_token (parser->lexer)))
15459 cp_parser_skip_to_end_of_block_or_statement (parser);
15460 return error_mark_node;
15463 /* Remember it, if there default args to post process. */
15464 cp_parser_save_default_args (parser, fn);
15466 /* Save away the tokens that make up the body of the
15468 first = parser->lexer->next_token;
15469 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15470 /* Handle function try blocks. */
15471 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15472 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15473 last = parser->lexer->next_token;
15475 /* Save away the inline definition; we will process it when the
15476 class is complete. */
15477 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15478 DECL_PENDING_INLINE_P (fn) = 1;
15480 /* We need to know that this was defined in the class, so that
15481 friend templates are handled correctly. */
15482 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15484 /* We're done with the inline definition. */
15485 finish_method (fn);
15487 /* Add FN to the queue of functions to be parsed later. */
15488 TREE_VALUE (parser->unparsed_functions_queues)
15489 = tree_cons (NULL_TREE, fn,
15490 TREE_VALUE (parser->unparsed_functions_queues));
15495 /* Parse a template-argument-list, as well as the trailing ">" (but
15496 not the opening ">"). See cp_parser_template_argument_list for the
15500 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15504 tree saved_qualifying_scope;
15505 tree saved_object_scope;
15506 bool saved_greater_than_is_operator_p;
15507 bool saved_skip_evaluation;
15511 When parsing a template-id, the first non-nested `>' is taken as
15512 the end of the template-argument-list rather than a greater-than
15514 saved_greater_than_is_operator_p
15515 = parser->greater_than_is_operator_p;
15516 parser->greater_than_is_operator_p = false;
15517 /* Parsing the argument list may modify SCOPE, so we save it
15519 saved_scope = parser->scope;
15520 saved_qualifying_scope = parser->qualifying_scope;
15521 saved_object_scope = parser->object_scope;
15522 /* We need to evaluate the template arguments, even though this
15523 template-id may be nested within a "sizeof". */
15524 saved_skip_evaluation = skip_evaluation;
15525 skip_evaluation = false;
15526 /* Parse the template-argument-list itself. */
15527 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15528 arguments = NULL_TREE;
15530 arguments = cp_parser_template_argument_list (parser);
15531 /* Look for the `>' that ends the template-argument-list. If we find
15532 a '>>' instead, it's probably just a typo. */
15533 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15535 if (!saved_greater_than_is_operator_p)
15537 /* If we're in a nested template argument list, the '>>' has
15538 to be a typo for '> >'. We emit the error message, but we
15539 continue parsing and we push a '>' as next token, so that
15540 the argument list will be parsed correctly. Note that the
15541 global source location is still on the token before the
15542 '>>', so we need to say explicitly where we want it. */
15543 cp_token *token = cp_lexer_peek_token (parser->lexer);
15544 error ("%H%<>>%> should be %<> >%> "
15545 "within a nested template argument list",
15548 /* ??? Proper recovery should terminate two levels of
15549 template argument list here. */
15550 token->type = CPP_GREATER;
15554 /* If this is not a nested template argument list, the '>>'
15555 is a typo for '>'. Emit an error message and continue.
15556 Same deal about the token location, but here we can get it
15557 right by consuming the '>>' before issuing the diagnostic. */
15558 cp_lexer_consume_token (parser->lexer);
15559 error ("spurious %<>>%>, use %<>%> to terminate "
15560 "a template argument list");
15564 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15565 /* The `>' token might be a greater-than operator again now. */
15566 parser->greater_than_is_operator_p
15567 = saved_greater_than_is_operator_p;
15568 /* Restore the SAVED_SCOPE. */
15569 parser->scope = saved_scope;
15570 parser->qualifying_scope = saved_qualifying_scope;
15571 parser->object_scope = saved_object_scope;
15572 skip_evaluation = saved_skip_evaluation;
15577 /* MEMBER_FUNCTION is a member function, or a friend. If default
15578 arguments, or the body of the function have not yet been parsed,
15582 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15584 /* If this member is a template, get the underlying
15586 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15587 member_function = DECL_TEMPLATE_RESULT (member_function);
15589 /* There should not be any class definitions in progress at this
15590 point; the bodies of members are only parsed outside of all class
15592 gcc_assert (parser->num_classes_being_defined == 0);
15593 /* While we're parsing the member functions we might encounter more
15594 classes. We want to handle them right away, but we don't want
15595 them getting mixed up with functions that are currently in the
15597 parser->unparsed_functions_queues
15598 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15600 /* Make sure that any template parameters are in scope. */
15601 maybe_begin_member_template_processing (member_function);
15603 /* If the body of the function has not yet been parsed, parse it
15605 if (DECL_PENDING_INLINE_P (member_function))
15607 tree function_scope;
15608 cp_token_cache *tokens;
15610 /* The function is no longer pending; we are processing it. */
15611 tokens = DECL_PENDING_INLINE_INFO (member_function);
15612 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15613 DECL_PENDING_INLINE_P (member_function) = 0;
15615 /* If this is a local class, enter the scope of the containing
15617 function_scope = current_function_decl;
15618 if (function_scope)
15619 push_function_context_to (function_scope);
15622 /* Push the body of the function onto the lexer stack. */
15623 cp_parser_push_lexer_for_tokens (parser, tokens);
15625 /* Let the front end know that we going to be defining this
15627 start_preparsed_function (member_function, NULL_TREE,
15628 SF_PRE_PARSED | SF_INCLASS_INLINE);
15630 /* Don't do access checking if it is a templated function. */
15631 if (processing_template_decl)
15632 push_deferring_access_checks (dk_no_check);
15634 /* Now, parse the body of the function. */
15635 cp_parser_function_definition_after_declarator (parser,
15636 /*inline_p=*/true);
15638 if (processing_template_decl)
15639 pop_deferring_access_checks ();
15641 /* Leave the scope of the containing function. */
15642 if (function_scope)
15643 pop_function_context_from (function_scope);
15644 cp_parser_pop_lexer (parser);
15647 /* Remove any template parameters from the symbol table. */
15648 maybe_end_member_template_processing ();
15650 /* Restore the queue. */
15651 parser->unparsed_functions_queues
15652 = TREE_CHAIN (parser->unparsed_functions_queues);
15655 /* If DECL contains any default args, remember it on the unparsed
15656 functions queue. */
15659 cp_parser_save_default_args (cp_parser* parser, tree decl)
15663 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15665 probe = TREE_CHAIN (probe))
15666 if (TREE_PURPOSE (probe))
15668 TREE_PURPOSE (parser->unparsed_functions_queues)
15669 = tree_cons (current_class_type, decl,
15670 TREE_PURPOSE (parser->unparsed_functions_queues));
15676 /* FN is a FUNCTION_DECL which may contains a parameter with an
15677 unparsed DEFAULT_ARG. Parse the default args now. This function
15678 assumes that the current scope is the scope in which the default
15679 argument should be processed. */
15682 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15684 bool saved_local_variables_forbidden_p;
15687 /* While we're parsing the default args, we might (due to the
15688 statement expression extension) encounter more classes. We want
15689 to handle them right away, but we don't want them getting mixed
15690 up with default args that are currently in the queue. */
15691 parser->unparsed_functions_queues
15692 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15694 /* Local variable names (and the `this' keyword) may not appear
15695 in a default argument. */
15696 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15697 parser->local_variables_forbidden_p = true;
15699 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15701 parm = TREE_CHAIN (parm))
15703 cp_token_cache *tokens;
15704 tree default_arg = TREE_PURPOSE (parm);
15706 VEC(tree,gc) *insts;
15713 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15714 /* This can happen for a friend declaration for a function
15715 already declared with default arguments. */
15718 /* Push the saved tokens for the default argument onto the parser's
15720 tokens = DEFARG_TOKENS (default_arg);
15721 cp_parser_push_lexer_for_tokens (parser, tokens);
15723 /* Parse the assignment-expression. */
15724 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15726 if (!processing_template_decl)
15727 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15729 TREE_PURPOSE (parm) = parsed_arg;
15731 /* Update any instantiations we've already created. */
15732 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15733 VEC_iterate (tree, insts, ix, copy); ix++)
15734 TREE_PURPOSE (copy) = parsed_arg;
15736 /* If the token stream has not been completely used up, then
15737 there was extra junk after the end of the default
15739 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15740 cp_parser_error (parser, "expected %<,%>");
15742 /* Revert to the main lexer. */
15743 cp_parser_pop_lexer (parser);
15746 /* Restore the state of local_variables_forbidden_p. */
15747 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15749 /* Restore the queue. */
15750 parser->unparsed_functions_queues
15751 = TREE_CHAIN (parser->unparsed_functions_queues);
15754 /* Parse the operand of `sizeof' (or a similar operator). Returns
15755 either a TYPE or an expression, depending on the form of the
15756 input. The KEYWORD indicates which kind of expression we have
15760 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15762 static const char *format;
15763 tree expr = NULL_TREE;
15764 const char *saved_message;
15765 bool saved_integral_constant_expression_p;
15766 bool saved_non_integral_constant_expression_p;
15768 /* Initialize FORMAT the first time we get here. */
15770 format = "types may not be defined in '%s' expressions";
15772 /* Types cannot be defined in a `sizeof' expression. Save away the
15774 saved_message = parser->type_definition_forbidden_message;
15775 /* And create the new one. */
15776 parser->type_definition_forbidden_message
15777 = XNEWVEC (const char, strlen (format)
15778 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15780 sprintf ((char *) parser->type_definition_forbidden_message,
15781 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15783 /* The restrictions on constant-expressions do not apply inside
15784 sizeof expressions. */
15785 saved_integral_constant_expression_p
15786 = parser->integral_constant_expression_p;
15787 saved_non_integral_constant_expression_p
15788 = parser->non_integral_constant_expression_p;
15789 parser->integral_constant_expression_p = false;
15791 /* Do not actually evaluate the expression. */
15793 /* If it's a `(', then we might be looking at the type-id
15795 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15798 bool saved_in_type_id_in_expr_p;
15800 /* We can't be sure yet whether we're looking at a type-id or an
15802 cp_parser_parse_tentatively (parser);
15803 /* Consume the `('. */
15804 cp_lexer_consume_token (parser->lexer);
15805 /* Parse the type-id. */
15806 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15807 parser->in_type_id_in_expr_p = true;
15808 type = cp_parser_type_id (parser);
15809 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15810 /* Now, look for the trailing `)'. */
15811 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15812 /* If all went well, then we're done. */
15813 if (cp_parser_parse_definitely (parser))
15815 cp_decl_specifier_seq decl_specs;
15817 /* Build a trivial decl-specifier-seq. */
15818 clear_decl_specs (&decl_specs);
15819 decl_specs.type = type;
15821 /* Call grokdeclarator to figure out what type this is. */
15822 expr = grokdeclarator (NULL,
15826 /*attrlist=*/NULL);
15830 /* If the type-id production did not work out, then we must be
15831 looking at the unary-expression production. */
15833 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15835 /* Go back to evaluating expressions. */
15838 /* Free the message we created. */
15839 free ((char *) parser->type_definition_forbidden_message);
15840 /* And restore the old one. */
15841 parser->type_definition_forbidden_message = saved_message;
15842 parser->integral_constant_expression_p
15843 = saved_integral_constant_expression_p;
15844 parser->non_integral_constant_expression_p
15845 = saved_non_integral_constant_expression_p;
15850 /* If the current declaration has no declarator, return true. */
15853 cp_parser_declares_only_class_p (cp_parser *parser)
15855 /* If the next token is a `;' or a `,' then there is no
15857 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15858 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15861 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15864 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15865 cp_storage_class storage_class)
15867 if (decl_specs->storage_class != sc_none)
15868 decl_specs->multiple_storage_classes_p = true;
15870 decl_specs->storage_class = storage_class;
15873 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15874 is true, the type is a user-defined type; otherwise it is a
15875 built-in type specified by a keyword. */
15878 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15880 bool user_defined_p)
15882 decl_specs->any_specifiers_p = true;
15884 /* If the user tries to redeclare bool or wchar_t (with, for
15885 example, in "typedef int wchar_t;") we remember that this is what
15886 happened. In system headers, we ignore these declarations so
15887 that G++ can work with system headers that are not C++-safe. */
15888 if (decl_specs->specs[(int) ds_typedef]
15890 && (type_spec == boolean_type_node
15891 || type_spec == wchar_type_node)
15892 && (decl_specs->type
15893 || decl_specs->specs[(int) ds_long]
15894 || decl_specs->specs[(int) ds_short]
15895 || decl_specs->specs[(int) ds_unsigned]
15896 || decl_specs->specs[(int) ds_signed]))
15898 decl_specs->redefined_builtin_type = type_spec;
15899 if (!decl_specs->type)
15901 decl_specs->type = type_spec;
15902 decl_specs->user_defined_type_p = false;
15905 else if (decl_specs->type)
15906 decl_specs->multiple_types_p = true;
15909 decl_specs->type = type_spec;
15910 decl_specs->user_defined_type_p = user_defined_p;
15911 decl_specs->redefined_builtin_type = NULL_TREE;
15915 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15916 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15919 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15921 return decl_specifiers->specs[(int) ds_friend] != 0;
15924 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15925 issue an error message indicating that TOKEN_DESC was expected.
15927 Returns the token consumed, if the token had the appropriate type.
15928 Otherwise, returns NULL. */
15931 cp_parser_require (cp_parser* parser,
15932 enum cpp_ttype type,
15933 const char* token_desc)
15935 if (cp_lexer_next_token_is (parser->lexer, type))
15936 return cp_lexer_consume_token (parser->lexer);
15939 /* Output the MESSAGE -- unless we're parsing tentatively. */
15940 if (!cp_parser_simulate_error (parser))
15942 char *message = concat ("expected ", token_desc, NULL);
15943 cp_parser_error (parser, message);
15950 /* Like cp_parser_require, except that tokens will be skipped until
15951 the desired token is found. An error message is still produced if
15952 the next token is not as expected. */
15955 cp_parser_skip_until_found (cp_parser* parser,
15956 enum cpp_ttype type,
15957 const char* token_desc)
15960 unsigned nesting_depth = 0;
15962 if (cp_parser_require (parser, type, token_desc))
15965 /* Skip tokens until the desired token is found. */
15968 /* Peek at the next token. */
15969 token = cp_lexer_peek_token (parser->lexer);
15970 /* If we've reached the token we want, consume it and
15972 if (token->type == type && !nesting_depth)
15974 cp_lexer_consume_token (parser->lexer);
15977 /* If we've run out of tokens, stop. */
15978 if (token->type == CPP_EOF)
15980 if (token->type == CPP_OPEN_BRACE
15981 || token->type == CPP_OPEN_PAREN
15982 || token->type == CPP_OPEN_SQUARE)
15984 else if (token->type == CPP_CLOSE_BRACE
15985 || token->type == CPP_CLOSE_PAREN
15986 || token->type == CPP_CLOSE_SQUARE)
15988 if (nesting_depth-- == 0)
15991 /* Consume this token. */
15992 cp_lexer_consume_token (parser->lexer);
15996 /* If the next token is the indicated keyword, consume it. Otherwise,
15997 issue an error message indicating that TOKEN_DESC was expected.
15999 Returns the token consumed, if the token had the appropriate type.
16000 Otherwise, returns NULL. */
16003 cp_parser_require_keyword (cp_parser* parser,
16005 const char* token_desc)
16007 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16009 if (token && token->keyword != keyword)
16011 dyn_string_t error_msg;
16013 /* Format the error message. */
16014 error_msg = dyn_string_new (0);
16015 dyn_string_append_cstr (error_msg, "expected ");
16016 dyn_string_append_cstr (error_msg, token_desc);
16017 cp_parser_error (parser, error_msg->s);
16018 dyn_string_delete (error_msg);
16025 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16026 function-definition. */
16029 cp_parser_token_starts_function_definition_p (cp_token* token)
16031 return (/* An ordinary function-body begins with an `{'. */
16032 token->type == CPP_OPEN_BRACE
16033 /* A ctor-initializer begins with a `:'. */
16034 || token->type == CPP_COLON
16035 /* A function-try-block begins with `try'. */
16036 || token->keyword == RID_TRY
16037 /* The named return value extension begins with `return'. */
16038 || token->keyword == RID_RETURN);
16041 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16045 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16049 token = cp_lexer_peek_token (parser->lexer);
16050 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16053 /* Returns TRUE iff the next token is the "," or ">" ending a
16054 template-argument. */
16057 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16061 token = cp_lexer_peek_token (parser->lexer);
16062 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16065 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16066 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16069 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16074 token = cp_lexer_peek_nth_token (parser->lexer, n);
16075 if (token->type == CPP_LESS)
16077 /* Check for the sequence `<::' in the original code. It would be lexed as
16078 `[:', where `[' is a digraph, and there is no whitespace before
16080 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16083 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16084 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16090 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16091 or none_type otherwise. */
16093 static enum tag_types
16094 cp_parser_token_is_class_key (cp_token* token)
16096 switch (token->keyword)
16101 return record_type;
16110 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16113 cp_parser_check_class_key (enum tag_types class_key, tree type)
16115 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16116 pedwarn ("%qs tag used in naming %q#T",
16117 class_key == union_type ? "union"
16118 : class_key == record_type ? "struct" : "class",
16122 /* Issue an error message if DECL is redeclared with different
16123 access than its original declaration [class.access.spec/3].
16124 This applies to nested classes and nested class templates.
16128 cp_parser_check_access_in_redeclaration (tree decl)
16130 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16133 if ((TREE_PRIVATE (decl)
16134 != (current_access_specifier == access_private_node))
16135 || (TREE_PROTECTED (decl)
16136 != (current_access_specifier == access_protected_node)))
16137 error ("%qD redeclared with different access", decl);
16140 /* Look for the `template' keyword, as a syntactic disambiguator.
16141 Return TRUE iff it is present, in which case it will be
16145 cp_parser_optional_template_keyword (cp_parser *parser)
16147 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16149 /* The `template' keyword can only be used within templates;
16150 outside templates the parser can always figure out what is a
16151 template and what is not. */
16152 if (!processing_template_decl)
16154 error ("%<template%> (as a disambiguator) is only allowed "
16155 "within templates");
16156 /* If this part of the token stream is rescanned, the same
16157 error message would be generated. So, we purge the token
16158 from the stream. */
16159 cp_lexer_purge_token (parser->lexer);
16164 /* Consume the `template' keyword. */
16165 cp_lexer_consume_token (parser->lexer);
16173 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16174 set PARSER->SCOPE, and perform other related actions. */
16177 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16182 /* Get the stored value. */
16183 value = cp_lexer_consume_token (parser->lexer)->value;
16184 /* Perform any access checks that were deferred. */
16185 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16186 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16187 /* Set the scope from the stored value. */
16188 parser->scope = TREE_VALUE (value);
16189 parser->qualifying_scope = TREE_TYPE (value);
16190 parser->object_scope = NULL_TREE;
16193 /* Consume tokens up through a non-nested END token. */
16196 cp_parser_cache_group (cp_parser *parser,
16197 enum cpp_ttype end,
16204 /* Abort a parenthesized expression if we encounter a brace. */
16205 if ((end == CPP_CLOSE_PAREN || depth == 0)
16206 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16208 /* If we've reached the end of the file, stop. */
16209 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16211 /* Consume the next token. */
16212 token = cp_lexer_consume_token (parser->lexer);
16213 /* See if it starts a new group. */
16214 if (token->type == CPP_OPEN_BRACE)
16216 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16220 else if (token->type == CPP_OPEN_PAREN)
16221 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16222 else if (token->type == end)
16227 /* Begin parsing tentatively. We always save tokens while parsing
16228 tentatively so that if the tentative parsing fails we can restore the
16232 cp_parser_parse_tentatively (cp_parser* parser)
16234 /* Enter a new parsing context. */
16235 parser->context = cp_parser_context_new (parser->context);
16236 /* Begin saving tokens. */
16237 cp_lexer_save_tokens (parser->lexer);
16238 /* In order to avoid repetitive access control error messages,
16239 access checks are queued up until we are no longer parsing
16241 push_deferring_access_checks (dk_deferred);
16244 /* Commit to the currently active tentative parse. */
16247 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16249 cp_parser_context *context;
16252 /* Mark all of the levels as committed. */
16253 lexer = parser->lexer;
16254 for (context = parser->context; context->next; context = context->next)
16256 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16258 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16259 while (!cp_lexer_saving_tokens (lexer))
16260 lexer = lexer->next;
16261 cp_lexer_commit_tokens (lexer);
16265 /* Abort the currently active tentative parse. All consumed tokens
16266 will be rolled back, and no diagnostics will be issued. */
16269 cp_parser_abort_tentative_parse (cp_parser* parser)
16271 cp_parser_simulate_error (parser);
16272 /* Now, pretend that we want to see if the construct was
16273 successfully parsed. */
16274 cp_parser_parse_definitely (parser);
16277 /* Stop parsing tentatively. If a parse error has occurred, restore the
16278 token stream. Otherwise, commit to the tokens we have consumed.
16279 Returns true if no error occurred; false otherwise. */
16282 cp_parser_parse_definitely (cp_parser* parser)
16284 bool error_occurred;
16285 cp_parser_context *context;
16287 /* Remember whether or not an error occurred, since we are about to
16288 destroy that information. */
16289 error_occurred = cp_parser_error_occurred (parser);
16290 /* Remove the topmost context from the stack. */
16291 context = parser->context;
16292 parser->context = context->next;
16293 /* If no parse errors occurred, commit to the tentative parse. */
16294 if (!error_occurred)
16296 /* Commit to the tokens read tentatively, unless that was
16298 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16299 cp_lexer_commit_tokens (parser->lexer);
16301 pop_to_parent_deferring_access_checks ();
16303 /* Otherwise, if errors occurred, roll back our state so that things
16304 are just as they were before we began the tentative parse. */
16307 cp_lexer_rollback_tokens (parser->lexer);
16308 pop_deferring_access_checks ();
16310 /* Add the context to the front of the free list. */
16311 context->next = cp_parser_context_free_list;
16312 cp_parser_context_free_list = context;
16314 return !error_occurred;
16317 /* Returns true if we are parsing tentatively and are not committed to
16318 this tentative parse. */
16321 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16323 return (cp_parser_parsing_tentatively (parser)
16324 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16327 /* Returns nonzero iff an error has occurred during the most recent
16328 tentative parse. */
16331 cp_parser_error_occurred (cp_parser* parser)
16333 return (cp_parser_parsing_tentatively (parser)
16334 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16337 /* Returns nonzero if GNU extensions are allowed. */
16340 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16342 return parser->allow_gnu_extensions_p;
16345 /* Objective-C++ Productions */
16348 /* Parse an Objective-C expression, which feeds into a primary-expression
16352 objc-message-expression
16353 objc-string-literal
16354 objc-encode-expression
16355 objc-protocol-expression
16356 objc-selector-expression
16358 Returns a tree representation of the expression. */
16361 cp_parser_objc_expression (cp_parser* parser)
16363 /* Try to figure out what kind of declaration is present. */
16364 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16368 case CPP_OPEN_SQUARE:
16369 return cp_parser_objc_message_expression (parser);
16371 case CPP_OBJC_STRING:
16372 kwd = cp_lexer_consume_token (parser->lexer);
16373 return objc_build_string_object (kwd->value);
16376 switch (kwd->keyword)
16378 case RID_AT_ENCODE:
16379 return cp_parser_objc_encode_expression (parser);
16381 case RID_AT_PROTOCOL:
16382 return cp_parser_objc_protocol_expression (parser);
16384 case RID_AT_SELECTOR:
16385 return cp_parser_objc_selector_expression (parser);
16391 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16392 cp_parser_skip_to_end_of_block_or_statement (parser);
16395 return error_mark_node;
16398 /* Parse an Objective-C message expression.
16400 objc-message-expression:
16401 [ objc-message-receiver objc-message-args ]
16403 Returns a representation of an Objective-C message. */
16406 cp_parser_objc_message_expression (cp_parser* parser)
16408 tree receiver, messageargs;
16410 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16411 receiver = cp_parser_objc_message_receiver (parser);
16412 messageargs = cp_parser_objc_message_args (parser);
16413 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16415 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16418 /* Parse an objc-message-receiver.
16420 objc-message-receiver:
16422 simple-type-specifier
16424 Returns a representation of the type or expression. */
16427 cp_parser_objc_message_receiver (cp_parser* parser)
16431 /* An Objective-C message receiver may be either (1) a type
16432 or (2) an expression. */
16433 cp_parser_parse_tentatively (parser);
16434 rcv = cp_parser_expression (parser, false);
16436 if (cp_parser_parse_definitely (parser))
16439 rcv = cp_parser_simple_type_specifier (parser,
16440 /*decl_specs=*/NULL,
16441 CP_PARSER_FLAGS_NONE);
16443 return objc_get_class_reference (rcv);
16446 /* Parse the arguments and selectors comprising an Objective-C message.
16451 objc-selector-args , objc-comma-args
16453 objc-selector-args:
16454 objc-selector [opt] : assignment-expression
16455 objc-selector-args objc-selector [opt] : assignment-expression
16458 assignment-expression
16459 objc-comma-args , assignment-expression
16461 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16462 selector arguments and TREE_VALUE containing a list of comma
16466 cp_parser_objc_message_args (cp_parser* parser)
16468 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16469 bool maybe_unary_selector_p = true;
16470 cp_token *token = cp_lexer_peek_token (parser->lexer);
16472 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16474 tree selector = NULL_TREE, arg;
16476 if (token->type != CPP_COLON)
16477 selector = cp_parser_objc_selector (parser);
16479 /* Detect if we have a unary selector. */
16480 if (maybe_unary_selector_p
16481 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16482 return build_tree_list (selector, NULL_TREE);
16484 maybe_unary_selector_p = false;
16485 cp_parser_require (parser, CPP_COLON, "`:'");
16486 arg = cp_parser_assignment_expression (parser, false);
16489 = chainon (sel_args,
16490 build_tree_list (selector, arg));
16492 token = cp_lexer_peek_token (parser->lexer);
16495 /* Handle non-selector arguments, if any. */
16496 while (token->type == CPP_COMMA)
16500 cp_lexer_consume_token (parser->lexer);
16501 arg = cp_parser_assignment_expression (parser, false);
16504 = chainon (addl_args,
16505 build_tree_list (NULL_TREE, arg));
16507 token = cp_lexer_peek_token (parser->lexer);
16510 return build_tree_list (sel_args, addl_args);
16513 /* Parse an Objective-C encode expression.
16515 objc-encode-expression:
16516 @encode objc-typename
16518 Returns an encoded representation of the type argument. */
16521 cp_parser_objc_encode_expression (cp_parser* parser)
16525 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16526 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16527 type = complete_type (cp_parser_type_id (parser));
16528 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16532 error ("%<@encode%> must specify a type as an argument");
16533 return error_mark_node;
16536 return objc_build_encode_expr (type);
16539 /* Parse an Objective-C @defs expression. */
16542 cp_parser_objc_defs_expression (cp_parser *parser)
16546 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16547 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16548 name = cp_parser_identifier (parser);
16549 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16551 return objc_get_class_ivars (name);
16554 /* Parse an Objective-C protocol expression.
16556 objc-protocol-expression:
16557 @protocol ( identifier )
16559 Returns a representation of the protocol expression. */
16562 cp_parser_objc_protocol_expression (cp_parser* parser)
16566 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16567 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16568 proto = cp_parser_identifier (parser);
16569 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16571 return objc_build_protocol_expr (proto);
16574 /* Parse an Objective-C selector expression.
16576 objc-selector-expression:
16577 @selector ( objc-method-signature )
16579 objc-method-signature:
16585 objc-selector-seq objc-selector :
16587 Returns a representation of the method selector. */
16590 cp_parser_objc_selector_expression (cp_parser* parser)
16592 tree sel_seq = NULL_TREE;
16593 bool maybe_unary_selector_p = true;
16596 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16597 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16598 token = cp_lexer_peek_token (parser->lexer);
16600 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16601 || token->type == CPP_SCOPE)
16603 tree selector = NULL_TREE;
16605 if (token->type != CPP_COLON
16606 || token->type == CPP_SCOPE)
16607 selector = cp_parser_objc_selector (parser);
16609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16610 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16612 /* Detect if we have a unary selector. */
16613 if (maybe_unary_selector_p)
16615 sel_seq = selector;
16616 goto finish_selector;
16620 cp_parser_error (parser, "expected %<:%>");
16623 maybe_unary_selector_p = false;
16624 token = cp_lexer_consume_token (parser->lexer);
16626 if (token->type == CPP_SCOPE)
16629 = chainon (sel_seq,
16630 build_tree_list (selector, NULL_TREE));
16632 = chainon (sel_seq,
16633 build_tree_list (NULL_TREE, NULL_TREE));
16637 = chainon (sel_seq,
16638 build_tree_list (selector, NULL_TREE));
16640 token = cp_lexer_peek_token (parser->lexer);
16644 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16646 return objc_build_selector_expr (sel_seq);
16649 /* Parse a list of identifiers.
16651 objc-identifier-list:
16653 objc-identifier-list , identifier
16655 Returns a TREE_LIST of identifier nodes. */
16658 cp_parser_objc_identifier_list (cp_parser* parser)
16660 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16661 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16663 while (sep->type == CPP_COMMA)
16665 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16666 list = chainon (list,
16667 build_tree_list (NULL_TREE,
16668 cp_parser_identifier (parser)));
16669 sep = cp_lexer_peek_token (parser->lexer);
16675 /* Parse an Objective-C alias declaration.
16677 objc-alias-declaration:
16678 @compatibility_alias identifier identifier ;
16680 This function registers the alias mapping with the Objective-C front-end.
16681 It returns nothing. */
16684 cp_parser_objc_alias_declaration (cp_parser* parser)
16688 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16689 alias = cp_parser_identifier (parser);
16690 orig = cp_parser_identifier (parser);
16691 objc_declare_alias (alias, orig);
16692 cp_parser_consume_semicolon_at_end_of_statement (parser);
16695 /* Parse an Objective-C class forward-declaration.
16697 objc-class-declaration:
16698 @class objc-identifier-list ;
16700 The function registers the forward declarations with the Objective-C
16701 front-end. It returns nothing. */
16704 cp_parser_objc_class_declaration (cp_parser* parser)
16706 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16707 objc_declare_class (cp_parser_objc_identifier_list (parser));
16708 cp_parser_consume_semicolon_at_end_of_statement (parser);
16711 /* Parse a list of Objective-C protocol references.
16713 objc-protocol-refs-opt:
16714 objc-protocol-refs [opt]
16716 objc-protocol-refs:
16717 < objc-identifier-list >
16719 Returns a TREE_LIST of identifiers, if any. */
16722 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16724 tree protorefs = NULL_TREE;
16726 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16728 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16729 protorefs = cp_parser_objc_identifier_list (parser);
16730 cp_parser_require (parser, CPP_GREATER, "`>'");
16736 /* Parse a Objective-C visibility specification. */
16739 cp_parser_objc_visibility_spec (cp_parser* parser)
16741 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16743 switch (vis->keyword)
16745 case RID_AT_PRIVATE:
16746 objc_set_visibility (2);
16748 case RID_AT_PROTECTED:
16749 objc_set_visibility (0);
16751 case RID_AT_PUBLIC:
16752 objc_set_visibility (1);
16758 /* Eat '@private'/'@protected'/'@public'. */
16759 cp_lexer_consume_token (parser->lexer);
16762 /* Parse an Objective-C method type. */
16765 cp_parser_objc_method_type (cp_parser* parser)
16767 objc_set_method_type
16768 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16773 /* Parse an Objective-C protocol qualifier. */
16776 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16778 tree quals = NULL_TREE, node;
16779 cp_token *token = cp_lexer_peek_token (parser->lexer);
16781 node = token->value;
16783 while (node && TREE_CODE (node) == IDENTIFIER_NODE
16784 && (node == ridpointers [(int) RID_IN]
16785 || node == ridpointers [(int) RID_OUT]
16786 || node == ridpointers [(int) RID_INOUT]
16787 || node == ridpointers [(int) RID_BYCOPY]
16788 || node == ridpointers [(int) RID_BYREF]
16789 || node == ridpointers [(int) RID_ONEWAY]))
16791 quals = tree_cons (NULL_TREE, node, quals);
16792 cp_lexer_consume_token (parser->lexer);
16793 token = cp_lexer_peek_token (parser->lexer);
16794 node = token->value;
16800 /* Parse an Objective-C typename. */
16803 cp_parser_objc_typename (cp_parser* parser)
16805 tree typename = NULL_TREE;
16807 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16809 tree proto_quals, cp_type = NULL_TREE;
16811 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
16812 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16814 /* An ObjC type name may consist of just protocol qualifiers, in which
16815 case the type shall default to 'id'. */
16816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16817 cp_type = cp_parser_type_id (parser);
16819 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16820 typename = build_tree_list (proto_quals, cp_type);
16826 /* Check to see if TYPE refers to an Objective-C selector name. */
16829 cp_parser_objc_selector_p (enum cpp_ttype type)
16831 return (type == CPP_NAME || type == CPP_KEYWORD
16832 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16833 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16834 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16835 || type == CPP_XOR || type == CPP_XOR_EQ);
16838 /* Parse an Objective-C selector. */
16841 cp_parser_objc_selector (cp_parser* parser)
16843 cp_token *token = cp_lexer_consume_token (parser->lexer);
16845 if (!cp_parser_objc_selector_p (token->type))
16847 error ("invalid Objective-C++ selector name");
16848 return error_mark_node;
16851 /* C++ operator names are allowed to appear in ObjC selectors. */
16852 switch (token->type)
16854 case CPP_AND_AND: return get_identifier ("and");
16855 case CPP_AND_EQ: return get_identifier ("and_eq");
16856 case CPP_AND: return get_identifier ("bitand");
16857 case CPP_OR: return get_identifier ("bitor");
16858 case CPP_COMPL: return get_identifier ("compl");
16859 case CPP_NOT: return get_identifier ("not");
16860 case CPP_NOT_EQ: return get_identifier ("not_eq");
16861 case CPP_OR_OR: return get_identifier ("or");
16862 case CPP_OR_EQ: return get_identifier ("or_eq");
16863 case CPP_XOR: return get_identifier ("xor");
16864 case CPP_XOR_EQ: return get_identifier ("xor_eq");
16865 default: return token->value;
16869 /* Parse an Objective-C params list. */
16872 cp_parser_objc_method_keyword_params (cp_parser* parser)
16874 tree params = NULL_TREE;
16875 bool maybe_unary_selector_p = true;
16876 cp_token *token = cp_lexer_peek_token (parser->lexer);
16878 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16880 tree selector = NULL_TREE, typename, identifier;
16882 if (token->type != CPP_COLON)
16883 selector = cp_parser_objc_selector (parser);
16885 /* Detect if we have a unary selector. */
16886 if (maybe_unary_selector_p
16887 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16890 maybe_unary_selector_p = false;
16891 cp_parser_require (parser, CPP_COLON, "`:'");
16892 typename = cp_parser_objc_typename (parser);
16893 identifier = cp_parser_identifier (parser);
16897 objc_build_keyword_decl (selector,
16901 token = cp_lexer_peek_token (parser->lexer);
16907 /* Parse the non-keyword Objective-C params. */
16910 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
16912 tree params = make_node (TREE_LIST);
16913 cp_token *token = cp_lexer_peek_token (parser->lexer);
16914 *ellipsisp = false; /* Initially, assume no ellipsis. */
16916 while (token->type == CPP_COMMA)
16918 cp_parameter_declarator *parmdecl;
16921 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16922 token = cp_lexer_peek_token (parser->lexer);
16924 if (token->type == CPP_ELLIPSIS)
16926 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
16931 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
16932 parm = grokdeclarator (parmdecl->declarator,
16933 &parmdecl->decl_specifiers,
16934 PARM, /*initialized=*/0,
16935 /*attrlist=*/NULL);
16937 chainon (params, build_tree_list (NULL_TREE, parm));
16938 token = cp_lexer_peek_token (parser->lexer);
16944 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
16947 cp_parser_objc_interstitial_code (cp_parser* parser)
16949 cp_token *token = cp_lexer_peek_token (parser->lexer);
16951 /* If the next token is `extern' and the following token is a string
16952 literal, then we have a linkage specification. */
16953 if (token->keyword == RID_EXTERN
16954 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
16955 cp_parser_linkage_specification (parser);
16956 /* Handle #pragma, if any. */
16957 else if (token->type == CPP_PRAGMA)
16958 cp_lexer_handle_pragma (parser->lexer);
16959 /* Allow stray semicolons. */
16960 else if (token->type == CPP_SEMICOLON)
16961 cp_lexer_consume_token (parser->lexer);
16962 /* Finally, try to parse a block-declaration, or a function-definition. */
16964 cp_parser_block_declaration (parser, /*statement_p=*/false);
16967 /* Parse a method signature. */
16970 cp_parser_objc_method_signature (cp_parser* parser)
16972 tree rettype, kwdparms, optparms;
16973 bool ellipsis = false;
16975 cp_parser_objc_method_type (parser);
16976 rettype = cp_parser_objc_typename (parser);
16977 kwdparms = cp_parser_objc_method_keyword_params (parser);
16978 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
16980 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
16983 /* Pars an Objective-C method prototype list. */
16986 cp_parser_objc_method_prototype_list (cp_parser* parser)
16988 cp_token *token = cp_lexer_peek_token (parser->lexer);
16990 while (token->keyword != RID_AT_END)
16992 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16994 objc_add_method_declaration
16995 (cp_parser_objc_method_signature (parser));
16996 cp_parser_consume_semicolon_at_end_of_statement (parser);
16999 /* Allow for interspersed non-ObjC++ code. */
17000 cp_parser_objc_interstitial_code (parser);
17002 token = cp_lexer_peek_token (parser->lexer);
17005 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17006 objc_finish_interface ();
17009 /* Parse an Objective-C method definition list. */
17012 cp_parser_objc_method_definition_list (cp_parser* parser)
17014 cp_token *token = cp_lexer_peek_token (parser->lexer);
17016 while (token->keyword != RID_AT_END)
17020 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17022 push_deferring_access_checks (dk_deferred);
17023 objc_start_method_definition
17024 (cp_parser_objc_method_signature (parser));
17026 /* For historical reasons, we accept an optional semicolon. */
17027 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17028 cp_lexer_consume_token (parser->lexer);
17030 perform_deferred_access_checks ();
17031 stop_deferring_access_checks ();
17032 meth = cp_parser_function_definition_after_declarator (parser,
17034 pop_deferring_access_checks ();
17035 objc_finish_method_definition (meth);
17038 /* Allow for interspersed non-ObjC++ code. */
17039 cp_parser_objc_interstitial_code (parser);
17041 token = cp_lexer_peek_token (parser->lexer);
17044 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17045 objc_finish_implementation ();
17048 /* Parse Objective-C ivars. */
17051 cp_parser_objc_class_ivars (cp_parser* parser)
17053 cp_token *token = cp_lexer_peek_token (parser->lexer);
17055 if (token->type != CPP_OPEN_BRACE)
17056 return; /* No ivars specified. */
17058 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17059 token = cp_lexer_peek_token (parser->lexer);
17061 while (token->type != CPP_CLOSE_BRACE)
17063 cp_decl_specifier_seq declspecs;
17064 int decl_class_or_enum_p;
17065 tree prefix_attributes;
17067 cp_parser_objc_visibility_spec (parser);
17069 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17072 cp_parser_decl_specifier_seq (parser,
17073 CP_PARSER_FLAGS_OPTIONAL,
17075 &decl_class_or_enum_p);
17076 prefix_attributes = declspecs.attributes;
17077 declspecs.attributes = NULL_TREE;
17079 /* Keep going until we hit the `;' at the end of the
17081 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17083 tree width = NULL_TREE, attributes, first_attribute, decl;
17084 cp_declarator *declarator = NULL;
17085 int ctor_dtor_or_conv_p;
17087 /* Check for a (possibly unnamed) bitfield declaration. */
17088 token = cp_lexer_peek_token (parser->lexer);
17089 if (token->type == CPP_COLON)
17092 if (token->type == CPP_NAME
17093 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17096 /* Get the name of the bitfield. */
17097 declarator = make_id_declarator (NULL_TREE,
17098 cp_parser_identifier (parser));
17101 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17102 /* Get the width of the bitfield. */
17104 = cp_parser_constant_expression (parser,
17105 /*allow_non_constant=*/false,
17110 /* Parse the declarator. */
17112 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17113 &ctor_dtor_or_conv_p,
17114 /*parenthesized_p=*/NULL,
17115 /*member_p=*/false);
17118 /* Look for attributes that apply to the ivar. */
17119 attributes = cp_parser_attributes_opt (parser);
17120 /* Remember which attributes are prefix attributes and
17122 first_attribute = attributes;
17123 /* Combine the attributes. */
17124 attributes = chainon (prefix_attributes, attributes);
17128 /* Create the bitfield declaration. */
17129 decl = grokbitfield (declarator, &declspecs, width);
17130 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17133 decl = grokfield (declarator, &declspecs, NULL_TREE,
17134 NULL_TREE, attributes);
17136 /* Add the instance variable. */
17137 objc_add_instance_variable (decl);
17139 /* Reset PREFIX_ATTRIBUTES. */
17140 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17141 attributes = TREE_CHAIN (attributes);
17143 TREE_CHAIN (attributes) = NULL_TREE;
17145 token = cp_lexer_peek_token (parser->lexer);
17147 if (token->type == CPP_COMMA)
17149 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17155 cp_parser_consume_semicolon_at_end_of_statement (parser);
17156 token = cp_lexer_peek_token (parser->lexer);
17159 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17160 /* For historical reasons, we accept an optional semicolon. */
17161 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17162 cp_lexer_consume_token (parser->lexer);
17165 /* Parse an Objective-C protocol declaration. */
17168 cp_parser_objc_protocol_declaration (cp_parser* parser)
17170 tree proto, protorefs;
17173 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17174 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17176 error ("identifier expected after %<@protocol%>");
17180 /* See if we have a forward declaration or a definition. */
17181 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17183 /* Try a forward declaration first. */
17184 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17186 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17188 cp_parser_consume_semicolon_at_end_of_statement (parser);
17191 /* Ok, we got a full-fledged definition (or at least should). */
17194 proto = cp_parser_identifier (parser);
17195 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17196 objc_start_protocol (proto, protorefs);
17197 cp_parser_objc_method_prototype_list (parser);
17201 /* Parse an Objective-C superclass or category. */
17204 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17207 cp_token *next = cp_lexer_peek_token (parser->lexer);
17209 *super = *categ = NULL_TREE;
17210 if (next->type == CPP_COLON)
17212 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17213 *super = cp_parser_identifier (parser);
17215 else if (next->type == CPP_OPEN_PAREN)
17217 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17218 *categ = cp_parser_identifier (parser);
17219 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17223 /* Parse an Objective-C class interface. */
17226 cp_parser_objc_class_interface (cp_parser* parser)
17228 tree name, super, categ, protos;
17230 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17231 name = cp_parser_identifier (parser);
17232 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17233 protos = cp_parser_objc_protocol_refs_opt (parser);
17235 /* We have either a class or a category on our hands. */
17237 objc_start_category_interface (name, categ, protos);
17240 objc_start_class_interface (name, super, protos);
17241 /* Handle instance variable declarations, if any. */
17242 cp_parser_objc_class_ivars (parser);
17243 objc_continue_interface ();
17246 cp_parser_objc_method_prototype_list (parser);
17249 /* Parse an Objective-C class implementation. */
17252 cp_parser_objc_class_implementation (cp_parser* parser)
17254 tree name, super, categ;
17256 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17257 name = cp_parser_identifier (parser);
17258 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17260 /* We have either a class or a category on our hands. */
17262 objc_start_category_implementation (name, categ);
17265 objc_start_class_implementation (name, super);
17266 /* Handle instance variable declarations, if any. */
17267 cp_parser_objc_class_ivars (parser);
17268 objc_continue_implementation ();
17271 cp_parser_objc_method_definition_list (parser);
17274 /* Consume the @end token and finish off the implementation. */
17277 cp_parser_objc_end_implementation (cp_parser* parser)
17279 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17280 objc_finish_implementation ();
17283 /* Parse an Objective-C declaration. */
17286 cp_parser_objc_declaration (cp_parser* parser)
17288 /* Try to figure out what kind of declaration is present. */
17289 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17291 switch (kwd->keyword)
17294 cp_parser_objc_alias_declaration (parser);
17297 cp_parser_objc_class_declaration (parser);
17299 case RID_AT_PROTOCOL:
17300 cp_parser_objc_protocol_declaration (parser);
17302 case RID_AT_INTERFACE:
17303 cp_parser_objc_class_interface (parser);
17305 case RID_AT_IMPLEMENTATION:
17306 cp_parser_objc_class_implementation (parser);
17309 cp_parser_objc_end_implementation (parser);
17312 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17313 cp_parser_skip_to_end_of_block_or_statement (parser);
17317 /* Parse an Objective-C try-catch-finally statement.
17319 objc-try-catch-finally-stmt:
17320 @try compound-statement objc-catch-clause-seq [opt]
17321 objc-finally-clause [opt]
17323 objc-catch-clause-seq:
17324 objc-catch-clause objc-catch-clause-seq [opt]
17327 @catch ( exception-declaration ) compound-statement
17329 objc-finally-clause
17330 @finally compound-statement
17332 Returns NULL_TREE. */
17335 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17336 location_t location;
17339 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17340 location = cp_lexer_peek_token (parser->lexer)->location;
17341 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17342 node, lest it get absorbed into the surrounding block. */
17343 stmt = push_stmt_list ();
17344 cp_parser_compound_statement (parser, NULL, false);
17345 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17347 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17349 cp_parameter_declarator *parmdecl;
17352 cp_lexer_consume_token (parser->lexer);
17353 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17354 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17355 parm = grokdeclarator (parmdecl->declarator,
17356 &parmdecl->decl_specifiers,
17357 PARM, /*initialized=*/0,
17358 /*attrlist=*/NULL);
17359 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17360 objc_begin_catch_clause (parm);
17361 cp_parser_compound_statement (parser, NULL, false);
17362 objc_finish_catch_clause ();
17365 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17367 cp_lexer_consume_token (parser->lexer);
17368 location = cp_lexer_peek_token (parser->lexer)->location;
17369 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17370 node, lest it get absorbed into the surrounding block. */
17371 stmt = push_stmt_list ();
17372 cp_parser_compound_statement (parser, NULL, false);
17373 objc_build_finally_clause (location, pop_stmt_list (stmt));
17376 return objc_finish_try_stmt ();
17379 /* Parse an Objective-C synchronized statement.
17381 objc-synchronized-stmt:
17382 @synchronized ( expression ) compound-statement
17384 Returns NULL_TREE. */
17387 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17388 location_t location;
17391 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17393 location = cp_lexer_peek_token (parser->lexer)->location;
17394 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17395 lock = cp_parser_expression (parser, false);
17396 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17398 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17399 node, lest it get absorbed into the surrounding block. */
17400 stmt = push_stmt_list ();
17401 cp_parser_compound_statement (parser, NULL, false);
17403 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17406 /* Parse an Objective-C throw statement.
17409 @throw assignment-expression [opt] ;
17411 Returns a constructed '@throw' statement. */
17414 cp_parser_objc_throw_statement (cp_parser *parser) {
17415 tree expr = NULL_TREE;
17417 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17419 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17420 expr = cp_parser_assignment_expression (parser, false);
17422 cp_parser_consume_semicolon_at_end_of_statement (parser);
17424 return objc_build_throw_stmt (expr);
17427 /* Parse an Objective-C statement. */
17430 cp_parser_objc_statement (cp_parser * parser) {
17431 /* Try to figure out what kind of declaration is present. */
17432 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17434 switch (kwd->keyword)
17437 return cp_parser_objc_try_catch_finally_statement (parser);
17438 case RID_AT_SYNCHRONIZED:
17439 return cp_parser_objc_synchronized_statement (parser);
17441 return cp_parser_objc_throw_statement (parser);
17443 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17444 cp_parser_skip_to_end_of_block_or_statement (parser);
17447 return error_mark_node;
17452 static GTY (()) cp_parser *the_parser;
17454 /* External interface. */
17456 /* Parse one entire translation unit. */
17459 c_parse_file (void)
17461 bool error_occurred;
17462 static bool already_called = false;
17464 if (already_called)
17466 sorry ("inter-module optimizations not implemented for C++");
17469 already_called = true;
17471 the_parser = cp_parser_new ();
17472 push_deferring_access_checks (flag_access_control
17473 ? dk_no_deferred : dk_no_check);
17474 error_occurred = cp_parser_translation_unit (the_parser);
17478 /* This variable must be provided by every front end. */
17482 #include "gt-cp-parser.h"