2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
49 typedef struct cp_token GTY (())
51 /* The kind of token. */
52 ENUM_BITFIELD (cpp_ttype) type : 8;
53 /* If this token is a keyword, this value indicates which keyword.
54 Otherwise, this value is RID_MAX. */
55 ENUM_BITFIELD (rid) keyword : 8;
58 /* True if this token is from a system header. */
59 BOOL_BITFIELD in_system_header : 1;
60 /* True if this token is from a context where it is implicitly extern "C" */
61 BOOL_BITFIELD implicit_extern_c : 1;
62 /* True for a CPP_NAME token that is not a keyword (i.e., for which
63 KEYWORD is RID_MAX) iff this name was looked up and found to be
64 ambiguous. An error has already been reported. */
65 BOOL_BITFIELD ambiguous_p : 1;
66 /* The value associated with this token, if any. */
68 /* The location at which this token was found. */
72 /* We use a stack of token pointer for saving token sets. */
73 typedef struct cp_token *cp_token_position;
74 DEF_VEC_P (cp_token_position);
75 DEF_VEC_ALLOC_P (cp_token_position,heap);
77 static const cp_token eof_token =
79 CPP_EOF, RID_MAX, 0, 0, 0, false, NULL_TREE,
80 #if USE_MAPPED_LOCATION
87 /* The cp_lexer structure represents the C++ lexer. It is responsible
88 for managing the token stream from the preprocessor and supplying
89 it to the parser. Tokens are never added to the cp_lexer after
92 typedef struct cp_lexer GTY (())
94 /* The memory allocated for the buffer. NULL if this lexer does not
95 own the token buffer. */
96 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
97 /* If the lexer owns the buffer, this is the number of tokens in the
101 /* A pointer just past the last available token. The tokens
102 in this lexer are [buffer, last_token). */
103 cp_token_position GTY ((skip)) last_token;
105 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
106 no more available tokens. */
107 cp_token_position GTY ((skip)) next_token;
109 /* A stack indicating positions at which cp_lexer_save_tokens was
110 called. The top entry is the most recent position at which we
111 began saving tokens. If the stack is non-empty, we are saving
113 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
115 /* True if we should output debugging information. */
118 /* The next lexer in a linked list of lexers. */
119 struct cp_lexer *next;
122 /* cp_token_cache is a range of tokens. There is no need to represent
123 allocate heap memory for it, since tokens are never removed from the
124 lexer's array. There is also no need for the GC to walk through
125 a cp_token_cache, since everything in here is referenced through
128 typedef struct cp_token_cache GTY(())
130 /* The beginning of the token range. */
131 cp_token * GTY((skip)) first;
133 /* Points immediately after the last token in the range. */
134 cp_token * GTY ((skip)) last;
139 static cp_lexer *cp_lexer_new_main
141 static cp_lexer *cp_lexer_new_from_tokens
142 (cp_token_cache *tokens);
143 static void cp_lexer_destroy
145 static int cp_lexer_saving_tokens
147 static cp_token_position cp_lexer_token_position
149 static cp_token *cp_lexer_token_at
150 (cp_lexer *, cp_token_position);
151 static void cp_lexer_get_preprocessor_token
152 (cp_lexer *, cp_token *);
153 static inline cp_token *cp_lexer_peek_token
155 static cp_token *cp_lexer_peek_nth_token
156 (cp_lexer *, size_t);
157 static inline bool cp_lexer_next_token_is
158 (cp_lexer *, enum cpp_ttype);
159 static bool cp_lexer_next_token_is_not
160 (cp_lexer *, enum cpp_ttype);
161 static bool cp_lexer_next_token_is_keyword
162 (cp_lexer *, enum rid);
163 static cp_token *cp_lexer_consume_token
165 static void cp_lexer_purge_token
167 static void cp_lexer_purge_tokens_after
168 (cp_lexer *, cp_token_position);
169 static void cp_lexer_handle_pragma
171 static void cp_lexer_save_tokens
173 static void cp_lexer_commit_tokens
175 static void cp_lexer_rollback_tokens
177 #ifdef ENABLE_CHECKING
178 static void cp_lexer_print_token
179 (FILE *, cp_token *);
180 static inline bool cp_lexer_debugging_p
182 static void cp_lexer_start_debugging
183 (cp_lexer *) ATTRIBUTE_UNUSED;
184 static void cp_lexer_stop_debugging
185 (cp_lexer *) ATTRIBUTE_UNUSED;
187 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
188 about passing NULL to functions that require non-NULL arguments
189 (fputs, fprintf). It will never be used, so all we need is a value
190 of the right type that's guaranteed not to be NULL. */
191 #define cp_lexer_debug_stream stdout
192 #define cp_lexer_print_token(str, tok) (void) 0
193 #define cp_lexer_debugging_p(lexer) 0
194 #endif /* ENABLE_CHECKING */
196 static cp_token_cache *cp_token_cache_new
197 (cp_token *, cp_token *);
199 /* Manifest constants. */
200 #define CP_LEXER_BUFFER_SIZE 10000
201 #define CP_SAVED_TOKEN_STACK 5
203 /* A token type for keywords, as opposed to ordinary identifiers. */
204 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
206 /* A token type for template-ids. If a template-id is processed while
207 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
208 the value of the CPP_TEMPLATE_ID is whatever was returned by
209 cp_parser_template_id. */
210 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
212 /* A token type for nested-name-specifiers. If a
213 nested-name-specifier is processed while parsing tentatively, it is
214 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
215 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
216 cp_parser_nested_name_specifier_opt. */
217 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
219 /* A token type for tokens that are not tokens at all; these are used
220 to represent slots in the array where there used to be a token
221 that has now been deleted. */
222 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
224 /* The number of token types, including C++-specific ones. */
225 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
229 #ifdef ENABLE_CHECKING
230 /* The stream to which debugging output should be written. */
231 static FILE *cp_lexer_debug_stream;
232 #endif /* ENABLE_CHECKING */
234 /* Create a new main C++ lexer, the lexer that gets tokens from the
238 cp_lexer_new_main (void)
240 cp_token first_token;
247 /* It's possible that lexing the first token will load a PCH file,
248 which is a GC collection point. So we have to grab the first
249 token before allocating any memory. Pragmas must not be deferred
250 as -fpch-preprocess can generate a pragma to load the PCH file in
251 the preprocessed output used by -save-temps. */
252 cp_lexer_get_preprocessor_token (NULL, &first_token);
254 /* Tell cpplib we want CPP_PRAGMA tokens. */
255 cpp_get_options (parse_in)->defer_pragmas = true;
257 /* Tell c_lex not to merge string constants. */
258 c_lex_return_raw_strings = true;
260 c_common_no_more_pch ();
262 /* Allocate the memory. */
263 lexer = GGC_CNEW (cp_lexer);
265 #ifdef ENABLE_CHECKING
266 /* Initially we are not debugging. */
267 lexer->debugging_p = false;
268 #endif /* ENABLE_CHECKING */
269 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
270 CP_SAVED_TOKEN_STACK);
272 /* Create the buffer. */
273 alloc = CP_LEXER_BUFFER_SIZE;
274 buffer = ggc_alloc (alloc * sizeof (cp_token));
276 /* Put the first token in the buffer. */
281 /* Get the remaining tokens from the preprocessor. */
282 while (pos->type != CPP_EOF)
289 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
290 pos = buffer + space;
292 cp_lexer_get_preprocessor_token (lexer, pos);
294 lexer->buffer = buffer;
295 lexer->buffer_length = alloc - space;
296 lexer->last_token = pos;
297 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
299 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
300 direct calls to c_lex. Those callers all expect c_lex to do
301 string constant concatenation. */
302 c_lex_return_raw_strings = false;
304 /* Subsequent preprocessor diagnostics should use compiler
305 diagnostic functions to get the compiler source location. */
306 cpp_get_options (parse_in)->client_diagnostic = true;
307 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
309 gcc_assert (lexer->next_token->type != CPP_PURGED);
313 /* Create a new lexer whose token stream is primed with the tokens in
314 CACHE. When these tokens are exhausted, no new tokens will be read. */
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
319 cp_token *first = cache->first;
320 cp_token *last = cache->last;
321 cp_lexer *lexer = GGC_CNEW (cp_lexer);
323 /* We do not own the buffer. */
324 lexer->buffer = NULL;
325 lexer->buffer_length = 0;
326 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327 lexer->last_token = last;
329 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330 CP_SAVED_TOKEN_STACK);
332 #ifdef ENABLE_CHECKING
333 /* Initially we are not debugging. */
334 lexer->debugging_p = false;
337 gcc_assert (lexer->next_token->type != CPP_PURGED);
341 /* Frees all resources associated with LEXER. */
344 cp_lexer_destroy (cp_lexer *lexer)
347 ggc_free (lexer->buffer);
348 VEC_free (cp_token_position, heap, lexer->saved_tokens);
352 /* Returns nonzero if debugging information should be output. */
354 #ifdef ENABLE_CHECKING
357 cp_lexer_debugging_p (cp_lexer *lexer)
359 return lexer->debugging_p;
362 #endif /* ENABLE_CHECKING */
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
367 gcc_assert (!previous_p || lexer->next_token != &eof_token);
369 return lexer->next_token - previous_p;
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
378 /* nonzero if we are presently saving tokens. */
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
383 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
386 /* Store the next token from the preprocessor in *TOKEN. Return true
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
393 static int is_extern_c = 0;
395 /* Get a new token from the preprocessor. */
397 = c_lex_with_flags (&token->value, &token->location, &token->flags);
398 token->in_system_header = in_system_header;
400 /* On some systems, some header files are surrounded by an
401 implicit extern "C" block. Set a flag in the token if it
402 comes from such a header. */
403 is_extern_c += pending_lang_change;
404 pending_lang_change = 0;
405 token->implicit_extern_c = is_extern_c > 0;
407 /* Check to see if this token is a keyword. */
408 if (token->type == CPP_NAME)
410 if (C_IS_RESERVED_WORD (token->value))
412 /* Mark this token as a keyword. */
413 token->type = CPP_KEYWORD;
414 /* Record which keyword. */
415 token->keyword = C_RID_CODE (token->value);
416 /* Update the value. Some keywords are mapped to particular
417 entities, rather than simply having the value of the
418 corresponding IDENTIFIER_NODE. For example, `__const' is
419 mapped to `const'. */
420 token->value = ridpointers[token->keyword];
424 token->ambiguous_p = false;
425 token->keyword = RID_MAX;
428 /* Handle Objective-C++ keywords. */
429 else if (token->type == CPP_AT_NAME)
431 token->type = CPP_KEYWORD;
432 switch (C_RID_CODE (token->value))
434 /* Map 'class' to '@class', 'private' to '@private', etc. */
435 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439 case RID_THROW: token->keyword = RID_AT_THROW; break;
440 case RID_TRY: token->keyword = RID_AT_TRY; break;
441 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442 default: token->keyword = C_RID_CODE (token->value);
446 token->keyword = RID_MAX;
449 /* Update the globals input_location and in_system_header from TOKEN. */
451 cp_lexer_set_source_position_from_token (cp_token *token)
453 if (token->type != CPP_EOF)
455 input_location = token->location;
456 in_system_header = token->in_system_header;
460 /* Return a pointer to the next token in the token stream, but do not
463 static inline cp_token *
464 cp_lexer_peek_token (cp_lexer *lexer)
466 if (cp_lexer_debugging_p (lexer))
468 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
469 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
470 putc ('\n', cp_lexer_debug_stream);
472 return lexer->next_token;
475 /* Return true if the next token has the indicated TYPE. */
478 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
480 return cp_lexer_peek_token (lexer)->type == type;
483 /* Return true if the next token does not have the indicated TYPE. */
486 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
488 return !cp_lexer_next_token_is (lexer, type);
491 /* Return true if the next token is the indicated KEYWORD. */
494 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
498 /* Peek at the next token. */
499 token = cp_lexer_peek_token (lexer);
500 /* Check to see if it is the indicated keyword. */
501 return token->keyword == keyword;
505 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
509 token = cp_lexer_peek_token (lexer);
510 switch (token->keyword)
512 /* Storage classes. */
519 /* Elaborated type specifiers. */
525 /* Simple type specifiers. */
537 /* GNU extensions. */
547 /* Return a pointer to the Nth token in the token stream. If N is 1,
548 then this is precisely equivalent to cp_lexer_peek_token (except
549 that it is not inline). One would like to disallow that case, but
550 there is one case (cp_parser_nth_token_starts_template_id) where
551 the caller passes a variable for N and it might be 1. */
554 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
558 /* N is 1-based, not zero-based. */
561 if (cp_lexer_debugging_p (lexer))
562 fprintf (cp_lexer_debug_stream,
563 "cp_lexer: peeking ahead %ld at token: ", (long)n);
566 token = lexer->next_token;
567 gcc_assert (!n || token != &eof_token);
571 if (token == lexer->last_token)
573 token = (cp_token *)&eof_token;
577 if (token->type != CPP_PURGED)
581 if (cp_lexer_debugging_p (lexer))
583 cp_lexer_print_token (cp_lexer_debug_stream, token);
584 putc ('\n', cp_lexer_debug_stream);
590 /* Return the next token, and advance the lexer's next_token pointer
591 to point to the next non-purged token. */
594 cp_lexer_consume_token (cp_lexer* lexer)
596 cp_token *token = lexer->next_token;
598 gcc_assert (token != &eof_token);
603 if (lexer->next_token == lexer->last_token)
605 lexer->next_token = (cp_token *)&eof_token;
610 while (lexer->next_token->type == CPP_PURGED);
612 cp_lexer_set_source_position_from_token (token);
614 /* Provide debugging output. */
615 if (cp_lexer_debugging_p (lexer))
617 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
618 cp_lexer_print_token (cp_lexer_debug_stream, token);
619 putc ('\n', cp_lexer_debug_stream);
625 /* Permanently remove the next token from the token stream, and
626 advance the next_token pointer to refer to the next non-purged
630 cp_lexer_purge_token (cp_lexer *lexer)
632 cp_token *tok = lexer->next_token;
634 gcc_assert (tok != &eof_token);
635 tok->type = CPP_PURGED;
636 tok->location = UNKNOWN_LOCATION;
637 tok->value = NULL_TREE;
638 tok->keyword = RID_MAX;
643 if (tok == lexer->last_token)
645 tok = (cp_token *)&eof_token;
649 while (tok->type == CPP_PURGED);
650 lexer->next_token = tok;
653 /* Permanently remove all tokens after TOK, up to, but not
654 including, the token that will be returned next by
655 cp_lexer_peek_token. */
658 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
660 cp_token *peek = lexer->next_token;
662 if (peek == &eof_token)
663 peek = lexer->last_token;
665 gcc_assert (tok < peek);
667 for ( tok += 1; tok != peek; tok += 1)
669 tok->type = CPP_PURGED;
670 tok->location = UNKNOWN_LOCATION;
671 tok->value = NULL_TREE;
672 tok->keyword = RID_MAX;
676 /* Consume and handle a pragma token. */
678 cp_lexer_handle_pragma (cp_lexer *lexer)
681 cp_token *token = cp_lexer_consume_token (lexer);
682 gcc_assert (token->type == CPP_PRAGMA);
683 gcc_assert (token->value);
685 s.len = TREE_STRING_LENGTH (token->value);
686 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
688 cpp_handle_deferred_pragma (parse_in, &s);
690 /* Clearing token->value here means that we will get an ICE if we
691 try to process this #pragma again (which should be impossible). */
695 /* Begin saving tokens. All tokens consumed after this point will be
699 cp_lexer_save_tokens (cp_lexer* lexer)
701 /* Provide debugging output. */
702 if (cp_lexer_debugging_p (lexer))
703 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
705 VEC_safe_push (cp_token_position, heap,
706 lexer->saved_tokens, lexer->next_token);
709 /* Commit to the portion of the token stream most recently saved. */
712 cp_lexer_commit_tokens (cp_lexer* lexer)
714 /* Provide debugging output. */
715 if (cp_lexer_debugging_p (lexer))
716 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
718 VEC_pop (cp_token_position, lexer->saved_tokens);
721 /* Return all tokens saved since the last call to cp_lexer_save_tokens
722 to the token stream. Stop saving tokens. */
725 cp_lexer_rollback_tokens (cp_lexer* lexer)
727 /* Provide debugging output. */
728 if (cp_lexer_debugging_p (lexer))
729 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
731 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
734 /* Print a representation of the TOKEN on the STREAM. */
736 #ifdef ENABLE_CHECKING
739 cp_lexer_print_token (FILE * stream, cp_token *token)
741 /* We don't use cpp_type2name here because the parser defines
742 a few tokens of its own. */
743 static const char *const token_names[] = {
744 /* cpplib-defined token types */
750 /* C++ parser token types - see "Manifest constants", above. */
753 "NESTED_NAME_SPECIFIER",
757 /* If we have a name for the token, print it out. Otherwise, we
758 simply give the numeric code. */
759 gcc_assert (token->type < ARRAY_SIZE(token_names));
760 fputs (token_names[token->type], stream);
762 /* For some tokens, print the associated data. */
766 /* Some keywords have a value that is not an IDENTIFIER_NODE.
767 For example, `struct' is mapped to an INTEGER_CST. */
768 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
770 /* else fall through */
772 fputs (IDENTIFIER_POINTER (token->value), stream);
778 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
786 /* Start emitting debugging information. */
789 cp_lexer_start_debugging (cp_lexer* lexer)
791 lexer->debugging_p = true;
794 /* Stop emitting debugging information. */
797 cp_lexer_stop_debugging (cp_lexer* lexer)
799 lexer->debugging_p = false;
802 #endif /* ENABLE_CHECKING */
804 /* Create a new cp_token_cache, representing a range of tokens. */
806 static cp_token_cache *
807 cp_token_cache_new (cp_token *first, cp_token *last)
809 cp_token_cache *cache = GGC_NEW (cp_token_cache);
810 cache->first = first;
816 /* Decl-specifiers. */
818 static void clear_decl_specs
819 (cp_decl_specifier_seq *);
821 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
824 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
826 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
831 /* Nothing other than the parser should be creating declarators;
832 declarators are a semi-syntactic representation of C++ entities.
833 Other parts of the front end that need to create entities (like
834 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
836 static cp_declarator *make_call_declarator
837 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
838 static cp_declarator *make_array_declarator
839 (cp_declarator *, tree);
840 static cp_declarator *make_pointer_declarator
841 (cp_cv_quals, cp_declarator *);
842 static cp_declarator *make_reference_declarator
843 (cp_cv_quals, cp_declarator *);
844 static cp_parameter_declarator *make_parameter_declarator
845 (cp_decl_specifier_seq *, cp_declarator *, tree);
846 static cp_declarator *make_ptrmem_declarator
847 (cp_cv_quals, tree, cp_declarator *);
849 cp_declarator *cp_error_declarator;
851 /* The obstack on which declarators and related data structures are
853 static struct obstack declarator_obstack;
855 /* Alloc BYTES from the declarator memory pool. */
858 alloc_declarator (size_t bytes)
860 return obstack_alloc (&declarator_obstack, bytes);
863 /* Allocate a declarator of the indicated KIND. Clear fields that are
864 common to all declarators. */
866 static cp_declarator *
867 make_declarator (cp_declarator_kind kind)
869 cp_declarator *declarator;
871 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
872 declarator->kind = kind;
873 declarator->attributes = NULL_TREE;
874 declarator->declarator = NULL;
879 /* Make a declarator for a generalized identifier. If
880 QUALIFYING_SCOPE is non-NULL, the identifier is
881 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
882 UNQUALIFIED_NAME. SFK indicates the kind of special function this
885 static cp_declarator *
886 make_id_declarator (tree qualifying_scope, tree unqualified_name,
887 special_function_kind sfk)
889 cp_declarator *declarator;
891 /* It is valid to write:
893 class C { void f(); };
897 The standard is not clear about whether `typedef const C D' is
898 legal; as of 2002-09-15 the committee is considering that
899 question. EDG 3.0 allows that syntax. Therefore, we do as
901 if (qualifying_scope && TYPE_P (qualifying_scope))
902 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
904 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
905 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
906 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
908 declarator = make_declarator (cdk_id);
909 declarator->u.id.qualifying_scope = qualifying_scope;
910 declarator->u.id.unqualified_name = unqualified_name;
911 declarator->u.id.sfk = sfk;
916 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
917 of modifiers such as const or volatile to apply to the pointer
918 type, represented as identifiers. */
921 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
923 cp_declarator *declarator;
925 declarator = make_declarator (cdk_pointer);
926 declarator->declarator = target;
927 declarator->u.pointer.qualifiers = cv_qualifiers;
928 declarator->u.pointer.class_type = NULL_TREE;
933 /* Like make_pointer_declarator -- but for references. */
936 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
938 cp_declarator *declarator;
940 declarator = make_declarator (cdk_reference);
941 declarator->declarator = target;
942 declarator->u.pointer.qualifiers = cv_qualifiers;
943 declarator->u.pointer.class_type = NULL_TREE;
948 /* Like make_pointer_declarator -- but for a pointer to a non-static
949 member of CLASS_TYPE. */
952 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
953 cp_declarator *pointee)
955 cp_declarator *declarator;
957 declarator = make_declarator (cdk_ptrmem);
958 declarator->declarator = pointee;
959 declarator->u.pointer.qualifiers = cv_qualifiers;
960 declarator->u.pointer.class_type = class_type;
965 /* Make a declarator for the function given by TARGET, with the
966 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
967 "const"-qualified member function. The EXCEPTION_SPECIFICATION
968 indicates what exceptions can be thrown. */
971 make_call_declarator (cp_declarator *target,
972 cp_parameter_declarator *parms,
973 cp_cv_quals cv_qualifiers,
974 tree exception_specification)
976 cp_declarator *declarator;
978 declarator = make_declarator (cdk_function);
979 declarator->declarator = target;
980 declarator->u.function.parameters = parms;
981 declarator->u.function.qualifiers = cv_qualifiers;
982 declarator->u.function.exception_specification = exception_specification;
987 /* Make a declarator for an array of BOUNDS elements, each of which is
988 defined by ELEMENT. */
991 make_array_declarator (cp_declarator *element, tree bounds)
993 cp_declarator *declarator;
995 declarator = make_declarator (cdk_array);
996 declarator->declarator = element;
997 declarator->u.array.bounds = bounds;
1002 cp_parameter_declarator *no_parameters;
1004 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1005 DECLARATOR and DEFAULT_ARGUMENT. */
1007 cp_parameter_declarator *
1008 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1009 cp_declarator *declarator,
1010 tree default_argument)
1012 cp_parameter_declarator *parameter;
1014 parameter = ((cp_parameter_declarator *)
1015 alloc_declarator (sizeof (cp_parameter_declarator)));
1016 parameter->next = NULL;
1017 if (decl_specifiers)
1018 parameter->decl_specifiers = *decl_specifiers;
1020 clear_decl_specs (¶meter->decl_specifiers);
1021 parameter->declarator = declarator;
1022 parameter->default_argument = default_argument;
1023 parameter->ellipsis_p = false;
1028 /* Returns true iff DECLARATOR is a declaration for a function. */
1031 function_declarator_p (const cp_declarator *declarator)
1035 if (declarator->kind == cdk_function
1036 && declarator->declarator->kind == cdk_id)
1038 if (declarator->kind == cdk_id
1039 || declarator->kind == cdk_error)
1041 declarator = declarator->declarator;
1051 A cp_parser parses the token stream as specified by the C++
1052 grammar. Its job is purely parsing, not semantic analysis. For
1053 example, the parser breaks the token stream into declarators,
1054 expressions, statements, and other similar syntactic constructs.
1055 It does not check that the types of the expressions on either side
1056 of an assignment-statement are compatible, or that a function is
1057 not declared with a parameter of type `void'.
1059 The parser invokes routines elsewhere in the compiler to perform
1060 semantic analysis and to build up the abstract syntax tree for the
1063 The parser (and the template instantiation code, which is, in a
1064 way, a close relative of parsing) are the only parts of the
1065 compiler that should be calling push_scope and pop_scope, or
1066 related functions. The parser (and template instantiation code)
1067 keeps track of what scope is presently active; everything else
1068 should simply honor that. (The code that generates static
1069 initializers may also need to set the scope, in order to check
1070 access control correctly when emitting the initializers.)
1075 The parser is of the standard recursive-descent variety. Upcoming
1076 tokens in the token stream are examined in order to determine which
1077 production to use when parsing a non-terminal. Some C++ constructs
1078 require arbitrary look ahead to disambiguate. For example, it is
1079 impossible, in the general case, to tell whether a statement is an
1080 expression or declaration without scanning the entire statement.
1081 Therefore, the parser is capable of "parsing tentatively." When the
1082 parser is not sure what construct comes next, it enters this mode.
1083 Then, while we attempt to parse the construct, the parser queues up
1084 error messages, rather than issuing them immediately, and saves the
1085 tokens it consumes. If the construct is parsed successfully, the
1086 parser "commits", i.e., it issues any queued error messages and
1087 the tokens that were being preserved are permanently discarded.
1088 If, however, the construct is not parsed successfully, the parser
1089 rolls back its state completely so that it can resume parsing using
1090 a different alternative.
1095 The performance of the parser could probably be improved substantially.
1096 We could often eliminate the need to parse tentatively by looking ahead
1097 a little bit. In some places, this approach might not entirely eliminate
1098 the need to parse tentatively, but it might still speed up the average
1101 /* Flags that are passed to some parsing functions. These values can
1102 be bitwise-ored together. */
1104 typedef enum cp_parser_flags
1107 CP_PARSER_FLAGS_NONE = 0x0,
1108 /* The construct is optional. If it is not present, then no error
1109 should be issued. */
1110 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1111 /* When parsing a type-specifier, do not allow user-defined types. */
1112 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1115 /* The different kinds of declarators we want to parse. */
1117 typedef enum cp_parser_declarator_kind
1119 /* We want an abstract declarator. */
1120 CP_PARSER_DECLARATOR_ABSTRACT,
1121 /* We want a named declarator. */
1122 CP_PARSER_DECLARATOR_NAMED,
1123 /* We don't mind, but the name must be an unqualified-id. */
1124 CP_PARSER_DECLARATOR_EITHER
1125 } cp_parser_declarator_kind;
1127 /* The precedence values used to parse binary expressions. The minimum value
1128 of PREC must be 1, because zero is reserved to quickly discriminate
1129 binary operators from other tokens. */
1134 PREC_LOGICAL_OR_EXPRESSION,
1135 PREC_LOGICAL_AND_EXPRESSION,
1136 PREC_INCLUSIVE_OR_EXPRESSION,
1137 PREC_EXCLUSIVE_OR_EXPRESSION,
1138 PREC_AND_EXPRESSION,
1139 PREC_EQUALITY_EXPRESSION,
1140 PREC_RELATIONAL_EXPRESSION,
1141 PREC_SHIFT_EXPRESSION,
1142 PREC_ADDITIVE_EXPRESSION,
1143 PREC_MULTIPLICATIVE_EXPRESSION,
1145 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1148 /* A mapping from a token type to a corresponding tree node type, with a
1149 precedence value. */
1151 typedef struct cp_parser_binary_operations_map_node
1153 /* The token type. */
1154 enum cpp_ttype token_type;
1155 /* The corresponding tree code. */
1156 enum tree_code tree_type;
1157 /* The precedence of this operator. */
1158 enum cp_parser_prec prec;
1159 } cp_parser_binary_operations_map_node;
1161 /* The status of a tentative parse. */
1163 typedef enum cp_parser_status_kind
1165 /* No errors have occurred. */
1166 CP_PARSER_STATUS_KIND_NO_ERROR,
1167 /* An error has occurred. */
1168 CP_PARSER_STATUS_KIND_ERROR,
1169 /* We are committed to this tentative parse, whether or not an error
1171 CP_PARSER_STATUS_KIND_COMMITTED
1172 } cp_parser_status_kind;
1174 typedef struct cp_parser_expression_stack_entry
1177 enum tree_code tree_type;
1179 } cp_parser_expression_stack_entry;
1181 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1182 entries because precedence levels on the stack are monotonically
1184 typedef struct cp_parser_expression_stack_entry
1185 cp_parser_expression_stack[NUM_PREC_VALUES];
1187 /* Context that is saved and restored when parsing tentatively. */
1188 typedef struct cp_parser_context GTY (())
1190 /* If this is a tentative parsing context, the status of the
1192 enum cp_parser_status_kind status;
1193 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1194 that are looked up in this context must be looked up both in the
1195 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1196 the context of the containing expression. */
1199 /* The next parsing context in the stack. */
1200 struct cp_parser_context *next;
1201 } cp_parser_context;
1205 /* Constructors and destructors. */
1207 static cp_parser_context *cp_parser_context_new
1208 (cp_parser_context *);
1210 /* Class variables. */
1212 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1214 /* The operator-precedence table used by cp_parser_binary_expression.
1215 Transformed into an associative array (binops_by_token) by
1218 static const cp_parser_binary_operations_map_node binops[] = {
1219 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1220 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1222 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1223 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1224 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1226 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1227 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1229 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1230 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1232 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1233 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1234 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1235 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1236 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1237 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1239 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1240 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1242 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1244 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1246 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1248 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1250 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1253 /* The same as binops, but initialized by cp_parser_new so that
1254 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1256 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1258 /* Constructors and destructors. */
1260 /* Construct a new context. The context below this one on the stack
1261 is given by NEXT. */
1263 static cp_parser_context *
1264 cp_parser_context_new (cp_parser_context* next)
1266 cp_parser_context *context;
1268 /* Allocate the storage. */
1269 if (cp_parser_context_free_list != NULL)
1271 /* Pull the first entry from the free list. */
1272 context = cp_parser_context_free_list;
1273 cp_parser_context_free_list = context->next;
1274 memset (context, 0, sizeof (*context));
1277 context = GGC_CNEW (cp_parser_context);
1279 /* No errors have occurred yet in this context. */
1280 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1281 /* If this is not the bottomost context, copy information that we
1282 need from the previous context. */
1285 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1286 expression, then we are parsing one in this context, too. */
1287 context->object_type = next->object_type;
1288 /* Thread the stack. */
1289 context->next = next;
1295 /* The cp_parser structure represents the C++ parser. */
1297 typedef struct cp_parser GTY(())
1299 /* The lexer from which we are obtaining tokens. */
1302 /* The scope in which names should be looked up. If NULL_TREE, then
1303 we look up names in the scope that is currently open in the
1304 source program. If non-NULL, this is either a TYPE or
1305 NAMESPACE_DECL for the scope in which we should look. It can
1306 also be ERROR_MARK, when we've parsed a bogus scope.
1308 This value is not cleared automatically after a name is looked
1309 up, so we must be careful to clear it before starting a new look
1310 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1311 will look up `Z' in the scope of `X', rather than the current
1312 scope.) Unfortunately, it is difficult to tell when name lookup
1313 is complete, because we sometimes peek at a token, look it up,
1314 and then decide not to consume it. */
1317 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1318 last lookup took place. OBJECT_SCOPE is used if an expression
1319 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1320 respectively. QUALIFYING_SCOPE is used for an expression of the
1321 form "X::Y"; it refers to X. */
1323 tree qualifying_scope;
1325 /* A stack of parsing contexts. All but the bottom entry on the
1326 stack will be tentative contexts.
1328 We parse tentatively in order to determine which construct is in
1329 use in some situations. For example, in order to determine
1330 whether a statement is an expression-statement or a
1331 declaration-statement we parse it tentatively as a
1332 declaration-statement. If that fails, we then reparse the same
1333 token stream as an expression-statement. */
1334 cp_parser_context *context;
1336 /* True if we are parsing GNU C++. If this flag is not set, then
1337 GNU extensions are not recognized. */
1338 bool allow_gnu_extensions_p;
1340 /* TRUE if the `>' token should be interpreted as the greater-than
1341 operator. FALSE if it is the end of a template-id or
1342 template-parameter-list. */
1343 bool greater_than_is_operator_p;
1345 /* TRUE if default arguments are allowed within a parameter list
1346 that starts at this point. FALSE if only a gnu extension makes
1347 them permissible. */
1348 bool default_arg_ok_p;
1350 /* TRUE if we are parsing an integral constant-expression. See
1351 [expr.const] for a precise definition. */
1352 bool integral_constant_expression_p;
1354 /* TRUE if we are parsing an integral constant-expression -- but a
1355 non-constant expression should be permitted as well. This flag
1356 is used when parsing an array bound so that GNU variable-length
1357 arrays are tolerated. */
1358 bool allow_non_integral_constant_expression_p;
1360 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1361 been seen that makes the expression non-constant. */
1362 bool non_integral_constant_expression_p;
1364 /* TRUE if local variable names and `this' are forbidden in the
1366 bool local_variables_forbidden_p;
1368 /* TRUE if the declaration we are parsing is part of a
1369 linkage-specification of the form `extern string-literal
1371 bool in_unbraced_linkage_specification_p;
1373 /* TRUE if we are presently parsing a declarator, after the
1374 direct-declarator. */
1375 bool in_declarator_p;
1377 /* TRUE if we are presently parsing a template-argument-list. */
1378 bool in_template_argument_list_p;
1380 /* TRUE if we are presently parsing the body of an
1381 iteration-statement. */
1382 bool in_iteration_statement_p;
1384 /* TRUE if we are presently parsing the body of a switch
1386 bool in_switch_statement_p;
1388 /* TRUE if we are parsing a type-id in an expression context. In
1389 such a situation, both "type (expr)" and "type (type)" are valid
1391 bool in_type_id_in_expr_p;
1393 /* TRUE if we are currently in a header file where declarations are
1394 implicitly extern "C". */
1395 bool implicit_extern_c;
1397 /* TRUE if strings in expressions should be translated to the execution
1399 bool translate_strings_p;
1401 /* TRUE if we are presently parsing the body of a function, but not
1403 bool in_function_body;
1405 /* If non-NULL, then we are parsing a construct where new type
1406 definitions are not permitted. The string stored here will be
1407 issued as an error message if a type is defined. */
1408 const char *type_definition_forbidden_message;
1410 /* A list of lists. The outer list is a stack, used for member
1411 functions of local classes. At each level there are two sub-list,
1412 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1413 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1414 TREE_VALUE's. The functions are chained in reverse declaration
1417 The TREE_PURPOSE sublist contains those functions with default
1418 arguments that need post processing, and the TREE_VALUE sublist
1419 contains those functions with definitions that need post
1422 These lists can only be processed once the outermost class being
1423 defined is complete. */
1424 tree unparsed_functions_queues;
1426 /* The number of classes whose definitions are currently in
1428 unsigned num_classes_being_defined;
1430 /* The number of template parameter lists that apply directly to the
1431 current declaration. */
1432 unsigned num_template_parameter_lists;
1435 /* The type of a function that parses some kind of expression. */
1436 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1440 /* Constructors and destructors. */
1442 static cp_parser *cp_parser_new
1445 /* Routines to parse various constructs.
1447 Those that return `tree' will return the error_mark_node (rather
1448 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1449 Sometimes, they will return an ordinary node if error-recovery was
1450 attempted, even though a parse error occurred. So, to check
1451 whether or not a parse error occurred, you should always use
1452 cp_parser_error_occurred. If the construct is optional (indicated
1453 either by an `_opt' in the name of the function that does the
1454 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1455 the construct is not present. */
1457 /* Lexical conventions [gram.lex] */
1459 static tree cp_parser_identifier
1461 static tree cp_parser_string_literal
1462 (cp_parser *, bool, bool);
1464 /* Basic concepts [gram.basic] */
1466 static bool cp_parser_translation_unit
1469 /* Expressions [gram.expr] */
1471 static tree cp_parser_primary_expression
1472 (cp_parser *, bool, bool, bool, cp_id_kind *);
1473 static tree cp_parser_id_expression
1474 (cp_parser *, bool, bool, bool *, bool);
1475 static tree cp_parser_unqualified_id
1476 (cp_parser *, bool, bool, bool);
1477 static tree cp_parser_nested_name_specifier_opt
1478 (cp_parser *, bool, bool, bool, bool);
1479 static tree cp_parser_nested_name_specifier
1480 (cp_parser *, bool, bool, bool, bool);
1481 static tree cp_parser_class_or_namespace_name
1482 (cp_parser *, bool, bool, bool, bool, bool);
1483 static tree cp_parser_postfix_expression
1484 (cp_parser *, bool, bool);
1485 static tree cp_parser_postfix_open_square_expression
1486 (cp_parser *, tree, bool);
1487 static tree cp_parser_postfix_dot_deref_expression
1488 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1489 static tree cp_parser_parenthesized_expression_list
1490 (cp_parser *, bool, bool, bool *);
1491 static void cp_parser_pseudo_destructor_name
1492 (cp_parser *, tree *, tree *);
1493 static tree cp_parser_unary_expression
1494 (cp_parser *, bool, bool);
1495 static enum tree_code cp_parser_unary_operator
1497 static tree cp_parser_new_expression
1499 static tree cp_parser_new_placement
1501 static tree cp_parser_new_type_id
1502 (cp_parser *, tree *);
1503 static cp_declarator *cp_parser_new_declarator_opt
1505 static cp_declarator *cp_parser_direct_new_declarator
1507 static tree cp_parser_new_initializer
1509 static tree cp_parser_delete_expression
1511 static tree cp_parser_cast_expression
1512 (cp_parser *, bool, bool);
1513 static tree cp_parser_binary_expression
1514 (cp_parser *, bool);
1515 static tree cp_parser_question_colon_clause
1516 (cp_parser *, tree);
1517 static tree cp_parser_assignment_expression
1518 (cp_parser *, bool);
1519 static enum tree_code cp_parser_assignment_operator_opt
1521 static tree cp_parser_expression
1522 (cp_parser *, bool);
1523 static tree cp_parser_constant_expression
1524 (cp_parser *, bool, bool *);
1525 static tree cp_parser_builtin_offsetof
1528 /* Statements [gram.stmt.stmt] */
1530 static void cp_parser_statement
1531 (cp_parser *, tree);
1532 static void cp_parser_label_for_labeled_statement
1534 static tree cp_parser_expression_statement
1535 (cp_parser *, tree);
1536 static tree cp_parser_compound_statement
1537 (cp_parser *, tree, bool);
1538 static void cp_parser_statement_seq_opt
1539 (cp_parser *, tree);
1540 static tree cp_parser_selection_statement
1542 static tree cp_parser_condition
1544 static tree cp_parser_iteration_statement
1546 static void cp_parser_for_init_statement
1548 static tree cp_parser_jump_statement
1550 static void cp_parser_declaration_statement
1553 static tree cp_parser_implicitly_scoped_statement
1555 static void cp_parser_already_scoped_statement
1558 /* Declarations [gram.dcl.dcl] */
1560 static void cp_parser_declaration_seq_opt
1562 static void cp_parser_declaration
1564 static void cp_parser_block_declaration
1565 (cp_parser *, bool);
1566 static void cp_parser_simple_declaration
1567 (cp_parser *, bool);
1568 static void cp_parser_decl_specifier_seq
1569 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1570 static tree cp_parser_storage_class_specifier_opt
1572 static tree cp_parser_function_specifier_opt
1573 (cp_parser *, cp_decl_specifier_seq *);
1574 static tree cp_parser_type_specifier
1575 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1577 static tree cp_parser_simple_type_specifier
1578 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1579 static tree cp_parser_type_name
1581 static tree cp_parser_elaborated_type_specifier
1582 (cp_parser *, bool, bool);
1583 static tree cp_parser_enum_specifier
1585 static void cp_parser_enumerator_list
1586 (cp_parser *, tree);
1587 static void cp_parser_enumerator_definition
1588 (cp_parser *, tree);
1589 static tree cp_parser_namespace_name
1591 static void cp_parser_namespace_definition
1593 static void cp_parser_namespace_body
1595 static tree cp_parser_qualified_namespace_specifier
1597 static void cp_parser_namespace_alias_definition
1599 static bool cp_parser_using_declaration
1600 (cp_parser *, bool);
1601 static void cp_parser_using_directive
1603 static void cp_parser_asm_definition
1605 static void cp_parser_linkage_specification
1608 /* Declarators [gram.dcl.decl] */
1610 static tree cp_parser_init_declarator
1611 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1612 static cp_declarator *cp_parser_declarator
1613 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1614 static cp_declarator *cp_parser_direct_declarator
1615 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1616 static enum tree_code cp_parser_ptr_operator
1617 (cp_parser *, tree *, cp_cv_quals *);
1618 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1620 static tree cp_parser_declarator_id
1622 static tree cp_parser_type_id
1624 static void cp_parser_type_specifier_seq
1625 (cp_parser *, bool, cp_decl_specifier_seq *);
1626 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1628 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1629 (cp_parser *, bool *);
1630 static cp_parameter_declarator *cp_parser_parameter_declaration
1631 (cp_parser *, bool, bool *);
1632 static void cp_parser_function_body
1634 static tree cp_parser_initializer
1635 (cp_parser *, bool *, bool *);
1636 static tree cp_parser_initializer_clause
1637 (cp_parser *, bool *);
1638 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1639 (cp_parser *, bool *);
1641 static bool cp_parser_ctor_initializer_opt_and_function_body
1644 /* Classes [gram.class] */
1646 static tree cp_parser_class_name
1647 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1648 static tree cp_parser_class_specifier
1650 static tree cp_parser_class_head
1651 (cp_parser *, bool *, tree *, tree *);
1652 static enum tag_types cp_parser_class_key
1654 static void cp_parser_member_specification_opt
1656 static void cp_parser_member_declaration
1658 static tree cp_parser_pure_specifier
1660 static tree cp_parser_constant_initializer
1663 /* Derived classes [gram.class.derived] */
1665 static tree cp_parser_base_clause
1667 static tree cp_parser_base_specifier
1670 /* Special member functions [gram.special] */
1672 static tree cp_parser_conversion_function_id
1674 static tree cp_parser_conversion_type_id
1676 static cp_declarator *cp_parser_conversion_declarator_opt
1678 static bool cp_parser_ctor_initializer_opt
1680 static void cp_parser_mem_initializer_list
1682 static tree cp_parser_mem_initializer
1684 static tree cp_parser_mem_initializer_id
1687 /* Overloading [gram.over] */
1689 static tree cp_parser_operator_function_id
1691 static tree cp_parser_operator
1694 /* Templates [gram.temp] */
1696 static void cp_parser_template_declaration
1697 (cp_parser *, bool);
1698 static tree cp_parser_template_parameter_list
1700 static tree cp_parser_template_parameter
1701 (cp_parser *, bool *);
1702 static tree cp_parser_type_parameter
1704 static tree cp_parser_template_id
1705 (cp_parser *, bool, bool, bool);
1706 static tree cp_parser_template_name
1707 (cp_parser *, bool, bool, bool, bool *);
1708 static tree cp_parser_template_argument_list
1710 static tree cp_parser_template_argument
1712 static void cp_parser_explicit_instantiation
1714 static void cp_parser_explicit_specialization
1717 /* Exception handling [gram.exception] */
1719 static tree cp_parser_try_block
1721 static bool cp_parser_function_try_block
1723 static void cp_parser_handler_seq
1725 static void cp_parser_handler
1727 static tree cp_parser_exception_declaration
1729 static tree cp_parser_throw_expression
1731 static tree cp_parser_exception_specification_opt
1733 static tree cp_parser_type_id_list
1736 /* GNU Extensions */
1738 static tree cp_parser_asm_specification_opt
1740 static tree cp_parser_asm_operand_list
1742 static tree cp_parser_asm_clobber_list
1744 static tree cp_parser_attributes_opt
1746 static tree cp_parser_attribute_list
1748 static bool cp_parser_extension_opt
1749 (cp_parser *, int *);
1750 static void cp_parser_label_declaration
1753 /* Objective-C++ Productions */
1755 static tree cp_parser_objc_message_receiver
1757 static tree cp_parser_objc_message_args
1759 static tree cp_parser_objc_message_expression
1761 static tree cp_parser_objc_encode_expression
1763 static tree cp_parser_objc_defs_expression
1765 static tree cp_parser_objc_protocol_expression
1767 static tree cp_parser_objc_selector_expression
1769 static tree cp_parser_objc_expression
1771 static bool cp_parser_objc_selector_p
1773 static tree cp_parser_objc_selector
1775 static tree cp_parser_objc_protocol_refs_opt
1777 static void cp_parser_objc_declaration
1779 static tree cp_parser_objc_statement
1782 /* Utility Routines */
1784 static tree cp_parser_lookup_name
1785 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1786 static tree cp_parser_lookup_name_simple
1787 (cp_parser *, tree);
1788 static tree cp_parser_maybe_treat_template_as_class
1790 static bool cp_parser_check_declarator_template_parameters
1791 (cp_parser *, cp_declarator *);
1792 static bool cp_parser_check_template_parameters
1793 (cp_parser *, unsigned);
1794 static tree cp_parser_simple_cast_expression
1796 static tree cp_parser_global_scope_opt
1797 (cp_parser *, bool);
1798 static bool cp_parser_constructor_declarator_p
1799 (cp_parser *, bool);
1800 static tree cp_parser_function_definition_from_specifiers_and_declarator
1801 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1802 static tree cp_parser_function_definition_after_declarator
1803 (cp_parser *, bool);
1804 static void cp_parser_template_declaration_after_export
1805 (cp_parser *, bool);
1806 static void cp_parser_perform_template_parameter_access_checks
1808 static tree cp_parser_single_declaration
1809 (cp_parser *, tree, bool, bool *);
1810 static tree cp_parser_functional_cast
1811 (cp_parser *, tree);
1812 static tree cp_parser_save_member_function_body
1813 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1814 static tree cp_parser_enclosed_template_argument_list
1816 static void cp_parser_save_default_args
1817 (cp_parser *, tree);
1818 static void cp_parser_late_parsing_for_member
1819 (cp_parser *, tree);
1820 static void cp_parser_late_parsing_default_args
1821 (cp_parser *, tree);
1822 static tree cp_parser_sizeof_operand
1823 (cp_parser *, enum rid);
1824 static bool cp_parser_declares_only_class_p
1826 static void cp_parser_set_storage_class
1827 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1828 static void cp_parser_set_decl_spec_type
1829 (cp_decl_specifier_seq *, tree, bool);
1830 static bool cp_parser_friend_p
1831 (const cp_decl_specifier_seq *);
1832 static cp_token *cp_parser_require
1833 (cp_parser *, enum cpp_ttype, const char *);
1834 static cp_token *cp_parser_require_keyword
1835 (cp_parser *, enum rid, const char *);
1836 static bool cp_parser_token_starts_function_definition_p
1838 static bool cp_parser_next_token_starts_class_definition_p
1840 static bool cp_parser_next_token_ends_template_argument_p
1842 static bool cp_parser_nth_token_starts_template_argument_list_p
1843 (cp_parser *, size_t);
1844 static enum tag_types cp_parser_token_is_class_key
1846 static void cp_parser_check_class_key
1847 (enum tag_types, tree type);
1848 static void cp_parser_check_access_in_redeclaration
1850 static bool cp_parser_optional_template_keyword
1852 static void cp_parser_pre_parsed_nested_name_specifier
1854 static void cp_parser_cache_group
1855 (cp_parser *, enum cpp_ttype, unsigned);
1856 static void cp_parser_parse_tentatively
1858 static void cp_parser_commit_to_tentative_parse
1860 static void cp_parser_abort_tentative_parse
1862 static bool cp_parser_parse_definitely
1864 static inline bool cp_parser_parsing_tentatively
1866 static bool cp_parser_uncommitted_to_tentative_parse_p
1868 static void cp_parser_error
1869 (cp_parser *, const char *);
1870 static void cp_parser_name_lookup_error
1871 (cp_parser *, tree, tree, const char *);
1872 static bool cp_parser_simulate_error
1874 static bool cp_parser_check_type_definition
1876 static void cp_parser_check_for_definition_in_return_type
1877 (cp_declarator *, tree);
1878 static void cp_parser_check_for_invalid_template_id
1879 (cp_parser *, tree);
1880 static bool cp_parser_non_integral_constant_expression
1881 (cp_parser *, const char *);
1882 static void cp_parser_diagnose_invalid_type_name
1883 (cp_parser *, tree, tree);
1884 static bool cp_parser_parse_and_diagnose_invalid_type_name
1886 static int cp_parser_skip_to_closing_parenthesis
1887 (cp_parser *, bool, bool, bool);
1888 static void cp_parser_skip_to_end_of_statement
1890 static void cp_parser_consume_semicolon_at_end_of_statement
1892 static void cp_parser_skip_to_end_of_block_or_statement
1894 static void cp_parser_skip_to_closing_brace
1896 static void cp_parser_skip_until_found
1897 (cp_parser *, enum cpp_ttype, const char *);
1898 static bool cp_parser_error_occurred
1900 static bool cp_parser_allow_gnu_extensions_p
1902 static bool cp_parser_is_string_literal
1904 static bool cp_parser_is_keyword
1905 (cp_token *, enum rid);
1906 static tree cp_parser_make_typename_type
1907 (cp_parser *, tree, tree);
1909 /* Returns nonzero if we are parsing tentatively. */
1912 cp_parser_parsing_tentatively (cp_parser* parser)
1914 return parser->context->next != NULL;
1917 /* Returns nonzero if TOKEN is a string literal. */
1920 cp_parser_is_string_literal (cp_token* token)
1922 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1925 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1928 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1930 return token->keyword == keyword;
1933 /* A minimum or maximum operator has been seen. As these are
1934 deprecated, issue a warning. */
1937 cp_parser_warn_min_max (void)
1939 if (warn_deprecated && !in_system_header)
1940 warning (0, "minimum/maximum operators are deprecated");
1943 /* If not parsing tentatively, issue a diagnostic of the form
1944 FILE:LINE: MESSAGE before TOKEN
1945 where TOKEN is the next token in the input stream. MESSAGE
1946 (specified by the caller) is usually of the form "expected
1950 cp_parser_error (cp_parser* parser, const char* message)
1952 if (!cp_parser_simulate_error (parser))
1954 cp_token *token = cp_lexer_peek_token (parser->lexer);
1955 /* This diagnostic makes more sense if it is tagged to the line
1956 of the token we just peeked at. */
1957 cp_lexer_set_source_position_from_token (token);
1958 if (token->type == CPP_PRAGMA)
1960 error ("%<#pragma%> is not allowed here");
1961 cp_lexer_purge_token (parser->lexer);
1964 c_parse_error (message,
1965 /* Because c_parser_error does not understand
1966 CPP_KEYWORD, keywords are treated like
1968 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1973 /* Issue an error about name-lookup failing. NAME is the
1974 IDENTIFIER_NODE DECL is the result of
1975 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1976 the thing that we hoped to find. */
1979 cp_parser_name_lookup_error (cp_parser* parser,
1982 const char* desired)
1984 /* If name lookup completely failed, tell the user that NAME was not
1986 if (decl == error_mark_node)
1988 if (parser->scope && parser->scope != global_namespace)
1989 error ("%<%D::%D%> has not been declared",
1990 parser->scope, name);
1991 else if (parser->scope == global_namespace)
1992 error ("%<::%D%> has not been declared", name);
1993 else if (parser->object_scope
1994 && !CLASS_TYPE_P (parser->object_scope))
1995 error ("request for member %qD in non-class type %qT",
1996 name, parser->object_scope);
1997 else if (parser->object_scope)
1998 error ("%<%T::%D%> has not been declared",
1999 parser->object_scope, name);
2001 error ("%qD has not been declared", name);
2003 else if (parser->scope && parser->scope != global_namespace)
2004 error ("%<%D::%D%> %s", parser->scope, name, desired);
2005 else if (parser->scope == global_namespace)
2006 error ("%<::%D%> %s", name, desired);
2008 error ("%qD %s", name, desired);
2011 /* If we are parsing tentatively, remember that an error has occurred
2012 during this tentative parse. Returns true if the error was
2013 simulated; false if a message should be issued by the caller. */
2016 cp_parser_simulate_error (cp_parser* parser)
2018 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2020 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2026 /* Check for repeated decl-specifiers. */
2029 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2033 for (ds = ds_first; ds != ds_last; ++ds)
2035 unsigned count = decl_specs->specs[(int)ds];
2038 /* The "long" specifier is a special case because of "long long". */
2042 error ("%<long long long%> is too long for GCC");
2043 else if (pedantic && !in_system_header && warn_long_long)
2044 pedwarn ("ISO C++ does not support %<long long%>");
2048 static const char *const decl_spec_names[] = {
2064 error ("duplicate %qs", decl_spec_names[(int)ds]);
2069 /* This function is called when a type is defined. If type
2070 definitions are forbidden at this point, an error message is
2074 cp_parser_check_type_definition (cp_parser* parser)
2076 /* If types are forbidden here, issue a message. */
2077 if (parser->type_definition_forbidden_message)
2079 /* Use `%s' to print the string in case there are any escape
2080 characters in the message. */
2081 error ("%s", parser->type_definition_forbidden_message);
2087 /* This function is called when the DECLARATOR is processed. The TYPE
2088 was a type defined in the decl-specifiers. If it is invalid to
2089 define a type in the decl-specifiers for DECLARATOR, an error is
2093 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2096 /* [dcl.fct] forbids type definitions in return types.
2097 Unfortunately, it's not easy to know whether or not we are
2098 processing a return type until after the fact. */
2100 && (declarator->kind == cdk_pointer
2101 || declarator->kind == cdk_reference
2102 || declarator->kind == cdk_ptrmem))
2103 declarator = declarator->declarator;
2105 && declarator->kind == cdk_function)
2107 error ("new types may not be defined in a return type");
2108 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2113 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2114 "<" in any valid C++ program. If the next token is indeed "<",
2115 issue a message warning the user about what appears to be an
2116 invalid attempt to form a template-id. */
2119 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2122 cp_token_position start = 0;
2124 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2127 error ("%qT is not a template", type);
2128 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2129 error ("%qE is not a template", type);
2131 error ("invalid template-id");
2132 /* Remember the location of the invalid "<". */
2133 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2134 start = cp_lexer_token_position (parser->lexer, true);
2135 /* Consume the "<". */
2136 cp_lexer_consume_token (parser->lexer);
2137 /* Parse the template arguments. */
2138 cp_parser_enclosed_template_argument_list (parser);
2139 /* Permanently remove the invalid template arguments so that
2140 this error message is not issued again. */
2142 cp_lexer_purge_tokens_after (parser->lexer, start);
2146 /* If parsing an integral constant-expression, issue an error message
2147 about the fact that THING appeared and return true. Otherwise,
2148 return false. In either case, set
2149 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2152 cp_parser_non_integral_constant_expression (cp_parser *parser,
2155 parser->non_integral_constant_expression_p = true;
2156 if (parser->integral_constant_expression_p)
2158 if (!parser->allow_non_integral_constant_expression_p)
2160 error ("%s cannot appear in a constant-expression", thing);
2167 /* Emit a diagnostic for an invalid type name. SCOPE is the
2168 qualifying scope (or NULL, if none) for ID. This function commits
2169 to the current active tentative parse, if any. (Otherwise, the
2170 problematic construct might be encountered again later, resulting
2171 in duplicate error messages.) */
2174 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2176 tree decl, old_scope;
2177 /* Try to lookup the identifier. */
2178 old_scope = parser->scope;
2179 parser->scope = scope;
2180 decl = cp_parser_lookup_name_simple (parser, id);
2181 parser->scope = old_scope;
2182 /* If the lookup found a template-name, it means that the user forgot
2183 to specify an argument list. Emit a useful error message. */
2184 if (TREE_CODE (decl) == TEMPLATE_DECL)
2185 error ("invalid use of template-name %qE without an argument list", decl);
2186 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2187 error ("invalid use of destructor %qD as a type", id);
2188 else if (TREE_CODE (decl) == TYPE_DECL)
2189 /* Something like 'unsigned A a;' */
2190 error ("invalid combination of multiple type-specifiers");
2191 else if (!parser->scope)
2193 /* Issue an error message. */
2194 error ("%qE does not name a type", id);
2195 /* If we're in a template class, it's possible that the user was
2196 referring to a type from a base class. For example:
2198 template <typename T> struct A { typedef T X; };
2199 template <typename T> struct B : public A<T> { X x; };
2201 The user should have said "typename A<T>::X". */
2202 if (processing_template_decl && current_class_type
2203 && TYPE_BINFO (current_class_type))
2207 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2211 tree base_type = BINFO_TYPE (b);
2212 if (CLASS_TYPE_P (base_type)
2213 && dependent_type_p (base_type))
2216 /* Go from a particular instantiation of the
2217 template (which will have an empty TYPE_FIELDs),
2218 to the main version. */
2219 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2220 for (field = TYPE_FIELDS (base_type);
2222 field = TREE_CHAIN (field))
2223 if (TREE_CODE (field) == TYPE_DECL
2224 && DECL_NAME (field) == id)
2226 inform ("(perhaps %<typename %T::%E%> was intended)",
2227 BINFO_TYPE (b), id);
2236 /* Here we diagnose qualified-ids where the scope is actually correct,
2237 but the identifier does not resolve to a valid type name. */
2238 else if (parser->scope != error_mark_node)
2240 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2241 error ("%qE in namespace %qE does not name a type",
2243 else if (TYPE_P (parser->scope))
2244 error ("%qE in class %qT does not name a type", id, parser->scope);
2248 cp_parser_commit_to_tentative_parse (parser);
2251 /* Check for a common situation where a type-name should be present,
2252 but is not, and issue a sensible error message. Returns true if an
2253 invalid type-name was detected.
2255 The situation handled by this function are variable declarations of the
2256 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2257 Usually, `ID' should name a type, but if we got here it means that it
2258 does not. We try to emit the best possible error message depending on
2259 how exactly the id-expression looks like.
2263 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2267 cp_parser_parse_tentatively (parser);
2268 id = cp_parser_id_expression (parser,
2269 /*template_keyword_p=*/false,
2270 /*check_dependency_p=*/true,
2271 /*template_p=*/NULL,
2272 /*declarator_p=*/true);
2273 /* After the id-expression, there should be a plain identifier,
2274 otherwise this is not a simple variable declaration. Also, if
2275 the scope is dependent, we cannot do much. */
2276 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2277 || (parser->scope && TYPE_P (parser->scope)
2278 && dependent_type_p (parser->scope)))
2280 cp_parser_abort_tentative_parse (parser);
2283 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2286 /* Emit a diagnostic for the invalid type. */
2287 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2288 /* Skip to the end of the declaration; there's no point in
2289 trying to process it. */
2290 cp_parser_skip_to_end_of_block_or_statement (parser);
2294 /* Consume tokens up to, and including, the next non-nested closing `)'.
2295 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2296 are doing error recovery. Returns -1 if OR_COMMA is true and we
2297 found an unnested comma. */
2300 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2305 unsigned paren_depth = 0;
2306 unsigned brace_depth = 0;
2309 if (recovering && !or_comma
2310 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2317 /* If we've run out of tokens, then there is no closing `)'. */
2318 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2324 token = cp_lexer_peek_token (parser->lexer);
2326 /* This matches the processing in skip_to_end_of_statement. */
2327 if (token->type == CPP_SEMICOLON && !brace_depth)
2332 if (token->type == CPP_OPEN_BRACE)
2334 if (token->type == CPP_CLOSE_BRACE)
2342 if (recovering && or_comma && token->type == CPP_COMMA
2343 && !brace_depth && !paren_depth)
2351 /* If it is an `(', we have entered another level of nesting. */
2352 if (token->type == CPP_OPEN_PAREN)
2354 /* If it is a `)', then we might be done. */
2355 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2358 cp_lexer_consume_token (parser->lexer);
2366 /* Consume the token. */
2367 cp_lexer_consume_token (parser->lexer);
2373 /* Consume tokens until we reach the end of the current statement.
2374 Normally, that will be just before consuming a `;'. However, if a
2375 non-nested `}' comes first, then we stop before consuming that. */
2378 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2380 unsigned nesting_depth = 0;
2386 /* Peek at the next token. */
2387 token = cp_lexer_peek_token (parser->lexer);
2388 /* If we've run out of tokens, stop. */
2389 if (token->type == CPP_EOF)
2391 /* If the next token is a `;', we have reached the end of the
2393 if (token->type == CPP_SEMICOLON && !nesting_depth)
2395 /* If the next token is a non-nested `}', then we have reached
2396 the end of the current block. */
2397 if (token->type == CPP_CLOSE_BRACE)
2399 /* If this is a non-nested `}', stop before consuming it.
2400 That way, when confronted with something like:
2404 we stop before consuming the closing `}', even though we
2405 have not yet reached a `;'. */
2406 if (nesting_depth == 0)
2408 /* If it is the closing `}' for a block that we have
2409 scanned, stop -- but only after consuming the token.
2415 we will stop after the body of the erroneously declared
2416 function, but before consuming the following `typedef'
2418 if (--nesting_depth == 0)
2420 cp_lexer_consume_token (parser->lexer);
2424 /* If it the next token is a `{', then we are entering a new
2425 block. Consume the entire block. */
2426 else if (token->type == CPP_OPEN_BRACE)
2428 /* Consume the token. */
2429 cp_lexer_consume_token (parser->lexer);
2433 /* This function is called at the end of a statement or declaration.
2434 If the next token is a semicolon, it is consumed; otherwise, error
2435 recovery is attempted. */
2438 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2440 /* Look for the trailing `;'. */
2441 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2443 /* If there is additional (erroneous) input, skip to the end of
2445 cp_parser_skip_to_end_of_statement (parser);
2446 /* If the next token is now a `;', consume it. */
2447 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2448 cp_lexer_consume_token (parser->lexer);
2452 /* Skip tokens until we have consumed an entire block, or until we
2453 have consumed a non-nested `;'. */
2456 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2458 int nesting_depth = 0;
2460 while (nesting_depth >= 0)
2462 cp_token *token = cp_lexer_peek_token (parser->lexer);
2464 if (token->type == CPP_EOF)
2467 switch (token->type)
2470 /* If we've run out of tokens, stop. */
2475 /* Stop if this is an unnested ';'. */
2480 case CPP_CLOSE_BRACE:
2481 /* Stop if this is an unnested '}', or closes the outermost
2488 case CPP_OPEN_BRACE:
2497 /* Consume the token. */
2498 cp_lexer_consume_token (parser->lexer);
2503 /* Skip tokens until a non-nested closing curly brace is the next
2507 cp_parser_skip_to_closing_brace (cp_parser *parser)
2509 unsigned nesting_depth = 0;
2515 /* Peek at the next token. */
2516 token = cp_lexer_peek_token (parser->lexer);
2517 /* If we've run out of tokens, stop. */
2518 if (token->type == CPP_EOF)
2520 /* If the next token is a non-nested `}', then we have reached
2521 the end of the current block. */
2522 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2524 /* If it the next token is a `{', then we are entering a new
2525 block. Consume the entire block. */
2526 else if (token->type == CPP_OPEN_BRACE)
2528 /* Consume the token. */
2529 cp_lexer_consume_token (parser->lexer);
2533 /* This is a simple wrapper around make_typename_type. When the id is
2534 an unresolved identifier node, we can provide a superior diagnostic
2535 using cp_parser_diagnose_invalid_type_name. */
2538 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2541 if (TREE_CODE (id) == IDENTIFIER_NODE)
2543 result = make_typename_type (scope, id, typename_type,
2545 if (result == error_mark_node)
2546 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2549 return make_typename_type (scope, id, typename_type, tf_error);
2553 /* Create a new C++ parser. */
2556 cp_parser_new (void)
2562 /* cp_lexer_new_main is called before calling ggc_alloc because
2563 cp_lexer_new_main might load a PCH file. */
2564 lexer = cp_lexer_new_main ();
2566 /* Initialize the binops_by_token so that we can get the tree
2567 directly from the token. */
2568 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2569 binops_by_token[binops[i].token_type] = binops[i];
2571 parser = GGC_CNEW (cp_parser);
2572 parser->lexer = lexer;
2573 parser->context = cp_parser_context_new (NULL);
2575 /* For now, we always accept GNU extensions. */
2576 parser->allow_gnu_extensions_p = 1;
2578 /* The `>' token is a greater-than operator, not the end of a
2580 parser->greater_than_is_operator_p = true;
2582 parser->default_arg_ok_p = true;
2584 /* We are not parsing a constant-expression. */
2585 parser->integral_constant_expression_p = false;
2586 parser->allow_non_integral_constant_expression_p = false;
2587 parser->non_integral_constant_expression_p = false;
2589 /* Local variable names are not forbidden. */
2590 parser->local_variables_forbidden_p = false;
2592 /* We are not processing an `extern "C"' declaration. */
2593 parser->in_unbraced_linkage_specification_p = false;
2595 /* We are not processing a declarator. */
2596 parser->in_declarator_p = false;
2598 /* We are not processing a template-argument-list. */
2599 parser->in_template_argument_list_p = false;
2601 /* We are not in an iteration statement. */
2602 parser->in_iteration_statement_p = false;
2604 /* We are not in a switch statement. */
2605 parser->in_switch_statement_p = false;
2607 /* We are not parsing a type-id inside an expression. */
2608 parser->in_type_id_in_expr_p = false;
2610 /* Declarations aren't implicitly extern "C". */
2611 parser->implicit_extern_c = false;
2613 /* String literals should be translated to the execution character set. */
2614 parser->translate_strings_p = true;
2616 /* We are not parsing a function body. */
2617 parser->in_function_body = false;
2619 /* The unparsed function queue is empty. */
2620 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2622 /* There are no classes being defined. */
2623 parser->num_classes_being_defined = 0;
2625 /* No template parameters apply. */
2626 parser->num_template_parameter_lists = 0;
2631 /* Create a cp_lexer structure which will emit the tokens in CACHE
2632 and push it onto the parser's lexer stack. This is used for delayed
2633 parsing of in-class method bodies and default arguments, and should
2634 not be confused with tentative parsing. */
2636 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2638 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2639 lexer->next = parser->lexer;
2640 parser->lexer = lexer;
2642 /* Move the current source position to that of the first token in the
2644 cp_lexer_set_source_position_from_token (lexer->next_token);
2647 /* Pop the top lexer off the parser stack. This is never used for the
2648 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2650 cp_parser_pop_lexer (cp_parser *parser)
2652 cp_lexer *lexer = parser->lexer;
2653 parser->lexer = lexer->next;
2654 cp_lexer_destroy (lexer);
2656 /* Put the current source position back where it was before this
2657 lexer was pushed. */
2658 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2661 /* Lexical conventions [gram.lex] */
2663 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2667 cp_parser_identifier (cp_parser* parser)
2671 /* Look for the identifier. */
2672 token = cp_parser_require (parser, CPP_NAME, "identifier");
2673 /* Return the value. */
2674 return token ? token->value : error_mark_node;
2677 /* Parse a sequence of adjacent string constants. Returns a
2678 TREE_STRING representing the combined, nul-terminated string
2679 constant. If TRANSLATE is true, translate the string to the
2680 execution character set. If WIDE_OK is true, a wide string is
2683 C++98 [lex.string] says that if a narrow string literal token is
2684 adjacent to a wide string literal token, the behavior is undefined.
2685 However, C99 6.4.5p4 says that this results in a wide string literal.
2686 We follow C99 here, for consistency with the C front end.
2688 This code is largely lifted from lex_string() in c-lex.c.
2690 FUTURE: ObjC++ will need to handle @-strings here. */
2692 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2697 struct obstack str_ob;
2698 cpp_string str, istr, *strs;
2701 tok = cp_lexer_peek_token (parser->lexer);
2702 if (!cp_parser_is_string_literal (tok))
2704 cp_parser_error (parser, "expected string-literal");
2705 return error_mark_node;
2708 /* Try to avoid the overhead of creating and destroying an obstack
2709 for the common case of just one string. */
2710 if (!cp_parser_is_string_literal
2711 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2713 cp_lexer_consume_token (parser->lexer);
2715 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2716 str.len = TREE_STRING_LENGTH (tok->value);
2718 if (tok->type == CPP_WSTRING)
2725 gcc_obstack_init (&str_ob);
2730 cp_lexer_consume_token (parser->lexer);
2732 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2733 str.len = TREE_STRING_LENGTH (tok->value);
2734 if (tok->type == CPP_WSTRING)
2737 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2739 tok = cp_lexer_peek_token (parser->lexer);
2741 while (cp_parser_is_string_literal (tok));
2743 strs = (cpp_string *) obstack_finish (&str_ob);
2746 if (wide && !wide_ok)
2748 cp_parser_error (parser, "a wide string is invalid in this context");
2752 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2753 (parse_in, strs, count, &istr, wide))
2755 value = build_string (istr.len, (char *)istr.text);
2756 free ((void *)istr.text);
2758 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2759 value = fix_string_type (value);
2762 /* cpp_interpret_string has issued an error. */
2763 value = error_mark_node;
2766 obstack_free (&str_ob, 0);
2772 /* Basic concepts [gram.basic] */
2774 /* Parse a translation-unit.
2777 declaration-seq [opt]
2779 Returns TRUE if all went well. */
2782 cp_parser_translation_unit (cp_parser* parser)
2784 /* The address of the first non-permanent object on the declarator
2786 static void *declarator_obstack_base;
2790 /* Create the declarator obstack, if necessary. */
2791 if (!cp_error_declarator)
2793 gcc_obstack_init (&declarator_obstack);
2794 /* Create the error declarator. */
2795 cp_error_declarator = make_declarator (cdk_error);
2796 /* Create the empty parameter list. */
2797 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2798 /* Remember where the base of the declarator obstack lies. */
2799 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2802 cp_parser_declaration_seq_opt (parser);
2804 /* If there are no tokens left then all went well. */
2805 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2807 /* Get rid of the token array; we don't need it any more. */
2808 cp_lexer_destroy (parser->lexer);
2809 parser->lexer = NULL;
2811 /* This file might have been a context that's implicitly extern
2812 "C". If so, pop the lang context. (Only relevant for PCH.) */
2813 if (parser->implicit_extern_c)
2815 pop_lang_context ();
2816 parser->implicit_extern_c = false;
2820 finish_translation_unit ();
2826 cp_parser_error (parser, "expected declaration");
2830 /* Make sure the declarator obstack was fully cleaned up. */
2831 gcc_assert (obstack_next_free (&declarator_obstack)
2832 == declarator_obstack_base);
2834 /* All went well. */
2838 /* Expressions [gram.expr] */
2840 /* Parse a primary-expression.
2851 ( compound-statement )
2852 __builtin_va_arg ( assignment-expression , type-id )
2854 Objective-C++ Extension:
2862 ADDRESS_P is true iff this expression was immediately preceded by
2863 "&" and therefore might denote a pointer-to-member. CAST_P is true
2864 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2865 true iff this expression is a tempalte argument.
2867 Returns a representation of the expression. Upon return, *IDK
2868 indicates what kind of id-expression (if any) was present. */
2871 cp_parser_primary_expression (cp_parser *parser,
2874 bool template_arg_p,
2879 /* Assume the primary expression is not an id-expression. */
2880 *idk = CP_ID_KIND_NONE;
2882 /* Peek at the next token. */
2883 token = cp_lexer_peek_token (parser->lexer);
2884 switch (token->type)
2895 token = cp_lexer_consume_token (parser->lexer);
2896 /* Floating-point literals are only allowed in an integral
2897 constant expression if they are cast to an integral or
2898 enumeration type. */
2899 if (TREE_CODE (token->value) == REAL_CST
2900 && parser->integral_constant_expression_p
2903 /* CAST_P will be set even in invalid code like "int(2.7 +
2904 ...)". Therefore, we have to check that the next token
2905 is sure to end the cast. */
2908 cp_token *next_token;
2910 next_token = cp_lexer_peek_token (parser->lexer);
2911 if (/* The comma at the end of an
2912 enumerator-definition. */
2913 next_token->type != CPP_COMMA
2914 /* The curly brace at the end of an enum-specifier. */
2915 && next_token->type != CPP_CLOSE_BRACE
2916 /* The end of a statement. */
2917 && next_token->type != CPP_SEMICOLON
2918 /* The end of the cast-expression. */
2919 && next_token->type != CPP_CLOSE_PAREN
2920 /* The end of an array bound. */
2921 && next_token->type != CPP_CLOSE_SQUARE
2922 /* The closing ">" in a template-argument-list. */
2923 && (next_token->type != CPP_GREATER
2924 || parser->greater_than_is_operator_p))
2928 /* If we are within a cast, then the constraint that the
2929 cast is to an integral or enumeration type will be
2930 checked at that point. If we are not within a cast, then
2931 this code is invalid. */
2933 cp_parser_non_integral_constant_expression
2934 (parser, "floating-point literal");
2936 return token->value;
2940 /* ??? Should wide strings be allowed when parser->translate_strings_p
2941 is false (i.e. in attributes)? If not, we can kill the third
2942 argument to cp_parser_string_literal. */
2943 return cp_parser_string_literal (parser,
2944 parser->translate_strings_p,
2947 case CPP_OPEN_PAREN:
2950 bool saved_greater_than_is_operator_p;
2952 /* Consume the `('. */
2953 cp_lexer_consume_token (parser->lexer);
2954 /* Within a parenthesized expression, a `>' token is always
2955 the greater-than operator. */
2956 saved_greater_than_is_operator_p
2957 = parser->greater_than_is_operator_p;
2958 parser->greater_than_is_operator_p = true;
2959 /* If we see `( { ' then we are looking at the beginning of
2960 a GNU statement-expression. */
2961 if (cp_parser_allow_gnu_extensions_p (parser)
2962 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2964 /* Statement-expressions are not allowed by the standard. */
2966 pedwarn ("ISO C++ forbids braced-groups within expressions");
2968 /* And they're not allowed outside of a function-body; you
2969 cannot, for example, write:
2971 int i = ({ int j = 3; j + 1; });
2973 at class or namespace scope. */
2974 if (!parser->in_function_body)
2975 error ("statement-expressions are allowed only inside functions");
2976 /* Start the statement-expression. */
2977 expr = begin_stmt_expr ();
2978 /* Parse the compound-statement. */
2979 cp_parser_compound_statement (parser, expr, false);
2981 expr = finish_stmt_expr (expr, false);
2985 /* Parse the parenthesized expression. */
2986 expr = cp_parser_expression (parser, cast_p);
2987 /* Let the front end know that this expression was
2988 enclosed in parentheses. This matters in case, for
2989 example, the expression is of the form `A::B', since
2990 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2992 finish_parenthesized_expr (expr);
2994 /* The `>' token might be the end of a template-id or
2995 template-parameter-list now. */
2996 parser->greater_than_is_operator_p
2997 = saved_greater_than_is_operator_p;
2998 /* Consume the `)'. */
2999 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3000 cp_parser_skip_to_end_of_statement (parser);
3006 switch (token->keyword)
3008 /* These two are the boolean literals. */
3010 cp_lexer_consume_token (parser->lexer);
3011 return boolean_true_node;
3013 cp_lexer_consume_token (parser->lexer);
3014 return boolean_false_node;
3016 /* The `__null' literal. */
3018 cp_lexer_consume_token (parser->lexer);
3021 /* Recognize the `this' keyword. */
3023 cp_lexer_consume_token (parser->lexer);
3024 if (parser->local_variables_forbidden_p)
3026 error ("%<this%> may not be used in this context");
3027 return error_mark_node;
3029 /* Pointers cannot appear in constant-expressions. */
3030 if (cp_parser_non_integral_constant_expression (parser,
3032 return error_mark_node;
3033 return finish_this_expr ();
3035 /* The `operator' keyword can be the beginning of an
3040 case RID_FUNCTION_NAME:
3041 case RID_PRETTY_FUNCTION_NAME:
3042 case RID_C99_FUNCTION_NAME:
3043 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3044 __func__ are the names of variables -- but they are
3045 treated specially. Therefore, they are handled here,
3046 rather than relying on the generic id-expression logic
3047 below. Grammatically, these names are id-expressions.
3049 Consume the token. */
3050 token = cp_lexer_consume_token (parser->lexer);
3051 /* Look up the name. */
3052 return finish_fname (token->value);
3059 /* The `__builtin_va_arg' construct is used to handle
3060 `va_arg'. Consume the `__builtin_va_arg' token. */
3061 cp_lexer_consume_token (parser->lexer);
3062 /* Look for the opening `('. */
3063 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3064 /* Now, parse the assignment-expression. */
3065 expression = cp_parser_assignment_expression (parser,
3067 /* Look for the `,'. */
3068 cp_parser_require (parser, CPP_COMMA, "`,'");
3069 /* Parse the type-id. */
3070 type = cp_parser_type_id (parser);
3071 /* Look for the closing `)'. */
3072 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3073 /* Using `va_arg' in a constant-expression is not
3075 if (cp_parser_non_integral_constant_expression (parser,
3077 return error_mark_node;
3078 return build_x_va_arg (expression, type);
3082 return cp_parser_builtin_offsetof (parser);
3084 /* Objective-C++ expressions. */
3086 case RID_AT_PROTOCOL:
3087 case RID_AT_SELECTOR:
3088 return cp_parser_objc_expression (parser);
3091 cp_parser_error (parser, "expected primary-expression");
3092 return error_mark_node;
3095 /* An id-expression can start with either an identifier, a
3096 `::' as the beginning of a qualified-id, or the "operator"
3100 case CPP_TEMPLATE_ID:
3101 case CPP_NESTED_NAME_SPECIFIER:
3105 const char *error_msg;
3110 /* Parse the id-expression. */
3112 = cp_parser_id_expression (parser,
3113 /*template_keyword_p=*/false,
3114 /*check_dependency_p=*/true,
3116 /*declarator_p=*/false);
3117 if (id_expression == error_mark_node)
3118 return error_mark_node;
3119 token = cp_lexer_peek_token (parser->lexer);
3120 done = (token->type != CPP_OPEN_SQUARE
3121 && token->type != CPP_OPEN_PAREN
3122 && token->type != CPP_DOT
3123 && token->type != CPP_DEREF
3124 && token->type != CPP_PLUS_PLUS
3125 && token->type != CPP_MINUS_MINUS);
3126 /* If we have a template-id, then no further lookup is
3127 required. If the template-id was for a template-class, we
3128 will sometimes have a TYPE_DECL at this point. */
3129 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3130 || TREE_CODE (id_expression) == TYPE_DECL)
3131 decl = id_expression;
3132 /* Look up the name. */
3135 tree ambiguous_decls;
3137 decl = cp_parser_lookup_name (parser, id_expression,
3140 /*is_namespace=*/false,
3141 /*check_dependency=*/true,
3143 /* If the lookup was ambiguous, an error will already have
3145 if (ambiguous_decls)
3146 return error_mark_node;
3148 /* In Objective-C++, an instance variable (ivar) may be preferred
3149 to whatever cp_parser_lookup_name() found. */
3150 decl = objc_lookup_ivar (decl, id_expression);
3152 /* If name lookup gives us a SCOPE_REF, then the
3153 qualifying scope was dependent. */
3154 if (TREE_CODE (decl) == SCOPE_REF)
3156 /* Check to see if DECL is a local variable in a context
3157 where that is forbidden. */
3158 if (parser->local_variables_forbidden_p
3159 && local_variable_p (decl))
3161 /* It might be that we only found DECL because we are
3162 trying to be generous with pre-ISO scoping rules.
3163 For example, consider:
3167 for (int i = 0; i < 10; ++i) {}
3168 extern void f(int j = i);
3171 Here, name look up will originally find the out
3172 of scope `i'. We need to issue a warning message,
3173 but then use the global `i'. */
3174 decl = check_for_out_of_scope_variable (decl);
3175 if (local_variable_p (decl))
3177 error ("local variable %qD may not appear in this context",
3179 return error_mark_node;
3184 decl = (finish_id_expression
3185 (id_expression, decl, parser->scope,
3187 parser->integral_constant_expression_p,
3188 parser->allow_non_integral_constant_expression_p,
3189 &parser->non_integral_constant_expression_p,
3190 template_p, done, address_p,
3194 cp_parser_error (parser, error_msg);
3198 /* Anything else is an error. */
3200 /* ...unless we have an Objective-C++ message or string literal, that is. */
3201 if (c_dialect_objc ()
3202 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3203 return cp_parser_objc_expression (parser);
3205 cp_parser_error (parser, "expected primary-expression");
3206 return error_mark_node;
3210 /* Parse an id-expression.
3217 :: [opt] nested-name-specifier template [opt] unqualified-id
3219 :: operator-function-id
3222 Return a representation of the unqualified portion of the
3223 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3224 a `::' or nested-name-specifier.
3226 Often, if the id-expression was a qualified-id, the caller will
3227 want to make a SCOPE_REF to represent the qualified-id. This
3228 function does not do this in order to avoid wastefully creating
3229 SCOPE_REFs when they are not required.
3231 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3234 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3235 uninstantiated templates.
3237 If *TEMPLATE_P is non-NULL, it is set to true iff the
3238 `template' keyword is used to explicitly indicate that the entity
3239 named is a template.
3241 If DECLARATOR_P is true, the id-expression is appearing as part of
3242 a declarator, rather than as part of an expression. */
3245 cp_parser_id_expression (cp_parser *parser,
3246 bool template_keyword_p,
3247 bool check_dependency_p,
3251 bool global_scope_p;
3252 bool nested_name_specifier_p;
3254 /* Assume the `template' keyword was not used. */
3256 *template_p = template_keyword_p;
3258 /* Look for the optional `::' operator. */
3260 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3262 /* Look for the optional nested-name-specifier. */
3263 nested_name_specifier_p
3264 = (cp_parser_nested_name_specifier_opt (parser,
3265 /*typename_keyword_p=*/false,
3270 /* If there is a nested-name-specifier, then we are looking at
3271 the first qualified-id production. */
3272 if (nested_name_specifier_p)
3275 tree saved_object_scope;
3276 tree saved_qualifying_scope;
3277 tree unqualified_id;
3280 /* See if the next token is the `template' keyword. */
3282 template_p = &is_template;
3283 *template_p = cp_parser_optional_template_keyword (parser);
3284 /* Name lookup we do during the processing of the
3285 unqualified-id might obliterate SCOPE. */
3286 saved_scope = parser->scope;
3287 saved_object_scope = parser->object_scope;
3288 saved_qualifying_scope = parser->qualifying_scope;
3289 /* Process the final unqualified-id. */
3290 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3293 /* Restore the SAVED_SCOPE for our caller. */
3294 parser->scope = saved_scope;
3295 parser->object_scope = saved_object_scope;
3296 parser->qualifying_scope = saved_qualifying_scope;
3298 return unqualified_id;
3300 /* Otherwise, if we are in global scope, then we are looking at one
3301 of the other qualified-id productions. */
3302 else if (global_scope_p)
3307 /* Peek at the next token. */
3308 token = cp_lexer_peek_token (parser->lexer);
3310 /* If it's an identifier, and the next token is not a "<", then
3311 we can avoid the template-id case. This is an optimization
3312 for this common case. */
3313 if (token->type == CPP_NAME
3314 && !cp_parser_nth_token_starts_template_argument_list_p
3316 return cp_parser_identifier (parser);
3318 cp_parser_parse_tentatively (parser);
3319 /* Try a template-id. */
3320 id = cp_parser_template_id (parser,
3321 /*template_keyword_p=*/false,
3322 /*check_dependency_p=*/true,
3324 /* If that worked, we're done. */
3325 if (cp_parser_parse_definitely (parser))
3328 /* Peek at the next token. (Changes in the token buffer may
3329 have invalidated the pointer obtained above.) */
3330 token = cp_lexer_peek_token (parser->lexer);
3332 switch (token->type)
3335 return cp_parser_identifier (parser);
3338 if (token->keyword == RID_OPERATOR)
3339 return cp_parser_operator_function_id (parser);
3343 cp_parser_error (parser, "expected id-expression");
3344 return error_mark_node;
3348 return cp_parser_unqualified_id (parser, template_keyword_p,
3349 /*check_dependency_p=*/true,
3353 /* Parse an unqualified-id.
3357 operator-function-id
3358 conversion-function-id
3362 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3363 keyword, in a construct like `A::template ...'.
3365 Returns a representation of unqualified-id. For the `identifier'
3366 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3367 production a BIT_NOT_EXPR is returned; the operand of the
3368 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3369 other productions, see the documentation accompanying the
3370 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3371 names are looked up in uninstantiated templates. If DECLARATOR_P
3372 is true, the unqualified-id is appearing as part of a declarator,
3373 rather than as part of an expression. */
3376 cp_parser_unqualified_id (cp_parser* parser,
3377 bool template_keyword_p,
3378 bool check_dependency_p,
3383 /* Peek at the next token. */
3384 token = cp_lexer_peek_token (parser->lexer);
3386 switch (token->type)
3392 /* We don't know yet whether or not this will be a
3394 cp_parser_parse_tentatively (parser);
3395 /* Try a template-id. */
3396 id = cp_parser_template_id (parser, template_keyword_p,
3399 /* If it worked, we're done. */
3400 if (cp_parser_parse_definitely (parser))
3402 /* Otherwise, it's an ordinary identifier. */
3403 return cp_parser_identifier (parser);
3406 case CPP_TEMPLATE_ID:
3407 return cp_parser_template_id (parser, template_keyword_p,
3414 tree qualifying_scope;
3419 /* Consume the `~' token. */
3420 cp_lexer_consume_token (parser->lexer);
3421 /* Parse the class-name. The standard, as written, seems to
3424 template <typename T> struct S { ~S (); };
3425 template <typename T> S<T>::~S() {}
3427 is invalid, since `~' must be followed by a class-name, but
3428 `S<T>' is dependent, and so not known to be a class.
3429 That's not right; we need to look in uninstantiated
3430 templates. A further complication arises from:
3432 template <typename T> void f(T t) {
3436 Here, it is not possible to look up `T' in the scope of `T'
3437 itself. We must look in both the current scope, and the
3438 scope of the containing complete expression.
3440 Yet another issue is:
3449 The standard does not seem to say that the `S' in `~S'
3450 should refer to the type `S' and not the data member
3453 /* DR 244 says that we look up the name after the "~" in the
3454 same scope as we looked up the qualifying name. That idea
3455 isn't fully worked out; it's more complicated than that. */
3456 scope = parser->scope;
3457 object_scope = parser->object_scope;
3458 qualifying_scope = parser->qualifying_scope;
3460 /* Check for invalid scopes. */
3461 if (scope == error_mark_node)
3463 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3464 cp_lexer_consume_token (parser->lexer);
3465 return error_mark_node;
3467 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3469 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3470 error ("scope %qT before %<~%> is not a class-name", scope);
3471 cp_parser_simulate_error (parser);
3472 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3473 cp_lexer_consume_token (parser->lexer);
3474 return error_mark_node;
3476 gcc_assert (!scope || TYPE_P (scope));
3478 /* If the name is of the form "X::~X" it's OK. */
3479 token = cp_lexer_peek_token (parser->lexer);
3481 && token->type == CPP_NAME
3482 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3484 && constructor_name_p (token->value, scope))
3486 cp_lexer_consume_token (parser->lexer);
3487 return build_nt (BIT_NOT_EXPR, scope);
3490 /* If there was an explicit qualification (S::~T), first look
3491 in the scope given by the qualification (i.e., S). */
3493 type_decl = NULL_TREE;
3496 cp_parser_parse_tentatively (parser);
3497 type_decl = cp_parser_class_name (parser,
3498 /*typename_keyword_p=*/false,
3499 /*template_keyword_p=*/false,
3501 /*check_dependency=*/false,
3502 /*class_head_p=*/false,
3504 if (cp_parser_parse_definitely (parser))
3507 /* In "N::S::~S", look in "N" as well. */
3508 if (!done && scope && qualifying_scope)
3510 cp_parser_parse_tentatively (parser);
3511 parser->scope = qualifying_scope;
3512 parser->object_scope = NULL_TREE;
3513 parser->qualifying_scope = NULL_TREE;
3515 = cp_parser_class_name (parser,
3516 /*typename_keyword_p=*/false,
3517 /*template_keyword_p=*/false,
3519 /*check_dependency=*/false,
3520 /*class_head_p=*/false,
3522 if (cp_parser_parse_definitely (parser))
3525 /* In "p->S::~T", look in the scope given by "*p" as well. */
3526 else if (!done && object_scope)
3528 cp_parser_parse_tentatively (parser);
3529 parser->scope = object_scope;
3530 parser->object_scope = NULL_TREE;
3531 parser->qualifying_scope = NULL_TREE;
3533 = cp_parser_class_name (parser,
3534 /*typename_keyword_p=*/false,
3535 /*template_keyword_p=*/false,
3537 /*check_dependency=*/false,
3538 /*class_head_p=*/false,
3540 if (cp_parser_parse_definitely (parser))
3543 /* Look in the surrounding context. */
3546 parser->scope = NULL_TREE;
3547 parser->object_scope = NULL_TREE;
3548 parser->qualifying_scope = NULL_TREE;
3550 = cp_parser_class_name (parser,
3551 /*typename_keyword_p=*/false,
3552 /*template_keyword_p=*/false,
3554 /*check_dependency=*/false,
3555 /*class_head_p=*/false,
3558 /* If an error occurred, assume that the name of the
3559 destructor is the same as the name of the qualifying
3560 class. That allows us to keep parsing after running
3561 into ill-formed destructor names. */
3562 if (type_decl == error_mark_node && scope)
3563 return build_nt (BIT_NOT_EXPR, scope);
3564 else if (type_decl == error_mark_node)
3565 return error_mark_node;
3567 /* Check that destructor name and scope match. */
3568 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3570 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3571 error ("declaration of %<~%T%> as member of %qT",
3573 cp_parser_simulate_error (parser);
3574 return error_mark_node;
3579 A typedef-name that names a class shall not be used as the
3580 identifier in the declarator for a destructor declaration. */
3582 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3583 && !DECL_SELF_REFERENCE_P (type_decl)
3584 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3585 error ("typedef-name %qD used as destructor declarator",
3588 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3592 if (token->keyword == RID_OPERATOR)
3596 /* This could be a template-id, so we try that first. */
3597 cp_parser_parse_tentatively (parser);
3598 /* Try a template-id. */
3599 id = cp_parser_template_id (parser, template_keyword_p,
3600 /*check_dependency_p=*/true,
3602 /* If that worked, we're done. */
3603 if (cp_parser_parse_definitely (parser))
3605 /* We still don't know whether we're looking at an
3606 operator-function-id or a conversion-function-id. */
3607 cp_parser_parse_tentatively (parser);
3608 /* Try an operator-function-id. */
3609 id = cp_parser_operator_function_id (parser);
3610 /* If that didn't work, try a conversion-function-id. */
3611 if (!cp_parser_parse_definitely (parser))
3612 id = cp_parser_conversion_function_id (parser);
3619 cp_parser_error (parser, "expected unqualified-id");
3620 return error_mark_node;
3624 /* Parse an (optional) nested-name-specifier.
3626 nested-name-specifier:
3627 class-or-namespace-name :: nested-name-specifier [opt]
3628 class-or-namespace-name :: template nested-name-specifier [opt]
3630 PARSER->SCOPE should be set appropriately before this function is
3631 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3632 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3635 Sets PARSER->SCOPE to the class (TYPE) or namespace
3636 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3637 it unchanged if there is no nested-name-specifier. Returns the new
3638 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3640 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3641 part of a declaration and/or decl-specifier. */
3644 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3645 bool typename_keyword_p,
3646 bool check_dependency_p,
3648 bool is_declaration)
3650 bool success = false;
3651 cp_token_position start = 0;
3654 /* Remember where the nested-name-specifier starts. */
3655 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3657 start = cp_lexer_token_position (parser->lexer, false);
3658 push_deferring_access_checks (dk_deferred);
3665 tree saved_qualifying_scope;
3666 bool template_keyword_p;
3668 /* Spot cases that cannot be the beginning of a
3669 nested-name-specifier. */
3670 token = cp_lexer_peek_token (parser->lexer);
3672 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3673 the already parsed nested-name-specifier. */
3674 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3676 /* Grab the nested-name-specifier and continue the loop. */
3677 cp_parser_pre_parsed_nested_name_specifier (parser);
3678 /* If we originally encountered this nested-name-specifier
3679 with IS_DECLARATION set to false, we will not have
3680 resolved TYPENAME_TYPEs, so we must do so here. */
3682 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3684 new_scope = resolve_typename_type (parser->scope,
3685 /*only_current_p=*/false);
3686 if (new_scope != error_mark_node)
3687 parser->scope = new_scope;
3693 /* Spot cases that cannot be the beginning of a
3694 nested-name-specifier. On the second and subsequent times
3695 through the loop, we look for the `template' keyword. */
3696 if (success && token->keyword == RID_TEMPLATE)
3698 /* A template-id can start a nested-name-specifier. */
3699 else if (token->type == CPP_TEMPLATE_ID)
3703 /* If the next token is not an identifier, then it is
3704 definitely not a class-or-namespace-name. */
3705 if (token->type != CPP_NAME)
3707 /* If the following token is neither a `<' (to begin a
3708 template-id), nor a `::', then we are not looking at a
3709 nested-name-specifier. */
3710 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3711 if (token->type != CPP_SCOPE
3712 && !cp_parser_nth_token_starts_template_argument_list_p
3717 /* The nested-name-specifier is optional, so we parse
3719 cp_parser_parse_tentatively (parser);
3721 /* Look for the optional `template' keyword, if this isn't the
3722 first time through the loop. */
3724 template_keyword_p = cp_parser_optional_template_keyword (parser);
3726 template_keyword_p = false;
3728 /* Save the old scope since the name lookup we are about to do
3729 might destroy it. */
3730 old_scope = parser->scope;
3731 saved_qualifying_scope = parser->qualifying_scope;
3732 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3733 look up names in "X<T>::I" in order to determine that "Y" is
3734 a template. So, if we have a typename at this point, we make
3735 an effort to look through it. */
3737 && !typename_keyword_p
3739 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3740 parser->scope = resolve_typename_type (parser->scope,
3741 /*only_current_p=*/false);
3742 /* Parse the qualifying entity. */
3744 = cp_parser_class_or_namespace_name (parser,
3750 /* Look for the `::' token. */
3751 cp_parser_require (parser, CPP_SCOPE, "`::'");
3753 /* If we found what we wanted, we keep going; otherwise, we're
3755 if (!cp_parser_parse_definitely (parser))
3757 bool error_p = false;
3759 /* Restore the OLD_SCOPE since it was valid before the
3760 failed attempt at finding the last
3761 class-or-namespace-name. */
3762 parser->scope = old_scope;
3763 parser->qualifying_scope = saved_qualifying_scope;
3764 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3766 /* If the next token is an identifier, and the one after
3767 that is a `::', then any valid interpretation would have
3768 found a class-or-namespace-name. */
3769 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3770 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3772 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3775 token = cp_lexer_consume_token (parser->lexer);
3778 if (!token->ambiguous_p)
3781 tree ambiguous_decls;
3783 decl = cp_parser_lookup_name (parser, token->value,
3785 /*is_template=*/false,
3786 /*is_namespace=*/false,
3787 /*check_dependency=*/true,
3789 if (TREE_CODE (decl) == TEMPLATE_DECL)
3790 error ("%qD used without template parameters", decl);
3791 else if (ambiguous_decls)
3793 error ("reference to %qD is ambiguous",
3795 print_candidates (ambiguous_decls);
3796 decl = error_mark_node;
3799 cp_parser_name_lookup_error
3800 (parser, token->value, decl,
3801 "is not a class or namespace");
3803 parser->scope = error_mark_node;
3805 /* Treat this as a successful nested-name-specifier
3810 If the name found is not a class-name (clause
3811 _class_) or namespace-name (_namespace.def_), the
3812 program is ill-formed. */
3815 cp_lexer_consume_token (parser->lexer);
3819 /* We've found one valid nested-name-specifier. */
3821 /* Name lookup always gives us a DECL. */
3822 if (TREE_CODE (new_scope) == TYPE_DECL)
3823 new_scope = TREE_TYPE (new_scope);
3824 /* Uses of "template" must be followed by actual templates. */
3825 if (template_keyword_p
3826 && !(CLASS_TYPE_P (new_scope)
3827 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3828 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3829 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3830 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3831 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3832 == TEMPLATE_ID_EXPR)))
3833 pedwarn (TYPE_P (new_scope)
3834 ? "%qT is not a template"
3835 : "%qD is not a template",
3837 /* If it is a class scope, try to complete it; we are about to
3838 be looking up names inside the class. */
3839 if (TYPE_P (new_scope)
3840 /* Since checking types for dependency can be expensive,
3841 avoid doing it if the type is already complete. */
3842 && !COMPLETE_TYPE_P (new_scope)
3843 /* Do not try to complete dependent types. */
3844 && !dependent_type_p (new_scope))
3845 new_scope = complete_type (new_scope);
3846 /* Make sure we look in the right scope the next time through
3848 parser->scope = new_scope;
3851 /* If parsing tentatively, replace the sequence of tokens that makes
3852 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3853 token. That way, should we re-parse the token stream, we will
3854 not have to repeat the effort required to do the parse, nor will
3855 we issue duplicate error messages. */
3856 if (success && start)
3861 token = cp_lexer_token_at (parser->lexer, start);
3862 /* Reset the contents of the START token. */
3863 token->type = CPP_NESTED_NAME_SPECIFIER;
3864 /* Retrieve any deferred checks. Do not pop this access checks yet
3865 so the memory will not be reclaimed during token replacing below. */
3866 access_checks = get_deferred_access_checks ();
3867 token->value = build_tree_list (copy_list (access_checks),
3869 TREE_TYPE (token->value) = parser->qualifying_scope;
3870 token->keyword = RID_MAX;
3872 /* Purge all subsequent tokens. */
3873 cp_lexer_purge_tokens_after (parser->lexer, start);
3877 pop_to_parent_deferring_access_checks ();
3879 return success ? parser->scope : NULL_TREE;
3882 /* Parse a nested-name-specifier. See
3883 cp_parser_nested_name_specifier_opt for details. This function
3884 behaves identically, except that it will an issue an error if no
3885 nested-name-specifier is present. */
3888 cp_parser_nested_name_specifier (cp_parser *parser,
3889 bool typename_keyword_p,
3890 bool check_dependency_p,
3892 bool is_declaration)
3896 /* Look for the nested-name-specifier. */
3897 scope = cp_parser_nested_name_specifier_opt (parser,
3902 /* If it was not present, issue an error message. */
3905 cp_parser_error (parser, "expected nested-name-specifier");
3906 parser->scope = NULL_TREE;
3912 /* Parse a class-or-namespace-name.
3914 class-or-namespace-name:
3918 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3919 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3920 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3921 TYPE_P is TRUE iff the next name should be taken as a class-name,
3922 even the same name is declared to be another entity in the same
3925 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3926 specified by the class-or-namespace-name. If neither is found the
3927 ERROR_MARK_NODE is returned. */
3930 cp_parser_class_or_namespace_name (cp_parser *parser,
3931 bool typename_keyword_p,
3932 bool template_keyword_p,
3933 bool check_dependency_p,
3935 bool is_declaration)
3938 tree saved_qualifying_scope;
3939 tree saved_object_scope;
3943 /* Before we try to parse the class-name, we must save away the
3944 current PARSER->SCOPE since cp_parser_class_name will destroy
3946 saved_scope = parser->scope;
3947 saved_qualifying_scope = parser->qualifying_scope;
3948 saved_object_scope = parser->object_scope;
3949 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3950 there is no need to look for a namespace-name. */
3951 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3953 cp_parser_parse_tentatively (parser);
3954 scope = cp_parser_class_name (parser,
3957 type_p ? class_type : none_type,
3959 /*class_head_p=*/false,
3961 /* If that didn't work, try for a namespace-name. */
3962 if (!only_class_p && !cp_parser_parse_definitely (parser))
3964 /* Restore the saved scope. */
3965 parser->scope = saved_scope;
3966 parser->qualifying_scope = saved_qualifying_scope;
3967 parser->object_scope = saved_object_scope;
3968 /* If we are not looking at an identifier followed by the scope
3969 resolution operator, then this is not part of a
3970 nested-name-specifier. (Note that this function is only used
3971 to parse the components of a nested-name-specifier.) */
3972 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3973 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3974 return error_mark_node;
3975 scope = cp_parser_namespace_name (parser);
3981 /* Parse a postfix-expression.
3985 postfix-expression [ expression ]
3986 postfix-expression ( expression-list [opt] )
3987 simple-type-specifier ( expression-list [opt] )
3988 typename :: [opt] nested-name-specifier identifier
3989 ( expression-list [opt] )
3990 typename :: [opt] nested-name-specifier template [opt] template-id
3991 ( expression-list [opt] )
3992 postfix-expression . template [opt] id-expression
3993 postfix-expression -> template [opt] id-expression
3994 postfix-expression . pseudo-destructor-name
3995 postfix-expression -> pseudo-destructor-name
3996 postfix-expression ++
3997 postfix-expression --
3998 dynamic_cast < type-id > ( expression )
3999 static_cast < type-id > ( expression )
4000 reinterpret_cast < type-id > ( expression )
4001 const_cast < type-id > ( expression )
4002 typeid ( expression )
4008 ( type-id ) { initializer-list , [opt] }
4010 This extension is a GNU version of the C99 compound-literal
4011 construct. (The C99 grammar uses `type-name' instead of `type-id',
4012 but they are essentially the same concept.)
4014 If ADDRESS_P is true, the postfix expression is the operand of the
4015 `&' operator. CAST_P is true if this expression is the target of a
4018 Returns a representation of the expression. */
4021 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4025 cp_id_kind idk = CP_ID_KIND_NONE;
4026 tree postfix_expression = NULL_TREE;
4028 /* Peek at the next token. */
4029 token = cp_lexer_peek_token (parser->lexer);
4030 /* Some of the productions are determined by keywords. */
4031 keyword = token->keyword;
4041 const char *saved_message;
4043 /* All of these can be handled in the same way from the point
4044 of view of parsing. Begin by consuming the token
4045 identifying the cast. */
4046 cp_lexer_consume_token (parser->lexer);
4048 /* New types cannot be defined in the cast. */
4049 saved_message = parser->type_definition_forbidden_message;
4050 parser->type_definition_forbidden_message
4051 = "types may not be defined in casts";
4053 /* Look for the opening `<'. */
4054 cp_parser_require (parser, CPP_LESS, "`<'");
4055 /* Parse the type to which we are casting. */
4056 type = cp_parser_type_id (parser);
4057 /* Look for the closing `>'. */
4058 cp_parser_require (parser, CPP_GREATER, "`>'");
4059 /* Restore the old message. */
4060 parser->type_definition_forbidden_message = saved_message;
4062 /* And the expression which is being cast. */
4063 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4064 expression = cp_parser_expression (parser, /*cast_p=*/true);
4065 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4067 /* Only type conversions to integral or enumeration types
4068 can be used in constant-expressions. */
4069 if (parser->integral_constant_expression_p
4070 && !dependent_type_p (type)
4071 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4072 && (cp_parser_non_integral_constant_expression
4074 "a cast to a type other than an integral or "
4075 "enumeration type")))
4076 return error_mark_node;
4082 = build_dynamic_cast (type, expression);
4086 = build_static_cast (type, expression);
4090 = build_reinterpret_cast (type, expression);
4094 = build_const_cast (type, expression);
4105 const char *saved_message;
4106 bool saved_in_type_id_in_expr_p;
4108 /* Consume the `typeid' token. */
4109 cp_lexer_consume_token (parser->lexer);
4110 /* Look for the `(' token. */
4111 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4112 /* Types cannot be defined in a `typeid' expression. */
4113 saved_message = parser->type_definition_forbidden_message;
4114 parser->type_definition_forbidden_message
4115 = "types may not be defined in a `typeid\' expression";
4116 /* We can't be sure yet whether we're looking at a type-id or an
4118 cp_parser_parse_tentatively (parser);
4119 /* Try a type-id first. */
4120 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4121 parser->in_type_id_in_expr_p = true;
4122 type = cp_parser_type_id (parser);
4123 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4124 /* Look for the `)' token. Otherwise, we can't be sure that
4125 we're not looking at an expression: consider `typeid (int
4126 (3))', for example. */
4127 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4128 /* If all went well, simply lookup the type-id. */
4129 if (cp_parser_parse_definitely (parser))
4130 postfix_expression = get_typeid (type);
4131 /* Otherwise, fall back to the expression variant. */
4136 /* Look for an expression. */
4137 expression = cp_parser_expression (parser, /*cast_p=*/false);
4138 /* Compute its typeid. */
4139 postfix_expression = build_typeid (expression);
4140 /* Look for the `)' token. */
4141 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4143 /* `typeid' may not appear in an integral constant expression. */
4144 if (cp_parser_non_integral_constant_expression(parser,
4145 "`typeid' operator"))
4146 return error_mark_node;
4147 /* Restore the saved message. */
4148 parser->type_definition_forbidden_message = saved_message;
4155 /* The syntax permitted here is the same permitted for an
4156 elaborated-type-specifier. */
4157 type = cp_parser_elaborated_type_specifier (parser,
4158 /*is_friend=*/false,
4159 /*is_declaration=*/false);
4160 postfix_expression = cp_parser_functional_cast (parser, type);
4168 /* If the next thing is a simple-type-specifier, we may be
4169 looking at a functional cast. We could also be looking at
4170 an id-expression. So, we try the functional cast, and if
4171 that doesn't work we fall back to the primary-expression. */
4172 cp_parser_parse_tentatively (parser);
4173 /* Look for the simple-type-specifier. */
4174 type = cp_parser_simple_type_specifier (parser,
4175 /*decl_specs=*/NULL,
4176 CP_PARSER_FLAGS_NONE);
4177 /* Parse the cast itself. */
4178 if (!cp_parser_error_occurred (parser))
4180 = cp_parser_functional_cast (parser, type);
4181 /* If that worked, we're done. */
4182 if (cp_parser_parse_definitely (parser))
4185 /* If the functional-cast didn't work out, try a
4186 compound-literal. */
4187 if (cp_parser_allow_gnu_extensions_p (parser)
4188 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4190 VEC(constructor_elt,gc) *initializer_list = NULL;
4191 bool saved_in_type_id_in_expr_p;
4193 cp_parser_parse_tentatively (parser);
4194 /* Consume the `('. */
4195 cp_lexer_consume_token (parser->lexer);
4196 /* Parse the type. */
4197 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4198 parser->in_type_id_in_expr_p = true;
4199 type = cp_parser_type_id (parser);
4200 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4201 /* Look for the `)'. */
4202 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4203 /* Look for the `{'. */
4204 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4205 /* If things aren't going well, there's no need to
4207 if (!cp_parser_error_occurred (parser))
4209 bool non_constant_p;
4210 /* Parse the initializer-list. */
4212 = cp_parser_initializer_list (parser, &non_constant_p);
4213 /* Allow a trailing `,'. */
4214 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4215 cp_lexer_consume_token (parser->lexer);
4216 /* Look for the final `}'. */
4217 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4219 /* If that worked, we're definitely looking at a
4220 compound-literal expression. */
4221 if (cp_parser_parse_definitely (parser))
4223 /* Warn the user that a compound literal is not
4224 allowed in standard C++. */
4226 pedwarn ("ISO C++ forbids compound-literals");
4227 /* Form the representation of the compound-literal. */
4229 = finish_compound_literal (type, initializer_list);
4234 /* It must be a primary-expression. */
4236 = cp_parser_primary_expression (parser, address_p, cast_p,
4237 /*template_arg_p=*/false,
4243 /* Keep looping until the postfix-expression is complete. */
4246 if (idk == CP_ID_KIND_UNQUALIFIED
4247 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4248 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4249 /* It is not a Koenig lookup function call. */
4251 = unqualified_name_lookup_error (postfix_expression);
4253 /* Peek at the next token. */
4254 token = cp_lexer_peek_token (parser->lexer);
4256 switch (token->type)
4258 case CPP_OPEN_SQUARE:
4260 = cp_parser_postfix_open_square_expression (parser,
4263 idk = CP_ID_KIND_NONE;
4266 case CPP_OPEN_PAREN:
4267 /* postfix-expression ( expression-list [opt] ) */
4270 bool is_builtin_constant_p;
4271 bool saved_integral_constant_expression_p = false;
4272 bool saved_non_integral_constant_expression_p = false;
4275 is_builtin_constant_p
4276 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4277 if (is_builtin_constant_p)
4279 /* The whole point of __builtin_constant_p is to allow
4280 non-constant expressions to appear as arguments. */
4281 saved_integral_constant_expression_p
4282 = parser->integral_constant_expression_p;
4283 saved_non_integral_constant_expression_p
4284 = parser->non_integral_constant_expression_p;
4285 parser->integral_constant_expression_p = false;
4287 args = (cp_parser_parenthesized_expression_list
4288 (parser, /*is_attribute_list=*/false,
4290 /*non_constant_p=*/NULL));
4291 if (is_builtin_constant_p)
4293 parser->integral_constant_expression_p
4294 = saved_integral_constant_expression_p;
4295 parser->non_integral_constant_expression_p
4296 = saved_non_integral_constant_expression_p;
4299 if (args == error_mark_node)
4301 postfix_expression = error_mark_node;
4305 /* Function calls are not permitted in
4306 constant-expressions. */
4307 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4308 && cp_parser_non_integral_constant_expression (parser,
4311 postfix_expression = error_mark_node;
4316 if (idk == CP_ID_KIND_UNQUALIFIED)
4318 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4324 = perform_koenig_lookup (postfix_expression, args);
4328 = unqualified_fn_lookup_error (postfix_expression);
4330 /* We do not perform argument-dependent lookup if
4331 normal lookup finds a non-function, in accordance
4332 with the expected resolution of DR 218. */
4333 else if (args && is_overloaded_fn (postfix_expression))
4335 tree fn = get_first_fn (postfix_expression);
4337 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4338 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4340 /* Only do argument dependent lookup if regular
4341 lookup does not find a set of member functions.
4342 [basic.lookup.koenig]/2a */
4343 if (!DECL_FUNCTION_MEMBER_P (fn))
4347 = perform_koenig_lookup (postfix_expression, args);
4352 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4354 tree instance = TREE_OPERAND (postfix_expression, 0);
4355 tree fn = TREE_OPERAND (postfix_expression, 1);
4357 if (processing_template_decl
4358 && (type_dependent_expression_p (instance)
4359 || (!BASELINK_P (fn)
4360 && TREE_CODE (fn) != FIELD_DECL)
4361 || type_dependent_expression_p (fn)
4362 || any_type_dependent_arguments_p (args)))
4365 = build_min_nt (CALL_EXPR, postfix_expression,
4370 if (BASELINK_P (fn))
4372 = (build_new_method_call
4373 (instance, fn, args, NULL_TREE,
4374 (idk == CP_ID_KIND_QUALIFIED
4375 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4378 = finish_call_expr (postfix_expression, args,
4379 /*disallow_virtual=*/false,
4380 /*koenig_p=*/false);
4382 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4383 || TREE_CODE (postfix_expression) == MEMBER_REF
4384 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4385 postfix_expression = (build_offset_ref_call_from_tree
4386 (postfix_expression, args));
4387 else if (idk == CP_ID_KIND_QUALIFIED)
4388 /* A call to a static class member, or a namespace-scope
4391 = finish_call_expr (postfix_expression, args,
4392 /*disallow_virtual=*/true,
4395 /* All other function calls. */
4397 = finish_call_expr (postfix_expression, args,
4398 /*disallow_virtual=*/false,
4401 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4402 idk = CP_ID_KIND_NONE;
4408 /* postfix-expression . template [opt] id-expression
4409 postfix-expression . pseudo-destructor-name
4410 postfix-expression -> template [opt] id-expression
4411 postfix-expression -> pseudo-destructor-name */
4413 /* Consume the `.' or `->' operator. */
4414 cp_lexer_consume_token (parser->lexer);
4417 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4423 /* postfix-expression ++ */
4424 /* Consume the `++' token. */
4425 cp_lexer_consume_token (parser->lexer);
4426 /* Generate a representation for the complete expression. */
4428 = finish_increment_expr (postfix_expression,
4429 POSTINCREMENT_EXPR);
4430 /* Increments may not appear in constant-expressions. */
4431 if (cp_parser_non_integral_constant_expression (parser,
4433 postfix_expression = error_mark_node;
4434 idk = CP_ID_KIND_NONE;
4437 case CPP_MINUS_MINUS:
4438 /* postfix-expression -- */
4439 /* Consume the `--' token. */
4440 cp_lexer_consume_token (parser->lexer);
4441 /* Generate a representation for the complete expression. */
4443 = finish_increment_expr (postfix_expression,
4444 POSTDECREMENT_EXPR);
4445 /* Decrements may not appear in constant-expressions. */
4446 if (cp_parser_non_integral_constant_expression (parser,
4448 postfix_expression = error_mark_node;
4449 idk = CP_ID_KIND_NONE;
4453 return postfix_expression;
4457 /* We should never get here. */
4459 return error_mark_node;
4462 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4463 by cp_parser_builtin_offsetof. We're looking for
4465 postfix-expression [ expression ]
4467 FOR_OFFSETOF is set if we're being called in that context, which
4468 changes how we deal with integer constant expressions. */
4471 cp_parser_postfix_open_square_expression (cp_parser *parser,
4472 tree postfix_expression,
4477 /* Consume the `[' token. */
4478 cp_lexer_consume_token (parser->lexer);
4480 /* Parse the index expression. */
4481 /* ??? For offsetof, there is a question of what to allow here. If
4482 offsetof is not being used in an integral constant expression context,
4483 then we *could* get the right answer by computing the value at runtime.
4484 If we are in an integral constant expression context, then we might
4485 could accept any constant expression; hard to say without analysis.
4486 Rather than open the barn door too wide right away, allow only integer
4487 constant expressions here. */
4489 index = cp_parser_constant_expression (parser, false, NULL);
4491 index = cp_parser_expression (parser, /*cast_p=*/false);
4493 /* Look for the closing `]'. */
4494 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4496 /* Build the ARRAY_REF. */
4497 postfix_expression = grok_array_decl (postfix_expression, index);
4499 /* When not doing offsetof, array references are not permitted in
4500 constant-expressions. */
4502 && (cp_parser_non_integral_constant_expression
4503 (parser, "an array reference")))
4504 postfix_expression = error_mark_node;
4506 return postfix_expression;
4509 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4510 by cp_parser_builtin_offsetof. We're looking for
4512 postfix-expression . template [opt] id-expression
4513 postfix-expression . pseudo-destructor-name
4514 postfix-expression -> template [opt] id-expression
4515 postfix-expression -> pseudo-destructor-name
4517 FOR_OFFSETOF is set if we're being called in that context. That sorta
4518 limits what of the above we'll actually accept, but nevermind.
4519 TOKEN_TYPE is the "." or "->" token, which will already have been
4520 removed from the stream. */
4523 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4524 enum cpp_ttype token_type,
4525 tree postfix_expression,
4526 bool for_offsetof, cp_id_kind *idk)
4530 bool pseudo_destructor_p;
4531 tree scope = NULL_TREE;
4533 /* If this is a `->' operator, dereference the pointer. */
4534 if (token_type == CPP_DEREF)
4535 postfix_expression = build_x_arrow (postfix_expression);
4536 /* Check to see whether or not the expression is type-dependent. */
4537 dependent_p = type_dependent_expression_p (postfix_expression);
4538 /* The identifier following the `->' or `.' is not qualified. */
4539 parser->scope = NULL_TREE;
4540 parser->qualifying_scope = NULL_TREE;
4541 parser->object_scope = NULL_TREE;
4542 *idk = CP_ID_KIND_NONE;
4543 /* Enter the scope corresponding to the type of the object
4544 given by the POSTFIX_EXPRESSION. */
4545 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4547 scope = TREE_TYPE (postfix_expression);
4548 /* According to the standard, no expression should ever have
4549 reference type. Unfortunately, we do not currently match
4550 the standard in this respect in that our internal representation
4551 of an expression may have reference type even when the standard
4552 says it does not. Therefore, we have to manually obtain the
4553 underlying type here. */
4554 scope = non_reference (scope);
4555 /* The type of the POSTFIX_EXPRESSION must be complete. */
4556 if (scope == unknown_type_node)
4558 error ("%qE does not have class type", postfix_expression);
4562 scope = complete_type_or_else (scope, NULL_TREE);
4563 /* Let the name lookup machinery know that we are processing a
4564 class member access expression. */
4565 parser->context->object_type = scope;
4566 /* If something went wrong, we want to be able to discern that case,
4567 as opposed to the case where there was no SCOPE due to the type
4568 of expression being dependent. */
4570 scope = error_mark_node;
4571 /* If the SCOPE was erroneous, make the various semantic analysis
4572 functions exit quickly -- and without issuing additional error
4574 if (scope == error_mark_node)
4575 postfix_expression = error_mark_node;
4578 /* Assume this expression is not a pseudo-destructor access. */
4579 pseudo_destructor_p = false;
4581 /* If the SCOPE is a scalar type, then, if this is a valid program,
4582 we must be looking at a pseudo-destructor-name. */
4583 if (scope && SCALAR_TYPE_P (scope))
4588 cp_parser_parse_tentatively (parser);
4589 /* Parse the pseudo-destructor-name. */
4591 cp_parser_pseudo_destructor_name (parser, &s, &type);
4592 if (cp_parser_parse_definitely (parser))
4594 pseudo_destructor_p = true;
4596 = finish_pseudo_destructor_expr (postfix_expression,
4597 s, TREE_TYPE (type));
4601 if (!pseudo_destructor_p)
4603 /* If the SCOPE is not a scalar type, we are looking at an
4604 ordinary class member access expression, rather than a
4605 pseudo-destructor-name. */
4607 /* Parse the id-expression. */
4608 name = (cp_parser_id_expression
4610 cp_parser_optional_template_keyword (parser),
4611 /*check_dependency_p=*/true,
4613 /*declarator_p=*/false));
4614 /* In general, build a SCOPE_REF if the member name is qualified.
4615 However, if the name was not dependent and has already been
4616 resolved; there is no need to build the SCOPE_REF. For example;
4618 struct X { void f(); };
4619 template <typename T> void f(T* t) { t->X::f(); }
4621 Even though "t" is dependent, "X::f" is not and has been resolved
4622 to a BASELINK; there is no need to include scope information. */
4624 /* But we do need to remember that there was an explicit scope for
4625 virtual function calls. */
4627 *idk = CP_ID_KIND_QUALIFIED;
4629 /* If the name is a template-id that names a type, we will get a
4630 TYPE_DECL here. That is invalid code. */
4631 if (TREE_CODE (name) == TYPE_DECL)
4633 error ("invalid use of %qD", name);
4634 postfix_expression = error_mark_node;
4638 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4640 name = build_qualified_name (/*type=*/NULL_TREE,
4644 parser->scope = NULL_TREE;
4645 parser->qualifying_scope = NULL_TREE;
4646 parser->object_scope = NULL_TREE;
4648 if (scope && name && BASELINK_P (name))
4649 adjust_result_of_qualified_name_lookup
4650 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4652 = finish_class_member_access_expr (postfix_expression, name,
4657 /* We no longer need to look up names in the scope of the object on
4658 the left-hand side of the `.' or `->' operator. */
4659 parser->context->object_type = NULL_TREE;
4661 /* Outside of offsetof, these operators may not appear in
4662 constant-expressions. */
4664 && (cp_parser_non_integral_constant_expression
4665 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4666 postfix_expression = error_mark_node;
4668 return postfix_expression;
4671 /* Parse a parenthesized expression-list.
4674 assignment-expression
4675 expression-list, assignment-expression
4680 identifier, expression-list
4682 CAST_P is true if this expression is the target of a cast.
4684 Returns a TREE_LIST. The TREE_VALUE of each node is a
4685 representation of an assignment-expression. Note that a TREE_LIST
4686 is returned even if there is only a single expression in the list.
4687 error_mark_node is returned if the ( and or ) are
4688 missing. NULL_TREE is returned on no expressions. The parentheses
4689 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4690 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4691 indicates whether or not all of the expressions in the list were
4695 cp_parser_parenthesized_expression_list (cp_parser* parser,
4696 bool is_attribute_list,
4698 bool *non_constant_p)
4700 tree expression_list = NULL_TREE;
4701 bool fold_expr_p = is_attribute_list;
4702 tree identifier = NULL_TREE;
4704 /* Assume all the expressions will be constant. */
4706 *non_constant_p = false;
4708 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4709 return error_mark_node;
4711 /* Consume expressions until there are no more. */
4712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4717 /* At the beginning of attribute lists, check to see if the
4718 next token is an identifier. */
4719 if (is_attribute_list
4720 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4724 /* Consume the identifier. */
4725 token = cp_lexer_consume_token (parser->lexer);
4726 /* Save the identifier. */
4727 identifier = token->value;
4731 /* Parse the next assignment-expression. */
4734 bool expr_non_constant_p;
4735 expr = (cp_parser_constant_expression
4736 (parser, /*allow_non_constant_p=*/true,
4737 &expr_non_constant_p));
4738 if (expr_non_constant_p)
4739 *non_constant_p = true;
4742 expr = cp_parser_assignment_expression (parser, cast_p);
4745 expr = fold_non_dependent_expr (expr);
4747 /* Add it to the list. We add error_mark_node
4748 expressions to the list, so that we can still tell if
4749 the correct form for a parenthesized expression-list
4750 is found. That gives better errors. */
4751 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4753 if (expr == error_mark_node)
4757 /* After the first item, attribute lists look the same as
4758 expression lists. */
4759 is_attribute_list = false;
4762 /* If the next token isn't a `,', then we are done. */
4763 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4766 /* Otherwise, consume the `,' and keep going. */
4767 cp_lexer_consume_token (parser->lexer);
4770 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4775 /* We try and resync to an unnested comma, as that will give the
4776 user better diagnostics. */
4777 ending = cp_parser_skip_to_closing_parenthesis (parser,
4778 /*recovering=*/true,
4780 /*consume_paren=*/true);
4784 return error_mark_node;
4787 /* We built up the list in reverse order so we must reverse it now. */
4788 expression_list = nreverse (expression_list);
4790 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4792 return expression_list;
4795 /* Parse a pseudo-destructor-name.
4797 pseudo-destructor-name:
4798 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4799 :: [opt] nested-name-specifier template template-id :: ~ type-name
4800 :: [opt] nested-name-specifier [opt] ~ type-name
4802 If either of the first two productions is used, sets *SCOPE to the
4803 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4804 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4805 or ERROR_MARK_NODE if the parse fails. */
4808 cp_parser_pseudo_destructor_name (cp_parser* parser,
4812 bool nested_name_specifier_p;
4814 /* Assume that things will not work out. */
4815 *type = error_mark_node;
4817 /* Look for the optional `::' operator. */
4818 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4819 /* Look for the optional nested-name-specifier. */
4820 nested_name_specifier_p
4821 = (cp_parser_nested_name_specifier_opt (parser,
4822 /*typename_keyword_p=*/false,
4823 /*check_dependency_p=*/true,
4825 /*is_declaration=*/true)
4827 /* Now, if we saw a nested-name-specifier, we might be doing the
4828 second production. */
4829 if (nested_name_specifier_p
4830 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4832 /* Consume the `template' keyword. */
4833 cp_lexer_consume_token (parser->lexer);
4834 /* Parse the template-id. */
4835 cp_parser_template_id (parser,
4836 /*template_keyword_p=*/true,
4837 /*check_dependency_p=*/false,
4838 /*is_declaration=*/true);
4839 /* Look for the `::' token. */
4840 cp_parser_require (parser, CPP_SCOPE, "`::'");
4842 /* If the next token is not a `~', then there might be some
4843 additional qualification. */
4844 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4846 /* Look for the type-name. */
4847 *scope = TREE_TYPE (cp_parser_type_name (parser));
4849 if (*scope == error_mark_node)
4852 /* If we don't have ::~, then something has gone wrong. Since
4853 the only caller of this function is looking for something
4854 after `.' or `->' after a scalar type, most likely the
4855 program is trying to get a member of a non-aggregate
4857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4858 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4860 cp_parser_error (parser, "request for member of non-aggregate type");
4864 /* Look for the `::' token. */
4865 cp_parser_require (parser, CPP_SCOPE, "`::'");
4870 /* Look for the `~'. */
4871 cp_parser_require (parser, CPP_COMPL, "`~'");
4872 /* Look for the type-name again. We are not responsible for
4873 checking that it matches the first type-name. */
4874 *type = cp_parser_type_name (parser);
4877 /* Parse a unary-expression.
4883 unary-operator cast-expression
4884 sizeof unary-expression
4892 __extension__ cast-expression
4893 __alignof__ unary-expression
4894 __alignof__ ( type-id )
4895 __real__ cast-expression
4896 __imag__ cast-expression
4899 ADDRESS_P is true iff the unary-expression is appearing as the
4900 operand of the `&' operator. CAST_P is true if this expression is
4901 the target of a cast.
4903 Returns a representation of the expression. */
4906 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4909 enum tree_code unary_operator;
4911 /* Peek at the next token. */
4912 token = cp_lexer_peek_token (parser->lexer);
4913 /* Some keywords give away the kind of expression. */
4914 if (token->type == CPP_KEYWORD)
4916 enum rid keyword = token->keyword;
4926 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4927 /* Consume the token. */
4928 cp_lexer_consume_token (parser->lexer);
4929 /* Parse the operand. */
4930 operand = cp_parser_sizeof_operand (parser, keyword);
4932 if (TYPE_P (operand))
4933 return cxx_sizeof_or_alignof_type (operand, op, true);
4935 return cxx_sizeof_or_alignof_expr (operand, op);
4939 return cp_parser_new_expression (parser);
4942 return cp_parser_delete_expression (parser);
4946 /* The saved value of the PEDANTIC flag. */
4950 /* Save away the PEDANTIC flag. */
4951 cp_parser_extension_opt (parser, &saved_pedantic);
4952 /* Parse the cast-expression. */
4953 expr = cp_parser_simple_cast_expression (parser);
4954 /* Restore the PEDANTIC flag. */
4955 pedantic = saved_pedantic;
4965 /* Consume the `__real__' or `__imag__' token. */
4966 cp_lexer_consume_token (parser->lexer);
4967 /* Parse the cast-expression. */
4968 expression = cp_parser_simple_cast_expression (parser);
4969 /* Create the complete representation. */
4970 return build_x_unary_op ((keyword == RID_REALPART
4971 ? REALPART_EXPR : IMAGPART_EXPR),
4981 /* Look for the `:: new' and `:: delete', which also signal the
4982 beginning of a new-expression, or delete-expression,
4983 respectively. If the next token is `::', then it might be one of
4985 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4989 /* See if the token after the `::' is one of the keywords in
4990 which we're interested. */
4991 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4992 /* If it's `new', we have a new-expression. */
4993 if (keyword == RID_NEW)
4994 return cp_parser_new_expression (parser);
4995 /* Similarly, for `delete'. */
4996 else if (keyword == RID_DELETE)
4997 return cp_parser_delete_expression (parser);
5000 /* Look for a unary operator. */
5001 unary_operator = cp_parser_unary_operator (token);
5002 /* The `++' and `--' operators can be handled similarly, even though
5003 they are not technically unary-operators in the grammar. */
5004 if (unary_operator == ERROR_MARK)
5006 if (token->type == CPP_PLUS_PLUS)
5007 unary_operator = PREINCREMENT_EXPR;
5008 else if (token->type == CPP_MINUS_MINUS)
5009 unary_operator = PREDECREMENT_EXPR;
5010 /* Handle the GNU address-of-label extension. */
5011 else if (cp_parser_allow_gnu_extensions_p (parser)
5012 && token->type == CPP_AND_AND)
5016 /* Consume the '&&' token. */
5017 cp_lexer_consume_token (parser->lexer);
5018 /* Look for the identifier. */
5019 identifier = cp_parser_identifier (parser);
5020 /* Create an expression representing the address. */
5021 return finish_label_address_expr (identifier);
5024 if (unary_operator != ERROR_MARK)
5026 tree cast_expression;
5027 tree expression = error_mark_node;
5028 const char *non_constant_p = NULL;
5030 /* Consume the operator token. */
5031 token = cp_lexer_consume_token (parser->lexer);
5032 /* Parse the cast-expression. */
5034 = cp_parser_cast_expression (parser,
5035 unary_operator == ADDR_EXPR,
5037 /* Now, build an appropriate representation. */
5038 switch (unary_operator)
5041 non_constant_p = "`*'";
5042 expression = build_x_indirect_ref (cast_expression, "unary *");
5046 non_constant_p = "`&'";
5049 expression = build_x_unary_op (unary_operator, cast_expression);
5052 case PREINCREMENT_EXPR:
5053 case PREDECREMENT_EXPR:
5054 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5057 case UNARY_PLUS_EXPR:
5059 case TRUTH_NOT_EXPR:
5060 expression = finish_unary_op_expr (unary_operator, cast_expression);
5068 && cp_parser_non_integral_constant_expression (parser,
5070 expression = error_mark_node;
5075 return cp_parser_postfix_expression (parser, address_p, cast_p);
5078 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5079 unary-operator, the corresponding tree code is returned. */
5081 static enum tree_code
5082 cp_parser_unary_operator (cp_token* token)
5084 switch (token->type)
5087 return INDIRECT_REF;
5093 return UNARY_PLUS_EXPR;
5099 return TRUTH_NOT_EXPR;
5102 return BIT_NOT_EXPR;
5109 /* Parse a new-expression.
5112 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5113 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5115 Returns a representation of the expression. */
5118 cp_parser_new_expression (cp_parser* parser)
5120 bool global_scope_p;
5126 /* Look for the optional `::' operator. */
5128 = (cp_parser_global_scope_opt (parser,
5129 /*current_scope_valid_p=*/false)
5131 /* Look for the `new' operator. */
5132 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5133 /* There's no easy way to tell a new-placement from the
5134 `( type-id )' construct. */
5135 cp_parser_parse_tentatively (parser);
5136 /* Look for a new-placement. */
5137 placement = cp_parser_new_placement (parser);
5138 /* If that didn't work out, there's no new-placement. */
5139 if (!cp_parser_parse_definitely (parser))
5140 placement = NULL_TREE;
5142 /* If the next token is a `(', then we have a parenthesized
5144 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5146 /* Consume the `('. */
5147 cp_lexer_consume_token (parser->lexer);
5148 /* Parse the type-id. */
5149 type = cp_parser_type_id (parser);
5150 /* Look for the closing `)'. */
5151 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5152 /* There should not be a direct-new-declarator in this production,
5153 but GCC used to allowed this, so we check and emit a sensible error
5154 message for this case. */
5155 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5157 error ("array bound forbidden after parenthesized type-id");
5158 inform ("try removing the parentheses around the type-id");
5159 cp_parser_direct_new_declarator (parser);
5163 /* Otherwise, there must be a new-type-id. */
5165 type = cp_parser_new_type_id (parser, &nelts);
5167 /* If the next token is a `(', then we have a new-initializer. */
5168 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5169 initializer = cp_parser_new_initializer (parser);
5171 initializer = NULL_TREE;
5173 /* A new-expression may not appear in an integral constant
5175 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5176 return error_mark_node;
5178 /* Create a representation of the new-expression. */
5179 return build_new (placement, type, nelts, initializer, global_scope_p);
5182 /* Parse a new-placement.
5187 Returns the same representation as for an expression-list. */
5190 cp_parser_new_placement (cp_parser* parser)
5192 tree expression_list;
5194 /* Parse the expression-list. */
5195 expression_list = (cp_parser_parenthesized_expression_list
5196 (parser, false, /*cast_p=*/false,
5197 /*non_constant_p=*/NULL));
5199 return expression_list;
5202 /* Parse a new-type-id.
5205 type-specifier-seq new-declarator [opt]
5207 Returns the TYPE allocated. If the new-type-id indicates an array
5208 type, *NELTS is set to the number of elements in the last array
5209 bound; the TYPE will not include the last array bound. */
5212 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5214 cp_decl_specifier_seq type_specifier_seq;
5215 cp_declarator *new_declarator;
5216 cp_declarator *declarator;
5217 cp_declarator *outer_declarator;
5218 const char *saved_message;
5221 /* The type-specifier sequence must not contain type definitions.
5222 (It cannot contain declarations of new types either, but if they
5223 are not definitions we will catch that because they are not
5225 saved_message = parser->type_definition_forbidden_message;
5226 parser->type_definition_forbidden_message
5227 = "types may not be defined in a new-type-id";
5228 /* Parse the type-specifier-seq. */
5229 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5230 &type_specifier_seq);
5231 /* Restore the old message. */
5232 parser->type_definition_forbidden_message = saved_message;
5233 /* Parse the new-declarator. */
5234 new_declarator = cp_parser_new_declarator_opt (parser);
5236 /* Determine the number of elements in the last array dimension, if
5239 /* Skip down to the last array dimension. */
5240 declarator = new_declarator;
5241 outer_declarator = NULL;
5242 while (declarator && (declarator->kind == cdk_pointer
5243 || declarator->kind == cdk_ptrmem))
5245 outer_declarator = declarator;
5246 declarator = declarator->declarator;
5249 && declarator->kind == cdk_array
5250 && declarator->declarator
5251 && declarator->declarator->kind == cdk_array)
5253 outer_declarator = declarator;
5254 declarator = declarator->declarator;
5257 if (declarator && declarator->kind == cdk_array)
5259 *nelts = declarator->u.array.bounds;
5260 if (*nelts == error_mark_node)
5261 *nelts = integer_one_node;
5263 if (outer_declarator)
5264 outer_declarator->declarator = declarator->declarator;
5266 new_declarator = NULL;
5269 type = groktypename (&type_specifier_seq, new_declarator);
5270 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5272 *nelts = array_type_nelts_top (type);
5273 type = TREE_TYPE (type);
5278 /* Parse an (optional) new-declarator.
5281 ptr-operator new-declarator [opt]
5282 direct-new-declarator
5284 Returns the declarator. */
5286 static cp_declarator *
5287 cp_parser_new_declarator_opt (cp_parser* parser)
5289 enum tree_code code;
5291 cp_cv_quals cv_quals;
5293 /* We don't know if there's a ptr-operator next, or not. */
5294 cp_parser_parse_tentatively (parser);
5295 /* Look for a ptr-operator. */
5296 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5297 /* If that worked, look for more new-declarators. */
5298 if (cp_parser_parse_definitely (parser))
5300 cp_declarator *declarator;
5302 /* Parse another optional declarator. */
5303 declarator = cp_parser_new_declarator_opt (parser);
5305 /* Create the representation of the declarator. */
5307 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5308 else if (code == INDIRECT_REF)
5309 declarator = make_pointer_declarator (cv_quals, declarator);
5311 declarator = make_reference_declarator (cv_quals, declarator);
5316 /* If the next token is a `[', there is a direct-new-declarator. */
5317 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5318 return cp_parser_direct_new_declarator (parser);
5323 /* Parse a direct-new-declarator.
5325 direct-new-declarator:
5327 direct-new-declarator [constant-expression]
5331 static cp_declarator *
5332 cp_parser_direct_new_declarator (cp_parser* parser)
5334 cp_declarator *declarator = NULL;
5340 /* Look for the opening `['. */
5341 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5342 /* The first expression is not required to be constant. */
5345 expression = cp_parser_expression (parser, /*cast_p=*/false);
5346 /* The standard requires that the expression have integral
5347 type. DR 74 adds enumeration types. We believe that the
5348 real intent is that these expressions be handled like the
5349 expression in a `switch' condition, which also allows
5350 classes with a single conversion to integral or
5351 enumeration type. */
5352 if (!processing_template_decl)
5355 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5360 error ("expression in new-declarator must have integral "
5361 "or enumeration type");
5362 expression = error_mark_node;
5366 /* But all the other expressions must be. */
5369 = cp_parser_constant_expression (parser,
5370 /*allow_non_constant=*/false,
5372 /* Look for the closing `]'. */
5373 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5375 /* Add this bound to the declarator. */
5376 declarator = make_array_declarator (declarator, expression);
5378 /* If the next token is not a `[', then there are no more
5380 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5387 /* Parse a new-initializer.
5390 ( expression-list [opt] )
5392 Returns a representation of the expression-list. If there is no
5393 expression-list, VOID_ZERO_NODE is returned. */
5396 cp_parser_new_initializer (cp_parser* parser)
5398 tree expression_list;
5400 expression_list = (cp_parser_parenthesized_expression_list
5401 (parser, false, /*cast_p=*/false,
5402 /*non_constant_p=*/NULL));
5403 if (!expression_list)
5404 expression_list = void_zero_node;
5406 return expression_list;
5409 /* Parse a delete-expression.
5412 :: [opt] delete cast-expression
5413 :: [opt] delete [ ] cast-expression
5415 Returns a representation of the expression. */
5418 cp_parser_delete_expression (cp_parser* parser)
5420 bool global_scope_p;
5424 /* Look for the optional `::' operator. */
5426 = (cp_parser_global_scope_opt (parser,
5427 /*current_scope_valid_p=*/false)
5429 /* Look for the `delete' keyword. */
5430 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5431 /* See if the array syntax is in use. */
5432 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5434 /* Consume the `[' token. */
5435 cp_lexer_consume_token (parser->lexer);
5436 /* Look for the `]' token. */
5437 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5438 /* Remember that this is the `[]' construct. */
5444 /* Parse the cast-expression. */
5445 expression = cp_parser_simple_cast_expression (parser);
5447 /* A delete-expression may not appear in an integral constant
5449 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5450 return error_mark_node;
5452 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5455 /* Parse a cast-expression.
5459 ( type-id ) cast-expression
5461 ADDRESS_P is true iff the unary-expression is appearing as the
5462 operand of the `&' operator. CAST_P is true if this expression is
5463 the target of a cast.
5465 Returns a representation of the expression. */
5468 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5470 /* If it's a `(', then we might be looking at a cast. */
5471 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5473 tree type = NULL_TREE;
5474 tree expr = NULL_TREE;
5475 bool compound_literal_p;
5476 const char *saved_message;
5478 /* There's no way to know yet whether or not this is a cast.
5479 For example, `(int (3))' is a unary-expression, while `(int)
5480 3' is a cast. So, we resort to parsing tentatively. */
5481 cp_parser_parse_tentatively (parser);
5482 /* Types may not be defined in a cast. */
5483 saved_message = parser->type_definition_forbidden_message;
5484 parser->type_definition_forbidden_message
5485 = "types may not be defined in casts";
5486 /* Consume the `('. */
5487 cp_lexer_consume_token (parser->lexer);
5488 /* A very tricky bit is that `(struct S) { 3 }' is a
5489 compound-literal (which we permit in C++ as an extension).
5490 But, that construct is not a cast-expression -- it is a
5491 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5492 is legal; if the compound-literal were a cast-expression,
5493 you'd need an extra set of parentheses.) But, if we parse
5494 the type-id, and it happens to be a class-specifier, then we
5495 will commit to the parse at that point, because we cannot
5496 undo the action that is done when creating a new class. So,
5497 then we cannot back up and do a postfix-expression.
5499 Therefore, we scan ahead to the closing `)', and check to see
5500 if the token after the `)' is a `{'. If so, we are not
5501 looking at a cast-expression.
5503 Save tokens so that we can put them back. */
5504 cp_lexer_save_tokens (parser->lexer);
5505 /* Skip tokens until the next token is a closing parenthesis.
5506 If we find the closing `)', and the next token is a `{', then
5507 we are looking at a compound-literal. */
5509 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5510 /*consume_paren=*/true)
5511 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5512 /* Roll back the tokens we skipped. */
5513 cp_lexer_rollback_tokens (parser->lexer);
5514 /* If we were looking at a compound-literal, simulate an error
5515 so that the call to cp_parser_parse_definitely below will
5517 if (compound_literal_p)
5518 cp_parser_simulate_error (parser);
5521 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5522 parser->in_type_id_in_expr_p = true;
5523 /* Look for the type-id. */
5524 type = cp_parser_type_id (parser);
5525 /* Look for the closing `)'. */
5526 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5527 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5530 /* Restore the saved message. */
5531 parser->type_definition_forbidden_message = saved_message;
5533 /* If ok so far, parse the dependent expression. We cannot be
5534 sure it is a cast. Consider `(T ())'. It is a parenthesized
5535 ctor of T, but looks like a cast to function returning T
5536 without a dependent expression. */
5537 if (!cp_parser_error_occurred (parser))
5538 expr = cp_parser_cast_expression (parser,
5539 /*address_p=*/false,
5542 if (cp_parser_parse_definitely (parser))
5544 /* Warn about old-style casts, if so requested. */
5545 if (warn_old_style_cast
5546 && !in_system_header
5547 && !VOID_TYPE_P (type)
5548 && current_lang_name != lang_name_c)
5549 warning (0, "use of old-style cast");
5551 /* Only type conversions to integral or enumeration types
5552 can be used in constant-expressions. */
5553 if (parser->integral_constant_expression_p
5554 && !dependent_type_p (type)
5555 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5556 && (cp_parser_non_integral_constant_expression
5558 "a cast to a type other than an integral or "
5559 "enumeration type")))
5560 return error_mark_node;
5562 /* Perform the cast. */
5563 expr = build_c_cast (type, expr);
5568 /* If we get here, then it's not a cast, so it must be a
5569 unary-expression. */
5570 return cp_parser_unary_expression (parser, address_p, cast_p);
5573 /* Parse a binary expression of the general form:
5577 pm-expression .* cast-expression
5578 pm-expression ->* cast-expression
5580 multiplicative-expression:
5582 multiplicative-expression * pm-expression
5583 multiplicative-expression / pm-expression
5584 multiplicative-expression % pm-expression
5586 additive-expression:
5587 multiplicative-expression
5588 additive-expression + multiplicative-expression
5589 additive-expression - multiplicative-expression
5593 shift-expression << additive-expression
5594 shift-expression >> additive-expression
5596 relational-expression:
5598 relational-expression < shift-expression
5599 relational-expression > shift-expression
5600 relational-expression <= shift-expression
5601 relational-expression >= shift-expression
5605 relational-expression:
5606 relational-expression <? shift-expression
5607 relational-expression >? shift-expression
5609 equality-expression:
5610 relational-expression
5611 equality-expression == relational-expression
5612 equality-expression != relational-expression
5616 and-expression & equality-expression
5618 exclusive-or-expression:
5620 exclusive-or-expression ^ and-expression
5622 inclusive-or-expression:
5623 exclusive-or-expression
5624 inclusive-or-expression | exclusive-or-expression
5626 logical-and-expression:
5627 inclusive-or-expression
5628 logical-and-expression && inclusive-or-expression
5630 logical-or-expression:
5631 logical-and-expression
5632 logical-or-expression || logical-and-expression
5634 All these are implemented with a single function like:
5637 simple-cast-expression
5638 binary-expression <token> binary-expression
5640 CAST_P is true if this expression is the target of a cast.
5642 The binops_by_token map is used to get the tree codes for each <token> type.
5643 binary-expressions are associated according to a precedence table. */
5645 #define TOKEN_PRECEDENCE(token) \
5646 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5647 ? PREC_NOT_OPERATOR \
5648 : binops_by_token[token->type].prec)
5651 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5653 cp_parser_expression_stack stack;
5654 cp_parser_expression_stack_entry *sp = &stack[0];
5657 enum tree_code tree_type;
5658 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5661 /* Parse the first expression. */
5662 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5666 /* Get an operator token. */
5667 token = cp_lexer_peek_token (parser->lexer);
5668 if (token->type == CPP_MIN || token->type == CPP_MAX)
5669 cp_parser_warn_min_max ();
5671 new_prec = TOKEN_PRECEDENCE (token);
5673 /* Popping an entry off the stack means we completed a subexpression:
5674 - either we found a token which is not an operator (`>' where it is not
5675 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5676 will happen repeatedly;
5677 - or, we found an operator which has lower priority. This is the case
5678 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5680 if (new_prec <= prec)
5689 tree_type = binops_by_token[token->type].tree_type;
5691 /* We used the operator token. */
5692 cp_lexer_consume_token (parser->lexer);
5694 /* Extract another operand. It may be the RHS of this expression
5695 or the LHS of a new, higher priority expression. */
5696 rhs = cp_parser_simple_cast_expression (parser);
5698 /* Get another operator token. Look up its precedence to avoid
5699 building a useless (immediately popped) stack entry for common
5700 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5701 token = cp_lexer_peek_token (parser->lexer);
5702 lookahead_prec = TOKEN_PRECEDENCE (token);
5703 if (lookahead_prec > new_prec)
5705 /* ... and prepare to parse the RHS of the new, higher priority
5706 expression. Since precedence levels on the stack are
5707 monotonically increasing, we do not have to care about
5710 sp->tree_type = tree_type;
5715 new_prec = lookahead_prec;
5719 /* If the stack is not empty, we have parsed into LHS the right side
5720 (`4' in the example above) of an expression we had suspended.
5721 We can use the information on the stack to recover the LHS (`3')
5722 from the stack together with the tree code (`MULT_EXPR'), and
5723 the precedence of the higher level subexpression
5724 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5725 which will be used to actually build the additive expression. */
5728 tree_type = sp->tree_type;
5733 overloaded_p = false;
5734 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5736 /* If the binary operator required the use of an overloaded operator,
5737 then this expression cannot be an integral constant-expression.
5738 An overloaded operator can be used even if both operands are
5739 otherwise permissible in an integral constant-expression if at
5740 least one of the operands is of enumeration type. */
5743 && (cp_parser_non_integral_constant_expression
5744 (parser, "calls to overloaded operators")))
5745 return error_mark_node;
5752 /* Parse the `? expression : assignment-expression' part of a
5753 conditional-expression. The LOGICAL_OR_EXPR is the
5754 logical-or-expression that started the conditional-expression.
5755 Returns a representation of the entire conditional-expression.
5757 This routine is used by cp_parser_assignment_expression.
5759 ? expression : assignment-expression
5763 ? : assignment-expression */
5766 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5769 tree assignment_expr;
5771 /* Consume the `?' token. */
5772 cp_lexer_consume_token (parser->lexer);
5773 if (cp_parser_allow_gnu_extensions_p (parser)
5774 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5775 /* Implicit true clause. */
5778 /* Parse the expression. */
5779 expr = cp_parser_expression (parser, /*cast_p=*/false);
5781 /* The next token should be a `:'. */
5782 cp_parser_require (parser, CPP_COLON, "`:'");
5783 /* Parse the assignment-expression. */
5784 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5786 /* Build the conditional-expression. */
5787 return build_x_conditional_expr (logical_or_expr,
5792 /* Parse an assignment-expression.
5794 assignment-expression:
5795 conditional-expression
5796 logical-or-expression assignment-operator assignment_expression
5799 CAST_P is true if this expression is the target of a cast.
5801 Returns a representation for the expression. */
5804 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5808 /* If the next token is the `throw' keyword, then we're looking at
5809 a throw-expression. */
5810 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5811 expr = cp_parser_throw_expression (parser);
5812 /* Otherwise, it must be that we are looking at a
5813 logical-or-expression. */
5816 /* Parse the binary expressions (logical-or-expression). */
5817 expr = cp_parser_binary_expression (parser, cast_p);
5818 /* If the next token is a `?' then we're actually looking at a
5819 conditional-expression. */
5820 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5821 return cp_parser_question_colon_clause (parser, expr);
5824 enum tree_code assignment_operator;
5826 /* If it's an assignment-operator, we're using the second
5829 = cp_parser_assignment_operator_opt (parser);
5830 if (assignment_operator != ERROR_MARK)
5834 /* Parse the right-hand side of the assignment. */
5835 rhs = cp_parser_assignment_expression (parser, cast_p);
5836 /* An assignment may not appear in a
5837 constant-expression. */
5838 if (cp_parser_non_integral_constant_expression (parser,
5840 return error_mark_node;
5841 /* Build the assignment expression. */
5842 expr = build_x_modify_expr (expr,
5843 assignment_operator,
5852 /* Parse an (optional) assignment-operator.
5854 assignment-operator: one of
5855 = *= /= %= += -= >>= <<= &= ^= |=
5859 assignment-operator: one of
5862 If the next token is an assignment operator, the corresponding tree
5863 code is returned, and the token is consumed. For example, for
5864 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5865 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5866 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5867 operator, ERROR_MARK is returned. */
5869 static enum tree_code
5870 cp_parser_assignment_operator_opt (cp_parser* parser)
5875 /* Peek at the next toen. */
5876 token = cp_lexer_peek_token (parser->lexer);
5878 switch (token->type)
5889 op = TRUNC_DIV_EXPR;
5893 op = TRUNC_MOD_EXPR;
5926 cp_parser_warn_min_max ();
5931 cp_parser_warn_min_max ();
5935 /* Nothing else is an assignment operator. */
5939 /* If it was an assignment operator, consume it. */
5940 if (op != ERROR_MARK)
5941 cp_lexer_consume_token (parser->lexer);
5946 /* Parse an expression.
5949 assignment-expression
5950 expression , assignment-expression
5952 CAST_P is true if this expression is the target of a cast.
5954 Returns a representation of the expression. */
5957 cp_parser_expression (cp_parser* parser, bool cast_p)
5959 tree expression = NULL_TREE;
5963 tree assignment_expression;
5965 /* Parse the next assignment-expression. */
5966 assignment_expression
5967 = cp_parser_assignment_expression (parser, cast_p);
5968 /* If this is the first assignment-expression, we can just
5971 expression = assignment_expression;
5973 expression = build_x_compound_expr (expression,
5974 assignment_expression);
5975 /* If the next token is not a comma, then we are done with the
5977 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5979 /* Consume the `,'. */
5980 cp_lexer_consume_token (parser->lexer);
5981 /* A comma operator cannot appear in a constant-expression. */
5982 if (cp_parser_non_integral_constant_expression (parser,
5983 "a comma operator"))
5984 expression = error_mark_node;
5990 /* Parse a constant-expression.
5992 constant-expression:
5993 conditional-expression
5995 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5996 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5997 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5998 is false, NON_CONSTANT_P should be NULL. */
6001 cp_parser_constant_expression (cp_parser* parser,
6002 bool allow_non_constant_p,
6003 bool *non_constant_p)
6005 bool saved_integral_constant_expression_p;
6006 bool saved_allow_non_integral_constant_expression_p;
6007 bool saved_non_integral_constant_expression_p;
6010 /* It might seem that we could simply parse the
6011 conditional-expression, and then check to see if it were
6012 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6013 one that the compiler can figure out is constant, possibly after
6014 doing some simplifications or optimizations. The standard has a
6015 precise definition of constant-expression, and we must honor
6016 that, even though it is somewhat more restrictive.
6022 is not a legal declaration, because `(2, 3)' is not a
6023 constant-expression. The `,' operator is forbidden in a
6024 constant-expression. However, GCC's constant-folding machinery
6025 will fold this operation to an INTEGER_CST for `3'. */
6027 /* Save the old settings. */
6028 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6029 saved_allow_non_integral_constant_expression_p
6030 = parser->allow_non_integral_constant_expression_p;
6031 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6032 /* We are now parsing a constant-expression. */
6033 parser->integral_constant_expression_p = true;
6034 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6035 parser->non_integral_constant_expression_p = false;
6036 /* Although the grammar says "conditional-expression", we parse an
6037 "assignment-expression", which also permits "throw-expression"
6038 and the use of assignment operators. In the case that
6039 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6040 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6041 actually essential that we look for an assignment-expression.
6042 For example, cp_parser_initializer_clauses uses this function to
6043 determine whether a particular assignment-expression is in fact
6045 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6046 /* Restore the old settings. */
6047 parser->integral_constant_expression_p
6048 = saved_integral_constant_expression_p;
6049 parser->allow_non_integral_constant_expression_p
6050 = saved_allow_non_integral_constant_expression_p;
6051 if (allow_non_constant_p)
6052 *non_constant_p = parser->non_integral_constant_expression_p;
6053 else if (parser->non_integral_constant_expression_p)
6054 expression = error_mark_node;
6055 parser->non_integral_constant_expression_p
6056 = saved_non_integral_constant_expression_p;
6061 /* Parse __builtin_offsetof.
6063 offsetof-expression:
6064 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6066 offsetof-member-designator:
6068 | offsetof-member-designator "." id-expression
6069 | offsetof-member-designator "[" expression "]"
6073 cp_parser_builtin_offsetof (cp_parser *parser)
6075 int save_ice_p, save_non_ice_p;
6079 /* We're about to accept non-integral-constant things, but will
6080 definitely yield an integral constant expression. Save and
6081 restore these values around our local parsing. */
6082 save_ice_p = parser->integral_constant_expression_p;
6083 save_non_ice_p = parser->non_integral_constant_expression_p;
6085 /* Consume the "__builtin_offsetof" token. */
6086 cp_lexer_consume_token (parser->lexer);
6087 /* Consume the opening `('. */
6088 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6089 /* Parse the type-id. */
6090 type = cp_parser_type_id (parser);
6091 /* Look for the `,'. */
6092 cp_parser_require (parser, CPP_COMMA, "`,'");
6094 /* Build the (type *)null that begins the traditional offsetof macro. */
6095 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6097 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6098 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6102 cp_token *token = cp_lexer_peek_token (parser->lexer);
6103 switch (token->type)
6105 case CPP_OPEN_SQUARE:
6106 /* offsetof-member-designator "[" expression "]" */
6107 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6111 /* offsetof-member-designator "." identifier */
6112 cp_lexer_consume_token (parser->lexer);
6113 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6117 case CPP_CLOSE_PAREN:
6118 /* Consume the ")" token. */
6119 cp_lexer_consume_token (parser->lexer);
6123 /* Error. We know the following require will fail, but
6124 that gives the proper error message. */
6125 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6126 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6127 expr = error_mark_node;
6133 /* If we're processing a template, we can't finish the semantics yet.
6134 Otherwise we can fold the entire expression now. */
6135 if (processing_template_decl)
6136 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6138 expr = finish_offsetof (expr);
6141 parser->integral_constant_expression_p = save_ice_p;
6142 parser->non_integral_constant_expression_p = save_non_ice_p;
6147 /* Statements [gram.stmt.stmt] */
6149 /* Parse a statement.
6153 expression-statement
6158 declaration-statement
6162 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6166 location_t statement_location;
6169 /* There is no statement yet. */
6170 statement = NULL_TREE;
6171 /* Peek at the next token. */
6172 token = cp_lexer_peek_token (parser->lexer);
6173 /* Remember the location of the first token in the statement. */
6174 statement_location = token->location;
6175 /* If this is a keyword, then that will often determine what kind of
6176 statement we have. */
6177 if (token->type == CPP_KEYWORD)
6179 enum rid keyword = token->keyword;
6185 /* Looks like a labeled-statement with a case label.
6186 Parse the label, and then use tail recursion to parse
6188 cp_parser_label_for_labeled_statement (parser);
6193 statement = cp_parser_selection_statement (parser);
6199 statement = cp_parser_iteration_statement (parser);
6206 statement = cp_parser_jump_statement (parser);
6209 /* Objective-C++ exception-handling constructs. */
6212 case RID_AT_FINALLY:
6213 case RID_AT_SYNCHRONIZED:
6215 statement = cp_parser_objc_statement (parser);
6219 statement = cp_parser_try_block (parser);
6223 /* It might be a keyword like `int' that can start a
6224 declaration-statement. */
6228 else if (token->type == CPP_NAME)
6230 /* If the next token is a `:', then we are looking at a
6231 labeled-statement. */
6232 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6233 if (token->type == CPP_COLON)
6235 /* Looks like a labeled-statement with an ordinary label.
6236 Parse the label, and then use tail recursion to parse
6238 cp_parser_label_for_labeled_statement (parser);
6242 /* Anything that starts with a `{' must be a compound-statement. */
6243 else if (token->type == CPP_OPEN_BRACE)
6244 statement = cp_parser_compound_statement (parser, NULL, false);
6245 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6246 a statement all its own. */
6247 else if (token->type == CPP_PRAGMA)
6249 cp_lexer_handle_pragma (parser->lexer);
6252 else if (token->type == CPP_EOF)
6254 cp_parser_error (parser, "expected statement");
6258 /* Everything else must be a declaration-statement or an
6259 expression-statement. Try for the declaration-statement
6260 first, unless we are looking at a `;', in which case we know that
6261 we have an expression-statement. */
6264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6266 cp_parser_parse_tentatively (parser);
6267 /* Try to parse the declaration-statement. */
6268 cp_parser_declaration_statement (parser);
6269 /* If that worked, we're done. */
6270 if (cp_parser_parse_definitely (parser))
6273 /* Look for an expression-statement instead. */
6274 statement = cp_parser_expression_statement (parser, in_statement_expr);
6277 /* Set the line number for the statement. */
6278 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6279 SET_EXPR_LOCATION (statement, statement_location);
6282 /* Parse the label for a labeled-statement, i.e.
6285 case constant-expression :
6289 case constant-expression ... constant-expression : statement
6291 When a label is parsed without errors, the label is added to the
6292 parse tree by the finish_* functions, so this function doesn't
6293 have to return the label. */
6296 cp_parser_label_for_labeled_statement (cp_parser* parser)
6300 /* The next token should be an identifier. */
6301 token = cp_lexer_peek_token (parser->lexer);
6302 if (token->type != CPP_NAME
6303 && token->type != CPP_KEYWORD)
6305 cp_parser_error (parser, "expected labeled-statement");
6309 switch (token->keyword)
6316 /* Consume the `case' token. */
6317 cp_lexer_consume_token (parser->lexer);
6318 /* Parse the constant-expression. */
6319 expr = cp_parser_constant_expression (parser,
6320 /*allow_non_constant_p=*/false,
6323 ellipsis = cp_lexer_peek_token (parser->lexer);
6324 if (ellipsis->type == CPP_ELLIPSIS)
6326 /* Consume the `...' token. */
6327 cp_lexer_consume_token (parser->lexer);
6329 cp_parser_constant_expression (parser,
6330 /*allow_non_constant_p=*/false,
6332 /* We don't need to emit warnings here, as the common code
6333 will do this for us. */
6336 expr_hi = NULL_TREE;
6338 if (!parser->in_switch_statement_p)
6339 error ("case label %qE not within a switch statement", expr);
6341 finish_case_label (expr, expr_hi);
6346 /* Consume the `default' token. */
6347 cp_lexer_consume_token (parser->lexer);
6348 if (!parser->in_switch_statement_p)
6349 error ("case label not within a switch statement");
6351 finish_case_label (NULL_TREE, NULL_TREE);
6355 /* Anything else must be an ordinary label. */
6356 finish_label_stmt (cp_parser_identifier (parser));
6360 /* Require the `:' token. */
6361 cp_parser_require (parser, CPP_COLON, "`:'");
6364 /* Parse an expression-statement.
6366 expression-statement:
6369 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6370 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6371 indicates whether this expression-statement is part of an
6372 expression statement. */
6375 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6377 tree statement = NULL_TREE;
6379 /* If the next token is a ';', then there is no expression
6381 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6382 statement = cp_parser_expression (parser, /*cast_p=*/false);
6384 /* Consume the final `;'. */
6385 cp_parser_consume_semicolon_at_end_of_statement (parser);
6387 if (in_statement_expr
6388 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6389 /* This is the final expression statement of a statement
6391 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6393 statement = finish_expr_stmt (statement);
6400 /* Parse a compound-statement.
6403 { statement-seq [opt] }
6405 Returns a tree representing the statement. */
6408 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6413 /* Consume the `{'. */
6414 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6415 return error_mark_node;
6416 /* Begin the compound-statement. */
6417 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6418 /* Parse an (optional) statement-seq. */
6419 cp_parser_statement_seq_opt (parser, in_statement_expr);
6420 /* Finish the compound-statement. */
6421 finish_compound_stmt (compound_stmt);
6422 /* Consume the `}'. */
6423 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6425 return compound_stmt;
6428 /* Parse an (optional) statement-seq.
6432 statement-seq [opt] statement */
6435 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6437 /* Scan statements until there aren't any more. */
6440 /* If we're looking at a `}', then we've run out of statements. */
6441 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6442 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6445 /* Parse the statement. */
6446 cp_parser_statement (parser, in_statement_expr);
6450 /* Parse a selection-statement.
6452 selection-statement:
6453 if ( condition ) statement
6454 if ( condition ) statement else statement
6455 switch ( condition ) statement
6457 Returns the new IF_STMT or SWITCH_STMT. */
6460 cp_parser_selection_statement (cp_parser* parser)
6465 /* Peek at the next token. */
6466 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6468 /* See what kind of keyword it is. */
6469 keyword = token->keyword;
6478 /* Look for the `('. */
6479 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6481 cp_parser_skip_to_end_of_statement (parser);
6482 return error_mark_node;
6485 /* Begin the selection-statement. */
6486 if (keyword == RID_IF)
6487 statement = begin_if_stmt ();
6489 statement = begin_switch_stmt ();
6491 /* Parse the condition. */
6492 condition = cp_parser_condition (parser);
6493 /* Look for the `)'. */
6494 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6495 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6496 /*consume_paren=*/true);
6498 if (keyword == RID_IF)
6500 /* Add the condition. */
6501 finish_if_stmt_cond (condition, statement);
6503 /* Parse the then-clause. */
6504 cp_parser_implicitly_scoped_statement (parser);
6505 finish_then_clause (statement);
6507 /* If the next token is `else', parse the else-clause. */
6508 if (cp_lexer_next_token_is_keyword (parser->lexer,
6511 /* Consume the `else' keyword. */
6512 cp_lexer_consume_token (parser->lexer);
6513 begin_else_clause (statement);
6514 /* Parse the else-clause. */
6515 cp_parser_implicitly_scoped_statement (parser);
6516 finish_else_clause (statement);
6519 /* Now we're all done with the if-statement. */
6520 finish_if_stmt (statement);
6524 bool in_switch_statement_p;
6526 /* Add the condition. */
6527 finish_switch_cond (condition, statement);
6529 /* Parse the body of the switch-statement. */
6530 in_switch_statement_p = parser->in_switch_statement_p;
6531 parser->in_switch_statement_p = true;
6532 cp_parser_implicitly_scoped_statement (parser);
6533 parser->in_switch_statement_p = in_switch_statement_p;
6535 /* Now we're all done with the switch-statement. */
6536 finish_switch_stmt (statement);
6544 cp_parser_error (parser, "expected selection-statement");
6545 return error_mark_node;
6549 /* Parse a condition.
6553 type-specifier-seq declarator = assignment-expression
6558 type-specifier-seq declarator asm-specification [opt]
6559 attributes [opt] = assignment-expression
6561 Returns the expression that should be tested. */
6564 cp_parser_condition (cp_parser* parser)
6566 cp_decl_specifier_seq type_specifiers;
6567 const char *saved_message;
6569 /* Try the declaration first. */
6570 cp_parser_parse_tentatively (parser);
6571 /* New types are not allowed in the type-specifier-seq for a
6573 saved_message = parser->type_definition_forbidden_message;
6574 parser->type_definition_forbidden_message
6575 = "types may not be defined in conditions";
6576 /* Parse the type-specifier-seq. */
6577 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6579 /* Restore the saved message. */
6580 parser->type_definition_forbidden_message = saved_message;
6581 /* If all is well, we might be looking at a declaration. */
6582 if (!cp_parser_error_occurred (parser))
6585 tree asm_specification;
6587 cp_declarator *declarator;
6588 tree initializer = NULL_TREE;
6590 /* Parse the declarator. */
6591 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6592 /*ctor_dtor_or_conv_p=*/NULL,
6593 /*parenthesized_p=*/NULL,
6594 /*member_p=*/false);
6595 /* Parse the attributes. */
6596 attributes = cp_parser_attributes_opt (parser);
6597 /* Parse the asm-specification. */
6598 asm_specification = cp_parser_asm_specification_opt (parser);
6599 /* If the next token is not an `=', then we might still be
6600 looking at an expression. For example:
6604 looks like a decl-specifier-seq and a declarator -- but then
6605 there is no `=', so this is an expression. */
6606 cp_parser_require (parser, CPP_EQ, "`='");
6607 /* If we did see an `=', then we are looking at a declaration
6609 if (cp_parser_parse_definitely (parser))
6612 bool non_constant_p;
6614 /* Create the declaration. */
6615 decl = start_decl (declarator, &type_specifiers,
6616 /*initialized_p=*/true,
6617 attributes, /*prefix_attributes=*/NULL_TREE,
6619 /* Parse the assignment-expression. */
6621 = cp_parser_constant_expression (parser,
6622 /*allow_non_constant_p=*/true,
6624 if (!non_constant_p)
6625 initializer = fold_non_dependent_expr (initializer);
6627 /* Process the initializer. */
6628 cp_finish_decl (decl,
6629 initializer, !non_constant_p,
6631 LOOKUP_ONLYCONVERTING);
6634 pop_scope (pushed_scope);
6636 return convert_from_reference (decl);
6639 /* If we didn't even get past the declarator successfully, we are
6640 definitely not looking at a declaration. */
6642 cp_parser_abort_tentative_parse (parser);
6644 /* Otherwise, we are looking at an expression. */
6645 return cp_parser_expression (parser, /*cast_p=*/false);
6648 /* Parse an iteration-statement.
6650 iteration-statement:
6651 while ( condition ) statement
6652 do statement while ( expression ) ;
6653 for ( for-init-statement condition [opt] ; expression [opt] )
6656 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6659 cp_parser_iteration_statement (cp_parser* parser)
6664 bool in_iteration_statement_p;
6667 /* Peek at the next token. */
6668 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6670 return error_mark_node;
6672 /* Remember whether or not we are already within an iteration
6674 in_iteration_statement_p = parser->in_iteration_statement_p;
6676 /* See what kind of keyword it is. */
6677 keyword = token->keyword;
6684 /* Begin the while-statement. */
6685 statement = begin_while_stmt ();
6686 /* Look for the `('. */
6687 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6688 /* Parse the condition. */
6689 condition = cp_parser_condition (parser);
6690 finish_while_stmt_cond (condition, statement);
6691 /* Look for the `)'. */
6692 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6693 /* Parse the dependent statement. */
6694 parser->in_iteration_statement_p = true;
6695 cp_parser_already_scoped_statement (parser);
6696 parser->in_iteration_statement_p = in_iteration_statement_p;
6697 /* We're done with the while-statement. */
6698 finish_while_stmt (statement);
6706 /* Begin the do-statement. */
6707 statement = begin_do_stmt ();
6708 /* Parse the body of the do-statement. */
6709 parser->in_iteration_statement_p = true;
6710 cp_parser_implicitly_scoped_statement (parser);
6711 parser->in_iteration_statement_p = in_iteration_statement_p;
6712 finish_do_body (statement);
6713 /* Look for the `while' keyword. */
6714 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6715 /* Look for the `('. */
6716 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6717 /* Parse the expression. */
6718 expression = cp_parser_expression (parser, /*cast_p=*/false);
6719 /* We're done with the do-statement. */
6720 finish_do_stmt (expression, statement);
6721 /* Look for the `)'. */
6722 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6723 /* Look for the `;'. */
6724 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6730 tree condition = NULL_TREE;
6731 tree expression = NULL_TREE;
6733 /* Begin the for-statement. */
6734 statement = begin_for_stmt ();
6735 /* Look for the `('. */
6736 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6737 /* Parse the initialization. */
6738 cp_parser_for_init_statement (parser);
6739 finish_for_init_stmt (statement);
6741 /* If there's a condition, process it. */
6742 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6743 condition = cp_parser_condition (parser);
6744 finish_for_cond (condition, statement);
6745 /* Look for the `;'. */
6746 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6748 /* If there's an expression, process it. */
6749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6750 expression = cp_parser_expression (parser, /*cast_p=*/false);
6751 finish_for_expr (expression, statement);
6752 /* Look for the `)'. */
6753 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6755 /* Parse the body of the for-statement. */
6756 parser->in_iteration_statement_p = true;
6757 cp_parser_already_scoped_statement (parser);
6758 parser->in_iteration_statement_p = in_iteration_statement_p;
6760 /* We're done with the for-statement. */
6761 finish_for_stmt (statement);
6766 cp_parser_error (parser, "expected iteration-statement");
6767 statement = error_mark_node;
6774 /* Parse a for-init-statement.
6777 expression-statement
6778 simple-declaration */
6781 cp_parser_for_init_statement (cp_parser* parser)
6783 /* If the next token is a `;', then we have an empty
6784 expression-statement. Grammatically, this is also a
6785 simple-declaration, but an invalid one, because it does not
6786 declare anything. Therefore, if we did not handle this case
6787 specially, we would issue an error message about an invalid
6789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6791 /* We're going to speculatively look for a declaration, falling back
6792 to an expression, if necessary. */
6793 cp_parser_parse_tentatively (parser);
6794 /* Parse the declaration. */
6795 cp_parser_simple_declaration (parser,
6796 /*function_definition_allowed_p=*/false);
6797 /* If the tentative parse failed, then we shall need to look for an
6798 expression-statement. */
6799 if (cp_parser_parse_definitely (parser))
6803 cp_parser_expression_statement (parser, false);
6806 /* Parse a jump-statement.
6811 return expression [opt] ;
6819 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6822 cp_parser_jump_statement (cp_parser* parser)
6824 tree statement = error_mark_node;
6828 /* Peek at the next token. */
6829 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6831 return error_mark_node;
6833 /* See what kind of keyword it is. */
6834 keyword = token->keyword;
6838 if (!parser->in_switch_statement_p
6839 && !parser->in_iteration_statement_p)
6841 error ("break statement not within loop or switch");
6842 statement = error_mark_node;
6845 statement = finish_break_stmt ();
6846 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6850 if (!parser->in_iteration_statement_p)
6852 error ("continue statement not within a loop");
6853 statement = error_mark_node;
6856 statement = finish_continue_stmt ();
6857 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6864 /* If the next token is a `;', then there is no
6866 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6867 expr = cp_parser_expression (parser, /*cast_p=*/false);
6870 /* Build the return-statement. */
6871 statement = finish_return_stmt (expr);
6872 /* Look for the final `;'. */
6873 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6878 /* Create the goto-statement. */
6879 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6881 /* Issue a warning about this use of a GNU extension. */
6883 pedwarn ("ISO C++ forbids computed gotos");
6884 /* Consume the '*' token. */
6885 cp_lexer_consume_token (parser->lexer);
6886 /* Parse the dependent expression. */
6887 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6890 finish_goto_stmt (cp_parser_identifier (parser));
6891 /* Look for the final `;'. */
6892 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6896 cp_parser_error (parser, "expected jump-statement");
6903 /* Parse a declaration-statement.
6905 declaration-statement:
6906 block-declaration */
6909 cp_parser_declaration_statement (cp_parser* parser)
6913 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6914 p = obstack_alloc (&declarator_obstack, 0);
6916 /* Parse the block-declaration. */
6917 cp_parser_block_declaration (parser, /*statement_p=*/true);
6919 /* Free any declarators allocated. */
6920 obstack_free (&declarator_obstack, p);
6922 /* Finish off the statement. */
6926 /* Some dependent statements (like `if (cond) statement'), are
6927 implicitly in their own scope. In other words, if the statement is
6928 a single statement (as opposed to a compound-statement), it is
6929 none-the-less treated as if it were enclosed in braces. Any
6930 declarations appearing in the dependent statement are out of scope
6931 after control passes that point. This function parses a statement,
6932 but ensures that is in its own scope, even if it is not a
6935 Returns the new statement. */
6938 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6942 /* If the token is not a `{', then we must take special action. */
6943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6945 /* Create a compound-statement. */
6946 statement = begin_compound_stmt (0);
6947 /* Parse the dependent-statement. */
6948 cp_parser_statement (parser, false);
6949 /* Finish the dummy compound-statement. */
6950 finish_compound_stmt (statement);
6952 /* Otherwise, we simply parse the statement directly. */
6954 statement = cp_parser_compound_statement (parser, NULL, false);
6956 /* Return the statement. */
6960 /* For some dependent statements (like `while (cond) statement'), we
6961 have already created a scope. Therefore, even if the dependent
6962 statement is a compound-statement, we do not want to create another
6966 cp_parser_already_scoped_statement (cp_parser* parser)
6968 /* If the token is a `{', then we must take special action. */
6969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6970 cp_parser_statement (parser, false);
6973 /* Avoid calling cp_parser_compound_statement, so that we
6974 don't create a new scope. Do everything else by hand. */
6975 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6976 cp_parser_statement_seq_opt (parser, false);
6977 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6981 /* Declarations [gram.dcl.dcl] */
6983 /* Parse an optional declaration-sequence.
6987 declaration-seq declaration */
6990 cp_parser_declaration_seq_opt (cp_parser* parser)
6996 token = cp_lexer_peek_token (parser->lexer);
6998 if (token->type == CPP_CLOSE_BRACE
6999 || token->type == CPP_EOF)
7002 if (token->type == CPP_SEMICOLON)
7004 /* A declaration consisting of a single semicolon is
7005 invalid. Allow it unless we're being pedantic. */
7006 cp_lexer_consume_token (parser->lexer);
7007 if (pedantic && !in_system_header)
7008 pedwarn ("extra %<;%>");
7012 /* If we're entering or exiting a region that's implicitly
7013 extern "C", modify the lang context appropriately. */
7014 if (!parser->implicit_extern_c && token->implicit_extern_c)
7016 push_lang_context (lang_name_c);
7017 parser->implicit_extern_c = true;
7019 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7021 pop_lang_context ();
7022 parser->implicit_extern_c = false;
7025 if (token->type == CPP_PRAGMA)
7027 /* A top-level declaration can consist solely of a #pragma.
7028 A nested declaration cannot, so this is done here and not
7029 in cp_parser_declaration. (A #pragma at block scope is
7030 handled in cp_parser_statement.) */
7031 cp_lexer_handle_pragma (parser->lexer);
7035 /* Parse the declaration itself. */
7036 cp_parser_declaration (parser);
7040 /* Parse a declaration.
7045 template-declaration
7046 explicit-instantiation
7047 explicit-specialization
7048 linkage-specification
7049 namespace-definition
7054 __extension__ declaration */
7057 cp_parser_declaration (cp_parser* parser)
7064 /* Check for the `__extension__' keyword. */
7065 if (cp_parser_extension_opt (parser, &saved_pedantic))
7067 /* Parse the qualified declaration. */
7068 cp_parser_declaration (parser);
7069 /* Restore the PEDANTIC flag. */
7070 pedantic = saved_pedantic;
7075 /* Try to figure out what kind of declaration is present. */
7076 token1 = *cp_lexer_peek_token (parser->lexer);
7078 if (token1.type != CPP_EOF)
7079 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7081 token2.type = token2.keyword = RID_MAX;
7083 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7084 p = obstack_alloc (&declarator_obstack, 0);
7086 /* If the next token is `extern' and the following token is a string
7087 literal, then we have a linkage specification. */
7088 if (token1.keyword == RID_EXTERN
7089 && cp_parser_is_string_literal (&token2))
7090 cp_parser_linkage_specification (parser);
7091 /* If the next token is `template', then we have either a template
7092 declaration, an explicit instantiation, or an explicit
7094 else if (token1.keyword == RID_TEMPLATE)
7096 /* `template <>' indicates a template specialization. */
7097 if (token2.type == CPP_LESS
7098 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7099 cp_parser_explicit_specialization (parser);
7100 /* `template <' indicates a template declaration. */
7101 else if (token2.type == CPP_LESS)
7102 cp_parser_template_declaration (parser, /*member_p=*/false);
7103 /* Anything else must be an explicit instantiation. */
7105 cp_parser_explicit_instantiation (parser);
7107 /* If the next token is `export', then we have a template
7109 else if (token1.keyword == RID_EXPORT)
7110 cp_parser_template_declaration (parser, /*member_p=*/false);
7111 /* If the next token is `extern', 'static' or 'inline' and the one
7112 after that is `template', we have a GNU extended explicit
7113 instantiation directive. */
7114 else if (cp_parser_allow_gnu_extensions_p (parser)
7115 && (token1.keyword == RID_EXTERN
7116 || token1.keyword == RID_STATIC
7117 || token1.keyword == RID_INLINE)
7118 && token2.keyword == RID_TEMPLATE)
7119 cp_parser_explicit_instantiation (parser);
7120 /* If the next token is `namespace', check for a named or unnamed
7121 namespace definition. */
7122 else if (token1.keyword == RID_NAMESPACE
7123 && (/* A named namespace definition. */
7124 (token2.type == CPP_NAME
7125 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7127 /* An unnamed namespace definition. */
7128 || token2.type == CPP_OPEN_BRACE))
7129 cp_parser_namespace_definition (parser);
7130 /* Objective-C++ declaration/definition. */
7131 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7132 cp_parser_objc_declaration (parser);
7133 /* We must have either a block declaration or a function
7136 /* Try to parse a block-declaration, or a function-definition. */
7137 cp_parser_block_declaration (parser, /*statement_p=*/false);
7139 /* Free any declarators allocated. */
7140 obstack_free (&declarator_obstack, p);
7143 /* Parse a block-declaration.
7148 namespace-alias-definition
7155 __extension__ block-declaration
7158 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7159 part of a declaration-statement. */
7162 cp_parser_block_declaration (cp_parser *parser,
7168 /* Check for the `__extension__' keyword. */
7169 if (cp_parser_extension_opt (parser, &saved_pedantic))
7171 /* Parse the qualified declaration. */
7172 cp_parser_block_declaration (parser, statement_p);
7173 /* Restore the PEDANTIC flag. */
7174 pedantic = saved_pedantic;
7179 /* Peek at the next token to figure out which kind of declaration is
7181 token1 = cp_lexer_peek_token (parser->lexer);
7183 /* If the next keyword is `asm', we have an asm-definition. */
7184 if (token1->keyword == RID_ASM)
7187 cp_parser_commit_to_tentative_parse (parser);
7188 cp_parser_asm_definition (parser);
7190 /* If the next keyword is `namespace', we have a
7191 namespace-alias-definition. */
7192 else if (token1->keyword == RID_NAMESPACE)
7193 cp_parser_namespace_alias_definition (parser);
7194 /* If the next keyword is `using', we have either a
7195 using-declaration or a using-directive. */
7196 else if (token1->keyword == RID_USING)
7201 cp_parser_commit_to_tentative_parse (parser);
7202 /* If the token after `using' is `namespace', then we have a
7204 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7205 if (token2->keyword == RID_NAMESPACE)
7206 cp_parser_using_directive (parser);
7207 /* Otherwise, it's a using-declaration. */
7209 cp_parser_using_declaration (parser,
7210 /*access_declaration_p=*/false);
7212 /* If the next keyword is `__label__' we have a label declaration. */
7213 else if (token1->keyword == RID_LABEL)
7216 cp_parser_commit_to_tentative_parse (parser);
7217 cp_parser_label_declaration (parser);
7219 /* Anything else must be a simple-declaration. */
7221 cp_parser_simple_declaration (parser, !statement_p);
7224 /* Parse a simple-declaration.
7227 decl-specifier-seq [opt] init-declarator-list [opt] ;
7229 init-declarator-list:
7231 init-declarator-list , init-declarator
7233 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7234 function-definition as a simple-declaration. */
7237 cp_parser_simple_declaration (cp_parser* parser,
7238 bool function_definition_allowed_p)
7240 cp_decl_specifier_seq decl_specifiers;
7241 int declares_class_or_enum;
7242 bool saw_declarator;
7244 /* Defer access checks until we know what is being declared; the
7245 checks for names appearing in the decl-specifier-seq should be
7246 done as if we were in the scope of the thing being declared. */
7247 push_deferring_access_checks (dk_deferred);
7249 /* Parse the decl-specifier-seq. We have to keep track of whether
7250 or not the decl-specifier-seq declares a named class or
7251 enumeration type, since that is the only case in which the
7252 init-declarator-list is allowed to be empty.
7256 In a simple-declaration, the optional init-declarator-list can be
7257 omitted only when declaring a class or enumeration, that is when
7258 the decl-specifier-seq contains either a class-specifier, an
7259 elaborated-type-specifier, or an enum-specifier. */
7260 cp_parser_decl_specifier_seq (parser,
7261 CP_PARSER_FLAGS_OPTIONAL,
7263 &declares_class_or_enum);
7264 /* We no longer need to defer access checks. */
7265 stop_deferring_access_checks ();
7267 /* In a block scope, a valid declaration must always have a
7268 decl-specifier-seq. By not trying to parse declarators, we can
7269 resolve the declaration/expression ambiguity more quickly. */
7270 if (!function_definition_allowed_p
7271 && !decl_specifiers.any_specifiers_p)
7273 cp_parser_error (parser, "expected declaration");
7277 /* If the next two tokens are both identifiers, the code is
7278 erroneous. The usual cause of this situation is code like:
7282 where "T" should name a type -- but does not. */
7283 if (!decl_specifiers.type
7284 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7286 /* If parsing tentatively, we should commit; we really are
7287 looking at a declaration. */
7288 cp_parser_commit_to_tentative_parse (parser);
7293 /* If we have seen at least one decl-specifier, and the next token
7294 is not a parenthesis, then we must be looking at a declaration.
7295 (After "int (" we might be looking at a functional cast.) */
7296 if (decl_specifiers.any_specifiers_p
7297 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7298 cp_parser_commit_to_tentative_parse (parser);
7300 /* Keep going until we hit the `;' at the end of the simple
7302 saw_declarator = false;
7303 while (cp_lexer_next_token_is_not (parser->lexer,
7307 bool function_definition_p;
7312 /* If we are processing next declarator, coma is expected */
7313 token = cp_lexer_peek_token (parser->lexer);
7314 gcc_assert (token->type == CPP_COMMA);
7315 cp_lexer_consume_token (parser->lexer);
7318 saw_declarator = true;
7320 /* Parse the init-declarator. */
7321 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7322 /*checks=*/NULL_TREE,
7323 function_definition_allowed_p,
7325 declares_class_or_enum,
7326 &function_definition_p);
7327 /* If an error occurred while parsing tentatively, exit quickly.
7328 (That usually happens when in the body of a function; each
7329 statement is treated as a declaration-statement until proven
7331 if (cp_parser_error_occurred (parser))
7333 /* Handle function definitions specially. */
7334 if (function_definition_p)
7336 /* If the next token is a `,', then we are probably
7337 processing something like:
7341 which is erroneous. */
7342 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7343 error ("mixing declarations and function-definitions is forbidden");
7344 /* Otherwise, we're done with the list of declarators. */
7347 pop_deferring_access_checks ();
7351 /* The next token should be either a `,' or a `;'. */
7352 token = cp_lexer_peek_token (parser->lexer);
7353 /* If it's a `,', there are more declarators to come. */
7354 if (token->type == CPP_COMMA)
7355 /* will be consumed next time around */;
7356 /* If it's a `;', we are done. */
7357 else if (token->type == CPP_SEMICOLON)
7359 /* Anything else is an error. */
7362 /* If we have already issued an error message we don't need
7363 to issue another one. */
7364 if (decl != error_mark_node
7365 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7366 cp_parser_error (parser, "expected %<,%> or %<;%>");
7367 /* Skip tokens until we reach the end of the statement. */
7368 cp_parser_skip_to_end_of_statement (parser);
7369 /* If the next token is now a `;', consume it. */
7370 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7371 cp_lexer_consume_token (parser->lexer);
7374 /* After the first time around, a function-definition is not
7375 allowed -- even if it was OK at first. For example:
7380 function_definition_allowed_p = false;
7383 /* Issue an error message if no declarators are present, and the
7384 decl-specifier-seq does not itself declare a class or
7386 if (!saw_declarator)
7388 if (cp_parser_declares_only_class_p (parser))
7389 shadow_tag (&decl_specifiers);
7390 /* Perform any deferred access checks. */
7391 perform_deferred_access_checks ();
7394 /* Consume the `;'. */
7395 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7398 pop_deferring_access_checks ();
7401 /* Parse a decl-specifier-seq.
7404 decl-specifier-seq [opt] decl-specifier
7407 storage-class-specifier
7418 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7420 The parser flags FLAGS is used to control type-specifier parsing.
7422 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7425 1: one of the decl-specifiers is an elaborated-type-specifier
7426 (i.e., a type declaration)
7427 2: one of the decl-specifiers is an enum-specifier or a
7428 class-specifier (i.e., a type definition)
7433 cp_parser_decl_specifier_seq (cp_parser* parser,
7434 cp_parser_flags flags,
7435 cp_decl_specifier_seq *decl_specs,
7436 int* declares_class_or_enum)
7438 bool constructor_possible_p = !parser->in_declarator_p;
7440 /* Clear DECL_SPECS. */
7441 clear_decl_specs (decl_specs);
7443 /* Assume no class or enumeration type is declared. */
7444 *declares_class_or_enum = 0;
7446 /* Keep reading specifiers until there are no more to read. */
7450 bool found_decl_spec;
7453 /* Peek at the next token. */
7454 token = cp_lexer_peek_token (parser->lexer);
7455 /* Handle attributes. */
7456 if (token->keyword == RID_ATTRIBUTE)
7458 /* Parse the attributes. */
7459 decl_specs->attributes
7460 = chainon (decl_specs->attributes,
7461 cp_parser_attributes_opt (parser));
7464 /* Assume we will find a decl-specifier keyword. */
7465 found_decl_spec = true;
7466 /* If the next token is an appropriate keyword, we can simply
7467 add it to the list. */
7468 switch (token->keyword)
7473 ++decl_specs->specs[(int) ds_friend];
7474 /* Consume the token. */
7475 cp_lexer_consume_token (parser->lexer);
7478 /* function-specifier:
7485 cp_parser_function_specifier_opt (parser, decl_specs);
7491 ++decl_specs->specs[(int) ds_typedef];
7492 /* Consume the token. */
7493 cp_lexer_consume_token (parser->lexer);
7494 /* A constructor declarator cannot appear in a typedef. */
7495 constructor_possible_p = false;
7496 /* The "typedef" keyword can only occur in a declaration; we
7497 may as well commit at this point. */
7498 cp_parser_commit_to_tentative_parse (parser);
7501 /* storage-class-specifier:
7515 /* Consume the token. */
7516 cp_lexer_consume_token (parser->lexer);
7517 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7520 /* Consume the token. */
7521 cp_lexer_consume_token (parser->lexer);
7522 ++decl_specs->specs[(int) ds_thread];
7526 /* We did not yet find a decl-specifier yet. */
7527 found_decl_spec = false;
7531 /* Constructors are a special case. The `S' in `S()' is not a
7532 decl-specifier; it is the beginning of the declarator. */
7535 && constructor_possible_p
7536 && (cp_parser_constructor_declarator_p
7537 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7539 /* If we don't have a DECL_SPEC yet, then we must be looking at
7540 a type-specifier. */
7541 if (!found_decl_spec && !constructor_p)
7543 int decl_spec_declares_class_or_enum;
7544 bool is_cv_qualifier;
7548 = cp_parser_type_specifier (parser, flags,
7550 /*is_declaration=*/true,
7551 &decl_spec_declares_class_or_enum,
7554 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7556 /* If this type-specifier referenced a user-defined type
7557 (a typedef, class-name, etc.), then we can't allow any
7558 more such type-specifiers henceforth.
7562 The longest sequence of decl-specifiers that could
7563 possibly be a type name is taken as the
7564 decl-specifier-seq of a declaration. The sequence shall
7565 be self-consistent as described below.
7569 As a general rule, at most one type-specifier is allowed
7570 in the complete decl-specifier-seq of a declaration. The
7571 only exceptions are the following:
7573 -- const or volatile can be combined with any other
7576 -- signed or unsigned can be combined with char, long,
7584 void g (const int Pc);
7586 Here, Pc is *not* part of the decl-specifier seq; it's
7587 the declarator. Therefore, once we see a type-specifier
7588 (other than a cv-qualifier), we forbid any additional
7589 user-defined types. We *do* still allow things like `int
7590 int' to be considered a decl-specifier-seq, and issue the
7591 error message later. */
7592 if (type_spec && !is_cv_qualifier)
7593 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7594 /* A constructor declarator cannot follow a type-specifier. */
7597 constructor_possible_p = false;
7598 found_decl_spec = true;
7602 /* If we still do not have a DECL_SPEC, then there are no more
7604 if (!found_decl_spec)
7607 decl_specs->any_specifiers_p = true;
7608 /* After we see one decl-specifier, further decl-specifiers are
7610 flags |= CP_PARSER_FLAGS_OPTIONAL;
7613 cp_parser_check_decl_spec (decl_specs);
7615 /* Don't allow a friend specifier with a class definition. */
7616 if (decl_specs->specs[(int) ds_friend] != 0
7617 && (*declares_class_or_enum & 2))
7618 error ("class definition may not be declared a friend");
7621 /* Parse an (optional) storage-class-specifier.
7623 storage-class-specifier:
7632 storage-class-specifier:
7635 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7638 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7640 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7648 /* Consume the token. */
7649 return cp_lexer_consume_token (parser->lexer)->value;
7656 /* Parse an (optional) function-specifier.
7663 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7664 Updates DECL_SPECS, if it is non-NULL. */
7667 cp_parser_function_specifier_opt (cp_parser* parser,
7668 cp_decl_specifier_seq *decl_specs)
7670 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7674 ++decl_specs->specs[(int) ds_inline];
7679 ++decl_specs->specs[(int) ds_virtual];
7684 ++decl_specs->specs[(int) ds_explicit];
7691 /* Consume the token. */
7692 return cp_lexer_consume_token (parser->lexer)->value;
7695 /* Parse a linkage-specification.
7697 linkage-specification:
7698 extern string-literal { declaration-seq [opt] }
7699 extern string-literal declaration */
7702 cp_parser_linkage_specification (cp_parser* parser)
7706 /* Look for the `extern' keyword. */
7707 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7709 /* Look for the string-literal. */
7710 linkage = cp_parser_string_literal (parser, false, false);
7712 /* Transform the literal into an identifier. If the literal is a
7713 wide-character string, or contains embedded NULs, then we can't
7714 handle it as the user wants. */
7715 if (strlen (TREE_STRING_POINTER (linkage))
7716 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7718 cp_parser_error (parser, "invalid linkage-specification");
7719 /* Assume C++ linkage. */
7720 linkage = lang_name_cplusplus;
7723 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7725 /* We're now using the new linkage. */
7726 push_lang_context (linkage);
7728 /* If the next token is a `{', then we're using the first
7730 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7732 /* Consume the `{' token. */
7733 cp_lexer_consume_token (parser->lexer);
7734 /* Parse the declarations. */
7735 cp_parser_declaration_seq_opt (parser);
7736 /* Look for the closing `}'. */
7737 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7739 /* Otherwise, there's just one declaration. */
7742 bool saved_in_unbraced_linkage_specification_p;
7744 saved_in_unbraced_linkage_specification_p
7745 = parser->in_unbraced_linkage_specification_p;
7746 parser->in_unbraced_linkage_specification_p = true;
7747 cp_parser_declaration (parser);
7748 parser->in_unbraced_linkage_specification_p
7749 = saved_in_unbraced_linkage_specification_p;
7752 /* We're done with the linkage-specification. */
7753 pop_lang_context ();
7756 /* Special member functions [gram.special] */
7758 /* Parse a conversion-function-id.
7760 conversion-function-id:
7761 operator conversion-type-id
7763 Returns an IDENTIFIER_NODE representing the operator. */
7766 cp_parser_conversion_function_id (cp_parser* parser)
7770 tree saved_qualifying_scope;
7771 tree saved_object_scope;
7772 tree pushed_scope = NULL_TREE;
7774 /* Look for the `operator' token. */
7775 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7776 return error_mark_node;
7777 /* When we parse the conversion-type-id, the current scope will be
7778 reset. However, we need that information in able to look up the
7779 conversion function later, so we save it here. */
7780 saved_scope = parser->scope;
7781 saved_qualifying_scope = parser->qualifying_scope;
7782 saved_object_scope = parser->object_scope;
7783 /* We must enter the scope of the class so that the names of
7784 entities declared within the class are available in the
7785 conversion-type-id. For example, consider:
7792 S::operator I() { ... }
7794 In order to see that `I' is a type-name in the definition, we
7795 must be in the scope of `S'. */
7797 pushed_scope = push_scope (saved_scope);
7798 /* Parse the conversion-type-id. */
7799 type = cp_parser_conversion_type_id (parser);
7800 /* Leave the scope of the class, if any. */
7802 pop_scope (pushed_scope);
7803 /* Restore the saved scope. */
7804 parser->scope = saved_scope;
7805 parser->qualifying_scope = saved_qualifying_scope;
7806 parser->object_scope = saved_object_scope;
7807 /* If the TYPE is invalid, indicate failure. */
7808 if (type == error_mark_node)
7809 return error_mark_node;
7810 return mangle_conv_op_name_for_type (type);
7813 /* Parse a conversion-type-id:
7816 type-specifier-seq conversion-declarator [opt]
7818 Returns the TYPE specified. */
7821 cp_parser_conversion_type_id (cp_parser* parser)
7824 cp_decl_specifier_seq type_specifiers;
7825 cp_declarator *declarator;
7826 tree type_specified;
7828 /* Parse the attributes. */
7829 attributes = cp_parser_attributes_opt (parser);
7830 /* Parse the type-specifiers. */
7831 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7833 /* If that didn't work, stop. */
7834 if (type_specifiers.type == error_mark_node)
7835 return error_mark_node;
7836 /* Parse the conversion-declarator. */
7837 declarator = cp_parser_conversion_declarator_opt (parser);
7839 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7840 /*initialized=*/0, &attributes);
7842 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7843 return type_specified;
7846 /* Parse an (optional) conversion-declarator.
7848 conversion-declarator:
7849 ptr-operator conversion-declarator [opt]
7853 static cp_declarator *
7854 cp_parser_conversion_declarator_opt (cp_parser* parser)
7856 enum tree_code code;
7858 cp_cv_quals cv_quals;
7860 /* We don't know if there's a ptr-operator next, or not. */
7861 cp_parser_parse_tentatively (parser);
7862 /* Try the ptr-operator. */
7863 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7864 /* If it worked, look for more conversion-declarators. */
7865 if (cp_parser_parse_definitely (parser))
7867 cp_declarator *declarator;
7869 /* Parse another optional declarator. */
7870 declarator = cp_parser_conversion_declarator_opt (parser);
7872 /* Create the representation of the declarator. */
7874 declarator = make_ptrmem_declarator (cv_quals, class_type,
7876 else if (code == INDIRECT_REF)
7877 declarator = make_pointer_declarator (cv_quals, declarator);
7879 declarator = make_reference_declarator (cv_quals, declarator);
7887 /* Parse an (optional) ctor-initializer.
7890 : mem-initializer-list
7892 Returns TRUE iff the ctor-initializer was actually present. */
7895 cp_parser_ctor_initializer_opt (cp_parser* parser)
7897 /* If the next token is not a `:', then there is no
7898 ctor-initializer. */
7899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7901 /* Do default initialization of any bases and members. */
7902 if (DECL_CONSTRUCTOR_P (current_function_decl))
7903 finish_mem_initializers (NULL_TREE);
7908 /* Consume the `:' token. */
7909 cp_lexer_consume_token (parser->lexer);
7910 /* And the mem-initializer-list. */
7911 cp_parser_mem_initializer_list (parser);
7916 /* Parse a mem-initializer-list.
7918 mem-initializer-list:
7920 mem-initializer , mem-initializer-list */
7923 cp_parser_mem_initializer_list (cp_parser* parser)
7925 tree mem_initializer_list = NULL_TREE;
7927 /* Let the semantic analysis code know that we are starting the
7928 mem-initializer-list. */
7929 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7930 error ("only constructors take base initializers");
7932 /* Loop through the list. */
7935 tree mem_initializer;
7937 /* Parse the mem-initializer. */
7938 mem_initializer = cp_parser_mem_initializer (parser);
7939 /* Add it to the list, unless it was erroneous. */
7940 if (mem_initializer != error_mark_node)
7942 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7943 mem_initializer_list = mem_initializer;
7945 /* If the next token is not a `,', we're done. */
7946 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7948 /* Consume the `,' token. */
7949 cp_lexer_consume_token (parser->lexer);
7952 /* Perform semantic analysis. */
7953 if (DECL_CONSTRUCTOR_P (current_function_decl))
7954 finish_mem_initializers (mem_initializer_list);
7957 /* Parse a mem-initializer.
7960 mem-initializer-id ( expression-list [opt] )
7965 ( expression-list [opt] )
7967 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7968 class) or FIELD_DECL (for a non-static data member) to initialize;
7969 the TREE_VALUE is the expression-list. An empty initialization
7970 list is represented by void_list_node. */
7973 cp_parser_mem_initializer (cp_parser* parser)
7975 tree mem_initializer_id;
7976 tree expression_list;
7979 /* Find out what is being initialized. */
7980 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7982 pedwarn ("anachronistic old-style base class initializer");
7983 mem_initializer_id = NULL_TREE;
7986 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7987 member = expand_member_init (mem_initializer_id);
7988 if (member && !DECL_P (member))
7989 in_base_initializer = 1;
7992 = cp_parser_parenthesized_expression_list (parser, false,
7994 /*non_constant_p=*/NULL);
7995 if (expression_list == error_mark_node)
7996 return error_mark_node;
7997 if (!expression_list)
7998 expression_list = void_type_node;
8000 in_base_initializer = 0;
8002 return member ? build_tree_list (member, expression_list) : error_mark_node;
8005 /* Parse a mem-initializer-id.
8008 :: [opt] nested-name-specifier [opt] class-name
8011 Returns a TYPE indicating the class to be initializer for the first
8012 production. Returns an IDENTIFIER_NODE indicating the data member
8013 to be initialized for the second production. */
8016 cp_parser_mem_initializer_id (cp_parser* parser)
8018 bool global_scope_p;
8019 bool nested_name_specifier_p;
8020 bool template_p = false;
8023 /* `typename' is not allowed in this context ([temp.res]). */
8024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8026 error ("keyword %<typename%> not allowed in this context (a qualified "
8027 "member initializer is implicitly a type)");
8028 cp_lexer_consume_token (parser->lexer);
8030 /* Look for the optional `::' operator. */
8032 = (cp_parser_global_scope_opt (parser,
8033 /*current_scope_valid_p=*/false)
8035 /* Look for the optional nested-name-specifier. The simplest way to
8040 The keyword `typename' is not permitted in a base-specifier or
8041 mem-initializer; in these contexts a qualified name that
8042 depends on a template-parameter is implicitly assumed to be a
8045 is to assume that we have seen the `typename' keyword at this
8047 nested_name_specifier_p
8048 = (cp_parser_nested_name_specifier_opt (parser,
8049 /*typename_keyword_p=*/true,
8050 /*check_dependency_p=*/true,
8052 /*is_declaration=*/true)
8054 if (nested_name_specifier_p)
8055 template_p = cp_parser_optional_template_keyword (parser);
8056 /* If there is a `::' operator or a nested-name-specifier, then we
8057 are definitely looking for a class-name. */
8058 if (global_scope_p || nested_name_specifier_p)
8059 return cp_parser_class_name (parser,
8060 /*typename_keyword_p=*/true,
8061 /*template_keyword_p=*/template_p,
8063 /*check_dependency_p=*/true,
8064 /*class_head_p=*/false,
8065 /*is_declaration=*/true);
8066 /* Otherwise, we could also be looking for an ordinary identifier. */
8067 cp_parser_parse_tentatively (parser);
8068 /* Try a class-name. */
8069 id = cp_parser_class_name (parser,
8070 /*typename_keyword_p=*/true,
8071 /*template_keyword_p=*/false,
8073 /*check_dependency_p=*/true,
8074 /*class_head_p=*/false,
8075 /*is_declaration=*/true);
8076 /* If we found one, we're done. */
8077 if (cp_parser_parse_definitely (parser))
8079 /* Otherwise, look for an ordinary identifier. */
8080 return cp_parser_identifier (parser);
8083 /* Overloading [gram.over] */
8085 /* Parse an operator-function-id.
8087 operator-function-id:
8090 Returns an IDENTIFIER_NODE for the operator which is a
8091 human-readable spelling of the identifier, e.g., `operator +'. */
8094 cp_parser_operator_function_id (cp_parser* parser)
8096 /* Look for the `operator' keyword. */
8097 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8098 return error_mark_node;
8099 /* And then the name of the operator itself. */
8100 return cp_parser_operator (parser);
8103 /* Parse an operator.
8106 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8107 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8108 || ++ -- , ->* -> () []
8115 Returns an IDENTIFIER_NODE for the operator which is a
8116 human-readable spelling of the identifier, e.g., `operator +'. */
8119 cp_parser_operator (cp_parser* parser)
8121 tree id = NULL_TREE;
8124 /* Peek at the next token. */
8125 token = cp_lexer_peek_token (parser->lexer);
8126 /* Figure out which operator we have. */
8127 switch (token->type)
8133 /* The keyword should be either `new' or `delete'. */
8134 if (token->keyword == RID_NEW)
8136 else if (token->keyword == RID_DELETE)
8141 /* Consume the `new' or `delete' token. */
8142 cp_lexer_consume_token (parser->lexer);
8144 /* Peek at the next token. */
8145 token = cp_lexer_peek_token (parser->lexer);
8146 /* If it's a `[' token then this is the array variant of the
8148 if (token->type == CPP_OPEN_SQUARE)
8150 /* Consume the `[' token. */
8151 cp_lexer_consume_token (parser->lexer);
8152 /* Look for the `]' token. */
8153 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8154 id = ansi_opname (op == NEW_EXPR
8155 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8157 /* Otherwise, we have the non-array variant. */
8159 id = ansi_opname (op);
8165 id = ansi_opname (PLUS_EXPR);
8169 id = ansi_opname (MINUS_EXPR);
8173 id = ansi_opname (MULT_EXPR);
8177 id = ansi_opname (TRUNC_DIV_EXPR);
8181 id = ansi_opname (TRUNC_MOD_EXPR);
8185 id = ansi_opname (BIT_XOR_EXPR);
8189 id = ansi_opname (BIT_AND_EXPR);
8193 id = ansi_opname (BIT_IOR_EXPR);
8197 id = ansi_opname (BIT_NOT_EXPR);
8201 id = ansi_opname (TRUTH_NOT_EXPR);
8205 id = ansi_assopname (NOP_EXPR);
8209 id = ansi_opname (LT_EXPR);
8213 id = ansi_opname (GT_EXPR);
8217 id = ansi_assopname (PLUS_EXPR);
8221 id = ansi_assopname (MINUS_EXPR);
8225 id = ansi_assopname (MULT_EXPR);
8229 id = ansi_assopname (TRUNC_DIV_EXPR);
8233 id = ansi_assopname (TRUNC_MOD_EXPR);
8237 id = ansi_assopname (BIT_XOR_EXPR);
8241 id = ansi_assopname (BIT_AND_EXPR);
8245 id = ansi_assopname (BIT_IOR_EXPR);
8249 id = ansi_opname (LSHIFT_EXPR);
8253 id = ansi_opname (RSHIFT_EXPR);
8257 id = ansi_assopname (LSHIFT_EXPR);
8261 id = ansi_assopname (RSHIFT_EXPR);
8265 id = ansi_opname (EQ_EXPR);
8269 id = ansi_opname (NE_EXPR);
8273 id = ansi_opname (LE_EXPR);
8276 case CPP_GREATER_EQ:
8277 id = ansi_opname (GE_EXPR);
8281 id = ansi_opname (TRUTH_ANDIF_EXPR);
8285 id = ansi_opname (TRUTH_ORIF_EXPR);
8289 id = ansi_opname (POSTINCREMENT_EXPR);
8292 case CPP_MINUS_MINUS:
8293 id = ansi_opname (PREDECREMENT_EXPR);
8297 id = ansi_opname (COMPOUND_EXPR);
8300 case CPP_DEREF_STAR:
8301 id = ansi_opname (MEMBER_REF);
8305 id = ansi_opname (COMPONENT_REF);
8308 case CPP_OPEN_PAREN:
8309 /* Consume the `('. */
8310 cp_lexer_consume_token (parser->lexer);
8311 /* Look for the matching `)'. */
8312 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8313 return ansi_opname (CALL_EXPR);
8315 case CPP_OPEN_SQUARE:
8316 /* Consume the `['. */
8317 cp_lexer_consume_token (parser->lexer);
8318 /* Look for the matching `]'. */
8319 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8320 return ansi_opname (ARRAY_REF);
8324 id = ansi_opname (MIN_EXPR);
8325 cp_parser_warn_min_max ();
8329 id = ansi_opname (MAX_EXPR);
8330 cp_parser_warn_min_max ();
8334 id = ansi_assopname (MIN_EXPR);
8335 cp_parser_warn_min_max ();
8339 id = ansi_assopname (MAX_EXPR);
8340 cp_parser_warn_min_max ();
8344 /* Anything else is an error. */
8348 /* If we have selected an identifier, we need to consume the
8351 cp_lexer_consume_token (parser->lexer);
8352 /* Otherwise, no valid operator name was present. */
8355 cp_parser_error (parser, "expected operator");
8356 id = error_mark_node;
8362 /* Parse a template-declaration.
8364 template-declaration:
8365 export [opt] template < template-parameter-list > declaration
8367 If MEMBER_P is TRUE, this template-declaration occurs within a
8370 The grammar rule given by the standard isn't correct. What
8373 template-declaration:
8374 export [opt] template-parameter-list-seq
8375 decl-specifier-seq [opt] init-declarator [opt] ;
8376 export [opt] template-parameter-list-seq
8379 template-parameter-list-seq:
8380 template-parameter-list-seq [opt]
8381 template < template-parameter-list > */
8384 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8386 /* Check for `export'. */
8387 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8389 /* Consume the `export' token. */
8390 cp_lexer_consume_token (parser->lexer);
8391 /* Warn that we do not support `export'. */
8392 warning (0, "keyword %<export%> not implemented, and will be ignored");
8395 cp_parser_template_declaration_after_export (parser, member_p);
8398 /* Parse a template-parameter-list.
8400 template-parameter-list:
8402 template-parameter-list , template-parameter
8404 Returns a TREE_LIST. Each node represents a template parameter.
8405 The nodes are connected via their TREE_CHAINs. */
8408 cp_parser_template_parameter_list (cp_parser* parser)
8410 tree parameter_list = NULL_TREE;
8418 /* Parse the template-parameter. */
8419 parameter = cp_parser_template_parameter (parser, &is_non_type);
8420 /* Add it to the list. */
8421 if (parameter != error_mark_node)
8422 parameter_list = process_template_parm (parameter_list,
8425 /* Peek at the next token. */
8426 token = cp_lexer_peek_token (parser->lexer);
8427 /* If it's not a `,', we're done. */
8428 if (token->type != CPP_COMMA)
8430 /* Otherwise, consume the `,' token. */
8431 cp_lexer_consume_token (parser->lexer);
8434 return parameter_list;
8437 /* Parse a template-parameter.
8441 parameter-declaration
8443 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8444 the parameter. The TREE_PURPOSE is the default value, if any.
8445 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8446 iff this parameter is a non-type parameter. */
8449 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8452 cp_parameter_declarator *parameter_declarator;
8455 /* Assume it is a type parameter or a template parameter. */
8456 *is_non_type = false;
8457 /* Peek at the next token. */
8458 token = cp_lexer_peek_token (parser->lexer);
8459 /* If it is `class' or `template', we have a type-parameter. */
8460 if (token->keyword == RID_TEMPLATE)
8461 return cp_parser_type_parameter (parser);
8462 /* If it is `class' or `typename' we do not know yet whether it is a
8463 type parameter or a non-type parameter. Consider:
8465 template <typename T, typename T::X X> ...
8469 template <class C, class D*> ...
8471 Here, the first parameter is a type parameter, and the second is
8472 a non-type parameter. We can tell by looking at the token after
8473 the identifier -- if it is a `,', `=', or `>' then we have a type
8475 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8477 /* Peek at the token after `class' or `typename'. */
8478 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8479 /* If it's an identifier, skip it. */
8480 if (token->type == CPP_NAME)
8481 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8482 /* Now, see if the token looks like the end of a template
8484 if (token->type == CPP_COMMA
8485 || token->type == CPP_EQ
8486 || token->type == CPP_GREATER)
8487 return cp_parser_type_parameter (parser);
8490 /* Otherwise, it is a non-type parameter.
8494 When parsing a default template-argument for a non-type
8495 template-parameter, the first non-nested `>' is taken as the end
8496 of the template parameter-list rather than a greater-than
8498 *is_non_type = true;
8499 parameter_declarator
8500 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8501 /*parenthesized_p=*/NULL);
8502 parm = grokdeclarator (parameter_declarator->declarator,
8503 ¶meter_declarator->decl_specifiers,
8504 PARM, /*initialized=*/0,
8506 if (parm == error_mark_node)
8507 return error_mark_node;
8508 return build_tree_list (parameter_declarator->default_argument, parm);
8511 /* Parse a type-parameter.
8514 class identifier [opt]
8515 class identifier [opt] = type-id
8516 typename identifier [opt]
8517 typename identifier [opt] = type-id
8518 template < template-parameter-list > class identifier [opt]
8519 template < template-parameter-list > class identifier [opt]
8522 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8523 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8524 the declaration of the parameter. */
8527 cp_parser_type_parameter (cp_parser* parser)
8532 /* Look for a keyword to tell us what kind of parameter this is. */
8533 token = cp_parser_require (parser, CPP_KEYWORD,
8534 "`class', `typename', or `template'");
8536 return error_mark_node;
8538 switch (token->keyword)
8544 tree default_argument;
8546 /* If the next token is an identifier, then it names the
8548 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8549 identifier = cp_parser_identifier (parser);
8551 identifier = NULL_TREE;
8553 /* Create the parameter. */
8554 parameter = finish_template_type_parm (class_type_node, identifier);
8556 /* If the next token is an `=', we have a default argument. */
8557 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8559 /* Consume the `=' token. */
8560 cp_lexer_consume_token (parser->lexer);
8561 /* Parse the default-argument. */
8562 push_deferring_access_checks (dk_no_deferred);
8563 default_argument = cp_parser_type_id (parser);
8564 pop_deferring_access_checks ();
8567 default_argument = NULL_TREE;
8569 /* Create the combined representation of the parameter and the
8570 default argument. */
8571 parameter = build_tree_list (default_argument, parameter);
8577 tree parameter_list;
8579 tree default_argument;
8581 /* Look for the `<'. */
8582 cp_parser_require (parser, CPP_LESS, "`<'");
8583 /* Parse the template-parameter-list. */
8584 begin_template_parm_list ();
8586 = cp_parser_template_parameter_list (parser);
8587 parameter_list = end_template_parm_list (parameter_list);
8588 /* Look for the `>'. */
8589 cp_parser_require (parser, CPP_GREATER, "`>'");
8590 /* Look for the `class' keyword. */
8591 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8592 /* If the next token is an `=', then there is a
8593 default-argument. If the next token is a `>', we are at
8594 the end of the parameter-list. If the next token is a `,',
8595 then we are at the end of this parameter. */
8596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8597 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8598 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8600 identifier = cp_parser_identifier (parser);
8601 /* Treat invalid names as if the parameter were nameless. */
8602 if (identifier == error_mark_node)
8603 identifier = NULL_TREE;
8606 identifier = NULL_TREE;
8608 /* Create the template parameter. */
8609 parameter = finish_template_template_parm (class_type_node,
8612 /* If the next token is an `=', then there is a
8613 default-argument. */
8614 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8618 /* Consume the `='. */
8619 cp_lexer_consume_token (parser->lexer);
8620 /* Parse the id-expression. */
8621 push_deferring_access_checks (dk_no_deferred);
8623 = cp_parser_id_expression (parser,
8624 /*template_keyword_p=*/false,
8625 /*check_dependency_p=*/true,
8626 /*template_p=*/&is_template,
8627 /*declarator_p=*/false);
8628 if (TREE_CODE (default_argument) == TYPE_DECL)
8629 /* If the id-expression was a template-id that refers to
8630 a template-class, we already have the declaration here,
8631 so no further lookup is needed. */
8634 /* Look up the name. */
8636 = cp_parser_lookup_name (parser, default_argument,
8638 /*is_template=*/is_template,
8639 /*is_namespace=*/false,
8640 /*check_dependency=*/true,
8641 /*ambiguous_decls=*/NULL);
8642 /* See if the default argument is valid. */
8644 = check_template_template_default_arg (default_argument);
8645 pop_deferring_access_checks ();
8648 default_argument = NULL_TREE;
8650 /* Create the combined representation of the parameter and the
8651 default argument. */
8652 parameter = build_tree_list (default_argument, parameter);
8664 /* Parse a template-id.
8667 template-name < template-argument-list [opt] >
8669 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8670 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8671 returned. Otherwise, if the template-name names a function, or set
8672 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8673 names a class, returns a TYPE_DECL for the specialization.
8675 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8676 uninstantiated templates. */
8679 cp_parser_template_id (cp_parser *parser,
8680 bool template_keyword_p,
8681 bool check_dependency_p,
8682 bool is_declaration)
8687 cp_token_position start_of_id = 0;
8688 tree access_check = NULL_TREE;
8689 cp_token *next_token, *next_token_2;
8692 /* If the next token corresponds to a template-id, there is no need
8694 next_token = cp_lexer_peek_token (parser->lexer);
8695 if (next_token->type == CPP_TEMPLATE_ID)
8700 /* Get the stored value. */
8701 value = cp_lexer_consume_token (parser->lexer)->value;
8702 /* Perform any access checks that were deferred. */
8703 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8704 perform_or_defer_access_check (TREE_PURPOSE (check),
8705 TREE_VALUE (check));
8706 /* Return the stored value. */
8707 return TREE_VALUE (value);
8710 /* Avoid performing name lookup if there is no possibility of
8711 finding a template-id. */
8712 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8713 || (next_token->type == CPP_NAME
8714 && !cp_parser_nth_token_starts_template_argument_list_p
8717 cp_parser_error (parser, "expected template-id");
8718 return error_mark_node;
8721 /* Remember where the template-id starts. */
8722 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8723 start_of_id = cp_lexer_token_position (parser->lexer, false);
8725 push_deferring_access_checks (dk_deferred);
8727 /* Parse the template-name. */
8728 is_identifier = false;
8729 template = cp_parser_template_name (parser, template_keyword_p,
8733 if (template == error_mark_node || is_identifier)
8735 pop_deferring_access_checks ();
8739 /* If we find the sequence `[:' after a template-name, it's probably
8740 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8741 parse correctly the argument list. */
8742 next_token = cp_lexer_peek_token (parser->lexer);
8743 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8744 if (next_token->type == CPP_OPEN_SQUARE
8745 && next_token->flags & DIGRAPH
8746 && next_token_2->type == CPP_COLON
8747 && !(next_token_2->flags & PREV_WHITE))
8749 cp_parser_parse_tentatively (parser);
8750 /* Change `:' into `::'. */
8751 next_token_2->type = CPP_SCOPE;
8752 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8754 cp_lexer_consume_token (parser->lexer);
8755 /* Parse the arguments. */
8756 arguments = cp_parser_enclosed_template_argument_list (parser);
8757 if (!cp_parser_parse_definitely (parser))
8759 /* If we couldn't parse an argument list, then we revert our changes
8760 and return simply an error. Maybe this is not a template-id
8762 next_token_2->type = CPP_COLON;
8763 cp_parser_error (parser, "expected %<<%>");
8764 pop_deferring_access_checks ();
8765 return error_mark_node;
8767 /* Otherwise, emit an error about the invalid digraph, but continue
8768 parsing because we got our argument list. */
8769 pedwarn ("%<<::%> cannot begin a template-argument list");
8770 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8771 "between %<<%> and %<::%>");
8772 if (!flag_permissive)
8777 inform ("(if you use -fpermissive G++ will accept your code)");
8784 /* Look for the `<' that starts the template-argument-list. */
8785 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8787 pop_deferring_access_checks ();
8788 return error_mark_node;
8790 /* Parse the arguments. */
8791 arguments = cp_parser_enclosed_template_argument_list (parser);
8794 /* Build a representation of the specialization. */
8795 if (TREE_CODE (template) == IDENTIFIER_NODE)
8796 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8797 else if (DECL_CLASS_TEMPLATE_P (template)
8798 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8800 bool entering_scope;
8801 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8802 template (rather than some instantiation thereof) only if
8803 is not nested within some other construct. For example, in
8804 "template <typename T> void f(T) { A<T>::", A<T> is just an
8805 instantiation of A. */
8806 entering_scope = (template_parm_scope_p ()
8807 && cp_lexer_next_token_is (parser->lexer,
8810 = finish_template_type (template, arguments, entering_scope);
8814 /* If it's not a class-template or a template-template, it should be
8815 a function-template. */
8816 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8817 || TREE_CODE (template) == OVERLOAD
8818 || BASELINK_P (template)));
8820 template_id = lookup_template_function (template, arguments);
8823 /* Retrieve any deferred checks. Do not pop this access checks yet
8824 so the memory will not be reclaimed during token replacing below. */
8825 access_check = get_deferred_access_checks ();
8827 /* If parsing tentatively, replace the sequence of tokens that makes
8828 up the template-id with a CPP_TEMPLATE_ID token. That way,
8829 should we re-parse the token stream, we will not have to repeat
8830 the effort required to do the parse, nor will we issue duplicate
8831 error messages about problems during instantiation of the
8835 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8837 /* Reset the contents of the START_OF_ID token. */
8838 token->type = CPP_TEMPLATE_ID;
8839 token->value = build_tree_list (access_check, template_id);
8840 token->keyword = RID_MAX;
8842 /* Purge all subsequent tokens. */
8843 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8845 /* ??? Can we actually assume that, if template_id ==
8846 error_mark_node, we will have issued a diagnostic to the
8847 user, as opposed to simply marking the tentative parse as
8849 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8850 error ("parse error in template argument list");
8853 pop_deferring_access_checks ();
8857 /* Parse a template-name.
8862 The standard should actually say:
8866 operator-function-id
8868 A defect report has been filed about this issue.
8870 A conversion-function-id cannot be a template name because they cannot
8871 be part of a template-id. In fact, looking at this code:
8875 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8876 It is impossible to call a templated conversion-function-id with an
8877 explicit argument list, since the only allowed template parameter is
8878 the type to which it is converting.
8880 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8881 `template' keyword, in a construction like:
8885 In that case `f' is taken to be a template-name, even though there
8886 is no way of knowing for sure.
8888 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8889 name refers to a set of overloaded functions, at least one of which
8890 is a template, or an IDENTIFIER_NODE with the name of the template,
8891 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8892 names are looked up inside uninstantiated templates. */
8895 cp_parser_template_name (cp_parser* parser,
8896 bool template_keyword_p,
8897 bool check_dependency_p,
8898 bool is_declaration,
8899 bool *is_identifier)
8905 /* If the next token is `operator', then we have either an
8906 operator-function-id or a conversion-function-id. */
8907 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8909 /* We don't know whether we're looking at an
8910 operator-function-id or a conversion-function-id. */
8911 cp_parser_parse_tentatively (parser);
8912 /* Try an operator-function-id. */
8913 identifier = cp_parser_operator_function_id (parser);
8914 /* If that didn't work, try a conversion-function-id. */
8915 if (!cp_parser_parse_definitely (parser))
8917 cp_parser_error (parser, "expected template-name");
8918 return error_mark_node;
8921 /* Look for the identifier. */
8923 identifier = cp_parser_identifier (parser);
8925 /* If we didn't find an identifier, we don't have a template-id. */
8926 if (identifier == error_mark_node)
8927 return error_mark_node;
8929 /* If the name immediately followed the `template' keyword, then it
8930 is a template-name. However, if the next token is not `<', then
8931 we do not treat it as a template-name, since it is not being used
8932 as part of a template-id. This enables us to handle constructs
8935 template <typename T> struct S { S(); };
8936 template <typename T> S<T>::S();
8938 correctly. We would treat `S' as a template -- if it were `S<T>'
8939 -- but we do not if there is no `<'. */
8941 if (processing_template_decl
8942 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8944 /* In a declaration, in a dependent context, we pretend that the
8945 "template" keyword was present in order to improve error
8946 recovery. For example, given:
8948 template <typename T> void f(T::X<int>);
8950 we want to treat "X<int>" as a template-id. */
8952 && !template_keyword_p
8953 && parser->scope && TYPE_P (parser->scope)
8954 && check_dependency_p
8955 && dependent_type_p (parser->scope)
8956 /* Do not do this for dtors (or ctors), since they never
8957 need the template keyword before their name. */
8958 && !constructor_name_p (identifier, parser->scope))
8960 cp_token_position start = 0;
8962 /* Explain what went wrong. */
8963 error ("non-template %qD used as template", identifier);
8964 inform ("use %<%T::template %D%> to indicate that it is a template",
8965 parser->scope, identifier);
8966 /* If parsing tentatively, find the location of the "<" token. */
8967 if (cp_parser_simulate_error (parser))
8968 start = cp_lexer_token_position (parser->lexer, true);
8969 /* Parse the template arguments so that we can issue error
8970 messages about them. */
8971 cp_lexer_consume_token (parser->lexer);
8972 cp_parser_enclosed_template_argument_list (parser);
8973 /* Skip tokens until we find a good place from which to
8974 continue parsing. */
8975 cp_parser_skip_to_closing_parenthesis (parser,
8976 /*recovering=*/true,
8978 /*consume_paren=*/false);
8979 /* If parsing tentatively, permanently remove the
8980 template argument list. That will prevent duplicate
8981 error messages from being issued about the missing
8982 "template" keyword. */
8984 cp_lexer_purge_tokens_after (parser->lexer, start);
8986 *is_identifier = true;
8990 /* If the "template" keyword is present, then there is generally
8991 no point in doing name-lookup, so we just return IDENTIFIER.
8992 But, if the qualifying scope is non-dependent then we can
8993 (and must) do name-lookup normally. */
8994 if (template_keyword_p
8996 || (TYPE_P (parser->scope)
8997 && dependent_type_p (parser->scope))))
9001 /* Look up the name. */
9002 decl = cp_parser_lookup_name (parser, identifier,
9004 /*is_template=*/false,
9005 /*is_namespace=*/false,
9007 /*ambiguous_decls=*/NULL);
9008 decl = maybe_get_template_decl_from_type_decl (decl);
9010 /* If DECL is a template, then the name was a template-name. */
9011 if (TREE_CODE (decl) == TEMPLATE_DECL)
9015 tree fn = NULL_TREE;
9017 /* The standard does not explicitly indicate whether a name that
9018 names a set of overloaded declarations, some of which are
9019 templates, is a template-name. However, such a name should
9020 be a template-name; otherwise, there is no way to form a
9021 template-id for the overloaded templates. */
9022 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9023 if (TREE_CODE (fns) == OVERLOAD)
9024 for (fn = fns; fn; fn = OVL_NEXT (fn))
9025 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9030 /* The name does not name a template. */
9031 cp_parser_error (parser, "expected template-name");
9032 return error_mark_node;
9036 /* If DECL is dependent, and refers to a function, then just return
9037 its name; we will look it up again during template instantiation. */
9038 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9040 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9041 if (TYPE_P (scope) && dependent_type_p (scope))
9048 /* Parse a template-argument-list.
9050 template-argument-list:
9052 template-argument-list , template-argument
9054 Returns a TREE_VEC containing the arguments. */
9057 cp_parser_template_argument_list (cp_parser* parser)
9059 tree fixed_args[10];
9060 unsigned n_args = 0;
9061 unsigned alloced = 10;
9062 tree *arg_ary = fixed_args;
9064 bool saved_in_template_argument_list_p;
9066 bool saved_non_ice_p;
9068 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9069 parser->in_template_argument_list_p = true;
9070 /* Even if the template-id appears in an integral
9071 constant-expression, the contents of the argument list do
9073 saved_ice_p = parser->integral_constant_expression_p;
9074 parser->integral_constant_expression_p = false;
9075 saved_non_ice_p = parser->non_integral_constant_expression_p;
9076 parser->non_integral_constant_expression_p = false;
9077 /* Parse the arguments. */
9083 /* Consume the comma. */
9084 cp_lexer_consume_token (parser->lexer);
9086 /* Parse the template-argument. */
9087 argument = cp_parser_template_argument (parser);
9088 if (n_args == alloced)
9092 if (arg_ary == fixed_args)
9094 arg_ary = xmalloc (sizeof (tree) * alloced);
9095 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9098 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
9100 arg_ary[n_args++] = argument;
9102 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9104 vec = make_tree_vec (n_args);
9107 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9109 if (arg_ary != fixed_args)
9111 parser->non_integral_constant_expression_p = saved_non_ice_p;
9112 parser->integral_constant_expression_p = saved_ice_p;
9113 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9117 /* Parse a template-argument.
9120 assignment-expression
9124 The representation is that of an assignment-expression, type-id, or
9125 id-expression -- except that the qualified id-expression is
9126 evaluated, so that the value returned is either a DECL or an
9129 Although the standard says "assignment-expression", it forbids
9130 throw-expressions or assignments in the template argument.
9131 Therefore, we use "conditional-expression" instead. */
9134 cp_parser_template_argument (cp_parser* parser)
9139 bool maybe_type_id = false;
9143 /* There's really no way to know what we're looking at, so we just
9144 try each alternative in order.
9148 In a template-argument, an ambiguity between a type-id and an
9149 expression is resolved to a type-id, regardless of the form of
9150 the corresponding template-parameter.
9152 Therefore, we try a type-id first. */
9153 cp_parser_parse_tentatively (parser);
9154 argument = cp_parser_type_id (parser);
9155 /* If there was no error parsing the type-id but the next token is a '>>',
9156 we probably found a typo for '> >'. But there are type-id which are
9157 also valid expressions. For instance:
9159 struct X { int operator >> (int); };
9160 template <int V> struct Foo {};
9163 Here 'X()' is a valid type-id of a function type, but the user just
9164 wanted to write the expression "X() >> 5". Thus, we remember that we
9165 found a valid type-id, but we still try to parse the argument as an
9166 expression to see what happens. */
9167 if (!cp_parser_error_occurred (parser)
9168 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9170 maybe_type_id = true;
9171 cp_parser_abort_tentative_parse (parser);
9175 /* If the next token isn't a `,' or a `>', then this argument wasn't
9176 really finished. This means that the argument is not a valid
9178 if (!cp_parser_next_token_ends_template_argument_p (parser))
9179 cp_parser_error (parser, "expected template-argument");
9180 /* If that worked, we're done. */
9181 if (cp_parser_parse_definitely (parser))
9184 /* We're still not sure what the argument will be. */
9185 cp_parser_parse_tentatively (parser);
9186 /* Try a template. */
9187 argument = cp_parser_id_expression (parser,
9188 /*template_keyword_p=*/false,
9189 /*check_dependency_p=*/true,
9191 /*declarator_p=*/false);
9192 /* If the next token isn't a `,' or a `>', then this argument wasn't
9194 if (!cp_parser_next_token_ends_template_argument_p (parser))
9195 cp_parser_error (parser, "expected template-argument");
9196 if (!cp_parser_error_occurred (parser))
9198 /* Figure out what is being referred to. If the id-expression
9199 was for a class template specialization, then we will have a
9200 TYPE_DECL at this point. There is no need to do name lookup
9201 at this point in that case. */
9202 if (TREE_CODE (argument) != TYPE_DECL)
9203 argument = cp_parser_lookup_name (parser, argument,
9205 /*is_template=*/template_p,
9206 /*is_namespace=*/false,
9207 /*check_dependency=*/true,
9208 /*ambiguous_decls=*/NULL);
9209 if (TREE_CODE (argument) != TEMPLATE_DECL
9210 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9211 cp_parser_error (parser, "expected template-name");
9213 if (cp_parser_parse_definitely (parser))
9215 /* It must be a non-type argument. There permitted cases are given
9216 in [temp.arg.nontype]:
9218 -- an integral constant-expression of integral or enumeration
9221 -- the name of a non-type template-parameter; or
9223 -- the name of an object or function with external linkage...
9225 -- the address of an object or function with external linkage...
9227 -- a pointer to member... */
9228 /* Look for a non-type template parameter. */
9229 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9231 cp_parser_parse_tentatively (parser);
9232 argument = cp_parser_primary_expression (parser,
9235 /*template_arg_p=*/true,
9237 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9238 || !cp_parser_next_token_ends_template_argument_p (parser))
9239 cp_parser_simulate_error (parser);
9240 if (cp_parser_parse_definitely (parser))
9244 /* If the next token is "&", the argument must be the address of an
9245 object or function with external linkage. */
9246 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9248 cp_lexer_consume_token (parser->lexer);
9249 /* See if we might have an id-expression. */
9250 token = cp_lexer_peek_token (parser->lexer);
9251 if (token->type == CPP_NAME
9252 || token->keyword == RID_OPERATOR
9253 || token->type == CPP_SCOPE
9254 || token->type == CPP_TEMPLATE_ID
9255 || token->type == CPP_NESTED_NAME_SPECIFIER)
9257 cp_parser_parse_tentatively (parser);
9258 argument = cp_parser_primary_expression (parser,
9261 /*template_arg_p=*/true,
9263 if (cp_parser_error_occurred (parser)
9264 || !cp_parser_next_token_ends_template_argument_p (parser))
9265 cp_parser_abort_tentative_parse (parser);
9268 if (TREE_CODE (argument) == INDIRECT_REF)
9270 gcc_assert (REFERENCE_REF_P (argument));
9271 argument = TREE_OPERAND (argument, 0);
9274 if (TREE_CODE (argument) == BASELINK)
9275 /* We don't need the information about what class was used
9276 to name the overloaded functions. */
9277 argument = BASELINK_FUNCTIONS (argument);
9279 if (TREE_CODE (argument) == VAR_DECL)
9281 /* A variable without external linkage might still be a
9282 valid constant-expression, so no error is issued here
9283 if the external-linkage check fails. */
9284 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9285 cp_parser_simulate_error (parser);
9287 else if (is_overloaded_fn (argument))
9288 /* All overloaded functions are allowed; if the external
9289 linkage test does not pass, an error will be issued
9293 && (TREE_CODE (argument) == OFFSET_REF
9294 || TREE_CODE (argument) == SCOPE_REF))
9295 /* A pointer-to-member. */
9297 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9300 cp_parser_simulate_error (parser);
9302 if (cp_parser_parse_definitely (parser))
9305 argument = build_x_unary_op (ADDR_EXPR, argument);
9310 /* If the argument started with "&", there are no other valid
9311 alternatives at this point. */
9314 cp_parser_error (parser, "invalid non-type template argument");
9315 return error_mark_node;
9318 /* If the argument wasn't successfully parsed as a type-id followed
9319 by '>>', the argument can only be a constant expression now.
9320 Otherwise, we try parsing the constant-expression tentatively,
9321 because the argument could really be a type-id. */
9323 cp_parser_parse_tentatively (parser);
9324 argument = cp_parser_constant_expression (parser,
9325 /*allow_non_constant_p=*/false,
9326 /*non_constant_p=*/NULL);
9327 argument = fold_non_dependent_expr (argument);
9330 if (!cp_parser_next_token_ends_template_argument_p (parser))
9331 cp_parser_error (parser, "expected template-argument");
9332 if (cp_parser_parse_definitely (parser))
9334 /* We did our best to parse the argument as a non type-id, but that
9335 was the only alternative that matched (albeit with a '>' after
9336 it). We can assume it's just a typo from the user, and a
9337 diagnostic will then be issued. */
9338 return cp_parser_type_id (parser);
9341 /* Parse an explicit-instantiation.
9343 explicit-instantiation:
9344 template declaration
9346 Although the standard says `declaration', what it really means is:
9348 explicit-instantiation:
9349 template decl-specifier-seq [opt] declarator [opt] ;
9351 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9352 supposed to be allowed. A defect report has been filed about this
9357 explicit-instantiation:
9358 storage-class-specifier template
9359 decl-specifier-seq [opt] declarator [opt] ;
9360 function-specifier template
9361 decl-specifier-seq [opt] declarator [opt] ; */
9364 cp_parser_explicit_instantiation (cp_parser* parser)
9366 int declares_class_or_enum;
9367 cp_decl_specifier_seq decl_specifiers;
9368 tree extension_specifier = NULL_TREE;
9370 /* Look for an (optional) storage-class-specifier or
9371 function-specifier. */
9372 if (cp_parser_allow_gnu_extensions_p (parser))
9375 = cp_parser_storage_class_specifier_opt (parser);
9376 if (!extension_specifier)
9378 = cp_parser_function_specifier_opt (parser,
9379 /*decl_specs=*/NULL);
9382 /* Look for the `template' keyword. */
9383 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9384 /* Let the front end know that we are processing an explicit
9386 begin_explicit_instantiation ();
9387 /* [temp.explicit] says that we are supposed to ignore access
9388 control while processing explicit instantiation directives. */
9389 push_deferring_access_checks (dk_no_check);
9390 /* Parse a decl-specifier-seq. */
9391 cp_parser_decl_specifier_seq (parser,
9392 CP_PARSER_FLAGS_OPTIONAL,
9394 &declares_class_or_enum);
9395 /* If there was exactly one decl-specifier, and it declared a class,
9396 and there's no declarator, then we have an explicit type
9398 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9402 type = check_tag_decl (&decl_specifiers);
9403 /* Turn access control back on for names used during
9404 template instantiation. */
9405 pop_deferring_access_checks ();
9407 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9411 cp_declarator *declarator;
9414 /* Parse the declarator. */
9416 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9417 /*ctor_dtor_or_conv_p=*/NULL,
9418 /*parenthesized_p=*/NULL,
9419 /*member_p=*/false);
9420 if (declares_class_or_enum & 2)
9421 cp_parser_check_for_definition_in_return_type (declarator,
9422 decl_specifiers.type);
9423 if (declarator != cp_error_declarator)
9425 decl = grokdeclarator (declarator, &decl_specifiers,
9427 /* Turn access control back on for names used during
9428 template instantiation. */
9429 pop_deferring_access_checks ();
9430 /* Do the explicit instantiation. */
9431 do_decl_instantiation (decl, extension_specifier);
9435 pop_deferring_access_checks ();
9436 /* Skip the body of the explicit instantiation. */
9437 cp_parser_skip_to_end_of_statement (parser);
9440 /* We're done with the instantiation. */
9441 end_explicit_instantiation ();
9443 cp_parser_consume_semicolon_at_end_of_statement (parser);
9446 /* Parse an explicit-specialization.
9448 explicit-specialization:
9449 template < > declaration
9451 Although the standard says `declaration', what it really means is:
9453 explicit-specialization:
9454 template <> decl-specifier [opt] init-declarator [opt] ;
9455 template <> function-definition
9456 template <> explicit-specialization
9457 template <> template-declaration */
9460 cp_parser_explicit_specialization (cp_parser* parser)
9463 /* Look for the `template' keyword. */
9464 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9465 /* Look for the `<'. */
9466 cp_parser_require (parser, CPP_LESS, "`<'");
9467 /* Look for the `>'. */
9468 cp_parser_require (parser, CPP_GREATER, "`>'");
9469 /* We have processed another parameter list. */
9470 ++parser->num_template_parameter_lists;
9473 A template ... explicit specialization ... shall not have C
9475 if (current_lang_name == lang_name_c)
9477 error ("template specialization with C linkage");
9478 /* Give it C++ linkage to avoid confusing other parts of the
9480 push_lang_context (lang_name_cplusplus);
9481 need_lang_pop = true;
9484 need_lang_pop = false;
9485 /* Let the front end know that we are beginning a specialization. */
9486 begin_specialization ();
9487 /* If the next keyword is `template', we need to figure out whether
9488 or not we're looking a template-declaration. */
9489 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9491 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9492 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9493 cp_parser_template_declaration_after_export (parser,
9494 /*member_p=*/false);
9496 cp_parser_explicit_specialization (parser);
9499 /* Parse the dependent declaration. */
9500 cp_parser_single_declaration (parser,
9501 /*checks=*/NULL_TREE,
9504 /* We're done with the specialization. */
9505 end_specialization ();
9506 /* For the erroneous case of a template with C linkage, we pushed an
9507 implicit C++ linkage scope; exit that scope now. */
9509 pop_lang_context ();
9510 /* We're done with this parameter list. */
9511 --parser->num_template_parameter_lists;
9514 /* Parse a type-specifier.
9517 simple-type-specifier
9520 elaborated-type-specifier
9528 Returns a representation of the type-specifier. For a
9529 class-specifier, enum-specifier, or elaborated-type-specifier, a
9530 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9532 The parser flags FLAGS is used to control type-specifier parsing.
9534 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9535 in a decl-specifier-seq.
9537 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9538 class-specifier, enum-specifier, or elaborated-type-specifier, then
9539 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9540 if a type is declared; 2 if it is defined. Otherwise, it is set to
9543 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9544 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9548 cp_parser_type_specifier (cp_parser* parser,
9549 cp_parser_flags flags,
9550 cp_decl_specifier_seq *decl_specs,
9551 bool is_declaration,
9552 int* declares_class_or_enum,
9553 bool* is_cv_qualifier)
9555 tree type_spec = NULL_TREE;
9558 cp_decl_spec ds = ds_last;
9560 /* Assume this type-specifier does not declare a new type. */
9561 if (declares_class_or_enum)
9562 *declares_class_or_enum = 0;
9563 /* And that it does not specify a cv-qualifier. */
9564 if (is_cv_qualifier)
9565 *is_cv_qualifier = false;
9566 /* Peek at the next token. */
9567 token = cp_lexer_peek_token (parser->lexer);
9569 /* If we're looking at a keyword, we can use that to guide the
9570 production we choose. */
9571 keyword = token->keyword;
9575 /* 'enum' [identifier] '{' introduces an enum-specifier;
9576 'enum' <anything else> introduces an elaborated-type-specifier. */
9577 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9578 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9579 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9582 if (parser->num_template_parameter_lists)
9584 error ("template declaration of %qs", "enum");
9585 cp_parser_skip_to_end_of_block_or_statement (parser);
9586 type_spec = error_mark_node;
9589 type_spec = cp_parser_enum_specifier (parser);
9591 if (declares_class_or_enum)
9592 *declares_class_or_enum = 2;
9594 cp_parser_set_decl_spec_type (decl_specs,
9596 /*user_defined_p=*/true);
9600 goto elaborated_type_specifier;
9602 /* Any of these indicate either a class-specifier, or an
9603 elaborated-type-specifier. */
9607 /* Parse tentatively so that we can back up if we don't find a
9609 cp_parser_parse_tentatively (parser);
9610 /* Look for the class-specifier. */
9611 type_spec = cp_parser_class_specifier (parser);
9612 /* If that worked, we're done. */
9613 if (cp_parser_parse_definitely (parser))
9615 if (declares_class_or_enum)
9616 *declares_class_or_enum = 2;
9618 cp_parser_set_decl_spec_type (decl_specs,
9620 /*user_defined_p=*/true);
9625 elaborated_type_specifier:
9626 /* We're declaring (not defining) a class or enum. */
9627 if (declares_class_or_enum)
9628 *declares_class_or_enum = 1;
9632 /* Look for an elaborated-type-specifier. */
9634 = (cp_parser_elaborated_type_specifier
9636 decl_specs && decl_specs->specs[(int) ds_friend],
9639 cp_parser_set_decl_spec_type (decl_specs,
9641 /*user_defined_p=*/true);
9646 if (is_cv_qualifier)
9647 *is_cv_qualifier = true;
9652 if (is_cv_qualifier)
9653 *is_cv_qualifier = true;
9658 if (is_cv_qualifier)
9659 *is_cv_qualifier = true;
9663 /* The `__complex__' keyword is a GNU extension. */
9671 /* Handle simple keywords. */
9676 ++decl_specs->specs[(int)ds];
9677 decl_specs->any_specifiers_p = true;
9679 return cp_lexer_consume_token (parser->lexer)->value;
9682 /* If we do not already have a type-specifier, assume we are looking
9683 at a simple-type-specifier. */
9684 type_spec = cp_parser_simple_type_specifier (parser,
9688 /* If we didn't find a type-specifier, and a type-specifier was not
9689 optional in this context, issue an error message. */
9690 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9692 cp_parser_error (parser, "expected type specifier");
9693 return error_mark_node;
9699 /* Parse a simple-type-specifier.
9701 simple-type-specifier:
9702 :: [opt] nested-name-specifier [opt] type-name
9703 :: [opt] nested-name-specifier template template-id
9718 simple-type-specifier:
9719 __typeof__ unary-expression
9720 __typeof__ ( type-id )
9722 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9723 appropriately updated. */
9726 cp_parser_simple_type_specifier (cp_parser* parser,
9727 cp_decl_specifier_seq *decl_specs,
9728 cp_parser_flags flags)
9730 tree type = NULL_TREE;
9733 /* Peek at the next token. */
9734 token = cp_lexer_peek_token (parser->lexer);
9736 /* If we're looking at a keyword, things are easy. */
9737 switch (token->keyword)
9741 decl_specs->explicit_char_p = true;
9742 type = char_type_node;
9745 type = wchar_type_node;
9748 type = boolean_type_node;
9752 ++decl_specs->specs[(int) ds_short];
9753 type = short_integer_type_node;
9757 decl_specs->explicit_int_p = true;
9758 type = integer_type_node;
9762 ++decl_specs->specs[(int) ds_long];
9763 type = long_integer_type_node;
9767 ++decl_specs->specs[(int) ds_signed];
9768 type = integer_type_node;
9772 ++decl_specs->specs[(int) ds_unsigned];
9773 type = unsigned_type_node;
9776 type = float_type_node;
9779 type = double_type_node;
9782 type = void_type_node;
9786 /* Consume the `typeof' token. */
9787 cp_lexer_consume_token (parser->lexer);
9788 /* Parse the operand to `typeof'. */
9789 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9790 /* If it is not already a TYPE, take its type. */
9792 type = finish_typeof (type);
9795 cp_parser_set_decl_spec_type (decl_specs, type,
9796 /*user_defined_p=*/true);
9804 /* If the type-specifier was for a built-in type, we're done. */
9809 /* Record the type. */
9811 && (token->keyword != RID_SIGNED
9812 && token->keyword != RID_UNSIGNED
9813 && token->keyword != RID_SHORT
9814 && token->keyword != RID_LONG))
9815 cp_parser_set_decl_spec_type (decl_specs,
9817 /*user_defined=*/false);
9819 decl_specs->any_specifiers_p = true;
9821 /* Consume the token. */
9822 id = cp_lexer_consume_token (parser->lexer)->value;
9824 /* There is no valid C++ program where a non-template type is
9825 followed by a "<". That usually indicates that the user thought
9826 that the type was a template. */
9827 cp_parser_check_for_invalid_template_id (parser, type);
9829 return TYPE_NAME (type);
9832 /* The type-specifier must be a user-defined type. */
9833 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9838 /* Don't gobble tokens or issue error messages if this is an
9839 optional type-specifier. */
9840 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9841 cp_parser_parse_tentatively (parser);
9843 /* Look for the optional `::' operator. */
9845 = (cp_parser_global_scope_opt (parser,
9846 /*current_scope_valid_p=*/false)
9848 /* Look for the nested-name specifier. */
9850 = (cp_parser_nested_name_specifier_opt (parser,
9851 /*typename_keyword_p=*/false,
9852 /*check_dependency_p=*/true,
9854 /*is_declaration=*/false)
9856 /* If we have seen a nested-name-specifier, and the next token
9857 is `template', then we are using the template-id production. */
9859 && cp_parser_optional_template_keyword (parser))
9861 /* Look for the template-id. */
9862 type = cp_parser_template_id (parser,
9863 /*template_keyword_p=*/true,
9864 /*check_dependency_p=*/true,
9865 /*is_declaration=*/false);
9866 /* If the template-id did not name a type, we are out of
9868 if (TREE_CODE (type) != TYPE_DECL)
9870 cp_parser_error (parser, "expected template-id for type");
9874 /* Otherwise, look for a type-name. */
9876 type = cp_parser_type_name (parser);
9877 /* Keep track of all name-lookups performed in class scopes. */
9881 && TREE_CODE (type) == TYPE_DECL
9882 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9883 maybe_note_name_used_in_class (DECL_NAME (type), type);
9884 /* If it didn't work out, we don't have a TYPE. */
9885 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9886 && !cp_parser_parse_definitely (parser))
9888 if (type && decl_specs)
9889 cp_parser_set_decl_spec_type (decl_specs, type,
9890 /*user_defined=*/true);
9893 /* If we didn't get a type-name, issue an error message. */
9894 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9896 cp_parser_error (parser, "expected type-name");
9897 return error_mark_node;
9900 /* There is no valid C++ program where a non-template type is
9901 followed by a "<". That usually indicates that the user thought
9902 that the type was a template. */
9903 if (type && type != error_mark_node)
9905 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9906 If it is, then the '<'...'>' enclose protocol names rather than
9907 template arguments, and so everything is fine. */
9908 if (c_dialect_objc ()
9909 && (objc_is_id (type) || objc_is_class_name (type)))
9911 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9912 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9914 /* Clobber the "unqualified" type previously entered into
9915 DECL_SPECS with the new, improved protocol-qualified version. */
9917 decl_specs->type = qual_type;
9922 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9928 /* Parse a type-name.
9941 Returns a TYPE_DECL for the type. */
9944 cp_parser_type_name (cp_parser* parser)
9949 /* We can't know yet whether it is a class-name or not. */
9950 cp_parser_parse_tentatively (parser);
9951 /* Try a class-name. */
9952 type_decl = cp_parser_class_name (parser,
9953 /*typename_keyword_p=*/false,
9954 /*template_keyword_p=*/false,
9956 /*check_dependency_p=*/true,
9957 /*class_head_p=*/false,
9958 /*is_declaration=*/false);
9959 /* If it's not a class-name, keep looking. */
9960 if (!cp_parser_parse_definitely (parser))
9962 /* It must be a typedef-name or an enum-name. */
9963 identifier = cp_parser_identifier (parser);
9964 if (identifier == error_mark_node)
9965 return error_mark_node;
9967 /* Look up the type-name. */
9968 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9970 if (TREE_CODE (type_decl) != TYPE_DECL
9971 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9973 /* See if this is an Objective-C type. */
9974 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9975 tree type = objc_get_protocol_qualified_type (identifier, protos);
9977 type_decl = TYPE_NAME (type);
9980 /* Issue an error if we did not find a type-name. */
9981 if (TREE_CODE (type_decl) != TYPE_DECL)
9983 if (!cp_parser_simulate_error (parser))
9984 cp_parser_name_lookup_error (parser, identifier, type_decl,
9986 type_decl = error_mark_node;
9988 /* Remember that the name was used in the definition of the
9989 current class so that we can check later to see if the
9990 meaning would have been different after the class was
9991 entirely defined. */
9992 else if (type_decl != error_mark_node
9994 maybe_note_name_used_in_class (identifier, type_decl);
10001 /* Parse an elaborated-type-specifier. Note that the grammar given
10002 here incorporates the resolution to DR68.
10004 elaborated-type-specifier:
10005 class-key :: [opt] nested-name-specifier [opt] identifier
10006 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10007 enum :: [opt] nested-name-specifier [opt] identifier
10008 typename :: [opt] nested-name-specifier identifier
10009 typename :: [opt] nested-name-specifier template [opt]
10014 elaborated-type-specifier:
10015 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10016 class-key attributes :: [opt] nested-name-specifier [opt]
10017 template [opt] template-id
10018 enum attributes :: [opt] nested-name-specifier [opt] identifier
10020 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10021 declared `friend'. If IS_DECLARATION is TRUE, then this
10022 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10023 something is being declared.
10025 Returns the TYPE specified. */
10028 cp_parser_elaborated_type_specifier (cp_parser* parser,
10030 bool is_declaration)
10032 enum tag_types tag_type;
10034 tree type = NULL_TREE;
10035 tree attributes = NULL_TREE;
10037 /* See if we're looking at the `enum' keyword. */
10038 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10040 /* Consume the `enum' token. */
10041 cp_lexer_consume_token (parser->lexer);
10042 /* Remember that it's an enumeration type. */
10043 tag_type = enum_type;
10044 /* Parse the attributes. */
10045 attributes = cp_parser_attributes_opt (parser);
10047 /* Or, it might be `typename'. */
10048 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10051 /* Consume the `typename' token. */
10052 cp_lexer_consume_token (parser->lexer);
10053 /* Remember that it's a `typename' type. */
10054 tag_type = typename_type;
10055 /* The `typename' keyword is only allowed in templates. */
10056 if (!processing_template_decl)
10057 pedwarn ("using %<typename%> outside of template");
10059 /* Otherwise it must be a class-key. */
10062 tag_type = cp_parser_class_key (parser);
10063 if (tag_type == none_type)
10064 return error_mark_node;
10065 /* Parse the attributes. */
10066 attributes = cp_parser_attributes_opt (parser);
10069 /* Look for the `::' operator. */
10070 cp_parser_global_scope_opt (parser,
10071 /*current_scope_valid_p=*/false);
10072 /* Look for the nested-name-specifier. */
10073 if (tag_type == typename_type)
10075 if (!cp_parser_nested_name_specifier (parser,
10076 /*typename_keyword_p=*/true,
10077 /*check_dependency_p=*/true,
10080 return error_mark_node;
10083 /* Even though `typename' is not present, the proposed resolution
10084 to Core Issue 180 says that in `class A<T>::B', `B' should be
10085 considered a type-name, even if `A<T>' is dependent. */
10086 cp_parser_nested_name_specifier_opt (parser,
10087 /*typename_keyword_p=*/true,
10088 /*check_dependency_p=*/true,
10091 /* For everything but enumeration types, consider a template-id. */
10092 if (tag_type != enum_type)
10094 bool template_p = false;
10097 /* Allow the `template' keyword. */
10098 template_p = cp_parser_optional_template_keyword (parser);
10099 /* If we didn't see `template', we don't know if there's a
10100 template-id or not. */
10102 cp_parser_parse_tentatively (parser);
10103 /* Parse the template-id. */
10104 decl = cp_parser_template_id (parser, template_p,
10105 /*check_dependency_p=*/true,
10107 /* If we didn't find a template-id, look for an ordinary
10109 if (!template_p && !cp_parser_parse_definitely (parser))
10111 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10112 in effect, then we must assume that, upon instantiation, the
10113 template will correspond to a class. */
10114 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10115 && tag_type == typename_type)
10116 type = make_typename_type (parser->scope, decl,
10120 type = TREE_TYPE (decl);
10123 /* For an enumeration type, consider only a plain identifier. */
10126 identifier = cp_parser_identifier (parser);
10128 if (identifier == error_mark_node)
10130 parser->scope = NULL_TREE;
10131 return error_mark_node;
10134 /* For a `typename', we needn't call xref_tag. */
10135 if (tag_type == typename_type
10136 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10137 return cp_parser_make_typename_type (parser, parser->scope,
10139 /* Look up a qualified name in the usual way. */
10144 decl = cp_parser_lookup_name (parser, identifier,
10146 /*is_template=*/false,
10147 /*is_namespace=*/false,
10148 /*check_dependency=*/true,
10149 /*ambiguous_decls=*/NULL);
10151 /* If we are parsing friend declaration, DECL may be a
10152 TEMPLATE_DECL tree node here. However, we need to check
10153 whether this TEMPLATE_DECL results in valid code. Consider
10154 the following example:
10157 template <class T> class C {};
10160 template <class T> friend class N::C; // #1, valid code
10162 template <class T> class Y {
10163 friend class N::C; // #2, invalid code
10166 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10167 name lookup of `N::C'. We see that friend declaration must
10168 be template for the code to be valid. Note that
10169 processing_template_decl does not work here since it is
10170 always 1 for the above two cases. */
10172 decl = (cp_parser_maybe_treat_template_as_class
10173 (decl, /*tag_name_p=*/is_friend
10174 && parser->num_template_parameter_lists));
10176 if (TREE_CODE (decl) != TYPE_DECL)
10178 cp_parser_diagnose_invalid_type_name (parser,
10181 return error_mark_node;
10184 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10185 check_elaborated_type_specifier
10187 (parser->num_template_parameter_lists
10188 || DECL_SELF_REFERENCE_P (decl)));
10190 type = TREE_TYPE (decl);
10194 /* An elaborated-type-specifier sometimes introduces a new type and
10195 sometimes names an existing type. Normally, the rule is that it
10196 introduces a new type only if there is not an existing type of
10197 the same name already in scope. For example, given:
10200 void f() { struct S s; }
10202 the `struct S' in the body of `f' is the same `struct S' as in
10203 the global scope; the existing definition is used. However, if
10204 there were no global declaration, this would introduce a new
10205 local class named `S'.
10207 An exception to this rule applies to the following code:
10209 namespace N { struct S; }
10211 Here, the elaborated-type-specifier names a new type
10212 unconditionally; even if there is already an `S' in the
10213 containing scope this declaration names a new type.
10214 This exception only applies if the elaborated-type-specifier
10215 forms the complete declaration:
10219 A declaration consisting solely of `class-key identifier ;' is
10220 either a redeclaration of the name in the current scope or a
10221 forward declaration of the identifier as a class name. It
10222 introduces the name into the current scope.
10224 We are in this situation precisely when the next token is a `;'.
10226 An exception to the exception is that a `friend' declaration does
10227 *not* name a new type; i.e., given:
10229 struct S { friend struct T; };
10231 `T' is not a new type in the scope of `S'.
10233 Also, `new struct S' or `sizeof (struct S)' never results in the
10234 definition of a new type; a new type can only be declared in a
10235 declaration context. */
10241 /* Friends have special name lookup rules. */
10242 ts = ts_within_enclosing_non_class;
10243 else if (is_declaration
10244 && cp_lexer_next_token_is (parser->lexer,
10246 /* This is a `class-key identifier ;' */
10251 /* Warn about attributes. They are ignored. */
10253 warning (OPT_Wattributes,
10254 "type attributes are honored only at type definition");
10257 (parser->num_template_parameter_lists
10258 && (cp_parser_next_token_starts_class_definition_p (parser)
10259 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10260 /* An unqualified name was used to reference this type, so
10261 there were no qualifying templates. */
10262 if (!cp_parser_check_template_parameters (parser,
10263 /*num_templates=*/0))
10264 return error_mark_node;
10265 type = xref_tag (tag_type, identifier, ts, template_p);
10268 if (tag_type != enum_type)
10269 cp_parser_check_class_key (tag_type, type);
10271 /* A "<" cannot follow an elaborated type specifier. If that
10272 happens, the user was probably trying to form a template-id. */
10273 cp_parser_check_for_invalid_template_id (parser, type);
10278 /* Parse an enum-specifier.
10281 enum identifier [opt] { enumerator-list [opt] }
10284 enum identifier [opt] { enumerator-list [opt] } attributes
10286 Returns an ENUM_TYPE representing the enumeration. */
10289 cp_parser_enum_specifier (cp_parser* parser)
10294 /* Caller guarantees that the current token is 'enum', an identifier
10295 possibly follows, and the token after that is an opening brace.
10296 If we don't have an identifier, fabricate an anonymous name for
10297 the enumeration being defined. */
10298 cp_lexer_consume_token (parser->lexer);
10300 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10301 identifier = cp_parser_identifier (parser);
10303 identifier = make_anon_name ();
10305 /* Issue an error message if type-definitions are forbidden here. */
10306 if (!cp_parser_check_type_definition (parser))
10307 type = error_mark_node;
10309 /* Create the new type. We do this before consuming the opening
10310 brace so the enum will be recorded as being on the line of its
10311 tag (or the 'enum' keyword, if there is no tag). */
10312 type = start_enum (identifier);
10314 /* Consume the opening brace. */
10315 cp_lexer_consume_token (parser->lexer);
10317 if (type == error_mark_node)
10319 cp_parser_skip_to_end_of_block_or_statement (parser);
10320 return error_mark_node;
10323 /* If the next token is not '}', then there are some enumerators. */
10324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10325 cp_parser_enumerator_list (parser, type);
10327 /* Consume the final '}'. */
10328 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10330 /* Look for trailing attributes to apply to this enumeration, and
10331 apply them if appropriate. */
10332 if (cp_parser_allow_gnu_extensions_p (parser))
10334 tree trailing_attr = cp_parser_attributes_opt (parser);
10335 cplus_decl_attributes (&type,
10337 (int) ATTR_FLAG_TYPE_IN_PLACE);
10340 /* Finish up the enumeration. */
10341 finish_enum (type);
10346 /* Parse an enumerator-list. The enumerators all have the indicated
10350 enumerator-definition
10351 enumerator-list , enumerator-definition */
10354 cp_parser_enumerator_list (cp_parser* parser, tree type)
10358 /* Parse an enumerator-definition. */
10359 cp_parser_enumerator_definition (parser, type);
10361 /* If the next token is not a ',', we've reached the end of
10363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10365 /* Otherwise, consume the `,' and keep going. */
10366 cp_lexer_consume_token (parser->lexer);
10367 /* If the next token is a `}', there is a trailing comma. */
10368 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10370 if (pedantic && !in_system_header)
10371 pedwarn ("comma at end of enumerator list");
10377 /* Parse an enumerator-definition. The enumerator has the indicated
10380 enumerator-definition:
10382 enumerator = constant-expression
10388 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10393 /* Look for the identifier. */
10394 identifier = cp_parser_identifier (parser);
10395 if (identifier == error_mark_node)
10398 /* If the next token is an '=', then there is an explicit value. */
10399 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10401 /* Consume the `=' token. */
10402 cp_lexer_consume_token (parser->lexer);
10403 /* Parse the value. */
10404 value = cp_parser_constant_expression (parser,
10405 /*allow_non_constant_p=*/false,
10411 /* Create the enumerator. */
10412 build_enumerator (identifier, value, type);
10415 /* Parse a namespace-name.
10418 original-namespace-name
10421 Returns the NAMESPACE_DECL for the namespace. */
10424 cp_parser_namespace_name (cp_parser* parser)
10427 tree namespace_decl;
10429 /* Get the name of the namespace. */
10430 identifier = cp_parser_identifier (parser);
10431 if (identifier == error_mark_node)
10432 return error_mark_node;
10434 /* Look up the identifier in the currently active scope. Look only
10435 for namespaces, due to:
10437 [basic.lookup.udir]
10439 When looking up a namespace-name in a using-directive or alias
10440 definition, only namespace names are considered.
10444 [basic.lookup.qual]
10446 During the lookup of a name preceding the :: scope resolution
10447 operator, object, function, and enumerator names are ignored.
10449 (Note that cp_parser_class_or_namespace_name only calls this
10450 function if the token after the name is the scope resolution
10452 namespace_decl = cp_parser_lookup_name (parser, identifier,
10454 /*is_template=*/false,
10455 /*is_namespace=*/true,
10456 /*check_dependency=*/true,
10457 /*ambiguous_decls=*/NULL);
10458 /* If it's not a namespace, issue an error. */
10459 if (namespace_decl == error_mark_node
10460 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10462 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10463 error ("%qD is not a namespace-name", identifier);
10464 cp_parser_error (parser, "expected namespace-name");
10465 namespace_decl = error_mark_node;
10468 return namespace_decl;
10471 /* Parse a namespace-definition.
10473 namespace-definition:
10474 named-namespace-definition
10475 unnamed-namespace-definition
10477 named-namespace-definition:
10478 original-namespace-definition
10479 extension-namespace-definition
10481 original-namespace-definition:
10482 namespace identifier { namespace-body }
10484 extension-namespace-definition:
10485 namespace original-namespace-name { namespace-body }
10487 unnamed-namespace-definition:
10488 namespace { namespace-body } */
10491 cp_parser_namespace_definition (cp_parser* parser)
10495 /* Look for the `namespace' keyword. */
10496 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10498 /* Get the name of the namespace. We do not attempt to distinguish
10499 between an original-namespace-definition and an
10500 extension-namespace-definition at this point. The semantic
10501 analysis routines are responsible for that. */
10502 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10503 identifier = cp_parser_identifier (parser);
10505 identifier = NULL_TREE;
10507 /* Look for the `{' to start the namespace. */
10508 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10509 /* Start the namespace. */
10510 push_namespace (identifier);
10511 /* Parse the body of the namespace. */
10512 cp_parser_namespace_body (parser);
10513 /* Finish the namespace. */
10515 /* Look for the final `}'. */
10516 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10519 /* Parse a namespace-body.
10522 declaration-seq [opt] */
10525 cp_parser_namespace_body (cp_parser* parser)
10527 cp_parser_declaration_seq_opt (parser);
10530 /* Parse a namespace-alias-definition.
10532 namespace-alias-definition:
10533 namespace identifier = qualified-namespace-specifier ; */
10536 cp_parser_namespace_alias_definition (cp_parser* parser)
10539 tree namespace_specifier;
10541 /* Look for the `namespace' keyword. */
10542 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10543 /* Look for the identifier. */
10544 identifier = cp_parser_identifier (parser);
10545 if (identifier == error_mark_node)
10547 /* Look for the `=' token. */
10548 cp_parser_require (parser, CPP_EQ, "`='");
10549 /* Look for the qualified-namespace-specifier. */
10550 namespace_specifier
10551 = cp_parser_qualified_namespace_specifier (parser);
10552 /* Look for the `;' token. */
10553 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10555 /* Register the alias in the symbol table. */
10556 do_namespace_alias (identifier, namespace_specifier);
10559 /* Parse a qualified-namespace-specifier.
10561 qualified-namespace-specifier:
10562 :: [opt] nested-name-specifier [opt] namespace-name
10564 Returns a NAMESPACE_DECL corresponding to the specified
10568 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10570 /* Look for the optional `::'. */
10571 cp_parser_global_scope_opt (parser,
10572 /*current_scope_valid_p=*/false);
10574 /* Look for the optional nested-name-specifier. */
10575 cp_parser_nested_name_specifier_opt (parser,
10576 /*typename_keyword_p=*/false,
10577 /*check_dependency_p=*/true,
10579 /*is_declaration=*/true);
10581 return cp_parser_namespace_name (parser);
10584 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10585 access declaration.
10588 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10589 using :: unqualified-id ;
10591 access-declaration:
10597 cp_parser_using_declaration (cp_parser* parser,
10598 bool access_declaration_p)
10601 bool typename_p = false;
10602 bool global_scope_p;
10607 if (access_declaration_p)
10608 cp_parser_parse_tentatively (parser);
10611 /* Look for the `using' keyword. */
10612 cp_parser_require_keyword (parser, RID_USING, "`using'");
10614 /* Peek at the next token. */
10615 token = cp_lexer_peek_token (parser->lexer);
10616 /* See if it's `typename'. */
10617 if (token->keyword == RID_TYPENAME)
10619 /* Remember that we've seen it. */
10621 /* Consume the `typename' token. */
10622 cp_lexer_consume_token (parser->lexer);
10626 /* Look for the optional global scope qualification. */
10628 = (cp_parser_global_scope_opt (parser,
10629 /*current_scope_valid_p=*/false)
10632 /* If we saw `typename', or didn't see `::', then there must be a
10633 nested-name-specifier present. */
10634 if (typename_p || !global_scope_p)
10635 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10636 /*check_dependency_p=*/true,
10638 /*is_declaration=*/true);
10639 /* Otherwise, we could be in either of the two productions. In that
10640 case, treat the nested-name-specifier as optional. */
10642 qscope = cp_parser_nested_name_specifier_opt (parser,
10643 /*typename_keyword_p=*/false,
10644 /*check_dependency_p=*/true,
10646 /*is_declaration=*/true);
10648 qscope = global_namespace;
10650 if (access_declaration_p && cp_parser_error_occurred (parser))
10651 /* Something has already gone wrong; there's no need to parse
10652 further. Since an error has occurred, the return value of
10653 cp_parser_parse_definitely will be false, as required. */
10654 return cp_parser_parse_definitely (parser);
10656 /* Parse the unqualified-id. */
10657 identifier = cp_parser_unqualified_id (parser,
10658 /*template_keyword_p=*/false,
10659 /*check_dependency_p=*/true,
10660 /*declarator_p=*/true);
10662 if (access_declaration_p)
10664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10665 cp_parser_simulate_error (parser);
10666 if (!cp_parser_parse_definitely (parser))
10670 /* The function we call to handle a using-declaration is different
10671 depending on what scope we are in. */
10672 if (qscope == error_mark_node || identifier == error_mark_node)
10674 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10675 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10676 /* [namespace.udecl]
10678 A using declaration shall not name a template-id. */
10679 error ("a template-id may not appear in a using-declaration");
10682 if (at_class_scope_p ())
10684 /* Create the USING_DECL. */
10685 decl = do_class_using_decl (parser->scope, identifier);
10686 /* Add it to the list of members in this class. */
10687 finish_member_declaration (decl);
10691 decl = cp_parser_lookup_name_simple (parser, identifier);
10692 if (decl == error_mark_node)
10693 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10694 else if (!at_namespace_scope_p ())
10695 do_local_using_decl (decl, qscope, identifier);
10697 do_toplevel_using_decl (decl, qscope, identifier);
10701 /* Look for the final `;'. */
10702 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10707 /* Parse a using-directive.
10710 using namespace :: [opt] nested-name-specifier [opt]
10711 namespace-name ; */
10714 cp_parser_using_directive (cp_parser* parser)
10716 tree namespace_decl;
10719 /* Look for the `using' keyword. */
10720 cp_parser_require_keyword (parser, RID_USING, "`using'");
10721 /* And the `namespace' keyword. */
10722 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10723 /* Look for the optional `::' operator. */
10724 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10725 /* And the optional nested-name-specifier. */
10726 cp_parser_nested_name_specifier_opt (parser,
10727 /*typename_keyword_p=*/false,
10728 /*check_dependency_p=*/true,
10730 /*is_declaration=*/true);
10731 /* Get the namespace being used. */
10732 namespace_decl = cp_parser_namespace_name (parser);
10733 /* And any specified attributes. */
10734 attribs = cp_parser_attributes_opt (parser);
10735 /* Update the symbol table. */
10736 parse_using_directive (namespace_decl, attribs);
10737 /* Look for the final `;'. */
10738 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10741 /* Parse an asm-definition.
10744 asm ( string-literal ) ;
10749 asm volatile [opt] ( string-literal ) ;
10750 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10751 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10752 : asm-operand-list [opt] ) ;
10753 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10754 : asm-operand-list [opt]
10755 : asm-operand-list [opt] ) ; */
10758 cp_parser_asm_definition (cp_parser* parser)
10761 tree outputs = NULL_TREE;
10762 tree inputs = NULL_TREE;
10763 tree clobbers = NULL_TREE;
10765 bool volatile_p = false;
10766 bool extended_p = false;
10768 /* Look for the `asm' keyword. */
10769 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10770 /* See if the next token is `volatile'. */
10771 if (cp_parser_allow_gnu_extensions_p (parser)
10772 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10774 /* Remember that we saw the `volatile' keyword. */
10776 /* Consume the token. */
10777 cp_lexer_consume_token (parser->lexer);
10779 /* Look for the opening `('. */
10780 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10782 /* Look for the string. */
10783 string = cp_parser_string_literal (parser, false, false);
10784 if (string == error_mark_node)
10786 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10787 /*consume_paren=*/true);
10791 /* If we're allowing GNU extensions, check for the extended assembly
10792 syntax. Unfortunately, the `:' tokens need not be separated by
10793 a space in C, and so, for compatibility, we tolerate that here
10794 too. Doing that means that we have to treat the `::' operator as
10796 if (cp_parser_allow_gnu_extensions_p (parser)
10797 && parser->in_function_body
10798 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10799 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10801 bool inputs_p = false;
10802 bool clobbers_p = false;
10804 /* The extended syntax was used. */
10807 /* Look for outputs. */
10808 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10810 /* Consume the `:'. */
10811 cp_lexer_consume_token (parser->lexer);
10812 /* Parse the output-operands. */
10813 if (cp_lexer_next_token_is_not (parser->lexer,
10815 && cp_lexer_next_token_is_not (parser->lexer,
10817 && cp_lexer_next_token_is_not (parser->lexer,
10819 outputs = cp_parser_asm_operand_list (parser);
10821 /* If the next token is `::', there are no outputs, and the
10822 next token is the beginning of the inputs. */
10823 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10824 /* The inputs are coming next. */
10827 /* Look for inputs. */
10829 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10831 /* Consume the `:' or `::'. */
10832 cp_lexer_consume_token (parser->lexer);
10833 /* Parse the output-operands. */
10834 if (cp_lexer_next_token_is_not (parser->lexer,
10836 && cp_lexer_next_token_is_not (parser->lexer,
10838 inputs = cp_parser_asm_operand_list (parser);
10840 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10841 /* The clobbers are coming next. */
10844 /* Look for clobbers. */
10846 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10848 /* Consume the `:' or `::'. */
10849 cp_lexer_consume_token (parser->lexer);
10850 /* Parse the clobbers. */
10851 if (cp_lexer_next_token_is_not (parser->lexer,
10853 clobbers = cp_parser_asm_clobber_list (parser);
10856 /* Look for the closing `)'. */
10857 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10858 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10859 /*consume_paren=*/true);
10860 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10862 /* Create the ASM_EXPR. */
10863 if (parser->in_function_body)
10865 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10867 /* If the extended syntax was not used, mark the ASM_EXPR. */
10870 tree temp = asm_stmt;
10871 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10872 temp = TREE_OPERAND (temp, 0);
10874 ASM_INPUT_P (temp) = 1;
10878 assemble_asm (string);
10881 /* Declarators [gram.dcl.decl] */
10883 /* Parse an init-declarator.
10886 declarator initializer [opt]
10891 declarator asm-specification [opt] attributes [opt] initializer [opt]
10893 function-definition:
10894 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10896 decl-specifier-seq [opt] declarator function-try-block
10900 function-definition:
10901 __extension__ function-definition
10903 The DECL_SPECIFIERS apply to this declarator. Returns a
10904 representation of the entity declared. If MEMBER_P is TRUE, then
10905 this declarator appears in a class scope. The new DECL created by
10906 this declarator is returned.
10908 The CHECKS are access checks that should be performed once we know
10909 what entity is being declared (and, therefore, what classes have
10912 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10913 for a function-definition here as well. If the declarator is a
10914 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10915 be TRUE upon return. By that point, the function-definition will
10916 have been completely parsed.
10918 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10922 cp_parser_init_declarator (cp_parser* parser,
10923 cp_decl_specifier_seq *decl_specifiers,
10925 bool function_definition_allowed_p,
10927 int declares_class_or_enum,
10928 bool* function_definition_p)
10931 cp_declarator *declarator;
10932 tree prefix_attributes;
10934 tree asm_specification;
10936 tree decl = NULL_TREE;
10938 bool is_initialized;
10939 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10940 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10942 enum cpp_ttype initialization_kind;
10943 bool is_parenthesized_init;
10944 bool is_non_constant_init;
10945 int ctor_dtor_or_conv_p;
10947 tree pushed_scope = NULL;
10949 /* Gather the attributes that were provided with the
10950 decl-specifiers. */
10951 prefix_attributes = decl_specifiers->attributes;
10953 /* Assume that this is not the declarator for a function
10955 if (function_definition_p)
10956 *function_definition_p = false;
10958 /* Defer access checks while parsing the declarator; we cannot know
10959 what names are accessible until we know what is being
10961 resume_deferring_access_checks ();
10963 /* Parse the declarator. */
10965 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10966 &ctor_dtor_or_conv_p,
10967 /*parenthesized_p=*/NULL,
10968 /*member_p=*/false);
10969 /* Gather up the deferred checks. */
10970 stop_deferring_access_checks ();
10972 /* If the DECLARATOR was erroneous, there's no need to go
10974 if (declarator == cp_error_declarator)
10975 return error_mark_node;
10977 /* Check that the number of template-parameter-lists is OK. */
10978 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10979 return error_mark_node;
10981 if (declares_class_or_enum & 2)
10982 cp_parser_check_for_definition_in_return_type (declarator,
10983 decl_specifiers->type);
10985 /* Figure out what scope the entity declared by the DECLARATOR is
10986 located in. `grokdeclarator' sometimes changes the scope, so
10987 we compute it now. */
10988 scope = get_scope_of_declarator (declarator);
10990 /* If we're allowing GNU extensions, look for an asm-specification
10992 if (cp_parser_allow_gnu_extensions_p (parser))
10994 /* Look for an asm-specification. */
10995 asm_specification = cp_parser_asm_specification_opt (parser);
10996 /* And attributes. */
10997 attributes = cp_parser_attributes_opt (parser);
11001 asm_specification = NULL_TREE;
11002 attributes = NULL_TREE;
11005 /* Peek at the next token. */
11006 token = cp_lexer_peek_token (parser->lexer);
11007 /* Check to see if the token indicates the start of a
11008 function-definition. */
11009 if (cp_parser_token_starts_function_definition_p (token))
11011 if (!function_definition_allowed_p)
11013 /* If a function-definition should not appear here, issue an
11015 cp_parser_error (parser,
11016 "a function-definition is not allowed here");
11017 return error_mark_node;
11021 /* Neither attributes nor an asm-specification are allowed
11022 on a function-definition. */
11023 if (asm_specification)
11024 error ("an asm-specification is not allowed on a function-definition");
11026 error ("attributes are not allowed on a function-definition");
11027 /* This is a function-definition. */
11028 *function_definition_p = true;
11030 /* Parse the function definition. */
11032 decl = cp_parser_save_member_function_body (parser,
11035 prefix_attributes);
11038 = (cp_parser_function_definition_from_specifiers_and_declarator
11039 (parser, decl_specifiers, prefix_attributes, declarator));
11047 Only in function declarations for constructors, destructors, and
11048 type conversions can the decl-specifier-seq be omitted.
11050 We explicitly postpone this check past the point where we handle
11051 function-definitions because we tolerate function-definitions
11052 that are missing their return types in some modes. */
11053 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11055 cp_parser_error (parser,
11056 "expected constructor, destructor, or type conversion");
11057 return error_mark_node;
11060 /* An `=' or an `(' indicates an initializer. */
11061 if (token->type == CPP_EQ
11062 || token->type == CPP_OPEN_PAREN)
11064 is_initialized = true;
11065 initialization_kind = token->type;
11069 /* If the init-declarator isn't initialized and isn't followed by a
11070 `,' or `;', it's not a valid init-declarator. */
11071 if (token->type != CPP_COMMA
11072 && token->type != CPP_SEMICOLON)
11074 cp_parser_error (parser, "expected initializer");
11075 return error_mark_node;
11077 is_initialized = false;
11078 initialization_kind = CPP_EOF;
11081 /* Because start_decl has side-effects, we should only call it if we
11082 know we're going ahead. By this point, we know that we cannot
11083 possibly be looking at any other construct. */
11084 cp_parser_commit_to_tentative_parse (parser);
11086 /* If the decl specifiers were bad, issue an error now that we're
11087 sure this was intended to be a declarator. Then continue
11088 declaring the variable(s), as int, to try to cut down on further
11090 if (decl_specifiers->any_specifiers_p
11091 && decl_specifiers->type == error_mark_node)
11093 cp_parser_error (parser, "invalid type in declaration");
11094 decl_specifiers->type = integer_type_node;
11097 /* Check to see whether or not this declaration is a friend. */
11098 friend_p = cp_parser_friend_p (decl_specifiers);
11100 /* Enter the newly declared entry in the symbol table. If we're
11101 processing a declaration in a class-specifier, we wait until
11102 after processing the initializer. */
11105 if (parser->in_unbraced_linkage_specification_p)
11106 decl_specifiers->storage_class = sc_extern;
11107 decl = start_decl (declarator, decl_specifiers,
11108 is_initialized, attributes, prefix_attributes,
11112 /* Enter the SCOPE. That way unqualified names appearing in the
11113 initializer will be looked up in SCOPE. */
11114 pushed_scope = push_scope (scope);
11116 /* Perform deferred access control checks, now that we know in which
11117 SCOPE the declared entity resides. */
11118 if (!member_p && decl)
11120 tree saved_current_function_decl = NULL_TREE;
11122 /* If the entity being declared is a function, pretend that we
11123 are in its scope. If it is a `friend', it may have access to
11124 things that would not otherwise be accessible. */
11125 if (TREE_CODE (decl) == FUNCTION_DECL)
11127 saved_current_function_decl = current_function_decl;
11128 current_function_decl = decl;
11131 /* Perform access checks for template parameters. */
11132 cp_parser_perform_template_parameter_access_checks (checks);
11134 /* Perform the access control checks for the declarator and the
11135 the decl-specifiers. */
11136 perform_deferred_access_checks ();
11138 /* Restore the saved value. */
11139 if (TREE_CODE (decl) == FUNCTION_DECL)
11140 current_function_decl = saved_current_function_decl;
11143 /* Parse the initializer. */
11144 initializer = NULL_TREE;
11145 is_parenthesized_init = false;
11146 is_non_constant_init = true;
11147 if (is_initialized)
11149 if (function_declarator_p (declarator))
11151 if (initialization_kind == CPP_EQ)
11152 initializer = cp_parser_pure_specifier (parser);
11155 /* If the declaration was erroneous, we don't really
11156 know what the user intended, so just silently
11157 consume the initializer. */
11158 if (decl != error_mark_node)
11159 error ("initializer provided for function");
11160 cp_parser_skip_to_closing_parenthesis (parser,
11161 /*recovering=*/true,
11162 /*or_comma=*/false,
11163 /*consume_paren=*/true);
11167 initializer = cp_parser_initializer (parser,
11168 &is_parenthesized_init,
11169 &is_non_constant_init);
11172 /* The old parser allows attributes to appear after a parenthesized
11173 initializer. Mark Mitchell proposed removing this functionality
11174 on the GCC mailing lists on 2002-08-13. This parser accepts the
11175 attributes -- but ignores them. */
11176 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11177 if (cp_parser_attributes_opt (parser))
11178 warning (OPT_Wattributes,
11179 "attributes after parenthesized initializer ignored");
11181 /* For an in-class declaration, use `grokfield' to create the
11187 pop_scope (pushed_scope);
11188 pushed_scope = false;
11190 decl = grokfield (declarator, decl_specifiers,
11191 initializer, !is_non_constant_init,
11192 /*asmspec=*/NULL_TREE,
11193 prefix_attributes);
11194 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11195 cp_parser_save_default_args (parser, decl);
11198 /* Finish processing the declaration. But, skip friend
11200 if (!friend_p && decl && decl != error_mark_node)
11202 cp_finish_decl (decl,
11203 initializer, !is_non_constant_init,
11205 /* If the initializer is in parentheses, then this is
11206 a direct-initialization, which means that an
11207 `explicit' constructor is OK. Otherwise, an
11208 `explicit' constructor cannot be used. */
11209 ((is_parenthesized_init || !is_initialized)
11210 ? 0 : LOOKUP_ONLYCONVERTING));
11212 if (!friend_p && pushed_scope)
11213 pop_scope (pushed_scope);
11218 /* Parse a declarator.
11222 ptr-operator declarator
11224 abstract-declarator:
11225 ptr-operator abstract-declarator [opt]
11226 direct-abstract-declarator
11231 attributes [opt] direct-declarator
11232 attributes [opt] ptr-operator declarator
11234 abstract-declarator:
11235 attributes [opt] ptr-operator abstract-declarator [opt]
11236 attributes [opt] direct-abstract-declarator
11238 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11239 detect constructor, destructor or conversion operators. It is set
11240 to -1 if the declarator is a name, and +1 if it is a
11241 function. Otherwise it is set to zero. Usually you just want to
11242 test for >0, but internally the negative value is used.
11244 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11245 a decl-specifier-seq unless it declares a constructor, destructor,
11246 or conversion. It might seem that we could check this condition in
11247 semantic analysis, rather than parsing, but that makes it difficult
11248 to handle something like `f()'. We want to notice that there are
11249 no decl-specifiers, and therefore realize that this is an
11250 expression, not a declaration.)
11252 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11253 the declarator is a direct-declarator of the form "(...)".
11255 MEMBER_P is true iff this declarator is a member-declarator. */
11257 static cp_declarator *
11258 cp_parser_declarator (cp_parser* parser,
11259 cp_parser_declarator_kind dcl_kind,
11260 int* ctor_dtor_or_conv_p,
11261 bool* parenthesized_p,
11265 cp_declarator *declarator;
11266 enum tree_code code;
11267 cp_cv_quals cv_quals;
11269 tree attributes = NULL_TREE;
11271 /* Assume this is not a constructor, destructor, or type-conversion
11273 if (ctor_dtor_or_conv_p)
11274 *ctor_dtor_or_conv_p = 0;
11276 if (cp_parser_allow_gnu_extensions_p (parser))
11277 attributes = cp_parser_attributes_opt (parser);
11279 /* Peek at the next token. */
11280 token = cp_lexer_peek_token (parser->lexer);
11282 /* Check for the ptr-operator production. */
11283 cp_parser_parse_tentatively (parser);
11284 /* Parse the ptr-operator. */
11285 code = cp_parser_ptr_operator (parser,
11288 /* If that worked, then we have a ptr-operator. */
11289 if (cp_parser_parse_definitely (parser))
11291 /* If a ptr-operator was found, then this declarator was not
11293 if (parenthesized_p)
11294 *parenthesized_p = true;
11295 /* The dependent declarator is optional if we are parsing an
11296 abstract-declarator. */
11297 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11298 cp_parser_parse_tentatively (parser);
11300 /* Parse the dependent declarator. */
11301 declarator = cp_parser_declarator (parser, dcl_kind,
11302 /*ctor_dtor_or_conv_p=*/NULL,
11303 /*parenthesized_p=*/NULL,
11304 /*member_p=*/false);
11306 /* If we are parsing an abstract-declarator, we must handle the
11307 case where the dependent declarator is absent. */
11308 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11309 && !cp_parser_parse_definitely (parser))
11312 /* Build the representation of the ptr-operator. */
11314 declarator = make_ptrmem_declarator (cv_quals,
11317 else if (code == INDIRECT_REF)
11318 declarator = make_pointer_declarator (cv_quals, declarator);
11320 declarator = make_reference_declarator (cv_quals, declarator);
11322 /* Everything else is a direct-declarator. */
11325 if (parenthesized_p)
11326 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11328 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11329 ctor_dtor_or_conv_p,
11333 if (attributes && declarator && declarator != cp_error_declarator)
11334 declarator->attributes = attributes;
11339 /* Parse a direct-declarator or direct-abstract-declarator.
11343 direct-declarator ( parameter-declaration-clause )
11344 cv-qualifier-seq [opt]
11345 exception-specification [opt]
11346 direct-declarator [ constant-expression [opt] ]
11349 direct-abstract-declarator:
11350 direct-abstract-declarator [opt]
11351 ( parameter-declaration-clause )
11352 cv-qualifier-seq [opt]
11353 exception-specification [opt]
11354 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11355 ( abstract-declarator )
11357 Returns a representation of the declarator. DCL_KIND is
11358 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11359 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11360 we are parsing a direct-declarator. It is
11361 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11362 of ambiguity we prefer an abstract declarator, as per
11363 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11364 cp_parser_declarator. */
11366 static cp_declarator *
11367 cp_parser_direct_declarator (cp_parser* parser,
11368 cp_parser_declarator_kind dcl_kind,
11369 int* ctor_dtor_or_conv_p,
11373 cp_declarator *declarator = NULL;
11374 tree scope = NULL_TREE;
11375 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11376 bool saved_in_declarator_p = parser->in_declarator_p;
11378 tree pushed_scope = NULL_TREE;
11382 /* Peek at the next token. */
11383 token = cp_lexer_peek_token (parser->lexer);
11384 if (token->type == CPP_OPEN_PAREN)
11386 /* This is either a parameter-declaration-clause, or a
11387 parenthesized declarator. When we know we are parsing a
11388 named declarator, it must be a parenthesized declarator
11389 if FIRST is true. For instance, `(int)' is a
11390 parameter-declaration-clause, with an omitted
11391 direct-abstract-declarator. But `((*))', is a
11392 parenthesized abstract declarator. Finally, when T is a
11393 template parameter `(T)' is a
11394 parameter-declaration-clause, and not a parenthesized
11397 We first try and parse a parameter-declaration-clause,
11398 and then try a nested declarator (if FIRST is true).
11400 It is not an error for it not to be a
11401 parameter-declaration-clause, even when FIRST is
11407 The first is the declaration of a function while the
11408 second is a the definition of a variable, including its
11411 Having seen only the parenthesis, we cannot know which of
11412 these two alternatives should be selected. Even more
11413 complex are examples like:
11418 The former is a function-declaration; the latter is a
11419 variable initialization.
11421 Thus again, we try a parameter-declaration-clause, and if
11422 that fails, we back out and return. */
11424 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11426 cp_parameter_declarator *params;
11427 unsigned saved_num_template_parameter_lists;
11429 /* In a member-declarator, the only valid interpretation
11430 of a parenthesis is the start of a
11431 parameter-declaration-clause. (It is invalid to
11432 initialize a static data member with a parenthesized
11433 initializer; only the "=" form of initialization is
11436 cp_parser_parse_tentatively (parser);
11438 /* Consume the `('. */
11439 cp_lexer_consume_token (parser->lexer);
11442 /* If this is going to be an abstract declarator, we're
11443 in a declarator and we can't have default args. */
11444 parser->default_arg_ok_p = false;
11445 parser->in_declarator_p = true;
11448 /* Inside the function parameter list, surrounding
11449 template-parameter-lists do not apply. */
11450 saved_num_template_parameter_lists
11451 = parser->num_template_parameter_lists;
11452 parser->num_template_parameter_lists = 0;
11454 /* Parse the parameter-declaration-clause. */
11455 params = cp_parser_parameter_declaration_clause (parser);
11457 parser->num_template_parameter_lists
11458 = saved_num_template_parameter_lists;
11460 /* If all went well, parse the cv-qualifier-seq and the
11461 exception-specification. */
11462 if (member_p || cp_parser_parse_definitely (parser))
11464 cp_cv_quals cv_quals;
11465 tree exception_specification;
11467 if (ctor_dtor_or_conv_p)
11468 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11470 /* Consume the `)'. */
11471 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11473 /* Parse the cv-qualifier-seq. */
11474 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11475 /* And the exception-specification. */
11476 exception_specification
11477 = cp_parser_exception_specification_opt (parser);
11479 /* Create the function-declarator. */
11480 declarator = make_call_declarator (declarator,
11483 exception_specification);
11484 /* Any subsequent parameter lists are to do with
11485 return type, so are not those of the declared
11487 parser->default_arg_ok_p = false;
11489 /* Repeat the main loop. */
11494 /* If this is the first, we can try a parenthesized
11498 bool saved_in_type_id_in_expr_p;
11500 parser->default_arg_ok_p = saved_default_arg_ok_p;
11501 parser->in_declarator_p = saved_in_declarator_p;
11503 /* Consume the `('. */
11504 cp_lexer_consume_token (parser->lexer);
11505 /* Parse the nested declarator. */
11506 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11507 parser->in_type_id_in_expr_p = true;
11509 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11510 /*parenthesized_p=*/NULL,
11512 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11514 /* Expect a `)'. */
11515 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11516 declarator = cp_error_declarator;
11517 if (declarator == cp_error_declarator)
11520 goto handle_declarator;
11522 /* Otherwise, we must be done. */
11526 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11527 && token->type == CPP_OPEN_SQUARE)
11529 /* Parse an array-declarator. */
11532 if (ctor_dtor_or_conv_p)
11533 *ctor_dtor_or_conv_p = 0;
11536 parser->default_arg_ok_p = false;
11537 parser->in_declarator_p = true;
11538 /* Consume the `['. */
11539 cp_lexer_consume_token (parser->lexer);
11540 /* Peek at the next token. */
11541 token = cp_lexer_peek_token (parser->lexer);
11542 /* If the next token is `]', then there is no
11543 constant-expression. */
11544 if (token->type != CPP_CLOSE_SQUARE)
11546 bool non_constant_p;
11549 = cp_parser_constant_expression (parser,
11550 /*allow_non_constant=*/true,
11552 if (!non_constant_p)
11553 bounds = fold_non_dependent_expr (bounds);
11554 /* Normally, the array bound must be an integral constant
11555 expression. However, as an extension, we allow VLAs
11556 in function scopes. */
11557 else if (!parser->in_function_body)
11559 error ("array bound is not an integer constant");
11560 bounds = error_mark_node;
11564 bounds = NULL_TREE;
11565 /* Look for the closing `]'. */
11566 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11568 declarator = cp_error_declarator;
11572 declarator = make_array_declarator (declarator, bounds);
11574 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11576 tree qualifying_scope;
11577 tree unqualified_name;
11578 special_function_kind sfk;
11580 /* Parse a declarator-id */
11581 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11582 cp_parser_parse_tentatively (parser);
11583 unqualified_name = cp_parser_declarator_id (parser);
11584 qualifying_scope = parser->scope;
11585 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11587 if (!cp_parser_parse_definitely (parser))
11588 unqualified_name = error_mark_node;
11589 else if (qualifying_scope
11590 || (TREE_CODE (unqualified_name)
11591 != IDENTIFIER_NODE))
11593 cp_parser_error (parser, "expected unqualified-id");
11594 unqualified_name = error_mark_node;
11598 if (unqualified_name == error_mark_node)
11600 declarator = cp_error_declarator;
11604 if (qualifying_scope && at_namespace_scope_p ()
11605 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11607 /* In the declaration of a member of a template class
11608 outside of the class itself, the SCOPE will sometimes
11609 be a TYPENAME_TYPE. For example, given:
11611 template <typename T>
11612 int S<T>::R::i = 3;
11614 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11615 this context, we must resolve S<T>::R to an ordinary
11616 type, rather than a typename type.
11618 The reason we normally avoid resolving TYPENAME_TYPEs
11619 is that a specialization of `S' might render
11620 `S<T>::R' not a type. However, if `S' is
11621 specialized, then this `i' will not be used, so there
11622 is no harm in resolving the types here. */
11625 /* Resolve the TYPENAME_TYPE. */
11626 type = resolve_typename_type (qualifying_scope,
11627 /*only_current_p=*/false);
11628 /* If that failed, the declarator is invalid. */
11629 if (type == error_mark_node)
11630 error ("%<%T::%D%> is not a type",
11631 TYPE_CONTEXT (qualifying_scope),
11632 TYPE_IDENTIFIER (qualifying_scope));
11633 qualifying_scope = type;
11637 if (unqualified_name)
11641 if (qualifying_scope
11642 && CLASS_TYPE_P (qualifying_scope))
11643 class_type = qualifying_scope;
11645 class_type = current_class_type;
11647 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11649 tree name_type = TREE_TYPE (unqualified_name);
11650 if (class_type && same_type_p (name_type, class_type))
11652 if (qualifying_scope
11653 && CLASSTYPE_USE_TEMPLATE (name_type))
11655 error ("invalid use of constructor as a template");
11656 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11657 "name the constructor in a qualified name",
11659 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11660 class_type, name_type);
11661 declarator = cp_error_declarator;
11665 unqualified_name = constructor_name (class_type);
11669 /* We do not attempt to print the declarator
11670 here because we do not have enough
11671 information about its original syntactic
11673 cp_parser_error (parser, "invalid declarator");
11674 declarator = cp_error_declarator;
11681 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11682 sfk = sfk_destructor;
11683 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11684 sfk = sfk_conversion;
11685 else if (/* There's no way to declare a constructor
11686 for an anonymous type, even if the type
11687 got a name for linkage purposes. */
11688 !TYPE_WAS_ANONYMOUS (class_type)
11689 && constructor_name_p (unqualified_name,
11692 unqualified_name = constructor_name (class_type);
11693 sfk = sfk_constructor;
11696 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11697 *ctor_dtor_or_conv_p = -1;
11700 declarator = make_id_declarator (qualifying_scope,
11703 declarator->id_loc = token->location;
11705 handle_declarator:;
11706 scope = get_scope_of_declarator (declarator);
11708 /* Any names that appear after the declarator-id for a
11709 member are looked up in the containing scope. */
11710 pushed_scope = push_scope (scope);
11711 parser->in_declarator_p = true;
11712 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11713 || (declarator && declarator->kind == cdk_id))
11714 /* Default args are only allowed on function
11716 parser->default_arg_ok_p = saved_default_arg_ok_p;
11718 parser->default_arg_ok_p = false;
11727 /* For an abstract declarator, we might wind up with nothing at this
11728 point. That's an error; the declarator is not optional. */
11730 cp_parser_error (parser, "expected declarator");
11732 /* If we entered a scope, we must exit it now. */
11734 pop_scope (pushed_scope);
11736 parser->default_arg_ok_p = saved_default_arg_ok_p;
11737 parser->in_declarator_p = saved_in_declarator_p;
11742 /* Parse a ptr-operator.
11745 * cv-qualifier-seq [opt]
11747 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11752 & cv-qualifier-seq [opt]
11754 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11755 Returns ADDR_EXPR if a reference was used. In the case of a
11756 pointer-to-member, *TYPE is filled in with the TYPE containing the
11757 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11758 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11759 ERROR_MARK if an error occurred. */
11761 static enum tree_code
11762 cp_parser_ptr_operator (cp_parser* parser,
11764 cp_cv_quals *cv_quals)
11766 enum tree_code code = ERROR_MARK;
11769 /* Assume that it's not a pointer-to-member. */
11771 /* And that there are no cv-qualifiers. */
11772 *cv_quals = TYPE_UNQUALIFIED;
11774 /* Peek at the next token. */
11775 token = cp_lexer_peek_token (parser->lexer);
11776 /* If it's a `*' or `&' we have a pointer or reference. */
11777 if (token->type == CPP_MULT || token->type == CPP_AND)
11779 /* Remember which ptr-operator we were processing. */
11780 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11782 /* Consume the `*' or `&'. */
11783 cp_lexer_consume_token (parser->lexer);
11785 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11786 `&', if we are allowing GNU extensions. (The only qualifier
11787 that can legally appear after `&' is `restrict', but that is
11788 enforced during semantic analysis. */
11789 if (code == INDIRECT_REF
11790 || cp_parser_allow_gnu_extensions_p (parser))
11791 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11795 /* Try the pointer-to-member case. */
11796 cp_parser_parse_tentatively (parser);
11797 /* Look for the optional `::' operator. */
11798 cp_parser_global_scope_opt (parser,
11799 /*current_scope_valid_p=*/false);
11800 /* Look for the nested-name specifier. */
11801 cp_parser_nested_name_specifier (parser,
11802 /*typename_keyword_p=*/false,
11803 /*check_dependency_p=*/true,
11805 /*is_declaration=*/false);
11806 /* If we found it, and the next token is a `*', then we are
11807 indeed looking at a pointer-to-member operator. */
11808 if (!cp_parser_error_occurred (parser)
11809 && cp_parser_require (parser, CPP_MULT, "`*'"))
11811 /* Indicate that the `*' operator was used. */
11812 code = INDIRECT_REF;
11814 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11815 error ("%qD is a namespace", parser->scope);
11818 /* The type of which the member is a member is given by the
11820 *type = parser->scope;
11821 /* The next name will not be qualified. */
11822 parser->scope = NULL_TREE;
11823 parser->qualifying_scope = NULL_TREE;
11824 parser->object_scope = NULL_TREE;
11825 /* Look for the optional cv-qualifier-seq. */
11826 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11829 /* If that didn't work we don't have a ptr-operator. */
11830 if (!cp_parser_parse_definitely (parser))
11831 cp_parser_error (parser, "expected ptr-operator");
11837 /* Parse an (optional) cv-qualifier-seq.
11840 cv-qualifier cv-qualifier-seq [opt]
11851 Returns a bitmask representing the cv-qualifiers. */
11854 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11856 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11861 cp_cv_quals cv_qualifier;
11863 /* Peek at the next token. */
11864 token = cp_lexer_peek_token (parser->lexer);
11865 /* See if it's a cv-qualifier. */
11866 switch (token->keyword)
11869 cv_qualifier = TYPE_QUAL_CONST;
11873 cv_qualifier = TYPE_QUAL_VOLATILE;
11877 cv_qualifier = TYPE_QUAL_RESTRICT;
11881 cv_qualifier = TYPE_UNQUALIFIED;
11888 if (cv_quals & cv_qualifier)
11890 error ("duplicate cv-qualifier");
11891 cp_lexer_purge_token (parser->lexer);
11895 cp_lexer_consume_token (parser->lexer);
11896 cv_quals |= cv_qualifier;
11903 /* Parse a declarator-id.
11907 :: [opt] nested-name-specifier [opt] type-name
11909 In the `id-expression' case, the value returned is as for
11910 cp_parser_id_expression if the id-expression was an unqualified-id.
11911 If the id-expression was a qualified-id, then a SCOPE_REF is
11912 returned. The first operand is the scope (either a NAMESPACE_DECL
11913 or TREE_TYPE), but the second is still just a representation of an
11917 cp_parser_declarator_id (cp_parser* parser)
11920 /* The expression must be an id-expression. Assume that qualified
11921 names are the names of types so that:
11924 int S<T>::R::i = 3;
11926 will work; we must treat `S<T>::R' as the name of a type.
11927 Similarly, assume that qualified names are templates, where
11931 int S<T>::R<T>::i = 3;
11934 id = cp_parser_id_expression (parser,
11935 /*template_keyword_p=*/false,
11936 /*check_dependency_p=*/false,
11937 /*template_p=*/NULL,
11938 /*declarator_p=*/true);
11939 if (BASELINK_P (id))
11940 id = BASELINK_FUNCTIONS (id);
11944 /* Parse a type-id.
11947 type-specifier-seq abstract-declarator [opt]
11949 Returns the TYPE specified. */
11952 cp_parser_type_id (cp_parser* parser)
11954 cp_decl_specifier_seq type_specifier_seq;
11955 cp_declarator *abstract_declarator;
11957 /* Parse the type-specifier-seq. */
11958 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11959 &type_specifier_seq);
11960 if (type_specifier_seq.type == error_mark_node)
11961 return error_mark_node;
11963 /* There might or might not be an abstract declarator. */
11964 cp_parser_parse_tentatively (parser);
11965 /* Look for the declarator. */
11966 abstract_declarator
11967 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11968 /*parenthesized_p=*/NULL,
11969 /*member_p=*/false);
11970 /* Check to see if there really was a declarator. */
11971 if (!cp_parser_parse_definitely (parser))
11972 abstract_declarator = NULL;
11974 return groktypename (&type_specifier_seq, abstract_declarator);
11977 /* Parse a type-specifier-seq.
11979 type-specifier-seq:
11980 type-specifier type-specifier-seq [opt]
11984 type-specifier-seq:
11985 attributes type-specifier-seq [opt]
11987 If IS_CONDITION is true, we are at the start of a "condition",
11988 e.g., we've just seen "if (".
11990 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11993 cp_parser_type_specifier_seq (cp_parser* parser,
11995 cp_decl_specifier_seq *type_specifier_seq)
11997 bool seen_type_specifier = false;
11998 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12000 /* Clear the TYPE_SPECIFIER_SEQ. */
12001 clear_decl_specs (type_specifier_seq);
12003 /* Parse the type-specifiers and attributes. */
12006 tree type_specifier;
12007 bool is_cv_qualifier;
12009 /* Check for attributes first. */
12010 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12012 type_specifier_seq->attributes =
12013 chainon (type_specifier_seq->attributes,
12014 cp_parser_attributes_opt (parser));
12018 /* Look for the type-specifier. */
12019 type_specifier = cp_parser_type_specifier (parser,
12021 type_specifier_seq,
12022 /*is_declaration=*/false,
12025 if (!type_specifier)
12027 /* If the first type-specifier could not be found, this is not a
12028 type-specifier-seq at all. */
12029 if (!seen_type_specifier)
12031 cp_parser_error (parser, "expected type-specifier");
12032 type_specifier_seq->type = error_mark_node;
12035 /* If subsequent type-specifiers could not be found, the
12036 type-specifier-seq is complete. */
12040 seen_type_specifier = true;
12041 /* The standard says that a condition can be:
12043 type-specifier-seq declarator = assignment-expression
12050 we should treat the "S" as a declarator, not as a
12051 type-specifier. The standard doesn't say that explicitly for
12052 type-specifier-seq, but it does say that for
12053 decl-specifier-seq in an ordinary declaration. Perhaps it
12054 would be clearer just to allow a decl-specifier-seq here, and
12055 then add a semantic restriction that if any decl-specifiers
12056 that are not type-specifiers appear, the program is invalid. */
12057 if (is_condition && !is_cv_qualifier)
12058 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12061 cp_parser_check_decl_spec (type_specifier_seq);
12064 /* Parse a parameter-declaration-clause.
12066 parameter-declaration-clause:
12067 parameter-declaration-list [opt] ... [opt]
12068 parameter-declaration-list , ...
12070 Returns a representation for the parameter declarations. A return
12071 value of NULL indicates a parameter-declaration-clause consisting
12072 only of an ellipsis. */
12074 static cp_parameter_declarator *
12075 cp_parser_parameter_declaration_clause (cp_parser* parser)
12077 cp_parameter_declarator *parameters;
12082 /* Peek at the next token. */
12083 token = cp_lexer_peek_token (parser->lexer);
12084 /* Check for trivial parameter-declaration-clauses. */
12085 if (token->type == CPP_ELLIPSIS)
12087 /* Consume the `...' token. */
12088 cp_lexer_consume_token (parser->lexer);
12091 else if (token->type == CPP_CLOSE_PAREN)
12092 /* There are no parameters. */
12094 #ifndef NO_IMPLICIT_EXTERN_C
12095 if (in_system_header && current_class_type == NULL
12096 && current_lang_name == lang_name_c)
12100 return no_parameters;
12102 /* Check for `(void)', too, which is a special case. */
12103 else if (token->keyword == RID_VOID
12104 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12105 == CPP_CLOSE_PAREN))
12107 /* Consume the `void' token. */
12108 cp_lexer_consume_token (parser->lexer);
12109 /* There are no parameters. */
12110 return no_parameters;
12113 /* Parse the parameter-declaration-list. */
12114 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12115 /* If a parse error occurred while parsing the
12116 parameter-declaration-list, then the entire
12117 parameter-declaration-clause is erroneous. */
12121 /* Peek at the next token. */
12122 token = cp_lexer_peek_token (parser->lexer);
12123 /* If it's a `,', the clause should terminate with an ellipsis. */
12124 if (token->type == CPP_COMMA)
12126 /* Consume the `,'. */
12127 cp_lexer_consume_token (parser->lexer);
12128 /* Expect an ellipsis. */
12130 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12132 /* It might also be `...' if the optional trailing `,' was
12134 else if (token->type == CPP_ELLIPSIS)
12136 /* Consume the `...' token. */
12137 cp_lexer_consume_token (parser->lexer);
12138 /* And remember that we saw it. */
12142 ellipsis_p = false;
12144 /* Finish the parameter list. */
12145 if (parameters && ellipsis_p)
12146 parameters->ellipsis_p = true;
12151 /* Parse a parameter-declaration-list.
12153 parameter-declaration-list:
12154 parameter-declaration
12155 parameter-declaration-list , parameter-declaration
12157 Returns a representation of the parameter-declaration-list, as for
12158 cp_parser_parameter_declaration_clause. However, the
12159 `void_list_node' is never appended to the list. Upon return,
12160 *IS_ERROR will be true iff an error occurred. */
12162 static cp_parameter_declarator *
12163 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12165 cp_parameter_declarator *parameters = NULL;
12166 cp_parameter_declarator **tail = ¶meters;
12167 bool saved_in_unbraced_linkage_specification_p;
12169 /* Assume all will go well. */
12171 /* The special considerations that apply to a function within an
12172 unbraced linkage specifications do not apply to the parameters
12173 to the function. */
12174 saved_in_unbraced_linkage_specification_p
12175 = parser->in_unbraced_linkage_specification_p;
12176 parser->in_unbraced_linkage_specification_p = false;
12178 /* Look for more parameters. */
12181 cp_parameter_declarator *parameter;
12182 bool parenthesized_p;
12183 /* Parse the parameter. */
12185 = cp_parser_parameter_declaration (parser,
12186 /*template_parm_p=*/false,
12189 /* If a parse error occurred parsing the parameter declaration,
12190 then the entire parameter-declaration-list is erroneous. */
12197 /* Add the new parameter to the list. */
12199 tail = ¶meter->next;
12201 /* Peek at the next token. */
12202 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12203 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12204 /* These are for Objective-C++ */
12205 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12206 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12207 /* The parameter-declaration-list is complete. */
12209 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12213 /* Peek at the next token. */
12214 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12215 /* If it's an ellipsis, then the list is complete. */
12216 if (token->type == CPP_ELLIPSIS)
12218 /* Otherwise, there must be more parameters. Consume the
12220 cp_lexer_consume_token (parser->lexer);
12221 /* When parsing something like:
12223 int i(float f, double d)
12225 we can tell after seeing the declaration for "f" that we
12226 are not looking at an initialization of a variable "i",
12227 but rather at the declaration of a function "i".
12229 Due to the fact that the parsing of template arguments
12230 (as specified to a template-id) requires backtracking we
12231 cannot use this technique when inside a template argument
12233 if (!parser->in_template_argument_list_p
12234 && !parser->in_type_id_in_expr_p
12235 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12236 /* However, a parameter-declaration of the form
12237 "foat(f)" (which is a valid declaration of a
12238 parameter "f") can also be interpreted as an
12239 expression (the conversion of "f" to "float"). */
12240 && !parenthesized_p)
12241 cp_parser_commit_to_tentative_parse (parser);
12245 cp_parser_error (parser, "expected %<,%> or %<...%>");
12246 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12247 cp_parser_skip_to_closing_parenthesis (parser,
12248 /*recovering=*/true,
12249 /*or_comma=*/false,
12250 /*consume_paren=*/false);
12255 parser->in_unbraced_linkage_specification_p
12256 = saved_in_unbraced_linkage_specification_p;
12261 /* Parse a parameter declaration.
12263 parameter-declaration:
12264 decl-specifier-seq declarator
12265 decl-specifier-seq declarator = assignment-expression
12266 decl-specifier-seq abstract-declarator [opt]
12267 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12269 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12270 declares a template parameter. (In that case, a non-nested `>'
12271 token encountered during the parsing of the assignment-expression
12272 is not interpreted as a greater-than operator.)
12274 Returns a representation of the parameter, or NULL if an error
12275 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12276 true iff the declarator is of the form "(p)". */
12278 static cp_parameter_declarator *
12279 cp_parser_parameter_declaration (cp_parser *parser,
12280 bool template_parm_p,
12281 bool *parenthesized_p)
12283 int declares_class_or_enum;
12284 bool greater_than_is_operator_p;
12285 cp_decl_specifier_seq decl_specifiers;
12286 cp_declarator *declarator;
12287 tree default_argument;
12289 const char *saved_message;
12291 /* In a template parameter, `>' is not an operator.
12295 When parsing a default template-argument for a non-type
12296 template-parameter, the first non-nested `>' is taken as the end
12297 of the template parameter-list rather than a greater-than
12299 greater_than_is_operator_p = !template_parm_p;
12301 /* Type definitions may not appear in parameter types. */
12302 saved_message = parser->type_definition_forbidden_message;
12303 parser->type_definition_forbidden_message
12304 = "types may not be defined in parameter types";
12306 /* Parse the declaration-specifiers. */
12307 cp_parser_decl_specifier_seq (parser,
12308 CP_PARSER_FLAGS_NONE,
12310 &declares_class_or_enum);
12311 /* If an error occurred, there's no reason to attempt to parse the
12312 rest of the declaration. */
12313 if (cp_parser_error_occurred (parser))
12315 parser->type_definition_forbidden_message = saved_message;
12319 /* Peek at the next token. */
12320 token = cp_lexer_peek_token (parser->lexer);
12321 /* If the next token is a `)', `,', `=', `>', or `...', then there
12322 is no declarator. */
12323 if (token->type == CPP_CLOSE_PAREN
12324 || token->type == CPP_COMMA
12325 || token->type == CPP_EQ
12326 || token->type == CPP_ELLIPSIS
12327 || token->type == CPP_GREATER)
12330 if (parenthesized_p)
12331 *parenthesized_p = false;
12333 /* Otherwise, there should be a declarator. */
12336 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12337 parser->default_arg_ok_p = false;
12339 /* After seeing a decl-specifier-seq, if the next token is not a
12340 "(", there is no possibility that the code is a valid
12341 expression. Therefore, if parsing tentatively, we commit at
12343 if (!parser->in_template_argument_list_p
12344 /* In an expression context, having seen:
12348 we cannot be sure whether we are looking at a
12349 function-type (taking a "char" as a parameter) or a cast
12350 of some object of type "char" to "int". */
12351 && !parser->in_type_id_in_expr_p
12352 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12353 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12354 cp_parser_commit_to_tentative_parse (parser);
12355 /* Parse the declarator. */
12356 declarator = cp_parser_declarator (parser,
12357 CP_PARSER_DECLARATOR_EITHER,
12358 /*ctor_dtor_or_conv_p=*/NULL,
12360 /*member_p=*/false);
12361 parser->default_arg_ok_p = saved_default_arg_ok_p;
12362 /* After the declarator, allow more attributes. */
12363 decl_specifiers.attributes
12364 = chainon (decl_specifiers.attributes,
12365 cp_parser_attributes_opt (parser));
12368 /* The restriction on defining new types applies only to the type
12369 of the parameter, not to the default argument. */
12370 parser->type_definition_forbidden_message = saved_message;
12372 /* If the next token is `=', then process a default argument. */
12373 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12375 bool saved_greater_than_is_operator_p;
12376 /* Consume the `='. */
12377 cp_lexer_consume_token (parser->lexer);
12379 /* If we are defining a class, then the tokens that make up the
12380 default argument must be saved and processed later. */
12381 if (!template_parm_p && at_class_scope_p ()
12382 && TYPE_BEING_DEFINED (current_class_type))
12384 unsigned depth = 0;
12385 cp_token *first_token;
12388 /* Add tokens until we have processed the entire default
12389 argument. We add the range [first_token, token). */
12390 first_token = cp_lexer_peek_token (parser->lexer);
12395 /* Peek at the next token. */
12396 token = cp_lexer_peek_token (parser->lexer);
12397 /* What we do depends on what token we have. */
12398 switch (token->type)
12400 /* In valid code, a default argument must be
12401 immediately followed by a `,' `)', or `...'. */
12403 case CPP_CLOSE_PAREN:
12405 /* If we run into a non-nested `;', `}', or `]',
12406 then the code is invalid -- but the default
12407 argument is certainly over. */
12408 case CPP_SEMICOLON:
12409 case CPP_CLOSE_BRACE:
12410 case CPP_CLOSE_SQUARE:
12413 /* Update DEPTH, if necessary. */
12414 else if (token->type == CPP_CLOSE_PAREN
12415 || token->type == CPP_CLOSE_BRACE
12416 || token->type == CPP_CLOSE_SQUARE)
12420 case CPP_OPEN_PAREN:
12421 case CPP_OPEN_SQUARE:
12422 case CPP_OPEN_BRACE:
12427 /* If we see a non-nested `>', and `>' is not an
12428 operator, then it marks the end of the default
12430 if (!depth && !greater_than_is_operator_p)
12434 /* If we run out of tokens, issue an error message. */
12436 error ("file ends in default argument");
12442 /* In these cases, we should look for template-ids.
12443 For example, if the default argument is
12444 `X<int, double>()', we need to do name lookup to
12445 figure out whether or not `X' is a template; if
12446 so, the `,' does not end the default argument.
12448 That is not yet done. */
12455 /* If we've reached the end, stop. */
12459 /* Add the token to the token block. */
12460 token = cp_lexer_consume_token (parser->lexer);
12463 /* Create a DEFAULT_ARG to represented the unparsed default
12465 default_argument = make_node (DEFAULT_ARG);
12466 DEFARG_TOKENS (default_argument)
12467 = cp_token_cache_new (first_token, token);
12468 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12470 /* Outside of a class definition, we can just parse the
12471 assignment-expression. */
12474 bool saved_local_variables_forbidden_p;
12476 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12478 saved_greater_than_is_operator_p
12479 = parser->greater_than_is_operator_p;
12480 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12481 /* Local variable names (and the `this' keyword) may not
12482 appear in a default argument. */
12483 saved_local_variables_forbidden_p
12484 = parser->local_variables_forbidden_p;
12485 parser->local_variables_forbidden_p = true;
12486 /* The default argument expression may cause implicitly
12487 defined member functions to be synthesized, which will
12488 result in garbage collection. We must treat this
12489 situation as if we were within the body of function so as
12490 to avoid collecting live data on the stack. */
12492 /* Parse the assignment-expression. */
12493 if (template_parm_p)
12494 push_deferring_access_checks (dk_no_deferred);
12496 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12497 if (template_parm_p)
12498 pop_deferring_access_checks ();
12499 /* Restore saved state. */
12501 parser->greater_than_is_operator_p
12502 = saved_greater_than_is_operator_p;
12503 parser->local_variables_forbidden_p
12504 = saved_local_variables_forbidden_p;
12506 if (!parser->default_arg_ok_p)
12508 if (!flag_pedantic_errors)
12509 warning (0, "deprecated use of default argument for parameter of non-function");
12512 error ("default arguments are only permitted for function parameters");
12513 default_argument = NULL_TREE;
12518 default_argument = NULL_TREE;
12520 return make_parameter_declarator (&decl_specifiers,
12525 /* Parse a function-body.
12528 compound_statement */
12531 cp_parser_function_body (cp_parser *parser)
12533 cp_parser_compound_statement (parser, NULL, false);
12536 /* Parse a ctor-initializer-opt followed by a function-body. Return
12537 true if a ctor-initializer was present. */
12540 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12543 bool ctor_initializer_p;
12545 /* Begin the function body. */
12546 body = begin_function_body ();
12547 /* Parse the optional ctor-initializer. */
12548 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12549 /* Parse the function-body. */
12550 cp_parser_function_body (parser);
12551 /* Finish the function body. */
12552 finish_function_body (body);
12554 return ctor_initializer_p;
12557 /* Parse an initializer.
12560 = initializer-clause
12561 ( expression-list )
12563 Returns an expression representing the initializer. If no
12564 initializer is present, NULL_TREE is returned.
12566 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12567 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12568 set to FALSE if there is no initializer present. If there is an
12569 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12570 is set to true; otherwise it is set to false. */
12573 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12574 bool* non_constant_p)
12579 /* Peek at the next token. */
12580 token = cp_lexer_peek_token (parser->lexer);
12582 /* Let our caller know whether or not this initializer was
12584 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12585 /* Assume that the initializer is constant. */
12586 *non_constant_p = false;
12588 if (token->type == CPP_EQ)
12590 /* Consume the `='. */
12591 cp_lexer_consume_token (parser->lexer);
12592 /* Parse the initializer-clause. */
12593 init = cp_parser_initializer_clause (parser, non_constant_p);
12595 else if (token->type == CPP_OPEN_PAREN)
12596 init = cp_parser_parenthesized_expression_list (parser, false,
12601 /* Anything else is an error. */
12602 cp_parser_error (parser, "expected initializer");
12603 init = error_mark_node;
12609 /* Parse an initializer-clause.
12611 initializer-clause:
12612 assignment-expression
12613 { initializer-list , [opt] }
12616 Returns an expression representing the initializer.
12618 If the `assignment-expression' production is used the value
12619 returned is simply a representation for the expression.
12621 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12622 the elements of the initializer-list (or NULL, if the last
12623 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12624 NULL_TREE. There is no way to detect whether or not the optional
12625 trailing `,' was provided. NON_CONSTANT_P is as for
12626 cp_parser_initializer. */
12629 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12633 /* Assume the expression is constant. */
12634 *non_constant_p = false;
12636 /* If it is not a `{', then we are looking at an
12637 assignment-expression. */
12638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12641 = cp_parser_constant_expression (parser,
12642 /*allow_non_constant_p=*/true,
12644 if (!*non_constant_p)
12645 initializer = fold_non_dependent_expr (initializer);
12649 /* Consume the `{' token. */
12650 cp_lexer_consume_token (parser->lexer);
12651 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12652 initializer = make_node (CONSTRUCTOR);
12653 /* If it's not a `}', then there is a non-trivial initializer. */
12654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12656 /* Parse the initializer list. */
12657 CONSTRUCTOR_ELTS (initializer)
12658 = cp_parser_initializer_list (parser, non_constant_p);
12659 /* A trailing `,' token is allowed. */
12660 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12661 cp_lexer_consume_token (parser->lexer);
12663 /* Now, there should be a trailing `}'. */
12664 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12667 return initializer;
12670 /* Parse an initializer-list.
12674 initializer-list , initializer-clause
12679 identifier : initializer-clause
12680 initializer-list, identifier : initializer-clause
12682 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12683 for the initializer. If the INDEX of the elt is non-NULL, it is the
12684 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12685 as for cp_parser_initializer. */
12687 static VEC(constructor_elt,gc) *
12688 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12690 VEC(constructor_elt,gc) *v = NULL;
12692 /* Assume all of the expressions are constant. */
12693 *non_constant_p = false;
12695 /* Parse the rest of the list. */
12701 bool clause_non_constant_p;
12703 /* If the next token is an identifier and the following one is a
12704 colon, we are looking at the GNU designated-initializer
12706 if (cp_parser_allow_gnu_extensions_p (parser)
12707 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12708 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12710 /* Consume the identifier. */
12711 identifier = cp_lexer_consume_token (parser->lexer)->value;
12712 /* Consume the `:'. */
12713 cp_lexer_consume_token (parser->lexer);
12716 identifier = NULL_TREE;
12718 /* Parse the initializer. */
12719 initializer = cp_parser_initializer_clause (parser,
12720 &clause_non_constant_p);
12721 /* If any clause is non-constant, so is the entire initializer. */
12722 if (clause_non_constant_p)
12723 *non_constant_p = true;
12725 /* Add it to the vector. */
12726 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12728 /* If the next token is not a comma, we have reached the end of
12730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12733 /* Peek at the next token. */
12734 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12735 /* If the next token is a `}', then we're still done. An
12736 initializer-clause can have a trailing `,' after the
12737 initializer-list and before the closing `}'. */
12738 if (token->type == CPP_CLOSE_BRACE)
12741 /* Consume the `,' token. */
12742 cp_lexer_consume_token (parser->lexer);
12748 /* Classes [gram.class] */
12750 /* Parse a class-name.
12756 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12757 to indicate that names looked up in dependent types should be
12758 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12759 keyword has been used to indicate that the name that appears next
12760 is a template. TAG_TYPE indicates the explicit tag given before
12761 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12762 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12763 is the class being defined in a class-head.
12765 Returns the TYPE_DECL representing the class. */
12768 cp_parser_class_name (cp_parser *parser,
12769 bool typename_keyword_p,
12770 bool template_keyword_p,
12771 enum tag_types tag_type,
12772 bool check_dependency_p,
12774 bool is_declaration)
12781 /* All class-names start with an identifier. */
12782 token = cp_lexer_peek_token (parser->lexer);
12783 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12785 cp_parser_error (parser, "expected class-name");
12786 return error_mark_node;
12789 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12790 to a template-id, so we save it here. */
12791 scope = parser->scope;
12792 if (scope == error_mark_node)
12793 return error_mark_node;
12795 /* Any name names a type if we're following the `typename' keyword
12796 in a qualified name where the enclosing scope is type-dependent. */
12797 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12798 && dependent_type_p (scope));
12799 /* Handle the common case (an identifier, but not a template-id)
12801 if (token->type == CPP_NAME
12802 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12804 cp_token *identifier_token;
12808 /* Look for the identifier. */
12809 identifier_token = cp_lexer_peek_token (parser->lexer);
12810 ambiguous_p = identifier_token->ambiguous_p;
12811 identifier = cp_parser_identifier (parser);
12812 /* If the next token isn't an identifier, we are certainly not
12813 looking at a class-name. */
12814 if (identifier == error_mark_node)
12815 decl = error_mark_node;
12816 /* If we know this is a type-name, there's no need to look it
12818 else if (typename_p)
12822 tree ambiguous_decls;
12823 /* If we already know that this lookup is ambiguous, then
12824 we've already issued an error message; there's no reason
12828 cp_parser_simulate_error (parser);
12829 return error_mark_node;
12831 /* If the next token is a `::', then the name must be a type
12834 [basic.lookup.qual]
12836 During the lookup for a name preceding the :: scope
12837 resolution operator, object, function, and enumerator
12838 names are ignored. */
12839 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12840 tag_type = typename_type;
12841 /* Look up the name. */
12842 decl = cp_parser_lookup_name (parser, identifier,
12844 /*is_template=*/false,
12845 /*is_namespace=*/false,
12846 check_dependency_p,
12848 if (ambiguous_decls)
12850 error ("reference to %qD is ambiguous", identifier);
12851 print_candidates (ambiguous_decls);
12852 if (cp_parser_parsing_tentatively (parser))
12854 identifier_token->ambiguous_p = true;
12855 cp_parser_simulate_error (parser);
12857 return error_mark_node;
12863 /* Try a template-id. */
12864 decl = cp_parser_template_id (parser, template_keyword_p,
12865 check_dependency_p,
12867 if (decl == error_mark_node)
12868 return error_mark_node;
12871 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12873 /* If this is a typename, create a TYPENAME_TYPE. */
12874 if (typename_p && decl != error_mark_node)
12876 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12877 if (decl != error_mark_node)
12878 decl = TYPE_NAME (decl);
12881 /* Check to see that it is really the name of a class. */
12882 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12883 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12884 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12885 /* Situations like this:
12887 template <typename T> struct A {
12888 typename T::template X<int>::I i;
12891 are problematic. Is `T::template X<int>' a class-name? The
12892 standard does not seem to be definitive, but there is no other
12893 valid interpretation of the following `::'. Therefore, those
12894 names are considered class-names. */
12896 decl = make_typename_type (scope, decl, tag_type, tf_error);
12897 if (decl != error_mark_node)
12898 decl = TYPE_NAME (decl);
12900 else if (TREE_CODE (decl) != TYPE_DECL
12901 || TREE_TYPE (decl) == error_mark_node
12902 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12903 decl = error_mark_node;
12905 if (decl == error_mark_node)
12906 cp_parser_error (parser, "expected class-name");
12911 /* Parse a class-specifier.
12914 class-head { member-specification [opt] }
12916 Returns the TREE_TYPE representing the class. */
12919 cp_parser_class_specifier (cp_parser* parser)
12923 tree attributes = NULL_TREE;
12924 int has_trailing_semicolon;
12925 bool nested_name_specifier_p;
12926 unsigned saved_num_template_parameter_lists;
12927 bool saved_in_function_body;
12928 tree old_scope = NULL_TREE;
12929 tree scope = NULL_TREE;
12930 tree bases = NULL_TREE;
12932 push_deferring_access_checks (dk_no_deferred);
12934 /* Parse the class-head. */
12935 type = cp_parser_class_head (parser,
12936 &nested_name_specifier_p,
12939 /* If the class-head was a semantic disaster, skip the entire body
12943 cp_parser_skip_to_end_of_block_or_statement (parser);
12944 pop_deferring_access_checks ();
12945 return error_mark_node;
12948 /* Look for the `{'. */
12949 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12951 pop_deferring_access_checks ();
12952 return error_mark_node;
12955 /* Process the base classes. */
12956 xref_basetypes (type, bases);
12958 /* Issue an error message if type-definitions are forbidden here. */
12959 cp_parser_check_type_definition (parser);
12960 /* Remember that we are defining one more class. */
12961 ++parser->num_classes_being_defined;
12962 /* Inside the class, surrounding template-parameter-lists do not
12964 saved_num_template_parameter_lists
12965 = parser->num_template_parameter_lists;
12966 parser->num_template_parameter_lists = 0;
12967 /* We are not in a function body. */
12968 saved_in_function_body = parser->in_function_body;
12969 parser->in_function_body = false;
12971 /* Start the class. */
12972 if (nested_name_specifier_p)
12974 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12975 old_scope = push_inner_scope (scope);
12977 type = begin_class_definition (type);
12979 if (type == error_mark_node)
12980 /* If the type is erroneous, skip the entire body of the class. */
12981 cp_parser_skip_to_closing_brace (parser);
12983 /* Parse the member-specification. */
12984 cp_parser_member_specification_opt (parser);
12986 /* Look for the trailing `}'. */
12987 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12988 /* We get better error messages by noticing a common problem: a
12989 missing trailing `;'. */
12990 token = cp_lexer_peek_token (parser->lexer);
12991 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12992 /* Look for trailing attributes to apply to this class. */
12993 if (cp_parser_allow_gnu_extensions_p (parser))
12995 tree sub_attr = cp_parser_attributes_opt (parser);
12996 attributes = chainon (attributes, sub_attr);
12998 if (type != error_mark_node)
12999 type = finish_struct (type, attributes);
13000 if (nested_name_specifier_p)
13001 pop_inner_scope (old_scope, scope);
13002 /* If this class is not itself within the scope of another class,
13003 then we need to parse the bodies of all of the queued function
13004 definitions. Note that the queued functions defined in a class
13005 are not always processed immediately following the
13006 class-specifier for that class. Consider:
13009 struct B { void f() { sizeof (A); } };
13012 If `f' were processed before the processing of `A' were
13013 completed, there would be no way to compute the size of `A'.
13014 Note that the nesting we are interested in here is lexical --
13015 not the semantic nesting given by TYPE_CONTEXT. In particular,
13018 struct A { struct B; };
13019 struct A::B { void f() { } };
13021 there is no need to delay the parsing of `A::B::f'. */
13022 if (--parser->num_classes_being_defined == 0)
13026 tree class_type = NULL_TREE;
13027 tree pushed_scope = NULL_TREE;
13029 /* In a first pass, parse default arguments to the functions.
13030 Then, in a second pass, parse the bodies of the functions.
13031 This two-phased approach handles cases like:
13039 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13040 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13041 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13042 TREE_PURPOSE (parser->unparsed_functions_queues)
13043 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13045 fn = TREE_VALUE (queue_entry);
13046 /* If there are default arguments that have not yet been processed,
13047 take care of them now. */
13048 if (class_type != TREE_PURPOSE (queue_entry))
13051 pop_scope (pushed_scope);
13052 class_type = TREE_PURPOSE (queue_entry);
13053 pushed_scope = push_scope (class_type);
13055 /* Make sure that any template parameters are in scope. */
13056 maybe_begin_member_template_processing (fn);
13057 /* Parse the default argument expressions. */
13058 cp_parser_late_parsing_default_args (parser, fn);
13059 /* Remove any template parameters from the symbol table. */
13060 maybe_end_member_template_processing ();
13063 pop_scope (pushed_scope);
13064 /* Now parse the body of the functions. */
13065 for (TREE_VALUE (parser->unparsed_functions_queues)
13066 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13067 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13068 TREE_VALUE (parser->unparsed_functions_queues)
13069 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13071 /* Figure out which function we need to process. */
13072 fn = TREE_VALUE (queue_entry);
13073 /* Parse the function. */
13074 cp_parser_late_parsing_for_member (parser, fn);
13078 /* Put back any saved access checks. */
13079 pop_deferring_access_checks ();
13081 /* Restore saved state. */
13082 parser->in_function_body = saved_in_function_body;
13083 parser->num_template_parameter_lists
13084 = saved_num_template_parameter_lists;
13089 /* Parse a class-head.
13092 class-key identifier [opt] base-clause [opt]
13093 class-key nested-name-specifier identifier base-clause [opt]
13094 class-key nested-name-specifier [opt] template-id
13098 class-key attributes identifier [opt] base-clause [opt]
13099 class-key attributes nested-name-specifier identifier base-clause [opt]
13100 class-key attributes nested-name-specifier [opt] template-id
13103 Returns the TYPE of the indicated class. Sets
13104 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13105 involving a nested-name-specifier was used, and FALSE otherwise.
13107 Returns error_mark_node if this is not a class-head.
13109 Returns NULL_TREE if the class-head is syntactically valid, but
13110 semantically invalid in a way that means we should skip the entire
13111 body of the class. */
13114 cp_parser_class_head (cp_parser* parser,
13115 bool* nested_name_specifier_p,
13116 tree *attributes_p,
13119 tree nested_name_specifier;
13120 enum tag_types class_key;
13121 tree id = NULL_TREE;
13122 tree type = NULL_TREE;
13124 bool template_id_p = false;
13125 bool qualified_p = false;
13126 bool invalid_nested_name_p = false;
13127 bool invalid_explicit_specialization_p = false;
13128 tree pushed_scope = NULL_TREE;
13129 unsigned num_templates;
13131 /* Assume no nested-name-specifier will be present. */
13132 *nested_name_specifier_p = false;
13133 /* Assume no template parameter lists will be used in defining the
13137 /* Look for the class-key. */
13138 class_key = cp_parser_class_key (parser);
13139 if (class_key == none_type)
13140 return error_mark_node;
13142 /* Parse the attributes. */
13143 attributes = cp_parser_attributes_opt (parser);
13145 /* If the next token is `::', that is invalid -- but sometimes
13146 people do try to write:
13150 Handle this gracefully by accepting the extra qualifier, and then
13151 issuing an error about it later if this really is a
13152 class-head. If it turns out just to be an elaborated type
13153 specifier, remain silent. */
13154 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13155 qualified_p = true;
13157 push_deferring_access_checks (dk_no_check);
13159 /* Determine the name of the class. Begin by looking for an
13160 optional nested-name-specifier. */
13161 nested_name_specifier
13162 = cp_parser_nested_name_specifier_opt (parser,
13163 /*typename_keyword_p=*/false,
13164 /*check_dependency_p=*/false,
13166 /*is_declaration=*/false);
13167 /* If there was a nested-name-specifier, then there *must* be an
13169 if (nested_name_specifier)
13171 /* Although the grammar says `identifier', it really means
13172 `class-name' or `template-name'. You are only allowed to
13173 define a class that has already been declared with this
13176 The proposed resolution for Core Issue 180 says that whever
13177 you see `class T::X' you should treat `X' as a type-name.
13179 It is OK to define an inaccessible class; for example:
13181 class A { class B; };
13184 We do not know if we will see a class-name, or a
13185 template-name. We look for a class-name first, in case the
13186 class-name is a template-id; if we looked for the
13187 template-name first we would stop after the template-name. */
13188 cp_parser_parse_tentatively (parser);
13189 type = cp_parser_class_name (parser,
13190 /*typename_keyword_p=*/false,
13191 /*template_keyword_p=*/false,
13193 /*check_dependency_p=*/false,
13194 /*class_head_p=*/true,
13195 /*is_declaration=*/false);
13196 /* If that didn't work, ignore the nested-name-specifier. */
13197 if (!cp_parser_parse_definitely (parser))
13199 invalid_nested_name_p = true;
13200 id = cp_parser_identifier (parser);
13201 if (id == error_mark_node)
13204 /* If we could not find a corresponding TYPE, treat this
13205 declaration like an unqualified declaration. */
13206 if (type == error_mark_node)
13207 nested_name_specifier = NULL_TREE;
13208 /* Otherwise, count the number of templates used in TYPE and its
13209 containing scopes. */
13214 for (scope = TREE_TYPE (type);
13215 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13216 scope = (TYPE_P (scope)
13217 ? TYPE_CONTEXT (scope)
13218 : DECL_CONTEXT (scope)))
13220 && CLASS_TYPE_P (scope)
13221 && CLASSTYPE_TEMPLATE_INFO (scope)
13222 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13223 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13227 /* Otherwise, the identifier is optional. */
13230 /* We don't know whether what comes next is a template-id,
13231 an identifier, or nothing at all. */
13232 cp_parser_parse_tentatively (parser);
13233 /* Check for a template-id. */
13234 id = cp_parser_template_id (parser,
13235 /*template_keyword_p=*/false,
13236 /*check_dependency_p=*/true,
13237 /*is_declaration=*/true);
13238 /* If that didn't work, it could still be an identifier. */
13239 if (!cp_parser_parse_definitely (parser))
13241 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13242 id = cp_parser_identifier (parser);
13248 template_id_p = true;
13253 pop_deferring_access_checks ();
13256 cp_parser_check_for_invalid_template_id (parser, id);
13258 /* If it's not a `:' or a `{' then we can't really be looking at a
13259 class-head, since a class-head only appears as part of a
13260 class-specifier. We have to detect this situation before calling
13261 xref_tag, since that has irreversible side-effects. */
13262 if (!cp_parser_next_token_starts_class_definition_p (parser))
13264 cp_parser_error (parser, "expected %<{%> or %<:%>");
13265 return error_mark_node;
13268 /* At this point, we're going ahead with the class-specifier, even
13269 if some other problem occurs. */
13270 cp_parser_commit_to_tentative_parse (parser);
13271 /* Issue the error about the overly-qualified name now. */
13273 cp_parser_error (parser,
13274 "global qualification of class name is invalid");
13275 else if (invalid_nested_name_p)
13276 cp_parser_error (parser,
13277 "qualified name does not name a class");
13278 else if (nested_name_specifier)
13282 /* Reject typedef-names in class heads. */
13283 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13285 error ("invalid class name in declaration of %qD", type);
13290 /* Figure out in what scope the declaration is being placed. */
13291 scope = current_scope ();
13292 /* If that scope does not contain the scope in which the
13293 class was originally declared, the program is invalid. */
13294 if (scope && !is_ancestor (scope, nested_name_specifier))
13296 error ("declaration of %qD in %qD which does not enclose %qD",
13297 type, scope, nested_name_specifier);
13303 A declarator-id shall not be qualified exception of the
13304 definition of a ... nested class outside of its class
13305 ... [or] a the definition or explicit instantiation of a
13306 class member of a namespace outside of its namespace. */
13307 if (scope == nested_name_specifier)
13309 pedwarn ("extra qualification ignored");
13310 nested_name_specifier = NULL_TREE;
13314 /* An explicit-specialization must be preceded by "template <>". If
13315 it is not, try to recover gracefully. */
13316 if (at_namespace_scope_p ()
13317 && parser->num_template_parameter_lists == 0
13320 error ("an explicit specialization must be preceded by %<template <>%>");
13321 invalid_explicit_specialization_p = true;
13322 /* Take the same action that would have been taken by
13323 cp_parser_explicit_specialization. */
13324 ++parser->num_template_parameter_lists;
13325 begin_specialization ();
13327 /* There must be no "return" statements between this point and the
13328 end of this function; set "type "to the correct return value and
13329 use "goto done;" to return. */
13330 /* Make sure that the right number of template parameters were
13332 if (!cp_parser_check_template_parameters (parser, num_templates))
13334 /* If something went wrong, there is no point in even trying to
13335 process the class-definition. */
13340 /* Look up the type. */
13343 type = TREE_TYPE (id);
13344 type = maybe_process_partial_specialization (type);
13345 if (nested_name_specifier)
13346 pushed_scope = push_scope (nested_name_specifier);
13348 else if (nested_name_specifier)
13354 template <typename T> struct S { struct T };
13355 template <typename T> struct S<T>::T { };
13357 we will get a TYPENAME_TYPE when processing the definition of
13358 `S::T'. We need to resolve it to the actual type before we
13359 try to define it. */
13360 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13362 class_type = resolve_typename_type (TREE_TYPE (type),
13363 /*only_current_p=*/false);
13364 if (class_type != error_mark_node)
13365 type = TYPE_NAME (class_type);
13368 cp_parser_error (parser, "could not resolve typename type");
13369 type = error_mark_node;
13373 maybe_process_partial_specialization (TREE_TYPE (type));
13374 class_type = current_class_type;
13375 /* Enter the scope indicated by the nested-name-specifier. */
13376 pushed_scope = push_scope (nested_name_specifier);
13377 /* Get the canonical version of this type. */
13378 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13379 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13380 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13382 type = push_template_decl (type);
13383 if (type == error_mark_node)
13390 type = TREE_TYPE (type);
13391 *nested_name_specifier_p = true;
13393 else /* The name is not a nested name. */
13395 /* If the class was unnamed, create a dummy name. */
13397 id = make_anon_name ();
13398 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13399 parser->num_template_parameter_lists);
13402 /* Indicate whether this class was declared as a `class' or as a
13404 if (TREE_CODE (type) == RECORD_TYPE)
13405 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13406 cp_parser_check_class_key (class_key, type);
13408 /* If this type was already complete, and we see another definition,
13409 that's an error. */
13410 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13412 error ("redefinition of %q#T", type);
13413 error ("previous definition of %q+#T", type);
13418 /* We will have entered the scope containing the class; the names of
13419 base classes should be looked up in that context. For example:
13421 struct A { struct B {}; struct C; };
13422 struct A::C : B {};
13425 *bases = NULL_TREE;
13427 /* Get the list of base-classes, if there is one. */
13428 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13429 *bases = cp_parser_base_clause (parser);
13432 /* Leave the scope given by the nested-name-specifier. We will
13433 enter the class scope itself while processing the members. */
13435 pop_scope (pushed_scope);
13437 if (invalid_explicit_specialization_p)
13439 end_specialization ();
13440 --parser->num_template_parameter_lists;
13442 *attributes_p = attributes;
13446 /* Parse a class-key.
13453 Returns the kind of class-key specified, or none_type to indicate
13456 static enum tag_types
13457 cp_parser_class_key (cp_parser* parser)
13460 enum tag_types tag_type;
13462 /* Look for the class-key. */
13463 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13467 /* Check to see if the TOKEN is a class-key. */
13468 tag_type = cp_parser_token_is_class_key (token);
13470 cp_parser_error (parser, "expected class-key");
13474 /* Parse an (optional) member-specification.
13476 member-specification:
13477 member-declaration member-specification [opt]
13478 access-specifier : member-specification [opt] */
13481 cp_parser_member_specification_opt (cp_parser* parser)
13488 /* Peek at the next token. */
13489 token = cp_lexer_peek_token (parser->lexer);
13490 /* If it's a `}', or EOF then we've seen all the members. */
13491 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13494 /* See if this token is a keyword. */
13495 keyword = token->keyword;
13499 case RID_PROTECTED:
13501 /* Consume the access-specifier. */
13502 cp_lexer_consume_token (parser->lexer);
13503 /* Remember which access-specifier is active. */
13504 current_access_specifier = token->value;
13505 /* Look for the `:'. */
13506 cp_parser_require (parser, CPP_COLON, "`:'");
13510 /* Accept #pragmas at class scope. */
13511 if (token->type == CPP_PRAGMA)
13513 cp_lexer_handle_pragma (parser->lexer);
13517 /* Otherwise, the next construction must be a
13518 member-declaration. */
13519 cp_parser_member_declaration (parser);
13524 /* Parse a member-declaration.
13526 member-declaration:
13527 decl-specifier-seq [opt] member-declarator-list [opt] ;
13528 function-definition ; [opt]
13529 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13531 template-declaration
13533 member-declarator-list:
13535 member-declarator-list , member-declarator
13538 declarator pure-specifier [opt]
13539 declarator constant-initializer [opt]
13540 identifier [opt] : constant-expression
13544 member-declaration:
13545 __extension__ member-declaration
13548 declarator attributes [opt] pure-specifier [opt]
13549 declarator attributes [opt] constant-initializer [opt]
13550 identifier [opt] attributes [opt] : constant-expression */
13553 cp_parser_member_declaration (cp_parser* parser)
13555 cp_decl_specifier_seq decl_specifiers;
13556 tree prefix_attributes;
13558 int declares_class_or_enum;
13561 int saved_pedantic;
13563 /* Check for the `__extension__' keyword. */
13564 if (cp_parser_extension_opt (parser, &saved_pedantic))
13567 cp_parser_member_declaration (parser);
13568 /* Restore the old value of the PEDANTIC flag. */
13569 pedantic = saved_pedantic;
13574 /* Check for a template-declaration. */
13575 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13577 /* An explicit specialization here is an error condition, and we
13578 expect the specialization handler to detect and report this. */
13579 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13580 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13581 cp_parser_explicit_specialization (parser);
13583 cp_parser_template_declaration (parser, /*member_p=*/true);
13588 /* Check for a using-declaration. */
13589 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13591 /* Parse the using-declaration. */
13592 cp_parser_using_declaration (parser,
13593 /*access_declaration_p=*/false);
13597 /* Check for @defs. */
13598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13601 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13602 ivar = ivar_chains;
13606 ivar = TREE_CHAIN (member);
13607 TREE_CHAIN (member) = NULL_TREE;
13608 finish_member_declaration (member);
13613 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13616 /* Parse the decl-specifier-seq. */
13617 cp_parser_decl_specifier_seq (parser,
13618 CP_PARSER_FLAGS_OPTIONAL,
13620 &declares_class_or_enum);
13621 prefix_attributes = decl_specifiers.attributes;
13622 decl_specifiers.attributes = NULL_TREE;
13623 /* Check for an invalid type-name. */
13624 if (!decl_specifiers.type
13625 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13627 /* If there is no declarator, then the decl-specifier-seq should
13629 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13631 /* If there was no decl-specifier-seq, and the next token is a
13632 `;', then we have something like:
13638 Each member-declaration shall declare at least one member
13639 name of the class. */
13640 if (!decl_specifiers.any_specifiers_p)
13642 cp_token *token = cp_lexer_peek_token (parser->lexer);
13643 if (pedantic && !token->in_system_header)
13644 pedwarn ("%Hextra %<;%>", &token->location);
13650 /* See if this declaration is a friend. */
13651 friend_p = cp_parser_friend_p (&decl_specifiers);
13652 /* If there were decl-specifiers, check to see if there was
13653 a class-declaration. */
13654 type = check_tag_decl (&decl_specifiers);
13655 /* Nested classes have already been added to the class, but
13656 a `friend' needs to be explicitly registered. */
13659 /* If the `friend' keyword was present, the friend must
13660 be introduced with a class-key. */
13661 if (!declares_class_or_enum)
13662 error ("a class-key must be used when declaring a friend");
13665 template <typename T> struct A {
13666 friend struct A<T>::B;
13669 A<T>::B will be represented by a TYPENAME_TYPE, and
13670 therefore not recognized by check_tag_decl. */
13672 && decl_specifiers.type
13673 && TYPE_P (decl_specifiers.type))
13674 type = decl_specifiers.type;
13675 if (!type || !TYPE_P (type))
13676 error ("friend declaration does not name a class or "
13679 make_friend_class (current_class_type, type,
13680 /*complain=*/true);
13682 /* If there is no TYPE, an error message will already have
13684 else if (!type || type == error_mark_node)
13686 /* An anonymous aggregate has to be handled specially; such
13687 a declaration really declares a data member (with a
13688 particular type), as opposed to a nested class. */
13689 else if (ANON_AGGR_TYPE_P (type))
13691 /* Remove constructors and such from TYPE, now that we
13692 know it is an anonymous aggregate. */
13693 fixup_anonymous_aggr (type);
13694 /* And make the corresponding data member. */
13695 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13696 /* Add it to the class. */
13697 finish_member_declaration (decl);
13700 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13705 /* See if these declarations will be friends. */
13706 friend_p = cp_parser_friend_p (&decl_specifiers);
13708 /* Keep going until we hit the `;' at the end of the
13710 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13712 tree attributes = NULL_TREE;
13713 tree first_attribute;
13715 /* Peek at the next token. */
13716 token = cp_lexer_peek_token (parser->lexer);
13718 /* Check for a bitfield declaration. */
13719 if (token->type == CPP_COLON
13720 || (token->type == CPP_NAME
13721 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13727 /* Get the name of the bitfield. Note that we cannot just
13728 check TOKEN here because it may have been invalidated by
13729 the call to cp_lexer_peek_nth_token above. */
13730 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13731 identifier = cp_parser_identifier (parser);
13733 identifier = NULL_TREE;
13735 /* Consume the `:' token. */
13736 cp_lexer_consume_token (parser->lexer);
13737 /* Get the width of the bitfield. */
13739 = cp_parser_constant_expression (parser,
13740 /*allow_non_constant=*/false,
13743 /* Look for attributes that apply to the bitfield. */
13744 attributes = cp_parser_attributes_opt (parser);
13745 /* Remember which attributes are prefix attributes and
13747 first_attribute = attributes;
13748 /* Combine the attributes. */
13749 attributes = chainon (prefix_attributes, attributes);
13751 /* Create the bitfield declaration. */
13752 decl = grokbitfield (identifier
13753 ? make_id_declarator (NULL_TREE,
13759 /* Apply the attributes. */
13760 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13764 cp_declarator *declarator;
13766 tree asm_specification;
13767 int ctor_dtor_or_conv_p;
13769 /* Parse the declarator. */
13771 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13772 &ctor_dtor_or_conv_p,
13773 /*parenthesized_p=*/NULL,
13774 /*member_p=*/true);
13776 /* If something went wrong parsing the declarator, make sure
13777 that we at least consume some tokens. */
13778 if (declarator == cp_error_declarator)
13780 /* Skip to the end of the statement. */
13781 cp_parser_skip_to_end_of_statement (parser);
13782 /* If the next token is not a semicolon, that is
13783 probably because we just skipped over the body of
13784 a function. So, we consume a semicolon if
13785 present, but do not issue an error message if it
13787 if (cp_lexer_next_token_is (parser->lexer,
13789 cp_lexer_consume_token (parser->lexer);
13793 if (declares_class_or_enum & 2)
13794 cp_parser_check_for_definition_in_return_type
13795 (declarator, decl_specifiers.type);
13797 /* Look for an asm-specification. */
13798 asm_specification = cp_parser_asm_specification_opt (parser);
13799 /* Look for attributes that apply to the declaration. */
13800 attributes = cp_parser_attributes_opt (parser);
13801 /* Remember which attributes are prefix attributes and
13803 first_attribute = attributes;
13804 /* Combine the attributes. */
13805 attributes = chainon (prefix_attributes, attributes);
13807 /* If it's an `=', then we have a constant-initializer or a
13808 pure-specifier. It is not correct to parse the
13809 initializer before registering the member declaration
13810 since the member declaration should be in scope while
13811 its initializer is processed. However, the rest of the
13812 front end does not yet provide an interface that allows
13813 us to handle this correctly. */
13814 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13818 A pure-specifier shall be used only in the declaration of
13819 a virtual function.
13821 A member-declarator can contain a constant-initializer
13822 only if it declares a static member of integral or
13825 Therefore, if the DECLARATOR is for a function, we look
13826 for a pure-specifier; otherwise, we look for a
13827 constant-initializer. When we call `grokfield', it will
13828 perform more stringent semantics checks. */
13829 if (function_declarator_p (declarator))
13830 initializer = cp_parser_pure_specifier (parser);
13832 /* Parse the initializer. */
13833 initializer = cp_parser_constant_initializer (parser);
13835 /* Otherwise, there is no initializer. */
13837 initializer = NULL_TREE;
13839 /* See if we are probably looking at a function
13840 definition. We are certainly not looking at a
13841 member-declarator. Calling `grokfield' has
13842 side-effects, so we must not do it unless we are sure
13843 that we are looking at a member-declarator. */
13844 if (cp_parser_token_starts_function_definition_p
13845 (cp_lexer_peek_token (parser->lexer)))
13847 /* The grammar does not allow a pure-specifier to be
13848 used when a member function is defined. (It is
13849 possible that this fact is an oversight in the
13850 standard, since a pure function may be defined
13851 outside of the class-specifier. */
13853 error ("pure-specifier on function-definition");
13854 decl = cp_parser_save_member_function_body (parser,
13858 /* If the member was not a friend, declare it here. */
13860 finish_member_declaration (decl);
13861 /* Peek at the next token. */
13862 token = cp_lexer_peek_token (parser->lexer);
13863 /* If the next token is a semicolon, consume it. */
13864 if (token->type == CPP_SEMICOLON)
13865 cp_lexer_consume_token (parser->lexer);
13869 /* Create the declaration. */
13870 decl = grokfield (declarator, &decl_specifiers,
13871 initializer, /*init_const_expr_p=*/true,
13876 /* Reset PREFIX_ATTRIBUTES. */
13877 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13878 attributes = TREE_CHAIN (attributes);
13880 TREE_CHAIN (attributes) = NULL_TREE;
13882 /* If there is any qualification still in effect, clear it
13883 now; we will be starting fresh with the next declarator. */
13884 parser->scope = NULL_TREE;
13885 parser->qualifying_scope = NULL_TREE;
13886 parser->object_scope = NULL_TREE;
13887 /* If it's a `,', then there are more declarators. */
13888 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13889 cp_lexer_consume_token (parser->lexer);
13890 /* If the next token isn't a `;', then we have a parse error. */
13891 else if (cp_lexer_next_token_is_not (parser->lexer,
13894 cp_parser_error (parser, "expected %<;%>");
13895 /* Skip tokens until we find a `;'. */
13896 cp_parser_skip_to_end_of_statement (parser);
13903 /* Add DECL to the list of members. */
13905 finish_member_declaration (decl);
13907 if (TREE_CODE (decl) == FUNCTION_DECL)
13908 cp_parser_save_default_args (parser, decl);
13913 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13916 /* Parse a pure-specifier.
13921 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13922 Otherwise, ERROR_MARK_NODE is returned. */
13925 cp_parser_pure_specifier (cp_parser* parser)
13929 /* Look for the `=' token. */
13930 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13931 return error_mark_node;
13932 /* Look for the `0' token. */
13933 token = cp_lexer_consume_token (parser->lexer);
13934 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13936 cp_parser_error (parser,
13937 "invalid pure specifier (only `= 0' is allowed)");
13938 cp_parser_skip_to_end_of_statement (parser);
13939 return error_mark_node;
13942 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13943 We need to get information from the lexer about how the number
13944 was spelled in order to fix this problem. */
13945 return integer_zero_node;
13948 /* Parse a constant-initializer.
13950 constant-initializer:
13951 = constant-expression
13953 Returns a representation of the constant-expression. */
13956 cp_parser_constant_initializer (cp_parser* parser)
13958 /* Look for the `=' token. */
13959 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13960 return error_mark_node;
13962 /* It is invalid to write:
13964 struct S { static const int i = { 7 }; };
13967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13969 cp_parser_error (parser,
13970 "a brace-enclosed initializer is not allowed here");
13971 /* Consume the opening brace. */
13972 cp_lexer_consume_token (parser->lexer);
13973 /* Skip the initializer. */
13974 cp_parser_skip_to_closing_brace (parser);
13975 /* Look for the trailing `}'. */
13976 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13978 return error_mark_node;
13981 return cp_parser_constant_expression (parser,
13982 /*allow_non_constant=*/false,
13986 /* Derived classes [gram.class.derived] */
13988 /* Parse a base-clause.
13991 : base-specifier-list
13993 base-specifier-list:
13995 base-specifier-list , base-specifier
13997 Returns a TREE_LIST representing the base-classes, in the order in
13998 which they were declared. The representation of each node is as
13999 described by cp_parser_base_specifier.
14001 In the case that no bases are specified, this function will return
14002 NULL_TREE, not ERROR_MARK_NODE. */
14005 cp_parser_base_clause (cp_parser* parser)
14007 tree bases = NULL_TREE;
14009 /* Look for the `:' that begins the list. */
14010 cp_parser_require (parser, CPP_COLON, "`:'");
14012 /* Scan the base-specifier-list. */
14018 /* Look for the base-specifier. */
14019 base = cp_parser_base_specifier (parser);
14020 /* Add BASE to the front of the list. */
14021 if (base != error_mark_node)
14023 TREE_CHAIN (base) = bases;
14026 /* Peek at the next token. */
14027 token = cp_lexer_peek_token (parser->lexer);
14028 /* If it's not a comma, then the list is complete. */
14029 if (token->type != CPP_COMMA)
14031 /* Consume the `,'. */
14032 cp_lexer_consume_token (parser->lexer);
14035 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14036 base class had a qualified name. However, the next name that
14037 appears is certainly not qualified. */
14038 parser->scope = NULL_TREE;
14039 parser->qualifying_scope = NULL_TREE;
14040 parser->object_scope = NULL_TREE;
14042 return nreverse (bases);
14045 /* Parse a base-specifier.
14048 :: [opt] nested-name-specifier [opt] class-name
14049 virtual access-specifier [opt] :: [opt] nested-name-specifier
14051 access-specifier virtual [opt] :: [opt] nested-name-specifier
14054 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14055 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14056 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14057 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14060 cp_parser_base_specifier (cp_parser* parser)
14064 bool virtual_p = false;
14065 bool duplicate_virtual_error_issued_p = false;
14066 bool duplicate_access_error_issued_p = false;
14067 bool class_scope_p, template_p;
14068 tree access = access_default_node;
14071 /* Process the optional `virtual' and `access-specifier'. */
14074 /* Peek at the next token. */
14075 token = cp_lexer_peek_token (parser->lexer);
14076 /* Process `virtual'. */
14077 switch (token->keyword)
14080 /* If `virtual' appears more than once, issue an error. */
14081 if (virtual_p && !duplicate_virtual_error_issued_p)
14083 cp_parser_error (parser,
14084 "%<virtual%> specified more than once in base-specified");
14085 duplicate_virtual_error_issued_p = true;
14090 /* Consume the `virtual' token. */
14091 cp_lexer_consume_token (parser->lexer);
14096 case RID_PROTECTED:
14098 /* If more than one access specifier appears, issue an
14100 if (access != access_default_node
14101 && !duplicate_access_error_issued_p)
14103 cp_parser_error (parser,
14104 "more than one access specifier in base-specified");
14105 duplicate_access_error_issued_p = true;
14108 access = ridpointers[(int) token->keyword];
14110 /* Consume the access-specifier. */
14111 cp_lexer_consume_token (parser->lexer);
14120 /* It is not uncommon to see programs mechanically, erroneously, use
14121 the 'typename' keyword to denote (dependent) qualified types
14122 as base classes. */
14123 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14125 if (!processing_template_decl)
14126 error ("keyword %<typename%> not allowed outside of templates");
14128 error ("keyword %<typename%> not allowed in this context "
14129 "(the base class is implicitly a type)");
14130 cp_lexer_consume_token (parser->lexer);
14133 /* Look for the optional `::' operator. */
14134 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14135 /* Look for the nested-name-specifier. The simplest way to
14140 The keyword `typename' is not permitted in a base-specifier or
14141 mem-initializer; in these contexts a qualified name that
14142 depends on a template-parameter is implicitly assumed to be a
14145 is to pretend that we have seen the `typename' keyword at this
14147 cp_parser_nested_name_specifier_opt (parser,
14148 /*typename_keyword_p=*/true,
14149 /*check_dependency_p=*/true,
14151 /*is_declaration=*/true);
14152 /* If the base class is given by a qualified name, assume that names
14153 we see are type names or templates, as appropriate. */
14154 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14155 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14157 /* Finally, look for the class-name. */
14158 type = cp_parser_class_name (parser,
14162 /*check_dependency_p=*/true,
14163 /*class_head_p=*/false,
14164 /*is_declaration=*/true);
14166 if (type == error_mark_node)
14167 return error_mark_node;
14169 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14172 /* Exception handling [gram.exception] */
14174 /* Parse an (optional) exception-specification.
14176 exception-specification:
14177 throw ( type-id-list [opt] )
14179 Returns a TREE_LIST representing the exception-specification. The
14180 TREE_VALUE of each node is a type. */
14183 cp_parser_exception_specification_opt (cp_parser* parser)
14188 /* Peek at the next token. */
14189 token = cp_lexer_peek_token (parser->lexer);
14190 /* If it's not `throw', then there's no exception-specification. */
14191 if (!cp_parser_is_keyword (token, RID_THROW))
14194 /* Consume the `throw'. */
14195 cp_lexer_consume_token (parser->lexer);
14197 /* Look for the `('. */
14198 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14200 /* Peek at the next token. */
14201 token = cp_lexer_peek_token (parser->lexer);
14202 /* If it's not a `)', then there is a type-id-list. */
14203 if (token->type != CPP_CLOSE_PAREN)
14205 const char *saved_message;
14207 /* Types may not be defined in an exception-specification. */
14208 saved_message = parser->type_definition_forbidden_message;
14209 parser->type_definition_forbidden_message
14210 = "types may not be defined in an exception-specification";
14211 /* Parse the type-id-list. */
14212 type_id_list = cp_parser_type_id_list (parser);
14213 /* Restore the saved message. */
14214 parser->type_definition_forbidden_message = saved_message;
14217 type_id_list = empty_except_spec;
14219 /* Look for the `)'. */
14220 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14222 return type_id_list;
14225 /* Parse an (optional) type-id-list.
14229 type-id-list , type-id
14231 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14232 in the order that the types were presented. */
14235 cp_parser_type_id_list (cp_parser* parser)
14237 tree types = NULL_TREE;
14244 /* Get the next type-id. */
14245 type = cp_parser_type_id (parser);
14246 /* Add it to the list. */
14247 types = add_exception_specifier (types, type, /*complain=*/1);
14248 /* Peek at the next token. */
14249 token = cp_lexer_peek_token (parser->lexer);
14250 /* If it is not a `,', we are done. */
14251 if (token->type != CPP_COMMA)
14253 /* Consume the `,'. */
14254 cp_lexer_consume_token (parser->lexer);
14257 return nreverse (types);
14260 /* Parse a try-block.
14263 try compound-statement handler-seq */
14266 cp_parser_try_block (cp_parser* parser)
14270 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14271 try_block = begin_try_block ();
14272 cp_parser_compound_statement (parser, NULL, true);
14273 finish_try_block (try_block);
14274 cp_parser_handler_seq (parser);
14275 finish_handler_sequence (try_block);
14280 /* Parse a function-try-block.
14282 function-try-block:
14283 try ctor-initializer [opt] function-body handler-seq */
14286 cp_parser_function_try_block (cp_parser* parser)
14288 tree compound_stmt;
14290 bool ctor_initializer_p;
14292 /* Look for the `try' keyword. */
14293 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14295 /* Let the rest of the front-end know where we are. */
14296 try_block = begin_function_try_block (&compound_stmt);
14297 /* Parse the function-body. */
14299 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14300 /* We're done with the `try' part. */
14301 finish_function_try_block (try_block);
14302 /* Parse the handlers. */
14303 cp_parser_handler_seq (parser);
14304 /* We're done with the handlers. */
14305 finish_function_handler_sequence (try_block, compound_stmt);
14307 return ctor_initializer_p;
14310 /* Parse a handler-seq.
14313 handler handler-seq [opt] */
14316 cp_parser_handler_seq (cp_parser* parser)
14322 /* Parse the handler. */
14323 cp_parser_handler (parser);
14324 /* Peek at the next token. */
14325 token = cp_lexer_peek_token (parser->lexer);
14326 /* If it's not `catch' then there are no more handlers. */
14327 if (!cp_parser_is_keyword (token, RID_CATCH))
14332 /* Parse a handler.
14335 catch ( exception-declaration ) compound-statement */
14338 cp_parser_handler (cp_parser* parser)
14343 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14344 handler = begin_handler ();
14345 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14346 declaration = cp_parser_exception_declaration (parser);
14347 finish_handler_parms (declaration, handler);
14348 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14349 cp_parser_compound_statement (parser, NULL, false);
14350 finish_handler (handler);
14353 /* Parse an exception-declaration.
14355 exception-declaration:
14356 type-specifier-seq declarator
14357 type-specifier-seq abstract-declarator
14361 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14362 ellipsis variant is used. */
14365 cp_parser_exception_declaration (cp_parser* parser)
14368 cp_decl_specifier_seq type_specifiers;
14369 cp_declarator *declarator;
14370 const char *saved_message;
14372 /* If it's an ellipsis, it's easy to handle. */
14373 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14375 /* Consume the `...' token. */
14376 cp_lexer_consume_token (parser->lexer);
14380 /* Types may not be defined in exception-declarations. */
14381 saved_message = parser->type_definition_forbidden_message;
14382 parser->type_definition_forbidden_message
14383 = "types may not be defined in exception-declarations";
14385 /* Parse the type-specifier-seq. */
14386 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14388 /* If it's a `)', then there is no declarator. */
14389 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14392 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14393 /*ctor_dtor_or_conv_p=*/NULL,
14394 /*parenthesized_p=*/NULL,
14395 /*member_p=*/false);
14397 /* Restore the saved message. */
14398 parser->type_definition_forbidden_message = saved_message;
14400 if (type_specifiers.any_specifiers_p)
14402 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14403 if (decl == NULL_TREE)
14404 error ("invalid catch parameter");
14412 /* Parse a throw-expression.
14415 throw assignment-expression [opt]
14417 Returns a THROW_EXPR representing the throw-expression. */
14420 cp_parser_throw_expression (cp_parser* parser)
14425 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14426 token = cp_lexer_peek_token (parser->lexer);
14427 /* Figure out whether or not there is an assignment-expression
14428 following the "throw" keyword. */
14429 if (token->type == CPP_COMMA
14430 || token->type == CPP_SEMICOLON
14431 || token->type == CPP_CLOSE_PAREN
14432 || token->type == CPP_CLOSE_SQUARE
14433 || token->type == CPP_CLOSE_BRACE
14434 || token->type == CPP_COLON)
14435 expression = NULL_TREE;
14437 expression = cp_parser_assignment_expression (parser,
14440 return build_throw (expression);
14443 /* GNU Extensions */
14445 /* Parse an (optional) asm-specification.
14448 asm ( string-literal )
14450 If the asm-specification is present, returns a STRING_CST
14451 corresponding to the string-literal. Otherwise, returns
14455 cp_parser_asm_specification_opt (cp_parser* parser)
14458 tree asm_specification;
14460 /* Peek at the next token. */
14461 token = cp_lexer_peek_token (parser->lexer);
14462 /* If the next token isn't the `asm' keyword, then there's no
14463 asm-specification. */
14464 if (!cp_parser_is_keyword (token, RID_ASM))
14467 /* Consume the `asm' token. */
14468 cp_lexer_consume_token (parser->lexer);
14469 /* Look for the `('. */
14470 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14472 /* Look for the string-literal. */
14473 asm_specification = cp_parser_string_literal (parser, false, false);
14475 /* Look for the `)'. */
14476 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14478 return asm_specification;
14481 /* Parse an asm-operand-list.
14485 asm-operand-list , asm-operand
14488 string-literal ( expression )
14489 [ string-literal ] string-literal ( expression )
14491 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14492 each node is the expression. The TREE_PURPOSE is itself a
14493 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14494 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14495 is a STRING_CST for the string literal before the parenthesis. */
14498 cp_parser_asm_operand_list (cp_parser* parser)
14500 tree asm_operands = NULL_TREE;
14504 tree string_literal;
14508 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14510 /* Consume the `[' token. */
14511 cp_lexer_consume_token (parser->lexer);
14512 /* Read the operand name. */
14513 name = cp_parser_identifier (parser);
14514 if (name != error_mark_node)
14515 name = build_string (IDENTIFIER_LENGTH (name),
14516 IDENTIFIER_POINTER (name));
14517 /* Look for the closing `]'. */
14518 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14522 /* Look for the string-literal. */
14523 string_literal = cp_parser_string_literal (parser, false, false);
14525 /* Look for the `('. */
14526 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14527 /* Parse the expression. */
14528 expression = cp_parser_expression (parser, /*cast_p=*/false);
14529 /* Look for the `)'. */
14530 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14532 /* Add this operand to the list. */
14533 asm_operands = tree_cons (build_tree_list (name, string_literal),
14536 /* If the next token is not a `,', there are no more
14538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14540 /* Consume the `,'. */
14541 cp_lexer_consume_token (parser->lexer);
14544 return nreverse (asm_operands);
14547 /* Parse an asm-clobber-list.
14551 asm-clobber-list , string-literal
14553 Returns a TREE_LIST, indicating the clobbers in the order that they
14554 appeared. The TREE_VALUE of each node is a STRING_CST. */
14557 cp_parser_asm_clobber_list (cp_parser* parser)
14559 tree clobbers = NULL_TREE;
14563 tree string_literal;
14565 /* Look for the string literal. */
14566 string_literal = cp_parser_string_literal (parser, false, false);
14567 /* Add it to the list. */
14568 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14569 /* If the next token is not a `,', then the list is
14571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14573 /* Consume the `,' token. */
14574 cp_lexer_consume_token (parser->lexer);
14580 /* Parse an (optional) series of attributes.
14583 attributes attribute
14586 __attribute__ (( attribute-list [opt] ))
14588 The return value is as for cp_parser_attribute_list. */
14591 cp_parser_attributes_opt (cp_parser* parser)
14593 tree attributes = NULL_TREE;
14598 tree attribute_list;
14600 /* Peek at the next token. */
14601 token = cp_lexer_peek_token (parser->lexer);
14602 /* If it's not `__attribute__', then we're done. */
14603 if (token->keyword != RID_ATTRIBUTE)
14606 /* Consume the `__attribute__' keyword. */
14607 cp_lexer_consume_token (parser->lexer);
14608 /* Look for the two `(' tokens. */
14609 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14610 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14612 /* Peek at the next token. */
14613 token = cp_lexer_peek_token (parser->lexer);
14614 if (token->type != CPP_CLOSE_PAREN)
14615 /* Parse the attribute-list. */
14616 attribute_list = cp_parser_attribute_list (parser);
14618 /* If the next token is a `)', then there is no attribute
14620 attribute_list = NULL;
14622 /* Look for the two `)' tokens. */
14623 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14624 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14626 /* Add these new attributes to the list. */
14627 attributes = chainon (attributes, attribute_list);
14633 /* Parse an attribute-list.
14637 attribute-list , attribute
14641 identifier ( identifier )
14642 identifier ( identifier , expression-list )
14643 identifier ( expression-list )
14645 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14646 to an attribute. The TREE_PURPOSE of each node is the identifier
14647 indicating which attribute is in use. The TREE_VALUE represents
14648 the arguments, if any. */
14651 cp_parser_attribute_list (cp_parser* parser)
14653 tree attribute_list = NULL_TREE;
14654 bool save_translate_strings_p = parser->translate_strings_p;
14656 parser->translate_strings_p = false;
14663 /* Look for the identifier. We also allow keywords here; for
14664 example `__attribute__ ((const))' is legal. */
14665 token = cp_lexer_peek_token (parser->lexer);
14666 if (token->type == CPP_NAME
14667 || token->type == CPP_KEYWORD)
14669 tree arguments = NULL_TREE;
14671 /* Consume the token. */
14672 token = cp_lexer_consume_token (parser->lexer);
14674 /* Save away the identifier that indicates which attribute
14676 identifier = token->value;
14677 attribute = build_tree_list (identifier, NULL_TREE);
14679 /* Peek at the next token. */
14680 token = cp_lexer_peek_token (parser->lexer);
14681 /* If it's an `(', then parse the attribute arguments. */
14682 if (token->type == CPP_OPEN_PAREN)
14684 arguments = cp_parser_parenthesized_expression_list
14685 (parser, true, /*cast_p=*/false,
14686 /*non_constant_p=*/NULL);
14687 /* Save the arguments away. */
14688 TREE_VALUE (attribute) = arguments;
14691 if (arguments != error_mark_node)
14693 /* Add this attribute to the list. */
14694 TREE_CHAIN (attribute) = attribute_list;
14695 attribute_list = attribute;
14698 token = cp_lexer_peek_token (parser->lexer);
14700 /* Now, look for more attributes. If the next token isn't a
14701 `,', we're done. */
14702 if (token->type != CPP_COMMA)
14705 /* Consume the comma and keep going. */
14706 cp_lexer_consume_token (parser->lexer);
14708 parser->translate_strings_p = save_translate_strings_p;
14710 /* We built up the list in reverse order. */
14711 return nreverse (attribute_list);
14714 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14715 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14716 current value of the PEDANTIC flag, regardless of whether or not
14717 the `__extension__' keyword is present. The caller is responsible
14718 for restoring the value of the PEDANTIC flag. */
14721 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14723 /* Save the old value of the PEDANTIC flag. */
14724 *saved_pedantic = pedantic;
14726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14728 /* Consume the `__extension__' token. */
14729 cp_lexer_consume_token (parser->lexer);
14730 /* We're not being pedantic while the `__extension__' keyword is
14740 /* Parse a label declaration.
14743 __label__ label-declarator-seq ;
14745 label-declarator-seq:
14746 identifier , label-declarator-seq
14750 cp_parser_label_declaration (cp_parser* parser)
14752 /* Look for the `__label__' keyword. */
14753 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14759 /* Look for an identifier. */
14760 identifier = cp_parser_identifier (parser);
14761 /* If we failed, stop. */
14762 if (identifier == error_mark_node)
14764 /* Declare it as a label. */
14765 finish_label_decl (identifier);
14766 /* If the next token is a `;', stop. */
14767 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14769 /* Look for the `,' separating the label declarations. */
14770 cp_parser_require (parser, CPP_COMMA, "`,'");
14773 /* Look for the final `;'. */
14774 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14777 /* Support Functions */
14779 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14780 NAME should have one of the representations used for an
14781 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14782 is returned. If PARSER->SCOPE is a dependent type, then a
14783 SCOPE_REF is returned.
14785 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14786 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14787 was formed. Abstractly, such entities should not be passed to this
14788 function, because they do not need to be looked up, but it is
14789 simpler to check for this special case here, rather than at the
14792 In cases not explicitly covered above, this function returns a
14793 DECL, OVERLOAD, or baselink representing the result of the lookup.
14794 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14797 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14798 (e.g., "struct") that was used. In that case bindings that do not
14799 refer to types are ignored.
14801 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14804 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14807 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14810 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14811 TREE_LIST of candiates if name-lookup results in an ambiguity, and
14812 NULL_TREE otherwise. */
14815 cp_parser_lookup_name (cp_parser *parser, tree name,
14816 enum tag_types tag_type,
14819 bool check_dependency,
14820 tree *ambiguous_decls)
14824 tree object_type = parser->context->object_type;
14826 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14827 flags |= LOOKUP_COMPLAIN;
14829 /* Assume that the lookup will be unambiguous. */
14830 if (ambiguous_decls)
14831 *ambiguous_decls = NULL_TREE;
14833 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14834 no longer valid. Note that if we are parsing tentatively, and
14835 the parse fails, OBJECT_TYPE will be automatically restored. */
14836 parser->context->object_type = NULL_TREE;
14838 if (name == error_mark_node)
14839 return error_mark_node;
14841 /* A template-id has already been resolved; there is no lookup to
14843 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14845 if (BASELINK_P (name))
14847 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14848 == TEMPLATE_ID_EXPR);
14852 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14853 it should already have been checked to make sure that the name
14854 used matches the type being destroyed. */
14855 if (TREE_CODE (name) == BIT_NOT_EXPR)
14859 /* Figure out to which type this destructor applies. */
14861 type = parser->scope;
14862 else if (object_type)
14863 type = object_type;
14865 type = current_class_type;
14866 /* If that's not a class type, there is no destructor. */
14867 if (!type || !CLASS_TYPE_P (type))
14868 return error_mark_node;
14869 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14870 lazily_declare_fn (sfk_destructor, type);
14871 if (!CLASSTYPE_DESTRUCTORS (type))
14872 return error_mark_node;
14873 /* If it was a class type, return the destructor. */
14874 return CLASSTYPE_DESTRUCTORS (type);
14877 /* By this point, the NAME should be an ordinary identifier. If
14878 the id-expression was a qualified name, the qualifying scope is
14879 stored in PARSER->SCOPE at this point. */
14880 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14882 /* Perform the lookup. */
14887 if (parser->scope == error_mark_node)
14888 return error_mark_node;
14890 /* If the SCOPE is dependent, the lookup must be deferred until
14891 the template is instantiated -- unless we are explicitly
14892 looking up names in uninstantiated templates. Even then, we
14893 cannot look up the name if the scope is not a class type; it
14894 might, for example, be a template type parameter. */
14895 dependent_p = (TYPE_P (parser->scope)
14896 && !(parser->in_declarator_p
14897 && currently_open_class (parser->scope))
14898 && dependent_type_p (parser->scope));
14899 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14906 /* The resolution to Core Issue 180 says that `struct
14907 A::B' should be considered a type-name, even if `A'
14909 type = make_typename_type (parser->scope, name, tag_type,
14911 decl = TYPE_NAME (type);
14913 else if (is_template
14914 && (cp_parser_next_token_ends_template_argument_p (parser)
14915 || cp_lexer_next_token_is (parser->lexer,
14917 decl = make_unbound_class_template (parser->scope,
14921 decl = build_qualified_name (/*type=*/NULL_TREE,
14922 parser->scope, name,
14927 tree pushed_scope = NULL_TREE;
14929 /* If PARSER->SCOPE is a dependent type, then it must be a
14930 class type, and we must not be checking dependencies;
14931 otherwise, we would have processed this lookup above. So
14932 that PARSER->SCOPE is not considered a dependent base by
14933 lookup_member, we must enter the scope here. */
14935 pushed_scope = push_scope (parser->scope);
14936 /* If the PARSER->SCOPE is a template specialization, it
14937 may be instantiated during name lookup. In that case,
14938 errors may be issued. Even if we rollback the current
14939 tentative parse, those errors are valid. */
14940 decl = lookup_qualified_name (parser->scope, name,
14941 tag_type != none_type,
14942 /*complain=*/true);
14944 pop_scope (pushed_scope);
14946 parser->qualifying_scope = parser->scope;
14947 parser->object_scope = NULL_TREE;
14949 else if (object_type)
14951 tree object_decl = NULL_TREE;
14952 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14953 OBJECT_TYPE is not a class. */
14954 if (CLASS_TYPE_P (object_type))
14955 /* If the OBJECT_TYPE is a template specialization, it may
14956 be instantiated during name lookup. In that case, errors
14957 may be issued. Even if we rollback the current tentative
14958 parse, those errors are valid. */
14959 object_decl = lookup_member (object_type,
14962 tag_type != none_type);
14963 /* Look it up in the enclosing context, too. */
14964 decl = lookup_name_real (name, tag_type != none_type,
14966 /*block_p=*/true, is_namespace, flags);
14967 parser->object_scope = object_type;
14968 parser->qualifying_scope = NULL_TREE;
14970 decl = object_decl;
14974 decl = lookup_name_real (name, tag_type != none_type,
14976 /*block_p=*/true, is_namespace, flags);
14977 parser->qualifying_scope = NULL_TREE;
14978 parser->object_scope = NULL_TREE;
14981 /* If the lookup failed, let our caller know. */
14982 if (!decl || decl == error_mark_node)
14983 return error_mark_node;
14985 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14986 if (TREE_CODE (decl) == TREE_LIST)
14988 if (ambiguous_decls)
14989 *ambiguous_decls = decl;
14990 /* The error message we have to print is too complicated for
14991 cp_parser_error, so we incorporate its actions directly. */
14992 if (!cp_parser_simulate_error (parser))
14994 error ("reference to %qD is ambiguous", name);
14995 print_candidates (decl);
14997 return error_mark_node;
15000 gcc_assert (DECL_P (decl)
15001 || TREE_CODE (decl) == OVERLOAD
15002 || TREE_CODE (decl) == SCOPE_REF
15003 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15004 || BASELINK_P (decl));
15006 /* If we have resolved the name of a member declaration, check to
15007 see if the declaration is accessible. When the name resolves to
15008 set of overloaded functions, accessibility is checked when
15009 overload resolution is done.
15011 During an explicit instantiation, access is not checked at all,
15012 as per [temp.explicit]. */
15014 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15019 /* Like cp_parser_lookup_name, but for use in the typical case where
15020 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15021 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15024 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15026 return cp_parser_lookup_name (parser, name,
15028 /*is_template=*/false,
15029 /*is_namespace=*/false,
15030 /*check_dependency=*/true,
15031 /*ambiguous_decls=*/NULL);
15034 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15035 the current context, return the TYPE_DECL. If TAG_NAME_P is
15036 true, the DECL indicates the class being defined in a class-head,
15037 or declared in an elaborated-type-specifier.
15039 Otherwise, return DECL. */
15042 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15044 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15045 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15048 template <typename T> struct B;
15051 template <typename T> struct A::B {};
15053 Similarly, in an elaborated-type-specifier:
15055 namespace N { struct X{}; }
15058 template <typename T> friend struct N::X;
15061 However, if the DECL refers to a class type, and we are in
15062 the scope of the class, then the name lookup automatically
15063 finds the TYPE_DECL created by build_self_reference rather
15064 than a TEMPLATE_DECL. For example, in:
15066 template <class T> struct S {
15070 there is no need to handle such case. */
15072 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15073 return DECL_TEMPLATE_RESULT (decl);
15078 /* If too many, or too few, template-parameter lists apply to the
15079 declarator, issue an error message. Returns TRUE if all went well,
15080 and FALSE otherwise. */
15083 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15084 cp_declarator *declarator)
15086 unsigned num_templates;
15088 /* We haven't seen any classes that involve template parameters yet. */
15091 switch (declarator->kind)
15094 if (declarator->u.id.qualifying_scope)
15099 scope = declarator->u.id.qualifying_scope;
15100 member = declarator->u.id.unqualified_name;
15102 while (scope && CLASS_TYPE_P (scope))
15104 /* You're supposed to have one `template <...>'
15105 for every template class, but you don't need one
15106 for a full specialization. For example:
15108 template <class T> struct S{};
15109 template <> struct S<int> { void f(); };
15110 void S<int>::f () {}
15112 is correct; there shouldn't be a `template <>' for
15113 the definition of `S<int>::f'. */
15114 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15115 /* If SCOPE does not have template information of any
15116 kind, then it is not a template, nor is it nested
15117 within a template. */
15119 if (explicit_class_specialization_p (scope))
15121 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15124 scope = TYPE_CONTEXT (scope);
15127 else if (TREE_CODE (declarator->u.id.unqualified_name)
15128 == TEMPLATE_ID_EXPR)
15129 /* If the DECLARATOR has the form `X<y>' then it uses one
15130 additional level of template parameters. */
15133 return cp_parser_check_template_parameters (parser,
15139 case cdk_reference:
15141 return (cp_parser_check_declarator_template_parameters
15142 (parser, declarator->declarator));
15148 gcc_unreachable ();
15153 /* NUM_TEMPLATES were used in the current declaration. If that is
15154 invalid, return FALSE and issue an error messages. Otherwise,
15158 cp_parser_check_template_parameters (cp_parser* parser,
15159 unsigned num_templates)
15161 /* If there are more template classes than parameter lists, we have
15164 template <class T> void S<T>::R<T>::f (); */
15165 if (parser->num_template_parameter_lists < num_templates)
15167 error ("too few template-parameter-lists");
15170 /* If there are the same number of template classes and parameter
15171 lists, that's OK. */
15172 if (parser->num_template_parameter_lists == num_templates)
15174 /* If there are more, but only one more, then we are referring to a
15175 member template. That's OK too. */
15176 if (parser->num_template_parameter_lists == num_templates + 1)
15178 /* Otherwise, there are too many template parameter lists. We have
15181 template <class T> template <class U> void S::f(); */
15182 error ("too many template-parameter-lists");
15186 /* Parse an optional `::' token indicating that the following name is
15187 from the global namespace. If so, PARSER->SCOPE is set to the
15188 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15189 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15190 Returns the new value of PARSER->SCOPE, if the `::' token is
15191 present, and NULL_TREE otherwise. */
15194 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15198 /* Peek at the next token. */
15199 token = cp_lexer_peek_token (parser->lexer);
15200 /* If we're looking at a `::' token then we're starting from the
15201 global namespace, not our current location. */
15202 if (token->type == CPP_SCOPE)
15204 /* Consume the `::' token. */
15205 cp_lexer_consume_token (parser->lexer);
15206 /* Set the SCOPE so that we know where to start the lookup. */
15207 parser->scope = global_namespace;
15208 parser->qualifying_scope = global_namespace;
15209 parser->object_scope = NULL_TREE;
15211 return parser->scope;
15213 else if (!current_scope_valid_p)
15215 parser->scope = NULL_TREE;
15216 parser->qualifying_scope = NULL_TREE;
15217 parser->object_scope = NULL_TREE;
15223 /* Returns TRUE if the upcoming token sequence is the start of a
15224 constructor declarator. If FRIEND_P is true, the declarator is
15225 preceded by the `friend' specifier. */
15228 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15230 bool constructor_p;
15231 tree type_decl = NULL_TREE;
15232 bool nested_name_p;
15233 cp_token *next_token;
15235 /* The common case is that this is not a constructor declarator, so
15236 try to avoid doing lots of work if at all possible. It's not
15237 valid declare a constructor at function scope. */
15238 if (parser->in_function_body)
15240 /* And only certain tokens can begin a constructor declarator. */
15241 next_token = cp_lexer_peek_token (parser->lexer);
15242 if (next_token->type != CPP_NAME
15243 && next_token->type != CPP_SCOPE
15244 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15245 && next_token->type != CPP_TEMPLATE_ID)
15248 /* Parse tentatively; we are going to roll back all of the tokens
15250 cp_parser_parse_tentatively (parser);
15251 /* Assume that we are looking at a constructor declarator. */
15252 constructor_p = true;
15254 /* Look for the optional `::' operator. */
15255 cp_parser_global_scope_opt (parser,
15256 /*current_scope_valid_p=*/false);
15257 /* Look for the nested-name-specifier. */
15259 = (cp_parser_nested_name_specifier_opt (parser,
15260 /*typename_keyword_p=*/false,
15261 /*check_dependency_p=*/false,
15263 /*is_declaration=*/false)
15265 /* Outside of a class-specifier, there must be a
15266 nested-name-specifier. */
15267 if (!nested_name_p &&
15268 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15270 constructor_p = false;
15271 /* If we still think that this might be a constructor-declarator,
15272 look for a class-name. */
15277 template <typename T> struct S { S(); };
15278 template <typename T> S<T>::S ();
15280 we must recognize that the nested `S' names a class.
15283 template <typename T> S<T>::S<T> ();
15285 we must recognize that the nested `S' names a template. */
15286 type_decl = cp_parser_class_name (parser,
15287 /*typename_keyword_p=*/false,
15288 /*template_keyword_p=*/false,
15290 /*check_dependency_p=*/false,
15291 /*class_head_p=*/false,
15292 /*is_declaration=*/false);
15293 /* If there was no class-name, then this is not a constructor. */
15294 constructor_p = !cp_parser_error_occurred (parser);
15297 /* If we're still considering a constructor, we have to see a `(',
15298 to begin the parameter-declaration-clause, followed by either a
15299 `)', an `...', or a decl-specifier. We need to check for a
15300 type-specifier to avoid being fooled into thinking that:
15304 is a constructor. (It is actually a function named `f' that
15305 takes one parameter (of type `int') and returns a value of type
15308 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15310 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15311 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15312 /* A parameter declaration begins with a decl-specifier,
15313 which is either the "attribute" keyword, a storage class
15314 specifier, or (usually) a type-specifier. */
15315 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15318 tree pushed_scope = NULL_TREE;
15319 unsigned saved_num_template_parameter_lists;
15321 /* Names appearing in the type-specifier should be looked up
15322 in the scope of the class. */
15323 if (current_class_type)
15327 type = TREE_TYPE (type_decl);
15328 if (TREE_CODE (type) == TYPENAME_TYPE)
15330 type = resolve_typename_type (type,
15331 /*only_current_p=*/false);
15332 if (type == error_mark_node)
15334 cp_parser_abort_tentative_parse (parser);
15338 pushed_scope = push_scope (type);
15341 /* Inside the constructor parameter list, surrounding
15342 template-parameter-lists do not apply. */
15343 saved_num_template_parameter_lists
15344 = parser->num_template_parameter_lists;
15345 parser->num_template_parameter_lists = 0;
15347 /* Look for the type-specifier. */
15348 cp_parser_type_specifier (parser,
15349 CP_PARSER_FLAGS_NONE,
15350 /*decl_specs=*/NULL,
15351 /*is_declarator=*/true,
15352 /*declares_class_or_enum=*/NULL,
15353 /*is_cv_qualifier=*/NULL);
15355 parser->num_template_parameter_lists
15356 = saved_num_template_parameter_lists;
15358 /* Leave the scope of the class. */
15360 pop_scope (pushed_scope);
15362 constructor_p = !cp_parser_error_occurred (parser);
15366 constructor_p = false;
15367 /* We did not really want to consume any tokens. */
15368 cp_parser_abort_tentative_parse (parser);
15370 return constructor_p;
15373 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15374 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15375 they must be performed once we are in the scope of the function.
15377 Returns the function defined. */
15380 cp_parser_function_definition_from_specifiers_and_declarator
15381 (cp_parser* parser,
15382 cp_decl_specifier_seq *decl_specifiers,
15384 const cp_declarator *declarator)
15389 /* Begin the function-definition. */
15390 success_p = start_function (decl_specifiers, declarator, attributes);
15392 /* The things we're about to see are not directly qualified by any
15393 template headers we've seen thus far. */
15394 reset_specialization ();
15396 /* If there were names looked up in the decl-specifier-seq that we
15397 did not check, check them now. We must wait until we are in the
15398 scope of the function to perform the checks, since the function
15399 might be a friend. */
15400 perform_deferred_access_checks ();
15404 /* Skip the entire function. */
15405 error ("invalid function declaration");
15406 cp_parser_skip_to_end_of_block_or_statement (parser);
15407 fn = error_mark_node;
15410 fn = cp_parser_function_definition_after_declarator (parser,
15411 /*inline_p=*/false);
15416 /* Parse the part of a function-definition that follows the
15417 declarator. INLINE_P is TRUE iff this function is an inline
15418 function defined with a class-specifier.
15420 Returns the function defined. */
15423 cp_parser_function_definition_after_declarator (cp_parser* parser,
15427 bool ctor_initializer_p = false;
15428 bool saved_in_unbraced_linkage_specification_p;
15429 bool saved_in_function_body;
15430 unsigned saved_num_template_parameter_lists;
15432 saved_in_function_body = parser->in_function_body;
15433 parser->in_function_body = true;
15434 /* If the next token is `return', then the code may be trying to
15435 make use of the "named return value" extension that G++ used to
15437 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15439 /* Consume the `return' keyword. */
15440 cp_lexer_consume_token (parser->lexer);
15441 /* Look for the identifier that indicates what value is to be
15443 cp_parser_identifier (parser);
15444 /* Issue an error message. */
15445 error ("named return values are no longer supported");
15446 /* Skip tokens until we reach the start of the function body. */
15447 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15448 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15449 cp_lexer_consume_token (parser->lexer);
15451 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15452 anything declared inside `f'. */
15453 saved_in_unbraced_linkage_specification_p
15454 = parser->in_unbraced_linkage_specification_p;
15455 parser->in_unbraced_linkage_specification_p = false;
15456 /* Inside the function, surrounding template-parameter-lists do not
15458 saved_num_template_parameter_lists
15459 = parser->num_template_parameter_lists;
15460 parser->num_template_parameter_lists = 0;
15461 /* If the next token is `try', then we are looking at a
15462 function-try-block. */
15463 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15464 ctor_initializer_p = cp_parser_function_try_block (parser);
15465 /* A function-try-block includes the function-body, so we only do
15466 this next part if we're not processing a function-try-block. */
15469 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15471 /* Finish the function. */
15472 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15473 (inline_p ? 2 : 0));
15474 /* Generate code for it, if necessary. */
15475 expand_or_defer_fn (fn);
15476 /* Restore the saved values. */
15477 parser->in_unbraced_linkage_specification_p
15478 = saved_in_unbraced_linkage_specification_p;
15479 parser->num_template_parameter_lists
15480 = saved_num_template_parameter_lists;
15481 parser->in_function_body = saved_in_function_body;
15486 /* Parse a template-declaration, assuming that the `export' (and
15487 `extern') keywords, if present, has already been scanned. MEMBER_P
15488 is as for cp_parser_template_declaration. */
15491 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15493 tree decl = NULL_TREE;
15495 tree parameter_list;
15496 bool friend_p = false;
15497 bool need_lang_pop;
15499 /* Look for the `template' keyword. */
15500 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15504 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15506 if (at_class_scope_p () && current_function_decl)
15508 /* 14.5.2.2 [temp.mem]
15510 A local class shall not have member templates. */
15511 error ("invalid declaration of member template in local class");
15512 cp_parser_skip_to_end_of_block_or_statement (parser);
15517 A template ... shall not have C linkage. */
15518 if (current_lang_name == lang_name_c)
15520 error ("template with C linkage");
15521 /* Give it C++ linkage to avoid confusing other parts of the
15523 push_lang_context (lang_name_cplusplus);
15524 need_lang_pop = true;
15527 need_lang_pop = false;
15529 /* We cannot perform access checks on the template parameter
15530 declarations until we know what is being declared, just as we
15531 cannot check the decl-specifier list. */
15532 push_deferring_access_checks (dk_deferred);
15534 /* If the next token is `>', then we have an invalid
15535 specialization. Rather than complain about an invalid template
15536 parameter, issue an error message here. */
15537 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15539 cp_parser_error (parser, "invalid explicit specialization");
15540 begin_specialization ();
15541 parameter_list = NULL_TREE;
15545 /* Parse the template parameters. */
15546 begin_template_parm_list ();
15547 parameter_list = cp_parser_template_parameter_list (parser);
15548 parameter_list = end_template_parm_list (parameter_list);
15551 /* Get the deferred access checks from the parameter list. These
15552 will be checked once we know what is being declared, as for a
15553 member template the checks must be performed in the scope of the
15554 class containing the member. */
15555 checks = get_deferred_access_checks ();
15557 /* Look for the `>'. */
15558 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15559 /* We just processed one more parameter list. */
15560 ++parser->num_template_parameter_lists;
15561 /* If the next token is `template', there are more template
15563 if (cp_lexer_next_token_is_keyword (parser->lexer,
15565 cp_parser_template_declaration_after_export (parser, member_p);
15568 /* There are no access checks when parsing a template, as we do not
15569 know if a specialization will be a friend. */
15570 push_deferring_access_checks (dk_no_check);
15571 decl = cp_parser_single_declaration (parser,
15575 pop_deferring_access_checks ();
15577 /* If this is a member template declaration, let the front
15579 if (member_p && !friend_p && decl)
15581 if (TREE_CODE (decl) == TYPE_DECL)
15582 cp_parser_check_access_in_redeclaration (decl);
15584 decl = finish_member_template_decl (decl);
15586 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15587 make_friend_class (current_class_type, TREE_TYPE (decl),
15588 /*complain=*/true);
15590 /* We are done with the current parameter list. */
15591 --parser->num_template_parameter_lists;
15593 pop_deferring_access_checks ();
15596 finish_template_decl (parameter_list);
15598 /* Register member declarations. */
15599 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15600 finish_member_declaration (decl);
15601 /* For the erroneous case of a template with C linkage, we pushed an
15602 implicit C++ linkage scope; exit that scope now. */
15604 pop_lang_context ();
15605 /* If DECL is a function template, we must return to parse it later.
15606 (Even though there is no definition, there might be default
15607 arguments that need handling.) */
15608 if (member_p && decl
15609 && (TREE_CODE (decl) == FUNCTION_DECL
15610 || DECL_FUNCTION_TEMPLATE_P (decl)))
15611 TREE_VALUE (parser->unparsed_functions_queues)
15612 = tree_cons (NULL_TREE, decl,
15613 TREE_VALUE (parser->unparsed_functions_queues));
15616 /* Perform the deferred access checks from a template-parameter-list.
15617 CHECKS is a TREE_LIST of access checks, as returned by
15618 get_deferred_access_checks. */
15621 cp_parser_perform_template_parameter_access_checks (tree checks)
15623 ++processing_template_parmlist;
15624 perform_access_checks (checks);
15625 --processing_template_parmlist;
15628 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15629 `function-definition' sequence. MEMBER_P is true, this declaration
15630 appears in a class scope.
15632 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15633 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15636 cp_parser_single_declaration (cp_parser* parser,
15641 int declares_class_or_enum;
15642 tree decl = NULL_TREE;
15643 cp_decl_specifier_seq decl_specifiers;
15644 bool function_definition_p = false;
15646 /* This function is only used when processing a template
15648 gcc_assert (innermost_scope_kind () == sk_template_parms
15649 || innermost_scope_kind () == sk_template_spec);
15651 /* Defer access checks until we know what is being declared. */
15652 push_deferring_access_checks (dk_deferred);
15654 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15656 cp_parser_decl_specifier_seq (parser,
15657 CP_PARSER_FLAGS_OPTIONAL,
15659 &declares_class_or_enum);
15661 *friend_p = cp_parser_friend_p (&decl_specifiers);
15663 /* There are no template typedefs. */
15664 if (decl_specifiers.specs[(int) ds_typedef])
15666 error ("template declaration of %qs", "typedef");
15667 decl = error_mark_node;
15670 /* Gather up the access checks that occurred the
15671 decl-specifier-seq. */
15672 stop_deferring_access_checks ();
15674 /* Check for the declaration of a template class. */
15675 if (declares_class_or_enum)
15677 if (cp_parser_declares_only_class_p (parser))
15679 decl = shadow_tag (&decl_specifiers);
15684 friend template <typename T> struct A<T>::B;
15687 A<T>::B will be represented by a TYPENAME_TYPE, and
15688 therefore not recognized by shadow_tag. */
15689 if (friend_p && *friend_p
15691 && decl_specifiers.type
15692 && TYPE_P (decl_specifiers.type))
15693 decl = decl_specifiers.type;
15695 if (decl && decl != error_mark_node)
15696 decl = TYPE_NAME (decl);
15698 decl = error_mark_node;
15700 /* Perform access checks for template parameters. */
15701 cp_parser_perform_template_parameter_access_checks (checks);
15704 /* If it's not a template class, try for a template function. If
15705 the next token is a `;', then this declaration does not declare
15706 anything. But, if there were errors in the decl-specifiers, then
15707 the error might well have come from an attempted class-specifier.
15708 In that case, there's no need to warn about a missing declarator. */
15710 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15711 || decl_specifiers.type != error_mark_node))
15712 decl = cp_parser_init_declarator (parser,
15715 /*function_definition_allowed_p=*/true,
15717 declares_class_or_enum,
15718 &function_definition_p);
15720 pop_deferring_access_checks ();
15722 /* Clear any current qualification; whatever comes next is the start
15723 of something new. */
15724 parser->scope = NULL_TREE;
15725 parser->qualifying_scope = NULL_TREE;
15726 parser->object_scope = NULL_TREE;
15727 /* Look for a trailing `;' after the declaration. */
15728 if (!function_definition_p
15729 && (decl == error_mark_node
15730 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15731 cp_parser_skip_to_end_of_block_or_statement (parser);
15736 /* Parse a cast-expression that is not the operand of a unary "&". */
15739 cp_parser_simple_cast_expression (cp_parser *parser)
15741 return cp_parser_cast_expression (parser, /*address_p=*/false,
15745 /* Parse a functional cast to TYPE. Returns an expression
15746 representing the cast. */
15749 cp_parser_functional_cast (cp_parser* parser, tree type)
15751 tree expression_list;
15755 = cp_parser_parenthesized_expression_list (parser, false,
15757 /*non_constant_p=*/NULL);
15759 cast = build_functional_cast (type, expression_list);
15760 /* [expr.const]/1: In an integral constant expression "only type
15761 conversions to integral or enumeration type can be used". */
15762 if (TREE_CODE (type) == TYPE_DECL)
15763 type = TREE_TYPE (type);
15764 if (cast != error_mark_node && !dependent_type_p (type)
15765 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15767 if (cp_parser_non_integral_constant_expression
15768 (parser, "a call to a constructor"))
15769 return error_mark_node;
15774 /* Save the tokens that make up the body of a member function defined
15775 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15776 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15777 specifiers applied to the declaration. Returns the FUNCTION_DECL
15778 for the member function. */
15781 cp_parser_save_member_function_body (cp_parser* parser,
15782 cp_decl_specifier_seq *decl_specifiers,
15783 cp_declarator *declarator,
15790 /* Create the function-declaration. */
15791 fn = start_method (decl_specifiers, declarator, attributes);
15792 /* If something went badly wrong, bail out now. */
15793 if (fn == error_mark_node)
15795 /* If there's a function-body, skip it. */
15796 if (cp_parser_token_starts_function_definition_p
15797 (cp_lexer_peek_token (parser->lexer)))
15798 cp_parser_skip_to_end_of_block_or_statement (parser);
15799 return error_mark_node;
15802 /* Remember it, if there default args to post process. */
15803 cp_parser_save_default_args (parser, fn);
15805 /* Save away the tokens that make up the body of the
15807 first = parser->lexer->next_token;
15808 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15809 /* Handle function try blocks. */
15810 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15811 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15812 last = parser->lexer->next_token;
15814 /* Save away the inline definition; we will process it when the
15815 class is complete. */
15816 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15817 DECL_PENDING_INLINE_P (fn) = 1;
15819 /* We need to know that this was defined in the class, so that
15820 friend templates are handled correctly. */
15821 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15823 /* We're done with the inline definition. */
15824 finish_method (fn);
15826 /* Add FN to the queue of functions to be parsed later. */
15827 TREE_VALUE (parser->unparsed_functions_queues)
15828 = tree_cons (NULL_TREE, fn,
15829 TREE_VALUE (parser->unparsed_functions_queues));
15834 /* Parse a template-argument-list, as well as the trailing ">" (but
15835 not the opening ">"). See cp_parser_template_argument_list for the
15839 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15843 tree saved_qualifying_scope;
15844 tree saved_object_scope;
15845 bool saved_greater_than_is_operator_p;
15846 bool saved_skip_evaluation;
15850 When parsing a template-id, the first non-nested `>' is taken as
15851 the end of the template-argument-list rather than a greater-than
15853 saved_greater_than_is_operator_p
15854 = parser->greater_than_is_operator_p;
15855 parser->greater_than_is_operator_p = false;
15856 /* Parsing the argument list may modify SCOPE, so we save it
15858 saved_scope = parser->scope;
15859 saved_qualifying_scope = parser->qualifying_scope;
15860 saved_object_scope = parser->object_scope;
15861 /* We need to evaluate the template arguments, even though this
15862 template-id may be nested within a "sizeof". */
15863 saved_skip_evaluation = skip_evaluation;
15864 skip_evaluation = false;
15865 /* Parse the template-argument-list itself. */
15866 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15867 arguments = NULL_TREE;
15869 arguments = cp_parser_template_argument_list (parser);
15870 /* Look for the `>' that ends the template-argument-list. If we find
15871 a '>>' instead, it's probably just a typo. */
15872 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15874 if (!saved_greater_than_is_operator_p)
15876 /* If we're in a nested template argument list, the '>>' has
15877 to be a typo for '> >'. We emit the error message, but we
15878 continue parsing and we push a '>' as next token, so that
15879 the argument list will be parsed correctly. Note that the
15880 global source location is still on the token before the
15881 '>>', so we need to say explicitly where we want it. */
15882 cp_token *token = cp_lexer_peek_token (parser->lexer);
15883 error ("%H%<>>%> should be %<> >%> "
15884 "within a nested template argument list",
15887 /* ??? Proper recovery should terminate two levels of
15888 template argument list here. */
15889 token->type = CPP_GREATER;
15893 /* If this is not a nested template argument list, the '>>'
15894 is a typo for '>'. Emit an error message and continue.
15895 Same deal about the token location, but here we can get it
15896 right by consuming the '>>' before issuing the diagnostic. */
15897 cp_lexer_consume_token (parser->lexer);
15898 error ("spurious %<>>%>, use %<>%> to terminate "
15899 "a template argument list");
15903 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15904 /* The `>' token might be a greater-than operator again now. */
15905 parser->greater_than_is_operator_p
15906 = saved_greater_than_is_operator_p;
15907 /* Restore the SAVED_SCOPE. */
15908 parser->scope = saved_scope;
15909 parser->qualifying_scope = saved_qualifying_scope;
15910 parser->object_scope = saved_object_scope;
15911 skip_evaluation = saved_skip_evaluation;
15916 /* MEMBER_FUNCTION is a member function, or a friend. If default
15917 arguments, or the body of the function have not yet been parsed,
15921 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15923 /* If this member is a template, get the underlying
15925 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15926 member_function = DECL_TEMPLATE_RESULT (member_function);
15928 /* There should not be any class definitions in progress at this
15929 point; the bodies of members are only parsed outside of all class
15931 gcc_assert (parser->num_classes_being_defined == 0);
15932 /* While we're parsing the member functions we might encounter more
15933 classes. We want to handle them right away, but we don't want
15934 them getting mixed up with functions that are currently in the
15936 parser->unparsed_functions_queues
15937 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15939 /* Make sure that any template parameters are in scope. */
15940 maybe_begin_member_template_processing (member_function);
15942 /* If the body of the function has not yet been parsed, parse it
15944 if (DECL_PENDING_INLINE_P (member_function))
15946 tree function_scope;
15947 cp_token_cache *tokens;
15949 /* The function is no longer pending; we are processing it. */
15950 tokens = DECL_PENDING_INLINE_INFO (member_function);
15951 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15952 DECL_PENDING_INLINE_P (member_function) = 0;
15954 /* If this is a local class, enter the scope of the containing
15956 function_scope = current_function_decl;
15957 if (function_scope)
15958 push_function_context_to (function_scope);
15961 /* Push the body of the function onto the lexer stack. */
15962 cp_parser_push_lexer_for_tokens (parser, tokens);
15964 /* Let the front end know that we going to be defining this
15966 start_preparsed_function (member_function, NULL_TREE,
15967 SF_PRE_PARSED | SF_INCLASS_INLINE);
15969 /* Don't do access checking if it is a templated function. */
15970 if (processing_template_decl)
15971 push_deferring_access_checks (dk_no_check);
15973 /* Now, parse the body of the function. */
15974 cp_parser_function_definition_after_declarator (parser,
15975 /*inline_p=*/true);
15977 if (processing_template_decl)
15978 pop_deferring_access_checks ();
15980 /* Leave the scope of the containing function. */
15981 if (function_scope)
15982 pop_function_context_from (function_scope);
15983 cp_parser_pop_lexer (parser);
15986 /* Remove any template parameters from the symbol table. */
15987 maybe_end_member_template_processing ();
15989 /* Restore the queue. */
15990 parser->unparsed_functions_queues
15991 = TREE_CHAIN (parser->unparsed_functions_queues);
15994 /* If DECL contains any default args, remember it on the unparsed
15995 functions queue. */
15998 cp_parser_save_default_args (cp_parser* parser, tree decl)
16002 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16004 probe = TREE_CHAIN (probe))
16005 if (TREE_PURPOSE (probe))
16007 TREE_PURPOSE (parser->unparsed_functions_queues)
16008 = tree_cons (current_class_type, decl,
16009 TREE_PURPOSE (parser->unparsed_functions_queues));
16015 /* FN is a FUNCTION_DECL which may contains a parameter with an
16016 unparsed DEFAULT_ARG. Parse the default args now. This function
16017 assumes that the current scope is the scope in which the default
16018 argument should be processed. */
16021 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16023 bool saved_local_variables_forbidden_p;
16026 /* While we're parsing the default args, we might (due to the
16027 statement expression extension) encounter more classes. We want
16028 to handle them right away, but we don't want them getting mixed
16029 up with default args that are currently in the queue. */
16030 parser->unparsed_functions_queues
16031 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16033 /* Local variable names (and the `this' keyword) may not appear
16034 in a default argument. */
16035 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16036 parser->local_variables_forbidden_p = true;
16038 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16040 parm = TREE_CHAIN (parm))
16042 cp_token_cache *tokens;
16043 tree default_arg = TREE_PURPOSE (parm);
16045 VEC(tree,gc) *insts;
16052 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16053 /* This can happen for a friend declaration for a function
16054 already declared with default arguments. */
16057 /* Push the saved tokens for the default argument onto the parser's
16059 tokens = DEFARG_TOKENS (default_arg);
16060 cp_parser_push_lexer_for_tokens (parser, tokens);
16062 /* Parse the assignment-expression. */
16063 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16065 if (!processing_template_decl)
16066 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16068 TREE_PURPOSE (parm) = parsed_arg;
16070 /* Update any instantiations we've already created. */
16071 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16072 VEC_iterate (tree, insts, ix, copy); ix++)
16073 TREE_PURPOSE (copy) = parsed_arg;
16075 /* If the token stream has not been completely used up, then
16076 there was extra junk after the end of the default
16078 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16079 cp_parser_error (parser, "expected %<,%>");
16081 /* Revert to the main lexer. */
16082 cp_parser_pop_lexer (parser);
16085 /* Make sure no default arg is missing. */
16086 check_default_args (fn);
16088 /* Restore the state of local_variables_forbidden_p. */
16089 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16091 /* Restore the queue. */
16092 parser->unparsed_functions_queues
16093 = TREE_CHAIN (parser->unparsed_functions_queues);
16096 /* Parse the operand of `sizeof' (or a similar operator). Returns
16097 either a TYPE or an expression, depending on the form of the
16098 input. The KEYWORD indicates which kind of expression we have
16102 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16104 static const char *format;
16105 tree expr = NULL_TREE;
16106 const char *saved_message;
16107 bool saved_integral_constant_expression_p;
16108 bool saved_non_integral_constant_expression_p;
16110 /* Initialize FORMAT the first time we get here. */
16112 format = "types may not be defined in '%s' expressions";
16114 /* Types cannot be defined in a `sizeof' expression. Save away the
16116 saved_message = parser->type_definition_forbidden_message;
16117 /* And create the new one. */
16118 parser->type_definition_forbidden_message
16119 = xmalloc (strlen (format)
16120 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16122 sprintf ((char *) parser->type_definition_forbidden_message,
16123 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16125 /* The restrictions on constant-expressions do not apply inside
16126 sizeof expressions. */
16127 saved_integral_constant_expression_p
16128 = parser->integral_constant_expression_p;
16129 saved_non_integral_constant_expression_p
16130 = parser->non_integral_constant_expression_p;
16131 parser->integral_constant_expression_p = false;
16133 /* Do not actually evaluate the expression. */
16135 /* If it's a `(', then we might be looking at the type-id
16137 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16140 bool saved_in_type_id_in_expr_p;
16142 /* We can't be sure yet whether we're looking at a type-id or an
16144 cp_parser_parse_tentatively (parser);
16145 /* Consume the `('. */
16146 cp_lexer_consume_token (parser->lexer);
16147 /* Parse the type-id. */
16148 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16149 parser->in_type_id_in_expr_p = true;
16150 type = cp_parser_type_id (parser);
16151 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16152 /* Now, look for the trailing `)'. */
16153 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16154 /* If all went well, then we're done. */
16155 if (cp_parser_parse_definitely (parser))
16157 cp_decl_specifier_seq decl_specs;
16159 /* Build a trivial decl-specifier-seq. */
16160 clear_decl_specs (&decl_specs);
16161 decl_specs.type = type;
16163 /* Call grokdeclarator to figure out what type this is. */
16164 expr = grokdeclarator (NULL,
16168 /*attrlist=*/NULL);
16172 /* If the type-id production did not work out, then we must be
16173 looking at the unary-expression production. */
16175 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16177 /* Go back to evaluating expressions. */
16180 /* Free the message we created. */
16181 free ((char *) parser->type_definition_forbidden_message);
16182 /* And restore the old one. */
16183 parser->type_definition_forbidden_message = saved_message;
16184 parser->integral_constant_expression_p
16185 = saved_integral_constant_expression_p;
16186 parser->non_integral_constant_expression_p
16187 = saved_non_integral_constant_expression_p;
16192 /* If the current declaration has no declarator, return true. */
16195 cp_parser_declares_only_class_p (cp_parser *parser)
16197 /* If the next token is a `;' or a `,' then there is no
16199 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16200 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16203 /* Update the DECL_SPECS to reflect the storage class indicated by
16207 cp_parser_set_storage_class (cp_parser *parser,
16208 cp_decl_specifier_seq *decl_specs,
16211 cp_storage_class storage_class;
16213 if (parser->in_unbraced_linkage_specification_p)
16215 error ("invalid use of %qD in linkage specification",
16216 ridpointers[keyword]);
16219 else if (decl_specs->storage_class != sc_none)
16221 decl_specs->multiple_storage_classes_p = true;
16225 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16226 && decl_specs->specs[(int) ds_thread])
16228 error ("%<__thread%> before %qD", ridpointers[keyword]);
16229 decl_specs->specs[(int) ds_thread] = 0;
16235 storage_class = sc_auto;
16238 storage_class = sc_register;
16241 storage_class = sc_static;
16244 storage_class = sc_extern;
16247 storage_class = sc_mutable;
16250 gcc_unreachable ();
16252 decl_specs->storage_class = storage_class;
16255 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16256 is true, the type is a user-defined type; otherwise it is a
16257 built-in type specified by a keyword. */
16260 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16262 bool user_defined_p)
16264 decl_specs->any_specifiers_p = true;
16266 /* If the user tries to redeclare bool or wchar_t (with, for
16267 example, in "typedef int wchar_t;") we remember that this is what
16268 happened. In system headers, we ignore these declarations so
16269 that G++ can work with system headers that are not C++-safe. */
16270 if (decl_specs->specs[(int) ds_typedef]
16272 && (type_spec == boolean_type_node
16273 || type_spec == wchar_type_node)
16274 && (decl_specs->type
16275 || decl_specs->specs[(int) ds_long]
16276 || decl_specs->specs[(int) ds_short]
16277 || decl_specs->specs[(int) ds_unsigned]
16278 || decl_specs->specs[(int) ds_signed]))
16280 decl_specs->redefined_builtin_type = type_spec;
16281 if (!decl_specs->type)
16283 decl_specs->type = type_spec;
16284 decl_specs->user_defined_type_p = false;
16287 else if (decl_specs->type)
16288 decl_specs->multiple_types_p = true;
16291 decl_specs->type = type_spec;
16292 decl_specs->user_defined_type_p = user_defined_p;
16293 decl_specs->redefined_builtin_type = NULL_TREE;
16297 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16298 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16301 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16303 return decl_specifiers->specs[(int) ds_friend] != 0;
16306 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16307 issue an error message indicating that TOKEN_DESC was expected.
16309 Returns the token consumed, if the token had the appropriate type.
16310 Otherwise, returns NULL. */
16313 cp_parser_require (cp_parser* parser,
16314 enum cpp_ttype type,
16315 const char* token_desc)
16317 if (cp_lexer_next_token_is (parser->lexer, type))
16318 return cp_lexer_consume_token (parser->lexer);
16321 /* Output the MESSAGE -- unless we're parsing tentatively. */
16322 if (!cp_parser_simulate_error (parser))
16324 char *message = concat ("expected ", token_desc, NULL);
16325 cp_parser_error (parser, message);
16332 /* Like cp_parser_require, except that tokens will be skipped until
16333 the desired token is found. An error message is still produced if
16334 the next token is not as expected. */
16337 cp_parser_skip_until_found (cp_parser* parser,
16338 enum cpp_ttype type,
16339 const char* token_desc)
16342 unsigned nesting_depth = 0;
16344 if (cp_parser_require (parser, type, token_desc))
16347 /* Skip tokens until the desired token is found. */
16350 /* Peek at the next token. */
16351 token = cp_lexer_peek_token (parser->lexer);
16352 /* If we've reached the token we want, consume it and
16354 if (token->type == type && !nesting_depth)
16356 cp_lexer_consume_token (parser->lexer);
16359 /* If we've run out of tokens, stop. */
16360 if (token->type == CPP_EOF)
16362 if (token->type == CPP_OPEN_BRACE
16363 || token->type == CPP_OPEN_PAREN
16364 || token->type == CPP_OPEN_SQUARE)
16366 else if (token->type == CPP_CLOSE_BRACE
16367 || token->type == CPP_CLOSE_PAREN
16368 || token->type == CPP_CLOSE_SQUARE)
16370 if (nesting_depth-- == 0)
16373 /* Consume this token. */
16374 cp_lexer_consume_token (parser->lexer);
16378 /* If the next token is the indicated keyword, consume it. Otherwise,
16379 issue an error message indicating that TOKEN_DESC was expected.
16381 Returns the token consumed, if the token had the appropriate type.
16382 Otherwise, returns NULL. */
16385 cp_parser_require_keyword (cp_parser* parser,
16387 const char* token_desc)
16389 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16391 if (token && token->keyword != keyword)
16393 dyn_string_t error_msg;
16395 /* Format the error message. */
16396 error_msg = dyn_string_new (0);
16397 dyn_string_append_cstr (error_msg, "expected ");
16398 dyn_string_append_cstr (error_msg, token_desc);
16399 cp_parser_error (parser, error_msg->s);
16400 dyn_string_delete (error_msg);
16407 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16408 function-definition. */
16411 cp_parser_token_starts_function_definition_p (cp_token* token)
16413 return (/* An ordinary function-body begins with an `{'. */
16414 token->type == CPP_OPEN_BRACE
16415 /* A ctor-initializer begins with a `:'. */
16416 || token->type == CPP_COLON
16417 /* A function-try-block begins with `try'. */
16418 || token->keyword == RID_TRY
16419 /* The named return value extension begins with `return'. */
16420 || token->keyword == RID_RETURN);
16423 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16427 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16431 token = cp_lexer_peek_token (parser->lexer);
16432 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16435 /* Returns TRUE iff the next token is the "," or ">" ending a
16436 template-argument. */
16439 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16443 token = cp_lexer_peek_token (parser->lexer);
16444 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16447 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16448 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16451 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16456 token = cp_lexer_peek_nth_token (parser->lexer, n);
16457 if (token->type == CPP_LESS)
16459 /* Check for the sequence `<::' in the original code. It would be lexed as
16460 `[:', where `[' is a digraph, and there is no whitespace before
16462 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16465 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16466 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16472 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16473 or none_type otherwise. */
16475 static enum tag_types
16476 cp_parser_token_is_class_key (cp_token* token)
16478 switch (token->keyword)
16483 return record_type;
16492 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16495 cp_parser_check_class_key (enum tag_types class_key, tree type)
16497 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16498 pedwarn ("%qs tag used in naming %q#T",
16499 class_key == union_type ? "union"
16500 : class_key == record_type ? "struct" : "class",
16504 /* Issue an error message if DECL is redeclared with different
16505 access than its original declaration [class.access.spec/3].
16506 This applies to nested classes and nested class templates.
16510 cp_parser_check_access_in_redeclaration (tree decl)
16512 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16515 if ((TREE_PRIVATE (decl)
16516 != (current_access_specifier == access_private_node))
16517 || (TREE_PROTECTED (decl)
16518 != (current_access_specifier == access_protected_node)))
16519 error ("%qD redeclared with different access", decl);
16522 /* Look for the `template' keyword, as a syntactic disambiguator.
16523 Return TRUE iff it is present, in which case it will be
16527 cp_parser_optional_template_keyword (cp_parser *parser)
16529 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16531 /* The `template' keyword can only be used within templates;
16532 outside templates the parser can always figure out what is a
16533 template and what is not. */
16534 if (!processing_template_decl)
16536 error ("%<template%> (as a disambiguator) is only allowed "
16537 "within templates");
16538 /* If this part of the token stream is rescanned, the same
16539 error message would be generated. So, we purge the token
16540 from the stream. */
16541 cp_lexer_purge_token (parser->lexer);
16546 /* Consume the `template' keyword. */
16547 cp_lexer_consume_token (parser->lexer);
16555 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16556 set PARSER->SCOPE, and perform other related actions. */
16559 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16564 /* Get the stored value. */
16565 value = cp_lexer_consume_token (parser->lexer)->value;
16566 /* Perform any access checks that were deferred. */
16567 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16568 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16569 /* Set the scope from the stored value. */
16570 parser->scope = TREE_VALUE (value);
16571 parser->qualifying_scope = TREE_TYPE (value);
16572 parser->object_scope = NULL_TREE;
16575 /* Consume tokens up through a non-nested END token. */
16578 cp_parser_cache_group (cp_parser *parser,
16579 enum cpp_ttype end,
16586 /* Abort a parenthesized expression if we encounter a brace. */
16587 if ((end == CPP_CLOSE_PAREN || depth == 0)
16588 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16590 /* If we've reached the end of the file, stop. */
16591 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16593 /* Consume the next token. */
16594 token = cp_lexer_consume_token (parser->lexer);
16595 /* See if it starts a new group. */
16596 if (token->type == CPP_OPEN_BRACE)
16598 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16602 else if (token->type == CPP_OPEN_PAREN)
16603 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16604 else if (token->type == end)
16609 /* Begin parsing tentatively. We always save tokens while parsing
16610 tentatively so that if the tentative parsing fails we can restore the
16614 cp_parser_parse_tentatively (cp_parser* parser)
16616 /* Enter a new parsing context. */
16617 parser->context = cp_parser_context_new (parser->context);
16618 /* Begin saving tokens. */
16619 cp_lexer_save_tokens (parser->lexer);
16620 /* In order to avoid repetitive access control error messages,
16621 access checks are queued up until we are no longer parsing
16623 push_deferring_access_checks (dk_deferred);
16626 /* Commit to the currently active tentative parse. */
16629 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16631 cp_parser_context *context;
16634 /* Mark all of the levels as committed. */
16635 lexer = parser->lexer;
16636 for (context = parser->context; context->next; context = context->next)
16638 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16640 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16641 while (!cp_lexer_saving_tokens (lexer))
16642 lexer = lexer->next;
16643 cp_lexer_commit_tokens (lexer);
16647 /* Abort the currently active tentative parse. All consumed tokens
16648 will be rolled back, and no diagnostics will be issued. */
16651 cp_parser_abort_tentative_parse (cp_parser* parser)
16653 cp_parser_simulate_error (parser);
16654 /* Now, pretend that we want to see if the construct was
16655 successfully parsed. */
16656 cp_parser_parse_definitely (parser);
16659 /* Stop parsing tentatively. If a parse error has occurred, restore the
16660 token stream. Otherwise, commit to the tokens we have consumed.
16661 Returns true if no error occurred; false otherwise. */
16664 cp_parser_parse_definitely (cp_parser* parser)
16666 bool error_occurred;
16667 cp_parser_context *context;
16669 /* Remember whether or not an error occurred, since we are about to
16670 destroy that information. */
16671 error_occurred = cp_parser_error_occurred (parser);
16672 /* Remove the topmost context from the stack. */
16673 context = parser->context;
16674 parser->context = context->next;
16675 /* If no parse errors occurred, commit to the tentative parse. */
16676 if (!error_occurred)
16678 /* Commit to the tokens read tentatively, unless that was
16680 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16681 cp_lexer_commit_tokens (parser->lexer);
16683 pop_to_parent_deferring_access_checks ();
16685 /* Otherwise, if errors occurred, roll back our state so that things
16686 are just as they were before we began the tentative parse. */
16689 cp_lexer_rollback_tokens (parser->lexer);
16690 pop_deferring_access_checks ();
16692 /* Add the context to the front of the free list. */
16693 context->next = cp_parser_context_free_list;
16694 cp_parser_context_free_list = context;
16696 return !error_occurred;
16699 /* Returns true if we are parsing tentatively and are not committed to
16700 this tentative parse. */
16703 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16705 return (cp_parser_parsing_tentatively (parser)
16706 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16709 /* Returns nonzero iff an error has occurred during the most recent
16710 tentative parse. */
16713 cp_parser_error_occurred (cp_parser* parser)
16715 return (cp_parser_parsing_tentatively (parser)
16716 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16719 /* Returns nonzero if GNU extensions are allowed. */
16722 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16724 return parser->allow_gnu_extensions_p;
16727 /* Objective-C++ Productions */
16730 /* Parse an Objective-C expression, which feeds into a primary-expression
16734 objc-message-expression
16735 objc-string-literal
16736 objc-encode-expression
16737 objc-protocol-expression
16738 objc-selector-expression
16740 Returns a tree representation of the expression. */
16743 cp_parser_objc_expression (cp_parser* parser)
16745 /* Try to figure out what kind of declaration is present. */
16746 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16750 case CPP_OPEN_SQUARE:
16751 return cp_parser_objc_message_expression (parser);
16753 case CPP_OBJC_STRING:
16754 kwd = cp_lexer_consume_token (parser->lexer);
16755 return objc_build_string_object (kwd->value);
16758 switch (kwd->keyword)
16760 case RID_AT_ENCODE:
16761 return cp_parser_objc_encode_expression (parser);
16763 case RID_AT_PROTOCOL:
16764 return cp_parser_objc_protocol_expression (parser);
16766 case RID_AT_SELECTOR:
16767 return cp_parser_objc_selector_expression (parser);
16773 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16774 cp_parser_skip_to_end_of_block_or_statement (parser);
16777 return error_mark_node;
16780 /* Parse an Objective-C message expression.
16782 objc-message-expression:
16783 [ objc-message-receiver objc-message-args ]
16785 Returns a representation of an Objective-C message. */
16788 cp_parser_objc_message_expression (cp_parser* parser)
16790 tree receiver, messageargs;
16792 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16793 receiver = cp_parser_objc_message_receiver (parser);
16794 messageargs = cp_parser_objc_message_args (parser);
16795 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16797 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16800 /* Parse an objc-message-receiver.
16802 objc-message-receiver:
16804 simple-type-specifier
16806 Returns a representation of the type or expression. */
16809 cp_parser_objc_message_receiver (cp_parser* parser)
16813 /* An Objective-C message receiver may be either (1) a type
16814 or (2) an expression. */
16815 cp_parser_parse_tentatively (parser);
16816 rcv = cp_parser_expression (parser, false);
16818 if (cp_parser_parse_definitely (parser))
16821 rcv = cp_parser_simple_type_specifier (parser,
16822 /*decl_specs=*/NULL,
16823 CP_PARSER_FLAGS_NONE);
16825 return objc_get_class_reference (rcv);
16828 /* Parse the arguments and selectors comprising an Objective-C message.
16833 objc-selector-args , objc-comma-args
16835 objc-selector-args:
16836 objc-selector [opt] : assignment-expression
16837 objc-selector-args objc-selector [opt] : assignment-expression
16840 assignment-expression
16841 objc-comma-args , assignment-expression
16843 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16844 selector arguments and TREE_VALUE containing a list of comma
16848 cp_parser_objc_message_args (cp_parser* parser)
16850 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16851 bool maybe_unary_selector_p = true;
16852 cp_token *token = cp_lexer_peek_token (parser->lexer);
16854 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16856 tree selector = NULL_TREE, arg;
16858 if (token->type != CPP_COLON)
16859 selector = cp_parser_objc_selector (parser);
16861 /* Detect if we have a unary selector. */
16862 if (maybe_unary_selector_p
16863 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16864 return build_tree_list (selector, NULL_TREE);
16866 maybe_unary_selector_p = false;
16867 cp_parser_require (parser, CPP_COLON, "`:'");
16868 arg = cp_parser_assignment_expression (parser, false);
16871 = chainon (sel_args,
16872 build_tree_list (selector, arg));
16874 token = cp_lexer_peek_token (parser->lexer);
16877 /* Handle non-selector arguments, if any. */
16878 while (token->type == CPP_COMMA)
16882 cp_lexer_consume_token (parser->lexer);
16883 arg = cp_parser_assignment_expression (parser, false);
16886 = chainon (addl_args,
16887 build_tree_list (NULL_TREE, arg));
16889 token = cp_lexer_peek_token (parser->lexer);
16892 return build_tree_list (sel_args, addl_args);
16895 /* Parse an Objective-C encode expression.
16897 objc-encode-expression:
16898 @encode objc-typename
16900 Returns an encoded representation of the type argument. */
16903 cp_parser_objc_encode_expression (cp_parser* parser)
16907 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16908 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16909 type = complete_type (cp_parser_type_id (parser));
16910 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16914 error ("%<@encode%> must specify a type as an argument");
16915 return error_mark_node;
16918 return objc_build_encode_expr (type);
16921 /* Parse an Objective-C @defs expression. */
16924 cp_parser_objc_defs_expression (cp_parser *parser)
16928 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16929 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16930 name = cp_parser_identifier (parser);
16931 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16933 return objc_get_class_ivars (name);
16936 /* Parse an Objective-C protocol expression.
16938 objc-protocol-expression:
16939 @protocol ( identifier )
16941 Returns a representation of the protocol expression. */
16944 cp_parser_objc_protocol_expression (cp_parser* parser)
16948 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16949 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16950 proto = cp_parser_identifier (parser);
16951 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16953 return objc_build_protocol_expr (proto);
16956 /* Parse an Objective-C selector expression.
16958 objc-selector-expression:
16959 @selector ( objc-method-signature )
16961 objc-method-signature:
16967 objc-selector-seq objc-selector :
16969 Returns a representation of the method selector. */
16972 cp_parser_objc_selector_expression (cp_parser* parser)
16974 tree sel_seq = NULL_TREE;
16975 bool maybe_unary_selector_p = true;
16978 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16979 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16980 token = cp_lexer_peek_token (parser->lexer);
16982 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16983 || token->type == CPP_SCOPE)
16985 tree selector = NULL_TREE;
16987 if (token->type != CPP_COLON
16988 || token->type == CPP_SCOPE)
16989 selector = cp_parser_objc_selector (parser);
16991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16992 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16994 /* Detect if we have a unary selector. */
16995 if (maybe_unary_selector_p)
16997 sel_seq = selector;
16998 goto finish_selector;
17002 cp_parser_error (parser, "expected %<:%>");
17005 maybe_unary_selector_p = false;
17006 token = cp_lexer_consume_token (parser->lexer);
17008 if (token->type == CPP_SCOPE)
17011 = chainon (sel_seq,
17012 build_tree_list (selector, NULL_TREE));
17014 = chainon (sel_seq,
17015 build_tree_list (NULL_TREE, NULL_TREE));
17019 = chainon (sel_seq,
17020 build_tree_list (selector, NULL_TREE));
17022 token = cp_lexer_peek_token (parser->lexer);
17026 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17028 return objc_build_selector_expr (sel_seq);
17031 /* Parse a list of identifiers.
17033 objc-identifier-list:
17035 objc-identifier-list , identifier
17037 Returns a TREE_LIST of identifier nodes. */
17040 cp_parser_objc_identifier_list (cp_parser* parser)
17042 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17043 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17045 while (sep->type == CPP_COMMA)
17047 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17048 list = chainon (list,
17049 build_tree_list (NULL_TREE,
17050 cp_parser_identifier (parser)));
17051 sep = cp_lexer_peek_token (parser->lexer);
17057 /* Parse an Objective-C alias declaration.
17059 objc-alias-declaration:
17060 @compatibility_alias identifier identifier ;
17062 This function registers the alias mapping with the Objective-C front-end.
17063 It returns nothing. */
17066 cp_parser_objc_alias_declaration (cp_parser* parser)
17070 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17071 alias = cp_parser_identifier (parser);
17072 orig = cp_parser_identifier (parser);
17073 objc_declare_alias (alias, orig);
17074 cp_parser_consume_semicolon_at_end_of_statement (parser);
17077 /* Parse an Objective-C class forward-declaration.
17079 objc-class-declaration:
17080 @class objc-identifier-list ;
17082 The function registers the forward declarations with the Objective-C
17083 front-end. It returns nothing. */
17086 cp_parser_objc_class_declaration (cp_parser* parser)
17088 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17089 objc_declare_class (cp_parser_objc_identifier_list (parser));
17090 cp_parser_consume_semicolon_at_end_of_statement (parser);
17093 /* Parse a list of Objective-C protocol references.
17095 objc-protocol-refs-opt:
17096 objc-protocol-refs [opt]
17098 objc-protocol-refs:
17099 < objc-identifier-list >
17101 Returns a TREE_LIST of identifiers, if any. */
17104 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17106 tree protorefs = NULL_TREE;
17108 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17110 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17111 protorefs = cp_parser_objc_identifier_list (parser);
17112 cp_parser_require (parser, CPP_GREATER, "`>'");
17118 /* Parse a Objective-C visibility specification. */
17121 cp_parser_objc_visibility_spec (cp_parser* parser)
17123 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17125 switch (vis->keyword)
17127 case RID_AT_PRIVATE:
17128 objc_set_visibility (2);
17130 case RID_AT_PROTECTED:
17131 objc_set_visibility (0);
17133 case RID_AT_PUBLIC:
17134 objc_set_visibility (1);
17140 /* Eat '@private'/'@protected'/'@public'. */
17141 cp_lexer_consume_token (parser->lexer);
17144 /* Parse an Objective-C method type. */
17147 cp_parser_objc_method_type (cp_parser* parser)
17149 objc_set_method_type
17150 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17155 /* Parse an Objective-C protocol qualifier. */
17158 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17160 tree quals = NULL_TREE, node;
17161 cp_token *token = cp_lexer_peek_token (parser->lexer);
17163 node = token->value;
17165 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17166 && (node == ridpointers [(int) RID_IN]
17167 || node == ridpointers [(int) RID_OUT]
17168 || node == ridpointers [(int) RID_INOUT]
17169 || node == ridpointers [(int) RID_BYCOPY]
17170 || node == ridpointers [(int) RID_BYREF]
17171 || node == ridpointers [(int) RID_ONEWAY]))
17173 quals = tree_cons (NULL_TREE, node, quals);
17174 cp_lexer_consume_token (parser->lexer);
17175 token = cp_lexer_peek_token (parser->lexer);
17176 node = token->value;
17182 /* Parse an Objective-C typename. */
17185 cp_parser_objc_typename (cp_parser* parser)
17187 tree typename = NULL_TREE;
17189 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17191 tree proto_quals, cp_type = NULL_TREE;
17193 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17194 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17196 /* An ObjC type name may consist of just protocol qualifiers, in which
17197 case the type shall default to 'id'. */
17198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17199 cp_type = cp_parser_type_id (parser);
17201 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17202 typename = build_tree_list (proto_quals, cp_type);
17208 /* Check to see if TYPE refers to an Objective-C selector name. */
17211 cp_parser_objc_selector_p (enum cpp_ttype type)
17213 return (type == CPP_NAME || type == CPP_KEYWORD
17214 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17215 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17216 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17217 || type == CPP_XOR || type == CPP_XOR_EQ);
17220 /* Parse an Objective-C selector. */
17223 cp_parser_objc_selector (cp_parser* parser)
17225 cp_token *token = cp_lexer_consume_token (parser->lexer);
17227 if (!cp_parser_objc_selector_p (token->type))
17229 error ("invalid Objective-C++ selector name");
17230 return error_mark_node;
17233 /* C++ operator names are allowed to appear in ObjC selectors. */
17234 switch (token->type)
17236 case CPP_AND_AND: return get_identifier ("and");
17237 case CPP_AND_EQ: return get_identifier ("and_eq");
17238 case CPP_AND: return get_identifier ("bitand");
17239 case CPP_OR: return get_identifier ("bitor");
17240 case CPP_COMPL: return get_identifier ("compl");
17241 case CPP_NOT: return get_identifier ("not");
17242 case CPP_NOT_EQ: return get_identifier ("not_eq");
17243 case CPP_OR_OR: return get_identifier ("or");
17244 case CPP_OR_EQ: return get_identifier ("or_eq");
17245 case CPP_XOR: return get_identifier ("xor");
17246 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17247 default: return token->value;
17251 /* Parse an Objective-C params list. */
17254 cp_parser_objc_method_keyword_params (cp_parser* parser)
17256 tree params = NULL_TREE;
17257 bool maybe_unary_selector_p = true;
17258 cp_token *token = cp_lexer_peek_token (parser->lexer);
17260 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17262 tree selector = NULL_TREE, typename, identifier;
17264 if (token->type != CPP_COLON)
17265 selector = cp_parser_objc_selector (parser);
17267 /* Detect if we have a unary selector. */
17268 if (maybe_unary_selector_p
17269 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17272 maybe_unary_selector_p = false;
17273 cp_parser_require (parser, CPP_COLON, "`:'");
17274 typename = cp_parser_objc_typename (parser);
17275 identifier = cp_parser_identifier (parser);
17279 objc_build_keyword_decl (selector,
17283 token = cp_lexer_peek_token (parser->lexer);
17289 /* Parse the non-keyword Objective-C params. */
17292 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17294 tree params = make_node (TREE_LIST);
17295 cp_token *token = cp_lexer_peek_token (parser->lexer);
17296 *ellipsisp = false; /* Initially, assume no ellipsis. */
17298 while (token->type == CPP_COMMA)
17300 cp_parameter_declarator *parmdecl;
17303 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17304 token = cp_lexer_peek_token (parser->lexer);
17306 if (token->type == CPP_ELLIPSIS)
17308 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17313 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17314 parm = grokdeclarator (parmdecl->declarator,
17315 &parmdecl->decl_specifiers,
17316 PARM, /*initialized=*/0,
17317 /*attrlist=*/NULL);
17319 chainon (params, build_tree_list (NULL_TREE, parm));
17320 token = cp_lexer_peek_token (parser->lexer);
17326 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17329 cp_parser_objc_interstitial_code (cp_parser* parser)
17331 cp_token *token = cp_lexer_peek_token (parser->lexer);
17333 /* If the next token is `extern' and the following token is a string
17334 literal, then we have a linkage specification. */
17335 if (token->keyword == RID_EXTERN
17336 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17337 cp_parser_linkage_specification (parser);
17338 /* Handle #pragma, if any. */
17339 else if (token->type == CPP_PRAGMA)
17340 cp_lexer_handle_pragma (parser->lexer);
17341 /* Allow stray semicolons. */
17342 else if (token->type == CPP_SEMICOLON)
17343 cp_lexer_consume_token (parser->lexer);
17344 /* Finally, try to parse a block-declaration, or a function-definition. */
17346 cp_parser_block_declaration (parser, /*statement_p=*/false);
17349 /* Parse a method signature. */
17352 cp_parser_objc_method_signature (cp_parser* parser)
17354 tree rettype, kwdparms, optparms;
17355 bool ellipsis = false;
17357 cp_parser_objc_method_type (parser);
17358 rettype = cp_parser_objc_typename (parser);
17359 kwdparms = cp_parser_objc_method_keyword_params (parser);
17360 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17362 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17365 /* Pars an Objective-C method prototype list. */
17368 cp_parser_objc_method_prototype_list (cp_parser* parser)
17370 cp_token *token = cp_lexer_peek_token (parser->lexer);
17372 while (token->keyword != RID_AT_END)
17374 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17376 objc_add_method_declaration
17377 (cp_parser_objc_method_signature (parser));
17378 cp_parser_consume_semicolon_at_end_of_statement (parser);
17381 /* Allow for interspersed non-ObjC++ code. */
17382 cp_parser_objc_interstitial_code (parser);
17384 token = cp_lexer_peek_token (parser->lexer);
17387 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17388 objc_finish_interface ();
17391 /* Parse an Objective-C method definition list. */
17394 cp_parser_objc_method_definition_list (cp_parser* parser)
17396 cp_token *token = cp_lexer_peek_token (parser->lexer);
17398 while (token->keyword != RID_AT_END)
17402 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17404 push_deferring_access_checks (dk_deferred);
17405 objc_start_method_definition
17406 (cp_parser_objc_method_signature (parser));
17408 /* For historical reasons, we accept an optional semicolon. */
17409 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17410 cp_lexer_consume_token (parser->lexer);
17412 perform_deferred_access_checks ();
17413 stop_deferring_access_checks ();
17414 meth = cp_parser_function_definition_after_declarator (parser,
17416 pop_deferring_access_checks ();
17417 objc_finish_method_definition (meth);
17420 /* Allow for interspersed non-ObjC++ code. */
17421 cp_parser_objc_interstitial_code (parser);
17423 token = cp_lexer_peek_token (parser->lexer);
17426 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17427 objc_finish_implementation ();
17430 /* Parse Objective-C ivars. */
17433 cp_parser_objc_class_ivars (cp_parser* parser)
17435 cp_token *token = cp_lexer_peek_token (parser->lexer);
17437 if (token->type != CPP_OPEN_BRACE)
17438 return; /* No ivars specified. */
17440 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17441 token = cp_lexer_peek_token (parser->lexer);
17443 while (token->type != CPP_CLOSE_BRACE)
17445 cp_decl_specifier_seq declspecs;
17446 int decl_class_or_enum_p;
17447 tree prefix_attributes;
17449 cp_parser_objc_visibility_spec (parser);
17451 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17454 cp_parser_decl_specifier_seq (parser,
17455 CP_PARSER_FLAGS_OPTIONAL,
17457 &decl_class_or_enum_p);
17458 prefix_attributes = declspecs.attributes;
17459 declspecs.attributes = NULL_TREE;
17461 /* Keep going until we hit the `;' at the end of the
17463 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17465 tree width = NULL_TREE, attributes, first_attribute, decl;
17466 cp_declarator *declarator = NULL;
17467 int ctor_dtor_or_conv_p;
17469 /* Check for a (possibly unnamed) bitfield declaration. */
17470 token = cp_lexer_peek_token (parser->lexer);
17471 if (token->type == CPP_COLON)
17474 if (token->type == CPP_NAME
17475 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17478 /* Get the name of the bitfield. */
17479 declarator = make_id_declarator (NULL_TREE,
17480 cp_parser_identifier (parser),
17484 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17485 /* Get the width of the bitfield. */
17487 = cp_parser_constant_expression (parser,
17488 /*allow_non_constant=*/false,
17493 /* Parse the declarator. */
17495 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17496 &ctor_dtor_or_conv_p,
17497 /*parenthesized_p=*/NULL,
17498 /*member_p=*/false);
17501 /* Look for attributes that apply to the ivar. */
17502 attributes = cp_parser_attributes_opt (parser);
17503 /* Remember which attributes are prefix attributes and
17505 first_attribute = attributes;
17506 /* Combine the attributes. */
17507 attributes = chainon (prefix_attributes, attributes);
17511 /* Create the bitfield declaration. */
17512 decl = grokbitfield (declarator, &declspecs, width);
17513 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17516 decl = grokfield (declarator, &declspecs,
17517 NULL_TREE, /*init_const_expr_p=*/false,
17518 NULL_TREE, attributes);
17520 /* Add the instance variable. */
17521 objc_add_instance_variable (decl);
17523 /* Reset PREFIX_ATTRIBUTES. */
17524 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17525 attributes = TREE_CHAIN (attributes);
17527 TREE_CHAIN (attributes) = NULL_TREE;
17529 token = cp_lexer_peek_token (parser->lexer);
17531 if (token->type == CPP_COMMA)
17533 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17539 cp_parser_consume_semicolon_at_end_of_statement (parser);
17540 token = cp_lexer_peek_token (parser->lexer);
17543 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17544 /* For historical reasons, we accept an optional semicolon. */
17545 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17546 cp_lexer_consume_token (parser->lexer);
17549 /* Parse an Objective-C protocol declaration. */
17552 cp_parser_objc_protocol_declaration (cp_parser* parser)
17554 tree proto, protorefs;
17557 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17558 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17560 error ("identifier expected after %<@protocol%>");
17564 /* See if we have a forward declaration or a definition. */
17565 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17567 /* Try a forward declaration first. */
17568 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17570 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17572 cp_parser_consume_semicolon_at_end_of_statement (parser);
17575 /* Ok, we got a full-fledged definition (or at least should). */
17578 proto = cp_parser_identifier (parser);
17579 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17580 objc_start_protocol (proto, protorefs);
17581 cp_parser_objc_method_prototype_list (parser);
17585 /* Parse an Objective-C superclass or category. */
17588 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17591 cp_token *next = cp_lexer_peek_token (parser->lexer);
17593 *super = *categ = NULL_TREE;
17594 if (next->type == CPP_COLON)
17596 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17597 *super = cp_parser_identifier (parser);
17599 else if (next->type == CPP_OPEN_PAREN)
17601 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17602 *categ = cp_parser_identifier (parser);
17603 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17607 /* Parse an Objective-C class interface. */
17610 cp_parser_objc_class_interface (cp_parser* parser)
17612 tree name, super, categ, protos;
17614 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17615 name = cp_parser_identifier (parser);
17616 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17617 protos = cp_parser_objc_protocol_refs_opt (parser);
17619 /* We have either a class or a category on our hands. */
17621 objc_start_category_interface (name, categ, protos);
17624 objc_start_class_interface (name, super, protos);
17625 /* Handle instance variable declarations, if any. */
17626 cp_parser_objc_class_ivars (parser);
17627 objc_continue_interface ();
17630 cp_parser_objc_method_prototype_list (parser);
17633 /* Parse an Objective-C class implementation. */
17636 cp_parser_objc_class_implementation (cp_parser* parser)
17638 tree name, super, categ;
17640 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17641 name = cp_parser_identifier (parser);
17642 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17644 /* We have either a class or a category on our hands. */
17646 objc_start_category_implementation (name, categ);
17649 objc_start_class_implementation (name, super);
17650 /* Handle instance variable declarations, if any. */
17651 cp_parser_objc_class_ivars (parser);
17652 objc_continue_implementation ();
17655 cp_parser_objc_method_definition_list (parser);
17658 /* Consume the @end token and finish off the implementation. */
17661 cp_parser_objc_end_implementation (cp_parser* parser)
17663 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17664 objc_finish_implementation ();
17667 /* Parse an Objective-C declaration. */
17670 cp_parser_objc_declaration (cp_parser* parser)
17672 /* Try to figure out what kind of declaration is present. */
17673 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17675 switch (kwd->keyword)
17678 cp_parser_objc_alias_declaration (parser);
17681 cp_parser_objc_class_declaration (parser);
17683 case RID_AT_PROTOCOL:
17684 cp_parser_objc_protocol_declaration (parser);
17686 case RID_AT_INTERFACE:
17687 cp_parser_objc_class_interface (parser);
17689 case RID_AT_IMPLEMENTATION:
17690 cp_parser_objc_class_implementation (parser);
17693 cp_parser_objc_end_implementation (parser);
17696 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17697 cp_parser_skip_to_end_of_block_or_statement (parser);
17701 /* Parse an Objective-C try-catch-finally statement.
17703 objc-try-catch-finally-stmt:
17704 @try compound-statement objc-catch-clause-seq [opt]
17705 objc-finally-clause [opt]
17707 objc-catch-clause-seq:
17708 objc-catch-clause objc-catch-clause-seq [opt]
17711 @catch ( exception-declaration ) compound-statement
17713 objc-finally-clause
17714 @finally compound-statement
17716 Returns NULL_TREE. */
17719 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17720 location_t location;
17723 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17724 location = cp_lexer_peek_token (parser->lexer)->location;
17725 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17726 node, lest it get absorbed into the surrounding block. */
17727 stmt = push_stmt_list ();
17728 cp_parser_compound_statement (parser, NULL, false);
17729 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17731 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17733 cp_parameter_declarator *parmdecl;
17736 cp_lexer_consume_token (parser->lexer);
17737 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17738 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17739 parm = grokdeclarator (parmdecl->declarator,
17740 &parmdecl->decl_specifiers,
17741 PARM, /*initialized=*/0,
17742 /*attrlist=*/NULL);
17743 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17744 objc_begin_catch_clause (parm);
17745 cp_parser_compound_statement (parser, NULL, false);
17746 objc_finish_catch_clause ();
17749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17751 cp_lexer_consume_token (parser->lexer);
17752 location = cp_lexer_peek_token (parser->lexer)->location;
17753 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17754 node, lest it get absorbed into the surrounding block. */
17755 stmt = push_stmt_list ();
17756 cp_parser_compound_statement (parser, NULL, false);
17757 objc_build_finally_clause (location, pop_stmt_list (stmt));
17760 return objc_finish_try_stmt ();
17763 /* Parse an Objective-C synchronized statement.
17765 objc-synchronized-stmt:
17766 @synchronized ( expression ) compound-statement
17768 Returns NULL_TREE. */
17771 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17772 location_t location;
17775 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17777 location = cp_lexer_peek_token (parser->lexer)->location;
17778 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17779 lock = cp_parser_expression (parser, false);
17780 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17782 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17783 node, lest it get absorbed into the surrounding block. */
17784 stmt = push_stmt_list ();
17785 cp_parser_compound_statement (parser, NULL, false);
17787 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17790 /* Parse an Objective-C throw statement.
17793 @throw assignment-expression [opt] ;
17795 Returns a constructed '@throw' statement. */
17798 cp_parser_objc_throw_statement (cp_parser *parser) {
17799 tree expr = NULL_TREE;
17801 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17804 expr = cp_parser_assignment_expression (parser, false);
17806 cp_parser_consume_semicolon_at_end_of_statement (parser);
17808 return objc_build_throw_stmt (expr);
17811 /* Parse an Objective-C statement. */
17814 cp_parser_objc_statement (cp_parser * parser) {
17815 /* Try to figure out what kind of declaration is present. */
17816 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17818 switch (kwd->keyword)
17821 return cp_parser_objc_try_catch_finally_statement (parser);
17822 case RID_AT_SYNCHRONIZED:
17823 return cp_parser_objc_synchronized_statement (parser);
17825 return cp_parser_objc_throw_statement (parser);
17827 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17828 cp_parser_skip_to_end_of_block_or_statement (parser);
17831 return error_mark_node;
17836 static GTY (()) cp_parser *the_parser;
17838 /* External interface. */
17840 /* Parse one entire translation unit. */
17843 c_parse_file (void)
17845 bool error_occurred;
17846 static bool already_called = false;
17848 if (already_called)
17850 sorry ("inter-module optimizations not implemented for C++");
17853 already_called = true;
17855 the_parser = cp_parser_new ();
17856 push_deferring_access_checks (flag_access_control
17857 ? dk_no_deferred : dk_no_check);
17858 error_occurred = cp_parser_translation_unit (the_parser);
17862 /* This variable must be provided by every front end. */
17866 #include "gt-cp-parser.h"