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 /* The value associated with this token, if any. */
64 /* The location at which this token was found. */
68 /* We use a stack of token pointer for saving token sets. */
69 typedef struct cp_token *cp_token_position;
70 DEF_VEC_P (cp_token_position);
71 DEF_VEC_ALLOC_P (cp_token_position,heap);
73 static const cp_token eof_token =
75 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
76 #if USE_MAPPED_LOCATION
83 /* The cp_lexer structure represents the C++ lexer. It is responsible
84 for managing the token stream from the preprocessor and supplying
85 it to the parser. Tokens are never added to the cp_lexer after
88 typedef struct cp_lexer GTY (())
90 /* The memory allocated for the buffer. NULL if this lexer does not
91 own the token buffer. */
92 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
93 /* If the lexer owns the buffer, this is the number of tokens in the
97 /* A pointer just past the last available token. The tokens
98 in this lexer are [buffer, last_token). */
99 cp_token_position GTY ((skip)) last_token;
101 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
102 no more available tokens. */
103 cp_token_position GTY ((skip)) next_token;
105 /* A stack indicating positions at which cp_lexer_save_tokens was
106 called. The top entry is the most recent position at which we
107 began saving tokens. If the stack is non-empty, we are saving
109 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
111 /* True if we should output debugging information. */
114 /* The next lexer in a linked list of lexers. */
115 struct cp_lexer *next;
118 /* cp_token_cache is a range of tokens. There is no need to represent
119 allocate heap memory for it, since tokens are never removed from the
120 lexer's array. There is also no need for the GC to walk through
121 a cp_token_cache, since everything in here is referenced through
124 typedef struct cp_token_cache GTY(())
126 /* The beginning of the token range. */
127 cp_token * GTY((skip)) first;
129 /* Points immediately after the last token in the range. */
130 cp_token * GTY ((skip)) last;
135 static cp_lexer *cp_lexer_new_main
137 static cp_lexer *cp_lexer_new_from_tokens
138 (cp_token_cache *tokens);
139 static void cp_lexer_destroy
141 static int cp_lexer_saving_tokens
143 static cp_token_position cp_lexer_token_position
145 static cp_token *cp_lexer_token_at
146 (cp_lexer *, cp_token_position);
147 static void cp_lexer_get_preprocessor_token
148 (cp_lexer *, cp_token *);
149 static inline cp_token *cp_lexer_peek_token
151 static cp_token *cp_lexer_peek_nth_token
152 (cp_lexer *, size_t);
153 static inline bool cp_lexer_next_token_is
154 (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_not
156 (cp_lexer *, enum cpp_ttype);
157 static bool cp_lexer_next_token_is_keyword
158 (cp_lexer *, enum rid);
159 static cp_token *cp_lexer_consume_token
161 static void cp_lexer_purge_token
163 static void cp_lexer_purge_tokens_after
164 (cp_lexer *, cp_token_position);
165 static void cp_lexer_handle_pragma
167 static void cp_lexer_save_tokens
169 static void cp_lexer_commit_tokens
171 static void cp_lexer_rollback_tokens
173 #ifdef ENABLE_CHECKING
174 static void cp_lexer_print_token
175 (FILE *, cp_token *);
176 static inline bool cp_lexer_debugging_p
178 static void cp_lexer_start_debugging
179 (cp_lexer *) ATTRIBUTE_UNUSED;
180 static void cp_lexer_stop_debugging
181 (cp_lexer *) ATTRIBUTE_UNUSED;
183 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
184 about passing NULL to functions that require non-NULL arguments
185 (fputs, fprintf). It will never be used, so all we need is a value
186 of the right type that's guaranteed not to be NULL. */
187 #define cp_lexer_debug_stream stdout
188 #define cp_lexer_print_token(str, tok) (void) 0
189 #define cp_lexer_debugging_p(lexer) 0
190 #endif /* ENABLE_CHECKING */
192 static cp_token_cache *cp_token_cache_new
193 (cp_token *, cp_token *);
195 /* Manifest constants. */
196 #define CP_LEXER_BUFFER_SIZE 10000
197 #define CP_SAVED_TOKEN_STACK 5
199 /* A token type for keywords, as opposed to ordinary identifiers. */
200 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
202 /* A token type for template-ids. If a template-id is processed while
203 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
204 the value of the CPP_TEMPLATE_ID is whatever was returned by
205 cp_parser_template_id. */
206 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
208 /* A token type for nested-name-specifiers. If a
209 nested-name-specifier is processed while parsing tentatively, it is
210 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
211 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
212 cp_parser_nested_name_specifier_opt. */
213 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
215 /* A token type for tokens that are not tokens at all; these are used
216 to represent slots in the array where there used to be a token
217 that has now been deleted. */
218 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
220 /* The number of token types, including C++-specific ones. */
221 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
225 #ifdef ENABLE_CHECKING
226 /* The stream to which debugging output should be written. */
227 static FILE *cp_lexer_debug_stream;
228 #endif /* ENABLE_CHECKING */
230 /* Create a new main C++ lexer, the lexer that gets tokens from the
234 cp_lexer_new_main (void)
236 cp_token first_token;
243 /* It's possible that lexing the first token will load a PCH file,
244 which is a GC collection point. So we have to grab the first
245 token before allocating any memory. Pragmas must not be deferred
246 as -fpch-preprocess can generate a pragma to load the PCH file in
247 the preprocessed output used by -save-temps. */
248 cp_lexer_get_preprocessor_token (NULL, &first_token);
250 /* Tell cpplib we want CPP_PRAGMA tokens. */
251 cpp_get_options (parse_in)->defer_pragmas = true;
253 /* Tell c_lex not to merge string constants. */
254 c_lex_return_raw_strings = true;
256 c_common_no_more_pch ();
258 /* Allocate the memory. */
259 lexer = GGC_CNEW (cp_lexer);
261 #ifdef ENABLE_CHECKING
262 /* Initially we are not debugging. */
263 lexer->debugging_p = false;
264 #endif /* ENABLE_CHECKING */
265 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
266 CP_SAVED_TOKEN_STACK);
268 /* Create the buffer. */
269 alloc = CP_LEXER_BUFFER_SIZE;
270 buffer = ggc_alloc (alloc * sizeof (cp_token));
272 /* Put the first token in the buffer. */
277 /* Get the remaining tokens from the preprocessor. */
278 while (pos->type != CPP_EOF)
285 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
286 pos = buffer + space;
288 cp_lexer_get_preprocessor_token (lexer, pos);
290 lexer->buffer = buffer;
291 lexer->buffer_length = alloc - space;
292 lexer->last_token = pos;
293 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
295 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
296 direct calls to c_lex. Those callers all expect c_lex to do
297 string constant concatenation. */
298 c_lex_return_raw_strings = false;
300 gcc_assert (lexer->next_token->type != CPP_PURGED);
304 /* Create a new lexer whose token stream is primed with the tokens in
305 CACHE. When these tokens are exhausted, no new tokens will be read. */
308 cp_lexer_new_from_tokens (cp_token_cache *cache)
310 cp_token *first = cache->first;
311 cp_token *last = cache->last;
312 cp_lexer *lexer = GGC_CNEW (cp_lexer);
314 /* We do not own the buffer. */
315 lexer->buffer = NULL;
316 lexer->buffer_length = 0;
317 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
318 lexer->last_token = last;
320 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
321 CP_SAVED_TOKEN_STACK);
323 #ifdef ENABLE_CHECKING
324 /* Initially we are not debugging. */
325 lexer->debugging_p = false;
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
332 /* Frees all resources associated with LEXER. */
335 cp_lexer_destroy (cp_lexer *lexer)
338 ggc_free (lexer->buffer);
339 VEC_free (cp_token_position, heap, lexer->saved_tokens);
343 /* Returns nonzero if debugging information should be output. */
345 #ifdef ENABLE_CHECKING
348 cp_lexer_debugging_p (cp_lexer *lexer)
350 return lexer->debugging_p;
353 #endif /* ENABLE_CHECKING */
355 static inline cp_token_position
356 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
358 gcc_assert (!previous_p || lexer->next_token != &eof_token);
360 return lexer->next_token - previous_p;
363 static inline cp_token *
364 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
369 /* nonzero if we are presently saving tokens. */
372 cp_lexer_saving_tokens (const cp_lexer* lexer)
374 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
377 /* Store the next token from the preprocessor in *TOKEN. Return true
381 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
384 static int is_extern_c = 0;
386 /* Get a new token from the preprocessor. */
388 = c_lex_with_flags (&token->value, &token->location, &token->flags);
389 token->in_system_header = in_system_header;
391 /* On some systems, some header files are surrounded by an
392 implicit extern "C" block. Set a flag in the token if it
393 comes from such a header. */
394 is_extern_c += pending_lang_change;
395 pending_lang_change = 0;
396 token->implicit_extern_c = is_extern_c > 0;
398 /* Check to see if this token is a keyword. */
399 if (token->type == CPP_NAME
400 && C_IS_RESERVED_WORD (token->value))
402 /* Mark this token as a keyword. */
403 token->type = CPP_KEYWORD;
404 /* Record which keyword. */
405 token->keyword = C_RID_CODE (token->value);
406 /* Update the value. Some keywords are mapped to particular
407 entities, rather than simply having the value of the
408 corresponding IDENTIFIER_NODE. For example, `__const' is
409 mapped to `const'. */
410 token->value = ridpointers[token->keyword];
412 /* Handle Objective-C++ keywords. */
413 else if (token->type == CPP_AT_NAME)
415 token->type = CPP_KEYWORD;
416 switch (C_RID_CODE (token->value))
418 /* Map 'class' to '@class', 'private' to '@private', etc. */
419 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
420 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
421 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
422 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
423 case RID_THROW: token->keyword = RID_AT_THROW; break;
424 case RID_TRY: token->keyword = RID_AT_TRY; break;
425 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
426 default: token->keyword = C_RID_CODE (token->value);
430 token->keyword = RID_MAX;
433 /* Update the globals input_location and in_system_header from TOKEN. */
435 cp_lexer_set_source_position_from_token (cp_token *token)
437 if (token->type != CPP_EOF)
439 input_location = token->location;
440 in_system_header = token->in_system_header;
444 /* Return a pointer to the next token in the token stream, but do not
447 static inline cp_token *
448 cp_lexer_peek_token (cp_lexer *lexer)
450 if (cp_lexer_debugging_p (lexer))
452 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
453 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
454 putc ('\n', cp_lexer_debug_stream);
456 return lexer->next_token;
459 /* Return true if the next token has the indicated TYPE. */
462 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
464 return cp_lexer_peek_token (lexer)->type == type;
467 /* Return true if the next token does not have the indicated TYPE. */
470 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
472 return !cp_lexer_next_token_is (lexer, type);
475 /* Return true if the next token is the indicated KEYWORD. */
478 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
482 /* Peek at the next token. */
483 token = cp_lexer_peek_token (lexer);
484 /* Check to see if it is the indicated keyword. */
485 return token->keyword == keyword;
488 /* Return a pointer to the Nth token in the token stream. If N is 1,
489 then this is precisely equivalent to cp_lexer_peek_token (except
490 that it is not inline). One would like to disallow that case, but
491 there is one case (cp_parser_nth_token_starts_template_id) where
492 the caller passes a variable for N and it might be 1. */
495 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
499 /* N is 1-based, not zero-based. */
502 if (cp_lexer_debugging_p (lexer))
503 fprintf (cp_lexer_debug_stream,
504 "cp_lexer: peeking ahead %ld at token: ", (long)n);
507 token = lexer->next_token;
508 gcc_assert (!n || token != &eof_token);
512 if (token == lexer->last_token)
514 token = (cp_token *)&eof_token;
518 if (token->type != CPP_PURGED)
522 if (cp_lexer_debugging_p (lexer))
524 cp_lexer_print_token (cp_lexer_debug_stream, token);
525 putc ('\n', cp_lexer_debug_stream);
531 /* Return the next token, and advance the lexer's next_token pointer
532 to point to the next non-purged token. */
535 cp_lexer_consume_token (cp_lexer* lexer)
537 cp_token *token = lexer->next_token;
539 gcc_assert (token != &eof_token);
544 if (lexer->next_token == lexer->last_token)
546 lexer->next_token = (cp_token *)&eof_token;
551 while (lexer->next_token->type == CPP_PURGED);
553 cp_lexer_set_source_position_from_token (token);
555 /* Provide debugging output. */
556 if (cp_lexer_debugging_p (lexer))
558 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
559 cp_lexer_print_token (cp_lexer_debug_stream, token);
560 putc ('\n', cp_lexer_debug_stream);
566 /* Permanently remove the next token from the token stream, and
567 advance the next_token pointer to refer to the next non-purged
571 cp_lexer_purge_token (cp_lexer *lexer)
573 cp_token *tok = lexer->next_token;
575 gcc_assert (tok != &eof_token);
576 tok->type = CPP_PURGED;
577 tok->location = UNKNOWN_LOCATION;
578 tok->value = NULL_TREE;
579 tok->keyword = RID_MAX;
584 if (tok == lexer->last_token)
586 tok = (cp_token *)&eof_token;
590 while (tok->type == CPP_PURGED);
591 lexer->next_token = tok;
594 /* Permanently remove all tokens after TOK, up to, but not
595 including, the token that will be returned next by
596 cp_lexer_peek_token. */
599 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
601 cp_token *peek = lexer->next_token;
603 if (peek == &eof_token)
604 peek = lexer->last_token;
606 gcc_assert (tok < peek);
608 for ( tok += 1; tok != peek; tok += 1)
610 tok->type = CPP_PURGED;
611 tok->location = UNKNOWN_LOCATION;
612 tok->value = NULL_TREE;
613 tok->keyword = RID_MAX;
617 /* Consume and handle a pragma token. */
619 cp_lexer_handle_pragma (cp_lexer *lexer)
622 cp_token *token = cp_lexer_consume_token (lexer);
623 gcc_assert (token->type == CPP_PRAGMA);
624 gcc_assert (token->value);
626 s.len = TREE_STRING_LENGTH (token->value);
627 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
629 cpp_handle_deferred_pragma (parse_in, &s);
631 /* Clearing token->value here means that we will get an ICE if we
632 try to process this #pragma again (which should be impossible). */
636 /* Begin saving tokens. All tokens consumed after this point will be
640 cp_lexer_save_tokens (cp_lexer* lexer)
642 /* Provide debugging output. */
643 if (cp_lexer_debugging_p (lexer))
644 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
646 VEC_safe_push (cp_token_position, heap,
647 lexer->saved_tokens, lexer->next_token);
650 /* Commit to the portion of the token stream most recently saved. */
653 cp_lexer_commit_tokens (cp_lexer* lexer)
655 /* Provide debugging output. */
656 if (cp_lexer_debugging_p (lexer))
657 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
659 VEC_pop (cp_token_position, lexer->saved_tokens);
662 /* Return all tokens saved since the last call to cp_lexer_save_tokens
663 to the token stream. Stop saving tokens. */
666 cp_lexer_rollback_tokens (cp_lexer* lexer)
668 /* Provide debugging output. */
669 if (cp_lexer_debugging_p (lexer))
670 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
672 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
675 /* Print a representation of the TOKEN on the STREAM. */
677 #ifdef ENABLE_CHECKING
680 cp_lexer_print_token (FILE * stream, cp_token *token)
682 /* We don't use cpp_type2name here because the parser defines
683 a few tokens of its own. */
684 static const char *const token_names[] = {
685 /* cpplib-defined token types */
691 /* C++ parser token types - see "Manifest constants", above. */
694 "NESTED_NAME_SPECIFIER",
698 /* If we have a name for the token, print it out. Otherwise, we
699 simply give the numeric code. */
700 gcc_assert (token->type < ARRAY_SIZE(token_names));
701 fputs (token_names[token->type], stream);
703 /* For some tokens, print the associated data. */
707 /* Some keywords have a value that is not an IDENTIFIER_NODE.
708 For example, `struct' is mapped to an INTEGER_CST. */
709 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
711 /* else fall through */
713 fputs (IDENTIFIER_POINTER (token->value), stream);
719 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
727 /* Start emitting debugging information. */
730 cp_lexer_start_debugging (cp_lexer* lexer)
732 lexer->debugging_p = true;
735 /* Stop emitting debugging information. */
738 cp_lexer_stop_debugging (cp_lexer* lexer)
740 lexer->debugging_p = false;
743 #endif /* ENABLE_CHECKING */
745 /* Create a new cp_token_cache, representing a range of tokens. */
747 static cp_token_cache *
748 cp_token_cache_new (cp_token *first, cp_token *last)
750 cp_token_cache *cache = GGC_NEW (cp_token_cache);
751 cache->first = first;
757 /* Decl-specifiers. */
759 static void clear_decl_specs
760 (cp_decl_specifier_seq *);
762 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
765 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
767 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
772 /* Nothing other than the parser should be creating declarators;
773 declarators are a semi-syntactic representation of C++ entities.
774 Other parts of the front end that need to create entities (like
775 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
777 static cp_declarator *make_call_declarator
778 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
779 static cp_declarator *make_array_declarator
780 (cp_declarator *, tree);
781 static cp_declarator *make_pointer_declarator
782 (cp_cv_quals, cp_declarator *);
783 static cp_declarator *make_reference_declarator
784 (cp_cv_quals, cp_declarator *);
785 static cp_parameter_declarator *make_parameter_declarator
786 (cp_decl_specifier_seq *, cp_declarator *, tree);
787 static cp_declarator *make_ptrmem_declarator
788 (cp_cv_quals, tree, cp_declarator *);
790 cp_declarator *cp_error_declarator;
792 /* The obstack on which declarators and related data structures are
794 static struct obstack declarator_obstack;
796 /* Alloc BYTES from the declarator memory pool. */
799 alloc_declarator (size_t bytes)
801 return obstack_alloc (&declarator_obstack, bytes);
804 /* Allocate a declarator of the indicated KIND. Clear fields that are
805 common to all declarators. */
807 static cp_declarator *
808 make_declarator (cp_declarator_kind kind)
810 cp_declarator *declarator;
812 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
813 declarator->kind = kind;
814 declarator->attributes = NULL_TREE;
815 declarator->declarator = NULL;
820 /* Make a declarator for a generalized identifier. If non-NULL, the
821 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
822 just UNQUALIFIED_NAME. */
824 static cp_declarator *
825 make_id_declarator (tree qualifying_scope, tree unqualified_name)
827 cp_declarator *declarator;
829 /* It is valid to write:
831 class C { void f(); };
835 The standard is not clear about whether `typedef const C D' is
836 legal; as of 2002-09-15 the committee is considering that
837 question. EDG 3.0 allows that syntax. Therefore, we do as
839 if (qualifying_scope && TYPE_P (qualifying_scope))
840 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
842 declarator = make_declarator (cdk_id);
843 declarator->u.id.qualifying_scope = qualifying_scope;
844 declarator->u.id.unqualified_name = unqualified_name;
845 declarator->u.id.sfk = sfk_none;
850 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
851 of modifiers such as const or volatile to apply to the pointer
852 type, represented as identifiers. */
855 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
857 cp_declarator *declarator;
859 declarator = make_declarator (cdk_pointer);
860 declarator->declarator = target;
861 declarator->u.pointer.qualifiers = cv_qualifiers;
862 declarator->u.pointer.class_type = NULL_TREE;
867 /* Like make_pointer_declarator -- but for references. */
870 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
872 cp_declarator *declarator;
874 declarator = make_declarator (cdk_reference);
875 declarator->declarator = target;
876 declarator->u.pointer.qualifiers = cv_qualifiers;
877 declarator->u.pointer.class_type = NULL_TREE;
882 /* Like make_pointer_declarator -- but for a pointer to a non-static
883 member of CLASS_TYPE. */
886 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
887 cp_declarator *pointee)
889 cp_declarator *declarator;
891 declarator = make_declarator (cdk_ptrmem);
892 declarator->declarator = pointee;
893 declarator->u.pointer.qualifiers = cv_qualifiers;
894 declarator->u.pointer.class_type = class_type;
899 /* Make a declarator for the function given by TARGET, with the
900 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
901 "const"-qualified member function. The EXCEPTION_SPECIFICATION
902 indicates what exceptions can be thrown. */
905 make_call_declarator (cp_declarator *target,
906 cp_parameter_declarator *parms,
907 cp_cv_quals cv_qualifiers,
908 tree exception_specification)
910 cp_declarator *declarator;
912 declarator = make_declarator (cdk_function);
913 declarator->declarator = target;
914 declarator->u.function.parameters = parms;
915 declarator->u.function.qualifiers = cv_qualifiers;
916 declarator->u.function.exception_specification = exception_specification;
921 /* Make a declarator for an array of BOUNDS elements, each of which is
922 defined by ELEMENT. */
925 make_array_declarator (cp_declarator *element, tree bounds)
927 cp_declarator *declarator;
929 declarator = make_declarator (cdk_array);
930 declarator->declarator = element;
931 declarator->u.array.bounds = bounds;
936 cp_parameter_declarator *no_parameters;
938 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
939 DECLARATOR and DEFAULT_ARGUMENT. */
941 cp_parameter_declarator *
942 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
943 cp_declarator *declarator,
944 tree default_argument)
946 cp_parameter_declarator *parameter;
948 parameter = ((cp_parameter_declarator *)
949 alloc_declarator (sizeof (cp_parameter_declarator)));
950 parameter->next = NULL;
952 parameter->decl_specifiers = *decl_specifiers;
954 clear_decl_specs (¶meter->decl_specifiers);
955 parameter->declarator = declarator;
956 parameter->default_argument = default_argument;
957 parameter->ellipsis_p = false;
967 A cp_parser parses the token stream as specified by the C++
968 grammar. Its job is purely parsing, not semantic analysis. For
969 example, the parser breaks the token stream into declarators,
970 expressions, statements, and other similar syntactic constructs.
971 It does not check that the types of the expressions on either side
972 of an assignment-statement are compatible, or that a function is
973 not declared with a parameter of type `void'.
975 The parser invokes routines elsewhere in the compiler to perform
976 semantic analysis and to build up the abstract syntax tree for the
979 The parser (and the template instantiation code, which is, in a
980 way, a close relative of parsing) are the only parts of the
981 compiler that should be calling push_scope and pop_scope, or
982 related functions. The parser (and template instantiation code)
983 keeps track of what scope is presently active; everything else
984 should simply honor that. (The code that generates static
985 initializers may also need to set the scope, in order to check
986 access control correctly when emitting the initializers.)
991 The parser is of the standard recursive-descent variety. Upcoming
992 tokens in the token stream are examined in order to determine which
993 production to use when parsing a non-terminal. Some C++ constructs
994 require arbitrary look ahead to disambiguate. For example, it is
995 impossible, in the general case, to tell whether a statement is an
996 expression or declaration without scanning the entire statement.
997 Therefore, the parser is capable of "parsing tentatively." When the
998 parser is not sure what construct comes next, it enters this mode.
999 Then, while we attempt to parse the construct, the parser queues up
1000 error messages, rather than issuing them immediately, and saves the
1001 tokens it consumes. If the construct is parsed successfully, the
1002 parser "commits", i.e., it issues any queued error messages and
1003 the tokens that were being preserved are permanently discarded.
1004 If, however, the construct is not parsed successfully, the parser
1005 rolls back its state completely so that it can resume parsing using
1006 a different alternative.
1011 The performance of the parser could probably be improved substantially.
1012 We could often eliminate the need to parse tentatively by looking ahead
1013 a little bit. In some places, this approach might not entirely eliminate
1014 the need to parse tentatively, but it might still speed up the average
1017 /* Flags that are passed to some parsing functions. These values can
1018 be bitwise-ored together. */
1020 typedef enum cp_parser_flags
1023 CP_PARSER_FLAGS_NONE = 0x0,
1024 /* The construct is optional. If it is not present, then no error
1025 should be issued. */
1026 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1027 /* When parsing a type-specifier, do not allow user-defined types. */
1028 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1031 /* The different kinds of declarators we want to parse. */
1033 typedef enum cp_parser_declarator_kind
1035 /* We want an abstract declarator. */
1036 CP_PARSER_DECLARATOR_ABSTRACT,
1037 /* We want a named declarator. */
1038 CP_PARSER_DECLARATOR_NAMED,
1039 /* We don't mind, but the name must be an unqualified-id. */
1040 CP_PARSER_DECLARATOR_EITHER
1041 } cp_parser_declarator_kind;
1043 /* The precedence values used to parse binary expressions. The minimum value
1044 of PREC must be 1, because zero is reserved to quickly discriminate
1045 binary operators from other tokens. */
1050 PREC_LOGICAL_OR_EXPRESSION,
1051 PREC_LOGICAL_AND_EXPRESSION,
1052 PREC_INCLUSIVE_OR_EXPRESSION,
1053 PREC_EXCLUSIVE_OR_EXPRESSION,
1054 PREC_AND_EXPRESSION,
1055 PREC_EQUALITY_EXPRESSION,
1056 PREC_RELATIONAL_EXPRESSION,
1057 PREC_SHIFT_EXPRESSION,
1058 PREC_ADDITIVE_EXPRESSION,
1059 PREC_MULTIPLICATIVE_EXPRESSION,
1061 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1064 /* A mapping from a token type to a corresponding tree node type, with a
1065 precedence value. */
1067 typedef struct cp_parser_binary_operations_map_node
1069 /* The token type. */
1070 enum cpp_ttype token_type;
1071 /* The corresponding tree code. */
1072 enum tree_code tree_type;
1073 /* The precedence of this operator. */
1074 enum cp_parser_prec prec;
1075 } cp_parser_binary_operations_map_node;
1077 /* The status of a tentative parse. */
1079 typedef enum cp_parser_status_kind
1081 /* No errors have occurred. */
1082 CP_PARSER_STATUS_KIND_NO_ERROR,
1083 /* An error has occurred. */
1084 CP_PARSER_STATUS_KIND_ERROR,
1085 /* We are committed to this tentative parse, whether or not an error
1087 CP_PARSER_STATUS_KIND_COMMITTED
1088 } cp_parser_status_kind;
1090 typedef struct cp_parser_expression_stack_entry
1093 enum tree_code tree_type;
1095 } cp_parser_expression_stack_entry;
1097 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1098 entries because precedence levels on the stack are monotonically
1100 typedef struct cp_parser_expression_stack_entry
1101 cp_parser_expression_stack[NUM_PREC_VALUES];
1103 /* Context that is saved and restored when parsing tentatively. */
1104 typedef struct cp_parser_context GTY (())
1106 /* If this is a tentative parsing context, the status of the
1108 enum cp_parser_status_kind status;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1115 /* The next parsing context in the stack. */
1116 struct cp_parser_context *next;
1117 } cp_parser_context;
1121 /* Constructors and destructors. */
1123 static cp_parser_context *cp_parser_context_new
1124 (cp_parser_context *);
1126 /* Class variables. */
1128 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1130 /* The operator-precedence table used by cp_parser_binary_expression.
1131 Transformed into an associative array (binops_by_token) by
1134 static const cp_parser_binary_operations_map_node binops[] = {
1135 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1136 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1138 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1142 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1145 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1148 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1153 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1155 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1156 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1158 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1160 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1162 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1164 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1166 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1169 /* The same as binops, but initialized by cp_parser_new so that
1170 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1172 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1174 /* Constructors and destructors. */
1176 /* Construct a new context. The context below this one on the stack
1177 is given by NEXT. */
1179 static cp_parser_context *
1180 cp_parser_context_new (cp_parser_context* next)
1182 cp_parser_context *context;
1184 /* Allocate the storage. */
1185 if (cp_parser_context_free_list != NULL)
1187 /* Pull the first entry from the free list. */
1188 context = cp_parser_context_free_list;
1189 cp_parser_context_free_list = context->next;
1190 memset (context, 0, sizeof (*context));
1193 context = GGC_CNEW (cp_parser_context);
1195 /* No errors have occurred yet in this context. */
1196 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1197 /* If this is not the bottomost context, copy information that we
1198 need from the previous context. */
1201 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1202 expression, then we are parsing one in this context, too. */
1203 context->object_type = next->object_type;
1204 /* Thread the stack. */
1205 context->next = next;
1211 /* The cp_parser structure represents the C++ parser. */
1213 typedef struct cp_parser GTY(())
1215 /* The lexer from which we are obtaining tokens. */
1218 /* The scope in which names should be looked up. If NULL_TREE, then
1219 we look up names in the scope that is currently open in the
1220 source program. If non-NULL, this is either a TYPE or
1221 NAMESPACE_DECL for the scope in which we should look. It can
1222 also be ERROR_MARK, when we've parsed a bogus scope.
1224 This value is not cleared automatically after a name is looked
1225 up, so we must be careful to clear it before starting a new look
1226 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1227 will look up `Z' in the scope of `X', rather than the current
1228 scope.) Unfortunately, it is difficult to tell when name lookup
1229 is complete, because we sometimes peek at a token, look it up,
1230 and then decide not to consume it. */
1233 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1234 last lookup took place. OBJECT_SCOPE is used if an expression
1235 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1236 respectively. QUALIFYING_SCOPE is used for an expression of the
1237 form "X::Y"; it refers to X. */
1239 tree qualifying_scope;
1241 /* A stack of parsing contexts. All but the bottom entry on the
1242 stack will be tentative contexts.
1244 We parse tentatively in order to determine which construct is in
1245 use in some situations. For example, in order to determine
1246 whether a statement is an expression-statement or a
1247 declaration-statement we parse it tentatively as a
1248 declaration-statement. If that fails, we then reparse the same
1249 token stream as an expression-statement. */
1250 cp_parser_context *context;
1252 /* True if we are parsing GNU C++. If this flag is not set, then
1253 GNU extensions are not recognized. */
1254 bool allow_gnu_extensions_p;
1256 /* TRUE if the `>' token should be interpreted as the greater-than
1257 operator. FALSE if it is the end of a template-id or
1258 template-parameter-list. */
1259 bool greater_than_is_operator_p;
1261 /* TRUE if default arguments are allowed within a parameter list
1262 that starts at this point. FALSE if only a gnu extension makes
1263 them permissible. */
1264 bool default_arg_ok_p;
1266 /* TRUE if we are parsing an integral constant-expression. See
1267 [expr.const] for a precise definition. */
1268 bool integral_constant_expression_p;
1270 /* TRUE if we are parsing an integral constant-expression -- but a
1271 non-constant expression should be permitted as well. This flag
1272 is used when parsing an array bound so that GNU variable-length
1273 arrays are tolerated. */
1274 bool allow_non_integral_constant_expression_p;
1276 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1277 been seen that makes the expression non-constant. */
1278 bool non_integral_constant_expression_p;
1280 /* TRUE if local variable names and `this' are forbidden in the
1282 bool local_variables_forbidden_p;
1284 /* TRUE if the declaration we are parsing is part of a
1285 linkage-specification of the form `extern string-literal
1287 bool in_unbraced_linkage_specification_p;
1289 /* TRUE if we are presently parsing a declarator, after the
1290 direct-declarator. */
1291 bool in_declarator_p;
1293 /* TRUE if we are presently parsing a template-argument-list. */
1294 bool in_template_argument_list_p;
1296 /* TRUE if we are presently parsing the body of an
1297 iteration-statement. */
1298 bool in_iteration_statement_p;
1300 /* TRUE if we are presently parsing the body of a switch
1302 bool in_switch_statement_p;
1304 /* TRUE if we are parsing a type-id in an expression context. In
1305 such a situation, both "type (expr)" and "type (type)" are valid
1307 bool in_type_id_in_expr_p;
1309 /* TRUE if we are currently in a header file where declarations are
1310 implicitly extern "C". */
1311 bool implicit_extern_c;
1313 /* TRUE if strings in expressions should be translated to the execution
1315 bool translate_strings_p;
1317 /* If non-NULL, then we are parsing a construct where new type
1318 definitions are not permitted. The string stored here will be
1319 issued as an error message if a type is defined. */
1320 const char *type_definition_forbidden_message;
1322 /* A list of lists. The outer list is a stack, used for member
1323 functions of local classes. At each level there are two sub-list,
1324 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1325 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1326 TREE_VALUE's. The functions are chained in reverse declaration
1329 The TREE_PURPOSE sublist contains those functions with default
1330 arguments that need post processing, and the TREE_VALUE sublist
1331 contains those functions with definitions that need post
1334 These lists can only be processed once the outermost class being
1335 defined is complete. */
1336 tree unparsed_functions_queues;
1338 /* The number of classes whose definitions are currently in
1340 unsigned num_classes_being_defined;
1342 /* The number of template parameter lists that apply directly to the
1343 current declaration. */
1344 unsigned num_template_parameter_lists;
1347 /* The type of a function that parses some kind of expression. */
1348 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1352 /* Constructors and destructors. */
1354 static cp_parser *cp_parser_new
1357 /* Routines to parse various constructs.
1359 Those that return `tree' will return the error_mark_node (rather
1360 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1361 Sometimes, they will return an ordinary node if error-recovery was
1362 attempted, even though a parse error occurred. So, to check
1363 whether or not a parse error occurred, you should always use
1364 cp_parser_error_occurred. If the construct is optional (indicated
1365 either by an `_opt' in the name of the function that does the
1366 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1367 the construct is not present. */
1369 /* Lexical conventions [gram.lex] */
1371 static tree cp_parser_identifier
1373 static tree cp_parser_string_literal
1374 (cp_parser *, bool, bool);
1376 /* Basic concepts [gram.basic] */
1378 static bool cp_parser_translation_unit
1381 /* Expressions [gram.expr] */
1383 static tree cp_parser_primary_expression
1384 (cp_parser *, bool, cp_id_kind *, tree *);
1385 static tree cp_parser_id_expression
1386 (cp_parser *, bool, bool, bool *, bool);
1387 static tree cp_parser_unqualified_id
1388 (cp_parser *, bool, bool, bool);
1389 static tree cp_parser_nested_name_specifier_opt
1390 (cp_parser *, bool, bool, bool, bool);
1391 static tree cp_parser_nested_name_specifier
1392 (cp_parser *, bool, bool, bool, bool);
1393 static tree cp_parser_class_or_namespace_name
1394 (cp_parser *, bool, bool, bool, bool, bool);
1395 static tree cp_parser_postfix_expression
1396 (cp_parser *, bool, bool);
1397 static tree cp_parser_postfix_open_square_expression
1398 (cp_parser *, tree, bool);
1399 static tree cp_parser_postfix_dot_deref_expression
1400 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1401 static tree cp_parser_parenthesized_expression_list
1402 (cp_parser *, bool, bool, bool *);
1403 static void cp_parser_pseudo_destructor_name
1404 (cp_parser *, tree *, tree *);
1405 static tree cp_parser_unary_expression
1406 (cp_parser *, bool, bool);
1407 static enum tree_code cp_parser_unary_operator
1409 static tree cp_parser_new_expression
1411 static tree cp_parser_new_placement
1413 static tree cp_parser_new_type_id
1414 (cp_parser *, tree *);
1415 static cp_declarator *cp_parser_new_declarator_opt
1417 static cp_declarator *cp_parser_direct_new_declarator
1419 static tree cp_parser_new_initializer
1421 static tree cp_parser_delete_expression
1423 static tree cp_parser_cast_expression
1424 (cp_parser *, bool, bool);
1425 static tree cp_parser_binary_expression
1426 (cp_parser *, bool);
1427 static tree cp_parser_question_colon_clause
1428 (cp_parser *, tree);
1429 static tree cp_parser_assignment_expression
1430 (cp_parser *, bool);
1431 static enum tree_code cp_parser_assignment_operator_opt
1433 static tree cp_parser_expression
1434 (cp_parser *, bool);
1435 static tree cp_parser_constant_expression
1436 (cp_parser *, bool, bool *);
1437 static tree cp_parser_builtin_offsetof
1440 /* Statements [gram.stmt.stmt] */
1442 static void cp_parser_statement
1443 (cp_parser *, tree);
1444 static tree cp_parser_labeled_statement
1445 (cp_parser *, tree);
1446 static tree cp_parser_expression_statement
1447 (cp_parser *, tree);
1448 static tree cp_parser_compound_statement
1449 (cp_parser *, tree, bool);
1450 static void cp_parser_statement_seq_opt
1451 (cp_parser *, tree);
1452 static tree cp_parser_selection_statement
1454 static tree cp_parser_condition
1456 static tree cp_parser_iteration_statement
1458 static void cp_parser_for_init_statement
1460 static tree cp_parser_jump_statement
1462 static void cp_parser_declaration_statement
1465 static tree cp_parser_implicitly_scoped_statement
1467 static void cp_parser_already_scoped_statement
1470 /* Declarations [gram.dcl.dcl] */
1472 static void cp_parser_declaration_seq_opt
1474 static void cp_parser_declaration
1476 static void cp_parser_block_declaration
1477 (cp_parser *, bool);
1478 static void cp_parser_simple_declaration
1479 (cp_parser *, bool);
1480 static void cp_parser_decl_specifier_seq
1481 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1482 static tree cp_parser_storage_class_specifier_opt
1484 static tree cp_parser_function_specifier_opt
1485 (cp_parser *, cp_decl_specifier_seq *);
1486 static tree cp_parser_type_specifier
1487 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1489 static tree cp_parser_simple_type_specifier
1490 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1491 static tree cp_parser_type_name
1493 static tree cp_parser_elaborated_type_specifier
1494 (cp_parser *, bool, bool);
1495 static tree cp_parser_enum_specifier
1497 static void cp_parser_enumerator_list
1498 (cp_parser *, tree);
1499 static void cp_parser_enumerator_definition
1500 (cp_parser *, tree);
1501 static tree cp_parser_namespace_name
1503 static void cp_parser_namespace_definition
1505 static void cp_parser_namespace_body
1507 static tree cp_parser_qualified_namespace_specifier
1509 static void cp_parser_namespace_alias_definition
1511 static void cp_parser_using_declaration
1513 static void cp_parser_using_directive
1515 static void cp_parser_asm_definition
1517 static void cp_parser_linkage_specification
1520 /* Declarators [gram.dcl.decl] */
1522 static tree cp_parser_init_declarator
1523 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1524 static cp_declarator *cp_parser_declarator
1525 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1526 static cp_declarator *cp_parser_direct_declarator
1527 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1528 static enum tree_code cp_parser_ptr_operator
1529 (cp_parser *, tree *, cp_cv_quals *);
1530 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1532 static tree cp_parser_declarator_id
1534 static tree cp_parser_type_id
1536 static void cp_parser_type_specifier_seq
1537 (cp_parser *, bool, cp_decl_specifier_seq *);
1538 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1540 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1541 (cp_parser *, bool *);
1542 static cp_parameter_declarator *cp_parser_parameter_declaration
1543 (cp_parser *, bool, bool *);
1544 static void cp_parser_function_body
1546 static tree cp_parser_initializer
1547 (cp_parser *, bool *, bool *);
1548 static tree cp_parser_initializer_clause
1549 (cp_parser *, bool *);
1550 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1551 (cp_parser *, bool *);
1553 static bool cp_parser_ctor_initializer_opt_and_function_body
1556 /* Classes [gram.class] */
1558 static tree cp_parser_class_name
1559 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1560 static tree cp_parser_class_specifier
1562 static tree cp_parser_class_head
1563 (cp_parser *, bool *, tree *);
1564 static enum tag_types cp_parser_class_key
1566 static void cp_parser_member_specification_opt
1568 static void cp_parser_member_declaration
1570 static tree cp_parser_pure_specifier
1572 static tree cp_parser_constant_initializer
1575 /* Derived classes [gram.class.derived] */
1577 static tree cp_parser_base_clause
1579 static tree cp_parser_base_specifier
1582 /* Special member functions [gram.special] */
1584 static tree cp_parser_conversion_function_id
1586 static tree cp_parser_conversion_type_id
1588 static cp_declarator *cp_parser_conversion_declarator_opt
1590 static bool cp_parser_ctor_initializer_opt
1592 static void cp_parser_mem_initializer_list
1594 static tree cp_parser_mem_initializer
1596 static tree cp_parser_mem_initializer_id
1599 /* Overloading [gram.over] */
1601 static tree cp_parser_operator_function_id
1603 static tree cp_parser_operator
1606 /* Templates [gram.temp] */
1608 static void cp_parser_template_declaration
1609 (cp_parser *, bool);
1610 static tree cp_parser_template_parameter_list
1612 static tree cp_parser_template_parameter
1613 (cp_parser *, bool *);
1614 static tree cp_parser_type_parameter
1616 static tree cp_parser_template_id
1617 (cp_parser *, bool, bool, bool);
1618 static tree cp_parser_template_name
1619 (cp_parser *, bool, bool, bool, bool *);
1620 static tree cp_parser_template_argument_list
1622 static tree cp_parser_template_argument
1624 static void cp_parser_explicit_instantiation
1626 static void cp_parser_explicit_specialization
1629 /* Exception handling [gram.exception] */
1631 static tree cp_parser_try_block
1633 static bool cp_parser_function_try_block
1635 static void cp_parser_handler_seq
1637 static void cp_parser_handler
1639 static tree cp_parser_exception_declaration
1641 static tree cp_parser_throw_expression
1643 static tree cp_parser_exception_specification_opt
1645 static tree cp_parser_type_id_list
1648 /* GNU Extensions */
1650 static tree cp_parser_asm_specification_opt
1652 static tree cp_parser_asm_operand_list
1654 static tree cp_parser_asm_clobber_list
1656 static tree cp_parser_attributes_opt
1658 static tree cp_parser_attribute_list
1660 static bool cp_parser_extension_opt
1661 (cp_parser *, int *);
1662 static void cp_parser_label_declaration
1665 /* Objective-C++ Productions */
1667 static tree cp_parser_objc_message_receiver
1669 static tree cp_parser_objc_message_args
1671 static tree cp_parser_objc_message_expression
1673 static tree cp_parser_objc_encode_expression
1675 static tree cp_parser_objc_defs_expression
1677 static tree cp_parser_objc_protocol_expression
1679 static tree cp_parser_objc_selector_expression
1681 static tree cp_parser_objc_expression
1683 static bool cp_parser_objc_selector_p
1685 static tree cp_parser_objc_selector
1687 static tree cp_parser_objc_protocol_refs_opt
1689 static void cp_parser_objc_declaration
1691 static tree cp_parser_objc_statement
1694 /* Utility Routines */
1696 static tree cp_parser_lookup_name
1697 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1698 static tree cp_parser_lookup_name_simple
1699 (cp_parser *, tree);
1700 static tree cp_parser_maybe_treat_template_as_class
1702 static bool cp_parser_check_declarator_template_parameters
1703 (cp_parser *, cp_declarator *);
1704 static bool cp_parser_check_template_parameters
1705 (cp_parser *, unsigned);
1706 static tree cp_parser_simple_cast_expression
1708 static tree cp_parser_global_scope_opt
1709 (cp_parser *, bool);
1710 static bool cp_parser_constructor_declarator_p
1711 (cp_parser *, bool);
1712 static tree cp_parser_function_definition_from_specifiers_and_declarator
1713 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1714 static tree cp_parser_function_definition_after_declarator
1715 (cp_parser *, bool);
1716 static void cp_parser_template_declaration_after_export
1717 (cp_parser *, bool);
1718 static tree cp_parser_single_declaration
1719 (cp_parser *, bool, bool *);
1720 static tree cp_parser_functional_cast
1721 (cp_parser *, tree);
1722 static tree cp_parser_save_member_function_body
1723 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1724 static tree cp_parser_enclosed_template_argument_list
1726 static void cp_parser_save_default_args
1727 (cp_parser *, tree);
1728 static void cp_parser_late_parsing_for_member
1729 (cp_parser *, tree);
1730 static void cp_parser_late_parsing_default_args
1731 (cp_parser *, tree);
1732 static tree cp_parser_sizeof_operand
1733 (cp_parser *, enum rid);
1734 static bool cp_parser_declares_only_class_p
1736 static void cp_parser_set_storage_class
1737 (cp_decl_specifier_seq *, cp_storage_class);
1738 static void cp_parser_set_decl_spec_type
1739 (cp_decl_specifier_seq *, tree, bool);
1740 static bool cp_parser_friend_p
1741 (const cp_decl_specifier_seq *);
1742 static cp_token *cp_parser_require
1743 (cp_parser *, enum cpp_ttype, const char *);
1744 static cp_token *cp_parser_require_keyword
1745 (cp_parser *, enum rid, const char *);
1746 static bool cp_parser_token_starts_function_definition_p
1748 static bool cp_parser_next_token_starts_class_definition_p
1750 static bool cp_parser_next_token_ends_template_argument_p
1752 static bool cp_parser_nth_token_starts_template_argument_list_p
1753 (cp_parser *, size_t);
1754 static enum tag_types cp_parser_token_is_class_key
1756 static void cp_parser_check_class_key
1757 (enum tag_types, tree type);
1758 static void cp_parser_check_access_in_redeclaration
1760 static bool cp_parser_optional_template_keyword
1762 static void cp_parser_pre_parsed_nested_name_specifier
1764 static void cp_parser_cache_group
1765 (cp_parser *, enum cpp_ttype, unsigned);
1766 static void cp_parser_parse_tentatively
1768 static void cp_parser_commit_to_tentative_parse
1770 static void cp_parser_abort_tentative_parse
1772 static bool cp_parser_parse_definitely
1774 static inline bool cp_parser_parsing_tentatively
1776 static bool cp_parser_uncommitted_to_tentative_parse_p
1778 static void cp_parser_error
1779 (cp_parser *, const char *);
1780 static void cp_parser_name_lookup_error
1781 (cp_parser *, tree, tree, const char *);
1782 static bool cp_parser_simulate_error
1784 static void cp_parser_check_type_definition
1786 static void cp_parser_check_for_definition_in_return_type
1787 (cp_declarator *, tree);
1788 static void cp_parser_check_for_invalid_template_id
1789 (cp_parser *, tree);
1790 static bool cp_parser_non_integral_constant_expression
1791 (cp_parser *, const char *);
1792 static void cp_parser_diagnose_invalid_type_name
1793 (cp_parser *, tree, tree);
1794 static bool cp_parser_parse_and_diagnose_invalid_type_name
1796 static int cp_parser_skip_to_closing_parenthesis
1797 (cp_parser *, bool, bool, bool);
1798 static void cp_parser_skip_to_end_of_statement
1800 static void cp_parser_consume_semicolon_at_end_of_statement
1802 static void cp_parser_skip_to_end_of_block_or_statement
1804 static void cp_parser_skip_to_closing_brace
1806 static void cp_parser_skip_until_found
1807 (cp_parser *, enum cpp_ttype, const char *);
1808 static bool cp_parser_error_occurred
1810 static bool cp_parser_allow_gnu_extensions_p
1812 static bool cp_parser_is_string_literal
1814 static bool cp_parser_is_keyword
1815 (cp_token *, enum rid);
1816 static tree cp_parser_make_typename_type
1817 (cp_parser *, tree, tree);
1819 /* Returns nonzero if we are parsing tentatively. */
1822 cp_parser_parsing_tentatively (cp_parser* parser)
1824 return parser->context->next != NULL;
1827 /* Returns nonzero if TOKEN is a string literal. */
1830 cp_parser_is_string_literal (cp_token* token)
1832 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1835 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1838 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1840 return token->keyword == keyword;
1843 /* A minimum or maximum operator has been seen. As these are
1844 deprecated, issue a warning. */
1847 cp_parser_warn_min_max (void)
1849 if (warn_deprecated && !in_system_header)
1850 warning (0, "minimum/maximum operators are deprecated");
1853 /* If not parsing tentatively, issue a diagnostic of the form
1854 FILE:LINE: MESSAGE before TOKEN
1855 where TOKEN is the next token in the input stream. MESSAGE
1856 (specified by the caller) is usually of the form "expected
1860 cp_parser_error (cp_parser* parser, const char* message)
1862 if (!cp_parser_simulate_error (parser))
1864 cp_token *token = cp_lexer_peek_token (parser->lexer);
1865 /* This diagnostic makes more sense if it is tagged to the line
1866 of the token we just peeked at. */
1867 cp_lexer_set_source_position_from_token (token);
1868 if (token->type == CPP_PRAGMA)
1870 error ("%<#pragma%> is not allowed here");
1871 cp_lexer_purge_token (parser->lexer);
1874 c_parse_error (message,
1875 /* Because c_parser_error does not understand
1876 CPP_KEYWORD, keywords are treated like
1878 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1883 /* Issue an error about name-lookup failing. NAME is the
1884 IDENTIFIER_NODE DECL is the result of
1885 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1886 the thing that we hoped to find. */
1889 cp_parser_name_lookup_error (cp_parser* parser,
1892 const char* desired)
1894 /* If name lookup completely failed, tell the user that NAME was not
1896 if (decl == error_mark_node)
1898 if (parser->scope && parser->scope != global_namespace)
1899 error ("%<%D::%D%> has not been declared",
1900 parser->scope, name);
1901 else if (parser->scope == global_namespace)
1902 error ("%<::%D%> has not been declared", name);
1903 else if (parser->object_scope
1904 && !CLASS_TYPE_P (parser->object_scope))
1905 error ("request for member %qD in non-class type %qT",
1906 name, parser->object_scope);
1907 else if (parser->object_scope)
1908 error ("%<%T::%D%> has not been declared",
1909 parser->object_scope, name);
1911 error ("%qD has not been declared", name);
1913 else if (parser->scope && parser->scope != global_namespace)
1914 error ("%<%D::%D%> %s", parser->scope, name, desired);
1915 else if (parser->scope == global_namespace)
1916 error ("%<::%D%> %s", name, desired);
1918 error ("%qD %s", name, desired);
1921 /* If we are parsing tentatively, remember that an error has occurred
1922 during this tentative parse. Returns true if the error was
1923 simulated; false if a message should be issued by the caller. */
1926 cp_parser_simulate_error (cp_parser* parser)
1928 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1930 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1936 /* This function is called when a type is defined. If type
1937 definitions are forbidden at this point, an error message is
1941 cp_parser_check_type_definition (cp_parser* parser)
1943 /* If types are forbidden here, issue a message. */
1944 if (parser->type_definition_forbidden_message)
1945 /* Use `%s' to print the string in case there are any escape
1946 characters in the message. */
1947 error ("%s", parser->type_definition_forbidden_message);
1950 /* This function is called when the DECLARATOR is processed. The TYPE
1951 was a type defined in the decl-specifiers. If it is invalid to
1952 define a type in the decl-specifiers for DECLARATOR, an error is
1956 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1959 /* [dcl.fct] forbids type definitions in return types.
1960 Unfortunately, it's not easy to know whether or not we are
1961 processing a return type until after the fact. */
1963 && (declarator->kind == cdk_pointer
1964 || declarator->kind == cdk_reference
1965 || declarator->kind == cdk_ptrmem))
1966 declarator = declarator->declarator;
1968 && declarator->kind == cdk_function)
1970 error ("new types may not be defined in a return type");
1971 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1976 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1977 "<" in any valid C++ program. If the next token is indeed "<",
1978 issue a message warning the user about what appears to be an
1979 invalid attempt to form a template-id. */
1982 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1985 cp_token_position start = 0;
1987 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1990 error ("%qT is not a template", type);
1991 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1992 error ("%qE is not a template", type);
1994 error ("invalid template-id");
1995 /* Remember the location of the invalid "<". */
1996 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1997 start = cp_lexer_token_position (parser->lexer, true);
1998 /* Consume the "<". */
1999 cp_lexer_consume_token (parser->lexer);
2000 /* Parse the template arguments. */
2001 cp_parser_enclosed_template_argument_list (parser);
2002 /* Permanently remove the invalid template arguments so that
2003 this error message is not issued again. */
2005 cp_lexer_purge_tokens_after (parser->lexer, start);
2009 /* If parsing an integral constant-expression, issue an error message
2010 about the fact that THING appeared and return true. Otherwise,
2011 return false. In either case, set
2012 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2015 cp_parser_non_integral_constant_expression (cp_parser *parser,
2018 parser->non_integral_constant_expression_p = true;
2019 if (parser->integral_constant_expression_p)
2021 if (!parser->allow_non_integral_constant_expression_p)
2023 error ("%s cannot appear in a constant-expression", thing);
2030 /* Emit a diagnostic for an invalid type name. SCOPE is the
2031 qualifying scope (or NULL, if none) for ID. This function commits
2032 to the current active tentative parse, if any. (Otherwise, the
2033 problematic construct might be encountered again later, resulting
2034 in duplicate error messages.) */
2037 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2039 tree decl, old_scope;
2040 /* Try to lookup the identifier. */
2041 old_scope = parser->scope;
2042 parser->scope = scope;
2043 decl = cp_parser_lookup_name_simple (parser, id);
2044 parser->scope = old_scope;
2045 /* If the lookup found a template-name, it means that the user forgot
2046 to specify an argument list. Emit a useful error message. */
2047 if (TREE_CODE (decl) == TEMPLATE_DECL)
2048 error ("invalid use of template-name %qE without an argument list",
2050 else if (!parser->scope || parser->scope == error_mark_node)
2052 /* Issue an error message. */
2053 error ("%qE does not name a type", id);
2054 /* If we're in a template class, it's possible that the user was
2055 referring to a type from a base class. For example:
2057 template <typename T> struct A { typedef T X; };
2058 template <typename T> struct B : public A<T> { X x; };
2060 The user should have said "typename A<T>::X". */
2061 if (processing_template_decl && current_class_type
2062 && TYPE_BINFO (current_class_type))
2066 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2070 tree base_type = BINFO_TYPE (b);
2071 if (CLASS_TYPE_P (base_type)
2072 && dependent_type_p (base_type))
2075 /* Go from a particular instantiation of the
2076 template (which will have an empty TYPE_FIELDs),
2077 to the main version. */
2078 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2079 for (field = TYPE_FIELDS (base_type);
2081 field = TREE_CHAIN (field))
2082 if (TREE_CODE (field) == TYPE_DECL
2083 && DECL_NAME (field) == id)
2085 inform ("(perhaps %<typename %T::%E%> was intended)",
2086 BINFO_TYPE (b), id);
2095 /* Here we diagnose qualified-ids where the scope is actually correct,
2096 but the identifier does not resolve to a valid type name. */
2099 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2100 error ("%qE in namespace %qE does not name a type",
2102 else if (TYPE_P (parser->scope))
2103 error ("%qE in class %qT does not name a type", id, parser->scope);
2107 cp_parser_commit_to_tentative_parse (parser);
2110 /* Check for a common situation where a type-name should be present,
2111 but is not, and issue a sensible error message. Returns true if an
2112 invalid type-name was detected.
2114 The situation handled by this function are variable declarations of the
2115 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2116 Usually, `ID' should name a type, but if we got here it means that it
2117 does not. We try to emit the best possible error message depending on
2118 how exactly the id-expression looks like.
2122 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2126 cp_parser_parse_tentatively (parser);
2127 id = cp_parser_id_expression (parser,
2128 /*template_keyword_p=*/false,
2129 /*check_dependency_p=*/true,
2130 /*template_p=*/NULL,
2131 /*declarator_p=*/true);
2132 /* After the id-expression, there should be a plain identifier,
2133 otherwise this is not a simple variable declaration. Also, if
2134 the scope is dependent, we cannot do much. */
2135 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2136 || (parser->scope && TYPE_P (parser->scope)
2137 && dependent_type_p (parser->scope)))
2139 cp_parser_abort_tentative_parse (parser);
2142 if (!cp_parser_parse_definitely (parser)
2143 || TREE_CODE (id) != IDENTIFIER_NODE)
2146 /* Emit a diagnostic for the invalid type. */
2147 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2148 /* Skip to the end of the declaration; there's no point in
2149 trying to process it. */
2150 cp_parser_skip_to_end_of_block_or_statement (parser);
2154 /* Consume tokens up to, and including, the next non-nested closing `)'.
2155 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2156 are doing error recovery. Returns -1 if OR_COMMA is true and we
2157 found an unnested comma. */
2160 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2165 unsigned paren_depth = 0;
2166 unsigned brace_depth = 0;
2169 if (recovering && !or_comma
2170 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2177 /* If we've run out of tokens, then there is no closing `)'. */
2178 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2184 token = cp_lexer_peek_token (parser->lexer);
2186 /* This matches the processing in skip_to_end_of_statement. */
2187 if (token->type == CPP_SEMICOLON && !brace_depth)
2192 if (token->type == CPP_OPEN_BRACE)
2194 if (token->type == CPP_CLOSE_BRACE)
2202 if (recovering && or_comma && token->type == CPP_COMMA
2203 && !brace_depth && !paren_depth)
2211 /* If it is an `(', we have entered another level of nesting. */
2212 if (token->type == CPP_OPEN_PAREN)
2214 /* If it is a `)', then we might be done. */
2215 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2218 cp_lexer_consume_token (parser->lexer);
2226 /* Consume the token. */
2227 cp_lexer_consume_token (parser->lexer);
2233 /* Consume tokens until we reach the end of the current statement.
2234 Normally, that will be just before consuming a `;'. However, if a
2235 non-nested `}' comes first, then we stop before consuming that. */
2238 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2240 unsigned nesting_depth = 0;
2246 /* Peek at the next token. */
2247 token = cp_lexer_peek_token (parser->lexer);
2248 /* If we've run out of tokens, stop. */
2249 if (token->type == CPP_EOF)
2251 /* If the next token is a `;', we have reached the end of the
2253 if (token->type == CPP_SEMICOLON && !nesting_depth)
2255 /* If the next token is a non-nested `}', then we have reached
2256 the end of the current block. */
2257 if (token->type == CPP_CLOSE_BRACE)
2259 /* If this is a non-nested `}', stop before consuming it.
2260 That way, when confronted with something like:
2264 we stop before consuming the closing `}', even though we
2265 have not yet reached a `;'. */
2266 if (nesting_depth == 0)
2268 /* If it is the closing `}' for a block that we have
2269 scanned, stop -- but only after consuming the token.
2275 we will stop after the body of the erroneously declared
2276 function, but before consuming the following `typedef'
2278 if (--nesting_depth == 0)
2280 cp_lexer_consume_token (parser->lexer);
2284 /* If it the next token is a `{', then we are entering a new
2285 block. Consume the entire block. */
2286 else if (token->type == CPP_OPEN_BRACE)
2288 /* Consume the token. */
2289 cp_lexer_consume_token (parser->lexer);
2293 /* This function is called at the end of a statement or declaration.
2294 If the next token is a semicolon, it is consumed; otherwise, error
2295 recovery is attempted. */
2298 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2300 /* Look for the trailing `;'. */
2301 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2303 /* If there is additional (erroneous) input, skip to the end of
2305 cp_parser_skip_to_end_of_statement (parser);
2306 /* If the next token is now a `;', consume it. */
2307 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2308 cp_lexer_consume_token (parser->lexer);
2312 /* Skip tokens until we have consumed an entire block, or until we
2313 have consumed a non-nested `;'. */
2316 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2318 int nesting_depth = 0;
2320 while (nesting_depth >= 0)
2322 cp_token *token = cp_lexer_peek_token (parser->lexer);
2324 if (token->type == CPP_EOF)
2327 switch (token->type)
2330 /* If we've run out of tokens, stop. */
2335 /* Stop if this is an unnested ';'. */
2340 case CPP_CLOSE_BRACE:
2341 /* Stop if this is an unnested '}', or closes the outermost
2348 case CPP_OPEN_BRACE:
2357 /* Consume the token. */
2358 cp_lexer_consume_token (parser->lexer);
2363 /* Skip tokens until a non-nested closing curly brace is the next
2367 cp_parser_skip_to_closing_brace (cp_parser *parser)
2369 unsigned nesting_depth = 0;
2375 /* Peek at the next token. */
2376 token = cp_lexer_peek_token (parser->lexer);
2377 /* If we've run out of tokens, stop. */
2378 if (token->type == CPP_EOF)
2380 /* If the next token is a non-nested `}', then we have reached
2381 the end of the current block. */
2382 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2384 /* If it the next token is a `{', then we are entering a new
2385 block. Consume the entire block. */
2386 else if (token->type == CPP_OPEN_BRACE)
2388 /* Consume the token. */
2389 cp_lexer_consume_token (parser->lexer);
2393 /* This is a simple wrapper around make_typename_type. When the id is
2394 an unresolved identifier node, we can provide a superior diagnostic
2395 using cp_parser_diagnose_invalid_type_name. */
2398 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2401 if (TREE_CODE (id) == IDENTIFIER_NODE)
2403 result = make_typename_type (scope, id, typename_type,
2405 if (result == error_mark_node)
2406 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2409 return make_typename_type (scope, id, typename_type, tf_error);
2413 /* Create a new C++ parser. */
2416 cp_parser_new (void)
2422 /* cp_lexer_new_main is called before calling ggc_alloc because
2423 cp_lexer_new_main might load a PCH file. */
2424 lexer = cp_lexer_new_main ();
2426 /* Initialize the binops_by_token so that we can get the tree
2427 directly from the token. */
2428 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2429 binops_by_token[binops[i].token_type] = binops[i];
2431 parser = GGC_CNEW (cp_parser);
2432 parser->lexer = lexer;
2433 parser->context = cp_parser_context_new (NULL);
2435 /* For now, we always accept GNU extensions. */
2436 parser->allow_gnu_extensions_p = 1;
2438 /* The `>' token is a greater-than operator, not the end of a
2440 parser->greater_than_is_operator_p = true;
2442 parser->default_arg_ok_p = true;
2444 /* We are not parsing a constant-expression. */
2445 parser->integral_constant_expression_p = false;
2446 parser->allow_non_integral_constant_expression_p = false;
2447 parser->non_integral_constant_expression_p = false;
2449 /* Local variable names are not forbidden. */
2450 parser->local_variables_forbidden_p = false;
2452 /* We are not processing an `extern "C"' declaration. */
2453 parser->in_unbraced_linkage_specification_p = false;
2455 /* We are not processing a declarator. */
2456 parser->in_declarator_p = false;
2458 /* We are not processing a template-argument-list. */
2459 parser->in_template_argument_list_p = false;
2461 /* We are not in an iteration statement. */
2462 parser->in_iteration_statement_p = false;
2464 /* We are not in a switch statement. */
2465 parser->in_switch_statement_p = false;
2467 /* We are not parsing a type-id inside an expression. */
2468 parser->in_type_id_in_expr_p = false;
2470 /* Declarations aren't implicitly extern "C". */
2471 parser->implicit_extern_c = false;
2473 /* String literals should be translated to the execution character set. */
2474 parser->translate_strings_p = true;
2476 /* The unparsed function queue is empty. */
2477 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2479 /* There are no classes being defined. */
2480 parser->num_classes_being_defined = 0;
2482 /* No template parameters apply. */
2483 parser->num_template_parameter_lists = 0;
2488 /* Create a cp_lexer structure which will emit the tokens in CACHE
2489 and push it onto the parser's lexer stack. This is used for delayed
2490 parsing of in-class method bodies and default arguments, and should
2491 not be confused with tentative parsing. */
2493 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2495 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2496 lexer->next = parser->lexer;
2497 parser->lexer = lexer;
2499 /* Move the current source position to that of the first token in the
2501 cp_lexer_set_source_position_from_token (lexer->next_token);
2504 /* Pop the top lexer off the parser stack. This is never used for the
2505 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2507 cp_parser_pop_lexer (cp_parser *parser)
2509 cp_lexer *lexer = parser->lexer;
2510 parser->lexer = lexer->next;
2511 cp_lexer_destroy (lexer);
2513 /* Put the current source position back where it was before this
2514 lexer was pushed. */
2515 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2518 /* Lexical conventions [gram.lex] */
2520 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2524 cp_parser_identifier (cp_parser* parser)
2528 /* Look for the identifier. */
2529 token = cp_parser_require (parser, CPP_NAME, "identifier");
2530 /* Return the value. */
2531 return token ? token->value : error_mark_node;
2534 /* Parse a sequence of adjacent string constants. Returns a
2535 TREE_STRING representing the combined, nul-terminated string
2536 constant. If TRANSLATE is true, translate the string to the
2537 execution character set. If WIDE_OK is true, a wide string is
2540 C++98 [lex.string] says that if a narrow string literal token is
2541 adjacent to a wide string literal token, the behavior is undefined.
2542 However, C99 6.4.5p4 says that this results in a wide string literal.
2543 We follow C99 here, for consistency with the C front end.
2545 This code is largely lifted from lex_string() in c-lex.c.
2547 FUTURE: ObjC++ will need to handle @-strings here. */
2549 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2554 struct obstack str_ob;
2555 cpp_string str, istr, *strs;
2558 tok = cp_lexer_peek_token (parser->lexer);
2559 if (!cp_parser_is_string_literal (tok))
2561 cp_parser_error (parser, "expected string-literal");
2562 return error_mark_node;
2565 /* Try to avoid the overhead of creating and destroying an obstack
2566 for the common case of just one string. */
2567 if (!cp_parser_is_string_literal
2568 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2570 cp_lexer_consume_token (parser->lexer);
2572 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2573 str.len = TREE_STRING_LENGTH (tok->value);
2575 if (tok->type == CPP_WSTRING)
2582 gcc_obstack_init (&str_ob);
2587 cp_lexer_consume_token (parser->lexer);
2589 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2590 str.len = TREE_STRING_LENGTH (tok->value);
2591 if (tok->type == CPP_WSTRING)
2594 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2596 tok = cp_lexer_peek_token (parser->lexer);
2598 while (cp_parser_is_string_literal (tok));
2600 strs = (cpp_string *) obstack_finish (&str_ob);
2603 if (wide && !wide_ok)
2605 cp_parser_error (parser, "a wide string is invalid in this context");
2609 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2610 (parse_in, strs, count, &istr, wide))
2612 value = build_string (istr.len, (char *)istr.text);
2613 free ((void *)istr.text);
2615 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2616 value = fix_string_type (value);
2619 /* cpp_interpret_string has issued an error. */
2620 value = error_mark_node;
2623 obstack_free (&str_ob, 0);
2629 /* Basic concepts [gram.basic] */
2631 /* Parse a translation-unit.
2634 declaration-seq [opt]
2636 Returns TRUE if all went well. */
2639 cp_parser_translation_unit (cp_parser* parser)
2641 /* The address of the first non-permanent object on the declarator
2643 static void *declarator_obstack_base;
2647 /* Create the declarator obstack, if necessary. */
2648 if (!cp_error_declarator)
2650 gcc_obstack_init (&declarator_obstack);
2651 /* Create the error declarator. */
2652 cp_error_declarator = make_declarator (cdk_error);
2653 /* Create the empty parameter list. */
2654 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2655 /* Remember where the base of the declarator obstack lies. */
2656 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2661 cp_parser_declaration_seq_opt (parser);
2663 /* If there are no tokens left then all went well. */
2664 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2666 /* Get rid of the token array; we don't need it any more. */
2667 cp_lexer_destroy (parser->lexer);
2668 parser->lexer = NULL;
2670 /* This file might have been a context that's implicitly extern
2671 "C". If so, pop the lang context. (Only relevant for PCH.) */
2672 if (parser->implicit_extern_c)
2674 pop_lang_context ();
2675 parser->implicit_extern_c = false;
2679 finish_translation_unit ();
2686 cp_parser_error (parser, "expected declaration");
2692 /* Make sure the declarator obstack was fully cleaned up. */
2693 gcc_assert (obstack_next_free (&declarator_obstack)
2694 == declarator_obstack_base);
2696 /* All went well. */
2700 /* Expressions [gram.expr] */
2702 /* Parse a primary-expression.
2713 ( compound-statement )
2714 __builtin_va_arg ( assignment-expression , type-id )
2716 Objective-C++ Extension:
2724 CAST_P is true if this primary expression is the target of a cast.
2726 Returns a representation of the expression.
2728 *IDK indicates what kind of id-expression (if any) was present.
2730 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2731 used as the operand of a pointer-to-member. In that case,
2732 *QUALIFYING_CLASS gives the class that is used as the qualifying
2733 class in the pointer-to-member. */
2736 cp_parser_primary_expression (cp_parser *parser,
2739 tree *qualifying_class)
2743 /* Assume the primary expression is not an id-expression. */
2744 *idk = CP_ID_KIND_NONE;
2745 /* And that it cannot be used as pointer-to-member. */
2746 *qualifying_class = NULL_TREE;
2748 /* Peek at the next token. */
2749 token = cp_lexer_peek_token (parser->lexer);
2750 switch (token->type)
2761 token = cp_lexer_consume_token (parser->lexer);
2762 /* Floating-point literals are only allowed in an integral
2763 constant expression if they are cast to an integral or
2764 enumeration type. */
2765 if (TREE_CODE (token->value) == REAL_CST
2766 && parser->integral_constant_expression_p
2769 /* CAST_P will be set even in invalid code like "int(2.7 +
2770 ...)". Therefore, we have to check that the next token
2771 is sure to end the cast. */
2774 cp_token *next_token;
2776 next_token = cp_lexer_peek_token (parser->lexer);
2777 if (/* The comma at the end of an
2778 enumerator-definition. */
2779 next_token->type != CPP_COMMA
2780 /* The curly brace at the end of an enum-specifier. */
2781 && next_token->type != CPP_CLOSE_BRACE
2782 /* The end of a statement. */
2783 && next_token->type != CPP_SEMICOLON
2784 /* The end of the cast-expression. */
2785 && next_token->type != CPP_CLOSE_PAREN
2786 /* The end of an array bound. */
2787 && next_token->type != CPP_CLOSE_SQUARE)
2791 /* If we are within a cast, then the constraint that the
2792 cast is to an integral or enumeration type will be
2793 checked at that point. If we are not within a cast, then
2794 this code is invalid. */
2796 cp_parser_non_integral_constant_expression
2797 (parser, "floating-point literal");
2799 return token->value;
2803 /* ??? Should wide strings be allowed when parser->translate_strings_p
2804 is false (i.e. in attributes)? If not, we can kill the third
2805 argument to cp_parser_string_literal. */
2806 return cp_parser_string_literal (parser,
2807 parser->translate_strings_p,
2810 case CPP_OPEN_PAREN:
2813 bool saved_greater_than_is_operator_p;
2815 /* Consume the `('. */
2816 cp_lexer_consume_token (parser->lexer);
2817 /* Within a parenthesized expression, a `>' token is always
2818 the greater-than operator. */
2819 saved_greater_than_is_operator_p
2820 = parser->greater_than_is_operator_p;
2821 parser->greater_than_is_operator_p = true;
2822 /* If we see `( { ' then we are looking at the beginning of
2823 a GNU statement-expression. */
2824 if (cp_parser_allow_gnu_extensions_p (parser)
2825 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2827 /* Statement-expressions are not allowed by the standard. */
2829 pedwarn ("ISO C++ forbids braced-groups within expressions");
2831 /* And they're not allowed outside of a function-body; you
2832 cannot, for example, write:
2834 int i = ({ int j = 3; j + 1; });
2836 at class or namespace scope. */
2837 if (!at_function_scope_p ())
2838 error ("statement-expressions are allowed only inside functions");
2839 /* Start the statement-expression. */
2840 expr = begin_stmt_expr ();
2841 /* Parse the compound-statement. */
2842 cp_parser_compound_statement (parser, expr, false);
2844 expr = finish_stmt_expr (expr, false);
2848 /* Parse the parenthesized expression. */
2849 expr = cp_parser_expression (parser, cast_p);
2850 /* Let the front end know that this expression was
2851 enclosed in parentheses. This matters in case, for
2852 example, the expression is of the form `A::B', since
2853 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2855 finish_parenthesized_expr (expr);
2857 /* The `>' token might be the end of a template-id or
2858 template-parameter-list now. */
2859 parser->greater_than_is_operator_p
2860 = saved_greater_than_is_operator_p;
2861 /* Consume the `)'. */
2862 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2863 cp_parser_skip_to_end_of_statement (parser);
2869 switch (token->keyword)
2871 /* These two are the boolean literals. */
2873 cp_lexer_consume_token (parser->lexer);
2874 return boolean_true_node;
2876 cp_lexer_consume_token (parser->lexer);
2877 return boolean_false_node;
2879 /* The `__null' literal. */
2881 cp_lexer_consume_token (parser->lexer);
2884 /* Recognize the `this' keyword. */
2886 cp_lexer_consume_token (parser->lexer);
2887 if (parser->local_variables_forbidden_p)
2889 error ("%<this%> may not be used in this context");
2890 return error_mark_node;
2892 /* Pointers cannot appear in constant-expressions. */
2893 if (cp_parser_non_integral_constant_expression (parser,
2895 return error_mark_node;
2896 return finish_this_expr ();
2898 /* The `operator' keyword can be the beginning of an
2903 case RID_FUNCTION_NAME:
2904 case RID_PRETTY_FUNCTION_NAME:
2905 case RID_C99_FUNCTION_NAME:
2906 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2907 __func__ are the names of variables -- but they are
2908 treated specially. Therefore, they are handled here,
2909 rather than relying on the generic id-expression logic
2910 below. Grammatically, these names are id-expressions.
2912 Consume the token. */
2913 token = cp_lexer_consume_token (parser->lexer);
2914 /* Look up the name. */
2915 return finish_fname (token->value);
2922 /* The `__builtin_va_arg' construct is used to handle
2923 `va_arg'. Consume the `__builtin_va_arg' token. */
2924 cp_lexer_consume_token (parser->lexer);
2925 /* Look for the opening `('. */
2926 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2927 /* Now, parse the assignment-expression. */
2928 expression = cp_parser_assignment_expression (parser,
2930 /* Look for the `,'. */
2931 cp_parser_require (parser, CPP_COMMA, "`,'");
2932 /* Parse the type-id. */
2933 type = cp_parser_type_id (parser);
2934 /* Look for the closing `)'. */
2935 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2936 /* Using `va_arg' in a constant-expression is not
2938 if (cp_parser_non_integral_constant_expression (parser,
2940 return error_mark_node;
2941 return build_x_va_arg (expression, type);
2945 return cp_parser_builtin_offsetof (parser);
2947 /* Objective-C++ expressions. */
2949 case RID_AT_PROTOCOL:
2950 case RID_AT_SELECTOR:
2951 return cp_parser_objc_expression (parser);
2954 cp_parser_error (parser, "expected primary-expression");
2955 return error_mark_node;
2958 /* An id-expression can start with either an identifier, a
2959 `::' as the beginning of a qualified-id, or the "operator"
2963 case CPP_TEMPLATE_ID:
2964 case CPP_NESTED_NAME_SPECIFIER:
2968 const char *error_msg;
2971 /* Parse the id-expression. */
2973 = cp_parser_id_expression (parser,
2974 /*template_keyword_p=*/false,
2975 /*check_dependency_p=*/true,
2976 /*template_p=*/NULL,
2977 /*declarator_p=*/false);
2978 if (id_expression == error_mark_node)
2979 return error_mark_node;
2980 /* If we have a template-id, then no further lookup is
2981 required. If the template-id was for a template-class, we
2982 will sometimes have a TYPE_DECL at this point. */
2983 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2984 || TREE_CODE (id_expression) == TYPE_DECL)
2985 decl = id_expression;
2986 /* Look up the name. */
2991 decl = cp_parser_lookup_name (parser, id_expression,
2993 /*is_template=*/false,
2994 /*is_namespace=*/false,
2995 /*check_dependency=*/true,
2997 /* If the lookup was ambiguous, an error will already have
3000 return error_mark_node;
3002 /* In Objective-C++, an instance variable (ivar) may be preferred
3003 to whatever cp_parser_lookup_name() found. */
3004 decl = objc_lookup_ivar (decl, id_expression);
3006 /* If name lookup gives us a SCOPE_REF, then the
3007 qualifying scope was dependent. Just propagate the
3009 if (TREE_CODE (decl) == SCOPE_REF)
3011 if (TYPE_P (TREE_OPERAND (decl, 0)))
3012 *qualifying_class = TREE_OPERAND (decl, 0);
3015 /* Check to see if DECL is a local variable in a context
3016 where that is forbidden. */
3017 if (parser->local_variables_forbidden_p
3018 && local_variable_p (decl))
3020 /* It might be that we only found DECL because we are
3021 trying to be generous with pre-ISO scoping rules.
3022 For example, consider:
3026 for (int i = 0; i < 10; ++i) {}
3027 extern void f(int j = i);
3030 Here, name look up will originally find the out
3031 of scope `i'. We need to issue a warning message,
3032 but then use the global `i'. */
3033 decl = check_for_out_of_scope_variable (decl);
3034 if (local_variable_p (decl))
3036 error ("local variable %qD may not appear in this context",
3038 return error_mark_node;
3043 decl = finish_id_expression (id_expression, decl, parser->scope,
3044 idk, qualifying_class,
3045 parser->integral_constant_expression_p,
3046 parser->allow_non_integral_constant_expression_p,
3047 &parser->non_integral_constant_expression_p,
3050 cp_parser_error (parser, error_msg);
3054 /* Anything else is an error. */
3056 /* ...unless we have an Objective-C++ message or string literal, that is. */
3057 if (c_dialect_objc ()
3058 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3059 return cp_parser_objc_expression (parser);
3061 cp_parser_error (parser, "expected primary-expression");
3062 return error_mark_node;
3066 /* Parse an id-expression.
3073 :: [opt] nested-name-specifier template [opt] unqualified-id
3075 :: operator-function-id
3078 Return a representation of the unqualified portion of the
3079 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3080 a `::' or nested-name-specifier.
3082 Often, if the id-expression was a qualified-id, the caller will
3083 want to make a SCOPE_REF to represent the qualified-id. This
3084 function does not do this in order to avoid wastefully creating
3085 SCOPE_REFs when they are not required.
3087 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3090 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3091 uninstantiated templates.
3093 If *TEMPLATE_P is non-NULL, it is set to true iff the
3094 `template' keyword is used to explicitly indicate that the entity
3095 named is a template.
3097 If DECLARATOR_P is true, the id-expression is appearing as part of
3098 a declarator, rather than as part of an expression. */
3101 cp_parser_id_expression (cp_parser *parser,
3102 bool template_keyword_p,
3103 bool check_dependency_p,
3107 bool global_scope_p;
3108 bool nested_name_specifier_p;
3110 /* Assume the `template' keyword was not used. */
3112 *template_p = false;
3114 /* Look for the optional `::' operator. */
3116 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3118 /* Look for the optional nested-name-specifier. */
3119 nested_name_specifier_p
3120 = (cp_parser_nested_name_specifier_opt (parser,
3121 /*typename_keyword_p=*/false,
3126 /* If there is a nested-name-specifier, then we are looking at
3127 the first qualified-id production. */
3128 if (nested_name_specifier_p)
3131 tree saved_object_scope;
3132 tree saved_qualifying_scope;
3133 tree unqualified_id;
3136 /* See if the next token is the `template' keyword. */
3138 template_p = &is_template;
3139 *template_p = cp_parser_optional_template_keyword (parser);
3140 /* Name lookup we do during the processing of the
3141 unqualified-id might obliterate SCOPE. */
3142 saved_scope = parser->scope;
3143 saved_object_scope = parser->object_scope;
3144 saved_qualifying_scope = parser->qualifying_scope;
3145 /* Process the final unqualified-id. */
3146 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3149 /* Restore the SAVED_SCOPE for our caller. */
3150 parser->scope = saved_scope;
3151 parser->object_scope = saved_object_scope;
3152 parser->qualifying_scope = saved_qualifying_scope;
3154 return unqualified_id;
3156 /* Otherwise, if we are in global scope, then we are looking at one
3157 of the other qualified-id productions. */
3158 else if (global_scope_p)
3163 /* Peek at the next token. */
3164 token = cp_lexer_peek_token (parser->lexer);
3166 /* If it's an identifier, and the next token is not a "<", then
3167 we can avoid the template-id case. This is an optimization
3168 for this common case. */
3169 if (token->type == CPP_NAME
3170 && !cp_parser_nth_token_starts_template_argument_list_p
3172 return cp_parser_identifier (parser);
3174 cp_parser_parse_tentatively (parser);
3175 /* Try a template-id. */
3176 id = cp_parser_template_id (parser,
3177 /*template_keyword_p=*/false,
3178 /*check_dependency_p=*/true,
3180 /* If that worked, we're done. */
3181 if (cp_parser_parse_definitely (parser))
3184 /* Peek at the next token. (Changes in the token buffer may
3185 have invalidated the pointer obtained above.) */
3186 token = cp_lexer_peek_token (parser->lexer);
3188 switch (token->type)
3191 return cp_parser_identifier (parser);
3194 if (token->keyword == RID_OPERATOR)
3195 return cp_parser_operator_function_id (parser);
3199 cp_parser_error (parser, "expected id-expression");
3200 return error_mark_node;
3204 return cp_parser_unqualified_id (parser, template_keyword_p,
3205 /*check_dependency_p=*/true,
3209 /* Parse an unqualified-id.
3213 operator-function-id
3214 conversion-function-id
3218 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3219 keyword, in a construct like `A::template ...'.
3221 Returns a representation of unqualified-id. For the `identifier'
3222 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3223 production a BIT_NOT_EXPR is returned; the operand of the
3224 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3225 other productions, see the documentation accompanying the
3226 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3227 names are looked up in uninstantiated templates. If DECLARATOR_P
3228 is true, the unqualified-id is appearing as part of a declarator,
3229 rather than as part of an expression. */
3232 cp_parser_unqualified_id (cp_parser* parser,
3233 bool template_keyword_p,
3234 bool check_dependency_p,
3239 /* Peek at the next token. */
3240 token = cp_lexer_peek_token (parser->lexer);
3242 switch (token->type)
3248 /* We don't know yet whether or not this will be a
3250 cp_parser_parse_tentatively (parser);
3251 /* Try a template-id. */
3252 id = cp_parser_template_id (parser, template_keyword_p,
3255 /* If it worked, we're done. */
3256 if (cp_parser_parse_definitely (parser))
3258 /* Otherwise, it's an ordinary identifier. */
3259 return cp_parser_identifier (parser);
3262 case CPP_TEMPLATE_ID:
3263 return cp_parser_template_id (parser, template_keyword_p,
3270 tree qualifying_scope;
3275 /* Consume the `~' token. */
3276 cp_lexer_consume_token (parser->lexer);
3277 /* Parse the class-name. The standard, as written, seems to
3280 template <typename T> struct S { ~S (); };
3281 template <typename T> S<T>::~S() {}
3283 is invalid, since `~' must be followed by a class-name, but
3284 `S<T>' is dependent, and so not known to be a class.
3285 That's not right; we need to look in uninstantiated
3286 templates. A further complication arises from:
3288 template <typename T> void f(T t) {
3292 Here, it is not possible to look up `T' in the scope of `T'
3293 itself. We must look in both the current scope, and the
3294 scope of the containing complete expression.
3296 Yet another issue is:
3305 The standard does not seem to say that the `S' in `~S'
3306 should refer to the type `S' and not the data member
3309 /* DR 244 says that we look up the name after the "~" in the
3310 same scope as we looked up the qualifying name. That idea
3311 isn't fully worked out; it's more complicated than that. */
3312 scope = parser->scope;
3313 object_scope = parser->object_scope;
3314 qualifying_scope = parser->qualifying_scope;
3316 /* If the name is of the form "X::~X" it's OK. */
3317 if (scope && TYPE_P (scope)
3318 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3319 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3321 && (cp_lexer_peek_token (parser->lexer)->value
3322 == TYPE_IDENTIFIER (scope)))
3324 cp_lexer_consume_token (parser->lexer);
3325 return build_nt (BIT_NOT_EXPR, scope);
3328 /* If there was an explicit qualification (S::~T), first look
3329 in the scope given by the qualification (i.e., S). */
3331 type_decl = NULL_TREE;
3334 cp_parser_parse_tentatively (parser);
3335 type_decl = cp_parser_class_name (parser,
3336 /*typename_keyword_p=*/false,
3337 /*template_keyword_p=*/false,
3339 /*check_dependency=*/false,
3340 /*class_head_p=*/false,
3342 if (cp_parser_parse_definitely (parser))
3345 /* In "N::S::~S", look in "N" as well. */
3346 if (!done && scope && qualifying_scope)
3348 cp_parser_parse_tentatively (parser);
3349 parser->scope = qualifying_scope;
3350 parser->object_scope = NULL_TREE;
3351 parser->qualifying_scope = NULL_TREE;
3353 = cp_parser_class_name (parser,
3354 /*typename_keyword_p=*/false,
3355 /*template_keyword_p=*/false,
3357 /*check_dependency=*/false,
3358 /*class_head_p=*/false,
3360 if (cp_parser_parse_definitely (parser))
3363 /* In "p->S::~T", look in the scope given by "*p" as well. */
3364 else if (!done && object_scope)
3366 cp_parser_parse_tentatively (parser);
3367 parser->scope = object_scope;
3368 parser->object_scope = NULL_TREE;
3369 parser->qualifying_scope = NULL_TREE;
3371 = cp_parser_class_name (parser,
3372 /*typename_keyword_p=*/false,
3373 /*template_keyword_p=*/false,
3375 /*check_dependency=*/false,
3376 /*class_head_p=*/false,
3378 if (cp_parser_parse_definitely (parser))
3381 /* Look in the surrounding context. */
3384 parser->scope = NULL_TREE;
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,
3396 /* If an error occurred, assume that the name of the
3397 destructor is the same as the name of the qualifying
3398 class. That allows us to keep parsing after running
3399 into ill-formed destructor names. */
3400 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3401 return build_nt (BIT_NOT_EXPR, scope);
3402 else if (type_decl == error_mark_node)
3403 return error_mark_node;
3407 A typedef-name that names a class shall not be used as the
3408 identifier in the declarator for a destructor declaration. */
3410 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3411 && !DECL_SELF_REFERENCE_P (type_decl)
3412 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3413 error ("typedef-name %qD used as destructor declarator",
3416 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3420 if (token->keyword == RID_OPERATOR)
3424 /* This could be a template-id, so we try that first. */
3425 cp_parser_parse_tentatively (parser);
3426 /* Try a template-id. */
3427 id = cp_parser_template_id (parser, template_keyword_p,
3428 /*check_dependency_p=*/true,
3430 /* If that worked, we're done. */
3431 if (cp_parser_parse_definitely (parser))
3433 /* We still don't know whether we're looking at an
3434 operator-function-id or a conversion-function-id. */
3435 cp_parser_parse_tentatively (parser);
3436 /* Try an operator-function-id. */
3437 id = cp_parser_operator_function_id (parser);
3438 /* If that didn't work, try a conversion-function-id. */
3439 if (!cp_parser_parse_definitely (parser))
3440 id = cp_parser_conversion_function_id (parser);
3447 cp_parser_error (parser, "expected unqualified-id");
3448 return error_mark_node;
3452 /* Parse an (optional) nested-name-specifier.
3454 nested-name-specifier:
3455 class-or-namespace-name :: nested-name-specifier [opt]
3456 class-or-namespace-name :: template nested-name-specifier [opt]
3458 PARSER->SCOPE should be set appropriately before this function is
3459 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3460 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3463 Sets PARSER->SCOPE to the class (TYPE) or namespace
3464 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3465 it unchanged if there is no nested-name-specifier. Returns the new
3466 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3468 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3469 part of a declaration and/or decl-specifier. */
3472 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3473 bool typename_keyword_p,
3474 bool check_dependency_p,
3476 bool is_declaration)
3478 bool success = false;
3479 tree access_check = NULL_TREE;
3480 cp_token_position start = 0;
3483 /* If the next token corresponds to a nested name specifier, there
3484 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3485 false, it may have been true before, in which case something
3486 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3487 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3488 CHECK_DEPENDENCY_P is false, we have to fall through into the
3490 if (check_dependency_p
3491 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3493 cp_parser_pre_parsed_nested_name_specifier (parser);
3494 return parser->scope;
3497 /* Remember where the nested-name-specifier starts. */
3498 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3499 start = cp_lexer_token_position (parser->lexer, false);
3501 push_deferring_access_checks (dk_deferred);
3507 tree saved_qualifying_scope;
3508 bool template_keyword_p;
3510 /* Spot cases that cannot be the beginning of a
3511 nested-name-specifier. */
3512 token = cp_lexer_peek_token (parser->lexer);
3514 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3515 the already parsed nested-name-specifier. */
3516 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3518 /* Grab the nested-name-specifier and continue the loop. */
3519 cp_parser_pre_parsed_nested_name_specifier (parser);
3524 /* Spot cases that cannot be the beginning of a
3525 nested-name-specifier. On the second and subsequent times
3526 through the loop, we look for the `template' keyword. */
3527 if (success && token->keyword == RID_TEMPLATE)
3529 /* A template-id can start a nested-name-specifier. */
3530 else if (token->type == CPP_TEMPLATE_ID)
3534 /* If the next token is not an identifier, then it is
3535 definitely not a class-or-namespace-name. */
3536 if (token->type != CPP_NAME)
3538 /* If the following token is neither a `<' (to begin a
3539 template-id), nor a `::', then we are not looking at a
3540 nested-name-specifier. */
3541 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3542 if (token->type != CPP_SCOPE
3543 && !cp_parser_nth_token_starts_template_argument_list_p
3548 /* The nested-name-specifier is optional, so we parse
3550 cp_parser_parse_tentatively (parser);
3552 /* Look for the optional `template' keyword, if this isn't the
3553 first time through the loop. */
3555 template_keyword_p = cp_parser_optional_template_keyword (parser);
3557 template_keyword_p = false;
3559 /* Save the old scope since the name lookup we are about to do
3560 might destroy it. */
3561 old_scope = parser->scope;
3562 saved_qualifying_scope = parser->qualifying_scope;
3563 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3564 look up names in "X<T>::I" in order to determine that "Y" is
3565 a template. So, if we have a typename at this point, we make
3566 an effort to look through it. */
3568 && !typename_keyword_p
3570 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3571 parser->scope = resolve_typename_type (parser->scope,
3572 /*only_current_p=*/false);
3573 /* Parse the qualifying entity. */
3575 = cp_parser_class_or_namespace_name (parser,
3581 /* Look for the `::' token. */
3582 cp_parser_require (parser, CPP_SCOPE, "`::'");
3584 /* If we found what we wanted, we keep going; otherwise, we're
3586 if (!cp_parser_parse_definitely (parser))
3588 bool error_p = false;
3590 /* Restore the OLD_SCOPE since it was valid before the
3591 failed attempt at finding the last
3592 class-or-namespace-name. */
3593 parser->scope = old_scope;
3594 parser->qualifying_scope = saved_qualifying_scope;
3595 /* If the next token is an identifier, and the one after
3596 that is a `::', then any valid interpretation would have
3597 found a class-or-namespace-name. */
3598 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3599 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3601 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3604 token = cp_lexer_consume_token (parser->lexer);
3609 decl = cp_parser_lookup_name_simple (parser, token->value);
3610 if (TREE_CODE (decl) == TEMPLATE_DECL)
3611 error ("%qD used without template parameters", decl);
3613 cp_parser_name_lookup_error
3614 (parser, token->value, decl,
3615 "is not a class or namespace");
3616 parser->scope = NULL_TREE;
3618 /* Treat this as a successful nested-name-specifier
3623 If the name found is not a class-name (clause
3624 _class_) or namespace-name (_namespace.def_), the
3625 program is ill-formed. */
3628 cp_lexer_consume_token (parser->lexer);
3633 /* We've found one valid nested-name-specifier. */
3635 /* Make sure we look in the right scope the next time through
3637 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3638 ? TREE_TYPE (new_scope)
3640 /* If it is a class scope, try to complete it; we are about to
3641 be looking up names inside the class. */
3642 if (TYPE_P (parser->scope)
3643 /* Since checking types for dependency can be expensive,
3644 avoid doing it if the type is already complete. */
3645 && !COMPLETE_TYPE_P (parser->scope)
3646 /* Do not try to complete dependent types. */
3647 && !dependent_type_p (parser->scope))
3648 complete_type (parser->scope);
3651 /* Retrieve any deferred checks. Do not pop this access checks yet
3652 so the memory will not be reclaimed during token replacing below. */
3653 access_check = get_deferred_access_checks ();
3655 /* If parsing tentatively, replace the sequence of tokens that makes
3656 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3657 token. That way, should we re-parse the token stream, we will
3658 not have to repeat the effort required to do the parse, nor will
3659 we issue duplicate error messages. */
3660 if (success && start)
3662 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3664 /* Reset the contents of the START token. */
3665 token->type = CPP_NESTED_NAME_SPECIFIER;
3666 token->value = build_tree_list (access_check, parser->scope);
3667 TREE_TYPE (token->value) = parser->qualifying_scope;
3668 token->keyword = RID_MAX;
3670 /* Purge all subsequent tokens. */
3671 cp_lexer_purge_tokens_after (parser->lexer, start);
3674 pop_deferring_access_checks ();
3675 return success ? parser->scope : NULL_TREE;
3678 /* Parse a nested-name-specifier. See
3679 cp_parser_nested_name_specifier_opt for details. This function
3680 behaves identically, except that it will an issue an error if no
3681 nested-name-specifier is present. */
3684 cp_parser_nested_name_specifier (cp_parser *parser,
3685 bool typename_keyword_p,
3686 bool check_dependency_p,
3688 bool is_declaration)
3692 /* Look for the nested-name-specifier. */
3693 scope = cp_parser_nested_name_specifier_opt (parser,
3698 /* If it was not present, issue an error message. */
3701 cp_parser_error (parser, "expected nested-name-specifier");
3702 parser->scope = NULL_TREE;
3708 /* Parse a class-or-namespace-name.
3710 class-or-namespace-name:
3714 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3715 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3716 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3717 TYPE_P is TRUE iff the next name should be taken as a class-name,
3718 even the same name is declared to be another entity in the same
3721 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3722 specified by the class-or-namespace-name. If neither is found the
3723 ERROR_MARK_NODE is returned. */
3726 cp_parser_class_or_namespace_name (cp_parser *parser,
3727 bool typename_keyword_p,
3728 bool template_keyword_p,
3729 bool check_dependency_p,
3731 bool is_declaration)
3734 tree saved_qualifying_scope;
3735 tree saved_object_scope;
3739 /* Before we try to parse the class-name, we must save away the
3740 current PARSER->SCOPE since cp_parser_class_name will destroy
3742 saved_scope = parser->scope;
3743 saved_qualifying_scope = parser->qualifying_scope;
3744 saved_object_scope = parser->object_scope;
3745 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3746 there is no need to look for a namespace-name. */
3747 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3749 cp_parser_parse_tentatively (parser);
3750 scope = cp_parser_class_name (parser,
3753 type_p ? class_type : none_type,
3755 /*class_head_p=*/false,
3757 /* If that didn't work, try for a namespace-name. */
3758 if (!only_class_p && !cp_parser_parse_definitely (parser))
3760 /* Restore the saved scope. */
3761 parser->scope = saved_scope;
3762 parser->qualifying_scope = saved_qualifying_scope;
3763 parser->object_scope = saved_object_scope;
3764 /* If we are not looking at an identifier followed by the scope
3765 resolution operator, then this is not part of a
3766 nested-name-specifier. (Note that this function is only used
3767 to parse the components of a nested-name-specifier.) */
3768 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3769 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3770 return error_mark_node;
3771 scope = cp_parser_namespace_name (parser);
3777 /* Parse a postfix-expression.
3781 postfix-expression [ expression ]
3782 postfix-expression ( expression-list [opt] )
3783 simple-type-specifier ( expression-list [opt] )
3784 typename :: [opt] nested-name-specifier identifier
3785 ( expression-list [opt] )
3786 typename :: [opt] nested-name-specifier template [opt] template-id
3787 ( expression-list [opt] )
3788 postfix-expression . template [opt] id-expression
3789 postfix-expression -> template [opt] id-expression
3790 postfix-expression . pseudo-destructor-name
3791 postfix-expression -> pseudo-destructor-name
3792 postfix-expression ++
3793 postfix-expression --
3794 dynamic_cast < type-id > ( expression )
3795 static_cast < type-id > ( expression )
3796 reinterpret_cast < type-id > ( expression )
3797 const_cast < type-id > ( expression )
3798 typeid ( expression )
3804 ( type-id ) { initializer-list , [opt] }
3806 This extension is a GNU version of the C99 compound-literal
3807 construct. (The C99 grammar uses `type-name' instead of `type-id',
3808 but they are essentially the same concept.)
3810 If ADDRESS_P is true, the postfix expression is the operand of the
3811 `&' operator. CAST_P is true if this expression is the target of a
3814 Returns a representation of the expression. */
3817 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3821 cp_id_kind idk = CP_ID_KIND_NONE;
3822 tree postfix_expression = NULL_TREE;
3823 /* Non-NULL only if the current postfix-expression can be used to
3824 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3825 class used to qualify the member. */
3826 tree qualifying_class = NULL_TREE;
3828 /* Peek at the next token. */
3829 token = cp_lexer_peek_token (parser->lexer);
3830 /* Some of the productions are determined by keywords. */
3831 keyword = token->keyword;
3841 const char *saved_message;
3843 /* All of these can be handled in the same way from the point
3844 of view of parsing. Begin by consuming the token
3845 identifying the cast. */
3846 cp_lexer_consume_token (parser->lexer);
3848 /* New types cannot be defined in the cast. */
3849 saved_message = parser->type_definition_forbidden_message;
3850 parser->type_definition_forbidden_message
3851 = "types may not be defined in casts";
3853 /* Look for the opening `<'. */
3854 cp_parser_require (parser, CPP_LESS, "`<'");
3855 /* Parse the type to which we are casting. */
3856 type = cp_parser_type_id (parser);
3857 /* Look for the closing `>'. */
3858 cp_parser_require (parser, CPP_GREATER, "`>'");
3859 /* Restore the old message. */
3860 parser->type_definition_forbidden_message = saved_message;
3862 /* And the expression which is being cast. */
3863 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3864 expression = cp_parser_expression (parser, /*cast_p=*/true);
3865 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3867 /* Only type conversions to integral or enumeration types
3868 can be used in constant-expressions. */
3869 if (parser->integral_constant_expression_p
3870 && !dependent_type_p (type)
3871 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3872 && (cp_parser_non_integral_constant_expression
3874 "a cast to a type other than an integral or "
3875 "enumeration type")))
3876 return error_mark_node;
3882 = build_dynamic_cast (type, expression);
3886 = build_static_cast (type, expression);
3890 = build_reinterpret_cast (type, expression);
3894 = build_const_cast (type, expression);
3905 const char *saved_message;
3906 bool saved_in_type_id_in_expr_p;
3908 /* Consume the `typeid' token. */
3909 cp_lexer_consume_token (parser->lexer);
3910 /* Look for the `(' token. */
3911 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3912 /* Types cannot be defined in a `typeid' expression. */
3913 saved_message = parser->type_definition_forbidden_message;
3914 parser->type_definition_forbidden_message
3915 = "types may not be defined in a `typeid\' expression";
3916 /* We can't be sure yet whether we're looking at a type-id or an
3918 cp_parser_parse_tentatively (parser);
3919 /* Try a type-id first. */
3920 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3921 parser->in_type_id_in_expr_p = true;
3922 type = cp_parser_type_id (parser);
3923 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3924 /* Look for the `)' token. Otherwise, we can't be sure that
3925 we're not looking at an expression: consider `typeid (int
3926 (3))', for example. */
3927 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3928 /* If all went well, simply lookup the type-id. */
3929 if (cp_parser_parse_definitely (parser))
3930 postfix_expression = get_typeid (type);
3931 /* Otherwise, fall back to the expression variant. */
3936 /* Look for an expression. */
3937 expression = cp_parser_expression (parser, /*cast_p=*/false);
3938 /* Compute its typeid. */
3939 postfix_expression = build_typeid (expression);
3940 /* Look for the `)' token. */
3941 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3943 /* `typeid' may not appear in an integral constant expression. */
3944 if (cp_parser_non_integral_constant_expression(parser,
3945 "`typeid' operator"))
3946 return error_mark_node;
3947 /* Restore the saved message. */
3948 parser->type_definition_forbidden_message = saved_message;
3954 bool template_p = false;
3959 /* Consume the `typename' token. */
3960 cp_lexer_consume_token (parser->lexer);
3961 /* Look for the optional `::' operator. */
3962 cp_parser_global_scope_opt (parser,
3963 /*current_scope_valid_p=*/false);
3964 /* Look for the nested-name-specifier. In case of error here,
3965 consume the trailing id to avoid subsequent error messages
3967 scope = cp_parser_nested_name_specifier (parser,
3968 /*typename_keyword_p=*/true,
3969 /*check_dependency_p=*/true,
3971 /*is_declaration=*/true);
3973 /* Look for the optional `template' keyword. */
3974 template_p = cp_parser_optional_template_keyword (parser);
3975 /* We don't know whether we're looking at a template-id or an
3977 cp_parser_parse_tentatively (parser);
3978 /* Try a template-id. */
3979 id = cp_parser_template_id (parser, template_p,
3980 /*check_dependency_p=*/true,
3981 /*is_declaration=*/true);
3982 /* If that didn't work, try an identifier. */
3983 if (!cp_parser_parse_definitely (parser))
3984 id = cp_parser_identifier (parser);
3986 /* Don't process id if nested name specifier is invalid. */
3987 if (!scope || scope == error_mark_node)
3988 return error_mark_node;
3989 /* If we look up a template-id in a non-dependent qualifying
3990 scope, there's no need to create a dependent type. */
3991 else if (TREE_CODE (id) == TYPE_DECL
3992 && !dependent_type_p (parser->scope))
3993 type = TREE_TYPE (id);
3994 /* Create a TYPENAME_TYPE to represent the type to which the
3995 functional cast is being performed. */
3997 type = make_typename_type (parser->scope, id,
4001 postfix_expression = cp_parser_functional_cast (parser, type);
4009 /* If the next thing is a simple-type-specifier, we may be
4010 looking at a functional cast. We could also be looking at
4011 an id-expression. So, we try the functional cast, and if
4012 that doesn't work we fall back to the primary-expression. */
4013 cp_parser_parse_tentatively (parser);
4014 /* Look for the simple-type-specifier. */
4015 type = cp_parser_simple_type_specifier (parser,
4016 /*decl_specs=*/NULL,
4017 CP_PARSER_FLAGS_NONE);
4018 /* Parse the cast itself. */
4019 if (!cp_parser_error_occurred (parser))
4021 = cp_parser_functional_cast (parser, type);
4022 /* If that worked, we're done. */
4023 if (cp_parser_parse_definitely (parser))
4026 /* If the functional-cast didn't work out, try a
4027 compound-literal. */
4028 if (cp_parser_allow_gnu_extensions_p (parser)
4029 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4031 VEC(constructor_elt,gc) *initializer_list = NULL;
4032 bool saved_in_type_id_in_expr_p;
4034 cp_parser_parse_tentatively (parser);
4035 /* Consume the `('. */
4036 cp_lexer_consume_token (parser->lexer);
4037 /* Parse the type. */
4038 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4039 parser->in_type_id_in_expr_p = true;
4040 type = cp_parser_type_id (parser);
4041 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4042 /* Look for the `)'. */
4043 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4044 /* Look for the `{'. */
4045 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4046 /* If things aren't going well, there's no need to
4048 if (!cp_parser_error_occurred (parser))
4050 bool non_constant_p;
4051 /* Parse the initializer-list. */
4053 = cp_parser_initializer_list (parser, &non_constant_p);
4054 /* Allow a trailing `,'. */
4055 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4056 cp_lexer_consume_token (parser->lexer);
4057 /* Look for the final `}'. */
4058 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4060 /* If that worked, we're definitely looking at a
4061 compound-literal expression. */
4062 if (cp_parser_parse_definitely (parser))
4064 /* Warn the user that a compound literal is not
4065 allowed in standard C++. */
4067 pedwarn ("ISO C++ forbids compound-literals");
4068 /* Form the representation of the compound-literal. */
4070 = finish_compound_literal (type, initializer_list);
4075 /* It must be a primary-expression. */
4076 postfix_expression = cp_parser_primary_expression (parser,
4084 /* If we were avoiding committing to the processing of a
4085 qualified-id until we knew whether or not we had a
4086 pointer-to-member, we now know. */
4087 if (qualifying_class)
4091 /* Peek at the next token. */
4092 token = cp_lexer_peek_token (parser->lexer);
4093 done = (token->type != CPP_OPEN_SQUARE
4094 && token->type != CPP_OPEN_PAREN
4095 && token->type != CPP_DOT
4096 && token->type != CPP_DEREF
4097 && token->type != CPP_PLUS_PLUS
4098 && token->type != CPP_MINUS_MINUS);
4100 postfix_expression = finish_qualified_id_expr (qualifying_class,
4105 return postfix_expression;
4108 /* Keep looping until the postfix-expression is complete. */
4111 if (idk == CP_ID_KIND_UNQUALIFIED
4112 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4113 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4114 /* It is not a Koenig lookup function call. */
4116 = unqualified_name_lookup_error (postfix_expression);
4118 /* Peek at the next token. */
4119 token = cp_lexer_peek_token (parser->lexer);
4121 switch (token->type)
4123 case CPP_OPEN_SQUARE:
4125 = cp_parser_postfix_open_square_expression (parser,
4128 idk = CP_ID_KIND_NONE;
4131 case CPP_OPEN_PAREN:
4132 /* postfix-expression ( expression-list [opt] ) */
4135 bool is_builtin_constant_p;
4136 bool saved_integral_constant_expression_p = false;
4137 bool saved_non_integral_constant_expression_p = false;
4140 is_builtin_constant_p
4141 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4142 if (is_builtin_constant_p)
4144 /* The whole point of __builtin_constant_p is to allow
4145 non-constant expressions to appear as arguments. */
4146 saved_integral_constant_expression_p
4147 = parser->integral_constant_expression_p;
4148 saved_non_integral_constant_expression_p
4149 = parser->non_integral_constant_expression_p;
4150 parser->integral_constant_expression_p = false;
4152 args = (cp_parser_parenthesized_expression_list
4153 (parser, /*is_attribute_list=*/false,
4155 /*non_constant_p=*/NULL));
4156 if (is_builtin_constant_p)
4158 parser->integral_constant_expression_p
4159 = saved_integral_constant_expression_p;
4160 parser->non_integral_constant_expression_p
4161 = saved_non_integral_constant_expression_p;
4164 if (args == error_mark_node)
4166 postfix_expression = error_mark_node;
4170 /* Function calls are not permitted in
4171 constant-expressions. */
4172 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4173 && cp_parser_non_integral_constant_expression (parser,
4176 postfix_expression = error_mark_node;
4181 if (idk == CP_ID_KIND_UNQUALIFIED)
4183 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4189 = perform_koenig_lookup (postfix_expression, args);
4193 = unqualified_fn_lookup_error (postfix_expression);
4195 /* We do not perform argument-dependent lookup if
4196 normal lookup finds a non-function, in accordance
4197 with the expected resolution of DR 218. */
4198 else if (args && is_overloaded_fn (postfix_expression))
4200 tree fn = get_first_fn (postfix_expression);
4202 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4203 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4205 /* Only do argument dependent lookup if regular
4206 lookup does not find a set of member functions.
4207 [basic.lookup.koenig]/2a */
4208 if (!DECL_FUNCTION_MEMBER_P (fn))
4212 = perform_koenig_lookup (postfix_expression, args);
4217 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4219 tree instance = TREE_OPERAND (postfix_expression, 0);
4220 tree fn = TREE_OPERAND (postfix_expression, 1);
4222 if (processing_template_decl
4223 && (type_dependent_expression_p (instance)
4224 || (!BASELINK_P (fn)
4225 && TREE_CODE (fn) != FIELD_DECL)
4226 || type_dependent_expression_p (fn)
4227 || any_type_dependent_arguments_p (args)))
4230 = build_min_nt (CALL_EXPR, postfix_expression,
4235 if (BASELINK_P (fn))
4237 = (build_new_method_call
4238 (instance, fn, args, NULL_TREE,
4239 (idk == CP_ID_KIND_QUALIFIED
4240 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4243 = finish_call_expr (postfix_expression, args,
4244 /*disallow_virtual=*/false,
4245 /*koenig_p=*/false);
4247 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4248 || TREE_CODE (postfix_expression) == MEMBER_REF
4249 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4250 postfix_expression = (build_offset_ref_call_from_tree
4251 (postfix_expression, args));
4252 else if (idk == CP_ID_KIND_QUALIFIED)
4253 /* A call to a static class member, or a namespace-scope
4256 = finish_call_expr (postfix_expression, args,
4257 /*disallow_virtual=*/true,
4260 /* All other function calls. */
4262 = finish_call_expr (postfix_expression, args,
4263 /*disallow_virtual=*/false,
4266 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4267 idk = CP_ID_KIND_NONE;
4273 /* postfix-expression . template [opt] id-expression
4274 postfix-expression . pseudo-destructor-name
4275 postfix-expression -> template [opt] id-expression
4276 postfix-expression -> pseudo-destructor-name */
4278 /* Consume the `.' or `->' operator. */
4279 cp_lexer_consume_token (parser->lexer);
4282 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4288 /* postfix-expression ++ */
4289 /* Consume the `++' token. */
4290 cp_lexer_consume_token (parser->lexer);
4291 /* Generate a representation for the complete expression. */
4293 = finish_increment_expr (postfix_expression,
4294 POSTINCREMENT_EXPR);
4295 /* Increments may not appear in constant-expressions. */
4296 if (cp_parser_non_integral_constant_expression (parser,
4298 postfix_expression = error_mark_node;
4299 idk = CP_ID_KIND_NONE;
4302 case CPP_MINUS_MINUS:
4303 /* postfix-expression -- */
4304 /* Consume the `--' token. */
4305 cp_lexer_consume_token (parser->lexer);
4306 /* Generate a representation for the complete expression. */
4308 = finish_increment_expr (postfix_expression,
4309 POSTDECREMENT_EXPR);
4310 /* Decrements may not appear in constant-expressions. */
4311 if (cp_parser_non_integral_constant_expression (parser,
4313 postfix_expression = error_mark_node;
4314 idk = CP_ID_KIND_NONE;
4318 return postfix_expression;
4322 /* We should never get here. */
4324 return error_mark_node;
4327 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4328 by cp_parser_builtin_offsetof. We're looking for
4330 postfix-expression [ expression ]
4332 FOR_OFFSETOF is set if we're being called in that context, which
4333 changes how we deal with integer constant expressions. */
4336 cp_parser_postfix_open_square_expression (cp_parser *parser,
4337 tree postfix_expression,
4342 /* Consume the `[' token. */
4343 cp_lexer_consume_token (parser->lexer);
4345 /* Parse the index expression. */
4346 /* ??? For offsetof, there is a question of what to allow here. If
4347 offsetof is not being used in an integral constant expression context,
4348 then we *could* get the right answer by computing the value at runtime.
4349 If we are in an integral constant expression context, then we might
4350 could accept any constant expression; hard to say without analysis.
4351 Rather than open the barn door too wide right away, allow only integer
4352 constant expressions here. */
4354 index = cp_parser_constant_expression (parser, false, NULL);
4356 index = cp_parser_expression (parser, /*cast_p=*/false);
4358 /* Look for the closing `]'. */
4359 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4361 /* Build the ARRAY_REF. */
4362 postfix_expression = grok_array_decl (postfix_expression, index);
4364 /* When not doing offsetof, array references are not permitted in
4365 constant-expressions. */
4367 && (cp_parser_non_integral_constant_expression
4368 (parser, "an array reference")))
4369 postfix_expression = error_mark_node;
4371 return postfix_expression;
4374 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4375 by cp_parser_builtin_offsetof. We're looking for
4377 postfix-expression . template [opt] id-expression
4378 postfix-expression . pseudo-destructor-name
4379 postfix-expression -> template [opt] id-expression
4380 postfix-expression -> pseudo-destructor-name
4382 FOR_OFFSETOF is set if we're being called in that context. That sorta
4383 limits what of the above we'll actually accept, but nevermind.
4384 TOKEN_TYPE is the "." or "->" token, which will already have been
4385 removed from the stream. */
4388 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4389 enum cpp_ttype token_type,
4390 tree postfix_expression,
4391 bool for_offsetof, cp_id_kind *idk)
4396 bool pseudo_destructor_p;
4397 tree scope = NULL_TREE;
4399 /* If this is a `->' operator, dereference the pointer. */
4400 if (token_type == CPP_DEREF)
4401 postfix_expression = build_x_arrow (postfix_expression);
4402 /* Check to see whether or not the expression is type-dependent. */
4403 dependent_p = type_dependent_expression_p (postfix_expression);
4404 /* The identifier following the `->' or `.' is not qualified. */
4405 parser->scope = NULL_TREE;
4406 parser->qualifying_scope = NULL_TREE;
4407 parser->object_scope = NULL_TREE;
4408 *idk = CP_ID_KIND_NONE;
4409 /* Enter the scope corresponding to the type of the object
4410 given by the POSTFIX_EXPRESSION. */
4411 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4413 scope = TREE_TYPE (postfix_expression);
4414 /* According to the standard, no expression should ever have
4415 reference type. Unfortunately, we do not currently match
4416 the standard in this respect in that our internal representation
4417 of an expression may have reference type even when the standard
4418 says it does not. Therefore, we have to manually obtain the
4419 underlying type here. */
4420 scope = non_reference (scope);
4421 /* The type of the POSTFIX_EXPRESSION must be complete. */
4422 scope = complete_type_or_else (scope, NULL_TREE);
4423 /* Let the name lookup machinery know that we are processing a
4424 class member access expression. */
4425 parser->context->object_type = scope;
4426 /* If something went wrong, we want to be able to discern that case,
4427 as opposed to the case where there was no SCOPE due to the type
4428 of expression being dependent. */
4430 scope = error_mark_node;
4431 /* If the SCOPE was erroneous, make the various semantic analysis
4432 functions exit quickly -- and without issuing additional error
4434 if (scope == error_mark_node)
4435 postfix_expression = error_mark_node;
4438 /* Assume this expression is not a pseudo-destructor access. */
4439 pseudo_destructor_p = false;
4441 /* If the SCOPE is a scalar type, then, if this is a valid program,
4442 we must be looking at a pseudo-destructor-name. */
4443 if (scope && SCALAR_TYPE_P (scope))
4448 cp_parser_parse_tentatively (parser);
4449 /* Parse the pseudo-destructor-name. */
4451 cp_parser_pseudo_destructor_name (parser, &s, &type);
4452 if (cp_parser_parse_definitely (parser))
4454 pseudo_destructor_p = true;
4456 = finish_pseudo_destructor_expr (postfix_expression,
4457 s, TREE_TYPE (type));
4461 if (!pseudo_destructor_p)
4463 /* If the SCOPE is not a scalar type, we are looking at an
4464 ordinary class member access expression, rather than a
4465 pseudo-destructor-name. */
4466 template_p = cp_parser_optional_template_keyword (parser);
4467 /* Parse the id-expression. */
4468 name = cp_parser_id_expression (parser, template_p,
4469 /*check_dependency_p=*/true,
4470 /*template_p=*/NULL,
4471 /*declarator_p=*/false);
4472 /* In general, build a SCOPE_REF if the member name is qualified.
4473 However, if the name was not dependent and has already been
4474 resolved; there is no need to build the SCOPE_REF. For example;
4476 struct X { void f(); };
4477 template <typename T> void f(T* t) { t->X::f(); }
4479 Even though "t" is dependent, "X::f" is not and has been resolved
4480 to a BASELINK; there is no need to include scope information. */
4482 /* But we do need to remember that there was an explicit scope for
4483 virtual function calls. */
4485 *idk = CP_ID_KIND_QUALIFIED;
4487 /* If the name is a template-id that names a type, we will get a
4488 TYPE_DECL here. That is invalid code. */
4489 if (TREE_CODE (name) == TYPE_DECL)
4491 error ("invalid use of %qD", name);
4492 postfix_expression = error_mark_node;
4496 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4498 name = build_nt (SCOPE_REF, parser->scope, name);
4499 parser->scope = NULL_TREE;
4500 parser->qualifying_scope = NULL_TREE;
4501 parser->object_scope = NULL_TREE;
4503 if (scope && name && BASELINK_P (name))
4504 adjust_result_of_qualified_name_lookup
4505 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4507 = finish_class_member_access_expr (postfix_expression, name);
4511 /* We no longer need to look up names in the scope of the object on
4512 the left-hand side of the `.' or `->' operator. */
4513 parser->context->object_type = NULL_TREE;
4515 /* Outside of offsetof, these operators may not appear in
4516 constant-expressions. */
4518 && (cp_parser_non_integral_constant_expression
4519 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4520 postfix_expression = error_mark_node;
4522 return postfix_expression;
4525 /* Parse a parenthesized expression-list.
4528 assignment-expression
4529 expression-list, assignment-expression
4534 identifier, expression-list
4536 CAST_P is true if this expression is the target of a cast.
4538 Returns a TREE_LIST. The TREE_VALUE of each node is a
4539 representation of an assignment-expression. Note that a TREE_LIST
4540 is returned even if there is only a single expression in the list.
4541 error_mark_node is returned if the ( and or ) are
4542 missing. NULL_TREE is returned on no expressions. The parentheses
4543 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4544 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4545 indicates whether or not all of the expressions in the list were
4549 cp_parser_parenthesized_expression_list (cp_parser* parser,
4550 bool is_attribute_list,
4552 bool *non_constant_p)
4554 tree expression_list = NULL_TREE;
4555 bool fold_expr_p = is_attribute_list;
4556 tree identifier = NULL_TREE;
4558 /* Assume all the expressions will be constant. */
4560 *non_constant_p = false;
4562 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4563 return error_mark_node;
4565 /* Consume expressions until there are no more. */
4566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4571 /* At the beginning of attribute lists, check to see if the
4572 next token is an identifier. */
4573 if (is_attribute_list
4574 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4578 /* Consume the identifier. */
4579 token = cp_lexer_consume_token (parser->lexer);
4580 /* Save the identifier. */
4581 identifier = token->value;
4585 /* Parse the next assignment-expression. */
4588 bool expr_non_constant_p;
4589 expr = (cp_parser_constant_expression
4590 (parser, /*allow_non_constant_p=*/true,
4591 &expr_non_constant_p));
4592 if (expr_non_constant_p)
4593 *non_constant_p = true;
4596 expr = cp_parser_assignment_expression (parser, cast_p);
4599 expr = fold_non_dependent_expr (expr);
4601 /* Add it to the list. We add error_mark_node
4602 expressions to the list, so that we can still tell if
4603 the correct form for a parenthesized expression-list
4604 is found. That gives better errors. */
4605 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4607 if (expr == error_mark_node)
4611 /* After the first item, attribute lists look the same as
4612 expression lists. */
4613 is_attribute_list = false;
4616 /* If the next token isn't a `,', then we are done. */
4617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4620 /* Otherwise, consume the `,' and keep going. */
4621 cp_lexer_consume_token (parser->lexer);
4624 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4629 /* We try and resync to an unnested comma, as that will give the
4630 user better diagnostics. */
4631 ending = cp_parser_skip_to_closing_parenthesis (parser,
4632 /*recovering=*/true,
4634 /*consume_paren=*/true);
4638 return error_mark_node;
4641 /* We built up the list in reverse order so we must reverse it now. */
4642 expression_list = nreverse (expression_list);
4644 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4646 return expression_list;
4649 /* Parse a pseudo-destructor-name.
4651 pseudo-destructor-name:
4652 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4653 :: [opt] nested-name-specifier template template-id :: ~ type-name
4654 :: [opt] nested-name-specifier [opt] ~ type-name
4656 If either of the first two productions is used, sets *SCOPE to the
4657 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4658 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4659 or ERROR_MARK_NODE if the parse fails. */
4662 cp_parser_pseudo_destructor_name (cp_parser* parser,
4666 bool nested_name_specifier_p;
4668 /* Assume that things will not work out. */
4669 *type = error_mark_node;
4671 /* Look for the optional `::' operator. */
4672 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4673 /* Look for the optional nested-name-specifier. */
4674 nested_name_specifier_p
4675 = (cp_parser_nested_name_specifier_opt (parser,
4676 /*typename_keyword_p=*/false,
4677 /*check_dependency_p=*/true,
4679 /*is_declaration=*/true)
4681 /* Now, if we saw a nested-name-specifier, we might be doing the
4682 second production. */
4683 if (nested_name_specifier_p
4684 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4686 /* Consume the `template' keyword. */
4687 cp_lexer_consume_token (parser->lexer);
4688 /* Parse the template-id. */
4689 cp_parser_template_id (parser,
4690 /*template_keyword_p=*/true,
4691 /*check_dependency_p=*/false,
4692 /*is_declaration=*/true);
4693 /* Look for the `::' token. */
4694 cp_parser_require (parser, CPP_SCOPE, "`::'");
4696 /* If the next token is not a `~', then there might be some
4697 additional qualification. */
4698 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4700 /* Look for the type-name. */
4701 *scope = TREE_TYPE (cp_parser_type_name (parser));
4703 if (*scope == error_mark_node)
4706 /* If we don't have ::~, then something has gone wrong. Since
4707 the only caller of this function is looking for something
4708 after `.' or `->' after a scalar type, most likely the
4709 program is trying to get a member of a non-aggregate
4711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4712 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4714 cp_parser_error (parser, "request for member of non-aggregate type");
4718 /* Look for the `::' token. */
4719 cp_parser_require (parser, CPP_SCOPE, "`::'");
4724 /* Look for the `~'. */
4725 cp_parser_require (parser, CPP_COMPL, "`~'");
4726 /* Look for the type-name again. We are not responsible for
4727 checking that it matches the first type-name. */
4728 *type = cp_parser_type_name (parser);
4731 /* Parse a unary-expression.
4737 unary-operator cast-expression
4738 sizeof unary-expression
4746 __extension__ cast-expression
4747 __alignof__ unary-expression
4748 __alignof__ ( type-id )
4749 __real__ cast-expression
4750 __imag__ cast-expression
4753 ADDRESS_P is true iff the unary-expression is appearing as the
4754 operand of the `&' operator. CAST_P is true if this expression is
4755 the target of a cast.
4757 Returns a representation of the expression. */
4760 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4763 enum tree_code unary_operator;
4765 /* Peek at the next token. */
4766 token = cp_lexer_peek_token (parser->lexer);
4767 /* Some keywords give away the kind of expression. */
4768 if (token->type == CPP_KEYWORD)
4770 enum rid keyword = token->keyword;
4780 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4781 /* Consume the token. */
4782 cp_lexer_consume_token (parser->lexer);
4783 /* Parse the operand. */
4784 operand = cp_parser_sizeof_operand (parser, keyword);
4786 if (TYPE_P (operand))
4787 return cxx_sizeof_or_alignof_type (operand, op, true);
4789 return cxx_sizeof_or_alignof_expr (operand, op);
4793 return cp_parser_new_expression (parser);
4796 return cp_parser_delete_expression (parser);
4800 /* The saved value of the PEDANTIC flag. */
4804 /* Save away the PEDANTIC flag. */
4805 cp_parser_extension_opt (parser, &saved_pedantic);
4806 /* Parse the cast-expression. */
4807 expr = cp_parser_simple_cast_expression (parser);
4808 /* Restore the PEDANTIC flag. */
4809 pedantic = saved_pedantic;
4819 /* Consume the `__real__' or `__imag__' token. */
4820 cp_lexer_consume_token (parser->lexer);
4821 /* Parse the cast-expression. */
4822 expression = cp_parser_simple_cast_expression (parser);
4823 /* Create the complete representation. */
4824 return build_x_unary_op ((keyword == RID_REALPART
4825 ? REALPART_EXPR : IMAGPART_EXPR),
4835 /* Look for the `:: new' and `:: delete', which also signal the
4836 beginning of a new-expression, or delete-expression,
4837 respectively. If the next token is `::', then it might be one of
4839 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4843 /* See if the token after the `::' is one of the keywords in
4844 which we're interested. */
4845 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4846 /* If it's `new', we have a new-expression. */
4847 if (keyword == RID_NEW)
4848 return cp_parser_new_expression (parser);
4849 /* Similarly, for `delete'. */
4850 else if (keyword == RID_DELETE)
4851 return cp_parser_delete_expression (parser);
4854 /* Look for a unary operator. */
4855 unary_operator = cp_parser_unary_operator (token);
4856 /* The `++' and `--' operators can be handled similarly, even though
4857 they are not technically unary-operators in the grammar. */
4858 if (unary_operator == ERROR_MARK)
4860 if (token->type == CPP_PLUS_PLUS)
4861 unary_operator = PREINCREMENT_EXPR;
4862 else if (token->type == CPP_MINUS_MINUS)
4863 unary_operator = PREDECREMENT_EXPR;
4864 /* Handle the GNU address-of-label extension. */
4865 else if (cp_parser_allow_gnu_extensions_p (parser)
4866 && token->type == CPP_AND_AND)
4870 /* Consume the '&&' token. */
4871 cp_lexer_consume_token (parser->lexer);
4872 /* Look for the identifier. */
4873 identifier = cp_parser_identifier (parser);
4874 /* Create an expression representing the address. */
4875 return finish_label_address_expr (identifier);
4878 if (unary_operator != ERROR_MARK)
4880 tree cast_expression;
4881 tree expression = error_mark_node;
4882 const char *non_constant_p = NULL;
4884 /* Consume the operator token. */
4885 token = cp_lexer_consume_token (parser->lexer);
4886 /* Parse the cast-expression. */
4888 = cp_parser_cast_expression (parser,
4889 unary_operator == ADDR_EXPR,
4891 /* Now, build an appropriate representation. */
4892 switch (unary_operator)
4895 non_constant_p = "`*'";
4896 expression = build_x_indirect_ref (cast_expression, "unary *");
4900 non_constant_p = "`&'";
4903 expression = build_x_unary_op (unary_operator, cast_expression);
4906 case PREINCREMENT_EXPR:
4907 case PREDECREMENT_EXPR:
4908 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4911 case UNARY_PLUS_EXPR:
4913 case TRUTH_NOT_EXPR:
4914 expression = finish_unary_op_expr (unary_operator, cast_expression);
4922 && cp_parser_non_integral_constant_expression (parser,
4924 expression = error_mark_node;
4929 return cp_parser_postfix_expression (parser, address_p, cast_p);
4932 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4933 unary-operator, the corresponding tree code is returned. */
4935 static enum tree_code
4936 cp_parser_unary_operator (cp_token* token)
4938 switch (token->type)
4941 return INDIRECT_REF;
4947 return UNARY_PLUS_EXPR;
4953 return TRUTH_NOT_EXPR;
4956 return BIT_NOT_EXPR;
4963 /* Parse a new-expression.
4966 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4967 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4969 Returns a representation of the expression. */
4972 cp_parser_new_expression (cp_parser* parser)
4974 bool global_scope_p;
4980 /* Look for the optional `::' operator. */
4982 = (cp_parser_global_scope_opt (parser,
4983 /*current_scope_valid_p=*/false)
4985 /* Look for the `new' operator. */
4986 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4987 /* There's no easy way to tell a new-placement from the
4988 `( type-id )' construct. */
4989 cp_parser_parse_tentatively (parser);
4990 /* Look for a new-placement. */
4991 placement = cp_parser_new_placement (parser);
4992 /* If that didn't work out, there's no new-placement. */
4993 if (!cp_parser_parse_definitely (parser))
4994 placement = NULL_TREE;
4996 /* If the next token is a `(', then we have a parenthesized
4998 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5000 /* Consume the `('. */
5001 cp_lexer_consume_token (parser->lexer);
5002 /* Parse the type-id. */
5003 type = cp_parser_type_id (parser);
5004 /* Look for the closing `)'. */
5005 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5006 /* There should not be a direct-new-declarator in this production,
5007 but GCC used to allowed this, so we check and emit a sensible error
5008 message for this case. */
5009 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5011 error ("array bound forbidden after parenthesized type-id");
5012 inform ("try removing the parentheses around the type-id");
5013 cp_parser_direct_new_declarator (parser);
5017 /* Otherwise, there must be a new-type-id. */
5019 type = cp_parser_new_type_id (parser, &nelts);
5021 /* If the next token is a `(', then we have a new-initializer. */
5022 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5023 initializer = cp_parser_new_initializer (parser);
5025 initializer = NULL_TREE;
5027 /* A new-expression may not appear in an integral constant
5029 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5030 return error_mark_node;
5032 /* Create a representation of the new-expression. */
5033 return build_new (placement, type, nelts, initializer, global_scope_p);
5036 /* Parse a new-placement.
5041 Returns the same representation as for an expression-list. */
5044 cp_parser_new_placement (cp_parser* parser)
5046 tree expression_list;
5048 /* Parse the expression-list. */
5049 expression_list = (cp_parser_parenthesized_expression_list
5050 (parser, false, /*cast_p=*/false,
5051 /*non_constant_p=*/NULL));
5053 return expression_list;
5056 /* Parse a new-type-id.
5059 type-specifier-seq new-declarator [opt]
5061 Returns the TYPE allocated. If the new-type-id indicates an array
5062 type, *NELTS is set to the number of elements in the last array
5063 bound; the TYPE will not include the last array bound. */
5066 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5068 cp_decl_specifier_seq type_specifier_seq;
5069 cp_declarator *new_declarator;
5070 cp_declarator *declarator;
5071 cp_declarator *outer_declarator;
5072 const char *saved_message;
5075 /* The type-specifier sequence must not contain type definitions.
5076 (It cannot contain declarations of new types either, but if they
5077 are not definitions we will catch that because they are not
5079 saved_message = parser->type_definition_forbidden_message;
5080 parser->type_definition_forbidden_message
5081 = "types may not be defined in a new-type-id";
5082 /* Parse the type-specifier-seq. */
5083 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5084 &type_specifier_seq);
5085 /* Restore the old message. */
5086 parser->type_definition_forbidden_message = saved_message;
5087 /* Parse the new-declarator. */
5088 new_declarator = cp_parser_new_declarator_opt (parser);
5090 /* Determine the number of elements in the last array dimension, if
5093 /* Skip down to the last array dimension. */
5094 declarator = new_declarator;
5095 outer_declarator = NULL;
5096 while (declarator && (declarator->kind == cdk_pointer
5097 || declarator->kind == cdk_ptrmem))
5099 outer_declarator = declarator;
5100 declarator = declarator->declarator;
5103 && declarator->kind == cdk_array
5104 && declarator->declarator
5105 && declarator->declarator->kind == cdk_array)
5107 outer_declarator = declarator;
5108 declarator = declarator->declarator;
5111 if (declarator && declarator->kind == cdk_array)
5113 *nelts = declarator->u.array.bounds;
5114 if (*nelts == error_mark_node)
5115 *nelts = integer_one_node;
5117 if (outer_declarator)
5118 outer_declarator->declarator = declarator->declarator;
5120 new_declarator = NULL;
5123 type = groktypename (&type_specifier_seq, new_declarator);
5124 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5126 *nelts = array_type_nelts_top (type);
5127 type = TREE_TYPE (type);
5132 /* Parse an (optional) new-declarator.
5135 ptr-operator new-declarator [opt]
5136 direct-new-declarator
5138 Returns the declarator. */
5140 static cp_declarator *
5141 cp_parser_new_declarator_opt (cp_parser* parser)
5143 enum tree_code code;
5145 cp_cv_quals cv_quals;
5147 /* We don't know if there's a ptr-operator next, or not. */
5148 cp_parser_parse_tentatively (parser);
5149 /* Look for a ptr-operator. */
5150 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5151 /* If that worked, look for more new-declarators. */
5152 if (cp_parser_parse_definitely (parser))
5154 cp_declarator *declarator;
5156 /* Parse another optional declarator. */
5157 declarator = cp_parser_new_declarator_opt (parser);
5159 /* Create the representation of the declarator. */
5161 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5162 else if (code == INDIRECT_REF)
5163 declarator = make_pointer_declarator (cv_quals, declarator);
5165 declarator = make_reference_declarator (cv_quals, declarator);
5170 /* If the next token is a `[', there is a direct-new-declarator. */
5171 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5172 return cp_parser_direct_new_declarator (parser);
5177 /* Parse a direct-new-declarator.
5179 direct-new-declarator:
5181 direct-new-declarator [constant-expression]
5185 static cp_declarator *
5186 cp_parser_direct_new_declarator (cp_parser* parser)
5188 cp_declarator *declarator = NULL;
5194 /* Look for the opening `['. */
5195 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5196 /* The first expression is not required to be constant. */
5199 expression = cp_parser_expression (parser, /*cast_p=*/false);
5200 /* The standard requires that the expression have integral
5201 type. DR 74 adds enumeration types. We believe that the
5202 real intent is that these expressions be handled like the
5203 expression in a `switch' condition, which also allows
5204 classes with a single conversion to integral or
5205 enumeration type. */
5206 if (!processing_template_decl)
5209 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5214 error ("expression in new-declarator must have integral "
5215 "or enumeration type");
5216 expression = error_mark_node;
5220 /* But all the other expressions must be. */
5223 = cp_parser_constant_expression (parser,
5224 /*allow_non_constant=*/false,
5226 /* Look for the closing `]'. */
5227 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5229 /* Add this bound to the declarator. */
5230 declarator = make_array_declarator (declarator, expression);
5232 /* If the next token is not a `[', then there are no more
5234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5241 /* Parse a new-initializer.
5244 ( expression-list [opt] )
5246 Returns a representation of the expression-list. If there is no
5247 expression-list, VOID_ZERO_NODE is returned. */
5250 cp_parser_new_initializer (cp_parser* parser)
5252 tree expression_list;
5254 expression_list = (cp_parser_parenthesized_expression_list
5255 (parser, false, /*cast_p=*/false,
5256 /*non_constant_p=*/NULL));
5257 if (!expression_list)
5258 expression_list = void_zero_node;
5260 return expression_list;
5263 /* Parse a delete-expression.
5266 :: [opt] delete cast-expression
5267 :: [opt] delete [ ] cast-expression
5269 Returns a representation of the expression. */
5272 cp_parser_delete_expression (cp_parser* parser)
5274 bool global_scope_p;
5278 /* Look for the optional `::' operator. */
5280 = (cp_parser_global_scope_opt (parser,
5281 /*current_scope_valid_p=*/false)
5283 /* Look for the `delete' keyword. */
5284 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5285 /* See if the array syntax is in use. */
5286 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5288 /* Consume the `[' token. */
5289 cp_lexer_consume_token (parser->lexer);
5290 /* Look for the `]' token. */
5291 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5292 /* Remember that this is the `[]' construct. */
5298 /* Parse the cast-expression. */
5299 expression = cp_parser_simple_cast_expression (parser);
5301 /* A delete-expression may not appear in an integral constant
5303 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5304 return error_mark_node;
5306 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5309 /* Parse a cast-expression.
5313 ( type-id ) cast-expression
5315 ADDRESS_P is true iff the unary-expression is appearing as the
5316 operand of the `&' operator. CAST_P is true if this expression is
5317 the target of a cast.
5319 Returns a representation of the expression. */
5322 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5324 /* If it's a `(', then we might be looking at a cast. */
5325 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5327 tree type = NULL_TREE;
5328 tree expr = NULL_TREE;
5329 bool compound_literal_p;
5330 const char *saved_message;
5332 /* There's no way to know yet whether or not this is a cast.
5333 For example, `(int (3))' is a unary-expression, while `(int)
5334 3' is a cast. So, we resort to parsing tentatively. */
5335 cp_parser_parse_tentatively (parser);
5336 /* Types may not be defined in a cast. */
5337 saved_message = parser->type_definition_forbidden_message;
5338 parser->type_definition_forbidden_message
5339 = "types may not be defined in casts";
5340 /* Consume the `('. */
5341 cp_lexer_consume_token (parser->lexer);
5342 /* A very tricky bit is that `(struct S) { 3 }' is a
5343 compound-literal (which we permit in C++ as an extension).
5344 But, that construct is not a cast-expression -- it is a
5345 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5346 is legal; if the compound-literal were a cast-expression,
5347 you'd need an extra set of parentheses.) But, if we parse
5348 the type-id, and it happens to be a class-specifier, then we
5349 will commit to the parse at that point, because we cannot
5350 undo the action that is done when creating a new class. So,
5351 then we cannot back up and do a postfix-expression.
5353 Therefore, we scan ahead to the closing `)', and check to see
5354 if the token after the `)' is a `{'. If so, we are not
5355 looking at a cast-expression.
5357 Save tokens so that we can put them back. */
5358 cp_lexer_save_tokens (parser->lexer);
5359 /* Skip tokens until the next token is a closing parenthesis.
5360 If we find the closing `)', and the next token is a `{', then
5361 we are looking at a compound-literal. */
5363 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5364 /*consume_paren=*/true)
5365 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5366 /* Roll back the tokens we skipped. */
5367 cp_lexer_rollback_tokens (parser->lexer);
5368 /* If we were looking at a compound-literal, simulate an error
5369 so that the call to cp_parser_parse_definitely below will
5371 if (compound_literal_p)
5372 cp_parser_simulate_error (parser);
5375 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5376 parser->in_type_id_in_expr_p = true;
5377 /* Look for the type-id. */
5378 type = cp_parser_type_id (parser);
5379 /* Look for the closing `)'. */
5380 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5381 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5384 /* Restore the saved message. */
5385 parser->type_definition_forbidden_message = saved_message;
5387 /* If ok so far, parse the dependent expression. We cannot be
5388 sure it is a cast. Consider `(T ())'. It is a parenthesized
5389 ctor of T, but looks like a cast to function returning T
5390 without a dependent expression. */
5391 if (!cp_parser_error_occurred (parser))
5392 expr = cp_parser_cast_expression (parser,
5393 /*address_p=*/false,
5396 if (cp_parser_parse_definitely (parser))
5398 /* Warn about old-style casts, if so requested. */
5399 if (warn_old_style_cast
5400 && !in_system_header
5401 && !VOID_TYPE_P (type)
5402 && current_lang_name != lang_name_c)
5403 warning (0, "use of old-style cast");
5405 /* Only type conversions to integral or enumeration types
5406 can be used in constant-expressions. */
5407 if (parser->integral_constant_expression_p
5408 && !dependent_type_p (type)
5409 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5410 && (cp_parser_non_integral_constant_expression
5412 "a cast to a type other than an integral or "
5413 "enumeration type")))
5414 return error_mark_node;
5416 /* Perform the cast. */
5417 expr = build_c_cast (type, expr);
5422 /* If we get here, then it's not a cast, so it must be a
5423 unary-expression. */
5424 return cp_parser_unary_expression (parser, address_p, cast_p);
5427 /* Parse a binary expression of the general form:
5431 pm-expression .* cast-expression
5432 pm-expression ->* cast-expression
5434 multiplicative-expression:
5436 multiplicative-expression * pm-expression
5437 multiplicative-expression / pm-expression
5438 multiplicative-expression % pm-expression
5440 additive-expression:
5441 multiplicative-expression
5442 additive-expression + multiplicative-expression
5443 additive-expression - multiplicative-expression
5447 shift-expression << additive-expression
5448 shift-expression >> additive-expression
5450 relational-expression:
5452 relational-expression < shift-expression
5453 relational-expression > shift-expression
5454 relational-expression <= shift-expression
5455 relational-expression >= shift-expression
5459 relational-expression:
5460 relational-expression <? shift-expression
5461 relational-expression >? shift-expression
5463 equality-expression:
5464 relational-expression
5465 equality-expression == relational-expression
5466 equality-expression != relational-expression
5470 and-expression & equality-expression
5472 exclusive-or-expression:
5474 exclusive-or-expression ^ and-expression
5476 inclusive-or-expression:
5477 exclusive-or-expression
5478 inclusive-or-expression | exclusive-or-expression
5480 logical-and-expression:
5481 inclusive-or-expression
5482 logical-and-expression && inclusive-or-expression
5484 logical-or-expression:
5485 logical-and-expression
5486 logical-or-expression || logical-and-expression
5488 All these are implemented with a single function like:
5491 simple-cast-expression
5492 binary-expression <token> binary-expression
5494 CAST_P is true if this expression is the target of a cast.
5496 The binops_by_token map is used to get the tree codes for each <token> type.
5497 binary-expressions are associated according to a precedence table. */
5499 #define TOKEN_PRECEDENCE(token) \
5500 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5501 ? PREC_NOT_OPERATOR \
5502 : binops_by_token[token->type].prec)
5505 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5507 cp_parser_expression_stack stack;
5508 cp_parser_expression_stack_entry *sp = &stack[0];
5511 enum tree_code tree_type;
5512 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5515 /* Parse the first expression. */
5516 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5520 /* Get an operator token. */
5521 token = cp_lexer_peek_token (parser->lexer);
5522 if (token->type == CPP_MIN || token->type == CPP_MAX)
5523 cp_parser_warn_min_max ();
5525 new_prec = TOKEN_PRECEDENCE (token);
5527 /* Popping an entry off the stack means we completed a subexpression:
5528 - either we found a token which is not an operator (`>' where it is not
5529 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5530 will happen repeatedly;
5531 - or, we found an operator which has lower priority. This is the case
5532 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5534 if (new_prec <= prec)
5543 tree_type = binops_by_token[token->type].tree_type;
5545 /* We used the operator token. */
5546 cp_lexer_consume_token (parser->lexer);
5548 /* Extract another operand. It may be the RHS of this expression
5549 or the LHS of a new, higher priority expression. */
5550 rhs = cp_parser_simple_cast_expression (parser);
5552 /* Get another operator token. Look up its precedence to avoid
5553 building a useless (immediately popped) stack entry for common
5554 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5555 token = cp_lexer_peek_token (parser->lexer);
5556 lookahead_prec = TOKEN_PRECEDENCE (token);
5557 if (lookahead_prec > new_prec)
5559 /* ... and prepare to parse the RHS of the new, higher priority
5560 expression. Since precedence levels on the stack are
5561 monotonically increasing, we do not have to care about
5564 sp->tree_type = tree_type;
5569 new_prec = lookahead_prec;
5573 /* If the stack is not empty, we have parsed into LHS the right side
5574 (`4' in the example above) of an expression we had suspended.
5575 We can use the information on the stack to recover the LHS (`3')
5576 from the stack together with the tree code (`MULT_EXPR'), and
5577 the precedence of the higher level subexpression
5578 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5579 which will be used to actually build the additive expression. */
5582 tree_type = sp->tree_type;
5587 overloaded_p = false;
5588 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5590 /* If the binary operator required the use of an overloaded operator,
5591 then this expression cannot be an integral constant-expression.
5592 An overloaded operator can be used even if both operands are
5593 otherwise permissible in an integral constant-expression if at
5594 least one of the operands is of enumeration type. */
5597 && (cp_parser_non_integral_constant_expression
5598 (parser, "calls to overloaded operators")))
5599 return error_mark_node;
5606 /* Parse the `? expression : assignment-expression' part of a
5607 conditional-expression. The LOGICAL_OR_EXPR is the
5608 logical-or-expression that started the conditional-expression.
5609 Returns a representation of the entire conditional-expression.
5611 This routine is used by cp_parser_assignment_expression.
5613 ? expression : assignment-expression
5617 ? : assignment-expression */
5620 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5623 tree assignment_expr;
5625 /* Consume the `?' token. */
5626 cp_lexer_consume_token (parser->lexer);
5627 if (cp_parser_allow_gnu_extensions_p (parser)
5628 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5629 /* Implicit true clause. */
5632 /* Parse the expression. */
5633 expr = cp_parser_expression (parser, /*cast_p=*/false);
5635 /* The next token should be a `:'. */
5636 cp_parser_require (parser, CPP_COLON, "`:'");
5637 /* Parse the assignment-expression. */
5638 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5640 /* Build the conditional-expression. */
5641 return build_x_conditional_expr (logical_or_expr,
5646 /* Parse an assignment-expression.
5648 assignment-expression:
5649 conditional-expression
5650 logical-or-expression assignment-operator assignment_expression
5653 CAST_P is true if this expression is the target of a cast.
5655 Returns a representation for the expression. */
5658 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5662 /* If the next token is the `throw' keyword, then we're looking at
5663 a throw-expression. */
5664 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5665 expr = cp_parser_throw_expression (parser);
5666 /* Otherwise, it must be that we are looking at a
5667 logical-or-expression. */
5670 /* Parse the binary expressions (logical-or-expression). */
5671 expr = cp_parser_binary_expression (parser, cast_p);
5672 /* If the next token is a `?' then we're actually looking at a
5673 conditional-expression. */
5674 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5675 return cp_parser_question_colon_clause (parser, expr);
5678 enum tree_code assignment_operator;
5680 /* If it's an assignment-operator, we're using the second
5683 = cp_parser_assignment_operator_opt (parser);
5684 if (assignment_operator != ERROR_MARK)
5688 /* Parse the right-hand side of the assignment. */
5689 rhs = cp_parser_assignment_expression (parser, cast_p);
5690 /* An assignment may not appear in a
5691 constant-expression. */
5692 if (cp_parser_non_integral_constant_expression (parser,
5694 return error_mark_node;
5695 /* Build the assignment expression. */
5696 expr = build_x_modify_expr (expr,
5697 assignment_operator,
5706 /* Parse an (optional) assignment-operator.
5708 assignment-operator: one of
5709 = *= /= %= += -= >>= <<= &= ^= |=
5713 assignment-operator: one of
5716 If the next token is an assignment operator, the corresponding tree
5717 code is returned, and the token is consumed. For example, for
5718 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5719 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5720 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5721 operator, ERROR_MARK is returned. */
5723 static enum tree_code
5724 cp_parser_assignment_operator_opt (cp_parser* parser)
5729 /* Peek at the next toen. */
5730 token = cp_lexer_peek_token (parser->lexer);
5732 switch (token->type)
5743 op = TRUNC_DIV_EXPR;
5747 op = TRUNC_MOD_EXPR;
5780 cp_parser_warn_min_max ();
5785 cp_parser_warn_min_max ();
5789 /* Nothing else is an assignment operator. */
5793 /* If it was an assignment operator, consume it. */
5794 if (op != ERROR_MARK)
5795 cp_lexer_consume_token (parser->lexer);
5800 /* Parse an expression.
5803 assignment-expression
5804 expression , assignment-expression
5806 CAST_P is true if this expression is the target of a cast.
5808 Returns a representation of the expression. */
5811 cp_parser_expression (cp_parser* parser, bool cast_p)
5813 tree expression = NULL_TREE;
5817 tree assignment_expression;
5819 /* Parse the next assignment-expression. */
5820 assignment_expression
5821 = cp_parser_assignment_expression (parser, cast_p);
5822 /* If this is the first assignment-expression, we can just
5825 expression = assignment_expression;
5827 expression = build_x_compound_expr (expression,
5828 assignment_expression);
5829 /* If the next token is not a comma, then we are done with the
5831 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5833 /* Consume the `,'. */
5834 cp_lexer_consume_token (parser->lexer);
5835 /* A comma operator cannot appear in a constant-expression. */
5836 if (cp_parser_non_integral_constant_expression (parser,
5837 "a comma operator"))
5838 expression = error_mark_node;
5844 /* Parse a constant-expression.
5846 constant-expression:
5847 conditional-expression
5849 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5850 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5851 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5852 is false, NON_CONSTANT_P should be NULL. */
5855 cp_parser_constant_expression (cp_parser* parser,
5856 bool allow_non_constant_p,
5857 bool *non_constant_p)
5859 bool saved_integral_constant_expression_p;
5860 bool saved_allow_non_integral_constant_expression_p;
5861 bool saved_non_integral_constant_expression_p;
5864 /* It might seem that we could simply parse the
5865 conditional-expression, and then check to see if it were
5866 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5867 one that the compiler can figure out is constant, possibly after
5868 doing some simplifications or optimizations. The standard has a
5869 precise definition of constant-expression, and we must honor
5870 that, even though it is somewhat more restrictive.
5876 is not a legal declaration, because `(2, 3)' is not a
5877 constant-expression. The `,' operator is forbidden in a
5878 constant-expression. However, GCC's constant-folding machinery
5879 will fold this operation to an INTEGER_CST for `3'. */
5881 /* Save the old settings. */
5882 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5883 saved_allow_non_integral_constant_expression_p
5884 = parser->allow_non_integral_constant_expression_p;
5885 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5886 /* We are now parsing a constant-expression. */
5887 parser->integral_constant_expression_p = true;
5888 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5889 parser->non_integral_constant_expression_p = false;
5890 /* Although the grammar says "conditional-expression", we parse an
5891 "assignment-expression", which also permits "throw-expression"
5892 and the use of assignment operators. In the case that
5893 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5894 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5895 actually essential that we look for an assignment-expression.
5896 For example, cp_parser_initializer_clauses uses this function to
5897 determine whether a particular assignment-expression is in fact
5899 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5900 /* Restore the old settings. */
5901 parser->integral_constant_expression_p
5902 = saved_integral_constant_expression_p;
5903 parser->allow_non_integral_constant_expression_p
5904 = saved_allow_non_integral_constant_expression_p;
5905 if (allow_non_constant_p)
5906 *non_constant_p = parser->non_integral_constant_expression_p;
5907 else if (parser->non_integral_constant_expression_p)
5908 expression = error_mark_node;
5909 parser->non_integral_constant_expression_p
5910 = saved_non_integral_constant_expression_p;
5915 /* Parse __builtin_offsetof.
5917 offsetof-expression:
5918 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5920 offsetof-member-designator:
5922 | offsetof-member-designator "." id-expression
5923 | offsetof-member-designator "[" expression "]"
5927 cp_parser_builtin_offsetof (cp_parser *parser)
5929 int save_ice_p, save_non_ice_p;
5933 /* We're about to accept non-integral-constant things, but will
5934 definitely yield an integral constant expression. Save and
5935 restore these values around our local parsing. */
5936 save_ice_p = parser->integral_constant_expression_p;
5937 save_non_ice_p = parser->non_integral_constant_expression_p;
5939 /* Consume the "__builtin_offsetof" token. */
5940 cp_lexer_consume_token (parser->lexer);
5941 /* Consume the opening `('. */
5942 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5943 /* Parse the type-id. */
5944 type = cp_parser_type_id (parser);
5945 /* Look for the `,'. */
5946 cp_parser_require (parser, CPP_COMMA, "`,'");
5948 /* Build the (type *)null that begins the traditional offsetof macro. */
5949 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5951 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5952 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5956 cp_token *token = cp_lexer_peek_token (parser->lexer);
5957 switch (token->type)
5959 case CPP_OPEN_SQUARE:
5960 /* offsetof-member-designator "[" expression "]" */
5961 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5965 /* offsetof-member-designator "." identifier */
5966 cp_lexer_consume_token (parser->lexer);
5967 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5971 case CPP_CLOSE_PAREN:
5972 /* Consume the ")" token. */
5973 cp_lexer_consume_token (parser->lexer);
5977 /* Error. We know the following require will fail, but
5978 that gives the proper error message. */
5979 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5980 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5981 expr = error_mark_node;
5987 /* If we're processing a template, we can't finish the semantics yet.
5988 Otherwise we can fold the entire expression now. */
5989 if (processing_template_decl)
5990 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5992 expr = fold_offsetof (expr);
5995 parser->integral_constant_expression_p = save_ice_p;
5996 parser->non_integral_constant_expression_p = save_non_ice_p;
6001 /* Statements [gram.stmt.stmt] */
6003 /* Parse a statement.
6007 expression-statement
6012 declaration-statement
6016 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6020 location_t statement_location;
6022 /* There is no statement yet. */
6023 statement = NULL_TREE;
6024 /* Peek at the next token. */
6025 token = cp_lexer_peek_token (parser->lexer);
6026 /* Remember the location of the first token in the statement. */
6027 statement_location = token->location;
6028 /* If this is a keyword, then that will often determine what kind of
6029 statement we have. */
6030 if (token->type == CPP_KEYWORD)
6032 enum rid keyword = token->keyword;
6038 statement = cp_parser_labeled_statement (parser,
6044 statement = cp_parser_selection_statement (parser);
6050 statement = cp_parser_iteration_statement (parser);
6057 statement = cp_parser_jump_statement (parser);
6060 /* Objective-C++ exception-handling constructs. */
6063 case RID_AT_FINALLY:
6064 case RID_AT_SYNCHRONIZED:
6066 statement = cp_parser_objc_statement (parser);
6070 statement = cp_parser_try_block (parser);
6074 /* It might be a keyword like `int' that can start a
6075 declaration-statement. */
6079 else if (token->type == CPP_NAME)
6081 /* If the next token is a `:', then we are looking at a
6082 labeled-statement. */
6083 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6084 if (token->type == CPP_COLON)
6085 statement = cp_parser_labeled_statement (parser, in_statement_expr);
6087 /* Anything that starts with a `{' must be a compound-statement. */
6088 else if (token->type == CPP_OPEN_BRACE)
6089 statement = cp_parser_compound_statement (parser, NULL, false);
6090 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6091 a statement all its own. */
6092 else if (token->type == CPP_PRAGMA)
6094 cp_lexer_handle_pragma (parser->lexer);
6098 /* Everything else must be a declaration-statement or an
6099 expression-statement. Try for the declaration-statement
6100 first, unless we are looking at a `;', in which case we know that
6101 we have an expression-statement. */
6104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6106 cp_parser_parse_tentatively (parser);
6107 /* Try to parse the declaration-statement. */
6108 cp_parser_declaration_statement (parser);
6109 /* If that worked, we're done. */
6110 if (cp_parser_parse_definitely (parser))
6113 /* Look for an expression-statement instead. */
6114 statement = cp_parser_expression_statement (parser, in_statement_expr);
6117 /* Set the line number for the statement. */
6118 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6119 SET_EXPR_LOCATION (statement, statement_location);
6122 /* Parse a labeled-statement.
6125 identifier : statement
6126 case constant-expression : statement
6132 case constant-expression ... constant-expression : statement
6134 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6135 For an ordinary label, returns a LABEL_EXPR. */
6138 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6141 tree statement = error_mark_node;
6143 /* The next token should be an identifier. */
6144 token = cp_lexer_peek_token (parser->lexer);
6145 if (token->type != CPP_NAME
6146 && token->type != CPP_KEYWORD)
6148 cp_parser_error (parser, "expected labeled-statement");
6149 return error_mark_node;
6152 switch (token->keyword)
6159 /* Consume the `case' token. */
6160 cp_lexer_consume_token (parser->lexer);
6161 /* Parse the constant-expression. */
6162 expr = cp_parser_constant_expression (parser,
6163 /*allow_non_constant_p=*/false,
6166 ellipsis = cp_lexer_peek_token (parser->lexer);
6167 if (ellipsis->type == CPP_ELLIPSIS)
6169 /* Consume the `...' token. */
6170 cp_lexer_consume_token (parser->lexer);
6172 cp_parser_constant_expression (parser,
6173 /*allow_non_constant_p=*/false,
6175 /* We don't need to emit warnings here, as the common code
6176 will do this for us. */
6179 expr_hi = NULL_TREE;
6181 if (!parser->in_switch_statement_p)
6182 error ("case label %qE not within a switch statement", expr);
6184 statement = finish_case_label (expr, expr_hi);
6189 /* Consume the `default' token. */
6190 cp_lexer_consume_token (parser->lexer);
6191 if (!parser->in_switch_statement_p)
6192 error ("case label not within a switch statement");
6194 statement = finish_case_label (NULL_TREE, NULL_TREE);
6198 /* Anything else must be an ordinary label. */
6199 statement = finish_label_stmt (cp_parser_identifier (parser));
6203 /* Require the `:' token. */
6204 cp_parser_require (parser, CPP_COLON, "`:'");
6205 /* Parse the labeled statement. */
6206 cp_parser_statement (parser, in_statement_expr);
6208 /* Return the label, in the case of a `case' or `default' label. */
6212 /* Parse an expression-statement.
6214 expression-statement:
6217 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6218 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6219 indicates whether this expression-statement is part of an
6220 expression statement. */
6223 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6225 tree statement = NULL_TREE;
6227 /* If the next token is a ';', then there is no expression
6229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6230 statement = cp_parser_expression (parser, /*cast_p=*/false);
6232 /* Consume the final `;'. */
6233 cp_parser_consume_semicolon_at_end_of_statement (parser);
6235 if (in_statement_expr
6236 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6237 /* This is the final expression statement of a statement
6239 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6241 statement = finish_expr_stmt (statement);
6248 /* Parse a compound-statement.
6251 { statement-seq [opt] }
6253 Returns a tree representing the statement. */
6256 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6261 /* Consume the `{'. */
6262 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6263 return error_mark_node;
6264 /* Begin the compound-statement. */
6265 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6266 /* Parse an (optional) statement-seq. */
6267 cp_parser_statement_seq_opt (parser, in_statement_expr);
6268 /* Finish the compound-statement. */
6269 finish_compound_stmt (compound_stmt);
6270 /* Consume the `}'. */
6271 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6273 return compound_stmt;
6276 /* Parse an (optional) statement-seq.
6280 statement-seq [opt] statement */
6283 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6285 /* Scan statements until there aren't any more. */
6288 /* If we're looking at a `}', then we've run out of statements. */
6289 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6290 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6293 /* Parse the statement. */
6294 cp_parser_statement (parser, in_statement_expr);
6298 /* Parse a selection-statement.
6300 selection-statement:
6301 if ( condition ) statement
6302 if ( condition ) statement else statement
6303 switch ( condition ) statement
6305 Returns the new IF_STMT or SWITCH_STMT. */
6308 cp_parser_selection_statement (cp_parser* parser)
6313 /* Peek at the next token. */
6314 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6316 /* See what kind of keyword it is. */
6317 keyword = token->keyword;
6326 /* Look for the `('. */
6327 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6329 cp_parser_skip_to_end_of_statement (parser);
6330 return error_mark_node;
6333 /* Begin the selection-statement. */
6334 if (keyword == RID_IF)
6335 statement = begin_if_stmt ();
6337 statement = begin_switch_stmt ();
6339 /* Parse the condition. */
6340 condition = cp_parser_condition (parser);
6341 /* Look for the `)'. */
6342 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6343 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6344 /*consume_paren=*/true);
6346 if (keyword == RID_IF)
6348 /* Add the condition. */
6349 finish_if_stmt_cond (condition, statement);
6351 /* Parse the then-clause. */
6352 cp_parser_implicitly_scoped_statement (parser);
6353 finish_then_clause (statement);
6355 /* If the next token is `else', parse the else-clause. */
6356 if (cp_lexer_next_token_is_keyword (parser->lexer,
6359 /* Consume the `else' keyword. */
6360 cp_lexer_consume_token (parser->lexer);
6361 begin_else_clause (statement);
6362 /* Parse the else-clause. */
6363 cp_parser_implicitly_scoped_statement (parser);
6364 finish_else_clause (statement);
6367 /* Now we're all done with the if-statement. */
6368 finish_if_stmt (statement);
6372 bool in_switch_statement_p;
6374 /* Add the condition. */
6375 finish_switch_cond (condition, statement);
6377 /* Parse the body of the switch-statement. */
6378 in_switch_statement_p = parser->in_switch_statement_p;
6379 parser->in_switch_statement_p = true;
6380 cp_parser_implicitly_scoped_statement (parser);
6381 parser->in_switch_statement_p = in_switch_statement_p;
6383 /* Now we're all done with the switch-statement. */
6384 finish_switch_stmt (statement);
6392 cp_parser_error (parser, "expected selection-statement");
6393 return error_mark_node;
6397 /* Parse a condition.
6401 type-specifier-seq declarator = assignment-expression
6406 type-specifier-seq declarator asm-specification [opt]
6407 attributes [opt] = assignment-expression
6409 Returns the expression that should be tested. */
6412 cp_parser_condition (cp_parser* parser)
6414 cp_decl_specifier_seq type_specifiers;
6415 const char *saved_message;
6417 /* Try the declaration first. */
6418 cp_parser_parse_tentatively (parser);
6419 /* New types are not allowed in the type-specifier-seq for a
6421 saved_message = parser->type_definition_forbidden_message;
6422 parser->type_definition_forbidden_message
6423 = "types may not be defined in conditions";
6424 /* Parse the type-specifier-seq. */
6425 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6427 /* Restore the saved message. */
6428 parser->type_definition_forbidden_message = saved_message;
6429 /* If all is well, we might be looking at a declaration. */
6430 if (!cp_parser_error_occurred (parser))
6433 tree asm_specification;
6435 cp_declarator *declarator;
6436 tree initializer = NULL_TREE;
6438 /* Parse the declarator. */
6439 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6440 /*ctor_dtor_or_conv_p=*/NULL,
6441 /*parenthesized_p=*/NULL,
6442 /*member_p=*/false);
6443 /* Parse the attributes. */
6444 attributes = cp_parser_attributes_opt (parser);
6445 /* Parse the asm-specification. */
6446 asm_specification = cp_parser_asm_specification_opt (parser);
6447 /* If the next token is not an `=', then we might still be
6448 looking at an expression. For example:
6452 looks like a decl-specifier-seq and a declarator -- but then
6453 there is no `=', so this is an expression. */
6454 cp_parser_require (parser, CPP_EQ, "`='");
6455 /* If we did see an `=', then we are looking at a declaration
6457 if (cp_parser_parse_definitely (parser))
6461 /* Create the declaration. */
6462 decl = start_decl (declarator, &type_specifiers,
6463 /*initialized_p=*/true,
6464 attributes, /*prefix_attributes=*/NULL_TREE,
6466 /* Parse the assignment-expression. */
6467 initializer = cp_parser_assignment_expression (parser,
6470 /* Process the initializer. */
6471 cp_finish_decl (decl,
6474 LOOKUP_ONLYCONVERTING);
6477 pop_scope (pushed_scope);
6479 return convert_from_reference (decl);
6482 /* If we didn't even get past the declarator successfully, we are
6483 definitely not looking at a declaration. */
6485 cp_parser_abort_tentative_parse (parser);
6487 /* Otherwise, we are looking at an expression. */
6488 return cp_parser_expression (parser, /*cast_p=*/false);
6491 /* Parse an iteration-statement.
6493 iteration-statement:
6494 while ( condition ) statement
6495 do statement while ( expression ) ;
6496 for ( for-init-statement condition [opt] ; expression [opt] )
6499 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6502 cp_parser_iteration_statement (cp_parser* parser)
6507 bool in_iteration_statement_p;
6510 /* Peek at the next token. */
6511 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6513 return error_mark_node;
6515 /* Remember whether or not we are already within an iteration
6517 in_iteration_statement_p = parser->in_iteration_statement_p;
6519 /* See what kind of keyword it is. */
6520 keyword = token->keyword;
6527 /* Begin the while-statement. */
6528 statement = begin_while_stmt ();
6529 /* Look for the `('. */
6530 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6531 /* Parse the condition. */
6532 condition = cp_parser_condition (parser);
6533 finish_while_stmt_cond (condition, statement);
6534 /* Look for the `)'. */
6535 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6536 /* Parse the dependent statement. */
6537 parser->in_iteration_statement_p = true;
6538 cp_parser_already_scoped_statement (parser);
6539 parser->in_iteration_statement_p = in_iteration_statement_p;
6540 /* We're done with the while-statement. */
6541 finish_while_stmt (statement);
6549 /* Begin the do-statement. */
6550 statement = begin_do_stmt ();
6551 /* Parse the body of the do-statement. */
6552 parser->in_iteration_statement_p = true;
6553 cp_parser_implicitly_scoped_statement (parser);
6554 parser->in_iteration_statement_p = in_iteration_statement_p;
6555 finish_do_body (statement);
6556 /* Look for the `while' keyword. */
6557 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6558 /* Look for the `('. */
6559 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6560 /* Parse the expression. */
6561 expression = cp_parser_expression (parser, /*cast_p=*/false);
6562 /* We're done with the do-statement. */
6563 finish_do_stmt (expression, statement);
6564 /* Look for the `)'. */
6565 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6566 /* Look for the `;'. */
6567 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6573 tree condition = NULL_TREE;
6574 tree expression = NULL_TREE;
6576 /* Begin the for-statement. */
6577 statement = begin_for_stmt ();
6578 /* Look for the `('. */
6579 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6580 /* Parse the initialization. */
6581 cp_parser_for_init_statement (parser);
6582 finish_for_init_stmt (statement);
6584 /* If there's a condition, process it. */
6585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6586 condition = cp_parser_condition (parser);
6587 finish_for_cond (condition, statement);
6588 /* Look for the `;'. */
6589 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6591 /* If there's an expression, process it. */
6592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6593 expression = cp_parser_expression (parser, /*cast_p=*/false);
6594 finish_for_expr (expression, statement);
6595 /* Look for the `)'. */
6596 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6598 /* Parse the body of the for-statement. */
6599 parser->in_iteration_statement_p = true;
6600 cp_parser_already_scoped_statement (parser);
6601 parser->in_iteration_statement_p = in_iteration_statement_p;
6603 /* We're done with the for-statement. */
6604 finish_for_stmt (statement);
6609 cp_parser_error (parser, "expected iteration-statement");
6610 statement = error_mark_node;
6617 /* Parse a for-init-statement.
6620 expression-statement
6621 simple-declaration */
6624 cp_parser_for_init_statement (cp_parser* parser)
6626 /* If the next token is a `;', then we have an empty
6627 expression-statement. Grammatically, this is also a
6628 simple-declaration, but an invalid one, because it does not
6629 declare anything. Therefore, if we did not handle this case
6630 specially, we would issue an error message about an invalid
6632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6634 /* We're going to speculatively look for a declaration, falling back
6635 to an expression, if necessary. */
6636 cp_parser_parse_tentatively (parser);
6637 /* Parse the declaration. */
6638 cp_parser_simple_declaration (parser,
6639 /*function_definition_allowed_p=*/false);
6640 /* If the tentative parse failed, then we shall need to look for an
6641 expression-statement. */
6642 if (cp_parser_parse_definitely (parser))
6646 cp_parser_expression_statement (parser, false);
6649 /* Parse a jump-statement.
6654 return expression [opt] ;
6662 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6665 cp_parser_jump_statement (cp_parser* parser)
6667 tree statement = error_mark_node;
6671 /* Peek at the next token. */
6672 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6674 return error_mark_node;
6676 /* See what kind of keyword it is. */
6677 keyword = token->keyword;
6681 if (!parser->in_switch_statement_p
6682 && !parser->in_iteration_statement_p)
6684 error ("break statement not within loop or switch");
6685 statement = error_mark_node;
6688 statement = finish_break_stmt ();
6689 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6693 if (!parser->in_iteration_statement_p)
6695 error ("continue statement not within a loop");
6696 statement = error_mark_node;
6699 statement = finish_continue_stmt ();
6700 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6707 /* If the next token is a `;', then there is no
6709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6710 expr = cp_parser_expression (parser, /*cast_p=*/false);
6713 /* Build the return-statement. */
6714 statement = finish_return_stmt (expr);
6715 /* Look for the final `;'. */
6716 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6721 /* Create the goto-statement. */
6722 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6724 /* Issue a warning about this use of a GNU extension. */
6726 pedwarn ("ISO C++ forbids computed gotos");
6727 /* Consume the '*' token. */
6728 cp_lexer_consume_token (parser->lexer);
6729 /* Parse the dependent expression. */
6730 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6733 finish_goto_stmt (cp_parser_identifier (parser));
6734 /* Look for the final `;'. */
6735 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6739 cp_parser_error (parser, "expected jump-statement");
6746 /* Parse a declaration-statement.
6748 declaration-statement:
6749 block-declaration */
6752 cp_parser_declaration_statement (cp_parser* parser)
6756 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6757 p = obstack_alloc (&declarator_obstack, 0);
6759 /* Parse the block-declaration. */
6760 cp_parser_block_declaration (parser, /*statement_p=*/true);
6762 /* Free any declarators allocated. */
6763 obstack_free (&declarator_obstack, p);
6765 /* Finish off the statement. */
6769 /* Some dependent statements (like `if (cond) statement'), are
6770 implicitly in their own scope. In other words, if the statement is
6771 a single statement (as opposed to a compound-statement), it is
6772 none-the-less treated as if it were enclosed in braces. Any
6773 declarations appearing in the dependent statement are out of scope
6774 after control passes that point. This function parses a statement,
6775 but ensures that is in its own scope, even if it is not a
6778 Returns the new statement. */
6781 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6785 /* If the token is not a `{', then we must take special action. */
6786 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6788 /* Create a compound-statement. */
6789 statement = begin_compound_stmt (0);
6790 /* Parse the dependent-statement. */
6791 cp_parser_statement (parser, false);
6792 /* Finish the dummy compound-statement. */
6793 finish_compound_stmt (statement);
6795 /* Otherwise, we simply parse the statement directly. */
6797 statement = cp_parser_compound_statement (parser, NULL, false);
6799 /* Return the statement. */
6803 /* For some dependent statements (like `while (cond) statement'), we
6804 have already created a scope. Therefore, even if the dependent
6805 statement is a compound-statement, we do not want to create another
6809 cp_parser_already_scoped_statement (cp_parser* parser)
6811 /* If the token is a `{', then we must take special action. */
6812 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6813 cp_parser_statement (parser, false);
6816 /* Avoid calling cp_parser_compound_statement, so that we
6817 don't create a new scope. Do everything else by hand. */
6818 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6819 cp_parser_statement_seq_opt (parser, false);
6820 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6824 /* Declarations [gram.dcl.dcl] */
6826 /* Parse an optional declaration-sequence.
6830 declaration-seq declaration */
6833 cp_parser_declaration_seq_opt (cp_parser* parser)
6839 token = cp_lexer_peek_token (parser->lexer);
6841 if (token->type == CPP_CLOSE_BRACE
6842 || token->type == CPP_EOF)
6845 if (token->type == CPP_SEMICOLON)
6847 /* A declaration consisting of a single semicolon is
6848 invalid. Allow it unless we're being pedantic. */
6849 cp_lexer_consume_token (parser->lexer);
6850 if (pedantic && !in_system_header)
6851 pedwarn ("extra %<;%>");
6855 /* If we're entering or exiting a region that's implicitly
6856 extern "C", modify the lang context appropriately. */
6857 if (!parser->implicit_extern_c && token->implicit_extern_c)
6859 push_lang_context (lang_name_c);
6860 parser->implicit_extern_c = true;
6862 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6864 pop_lang_context ();
6865 parser->implicit_extern_c = false;
6868 if (token->type == CPP_PRAGMA)
6870 /* A top-level declaration can consist solely of a #pragma.
6871 A nested declaration cannot, so this is done here and not
6872 in cp_parser_declaration. (A #pragma at block scope is
6873 handled in cp_parser_statement.) */
6874 cp_lexer_handle_pragma (parser->lexer);
6878 /* Parse the declaration itself. */
6879 cp_parser_declaration (parser);
6883 /* Parse a declaration.
6888 template-declaration
6889 explicit-instantiation
6890 explicit-specialization
6891 linkage-specification
6892 namespace-definition
6897 __extension__ declaration */
6900 cp_parser_declaration (cp_parser* parser)
6907 /* Check for the `__extension__' keyword. */
6908 if (cp_parser_extension_opt (parser, &saved_pedantic))
6910 /* Parse the qualified declaration. */
6911 cp_parser_declaration (parser);
6912 /* Restore the PEDANTIC flag. */
6913 pedantic = saved_pedantic;
6918 /* Try to figure out what kind of declaration is present. */
6919 token1 = *cp_lexer_peek_token (parser->lexer);
6921 if (token1.type != CPP_EOF)
6922 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6924 token2.type = token2.keyword = RID_MAX;
6926 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6927 p = obstack_alloc (&declarator_obstack, 0);
6929 /* If the next token is `extern' and the following token is a string
6930 literal, then we have a linkage specification. */
6931 if (token1.keyword == RID_EXTERN
6932 && cp_parser_is_string_literal (&token2))
6933 cp_parser_linkage_specification (parser);
6934 /* If the next token is `template', then we have either a template
6935 declaration, an explicit instantiation, or an explicit
6937 else if (token1.keyword == RID_TEMPLATE)
6939 /* `template <>' indicates a template specialization. */
6940 if (token2.type == CPP_LESS
6941 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6942 cp_parser_explicit_specialization (parser);
6943 /* `template <' indicates a template declaration. */
6944 else if (token2.type == CPP_LESS)
6945 cp_parser_template_declaration (parser, /*member_p=*/false);
6946 /* Anything else must be an explicit instantiation. */
6948 cp_parser_explicit_instantiation (parser);
6950 /* If the next token is `export', then we have a template
6952 else if (token1.keyword == RID_EXPORT)
6953 cp_parser_template_declaration (parser, /*member_p=*/false);
6954 /* If the next token is `extern', 'static' or 'inline' and the one
6955 after that is `template', we have a GNU extended explicit
6956 instantiation directive. */
6957 else if (cp_parser_allow_gnu_extensions_p (parser)
6958 && (token1.keyword == RID_EXTERN
6959 || token1.keyword == RID_STATIC
6960 || token1.keyword == RID_INLINE)
6961 && token2.keyword == RID_TEMPLATE)
6962 cp_parser_explicit_instantiation (parser);
6963 /* If the next token is `namespace', check for a named or unnamed
6964 namespace definition. */
6965 else if (token1.keyword == RID_NAMESPACE
6966 && (/* A named namespace definition. */
6967 (token2.type == CPP_NAME
6968 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6970 /* An unnamed namespace definition. */
6971 || token2.type == CPP_OPEN_BRACE))
6972 cp_parser_namespace_definition (parser);
6973 /* Objective-C++ declaration/definition. */
6974 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6975 cp_parser_objc_declaration (parser);
6976 /* We must have either a block declaration or a function
6979 /* Try to parse a block-declaration, or a function-definition. */
6980 cp_parser_block_declaration (parser, /*statement_p=*/false);
6982 /* Free any declarators allocated. */
6983 obstack_free (&declarator_obstack, p);
6986 /* Parse a block-declaration.
6991 namespace-alias-definition
6998 __extension__ block-declaration
7001 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7002 part of a declaration-statement. */
7005 cp_parser_block_declaration (cp_parser *parser,
7011 /* Check for the `__extension__' keyword. */
7012 if (cp_parser_extension_opt (parser, &saved_pedantic))
7014 /* Parse the qualified declaration. */
7015 cp_parser_block_declaration (parser, statement_p);
7016 /* Restore the PEDANTIC flag. */
7017 pedantic = saved_pedantic;
7022 /* Peek at the next token to figure out which kind of declaration is
7024 token1 = cp_lexer_peek_token (parser->lexer);
7026 /* If the next keyword is `asm', we have an asm-definition. */
7027 if (token1->keyword == RID_ASM)
7030 cp_parser_commit_to_tentative_parse (parser);
7031 cp_parser_asm_definition (parser);
7033 /* If the next keyword is `namespace', we have a
7034 namespace-alias-definition. */
7035 else if (token1->keyword == RID_NAMESPACE)
7036 cp_parser_namespace_alias_definition (parser);
7037 /* If the next keyword is `using', we have either a
7038 using-declaration or a using-directive. */
7039 else if (token1->keyword == RID_USING)
7044 cp_parser_commit_to_tentative_parse (parser);
7045 /* If the token after `using' is `namespace', then we have a
7047 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7048 if (token2->keyword == RID_NAMESPACE)
7049 cp_parser_using_directive (parser);
7050 /* Otherwise, it's a using-declaration. */
7052 cp_parser_using_declaration (parser);
7054 /* If the next keyword is `__label__' we have a label declaration. */
7055 else if (token1->keyword == RID_LABEL)
7058 cp_parser_commit_to_tentative_parse (parser);
7059 cp_parser_label_declaration (parser);
7061 /* Anything else must be a simple-declaration. */
7063 cp_parser_simple_declaration (parser, !statement_p);
7066 /* Parse a simple-declaration.
7069 decl-specifier-seq [opt] init-declarator-list [opt] ;
7071 init-declarator-list:
7073 init-declarator-list , init-declarator
7075 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7076 function-definition as a simple-declaration. */
7079 cp_parser_simple_declaration (cp_parser* parser,
7080 bool function_definition_allowed_p)
7082 cp_decl_specifier_seq decl_specifiers;
7083 int declares_class_or_enum;
7084 bool saw_declarator;
7086 /* Defer access checks until we know what is being declared; the
7087 checks for names appearing in the decl-specifier-seq should be
7088 done as if we were in the scope of the thing being declared. */
7089 push_deferring_access_checks (dk_deferred);
7091 /* Parse the decl-specifier-seq. We have to keep track of whether
7092 or not the decl-specifier-seq declares a named class or
7093 enumeration type, since that is the only case in which the
7094 init-declarator-list is allowed to be empty.
7098 In a simple-declaration, the optional init-declarator-list can be
7099 omitted only when declaring a class or enumeration, that is when
7100 the decl-specifier-seq contains either a class-specifier, an
7101 elaborated-type-specifier, or an enum-specifier. */
7102 cp_parser_decl_specifier_seq (parser,
7103 CP_PARSER_FLAGS_OPTIONAL,
7105 &declares_class_or_enum);
7106 /* We no longer need to defer access checks. */
7107 stop_deferring_access_checks ();
7109 /* In a block scope, a valid declaration must always have a
7110 decl-specifier-seq. By not trying to parse declarators, we can
7111 resolve the declaration/expression ambiguity more quickly. */
7112 if (!function_definition_allowed_p
7113 && !decl_specifiers.any_specifiers_p)
7115 cp_parser_error (parser, "expected declaration");
7119 /* If the next two tokens are both identifiers, the code is
7120 erroneous. The usual cause of this situation is code like:
7124 where "T" should name a type -- but does not. */
7125 if (!decl_specifiers.type
7126 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7128 /* If parsing tentatively, we should commit; we really are
7129 looking at a declaration. */
7130 cp_parser_commit_to_tentative_parse (parser);
7135 /* If we have seen at least one decl-specifier, and the next token
7136 is not a parenthesis, then we must be looking at a declaration.
7137 (After "int (" we might be looking at a functional cast.) */
7138 if (decl_specifiers.any_specifiers_p
7139 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7140 cp_parser_commit_to_tentative_parse (parser);
7142 /* Keep going until we hit the `;' at the end of the simple
7144 saw_declarator = false;
7145 while (cp_lexer_next_token_is_not (parser->lexer,
7149 bool function_definition_p;
7152 saw_declarator = true;
7153 /* Parse the init-declarator. */
7154 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7155 function_definition_allowed_p,
7157 declares_class_or_enum,
7158 &function_definition_p);
7159 /* If an error occurred while parsing tentatively, exit quickly.
7160 (That usually happens when in the body of a function; each
7161 statement is treated as a declaration-statement until proven
7163 if (cp_parser_error_occurred (parser))
7165 /* Handle function definitions specially. */
7166 if (function_definition_p)
7168 /* If the next token is a `,', then we are probably
7169 processing something like:
7173 which is erroneous. */
7174 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7175 error ("mixing declarations and function-definitions is forbidden");
7176 /* Otherwise, we're done with the list of declarators. */
7179 pop_deferring_access_checks ();
7183 /* The next token should be either a `,' or a `;'. */
7184 token = cp_lexer_peek_token (parser->lexer);
7185 /* If it's a `,', there are more declarators to come. */
7186 if (token->type == CPP_COMMA)
7187 cp_lexer_consume_token (parser->lexer);
7188 /* If it's a `;', we are done. */
7189 else if (token->type == CPP_SEMICOLON)
7191 /* Anything else is an error. */
7194 /* If we have already issued an error message we don't need
7195 to issue another one. */
7196 if (decl != error_mark_node
7197 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7198 cp_parser_error (parser, "expected %<,%> or %<;%>");
7199 /* Skip tokens until we reach the end of the statement. */
7200 cp_parser_skip_to_end_of_statement (parser);
7201 /* If the next token is now a `;', consume it. */
7202 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7203 cp_lexer_consume_token (parser->lexer);
7206 /* After the first time around, a function-definition is not
7207 allowed -- even if it was OK at first. For example:
7212 function_definition_allowed_p = false;
7215 /* Issue an error message if no declarators are present, and the
7216 decl-specifier-seq does not itself declare a class or
7218 if (!saw_declarator)
7220 if (cp_parser_declares_only_class_p (parser))
7221 shadow_tag (&decl_specifiers);
7222 /* Perform any deferred access checks. */
7223 perform_deferred_access_checks ();
7226 /* Consume the `;'. */
7227 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7230 pop_deferring_access_checks ();
7233 /* Parse a decl-specifier-seq.
7236 decl-specifier-seq [opt] decl-specifier
7239 storage-class-specifier
7250 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7252 The parser flags FLAGS is used to control type-specifier parsing.
7254 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7257 1: one of the decl-specifiers is an elaborated-type-specifier
7258 (i.e., a type declaration)
7259 2: one of the decl-specifiers is an enum-specifier or a
7260 class-specifier (i.e., a type definition)
7265 cp_parser_decl_specifier_seq (cp_parser* parser,
7266 cp_parser_flags flags,
7267 cp_decl_specifier_seq *decl_specs,
7268 int* declares_class_or_enum)
7270 bool constructor_possible_p = !parser->in_declarator_p;
7272 /* Clear DECL_SPECS. */
7273 clear_decl_specs (decl_specs);
7275 /* Assume no class or enumeration type is declared. */
7276 *declares_class_or_enum = 0;
7278 /* Keep reading specifiers until there are no more to read. */
7282 bool found_decl_spec;
7285 /* Peek at the next token. */
7286 token = cp_lexer_peek_token (parser->lexer);
7287 /* Handle attributes. */
7288 if (token->keyword == RID_ATTRIBUTE)
7290 /* Parse the attributes. */
7291 decl_specs->attributes
7292 = chainon (decl_specs->attributes,
7293 cp_parser_attributes_opt (parser));
7296 /* Assume we will find a decl-specifier keyword. */
7297 found_decl_spec = true;
7298 /* If the next token is an appropriate keyword, we can simply
7299 add it to the list. */
7300 switch (token->keyword)
7305 if (decl_specs->specs[(int) ds_friend]++)
7306 error ("duplicate %<friend%>");
7307 /* Consume the token. */
7308 cp_lexer_consume_token (parser->lexer);
7311 /* function-specifier:
7318 cp_parser_function_specifier_opt (parser, decl_specs);
7324 ++decl_specs->specs[(int) ds_typedef];
7325 /* Consume the token. */
7326 cp_lexer_consume_token (parser->lexer);
7327 /* A constructor declarator cannot appear in a typedef. */
7328 constructor_possible_p = false;
7329 /* The "typedef" keyword can only occur in a declaration; we
7330 may as well commit at this point. */
7331 cp_parser_commit_to_tentative_parse (parser);
7334 /* storage-class-specifier:
7344 /* Consume the token. */
7345 cp_lexer_consume_token (parser->lexer);
7346 cp_parser_set_storage_class (decl_specs, sc_auto);
7349 /* Consume the token. */
7350 cp_lexer_consume_token (parser->lexer);
7351 cp_parser_set_storage_class (decl_specs, sc_register);
7354 /* Consume the token. */
7355 cp_lexer_consume_token (parser->lexer);
7356 if (decl_specs->specs[(int) ds_thread])
7358 error ("%<__thread%> before %<static%>");
7359 decl_specs->specs[(int) ds_thread] = 0;
7361 cp_parser_set_storage_class (decl_specs, sc_static);
7364 /* Consume the token. */
7365 cp_lexer_consume_token (parser->lexer);
7366 if (decl_specs->specs[(int) ds_thread])
7368 error ("%<__thread%> before %<extern%>");
7369 decl_specs->specs[(int) ds_thread] = 0;
7371 cp_parser_set_storage_class (decl_specs, sc_extern);
7374 /* Consume the token. */
7375 cp_lexer_consume_token (parser->lexer);
7376 cp_parser_set_storage_class (decl_specs, sc_mutable);
7379 /* Consume the token. */
7380 cp_lexer_consume_token (parser->lexer);
7381 ++decl_specs->specs[(int) ds_thread];
7385 /* We did not yet find a decl-specifier yet. */
7386 found_decl_spec = false;
7390 /* Constructors are a special case. The `S' in `S()' is not a
7391 decl-specifier; it is the beginning of the declarator. */
7394 && constructor_possible_p
7395 && (cp_parser_constructor_declarator_p
7396 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7398 /* If we don't have a DECL_SPEC yet, then we must be looking at
7399 a type-specifier. */
7400 if (!found_decl_spec && !constructor_p)
7402 int decl_spec_declares_class_or_enum;
7403 bool is_cv_qualifier;
7407 = cp_parser_type_specifier (parser, flags,
7409 /*is_declaration=*/true,
7410 &decl_spec_declares_class_or_enum,
7413 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7415 /* If this type-specifier referenced a user-defined type
7416 (a typedef, class-name, etc.), then we can't allow any
7417 more such type-specifiers henceforth.
7421 The longest sequence of decl-specifiers that could
7422 possibly be a type name is taken as the
7423 decl-specifier-seq of a declaration. The sequence shall
7424 be self-consistent as described below.
7428 As a general rule, at most one type-specifier is allowed
7429 in the complete decl-specifier-seq of a declaration. The
7430 only exceptions are the following:
7432 -- const or volatile can be combined with any other
7435 -- signed or unsigned can be combined with char, long,
7443 void g (const int Pc);
7445 Here, Pc is *not* part of the decl-specifier seq; it's
7446 the declarator. Therefore, once we see a type-specifier
7447 (other than a cv-qualifier), we forbid any additional
7448 user-defined types. We *do* still allow things like `int
7449 int' to be considered a decl-specifier-seq, and issue the
7450 error message later. */
7451 if (type_spec && !is_cv_qualifier)
7452 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7453 /* A constructor declarator cannot follow a type-specifier. */
7456 constructor_possible_p = false;
7457 found_decl_spec = true;
7461 /* If we still do not have a DECL_SPEC, then there are no more
7463 if (!found_decl_spec)
7466 decl_specs->any_specifiers_p = true;
7467 /* After we see one decl-specifier, further decl-specifiers are
7469 flags |= CP_PARSER_FLAGS_OPTIONAL;
7472 /* Don't allow a friend specifier with a class definition. */
7473 if (decl_specs->specs[(int) ds_friend] != 0
7474 && (*declares_class_or_enum & 2))
7475 error ("class definition may not be declared a friend");
7478 /* Parse an (optional) storage-class-specifier.
7480 storage-class-specifier:
7489 storage-class-specifier:
7492 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7495 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7497 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7505 /* Consume the token. */
7506 return cp_lexer_consume_token (parser->lexer)->value;
7513 /* Parse an (optional) function-specifier.
7520 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7521 Updates DECL_SPECS, if it is non-NULL. */
7524 cp_parser_function_specifier_opt (cp_parser* parser,
7525 cp_decl_specifier_seq *decl_specs)
7527 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7531 ++decl_specs->specs[(int) ds_inline];
7536 ++decl_specs->specs[(int) ds_virtual];
7541 ++decl_specs->specs[(int) ds_explicit];
7548 /* Consume the token. */
7549 return cp_lexer_consume_token (parser->lexer)->value;
7552 /* Parse a linkage-specification.
7554 linkage-specification:
7555 extern string-literal { declaration-seq [opt] }
7556 extern string-literal declaration */
7559 cp_parser_linkage_specification (cp_parser* parser)
7563 /* Look for the `extern' keyword. */
7564 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7566 /* Look for the string-literal. */
7567 linkage = cp_parser_string_literal (parser, false, false);
7569 /* Transform the literal into an identifier. If the literal is a
7570 wide-character string, or contains embedded NULs, then we can't
7571 handle it as the user wants. */
7572 if (strlen (TREE_STRING_POINTER (linkage))
7573 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7575 cp_parser_error (parser, "invalid linkage-specification");
7576 /* Assume C++ linkage. */
7577 linkage = lang_name_cplusplus;
7580 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7582 /* We're now using the new linkage. */
7583 push_lang_context (linkage);
7585 /* If the next token is a `{', then we're using the first
7587 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7589 /* Consume the `{' token. */
7590 cp_lexer_consume_token (parser->lexer);
7591 /* Parse the declarations. */
7592 cp_parser_declaration_seq_opt (parser);
7593 /* Look for the closing `}'. */
7594 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7596 /* Otherwise, there's just one declaration. */
7599 bool saved_in_unbraced_linkage_specification_p;
7601 saved_in_unbraced_linkage_specification_p
7602 = parser->in_unbraced_linkage_specification_p;
7603 parser->in_unbraced_linkage_specification_p = true;
7604 have_extern_spec = true;
7605 cp_parser_declaration (parser);
7606 have_extern_spec = false;
7607 parser->in_unbraced_linkage_specification_p
7608 = saved_in_unbraced_linkage_specification_p;
7611 /* We're done with the linkage-specification. */
7612 pop_lang_context ();
7615 /* Special member functions [gram.special] */
7617 /* Parse a conversion-function-id.
7619 conversion-function-id:
7620 operator conversion-type-id
7622 Returns an IDENTIFIER_NODE representing the operator. */
7625 cp_parser_conversion_function_id (cp_parser* parser)
7629 tree saved_qualifying_scope;
7630 tree saved_object_scope;
7631 tree pushed_scope = NULL_TREE;
7633 /* Look for the `operator' token. */
7634 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7635 return error_mark_node;
7636 /* When we parse the conversion-type-id, the current scope will be
7637 reset. However, we need that information in able to look up the
7638 conversion function later, so we save it here. */
7639 saved_scope = parser->scope;
7640 saved_qualifying_scope = parser->qualifying_scope;
7641 saved_object_scope = parser->object_scope;
7642 /* We must enter the scope of the class so that the names of
7643 entities declared within the class are available in the
7644 conversion-type-id. For example, consider:
7651 S::operator I() { ... }
7653 In order to see that `I' is a type-name in the definition, we
7654 must be in the scope of `S'. */
7656 pushed_scope = push_scope (saved_scope);
7657 /* Parse the conversion-type-id. */
7658 type = cp_parser_conversion_type_id (parser);
7659 /* Leave the scope of the class, if any. */
7661 pop_scope (pushed_scope);
7662 /* Restore the saved scope. */
7663 parser->scope = saved_scope;
7664 parser->qualifying_scope = saved_qualifying_scope;
7665 parser->object_scope = saved_object_scope;
7666 /* If the TYPE is invalid, indicate failure. */
7667 if (type == error_mark_node)
7668 return error_mark_node;
7669 return mangle_conv_op_name_for_type (type);
7672 /* Parse a conversion-type-id:
7675 type-specifier-seq conversion-declarator [opt]
7677 Returns the TYPE specified. */
7680 cp_parser_conversion_type_id (cp_parser* parser)
7683 cp_decl_specifier_seq type_specifiers;
7684 cp_declarator *declarator;
7685 tree type_specified;
7687 /* Parse the attributes. */
7688 attributes = cp_parser_attributes_opt (parser);
7689 /* Parse the type-specifiers. */
7690 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7692 /* If that didn't work, stop. */
7693 if (type_specifiers.type == error_mark_node)
7694 return error_mark_node;
7695 /* Parse the conversion-declarator. */
7696 declarator = cp_parser_conversion_declarator_opt (parser);
7698 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7699 /*initialized=*/0, &attributes);
7701 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7702 return type_specified;
7705 /* Parse an (optional) conversion-declarator.
7707 conversion-declarator:
7708 ptr-operator conversion-declarator [opt]
7712 static cp_declarator *
7713 cp_parser_conversion_declarator_opt (cp_parser* parser)
7715 enum tree_code code;
7717 cp_cv_quals cv_quals;
7719 /* We don't know if there's a ptr-operator next, or not. */
7720 cp_parser_parse_tentatively (parser);
7721 /* Try the ptr-operator. */
7722 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7723 /* If it worked, look for more conversion-declarators. */
7724 if (cp_parser_parse_definitely (parser))
7726 cp_declarator *declarator;
7728 /* Parse another optional declarator. */
7729 declarator = cp_parser_conversion_declarator_opt (parser);
7731 /* Create the representation of the declarator. */
7733 declarator = make_ptrmem_declarator (cv_quals, class_type,
7735 else if (code == INDIRECT_REF)
7736 declarator = make_pointer_declarator (cv_quals, declarator);
7738 declarator = make_reference_declarator (cv_quals, declarator);
7746 /* Parse an (optional) ctor-initializer.
7749 : mem-initializer-list
7751 Returns TRUE iff the ctor-initializer was actually present. */
7754 cp_parser_ctor_initializer_opt (cp_parser* parser)
7756 /* If the next token is not a `:', then there is no
7757 ctor-initializer. */
7758 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7760 /* Do default initialization of any bases and members. */
7761 if (DECL_CONSTRUCTOR_P (current_function_decl))
7762 finish_mem_initializers (NULL_TREE);
7767 /* Consume the `:' token. */
7768 cp_lexer_consume_token (parser->lexer);
7769 /* And the mem-initializer-list. */
7770 cp_parser_mem_initializer_list (parser);
7775 /* Parse a mem-initializer-list.
7777 mem-initializer-list:
7779 mem-initializer , mem-initializer-list */
7782 cp_parser_mem_initializer_list (cp_parser* parser)
7784 tree mem_initializer_list = NULL_TREE;
7786 /* Let the semantic analysis code know that we are starting the
7787 mem-initializer-list. */
7788 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7789 error ("only constructors take base initializers");
7791 /* Loop through the list. */
7794 tree mem_initializer;
7796 /* Parse the mem-initializer. */
7797 mem_initializer = cp_parser_mem_initializer (parser);
7798 /* Add it to the list, unless it was erroneous. */
7799 if (mem_initializer)
7801 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7802 mem_initializer_list = mem_initializer;
7804 /* If the next token is not a `,', we're done. */
7805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7807 /* Consume the `,' token. */
7808 cp_lexer_consume_token (parser->lexer);
7811 /* Perform semantic analysis. */
7812 if (DECL_CONSTRUCTOR_P (current_function_decl))
7813 finish_mem_initializers (mem_initializer_list);
7816 /* Parse a mem-initializer.
7819 mem-initializer-id ( expression-list [opt] )
7824 ( expression-list [opt] )
7826 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7827 class) or FIELD_DECL (for a non-static data member) to initialize;
7828 the TREE_VALUE is the expression-list. */
7831 cp_parser_mem_initializer (cp_parser* parser)
7833 tree mem_initializer_id;
7834 tree expression_list;
7837 /* Find out what is being initialized. */
7838 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7840 pedwarn ("anachronistic old-style base class initializer");
7841 mem_initializer_id = NULL_TREE;
7844 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7845 member = expand_member_init (mem_initializer_id);
7846 if (member && !DECL_P (member))
7847 in_base_initializer = 1;
7850 = cp_parser_parenthesized_expression_list (parser, false,
7852 /*non_constant_p=*/NULL);
7853 if (!expression_list)
7854 expression_list = void_type_node;
7856 in_base_initializer = 0;
7858 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7861 /* Parse a mem-initializer-id.
7864 :: [opt] nested-name-specifier [opt] class-name
7867 Returns a TYPE indicating the class to be initializer for the first
7868 production. Returns an IDENTIFIER_NODE indicating the data member
7869 to be initialized for the second production. */
7872 cp_parser_mem_initializer_id (cp_parser* parser)
7874 bool global_scope_p;
7875 bool nested_name_specifier_p;
7876 bool template_p = false;
7879 /* `typename' is not allowed in this context ([temp.res]). */
7880 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7882 error ("keyword %<typename%> not allowed in this context (a qualified "
7883 "member initializer is implicitly a type)");
7884 cp_lexer_consume_token (parser->lexer);
7886 /* Look for the optional `::' operator. */
7888 = (cp_parser_global_scope_opt (parser,
7889 /*current_scope_valid_p=*/false)
7891 /* Look for the optional nested-name-specifier. The simplest way to
7896 The keyword `typename' is not permitted in a base-specifier or
7897 mem-initializer; in these contexts a qualified name that
7898 depends on a template-parameter is implicitly assumed to be a
7901 is to assume that we have seen the `typename' keyword at this
7903 nested_name_specifier_p
7904 = (cp_parser_nested_name_specifier_opt (parser,
7905 /*typename_keyword_p=*/true,
7906 /*check_dependency_p=*/true,
7908 /*is_declaration=*/true)
7910 if (nested_name_specifier_p)
7911 template_p = cp_parser_optional_template_keyword (parser);
7912 /* If there is a `::' operator or a nested-name-specifier, then we
7913 are definitely looking for a class-name. */
7914 if (global_scope_p || nested_name_specifier_p)
7915 return cp_parser_class_name (parser,
7916 /*typename_keyword_p=*/true,
7917 /*template_keyword_p=*/template_p,
7919 /*check_dependency_p=*/true,
7920 /*class_head_p=*/false,
7921 /*is_declaration=*/true);
7922 /* Otherwise, we could also be looking for an ordinary identifier. */
7923 cp_parser_parse_tentatively (parser);
7924 /* Try a class-name. */
7925 id = cp_parser_class_name (parser,
7926 /*typename_keyword_p=*/true,
7927 /*template_keyword_p=*/false,
7929 /*check_dependency_p=*/true,
7930 /*class_head_p=*/false,
7931 /*is_declaration=*/true);
7932 /* If we found one, we're done. */
7933 if (cp_parser_parse_definitely (parser))
7935 /* Otherwise, look for an ordinary identifier. */
7936 return cp_parser_identifier (parser);
7939 /* Overloading [gram.over] */
7941 /* Parse an operator-function-id.
7943 operator-function-id:
7946 Returns an IDENTIFIER_NODE for the operator which is a
7947 human-readable spelling of the identifier, e.g., `operator +'. */
7950 cp_parser_operator_function_id (cp_parser* parser)
7952 /* Look for the `operator' keyword. */
7953 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7954 return error_mark_node;
7955 /* And then the name of the operator itself. */
7956 return cp_parser_operator (parser);
7959 /* Parse an operator.
7962 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7963 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7964 || ++ -- , ->* -> () []
7971 Returns an IDENTIFIER_NODE for the operator which is a
7972 human-readable spelling of the identifier, e.g., `operator +'. */
7975 cp_parser_operator (cp_parser* parser)
7977 tree id = NULL_TREE;
7980 /* Peek at the next token. */
7981 token = cp_lexer_peek_token (parser->lexer);
7982 /* Figure out which operator we have. */
7983 switch (token->type)
7989 /* The keyword should be either `new' or `delete'. */
7990 if (token->keyword == RID_NEW)
7992 else if (token->keyword == RID_DELETE)
7997 /* Consume the `new' or `delete' token. */
7998 cp_lexer_consume_token (parser->lexer);
8000 /* Peek at the next token. */
8001 token = cp_lexer_peek_token (parser->lexer);
8002 /* If it's a `[' token then this is the array variant of the
8004 if (token->type == CPP_OPEN_SQUARE)
8006 /* Consume the `[' token. */
8007 cp_lexer_consume_token (parser->lexer);
8008 /* Look for the `]' token. */
8009 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8010 id = ansi_opname (op == NEW_EXPR
8011 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8013 /* Otherwise, we have the non-array variant. */
8015 id = ansi_opname (op);
8021 id = ansi_opname (PLUS_EXPR);
8025 id = ansi_opname (MINUS_EXPR);
8029 id = ansi_opname (MULT_EXPR);
8033 id = ansi_opname (TRUNC_DIV_EXPR);
8037 id = ansi_opname (TRUNC_MOD_EXPR);
8041 id = ansi_opname (BIT_XOR_EXPR);
8045 id = ansi_opname (BIT_AND_EXPR);
8049 id = ansi_opname (BIT_IOR_EXPR);
8053 id = ansi_opname (BIT_NOT_EXPR);
8057 id = ansi_opname (TRUTH_NOT_EXPR);
8061 id = ansi_assopname (NOP_EXPR);
8065 id = ansi_opname (LT_EXPR);
8069 id = ansi_opname (GT_EXPR);
8073 id = ansi_assopname (PLUS_EXPR);
8077 id = ansi_assopname (MINUS_EXPR);
8081 id = ansi_assopname (MULT_EXPR);
8085 id = ansi_assopname (TRUNC_DIV_EXPR);
8089 id = ansi_assopname (TRUNC_MOD_EXPR);
8093 id = ansi_assopname (BIT_XOR_EXPR);
8097 id = ansi_assopname (BIT_AND_EXPR);
8101 id = ansi_assopname (BIT_IOR_EXPR);
8105 id = ansi_opname (LSHIFT_EXPR);
8109 id = ansi_opname (RSHIFT_EXPR);
8113 id = ansi_assopname (LSHIFT_EXPR);
8117 id = ansi_assopname (RSHIFT_EXPR);
8121 id = ansi_opname (EQ_EXPR);
8125 id = ansi_opname (NE_EXPR);
8129 id = ansi_opname (LE_EXPR);
8132 case CPP_GREATER_EQ:
8133 id = ansi_opname (GE_EXPR);
8137 id = ansi_opname (TRUTH_ANDIF_EXPR);
8141 id = ansi_opname (TRUTH_ORIF_EXPR);
8145 id = ansi_opname (POSTINCREMENT_EXPR);
8148 case CPP_MINUS_MINUS:
8149 id = ansi_opname (PREDECREMENT_EXPR);
8153 id = ansi_opname (COMPOUND_EXPR);
8156 case CPP_DEREF_STAR:
8157 id = ansi_opname (MEMBER_REF);
8161 id = ansi_opname (COMPONENT_REF);
8164 case CPP_OPEN_PAREN:
8165 /* Consume the `('. */
8166 cp_lexer_consume_token (parser->lexer);
8167 /* Look for the matching `)'. */
8168 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8169 return ansi_opname (CALL_EXPR);
8171 case CPP_OPEN_SQUARE:
8172 /* Consume the `['. */
8173 cp_lexer_consume_token (parser->lexer);
8174 /* Look for the matching `]'. */
8175 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8176 return ansi_opname (ARRAY_REF);
8180 id = ansi_opname (MIN_EXPR);
8181 cp_parser_warn_min_max ();
8185 id = ansi_opname (MAX_EXPR);
8186 cp_parser_warn_min_max ();
8190 id = ansi_assopname (MIN_EXPR);
8191 cp_parser_warn_min_max ();
8195 id = ansi_assopname (MAX_EXPR);
8196 cp_parser_warn_min_max ();
8200 /* Anything else is an error. */
8204 /* If we have selected an identifier, we need to consume the
8207 cp_lexer_consume_token (parser->lexer);
8208 /* Otherwise, no valid operator name was present. */
8211 cp_parser_error (parser, "expected operator");
8212 id = error_mark_node;
8218 /* Parse a template-declaration.
8220 template-declaration:
8221 export [opt] template < template-parameter-list > declaration
8223 If MEMBER_P is TRUE, this template-declaration occurs within a
8226 The grammar rule given by the standard isn't correct. What
8229 template-declaration:
8230 export [opt] template-parameter-list-seq
8231 decl-specifier-seq [opt] init-declarator [opt] ;
8232 export [opt] template-parameter-list-seq
8235 template-parameter-list-seq:
8236 template-parameter-list-seq [opt]
8237 template < template-parameter-list > */
8240 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8242 /* Check for `export'. */
8243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8245 /* Consume the `export' token. */
8246 cp_lexer_consume_token (parser->lexer);
8247 /* Warn that we do not support `export'. */
8248 warning (0, "keyword %<export%> not implemented, and will be ignored");
8251 cp_parser_template_declaration_after_export (parser, member_p);
8254 /* Parse a template-parameter-list.
8256 template-parameter-list:
8258 template-parameter-list , template-parameter
8260 Returns a TREE_LIST. Each node represents a template parameter.
8261 The nodes are connected via their TREE_CHAINs. */
8264 cp_parser_template_parameter_list (cp_parser* parser)
8266 tree parameter_list = NULL_TREE;
8274 /* Parse the template-parameter. */
8275 parameter = cp_parser_template_parameter (parser, &is_non_type);
8276 /* Add it to the list. */
8277 if (parameter != error_mark_node)
8278 parameter_list = process_template_parm (parameter_list,
8281 /* Peek at the next token. */
8282 token = cp_lexer_peek_token (parser->lexer);
8283 /* If it's not a `,', we're done. */
8284 if (token->type != CPP_COMMA)
8286 /* Otherwise, consume the `,' token. */
8287 cp_lexer_consume_token (parser->lexer);
8290 return parameter_list;
8293 /* Parse a template-parameter.
8297 parameter-declaration
8299 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8300 the parameter. The TREE_PURPOSE is the default value, if any.
8301 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8302 iff this parameter is a non-type parameter. */
8305 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8308 cp_parameter_declarator *parameter_declarator;
8311 /* Assume it is a type parameter or a template parameter. */
8312 *is_non_type = false;
8313 /* Peek at the next token. */
8314 token = cp_lexer_peek_token (parser->lexer);
8315 /* If it is `class' or `template', we have a type-parameter. */
8316 if (token->keyword == RID_TEMPLATE)
8317 return cp_parser_type_parameter (parser);
8318 /* If it is `class' or `typename' we do not know yet whether it is a
8319 type parameter or a non-type parameter. Consider:
8321 template <typename T, typename T::X X> ...
8325 template <class C, class D*> ...
8327 Here, the first parameter is a type parameter, and the second is
8328 a non-type parameter. We can tell by looking at the token after
8329 the identifier -- if it is a `,', `=', or `>' then we have a type
8331 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8333 /* Peek at the token after `class' or `typename'. */
8334 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8335 /* If it's an identifier, skip it. */
8336 if (token->type == CPP_NAME)
8337 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8338 /* Now, see if the token looks like the end of a template
8340 if (token->type == CPP_COMMA
8341 || token->type == CPP_EQ
8342 || token->type == CPP_GREATER)
8343 return cp_parser_type_parameter (parser);
8346 /* Otherwise, it is a non-type parameter.
8350 When parsing a default template-argument for a non-type
8351 template-parameter, the first non-nested `>' is taken as the end
8352 of the template parameter-list rather than a greater-than
8354 *is_non_type = true;
8355 parameter_declarator
8356 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8357 /*parenthesized_p=*/NULL);
8358 parm = grokdeclarator (parameter_declarator->declarator,
8359 ¶meter_declarator->decl_specifiers,
8360 PARM, /*initialized=*/0,
8362 if (parm == error_mark_node)
8363 return error_mark_node;
8364 return build_tree_list (parameter_declarator->default_argument, parm);
8367 /* Parse a type-parameter.
8370 class identifier [opt]
8371 class identifier [opt] = type-id
8372 typename identifier [opt]
8373 typename identifier [opt] = type-id
8374 template < template-parameter-list > class identifier [opt]
8375 template < template-parameter-list > class identifier [opt]
8378 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8379 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8380 the declaration of the parameter. */
8383 cp_parser_type_parameter (cp_parser* parser)
8388 /* Look for a keyword to tell us what kind of parameter this is. */
8389 token = cp_parser_require (parser, CPP_KEYWORD,
8390 "`class', `typename', or `template'");
8392 return error_mark_node;
8394 switch (token->keyword)
8400 tree default_argument;
8402 /* If the next token is an identifier, then it names the
8404 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8405 identifier = cp_parser_identifier (parser);
8407 identifier = NULL_TREE;
8409 /* Create the parameter. */
8410 parameter = finish_template_type_parm (class_type_node, identifier);
8412 /* If the next token is an `=', we have a default argument. */
8413 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8415 /* Consume the `=' token. */
8416 cp_lexer_consume_token (parser->lexer);
8417 /* Parse the default-argument. */
8418 default_argument = cp_parser_type_id (parser);
8421 default_argument = NULL_TREE;
8423 /* Create the combined representation of the parameter and the
8424 default argument. */
8425 parameter = build_tree_list (default_argument, parameter);
8431 tree parameter_list;
8433 tree default_argument;
8435 /* Look for the `<'. */
8436 cp_parser_require (parser, CPP_LESS, "`<'");
8437 /* Parse the template-parameter-list. */
8438 begin_template_parm_list ();
8440 = cp_parser_template_parameter_list (parser);
8441 parameter_list = end_template_parm_list (parameter_list);
8442 /* Look for the `>'. */
8443 cp_parser_require (parser, CPP_GREATER, "`>'");
8444 /* Look for the `class' keyword. */
8445 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8446 /* If the next token is an `=', then there is a
8447 default-argument. If the next token is a `>', we are at
8448 the end of the parameter-list. If the next token is a `,',
8449 then we are at the end of this parameter. */
8450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8451 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8452 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8454 identifier = cp_parser_identifier (parser);
8455 /* Treat invalid names as if the parameter were nameless. */
8456 if (identifier == error_mark_node)
8457 identifier = NULL_TREE;
8460 identifier = NULL_TREE;
8462 /* Create the template parameter. */
8463 parameter = finish_template_template_parm (class_type_node,
8466 /* If the next token is an `=', then there is a
8467 default-argument. */
8468 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8472 /* Consume the `='. */
8473 cp_lexer_consume_token (parser->lexer);
8474 /* Parse the id-expression. */
8476 = cp_parser_id_expression (parser,
8477 /*template_keyword_p=*/false,
8478 /*check_dependency_p=*/true,
8479 /*template_p=*/&is_template,
8480 /*declarator_p=*/false);
8481 if (TREE_CODE (default_argument) == TYPE_DECL)
8482 /* If the id-expression was a template-id that refers to
8483 a template-class, we already have the declaration here,
8484 so no further lookup is needed. */
8487 /* Look up the name. */
8489 = cp_parser_lookup_name (parser, default_argument,
8491 /*is_template=*/is_template,
8492 /*is_namespace=*/false,
8493 /*check_dependency=*/true,
8494 /*ambiguous_p=*/NULL);
8495 /* See if the default argument is valid. */
8497 = check_template_template_default_arg (default_argument);
8500 default_argument = NULL_TREE;
8502 /* Create the combined representation of the parameter and the
8503 default argument. */
8504 parameter = build_tree_list (default_argument, parameter);
8516 /* Parse a template-id.
8519 template-name < template-argument-list [opt] >
8521 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8522 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8523 returned. Otherwise, if the template-name names a function, or set
8524 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8525 names a class, returns a TYPE_DECL for the specialization.
8527 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8528 uninstantiated templates. */
8531 cp_parser_template_id (cp_parser *parser,
8532 bool template_keyword_p,
8533 bool check_dependency_p,
8534 bool is_declaration)
8539 cp_token_position start_of_id = 0;
8540 tree access_check = NULL_TREE;
8541 cp_token *next_token, *next_token_2;
8544 /* If the next token corresponds to a template-id, there is no need
8546 next_token = cp_lexer_peek_token (parser->lexer);
8547 if (next_token->type == CPP_TEMPLATE_ID)
8552 /* Get the stored value. */
8553 value = cp_lexer_consume_token (parser->lexer)->value;
8554 /* Perform any access checks that were deferred. */
8555 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8556 perform_or_defer_access_check (TREE_PURPOSE (check),
8557 TREE_VALUE (check));
8558 /* Return the stored value. */
8559 return TREE_VALUE (value);
8562 /* Avoid performing name lookup if there is no possibility of
8563 finding a template-id. */
8564 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8565 || (next_token->type == CPP_NAME
8566 && !cp_parser_nth_token_starts_template_argument_list_p
8569 cp_parser_error (parser, "expected template-id");
8570 return error_mark_node;
8573 /* Remember where the template-id starts. */
8574 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8575 start_of_id = cp_lexer_token_position (parser->lexer, false);
8577 push_deferring_access_checks (dk_deferred);
8579 /* Parse the template-name. */
8580 is_identifier = false;
8581 template = cp_parser_template_name (parser, template_keyword_p,
8585 if (template == error_mark_node || is_identifier)
8587 pop_deferring_access_checks ();
8591 /* If we find the sequence `[:' after a template-name, it's probably
8592 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8593 parse correctly the argument list. */
8594 next_token = cp_lexer_peek_token (parser->lexer);
8595 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8596 if (next_token->type == CPP_OPEN_SQUARE
8597 && next_token->flags & DIGRAPH
8598 && next_token_2->type == CPP_COLON
8599 && !(next_token_2->flags & PREV_WHITE))
8601 cp_parser_parse_tentatively (parser);
8602 /* Change `:' into `::'. */
8603 next_token_2->type = CPP_SCOPE;
8604 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8606 cp_lexer_consume_token (parser->lexer);
8607 /* Parse the arguments. */
8608 arguments = cp_parser_enclosed_template_argument_list (parser);
8609 if (!cp_parser_parse_definitely (parser))
8611 /* If we couldn't parse an argument list, then we revert our changes
8612 and return simply an error. Maybe this is not a template-id
8614 next_token_2->type = CPP_COLON;
8615 cp_parser_error (parser, "expected %<<%>");
8616 pop_deferring_access_checks ();
8617 return error_mark_node;
8619 /* Otherwise, emit an error about the invalid digraph, but continue
8620 parsing because we got our argument list. */
8621 pedwarn ("%<<::%> cannot begin a template-argument list");
8622 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8623 "between %<<%> and %<::%>");
8624 if (!flag_permissive)
8629 inform ("(if you use -fpermissive G++ will accept your code)");
8636 /* Look for the `<' that starts the template-argument-list. */
8637 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8639 pop_deferring_access_checks ();
8640 return error_mark_node;
8642 /* Parse the arguments. */
8643 arguments = cp_parser_enclosed_template_argument_list (parser);
8646 /* Build a representation of the specialization. */
8647 if (TREE_CODE (template) == IDENTIFIER_NODE)
8648 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8649 else if (DECL_CLASS_TEMPLATE_P (template)
8650 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8652 = finish_template_type (template, arguments,
8653 cp_lexer_next_token_is (parser->lexer,
8657 /* If it's not a class-template or a template-template, it should be
8658 a function-template. */
8659 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8660 || TREE_CODE (template) == OVERLOAD
8661 || BASELINK_P (template)));
8663 template_id = lookup_template_function (template, arguments);
8666 /* Retrieve any deferred checks. Do not pop this access checks yet
8667 so the memory will not be reclaimed during token replacing below. */
8668 access_check = get_deferred_access_checks ();
8670 /* If parsing tentatively, replace the sequence of tokens that makes
8671 up the template-id with a CPP_TEMPLATE_ID token. That way,
8672 should we re-parse the token stream, we will not have to repeat
8673 the effort required to do the parse, nor will we issue duplicate
8674 error messages about problems during instantiation of the
8678 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8680 /* Reset the contents of the START_OF_ID token. */
8681 token->type = CPP_TEMPLATE_ID;
8682 token->value = build_tree_list (access_check, template_id);
8683 token->keyword = RID_MAX;
8685 /* Purge all subsequent tokens. */
8686 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8688 /* ??? Can we actually assume that, if template_id ==
8689 error_mark_node, we will have issued a diagnostic to the
8690 user, as opposed to simply marking the tentative parse as
8692 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8693 error ("parse error in template argument list");
8696 pop_deferring_access_checks ();
8700 /* Parse a template-name.
8705 The standard should actually say:
8709 operator-function-id
8711 A defect report has been filed about this issue.
8713 A conversion-function-id cannot be a template name because they cannot
8714 be part of a template-id. In fact, looking at this code:
8718 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8719 It is impossible to call a templated conversion-function-id with an
8720 explicit argument list, since the only allowed template parameter is
8721 the type to which it is converting.
8723 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8724 `template' keyword, in a construction like:
8728 In that case `f' is taken to be a template-name, even though there
8729 is no way of knowing for sure.
8731 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8732 name refers to a set of overloaded functions, at least one of which
8733 is a template, or an IDENTIFIER_NODE with the name of the template,
8734 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8735 names are looked up inside uninstantiated templates. */
8738 cp_parser_template_name (cp_parser* parser,
8739 bool template_keyword_p,
8740 bool check_dependency_p,
8741 bool is_declaration,
8742 bool *is_identifier)
8748 /* If the next token is `operator', then we have either an
8749 operator-function-id or a conversion-function-id. */
8750 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8752 /* We don't know whether we're looking at an
8753 operator-function-id or a conversion-function-id. */
8754 cp_parser_parse_tentatively (parser);
8755 /* Try an operator-function-id. */
8756 identifier = cp_parser_operator_function_id (parser);
8757 /* If that didn't work, try a conversion-function-id. */
8758 if (!cp_parser_parse_definitely (parser))
8760 cp_parser_error (parser, "expected template-name");
8761 return error_mark_node;
8764 /* Look for the identifier. */
8766 identifier = cp_parser_identifier (parser);
8768 /* If we didn't find an identifier, we don't have a template-id. */
8769 if (identifier == error_mark_node)
8770 return error_mark_node;
8772 /* If the name immediately followed the `template' keyword, then it
8773 is a template-name. However, if the next token is not `<', then
8774 we do not treat it as a template-name, since it is not being used
8775 as part of a template-id. This enables us to handle constructs
8778 template <typename T> struct S { S(); };
8779 template <typename T> S<T>::S();
8781 correctly. We would treat `S' as a template -- if it were `S<T>'
8782 -- but we do not if there is no `<'. */
8784 if (processing_template_decl
8785 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8787 /* In a declaration, in a dependent context, we pretend that the
8788 "template" keyword was present in order to improve error
8789 recovery. For example, given:
8791 template <typename T> void f(T::X<int>);
8793 we want to treat "X<int>" as a template-id. */
8795 && !template_keyword_p
8796 && parser->scope && TYPE_P (parser->scope)
8797 && check_dependency_p
8798 && dependent_type_p (parser->scope)
8799 /* Do not do this for dtors (or ctors), since they never
8800 need the template keyword before their name. */
8801 && !constructor_name_p (identifier, parser->scope))
8803 cp_token_position start = 0;
8805 /* Explain what went wrong. */
8806 error ("non-template %qD used as template", identifier);
8807 inform ("use %<%T::template %D%> to indicate that it is a template",
8808 parser->scope, identifier);
8809 /* If parsing tentatively, find the location of the "<" token. */
8810 if (cp_parser_simulate_error (parser))
8811 start = cp_lexer_token_position (parser->lexer, true);
8812 /* Parse the template arguments so that we can issue error
8813 messages about them. */
8814 cp_lexer_consume_token (parser->lexer);
8815 cp_parser_enclosed_template_argument_list (parser);
8816 /* Skip tokens until we find a good place from which to
8817 continue parsing. */
8818 cp_parser_skip_to_closing_parenthesis (parser,
8819 /*recovering=*/true,
8821 /*consume_paren=*/false);
8822 /* If parsing tentatively, permanently remove the
8823 template argument list. That will prevent duplicate
8824 error messages from being issued about the missing
8825 "template" keyword. */
8827 cp_lexer_purge_tokens_after (parser->lexer, start);
8829 *is_identifier = true;
8833 /* If the "template" keyword is present, then there is generally
8834 no point in doing name-lookup, so we just return IDENTIFIER.
8835 But, if the qualifying scope is non-dependent then we can
8836 (and must) do name-lookup normally. */
8837 if (template_keyword_p
8839 || (TYPE_P (parser->scope)
8840 && dependent_type_p (parser->scope))))
8844 /* Look up the name. */
8845 decl = cp_parser_lookup_name (parser, identifier,
8847 /*is_template=*/false,
8848 /*is_namespace=*/false,
8850 /*ambiguous_p=*/NULL);
8851 decl = maybe_get_template_decl_from_type_decl (decl);
8853 /* If DECL is a template, then the name was a template-name. */
8854 if (TREE_CODE (decl) == TEMPLATE_DECL)
8858 tree fn = NULL_TREE;
8860 /* The standard does not explicitly indicate whether a name that
8861 names a set of overloaded declarations, some of which are
8862 templates, is a template-name. However, such a name should
8863 be a template-name; otherwise, there is no way to form a
8864 template-id for the overloaded templates. */
8865 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8866 if (TREE_CODE (fns) == OVERLOAD)
8867 for (fn = fns; fn; fn = OVL_NEXT (fn))
8868 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8873 /* The name does not name a template. */
8874 cp_parser_error (parser, "expected template-name");
8875 return error_mark_node;
8879 /* If DECL is dependent, and refers to a function, then just return
8880 its name; we will look it up again during template instantiation. */
8881 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8883 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8884 if (TYPE_P (scope) && dependent_type_p (scope))
8891 /* Parse a template-argument-list.
8893 template-argument-list:
8895 template-argument-list , template-argument
8897 Returns a TREE_VEC containing the arguments. */
8900 cp_parser_template_argument_list (cp_parser* parser)
8902 tree fixed_args[10];
8903 unsigned n_args = 0;
8904 unsigned alloced = 10;
8905 tree *arg_ary = fixed_args;
8907 bool saved_in_template_argument_list_p;
8909 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8910 parser->in_template_argument_list_p = true;
8916 /* Consume the comma. */
8917 cp_lexer_consume_token (parser->lexer);
8919 /* Parse the template-argument. */
8920 argument = cp_parser_template_argument (parser);
8921 if (n_args == alloced)
8925 if (arg_ary == fixed_args)
8927 arg_ary = xmalloc (sizeof (tree) * alloced);
8928 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8931 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8933 arg_ary[n_args++] = argument;
8935 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8937 vec = make_tree_vec (n_args);
8940 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8942 if (arg_ary != fixed_args)
8944 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8948 /* Parse a template-argument.
8951 assignment-expression
8955 The representation is that of an assignment-expression, type-id, or
8956 id-expression -- except that the qualified id-expression is
8957 evaluated, so that the value returned is either a DECL or an
8960 Although the standard says "assignment-expression", it forbids
8961 throw-expressions or assignments in the template argument.
8962 Therefore, we use "conditional-expression" instead. */
8965 cp_parser_template_argument (cp_parser* parser)
8970 bool maybe_type_id = false;
8973 tree qualifying_class;
8975 /* There's really no way to know what we're looking at, so we just
8976 try each alternative in order.
8980 In a template-argument, an ambiguity between a type-id and an
8981 expression is resolved to a type-id, regardless of the form of
8982 the corresponding template-parameter.
8984 Therefore, we try a type-id first. */
8985 cp_parser_parse_tentatively (parser);
8986 argument = cp_parser_type_id (parser);
8987 /* If there was no error parsing the type-id but the next token is a '>>',
8988 we probably found a typo for '> >'. But there are type-id which are
8989 also valid expressions. For instance:
8991 struct X { int operator >> (int); };
8992 template <int V> struct Foo {};
8995 Here 'X()' is a valid type-id of a function type, but the user just
8996 wanted to write the expression "X() >> 5". Thus, we remember that we
8997 found a valid type-id, but we still try to parse the argument as an
8998 expression to see what happens. */
8999 if (!cp_parser_error_occurred (parser)
9000 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9002 maybe_type_id = true;
9003 cp_parser_abort_tentative_parse (parser);
9007 /* If the next token isn't a `,' or a `>', then this argument wasn't
9008 really finished. This means that the argument is not a valid
9010 if (!cp_parser_next_token_ends_template_argument_p (parser))
9011 cp_parser_error (parser, "expected template-argument");
9012 /* If that worked, we're done. */
9013 if (cp_parser_parse_definitely (parser))
9016 /* We're still not sure what the argument will be. */
9017 cp_parser_parse_tentatively (parser);
9018 /* Try a template. */
9019 argument = cp_parser_id_expression (parser,
9020 /*template_keyword_p=*/false,
9021 /*check_dependency_p=*/true,
9023 /*declarator_p=*/false);
9024 /* If the next token isn't a `,' or a `>', then this argument wasn't
9026 if (!cp_parser_next_token_ends_template_argument_p (parser))
9027 cp_parser_error (parser, "expected template-argument");
9028 if (!cp_parser_error_occurred (parser))
9030 /* Figure out what is being referred to. If the id-expression
9031 was for a class template specialization, then we will have a
9032 TYPE_DECL at this point. There is no need to do name lookup
9033 at this point in that case. */
9034 if (TREE_CODE (argument) != TYPE_DECL)
9035 argument = cp_parser_lookup_name (parser, argument,
9037 /*is_template=*/template_p,
9038 /*is_namespace=*/false,
9039 /*check_dependency=*/true,
9040 /*ambiguous_p=*/NULL);
9041 if (TREE_CODE (argument) != TEMPLATE_DECL
9042 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9043 cp_parser_error (parser, "expected template-name");
9045 if (cp_parser_parse_definitely (parser))
9047 /* It must be a non-type argument. There permitted cases are given
9048 in [temp.arg.nontype]:
9050 -- an integral constant-expression of integral or enumeration
9053 -- the name of a non-type template-parameter; or
9055 -- the name of an object or function with external linkage...
9057 -- the address of an object or function with external linkage...
9059 -- a pointer to member... */
9060 /* Look for a non-type template parameter. */
9061 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9063 cp_parser_parse_tentatively (parser);
9064 argument = cp_parser_primary_expression (parser,
9068 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9069 || !cp_parser_next_token_ends_template_argument_p (parser))
9070 cp_parser_simulate_error (parser);
9071 if (cp_parser_parse_definitely (parser))
9075 /* If the next token is "&", the argument must be the address of an
9076 object or function with external linkage. */
9077 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9079 cp_lexer_consume_token (parser->lexer);
9080 /* See if we might have an id-expression. */
9081 token = cp_lexer_peek_token (parser->lexer);
9082 if (token->type == CPP_NAME
9083 || token->keyword == RID_OPERATOR
9084 || token->type == CPP_SCOPE
9085 || token->type == CPP_TEMPLATE_ID
9086 || token->type == CPP_NESTED_NAME_SPECIFIER)
9088 cp_parser_parse_tentatively (parser);
9089 argument = cp_parser_primary_expression (parser,
9093 if (cp_parser_error_occurred (parser)
9094 || !cp_parser_next_token_ends_template_argument_p (parser))
9095 cp_parser_abort_tentative_parse (parser);
9098 if (TREE_CODE (argument) == INDIRECT_REF)
9100 gcc_assert (REFERENCE_REF_P (argument));
9101 argument = TREE_OPERAND (argument, 0);
9104 if (qualifying_class)
9105 argument = finish_qualified_id_expr (qualifying_class,
9109 if (TREE_CODE (argument) == VAR_DECL)
9111 /* A variable without external linkage might still be a
9112 valid constant-expression, so no error is issued here
9113 if the external-linkage check fails. */
9114 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9115 cp_parser_simulate_error (parser);
9117 else if (is_overloaded_fn (argument))
9118 /* All overloaded functions are allowed; if the external
9119 linkage test does not pass, an error will be issued
9123 && (TREE_CODE (argument) == OFFSET_REF
9124 || TREE_CODE (argument) == SCOPE_REF))
9125 /* A pointer-to-member. */
9127 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9130 cp_parser_simulate_error (parser);
9132 if (cp_parser_parse_definitely (parser))
9135 argument = build_x_unary_op (ADDR_EXPR, argument);
9140 /* If the argument started with "&", there are no other valid
9141 alternatives at this point. */
9144 cp_parser_error (parser, "invalid non-type template argument");
9145 return error_mark_node;
9148 /* If the argument wasn't successfully parsed as a type-id followed
9149 by '>>', the argument can only be a constant expression now.
9150 Otherwise, we try parsing the constant-expression tentatively,
9151 because the argument could really be a type-id. */
9153 cp_parser_parse_tentatively (parser);
9154 argument = cp_parser_constant_expression (parser,
9155 /*allow_non_constant_p=*/false,
9156 /*non_constant_p=*/NULL);
9157 argument = fold_non_dependent_expr (argument);
9160 if (!cp_parser_next_token_ends_template_argument_p (parser))
9161 cp_parser_error (parser, "expected template-argument");
9162 if (cp_parser_parse_definitely (parser))
9164 /* We did our best to parse the argument as a non type-id, but that
9165 was the only alternative that matched (albeit with a '>' after
9166 it). We can assume it's just a typo from the user, and a
9167 diagnostic will then be issued. */
9168 return cp_parser_type_id (parser);
9171 /* Parse an explicit-instantiation.
9173 explicit-instantiation:
9174 template declaration
9176 Although the standard says `declaration', what it really means is:
9178 explicit-instantiation:
9179 template decl-specifier-seq [opt] declarator [opt] ;
9181 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9182 supposed to be allowed. A defect report has been filed about this
9187 explicit-instantiation:
9188 storage-class-specifier template
9189 decl-specifier-seq [opt] declarator [opt] ;
9190 function-specifier template
9191 decl-specifier-seq [opt] declarator [opt] ; */
9194 cp_parser_explicit_instantiation (cp_parser* parser)
9196 int declares_class_or_enum;
9197 cp_decl_specifier_seq decl_specifiers;
9198 tree extension_specifier = NULL_TREE;
9200 /* Look for an (optional) storage-class-specifier or
9201 function-specifier. */
9202 if (cp_parser_allow_gnu_extensions_p (parser))
9205 = cp_parser_storage_class_specifier_opt (parser);
9206 if (!extension_specifier)
9208 = cp_parser_function_specifier_opt (parser,
9209 /*decl_specs=*/NULL);
9212 /* Look for the `template' keyword. */
9213 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9214 /* Let the front end know that we are processing an explicit
9216 begin_explicit_instantiation ();
9217 /* [temp.explicit] says that we are supposed to ignore access
9218 control while processing explicit instantiation directives. */
9219 push_deferring_access_checks (dk_no_check);
9220 /* Parse a decl-specifier-seq. */
9221 cp_parser_decl_specifier_seq (parser,
9222 CP_PARSER_FLAGS_OPTIONAL,
9224 &declares_class_or_enum);
9225 /* If there was exactly one decl-specifier, and it declared a class,
9226 and there's no declarator, then we have an explicit type
9228 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9232 type = check_tag_decl (&decl_specifiers);
9233 /* Turn access control back on for names used during
9234 template instantiation. */
9235 pop_deferring_access_checks ();
9237 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9241 cp_declarator *declarator;
9244 /* Parse the declarator. */
9246 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9247 /*ctor_dtor_or_conv_p=*/NULL,
9248 /*parenthesized_p=*/NULL,
9249 /*member_p=*/false);
9250 if (declares_class_or_enum & 2)
9251 cp_parser_check_for_definition_in_return_type (declarator,
9252 decl_specifiers.type);
9253 if (declarator != cp_error_declarator)
9255 decl = grokdeclarator (declarator, &decl_specifiers,
9257 /* Turn access control back on for names used during
9258 template instantiation. */
9259 pop_deferring_access_checks ();
9260 /* Do the explicit instantiation. */
9261 do_decl_instantiation (decl, extension_specifier);
9265 pop_deferring_access_checks ();
9266 /* Skip the body of the explicit instantiation. */
9267 cp_parser_skip_to_end_of_statement (parser);
9270 /* We're done with the instantiation. */
9271 end_explicit_instantiation ();
9273 cp_parser_consume_semicolon_at_end_of_statement (parser);
9276 /* Parse an explicit-specialization.
9278 explicit-specialization:
9279 template < > declaration
9281 Although the standard says `declaration', what it really means is:
9283 explicit-specialization:
9284 template <> decl-specifier [opt] init-declarator [opt] ;
9285 template <> function-definition
9286 template <> explicit-specialization
9287 template <> template-declaration */
9290 cp_parser_explicit_specialization (cp_parser* parser)
9292 /* Look for the `template' keyword. */
9293 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9294 /* Look for the `<'. */
9295 cp_parser_require (parser, CPP_LESS, "`<'");
9296 /* Look for the `>'. */
9297 cp_parser_require (parser, CPP_GREATER, "`>'");
9298 /* We have processed another parameter list. */
9299 ++parser->num_template_parameter_lists;
9300 /* Let the front end know that we are beginning a specialization. */
9301 begin_specialization ();
9303 /* If the next keyword is `template', we need to figure out whether
9304 or not we're looking a template-declaration. */
9305 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9307 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9308 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9309 cp_parser_template_declaration_after_export (parser,
9310 /*member_p=*/false);
9312 cp_parser_explicit_specialization (parser);
9315 /* Parse the dependent declaration. */
9316 cp_parser_single_declaration (parser,
9320 /* We're done with the specialization. */
9321 end_specialization ();
9322 /* We're done with this parameter list. */
9323 --parser->num_template_parameter_lists;
9326 /* Parse a type-specifier.
9329 simple-type-specifier
9332 elaborated-type-specifier
9340 Returns a representation of the type-specifier. For a
9341 class-specifier, enum-specifier, or elaborated-type-specifier, a
9342 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9344 The parser flags FLAGS is used to control type-specifier parsing.
9346 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9347 in a decl-specifier-seq.
9349 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9350 class-specifier, enum-specifier, or elaborated-type-specifier, then
9351 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9352 if a type is declared; 2 if it is defined. Otherwise, it is set to
9355 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9356 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9360 cp_parser_type_specifier (cp_parser* parser,
9361 cp_parser_flags flags,
9362 cp_decl_specifier_seq *decl_specs,
9363 bool is_declaration,
9364 int* declares_class_or_enum,
9365 bool* is_cv_qualifier)
9367 tree type_spec = NULL_TREE;
9370 cp_decl_spec ds = ds_last;
9372 /* Assume this type-specifier does not declare a new type. */
9373 if (declares_class_or_enum)
9374 *declares_class_or_enum = 0;
9375 /* And that it does not specify a cv-qualifier. */
9376 if (is_cv_qualifier)
9377 *is_cv_qualifier = false;
9378 /* Peek at the next token. */
9379 token = cp_lexer_peek_token (parser->lexer);
9381 /* If we're looking at a keyword, we can use that to guide the
9382 production we choose. */
9383 keyword = token->keyword;
9387 /* 'enum' [identifier] '{' introduces an enum-specifier;
9388 'enum' <anything else> introduces an elaborated-type-specifier. */
9389 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9390 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9391 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9394 if (parser->num_template_parameter_lists)
9396 error ("template declaration of %qs", "enum");
9397 cp_parser_skip_to_end_of_block_or_statement (parser);
9398 type_spec = error_mark_node;
9401 type_spec = cp_parser_enum_specifier (parser);
9403 if (declares_class_or_enum)
9404 *declares_class_or_enum = 2;
9406 cp_parser_set_decl_spec_type (decl_specs,
9408 /*user_defined_p=*/true);
9412 goto elaborated_type_specifier;
9414 /* Any of these indicate either a class-specifier, or an
9415 elaborated-type-specifier. */
9419 /* Parse tentatively so that we can back up if we don't find a
9421 cp_parser_parse_tentatively (parser);
9422 /* Look for the class-specifier. */
9423 type_spec = cp_parser_class_specifier (parser);
9424 /* If that worked, we're done. */
9425 if (cp_parser_parse_definitely (parser))
9427 if (declares_class_or_enum)
9428 *declares_class_or_enum = 2;
9430 cp_parser_set_decl_spec_type (decl_specs,
9432 /*user_defined_p=*/true);
9437 elaborated_type_specifier:
9438 /* We're declaring (not defining) a class or enum. */
9439 if (declares_class_or_enum)
9440 *declares_class_or_enum = 1;
9444 /* Look for an elaborated-type-specifier. */
9446 = (cp_parser_elaborated_type_specifier
9448 decl_specs && decl_specs->specs[(int) ds_friend],
9451 cp_parser_set_decl_spec_type (decl_specs,
9453 /*user_defined_p=*/true);
9458 if (is_cv_qualifier)
9459 *is_cv_qualifier = true;
9464 if (is_cv_qualifier)
9465 *is_cv_qualifier = true;
9470 if (is_cv_qualifier)
9471 *is_cv_qualifier = true;
9475 /* The `__complex__' keyword is a GNU extension. */
9483 /* Handle simple keywords. */
9488 ++decl_specs->specs[(int)ds];
9489 decl_specs->any_specifiers_p = true;
9491 return cp_lexer_consume_token (parser->lexer)->value;
9494 /* If we do not already have a type-specifier, assume we are looking
9495 at a simple-type-specifier. */
9496 type_spec = cp_parser_simple_type_specifier (parser,
9500 /* If we didn't find a type-specifier, and a type-specifier was not
9501 optional in this context, issue an error message. */
9502 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9504 cp_parser_error (parser, "expected type specifier");
9505 return error_mark_node;
9511 /* Parse a simple-type-specifier.
9513 simple-type-specifier:
9514 :: [opt] nested-name-specifier [opt] type-name
9515 :: [opt] nested-name-specifier template template-id
9530 simple-type-specifier:
9531 __typeof__ unary-expression
9532 __typeof__ ( type-id )
9534 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9535 appropriately updated. */
9538 cp_parser_simple_type_specifier (cp_parser* parser,
9539 cp_decl_specifier_seq *decl_specs,
9540 cp_parser_flags flags)
9542 tree type = NULL_TREE;
9545 /* Peek at the next token. */
9546 token = cp_lexer_peek_token (parser->lexer);
9548 /* If we're looking at a keyword, things are easy. */
9549 switch (token->keyword)
9553 decl_specs->explicit_char_p = true;
9554 type = char_type_node;
9557 type = wchar_type_node;
9560 type = boolean_type_node;
9564 ++decl_specs->specs[(int) ds_short];
9565 type = short_integer_type_node;
9569 decl_specs->explicit_int_p = true;
9570 type = integer_type_node;
9574 ++decl_specs->specs[(int) ds_long];
9575 type = long_integer_type_node;
9579 ++decl_specs->specs[(int) ds_signed];
9580 type = integer_type_node;
9584 ++decl_specs->specs[(int) ds_unsigned];
9585 type = unsigned_type_node;
9588 type = float_type_node;
9591 type = double_type_node;
9594 type = void_type_node;
9598 /* Consume the `typeof' token. */
9599 cp_lexer_consume_token (parser->lexer);
9600 /* Parse the operand to `typeof'. */
9601 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9602 /* If it is not already a TYPE, take its type. */
9604 type = finish_typeof (type);
9607 cp_parser_set_decl_spec_type (decl_specs, type,
9608 /*user_defined_p=*/true);
9616 /* If the type-specifier was for a built-in type, we're done. */
9621 /* Record the type. */
9623 && (token->keyword != RID_SIGNED
9624 && token->keyword != RID_UNSIGNED
9625 && token->keyword != RID_SHORT
9626 && token->keyword != RID_LONG))
9627 cp_parser_set_decl_spec_type (decl_specs,
9629 /*user_defined=*/false);
9631 decl_specs->any_specifiers_p = true;
9633 /* Consume the token. */
9634 id = cp_lexer_consume_token (parser->lexer)->value;
9636 /* There is no valid C++ program where a non-template type is
9637 followed by a "<". That usually indicates that the user thought
9638 that the type was a template. */
9639 cp_parser_check_for_invalid_template_id (parser, type);
9641 return TYPE_NAME (type);
9644 /* The type-specifier must be a user-defined type. */
9645 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9650 /* Don't gobble tokens or issue error messages if this is an
9651 optional type-specifier. */
9652 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9653 cp_parser_parse_tentatively (parser);
9655 /* Look for the optional `::' operator. */
9657 = (cp_parser_global_scope_opt (parser,
9658 /*current_scope_valid_p=*/false)
9660 /* Look for the nested-name specifier. */
9662 = (cp_parser_nested_name_specifier_opt (parser,
9663 /*typename_keyword_p=*/false,
9664 /*check_dependency_p=*/true,
9666 /*is_declaration=*/false)
9668 /* If we have seen a nested-name-specifier, and the next token
9669 is `template', then we are using the template-id production. */
9671 && cp_parser_optional_template_keyword (parser))
9673 /* Look for the template-id. */
9674 type = cp_parser_template_id (parser,
9675 /*template_keyword_p=*/true,
9676 /*check_dependency_p=*/true,
9677 /*is_declaration=*/false);
9678 /* If the template-id did not name a type, we are out of
9680 if (TREE_CODE (type) != TYPE_DECL)
9682 cp_parser_error (parser, "expected template-id for type");
9686 /* Otherwise, look for a type-name. */
9688 type = cp_parser_type_name (parser);
9689 /* Keep track of all name-lookups performed in class scopes. */
9693 && TREE_CODE (type) == TYPE_DECL
9694 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9695 maybe_note_name_used_in_class (DECL_NAME (type), type);
9696 /* If it didn't work out, we don't have a TYPE. */
9697 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9698 && !cp_parser_parse_definitely (parser))
9700 if (type && decl_specs)
9701 cp_parser_set_decl_spec_type (decl_specs, type,
9702 /*user_defined=*/true);
9705 /* If we didn't get a type-name, issue an error message. */
9706 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9708 cp_parser_error (parser, "expected type-name");
9709 return error_mark_node;
9712 /* There is no valid C++ program where a non-template type is
9713 followed by a "<". That usually indicates that the user thought
9714 that the type was a template. */
9715 if (type && type != error_mark_node)
9717 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9718 If it is, then the '<'...'>' enclose protocol names rather than
9719 template arguments, and so everything is fine. */
9720 if (c_dialect_objc ()
9721 && (objc_is_id (type) || objc_is_class_name (type)))
9723 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9724 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9726 /* Clobber the "unqualified" type previously entered into
9727 DECL_SPECS with the new, improved protocol-qualified version. */
9729 decl_specs->type = qual_type;
9734 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9740 /* Parse a type-name.
9753 Returns a TYPE_DECL for the type. */
9756 cp_parser_type_name (cp_parser* parser)
9761 /* We can't know yet whether it is a class-name or not. */
9762 cp_parser_parse_tentatively (parser);
9763 /* Try a class-name. */
9764 type_decl = cp_parser_class_name (parser,
9765 /*typename_keyword_p=*/false,
9766 /*template_keyword_p=*/false,
9768 /*check_dependency_p=*/true,
9769 /*class_head_p=*/false,
9770 /*is_declaration=*/false);
9771 /* If it's not a class-name, keep looking. */
9772 if (!cp_parser_parse_definitely (parser))
9774 /* It must be a typedef-name or an enum-name. */
9775 identifier = cp_parser_identifier (parser);
9776 if (identifier == error_mark_node)
9777 return error_mark_node;
9779 /* Look up the type-name. */
9780 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9782 if (TREE_CODE (type_decl) != TYPE_DECL
9783 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9785 /* See if this is an Objective-C type. */
9786 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9787 tree type = objc_get_protocol_qualified_type (identifier, protos);
9789 type_decl = TYPE_NAME (type);
9792 /* Issue an error if we did not find a type-name. */
9793 if (TREE_CODE (type_decl) != TYPE_DECL)
9795 if (!cp_parser_simulate_error (parser))
9796 cp_parser_name_lookup_error (parser, identifier, type_decl,
9798 type_decl = error_mark_node;
9800 /* Remember that the name was used in the definition of the
9801 current class so that we can check later to see if the
9802 meaning would have been different after the class was
9803 entirely defined. */
9804 else if (type_decl != error_mark_node
9806 maybe_note_name_used_in_class (identifier, type_decl);
9813 /* Parse an elaborated-type-specifier. Note that the grammar given
9814 here incorporates the resolution to DR68.
9816 elaborated-type-specifier:
9817 class-key :: [opt] nested-name-specifier [opt] identifier
9818 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9819 enum :: [opt] nested-name-specifier [opt] identifier
9820 typename :: [opt] nested-name-specifier identifier
9821 typename :: [opt] nested-name-specifier template [opt]
9826 elaborated-type-specifier:
9827 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9828 class-key attributes :: [opt] nested-name-specifier [opt]
9829 template [opt] template-id
9830 enum attributes :: [opt] nested-name-specifier [opt] identifier
9832 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9833 declared `friend'. If IS_DECLARATION is TRUE, then this
9834 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9835 something is being declared.
9837 Returns the TYPE specified. */
9840 cp_parser_elaborated_type_specifier (cp_parser* parser,
9842 bool is_declaration)
9844 enum tag_types tag_type;
9846 tree type = NULL_TREE;
9847 tree attributes = NULL_TREE;
9849 /* See if we're looking at the `enum' keyword. */
9850 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9852 /* Consume the `enum' token. */
9853 cp_lexer_consume_token (parser->lexer);
9854 /* Remember that it's an enumeration type. */
9855 tag_type = enum_type;
9856 /* Parse the attributes. */
9857 attributes = cp_parser_attributes_opt (parser);
9859 /* Or, it might be `typename'. */
9860 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9863 /* Consume the `typename' token. */
9864 cp_lexer_consume_token (parser->lexer);
9865 /* Remember that it's a `typename' type. */
9866 tag_type = typename_type;
9867 /* The `typename' keyword is only allowed in templates. */
9868 if (!processing_template_decl)
9869 pedwarn ("using %<typename%> outside of template");
9871 /* Otherwise it must be a class-key. */
9874 tag_type = cp_parser_class_key (parser);
9875 if (tag_type == none_type)
9876 return error_mark_node;
9877 /* Parse the attributes. */
9878 attributes = cp_parser_attributes_opt (parser);
9881 /* Look for the `::' operator. */
9882 cp_parser_global_scope_opt (parser,
9883 /*current_scope_valid_p=*/false);
9884 /* Look for the nested-name-specifier. */
9885 if (tag_type == typename_type)
9887 if (!cp_parser_nested_name_specifier (parser,
9888 /*typename_keyword_p=*/true,
9889 /*check_dependency_p=*/true,
9892 return error_mark_node;
9895 /* Even though `typename' is not present, the proposed resolution
9896 to Core Issue 180 says that in `class A<T>::B', `B' should be
9897 considered a type-name, even if `A<T>' is dependent. */
9898 cp_parser_nested_name_specifier_opt (parser,
9899 /*typename_keyword_p=*/true,
9900 /*check_dependency_p=*/true,
9903 /* For everything but enumeration types, consider a template-id. */
9904 if (tag_type != enum_type)
9906 bool template_p = false;
9909 /* Allow the `template' keyword. */
9910 template_p = cp_parser_optional_template_keyword (parser);
9911 /* If we didn't see `template', we don't know if there's a
9912 template-id or not. */
9914 cp_parser_parse_tentatively (parser);
9915 /* Parse the template-id. */
9916 decl = cp_parser_template_id (parser, template_p,
9917 /*check_dependency_p=*/true,
9919 /* If we didn't find a template-id, look for an ordinary
9921 if (!template_p && !cp_parser_parse_definitely (parser))
9923 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9924 in effect, then we must assume that, upon instantiation, the
9925 template will correspond to a class. */
9926 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9927 && tag_type == typename_type)
9928 type = make_typename_type (parser->scope, decl,
9932 type = TREE_TYPE (decl);
9935 /* For an enumeration type, consider only a plain identifier. */
9938 identifier = cp_parser_identifier (parser);
9940 if (identifier == error_mark_node)
9942 parser->scope = NULL_TREE;
9943 return error_mark_node;
9946 /* For a `typename', we needn't call xref_tag. */
9947 if (tag_type == typename_type
9948 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9949 return cp_parser_make_typename_type (parser, parser->scope,
9951 /* Look up a qualified name in the usual way. */
9956 decl = cp_parser_lookup_name (parser, identifier,
9958 /*is_template=*/false,
9959 /*is_namespace=*/false,
9960 /*check_dependency=*/true,
9961 /*ambiguous_p=*/NULL);
9963 /* If we are parsing friend declaration, DECL may be a
9964 TEMPLATE_DECL tree node here. However, we need to check
9965 whether this TEMPLATE_DECL results in valid code. Consider
9966 the following example:
9969 template <class T> class C {};
9972 template <class T> friend class N::C; // #1, valid code
9974 template <class T> class Y {
9975 friend class N::C; // #2, invalid code
9978 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9979 name lookup of `N::C'. We see that friend declaration must
9980 be template for the code to be valid. Note that
9981 processing_template_decl does not work here since it is
9982 always 1 for the above two cases. */
9984 decl = (cp_parser_maybe_treat_template_as_class
9985 (decl, /*tag_name_p=*/is_friend
9986 && parser->num_template_parameter_lists));
9988 if (TREE_CODE (decl) != TYPE_DECL)
9990 cp_parser_diagnose_invalid_type_name (parser,
9993 return error_mark_node;
9996 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9997 check_elaborated_type_specifier
9999 (parser->num_template_parameter_lists
10000 || DECL_SELF_REFERENCE_P (decl)));
10002 type = TREE_TYPE (decl);
10006 /* An elaborated-type-specifier sometimes introduces a new type and
10007 sometimes names an existing type. Normally, the rule is that it
10008 introduces a new type only if there is not an existing type of
10009 the same name already in scope. For example, given:
10012 void f() { struct S s; }
10014 the `struct S' in the body of `f' is the same `struct S' as in
10015 the global scope; the existing definition is used. However, if
10016 there were no global declaration, this would introduce a new
10017 local class named `S'.
10019 An exception to this rule applies to the following code:
10021 namespace N { struct S; }
10023 Here, the elaborated-type-specifier names a new type
10024 unconditionally; even if there is already an `S' in the
10025 containing scope this declaration names a new type.
10026 This exception only applies if the elaborated-type-specifier
10027 forms the complete declaration:
10031 A declaration consisting solely of `class-key identifier ;' is
10032 either a redeclaration of the name in the current scope or a
10033 forward declaration of the identifier as a class name. It
10034 introduces the name into the current scope.
10036 We are in this situation precisely when the next token is a `;'.
10038 An exception to the exception is that a `friend' declaration does
10039 *not* name a new type; i.e., given:
10041 struct S { friend struct T; };
10043 `T' is not a new type in the scope of `S'.
10045 Also, `new struct S' or `sizeof (struct S)' never results in the
10046 definition of a new type; a new type can only be declared in a
10047 declaration context. */
10051 /* Friends have special name lookup rules. */
10052 ts = ts_within_enclosing_non_class;
10053 else if (is_declaration
10054 && cp_lexer_next_token_is (parser->lexer,
10056 /* This is a `class-key identifier ;' */
10061 /* Warn about attributes. They are ignored. */
10063 warning (OPT_Wattributes,
10064 "type attributes are honored only at type definition");
10066 type = xref_tag (tag_type, identifier, ts,
10067 parser->num_template_parameter_lists);
10070 if (tag_type != enum_type)
10071 cp_parser_check_class_key (tag_type, type);
10073 /* A "<" cannot follow an elaborated type specifier. If that
10074 happens, the user was probably trying to form a template-id. */
10075 cp_parser_check_for_invalid_template_id (parser, type);
10080 /* Parse an enum-specifier.
10083 enum identifier [opt] { enumerator-list [opt] }
10086 enum identifier [opt] { enumerator-list [opt] } attributes
10088 Returns an ENUM_TYPE representing the enumeration. */
10091 cp_parser_enum_specifier (cp_parser* parser)
10096 /* Caller guarantees that the current token is 'enum', an identifier
10097 possibly follows, and the token after that is an opening brace.
10098 If we don't have an identifier, fabricate an anonymous name for
10099 the enumeration being defined. */
10100 cp_lexer_consume_token (parser->lexer);
10102 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10103 identifier = cp_parser_identifier (parser);
10105 identifier = make_anon_name ();
10107 /* Issue an error message if type-definitions are forbidden here. */
10108 cp_parser_check_type_definition (parser);
10110 /* Create the new type. We do this before consuming the opening brace
10111 so the enum will be recorded as being on the line of its tag (or the
10112 'enum' keyword, if there is no tag). */
10113 type = start_enum (identifier);
10115 /* Consume the opening brace. */
10116 cp_lexer_consume_token (parser->lexer);
10118 /* If the next token is not '}', then there are some enumerators. */
10119 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10120 cp_parser_enumerator_list (parser, type);
10122 /* Consume the final '}'. */
10123 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10125 /* Look for trailing attributes to apply to this enumeration, and
10126 apply them if appropriate. */
10127 if (cp_parser_allow_gnu_extensions_p (parser))
10129 tree trailing_attr = cp_parser_attributes_opt (parser);
10130 cplus_decl_attributes (&type,
10132 (int) ATTR_FLAG_TYPE_IN_PLACE);
10135 /* Finish up the enumeration. */
10136 finish_enum (type);
10141 /* Parse an enumerator-list. The enumerators all have the indicated
10145 enumerator-definition
10146 enumerator-list , enumerator-definition */
10149 cp_parser_enumerator_list (cp_parser* parser, tree type)
10153 /* Parse an enumerator-definition. */
10154 cp_parser_enumerator_definition (parser, type);
10156 /* If the next token is not a ',', we've reached the end of
10158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10160 /* Otherwise, consume the `,' and keep going. */
10161 cp_lexer_consume_token (parser->lexer);
10162 /* If the next token is a `}', there is a trailing comma. */
10163 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10165 if (pedantic && !in_system_header)
10166 pedwarn ("comma at end of enumerator list");
10172 /* Parse an enumerator-definition. The enumerator has the indicated
10175 enumerator-definition:
10177 enumerator = constant-expression
10183 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10188 /* Look for the identifier. */
10189 identifier = cp_parser_identifier (parser);
10190 if (identifier == error_mark_node)
10193 /* If the next token is an '=', then there is an explicit value. */
10194 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10196 /* Consume the `=' token. */
10197 cp_lexer_consume_token (parser->lexer);
10198 /* Parse the value. */
10199 value = cp_parser_constant_expression (parser,
10200 /*allow_non_constant_p=*/false,
10206 /* Create the enumerator. */
10207 build_enumerator (identifier, value, type);
10210 /* Parse a namespace-name.
10213 original-namespace-name
10216 Returns the NAMESPACE_DECL for the namespace. */
10219 cp_parser_namespace_name (cp_parser* parser)
10222 tree namespace_decl;
10224 /* Get the name of the namespace. */
10225 identifier = cp_parser_identifier (parser);
10226 if (identifier == error_mark_node)
10227 return error_mark_node;
10229 /* Look up the identifier in the currently active scope. Look only
10230 for namespaces, due to:
10232 [basic.lookup.udir]
10234 When looking up a namespace-name in a using-directive or alias
10235 definition, only namespace names are considered.
10239 [basic.lookup.qual]
10241 During the lookup of a name preceding the :: scope resolution
10242 operator, object, function, and enumerator names are ignored.
10244 (Note that cp_parser_class_or_namespace_name only calls this
10245 function if the token after the name is the scope resolution
10247 namespace_decl = cp_parser_lookup_name (parser, identifier,
10249 /*is_template=*/false,
10250 /*is_namespace=*/true,
10251 /*check_dependency=*/true,
10252 /*ambiguous_p=*/NULL);
10253 /* If it's not a namespace, issue an error. */
10254 if (namespace_decl == error_mark_node
10255 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10257 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10258 error ("%qD is not a namespace-name", identifier);
10259 cp_parser_error (parser, "expected namespace-name");
10260 namespace_decl = error_mark_node;
10263 return namespace_decl;
10266 /* Parse a namespace-definition.
10268 namespace-definition:
10269 named-namespace-definition
10270 unnamed-namespace-definition
10272 named-namespace-definition:
10273 original-namespace-definition
10274 extension-namespace-definition
10276 original-namespace-definition:
10277 namespace identifier { namespace-body }
10279 extension-namespace-definition:
10280 namespace original-namespace-name { namespace-body }
10282 unnamed-namespace-definition:
10283 namespace { namespace-body } */
10286 cp_parser_namespace_definition (cp_parser* parser)
10290 /* Look for the `namespace' keyword. */
10291 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10293 /* Get the name of the namespace. We do not attempt to distinguish
10294 between an original-namespace-definition and an
10295 extension-namespace-definition at this point. The semantic
10296 analysis routines are responsible for that. */
10297 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10298 identifier = cp_parser_identifier (parser);
10300 identifier = NULL_TREE;
10302 /* Look for the `{' to start the namespace. */
10303 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10304 /* Start the namespace. */
10305 push_namespace (identifier);
10306 /* Parse the body of the namespace. */
10307 cp_parser_namespace_body (parser);
10308 /* Finish the namespace. */
10310 /* Look for the final `}'. */
10311 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10314 /* Parse a namespace-body.
10317 declaration-seq [opt] */
10320 cp_parser_namespace_body (cp_parser* parser)
10322 cp_parser_declaration_seq_opt (parser);
10325 /* Parse a namespace-alias-definition.
10327 namespace-alias-definition:
10328 namespace identifier = qualified-namespace-specifier ; */
10331 cp_parser_namespace_alias_definition (cp_parser* parser)
10334 tree namespace_specifier;
10336 /* Look for the `namespace' keyword. */
10337 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10338 /* Look for the identifier. */
10339 identifier = cp_parser_identifier (parser);
10340 if (identifier == error_mark_node)
10342 /* Look for the `=' token. */
10343 cp_parser_require (parser, CPP_EQ, "`='");
10344 /* Look for the qualified-namespace-specifier. */
10345 namespace_specifier
10346 = cp_parser_qualified_namespace_specifier (parser);
10347 /* Look for the `;' token. */
10348 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10350 /* Register the alias in the symbol table. */
10351 do_namespace_alias (identifier, namespace_specifier);
10354 /* Parse a qualified-namespace-specifier.
10356 qualified-namespace-specifier:
10357 :: [opt] nested-name-specifier [opt] namespace-name
10359 Returns a NAMESPACE_DECL corresponding to the specified
10363 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10365 /* Look for the optional `::'. */
10366 cp_parser_global_scope_opt (parser,
10367 /*current_scope_valid_p=*/false);
10369 /* Look for the optional nested-name-specifier. */
10370 cp_parser_nested_name_specifier_opt (parser,
10371 /*typename_keyword_p=*/false,
10372 /*check_dependency_p=*/true,
10374 /*is_declaration=*/true);
10376 return cp_parser_namespace_name (parser);
10379 /* Parse a using-declaration.
10382 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10383 using :: unqualified-id ; */
10386 cp_parser_using_declaration (cp_parser* parser)
10389 bool typename_p = false;
10390 bool global_scope_p;
10395 /* Look for the `using' keyword. */
10396 cp_parser_require_keyword (parser, RID_USING, "`using'");
10398 /* Peek at the next token. */
10399 token = cp_lexer_peek_token (parser->lexer);
10400 /* See if it's `typename'. */
10401 if (token->keyword == RID_TYPENAME)
10403 /* Remember that we've seen it. */
10405 /* Consume the `typename' token. */
10406 cp_lexer_consume_token (parser->lexer);
10409 /* Look for the optional global scope qualification. */
10411 = (cp_parser_global_scope_opt (parser,
10412 /*current_scope_valid_p=*/false)
10415 /* If we saw `typename', or didn't see `::', then there must be a
10416 nested-name-specifier present. */
10417 if (typename_p || !global_scope_p)
10418 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10419 /*check_dependency_p=*/true,
10421 /*is_declaration=*/true);
10422 /* Otherwise, we could be in either of the two productions. In that
10423 case, treat the nested-name-specifier as optional. */
10425 qscope = cp_parser_nested_name_specifier_opt (parser,
10426 /*typename_keyword_p=*/false,
10427 /*check_dependency_p=*/true,
10429 /*is_declaration=*/true);
10431 qscope = global_namespace;
10433 /* Parse the unqualified-id. */
10434 identifier = cp_parser_unqualified_id (parser,
10435 /*template_keyword_p=*/false,
10436 /*check_dependency_p=*/true,
10437 /*declarator_p=*/true);
10439 /* The function we call to handle a using-declaration is different
10440 depending on what scope we are in. */
10441 if (identifier == error_mark_node)
10443 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10444 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10445 /* [namespace.udecl]
10447 A using declaration shall not name a template-id. */
10448 error ("a template-id may not appear in a using-declaration");
10451 if (at_class_scope_p ())
10453 /* Create the USING_DECL. */
10454 decl = do_class_using_decl (parser->scope, identifier);
10455 /* Add it to the list of members in this class. */
10456 finish_member_declaration (decl);
10460 decl = cp_parser_lookup_name_simple (parser, identifier);
10461 if (decl == error_mark_node)
10462 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10463 else if (!at_namespace_scope_p ())
10464 do_local_using_decl (decl, qscope, identifier);
10466 do_toplevel_using_decl (decl, qscope, identifier);
10470 /* Look for the final `;'. */
10471 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10474 /* Parse a using-directive.
10477 using namespace :: [opt] nested-name-specifier [opt]
10478 namespace-name ; */
10481 cp_parser_using_directive (cp_parser* parser)
10483 tree namespace_decl;
10486 /* Look for the `using' keyword. */
10487 cp_parser_require_keyword (parser, RID_USING, "`using'");
10488 /* And the `namespace' keyword. */
10489 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10490 /* Look for the optional `::' operator. */
10491 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10492 /* And the optional nested-name-specifier. */
10493 cp_parser_nested_name_specifier_opt (parser,
10494 /*typename_keyword_p=*/false,
10495 /*check_dependency_p=*/true,
10497 /*is_declaration=*/true);
10498 /* Get the namespace being used. */
10499 namespace_decl = cp_parser_namespace_name (parser);
10500 /* And any specified attributes. */
10501 attribs = cp_parser_attributes_opt (parser);
10502 /* Update the symbol table. */
10503 parse_using_directive (namespace_decl, attribs);
10504 /* Look for the final `;'. */
10505 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10508 /* Parse an asm-definition.
10511 asm ( string-literal ) ;
10516 asm volatile [opt] ( string-literal ) ;
10517 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10518 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10519 : asm-operand-list [opt] ) ;
10520 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10521 : asm-operand-list [opt]
10522 : asm-operand-list [opt] ) ; */
10525 cp_parser_asm_definition (cp_parser* parser)
10528 tree outputs = NULL_TREE;
10529 tree inputs = NULL_TREE;
10530 tree clobbers = NULL_TREE;
10532 bool volatile_p = false;
10533 bool extended_p = false;
10535 /* Look for the `asm' keyword. */
10536 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10537 /* See if the next token is `volatile'. */
10538 if (cp_parser_allow_gnu_extensions_p (parser)
10539 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10541 /* Remember that we saw the `volatile' keyword. */
10543 /* Consume the token. */
10544 cp_lexer_consume_token (parser->lexer);
10546 /* Look for the opening `('. */
10547 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10549 /* Look for the string. */
10550 string = cp_parser_string_literal (parser, false, false);
10551 if (string == error_mark_node)
10553 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10554 /*consume_paren=*/true);
10558 /* If we're allowing GNU extensions, check for the extended assembly
10559 syntax. Unfortunately, the `:' tokens need not be separated by
10560 a space in C, and so, for compatibility, we tolerate that here
10561 too. Doing that means that we have to treat the `::' operator as
10563 if (cp_parser_allow_gnu_extensions_p (parser)
10564 && at_function_scope_p ()
10565 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10566 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10568 bool inputs_p = false;
10569 bool clobbers_p = false;
10571 /* The extended syntax was used. */
10574 /* Look for outputs. */
10575 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10577 /* Consume the `:'. */
10578 cp_lexer_consume_token (parser->lexer);
10579 /* Parse the output-operands. */
10580 if (cp_lexer_next_token_is_not (parser->lexer,
10582 && cp_lexer_next_token_is_not (parser->lexer,
10584 && cp_lexer_next_token_is_not (parser->lexer,
10586 outputs = cp_parser_asm_operand_list (parser);
10588 /* If the next token is `::', there are no outputs, and the
10589 next token is the beginning of the inputs. */
10590 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10591 /* The inputs are coming next. */
10594 /* Look for inputs. */
10596 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10598 /* Consume the `:' or `::'. */
10599 cp_lexer_consume_token (parser->lexer);
10600 /* Parse the output-operands. */
10601 if (cp_lexer_next_token_is_not (parser->lexer,
10603 && cp_lexer_next_token_is_not (parser->lexer,
10605 inputs = cp_parser_asm_operand_list (parser);
10607 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10608 /* The clobbers are coming next. */
10611 /* Look for clobbers. */
10613 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10615 /* Consume the `:' or `::'. */
10616 cp_lexer_consume_token (parser->lexer);
10617 /* Parse the clobbers. */
10618 if (cp_lexer_next_token_is_not (parser->lexer,
10620 clobbers = cp_parser_asm_clobber_list (parser);
10623 /* Look for the closing `)'. */
10624 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10625 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10626 /*consume_paren=*/true);
10627 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10629 /* Create the ASM_EXPR. */
10630 if (at_function_scope_p ())
10632 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10634 /* If the extended syntax was not used, mark the ASM_EXPR. */
10637 tree temp = asm_stmt;
10638 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10639 temp = TREE_OPERAND (temp, 0);
10641 ASM_INPUT_P (temp) = 1;
10645 assemble_asm (string);
10648 /* Declarators [gram.dcl.decl] */
10650 /* Parse an init-declarator.
10653 declarator initializer [opt]
10658 declarator asm-specification [opt] attributes [opt] initializer [opt]
10660 function-definition:
10661 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10663 decl-specifier-seq [opt] declarator function-try-block
10667 function-definition:
10668 __extension__ function-definition
10670 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10671 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10672 then this declarator appears in a class scope. The new DECL created
10673 by this declarator is returned.
10675 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10676 for a function-definition here as well. If the declarator is a
10677 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10678 be TRUE upon return. By that point, the function-definition will
10679 have been completely parsed.
10681 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10685 cp_parser_init_declarator (cp_parser* parser,
10686 cp_decl_specifier_seq *decl_specifiers,
10687 bool function_definition_allowed_p,
10689 int declares_class_or_enum,
10690 bool* function_definition_p)
10693 cp_declarator *declarator;
10694 tree prefix_attributes;
10696 tree asm_specification;
10698 tree decl = NULL_TREE;
10700 bool is_initialized;
10701 bool is_parenthesized_init;
10702 bool is_non_constant_init;
10703 int ctor_dtor_or_conv_p;
10705 tree pushed_scope = NULL;
10707 /* Gather the attributes that were provided with the
10708 decl-specifiers. */
10709 prefix_attributes = decl_specifiers->attributes;
10711 /* Assume that this is not the declarator for a function
10713 if (function_definition_p)
10714 *function_definition_p = false;
10716 /* Defer access checks while parsing the declarator; we cannot know
10717 what names are accessible until we know what is being
10719 resume_deferring_access_checks ();
10721 /* Parse the declarator. */
10723 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10724 &ctor_dtor_or_conv_p,
10725 /*parenthesized_p=*/NULL,
10726 /*member_p=*/false);
10727 /* Gather up the deferred checks. */
10728 stop_deferring_access_checks ();
10730 /* If the DECLARATOR was erroneous, there's no need to go
10732 if (declarator == cp_error_declarator)
10733 return error_mark_node;
10735 if (declares_class_or_enum & 2)
10736 cp_parser_check_for_definition_in_return_type (declarator,
10737 decl_specifiers->type);
10739 /* Figure out what scope the entity declared by the DECLARATOR is
10740 located in. `grokdeclarator' sometimes changes the scope, so
10741 we compute it now. */
10742 scope = get_scope_of_declarator (declarator);
10744 /* If we're allowing GNU extensions, look for an asm-specification
10746 if (cp_parser_allow_gnu_extensions_p (parser))
10748 /* Look for an asm-specification. */
10749 asm_specification = cp_parser_asm_specification_opt (parser);
10750 /* And attributes. */
10751 attributes = cp_parser_attributes_opt (parser);
10755 asm_specification = NULL_TREE;
10756 attributes = NULL_TREE;
10759 /* Peek at the next token. */
10760 token = cp_lexer_peek_token (parser->lexer);
10761 /* Check to see if the token indicates the start of a
10762 function-definition. */
10763 if (cp_parser_token_starts_function_definition_p (token))
10765 if (!function_definition_allowed_p)
10767 /* If a function-definition should not appear here, issue an
10769 cp_parser_error (parser,
10770 "a function-definition is not allowed here");
10771 return error_mark_node;
10775 /* Neither attributes nor an asm-specification are allowed
10776 on a function-definition. */
10777 if (asm_specification)
10778 error ("an asm-specification is not allowed on a function-definition");
10780 error ("attributes are not allowed on a function-definition");
10781 /* This is a function-definition. */
10782 *function_definition_p = true;
10784 /* Parse the function definition. */
10786 decl = cp_parser_save_member_function_body (parser,
10789 prefix_attributes);
10792 = (cp_parser_function_definition_from_specifiers_and_declarator
10793 (parser, decl_specifiers, prefix_attributes, declarator));
10801 Only in function declarations for constructors, destructors, and
10802 type conversions can the decl-specifier-seq be omitted.
10804 We explicitly postpone this check past the point where we handle
10805 function-definitions because we tolerate function-definitions
10806 that are missing their return types in some modes. */
10807 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10809 cp_parser_error (parser,
10810 "expected constructor, destructor, or type conversion");
10811 return error_mark_node;
10814 /* An `=' or an `(' indicates an initializer. */
10815 is_initialized = (token->type == CPP_EQ
10816 || token->type == CPP_OPEN_PAREN);
10817 /* If the init-declarator isn't initialized and isn't followed by a
10818 `,' or `;', it's not a valid init-declarator. */
10819 if (!is_initialized
10820 && token->type != CPP_COMMA
10821 && token->type != CPP_SEMICOLON)
10823 cp_parser_error (parser, "expected initializer");
10824 return error_mark_node;
10827 /* Because start_decl has side-effects, we should only call it if we
10828 know we're going ahead. By this point, we know that we cannot
10829 possibly be looking at any other construct. */
10830 cp_parser_commit_to_tentative_parse (parser);
10832 /* If the decl specifiers were bad, issue an error now that we're
10833 sure this was intended to be a declarator. Then continue
10834 declaring the variable(s), as int, to try to cut down on further
10836 if (decl_specifiers->any_specifiers_p
10837 && decl_specifiers->type == error_mark_node)
10839 cp_parser_error (parser, "invalid type in declaration");
10840 decl_specifiers->type = integer_type_node;
10843 /* Check to see whether or not this declaration is a friend. */
10844 friend_p = cp_parser_friend_p (decl_specifiers);
10846 /* Check that the number of template-parameter-lists is OK. */
10847 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10848 return error_mark_node;
10850 /* Enter the newly declared entry in the symbol table. If we're
10851 processing a declaration in a class-specifier, we wait until
10852 after processing the initializer. */
10855 if (parser->in_unbraced_linkage_specification_p)
10857 decl_specifiers->storage_class = sc_extern;
10858 have_extern_spec = false;
10860 decl = start_decl (declarator, decl_specifiers,
10861 is_initialized, attributes, prefix_attributes,
10865 /* Enter the SCOPE. That way unqualified names appearing in the
10866 initializer will be looked up in SCOPE. */
10867 pushed_scope = push_scope (scope);
10869 /* Perform deferred access control checks, now that we know in which
10870 SCOPE the declared entity resides. */
10871 if (!member_p && decl)
10873 tree saved_current_function_decl = NULL_TREE;
10875 /* If the entity being declared is a function, pretend that we
10876 are in its scope. If it is a `friend', it may have access to
10877 things that would not otherwise be accessible. */
10878 if (TREE_CODE (decl) == FUNCTION_DECL)
10880 saved_current_function_decl = current_function_decl;
10881 current_function_decl = decl;
10884 /* Perform the access control checks for the declarator and the
10885 the decl-specifiers. */
10886 perform_deferred_access_checks ();
10888 /* Restore the saved value. */
10889 if (TREE_CODE (decl) == FUNCTION_DECL)
10890 current_function_decl = saved_current_function_decl;
10893 /* Parse the initializer. */
10894 if (is_initialized)
10895 initializer = cp_parser_initializer (parser,
10896 &is_parenthesized_init,
10897 &is_non_constant_init);
10900 initializer = NULL_TREE;
10901 is_parenthesized_init = false;
10902 is_non_constant_init = true;
10905 /* The old parser allows attributes to appear after a parenthesized
10906 initializer. Mark Mitchell proposed removing this functionality
10907 on the GCC mailing lists on 2002-08-13. This parser accepts the
10908 attributes -- but ignores them. */
10909 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10910 if (cp_parser_attributes_opt (parser))
10911 warning (OPT_Wattributes,
10912 "attributes after parenthesized initializer ignored");
10914 /* For an in-class declaration, use `grokfield' to create the
10920 pop_scope (pushed_scope);
10921 pushed_scope = false;
10923 decl = grokfield (declarator, decl_specifiers,
10924 initializer, /*asmspec=*/NULL_TREE,
10925 /*attributes=*/NULL_TREE);
10926 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10927 cp_parser_save_default_args (parser, decl);
10930 /* Finish processing the declaration. But, skip friend
10932 if (!friend_p && decl && decl != error_mark_node)
10934 cp_finish_decl (decl,
10937 /* If the initializer is in parentheses, then this is
10938 a direct-initialization, which means that an
10939 `explicit' constructor is OK. Otherwise, an
10940 `explicit' constructor cannot be used. */
10941 ((is_parenthesized_init || !is_initialized)
10942 ? 0 : LOOKUP_ONLYCONVERTING));
10944 if (!friend_p && pushed_scope)
10945 pop_scope (pushed_scope);
10947 /* Remember whether or not variables were initialized by
10948 constant-expressions. */
10949 if (decl && TREE_CODE (decl) == VAR_DECL
10950 && is_initialized && !is_non_constant_init)
10951 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10956 /* Parse a declarator.
10960 ptr-operator declarator
10962 abstract-declarator:
10963 ptr-operator abstract-declarator [opt]
10964 direct-abstract-declarator
10969 attributes [opt] direct-declarator
10970 attributes [opt] ptr-operator declarator
10972 abstract-declarator:
10973 attributes [opt] ptr-operator abstract-declarator [opt]
10974 attributes [opt] direct-abstract-declarator
10976 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10977 detect constructor, destructor or conversion operators. It is set
10978 to -1 if the declarator is a name, and +1 if it is a
10979 function. Otherwise it is set to zero. Usually you just want to
10980 test for >0, but internally the negative value is used.
10982 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10983 a decl-specifier-seq unless it declares a constructor, destructor,
10984 or conversion. It might seem that we could check this condition in
10985 semantic analysis, rather than parsing, but that makes it difficult
10986 to handle something like `f()'. We want to notice that there are
10987 no decl-specifiers, and therefore realize that this is an
10988 expression, not a declaration.)
10990 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10991 the declarator is a direct-declarator of the form "(...)".
10993 MEMBER_P is true iff this declarator is a member-declarator. */
10995 static cp_declarator *
10996 cp_parser_declarator (cp_parser* parser,
10997 cp_parser_declarator_kind dcl_kind,
10998 int* ctor_dtor_or_conv_p,
10999 bool* parenthesized_p,
11003 cp_declarator *declarator;
11004 enum tree_code code;
11005 cp_cv_quals cv_quals;
11007 tree attributes = NULL_TREE;
11009 /* Assume this is not a constructor, destructor, or type-conversion
11011 if (ctor_dtor_or_conv_p)
11012 *ctor_dtor_or_conv_p = 0;
11014 if (cp_parser_allow_gnu_extensions_p (parser))
11015 attributes = cp_parser_attributes_opt (parser);
11017 /* Peek at the next token. */
11018 token = cp_lexer_peek_token (parser->lexer);
11020 /* Check for the ptr-operator production. */
11021 cp_parser_parse_tentatively (parser);
11022 /* Parse the ptr-operator. */
11023 code = cp_parser_ptr_operator (parser,
11026 /* If that worked, then we have a ptr-operator. */
11027 if (cp_parser_parse_definitely (parser))
11029 /* If a ptr-operator was found, then this declarator was not
11031 if (parenthesized_p)
11032 *parenthesized_p = true;
11033 /* The dependent declarator is optional if we are parsing an
11034 abstract-declarator. */
11035 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11036 cp_parser_parse_tentatively (parser);
11038 /* Parse the dependent declarator. */
11039 declarator = cp_parser_declarator (parser, dcl_kind,
11040 /*ctor_dtor_or_conv_p=*/NULL,
11041 /*parenthesized_p=*/NULL,
11042 /*member_p=*/false);
11044 /* If we are parsing an abstract-declarator, we must handle the
11045 case where the dependent declarator is absent. */
11046 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11047 && !cp_parser_parse_definitely (parser))
11050 /* Build the representation of the ptr-operator. */
11052 declarator = make_ptrmem_declarator (cv_quals,
11055 else if (code == INDIRECT_REF)
11056 declarator = make_pointer_declarator (cv_quals, declarator);
11058 declarator = make_reference_declarator (cv_quals, declarator);
11060 /* Everything else is a direct-declarator. */
11063 if (parenthesized_p)
11064 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11066 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11067 ctor_dtor_or_conv_p,
11071 if (attributes && declarator != cp_error_declarator)
11072 declarator->attributes = attributes;
11077 /* Parse a direct-declarator or direct-abstract-declarator.
11081 direct-declarator ( parameter-declaration-clause )
11082 cv-qualifier-seq [opt]
11083 exception-specification [opt]
11084 direct-declarator [ constant-expression [opt] ]
11087 direct-abstract-declarator:
11088 direct-abstract-declarator [opt]
11089 ( parameter-declaration-clause )
11090 cv-qualifier-seq [opt]
11091 exception-specification [opt]
11092 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11093 ( abstract-declarator )
11095 Returns a representation of the declarator. DCL_KIND is
11096 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11097 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11098 we are parsing a direct-declarator. It is
11099 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11100 of ambiguity we prefer an abstract declarator, as per
11101 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11102 cp_parser_declarator. */
11104 static cp_declarator *
11105 cp_parser_direct_declarator (cp_parser* parser,
11106 cp_parser_declarator_kind dcl_kind,
11107 int* ctor_dtor_or_conv_p,
11111 cp_declarator *declarator = NULL;
11112 tree scope = NULL_TREE;
11113 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11114 bool saved_in_declarator_p = parser->in_declarator_p;
11116 tree pushed_scope = NULL_TREE;
11120 /* Peek at the next token. */
11121 token = cp_lexer_peek_token (parser->lexer);
11122 if (token->type == CPP_OPEN_PAREN)
11124 /* This is either a parameter-declaration-clause, or a
11125 parenthesized declarator. When we know we are parsing a
11126 named declarator, it must be a parenthesized declarator
11127 if FIRST is true. For instance, `(int)' is a
11128 parameter-declaration-clause, with an omitted
11129 direct-abstract-declarator. But `((*))', is a
11130 parenthesized abstract declarator. Finally, when T is a
11131 template parameter `(T)' is a
11132 parameter-declaration-clause, and not a parenthesized
11135 We first try and parse a parameter-declaration-clause,
11136 and then try a nested declarator (if FIRST is true).
11138 It is not an error for it not to be a
11139 parameter-declaration-clause, even when FIRST is
11145 The first is the declaration of a function while the
11146 second is a the definition of a variable, including its
11149 Having seen only the parenthesis, we cannot know which of
11150 these two alternatives should be selected. Even more
11151 complex are examples like:
11156 The former is a function-declaration; the latter is a
11157 variable initialization.
11159 Thus again, we try a parameter-declaration-clause, and if
11160 that fails, we back out and return. */
11162 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11164 cp_parameter_declarator *params;
11165 unsigned saved_num_template_parameter_lists;
11167 /* In a member-declarator, the only valid interpretation
11168 of a parenthesis is the start of a
11169 parameter-declaration-clause. (It is invalid to
11170 initialize a static data member with a parenthesized
11171 initializer; only the "=" form of initialization is
11174 cp_parser_parse_tentatively (parser);
11176 /* Consume the `('. */
11177 cp_lexer_consume_token (parser->lexer);
11180 /* If this is going to be an abstract declarator, we're
11181 in a declarator and we can't have default args. */
11182 parser->default_arg_ok_p = false;
11183 parser->in_declarator_p = true;
11186 /* Inside the function parameter list, surrounding
11187 template-parameter-lists do not apply. */
11188 saved_num_template_parameter_lists
11189 = parser->num_template_parameter_lists;
11190 parser->num_template_parameter_lists = 0;
11192 /* Parse the parameter-declaration-clause. */
11193 params = cp_parser_parameter_declaration_clause (parser);
11195 parser->num_template_parameter_lists
11196 = saved_num_template_parameter_lists;
11198 /* If all went well, parse the cv-qualifier-seq and the
11199 exception-specification. */
11200 if (member_p || cp_parser_parse_definitely (parser))
11202 cp_cv_quals cv_quals;
11203 tree exception_specification;
11205 if (ctor_dtor_or_conv_p)
11206 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11208 /* Consume the `)'. */
11209 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11211 /* Parse the cv-qualifier-seq. */
11212 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11213 /* And the exception-specification. */
11214 exception_specification
11215 = cp_parser_exception_specification_opt (parser);
11217 /* Create the function-declarator. */
11218 declarator = make_call_declarator (declarator,
11221 exception_specification);
11222 /* Any subsequent parameter lists are to do with
11223 return type, so are not those of the declared
11225 parser->default_arg_ok_p = false;
11227 /* Repeat the main loop. */
11232 /* If this is the first, we can try a parenthesized
11236 bool saved_in_type_id_in_expr_p;
11238 parser->default_arg_ok_p = saved_default_arg_ok_p;
11239 parser->in_declarator_p = saved_in_declarator_p;
11241 /* Consume the `('. */
11242 cp_lexer_consume_token (parser->lexer);
11243 /* Parse the nested declarator. */
11244 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11245 parser->in_type_id_in_expr_p = true;
11247 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11248 /*parenthesized_p=*/NULL,
11250 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11252 /* Expect a `)'. */
11253 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11254 declarator = cp_error_declarator;
11255 if (declarator == cp_error_declarator)
11258 goto handle_declarator;
11260 /* Otherwise, we must be done. */
11264 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11265 && token->type == CPP_OPEN_SQUARE)
11267 /* Parse an array-declarator. */
11270 if (ctor_dtor_or_conv_p)
11271 *ctor_dtor_or_conv_p = 0;
11274 parser->default_arg_ok_p = false;
11275 parser->in_declarator_p = true;
11276 /* Consume the `['. */
11277 cp_lexer_consume_token (parser->lexer);
11278 /* Peek at the next token. */
11279 token = cp_lexer_peek_token (parser->lexer);
11280 /* If the next token is `]', then there is no
11281 constant-expression. */
11282 if (token->type != CPP_CLOSE_SQUARE)
11284 bool non_constant_p;
11287 = cp_parser_constant_expression (parser,
11288 /*allow_non_constant=*/true,
11290 if (!non_constant_p)
11291 bounds = fold_non_dependent_expr (bounds);
11292 /* Normally, the array bound must be an integral constant
11293 expression. However, as an extension, we allow VLAs
11294 in function scopes. */
11295 else if (!at_function_scope_p ())
11297 error ("array bound is not an integer constant");
11298 bounds = error_mark_node;
11302 bounds = NULL_TREE;
11303 /* Look for the closing `]'. */
11304 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11306 declarator = cp_error_declarator;
11310 declarator = make_array_declarator (declarator, bounds);
11312 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11314 tree qualifying_scope;
11315 tree unqualified_name;
11317 /* Parse a declarator-id */
11318 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11319 cp_parser_parse_tentatively (parser);
11320 unqualified_name = cp_parser_declarator_id (parser);
11321 qualifying_scope = parser->scope;
11322 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11324 if (!cp_parser_parse_definitely (parser))
11325 unqualified_name = error_mark_node;
11326 else if (qualifying_scope
11327 || (TREE_CODE (unqualified_name)
11328 != IDENTIFIER_NODE))
11330 cp_parser_error (parser, "expected unqualified-id");
11331 unqualified_name = error_mark_node;
11335 if (unqualified_name == error_mark_node)
11337 declarator = cp_error_declarator;
11341 if (qualifying_scope && at_namespace_scope_p ()
11342 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11344 /* In the declaration of a member of a template class
11345 outside of the class itself, the SCOPE will sometimes
11346 be a TYPENAME_TYPE. For example, given:
11348 template <typename T>
11349 int S<T>::R::i = 3;
11351 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11352 this context, we must resolve S<T>::R to an ordinary
11353 type, rather than a typename type.
11355 The reason we normally avoid resolving TYPENAME_TYPEs
11356 is that a specialization of `S' might render
11357 `S<T>::R' not a type. However, if `S' is
11358 specialized, then this `i' will not be used, so there
11359 is no harm in resolving the types here. */
11362 /* Resolve the TYPENAME_TYPE. */
11363 type = resolve_typename_type (qualifying_scope,
11364 /*only_current_p=*/false);
11365 /* If that failed, the declarator is invalid. */
11366 if (type == error_mark_node)
11367 error ("%<%T::%D%> is not a type",
11368 TYPE_CONTEXT (qualifying_scope),
11369 TYPE_IDENTIFIER (qualifying_scope));
11370 qualifying_scope = type;
11373 declarator = make_id_declarator (qualifying_scope,
11375 declarator->id_loc = token->location;
11376 if (unqualified_name)
11380 if (qualifying_scope
11381 && CLASS_TYPE_P (qualifying_scope))
11382 class_type = qualifying_scope;
11384 class_type = current_class_type;
11388 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11389 declarator->u.id.sfk = sfk_destructor;
11390 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11391 declarator->u.id.sfk = sfk_conversion;
11392 else if (/* There's no way to declare a constructor
11393 for an anonymous type, even if the type
11394 got a name for linkage purposes. */
11395 !TYPE_WAS_ANONYMOUS (class_type)
11396 && (constructor_name_p (unqualified_name,
11398 || (TREE_CODE (unqualified_name) == TYPE_DECL
11400 (TREE_TYPE (unqualified_name),
11402 declarator->u.id.sfk = sfk_constructor;
11404 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11405 *ctor_dtor_or_conv_p = -1;
11406 if (qualifying_scope
11407 && TREE_CODE (unqualified_name) == TYPE_DECL
11408 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11410 error ("invalid use of constructor as a template");
11411 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11412 "the constructor in a qualified name",
11414 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11415 class_type, class_type);
11420 handle_declarator:;
11421 scope = get_scope_of_declarator (declarator);
11423 /* Any names that appear after the declarator-id for a
11424 member are looked up in the containing scope. */
11425 pushed_scope = push_scope (scope);
11426 parser->in_declarator_p = true;
11427 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11428 || (declarator && declarator->kind == cdk_id))
11429 /* Default args are only allowed on function
11431 parser->default_arg_ok_p = saved_default_arg_ok_p;
11433 parser->default_arg_ok_p = false;
11442 /* For an abstract declarator, we might wind up with nothing at this
11443 point. That's an error; the declarator is not optional. */
11445 cp_parser_error (parser, "expected declarator");
11447 /* If we entered a scope, we must exit it now. */
11449 pop_scope (pushed_scope);
11451 parser->default_arg_ok_p = saved_default_arg_ok_p;
11452 parser->in_declarator_p = saved_in_declarator_p;
11457 /* Parse a ptr-operator.
11460 * cv-qualifier-seq [opt]
11462 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11467 & cv-qualifier-seq [opt]
11469 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11470 Returns ADDR_EXPR if a reference was used. In the case of a
11471 pointer-to-member, *TYPE is filled in with the TYPE containing the
11472 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11473 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11474 ERROR_MARK if an error occurred. */
11476 static enum tree_code
11477 cp_parser_ptr_operator (cp_parser* parser,
11479 cp_cv_quals *cv_quals)
11481 enum tree_code code = ERROR_MARK;
11484 /* Assume that it's not a pointer-to-member. */
11486 /* And that there are no cv-qualifiers. */
11487 *cv_quals = TYPE_UNQUALIFIED;
11489 /* Peek at the next token. */
11490 token = cp_lexer_peek_token (parser->lexer);
11491 /* If it's a `*' or `&' we have a pointer or reference. */
11492 if (token->type == CPP_MULT || token->type == CPP_AND)
11494 /* Remember which ptr-operator we were processing. */
11495 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11497 /* Consume the `*' or `&'. */
11498 cp_lexer_consume_token (parser->lexer);
11500 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11501 `&', if we are allowing GNU extensions. (The only qualifier
11502 that can legally appear after `&' is `restrict', but that is
11503 enforced during semantic analysis. */
11504 if (code == INDIRECT_REF
11505 || cp_parser_allow_gnu_extensions_p (parser))
11506 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11510 /* Try the pointer-to-member case. */
11511 cp_parser_parse_tentatively (parser);
11512 /* Look for the optional `::' operator. */
11513 cp_parser_global_scope_opt (parser,
11514 /*current_scope_valid_p=*/false);
11515 /* Look for the nested-name specifier. */
11516 cp_parser_nested_name_specifier (parser,
11517 /*typename_keyword_p=*/false,
11518 /*check_dependency_p=*/true,
11520 /*is_declaration=*/false);
11521 /* If we found it, and the next token is a `*', then we are
11522 indeed looking at a pointer-to-member operator. */
11523 if (!cp_parser_error_occurred (parser)
11524 && cp_parser_require (parser, CPP_MULT, "`*'"))
11526 /* The type of which the member is a member is given by the
11528 *type = parser->scope;
11529 /* The next name will not be qualified. */
11530 parser->scope = NULL_TREE;
11531 parser->qualifying_scope = NULL_TREE;
11532 parser->object_scope = NULL_TREE;
11533 /* Indicate that the `*' operator was used. */
11534 code = INDIRECT_REF;
11535 /* Look for the optional cv-qualifier-seq. */
11536 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11538 /* If that didn't work we don't have a ptr-operator. */
11539 if (!cp_parser_parse_definitely (parser))
11540 cp_parser_error (parser, "expected ptr-operator");
11546 /* Parse an (optional) cv-qualifier-seq.
11549 cv-qualifier cv-qualifier-seq [opt]
11560 Returns a bitmask representing the cv-qualifiers. */
11563 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11565 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11570 cp_cv_quals cv_qualifier;
11572 /* Peek at the next token. */
11573 token = cp_lexer_peek_token (parser->lexer);
11574 /* See if it's a cv-qualifier. */
11575 switch (token->keyword)
11578 cv_qualifier = TYPE_QUAL_CONST;
11582 cv_qualifier = TYPE_QUAL_VOLATILE;
11586 cv_qualifier = TYPE_QUAL_RESTRICT;
11590 cv_qualifier = TYPE_UNQUALIFIED;
11597 if (cv_quals & cv_qualifier)
11599 error ("duplicate cv-qualifier");
11600 cp_lexer_purge_token (parser->lexer);
11604 cp_lexer_consume_token (parser->lexer);
11605 cv_quals |= cv_qualifier;
11612 /* Parse a declarator-id.
11616 :: [opt] nested-name-specifier [opt] type-name
11618 In the `id-expression' case, the value returned is as for
11619 cp_parser_id_expression if the id-expression was an unqualified-id.
11620 If the id-expression was a qualified-id, then a SCOPE_REF is
11621 returned. The first operand is the scope (either a NAMESPACE_DECL
11622 or TREE_TYPE), but the second is still just a representation of an
11626 cp_parser_declarator_id (cp_parser* parser)
11628 /* The expression must be an id-expression. Assume that qualified
11629 names are the names of types so that:
11632 int S<T>::R::i = 3;
11634 will work; we must treat `S<T>::R' as the name of a type.
11635 Similarly, assume that qualified names are templates, where
11639 int S<T>::R<T>::i = 3;
11642 return cp_parser_id_expression (parser,
11643 /*template_keyword_p=*/false,
11644 /*check_dependency_p=*/false,
11645 /*template_p=*/NULL,
11646 /*declarator_p=*/true);
11649 /* Parse a type-id.
11652 type-specifier-seq abstract-declarator [opt]
11654 Returns the TYPE specified. */
11657 cp_parser_type_id (cp_parser* parser)
11659 cp_decl_specifier_seq type_specifier_seq;
11660 cp_declarator *abstract_declarator;
11662 /* Parse the type-specifier-seq. */
11663 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11664 &type_specifier_seq);
11665 if (type_specifier_seq.type == error_mark_node)
11666 return error_mark_node;
11668 /* There might or might not be an abstract declarator. */
11669 cp_parser_parse_tentatively (parser);
11670 /* Look for the declarator. */
11671 abstract_declarator
11672 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11673 /*parenthesized_p=*/NULL,
11674 /*member_p=*/false);
11675 /* Check to see if there really was a declarator. */
11676 if (!cp_parser_parse_definitely (parser))
11677 abstract_declarator = NULL;
11679 return groktypename (&type_specifier_seq, abstract_declarator);
11682 /* Parse a type-specifier-seq.
11684 type-specifier-seq:
11685 type-specifier type-specifier-seq [opt]
11689 type-specifier-seq:
11690 attributes type-specifier-seq [opt]
11692 If IS_CONDITION is true, we are at the start of a "condition",
11693 e.g., we've just seen "if (".
11695 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11698 cp_parser_type_specifier_seq (cp_parser* parser,
11700 cp_decl_specifier_seq *type_specifier_seq)
11702 bool seen_type_specifier = false;
11703 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11705 /* Clear the TYPE_SPECIFIER_SEQ. */
11706 clear_decl_specs (type_specifier_seq);
11708 /* Parse the type-specifiers and attributes. */
11711 tree type_specifier;
11712 bool is_cv_qualifier;
11714 /* Check for attributes first. */
11715 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11717 type_specifier_seq->attributes =
11718 chainon (type_specifier_seq->attributes,
11719 cp_parser_attributes_opt (parser));
11723 /* Look for the type-specifier. */
11724 type_specifier = cp_parser_type_specifier (parser,
11726 type_specifier_seq,
11727 /*is_declaration=*/false,
11730 if (!type_specifier)
11732 /* If the first type-specifier could not be found, this is not a
11733 type-specifier-seq at all. */
11734 if (!seen_type_specifier)
11736 cp_parser_error (parser, "expected type-specifier");
11737 type_specifier_seq->type = error_mark_node;
11740 /* If subsequent type-specifiers could not be found, the
11741 type-specifier-seq is complete. */
11745 seen_type_specifier = true;
11746 /* The standard says that a condition can be:
11748 type-specifier-seq declarator = assignment-expression
11755 we should treat the "S" as a declarator, not as a
11756 type-specifier. The standard doesn't say that explicitly for
11757 type-specifier-seq, but it does say that for
11758 decl-specifier-seq in an ordinary declaration. Perhaps it
11759 would be clearer just to allow a decl-specifier-seq here, and
11760 then add a semantic restriction that if any decl-specifiers
11761 that are not type-specifiers appear, the program is invalid. */
11762 if (is_condition && !is_cv_qualifier)
11763 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11769 /* Parse a parameter-declaration-clause.
11771 parameter-declaration-clause:
11772 parameter-declaration-list [opt] ... [opt]
11773 parameter-declaration-list , ...
11775 Returns a representation for the parameter declarations. A return
11776 value of NULL indicates a parameter-declaration-clause consisting
11777 only of an ellipsis. */
11779 static cp_parameter_declarator *
11780 cp_parser_parameter_declaration_clause (cp_parser* parser)
11782 cp_parameter_declarator *parameters;
11787 /* Peek at the next token. */
11788 token = cp_lexer_peek_token (parser->lexer);
11789 /* Check for trivial parameter-declaration-clauses. */
11790 if (token->type == CPP_ELLIPSIS)
11792 /* Consume the `...' token. */
11793 cp_lexer_consume_token (parser->lexer);
11796 else if (token->type == CPP_CLOSE_PAREN)
11797 /* There are no parameters. */
11799 #ifndef NO_IMPLICIT_EXTERN_C
11800 if (in_system_header && current_class_type == NULL
11801 && current_lang_name == lang_name_c)
11805 return no_parameters;
11807 /* Check for `(void)', too, which is a special case. */
11808 else if (token->keyword == RID_VOID
11809 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11810 == CPP_CLOSE_PAREN))
11812 /* Consume the `void' token. */
11813 cp_lexer_consume_token (parser->lexer);
11814 /* There are no parameters. */
11815 return no_parameters;
11818 /* Parse the parameter-declaration-list. */
11819 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11820 /* If a parse error occurred while parsing the
11821 parameter-declaration-list, then the entire
11822 parameter-declaration-clause is erroneous. */
11826 /* Peek at the next token. */
11827 token = cp_lexer_peek_token (parser->lexer);
11828 /* If it's a `,', the clause should terminate with an ellipsis. */
11829 if (token->type == CPP_COMMA)
11831 /* Consume the `,'. */
11832 cp_lexer_consume_token (parser->lexer);
11833 /* Expect an ellipsis. */
11835 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11837 /* It might also be `...' if the optional trailing `,' was
11839 else if (token->type == CPP_ELLIPSIS)
11841 /* Consume the `...' token. */
11842 cp_lexer_consume_token (parser->lexer);
11843 /* And remember that we saw it. */
11847 ellipsis_p = false;
11849 /* Finish the parameter list. */
11850 if (parameters && ellipsis_p)
11851 parameters->ellipsis_p = true;
11856 /* Parse a parameter-declaration-list.
11858 parameter-declaration-list:
11859 parameter-declaration
11860 parameter-declaration-list , parameter-declaration
11862 Returns a representation of the parameter-declaration-list, as for
11863 cp_parser_parameter_declaration_clause. However, the
11864 `void_list_node' is never appended to the list. Upon return,
11865 *IS_ERROR will be true iff an error occurred. */
11867 static cp_parameter_declarator *
11868 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11870 cp_parameter_declarator *parameters = NULL;
11871 cp_parameter_declarator **tail = ¶meters;
11873 /* Assume all will go well. */
11876 /* Look for more parameters. */
11879 cp_parameter_declarator *parameter;
11880 bool parenthesized_p;
11881 /* Parse the parameter. */
11883 = cp_parser_parameter_declaration (parser,
11884 /*template_parm_p=*/false,
11887 /* If a parse error occurred parsing the parameter declaration,
11888 then the entire parameter-declaration-list is erroneous. */
11895 /* Add the new parameter to the list. */
11897 tail = ¶meter->next;
11899 /* Peek at the next token. */
11900 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11901 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11902 /* These are for Objective-C++ */
11903 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11904 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11905 /* The parameter-declaration-list is complete. */
11907 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11911 /* Peek at the next token. */
11912 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11913 /* If it's an ellipsis, then the list is complete. */
11914 if (token->type == CPP_ELLIPSIS)
11916 /* Otherwise, there must be more parameters. Consume the
11918 cp_lexer_consume_token (parser->lexer);
11919 /* When parsing something like:
11921 int i(float f, double d)
11923 we can tell after seeing the declaration for "f" that we
11924 are not looking at an initialization of a variable "i",
11925 but rather at the declaration of a function "i".
11927 Due to the fact that the parsing of template arguments
11928 (as specified to a template-id) requires backtracking we
11929 cannot use this technique when inside a template argument
11931 if (!parser->in_template_argument_list_p
11932 && !parser->in_type_id_in_expr_p
11933 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11934 /* However, a parameter-declaration of the form
11935 "foat(f)" (which is a valid declaration of a
11936 parameter "f") can also be interpreted as an
11937 expression (the conversion of "f" to "float"). */
11938 && !parenthesized_p)
11939 cp_parser_commit_to_tentative_parse (parser);
11943 cp_parser_error (parser, "expected %<,%> or %<...%>");
11944 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11945 cp_parser_skip_to_closing_parenthesis (parser,
11946 /*recovering=*/true,
11947 /*or_comma=*/false,
11948 /*consume_paren=*/false);
11956 /* Parse a parameter declaration.
11958 parameter-declaration:
11959 decl-specifier-seq declarator
11960 decl-specifier-seq declarator = assignment-expression
11961 decl-specifier-seq abstract-declarator [opt]
11962 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11964 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11965 declares a template parameter. (In that case, a non-nested `>'
11966 token encountered during the parsing of the assignment-expression
11967 is not interpreted as a greater-than operator.)
11969 Returns a representation of the parameter, or NULL if an error
11970 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11971 true iff the declarator is of the form "(p)". */
11973 static cp_parameter_declarator *
11974 cp_parser_parameter_declaration (cp_parser *parser,
11975 bool template_parm_p,
11976 bool *parenthesized_p)
11978 int declares_class_or_enum;
11979 bool greater_than_is_operator_p;
11980 cp_decl_specifier_seq decl_specifiers;
11981 cp_declarator *declarator;
11982 tree default_argument;
11984 const char *saved_message;
11986 /* In a template parameter, `>' is not an operator.
11990 When parsing a default template-argument for a non-type
11991 template-parameter, the first non-nested `>' is taken as the end
11992 of the template parameter-list rather than a greater-than
11994 greater_than_is_operator_p = !template_parm_p;
11996 /* Type definitions may not appear in parameter types. */
11997 saved_message = parser->type_definition_forbidden_message;
11998 parser->type_definition_forbidden_message
11999 = "types may not be defined in parameter types";
12001 /* Parse the declaration-specifiers. */
12002 cp_parser_decl_specifier_seq (parser,
12003 CP_PARSER_FLAGS_NONE,
12005 &declares_class_or_enum);
12006 /* If an error occurred, there's no reason to attempt to parse the
12007 rest of the declaration. */
12008 if (cp_parser_error_occurred (parser))
12010 parser->type_definition_forbidden_message = saved_message;
12014 /* Peek at the next token. */
12015 token = cp_lexer_peek_token (parser->lexer);
12016 /* If the next token is a `)', `,', `=', `>', or `...', then there
12017 is no declarator. */
12018 if (token->type == CPP_CLOSE_PAREN
12019 || token->type == CPP_COMMA
12020 || token->type == CPP_EQ
12021 || token->type == CPP_ELLIPSIS
12022 || token->type == CPP_GREATER)
12025 if (parenthesized_p)
12026 *parenthesized_p = false;
12028 /* Otherwise, there should be a declarator. */
12031 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12032 parser->default_arg_ok_p = false;
12034 /* After seeing a decl-specifier-seq, if the next token is not a
12035 "(", there is no possibility that the code is a valid
12036 expression. Therefore, if parsing tentatively, we commit at
12038 if (!parser->in_template_argument_list_p
12039 /* In an expression context, having seen:
12043 we cannot be sure whether we are looking at a
12044 function-type (taking a "char" as a parameter) or a cast
12045 of some object of type "char" to "int". */
12046 && !parser->in_type_id_in_expr_p
12047 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12048 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12049 cp_parser_commit_to_tentative_parse (parser);
12050 /* Parse the declarator. */
12051 declarator = cp_parser_declarator (parser,
12052 CP_PARSER_DECLARATOR_EITHER,
12053 /*ctor_dtor_or_conv_p=*/NULL,
12055 /*member_p=*/false);
12056 parser->default_arg_ok_p = saved_default_arg_ok_p;
12057 /* After the declarator, allow more attributes. */
12058 decl_specifiers.attributes
12059 = chainon (decl_specifiers.attributes,
12060 cp_parser_attributes_opt (parser));
12063 /* The restriction on defining new types applies only to the type
12064 of the parameter, not to the default argument. */
12065 parser->type_definition_forbidden_message = saved_message;
12067 /* If the next token is `=', then process a default argument. */
12068 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12070 bool saved_greater_than_is_operator_p;
12071 /* Consume the `='. */
12072 cp_lexer_consume_token (parser->lexer);
12074 /* If we are defining a class, then the tokens that make up the
12075 default argument must be saved and processed later. */
12076 if (!template_parm_p && at_class_scope_p ()
12077 && TYPE_BEING_DEFINED (current_class_type))
12079 unsigned depth = 0;
12080 cp_token *first_token;
12083 /* Add tokens until we have processed the entire default
12084 argument. We add the range [first_token, token). */
12085 first_token = cp_lexer_peek_token (parser->lexer);
12090 /* Peek at the next token. */
12091 token = cp_lexer_peek_token (parser->lexer);
12092 /* What we do depends on what token we have. */
12093 switch (token->type)
12095 /* In valid code, a default argument must be
12096 immediately followed by a `,' `)', or `...'. */
12098 case CPP_CLOSE_PAREN:
12100 /* If we run into a non-nested `;', `}', or `]',
12101 then the code is invalid -- but the default
12102 argument is certainly over. */
12103 case CPP_SEMICOLON:
12104 case CPP_CLOSE_BRACE:
12105 case CPP_CLOSE_SQUARE:
12108 /* Update DEPTH, if necessary. */
12109 else if (token->type == CPP_CLOSE_PAREN
12110 || token->type == CPP_CLOSE_BRACE
12111 || token->type == CPP_CLOSE_SQUARE)
12115 case CPP_OPEN_PAREN:
12116 case CPP_OPEN_SQUARE:
12117 case CPP_OPEN_BRACE:
12122 /* If we see a non-nested `>', and `>' is not an
12123 operator, then it marks the end of the default
12125 if (!depth && !greater_than_is_operator_p)
12129 /* If we run out of tokens, issue an error message. */
12131 error ("file ends in default argument");
12137 /* In these cases, we should look for template-ids.
12138 For example, if the default argument is
12139 `X<int, double>()', we need to do name lookup to
12140 figure out whether or not `X' is a template; if
12141 so, the `,' does not end the default argument.
12143 That is not yet done. */
12150 /* If we've reached the end, stop. */
12154 /* Add the token to the token block. */
12155 token = cp_lexer_consume_token (parser->lexer);
12158 /* Create a DEFAULT_ARG to represented the unparsed default
12160 default_argument = make_node (DEFAULT_ARG);
12161 DEFARG_TOKENS (default_argument)
12162 = cp_token_cache_new (first_token, token);
12163 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12165 /* Outside of a class definition, we can just parse the
12166 assignment-expression. */
12169 bool saved_local_variables_forbidden_p;
12171 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12173 saved_greater_than_is_operator_p
12174 = parser->greater_than_is_operator_p;
12175 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12176 /* Local variable names (and the `this' keyword) may not
12177 appear in a default argument. */
12178 saved_local_variables_forbidden_p
12179 = parser->local_variables_forbidden_p;
12180 parser->local_variables_forbidden_p = true;
12181 /* Parse the assignment-expression. */
12183 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12184 /* Restore saved state. */
12185 parser->greater_than_is_operator_p
12186 = saved_greater_than_is_operator_p;
12187 parser->local_variables_forbidden_p
12188 = saved_local_variables_forbidden_p;
12190 if (!parser->default_arg_ok_p)
12192 if (!flag_pedantic_errors)
12193 warning (0, "deprecated use of default argument for parameter of non-function");
12196 error ("default arguments are only permitted for function parameters");
12197 default_argument = NULL_TREE;
12202 default_argument = NULL_TREE;
12204 return make_parameter_declarator (&decl_specifiers,
12209 /* Parse a function-body.
12212 compound_statement */
12215 cp_parser_function_body (cp_parser *parser)
12217 cp_parser_compound_statement (parser, NULL, false);
12220 /* Parse a ctor-initializer-opt followed by a function-body. Return
12221 true if a ctor-initializer was present. */
12224 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12227 bool ctor_initializer_p;
12229 /* Begin the function body. */
12230 body = begin_function_body ();
12231 /* Parse the optional ctor-initializer. */
12232 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12233 /* Parse the function-body. */
12234 cp_parser_function_body (parser);
12235 /* Finish the function body. */
12236 finish_function_body (body);
12238 return ctor_initializer_p;
12241 /* Parse an initializer.
12244 = initializer-clause
12245 ( expression-list )
12247 Returns an expression representing the initializer. If no
12248 initializer is present, NULL_TREE is returned.
12250 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12251 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12252 set to FALSE if there is no initializer present. If there is an
12253 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12254 is set to true; otherwise it is set to false. */
12257 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12258 bool* non_constant_p)
12263 /* Peek at the next token. */
12264 token = cp_lexer_peek_token (parser->lexer);
12266 /* Let our caller know whether or not this initializer was
12268 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12269 /* Assume that the initializer is constant. */
12270 *non_constant_p = false;
12272 if (token->type == CPP_EQ)
12274 /* Consume the `='. */
12275 cp_lexer_consume_token (parser->lexer);
12276 /* Parse the initializer-clause. */
12277 init = cp_parser_initializer_clause (parser, non_constant_p);
12279 else if (token->type == CPP_OPEN_PAREN)
12280 init = cp_parser_parenthesized_expression_list (parser, false,
12285 /* Anything else is an error. */
12286 cp_parser_error (parser, "expected initializer");
12287 init = error_mark_node;
12293 /* Parse an initializer-clause.
12295 initializer-clause:
12296 assignment-expression
12297 { initializer-list , [opt] }
12300 Returns an expression representing the initializer.
12302 If the `assignment-expression' production is used the value
12303 returned is simply a representation for the expression.
12305 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12306 the elements of the initializer-list (or NULL, if the last
12307 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12308 NULL_TREE. There is no way to detect whether or not the optional
12309 trailing `,' was provided. NON_CONSTANT_P is as for
12310 cp_parser_initializer. */
12313 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12317 /* Assume the expression is constant. */
12318 *non_constant_p = false;
12320 /* If it is not a `{', then we are looking at an
12321 assignment-expression. */
12322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12325 = cp_parser_constant_expression (parser,
12326 /*allow_non_constant_p=*/true,
12328 if (!*non_constant_p)
12329 initializer = fold_non_dependent_expr (initializer);
12333 /* Consume the `{' token. */
12334 cp_lexer_consume_token (parser->lexer);
12335 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12336 initializer = make_node (CONSTRUCTOR);
12337 /* If it's not a `}', then there is a non-trivial initializer. */
12338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12340 /* Parse the initializer list. */
12341 CONSTRUCTOR_ELTS (initializer)
12342 = cp_parser_initializer_list (parser, non_constant_p);
12343 /* A trailing `,' token is allowed. */
12344 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12345 cp_lexer_consume_token (parser->lexer);
12347 /* Now, there should be a trailing `}'. */
12348 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12351 return initializer;
12354 /* Parse an initializer-list.
12358 initializer-list , initializer-clause
12363 identifier : initializer-clause
12364 initializer-list, identifier : initializer-clause
12366 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12367 for the initializer. If the INDEX of the elt is non-NULL, it is the
12368 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12369 as for cp_parser_initializer. */
12371 static VEC(constructor_elt,gc) *
12372 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12374 VEC(constructor_elt,gc) *v = NULL;
12376 /* Assume all of the expressions are constant. */
12377 *non_constant_p = false;
12379 /* Parse the rest of the list. */
12385 bool clause_non_constant_p;
12387 /* If the next token is an identifier and the following one is a
12388 colon, we are looking at the GNU designated-initializer
12390 if (cp_parser_allow_gnu_extensions_p (parser)
12391 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12392 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12394 /* Consume the identifier. */
12395 identifier = cp_lexer_consume_token (parser->lexer)->value;
12396 /* Consume the `:'. */
12397 cp_lexer_consume_token (parser->lexer);
12400 identifier = NULL_TREE;
12402 /* Parse the initializer. */
12403 initializer = cp_parser_initializer_clause (parser,
12404 &clause_non_constant_p);
12405 /* If any clause is non-constant, so is the entire initializer. */
12406 if (clause_non_constant_p)
12407 *non_constant_p = true;
12409 /* Add it to the vector. */
12410 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12412 /* If the next token is not a comma, we have reached the end of
12414 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12417 /* Peek at the next token. */
12418 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12419 /* If the next token is a `}', then we're still done. An
12420 initializer-clause can have a trailing `,' after the
12421 initializer-list and before the closing `}'. */
12422 if (token->type == CPP_CLOSE_BRACE)
12425 /* Consume the `,' token. */
12426 cp_lexer_consume_token (parser->lexer);
12432 /* Classes [gram.class] */
12434 /* Parse a class-name.
12440 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12441 to indicate that names looked up in dependent types should be
12442 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12443 keyword has been used to indicate that the name that appears next
12444 is a template. TAG_TYPE indicates the explicit tag given before
12445 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12446 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12447 is the class being defined in a class-head.
12449 Returns the TYPE_DECL representing the class. */
12452 cp_parser_class_name (cp_parser *parser,
12453 bool typename_keyword_p,
12454 bool template_keyword_p,
12455 enum tag_types tag_type,
12456 bool check_dependency_p,
12458 bool is_declaration)
12465 /* All class-names start with an identifier. */
12466 token = cp_lexer_peek_token (parser->lexer);
12467 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12469 cp_parser_error (parser, "expected class-name");
12470 return error_mark_node;
12473 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12474 to a template-id, so we save it here. */
12475 scope = parser->scope;
12476 if (scope == error_mark_node)
12477 return error_mark_node;
12479 /* Any name names a type if we're following the `typename' keyword
12480 in a qualified name where the enclosing scope is type-dependent. */
12481 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12482 && dependent_type_p (scope));
12483 /* Handle the common case (an identifier, but not a template-id)
12485 if (token->type == CPP_NAME
12486 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12490 /* Look for the identifier. */
12491 identifier = cp_parser_identifier (parser);
12492 /* If the next token isn't an identifier, we are certainly not
12493 looking at a class-name. */
12494 if (identifier == error_mark_node)
12495 decl = error_mark_node;
12496 /* If we know this is a type-name, there's no need to look it
12498 else if (typename_p)
12502 /* If the next token is a `::', then the name must be a type
12505 [basic.lookup.qual]
12507 During the lookup for a name preceding the :: scope
12508 resolution operator, object, function, and enumerator
12509 names are ignored. */
12510 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12511 tag_type = typename_type;
12512 /* Look up the name. */
12513 decl = cp_parser_lookup_name (parser, identifier,
12515 /*is_template=*/false,
12516 /*is_namespace=*/false,
12517 check_dependency_p,
12518 /*ambiguous_p=*/NULL);
12523 /* Try a template-id. */
12524 decl = cp_parser_template_id (parser, template_keyword_p,
12525 check_dependency_p,
12527 if (decl == error_mark_node)
12528 return error_mark_node;
12531 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12533 /* If this is a typename, create a TYPENAME_TYPE. */
12534 if (typename_p && decl != error_mark_node)
12536 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12537 if (decl != error_mark_node)
12538 decl = TYPE_NAME (decl);
12541 /* Check to see that it is really the name of a class. */
12542 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12543 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12544 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12545 /* Situations like this:
12547 template <typename T> struct A {
12548 typename T::template X<int>::I i;
12551 are problematic. Is `T::template X<int>' a class-name? The
12552 standard does not seem to be definitive, but there is no other
12553 valid interpretation of the following `::'. Therefore, those
12554 names are considered class-names. */
12555 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12556 else if (decl == error_mark_node
12557 || TREE_CODE (decl) != TYPE_DECL
12558 || TREE_TYPE (decl) == error_mark_node
12559 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12561 cp_parser_error (parser, "expected class-name");
12562 return error_mark_node;
12568 /* Parse a class-specifier.
12571 class-head { member-specification [opt] }
12573 Returns the TREE_TYPE representing the class. */
12576 cp_parser_class_specifier (cp_parser* parser)
12580 tree attributes = NULL_TREE;
12581 int has_trailing_semicolon;
12582 bool nested_name_specifier_p;
12583 unsigned saved_num_template_parameter_lists;
12584 tree old_scope = NULL_TREE;
12585 tree scope = NULL_TREE;
12587 push_deferring_access_checks (dk_no_deferred);
12589 /* Parse the class-head. */
12590 type = cp_parser_class_head (parser,
12591 &nested_name_specifier_p,
12593 /* If the class-head was a semantic disaster, skip the entire body
12597 cp_parser_skip_to_end_of_block_or_statement (parser);
12598 pop_deferring_access_checks ();
12599 return error_mark_node;
12602 /* Look for the `{'. */
12603 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12605 pop_deferring_access_checks ();
12606 return error_mark_node;
12609 /* Issue an error message if type-definitions are forbidden here. */
12610 cp_parser_check_type_definition (parser);
12611 /* Remember that we are defining one more class. */
12612 ++parser->num_classes_being_defined;
12613 /* Inside the class, surrounding template-parameter-lists do not
12615 saved_num_template_parameter_lists
12616 = parser->num_template_parameter_lists;
12617 parser->num_template_parameter_lists = 0;
12619 /* Start the class. */
12620 if (nested_name_specifier_p)
12622 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12623 old_scope = push_inner_scope (scope);
12625 type = begin_class_definition (type);
12627 if (type == error_mark_node)
12628 /* If the type is erroneous, skip the entire body of the class. */
12629 cp_parser_skip_to_closing_brace (parser);
12631 /* Parse the member-specification. */
12632 cp_parser_member_specification_opt (parser);
12634 /* Look for the trailing `}'. */
12635 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12636 /* We get better error messages by noticing a common problem: a
12637 missing trailing `;'. */
12638 token = cp_lexer_peek_token (parser->lexer);
12639 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12640 /* Look for trailing attributes to apply to this class. */
12641 if (cp_parser_allow_gnu_extensions_p (parser))
12643 tree sub_attr = cp_parser_attributes_opt (parser);
12644 attributes = chainon (attributes, sub_attr);
12646 if (type != error_mark_node)
12647 type = finish_struct (type, attributes);
12648 if (nested_name_specifier_p)
12649 pop_inner_scope (old_scope, scope);
12650 /* If this class is not itself within the scope of another class,
12651 then we need to parse the bodies of all of the queued function
12652 definitions. Note that the queued functions defined in a class
12653 are not always processed immediately following the
12654 class-specifier for that class. Consider:
12657 struct B { void f() { sizeof (A); } };
12660 If `f' were processed before the processing of `A' were
12661 completed, there would be no way to compute the size of `A'.
12662 Note that the nesting we are interested in here is lexical --
12663 not the semantic nesting given by TYPE_CONTEXT. In particular,
12666 struct A { struct B; };
12667 struct A::B { void f() { } };
12669 there is no need to delay the parsing of `A::B::f'. */
12670 if (--parser->num_classes_being_defined == 0)
12674 tree class_type = NULL_TREE;
12675 tree pushed_scope = NULL_TREE;
12677 /* In a first pass, parse default arguments to the functions.
12678 Then, in a second pass, parse the bodies of the functions.
12679 This two-phased approach handles cases like:
12687 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12688 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12689 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12690 TREE_PURPOSE (parser->unparsed_functions_queues)
12691 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12693 fn = TREE_VALUE (queue_entry);
12694 /* If there are default arguments that have not yet been processed,
12695 take care of them now. */
12696 if (class_type != TREE_PURPOSE (queue_entry))
12699 pop_scope (pushed_scope);
12700 class_type = TREE_PURPOSE (queue_entry);
12701 pushed_scope = push_scope (class_type);
12703 /* Make sure that any template parameters are in scope. */
12704 maybe_begin_member_template_processing (fn);
12705 /* Parse the default argument expressions. */
12706 cp_parser_late_parsing_default_args (parser, fn);
12707 /* Remove any template parameters from the symbol table. */
12708 maybe_end_member_template_processing ();
12711 pop_scope (pushed_scope);
12712 /* Now parse the body of the functions. */
12713 for (TREE_VALUE (parser->unparsed_functions_queues)
12714 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12715 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12716 TREE_VALUE (parser->unparsed_functions_queues)
12717 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12719 /* Figure out which function we need to process. */
12720 fn = TREE_VALUE (queue_entry);
12721 /* Parse the function. */
12722 cp_parser_late_parsing_for_member (parser, fn);
12726 /* Put back any saved access checks. */
12727 pop_deferring_access_checks ();
12729 /* Restore the count of active template-parameter-lists. */
12730 parser->num_template_parameter_lists
12731 = saved_num_template_parameter_lists;
12736 /* Parse a class-head.
12739 class-key identifier [opt] base-clause [opt]
12740 class-key nested-name-specifier identifier base-clause [opt]
12741 class-key nested-name-specifier [opt] template-id
12745 class-key attributes identifier [opt] base-clause [opt]
12746 class-key attributes nested-name-specifier identifier base-clause [opt]
12747 class-key attributes nested-name-specifier [opt] template-id
12750 Returns the TYPE of the indicated class. Sets
12751 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12752 involving a nested-name-specifier was used, and FALSE otherwise.
12754 Returns error_mark_node if this is not a class-head.
12756 Returns NULL_TREE if the class-head is syntactically valid, but
12757 semantically invalid in a way that means we should skip the entire
12758 body of the class. */
12761 cp_parser_class_head (cp_parser* parser,
12762 bool* nested_name_specifier_p,
12763 tree *attributes_p)
12765 tree nested_name_specifier;
12766 enum tag_types class_key;
12767 tree id = NULL_TREE;
12768 tree type = NULL_TREE;
12770 bool template_id_p = false;
12771 bool qualified_p = false;
12772 bool invalid_nested_name_p = false;
12773 bool invalid_explicit_specialization_p = false;
12774 tree pushed_scope = NULL_TREE;
12775 unsigned num_templates;
12778 /* Assume no nested-name-specifier will be present. */
12779 *nested_name_specifier_p = false;
12780 /* Assume no template parameter lists will be used in defining the
12784 /* Look for the class-key. */
12785 class_key = cp_parser_class_key (parser);
12786 if (class_key == none_type)
12787 return error_mark_node;
12789 /* Parse the attributes. */
12790 attributes = cp_parser_attributes_opt (parser);
12792 /* If the next token is `::', that is invalid -- but sometimes
12793 people do try to write:
12797 Handle this gracefully by accepting the extra qualifier, and then
12798 issuing an error about it later if this really is a
12799 class-head. If it turns out just to be an elaborated type
12800 specifier, remain silent. */
12801 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12802 qualified_p = true;
12804 push_deferring_access_checks (dk_no_check);
12806 /* Determine the name of the class. Begin by looking for an
12807 optional nested-name-specifier. */
12808 nested_name_specifier
12809 = cp_parser_nested_name_specifier_opt (parser,
12810 /*typename_keyword_p=*/false,
12811 /*check_dependency_p=*/false,
12813 /*is_declaration=*/false);
12814 /* If there was a nested-name-specifier, then there *must* be an
12816 if (nested_name_specifier)
12818 /* Although the grammar says `identifier', it really means
12819 `class-name' or `template-name'. You are only allowed to
12820 define a class that has already been declared with this
12823 The proposed resolution for Core Issue 180 says that whever
12824 you see `class T::X' you should treat `X' as a type-name.
12826 It is OK to define an inaccessible class; for example:
12828 class A { class B; };
12831 We do not know if we will see a class-name, or a
12832 template-name. We look for a class-name first, in case the
12833 class-name is a template-id; if we looked for the
12834 template-name first we would stop after the template-name. */
12835 cp_parser_parse_tentatively (parser);
12836 type = cp_parser_class_name (parser,
12837 /*typename_keyword_p=*/false,
12838 /*template_keyword_p=*/false,
12840 /*check_dependency_p=*/false,
12841 /*class_head_p=*/true,
12842 /*is_declaration=*/false);
12843 /* If that didn't work, ignore the nested-name-specifier. */
12844 if (!cp_parser_parse_definitely (parser))
12846 invalid_nested_name_p = true;
12847 id = cp_parser_identifier (parser);
12848 if (id == error_mark_node)
12851 /* If we could not find a corresponding TYPE, treat this
12852 declaration like an unqualified declaration. */
12853 if (type == error_mark_node)
12854 nested_name_specifier = NULL_TREE;
12855 /* Otherwise, count the number of templates used in TYPE and its
12856 containing scopes. */
12861 for (scope = TREE_TYPE (type);
12862 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12863 scope = (TYPE_P (scope)
12864 ? TYPE_CONTEXT (scope)
12865 : DECL_CONTEXT (scope)))
12867 && CLASS_TYPE_P (scope)
12868 && CLASSTYPE_TEMPLATE_INFO (scope)
12869 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12870 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12874 /* Otherwise, the identifier is optional. */
12877 /* We don't know whether what comes next is a template-id,
12878 an identifier, or nothing at all. */
12879 cp_parser_parse_tentatively (parser);
12880 /* Check for a template-id. */
12881 id = cp_parser_template_id (parser,
12882 /*template_keyword_p=*/false,
12883 /*check_dependency_p=*/true,
12884 /*is_declaration=*/true);
12885 /* If that didn't work, it could still be an identifier. */
12886 if (!cp_parser_parse_definitely (parser))
12888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12889 id = cp_parser_identifier (parser);
12895 template_id_p = true;
12900 pop_deferring_access_checks ();
12903 cp_parser_check_for_invalid_template_id (parser, id);
12905 /* If it's not a `:' or a `{' then we can't really be looking at a
12906 class-head, since a class-head only appears as part of a
12907 class-specifier. We have to detect this situation before calling
12908 xref_tag, since that has irreversible side-effects. */
12909 if (!cp_parser_next_token_starts_class_definition_p (parser))
12911 cp_parser_error (parser, "expected %<{%> or %<:%>");
12912 return error_mark_node;
12915 /* At this point, we're going ahead with the class-specifier, even
12916 if some other problem occurs. */
12917 cp_parser_commit_to_tentative_parse (parser);
12918 /* Issue the error about the overly-qualified name now. */
12920 cp_parser_error (parser,
12921 "global qualification of class name is invalid");
12922 else if (invalid_nested_name_p)
12923 cp_parser_error (parser,
12924 "qualified name does not name a class");
12925 else if (nested_name_specifier)
12929 /* Reject typedef-names in class heads. */
12930 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12932 error ("invalid class name in declaration of %qD", type);
12937 /* Figure out in what scope the declaration is being placed. */
12938 scope = current_scope ();
12939 /* If that scope does not contain the scope in which the
12940 class was originally declared, the program is invalid. */
12941 if (scope && !is_ancestor (scope, nested_name_specifier))
12943 error ("declaration of %qD in %qD which does not enclose %qD",
12944 type, scope, nested_name_specifier);
12950 A declarator-id shall not be qualified exception of the
12951 definition of a ... nested class outside of its class
12952 ... [or] a the definition or explicit instantiation of a
12953 class member of a namespace outside of its namespace. */
12954 if (scope == nested_name_specifier)
12956 pedwarn ("extra qualification ignored");
12957 nested_name_specifier = NULL_TREE;
12961 /* An explicit-specialization must be preceded by "template <>". If
12962 it is not, try to recover gracefully. */
12963 if (at_namespace_scope_p ()
12964 && parser->num_template_parameter_lists == 0
12967 error ("an explicit specialization must be preceded by %<template <>%>");
12968 invalid_explicit_specialization_p = true;
12969 /* Take the same action that would have been taken by
12970 cp_parser_explicit_specialization. */
12971 ++parser->num_template_parameter_lists;
12972 begin_specialization ();
12974 /* There must be no "return" statements between this point and the
12975 end of this function; set "type "to the correct return value and
12976 use "goto done;" to return. */
12977 /* Make sure that the right number of template parameters were
12979 if (!cp_parser_check_template_parameters (parser, num_templates))
12981 /* If something went wrong, there is no point in even trying to
12982 process the class-definition. */
12987 /* Look up the type. */
12990 type = TREE_TYPE (id);
12991 maybe_process_partial_specialization (type);
12992 if (nested_name_specifier)
12993 pushed_scope = push_scope (nested_name_specifier);
12995 else if (nested_name_specifier)
13001 template <typename T> struct S { struct T };
13002 template <typename T> struct S<T>::T { };
13004 we will get a TYPENAME_TYPE when processing the definition of
13005 `S::T'. We need to resolve it to the actual type before we
13006 try to define it. */
13007 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13009 class_type = resolve_typename_type (TREE_TYPE (type),
13010 /*only_current_p=*/false);
13011 if (class_type != error_mark_node)
13012 type = TYPE_NAME (class_type);
13015 cp_parser_error (parser, "could not resolve typename type");
13016 type = error_mark_node;
13020 maybe_process_partial_specialization (TREE_TYPE (type));
13021 class_type = current_class_type;
13022 /* Enter the scope indicated by the nested-name-specifier. */
13023 pushed_scope = push_scope (nested_name_specifier);
13024 /* Get the canonical version of this type. */
13025 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13026 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13027 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13029 type = push_template_decl (type);
13030 if (type == error_mark_node)
13037 type = TREE_TYPE (type);
13038 *nested_name_specifier_p = true;
13040 else /* The name is not a nested name. */
13042 /* If the class was unnamed, create a dummy name. */
13044 id = make_anon_name ();
13045 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13046 parser->num_template_parameter_lists);
13049 /* Indicate whether this class was declared as a `class' or as a
13051 if (TREE_CODE (type) == RECORD_TYPE)
13052 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13053 cp_parser_check_class_key (class_key, type);
13055 /* If this type was already complete, and we see another definition,
13056 that's an error. */
13057 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13059 error ("redefinition of %q#T", type);
13060 error ("previous definition of %q+#T", type);
13065 /* We will have entered the scope containing the class; the names of
13066 base classes should be looked up in that context. For example:
13068 struct A { struct B {}; struct C; };
13069 struct A::C : B {};
13074 /* Get the list of base-classes, if there is one. */
13075 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13076 bases = cp_parser_base_clause (parser);
13078 /* Process the base classes. */
13079 xref_basetypes (type, bases);
13082 /* Leave the scope given by the nested-name-specifier. We will
13083 enter the class scope itself while processing the members. */
13085 pop_scope (pushed_scope);
13087 if (invalid_explicit_specialization_p)
13089 end_specialization ();
13090 --parser->num_template_parameter_lists;
13092 *attributes_p = attributes;
13096 /* Parse a class-key.
13103 Returns the kind of class-key specified, or none_type to indicate
13106 static enum tag_types
13107 cp_parser_class_key (cp_parser* parser)
13110 enum tag_types tag_type;
13112 /* Look for the class-key. */
13113 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13117 /* Check to see if the TOKEN is a class-key. */
13118 tag_type = cp_parser_token_is_class_key (token);
13120 cp_parser_error (parser, "expected class-key");
13124 /* Parse an (optional) member-specification.
13126 member-specification:
13127 member-declaration member-specification [opt]
13128 access-specifier : member-specification [opt] */
13131 cp_parser_member_specification_opt (cp_parser* parser)
13138 /* Peek at the next token. */
13139 token = cp_lexer_peek_token (parser->lexer);
13140 /* If it's a `}', or EOF then we've seen all the members. */
13141 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13144 /* See if this token is a keyword. */
13145 keyword = token->keyword;
13149 case RID_PROTECTED:
13151 /* Consume the access-specifier. */
13152 cp_lexer_consume_token (parser->lexer);
13153 /* Remember which access-specifier is active. */
13154 current_access_specifier = token->value;
13155 /* Look for the `:'. */
13156 cp_parser_require (parser, CPP_COLON, "`:'");
13160 /* Accept #pragmas at class scope. */
13161 if (token->type == CPP_PRAGMA)
13163 cp_lexer_handle_pragma (parser->lexer);
13167 /* Otherwise, the next construction must be a
13168 member-declaration. */
13169 cp_parser_member_declaration (parser);
13174 /* Parse a member-declaration.
13176 member-declaration:
13177 decl-specifier-seq [opt] member-declarator-list [opt] ;
13178 function-definition ; [opt]
13179 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13181 template-declaration
13183 member-declarator-list:
13185 member-declarator-list , member-declarator
13188 declarator pure-specifier [opt]
13189 declarator constant-initializer [opt]
13190 identifier [opt] : constant-expression
13194 member-declaration:
13195 __extension__ member-declaration
13198 declarator attributes [opt] pure-specifier [opt]
13199 declarator attributes [opt] constant-initializer [opt]
13200 identifier [opt] attributes [opt] : constant-expression */
13203 cp_parser_member_declaration (cp_parser* parser)
13205 cp_decl_specifier_seq decl_specifiers;
13206 tree prefix_attributes;
13208 int declares_class_or_enum;
13211 int saved_pedantic;
13213 /* Check for the `__extension__' keyword. */
13214 if (cp_parser_extension_opt (parser, &saved_pedantic))
13217 cp_parser_member_declaration (parser);
13218 /* Restore the old value of the PEDANTIC flag. */
13219 pedantic = saved_pedantic;
13224 /* Check for a template-declaration. */
13225 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13227 /* Parse the template-declaration. */
13228 cp_parser_template_declaration (parser, /*member_p=*/true);
13233 /* Check for a using-declaration. */
13234 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13236 /* Parse the using-declaration. */
13237 cp_parser_using_declaration (parser);
13242 /* Check for @defs. */
13243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13246 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13247 ivar = ivar_chains;
13251 ivar = TREE_CHAIN (member);
13252 TREE_CHAIN (member) = NULL_TREE;
13253 finish_member_declaration (member);
13258 /* Parse the decl-specifier-seq. */
13259 cp_parser_decl_specifier_seq (parser,
13260 CP_PARSER_FLAGS_OPTIONAL,
13262 &declares_class_or_enum);
13263 prefix_attributes = decl_specifiers.attributes;
13264 decl_specifiers.attributes = NULL_TREE;
13265 /* Check for an invalid type-name. */
13266 if (!decl_specifiers.type
13267 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13269 /* If there is no declarator, then the decl-specifier-seq should
13271 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13273 /* If there was no decl-specifier-seq, and the next token is a
13274 `;', then we have something like:
13280 Each member-declaration shall declare at least one member
13281 name of the class. */
13282 if (!decl_specifiers.any_specifiers_p)
13284 cp_token *token = cp_lexer_peek_token (parser->lexer);
13285 if (pedantic && !token->in_system_header)
13286 pedwarn ("%Hextra %<;%>", &token->location);
13292 /* See if this declaration is a friend. */
13293 friend_p = cp_parser_friend_p (&decl_specifiers);
13294 /* If there were decl-specifiers, check to see if there was
13295 a class-declaration. */
13296 type = check_tag_decl (&decl_specifiers);
13297 /* Nested classes have already been added to the class, but
13298 a `friend' needs to be explicitly registered. */
13301 /* If the `friend' keyword was present, the friend must
13302 be introduced with a class-key. */
13303 if (!declares_class_or_enum)
13304 error ("a class-key must be used when declaring a friend");
13307 template <typename T> struct A {
13308 friend struct A<T>::B;
13311 A<T>::B will be represented by a TYPENAME_TYPE, and
13312 therefore not recognized by check_tag_decl. */
13314 && decl_specifiers.type
13315 && TYPE_P (decl_specifiers.type))
13316 type = decl_specifiers.type;
13317 if (!type || !TYPE_P (type))
13318 error ("friend declaration does not name a class or "
13321 make_friend_class (current_class_type, type,
13322 /*complain=*/true);
13324 /* If there is no TYPE, an error message will already have
13326 else if (!type || type == error_mark_node)
13328 /* An anonymous aggregate has to be handled specially; such
13329 a declaration really declares a data member (with a
13330 particular type), as opposed to a nested class. */
13331 else if (ANON_AGGR_TYPE_P (type))
13333 /* Remove constructors and such from TYPE, now that we
13334 know it is an anonymous aggregate. */
13335 fixup_anonymous_aggr (type);
13336 /* And make the corresponding data member. */
13337 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13338 /* Add it to the class. */
13339 finish_member_declaration (decl);
13342 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13347 /* See if these declarations will be friends. */
13348 friend_p = cp_parser_friend_p (&decl_specifiers);
13350 /* Keep going until we hit the `;' at the end of the
13352 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13354 tree attributes = NULL_TREE;
13355 tree first_attribute;
13357 /* Peek at the next token. */
13358 token = cp_lexer_peek_token (parser->lexer);
13360 /* Check for a bitfield declaration. */
13361 if (token->type == CPP_COLON
13362 || (token->type == CPP_NAME
13363 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13369 /* Get the name of the bitfield. Note that we cannot just
13370 check TOKEN here because it may have been invalidated by
13371 the call to cp_lexer_peek_nth_token above. */
13372 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13373 identifier = cp_parser_identifier (parser);
13375 identifier = NULL_TREE;
13377 /* Consume the `:' token. */
13378 cp_lexer_consume_token (parser->lexer);
13379 /* Get the width of the bitfield. */
13381 = cp_parser_constant_expression (parser,
13382 /*allow_non_constant=*/false,
13385 /* Look for attributes that apply to the bitfield. */
13386 attributes = cp_parser_attributes_opt (parser);
13387 /* Remember which attributes are prefix attributes and
13389 first_attribute = attributes;
13390 /* Combine the attributes. */
13391 attributes = chainon (prefix_attributes, attributes);
13393 /* Create the bitfield declaration. */
13394 decl = grokbitfield (identifier
13395 ? make_id_declarator (NULL_TREE,
13400 /* Apply the attributes. */
13401 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13405 cp_declarator *declarator;
13407 tree asm_specification;
13408 int ctor_dtor_or_conv_p;
13410 /* Parse the declarator. */
13412 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13413 &ctor_dtor_or_conv_p,
13414 /*parenthesized_p=*/NULL,
13415 /*member_p=*/true);
13417 /* If something went wrong parsing the declarator, make sure
13418 that we at least consume some tokens. */
13419 if (declarator == cp_error_declarator)
13421 /* Skip to the end of the statement. */
13422 cp_parser_skip_to_end_of_statement (parser);
13423 /* If the next token is not a semicolon, that is
13424 probably because we just skipped over the body of
13425 a function. So, we consume a semicolon if
13426 present, but do not issue an error message if it
13428 if (cp_lexer_next_token_is (parser->lexer,
13430 cp_lexer_consume_token (parser->lexer);
13434 if (declares_class_or_enum & 2)
13435 cp_parser_check_for_definition_in_return_type
13436 (declarator, decl_specifiers.type);
13438 /* Look for an asm-specification. */
13439 asm_specification = cp_parser_asm_specification_opt (parser);
13440 /* Look for attributes that apply to the declaration. */
13441 attributes = cp_parser_attributes_opt (parser);
13442 /* Remember which attributes are prefix attributes and
13444 first_attribute = attributes;
13445 /* Combine the attributes. */
13446 attributes = chainon (prefix_attributes, attributes);
13448 /* If it's an `=', then we have a constant-initializer or a
13449 pure-specifier. It is not correct to parse the
13450 initializer before registering the member declaration
13451 since the member declaration should be in scope while
13452 its initializer is processed. However, the rest of the
13453 front end does not yet provide an interface that allows
13454 us to handle this correctly. */
13455 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13459 A pure-specifier shall be used only in the declaration of
13460 a virtual function.
13462 A member-declarator can contain a constant-initializer
13463 only if it declares a static member of integral or
13466 Therefore, if the DECLARATOR is for a function, we look
13467 for a pure-specifier; otherwise, we look for a
13468 constant-initializer. When we call `grokfield', it will
13469 perform more stringent semantics checks. */
13470 if (declarator->kind == cdk_function)
13471 initializer = cp_parser_pure_specifier (parser);
13473 /* Parse the initializer. */
13474 initializer = cp_parser_constant_initializer (parser);
13476 /* Otherwise, there is no initializer. */
13478 initializer = NULL_TREE;
13480 /* See if we are probably looking at a function
13481 definition. We are certainly not looking at a
13482 member-declarator. Calling `grokfield' has
13483 side-effects, so we must not do it unless we are sure
13484 that we are looking at a member-declarator. */
13485 if (cp_parser_token_starts_function_definition_p
13486 (cp_lexer_peek_token (parser->lexer)))
13488 /* The grammar does not allow a pure-specifier to be
13489 used when a member function is defined. (It is
13490 possible that this fact is an oversight in the
13491 standard, since a pure function may be defined
13492 outside of the class-specifier. */
13494 error ("pure-specifier on function-definition");
13495 decl = cp_parser_save_member_function_body (parser,
13499 /* If the member was not a friend, declare it here. */
13501 finish_member_declaration (decl);
13502 /* Peek at the next token. */
13503 token = cp_lexer_peek_token (parser->lexer);
13504 /* If the next token is a semicolon, consume it. */
13505 if (token->type == CPP_SEMICOLON)
13506 cp_lexer_consume_token (parser->lexer);
13511 /* Create the declaration. */
13512 decl = grokfield (declarator, &decl_specifiers,
13513 initializer, asm_specification,
13515 /* Any initialization must have been from a
13516 constant-expression. */
13517 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13518 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13522 /* Reset PREFIX_ATTRIBUTES. */
13523 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13524 attributes = TREE_CHAIN (attributes);
13526 TREE_CHAIN (attributes) = NULL_TREE;
13528 /* If there is any qualification still in effect, clear it
13529 now; we will be starting fresh with the next declarator. */
13530 parser->scope = NULL_TREE;
13531 parser->qualifying_scope = NULL_TREE;
13532 parser->object_scope = NULL_TREE;
13533 /* If it's a `,', then there are more declarators. */
13534 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13535 cp_lexer_consume_token (parser->lexer);
13536 /* If the next token isn't a `;', then we have a parse error. */
13537 else if (cp_lexer_next_token_is_not (parser->lexer,
13540 cp_parser_error (parser, "expected %<;%>");
13541 /* Skip tokens until we find a `;'. */
13542 cp_parser_skip_to_end_of_statement (parser);
13549 /* Add DECL to the list of members. */
13551 finish_member_declaration (decl);
13553 if (TREE_CODE (decl) == FUNCTION_DECL)
13554 cp_parser_save_default_args (parser, decl);
13559 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13562 /* Parse a pure-specifier.
13567 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13568 Otherwise, ERROR_MARK_NODE is returned. */
13571 cp_parser_pure_specifier (cp_parser* parser)
13575 /* Look for the `=' token. */
13576 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13577 return error_mark_node;
13578 /* Look for the `0' token. */
13579 token = cp_lexer_consume_token (parser->lexer);
13580 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13582 cp_parser_error (parser,
13583 "invalid pure specifier (only `= 0' is allowed)");
13584 cp_parser_skip_to_end_of_statement (parser);
13585 return error_mark_node;
13588 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13589 We need to get information from the lexer about how the number
13590 was spelled in order to fix this problem. */
13591 return integer_zero_node;
13594 /* Parse a constant-initializer.
13596 constant-initializer:
13597 = constant-expression
13599 Returns a representation of the constant-expression. */
13602 cp_parser_constant_initializer (cp_parser* parser)
13604 /* Look for the `=' token. */
13605 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13606 return error_mark_node;
13608 /* It is invalid to write:
13610 struct S { static const int i = { 7 }; };
13613 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13615 cp_parser_error (parser,
13616 "a brace-enclosed initializer is not allowed here");
13617 /* Consume the opening brace. */
13618 cp_lexer_consume_token (parser->lexer);
13619 /* Skip the initializer. */
13620 cp_parser_skip_to_closing_brace (parser);
13621 /* Look for the trailing `}'. */
13622 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13624 return error_mark_node;
13627 return cp_parser_constant_expression (parser,
13628 /*allow_non_constant=*/false,
13632 /* Derived classes [gram.class.derived] */
13634 /* Parse a base-clause.
13637 : base-specifier-list
13639 base-specifier-list:
13641 base-specifier-list , base-specifier
13643 Returns a TREE_LIST representing the base-classes, in the order in
13644 which they were declared. The representation of each node is as
13645 described by cp_parser_base_specifier.
13647 In the case that no bases are specified, this function will return
13648 NULL_TREE, not ERROR_MARK_NODE. */
13651 cp_parser_base_clause (cp_parser* parser)
13653 tree bases = NULL_TREE;
13655 /* Look for the `:' that begins the list. */
13656 cp_parser_require (parser, CPP_COLON, "`:'");
13658 /* Scan the base-specifier-list. */
13664 /* Look for the base-specifier. */
13665 base = cp_parser_base_specifier (parser);
13666 /* Add BASE to the front of the list. */
13667 if (base != error_mark_node)
13669 TREE_CHAIN (base) = bases;
13672 /* Peek at the next token. */
13673 token = cp_lexer_peek_token (parser->lexer);
13674 /* If it's not a comma, then the list is complete. */
13675 if (token->type != CPP_COMMA)
13677 /* Consume the `,'. */
13678 cp_lexer_consume_token (parser->lexer);
13681 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13682 base class had a qualified name. However, the next name that
13683 appears is certainly not qualified. */
13684 parser->scope = NULL_TREE;
13685 parser->qualifying_scope = NULL_TREE;
13686 parser->object_scope = NULL_TREE;
13688 return nreverse (bases);
13691 /* Parse a base-specifier.
13694 :: [opt] nested-name-specifier [opt] class-name
13695 virtual access-specifier [opt] :: [opt] nested-name-specifier
13697 access-specifier virtual [opt] :: [opt] nested-name-specifier
13700 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13701 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13702 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13703 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13706 cp_parser_base_specifier (cp_parser* parser)
13710 bool virtual_p = false;
13711 bool duplicate_virtual_error_issued_p = false;
13712 bool duplicate_access_error_issued_p = false;
13713 bool class_scope_p, template_p;
13714 tree access = access_default_node;
13717 /* Process the optional `virtual' and `access-specifier'. */
13720 /* Peek at the next token. */
13721 token = cp_lexer_peek_token (parser->lexer);
13722 /* Process `virtual'. */
13723 switch (token->keyword)
13726 /* If `virtual' appears more than once, issue an error. */
13727 if (virtual_p && !duplicate_virtual_error_issued_p)
13729 cp_parser_error (parser,
13730 "%<virtual%> specified more than once in base-specified");
13731 duplicate_virtual_error_issued_p = true;
13736 /* Consume the `virtual' token. */
13737 cp_lexer_consume_token (parser->lexer);
13742 case RID_PROTECTED:
13744 /* If more than one access specifier appears, issue an
13746 if (access != access_default_node
13747 && !duplicate_access_error_issued_p)
13749 cp_parser_error (parser,
13750 "more than one access specifier in base-specified");
13751 duplicate_access_error_issued_p = true;
13754 access = ridpointers[(int) token->keyword];
13756 /* Consume the access-specifier. */
13757 cp_lexer_consume_token (parser->lexer);
13766 /* It is not uncommon to see programs mechanically, erroneously, use
13767 the 'typename' keyword to denote (dependent) qualified types
13768 as base classes. */
13769 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13771 if (!processing_template_decl)
13772 error ("keyword %<typename%> not allowed outside of templates");
13774 error ("keyword %<typename%> not allowed in this context "
13775 "(the base class is implicitly a type)");
13776 cp_lexer_consume_token (parser->lexer);
13779 /* Look for the optional `::' operator. */
13780 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13781 /* Look for the nested-name-specifier. The simplest way to
13786 The keyword `typename' is not permitted in a base-specifier or
13787 mem-initializer; in these contexts a qualified name that
13788 depends on a template-parameter is implicitly assumed to be a
13791 is to pretend that we have seen the `typename' keyword at this
13793 cp_parser_nested_name_specifier_opt (parser,
13794 /*typename_keyword_p=*/true,
13795 /*check_dependency_p=*/true,
13797 /*is_declaration=*/true);
13798 /* If the base class is given by a qualified name, assume that names
13799 we see are type names or templates, as appropriate. */
13800 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13801 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13803 /* Finally, look for the class-name. */
13804 type = cp_parser_class_name (parser,
13808 /*check_dependency_p=*/true,
13809 /*class_head_p=*/false,
13810 /*is_declaration=*/true);
13812 if (type == error_mark_node)
13813 return error_mark_node;
13815 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13818 /* Exception handling [gram.exception] */
13820 /* Parse an (optional) exception-specification.
13822 exception-specification:
13823 throw ( type-id-list [opt] )
13825 Returns a TREE_LIST representing the exception-specification. The
13826 TREE_VALUE of each node is a type. */
13829 cp_parser_exception_specification_opt (cp_parser* parser)
13834 /* Peek at the next token. */
13835 token = cp_lexer_peek_token (parser->lexer);
13836 /* If it's not `throw', then there's no exception-specification. */
13837 if (!cp_parser_is_keyword (token, RID_THROW))
13840 /* Consume the `throw'. */
13841 cp_lexer_consume_token (parser->lexer);
13843 /* Look for the `('. */
13844 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13846 /* Peek at the next token. */
13847 token = cp_lexer_peek_token (parser->lexer);
13848 /* If it's not a `)', then there is a type-id-list. */
13849 if (token->type != CPP_CLOSE_PAREN)
13851 const char *saved_message;
13853 /* Types may not be defined in an exception-specification. */
13854 saved_message = parser->type_definition_forbidden_message;
13855 parser->type_definition_forbidden_message
13856 = "types may not be defined in an exception-specification";
13857 /* Parse the type-id-list. */
13858 type_id_list = cp_parser_type_id_list (parser);
13859 /* Restore the saved message. */
13860 parser->type_definition_forbidden_message = saved_message;
13863 type_id_list = empty_except_spec;
13865 /* Look for the `)'. */
13866 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13868 return type_id_list;
13871 /* Parse an (optional) type-id-list.
13875 type-id-list , type-id
13877 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13878 in the order that the types were presented. */
13881 cp_parser_type_id_list (cp_parser* parser)
13883 tree types = NULL_TREE;
13890 /* Get the next type-id. */
13891 type = cp_parser_type_id (parser);
13892 /* Add it to the list. */
13893 types = add_exception_specifier (types, type, /*complain=*/1);
13894 /* Peek at the next token. */
13895 token = cp_lexer_peek_token (parser->lexer);
13896 /* If it is not a `,', we are done. */
13897 if (token->type != CPP_COMMA)
13899 /* Consume the `,'. */
13900 cp_lexer_consume_token (parser->lexer);
13903 return nreverse (types);
13906 /* Parse a try-block.
13909 try compound-statement handler-seq */
13912 cp_parser_try_block (cp_parser* parser)
13916 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13917 try_block = begin_try_block ();
13918 cp_parser_compound_statement (parser, NULL, true);
13919 finish_try_block (try_block);
13920 cp_parser_handler_seq (parser);
13921 finish_handler_sequence (try_block);
13926 /* Parse a function-try-block.
13928 function-try-block:
13929 try ctor-initializer [opt] function-body handler-seq */
13932 cp_parser_function_try_block (cp_parser* parser)
13935 bool ctor_initializer_p;
13937 /* Look for the `try' keyword. */
13938 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13940 /* Let the rest of the front-end know where we are. */
13941 try_block = begin_function_try_block ();
13942 /* Parse the function-body. */
13944 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13945 /* We're done with the `try' part. */
13946 finish_function_try_block (try_block);
13947 /* Parse the handlers. */
13948 cp_parser_handler_seq (parser);
13949 /* We're done with the handlers. */
13950 finish_function_handler_sequence (try_block);
13952 return ctor_initializer_p;
13955 /* Parse a handler-seq.
13958 handler handler-seq [opt] */
13961 cp_parser_handler_seq (cp_parser* parser)
13967 /* Parse the handler. */
13968 cp_parser_handler (parser);
13969 /* Peek at the next token. */
13970 token = cp_lexer_peek_token (parser->lexer);
13971 /* If it's not `catch' then there are no more handlers. */
13972 if (!cp_parser_is_keyword (token, RID_CATCH))
13977 /* Parse a handler.
13980 catch ( exception-declaration ) compound-statement */
13983 cp_parser_handler (cp_parser* parser)
13988 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13989 handler = begin_handler ();
13990 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13991 declaration = cp_parser_exception_declaration (parser);
13992 finish_handler_parms (declaration, handler);
13993 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13994 cp_parser_compound_statement (parser, NULL, false);
13995 finish_handler (handler);
13998 /* Parse an exception-declaration.
14000 exception-declaration:
14001 type-specifier-seq declarator
14002 type-specifier-seq abstract-declarator
14006 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14007 ellipsis variant is used. */
14010 cp_parser_exception_declaration (cp_parser* parser)
14013 cp_decl_specifier_seq type_specifiers;
14014 cp_declarator *declarator;
14015 const char *saved_message;
14017 /* If it's an ellipsis, it's easy to handle. */
14018 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14020 /* Consume the `...' token. */
14021 cp_lexer_consume_token (parser->lexer);
14025 /* Types may not be defined in exception-declarations. */
14026 saved_message = parser->type_definition_forbidden_message;
14027 parser->type_definition_forbidden_message
14028 = "types may not be defined in exception-declarations";
14030 /* Parse the type-specifier-seq. */
14031 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14033 /* If it's a `)', then there is no declarator. */
14034 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14037 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14038 /*ctor_dtor_or_conv_p=*/NULL,
14039 /*parenthesized_p=*/NULL,
14040 /*member_p=*/false);
14042 /* Restore the saved message. */
14043 parser->type_definition_forbidden_message = saved_message;
14045 if (type_specifiers.any_specifiers_p)
14047 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14048 if (decl == NULL_TREE)
14049 error ("invalid catch parameter");
14057 /* Parse a throw-expression.
14060 throw assignment-expression [opt]
14062 Returns a THROW_EXPR representing the throw-expression. */
14065 cp_parser_throw_expression (cp_parser* parser)
14070 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14071 token = cp_lexer_peek_token (parser->lexer);
14072 /* Figure out whether or not there is an assignment-expression
14073 following the "throw" keyword. */
14074 if (token->type == CPP_COMMA
14075 || token->type == CPP_SEMICOLON
14076 || token->type == CPP_CLOSE_PAREN
14077 || token->type == CPP_CLOSE_SQUARE
14078 || token->type == CPP_CLOSE_BRACE
14079 || token->type == CPP_COLON)
14080 expression = NULL_TREE;
14082 expression = cp_parser_assignment_expression (parser,
14085 return build_throw (expression);
14088 /* GNU Extensions */
14090 /* Parse an (optional) asm-specification.
14093 asm ( string-literal )
14095 If the asm-specification is present, returns a STRING_CST
14096 corresponding to the string-literal. Otherwise, returns
14100 cp_parser_asm_specification_opt (cp_parser* parser)
14103 tree asm_specification;
14105 /* Peek at the next token. */
14106 token = cp_lexer_peek_token (parser->lexer);
14107 /* If the next token isn't the `asm' keyword, then there's no
14108 asm-specification. */
14109 if (!cp_parser_is_keyword (token, RID_ASM))
14112 /* Consume the `asm' token. */
14113 cp_lexer_consume_token (parser->lexer);
14114 /* Look for the `('. */
14115 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14117 /* Look for the string-literal. */
14118 asm_specification = cp_parser_string_literal (parser, false, false);
14120 /* Look for the `)'. */
14121 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14123 return asm_specification;
14126 /* Parse an asm-operand-list.
14130 asm-operand-list , asm-operand
14133 string-literal ( expression )
14134 [ string-literal ] string-literal ( expression )
14136 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14137 each node is the expression. The TREE_PURPOSE is itself a
14138 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14139 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14140 is a STRING_CST for the string literal before the parenthesis. */
14143 cp_parser_asm_operand_list (cp_parser* parser)
14145 tree asm_operands = NULL_TREE;
14149 tree string_literal;
14153 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14155 /* Consume the `[' token. */
14156 cp_lexer_consume_token (parser->lexer);
14157 /* Read the operand name. */
14158 name = cp_parser_identifier (parser);
14159 if (name != error_mark_node)
14160 name = build_string (IDENTIFIER_LENGTH (name),
14161 IDENTIFIER_POINTER (name));
14162 /* Look for the closing `]'. */
14163 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14167 /* Look for the string-literal. */
14168 string_literal = cp_parser_string_literal (parser, false, false);
14170 /* Look for the `('. */
14171 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14172 /* Parse the expression. */
14173 expression = cp_parser_expression (parser, /*cast_p=*/false);
14174 /* Look for the `)'. */
14175 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14177 /* Add this operand to the list. */
14178 asm_operands = tree_cons (build_tree_list (name, string_literal),
14181 /* If the next token is not a `,', there are no more
14183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14185 /* Consume the `,'. */
14186 cp_lexer_consume_token (parser->lexer);
14189 return nreverse (asm_operands);
14192 /* Parse an asm-clobber-list.
14196 asm-clobber-list , string-literal
14198 Returns a TREE_LIST, indicating the clobbers in the order that they
14199 appeared. The TREE_VALUE of each node is a STRING_CST. */
14202 cp_parser_asm_clobber_list (cp_parser* parser)
14204 tree clobbers = NULL_TREE;
14208 tree string_literal;
14210 /* Look for the string literal. */
14211 string_literal = cp_parser_string_literal (parser, false, false);
14212 /* Add it to the list. */
14213 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14214 /* If the next token is not a `,', then the list is
14216 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14218 /* Consume the `,' token. */
14219 cp_lexer_consume_token (parser->lexer);
14225 /* Parse an (optional) series of attributes.
14228 attributes attribute
14231 __attribute__ (( attribute-list [opt] ))
14233 The return value is as for cp_parser_attribute_list. */
14236 cp_parser_attributes_opt (cp_parser* parser)
14238 tree attributes = NULL_TREE;
14243 tree attribute_list;
14245 /* Peek at the next token. */
14246 token = cp_lexer_peek_token (parser->lexer);
14247 /* If it's not `__attribute__', then we're done. */
14248 if (token->keyword != RID_ATTRIBUTE)
14251 /* Consume the `__attribute__' keyword. */
14252 cp_lexer_consume_token (parser->lexer);
14253 /* Look for the two `(' tokens. */
14254 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14255 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14257 /* Peek at the next token. */
14258 token = cp_lexer_peek_token (parser->lexer);
14259 if (token->type != CPP_CLOSE_PAREN)
14260 /* Parse the attribute-list. */
14261 attribute_list = cp_parser_attribute_list (parser);
14263 /* If the next token is a `)', then there is no attribute
14265 attribute_list = NULL;
14267 /* Look for the two `)' tokens. */
14268 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14269 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14271 /* Add these new attributes to the list. */
14272 attributes = chainon (attributes, attribute_list);
14278 /* Parse an attribute-list.
14282 attribute-list , attribute
14286 identifier ( identifier )
14287 identifier ( identifier , expression-list )
14288 identifier ( expression-list )
14290 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14291 to an attribute. The TREE_PURPOSE of each node is the identifier
14292 indicating which attribute is in use. The TREE_VALUE represents
14293 the arguments, if any. */
14296 cp_parser_attribute_list (cp_parser* parser)
14298 tree attribute_list = NULL_TREE;
14299 bool save_translate_strings_p = parser->translate_strings_p;
14301 parser->translate_strings_p = false;
14308 /* Look for the identifier. We also allow keywords here; for
14309 example `__attribute__ ((const))' is legal. */
14310 token = cp_lexer_peek_token (parser->lexer);
14311 if (token->type == CPP_NAME
14312 || token->type == CPP_KEYWORD)
14314 /* Consume the token. */
14315 token = cp_lexer_consume_token (parser->lexer);
14317 /* Save away the identifier that indicates which attribute
14319 identifier = token->value;
14320 attribute = build_tree_list (identifier, NULL_TREE);
14322 /* Peek at the next token. */
14323 token = cp_lexer_peek_token (parser->lexer);
14324 /* If it's an `(', then parse the attribute arguments. */
14325 if (token->type == CPP_OPEN_PAREN)
14329 arguments = (cp_parser_parenthesized_expression_list
14330 (parser, true, /*cast_p=*/false,
14331 /*non_constant_p=*/NULL));
14332 /* Save the identifier and arguments away. */
14333 TREE_VALUE (attribute) = arguments;
14336 /* Add this attribute to the list. */
14337 TREE_CHAIN (attribute) = attribute_list;
14338 attribute_list = attribute;
14340 token = cp_lexer_peek_token (parser->lexer);
14342 /* Now, look for more attributes. If the next token isn't a
14343 `,', we're done. */
14344 if (token->type != CPP_COMMA)
14347 /* Consume the comma and keep going. */
14348 cp_lexer_consume_token (parser->lexer);
14350 parser->translate_strings_p = save_translate_strings_p;
14352 /* We built up the list in reverse order. */
14353 return nreverse (attribute_list);
14356 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14357 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14358 current value of the PEDANTIC flag, regardless of whether or not
14359 the `__extension__' keyword is present. The caller is responsible
14360 for restoring the value of the PEDANTIC flag. */
14363 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14365 /* Save the old value of the PEDANTIC flag. */
14366 *saved_pedantic = pedantic;
14368 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14370 /* Consume the `__extension__' token. */
14371 cp_lexer_consume_token (parser->lexer);
14372 /* We're not being pedantic while the `__extension__' keyword is
14382 /* Parse a label declaration.
14385 __label__ label-declarator-seq ;
14387 label-declarator-seq:
14388 identifier , label-declarator-seq
14392 cp_parser_label_declaration (cp_parser* parser)
14394 /* Look for the `__label__' keyword. */
14395 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14401 /* Look for an identifier. */
14402 identifier = cp_parser_identifier (parser);
14403 /* If we failed, stop. */
14404 if (identifier == error_mark_node)
14406 /* Declare it as a label. */
14407 finish_label_decl (identifier);
14408 /* If the next token is a `;', stop. */
14409 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14411 /* Look for the `,' separating the label declarations. */
14412 cp_parser_require (parser, CPP_COMMA, "`,'");
14415 /* Look for the final `;'. */
14416 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14419 /* Support Functions */
14421 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14422 NAME should have one of the representations used for an
14423 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14424 is returned. If PARSER->SCOPE is a dependent type, then a
14425 SCOPE_REF is returned.
14427 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14428 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14429 was formed. Abstractly, such entities should not be passed to this
14430 function, because they do not need to be looked up, but it is
14431 simpler to check for this special case here, rather than at the
14434 In cases not explicitly covered above, this function returns a
14435 DECL, OVERLOAD, or baselink representing the result of the lookup.
14436 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14439 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14440 (e.g., "struct") that was used. In that case bindings that do not
14441 refer to types are ignored.
14443 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14446 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14449 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14452 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14453 results in an ambiguity, and false otherwise. */
14456 cp_parser_lookup_name (cp_parser *parser, tree name,
14457 enum tag_types tag_type,
14458 bool is_template, bool is_namespace,
14459 bool check_dependency,
14464 tree object_type = parser->context->object_type;
14466 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14467 flags |= LOOKUP_COMPLAIN;
14469 /* Assume that the lookup will be unambiguous. */
14471 *ambiguous_p = false;
14473 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14474 no longer valid. Note that if we are parsing tentatively, and
14475 the parse fails, OBJECT_TYPE will be automatically restored. */
14476 parser->context->object_type = NULL_TREE;
14478 if (name == error_mark_node)
14479 return error_mark_node;
14481 /* A template-id has already been resolved; there is no lookup to
14483 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14485 if (BASELINK_P (name))
14487 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14488 == TEMPLATE_ID_EXPR);
14492 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14493 it should already have been checked to make sure that the name
14494 used matches the type being destroyed. */
14495 if (TREE_CODE (name) == BIT_NOT_EXPR)
14499 /* Figure out to which type this destructor applies. */
14501 type = parser->scope;
14502 else if (object_type)
14503 type = object_type;
14505 type = current_class_type;
14506 /* If that's not a class type, there is no destructor. */
14507 if (!type || !CLASS_TYPE_P (type))
14508 return error_mark_node;
14509 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14510 lazily_declare_fn (sfk_destructor, type);
14511 if (!CLASSTYPE_DESTRUCTORS (type))
14512 return error_mark_node;
14513 /* If it was a class type, return the destructor. */
14514 return CLASSTYPE_DESTRUCTORS (type);
14517 /* By this point, the NAME should be an ordinary identifier. If
14518 the id-expression was a qualified name, the qualifying scope is
14519 stored in PARSER->SCOPE at this point. */
14520 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14522 /* Perform the lookup. */
14527 if (parser->scope == error_mark_node)
14528 return error_mark_node;
14530 /* If the SCOPE is dependent, the lookup must be deferred until
14531 the template is instantiated -- unless we are explicitly
14532 looking up names in uninstantiated templates. Even then, we
14533 cannot look up the name if the scope is not a class type; it
14534 might, for example, be a template type parameter. */
14535 dependent_p = (TYPE_P (parser->scope)
14536 && !(parser->in_declarator_p
14537 && currently_open_class (parser->scope))
14538 && dependent_type_p (parser->scope));
14539 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14546 /* The resolution to Core Issue 180 says that `struct
14547 A::B' should be considered a type-name, even if `A'
14549 type = make_typename_type (parser->scope, name, tag_type,
14551 decl = TYPE_NAME (type);
14553 else if (is_template)
14554 decl = make_unbound_class_template (parser->scope,
14558 decl = build_nt (SCOPE_REF, parser->scope, name);
14562 tree pushed_scope = NULL_TREE;
14564 /* If PARSER->SCOPE is a dependent type, then it must be a
14565 class type, and we must not be checking dependencies;
14566 otherwise, we would have processed this lookup above. So
14567 that PARSER->SCOPE is not considered a dependent base by
14568 lookup_member, we must enter the scope here. */
14570 pushed_scope = push_scope (parser->scope);
14571 /* If the PARSER->SCOPE is a template specialization, it
14572 may be instantiated during name lookup. In that case,
14573 errors may be issued. Even if we rollback the current
14574 tentative parse, those errors are valid. */
14575 decl = lookup_qualified_name (parser->scope, name,
14576 tag_type != none_type,
14577 /*complain=*/true);
14579 pop_scope (pushed_scope);
14581 parser->qualifying_scope = parser->scope;
14582 parser->object_scope = NULL_TREE;
14584 else if (object_type)
14586 tree object_decl = NULL_TREE;
14587 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14588 OBJECT_TYPE is not a class. */
14589 if (CLASS_TYPE_P (object_type))
14590 /* If the OBJECT_TYPE is a template specialization, it may
14591 be instantiated during name lookup. In that case, errors
14592 may be issued. Even if we rollback the current tentative
14593 parse, those errors are valid. */
14594 object_decl = lookup_member (object_type,
14597 tag_type != none_type);
14598 /* Look it up in the enclosing context, too. */
14599 decl = lookup_name_real (name, tag_type != none_type,
14601 /*block_p=*/true, is_namespace, flags);
14602 parser->object_scope = object_type;
14603 parser->qualifying_scope = NULL_TREE;
14605 decl = object_decl;
14609 decl = lookup_name_real (name, tag_type != none_type,
14611 /*block_p=*/true, is_namespace, flags);
14612 parser->qualifying_scope = NULL_TREE;
14613 parser->object_scope = NULL_TREE;
14616 /* If the lookup failed, let our caller know. */
14617 if (!decl || decl == error_mark_node)
14618 return error_mark_node;
14620 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14621 if (TREE_CODE (decl) == TREE_LIST)
14624 *ambiguous_p = true;
14625 /* The error message we have to print is too complicated for
14626 cp_parser_error, so we incorporate its actions directly. */
14627 if (!cp_parser_simulate_error (parser))
14629 error ("reference to %qD is ambiguous", name);
14630 print_candidates (decl);
14632 return error_mark_node;
14635 gcc_assert (DECL_P (decl)
14636 || TREE_CODE (decl) == OVERLOAD
14637 || TREE_CODE (decl) == SCOPE_REF
14638 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14639 || BASELINK_P (decl));
14641 /* If we have resolved the name of a member declaration, check to
14642 see if the declaration is accessible. When the name resolves to
14643 set of overloaded functions, accessibility is checked when
14644 overload resolution is done.
14646 During an explicit instantiation, access is not checked at all,
14647 as per [temp.explicit]. */
14649 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14654 /* Like cp_parser_lookup_name, but for use in the typical case where
14655 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14656 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14659 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14661 return cp_parser_lookup_name (parser, name,
14663 /*is_template=*/false,
14664 /*is_namespace=*/false,
14665 /*check_dependency=*/true,
14666 /*ambiguous_p=*/NULL);
14669 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14670 the current context, return the TYPE_DECL. If TAG_NAME_P is
14671 true, the DECL indicates the class being defined in a class-head,
14672 or declared in an elaborated-type-specifier.
14674 Otherwise, return DECL. */
14677 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14679 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14680 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14683 template <typename T> struct B;
14686 template <typename T> struct A::B {};
14688 Similarly, in an elaborated-type-specifier:
14690 namespace N { struct X{}; }
14693 template <typename T> friend struct N::X;
14696 However, if the DECL refers to a class type, and we are in
14697 the scope of the class, then the name lookup automatically
14698 finds the TYPE_DECL created by build_self_reference rather
14699 than a TEMPLATE_DECL. For example, in:
14701 template <class T> struct S {
14705 there is no need to handle such case. */
14707 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14708 return DECL_TEMPLATE_RESULT (decl);
14713 /* If too many, or too few, template-parameter lists apply to the
14714 declarator, issue an error message. Returns TRUE if all went well,
14715 and FALSE otherwise. */
14718 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14719 cp_declarator *declarator)
14721 unsigned num_templates;
14723 /* We haven't seen any classes that involve template parameters yet. */
14726 switch (declarator->kind)
14729 if (declarator->u.id.qualifying_scope)
14734 scope = declarator->u.id.qualifying_scope;
14735 member = declarator->u.id.unqualified_name;
14737 while (scope && CLASS_TYPE_P (scope))
14739 /* You're supposed to have one `template <...>'
14740 for every template class, but you don't need one
14741 for a full specialization. For example:
14743 template <class T> struct S{};
14744 template <> struct S<int> { void f(); };
14745 void S<int>::f () {}
14747 is correct; there shouldn't be a `template <>' for
14748 the definition of `S<int>::f'. */
14749 if (CLASSTYPE_TEMPLATE_INFO (scope)
14750 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14751 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14752 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14755 scope = TYPE_CONTEXT (scope);
14758 else if (TREE_CODE (declarator->u.id.unqualified_name)
14759 == TEMPLATE_ID_EXPR)
14760 /* If the DECLARATOR has the form `X<y>' then it uses one
14761 additional level of template parameters. */
14764 return cp_parser_check_template_parameters (parser,
14770 case cdk_reference:
14772 return (cp_parser_check_declarator_template_parameters
14773 (parser, declarator->declarator));
14779 gcc_unreachable ();
14784 /* NUM_TEMPLATES were used in the current declaration. If that is
14785 invalid, return FALSE and issue an error messages. Otherwise,
14789 cp_parser_check_template_parameters (cp_parser* parser,
14790 unsigned num_templates)
14792 /* If there are more template classes than parameter lists, we have
14795 template <class T> void S<T>::R<T>::f (); */
14796 if (parser->num_template_parameter_lists < num_templates)
14798 error ("too few template-parameter-lists");
14801 /* If there are the same number of template classes and parameter
14802 lists, that's OK. */
14803 if (parser->num_template_parameter_lists == num_templates)
14805 /* If there are more, but only one more, then we are referring to a
14806 member template. That's OK too. */
14807 if (parser->num_template_parameter_lists == num_templates + 1)
14809 /* Otherwise, there are too many template parameter lists. We have
14812 template <class T> template <class U> void S::f(); */
14813 error ("too many template-parameter-lists");
14817 /* Parse an optional `::' token indicating that the following name is
14818 from the global namespace. If so, PARSER->SCOPE is set to the
14819 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14820 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14821 Returns the new value of PARSER->SCOPE, if the `::' token is
14822 present, and NULL_TREE otherwise. */
14825 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14829 /* Peek at the next token. */
14830 token = cp_lexer_peek_token (parser->lexer);
14831 /* If we're looking at a `::' token then we're starting from the
14832 global namespace, not our current location. */
14833 if (token->type == CPP_SCOPE)
14835 /* Consume the `::' token. */
14836 cp_lexer_consume_token (parser->lexer);
14837 /* Set the SCOPE so that we know where to start the lookup. */
14838 parser->scope = global_namespace;
14839 parser->qualifying_scope = global_namespace;
14840 parser->object_scope = NULL_TREE;
14842 return parser->scope;
14844 else if (!current_scope_valid_p)
14846 parser->scope = NULL_TREE;
14847 parser->qualifying_scope = NULL_TREE;
14848 parser->object_scope = NULL_TREE;
14854 /* Returns TRUE if the upcoming token sequence is the start of a
14855 constructor declarator. If FRIEND_P is true, the declarator is
14856 preceded by the `friend' specifier. */
14859 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14861 bool constructor_p;
14862 tree type_decl = NULL_TREE;
14863 bool nested_name_p;
14864 cp_token *next_token;
14866 /* The common case is that this is not a constructor declarator, so
14867 try to avoid doing lots of work if at all possible. It's not
14868 valid declare a constructor at function scope. */
14869 if (at_function_scope_p ())
14871 /* And only certain tokens can begin a constructor declarator. */
14872 next_token = cp_lexer_peek_token (parser->lexer);
14873 if (next_token->type != CPP_NAME
14874 && next_token->type != CPP_SCOPE
14875 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14876 && next_token->type != CPP_TEMPLATE_ID)
14879 /* Parse tentatively; we are going to roll back all of the tokens
14881 cp_parser_parse_tentatively (parser);
14882 /* Assume that we are looking at a constructor declarator. */
14883 constructor_p = true;
14885 /* Look for the optional `::' operator. */
14886 cp_parser_global_scope_opt (parser,
14887 /*current_scope_valid_p=*/false);
14888 /* Look for the nested-name-specifier. */
14890 = (cp_parser_nested_name_specifier_opt (parser,
14891 /*typename_keyword_p=*/false,
14892 /*check_dependency_p=*/false,
14894 /*is_declaration=*/false)
14896 /* Outside of a class-specifier, there must be a
14897 nested-name-specifier. */
14898 if (!nested_name_p &&
14899 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14901 constructor_p = false;
14902 /* If we still think that this might be a constructor-declarator,
14903 look for a class-name. */
14908 template <typename T> struct S { S(); };
14909 template <typename T> S<T>::S ();
14911 we must recognize that the nested `S' names a class.
14914 template <typename T> S<T>::S<T> ();
14916 we must recognize that the nested `S' names a template. */
14917 type_decl = cp_parser_class_name (parser,
14918 /*typename_keyword_p=*/false,
14919 /*template_keyword_p=*/false,
14921 /*check_dependency_p=*/false,
14922 /*class_head_p=*/false,
14923 /*is_declaration=*/false);
14924 /* If there was no class-name, then this is not a constructor. */
14925 constructor_p = !cp_parser_error_occurred (parser);
14928 /* If we're still considering a constructor, we have to see a `(',
14929 to begin the parameter-declaration-clause, followed by either a
14930 `)', an `...', or a decl-specifier. We need to check for a
14931 type-specifier to avoid being fooled into thinking that:
14935 is a constructor. (It is actually a function named `f' that
14936 takes one parameter (of type `int') and returns a value of type
14939 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14941 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14942 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14943 /* A parameter declaration begins with a decl-specifier,
14944 which is either the "attribute" keyword, a storage class
14945 specifier, or (usually) a type-specifier. */
14946 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14947 && !cp_parser_storage_class_specifier_opt (parser))
14950 tree pushed_scope = NULL_TREE;
14951 unsigned saved_num_template_parameter_lists;
14953 /* Names appearing in the type-specifier should be looked up
14954 in the scope of the class. */
14955 if (current_class_type)
14959 type = TREE_TYPE (type_decl);
14960 if (TREE_CODE (type) == TYPENAME_TYPE)
14962 type = resolve_typename_type (type,
14963 /*only_current_p=*/false);
14964 if (type == error_mark_node)
14966 cp_parser_abort_tentative_parse (parser);
14970 pushed_scope = push_scope (type);
14973 /* Inside the constructor parameter list, surrounding
14974 template-parameter-lists do not apply. */
14975 saved_num_template_parameter_lists
14976 = parser->num_template_parameter_lists;
14977 parser->num_template_parameter_lists = 0;
14979 /* Look for the type-specifier. */
14980 cp_parser_type_specifier (parser,
14981 CP_PARSER_FLAGS_NONE,
14982 /*decl_specs=*/NULL,
14983 /*is_declarator=*/true,
14984 /*declares_class_or_enum=*/NULL,
14985 /*is_cv_qualifier=*/NULL);
14987 parser->num_template_parameter_lists
14988 = saved_num_template_parameter_lists;
14990 /* Leave the scope of the class. */
14992 pop_scope (pushed_scope);
14994 constructor_p = !cp_parser_error_occurred (parser);
14998 constructor_p = false;
14999 /* We did not really want to consume any tokens. */
15000 cp_parser_abort_tentative_parse (parser);
15002 return constructor_p;
15005 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15006 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15007 they must be performed once we are in the scope of the function.
15009 Returns the function defined. */
15012 cp_parser_function_definition_from_specifiers_and_declarator
15013 (cp_parser* parser,
15014 cp_decl_specifier_seq *decl_specifiers,
15016 const cp_declarator *declarator)
15021 /* Begin the function-definition. */
15022 success_p = start_function (decl_specifiers, declarator, attributes);
15024 /* The things we're about to see are not directly qualified by any
15025 template headers we've seen thus far. */
15026 reset_specialization ();
15028 /* If there were names looked up in the decl-specifier-seq that we
15029 did not check, check them now. We must wait until we are in the
15030 scope of the function to perform the checks, since the function
15031 might be a friend. */
15032 perform_deferred_access_checks ();
15036 /* Skip the entire function. */
15037 error ("invalid function declaration");
15038 cp_parser_skip_to_end_of_block_or_statement (parser);
15039 fn = error_mark_node;
15042 fn = cp_parser_function_definition_after_declarator (parser,
15043 /*inline_p=*/false);
15048 /* Parse the part of a function-definition that follows the
15049 declarator. INLINE_P is TRUE iff this function is an inline
15050 function defined with a class-specifier.
15052 Returns the function defined. */
15055 cp_parser_function_definition_after_declarator (cp_parser* parser,
15059 bool ctor_initializer_p = false;
15060 bool saved_in_unbraced_linkage_specification_p;
15061 unsigned saved_num_template_parameter_lists;
15063 /* If the next token is `return', then the code may be trying to
15064 make use of the "named return value" extension that G++ used to
15066 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15068 /* Consume the `return' keyword. */
15069 cp_lexer_consume_token (parser->lexer);
15070 /* Look for the identifier that indicates what value is to be
15072 cp_parser_identifier (parser);
15073 /* Issue an error message. */
15074 error ("named return values are no longer supported");
15075 /* Skip tokens until we reach the start of the function body. */
15076 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15077 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15078 cp_lexer_consume_token (parser->lexer);
15080 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15081 anything declared inside `f'. */
15082 saved_in_unbraced_linkage_specification_p
15083 = parser->in_unbraced_linkage_specification_p;
15084 parser->in_unbraced_linkage_specification_p = false;
15085 /* Inside the function, surrounding template-parameter-lists do not
15087 saved_num_template_parameter_lists
15088 = parser->num_template_parameter_lists;
15089 parser->num_template_parameter_lists = 0;
15090 /* If the next token is `try', then we are looking at a
15091 function-try-block. */
15092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15093 ctor_initializer_p = cp_parser_function_try_block (parser);
15094 /* A function-try-block includes the function-body, so we only do
15095 this next part if we're not processing a function-try-block. */
15098 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15100 /* Finish the function. */
15101 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15102 (inline_p ? 2 : 0));
15103 /* Generate code for it, if necessary. */
15104 expand_or_defer_fn (fn);
15105 /* Restore the saved values. */
15106 parser->in_unbraced_linkage_specification_p
15107 = saved_in_unbraced_linkage_specification_p;
15108 parser->num_template_parameter_lists
15109 = saved_num_template_parameter_lists;
15114 /* Parse a template-declaration, assuming that the `export' (and
15115 `extern') keywords, if present, has already been scanned. MEMBER_P
15116 is as for cp_parser_template_declaration. */
15119 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15121 tree decl = NULL_TREE;
15122 tree parameter_list;
15123 bool friend_p = false;
15125 /* Look for the `template' keyword. */
15126 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15130 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15133 /* If the next token is `>', then we have an invalid
15134 specialization. Rather than complain about an invalid template
15135 parameter, issue an error message here. */
15136 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15138 cp_parser_error (parser, "invalid explicit specialization");
15139 begin_specialization ();
15140 parameter_list = NULL_TREE;
15144 /* Parse the template parameters. */
15145 begin_template_parm_list ();
15146 parameter_list = cp_parser_template_parameter_list (parser);
15147 parameter_list = end_template_parm_list (parameter_list);
15150 /* Look for the `>'. */
15151 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15152 /* We just processed one more parameter list. */
15153 ++parser->num_template_parameter_lists;
15154 /* If the next token is `template', there are more template
15156 if (cp_lexer_next_token_is_keyword (parser->lexer,
15158 cp_parser_template_declaration_after_export (parser, member_p);
15161 /* There are no access checks when parsing a template, as we do not
15162 know if a specialization will be a friend. */
15163 push_deferring_access_checks (dk_no_check);
15165 decl = cp_parser_single_declaration (parser,
15169 pop_deferring_access_checks ();
15171 /* If this is a member template declaration, let the front
15173 if (member_p && !friend_p && decl)
15175 if (TREE_CODE (decl) == TYPE_DECL)
15176 cp_parser_check_access_in_redeclaration (decl);
15178 decl = finish_member_template_decl (decl);
15180 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15181 make_friend_class (current_class_type, TREE_TYPE (decl),
15182 /*complain=*/true);
15184 /* We are done with the current parameter list. */
15185 --parser->num_template_parameter_lists;
15188 finish_template_decl (parameter_list);
15190 /* Register member declarations. */
15191 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15192 finish_member_declaration (decl);
15194 /* If DECL is a function template, we must return to parse it later.
15195 (Even though there is no definition, there might be default
15196 arguments that need handling.) */
15197 if (member_p && decl
15198 && (TREE_CODE (decl) == FUNCTION_DECL
15199 || DECL_FUNCTION_TEMPLATE_P (decl)))
15200 TREE_VALUE (parser->unparsed_functions_queues)
15201 = tree_cons (NULL_TREE, decl,
15202 TREE_VALUE (parser->unparsed_functions_queues));
15205 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15206 `function-definition' sequence. MEMBER_P is true, this declaration
15207 appears in a class scope.
15209 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15210 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15213 cp_parser_single_declaration (cp_parser* parser,
15217 int declares_class_or_enum;
15218 tree decl = NULL_TREE;
15219 cp_decl_specifier_seq decl_specifiers;
15220 bool function_definition_p = false;
15222 /* This function is only used when processing a template
15224 gcc_assert (innermost_scope_kind () == sk_template_parms
15225 || innermost_scope_kind () == sk_template_spec);
15227 /* Defer access checks until we know what is being declared. */
15228 push_deferring_access_checks (dk_deferred);
15230 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15232 cp_parser_decl_specifier_seq (parser,
15233 CP_PARSER_FLAGS_OPTIONAL,
15235 &declares_class_or_enum);
15237 *friend_p = cp_parser_friend_p (&decl_specifiers);
15239 /* There are no template typedefs. */
15240 if (decl_specifiers.specs[(int) ds_typedef])
15242 error ("template declaration of %qs", "typedef");
15243 decl = error_mark_node;
15246 /* Gather up the access checks that occurred the
15247 decl-specifier-seq. */
15248 stop_deferring_access_checks ();
15250 /* Check for the declaration of a template class. */
15251 if (declares_class_or_enum)
15253 if (cp_parser_declares_only_class_p (parser))
15255 decl = shadow_tag (&decl_specifiers);
15260 friend template <typename T> struct A<T>::B;
15263 A<T>::B will be represented by a TYPENAME_TYPE, and
15264 therefore not recognized by shadow_tag. */
15265 if (friend_p && *friend_p
15267 && decl_specifiers.type
15268 && TYPE_P (decl_specifiers.type))
15269 decl = decl_specifiers.type;
15271 if (decl && decl != error_mark_node)
15272 decl = TYPE_NAME (decl);
15274 decl = error_mark_node;
15277 /* If it's not a template class, try for a template function. If
15278 the next token is a `;', then this declaration does not declare
15279 anything. But, if there were errors in the decl-specifiers, then
15280 the error might well have come from an attempted class-specifier.
15281 In that case, there's no need to warn about a missing declarator. */
15283 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15284 || decl_specifiers.type != error_mark_node))
15285 decl = cp_parser_init_declarator (parser,
15287 /*function_definition_allowed_p=*/true,
15289 declares_class_or_enum,
15290 &function_definition_p);
15292 pop_deferring_access_checks ();
15294 /* Clear any current qualification; whatever comes next is the start
15295 of something new. */
15296 parser->scope = NULL_TREE;
15297 parser->qualifying_scope = NULL_TREE;
15298 parser->object_scope = NULL_TREE;
15299 /* Look for a trailing `;' after the declaration. */
15300 if (!function_definition_p
15301 && (decl == error_mark_node
15302 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15303 cp_parser_skip_to_end_of_block_or_statement (parser);
15308 /* Parse a cast-expression that is not the operand of a unary "&". */
15311 cp_parser_simple_cast_expression (cp_parser *parser)
15313 return cp_parser_cast_expression (parser, /*address_p=*/false,
15317 /* Parse a functional cast to TYPE. Returns an expression
15318 representing the cast. */
15321 cp_parser_functional_cast (cp_parser* parser, tree type)
15323 tree expression_list;
15327 = cp_parser_parenthesized_expression_list (parser, false,
15329 /*non_constant_p=*/NULL);
15331 cast = build_functional_cast (type, expression_list);
15332 /* [expr.const]/1: In an integral constant expression "only type
15333 conversions to integral or enumeration type can be used". */
15334 if (cast != error_mark_node && !type_dependent_expression_p (type)
15335 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15337 if (cp_parser_non_integral_constant_expression
15338 (parser, "a call to a constructor"))
15339 return error_mark_node;
15344 /* Save the tokens that make up the body of a member function defined
15345 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15346 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15347 specifiers applied to the declaration. Returns the FUNCTION_DECL
15348 for the member function. */
15351 cp_parser_save_member_function_body (cp_parser* parser,
15352 cp_decl_specifier_seq *decl_specifiers,
15353 cp_declarator *declarator,
15360 /* Create the function-declaration. */
15361 fn = start_method (decl_specifiers, declarator, attributes);
15362 /* If something went badly wrong, bail out now. */
15363 if (fn == error_mark_node)
15365 /* If there's a function-body, skip it. */
15366 if (cp_parser_token_starts_function_definition_p
15367 (cp_lexer_peek_token (parser->lexer)))
15368 cp_parser_skip_to_end_of_block_or_statement (parser);
15369 return error_mark_node;
15372 /* Remember it, if there default args to post process. */
15373 cp_parser_save_default_args (parser, fn);
15375 /* Save away the tokens that make up the body of the
15377 first = parser->lexer->next_token;
15378 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15379 /* Handle function try blocks. */
15380 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15381 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15382 last = parser->lexer->next_token;
15384 /* Save away the inline definition; we will process it when the
15385 class is complete. */
15386 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15387 DECL_PENDING_INLINE_P (fn) = 1;
15389 /* We need to know that this was defined in the class, so that
15390 friend templates are handled correctly. */
15391 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15393 /* We're done with the inline definition. */
15394 finish_method (fn);
15396 /* Add FN to the queue of functions to be parsed later. */
15397 TREE_VALUE (parser->unparsed_functions_queues)
15398 = tree_cons (NULL_TREE, fn,
15399 TREE_VALUE (parser->unparsed_functions_queues));
15404 /* Parse a template-argument-list, as well as the trailing ">" (but
15405 not the opening ">"). See cp_parser_template_argument_list for the
15409 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15413 tree saved_qualifying_scope;
15414 tree saved_object_scope;
15415 bool saved_greater_than_is_operator_p;
15419 When parsing a template-id, the first non-nested `>' is taken as
15420 the end of the template-argument-list rather than a greater-than
15422 saved_greater_than_is_operator_p
15423 = parser->greater_than_is_operator_p;
15424 parser->greater_than_is_operator_p = false;
15425 /* Parsing the argument list may modify SCOPE, so we save it
15427 saved_scope = parser->scope;
15428 saved_qualifying_scope = parser->qualifying_scope;
15429 saved_object_scope = parser->object_scope;
15430 /* Parse the template-argument-list itself. */
15431 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15432 arguments = NULL_TREE;
15434 arguments = cp_parser_template_argument_list (parser);
15435 /* Look for the `>' that ends the template-argument-list. If we find
15436 a '>>' instead, it's probably just a typo. */
15437 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15439 if (!saved_greater_than_is_operator_p)
15441 /* If we're in a nested template argument list, the '>>' has
15442 to be a typo for '> >'. We emit the error message, but we
15443 continue parsing and we push a '>' as next token, so that
15444 the argument list will be parsed correctly. Note that the
15445 global source location is still on the token before the
15446 '>>', so we need to say explicitly where we want it. */
15447 cp_token *token = cp_lexer_peek_token (parser->lexer);
15448 error ("%H%<>>%> should be %<> >%> "
15449 "within a nested template argument list",
15452 /* ??? Proper recovery should terminate two levels of
15453 template argument list here. */
15454 token->type = CPP_GREATER;
15458 /* If this is not a nested template argument list, the '>>'
15459 is a typo for '>'. Emit an error message and continue.
15460 Same deal about the token location, but here we can get it
15461 right by consuming the '>>' before issuing the diagnostic. */
15462 cp_lexer_consume_token (parser->lexer);
15463 error ("spurious %<>>%>, use %<>%> to terminate "
15464 "a template argument list");
15467 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15468 error ("missing %<>%> to terminate the template argument list");
15470 /* It's what we want, a '>'; consume it. */
15471 cp_lexer_consume_token (parser->lexer);
15472 /* The `>' token might be a greater-than operator again now. */
15473 parser->greater_than_is_operator_p
15474 = saved_greater_than_is_operator_p;
15475 /* Restore the SAVED_SCOPE. */
15476 parser->scope = saved_scope;
15477 parser->qualifying_scope = saved_qualifying_scope;
15478 parser->object_scope = saved_object_scope;
15483 /* MEMBER_FUNCTION is a member function, or a friend. If default
15484 arguments, or the body of the function have not yet been parsed,
15488 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15490 /* If this member is a template, get the underlying
15492 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15493 member_function = DECL_TEMPLATE_RESULT (member_function);
15495 /* There should not be any class definitions in progress at this
15496 point; the bodies of members are only parsed outside of all class
15498 gcc_assert (parser->num_classes_being_defined == 0);
15499 /* While we're parsing the member functions we might encounter more
15500 classes. We want to handle them right away, but we don't want
15501 them getting mixed up with functions that are currently in the
15503 parser->unparsed_functions_queues
15504 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15506 /* Make sure that any template parameters are in scope. */
15507 maybe_begin_member_template_processing (member_function);
15509 /* If the body of the function has not yet been parsed, parse it
15511 if (DECL_PENDING_INLINE_P (member_function))
15513 tree function_scope;
15514 cp_token_cache *tokens;
15516 /* The function is no longer pending; we are processing it. */
15517 tokens = DECL_PENDING_INLINE_INFO (member_function);
15518 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15519 DECL_PENDING_INLINE_P (member_function) = 0;
15521 /* If this is a local class, enter the scope of the containing
15523 function_scope = current_function_decl;
15524 if (function_scope)
15525 push_function_context_to (function_scope);
15528 /* Push the body of the function onto the lexer stack. */
15529 cp_parser_push_lexer_for_tokens (parser, tokens);
15531 /* Let the front end know that we going to be defining this
15533 start_preparsed_function (member_function, NULL_TREE,
15534 SF_PRE_PARSED | SF_INCLASS_INLINE);
15536 /* Don't do access checking if it is a templated function. */
15537 if (processing_template_decl)
15538 push_deferring_access_checks (dk_no_check);
15540 /* Now, parse the body of the function. */
15541 cp_parser_function_definition_after_declarator (parser,
15542 /*inline_p=*/true);
15544 if (processing_template_decl)
15545 pop_deferring_access_checks ();
15547 /* Leave the scope of the containing function. */
15548 if (function_scope)
15549 pop_function_context_from (function_scope);
15550 cp_parser_pop_lexer (parser);
15553 /* Remove any template parameters from the symbol table. */
15554 maybe_end_member_template_processing ();
15556 /* Restore the queue. */
15557 parser->unparsed_functions_queues
15558 = TREE_CHAIN (parser->unparsed_functions_queues);
15561 /* If DECL contains any default args, remember it on the unparsed
15562 functions queue. */
15565 cp_parser_save_default_args (cp_parser* parser, tree decl)
15569 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15571 probe = TREE_CHAIN (probe))
15572 if (TREE_PURPOSE (probe))
15574 TREE_PURPOSE (parser->unparsed_functions_queues)
15575 = tree_cons (current_class_type, decl,
15576 TREE_PURPOSE (parser->unparsed_functions_queues));
15582 /* FN is a FUNCTION_DECL which may contains a parameter with an
15583 unparsed DEFAULT_ARG. Parse the default args now. This function
15584 assumes that the current scope is the scope in which the default
15585 argument should be processed. */
15588 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15590 bool saved_local_variables_forbidden_p;
15593 /* While we're parsing the default args, we might (due to the
15594 statement expression extension) encounter more classes. We want
15595 to handle them right away, but we don't want them getting mixed
15596 up with default args that are currently in the queue. */
15597 parser->unparsed_functions_queues
15598 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15600 /* Local variable names (and the `this' keyword) may not appear
15601 in a default argument. */
15602 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15603 parser->local_variables_forbidden_p = true;
15605 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15607 parm = TREE_CHAIN (parm))
15609 cp_token_cache *tokens;
15610 tree default_arg = TREE_PURPOSE (parm);
15612 VEC(tree,gc) *insts;
15619 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15620 /* This can happen for a friend declaration for a function
15621 already declared with default arguments. */
15624 /* Push the saved tokens for the default argument onto the parser's
15626 tokens = DEFARG_TOKENS (default_arg);
15627 cp_parser_push_lexer_for_tokens (parser, tokens);
15629 /* Parse the assignment-expression. */
15630 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15632 TREE_PURPOSE (parm) = parsed_arg;
15634 /* Update any instantiations we've already created. */
15635 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15636 VEC_iterate (tree, insts, ix, copy); ix++)
15637 TREE_PURPOSE (copy) = parsed_arg;
15639 /* If the token stream has not been completely used up, then
15640 there was extra junk after the end of the default
15642 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15643 cp_parser_error (parser, "expected %<,%>");
15645 /* Revert to the main lexer. */
15646 cp_parser_pop_lexer (parser);
15649 /* Restore the state of local_variables_forbidden_p. */
15650 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15652 /* Restore the queue. */
15653 parser->unparsed_functions_queues
15654 = TREE_CHAIN (parser->unparsed_functions_queues);
15657 /* Parse the operand of `sizeof' (or a similar operator). Returns
15658 either a TYPE or an expression, depending on the form of the
15659 input. The KEYWORD indicates which kind of expression we have
15663 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15665 static const char *format;
15666 tree expr = NULL_TREE;
15667 const char *saved_message;
15668 bool saved_integral_constant_expression_p;
15669 bool saved_non_integral_constant_expression_p;
15671 /* Initialize FORMAT the first time we get here. */
15673 format = "types may not be defined in '%s' expressions";
15675 /* Types cannot be defined in a `sizeof' expression. Save away the
15677 saved_message = parser->type_definition_forbidden_message;
15678 /* And create the new one. */
15679 parser->type_definition_forbidden_message
15680 = xmalloc (strlen (format)
15681 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15683 sprintf ((char *) parser->type_definition_forbidden_message,
15684 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15686 /* The restrictions on constant-expressions do not apply inside
15687 sizeof expressions. */
15688 saved_integral_constant_expression_p
15689 = parser->integral_constant_expression_p;
15690 saved_non_integral_constant_expression_p
15691 = parser->non_integral_constant_expression_p;
15692 parser->integral_constant_expression_p = false;
15694 /* Do not actually evaluate the expression. */
15696 /* If it's a `(', then we might be looking at the type-id
15698 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15701 bool saved_in_type_id_in_expr_p;
15703 /* We can't be sure yet whether we're looking at a type-id or an
15705 cp_parser_parse_tentatively (parser);
15706 /* Consume the `('. */
15707 cp_lexer_consume_token (parser->lexer);
15708 /* Parse the type-id. */
15709 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15710 parser->in_type_id_in_expr_p = true;
15711 type = cp_parser_type_id (parser);
15712 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15713 /* Now, look for the trailing `)'. */
15714 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15715 /* If all went well, then we're done. */
15716 if (cp_parser_parse_definitely (parser))
15718 cp_decl_specifier_seq decl_specs;
15720 /* Build a trivial decl-specifier-seq. */
15721 clear_decl_specs (&decl_specs);
15722 decl_specs.type = type;
15724 /* Call grokdeclarator to figure out what type this is. */
15725 expr = grokdeclarator (NULL,
15729 /*attrlist=*/NULL);
15733 /* If the type-id production did not work out, then we must be
15734 looking at the unary-expression production. */
15736 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15738 /* Go back to evaluating expressions. */
15741 /* Free the message we created. */
15742 free ((char *) parser->type_definition_forbidden_message);
15743 /* And restore the old one. */
15744 parser->type_definition_forbidden_message = saved_message;
15745 parser->integral_constant_expression_p
15746 = saved_integral_constant_expression_p;
15747 parser->non_integral_constant_expression_p
15748 = saved_non_integral_constant_expression_p;
15753 /* If the current declaration has no declarator, return true. */
15756 cp_parser_declares_only_class_p (cp_parser *parser)
15758 /* If the next token is a `;' or a `,' then there is no
15760 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15761 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15764 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15767 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15768 cp_storage_class storage_class)
15770 if (decl_specs->storage_class != sc_none)
15771 decl_specs->multiple_storage_classes_p = true;
15773 decl_specs->storage_class = storage_class;
15776 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15777 is true, the type is a user-defined type; otherwise it is a
15778 built-in type specified by a keyword. */
15781 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15783 bool user_defined_p)
15785 decl_specs->any_specifiers_p = true;
15787 /* If the user tries to redeclare bool or wchar_t (with, for
15788 example, in "typedef int wchar_t;") we remember that this is what
15789 happened. In system headers, we ignore these declarations so
15790 that G++ can work with system headers that are not C++-safe. */
15791 if (decl_specs->specs[(int) ds_typedef]
15793 && (type_spec == boolean_type_node
15794 || type_spec == wchar_type_node)
15795 && (decl_specs->type
15796 || decl_specs->specs[(int) ds_long]
15797 || decl_specs->specs[(int) ds_short]
15798 || decl_specs->specs[(int) ds_unsigned]
15799 || decl_specs->specs[(int) ds_signed]))
15801 decl_specs->redefined_builtin_type = type_spec;
15802 if (!decl_specs->type)
15804 decl_specs->type = type_spec;
15805 decl_specs->user_defined_type_p = false;
15808 else if (decl_specs->type)
15809 decl_specs->multiple_types_p = true;
15812 decl_specs->type = type_spec;
15813 decl_specs->user_defined_type_p = user_defined_p;
15814 decl_specs->redefined_builtin_type = NULL_TREE;
15818 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15819 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15822 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15824 return decl_specifiers->specs[(int) ds_friend] != 0;
15827 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15828 issue an error message indicating that TOKEN_DESC was expected.
15830 Returns the token consumed, if the token had the appropriate type.
15831 Otherwise, returns NULL. */
15834 cp_parser_require (cp_parser* parser,
15835 enum cpp_ttype type,
15836 const char* token_desc)
15838 if (cp_lexer_next_token_is (parser->lexer, type))
15839 return cp_lexer_consume_token (parser->lexer);
15842 /* Output the MESSAGE -- unless we're parsing tentatively. */
15843 if (!cp_parser_simulate_error (parser))
15845 char *message = concat ("expected ", token_desc, NULL);
15846 cp_parser_error (parser, message);
15853 /* Like cp_parser_require, except that tokens will be skipped until
15854 the desired token is found. An error message is still produced if
15855 the next token is not as expected. */
15858 cp_parser_skip_until_found (cp_parser* parser,
15859 enum cpp_ttype type,
15860 const char* token_desc)
15863 unsigned nesting_depth = 0;
15865 if (cp_parser_require (parser, type, token_desc))
15868 /* Skip tokens until the desired token is found. */
15871 /* Peek at the next token. */
15872 token = cp_lexer_peek_token (parser->lexer);
15873 /* If we've reached the token we want, consume it and
15875 if (token->type == type && !nesting_depth)
15877 cp_lexer_consume_token (parser->lexer);
15880 /* If we've run out of tokens, stop. */
15881 if (token->type == CPP_EOF)
15883 if (token->type == CPP_OPEN_BRACE
15884 || token->type == CPP_OPEN_PAREN
15885 || token->type == CPP_OPEN_SQUARE)
15887 else if (token->type == CPP_CLOSE_BRACE
15888 || token->type == CPP_CLOSE_PAREN
15889 || token->type == CPP_CLOSE_SQUARE)
15891 if (nesting_depth-- == 0)
15894 /* Consume this token. */
15895 cp_lexer_consume_token (parser->lexer);
15899 /* If the next token is the indicated keyword, consume it. Otherwise,
15900 issue an error message indicating that TOKEN_DESC was expected.
15902 Returns the token consumed, if the token had the appropriate type.
15903 Otherwise, returns NULL. */
15906 cp_parser_require_keyword (cp_parser* parser,
15908 const char* token_desc)
15910 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15912 if (token && token->keyword != keyword)
15914 dyn_string_t error_msg;
15916 /* Format the error message. */
15917 error_msg = dyn_string_new (0);
15918 dyn_string_append_cstr (error_msg, "expected ");
15919 dyn_string_append_cstr (error_msg, token_desc);
15920 cp_parser_error (parser, error_msg->s);
15921 dyn_string_delete (error_msg);
15928 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15929 function-definition. */
15932 cp_parser_token_starts_function_definition_p (cp_token* token)
15934 return (/* An ordinary function-body begins with an `{'. */
15935 token->type == CPP_OPEN_BRACE
15936 /* A ctor-initializer begins with a `:'. */
15937 || token->type == CPP_COLON
15938 /* A function-try-block begins with `try'. */
15939 || token->keyword == RID_TRY
15940 /* The named return value extension begins with `return'. */
15941 || token->keyword == RID_RETURN);
15944 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15948 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15952 token = cp_lexer_peek_token (parser->lexer);
15953 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15956 /* Returns TRUE iff the next token is the "," or ">" ending a
15957 template-argument. */
15960 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15964 token = cp_lexer_peek_token (parser->lexer);
15965 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15968 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
15969 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15972 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15977 token = cp_lexer_peek_nth_token (parser->lexer, n);
15978 if (token->type == CPP_LESS)
15980 /* Check for the sequence `<::' in the original code. It would be lexed as
15981 `[:', where `[' is a digraph, and there is no whitespace before
15983 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15986 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15987 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15993 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15994 or none_type otherwise. */
15996 static enum tag_types
15997 cp_parser_token_is_class_key (cp_token* token)
15999 switch (token->keyword)
16004 return record_type;
16013 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16016 cp_parser_check_class_key (enum tag_types class_key, tree type)
16018 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16019 pedwarn ("%qs tag used in naming %q#T",
16020 class_key == union_type ? "union"
16021 : class_key == record_type ? "struct" : "class",
16025 /* Issue an error message if DECL is redeclared with different
16026 access than its original declaration [class.access.spec/3].
16027 This applies to nested classes and nested class templates.
16031 cp_parser_check_access_in_redeclaration (tree decl)
16033 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16036 if ((TREE_PRIVATE (decl)
16037 != (current_access_specifier == access_private_node))
16038 || (TREE_PROTECTED (decl)
16039 != (current_access_specifier == access_protected_node)))
16040 error ("%qD redeclared with different access", decl);
16043 /* Look for the `template' keyword, as a syntactic disambiguator.
16044 Return TRUE iff it is present, in which case it will be
16048 cp_parser_optional_template_keyword (cp_parser *parser)
16050 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16052 /* The `template' keyword can only be used within templates;
16053 outside templates the parser can always figure out what is a
16054 template and what is not. */
16055 if (!processing_template_decl)
16057 error ("%<template%> (as a disambiguator) is only allowed "
16058 "within templates");
16059 /* If this part of the token stream is rescanned, the same
16060 error message would be generated. So, we purge the token
16061 from the stream. */
16062 cp_lexer_purge_token (parser->lexer);
16067 /* Consume the `template' keyword. */
16068 cp_lexer_consume_token (parser->lexer);
16076 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16077 set PARSER->SCOPE, and perform other related actions. */
16080 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16085 /* Get the stored value. */
16086 value = cp_lexer_consume_token (parser->lexer)->value;
16087 /* Perform any access checks that were deferred. */
16088 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16089 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16090 /* Set the scope from the stored value. */
16091 parser->scope = TREE_VALUE (value);
16092 parser->qualifying_scope = TREE_TYPE (value);
16093 parser->object_scope = NULL_TREE;
16096 /* Consume tokens up through a non-nested END token. */
16099 cp_parser_cache_group (cp_parser *parser,
16100 enum cpp_ttype end,
16107 /* Abort a parenthesized expression if we encounter a brace. */
16108 if ((end == CPP_CLOSE_PAREN || depth == 0)
16109 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16111 /* If we've reached the end of the file, stop. */
16112 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16114 /* Consume the next token. */
16115 token = cp_lexer_consume_token (parser->lexer);
16116 /* See if it starts a new group. */
16117 if (token->type == CPP_OPEN_BRACE)
16119 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16123 else if (token->type == CPP_OPEN_PAREN)
16124 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16125 else if (token->type == end)
16130 /* Begin parsing tentatively. We always save tokens while parsing
16131 tentatively so that if the tentative parsing fails we can restore the
16135 cp_parser_parse_tentatively (cp_parser* parser)
16137 /* Enter a new parsing context. */
16138 parser->context = cp_parser_context_new (parser->context);
16139 /* Begin saving tokens. */
16140 cp_lexer_save_tokens (parser->lexer);
16141 /* In order to avoid repetitive access control error messages,
16142 access checks are queued up until we are no longer parsing
16144 push_deferring_access_checks (dk_deferred);
16147 /* Commit to the currently active tentative parse. */
16150 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16152 cp_parser_context *context;
16155 /* Mark all of the levels as committed. */
16156 lexer = parser->lexer;
16157 for (context = parser->context; context->next; context = context->next)
16159 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16161 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16162 while (!cp_lexer_saving_tokens (lexer))
16163 lexer = lexer->next;
16164 cp_lexer_commit_tokens (lexer);
16168 /* Abort the currently active tentative parse. All consumed tokens
16169 will be rolled back, and no diagnostics will be issued. */
16172 cp_parser_abort_tentative_parse (cp_parser* parser)
16174 cp_parser_simulate_error (parser);
16175 /* Now, pretend that we want to see if the construct was
16176 successfully parsed. */
16177 cp_parser_parse_definitely (parser);
16180 /* Stop parsing tentatively. If a parse error has occurred, restore the
16181 token stream. Otherwise, commit to the tokens we have consumed.
16182 Returns true if no error occurred; false otherwise. */
16185 cp_parser_parse_definitely (cp_parser* parser)
16187 bool error_occurred;
16188 cp_parser_context *context;
16190 /* Remember whether or not an error occurred, since we are about to
16191 destroy that information. */
16192 error_occurred = cp_parser_error_occurred (parser);
16193 /* Remove the topmost context from the stack. */
16194 context = parser->context;
16195 parser->context = context->next;
16196 /* If no parse errors occurred, commit to the tentative parse. */
16197 if (!error_occurred)
16199 /* Commit to the tokens read tentatively, unless that was
16201 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16202 cp_lexer_commit_tokens (parser->lexer);
16204 pop_to_parent_deferring_access_checks ();
16206 /* Otherwise, if errors occurred, roll back our state so that things
16207 are just as they were before we began the tentative parse. */
16210 cp_lexer_rollback_tokens (parser->lexer);
16211 pop_deferring_access_checks ();
16213 /* Add the context to the front of the free list. */
16214 context->next = cp_parser_context_free_list;
16215 cp_parser_context_free_list = context;
16217 return !error_occurred;
16220 /* Returns true if we are parsing tentatively and are not committed to
16221 this tentative parse. */
16224 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16226 return (cp_parser_parsing_tentatively (parser)
16227 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16230 /* Returns nonzero iff an error has occurred during the most recent
16231 tentative parse. */
16234 cp_parser_error_occurred (cp_parser* parser)
16236 return (cp_parser_parsing_tentatively (parser)
16237 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16240 /* Returns nonzero if GNU extensions are allowed. */
16243 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16245 return parser->allow_gnu_extensions_p;
16248 /* Objective-C++ Productions */
16251 /* Parse an Objective-C expression, which feeds into a primary-expression
16255 objc-message-expression
16256 objc-string-literal
16257 objc-encode-expression
16258 objc-protocol-expression
16259 objc-selector-expression
16261 Returns a tree representation of the expression. */
16264 cp_parser_objc_expression (cp_parser* parser)
16266 /* Try to figure out what kind of declaration is present. */
16267 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16271 case CPP_OPEN_SQUARE:
16272 return cp_parser_objc_message_expression (parser);
16274 case CPP_OBJC_STRING:
16275 kwd = cp_lexer_consume_token (parser->lexer);
16276 return objc_build_string_object (kwd->value);
16279 switch (kwd->keyword)
16281 case RID_AT_ENCODE:
16282 return cp_parser_objc_encode_expression (parser);
16284 case RID_AT_PROTOCOL:
16285 return cp_parser_objc_protocol_expression (parser);
16287 case RID_AT_SELECTOR:
16288 return cp_parser_objc_selector_expression (parser);
16294 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16295 cp_parser_skip_to_end_of_block_or_statement (parser);
16298 return error_mark_node;
16301 /* Parse an Objective-C message expression.
16303 objc-message-expression:
16304 [ objc-message-receiver objc-message-args ]
16306 Returns a representation of an Objective-C message. */
16309 cp_parser_objc_message_expression (cp_parser* parser)
16311 tree receiver, messageargs;
16313 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16314 receiver = cp_parser_objc_message_receiver (parser);
16315 messageargs = cp_parser_objc_message_args (parser);
16316 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16318 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16321 /* Parse an objc-message-receiver.
16323 objc-message-receiver:
16325 simple-type-specifier
16327 Returns a representation of the type or expression. */
16330 cp_parser_objc_message_receiver (cp_parser* parser)
16334 /* An Objective-C message receiver may be either (1) a type
16335 or (2) an expression. */
16336 cp_parser_parse_tentatively (parser);
16337 rcv = cp_parser_expression (parser, false);
16339 if (cp_parser_parse_definitely (parser))
16342 rcv = cp_parser_simple_type_specifier (parser,
16343 /*decl_specs=*/NULL,
16344 CP_PARSER_FLAGS_NONE);
16346 return objc_get_class_reference (rcv);
16349 /* Parse the arguments and selectors comprising an Objective-C message.
16354 objc-selector-args , objc-comma-args
16356 objc-selector-args:
16357 objc-selector [opt] : assignment-expression
16358 objc-selector-args objc-selector [opt] : assignment-expression
16361 assignment-expression
16362 objc-comma-args , assignment-expression
16364 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16365 selector arguments and TREE_VALUE containing a list of comma
16369 cp_parser_objc_message_args (cp_parser* parser)
16371 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16372 bool maybe_unary_selector_p = true;
16373 cp_token *token = cp_lexer_peek_token (parser->lexer);
16375 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16377 tree selector = NULL_TREE, arg;
16379 if (token->type != CPP_COLON)
16380 selector = cp_parser_objc_selector (parser);
16382 /* Detect if we have a unary selector. */
16383 if (maybe_unary_selector_p
16384 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16385 return build_tree_list (selector, NULL_TREE);
16387 maybe_unary_selector_p = false;
16388 cp_parser_require (parser, CPP_COLON, "`:'");
16389 arg = cp_parser_assignment_expression (parser, false);
16392 = chainon (sel_args,
16393 build_tree_list (selector, arg));
16395 token = cp_lexer_peek_token (parser->lexer);
16398 /* Handle non-selector arguments, if any. */
16399 while (token->type == CPP_COMMA)
16403 cp_lexer_consume_token (parser->lexer);
16404 arg = cp_parser_assignment_expression (parser, false);
16407 = chainon (addl_args,
16408 build_tree_list (NULL_TREE, arg));
16410 token = cp_lexer_peek_token (parser->lexer);
16413 return build_tree_list (sel_args, addl_args);
16416 /* Parse an Objective-C encode expression.
16418 objc-encode-expression:
16419 @encode objc-typename
16421 Returns an encoded representation of the type argument. */
16424 cp_parser_objc_encode_expression (cp_parser* parser)
16428 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16429 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16430 type = complete_type (cp_parser_type_id (parser));
16431 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16435 error ("%<@encode%> must specify a type as an argument");
16436 return error_mark_node;
16439 return objc_build_encode_expr (type);
16442 /* Parse an Objective-C @defs expression. */
16445 cp_parser_objc_defs_expression (cp_parser *parser)
16449 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16450 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16451 name = cp_parser_identifier (parser);
16452 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16454 return objc_get_class_ivars (name);
16457 /* Parse an Objective-C protocol expression.
16459 objc-protocol-expression:
16460 @protocol ( identifier )
16462 Returns a representation of the protocol expression. */
16465 cp_parser_objc_protocol_expression (cp_parser* parser)
16469 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16470 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16471 proto = cp_parser_identifier (parser);
16472 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16474 return objc_build_protocol_expr (proto);
16477 /* Parse an Objective-C selector expression.
16479 objc-selector-expression:
16480 @selector ( objc-method-signature )
16482 objc-method-signature:
16488 objc-selector-seq objc-selector :
16490 Returns a representation of the method selector. */
16493 cp_parser_objc_selector_expression (cp_parser* parser)
16495 tree sel_seq = NULL_TREE;
16496 bool maybe_unary_selector_p = true;
16499 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16500 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16501 token = cp_lexer_peek_token (parser->lexer);
16503 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16504 || token->type == CPP_SCOPE)
16506 tree selector = NULL_TREE;
16508 if (token->type != CPP_COLON
16509 || token->type == CPP_SCOPE)
16510 selector = cp_parser_objc_selector (parser);
16512 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16513 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16515 /* Detect if we have a unary selector. */
16516 if (maybe_unary_selector_p)
16518 sel_seq = selector;
16519 goto finish_selector;
16523 cp_parser_error (parser, "expected %<:%>");
16526 maybe_unary_selector_p = false;
16527 token = cp_lexer_consume_token (parser->lexer);
16529 if (token->type == CPP_SCOPE)
16532 = chainon (sel_seq,
16533 build_tree_list (selector, NULL_TREE));
16535 = chainon (sel_seq,
16536 build_tree_list (NULL_TREE, NULL_TREE));
16540 = chainon (sel_seq,
16541 build_tree_list (selector, NULL_TREE));
16543 token = cp_lexer_peek_token (parser->lexer);
16547 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16549 return objc_build_selector_expr (sel_seq);
16552 /* Parse a list of identifiers.
16554 objc-identifier-list:
16556 objc-identifier-list , identifier
16558 Returns a TREE_LIST of identifier nodes. */
16561 cp_parser_objc_identifier_list (cp_parser* parser)
16563 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16564 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16566 while (sep->type == CPP_COMMA)
16568 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16569 list = chainon (list,
16570 build_tree_list (NULL_TREE,
16571 cp_parser_identifier (parser)));
16572 sep = cp_lexer_peek_token (parser->lexer);
16578 /* Parse an Objective-C alias declaration.
16580 objc-alias-declaration:
16581 @compatibility_alias identifier identifier ;
16583 This function registers the alias mapping with the Objective-C front-end.
16584 It returns nothing. */
16587 cp_parser_objc_alias_declaration (cp_parser* parser)
16591 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16592 alias = cp_parser_identifier (parser);
16593 orig = cp_parser_identifier (parser);
16594 objc_declare_alias (alias, orig);
16595 cp_parser_consume_semicolon_at_end_of_statement (parser);
16598 /* Parse an Objective-C class forward-declaration.
16600 objc-class-declaration:
16601 @class objc-identifier-list ;
16603 The function registers the forward declarations with the Objective-C
16604 front-end. It returns nothing. */
16607 cp_parser_objc_class_declaration (cp_parser* parser)
16609 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16610 objc_declare_class (cp_parser_objc_identifier_list (parser));
16611 cp_parser_consume_semicolon_at_end_of_statement (parser);
16614 /* Parse a list of Objective-C protocol references.
16616 objc-protocol-refs-opt:
16617 objc-protocol-refs [opt]
16619 objc-protocol-refs:
16620 < objc-identifier-list >
16622 Returns a TREE_LIST of identifiers, if any. */
16625 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16627 tree protorefs = NULL_TREE;
16629 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16631 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16632 protorefs = cp_parser_objc_identifier_list (parser);
16633 cp_parser_require (parser, CPP_GREATER, "`>'");
16639 /* Parse a Objective-C visibility specification. */
16642 cp_parser_objc_visibility_spec (cp_parser* parser)
16644 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16646 switch (vis->keyword)
16648 case RID_AT_PRIVATE:
16649 objc_set_visibility (2);
16651 case RID_AT_PROTECTED:
16652 objc_set_visibility (0);
16654 case RID_AT_PUBLIC:
16655 objc_set_visibility (1);
16661 /* Eat '@private'/'@protected'/'@public'. */
16662 cp_lexer_consume_token (parser->lexer);
16665 /* Parse an Objective-C method type. */
16668 cp_parser_objc_method_type (cp_parser* parser)
16670 objc_set_method_type
16671 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16676 /* Parse an Objective-C protocol qualifier. */
16679 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16681 tree quals = NULL_TREE, node;
16682 cp_token *token = cp_lexer_peek_token (parser->lexer);
16684 node = token->value;
16686 while (node && TREE_CODE (node) == IDENTIFIER_NODE
16687 && (node == ridpointers [(int) RID_IN]
16688 || node == ridpointers [(int) RID_OUT]
16689 || node == ridpointers [(int) RID_INOUT]
16690 || node == ridpointers [(int) RID_BYCOPY]
16691 || node == ridpointers [(int) RID_BYREF]
16692 || node == ridpointers [(int) RID_ONEWAY]))
16694 quals = tree_cons (NULL_TREE, node, quals);
16695 cp_lexer_consume_token (parser->lexer);
16696 token = cp_lexer_peek_token (parser->lexer);
16697 node = token->value;
16703 /* Parse an Objective-C typename. */
16706 cp_parser_objc_typename (cp_parser* parser)
16708 tree typename = NULL_TREE;
16710 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16712 tree proto_quals, cp_type = NULL_TREE;
16714 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
16715 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16717 /* An ObjC type name may consist of just protocol qualifiers, in which
16718 case the type shall default to 'id'. */
16719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16720 cp_type = cp_parser_type_id (parser);
16722 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16723 typename = build_tree_list (proto_quals, cp_type);
16729 /* Check to see if TYPE refers to an Objective-C selector name. */
16732 cp_parser_objc_selector_p (enum cpp_ttype type)
16734 return (type == CPP_NAME || type == CPP_KEYWORD
16735 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16736 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16737 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16738 || type == CPP_XOR || type == CPP_XOR_EQ);
16741 /* Parse an Objective-C selector. */
16744 cp_parser_objc_selector (cp_parser* parser)
16746 cp_token *token = cp_lexer_consume_token (parser->lexer);
16748 if (!cp_parser_objc_selector_p (token->type))
16750 error ("invalid Objective-C++ selector name");
16751 return error_mark_node;
16754 /* C++ operator names are allowed to appear in ObjC selectors. */
16755 switch (token->type)
16757 case CPP_AND_AND: return get_identifier ("and");
16758 case CPP_AND_EQ: return get_identifier ("and_eq");
16759 case CPP_AND: return get_identifier ("bitand");
16760 case CPP_OR: return get_identifier ("bitor");
16761 case CPP_COMPL: return get_identifier ("compl");
16762 case CPP_NOT: return get_identifier ("not");
16763 case CPP_NOT_EQ: return get_identifier ("not_eq");
16764 case CPP_OR_OR: return get_identifier ("or");
16765 case CPP_OR_EQ: return get_identifier ("or_eq");
16766 case CPP_XOR: return get_identifier ("xor");
16767 case CPP_XOR_EQ: return get_identifier ("xor_eq");
16768 default: return token->value;
16772 /* Parse an Objective-C params list. */
16775 cp_parser_objc_method_keyword_params (cp_parser* parser)
16777 tree params = NULL_TREE;
16778 bool maybe_unary_selector_p = true;
16779 cp_token *token = cp_lexer_peek_token (parser->lexer);
16781 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16783 tree selector = NULL_TREE, typename, identifier;
16785 if (token->type != CPP_COLON)
16786 selector = cp_parser_objc_selector (parser);
16788 /* Detect if we have a unary selector. */
16789 if (maybe_unary_selector_p
16790 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16793 maybe_unary_selector_p = false;
16794 cp_parser_require (parser, CPP_COLON, "`:'");
16795 typename = cp_parser_objc_typename (parser);
16796 identifier = cp_parser_identifier (parser);
16800 objc_build_keyword_decl (selector,
16804 token = cp_lexer_peek_token (parser->lexer);
16810 /* Parse the non-keyword Objective-C params. */
16813 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
16815 tree params = make_node (TREE_LIST);
16816 cp_token *token = cp_lexer_peek_token (parser->lexer);
16817 *ellipsisp = false; /* Initially, assume no ellipsis. */
16819 while (token->type == CPP_COMMA)
16821 cp_parameter_declarator *parmdecl;
16824 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16825 token = cp_lexer_peek_token (parser->lexer);
16827 if (token->type == CPP_ELLIPSIS)
16829 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
16834 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
16835 parm = grokdeclarator (parmdecl->declarator,
16836 &parmdecl->decl_specifiers,
16837 PARM, /*initialized=*/0,
16838 /*attrlist=*/NULL);
16840 chainon (params, build_tree_list (NULL_TREE, parm));
16841 token = cp_lexer_peek_token (parser->lexer);
16847 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
16850 cp_parser_objc_interstitial_code (cp_parser* parser)
16852 cp_token *token = cp_lexer_peek_token (parser->lexer);
16854 /* If the next token is `extern' and the following token is a string
16855 literal, then we have a linkage specification. */
16856 if (token->keyword == RID_EXTERN
16857 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
16858 cp_parser_linkage_specification (parser);
16859 /* Handle #pragma, if any. */
16860 else if (token->type == CPP_PRAGMA)
16861 cp_lexer_handle_pragma (parser->lexer);
16862 /* Allow stray semicolons. */
16863 else if (token->type == CPP_SEMICOLON)
16864 cp_lexer_consume_token (parser->lexer);
16865 /* Finally, try to parse a block-declaration, or a function-definition. */
16867 cp_parser_block_declaration (parser, /*statement_p=*/false);
16870 /* Parse a method signature. */
16873 cp_parser_objc_method_signature (cp_parser* parser)
16875 tree rettype, kwdparms, optparms;
16876 bool ellipsis = false;
16878 cp_parser_objc_method_type (parser);
16879 rettype = cp_parser_objc_typename (parser);
16880 kwdparms = cp_parser_objc_method_keyword_params (parser);
16881 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
16883 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
16886 /* Pars an Objective-C method prototype list. */
16889 cp_parser_objc_method_prototype_list (cp_parser* parser)
16891 cp_token *token = cp_lexer_peek_token (parser->lexer);
16893 while (token->keyword != RID_AT_END)
16895 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16897 objc_add_method_declaration
16898 (cp_parser_objc_method_signature (parser));
16899 cp_parser_consume_semicolon_at_end_of_statement (parser);
16902 /* Allow for interspersed non-ObjC++ code. */
16903 cp_parser_objc_interstitial_code (parser);
16905 token = cp_lexer_peek_token (parser->lexer);
16908 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
16909 objc_finish_interface ();
16912 /* Parse an Objective-C method definition list. */
16915 cp_parser_objc_method_definition_list (cp_parser* parser)
16917 cp_token *token = cp_lexer_peek_token (parser->lexer);
16919 while (token->keyword != RID_AT_END)
16923 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16925 push_deferring_access_checks (dk_deferred);
16926 objc_start_method_definition
16927 (cp_parser_objc_method_signature (parser));
16929 /* For historical reasons, we accept an optional semicolon. */
16930 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16931 cp_lexer_consume_token (parser->lexer);
16933 perform_deferred_access_checks ();
16934 stop_deferring_access_checks ();
16935 meth = cp_parser_function_definition_after_declarator (parser,
16937 pop_deferring_access_checks ();
16938 objc_finish_method_definition (meth);
16941 /* Allow for interspersed non-ObjC++ code. */
16942 cp_parser_objc_interstitial_code (parser);
16944 token = cp_lexer_peek_token (parser->lexer);
16947 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
16948 objc_finish_implementation ();
16951 /* Parse Objective-C ivars. */
16954 cp_parser_objc_class_ivars (cp_parser* parser)
16956 cp_token *token = cp_lexer_peek_token (parser->lexer);
16958 if (token->type != CPP_OPEN_BRACE)
16959 return; /* No ivars specified. */
16961 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
16962 token = cp_lexer_peek_token (parser->lexer);
16964 while (token->type != CPP_CLOSE_BRACE)
16966 cp_decl_specifier_seq declspecs;
16967 int decl_class_or_enum_p;
16968 tree prefix_attributes;
16970 cp_parser_objc_visibility_spec (parser);
16972 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16975 cp_parser_decl_specifier_seq (parser,
16976 CP_PARSER_FLAGS_OPTIONAL,
16978 &decl_class_or_enum_p);
16979 prefix_attributes = declspecs.attributes;
16980 declspecs.attributes = NULL_TREE;
16982 /* Keep going until we hit the `;' at the end of the
16984 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16986 tree width = NULL_TREE, attributes, first_attribute, decl;
16987 cp_declarator *declarator = NULL;
16988 int ctor_dtor_or_conv_p;
16990 /* Check for a (possibly unnamed) bitfield declaration. */
16991 token = cp_lexer_peek_token (parser->lexer);
16992 if (token->type == CPP_COLON)
16995 if (token->type == CPP_NAME
16996 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16999 /* Get the name of the bitfield. */
17000 declarator = make_id_declarator (NULL_TREE,
17001 cp_parser_identifier (parser));
17004 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17005 /* Get the width of the bitfield. */
17007 = cp_parser_constant_expression (parser,
17008 /*allow_non_constant=*/false,
17013 /* Parse the declarator. */
17015 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17016 &ctor_dtor_or_conv_p,
17017 /*parenthesized_p=*/NULL,
17018 /*member_p=*/false);
17021 /* Look for attributes that apply to the ivar. */
17022 attributes = cp_parser_attributes_opt (parser);
17023 /* Remember which attributes are prefix attributes and
17025 first_attribute = attributes;
17026 /* Combine the attributes. */
17027 attributes = chainon (prefix_attributes, attributes);
17031 /* Create the bitfield declaration. */
17032 decl = grokbitfield (declarator, &declspecs, width);
17033 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17036 decl = grokfield (declarator, &declspecs, NULL_TREE,
17037 NULL_TREE, attributes);
17039 /* Add the instance variable. */
17040 objc_add_instance_variable (decl);
17042 /* Reset PREFIX_ATTRIBUTES. */
17043 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17044 attributes = TREE_CHAIN (attributes);
17046 TREE_CHAIN (attributes) = NULL_TREE;
17048 token = cp_lexer_peek_token (parser->lexer);
17050 if (token->type == CPP_COMMA)
17052 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17058 cp_parser_consume_semicolon_at_end_of_statement (parser);
17059 token = cp_lexer_peek_token (parser->lexer);
17062 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17063 /* For historical reasons, we accept an optional semicolon. */
17064 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17065 cp_lexer_consume_token (parser->lexer);
17068 /* Parse an Objective-C protocol declaration. */
17071 cp_parser_objc_protocol_declaration (cp_parser* parser)
17073 tree proto, protorefs;
17076 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17077 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17079 error ("identifier expected after %<@protocol%>");
17083 /* See if we have a forward declaration or a definition. */
17084 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17086 /* Try a forward declaration first. */
17087 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17089 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17091 cp_parser_consume_semicolon_at_end_of_statement (parser);
17094 /* Ok, we got a full-fledged definition (or at least should). */
17097 proto = cp_parser_identifier (parser);
17098 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17099 objc_start_protocol (proto, protorefs);
17100 cp_parser_objc_method_prototype_list (parser);
17104 /* Parse an Objective-C superclass or category. */
17107 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17110 cp_token *next = cp_lexer_peek_token (parser->lexer);
17112 *super = *categ = NULL_TREE;
17113 if (next->type == CPP_COLON)
17115 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17116 *super = cp_parser_identifier (parser);
17118 else if (next->type == CPP_OPEN_PAREN)
17120 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17121 *categ = cp_parser_identifier (parser);
17122 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17126 /* Parse an Objective-C class interface. */
17129 cp_parser_objc_class_interface (cp_parser* parser)
17131 tree name, super, categ, protos;
17133 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17134 name = cp_parser_identifier (parser);
17135 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17136 protos = cp_parser_objc_protocol_refs_opt (parser);
17138 /* We have either a class or a category on our hands. */
17140 objc_start_category_interface (name, categ, protos);
17143 objc_start_class_interface (name, super, protos);
17144 /* Handle instance variable declarations, if any. */
17145 cp_parser_objc_class_ivars (parser);
17146 objc_continue_interface ();
17149 cp_parser_objc_method_prototype_list (parser);
17152 /* Parse an Objective-C class implementation. */
17155 cp_parser_objc_class_implementation (cp_parser* parser)
17157 tree name, super, categ;
17159 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17160 name = cp_parser_identifier (parser);
17161 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17163 /* We have either a class or a category on our hands. */
17165 objc_start_category_implementation (name, categ);
17168 objc_start_class_implementation (name, super);
17169 /* Handle instance variable declarations, if any. */
17170 cp_parser_objc_class_ivars (parser);
17171 objc_continue_implementation ();
17174 cp_parser_objc_method_definition_list (parser);
17177 /* Consume the @end token and finish off the implementation. */
17180 cp_parser_objc_end_implementation (cp_parser* parser)
17182 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17183 objc_finish_implementation ();
17186 /* Parse an Objective-C declaration. */
17189 cp_parser_objc_declaration (cp_parser* parser)
17191 /* Try to figure out what kind of declaration is present. */
17192 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17194 switch (kwd->keyword)
17197 cp_parser_objc_alias_declaration (parser);
17200 cp_parser_objc_class_declaration (parser);
17202 case RID_AT_PROTOCOL:
17203 cp_parser_objc_protocol_declaration (parser);
17205 case RID_AT_INTERFACE:
17206 cp_parser_objc_class_interface (parser);
17208 case RID_AT_IMPLEMENTATION:
17209 cp_parser_objc_class_implementation (parser);
17212 cp_parser_objc_end_implementation (parser);
17215 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17216 cp_parser_skip_to_end_of_block_or_statement (parser);
17220 /* Parse an Objective-C try-catch-finally statement.
17222 objc-try-catch-finally-stmt:
17223 @try compound-statement objc-catch-clause-seq [opt]
17224 objc-finally-clause [opt]
17226 objc-catch-clause-seq:
17227 objc-catch-clause objc-catch-clause-seq [opt]
17230 @catch ( exception-declaration ) compound-statement
17232 objc-finally-clause
17233 @finally compound-statement
17235 Returns NULL_TREE. */
17238 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17239 location_t location;
17242 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17243 location = cp_lexer_peek_token (parser->lexer)->location;
17244 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17245 node, lest it get absorbed into the surrounding block. */
17246 stmt = push_stmt_list ();
17247 cp_parser_compound_statement (parser, NULL, false);
17248 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17250 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17252 cp_parameter_declarator *parmdecl;
17255 cp_lexer_consume_token (parser->lexer);
17256 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17257 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17258 parm = grokdeclarator (parmdecl->declarator,
17259 &parmdecl->decl_specifiers,
17260 PARM, /*initialized=*/0,
17261 /*attrlist=*/NULL);
17262 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17263 objc_begin_catch_clause (parm);
17264 cp_parser_compound_statement (parser, NULL, false);
17265 objc_finish_catch_clause ();
17268 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17270 cp_lexer_consume_token (parser->lexer);
17271 location = cp_lexer_peek_token (parser->lexer)->location;
17272 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17273 node, lest it get absorbed into the surrounding block. */
17274 stmt = push_stmt_list ();
17275 cp_parser_compound_statement (parser, NULL, false);
17276 objc_build_finally_clause (location, pop_stmt_list (stmt));
17279 return objc_finish_try_stmt ();
17282 /* Parse an Objective-C synchronized statement.
17284 objc-synchronized-stmt:
17285 @synchronized ( expression ) compound-statement
17287 Returns NULL_TREE. */
17290 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17291 location_t location;
17294 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17296 location = cp_lexer_peek_token (parser->lexer)->location;
17297 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17298 lock = cp_parser_expression (parser, false);
17299 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17301 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17302 node, lest it get absorbed into the surrounding block. */
17303 stmt = push_stmt_list ();
17304 cp_parser_compound_statement (parser, NULL, false);
17306 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17309 /* Parse an Objective-C throw statement.
17312 @throw assignment-expression [opt] ;
17314 Returns a constructed '@throw' statement. */
17317 cp_parser_objc_throw_statement (cp_parser *parser) {
17318 tree expr = NULL_TREE;
17320 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17323 expr = cp_parser_assignment_expression (parser, false);
17325 cp_parser_consume_semicolon_at_end_of_statement (parser);
17327 return objc_build_throw_stmt (expr);
17330 /* Parse an Objective-C statement. */
17333 cp_parser_objc_statement (cp_parser * parser) {
17334 /* Try to figure out what kind of declaration is present. */
17335 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17337 switch (kwd->keyword)
17340 return cp_parser_objc_try_catch_finally_statement (parser);
17341 case RID_AT_SYNCHRONIZED:
17342 return cp_parser_objc_synchronized_statement (parser);
17344 return cp_parser_objc_throw_statement (parser);
17346 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17347 cp_parser_skip_to_end_of_block_or_statement (parser);
17350 return error_mark_node;
17355 static GTY (()) cp_parser *the_parser;
17357 /* External interface. */
17359 /* Parse one entire translation unit. */
17362 c_parse_file (void)
17364 bool error_occurred;
17365 static bool already_called = false;
17367 if (already_called)
17369 sorry ("inter-module optimizations not implemented for C++");
17372 already_called = true;
17374 the_parser = cp_parser_new ();
17375 push_deferring_access_checks (flag_access_control
17376 ? dk_no_deferred : dk_no_check);
17377 error_occurred = cp_parser_translation_unit (the_parser);
17381 /* This variable must be provided by every front end. */
17385 #include "gt-cp-parser.h"