2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
49 typedef struct cp_token GTY (())
51 /* The kind of token. */
52 ENUM_BITFIELD (cpp_ttype) type : 8;
53 /* If this token is a keyword, this value indicates which keyword.
54 Otherwise, this value is RID_MAX. */
55 ENUM_BITFIELD (rid) keyword : 8;
58 /* True if this token is from a system header. */
59 BOOL_BITFIELD in_system_header : 1;
60 /* True if this token is from a context where it is implicitly extern "C" */
61 BOOL_BITFIELD implicit_extern_c : 1;
62 /* The value associated with this token, if any. */
64 /* The location at which this token was found. */
68 /* We use a stack of token pointer for saving token sets. */
69 typedef struct cp_token *cp_token_position;
70 DEF_VEC_P (cp_token_position);
71 DEF_VEC_ALLOC_P (cp_token_position,heap);
73 static const cp_token eof_token =
75 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
76 #if USE_MAPPED_LOCATION
83 /* The cp_lexer structure represents the C++ lexer. It is responsible
84 for managing the token stream from the preprocessor and supplying
85 it to the parser. Tokens are never added to the cp_lexer after
88 typedef struct cp_lexer GTY (())
90 /* The memory allocated for the buffer. NULL if this lexer does not
91 own the token buffer. */
92 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
93 /* If the lexer owns the buffer, this is the number of tokens in the
97 /* A pointer just past the last available token. The tokens
98 in this lexer are [buffer, last_token). */
99 cp_token_position GTY ((skip)) last_token;
101 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
102 no more available tokens. */
103 cp_token_position GTY ((skip)) next_token;
105 /* A stack indicating positions at which cp_lexer_save_tokens was
106 called. The top entry is the most recent position at which we
107 began saving tokens. If the stack is non-empty, we are saving
109 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
111 /* True if we should output debugging information. */
114 /* The next lexer in a linked list of lexers. */
115 struct cp_lexer *next;
118 /* cp_token_cache is a range of tokens. There is no need to represent
119 allocate heap memory for it, since tokens are never removed from the
120 lexer's array. There is also no need for the GC to walk through
121 a cp_token_cache, since everything in here is referenced through
124 typedef struct cp_token_cache GTY(())
126 /* The beginning of the token range. */
127 cp_token * GTY((skip)) first;
129 /* Points immediately after the last token in the range. */
130 cp_token * GTY ((skip)) last;
135 static cp_lexer *cp_lexer_new_main
137 static cp_lexer *cp_lexer_new_from_tokens
138 (cp_token_cache *tokens);
139 static void cp_lexer_destroy
141 static int cp_lexer_saving_tokens
143 static cp_token_position cp_lexer_token_position
145 static cp_token *cp_lexer_token_at
146 (cp_lexer *, cp_token_position);
147 static void cp_lexer_get_preprocessor_token
148 (cp_lexer *, cp_token *);
149 static inline cp_token *cp_lexer_peek_token
151 static cp_token *cp_lexer_peek_nth_token
152 (cp_lexer *, size_t);
153 static inline bool cp_lexer_next_token_is
154 (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_not
156 (cp_lexer *, enum cpp_ttype);
157 static bool cp_lexer_next_token_is_keyword
158 (cp_lexer *, enum rid);
159 static cp_token *cp_lexer_consume_token
161 static void cp_lexer_purge_token
163 static void cp_lexer_purge_tokens_after
164 (cp_lexer *, cp_token_position);
165 static void cp_lexer_handle_pragma
167 static void cp_lexer_save_tokens
169 static void cp_lexer_commit_tokens
171 static void cp_lexer_rollback_tokens
173 #ifdef ENABLE_CHECKING
174 static void cp_lexer_print_token
175 (FILE *, cp_token *);
176 static inline bool cp_lexer_debugging_p
178 static void cp_lexer_start_debugging
179 (cp_lexer *) ATTRIBUTE_UNUSED;
180 static void cp_lexer_stop_debugging
181 (cp_lexer *) ATTRIBUTE_UNUSED;
183 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
184 about passing NULL to functions that require non-NULL arguments
185 (fputs, fprintf). It will never be used, so all we need is a value
186 of the right type that's guaranteed not to be NULL. */
187 #define cp_lexer_debug_stream stdout
188 #define cp_lexer_print_token(str, tok) (void) 0
189 #define cp_lexer_debugging_p(lexer) 0
190 #endif /* ENABLE_CHECKING */
192 static cp_token_cache *cp_token_cache_new
193 (cp_token *, cp_token *);
195 /* Manifest constants. */
196 #define CP_LEXER_BUFFER_SIZE 10000
197 #define CP_SAVED_TOKEN_STACK 5
199 /* A token type for keywords, as opposed to ordinary identifiers. */
200 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
202 /* A token type for template-ids. If a template-id is processed while
203 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
204 the value of the CPP_TEMPLATE_ID is whatever was returned by
205 cp_parser_template_id. */
206 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
208 /* A token type for nested-name-specifiers. If a
209 nested-name-specifier is processed while parsing tentatively, it is
210 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
211 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
212 cp_parser_nested_name_specifier_opt. */
213 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
215 /* A token type for tokens that are not tokens at all; these are used
216 to represent slots in the array where there used to be a token
217 that has now been deleted. */
218 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
220 /* The number of token types, including C++-specific ones. */
221 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
225 #ifdef ENABLE_CHECKING
226 /* The stream to which debugging output should be written. */
227 static FILE *cp_lexer_debug_stream;
228 #endif /* ENABLE_CHECKING */
230 /* Create a new main C++ lexer, the lexer that gets tokens from the
234 cp_lexer_new_main (void)
236 cp_token first_token;
243 /* It's possible that lexing the first token will load a PCH file,
244 which is a GC collection point. So we have to grab the first
245 token before allocating any memory. Pragmas must not be deferred
246 as -fpch-preprocess can generate a pragma to load the PCH file in
247 the preprocessed output used by -save-temps. */
248 cp_lexer_get_preprocessor_token (NULL, &first_token);
250 /* Tell cpplib we want CPP_PRAGMA tokens. */
251 cpp_get_options (parse_in)->defer_pragmas = true;
253 /* Tell c_lex not to merge string constants. */
254 c_lex_return_raw_strings = true;
256 c_common_no_more_pch ();
258 /* Allocate the memory. */
259 lexer = GGC_CNEW (cp_lexer);
261 #ifdef ENABLE_CHECKING
262 /* Initially we are not debugging. */
263 lexer->debugging_p = false;
264 #endif /* ENABLE_CHECKING */
265 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
266 CP_SAVED_TOKEN_STACK);
268 /* Create the buffer. */
269 alloc = CP_LEXER_BUFFER_SIZE;
270 buffer = ggc_alloc (alloc * sizeof (cp_token));
272 /* Put the first token in the buffer. */
277 /* Get the remaining tokens from the preprocessor. */
278 while (pos->type != CPP_EOF)
285 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
286 pos = buffer + space;
288 cp_lexer_get_preprocessor_token (lexer, pos);
290 lexer->buffer = buffer;
291 lexer->buffer_length = alloc - space;
292 lexer->last_token = pos;
293 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
295 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
296 direct calls to c_lex. Those callers all expect c_lex to do
297 string constant concatenation. */
298 c_lex_return_raw_strings = false;
300 gcc_assert (lexer->next_token->type != CPP_PURGED);
304 /* Create a new lexer whose token stream is primed with the tokens in
305 CACHE. When these tokens are exhausted, no new tokens will be read. */
308 cp_lexer_new_from_tokens (cp_token_cache *cache)
310 cp_token *first = cache->first;
311 cp_token *last = cache->last;
312 cp_lexer *lexer = GGC_CNEW (cp_lexer);
314 /* We do not own the buffer. */
315 lexer->buffer = NULL;
316 lexer->buffer_length = 0;
317 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
318 lexer->last_token = last;
320 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
321 CP_SAVED_TOKEN_STACK);
323 #ifdef ENABLE_CHECKING
324 /* Initially we are not debugging. */
325 lexer->debugging_p = false;
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
332 /* Frees all resources associated with LEXER. */
335 cp_lexer_destroy (cp_lexer *lexer)
338 ggc_free (lexer->buffer);
339 VEC_free (cp_token_position, heap, lexer->saved_tokens);
343 /* Returns nonzero if debugging information should be output. */
345 #ifdef ENABLE_CHECKING
348 cp_lexer_debugging_p (cp_lexer *lexer)
350 return lexer->debugging_p;
353 #endif /* ENABLE_CHECKING */
355 static inline cp_token_position
356 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
358 gcc_assert (!previous_p || lexer->next_token != &eof_token);
360 return lexer->next_token - previous_p;
363 static inline cp_token *
364 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
369 /* nonzero if we are presently saving tokens. */
372 cp_lexer_saving_tokens (const cp_lexer* lexer)
374 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
377 /* Store the next token from the preprocessor in *TOKEN. Return true
381 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
384 static int is_extern_c = 0;
386 /* Get a new token from the preprocessor. */
388 = c_lex_with_flags (&token->value, &token->location, &token->flags);
389 token->in_system_header = in_system_header;
391 /* On some systems, some header files are surrounded by an
392 implicit extern "C" block. Set a flag in the token if it
393 comes from such a header. */
394 is_extern_c += pending_lang_change;
395 pending_lang_change = 0;
396 token->implicit_extern_c = is_extern_c > 0;
398 /* Check to see if this token is a keyword. */
399 if (token->type == CPP_NAME
400 && C_IS_RESERVED_WORD (token->value))
402 /* Mark this token as a keyword. */
403 token->type = CPP_KEYWORD;
404 /* Record which keyword. */
405 token->keyword = C_RID_CODE (token->value);
406 /* Update the value. Some keywords are mapped to particular
407 entities, rather than simply having the value of the
408 corresponding IDENTIFIER_NODE. For example, `__const' is
409 mapped to `const'. */
410 token->value = ridpointers[token->keyword];
412 /* Handle Objective-C++ keywords. */
413 else if (token->type == CPP_AT_NAME)
415 token->type = CPP_KEYWORD;
416 switch (C_RID_CODE (token->value))
418 /* Map 'class' to '@class', 'private' to '@private', etc. */
419 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
420 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
421 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
422 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
423 case RID_THROW: token->keyword = RID_AT_THROW; break;
424 case RID_TRY: token->keyword = RID_AT_TRY; break;
425 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
426 default: token->keyword = C_RID_CODE (token->value);
430 token->keyword = RID_MAX;
433 /* Update the globals input_location and in_system_header from TOKEN. */
435 cp_lexer_set_source_position_from_token (cp_token *token)
437 if (token->type != CPP_EOF)
439 input_location = token->location;
440 in_system_header = token->in_system_header;
444 /* Return a pointer to the next token in the token stream, but do not
447 static inline cp_token *
448 cp_lexer_peek_token (cp_lexer *lexer)
450 if (cp_lexer_debugging_p (lexer))
452 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
453 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
454 putc ('\n', cp_lexer_debug_stream);
456 return lexer->next_token;
459 /* Return true if the next token has the indicated TYPE. */
462 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
464 return cp_lexer_peek_token (lexer)->type == type;
467 /* Return true if the next token does not have the indicated TYPE. */
470 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
472 return !cp_lexer_next_token_is (lexer, type);
475 /* Return true if the next token is the indicated KEYWORD. */
478 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
482 /* Peek at the next token. */
483 token = cp_lexer_peek_token (lexer);
484 /* Check to see if it is the indicated keyword. */
485 return token->keyword == keyword;
488 /* Return a pointer to the Nth token in the token stream. If N is 1,
489 then this is precisely equivalent to cp_lexer_peek_token (except
490 that it is not inline). One would like to disallow that case, but
491 there is one case (cp_parser_nth_token_starts_template_id) where
492 the caller passes a variable for N and it might be 1. */
495 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
499 /* N is 1-based, not zero-based. */
500 gcc_assert (n > 0 && lexer->next_token != &eof_token);
502 if (cp_lexer_debugging_p (lexer))
503 fprintf (cp_lexer_debug_stream,
504 "cp_lexer: peeking ahead %ld at token: ", (long)n);
507 token = lexer->next_token;
511 if (token == lexer->last_token)
513 token = (cp_token *)&eof_token;
517 if (token->type != CPP_PURGED)
521 if (cp_lexer_debugging_p (lexer))
523 cp_lexer_print_token (cp_lexer_debug_stream, token);
524 putc ('\n', cp_lexer_debug_stream);
530 /* Return the next token, and advance the lexer's next_token pointer
531 to point to the next non-purged token. */
534 cp_lexer_consume_token (cp_lexer* lexer)
536 cp_token *token = lexer->next_token;
538 gcc_assert (token != &eof_token);
543 if (lexer->next_token == lexer->last_token)
545 lexer->next_token = (cp_token *)&eof_token;
550 while (lexer->next_token->type == CPP_PURGED);
552 cp_lexer_set_source_position_from_token (token);
554 /* Provide debugging output. */
555 if (cp_lexer_debugging_p (lexer))
557 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
558 cp_lexer_print_token (cp_lexer_debug_stream, token);
559 putc ('\n', cp_lexer_debug_stream);
565 /* Permanently remove the next token from the token stream, and
566 advance the next_token pointer to refer to the next non-purged
570 cp_lexer_purge_token (cp_lexer *lexer)
572 cp_token *tok = lexer->next_token;
574 gcc_assert (tok != &eof_token);
575 tok->type = CPP_PURGED;
576 tok->location = UNKNOWN_LOCATION;
577 tok->value = NULL_TREE;
578 tok->keyword = RID_MAX;
583 if (tok == lexer->last_token)
585 tok = (cp_token *)&eof_token;
589 while (tok->type == CPP_PURGED);
590 lexer->next_token = tok;
593 /* Permanently remove all tokens after TOK, up to, but not
594 including, the token that will be returned next by
595 cp_lexer_peek_token. */
598 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
600 cp_token *peek = lexer->next_token;
602 if (peek == &eof_token)
603 peek = lexer->last_token;
605 gcc_assert (tok < peek);
607 for ( tok += 1; tok != peek; tok += 1)
609 tok->type = CPP_PURGED;
610 tok->location = UNKNOWN_LOCATION;
611 tok->value = NULL_TREE;
612 tok->keyword = RID_MAX;
616 /* Consume and handle a pragma token. */
618 cp_lexer_handle_pragma (cp_lexer *lexer)
621 cp_token *token = cp_lexer_consume_token (lexer);
622 gcc_assert (token->type == CPP_PRAGMA);
623 gcc_assert (token->value);
625 s.len = TREE_STRING_LENGTH (token->value);
626 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
628 cpp_handle_deferred_pragma (parse_in, &s);
630 /* Clearing token->value here means that we will get an ICE if we
631 try to process this #pragma again (which should be impossible). */
635 /* Begin saving tokens. All tokens consumed after this point will be
639 cp_lexer_save_tokens (cp_lexer* lexer)
641 /* Provide debugging output. */
642 if (cp_lexer_debugging_p (lexer))
643 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
645 VEC_safe_push (cp_token_position, heap,
646 lexer->saved_tokens, lexer->next_token);
649 /* Commit to the portion of the token stream most recently saved. */
652 cp_lexer_commit_tokens (cp_lexer* lexer)
654 /* Provide debugging output. */
655 if (cp_lexer_debugging_p (lexer))
656 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
658 VEC_pop (cp_token_position, lexer->saved_tokens);
661 /* Return all tokens saved since the last call to cp_lexer_save_tokens
662 to the token stream. Stop saving tokens. */
665 cp_lexer_rollback_tokens (cp_lexer* lexer)
667 /* Provide debugging output. */
668 if (cp_lexer_debugging_p (lexer))
669 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
671 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
674 /* Print a representation of the TOKEN on the STREAM. */
676 #ifdef ENABLE_CHECKING
679 cp_lexer_print_token (FILE * stream, cp_token *token)
681 /* We don't use cpp_type2name here because the parser defines
682 a few tokens of its own. */
683 static const char *const token_names[] = {
684 /* cpplib-defined token types */
690 /* C++ parser token types - see "Manifest constants", above. */
693 "NESTED_NAME_SPECIFIER",
697 /* If we have a name for the token, print it out. Otherwise, we
698 simply give the numeric code. */
699 gcc_assert (token->type < ARRAY_SIZE(token_names));
700 fputs (token_names[token->type], stream);
702 /* For some tokens, print the associated data. */
706 /* Some keywords have a value that is not an IDENTIFIER_NODE.
707 For example, `struct' is mapped to an INTEGER_CST. */
708 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
710 /* else fall through */
712 fputs (IDENTIFIER_POINTER (token->value), stream);
718 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
726 /* Start emitting debugging information. */
729 cp_lexer_start_debugging (cp_lexer* lexer)
731 lexer->debugging_p = true;
734 /* Stop emitting debugging information. */
737 cp_lexer_stop_debugging (cp_lexer* lexer)
739 lexer->debugging_p = false;
742 #endif /* ENABLE_CHECKING */
744 /* Create a new cp_token_cache, representing a range of tokens. */
746 static cp_token_cache *
747 cp_token_cache_new (cp_token *first, cp_token *last)
749 cp_token_cache *cache = GGC_NEW (cp_token_cache);
750 cache->first = first;
756 /* Decl-specifiers. */
758 static void clear_decl_specs
759 (cp_decl_specifier_seq *);
761 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
764 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
766 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
771 /* Nothing other than the parser should be creating declarators;
772 declarators are a semi-syntactic representation of C++ entities.
773 Other parts of the front end that need to create entities (like
774 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
776 static cp_declarator *make_call_declarator
777 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
778 static cp_declarator *make_array_declarator
779 (cp_declarator *, tree);
780 static cp_declarator *make_pointer_declarator
781 (cp_cv_quals, cp_declarator *);
782 static cp_declarator *make_reference_declarator
783 (cp_cv_quals, cp_declarator *);
784 static cp_parameter_declarator *make_parameter_declarator
785 (cp_decl_specifier_seq *, cp_declarator *, tree);
786 static cp_declarator *make_ptrmem_declarator
787 (cp_cv_quals, tree, cp_declarator *);
789 cp_declarator *cp_error_declarator;
791 /* The obstack on which declarators and related data structures are
793 static struct obstack declarator_obstack;
795 /* Alloc BYTES from the declarator memory pool. */
798 alloc_declarator (size_t bytes)
800 return obstack_alloc (&declarator_obstack, bytes);
803 /* Allocate a declarator of the indicated KIND. Clear fields that are
804 common to all declarators. */
806 static cp_declarator *
807 make_declarator (cp_declarator_kind kind)
809 cp_declarator *declarator;
811 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
812 declarator->kind = kind;
813 declarator->attributes = NULL_TREE;
814 declarator->declarator = NULL;
819 /* Make a declarator for a generalized identifier. If non-NULL, the
820 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
821 just UNQUALIFIED_NAME. */
823 static cp_declarator *
824 make_id_declarator (tree qualifying_scope, tree unqualified_name)
826 cp_declarator *declarator;
828 /* It is valid to write:
830 class C { void f(); };
834 The standard is not clear about whether `typedef const C D' is
835 legal; as of 2002-09-15 the committee is considering that
836 question. EDG 3.0 allows that syntax. Therefore, we do as
838 if (qualifying_scope && TYPE_P (qualifying_scope))
839 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
841 declarator = make_declarator (cdk_id);
842 declarator->u.id.qualifying_scope = qualifying_scope;
843 declarator->u.id.unqualified_name = unqualified_name;
844 declarator->u.id.sfk = sfk_none;
849 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
850 of modifiers such as const or volatile to apply to the pointer
851 type, represented as identifiers. */
854 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
856 cp_declarator *declarator;
858 declarator = make_declarator (cdk_pointer);
859 declarator->declarator = target;
860 declarator->u.pointer.qualifiers = cv_qualifiers;
861 declarator->u.pointer.class_type = NULL_TREE;
866 /* Like make_pointer_declarator -- but for references. */
869 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
871 cp_declarator *declarator;
873 declarator = make_declarator (cdk_reference);
874 declarator->declarator = target;
875 declarator->u.pointer.qualifiers = cv_qualifiers;
876 declarator->u.pointer.class_type = NULL_TREE;
881 /* Like make_pointer_declarator -- but for a pointer to a non-static
882 member of CLASS_TYPE. */
885 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
886 cp_declarator *pointee)
888 cp_declarator *declarator;
890 declarator = make_declarator (cdk_ptrmem);
891 declarator->declarator = pointee;
892 declarator->u.pointer.qualifiers = cv_qualifiers;
893 declarator->u.pointer.class_type = class_type;
898 /* Make a declarator for the function given by TARGET, with the
899 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
900 "const"-qualified member function. The EXCEPTION_SPECIFICATION
901 indicates what exceptions can be thrown. */
904 make_call_declarator (cp_declarator *target,
905 cp_parameter_declarator *parms,
906 cp_cv_quals cv_qualifiers,
907 tree exception_specification)
909 cp_declarator *declarator;
911 declarator = make_declarator (cdk_function);
912 declarator->declarator = target;
913 declarator->u.function.parameters = parms;
914 declarator->u.function.qualifiers = cv_qualifiers;
915 declarator->u.function.exception_specification = exception_specification;
920 /* Make a declarator for an array of BOUNDS elements, each of which is
921 defined by ELEMENT. */
924 make_array_declarator (cp_declarator *element, tree bounds)
926 cp_declarator *declarator;
928 declarator = make_declarator (cdk_array);
929 declarator->declarator = element;
930 declarator->u.array.bounds = bounds;
935 cp_parameter_declarator *no_parameters;
937 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
938 DECLARATOR and DEFAULT_ARGUMENT. */
940 cp_parameter_declarator *
941 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
942 cp_declarator *declarator,
943 tree default_argument)
945 cp_parameter_declarator *parameter;
947 parameter = ((cp_parameter_declarator *)
948 alloc_declarator (sizeof (cp_parameter_declarator)));
949 parameter->next = NULL;
951 parameter->decl_specifiers = *decl_specifiers;
953 clear_decl_specs (¶meter->decl_specifiers);
954 parameter->declarator = declarator;
955 parameter->default_argument = default_argument;
956 parameter->ellipsis_p = false;
966 A cp_parser parses the token stream as specified by the C++
967 grammar. Its job is purely parsing, not semantic analysis. For
968 example, the parser breaks the token stream into declarators,
969 expressions, statements, and other similar syntactic constructs.
970 It does not check that the types of the expressions on either side
971 of an assignment-statement are compatible, or that a function is
972 not declared with a parameter of type `void'.
974 The parser invokes routines elsewhere in the compiler to perform
975 semantic analysis and to build up the abstract syntax tree for the
978 The parser (and the template instantiation code, which is, in a
979 way, a close relative of parsing) are the only parts of the
980 compiler that should be calling push_scope and pop_scope, or
981 related functions. The parser (and template instantiation code)
982 keeps track of what scope is presently active; everything else
983 should simply honor that. (The code that generates static
984 initializers may also need to set the scope, in order to check
985 access control correctly when emitting the initializers.)
990 The parser is of the standard recursive-descent variety. Upcoming
991 tokens in the token stream are examined in order to determine which
992 production to use when parsing a non-terminal. Some C++ constructs
993 require arbitrary look ahead to disambiguate. For example, it is
994 impossible, in the general case, to tell whether a statement is an
995 expression or declaration without scanning the entire statement.
996 Therefore, the parser is capable of "parsing tentatively." When the
997 parser is not sure what construct comes next, it enters this mode.
998 Then, while we attempt to parse the construct, the parser queues up
999 error messages, rather than issuing them immediately, and saves the
1000 tokens it consumes. If the construct is parsed successfully, the
1001 parser "commits", i.e., it issues any queued error messages and
1002 the tokens that were being preserved are permanently discarded.
1003 If, however, the construct is not parsed successfully, the parser
1004 rolls back its state completely so that it can resume parsing using
1005 a different alternative.
1010 The performance of the parser could probably be improved substantially.
1011 We could often eliminate the need to parse tentatively by looking ahead
1012 a little bit. In some places, this approach might not entirely eliminate
1013 the need to parse tentatively, but it might still speed up the average
1016 /* Flags that are passed to some parsing functions. These values can
1017 be bitwise-ored together. */
1019 typedef enum cp_parser_flags
1022 CP_PARSER_FLAGS_NONE = 0x0,
1023 /* The construct is optional. If it is not present, then no error
1024 should be issued. */
1025 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1026 /* When parsing a type-specifier, do not allow user-defined types. */
1027 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1030 /* The different kinds of declarators we want to parse. */
1032 typedef enum cp_parser_declarator_kind
1034 /* We want an abstract declarator. */
1035 CP_PARSER_DECLARATOR_ABSTRACT,
1036 /* We want a named declarator. */
1037 CP_PARSER_DECLARATOR_NAMED,
1038 /* We don't mind, but the name must be an unqualified-id. */
1039 CP_PARSER_DECLARATOR_EITHER
1040 } cp_parser_declarator_kind;
1042 /* The precedence values used to parse binary expressions. The minimum value
1043 of PREC must be 1, because zero is reserved to quickly discriminate
1044 binary operators from other tokens. */
1049 PREC_LOGICAL_OR_EXPRESSION,
1050 PREC_LOGICAL_AND_EXPRESSION,
1051 PREC_INCLUSIVE_OR_EXPRESSION,
1052 PREC_EXCLUSIVE_OR_EXPRESSION,
1053 PREC_AND_EXPRESSION,
1054 PREC_EQUALITY_EXPRESSION,
1055 PREC_RELATIONAL_EXPRESSION,
1056 PREC_SHIFT_EXPRESSION,
1057 PREC_ADDITIVE_EXPRESSION,
1058 PREC_MULTIPLICATIVE_EXPRESSION,
1060 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1063 /* A mapping from a token type to a corresponding tree node type, with a
1064 precedence value. */
1066 typedef struct cp_parser_binary_operations_map_node
1068 /* The token type. */
1069 enum cpp_ttype token_type;
1070 /* The corresponding tree code. */
1071 enum tree_code tree_type;
1072 /* The precedence of this operator. */
1073 enum cp_parser_prec prec;
1074 } cp_parser_binary_operations_map_node;
1076 /* The status of a tentative parse. */
1078 typedef enum cp_parser_status_kind
1080 /* No errors have occurred. */
1081 CP_PARSER_STATUS_KIND_NO_ERROR,
1082 /* An error has occurred. */
1083 CP_PARSER_STATUS_KIND_ERROR,
1084 /* We are committed to this tentative parse, whether or not an error
1086 CP_PARSER_STATUS_KIND_COMMITTED
1087 } cp_parser_status_kind;
1089 typedef struct cp_parser_expression_stack_entry
1092 enum tree_code tree_type;
1094 } cp_parser_expression_stack_entry;
1096 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1097 entries because precedence levels on the stack are monotonically
1099 typedef struct cp_parser_expression_stack_entry
1100 cp_parser_expression_stack[NUM_PREC_VALUES];
1102 /* Context that is saved and restored when parsing tentatively. */
1103 typedef struct cp_parser_context GTY (())
1105 /* If this is a tentative parsing context, the status of the
1107 enum cp_parser_status_kind status;
1108 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1109 that are looked up in this context must be looked up both in the
1110 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1111 the context of the containing expression. */
1114 /* The next parsing context in the stack. */
1115 struct cp_parser_context *next;
1116 } cp_parser_context;
1120 /* Constructors and destructors. */
1122 static cp_parser_context *cp_parser_context_new
1123 (cp_parser_context *);
1125 /* Class variables. */
1127 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1129 /* The operator-precedence table used by cp_parser_binary_expression.
1130 Transformed into an associative array (binops_by_token) by
1133 static const cp_parser_binary_operations_map_node binops[] = {
1134 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1135 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1137 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1138 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1141 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1142 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1144 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1145 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1147 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1148 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1150 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1152 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1154 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1155 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1157 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1159 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1161 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1163 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1165 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1168 /* The same as binops, but initialized by cp_parser_new so that
1169 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1171 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1173 /* Constructors and destructors. */
1175 /* Construct a new context. The context below this one on the stack
1176 is given by NEXT. */
1178 static cp_parser_context *
1179 cp_parser_context_new (cp_parser_context* next)
1181 cp_parser_context *context;
1183 /* Allocate the storage. */
1184 if (cp_parser_context_free_list != NULL)
1186 /* Pull the first entry from the free list. */
1187 context = cp_parser_context_free_list;
1188 cp_parser_context_free_list = context->next;
1189 memset (context, 0, sizeof (*context));
1192 context = GGC_CNEW (cp_parser_context);
1194 /* No errors have occurred yet in this context. */
1195 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1196 /* If this is not the bottomost context, copy information that we
1197 need from the previous context. */
1200 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1201 expression, then we are parsing one in this context, too. */
1202 context->object_type = next->object_type;
1203 /* Thread the stack. */
1204 context->next = next;
1210 /* The cp_parser structure represents the C++ parser. */
1212 typedef struct cp_parser GTY(())
1214 /* The lexer from which we are obtaining tokens. */
1217 /* The scope in which names should be looked up. If NULL_TREE, then
1218 we look up names in the scope that is currently open in the
1219 source program. If non-NULL, this is either a TYPE or
1220 NAMESPACE_DECL for the scope in which we should look. It can
1221 also be ERROR_MARK, when we've parsed a bogus scope.
1223 This value is not cleared automatically after a name is looked
1224 up, so we must be careful to clear it before starting a new look
1225 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1226 will look up `Z' in the scope of `X', rather than the current
1227 scope.) Unfortunately, it is difficult to tell when name lookup
1228 is complete, because we sometimes peek at a token, look it up,
1229 and then decide not to consume it. */
1232 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1233 last lookup took place. OBJECT_SCOPE is used if an expression
1234 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1235 respectively. QUALIFYING_SCOPE is used for an expression of the
1236 form "X::Y"; it refers to X. */
1238 tree qualifying_scope;
1240 /* A stack of parsing contexts. All but the bottom entry on the
1241 stack will be tentative contexts.
1243 We parse tentatively in order to determine which construct is in
1244 use in some situations. For example, in order to determine
1245 whether a statement is an expression-statement or a
1246 declaration-statement we parse it tentatively as a
1247 declaration-statement. If that fails, we then reparse the same
1248 token stream as an expression-statement. */
1249 cp_parser_context *context;
1251 /* True if we are parsing GNU C++. If this flag is not set, then
1252 GNU extensions are not recognized. */
1253 bool allow_gnu_extensions_p;
1255 /* TRUE if the `>' token should be interpreted as the greater-than
1256 operator. FALSE if it is the end of a template-id or
1257 template-parameter-list. */
1258 bool greater_than_is_operator_p;
1260 /* TRUE if default arguments are allowed within a parameter list
1261 that starts at this point. FALSE if only a gnu extension makes
1262 them permissible. */
1263 bool default_arg_ok_p;
1265 /* TRUE if we are parsing an integral constant-expression. See
1266 [expr.const] for a precise definition. */
1267 bool integral_constant_expression_p;
1269 /* TRUE if we are parsing an integral constant-expression -- but a
1270 non-constant expression should be permitted as well. This flag
1271 is used when parsing an array bound so that GNU variable-length
1272 arrays are tolerated. */
1273 bool allow_non_integral_constant_expression_p;
1275 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1276 been seen that makes the expression non-constant. */
1277 bool non_integral_constant_expression_p;
1279 /* TRUE if local variable names and `this' are forbidden in the
1281 bool local_variables_forbidden_p;
1283 /* TRUE if the declaration we are parsing is part of a
1284 linkage-specification of the form `extern string-literal
1286 bool in_unbraced_linkage_specification_p;
1288 /* TRUE if we are presently parsing a declarator, after the
1289 direct-declarator. */
1290 bool in_declarator_p;
1292 /* TRUE if we are presently parsing a template-argument-list. */
1293 bool in_template_argument_list_p;
1295 /* TRUE if we are presently parsing the body of an
1296 iteration-statement. */
1297 bool in_iteration_statement_p;
1299 /* TRUE if we are presently parsing the body of a switch
1301 bool in_switch_statement_p;
1303 /* TRUE if we are parsing a type-id in an expression context. In
1304 such a situation, both "type (expr)" and "type (type)" are valid
1306 bool in_type_id_in_expr_p;
1308 /* TRUE if we are currently in a header file where declarations are
1309 implicitly extern "C". */
1310 bool implicit_extern_c;
1312 /* TRUE if strings in expressions should be translated to the execution
1314 bool translate_strings_p;
1316 /* If non-NULL, then we are parsing a construct where new type
1317 definitions are not permitted. The string stored here will be
1318 issued as an error message if a type is defined. */
1319 const char *type_definition_forbidden_message;
1321 /* A list of lists. The outer list is a stack, used for member
1322 functions of local classes. At each level there are two sub-list,
1323 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1324 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1325 TREE_VALUE's. The functions are chained in reverse declaration
1328 The TREE_PURPOSE sublist contains those functions with default
1329 arguments that need post processing, and the TREE_VALUE sublist
1330 contains those functions with definitions that need post
1333 These lists can only be processed once the outermost class being
1334 defined is complete. */
1335 tree unparsed_functions_queues;
1337 /* The number of classes whose definitions are currently in
1339 unsigned num_classes_being_defined;
1341 /* The number of template parameter lists that apply directly to the
1342 current declaration. */
1343 unsigned num_template_parameter_lists;
1346 /* The type of a function that parses some kind of expression. */
1347 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1351 /* Constructors and destructors. */
1353 static cp_parser *cp_parser_new
1356 /* Routines to parse various constructs.
1358 Those that return `tree' will return the error_mark_node (rather
1359 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1360 Sometimes, they will return an ordinary node if error-recovery was
1361 attempted, even though a parse error occurred. So, to check
1362 whether or not a parse error occurred, you should always use
1363 cp_parser_error_occurred. If the construct is optional (indicated
1364 either by an `_opt' in the name of the function that does the
1365 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1366 the construct is not present. */
1368 /* Lexical conventions [gram.lex] */
1370 static tree cp_parser_identifier
1372 static tree cp_parser_string_literal
1373 (cp_parser *, bool, bool);
1375 /* Basic concepts [gram.basic] */
1377 static bool cp_parser_translation_unit
1380 /* Expressions [gram.expr] */
1382 static tree cp_parser_primary_expression
1383 (cp_parser *, bool, cp_id_kind *, tree *);
1384 static tree cp_parser_id_expression
1385 (cp_parser *, bool, bool, bool *, bool);
1386 static tree cp_parser_unqualified_id
1387 (cp_parser *, bool, bool, bool);
1388 static tree cp_parser_nested_name_specifier_opt
1389 (cp_parser *, bool, bool, bool, bool);
1390 static tree cp_parser_nested_name_specifier
1391 (cp_parser *, bool, bool, bool, bool);
1392 static tree cp_parser_class_or_namespace_name
1393 (cp_parser *, bool, bool, bool, bool, bool);
1394 static tree cp_parser_postfix_expression
1395 (cp_parser *, bool, bool);
1396 static tree cp_parser_postfix_open_square_expression
1397 (cp_parser *, tree, bool);
1398 static tree cp_parser_postfix_dot_deref_expression
1399 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1400 static tree cp_parser_parenthesized_expression_list
1401 (cp_parser *, bool, bool, bool *);
1402 static void cp_parser_pseudo_destructor_name
1403 (cp_parser *, tree *, tree *);
1404 static tree cp_parser_unary_expression
1405 (cp_parser *, bool, bool);
1406 static enum tree_code cp_parser_unary_operator
1408 static tree cp_parser_new_expression
1410 static tree cp_parser_new_placement
1412 static tree cp_parser_new_type_id
1413 (cp_parser *, tree *);
1414 static cp_declarator *cp_parser_new_declarator_opt
1416 static cp_declarator *cp_parser_direct_new_declarator
1418 static tree cp_parser_new_initializer
1420 static tree cp_parser_delete_expression
1422 static tree cp_parser_cast_expression
1423 (cp_parser *, bool, bool);
1424 static tree cp_parser_binary_expression
1425 (cp_parser *, bool);
1426 static tree cp_parser_question_colon_clause
1427 (cp_parser *, tree);
1428 static tree cp_parser_assignment_expression
1429 (cp_parser *, bool);
1430 static enum tree_code cp_parser_assignment_operator_opt
1432 static tree cp_parser_expression
1433 (cp_parser *, bool);
1434 static tree cp_parser_constant_expression
1435 (cp_parser *, bool, bool *);
1436 static tree cp_parser_builtin_offsetof
1439 /* Statements [gram.stmt.stmt] */
1441 static void cp_parser_statement
1442 (cp_parser *, tree);
1443 static tree cp_parser_labeled_statement
1444 (cp_parser *, tree);
1445 static tree cp_parser_expression_statement
1446 (cp_parser *, tree);
1447 static tree cp_parser_compound_statement
1448 (cp_parser *, tree, bool);
1449 static void cp_parser_statement_seq_opt
1450 (cp_parser *, tree);
1451 static tree cp_parser_selection_statement
1453 static tree cp_parser_condition
1455 static tree cp_parser_iteration_statement
1457 static void cp_parser_for_init_statement
1459 static tree cp_parser_jump_statement
1461 static void cp_parser_declaration_statement
1464 static tree cp_parser_implicitly_scoped_statement
1466 static void cp_parser_already_scoped_statement
1469 /* Declarations [gram.dcl.dcl] */
1471 static void cp_parser_declaration_seq_opt
1473 static void cp_parser_declaration
1475 static void cp_parser_block_declaration
1476 (cp_parser *, bool);
1477 static void cp_parser_simple_declaration
1478 (cp_parser *, bool);
1479 static void cp_parser_decl_specifier_seq
1480 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1481 static tree cp_parser_storage_class_specifier_opt
1483 static tree cp_parser_function_specifier_opt
1484 (cp_parser *, cp_decl_specifier_seq *);
1485 static tree cp_parser_type_specifier
1486 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1488 static tree cp_parser_simple_type_specifier
1489 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1490 static tree cp_parser_type_name
1492 static tree cp_parser_elaborated_type_specifier
1493 (cp_parser *, bool, bool);
1494 static tree cp_parser_enum_specifier
1496 static void cp_parser_enumerator_list
1497 (cp_parser *, tree);
1498 static void cp_parser_enumerator_definition
1499 (cp_parser *, tree);
1500 static tree cp_parser_namespace_name
1502 static void cp_parser_namespace_definition
1504 static void cp_parser_namespace_body
1506 static tree cp_parser_qualified_namespace_specifier
1508 static void cp_parser_namespace_alias_definition
1510 static void cp_parser_using_declaration
1512 static void cp_parser_using_directive
1514 static void cp_parser_asm_definition
1516 static void cp_parser_linkage_specification
1519 /* Declarators [gram.dcl.decl] */
1521 static tree cp_parser_init_declarator
1522 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1523 static cp_declarator *cp_parser_declarator
1524 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1525 static cp_declarator *cp_parser_direct_declarator
1526 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1527 static enum tree_code cp_parser_ptr_operator
1528 (cp_parser *, tree *, cp_cv_quals *);
1529 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1531 static tree cp_parser_declarator_id
1533 static tree cp_parser_type_id
1535 static void cp_parser_type_specifier_seq
1536 (cp_parser *, bool, cp_decl_specifier_seq *);
1537 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1539 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1540 (cp_parser *, bool *);
1541 static cp_parameter_declarator *cp_parser_parameter_declaration
1542 (cp_parser *, bool, bool *);
1543 static void cp_parser_function_body
1545 static tree cp_parser_initializer
1546 (cp_parser *, bool *, bool *);
1547 static tree cp_parser_initializer_clause
1548 (cp_parser *, bool *);
1549 static tree cp_parser_initializer_list
1550 (cp_parser *, bool *);
1552 static bool cp_parser_ctor_initializer_opt_and_function_body
1555 /* Classes [gram.class] */
1557 static tree cp_parser_class_name
1558 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1559 static tree cp_parser_class_specifier
1561 static tree cp_parser_class_head
1562 (cp_parser *, bool *, tree *);
1563 static enum tag_types cp_parser_class_key
1565 static void cp_parser_member_specification_opt
1567 static void cp_parser_member_declaration
1569 static tree cp_parser_pure_specifier
1571 static tree cp_parser_constant_initializer
1574 /* Derived classes [gram.class.derived] */
1576 static tree cp_parser_base_clause
1578 static tree cp_parser_base_specifier
1581 /* Special member functions [gram.special] */
1583 static tree cp_parser_conversion_function_id
1585 static tree cp_parser_conversion_type_id
1587 static cp_declarator *cp_parser_conversion_declarator_opt
1589 static bool cp_parser_ctor_initializer_opt
1591 static void cp_parser_mem_initializer_list
1593 static tree cp_parser_mem_initializer
1595 static tree cp_parser_mem_initializer_id
1598 /* Overloading [gram.over] */
1600 static tree cp_parser_operator_function_id
1602 static tree cp_parser_operator
1605 /* Templates [gram.temp] */
1607 static void cp_parser_template_declaration
1608 (cp_parser *, bool);
1609 static tree cp_parser_template_parameter_list
1611 static tree cp_parser_template_parameter
1612 (cp_parser *, bool *);
1613 static tree cp_parser_type_parameter
1615 static tree cp_parser_template_id
1616 (cp_parser *, bool, bool, bool);
1617 static tree cp_parser_template_name
1618 (cp_parser *, bool, bool, bool, bool *);
1619 static tree cp_parser_template_argument_list
1621 static tree cp_parser_template_argument
1623 static void cp_parser_explicit_instantiation
1625 static void cp_parser_explicit_specialization
1628 /* Exception handling [gram.exception] */
1630 static tree cp_parser_try_block
1632 static bool cp_parser_function_try_block
1634 static void cp_parser_handler_seq
1636 static void cp_parser_handler
1638 static tree cp_parser_exception_declaration
1640 static tree cp_parser_throw_expression
1642 static tree cp_parser_exception_specification_opt
1644 static tree cp_parser_type_id_list
1647 /* GNU Extensions */
1649 static tree cp_parser_asm_specification_opt
1651 static tree cp_parser_asm_operand_list
1653 static tree cp_parser_asm_clobber_list
1655 static tree cp_parser_attributes_opt
1657 static tree cp_parser_attribute_list
1659 static bool cp_parser_extension_opt
1660 (cp_parser *, int *);
1661 static void cp_parser_label_declaration
1664 /* Objective-C++ Productions */
1666 static tree cp_parser_objc_message_receiver
1668 static tree cp_parser_objc_message_args
1670 static tree cp_parser_objc_message_expression
1672 static tree cp_parser_objc_encode_expression
1674 static tree cp_parser_objc_defs_expression
1676 static tree cp_parser_objc_protocol_expression
1678 static tree cp_parser_objc_selector_expression
1680 static tree cp_parser_objc_expression
1682 static bool cp_parser_objc_selector_p
1684 static tree cp_parser_objc_selector
1686 static tree cp_parser_objc_protocol_refs_opt
1688 static void cp_parser_objc_declaration
1690 static tree cp_parser_objc_statement
1693 /* Utility Routines */
1695 static tree cp_parser_lookup_name
1696 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1697 static tree cp_parser_lookup_name_simple
1698 (cp_parser *, tree);
1699 static tree cp_parser_maybe_treat_template_as_class
1701 static bool cp_parser_check_declarator_template_parameters
1702 (cp_parser *, cp_declarator *);
1703 static bool cp_parser_check_template_parameters
1704 (cp_parser *, unsigned);
1705 static tree cp_parser_simple_cast_expression
1707 static tree cp_parser_global_scope_opt
1708 (cp_parser *, bool);
1709 static bool cp_parser_constructor_declarator_p
1710 (cp_parser *, bool);
1711 static tree cp_parser_function_definition_from_specifiers_and_declarator
1712 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1713 static tree cp_parser_function_definition_after_declarator
1714 (cp_parser *, bool);
1715 static void cp_parser_template_declaration_after_export
1716 (cp_parser *, bool);
1717 static tree cp_parser_single_declaration
1718 (cp_parser *, bool, bool *);
1719 static tree cp_parser_functional_cast
1720 (cp_parser *, tree);
1721 static tree cp_parser_save_member_function_body
1722 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1723 static tree cp_parser_enclosed_template_argument_list
1725 static void cp_parser_save_default_args
1726 (cp_parser *, tree);
1727 static void cp_parser_late_parsing_for_member
1728 (cp_parser *, tree);
1729 static void cp_parser_late_parsing_default_args
1730 (cp_parser *, tree);
1731 static tree cp_parser_sizeof_operand
1732 (cp_parser *, enum rid);
1733 static bool cp_parser_declares_only_class_p
1735 static void cp_parser_set_storage_class
1736 (cp_decl_specifier_seq *, cp_storage_class);
1737 static void cp_parser_set_decl_spec_type
1738 (cp_decl_specifier_seq *, tree, bool);
1739 static bool cp_parser_friend_p
1740 (const cp_decl_specifier_seq *);
1741 static cp_token *cp_parser_require
1742 (cp_parser *, enum cpp_ttype, const char *);
1743 static cp_token *cp_parser_require_keyword
1744 (cp_parser *, enum rid, const char *);
1745 static bool cp_parser_token_starts_function_definition_p
1747 static bool cp_parser_next_token_starts_class_definition_p
1749 static bool cp_parser_next_token_ends_template_argument_p
1751 static bool cp_parser_nth_token_starts_template_argument_list_p
1752 (cp_parser *, size_t);
1753 static enum tag_types cp_parser_token_is_class_key
1755 static void cp_parser_check_class_key
1756 (enum tag_types, tree type);
1757 static void cp_parser_check_access_in_redeclaration
1759 static bool cp_parser_optional_template_keyword
1761 static void cp_parser_pre_parsed_nested_name_specifier
1763 static void cp_parser_cache_group
1764 (cp_parser *, enum cpp_ttype, unsigned);
1765 static void cp_parser_parse_tentatively
1767 static void cp_parser_commit_to_tentative_parse
1769 static void cp_parser_abort_tentative_parse
1771 static bool cp_parser_parse_definitely
1773 static inline bool cp_parser_parsing_tentatively
1775 static bool cp_parser_uncommitted_to_tentative_parse_p
1777 static void cp_parser_error
1778 (cp_parser *, const char *);
1779 static void cp_parser_name_lookup_error
1780 (cp_parser *, tree, tree, const char *);
1781 static bool cp_parser_simulate_error
1783 static void cp_parser_check_type_definition
1785 static void cp_parser_check_for_definition_in_return_type
1786 (cp_declarator *, tree);
1787 static void cp_parser_check_for_invalid_template_id
1788 (cp_parser *, tree);
1789 static bool cp_parser_non_integral_constant_expression
1790 (cp_parser *, const char *);
1791 static void cp_parser_diagnose_invalid_type_name
1792 (cp_parser *, tree, tree);
1793 static bool cp_parser_parse_and_diagnose_invalid_type_name
1795 static int cp_parser_skip_to_closing_parenthesis
1796 (cp_parser *, bool, bool, bool);
1797 static void cp_parser_skip_to_end_of_statement
1799 static void cp_parser_consume_semicolon_at_end_of_statement
1801 static void cp_parser_skip_to_end_of_block_or_statement
1803 static void cp_parser_skip_to_closing_brace
1805 static void cp_parser_skip_until_found
1806 (cp_parser *, enum cpp_ttype, const char *);
1807 static bool cp_parser_error_occurred
1809 static bool cp_parser_allow_gnu_extensions_p
1811 static bool cp_parser_is_string_literal
1813 static bool cp_parser_is_keyword
1814 (cp_token *, enum rid);
1815 static tree cp_parser_make_typename_type
1816 (cp_parser *, tree, tree);
1818 /* Returns nonzero if we are parsing tentatively. */
1821 cp_parser_parsing_tentatively (cp_parser* parser)
1823 return parser->context->next != NULL;
1826 /* Returns nonzero if TOKEN is a string literal. */
1829 cp_parser_is_string_literal (cp_token* token)
1831 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1834 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1837 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1839 return token->keyword == keyword;
1842 /* A minimum or maximum operator has been seen. As these are
1843 deprecated, issue a warning. */
1846 cp_parser_warn_min_max (void)
1848 if (warn_deprecated && !in_system_header)
1849 warning (0, "minimum/maximum operators are deprecated");
1852 /* If not parsing tentatively, issue a diagnostic of the form
1853 FILE:LINE: MESSAGE before TOKEN
1854 where TOKEN is the next token in the input stream. MESSAGE
1855 (specified by the caller) is usually of the form "expected
1859 cp_parser_error (cp_parser* parser, const char* message)
1861 if (!cp_parser_simulate_error (parser))
1863 cp_token *token = cp_lexer_peek_token (parser->lexer);
1864 /* This diagnostic makes more sense if it is tagged to the line
1865 of the token we just peeked at. */
1866 cp_lexer_set_source_position_from_token (token);
1867 if (token->type == CPP_PRAGMA)
1869 error ("%<#pragma%> is not allowed here");
1870 cp_lexer_purge_token (parser->lexer);
1873 c_parse_error (message,
1874 /* Because c_parser_error does not understand
1875 CPP_KEYWORD, keywords are treated like
1877 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1882 /* Issue an error about name-lookup failing. NAME is the
1883 IDENTIFIER_NODE DECL is the result of
1884 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1885 the thing that we hoped to find. */
1888 cp_parser_name_lookup_error (cp_parser* parser,
1891 const char* desired)
1893 /* If name lookup completely failed, tell the user that NAME was not
1895 if (decl == error_mark_node)
1897 if (parser->scope && parser->scope != global_namespace)
1898 error ("%<%D::%D%> has not been declared",
1899 parser->scope, name);
1900 else if (parser->scope == global_namespace)
1901 error ("%<::%D%> has not been declared", name);
1902 else if (parser->object_scope
1903 && !CLASS_TYPE_P (parser->object_scope))
1904 error ("request for member %qD in non-class type %qT",
1905 name, parser->object_scope);
1906 else if (parser->object_scope)
1907 error ("%<%T::%D%> has not been declared",
1908 parser->object_scope, name);
1910 error ("%qD has not been declared", name);
1912 else if (parser->scope && parser->scope != global_namespace)
1913 error ("%<%D::%D%> %s", parser->scope, name, desired);
1914 else if (parser->scope == global_namespace)
1915 error ("%<::%D%> %s", name, desired);
1917 error ("%qD %s", name, desired);
1920 /* If we are parsing tentatively, remember that an error has occurred
1921 during this tentative parse. Returns true if the error was
1922 simulated; false if a message should be issued by the caller. */
1925 cp_parser_simulate_error (cp_parser* parser)
1927 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1929 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1935 /* This function is called when a type is defined. If type
1936 definitions are forbidden at this point, an error message is
1940 cp_parser_check_type_definition (cp_parser* parser)
1942 /* If types are forbidden here, issue a message. */
1943 if (parser->type_definition_forbidden_message)
1944 /* Use `%s' to print the string in case there are any escape
1945 characters in the message. */
1946 error ("%s", parser->type_definition_forbidden_message);
1949 /* This function is called when the DECLARATOR is processed. The TYPE
1950 was a type defined in the decl-specifiers. If it is invalid to
1951 define a type in the decl-specifiers for DECLARATOR, an error is
1955 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1958 /* [dcl.fct] forbids type definitions in return types.
1959 Unfortunately, it's not easy to know whether or not we are
1960 processing a return type until after the fact. */
1962 && (declarator->kind == cdk_pointer
1963 || declarator->kind == cdk_reference
1964 || declarator->kind == cdk_ptrmem))
1965 declarator = declarator->declarator;
1967 && declarator->kind == cdk_function)
1969 error ("new types may not be defined in a return type");
1970 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1975 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1976 "<" in any valid C++ program. If the next token is indeed "<",
1977 issue a message warning the user about what appears to be an
1978 invalid attempt to form a template-id. */
1981 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1984 cp_token_position start = 0;
1986 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1989 error ("%qT is not a template", type);
1990 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1991 error ("%qE is not a template", type);
1993 error ("invalid template-id");
1994 /* Remember the location of the invalid "<". */
1995 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1996 start = cp_lexer_token_position (parser->lexer, true);
1997 /* Consume the "<". */
1998 cp_lexer_consume_token (parser->lexer);
1999 /* Parse the template arguments. */
2000 cp_parser_enclosed_template_argument_list (parser);
2001 /* Permanently remove the invalid template arguments so that
2002 this error message is not issued again. */
2004 cp_lexer_purge_tokens_after (parser->lexer, start);
2008 /* If parsing an integral constant-expression, issue an error message
2009 about the fact that THING appeared and return true. Otherwise,
2010 return false. In either case, set
2011 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2014 cp_parser_non_integral_constant_expression (cp_parser *parser,
2017 parser->non_integral_constant_expression_p = true;
2018 if (parser->integral_constant_expression_p)
2020 if (!parser->allow_non_integral_constant_expression_p)
2022 error ("%s cannot appear in a constant-expression", thing);
2029 /* Emit a diagnostic for an invalid type name. SCOPE is the
2030 qualifying scope (or NULL, if none) for ID. This function commits
2031 to the current active tentative parse, if any. (Otherwise, the
2032 problematic construct might be encountered again later, resulting
2033 in duplicate error messages.) */
2036 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2038 tree decl, old_scope;
2039 /* Try to lookup the identifier. */
2040 old_scope = parser->scope;
2041 parser->scope = scope;
2042 decl = cp_parser_lookup_name_simple (parser, id);
2043 parser->scope = old_scope;
2044 /* If the lookup found a template-name, it means that the user forgot
2045 to specify an argument list. Emit an useful error message. */
2046 if (TREE_CODE (decl) == TEMPLATE_DECL)
2047 error ("invalid use of template-name %qE without an argument list",
2049 else if (!parser->scope || parser->scope == error_mark_node)
2051 /* Issue an error message. */
2052 error ("%qE does not name a type", id);
2053 /* If we're in a template class, it's possible that the user was
2054 referring to a type from a base class. For example:
2056 template <typename T> struct A { typedef T X; };
2057 template <typename T> struct B : public A<T> { X x; };
2059 The user should have said "typename A<T>::X". */
2060 if (processing_template_decl && current_class_type
2061 && TYPE_BINFO (current_class_type))
2065 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2069 tree base_type = BINFO_TYPE (b);
2070 if (CLASS_TYPE_P (base_type)
2071 && dependent_type_p (base_type))
2074 /* Go from a particular instantiation of the
2075 template (which will have an empty TYPE_FIELDs),
2076 to the main version. */
2077 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2078 for (field = TYPE_FIELDS (base_type);
2080 field = TREE_CHAIN (field))
2081 if (TREE_CODE (field) == TYPE_DECL
2082 && DECL_NAME (field) == id)
2084 inform ("(perhaps %<typename %T::%E%> was intended)",
2085 BINFO_TYPE (b), id);
2094 /* Here we diagnose qualified-ids where the scope is actually correct,
2095 but the identifier does not resolve to a valid type name. */
2098 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2099 error ("%qE in namespace %qE does not name a type",
2101 else if (TYPE_P (parser->scope))
2102 error ("%qE in class %qT does not name a type", id, parser->scope);
2106 cp_parser_commit_to_tentative_parse (parser);
2109 /* Check for a common situation where a type-name should be present,
2110 but is not, and issue a sensible error message. Returns true if an
2111 invalid type-name was detected.
2113 The situation handled by this function are variable declarations of the
2114 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2115 Usually, `ID' should name a type, but if we got here it means that it
2116 does not. We try to emit the best possible error message depending on
2117 how exactly the id-expression looks like.
2121 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2125 cp_parser_parse_tentatively (parser);
2126 id = cp_parser_id_expression (parser,
2127 /*template_keyword_p=*/false,
2128 /*check_dependency_p=*/true,
2129 /*template_p=*/NULL,
2130 /*declarator_p=*/true);
2131 /* After the id-expression, there should be a plain identifier,
2132 otherwise this is not a simple variable declaration. Also, if
2133 the scope is dependent, we cannot do much. */
2134 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2135 || (parser->scope && TYPE_P (parser->scope)
2136 && dependent_type_p (parser->scope)))
2138 cp_parser_abort_tentative_parse (parser);
2141 if (!cp_parser_parse_definitely (parser)
2142 || TREE_CODE (id) != IDENTIFIER_NODE)
2145 /* Emit a diagnostic for the invalid type. */
2146 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2147 /* Skip to the end of the declaration; there's no point in
2148 trying to process it. */
2149 cp_parser_skip_to_end_of_block_or_statement (parser);
2153 /* Consume tokens up to, and including, the next non-nested closing `)'.
2154 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2155 are doing error recovery. Returns -1 if OR_COMMA is true and we
2156 found an unnested comma. */
2159 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2164 unsigned paren_depth = 0;
2165 unsigned brace_depth = 0;
2168 if (recovering && !or_comma
2169 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2176 /* If we've run out of tokens, then there is no closing `)'. */
2177 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2183 token = cp_lexer_peek_token (parser->lexer);
2185 /* This matches the processing in skip_to_end_of_statement. */
2186 if (token->type == CPP_SEMICOLON && !brace_depth)
2191 if (token->type == CPP_OPEN_BRACE)
2193 if (token->type == CPP_CLOSE_BRACE)
2201 if (recovering && or_comma && token->type == CPP_COMMA
2202 && !brace_depth && !paren_depth)
2210 /* If it is an `(', we have entered another level of nesting. */
2211 if (token->type == CPP_OPEN_PAREN)
2213 /* If it is a `)', then we might be done. */
2214 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2217 cp_lexer_consume_token (parser->lexer);
2225 /* Consume the token. */
2226 cp_lexer_consume_token (parser->lexer);
2232 /* Consume tokens until we reach the end of the current statement.
2233 Normally, that will be just before consuming a `;'. However, if a
2234 non-nested `}' comes first, then we stop before consuming that. */
2237 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2239 unsigned nesting_depth = 0;
2245 /* Peek at the next token. */
2246 token = cp_lexer_peek_token (parser->lexer);
2247 /* If we've run out of tokens, stop. */
2248 if (token->type == CPP_EOF)
2250 /* If the next token is a `;', we have reached the end of the
2252 if (token->type == CPP_SEMICOLON && !nesting_depth)
2254 /* If the next token is a non-nested `}', then we have reached
2255 the end of the current block. */
2256 if (token->type == CPP_CLOSE_BRACE)
2258 /* If this is a non-nested `}', stop before consuming it.
2259 That way, when confronted with something like:
2263 we stop before consuming the closing `}', even though we
2264 have not yet reached a `;'. */
2265 if (nesting_depth == 0)
2267 /* If it is the closing `}' for a block that we have
2268 scanned, stop -- but only after consuming the token.
2274 we will stop after the body of the erroneously declared
2275 function, but before consuming the following `typedef'
2277 if (--nesting_depth == 0)
2279 cp_lexer_consume_token (parser->lexer);
2283 /* If it the next token is a `{', then we are entering a new
2284 block. Consume the entire block. */
2285 else if (token->type == CPP_OPEN_BRACE)
2287 /* Consume the token. */
2288 cp_lexer_consume_token (parser->lexer);
2292 /* This function is called at the end of a statement or declaration.
2293 If the next token is a semicolon, it is consumed; otherwise, error
2294 recovery is attempted. */
2297 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2299 /* Look for the trailing `;'. */
2300 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2302 /* If there is additional (erroneous) input, skip to the end of
2304 cp_parser_skip_to_end_of_statement (parser);
2305 /* If the next token is now a `;', consume it. */
2306 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2307 cp_lexer_consume_token (parser->lexer);
2311 /* Skip tokens until we have consumed an entire block, or until we
2312 have consumed a non-nested `;'. */
2315 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2317 int nesting_depth = 0;
2319 while (nesting_depth >= 0)
2321 cp_token *token = cp_lexer_peek_token (parser->lexer);
2323 if (token->type == CPP_EOF)
2326 switch (token->type)
2329 /* If we've run out of tokens, stop. */
2334 /* Stop if this is an unnested ';'. */
2339 case CPP_CLOSE_BRACE:
2340 /* Stop if this is an unnested '}', or closes the outermost
2347 case CPP_OPEN_BRACE:
2356 /* Consume the token. */
2357 cp_lexer_consume_token (parser->lexer);
2362 /* Skip tokens until a non-nested closing curly brace is the next
2366 cp_parser_skip_to_closing_brace (cp_parser *parser)
2368 unsigned nesting_depth = 0;
2374 /* Peek at the next token. */
2375 token = cp_lexer_peek_token (parser->lexer);
2376 /* If we've run out of tokens, stop. */
2377 if (token->type == CPP_EOF)
2379 /* If the next token is a non-nested `}', then we have reached
2380 the end of the current block. */
2381 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2383 /* If it the next token is a `{', then we are entering a new
2384 block. Consume the entire block. */
2385 else if (token->type == CPP_OPEN_BRACE)
2387 /* Consume the token. */
2388 cp_lexer_consume_token (parser->lexer);
2392 /* This is a simple wrapper around make_typename_type. When the id is
2393 an unresolved identifier node, we can provide a superior diagnostic
2394 using cp_parser_diagnose_invalid_type_name. */
2397 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2400 if (TREE_CODE (id) == IDENTIFIER_NODE)
2402 result = make_typename_type (scope, id, typename_type,
2404 if (result == error_mark_node)
2405 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2408 return make_typename_type (scope, id, typename_type, tf_error);
2412 /* Create a new C++ parser. */
2415 cp_parser_new (void)
2421 /* cp_lexer_new_main is called before calling ggc_alloc because
2422 cp_lexer_new_main might load a PCH file. */
2423 lexer = cp_lexer_new_main ();
2425 /* Initialize the binops_by_token so that we can get the tree
2426 directly from the token. */
2427 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2428 binops_by_token[binops[i].token_type] = binops[i];
2430 parser = GGC_CNEW (cp_parser);
2431 parser->lexer = lexer;
2432 parser->context = cp_parser_context_new (NULL);
2434 /* For now, we always accept GNU extensions. */
2435 parser->allow_gnu_extensions_p = 1;
2437 /* The `>' token is a greater-than operator, not the end of a
2439 parser->greater_than_is_operator_p = true;
2441 parser->default_arg_ok_p = true;
2443 /* We are not parsing a constant-expression. */
2444 parser->integral_constant_expression_p = false;
2445 parser->allow_non_integral_constant_expression_p = false;
2446 parser->non_integral_constant_expression_p = false;
2448 /* Local variable names are not forbidden. */
2449 parser->local_variables_forbidden_p = false;
2451 /* We are not processing an `extern "C"' declaration. */
2452 parser->in_unbraced_linkage_specification_p = false;
2454 /* We are not processing a declarator. */
2455 parser->in_declarator_p = false;
2457 /* We are not processing a template-argument-list. */
2458 parser->in_template_argument_list_p = false;
2460 /* We are not in an iteration statement. */
2461 parser->in_iteration_statement_p = false;
2463 /* We are not in a switch statement. */
2464 parser->in_switch_statement_p = false;
2466 /* We are not parsing a type-id inside an expression. */
2467 parser->in_type_id_in_expr_p = false;
2469 /* Declarations aren't implicitly extern "C". */
2470 parser->implicit_extern_c = false;
2472 /* String literals should be translated to the execution character set. */
2473 parser->translate_strings_p = true;
2475 /* The unparsed function queue is empty. */
2476 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2478 /* There are no classes being defined. */
2479 parser->num_classes_being_defined = 0;
2481 /* No template parameters apply. */
2482 parser->num_template_parameter_lists = 0;
2487 /* Create a cp_lexer structure which will emit the tokens in CACHE
2488 and push it onto the parser's lexer stack. This is used for delayed
2489 parsing of in-class method bodies and default arguments, and should
2490 not be confused with tentative parsing. */
2492 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2494 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2495 lexer->next = parser->lexer;
2496 parser->lexer = lexer;
2498 /* Move the current source position to that of the first token in the
2500 cp_lexer_set_source_position_from_token (lexer->next_token);
2503 /* Pop the top lexer off the parser stack. This is never used for the
2504 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2506 cp_parser_pop_lexer (cp_parser *parser)
2508 cp_lexer *lexer = parser->lexer;
2509 parser->lexer = lexer->next;
2510 cp_lexer_destroy (lexer);
2512 /* Put the current source position back where it was before this
2513 lexer was pushed. */
2514 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2517 /* Lexical conventions [gram.lex] */
2519 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2523 cp_parser_identifier (cp_parser* parser)
2527 /* Look for the identifier. */
2528 token = cp_parser_require (parser, CPP_NAME, "identifier");
2529 /* Return the value. */
2530 return token ? token->value : error_mark_node;
2533 /* Parse a sequence of adjacent string constants. Returns a
2534 TREE_STRING representing the combined, nul-terminated string
2535 constant. If TRANSLATE is true, translate the string to the
2536 execution character set. If WIDE_OK is true, a wide string is
2539 C++98 [lex.string] says that if a narrow string literal token is
2540 adjacent to a wide string literal token, the behavior is undefined.
2541 However, C99 6.4.5p4 says that this results in a wide string literal.
2542 We follow C99 here, for consistency with the C front end.
2544 This code is largely lifted from lex_string() in c-lex.c.
2546 FUTURE: ObjC++ will need to handle @-strings here. */
2548 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2553 struct obstack str_ob;
2554 cpp_string str, istr, *strs;
2557 tok = cp_lexer_peek_token (parser->lexer);
2558 if (!cp_parser_is_string_literal (tok))
2560 cp_parser_error (parser, "expected string-literal");
2561 return error_mark_node;
2564 /* Try to avoid the overhead of creating and destroying an obstack
2565 for the common case of just one string. */
2566 if (!cp_parser_is_string_literal
2567 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2569 cp_lexer_consume_token (parser->lexer);
2571 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2572 str.len = TREE_STRING_LENGTH (tok->value);
2574 if (tok->type == CPP_WSTRING)
2581 gcc_obstack_init (&str_ob);
2586 cp_lexer_consume_token (parser->lexer);
2588 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2589 str.len = TREE_STRING_LENGTH (tok->value);
2590 if (tok->type == CPP_WSTRING)
2593 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2595 tok = cp_lexer_peek_token (parser->lexer);
2597 while (cp_parser_is_string_literal (tok));
2599 strs = (cpp_string *) obstack_finish (&str_ob);
2602 if (wide && !wide_ok)
2604 cp_parser_error (parser, "a wide string is invalid in this context");
2608 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2609 (parse_in, strs, count, &istr, wide))
2611 value = build_string (istr.len, (char *)istr.text);
2612 free ((void *)istr.text);
2614 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2615 value = fix_string_type (value);
2618 /* cpp_interpret_string has issued an error. */
2619 value = error_mark_node;
2622 obstack_free (&str_ob, 0);
2628 /* Basic concepts [gram.basic] */
2630 /* Parse a translation-unit.
2633 declaration-seq [opt]
2635 Returns TRUE if all went well. */
2638 cp_parser_translation_unit (cp_parser* parser)
2640 /* The address of the first non-permanent object on the declarator
2642 static void *declarator_obstack_base;
2646 /* Create the declarator obstack, if necessary. */
2647 if (!cp_error_declarator)
2649 gcc_obstack_init (&declarator_obstack);
2650 /* Create the error declarator. */
2651 cp_error_declarator = make_declarator (cdk_error);
2652 /* Create the empty parameter list. */
2653 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2654 /* Remember where the base of the declarator obstack lies. */
2655 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2660 cp_parser_declaration_seq_opt (parser);
2662 /* If there are no tokens left then all went well. */
2663 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2665 /* Get rid of the token array; we don't need it any more. */
2666 cp_lexer_destroy (parser->lexer);
2667 parser->lexer = NULL;
2669 /* This file might have been a context that's implicitly extern
2670 "C". If so, pop the lang context. (Only relevant for PCH.) */
2671 if (parser->implicit_extern_c)
2673 pop_lang_context ();
2674 parser->implicit_extern_c = false;
2678 finish_translation_unit ();
2685 cp_parser_error (parser, "expected declaration");
2691 /* Make sure the declarator obstack was fully cleaned up. */
2692 gcc_assert (obstack_next_free (&declarator_obstack)
2693 == declarator_obstack_base);
2695 /* All went well. */
2699 /* Expressions [gram.expr] */
2701 /* Parse a primary-expression.
2712 ( compound-statement )
2713 __builtin_va_arg ( assignment-expression , type-id )
2715 Objective-C++ Extension:
2723 CAST_P is true if this primary expression is the target of a cast.
2725 Returns a representation of the expression.
2727 *IDK indicates what kind of id-expression (if any) was present.
2729 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2730 used as the operand of a pointer-to-member. In that case,
2731 *QUALIFYING_CLASS gives the class that is used as the qualifying
2732 class in the pointer-to-member. */
2735 cp_parser_primary_expression (cp_parser *parser,
2738 tree *qualifying_class)
2742 /* Assume the primary expression is not an id-expression. */
2743 *idk = CP_ID_KIND_NONE;
2744 /* And that it cannot be used as pointer-to-member. */
2745 *qualifying_class = NULL_TREE;
2747 /* Peek at the next token. */
2748 token = cp_lexer_peek_token (parser->lexer);
2749 switch (token->type)
2760 token = cp_lexer_consume_token (parser->lexer);
2761 /* Floating-point literals are only allowed in an integral
2762 constant expression if they are cast to an integral or
2763 enumeration type. */
2764 if (TREE_CODE (token->value) == REAL_CST
2765 && parser->integral_constant_expression_p
2768 /* CAST_P will be set even in invalid code like "int(2.7 +
2769 ...)". Therefore, we have to check that the next token
2770 is sure to end the cast. */
2773 cp_token *next_token;
2775 next_token = cp_lexer_peek_token (parser->lexer);
2776 if (/* The comma at the end of an
2777 enumerator-definition. */
2778 next_token->type != CPP_COMMA
2779 /* The curly brace at the end of an enum-specifier. */
2780 && next_token->type != CPP_CLOSE_BRACE
2781 /* The end of a statement. */
2782 && next_token->type != CPP_SEMICOLON
2783 /* The end of the cast-expression. */
2784 && next_token->type != CPP_CLOSE_PAREN
2785 /* The end of an array bound. */
2786 && next_token->type != CPP_CLOSE_SQUARE)
2790 /* If we are within a cast, then the constraint that the
2791 cast is to an integral or enumeration type will be
2792 checked at that point. If we are not within a cast, then
2793 this code is invalid. */
2795 cp_parser_non_integral_constant_expression
2796 (parser, "floating-point literal");
2798 return token->value;
2802 /* ??? Should wide strings be allowed when parser->translate_strings_p
2803 is false (i.e. in attributes)? If not, we can kill the third
2804 argument to cp_parser_string_literal. */
2805 return cp_parser_string_literal (parser,
2806 parser->translate_strings_p,
2809 case CPP_OPEN_PAREN:
2812 bool saved_greater_than_is_operator_p;
2814 /* Consume the `('. */
2815 cp_lexer_consume_token (parser->lexer);
2816 /* Within a parenthesized expression, a `>' token is always
2817 the greater-than operator. */
2818 saved_greater_than_is_operator_p
2819 = parser->greater_than_is_operator_p;
2820 parser->greater_than_is_operator_p = true;
2821 /* If we see `( { ' then we are looking at the beginning of
2822 a GNU statement-expression. */
2823 if (cp_parser_allow_gnu_extensions_p (parser)
2824 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2826 /* Statement-expressions are not allowed by the standard. */
2828 pedwarn ("ISO C++ forbids braced-groups within expressions");
2830 /* And they're not allowed outside of a function-body; you
2831 cannot, for example, write:
2833 int i = ({ int j = 3; j + 1; });
2835 at class or namespace scope. */
2836 if (!at_function_scope_p ())
2837 error ("statement-expressions are allowed only inside functions");
2838 /* Start the statement-expression. */
2839 expr = begin_stmt_expr ();
2840 /* Parse the compound-statement. */
2841 cp_parser_compound_statement (parser, expr, false);
2843 expr = finish_stmt_expr (expr, false);
2847 /* Parse the parenthesized expression. */
2848 expr = cp_parser_expression (parser, cast_p);
2849 /* Let the front end know that this expression was
2850 enclosed in parentheses. This matters in case, for
2851 example, the expression is of the form `A::B', since
2852 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2854 finish_parenthesized_expr (expr);
2856 /* The `>' token might be the end of a template-id or
2857 template-parameter-list now. */
2858 parser->greater_than_is_operator_p
2859 = saved_greater_than_is_operator_p;
2860 /* Consume the `)'. */
2861 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2862 cp_parser_skip_to_end_of_statement (parser);
2868 switch (token->keyword)
2870 /* These two are the boolean literals. */
2872 cp_lexer_consume_token (parser->lexer);
2873 return boolean_true_node;
2875 cp_lexer_consume_token (parser->lexer);
2876 return boolean_false_node;
2878 /* The `__null' literal. */
2880 cp_lexer_consume_token (parser->lexer);
2883 /* Recognize the `this' keyword. */
2885 cp_lexer_consume_token (parser->lexer);
2886 if (parser->local_variables_forbidden_p)
2888 error ("%<this%> may not be used in this context");
2889 return error_mark_node;
2891 /* Pointers cannot appear in constant-expressions. */
2892 if (cp_parser_non_integral_constant_expression (parser,
2894 return error_mark_node;
2895 return finish_this_expr ();
2897 /* The `operator' keyword can be the beginning of an
2902 case RID_FUNCTION_NAME:
2903 case RID_PRETTY_FUNCTION_NAME:
2904 case RID_C99_FUNCTION_NAME:
2905 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2906 __func__ are the names of variables -- but they are
2907 treated specially. Therefore, they are handled here,
2908 rather than relying on the generic id-expression logic
2909 below. Grammatically, these names are id-expressions.
2911 Consume the token. */
2912 token = cp_lexer_consume_token (parser->lexer);
2913 /* Look up the name. */
2914 return finish_fname (token->value);
2921 /* The `__builtin_va_arg' construct is used to handle
2922 `va_arg'. Consume the `__builtin_va_arg' token. */
2923 cp_lexer_consume_token (parser->lexer);
2924 /* Look for the opening `('. */
2925 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2926 /* Now, parse the assignment-expression. */
2927 expression = cp_parser_assignment_expression (parser,
2929 /* Look for the `,'. */
2930 cp_parser_require (parser, CPP_COMMA, "`,'");
2931 /* Parse the type-id. */
2932 type = cp_parser_type_id (parser);
2933 /* Look for the closing `)'. */
2934 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2935 /* Using `va_arg' in a constant-expression is not
2937 if (cp_parser_non_integral_constant_expression (parser,
2939 return error_mark_node;
2940 return build_x_va_arg (expression, type);
2944 return cp_parser_builtin_offsetof (parser);
2946 /* Objective-C++ expressions. */
2948 case RID_AT_PROTOCOL:
2949 case RID_AT_SELECTOR:
2950 return cp_parser_objc_expression (parser);
2953 cp_parser_error (parser, "expected primary-expression");
2954 return error_mark_node;
2957 /* An id-expression can start with either an identifier, a
2958 `::' as the beginning of a qualified-id, or the "operator"
2962 case CPP_TEMPLATE_ID:
2963 case CPP_NESTED_NAME_SPECIFIER:
2967 const char *error_msg;
2970 /* Parse the id-expression. */
2972 = cp_parser_id_expression (parser,
2973 /*template_keyword_p=*/false,
2974 /*check_dependency_p=*/true,
2975 /*template_p=*/NULL,
2976 /*declarator_p=*/false);
2977 if (id_expression == error_mark_node)
2978 return error_mark_node;
2979 /* If we have a template-id, then no further lookup is
2980 required. If the template-id was for a template-class, we
2981 will sometimes have a TYPE_DECL at this point. */
2982 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2983 || TREE_CODE (id_expression) == TYPE_DECL)
2984 decl = id_expression;
2985 /* Look up the name. */
2990 decl = cp_parser_lookup_name (parser, id_expression,
2992 /*is_template=*/false,
2993 /*is_namespace=*/false,
2994 /*check_dependency=*/true,
2996 /* If the lookup was ambiguous, an error will already have
2999 return error_mark_node;
3001 /* In Objective-C++, an instance variable (ivar) may be preferred
3002 to whatever cp_parser_lookup_name() found. */
3003 decl = objc_lookup_ivar (decl, id_expression);
3005 /* If name lookup gives us a SCOPE_REF, then the
3006 qualifying scope was dependent. Just propagate the
3008 if (TREE_CODE (decl) == SCOPE_REF)
3010 if (TYPE_P (TREE_OPERAND (decl, 0)))
3011 *qualifying_class = TREE_OPERAND (decl, 0);
3014 /* Check to see if DECL is a local variable in a context
3015 where that is forbidden. */
3016 if (parser->local_variables_forbidden_p
3017 && local_variable_p (decl))
3019 /* It might be that we only found DECL because we are
3020 trying to be generous with pre-ISO scoping rules.
3021 For example, consider:
3025 for (int i = 0; i < 10; ++i) {}
3026 extern void f(int j = i);
3029 Here, name look up will originally find the out
3030 of scope `i'. We need to issue a warning message,
3031 but then use the global `i'. */
3032 decl = check_for_out_of_scope_variable (decl);
3033 if (local_variable_p (decl))
3035 error ("local variable %qD may not appear in this context",
3037 return error_mark_node;
3042 decl = finish_id_expression (id_expression, decl, parser->scope,
3043 idk, qualifying_class,
3044 parser->integral_constant_expression_p,
3045 parser->allow_non_integral_constant_expression_p,
3046 &parser->non_integral_constant_expression_p,
3049 cp_parser_error (parser, error_msg);
3053 /* Anything else is an error. */
3055 /* ...unless we have an Objective-C++ message or string literal, that is. */
3056 if (c_dialect_objc ()
3057 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3058 return cp_parser_objc_expression (parser);
3060 cp_parser_error (parser, "expected primary-expression");
3061 return error_mark_node;
3065 /* Parse an id-expression.
3072 :: [opt] nested-name-specifier template [opt] unqualified-id
3074 :: operator-function-id
3077 Return a representation of the unqualified portion of the
3078 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3079 a `::' or nested-name-specifier.
3081 Often, if the id-expression was a qualified-id, the caller will
3082 want to make a SCOPE_REF to represent the qualified-id. This
3083 function does not do this in order to avoid wastefully creating
3084 SCOPE_REFs when they are not required.
3086 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3089 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3090 uninstantiated templates.
3092 If *TEMPLATE_P is non-NULL, it is set to true iff the
3093 `template' keyword is used to explicitly indicate that the entity
3094 named is a template.
3096 If DECLARATOR_P is true, the id-expression is appearing as part of
3097 a declarator, rather than as part of an expression. */
3100 cp_parser_id_expression (cp_parser *parser,
3101 bool template_keyword_p,
3102 bool check_dependency_p,
3106 bool global_scope_p;
3107 bool nested_name_specifier_p;
3109 /* Assume the `template' keyword was not used. */
3111 *template_p = false;
3113 /* Look for the optional `::' operator. */
3115 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3117 /* Look for the optional nested-name-specifier. */
3118 nested_name_specifier_p
3119 = (cp_parser_nested_name_specifier_opt (parser,
3120 /*typename_keyword_p=*/false,
3125 /* If there is a nested-name-specifier, then we are looking at
3126 the first qualified-id production. */
3127 if (nested_name_specifier_p)
3130 tree saved_object_scope;
3131 tree saved_qualifying_scope;
3132 tree unqualified_id;
3135 /* See if the next token is the `template' keyword. */
3137 template_p = &is_template;
3138 *template_p = cp_parser_optional_template_keyword (parser);
3139 /* Name lookup we do during the processing of the
3140 unqualified-id might obliterate SCOPE. */
3141 saved_scope = parser->scope;
3142 saved_object_scope = parser->object_scope;
3143 saved_qualifying_scope = parser->qualifying_scope;
3144 /* Process the final unqualified-id. */
3145 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3148 /* Restore the SAVED_SCOPE for our caller. */
3149 parser->scope = saved_scope;
3150 parser->object_scope = saved_object_scope;
3151 parser->qualifying_scope = saved_qualifying_scope;
3153 return unqualified_id;
3155 /* Otherwise, if we are in global scope, then we are looking at one
3156 of the other qualified-id productions. */
3157 else if (global_scope_p)
3162 /* Peek at the next token. */
3163 token = cp_lexer_peek_token (parser->lexer);
3165 /* If it's an identifier, and the next token is not a "<", then
3166 we can avoid the template-id case. This is an optimization
3167 for this common case. */
3168 if (token->type == CPP_NAME
3169 && !cp_parser_nth_token_starts_template_argument_list_p
3171 return cp_parser_identifier (parser);
3173 cp_parser_parse_tentatively (parser);
3174 /* Try a template-id. */
3175 id = cp_parser_template_id (parser,
3176 /*template_keyword_p=*/false,
3177 /*check_dependency_p=*/true,
3179 /* If that worked, we're done. */
3180 if (cp_parser_parse_definitely (parser))
3183 /* Peek at the next token. (Changes in the token buffer may
3184 have invalidated the pointer obtained above.) */
3185 token = cp_lexer_peek_token (parser->lexer);
3187 switch (token->type)
3190 return cp_parser_identifier (parser);
3193 if (token->keyword == RID_OPERATOR)
3194 return cp_parser_operator_function_id (parser);
3198 cp_parser_error (parser, "expected id-expression");
3199 return error_mark_node;
3203 return cp_parser_unqualified_id (parser, template_keyword_p,
3204 /*check_dependency_p=*/true,
3208 /* Parse an unqualified-id.
3212 operator-function-id
3213 conversion-function-id
3217 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3218 keyword, in a construct like `A::template ...'.
3220 Returns a representation of unqualified-id. For the `identifier'
3221 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3222 production a BIT_NOT_EXPR is returned; the operand of the
3223 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3224 other productions, see the documentation accompanying the
3225 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3226 names are looked up in uninstantiated templates. If DECLARATOR_P
3227 is true, the unqualified-id is appearing as part of a declarator,
3228 rather than as part of an expression. */
3231 cp_parser_unqualified_id (cp_parser* parser,
3232 bool template_keyword_p,
3233 bool check_dependency_p,
3238 /* Peek at the next token. */
3239 token = cp_lexer_peek_token (parser->lexer);
3241 switch (token->type)
3247 /* We don't know yet whether or not this will be a
3249 cp_parser_parse_tentatively (parser);
3250 /* Try a template-id. */
3251 id = cp_parser_template_id (parser, template_keyword_p,
3254 /* If it worked, we're done. */
3255 if (cp_parser_parse_definitely (parser))
3257 /* Otherwise, it's an ordinary identifier. */
3258 return cp_parser_identifier (parser);
3261 case CPP_TEMPLATE_ID:
3262 return cp_parser_template_id (parser, template_keyword_p,
3269 tree qualifying_scope;
3274 /* Consume the `~' token. */
3275 cp_lexer_consume_token (parser->lexer);
3276 /* Parse the class-name. The standard, as written, seems to
3279 template <typename T> struct S { ~S (); };
3280 template <typename T> S<T>::~S() {}
3282 is invalid, since `~' must be followed by a class-name, but
3283 `S<T>' is dependent, and so not known to be a class.
3284 That's not right; we need to look in uninstantiated
3285 templates. A further complication arises from:
3287 template <typename T> void f(T t) {
3291 Here, it is not possible to look up `T' in the scope of `T'
3292 itself. We must look in both the current scope, and the
3293 scope of the containing complete expression.
3295 Yet another issue is:
3304 The standard does not seem to say that the `S' in `~S'
3305 should refer to the type `S' and not the data member
3308 /* DR 244 says that we look up the name after the "~" in the
3309 same scope as we looked up the qualifying name. That idea
3310 isn't fully worked out; it's more complicated than that. */
3311 scope = parser->scope;
3312 object_scope = parser->object_scope;
3313 qualifying_scope = parser->qualifying_scope;
3315 /* If the name is of the form "X::~X" it's OK. */
3316 if (scope && TYPE_P (scope)
3317 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3318 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3320 && (cp_lexer_peek_token (parser->lexer)->value
3321 == TYPE_IDENTIFIER (scope)))
3323 cp_lexer_consume_token (parser->lexer);
3324 return build_nt (BIT_NOT_EXPR, scope);
3327 /* If there was an explicit qualification (S::~T), first look
3328 in the scope given by the qualification (i.e., S). */
3330 type_decl = NULL_TREE;
3333 cp_parser_parse_tentatively (parser);
3334 type_decl = cp_parser_class_name (parser,
3335 /*typename_keyword_p=*/false,
3336 /*template_keyword_p=*/false,
3338 /*check_dependency=*/false,
3339 /*class_head_p=*/false,
3341 if (cp_parser_parse_definitely (parser))
3344 /* In "N::S::~S", look in "N" as well. */
3345 if (!done && scope && qualifying_scope)
3347 cp_parser_parse_tentatively (parser);
3348 parser->scope = qualifying_scope;
3349 parser->object_scope = NULL_TREE;
3350 parser->qualifying_scope = NULL_TREE;
3352 = cp_parser_class_name (parser,
3353 /*typename_keyword_p=*/false,
3354 /*template_keyword_p=*/false,
3356 /*check_dependency=*/false,
3357 /*class_head_p=*/false,
3359 if (cp_parser_parse_definitely (parser))
3362 /* In "p->S::~T", look in the scope given by "*p" as well. */
3363 else if (!done && object_scope)
3365 cp_parser_parse_tentatively (parser);
3366 parser->scope = object_scope;
3367 parser->object_scope = NULL_TREE;
3368 parser->qualifying_scope = NULL_TREE;
3370 = cp_parser_class_name (parser,
3371 /*typename_keyword_p=*/false,
3372 /*template_keyword_p=*/false,
3374 /*check_dependency=*/false,
3375 /*class_head_p=*/false,
3377 if (cp_parser_parse_definitely (parser))
3380 /* Look in the surrounding context. */
3383 parser->scope = NULL_TREE;
3384 parser->object_scope = NULL_TREE;
3385 parser->qualifying_scope = NULL_TREE;
3387 = cp_parser_class_name (parser,
3388 /*typename_keyword_p=*/false,
3389 /*template_keyword_p=*/false,
3391 /*check_dependency=*/false,
3392 /*class_head_p=*/false,
3395 /* If an error occurred, assume that the name of the
3396 destructor is the same as the name of the qualifying
3397 class. That allows us to keep parsing after running
3398 into ill-formed destructor names. */
3399 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3400 return build_nt (BIT_NOT_EXPR, scope);
3401 else if (type_decl == error_mark_node)
3402 return error_mark_node;
3406 A typedef-name that names a class shall not be used as the
3407 identifier in the declarator for a destructor declaration. */
3409 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3410 && !DECL_SELF_REFERENCE_P (type_decl)
3411 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3412 error ("typedef-name %qD used as destructor declarator",
3415 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3419 if (token->keyword == RID_OPERATOR)
3423 /* This could be a template-id, so we try that first. */
3424 cp_parser_parse_tentatively (parser);
3425 /* Try a template-id. */
3426 id = cp_parser_template_id (parser, template_keyword_p,
3427 /*check_dependency_p=*/true,
3429 /* If that worked, we're done. */
3430 if (cp_parser_parse_definitely (parser))
3432 /* We still don't know whether we're looking at an
3433 operator-function-id or a conversion-function-id. */
3434 cp_parser_parse_tentatively (parser);
3435 /* Try an operator-function-id. */
3436 id = cp_parser_operator_function_id (parser);
3437 /* If that didn't work, try a conversion-function-id. */
3438 if (!cp_parser_parse_definitely (parser))
3439 id = cp_parser_conversion_function_id (parser);
3446 cp_parser_error (parser, "expected unqualified-id");
3447 return error_mark_node;
3451 /* Parse an (optional) nested-name-specifier.
3453 nested-name-specifier:
3454 class-or-namespace-name :: nested-name-specifier [opt]
3455 class-or-namespace-name :: template nested-name-specifier [opt]
3457 PARSER->SCOPE should be set appropriately before this function is
3458 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3459 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3462 Sets PARSER->SCOPE to the class (TYPE) or namespace
3463 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3464 it unchanged if there is no nested-name-specifier. Returns the new
3465 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3467 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3468 part of a declaration and/or decl-specifier. */
3471 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3472 bool typename_keyword_p,
3473 bool check_dependency_p,
3475 bool is_declaration)
3477 bool success = false;
3478 tree access_check = NULL_TREE;
3479 cp_token_position start = 0;
3482 /* If the next token corresponds to a nested name specifier, there
3483 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3484 false, it may have been true before, in which case something
3485 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3486 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3487 CHECK_DEPENDENCY_P is false, we have to fall through into the
3489 if (check_dependency_p
3490 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3492 cp_parser_pre_parsed_nested_name_specifier (parser);
3493 return parser->scope;
3496 /* Remember where the nested-name-specifier starts. */
3497 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3498 start = cp_lexer_token_position (parser->lexer, false);
3500 push_deferring_access_checks (dk_deferred);
3506 tree saved_qualifying_scope;
3507 bool template_keyword_p;
3509 /* Spot cases that cannot be the beginning of a
3510 nested-name-specifier. */
3511 token = cp_lexer_peek_token (parser->lexer);
3513 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3514 the already parsed nested-name-specifier. */
3515 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3517 /* Grab the nested-name-specifier and continue the loop. */
3518 cp_parser_pre_parsed_nested_name_specifier (parser);
3523 /* Spot cases that cannot be the beginning of a
3524 nested-name-specifier. On the second and subsequent times
3525 through the loop, we look for the `template' keyword. */
3526 if (success && token->keyword == RID_TEMPLATE)
3528 /* A template-id can start a nested-name-specifier. */
3529 else if (token->type == CPP_TEMPLATE_ID)
3533 /* If the next token is not an identifier, then it is
3534 definitely not a class-or-namespace-name. */
3535 if (token->type != CPP_NAME)
3537 /* If the following token is neither a `<' (to begin a
3538 template-id), nor a `::', then we are not looking at a
3539 nested-name-specifier. */
3540 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3541 if (token->type != CPP_SCOPE
3542 && !cp_parser_nth_token_starts_template_argument_list_p
3547 /* The nested-name-specifier is optional, so we parse
3549 cp_parser_parse_tentatively (parser);
3551 /* Look for the optional `template' keyword, if this isn't the
3552 first time through the loop. */
3554 template_keyword_p = cp_parser_optional_template_keyword (parser);
3556 template_keyword_p = false;
3558 /* Save the old scope since the name lookup we are about to do
3559 might destroy it. */
3560 old_scope = parser->scope;
3561 saved_qualifying_scope = parser->qualifying_scope;
3562 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3563 look up names in "X<T>::I" in order to determine that "Y" is
3564 a template. So, if we have a typename at this point, we make
3565 an effort to look through it. */
3567 && !typename_keyword_p
3569 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3570 parser->scope = resolve_typename_type (parser->scope,
3571 /*only_current_p=*/false);
3572 /* Parse the qualifying entity. */
3574 = cp_parser_class_or_namespace_name (parser,
3580 /* Look for the `::' token. */
3581 cp_parser_require (parser, CPP_SCOPE, "`::'");
3583 /* If we found what we wanted, we keep going; otherwise, we're
3585 if (!cp_parser_parse_definitely (parser))
3587 bool error_p = false;
3589 /* Restore the OLD_SCOPE since it was valid before the
3590 failed attempt at finding the last
3591 class-or-namespace-name. */
3592 parser->scope = old_scope;
3593 parser->qualifying_scope = saved_qualifying_scope;
3594 /* If the next token is an identifier, and the one after
3595 that is a `::', then any valid interpretation would have
3596 found a class-or-namespace-name. */
3597 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3598 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3600 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3603 token = cp_lexer_consume_token (parser->lexer);
3608 decl = cp_parser_lookup_name_simple (parser, token->value);
3609 if (TREE_CODE (decl) == TEMPLATE_DECL)
3610 error ("%qD used without template parameters", decl);
3612 cp_parser_name_lookup_error
3613 (parser, token->value, decl,
3614 "is not a class or namespace");
3615 parser->scope = NULL_TREE;
3617 /* Treat this as a successful nested-name-specifier
3622 If the name found is not a class-name (clause
3623 _class_) or namespace-name (_namespace.def_), the
3624 program is ill-formed. */
3627 cp_lexer_consume_token (parser->lexer);
3632 /* We've found one valid nested-name-specifier. */
3634 /* Make sure we look in the right scope the next time through
3636 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3637 ? TREE_TYPE (new_scope)
3639 /* If it is a class scope, try to complete it; we are about to
3640 be looking up names inside the class. */
3641 if (TYPE_P (parser->scope)
3642 /* Since checking types for dependency can be expensive,
3643 avoid doing it if the type is already complete. */
3644 && !COMPLETE_TYPE_P (parser->scope)
3645 /* Do not try to complete dependent types. */
3646 && !dependent_type_p (parser->scope))
3647 complete_type (parser->scope);
3650 /* Retrieve any deferred checks. Do not pop this access checks yet
3651 so the memory will not be reclaimed during token replacing below. */
3652 access_check = get_deferred_access_checks ();
3654 /* If parsing tentatively, replace the sequence of tokens that makes
3655 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3656 token. That way, should we re-parse the token stream, we will
3657 not have to repeat the effort required to do the parse, nor will
3658 we issue duplicate error messages. */
3659 if (success && start)
3661 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3663 /* Reset the contents of the START token. */
3664 token->type = CPP_NESTED_NAME_SPECIFIER;
3665 token->value = build_tree_list (access_check, parser->scope);
3666 TREE_TYPE (token->value) = parser->qualifying_scope;
3667 token->keyword = RID_MAX;
3669 /* Purge all subsequent tokens. */
3670 cp_lexer_purge_tokens_after (parser->lexer, start);
3673 pop_deferring_access_checks ();
3674 return success ? parser->scope : NULL_TREE;
3677 /* Parse a nested-name-specifier. See
3678 cp_parser_nested_name_specifier_opt for details. This function
3679 behaves identically, except that it will an issue an error if no
3680 nested-name-specifier is present. */
3683 cp_parser_nested_name_specifier (cp_parser *parser,
3684 bool typename_keyword_p,
3685 bool check_dependency_p,
3687 bool is_declaration)
3691 /* Look for the nested-name-specifier. */
3692 scope = cp_parser_nested_name_specifier_opt (parser,
3697 /* If it was not present, issue an error message. */
3700 cp_parser_error (parser, "expected nested-name-specifier");
3701 parser->scope = NULL_TREE;
3707 /* Parse a class-or-namespace-name.
3709 class-or-namespace-name:
3713 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3714 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3715 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3716 TYPE_P is TRUE iff the next name should be taken as a class-name,
3717 even the same name is declared to be another entity in the same
3720 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3721 specified by the class-or-namespace-name. If neither is found the
3722 ERROR_MARK_NODE is returned. */
3725 cp_parser_class_or_namespace_name (cp_parser *parser,
3726 bool typename_keyword_p,
3727 bool template_keyword_p,
3728 bool check_dependency_p,
3730 bool is_declaration)
3733 tree saved_qualifying_scope;
3734 tree saved_object_scope;
3738 /* Before we try to parse the class-name, we must save away the
3739 current PARSER->SCOPE since cp_parser_class_name will destroy
3741 saved_scope = parser->scope;
3742 saved_qualifying_scope = parser->qualifying_scope;
3743 saved_object_scope = parser->object_scope;
3744 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3745 there is no need to look for a namespace-name. */
3746 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3748 cp_parser_parse_tentatively (parser);
3749 scope = cp_parser_class_name (parser,
3752 type_p ? class_type : none_type,
3754 /*class_head_p=*/false,
3756 /* If that didn't work, try for a namespace-name. */
3757 if (!only_class_p && !cp_parser_parse_definitely (parser))
3759 /* Restore the saved scope. */
3760 parser->scope = saved_scope;
3761 parser->qualifying_scope = saved_qualifying_scope;
3762 parser->object_scope = saved_object_scope;
3763 /* If we are not looking at an identifier followed by the scope
3764 resolution operator, then this is not part of a
3765 nested-name-specifier. (Note that this function is only used
3766 to parse the components of a nested-name-specifier.) */
3767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3768 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3769 return error_mark_node;
3770 scope = cp_parser_namespace_name (parser);
3776 /* Parse a postfix-expression.
3780 postfix-expression [ expression ]
3781 postfix-expression ( expression-list [opt] )
3782 simple-type-specifier ( expression-list [opt] )
3783 typename :: [opt] nested-name-specifier identifier
3784 ( expression-list [opt] )
3785 typename :: [opt] nested-name-specifier template [opt] template-id
3786 ( expression-list [opt] )
3787 postfix-expression . template [opt] id-expression
3788 postfix-expression -> template [opt] id-expression
3789 postfix-expression . pseudo-destructor-name
3790 postfix-expression -> pseudo-destructor-name
3791 postfix-expression ++
3792 postfix-expression --
3793 dynamic_cast < type-id > ( expression )
3794 static_cast < type-id > ( expression )
3795 reinterpret_cast < type-id > ( expression )
3796 const_cast < type-id > ( expression )
3797 typeid ( expression )
3803 ( type-id ) { initializer-list , [opt] }
3805 This extension is a GNU version of the C99 compound-literal
3806 construct. (The C99 grammar uses `type-name' instead of `type-id',
3807 but they are essentially the same concept.)
3809 If ADDRESS_P is true, the postfix expression is the operand of the
3810 `&' operator. CAST_P is true if this expression is the target of a
3813 Returns a representation of the expression. */
3816 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3820 cp_id_kind idk = CP_ID_KIND_NONE;
3821 tree postfix_expression = NULL_TREE;
3822 /* Non-NULL only if the current postfix-expression can be used to
3823 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3824 class used to qualify the member. */
3825 tree qualifying_class = NULL_TREE;
3827 /* Peek at the next token. */
3828 token = cp_lexer_peek_token (parser->lexer);
3829 /* Some of the productions are determined by keywords. */
3830 keyword = token->keyword;
3840 const char *saved_message;
3842 /* All of these can be handled in the same way from the point
3843 of view of parsing. Begin by consuming the token
3844 identifying the cast. */
3845 cp_lexer_consume_token (parser->lexer);
3847 /* New types cannot be defined in the cast. */
3848 saved_message = parser->type_definition_forbidden_message;
3849 parser->type_definition_forbidden_message
3850 = "types may not be defined in casts";
3852 /* Look for the opening `<'. */
3853 cp_parser_require (parser, CPP_LESS, "`<'");
3854 /* Parse the type to which we are casting. */
3855 type = cp_parser_type_id (parser);
3856 /* Look for the closing `>'. */
3857 cp_parser_require (parser, CPP_GREATER, "`>'");
3858 /* Restore the old message. */
3859 parser->type_definition_forbidden_message = saved_message;
3861 /* And the expression which is being cast. */
3862 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3863 expression = cp_parser_expression (parser, /*cast_p=*/true);
3864 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3866 /* Only type conversions to integral or enumeration types
3867 can be used in constant-expressions. */
3868 if (parser->integral_constant_expression_p
3869 && !dependent_type_p (type)
3870 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3871 && (cp_parser_non_integral_constant_expression
3873 "a cast to a type other than an integral or "
3874 "enumeration type")))
3875 return error_mark_node;
3881 = build_dynamic_cast (type, expression);
3885 = build_static_cast (type, expression);
3889 = build_reinterpret_cast (type, expression);
3893 = build_const_cast (type, expression);
3904 const char *saved_message;
3905 bool saved_in_type_id_in_expr_p;
3907 /* Consume the `typeid' token. */
3908 cp_lexer_consume_token (parser->lexer);
3909 /* Look for the `(' token. */
3910 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3911 /* Types cannot be defined in a `typeid' expression. */
3912 saved_message = parser->type_definition_forbidden_message;
3913 parser->type_definition_forbidden_message
3914 = "types may not be defined in a `typeid\' expression";
3915 /* We can't be sure yet whether we're looking at a type-id or an
3917 cp_parser_parse_tentatively (parser);
3918 /* Try a type-id first. */
3919 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3920 parser->in_type_id_in_expr_p = true;
3921 type = cp_parser_type_id (parser);
3922 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3923 /* Look for the `)' token. Otherwise, we can't be sure that
3924 we're not looking at an expression: consider `typeid (int
3925 (3))', for example. */
3926 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3927 /* If all went well, simply lookup the type-id. */
3928 if (cp_parser_parse_definitely (parser))
3929 postfix_expression = get_typeid (type);
3930 /* Otherwise, fall back to the expression variant. */
3935 /* Look for an expression. */
3936 expression = cp_parser_expression (parser, /*cast_p=*/false);
3937 /* Compute its typeid. */
3938 postfix_expression = build_typeid (expression);
3939 /* Look for the `)' token. */
3940 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3942 /* `typeid' may not appear in an integral constant expression. */
3943 if (cp_parser_non_integral_constant_expression(parser,
3944 "`typeid' operator"))
3945 return error_mark_node;
3946 /* Restore the saved message. */
3947 parser->type_definition_forbidden_message = saved_message;
3953 bool template_p = false;
3958 /* Consume the `typename' token. */
3959 cp_lexer_consume_token (parser->lexer);
3960 /* Look for the optional `::' operator. */
3961 cp_parser_global_scope_opt (parser,
3962 /*current_scope_valid_p=*/false);
3963 /* Look for the nested-name-specifier. In case of error here,
3964 consume the trailing id to avoid subsequent error messages
3966 scope = cp_parser_nested_name_specifier (parser,
3967 /*typename_keyword_p=*/true,
3968 /*check_dependency_p=*/true,
3970 /*is_declaration=*/true);
3972 /* Look for the optional `template' keyword. */
3973 template_p = cp_parser_optional_template_keyword (parser);
3974 /* We don't know whether we're looking at a template-id or an
3976 cp_parser_parse_tentatively (parser);
3977 /* Try a template-id. */
3978 id = cp_parser_template_id (parser, template_p,
3979 /*check_dependency_p=*/true,
3980 /*is_declaration=*/true);
3981 /* If that didn't work, try an identifier. */
3982 if (!cp_parser_parse_definitely (parser))
3983 id = cp_parser_identifier (parser);
3985 /* Don't process id if nested name specifier is invalid. */
3986 if (!scope || scope == error_mark_node)
3987 return error_mark_node;
3988 /* If we look up a template-id in a non-dependent qualifying
3989 scope, there's no need to create a dependent type. */
3990 else if (TREE_CODE (id) == TYPE_DECL
3991 && !dependent_type_p (parser->scope))
3992 type = TREE_TYPE (id);
3993 /* Create a TYPENAME_TYPE to represent the type to which the
3994 functional cast is being performed. */
3996 type = make_typename_type (parser->scope, id,
4000 postfix_expression = cp_parser_functional_cast (parser, type);
4008 /* If the next thing is a simple-type-specifier, we may be
4009 looking at a functional cast. We could also be looking at
4010 an id-expression. So, we try the functional cast, and if
4011 that doesn't work we fall back to the primary-expression. */
4012 cp_parser_parse_tentatively (parser);
4013 /* Look for the simple-type-specifier. */
4014 type = cp_parser_simple_type_specifier (parser,
4015 /*decl_specs=*/NULL,
4016 CP_PARSER_FLAGS_NONE);
4017 /* Parse the cast itself. */
4018 if (!cp_parser_error_occurred (parser))
4020 = cp_parser_functional_cast (parser, type);
4021 /* If that worked, we're done. */
4022 if (cp_parser_parse_definitely (parser))
4025 /* If the functional-cast didn't work out, try a
4026 compound-literal. */
4027 if (cp_parser_allow_gnu_extensions_p (parser)
4028 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4030 tree initializer_list = NULL_TREE;
4031 bool saved_in_type_id_in_expr_p;
4033 cp_parser_parse_tentatively (parser);
4034 /* Consume the `('. */
4035 cp_lexer_consume_token (parser->lexer);
4036 /* Parse the type. */
4037 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4038 parser->in_type_id_in_expr_p = true;
4039 type = cp_parser_type_id (parser);
4040 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4041 /* Look for the `)'. */
4042 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4043 /* Look for the `{'. */
4044 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4045 /* If things aren't going well, there's no need to
4047 if (!cp_parser_error_occurred (parser))
4049 bool non_constant_p;
4050 /* Parse the initializer-list. */
4052 = cp_parser_initializer_list (parser, &non_constant_p);
4053 /* Allow a trailing `,'. */
4054 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4055 cp_lexer_consume_token (parser->lexer);
4056 /* Look for the final `}'. */
4057 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4059 /* If that worked, we're definitely looking at a
4060 compound-literal expression. */
4061 if (cp_parser_parse_definitely (parser))
4063 /* Warn the user that a compound literal is not
4064 allowed in standard C++. */
4066 pedwarn ("ISO C++ forbids compound-literals");
4067 /* Form the representation of the compound-literal. */
4069 = finish_compound_literal (type, initializer_list);
4074 /* It must be a primary-expression. */
4075 postfix_expression = cp_parser_primary_expression (parser,
4083 /* If we were avoiding committing to the processing of a
4084 qualified-id until we knew whether or not we had a
4085 pointer-to-member, we now know. */
4086 if (qualifying_class)
4090 /* Peek at the next token. */
4091 token = cp_lexer_peek_token (parser->lexer);
4092 done = (token->type != CPP_OPEN_SQUARE
4093 && token->type != CPP_OPEN_PAREN
4094 && token->type != CPP_DOT
4095 && token->type != CPP_DEREF
4096 && token->type != CPP_PLUS_PLUS
4097 && token->type != CPP_MINUS_MINUS);
4099 postfix_expression = finish_qualified_id_expr (qualifying_class,
4104 return postfix_expression;
4107 /* Keep looping until the postfix-expression is complete. */
4110 if (idk == CP_ID_KIND_UNQUALIFIED
4111 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4112 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4113 /* It is not a Koenig lookup function call. */
4115 = unqualified_name_lookup_error (postfix_expression);
4117 /* Peek at the next token. */
4118 token = cp_lexer_peek_token (parser->lexer);
4120 switch (token->type)
4122 case CPP_OPEN_SQUARE:
4124 = cp_parser_postfix_open_square_expression (parser,
4127 idk = CP_ID_KIND_NONE;
4130 case CPP_OPEN_PAREN:
4131 /* postfix-expression ( expression-list [opt] ) */
4134 bool is_builtin_constant_p;
4135 bool saved_integral_constant_expression_p = false;
4136 bool saved_non_integral_constant_expression_p = false;
4139 is_builtin_constant_p
4140 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4141 if (is_builtin_constant_p)
4143 /* The whole point of __builtin_constant_p is to allow
4144 non-constant expressions to appear as arguments. */
4145 saved_integral_constant_expression_p
4146 = parser->integral_constant_expression_p;
4147 saved_non_integral_constant_expression_p
4148 = parser->non_integral_constant_expression_p;
4149 parser->integral_constant_expression_p = false;
4151 args = (cp_parser_parenthesized_expression_list
4152 (parser, /*is_attribute_list=*/false,
4154 /*non_constant_p=*/NULL));
4155 if (is_builtin_constant_p)
4157 parser->integral_constant_expression_p
4158 = saved_integral_constant_expression_p;
4159 parser->non_integral_constant_expression_p
4160 = saved_non_integral_constant_expression_p;
4163 if (args == error_mark_node)
4165 postfix_expression = error_mark_node;
4169 /* Function calls are not permitted in
4170 constant-expressions. */
4171 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4172 && cp_parser_non_integral_constant_expression (parser,
4175 postfix_expression = error_mark_node;
4180 if (idk == CP_ID_KIND_UNQUALIFIED)
4182 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4188 = perform_koenig_lookup (postfix_expression, args);
4192 = unqualified_fn_lookup_error (postfix_expression);
4194 /* We do not perform argument-dependent lookup if
4195 normal lookup finds a non-function, in accordance
4196 with the expected resolution of DR 218. */
4197 else if (args && is_overloaded_fn (postfix_expression))
4199 tree fn = get_first_fn (postfix_expression);
4201 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4202 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4204 /* Only do argument dependent lookup if regular
4205 lookup does not find a set of member functions.
4206 [basic.lookup.koenig]/2a */
4207 if (!DECL_FUNCTION_MEMBER_P (fn))
4211 = perform_koenig_lookup (postfix_expression, args);
4216 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4218 tree instance = TREE_OPERAND (postfix_expression, 0);
4219 tree fn = TREE_OPERAND (postfix_expression, 1);
4221 if (processing_template_decl
4222 && (type_dependent_expression_p (instance)
4223 || (!BASELINK_P (fn)
4224 && TREE_CODE (fn) != FIELD_DECL)
4225 || type_dependent_expression_p (fn)
4226 || any_type_dependent_arguments_p (args)))
4229 = build_min_nt (CALL_EXPR, postfix_expression,
4234 if (BASELINK_P (fn))
4236 = (build_new_method_call
4237 (instance, fn, args, NULL_TREE,
4238 (idk == CP_ID_KIND_QUALIFIED
4239 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4242 = finish_call_expr (postfix_expression, args,
4243 /*disallow_virtual=*/false,
4244 /*koenig_p=*/false);
4246 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4247 || TREE_CODE (postfix_expression) == MEMBER_REF
4248 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4249 postfix_expression = (build_offset_ref_call_from_tree
4250 (postfix_expression, args));
4251 else if (idk == CP_ID_KIND_QUALIFIED)
4252 /* A call to a static class member, or a namespace-scope
4255 = finish_call_expr (postfix_expression, args,
4256 /*disallow_virtual=*/true,
4259 /* All other function calls. */
4261 = finish_call_expr (postfix_expression, args,
4262 /*disallow_virtual=*/false,
4265 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4266 idk = CP_ID_KIND_NONE;
4272 /* postfix-expression . template [opt] id-expression
4273 postfix-expression . pseudo-destructor-name
4274 postfix-expression -> template [opt] id-expression
4275 postfix-expression -> pseudo-destructor-name */
4277 /* Consume the `.' or `->' operator. */
4278 cp_lexer_consume_token (parser->lexer);
4281 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4287 /* postfix-expression ++ */
4288 /* Consume the `++' token. */
4289 cp_lexer_consume_token (parser->lexer);
4290 /* Generate a representation for the complete expression. */
4292 = finish_increment_expr (postfix_expression,
4293 POSTINCREMENT_EXPR);
4294 /* Increments may not appear in constant-expressions. */
4295 if (cp_parser_non_integral_constant_expression (parser,
4297 postfix_expression = error_mark_node;
4298 idk = CP_ID_KIND_NONE;
4301 case CPP_MINUS_MINUS:
4302 /* postfix-expression -- */
4303 /* Consume the `--' token. */
4304 cp_lexer_consume_token (parser->lexer);
4305 /* Generate a representation for the complete expression. */
4307 = finish_increment_expr (postfix_expression,
4308 POSTDECREMENT_EXPR);
4309 /* Decrements may not appear in constant-expressions. */
4310 if (cp_parser_non_integral_constant_expression (parser,
4312 postfix_expression = error_mark_node;
4313 idk = CP_ID_KIND_NONE;
4317 return postfix_expression;
4321 /* We should never get here. */
4323 return error_mark_node;
4326 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4327 by cp_parser_builtin_offsetof. We're looking for
4329 postfix-expression [ expression ]
4331 FOR_OFFSETOF is set if we're being called in that context, which
4332 changes how we deal with integer constant expressions. */
4335 cp_parser_postfix_open_square_expression (cp_parser *parser,
4336 tree postfix_expression,
4341 /* Consume the `[' token. */
4342 cp_lexer_consume_token (parser->lexer);
4344 /* Parse the index expression. */
4345 /* ??? For offsetof, there is a question of what to allow here. If
4346 offsetof is not being used in an integral constant expression context,
4347 then we *could* get the right answer by computing the value at runtime.
4348 If we are in an integral constant expression context, then we might
4349 could accept any constant expression; hard to say without analysis.
4350 Rather than open the barn door too wide right away, allow only integer
4351 constant expressions here. */
4353 index = cp_parser_constant_expression (parser, false, NULL);
4355 index = cp_parser_expression (parser, /*cast_p=*/false);
4357 /* Look for the closing `]'. */
4358 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4360 /* Build the ARRAY_REF. */
4361 postfix_expression = grok_array_decl (postfix_expression, index);
4363 /* When not doing offsetof, array references are not permitted in
4364 constant-expressions. */
4366 && (cp_parser_non_integral_constant_expression
4367 (parser, "an array reference")))
4368 postfix_expression = error_mark_node;
4370 return postfix_expression;
4373 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4374 by cp_parser_builtin_offsetof. We're looking for
4376 postfix-expression . template [opt] id-expression
4377 postfix-expression . pseudo-destructor-name
4378 postfix-expression -> template [opt] id-expression
4379 postfix-expression -> pseudo-destructor-name
4381 FOR_OFFSETOF is set if we're being called in that context. That sorta
4382 limits what of the above we'll actually accept, but nevermind.
4383 TOKEN_TYPE is the "." or "->" token, which will already have been
4384 removed from the stream. */
4387 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4388 enum cpp_ttype token_type,
4389 tree postfix_expression,
4390 bool for_offsetof, cp_id_kind *idk)
4395 bool pseudo_destructor_p;
4396 tree scope = NULL_TREE;
4398 /* If this is a `->' operator, dereference the pointer. */
4399 if (token_type == CPP_DEREF)
4400 postfix_expression = build_x_arrow (postfix_expression);
4401 /* Check to see whether or not the expression is type-dependent. */
4402 dependent_p = type_dependent_expression_p (postfix_expression);
4403 /* The identifier following the `->' or `.' is not qualified. */
4404 parser->scope = NULL_TREE;
4405 parser->qualifying_scope = NULL_TREE;
4406 parser->object_scope = NULL_TREE;
4407 *idk = CP_ID_KIND_NONE;
4408 /* Enter the scope corresponding to the type of the object
4409 given by the POSTFIX_EXPRESSION. */
4410 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4412 scope = TREE_TYPE (postfix_expression);
4413 /* According to the standard, no expression should ever have
4414 reference type. Unfortunately, we do not currently match
4415 the standard in this respect in that our internal representation
4416 of an expression may have reference type even when the standard
4417 says it does not. Therefore, we have to manually obtain the
4418 underlying type here. */
4419 scope = non_reference (scope);
4420 /* The type of the POSTFIX_EXPRESSION must be complete. */
4421 scope = complete_type_or_else (scope, NULL_TREE);
4422 /* Let the name lookup machinery know that we are processing a
4423 class member access expression. */
4424 parser->context->object_type = scope;
4425 /* If something went wrong, we want to be able to discern that case,
4426 as opposed to the case where there was no SCOPE due to the type
4427 of expression being dependent. */
4429 scope = error_mark_node;
4430 /* If the SCOPE was erroneous, make the various semantic analysis
4431 functions exit quickly -- and without issuing additional error
4433 if (scope == error_mark_node)
4434 postfix_expression = error_mark_node;
4437 /* Assume this expression is not a pseudo-destructor access. */
4438 pseudo_destructor_p = false;
4440 /* If the SCOPE is a scalar type, then, if this is a valid program,
4441 we must be looking at a pseudo-destructor-name. */
4442 if (scope && SCALAR_TYPE_P (scope))
4447 cp_parser_parse_tentatively (parser);
4448 /* Parse the pseudo-destructor-name. */
4450 cp_parser_pseudo_destructor_name (parser, &s, &type);
4451 if (cp_parser_parse_definitely (parser))
4453 pseudo_destructor_p = true;
4455 = finish_pseudo_destructor_expr (postfix_expression,
4456 s, TREE_TYPE (type));
4460 if (!pseudo_destructor_p)
4462 /* If the SCOPE is not a scalar type, we are looking at an
4463 ordinary class member access expression, rather than a
4464 pseudo-destructor-name. */
4465 template_p = cp_parser_optional_template_keyword (parser);
4466 /* Parse the id-expression. */
4467 name = cp_parser_id_expression (parser, template_p,
4468 /*check_dependency_p=*/true,
4469 /*template_p=*/NULL,
4470 /*declarator_p=*/false);
4471 /* In general, build a SCOPE_REF if the member name is qualified.
4472 However, if the name was not dependent and has already been
4473 resolved; there is no need to build the SCOPE_REF. For example;
4475 struct X { void f(); };
4476 template <typename T> void f(T* t) { t->X::f(); }
4478 Even though "t" is dependent, "X::f" is not and has been resolved
4479 to a BASELINK; there is no need to include scope information. */
4481 /* But we do need to remember that there was an explicit scope for
4482 virtual function calls. */
4484 *idk = CP_ID_KIND_QUALIFIED;
4486 /* If the name is a template-id that names a type, we will get a
4487 TYPE_DECL here. That is invalid code. */
4488 if (TREE_CODE (name) == TYPE_DECL)
4490 error ("invalid use of %qD", name);
4491 postfix_expression = error_mark_node;
4495 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4497 name = build_nt (SCOPE_REF, parser->scope, name);
4498 parser->scope = NULL_TREE;
4499 parser->qualifying_scope = NULL_TREE;
4500 parser->object_scope = NULL_TREE;
4502 if (scope && name && BASELINK_P (name))
4503 adjust_result_of_qualified_name_lookup
4504 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4506 = finish_class_member_access_expr (postfix_expression, name);
4510 /* We no longer need to look up names in the scope of the object on
4511 the left-hand side of the `.' or `->' operator. */
4512 parser->context->object_type = NULL_TREE;
4514 /* Outside of offsetof, these operators may not appear in
4515 constant-expressions. */
4517 && (cp_parser_non_integral_constant_expression
4518 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4519 postfix_expression = error_mark_node;
4521 return postfix_expression;
4524 /* Parse a parenthesized expression-list.
4527 assignment-expression
4528 expression-list, assignment-expression
4533 identifier, expression-list
4535 CAST_P is true if this expression is the target of a cast.
4537 Returns a TREE_LIST. The TREE_VALUE of each node is a
4538 representation of an assignment-expression. Note that a TREE_LIST
4539 is returned even if there is only a single expression in the list.
4540 error_mark_node is returned if the ( and or ) are
4541 missing. NULL_TREE is returned on no expressions. The parentheses
4542 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4543 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4544 indicates whether or not all of the expressions in the list were
4548 cp_parser_parenthesized_expression_list (cp_parser* parser,
4549 bool is_attribute_list,
4551 bool *non_constant_p)
4553 tree expression_list = NULL_TREE;
4554 bool fold_expr_p = is_attribute_list;
4555 tree identifier = NULL_TREE;
4557 /* Assume all the expressions will be constant. */
4559 *non_constant_p = false;
4561 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4562 return error_mark_node;
4564 /* Consume expressions until there are no more. */
4565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4570 /* At the beginning of attribute lists, check to see if the
4571 next token is an identifier. */
4572 if (is_attribute_list
4573 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4577 /* Consume the identifier. */
4578 token = cp_lexer_consume_token (parser->lexer);
4579 /* Save the identifier. */
4580 identifier = token->value;
4584 /* Parse the next assignment-expression. */
4587 bool expr_non_constant_p;
4588 expr = (cp_parser_constant_expression
4589 (parser, /*allow_non_constant_p=*/true,
4590 &expr_non_constant_p));
4591 if (expr_non_constant_p)
4592 *non_constant_p = true;
4595 expr = cp_parser_assignment_expression (parser, cast_p);
4598 expr = fold_non_dependent_expr (expr);
4600 /* Add it to the list. We add error_mark_node
4601 expressions to the list, so that we can still tell if
4602 the correct form for a parenthesized expression-list
4603 is found. That gives better errors. */
4604 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4606 if (expr == error_mark_node)
4610 /* After the first item, attribute lists look the same as
4611 expression lists. */
4612 is_attribute_list = false;
4615 /* If the next token isn't a `,', then we are done. */
4616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4619 /* Otherwise, consume the `,' and keep going. */
4620 cp_lexer_consume_token (parser->lexer);
4623 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4628 /* We try and resync to an unnested comma, as that will give the
4629 user better diagnostics. */
4630 ending = cp_parser_skip_to_closing_parenthesis (parser,
4631 /*recovering=*/true,
4633 /*consume_paren=*/true);
4637 return error_mark_node;
4640 /* We built up the list in reverse order so we must reverse it now. */
4641 expression_list = nreverse (expression_list);
4643 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4645 return expression_list;
4648 /* Parse a pseudo-destructor-name.
4650 pseudo-destructor-name:
4651 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4652 :: [opt] nested-name-specifier template template-id :: ~ type-name
4653 :: [opt] nested-name-specifier [opt] ~ type-name
4655 If either of the first two productions is used, sets *SCOPE to the
4656 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4657 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4658 or ERROR_MARK_NODE if the parse fails. */
4661 cp_parser_pseudo_destructor_name (cp_parser* parser,
4665 bool nested_name_specifier_p;
4667 /* Assume that things will not work out. */
4668 *type = error_mark_node;
4670 /* Look for the optional `::' operator. */
4671 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4672 /* Look for the optional nested-name-specifier. */
4673 nested_name_specifier_p
4674 = (cp_parser_nested_name_specifier_opt (parser,
4675 /*typename_keyword_p=*/false,
4676 /*check_dependency_p=*/true,
4678 /*is_declaration=*/true)
4680 /* Now, if we saw a nested-name-specifier, we might be doing the
4681 second production. */
4682 if (nested_name_specifier_p
4683 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4685 /* Consume the `template' keyword. */
4686 cp_lexer_consume_token (parser->lexer);
4687 /* Parse the template-id. */
4688 cp_parser_template_id (parser,
4689 /*template_keyword_p=*/true,
4690 /*check_dependency_p=*/false,
4691 /*is_declaration=*/true);
4692 /* Look for the `::' token. */
4693 cp_parser_require (parser, CPP_SCOPE, "`::'");
4695 /* If the next token is not a `~', then there might be some
4696 additional qualification. */
4697 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4699 /* Look for the type-name. */
4700 *scope = TREE_TYPE (cp_parser_type_name (parser));
4702 if (*scope == error_mark_node)
4705 /* If we don't have ::~, then something has gone wrong. Since
4706 the only caller of this function is looking for something
4707 after `.' or `->' after a scalar type, most likely the
4708 program is trying to get a member of a non-aggregate
4710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4711 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4713 cp_parser_error (parser, "request for member of non-aggregate type");
4717 /* Look for the `::' token. */
4718 cp_parser_require (parser, CPP_SCOPE, "`::'");
4723 /* Look for the `~'. */
4724 cp_parser_require (parser, CPP_COMPL, "`~'");
4725 /* Look for the type-name again. We are not responsible for
4726 checking that it matches the first type-name. */
4727 *type = cp_parser_type_name (parser);
4730 /* Parse a unary-expression.
4736 unary-operator cast-expression
4737 sizeof unary-expression
4745 __extension__ cast-expression
4746 __alignof__ unary-expression
4747 __alignof__ ( type-id )
4748 __real__ cast-expression
4749 __imag__ cast-expression
4752 ADDRESS_P is true iff the unary-expression is appearing as the
4753 operand of the `&' operator. CAST_P is true if this expression is
4754 the target of a cast.
4756 Returns a representation of the expression. */
4759 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4762 enum tree_code unary_operator;
4764 /* Peek at the next token. */
4765 token = cp_lexer_peek_token (parser->lexer);
4766 /* Some keywords give away the kind of expression. */
4767 if (token->type == CPP_KEYWORD)
4769 enum rid keyword = token->keyword;
4779 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4780 /* Consume the token. */
4781 cp_lexer_consume_token (parser->lexer);
4782 /* Parse the operand. */
4783 operand = cp_parser_sizeof_operand (parser, keyword);
4785 if (TYPE_P (operand))
4786 return cxx_sizeof_or_alignof_type (operand, op, true);
4788 return cxx_sizeof_or_alignof_expr (operand, op);
4792 return cp_parser_new_expression (parser);
4795 return cp_parser_delete_expression (parser);
4799 /* The saved value of the PEDANTIC flag. */
4803 /* Save away the PEDANTIC flag. */
4804 cp_parser_extension_opt (parser, &saved_pedantic);
4805 /* Parse the cast-expression. */
4806 expr = cp_parser_simple_cast_expression (parser);
4807 /* Restore the PEDANTIC flag. */
4808 pedantic = saved_pedantic;
4818 /* Consume the `__real__' or `__imag__' token. */
4819 cp_lexer_consume_token (parser->lexer);
4820 /* Parse the cast-expression. */
4821 expression = cp_parser_simple_cast_expression (parser);
4822 /* Create the complete representation. */
4823 return build_x_unary_op ((keyword == RID_REALPART
4824 ? REALPART_EXPR : IMAGPART_EXPR),
4834 /* Look for the `:: new' and `:: delete', which also signal the
4835 beginning of a new-expression, or delete-expression,
4836 respectively. If the next token is `::', then it might be one of
4838 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4842 /* See if the token after the `::' is one of the keywords in
4843 which we're interested. */
4844 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4845 /* If it's `new', we have a new-expression. */
4846 if (keyword == RID_NEW)
4847 return cp_parser_new_expression (parser);
4848 /* Similarly, for `delete'. */
4849 else if (keyword == RID_DELETE)
4850 return cp_parser_delete_expression (parser);
4853 /* Look for a unary operator. */
4854 unary_operator = cp_parser_unary_operator (token);
4855 /* The `++' and `--' operators can be handled similarly, even though
4856 they are not technically unary-operators in the grammar. */
4857 if (unary_operator == ERROR_MARK)
4859 if (token->type == CPP_PLUS_PLUS)
4860 unary_operator = PREINCREMENT_EXPR;
4861 else if (token->type == CPP_MINUS_MINUS)
4862 unary_operator = PREDECREMENT_EXPR;
4863 /* Handle the GNU address-of-label extension. */
4864 else if (cp_parser_allow_gnu_extensions_p (parser)
4865 && token->type == CPP_AND_AND)
4869 /* Consume the '&&' token. */
4870 cp_lexer_consume_token (parser->lexer);
4871 /* Look for the identifier. */
4872 identifier = cp_parser_identifier (parser);
4873 /* Create an expression representing the address. */
4874 return finish_label_address_expr (identifier);
4877 if (unary_operator != ERROR_MARK)
4879 tree cast_expression;
4880 tree expression = error_mark_node;
4881 const char *non_constant_p = NULL;
4883 /* Consume the operator token. */
4884 token = cp_lexer_consume_token (parser->lexer);
4885 /* Parse the cast-expression. */
4887 = cp_parser_cast_expression (parser,
4888 unary_operator == ADDR_EXPR,
4890 /* Now, build an appropriate representation. */
4891 switch (unary_operator)
4894 non_constant_p = "`*'";
4895 expression = build_x_indirect_ref (cast_expression, "unary *");
4899 non_constant_p = "`&'";
4902 expression = build_x_unary_op (unary_operator, cast_expression);
4905 case PREINCREMENT_EXPR:
4906 case PREDECREMENT_EXPR:
4907 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4910 case UNARY_PLUS_EXPR:
4912 case TRUTH_NOT_EXPR:
4913 expression = finish_unary_op_expr (unary_operator, cast_expression);
4921 && cp_parser_non_integral_constant_expression (parser,
4923 expression = error_mark_node;
4928 return cp_parser_postfix_expression (parser, address_p, cast_p);
4931 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4932 unary-operator, the corresponding tree code is returned. */
4934 static enum tree_code
4935 cp_parser_unary_operator (cp_token* token)
4937 switch (token->type)
4940 return INDIRECT_REF;
4946 return UNARY_PLUS_EXPR;
4952 return TRUTH_NOT_EXPR;
4955 return BIT_NOT_EXPR;
4962 /* Parse a new-expression.
4965 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4966 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4968 Returns a representation of the expression. */
4971 cp_parser_new_expression (cp_parser* parser)
4973 bool global_scope_p;
4979 /* Look for the optional `::' operator. */
4981 = (cp_parser_global_scope_opt (parser,
4982 /*current_scope_valid_p=*/false)
4984 /* Look for the `new' operator. */
4985 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4986 /* There's no easy way to tell a new-placement from the
4987 `( type-id )' construct. */
4988 cp_parser_parse_tentatively (parser);
4989 /* Look for a new-placement. */
4990 placement = cp_parser_new_placement (parser);
4991 /* If that didn't work out, there's no new-placement. */
4992 if (!cp_parser_parse_definitely (parser))
4993 placement = NULL_TREE;
4995 /* If the next token is a `(', then we have a parenthesized
4997 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4999 /* Consume the `('. */
5000 cp_lexer_consume_token (parser->lexer);
5001 /* Parse the type-id. */
5002 type = cp_parser_type_id (parser);
5003 /* Look for the closing `)'. */
5004 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5005 /* There should not be a direct-new-declarator in this production,
5006 but GCC used to allowed this, so we check and emit a sensible error
5007 message for this case. */
5008 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5010 error ("array bound forbidden after parenthesized type-id");
5011 inform ("try removing the parentheses around the type-id");
5012 cp_parser_direct_new_declarator (parser);
5016 /* Otherwise, there must be a new-type-id. */
5018 type = cp_parser_new_type_id (parser, &nelts);
5020 /* If the next token is a `(', then we have a new-initializer. */
5021 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5022 initializer = cp_parser_new_initializer (parser);
5024 initializer = NULL_TREE;
5026 /* A new-expression may not appear in an integral constant
5028 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5029 return error_mark_node;
5031 /* Create a representation of the new-expression. */
5032 return build_new (placement, type, nelts, initializer, global_scope_p);
5035 /* Parse a new-placement.
5040 Returns the same representation as for an expression-list. */
5043 cp_parser_new_placement (cp_parser* parser)
5045 tree expression_list;
5047 /* Parse the expression-list. */
5048 expression_list = (cp_parser_parenthesized_expression_list
5049 (parser, false, /*cast_p=*/false,
5050 /*non_constant_p=*/NULL));
5052 return expression_list;
5055 /* Parse a new-type-id.
5058 type-specifier-seq new-declarator [opt]
5060 Returns the TYPE allocated. If the new-type-id indicates an array
5061 type, *NELTS is set to the number of elements in the last array
5062 bound; the TYPE will not include the last array bound. */
5065 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5067 cp_decl_specifier_seq type_specifier_seq;
5068 cp_declarator *new_declarator;
5069 cp_declarator *declarator;
5070 cp_declarator *outer_declarator;
5071 const char *saved_message;
5074 /* The type-specifier sequence must not contain type definitions.
5075 (It cannot contain declarations of new types either, but if they
5076 are not definitions we will catch that because they are not
5078 saved_message = parser->type_definition_forbidden_message;
5079 parser->type_definition_forbidden_message
5080 = "types may not be defined in a new-type-id";
5081 /* Parse the type-specifier-seq. */
5082 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5083 &type_specifier_seq);
5084 /* Restore the old message. */
5085 parser->type_definition_forbidden_message = saved_message;
5086 /* Parse the new-declarator. */
5087 new_declarator = cp_parser_new_declarator_opt (parser);
5089 /* Determine the number of elements in the last array dimension, if
5092 /* Skip down to the last array dimension. */
5093 declarator = new_declarator;
5094 outer_declarator = NULL;
5095 while (declarator && (declarator->kind == cdk_pointer
5096 || declarator->kind == cdk_ptrmem))
5098 outer_declarator = declarator;
5099 declarator = declarator->declarator;
5102 && declarator->kind == cdk_array
5103 && declarator->declarator
5104 && declarator->declarator->kind == cdk_array)
5106 outer_declarator = declarator;
5107 declarator = declarator->declarator;
5110 if (declarator && declarator->kind == cdk_array)
5112 *nelts = declarator->u.array.bounds;
5113 if (*nelts == error_mark_node)
5114 *nelts = integer_one_node;
5116 if (outer_declarator)
5117 outer_declarator->declarator = declarator->declarator;
5119 new_declarator = NULL;
5122 type = groktypename (&type_specifier_seq, new_declarator);
5123 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5125 *nelts = array_type_nelts_top (type);
5126 type = TREE_TYPE (type);
5131 /* Parse an (optional) new-declarator.
5134 ptr-operator new-declarator [opt]
5135 direct-new-declarator
5137 Returns the declarator. */
5139 static cp_declarator *
5140 cp_parser_new_declarator_opt (cp_parser* parser)
5142 enum tree_code code;
5144 cp_cv_quals cv_quals;
5146 /* We don't know if there's a ptr-operator next, or not. */
5147 cp_parser_parse_tentatively (parser);
5148 /* Look for a ptr-operator. */
5149 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5150 /* If that worked, look for more new-declarators. */
5151 if (cp_parser_parse_definitely (parser))
5153 cp_declarator *declarator;
5155 /* Parse another optional declarator. */
5156 declarator = cp_parser_new_declarator_opt (parser);
5158 /* Create the representation of the declarator. */
5160 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5161 else if (code == INDIRECT_REF)
5162 declarator = make_pointer_declarator (cv_quals, declarator);
5164 declarator = make_reference_declarator (cv_quals, declarator);
5169 /* If the next token is a `[', there is a direct-new-declarator. */
5170 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5171 return cp_parser_direct_new_declarator (parser);
5176 /* Parse a direct-new-declarator.
5178 direct-new-declarator:
5180 direct-new-declarator [constant-expression]
5184 static cp_declarator *
5185 cp_parser_direct_new_declarator (cp_parser* parser)
5187 cp_declarator *declarator = NULL;
5193 /* Look for the opening `['. */
5194 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5195 /* The first expression is not required to be constant. */
5198 expression = cp_parser_expression (parser, /*cast_p=*/false);
5199 /* The standard requires that the expression have integral
5200 type. DR 74 adds enumeration types. We believe that the
5201 real intent is that these expressions be handled like the
5202 expression in a `switch' condition, which also allows
5203 classes with a single conversion to integral or
5204 enumeration type. */
5205 if (!processing_template_decl)
5208 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5213 error ("expression in new-declarator must have integral "
5214 "or enumeration type");
5215 expression = error_mark_node;
5219 /* But all the other expressions must be. */
5222 = cp_parser_constant_expression (parser,
5223 /*allow_non_constant=*/false,
5225 /* Look for the closing `]'. */
5226 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5228 /* Add this bound to the declarator. */
5229 declarator = make_array_declarator (declarator, expression);
5231 /* If the next token is not a `[', then there are no more
5233 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5240 /* Parse a new-initializer.
5243 ( expression-list [opt] )
5245 Returns a representation of the expression-list. If there is no
5246 expression-list, VOID_ZERO_NODE is returned. */
5249 cp_parser_new_initializer (cp_parser* parser)
5251 tree expression_list;
5253 expression_list = (cp_parser_parenthesized_expression_list
5254 (parser, false, /*cast_p=*/false,
5255 /*non_constant_p=*/NULL));
5256 if (!expression_list)
5257 expression_list = void_zero_node;
5259 return expression_list;
5262 /* Parse a delete-expression.
5265 :: [opt] delete cast-expression
5266 :: [opt] delete [ ] cast-expression
5268 Returns a representation of the expression. */
5271 cp_parser_delete_expression (cp_parser* parser)
5273 bool global_scope_p;
5277 /* Look for the optional `::' operator. */
5279 = (cp_parser_global_scope_opt (parser,
5280 /*current_scope_valid_p=*/false)
5282 /* Look for the `delete' keyword. */
5283 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5284 /* See if the array syntax is in use. */
5285 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5287 /* Consume the `[' token. */
5288 cp_lexer_consume_token (parser->lexer);
5289 /* Look for the `]' token. */
5290 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5291 /* Remember that this is the `[]' construct. */
5297 /* Parse the cast-expression. */
5298 expression = cp_parser_simple_cast_expression (parser);
5300 /* A delete-expression may not appear in an integral constant
5302 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5303 return error_mark_node;
5305 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5308 /* Parse a cast-expression.
5312 ( type-id ) cast-expression
5314 ADDRESS_P is true iff the unary-expression is appearing as the
5315 operand of the `&' operator. CAST_P is true if this expression is
5316 the target of a cast.
5318 Returns a representation of the expression. */
5321 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5323 /* If it's a `(', then we might be looking at a cast. */
5324 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5326 tree type = NULL_TREE;
5327 tree expr = NULL_TREE;
5328 bool compound_literal_p;
5329 const char *saved_message;
5331 /* There's no way to know yet whether or not this is a cast.
5332 For example, `(int (3))' is a unary-expression, while `(int)
5333 3' is a cast. So, we resort to parsing tentatively. */
5334 cp_parser_parse_tentatively (parser);
5335 /* Types may not be defined in a cast. */
5336 saved_message = parser->type_definition_forbidden_message;
5337 parser->type_definition_forbidden_message
5338 = "types may not be defined in casts";
5339 /* Consume the `('. */
5340 cp_lexer_consume_token (parser->lexer);
5341 /* A very tricky bit is that `(struct S) { 3 }' is a
5342 compound-literal (which we permit in C++ as an extension).
5343 But, that construct is not a cast-expression -- it is a
5344 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5345 is legal; if the compound-literal were a cast-expression,
5346 you'd need an extra set of parentheses.) But, if we parse
5347 the type-id, and it happens to be a class-specifier, then we
5348 will commit to the parse at that point, because we cannot
5349 undo the action that is done when creating a new class. So,
5350 then we cannot back up and do a postfix-expression.
5352 Therefore, we scan ahead to the closing `)', and check to see
5353 if the token after the `)' is a `{'. If so, we are not
5354 looking at a cast-expression.
5356 Save tokens so that we can put them back. */
5357 cp_lexer_save_tokens (parser->lexer);
5358 /* Skip tokens until the next token is a closing parenthesis.
5359 If we find the closing `)', and the next token is a `{', then
5360 we are looking at a compound-literal. */
5362 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5363 /*consume_paren=*/true)
5364 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5365 /* Roll back the tokens we skipped. */
5366 cp_lexer_rollback_tokens (parser->lexer);
5367 /* If we were looking at a compound-literal, simulate an error
5368 so that the call to cp_parser_parse_definitely below will
5370 if (compound_literal_p)
5371 cp_parser_simulate_error (parser);
5374 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5375 parser->in_type_id_in_expr_p = true;
5376 /* Look for the type-id. */
5377 type = cp_parser_type_id (parser);
5378 /* Look for the closing `)'. */
5379 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5380 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5383 /* Restore the saved message. */
5384 parser->type_definition_forbidden_message = saved_message;
5386 /* If ok so far, parse the dependent expression. We cannot be
5387 sure it is a cast. Consider `(T ())'. It is a parenthesized
5388 ctor of T, but looks like a cast to function returning T
5389 without a dependent expression. */
5390 if (!cp_parser_error_occurred (parser))
5391 expr = cp_parser_cast_expression (parser,
5392 /*address_p=*/false,
5395 if (cp_parser_parse_definitely (parser))
5397 /* Warn about old-style casts, if so requested. */
5398 if (warn_old_style_cast
5399 && !in_system_header
5400 && !VOID_TYPE_P (type)
5401 && current_lang_name != lang_name_c)
5402 warning (0, "use of old-style cast");
5404 /* Only type conversions to integral or enumeration types
5405 can be used in constant-expressions. */
5406 if (parser->integral_constant_expression_p
5407 && !dependent_type_p (type)
5408 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5409 && (cp_parser_non_integral_constant_expression
5411 "a cast to a type other than an integral or "
5412 "enumeration type")))
5413 return error_mark_node;
5415 /* Perform the cast. */
5416 expr = build_c_cast (type, expr);
5421 /* If we get here, then it's not a cast, so it must be a
5422 unary-expression. */
5423 return cp_parser_unary_expression (parser, address_p, cast_p);
5426 /* Parse a binary expression of the general form:
5430 pm-expression .* cast-expression
5431 pm-expression ->* cast-expression
5433 multiplicative-expression:
5435 multiplicative-expression * pm-expression
5436 multiplicative-expression / pm-expression
5437 multiplicative-expression % pm-expression
5439 additive-expression:
5440 multiplicative-expression
5441 additive-expression + multiplicative-expression
5442 additive-expression - multiplicative-expression
5446 shift-expression << additive-expression
5447 shift-expression >> additive-expression
5449 relational-expression:
5451 relational-expression < shift-expression
5452 relational-expression > shift-expression
5453 relational-expression <= shift-expression
5454 relational-expression >= shift-expression
5458 relational-expression:
5459 relational-expression <? shift-expression
5460 relational-expression >? shift-expression
5462 equality-expression:
5463 relational-expression
5464 equality-expression == relational-expression
5465 equality-expression != relational-expression
5469 and-expression & equality-expression
5471 exclusive-or-expression:
5473 exclusive-or-expression ^ and-expression
5475 inclusive-or-expression:
5476 exclusive-or-expression
5477 inclusive-or-expression | exclusive-or-expression
5479 logical-and-expression:
5480 inclusive-or-expression
5481 logical-and-expression && inclusive-or-expression
5483 logical-or-expression:
5484 logical-and-expression
5485 logical-or-expression || logical-and-expression
5487 All these are implemented with a single function like:
5490 simple-cast-expression
5491 binary-expression <token> binary-expression
5493 CAST_P is true if this expression is the target of a cast.
5495 The binops_by_token map is used to get the tree codes for each <token> type.
5496 binary-expressions are associated according to a precedence table. */
5498 #define TOKEN_PRECEDENCE(token) \
5499 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5500 ? PREC_NOT_OPERATOR \
5501 : binops_by_token[token->type].prec)
5504 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5506 cp_parser_expression_stack stack;
5507 cp_parser_expression_stack_entry *sp = &stack[0];
5510 enum tree_code tree_type;
5511 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5514 /* Parse the first expression. */
5515 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5519 /* Get an operator token. */
5520 token = cp_lexer_peek_token (parser->lexer);
5521 if (token->type == CPP_MIN || token->type == CPP_MAX)
5522 cp_parser_warn_min_max ();
5524 new_prec = TOKEN_PRECEDENCE (token);
5526 /* Popping an entry off the stack means we completed a subexpression:
5527 - either we found a token which is not an operator (`>' where it is not
5528 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5529 will happen repeatedly;
5530 - or, we found an operator which has lower priority. This is the case
5531 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5533 if (new_prec <= prec)
5542 tree_type = binops_by_token[token->type].tree_type;
5544 /* We used the operator token. */
5545 cp_lexer_consume_token (parser->lexer);
5547 /* Extract another operand. It may be the RHS of this expression
5548 or the LHS of a new, higher priority expression. */
5549 rhs = cp_parser_simple_cast_expression (parser);
5551 /* Get another operator token. Look up its precedence to avoid
5552 building a useless (immediately popped) stack entry for common
5553 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5554 token = cp_lexer_peek_token (parser->lexer);
5555 lookahead_prec = TOKEN_PRECEDENCE (token);
5556 if (lookahead_prec > new_prec)
5558 /* ... and prepare to parse the RHS of the new, higher priority
5559 expression. Since precedence levels on the stack are
5560 monotonically increasing, we do not have to care about
5563 sp->tree_type = tree_type;
5568 new_prec = lookahead_prec;
5572 /* If the stack is not empty, we have parsed into LHS the right side
5573 (`4' in the example above) of an expression we had suspended.
5574 We can use the information on the stack to recover the LHS (`3')
5575 from the stack together with the tree code (`MULT_EXPR'), and
5576 the precedence of the higher level subexpression
5577 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5578 which will be used to actually build the additive expression. */
5581 tree_type = sp->tree_type;
5586 overloaded_p = false;
5587 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5589 /* If the binary operator required the use of an overloaded operator,
5590 then this expression cannot be an integral constant-expression.
5591 An overloaded operator can be used even if both operands are
5592 otherwise permissible in an integral constant-expression if at
5593 least one of the operands is of enumeration type. */
5596 && (cp_parser_non_integral_constant_expression
5597 (parser, "calls to overloaded operators")))
5598 return error_mark_node;
5605 /* Parse the `? expression : assignment-expression' part of a
5606 conditional-expression. The LOGICAL_OR_EXPR is the
5607 logical-or-expression that started the conditional-expression.
5608 Returns a representation of the entire conditional-expression.
5610 This routine is used by cp_parser_assignment_expression.
5612 ? expression : assignment-expression
5616 ? : assignment-expression */
5619 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5622 tree assignment_expr;
5624 /* Consume the `?' token. */
5625 cp_lexer_consume_token (parser->lexer);
5626 if (cp_parser_allow_gnu_extensions_p (parser)
5627 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5628 /* Implicit true clause. */
5631 /* Parse the expression. */
5632 expr = cp_parser_expression (parser, /*cast_p=*/false);
5634 /* The next token should be a `:'. */
5635 cp_parser_require (parser, CPP_COLON, "`:'");
5636 /* Parse the assignment-expression. */
5637 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5639 /* Build the conditional-expression. */
5640 return build_x_conditional_expr (logical_or_expr,
5645 /* Parse an assignment-expression.
5647 assignment-expression:
5648 conditional-expression
5649 logical-or-expression assignment-operator assignment_expression
5652 CAST_P is true if this expression is the target of a cast.
5654 Returns a representation for the expression. */
5657 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5661 /* If the next token is the `throw' keyword, then we're looking at
5662 a throw-expression. */
5663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5664 expr = cp_parser_throw_expression (parser);
5665 /* Otherwise, it must be that we are looking at a
5666 logical-or-expression. */
5669 /* Parse the binary expressions (logical-or-expression). */
5670 expr = cp_parser_binary_expression (parser, cast_p);
5671 /* If the next token is a `?' then we're actually looking at a
5672 conditional-expression. */
5673 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5674 return cp_parser_question_colon_clause (parser, expr);
5677 enum tree_code assignment_operator;
5679 /* If it's an assignment-operator, we're using the second
5682 = cp_parser_assignment_operator_opt (parser);
5683 if (assignment_operator != ERROR_MARK)
5687 /* Parse the right-hand side of the assignment. */
5688 rhs = cp_parser_assignment_expression (parser, cast_p);
5689 /* An assignment may not appear in a
5690 constant-expression. */
5691 if (cp_parser_non_integral_constant_expression (parser,
5693 return error_mark_node;
5694 /* Build the assignment expression. */
5695 expr = build_x_modify_expr (expr,
5696 assignment_operator,
5705 /* Parse an (optional) assignment-operator.
5707 assignment-operator: one of
5708 = *= /= %= += -= >>= <<= &= ^= |=
5712 assignment-operator: one of
5715 If the next token is an assignment operator, the corresponding tree
5716 code is returned, and the token is consumed. For example, for
5717 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5718 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5719 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5720 operator, ERROR_MARK is returned. */
5722 static enum tree_code
5723 cp_parser_assignment_operator_opt (cp_parser* parser)
5728 /* Peek at the next toen. */
5729 token = cp_lexer_peek_token (parser->lexer);
5731 switch (token->type)
5742 op = TRUNC_DIV_EXPR;
5746 op = TRUNC_MOD_EXPR;
5779 cp_parser_warn_min_max ();
5784 cp_parser_warn_min_max ();
5788 /* Nothing else is an assignment operator. */
5792 /* If it was an assignment operator, consume it. */
5793 if (op != ERROR_MARK)
5794 cp_lexer_consume_token (parser->lexer);
5799 /* Parse an expression.
5802 assignment-expression
5803 expression , assignment-expression
5805 CAST_P is true if this expression is the target of a cast.
5807 Returns a representation of the expression. */
5810 cp_parser_expression (cp_parser* parser, bool cast_p)
5812 tree expression = NULL_TREE;
5816 tree assignment_expression;
5818 /* Parse the next assignment-expression. */
5819 assignment_expression
5820 = cp_parser_assignment_expression (parser, cast_p);
5821 /* If this is the first assignment-expression, we can just
5824 expression = assignment_expression;
5826 expression = build_x_compound_expr (expression,
5827 assignment_expression);
5828 /* If the next token is not a comma, then we are done with the
5830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5832 /* Consume the `,'. */
5833 cp_lexer_consume_token (parser->lexer);
5834 /* A comma operator cannot appear in a constant-expression. */
5835 if (cp_parser_non_integral_constant_expression (parser,
5836 "a comma operator"))
5837 expression = error_mark_node;
5843 /* Parse a constant-expression.
5845 constant-expression:
5846 conditional-expression
5848 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5849 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5850 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5851 is false, NON_CONSTANT_P should be NULL. */
5854 cp_parser_constant_expression (cp_parser* parser,
5855 bool allow_non_constant_p,
5856 bool *non_constant_p)
5858 bool saved_integral_constant_expression_p;
5859 bool saved_allow_non_integral_constant_expression_p;
5860 bool saved_non_integral_constant_expression_p;
5863 /* It might seem that we could simply parse the
5864 conditional-expression, and then check to see if it were
5865 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5866 one that the compiler can figure out is constant, possibly after
5867 doing some simplifications or optimizations. The standard has a
5868 precise definition of constant-expression, and we must honor
5869 that, even though it is somewhat more restrictive.
5875 is not a legal declaration, because `(2, 3)' is not a
5876 constant-expression. The `,' operator is forbidden in a
5877 constant-expression. However, GCC's constant-folding machinery
5878 will fold this operation to an INTEGER_CST for `3'. */
5880 /* Save the old settings. */
5881 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5882 saved_allow_non_integral_constant_expression_p
5883 = parser->allow_non_integral_constant_expression_p;
5884 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5885 /* We are now parsing a constant-expression. */
5886 parser->integral_constant_expression_p = true;
5887 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5888 parser->non_integral_constant_expression_p = false;
5889 /* Although the grammar says "conditional-expression", we parse an
5890 "assignment-expression", which also permits "throw-expression"
5891 and the use of assignment operators. In the case that
5892 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5893 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5894 actually essential that we look for an assignment-expression.
5895 For example, cp_parser_initializer_clauses uses this function to
5896 determine whether a particular assignment-expression is in fact
5898 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5899 /* Restore the old settings. */
5900 parser->integral_constant_expression_p
5901 = saved_integral_constant_expression_p;
5902 parser->allow_non_integral_constant_expression_p
5903 = saved_allow_non_integral_constant_expression_p;
5904 if (allow_non_constant_p)
5905 *non_constant_p = parser->non_integral_constant_expression_p;
5906 else if (parser->non_integral_constant_expression_p)
5907 expression = error_mark_node;
5908 parser->non_integral_constant_expression_p
5909 = saved_non_integral_constant_expression_p;
5914 /* Parse __builtin_offsetof.
5916 offsetof-expression:
5917 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5919 offsetof-member-designator:
5921 | offsetof-member-designator "." id-expression
5922 | offsetof-member-designator "[" expression "]"
5926 cp_parser_builtin_offsetof (cp_parser *parser)
5928 int save_ice_p, save_non_ice_p;
5932 /* We're about to accept non-integral-constant things, but will
5933 definitely yield an integral constant expression. Save and
5934 restore these values around our local parsing. */
5935 save_ice_p = parser->integral_constant_expression_p;
5936 save_non_ice_p = parser->non_integral_constant_expression_p;
5938 /* Consume the "__builtin_offsetof" token. */
5939 cp_lexer_consume_token (parser->lexer);
5940 /* Consume the opening `('. */
5941 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5942 /* Parse the type-id. */
5943 type = cp_parser_type_id (parser);
5944 /* Look for the `,'. */
5945 cp_parser_require (parser, CPP_COMMA, "`,'");
5947 /* Build the (type *)null that begins the traditional offsetof macro. */
5948 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5950 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5951 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5955 cp_token *token = cp_lexer_peek_token (parser->lexer);
5956 switch (token->type)
5958 case CPP_OPEN_SQUARE:
5959 /* offsetof-member-designator "[" expression "]" */
5960 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5964 /* offsetof-member-designator "." identifier */
5965 cp_lexer_consume_token (parser->lexer);
5966 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5970 case CPP_CLOSE_PAREN:
5971 /* Consume the ")" token. */
5972 cp_lexer_consume_token (parser->lexer);
5976 /* Error. We know the following require will fail, but
5977 that gives the proper error message. */
5978 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5979 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5980 expr = error_mark_node;
5986 /* If we're processing a template, we can't finish the semantics yet.
5987 Otherwise we can fold the entire expression now. */
5988 if (processing_template_decl)
5989 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5991 expr = fold_offsetof (expr);
5994 parser->integral_constant_expression_p = save_ice_p;
5995 parser->non_integral_constant_expression_p = save_non_ice_p;
6000 /* Statements [gram.stmt.stmt] */
6002 /* Parse a statement.
6006 expression-statement
6011 declaration-statement
6015 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6019 location_t statement_location;
6021 /* There is no statement yet. */
6022 statement = NULL_TREE;
6023 /* Peek at the next token. */
6024 token = cp_lexer_peek_token (parser->lexer);
6025 /* Remember the location of the first token in the statement. */
6026 statement_location = token->location;
6027 /* If this is a keyword, then that will often determine what kind of
6028 statement we have. */
6029 if (token->type == CPP_KEYWORD)
6031 enum rid keyword = token->keyword;
6037 statement = cp_parser_labeled_statement (parser,
6043 statement = cp_parser_selection_statement (parser);
6049 statement = cp_parser_iteration_statement (parser);
6056 statement = cp_parser_jump_statement (parser);
6059 /* Objective-C++ exception-handling constructs. */
6062 case RID_AT_FINALLY:
6063 case RID_AT_SYNCHRONIZED:
6065 statement = cp_parser_objc_statement (parser);
6069 statement = cp_parser_try_block (parser);
6073 /* It might be a keyword like `int' that can start a
6074 declaration-statement. */
6078 else if (token->type == CPP_NAME)
6080 /* If the next token is a `:', then we are looking at a
6081 labeled-statement. */
6082 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6083 if (token->type == CPP_COLON)
6084 statement = cp_parser_labeled_statement (parser, in_statement_expr);
6086 /* Anything that starts with a `{' must be a compound-statement. */
6087 else if (token->type == CPP_OPEN_BRACE)
6088 statement = cp_parser_compound_statement (parser, NULL, false);
6089 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6090 a statement all its own. */
6091 else if (token->type == CPP_PRAGMA)
6093 cp_lexer_handle_pragma (parser->lexer);
6097 /* Everything else must be a declaration-statement or an
6098 expression-statement. Try for the declaration-statement
6099 first, unless we are looking at a `;', in which case we know that
6100 we have an expression-statement. */
6103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6105 cp_parser_parse_tentatively (parser);
6106 /* Try to parse the declaration-statement. */
6107 cp_parser_declaration_statement (parser);
6108 /* If that worked, we're done. */
6109 if (cp_parser_parse_definitely (parser))
6112 /* Look for an expression-statement instead. */
6113 statement = cp_parser_expression_statement (parser, in_statement_expr);
6116 /* Set the line number for the statement. */
6117 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6118 SET_EXPR_LOCATION (statement, statement_location);
6121 /* Parse a labeled-statement.
6124 identifier : statement
6125 case constant-expression : statement
6131 case constant-expression ... constant-expression : statement
6133 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6134 For an ordinary label, returns a LABEL_EXPR. */
6137 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6140 tree statement = error_mark_node;
6142 /* The next token should be an identifier. */
6143 token = cp_lexer_peek_token (parser->lexer);
6144 if (token->type != CPP_NAME
6145 && token->type != CPP_KEYWORD)
6147 cp_parser_error (parser, "expected labeled-statement");
6148 return error_mark_node;
6151 switch (token->keyword)
6158 /* Consume the `case' token. */
6159 cp_lexer_consume_token (parser->lexer);
6160 /* Parse the constant-expression. */
6161 expr = cp_parser_constant_expression (parser,
6162 /*allow_non_constant_p=*/false,
6165 ellipsis = cp_lexer_peek_token (parser->lexer);
6166 if (ellipsis->type == CPP_ELLIPSIS)
6168 /* Consume the `...' token. */
6169 cp_lexer_consume_token (parser->lexer);
6171 cp_parser_constant_expression (parser,
6172 /*allow_non_constant_p=*/false,
6174 /* We don't need to emit warnings here, as the common code
6175 will do this for us. */
6178 expr_hi = NULL_TREE;
6180 if (!parser->in_switch_statement_p)
6181 error ("case label %qE not within a switch statement", expr);
6183 statement = finish_case_label (expr, expr_hi);
6188 /* Consume the `default' token. */
6189 cp_lexer_consume_token (parser->lexer);
6190 if (!parser->in_switch_statement_p)
6191 error ("case label not within a switch statement");
6193 statement = finish_case_label (NULL_TREE, NULL_TREE);
6197 /* Anything else must be an ordinary label. */
6198 statement = finish_label_stmt (cp_parser_identifier (parser));
6202 /* Require the `:' token. */
6203 cp_parser_require (parser, CPP_COLON, "`:'");
6204 /* Parse the labeled statement. */
6205 cp_parser_statement (parser, in_statement_expr);
6207 /* Return the label, in the case of a `case' or `default' label. */
6211 /* Parse an expression-statement.
6213 expression-statement:
6216 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6217 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6218 indicates whether this expression-statement is part of an
6219 expression statement. */
6222 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6224 tree statement = NULL_TREE;
6226 /* If the next token is a ';', then there is no expression
6228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6229 statement = cp_parser_expression (parser, /*cast_p=*/false);
6231 /* Consume the final `;'. */
6232 cp_parser_consume_semicolon_at_end_of_statement (parser);
6234 if (in_statement_expr
6235 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6236 /* This is the final expression statement of a statement
6238 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6240 statement = finish_expr_stmt (statement);
6247 /* Parse a compound-statement.
6250 { statement-seq [opt] }
6252 Returns a tree representing the statement. */
6255 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6260 /* Consume the `{'. */
6261 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6262 return error_mark_node;
6263 /* Begin the compound-statement. */
6264 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6265 /* Parse an (optional) statement-seq. */
6266 cp_parser_statement_seq_opt (parser, in_statement_expr);
6267 /* Finish the compound-statement. */
6268 finish_compound_stmt (compound_stmt);
6269 /* Consume the `}'. */
6270 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6272 return compound_stmt;
6275 /* Parse an (optional) statement-seq.
6279 statement-seq [opt] statement */
6282 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6284 /* Scan statements until there aren't any more. */
6287 /* If we're looking at a `}', then we've run out of statements. */
6288 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6289 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6292 /* Parse the statement. */
6293 cp_parser_statement (parser, in_statement_expr);
6297 /* Parse a selection-statement.
6299 selection-statement:
6300 if ( condition ) statement
6301 if ( condition ) statement else statement
6302 switch ( condition ) statement
6304 Returns the new IF_STMT or SWITCH_STMT. */
6307 cp_parser_selection_statement (cp_parser* parser)
6312 /* Peek at the next token. */
6313 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6315 /* See what kind of keyword it is. */
6316 keyword = token->keyword;
6325 /* Look for the `('. */
6326 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6328 cp_parser_skip_to_end_of_statement (parser);
6329 return error_mark_node;
6332 /* Begin the selection-statement. */
6333 if (keyword == RID_IF)
6334 statement = begin_if_stmt ();
6336 statement = begin_switch_stmt ();
6338 /* Parse the condition. */
6339 condition = cp_parser_condition (parser);
6340 /* Look for the `)'. */
6341 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6342 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6343 /*consume_paren=*/true);
6345 if (keyword == RID_IF)
6347 /* Add the condition. */
6348 finish_if_stmt_cond (condition, statement);
6350 /* Parse the then-clause. */
6351 cp_parser_implicitly_scoped_statement (parser);
6352 finish_then_clause (statement);
6354 /* If the next token is `else', parse the else-clause. */
6355 if (cp_lexer_next_token_is_keyword (parser->lexer,
6358 /* Consume the `else' keyword. */
6359 cp_lexer_consume_token (parser->lexer);
6360 begin_else_clause (statement);
6361 /* Parse the else-clause. */
6362 cp_parser_implicitly_scoped_statement (parser);
6363 finish_else_clause (statement);
6366 /* Now we're all done with the if-statement. */
6367 finish_if_stmt (statement);
6371 bool in_switch_statement_p;
6373 /* Add the condition. */
6374 finish_switch_cond (condition, statement);
6376 /* Parse the body of the switch-statement. */
6377 in_switch_statement_p = parser->in_switch_statement_p;
6378 parser->in_switch_statement_p = true;
6379 cp_parser_implicitly_scoped_statement (parser);
6380 parser->in_switch_statement_p = in_switch_statement_p;
6382 /* Now we're all done with the switch-statement. */
6383 finish_switch_stmt (statement);
6391 cp_parser_error (parser, "expected selection-statement");
6392 return error_mark_node;
6396 /* Parse a condition.
6400 type-specifier-seq declarator = assignment-expression
6405 type-specifier-seq declarator asm-specification [opt]
6406 attributes [opt] = assignment-expression
6408 Returns the expression that should be tested. */
6411 cp_parser_condition (cp_parser* parser)
6413 cp_decl_specifier_seq type_specifiers;
6414 const char *saved_message;
6416 /* Try the declaration first. */
6417 cp_parser_parse_tentatively (parser);
6418 /* New types are not allowed in the type-specifier-seq for a
6420 saved_message = parser->type_definition_forbidden_message;
6421 parser->type_definition_forbidden_message
6422 = "types may not be defined in conditions";
6423 /* Parse the type-specifier-seq. */
6424 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6426 /* Restore the saved message. */
6427 parser->type_definition_forbidden_message = saved_message;
6428 /* If all is well, we might be looking at a declaration. */
6429 if (!cp_parser_error_occurred (parser))
6432 tree asm_specification;
6434 cp_declarator *declarator;
6435 tree initializer = NULL_TREE;
6437 /* Parse the declarator. */
6438 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6439 /*ctor_dtor_or_conv_p=*/NULL,
6440 /*parenthesized_p=*/NULL,
6441 /*member_p=*/false);
6442 /* Parse the attributes. */
6443 attributes = cp_parser_attributes_opt (parser);
6444 /* Parse the asm-specification. */
6445 asm_specification = cp_parser_asm_specification_opt (parser);
6446 /* If the next token is not an `=', then we might still be
6447 looking at an expression. For example:
6451 looks like a decl-specifier-seq and a declarator -- but then
6452 there is no `=', so this is an expression. */
6453 cp_parser_require (parser, CPP_EQ, "`='");
6454 /* If we did see an `=', then we are looking at a declaration
6456 if (cp_parser_parse_definitely (parser))
6460 /* Create the declaration. */
6461 decl = start_decl (declarator, &type_specifiers,
6462 /*initialized_p=*/true,
6463 attributes, /*prefix_attributes=*/NULL_TREE,
6465 /* Parse the assignment-expression. */
6466 initializer = cp_parser_assignment_expression (parser,
6469 /* Process the initializer. */
6470 cp_finish_decl (decl,
6473 LOOKUP_ONLYCONVERTING);
6476 pop_scope (pushed_scope);
6478 return convert_from_reference (decl);
6481 /* If we didn't even get past the declarator successfully, we are
6482 definitely not looking at a declaration. */
6484 cp_parser_abort_tentative_parse (parser);
6486 /* Otherwise, we are looking at an expression. */
6487 return cp_parser_expression (parser, /*cast_p=*/false);
6490 /* Parse an iteration-statement.
6492 iteration-statement:
6493 while ( condition ) statement
6494 do statement while ( expression ) ;
6495 for ( for-init-statement condition [opt] ; expression [opt] )
6498 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6501 cp_parser_iteration_statement (cp_parser* parser)
6506 bool in_iteration_statement_p;
6509 /* Peek at the next token. */
6510 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6512 return error_mark_node;
6514 /* Remember whether or not we are already within an iteration
6516 in_iteration_statement_p = parser->in_iteration_statement_p;
6518 /* See what kind of keyword it is. */
6519 keyword = token->keyword;
6526 /* Begin the while-statement. */
6527 statement = begin_while_stmt ();
6528 /* Look for the `('. */
6529 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6530 /* Parse the condition. */
6531 condition = cp_parser_condition (parser);
6532 finish_while_stmt_cond (condition, statement);
6533 /* Look for the `)'. */
6534 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6535 /* Parse the dependent statement. */
6536 parser->in_iteration_statement_p = true;
6537 cp_parser_already_scoped_statement (parser);
6538 parser->in_iteration_statement_p = in_iteration_statement_p;
6539 /* We're done with the while-statement. */
6540 finish_while_stmt (statement);
6548 /* Begin the do-statement. */
6549 statement = begin_do_stmt ();
6550 /* Parse the body of the do-statement. */
6551 parser->in_iteration_statement_p = true;
6552 cp_parser_implicitly_scoped_statement (parser);
6553 parser->in_iteration_statement_p = in_iteration_statement_p;
6554 finish_do_body (statement);
6555 /* Look for the `while' keyword. */
6556 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6557 /* Look for the `('. */
6558 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6559 /* Parse the expression. */
6560 expression = cp_parser_expression (parser, /*cast_p=*/false);
6561 /* We're done with the do-statement. */
6562 finish_do_stmt (expression, statement);
6563 /* Look for the `)'. */
6564 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6565 /* Look for the `;'. */
6566 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6572 tree condition = NULL_TREE;
6573 tree expression = NULL_TREE;
6575 /* Begin the for-statement. */
6576 statement = begin_for_stmt ();
6577 /* Look for the `('. */
6578 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6579 /* Parse the initialization. */
6580 cp_parser_for_init_statement (parser);
6581 finish_for_init_stmt (statement);
6583 /* If there's a condition, process it. */
6584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6585 condition = cp_parser_condition (parser);
6586 finish_for_cond (condition, statement);
6587 /* Look for the `;'. */
6588 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6590 /* If there's an expression, process it. */
6591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6592 expression = cp_parser_expression (parser, /*cast_p=*/false);
6593 finish_for_expr (expression, statement);
6594 /* Look for the `)'. */
6595 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6597 /* Parse the body of the for-statement. */
6598 parser->in_iteration_statement_p = true;
6599 cp_parser_already_scoped_statement (parser);
6600 parser->in_iteration_statement_p = in_iteration_statement_p;
6602 /* We're done with the for-statement. */
6603 finish_for_stmt (statement);
6608 cp_parser_error (parser, "expected iteration-statement");
6609 statement = error_mark_node;
6616 /* Parse a for-init-statement.
6619 expression-statement
6620 simple-declaration */
6623 cp_parser_for_init_statement (cp_parser* parser)
6625 /* If the next token is a `;', then we have an empty
6626 expression-statement. Grammatically, this is also a
6627 simple-declaration, but an invalid one, because it does not
6628 declare anything. Therefore, if we did not handle this case
6629 specially, we would issue an error message about an invalid
6631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6633 /* We're going to speculatively look for a declaration, falling back
6634 to an expression, if necessary. */
6635 cp_parser_parse_tentatively (parser);
6636 /* Parse the declaration. */
6637 cp_parser_simple_declaration (parser,
6638 /*function_definition_allowed_p=*/false);
6639 /* If the tentative parse failed, then we shall need to look for an
6640 expression-statement. */
6641 if (cp_parser_parse_definitely (parser))
6645 cp_parser_expression_statement (parser, false);
6648 /* Parse a jump-statement.
6653 return expression [opt] ;
6661 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6664 cp_parser_jump_statement (cp_parser* parser)
6666 tree statement = error_mark_node;
6670 /* Peek at the next token. */
6671 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6673 return error_mark_node;
6675 /* See what kind of keyword it is. */
6676 keyword = token->keyword;
6680 if (!parser->in_switch_statement_p
6681 && !parser->in_iteration_statement_p)
6683 error ("break statement not within loop or switch");
6684 statement = error_mark_node;
6687 statement = finish_break_stmt ();
6688 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6692 if (!parser->in_iteration_statement_p)
6694 error ("continue statement not within a loop");
6695 statement = error_mark_node;
6698 statement = finish_continue_stmt ();
6699 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6706 /* If the next token is a `;', then there is no
6708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6709 expr = cp_parser_expression (parser, /*cast_p=*/false);
6712 /* Build the return-statement. */
6713 statement = finish_return_stmt (expr);
6714 /* Look for the final `;'. */
6715 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6720 /* Create the goto-statement. */
6721 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6723 /* Issue a warning about this use of a GNU extension. */
6725 pedwarn ("ISO C++ forbids computed gotos");
6726 /* Consume the '*' token. */
6727 cp_lexer_consume_token (parser->lexer);
6728 /* Parse the dependent expression. */
6729 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6732 finish_goto_stmt (cp_parser_identifier (parser));
6733 /* Look for the final `;'. */
6734 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6738 cp_parser_error (parser, "expected jump-statement");
6745 /* Parse a declaration-statement.
6747 declaration-statement:
6748 block-declaration */
6751 cp_parser_declaration_statement (cp_parser* parser)
6755 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6756 p = obstack_alloc (&declarator_obstack, 0);
6758 /* Parse the block-declaration. */
6759 cp_parser_block_declaration (parser, /*statement_p=*/true);
6761 /* Free any declarators allocated. */
6762 obstack_free (&declarator_obstack, p);
6764 /* Finish off the statement. */
6768 /* Some dependent statements (like `if (cond) statement'), are
6769 implicitly in their own scope. In other words, if the statement is
6770 a single statement (as opposed to a compound-statement), it is
6771 none-the-less treated as if it were enclosed in braces. Any
6772 declarations appearing in the dependent statement are out of scope
6773 after control passes that point. This function parses a statement,
6774 but ensures that is in its own scope, even if it is not a
6777 Returns the new statement. */
6780 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6784 /* If the token is not a `{', then we must take special action. */
6785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6787 /* Create a compound-statement. */
6788 statement = begin_compound_stmt (0);
6789 /* Parse the dependent-statement. */
6790 cp_parser_statement (parser, false);
6791 /* Finish the dummy compound-statement. */
6792 finish_compound_stmt (statement);
6794 /* Otherwise, we simply parse the statement directly. */
6796 statement = cp_parser_compound_statement (parser, NULL, false);
6798 /* Return the statement. */
6802 /* For some dependent statements (like `while (cond) statement'), we
6803 have already created a scope. Therefore, even if the dependent
6804 statement is a compound-statement, we do not want to create another
6808 cp_parser_already_scoped_statement (cp_parser* parser)
6810 /* If the token is a `{', then we must take special action. */
6811 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6812 cp_parser_statement (parser, false);
6815 /* Avoid calling cp_parser_compound_statement, so that we
6816 don't create a new scope. Do everything else by hand. */
6817 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6818 cp_parser_statement_seq_opt (parser, false);
6819 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6823 /* Declarations [gram.dcl.dcl] */
6825 /* Parse an optional declaration-sequence.
6829 declaration-seq declaration */
6832 cp_parser_declaration_seq_opt (cp_parser* parser)
6838 token = cp_lexer_peek_token (parser->lexer);
6840 if (token->type == CPP_CLOSE_BRACE
6841 || token->type == CPP_EOF)
6844 if (token->type == CPP_SEMICOLON)
6846 /* A declaration consisting of a single semicolon is
6847 invalid. Allow it unless we're being pedantic. */
6848 cp_lexer_consume_token (parser->lexer);
6849 if (pedantic && !in_system_header)
6850 pedwarn ("extra %<;%>");
6854 /* If we're entering or exiting a region that's implicitly
6855 extern "C", modify the lang context appropriately. */
6856 if (!parser->implicit_extern_c && token->implicit_extern_c)
6858 push_lang_context (lang_name_c);
6859 parser->implicit_extern_c = true;
6861 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6863 pop_lang_context ();
6864 parser->implicit_extern_c = false;
6867 if (token->type == CPP_PRAGMA)
6869 /* A top-level declaration can consist solely of a #pragma.
6870 A nested declaration cannot, so this is done here and not
6871 in cp_parser_declaration. (A #pragma at block scope is
6872 handled in cp_parser_statement.) */
6873 cp_lexer_handle_pragma (parser->lexer);
6877 /* Parse the declaration itself. */
6878 cp_parser_declaration (parser);
6882 /* Parse a declaration.
6887 template-declaration
6888 explicit-instantiation
6889 explicit-specialization
6890 linkage-specification
6891 namespace-definition
6896 __extension__ declaration */
6899 cp_parser_declaration (cp_parser* parser)
6906 /* Check for the `__extension__' keyword. */
6907 if (cp_parser_extension_opt (parser, &saved_pedantic))
6909 /* Parse the qualified declaration. */
6910 cp_parser_declaration (parser);
6911 /* Restore the PEDANTIC flag. */
6912 pedantic = saved_pedantic;
6917 /* Try to figure out what kind of declaration is present. */
6918 token1 = *cp_lexer_peek_token (parser->lexer);
6920 if (token1.type != CPP_EOF)
6921 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6923 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6924 p = obstack_alloc (&declarator_obstack, 0);
6926 /* If the next token is `extern' and the following token is a string
6927 literal, then we have a linkage specification. */
6928 if (token1.keyword == RID_EXTERN
6929 && cp_parser_is_string_literal (&token2))
6930 cp_parser_linkage_specification (parser);
6931 /* If the next token is `template', then we have either a template
6932 declaration, an explicit instantiation, or an explicit
6934 else if (token1.keyword == RID_TEMPLATE)
6936 /* `template <>' indicates a template specialization. */
6937 if (token2.type == CPP_LESS
6938 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6939 cp_parser_explicit_specialization (parser);
6940 /* `template <' indicates a template declaration. */
6941 else if (token2.type == CPP_LESS)
6942 cp_parser_template_declaration (parser, /*member_p=*/false);
6943 /* Anything else must be an explicit instantiation. */
6945 cp_parser_explicit_instantiation (parser);
6947 /* If the next token is `export', then we have a template
6949 else if (token1.keyword == RID_EXPORT)
6950 cp_parser_template_declaration (parser, /*member_p=*/false);
6951 /* If the next token is `extern', 'static' or 'inline' and the one
6952 after that is `template', we have a GNU extended explicit
6953 instantiation directive. */
6954 else if (cp_parser_allow_gnu_extensions_p (parser)
6955 && (token1.keyword == RID_EXTERN
6956 || token1.keyword == RID_STATIC
6957 || token1.keyword == RID_INLINE)
6958 && token2.keyword == RID_TEMPLATE)
6959 cp_parser_explicit_instantiation (parser);
6960 /* If the next token is `namespace', check for a named or unnamed
6961 namespace definition. */
6962 else if (token1.keyword == RID_NAMESPACE
6963 && (/* A named namespace definition. */
6964 (token2.type == CPP_NAME
6965 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6967 /* An unnamed namespace definition. */
6968 || token2.type == CPP_OPEN_BRACE))
6969 cp_parser_namespace_definition (parser);
6970 /* Objective-C++ declaration/definition. */
6971 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6972 cp_parser_objc_declaration (parser);
6973 /* We must have either a block declaration or a function
6976 /* Try to parse a block-declaration, or a function-definition. */
6977 cp_parser_block_declaration (parser, /*statement_p=*/false);
6979 /* Free any declarators allocated. */
6980 obstack_free (&declarator_obstack, p);
6983 /* Parse a block-declaration.
6988 namespace-alias-definition
6995 __extension__ block-declaration
6998 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6999 part of a declaration-statement. */
7002 cp_parser_block_declaration (cp_parser *parser,
7008 /* Check for the `__extension__' keyword. */
7009 if (cp_parser_extension_opt (parser, &saved_pedantic))
7011 /* Parse the qualified declaration. */
7012 cp_parser_block_declaration (parser, statement_p);
7013 /* Restore the PEDANTIC flag. */
7014 pedantic = saved_pedantic;
7019 /* Peek at the next token to figure out which kind of declaration is
7021 token1 = cp_lexer_peek_token (parser->lexer);
7023 /* If the next keyword is `asm', we have an asm-definition. */
7024 if (token1->keyword == RID_ASM)
7027 cp_parser_commit_to_tentative_parse (parser);
7028 cp_parser_asm_definition (parser);
7030 /* If the next keyword is `namespace', we have a
7031 namespace-alias-definition. */
7032 else if (token1->keyword == RID_NAMESPACE)
7033 cp_parser_namespace_alias_definition (parser);
7034 /* If the next keyword is `using', we have either a
7035 using-declaration or a using-directive. */
7036 else if (token1->keyword == RID_USING)
7041 cp_parser_commit_to_tentative_parse (parser);
7042 /* If the token after `using' is `namespace', then we have a
7044 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7045 if (token2->keyword == RID_NAMESPACE)
7046 cp_parser_using_directive (parser);
7047 /* Otherwise, it's a using-declaration. */
7049 cp_parser_using_declaration (parser);
7051 /* If the next keyword is `__label__' we have a label declaration. */
7052 else if (token1->keyword == RID_LABEL)
7055 cp_parser_commit_to_tentative_parse (parser);
7056 cp_parser_label_declaration (parser);
7058 /* Anything else must be a simple-declaration. */
7060 cp_parser_simple_declaration (parser, !statement_p);
7063 /* Parse a simple-declaration.
7066 decl-specifier-seq [opt] init-declarator-list [opt] ;
7068 init-declarator-list:
7070 init-declarator-list , init-declarator
7072 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7073 function-definition as a simple-declaration. */
7076 cp_parser_simple_declaration (cp_parser* parser,
7077 bool function_definition_allowed_p)
7079 cp_decl_specifier_seq decl_specifiers;
7080 int declares_class_or_enum;
7081 bool saw_declarator;
7083 /* Defer access checks until we know what is being declared; the
7084 checks for names appearing in the decl-specifier-seq should be
7085 done as if we were in the scope of the thing being declared. */
7086 push_deferring_access_checks (dk_deferred);
7088 /* Parse the decl-specifier-seq. We have to keep track of whether
7089 or not the decl-specifier-seq declares a named class or
7090 enumeration type, since that is the only case in which the
7091 init-declarator-list is allowed to be empty.
7095 In a simple-declaration, the optional init-declarator-list can be
7096 omitted only when declaring a class or enumeration, that is when
7097 the decl-specifier-seq contains either a class-specifier, an
7098 elaborated-type-specifier, or an enum-specifier. */
7099 cp_parser_decl_specifier_seq (parser,
7100 CP_PARSER_FLAGS_OPTIONAL,
7102 &declares_class_or_enum);
7103 /* We no longer need to defer access checks. */
7104 stop_deferring_access_checks ();
7106 /* In a block scope, a valid declaration must always have a
7107 decl-specifier-seq. By not trying to parse declarators, we can
7108 resolve the declaration/expression ambiguity more quickly. */
7109 if (!function_definition_allowed_p
7110 && !decl_specifiers.any_specifiers_p)
7112 cp_parser_error (parser, "expected declaration");
7116 /* If the next two tokens are both identifiers, the code is
7117 erroneous. The usual cause of this situation is code like:
7121 where "T" should name a type -- but does not. */
7122 if (!decl_specifiers.type
7123 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7125 /* If parsing tentatively, we should commit; we really are
7126 looking at a declaration. */
7127 cp_parser_commit_to_tentative_parse (parser);
7132 /* If we have seen at least one decl-specifier, and the next token
7133 is not a parenthesis, then we must be looking at a declaration.
7134 (After "int (" we might be looking at a functional cast.) */
7135 if (decl_specifiers.any_specifiers_p
7136 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7137 cp_parser_commit_to_tentative_parse (parser);
7139 /* Keep going until we hit the `;' at the end of the simple
7141 saw_declarator = false;
7142 while (cp_lexer_next_token_is_not (parser->lexer,
7146 bool function_definition_p;
7149 saw_declarator = true;
7150 /* Parse the init-declarator. */
7151 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7152 function_definition_allowed_p,
7154 declares_class_or_enum,
7155 &function_definition_p);
7156 /* If an error occurred while parsing tentatively, exit quickly.
7157 (That usually happens when in the body of a function; each
7158 statement is treated as a declaration-statement until proven
7160 if (cp_parser_error_occurred (parser))
7162 /* Handle function definitions specially. */
7163 if (function_definition_p)
7165 /* If the next token is a `,', then we are probably
7166 processing something like:
7170 which is erroneous. */
7171 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7172 error ("mixing declarations and function-definitions is forbidden");
7173 /* Otherwise, we're done with the list of declarators. */
7176 pop_deferring_access_checks ();
7180 /* The next token should be either a `,' or a `;'. */
7181 token = cp_lexer_peek_token (parser->lexer);
7182 /* If it's a `,', there are more declarators to come. */
7183 if (token->type == CPP_COMMA)
7184 cp_lexer_consume_token (parser->lexer);
7185 /* If it's a `;', we are done. */
7186 else if (token->type == CPP_SEMICOLON)
7188 /* Anything else is an error. */
7191 /* If we have already issued an error message we don't need
7192 to issue another one. */
7193 if (decl != error_mark_node
7194 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7195 cp_parser_error (parser, "expected %<,%> or %<;%>");
7196 /* Skip tokens until we reach the end of the statement. */
7197 cp_parser_skip_to_end_of_statement (parser);
7198 /* If the next token is now a `;', consume it. */
7199 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7200 cp_lexer_consume_token (parser->lexer);
7203 /* After the first time around, a function-definition is not
7204 allowed -- even if it was OK at first. For example:
7209 function_definition_allowed_p = false;
7212 /* Issue an error message if no declarators are present, and the
7213 decl-specifier-seq does not itself declare a class or
7215 if (!saw_declarator)
7217 if (cp_parser_declares_only_class_p (parser))
7218 shadow_tag (&decl_specifiers);
7219 /* Perform any deferred access checks. */
7220 perform_deferred_access_checks ();
7223 /* Consume the `;'. */
7224 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7227 pop_deferring_access_checks ();
7230 /* Parse a decl-specifier-seq.
7233 decl-specifier-seq [opt] decl-specifier
7236 storage-class-specifier
7247 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7249 The parser flags FLAGS is used to control type-specifier parsing.
7251 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7254 1: one of the decl-specifiers is an elaborated-type-specifier
7255 (i.e., a type declaration)
7256 2: one of the decl-specifiers is an enum-specifier or a
7257 class-specifier (i.e., a type definition)
7262 cp_parser_decl_specifier_seq (cp_parser* parser,
7263 cp_parser_flags flags,
7264 cp_decl_specifier_seq *decl_specs,
7265 int* declares_class_or_enum)
7267 bool constructor_possible_p = !parser->in_declarator_p;
7269 /* Clear DECL_SPECS. */
7270 clear_decl_specs (decl_specs);
7272 /* Assume no class or enumeration type is declared. */
7273 *declares_class_or_enum = 0;
7275 /* Keep reading specifiers until there are no more to read. */
7279 bool found_decl_spec;
7282 /* Peek at the next token. */
7283 token = cp_lexer_peek_token (parser->lexer);
7284 /* Handle attributes. */
7285 if (token->keyword == RID_ATTRIBUTE)
7287 /* Parse the attributes. */
7288 decl_specs->attributes
7289 = chainon (decl_specs->attributes,
7290 cp_parser_attributes_opt (parser));
7293 /* Assume we will find a decl-specifier keyword. */
7294 found_decl_spec = true;
7295 /* If the next token is an appropriate keyword, we can simply
7296 add it to the list. */
7297 switch (token->keyword)
7302 if (decl_specs->specs[(int) ds_friend]++)
7303 error ("duplicate %<friend%>");
7304 /* Consume the token. */
7305 cp_lexer_consume_token (parser->lexer);
7308 /* function-specifier:
7315 cp_parser_function_specifier_opt (parser, decl_specs);
7321 ++decl_specs->specs[(int) ds_typedef];
7322 /* Consume the token. */
7323 cp_lexer_consume_token (parser->lexer);
7324 /* A constructor declarator cannot appear in a typedef. */
7325 constructor_possible_p = false;
7326 /* The "typedef" keyword can only occur in a declaration; we
7327 may as well commit at this point. */
7328 cp_parser_commit_to_tentative_parse (parser);
7331 /* storage-class-specifier:
7341 /* Consume the token. */
7342 cp_lexer_consume_token (parser->lexer);
7343 cp_parser_set_storage_class (decl_specs, sc_auto);
7346 /* Consume the token. */
7347 cp_lexer_consume_token (parser->lexer);
7348 cp_parser_set_storage_class (decl_specs, sc_register);
7351 /* Consume the token. */
7352 cp_lexer_consume_token (parser->lexer);
7353 if (decl_specs->specs[(int) ds_thread])
7355 error ("%<__thread%> before %<static%>");
7356 decl_specs->specs[(int) ds_thread] = 0;
7358 cp_parser_set_storage_class (decl_specs, sc_static);
7361 /* Consume the token. */
7362 cp_lexer_consume_token (parser->lexer);
7363 if (decl_specs->specs[(int) ds_thread])
7365 error ("%<__thread%> before %<extern%>");
7366 decl_specs->specs[(int) ds_thread] = 0;
7368 cp_parser_set_storage_class (decl_specs, sc_extern);
7371 /* Consume the token. */
7372 cp_lexer_consume_token (parser->lexer);
7373 cp_parser_set_storage_class (decl_specs, sc_mutable);
7376 /* Consume the token. */
7377 cp_lexer_consume_token (parser->lexer);
7378 ++decl_specs->specs[(int) ds_thread];
7382 /* We did not yet find a decl-specifier yet. */
7383 found_decl_spec = false;
7387 /* Constructors are a special case. The `S' in `S()' is not a
7388 decl-specifier; it is the beginning of the declarator. */
7391 && constructor_possible_p
7392 && (cp_parser_constructor_declarator_p
7393 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7395 /* If we don't have a DECL_SPEC yet, then we must be looking at
7396 a type-specifier. */
7397 if (!found_decl_spec && !constructor_p)
7399 int decl_spec_declares_class_or_enum;
7400 bool is_cv_qualifier;
7404 = cp_parser_type_specifier (parser, flags,
7406 /*is_declaration=*/true,
7407 &decl_spec_declares_class_or_enum,
7410 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7412 /* If this type-specifier referenced a user-defined type
7413 (a typedef, class-name, etc.), then we can't allow any
7414 more such type-specifiers henceforth.
7418 The longest sequence of decl-specifiers that could
7419 possibly be a type name is taken as the
7420 decl-specifier-seq of a declaration. The sequence shall
7421 be self-consistent as described below.
7425 As a general rule, at most one type-specifier is allowed
7426 in the complete decl-specifier-seq of a declaration. The
7427 only exceptions are the following:
7429 -- const or volatile can be combined with any other
7432 -- signed or unsigned can be combined with char, long,
7440 void g (const int Pc);
7442 Here, Pc is *not* part of the decl-specifier seq; it's
7443 the declarator. Therefore, once we see a type-specifier
7444 (other than a cv-qualifier), we forbid any additional
7445 user-defined types. We *do* still allow things like `int
7446 int' to be considered a decl-specifier-seq, and issue the
7447 error message later. */
7448 if (type_spec && !is_cv_qualifier)
7449 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7450 /* A constructor declarator cannot follow a type-specifier. */
7453 constructor_possible_p = false;
7454 found_decl_spec = true;
7458 /* If we still do not have a DECL_SPEC, then there are no more
7460 if (!found_decl_spec)
7463 decl_specs->any_specifiers_p = true;
7464 /* After we see one decl-specifier, further decl-specifiers are
7466 flags |= CP_PARSER_FLAGS_OPTIONAL;
7469 /* Don't allow a friend specifier with a class definition. */
7470 if (decl_specs->specs[(int) ds_friend] != 0
7471 && (*declares_class_or_enum & 2))
7472 error ("class definition may not be declared a friend");
7475 /* Parse an (optional) storage-class-specifier.
7477 storage-class-specifier:
7486 storage-class-specifier:
7489 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7492 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7494 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7502 /* Consume the token. */
7503 return cp_lexer_consume_token (parser->lexer)->value;
7510 /* Parse an (optional) function-specifier.
7517 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7518 Updates DECL_SPECS, if it is non-NULL. */
7521 cp_parser_function_specifier_opt (cp_parser* parser,
7522 cp_decl_specifier_seq *decl_specs)
7524 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7528 ++decl_specs->specs[(int) ds_inline];
7533 ++decl_specs->specs[(int) ds_virtual];
7538 ++decl_specs->specs[(int) ds_explicit];
7545 /* Consume the token. */
7546 return cp_lexer_consume_token (parser->lexer)->value;
7549 /* Parse a linkage-specification.
7551 linkage-specification:
7552 extern string-literal { declaration-seq [opt] }
7553 extern string-literal declaration */
7556 cp_parser_linkage_specification (cp_parser* parser)
7560 /* Look for the `extern' keyword. */
7561 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7563 /* Look for the string-literal. */
7564 linkage = cp_parser_string_literal (parser, false, false);
7566 /* Transform the literal into an identifier. If the literal is a
7567 wide-character string, or contains embedded NULs, then we can't
7568 handle it as the user wants. */
7569 if (strlen (TREE_STRING_POINTER (linkage))
7570 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7572 cp_parser_error (parser, "invalid linkage-specification");
7573 /* Assume C++ linkage. */
7574 linkage = lang_name_cplusplus;
7577 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7579 /* We're now using the new linkage. */
7580 push_lang_context (linkage);
7582 /* If the next token is a `{', then we're using the first
7584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7586 /* Consume the `{' token. */
7587 cp_lexer_consume_token (parser->lexer);
7588 /* Parse the declarations. */
7589 cp_parser_declaration_seq_opt (parser);
7590 /* Look for the closing `}'. */
7591 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7593 /* Otherwise, there's just one declaration. */
7596 bool saved_in_unbraced_linkage_specification_p;
7598 saved_in_unbraced_linkage_specification_p
7599 = parser->in_unbraced_linkage_specification_p;
7600 parser->in_unbraced_linkage_specification_p = true;
7601 have_extern_spec = true;
7602 cp_parser_declaration (parser);
7603 have_extern_spec = false;
7604 parser->in_unbraced_linkage_specification_p
7605 = saved_in_unbraced_linkage_specification_p;
7608 /* We're done with the linkage-specification. */
7609 pop_lang_context ();
7612 /* Special member functions [gram.special] */
7614 /* Parse a conversion-function-id.
7616 conversion-function-id:
7617 operator conversion-type-id
7619 Returns an IDENTIFIER_NODE representing the operator. */
7622 cp_parser_conversion_function_id (cp_parser* parser)
7626 tree saved_qualifying_scope;
7627 tree saved_object_scope;
7628 tree pushed_scope = NULL_TREE;
7630 /* Look for the `operator' token. */
7631 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7632 return error_mark_node;
7633 /* When we parse the conversion-type-id, the current scope will be
7634 reset. However, we need that information in able to look up the
7635 conversion function later, so we save it here. */
7636 saved_scope = parser->scope;
7637 saved_qualifying_scope = parser->qualifying_scope;
7638 saved_object_scope = parser->object_scope;
7639 /* We must enter the scope of the class so that the names of
7640 entities declared within the class are available in the
7641 conversion-type-id. For example, consider:
7648 S::operator I() { ... }
7650 In order to see that `I' is a type-name in the definition, we
7651 must be in the scope of `S'. */
7653 pushed_scope = push_scope (saved_scope);
7654 /* Parse the conversion-type-id. */
7655 type = cp_parser_conversion_type_id (parser);
7656 /* Leave the scope of the class, if any. */
7658 pop_scope (pushed_scope);
7659 /* Restore the saved scope. */
7660 parser->scope = saved_scope;
7661 parser->qualifying_scope = saved_qualifying_scope;
7662 parser->object_scope = saved_object_scope;
7663 /* If the TYPE is invalid, indicate failure. */
7664 if (type == error_mark_node)
7665 return error_mark_node;
7666 return mangle_conv_op_name_for_type (type);
7669 /* Parse a conversion-type-id:
7672 type-specifier-seq conversion-declarator [opt]
7674 Returns the TYPE specified. */
7677 cp_parser_conversion_type_id (cp_parser* parser)
7680 cp_decl_specifier_seq type_specifiers;
7681 cp_declarator *declarator;
7682 tree type_specified;
7684 /* Parse the attributes. */
7685 attributes = cp_parser_attributes_opt (parser);
7686 /* Parse the type-specifiers. */
7687 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7689 /* If that didn't work, stop. */
7690 if (type_specifiers.type == error_mark_node)
7691 return error_mark_node;
7692 /* Parse the conversion-declarator. */
7693 declarator = cp_parser_conversion_declarator_opt (parser);
7695 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7696 /*initialized=*/0, &attributes);
7698 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7699 return type_specified;
7702 /* Parse an (optional) conversion-declarator.
7704 conversion-declarator:
7705 ptr-operator conversion-declarator [opt]
7709 static cp_declarator *
7710 cp_parser_conversion_declarator_opt (cp_parser* parser)
7712 enum tree_code code;
7714 cp_cv_quals cv_quals;
7716 /* We don't know if there's a ptr-operator next, or not. */
7717 cp_parser_parse_tentatively (parser);
7718 /* Try the ptr-operator. */
7719 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7720 /* If it worked, look for more conversion-declarators. */
7721 if (cp_parser_parse_definitely (parser))
7723 cp_declarator *declarator;
7725 /* Parse another optional declarator. */
7726 declarator = cp_parser_conversion_declarator_opt (parser);
7728 /* Create the representation of the declarator. */
7730 declarator = make_ptrmem_declarator (cv_quals, class_type,
7732 else if (code == INDIRECT_REF)
7733 declarator = make_pointer_declarator (cv_quals, declarator);
7735 declarator = make_reference_declarator (cv_quals, declarator);
7743 /* Parse an (optional) ctor-initializer.
7746 : mem-initializer-list
7748 Returns TRUE iff the ctor-initializer was actually present. */
7751 cp_parser_ctor_initializer_opt (cp_parser* parser)
7753 /* If the next token is not a `:', then there is no
7754 ctor-initializer. */
7755 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7757 /* Do default initialization of any bases and members. */
7758 if (DECL_CONSTRUCTOR_P (current_function_decl))
7759 finish_mem_initializers (NULL_TREE);
7764 /* Consume the `:' token. */
7765 cp_lexer_consume_token (parser->lexer);
7766 /* And the mem-initializer-list. */
7767 cp_parser_mem_initializer_list (parser);
7772 /* Parse a mem-initializer-list.
7774 mem-initializer-list:
7776 mem-initializer , mem-initializer-list */
7779 cp_parser_mem_initializer_list (cp_parser* parser)
7781 tree mem_initializer_list = NULL_TREE;
7783 /* Let the semantic analysis code know that we are starting the
7784 mem-initializer-list. */
7785 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7786 error ("only constructors take base initializers");
7788 /* Loop through the list. */
7791 tree mem_initializer;
7793 /* Parse the mem-initializer. */
7794 mem_initializer = cp_parser_mem_initializer (parser);
7795 /* Add it to the list, unless it was erroneous. */
7796 if (mem_initializer)
7798 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7799 mem_initializer_list = mem_initializer;
7801 /* If the next token is not a `,', we're done. */
7802 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7804 /* Consume the `,' token. */
7805 cp_lexer_consume_token (parser->lexer);
7808 /* Perform semantic analysis. */
7809 if (DECL_CONSTRUCTOR_P (current_function_decl))
7810 finish_mem_initializers (mem_initializer_list);
7813 /* Parse a mem-initializer.
7816 mem-initializer-id ( expression-list [opt] )
7821 ( expression-list [opt] )
7823 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7824 class) or FIELD_DECL (for a non-static data member) to initialize;
7825 the TREE_VALUE is the expression-list. */
7828 cp_parser_mem_initializer (cp_parser* parser)
7830 tree mem_initializer_id;
7831 tree expression_list;
7834 /* Find out what is being initialized. */
7835 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7837 pedwarn ("anachronistic old-style base class initializer");
7838 mem_initializer_id = NULL_TREE;
7841 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7842 member = expand_member_init (mem_initializer_id);
7843 if (member && !DECL_P (member))
7844 in_base_initializer = 1;
7847 = cp_parser_parenthesized_expression_list (parser, false,
7849 /*non_constant_p=*/NULL);
7850 if (!expression_list)
7851 expression_list = void_type_node;
7853 in_base_initializer = 0;
7855 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7858 /* Parse a mem-initializer-id.
7861 :: [opt] nested-name-specifier [opt] class-name
7864 Returns a TYPE indicating the class to be initializer for the first
7865 production. Returns an IDENTIFIER_NODE indicating the data member
7866 to be initialized for the second production. */
7869 cp_parser_mem_initializer_id (cp_parser* parser)
7871 bool global_scope_p;
7872 bool nested_name_specifier_p;
7873 bool template_p = false;
7876 /* `typename' is not allowed in this context ([temp.res]). */
7877 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7879 error ("keyword %<typename%> not allowed in this context (a qualified "
7880 "member initializer is implicitly a type)");
7881 cp_lexer_consume_token (parser->lexer);
7883 /* Look for the optional `::' operator. */
7885 = (cp_parser_global_scope_opt (parser,
7886 /*current_scope_valid_p=*/false)
7888 /* Look for the optional nested-name-specifier. The simplest way to
7893 The keyword `typename' is not permitted in a base-specifier or
7894 mem-initializer; in these contexts a qualified name that
7895 depends on a template-parameter is implicitly assumed to be a
7898 is to assume that we have seen the `typename' keyword at this
7900 nested_name_specifier_p
7901 = (cp_parser_nested_name_specifier_opt (parser,
7902 /*typename_keyword_p=*/true,
7903 /*check_dependency_p=*/true,
7905 /*is_declaration=*/true)
7907 if (nested_name_specifier_p)
7908 template_p = cp_parser_optional_template_keyword (parser);
7909 /* If there is a `::' operator or a nested-name-specifier, then we
7910 are definitely looking for a class-name. */
7911 if (global_scope_p || nested_name_specifier_p)
7912 return cp_parser_class_name (parser,
7913 /*typename_keyword_p=*/true,
7914 /*template_keyword_p=*/template_p,
7916 /*check_dependency_p=*/true,
7917 /*class_head_p=*/false,
7918 /*is_declaration=*/true);
7919 /* Otherwise, we could also be looking for an ordinary identifier. */
7920 cp_parser_parse_tentatively (parser);
7921 /* Try a class-name. */
7922 id = cp_parser_class_name (parser,
7923 /*typename_keyword_p=*/true,
7924 /*template_keyword_p=*/false,
7926 /*check_dependency_p=*/true,
7927 /*class_head_p=*/false,
7928 /*is_declaration=*/true);
7929 /* If we found one, we're done. */
7930 if (cp_parser_parse_definitely (parser))
7932 /* Otherwise, look for an ordinary identifier. */
7933 return cp_parser_identifier (parser);
7936 /* Overloading [gram.over] */
7938 /* Parse an operator-function-id.
7940 operator-function-id:
7943 Returns an IDENTIFIER_NODE for the operator which is a
7944 human-readable spelling of the identifier, e.g., `operator +'. */
7947 cp_parser_operator_function_id (cp_parser* parser)
7949 /* Look for the `operator' keyword. */
7950 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7951 return error_mark_node;
7952 /* And then the name of the operator itself. */
7953 return cp_parser_operator (parser);
7956 /* Parse an operator.
7959 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7960 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7961 || ++ -- , ->* -> () []
7968 Returns an IDENTIFIER_NODE for the operator which is a
7969 human-readable spelling of the identifier, e.g., `operator +'. */
7972 cp_parser_operator (cp_parser* parser)
7974 tree id = NULL_TREE;
7977 /* Peek at the next token. */
7978 token = cp_lexer_peek_token (parser->lexer);
7979 /* Figure out which operator we have. */
7980 switch (token->type)
7986 /* The keyword should be either `new' or `delete'. */
7987 if (token->keyword == RID_NEW)
7989 else if (token->keyword == RID_DELETE)
7994 /* Consume the `new' or `delete' token. */
7995 cp_lexer_consume_token (parser->lexer);
7997 /* Peek at the next token. */
7998 token = cp_lexer_peek_token (parser->lexer);
7999 /* If it's a `[' token then this is the array variant of the
8001 if (token->type == CPP_OPEN_SQUARE)
8003 /* Consume the `[' token. */
8004 cp_lexer_consume_token (parser->lexer);
8005 /* Look for the `]' token. */
8006 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8007 id = ansi_opname (op == NEW_EXPR
8008 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8010 /* Otherwise, we have the non-array variant. */
8012 id = ansi_opname (op);
8018 id = ansi_opname (PLUS_EXPR);
8022 id = ansi_opname (MINUS_EXPR);
8026 id = ansi_opname (MULT_EXPR);
8030 id = ansi_opname (TRUNC_DIV_EXPR);
8034 id = ansi_opname (TRUNC_MOD_EXPR);
8038 id = ansi_opname (BIT_XOR_EXPR);
8042 id = ansi_opname (BIT_AND_EXPR);
8046 id = ansi_opname (BIT_IOR_EXPR);
8050 id = ansi_opname (BIT_NOT_EXPR);
8054 id = ansi_opname (TRUTH_NOT_EXPR);
8058 id = ansi_assopname (NOP_EXPR);
8062 id = ansi_opname (LT_EXPR);
8066 id = ansi_opname (GT_EXPR);
8070 id = ansi_assopname (PLUS_EXPR);
8074 id = ansi_assopname (MINUS_EXPR);
8078 id = ansi_assopname (MULT_EXPR);
8082 id = ansi_assopname (TRUNC_DIV_EXPR);
8086 id = ansi_assopname (TRUNC_MOD_EXPR);
8090 id = ansi_assopname (BIT_XOR_EXPR);
8094 id = ansi_assopname (BIT_AND_EXPR);
8098 id = ansi_assopname (BIT_IOR_EXPR);
8102 id = ansi_opname (LSHIFT_EXPR);
8106 id = ansi_opname (RSHIFT_EXPR);
8110 id = ansi_assopname (LSHIFT_EXPR);
8114 id = ansi_assopname (RSHIFT_EXPR);
8118 id = ansi_opname (EQ_EXPR);
8122 id = ansi_opname (NE_EXPR);
8126 id = ansi_opname (LE_EXPR);
8129 case CPP_GREATER_EQ:
8130 id = ansi_opname (GE_EXPR);
8134 id = ansi_opname (TRUTH_ANDIF_EXPR);
8138 id = ansi_opname (TRUTH_ORIF_EXPR);
8142 id = ansi_opname (POSTINCREMENT_EXPR);
8145 case CPP_MINUS_MINUS:
8146 id = ansi_opname (PREDECREMENT_EXPR);
8150 id = ansi_opname (COMPOUND_EXPR);
8153 case CPP_DEREF_STAR:
8154 id = ansi_opname (MEMBER_REF);
8158 id = ansi_opname (COMPONENT_REF);
8161 case CPP_OPEN_PAREN:
8162 /* Consume the `('. */
8163 cp_lexer_consume_token (parser->lexer);
8164 /* Look for the matching `)'. */
8165 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8166 return ansi_opname (CALL_EXPR);
8168 case CPP_OPEN_SQUARE:
8169 /* Consume the `['. */
8170 cp_lexer_consume_token (parser->lexer);
8171 /* Look for the matching `]'. */
8172 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8173 return ansi_opname (ARRAY_REF);
8177 id = ansi_opname (MIN_EXPR);
8178 cp_parser_warn_min_max ();
8182 id = ansi_opname (MAX_EXPR);
8183 cp_parser_warn_min_max ();
8187 id = ansi_assopname (MIN_EXPR);
8188 cp_parser_warn_min_max ();
8192 id = ansi_assopname (MAX_EXPR);
8193 cp_parser_warn_min_max ();
8197 /* Anything else is an error. */
8201 /* If we have selected an identifier, we need to consume the
8204 cp_lexer_consume_token (parser->lexer);
8205 /* Otherwise, no valid operator name was present. */
8208 cp_parser_error (parser, "expected operator");
8209 id = error_mark_node;
8215 /* Parse a template-declaration.
8217 template-declaration:
8218 export [opt] template < template-parameter-list > declaration
8220 If MEMBER_P is TRUE, this template-declaration occurs within a
8223 The grammar rule given by the standard isn't correct. What
8226 template-declaration:
8227 export [opt] template-parameter-list-seq
8228 decl-specifier-seq [opt] init-declarator [opt] ;
8229 export [opt] template-parameter-list-seq
8232 template-parameter-list-seq:
8233 template-parameter-list-seq [opt]
8234 template < template-parameter-list > */
8237 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8239 /* Check for `export'. */
8240 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8242 /* Consume the `export' token. */
8243 cp_lexer_consume_token (parser->lexer);
8244 /* Warn that we do not support `export'. */
8245 warning (0, "keyword %<export%> not implemented, and will be ignored");
8248 cp_parser_template_declaration_after_export (parser, member_p);
8251 /* Parse a template-parameter-list.
8253 template-parameter-list:
8255 template-parameter-list , template-parameter
8257 Returns a TREE_LIST. Each node represents a template parameter.
8258 The nodes are connected via their TREE_CHAINs. */
8261 cp_parser_template_parameter_list (cp_parser* parser)
8263 tree parameter_list = NULL_TREE;
8271 /* Parse the template-parameter. */
8272 parameter = cp_parser_template_parameter (parser, &is_non_type);
8273 /* Add it to the list. */
8274 if (parameter != error_mark_node)
8275 parameter_list = process_template_parm (parameter_list,
8278 /* Peek at the next token. */
8279 token = cp_lexer_peek_token (parser->lexer);
8280 /* If it's not a `,', we're done. */
8281 if (token->type != CPP_COMMA)
8283 /* Otherwise, consume the `,' token. */
8284 cp_lexer_consume_token (parser->lexer);
8287 return parameter_list;
8290 /* Parse a template-parameter.
8294 parameter-declaration
8296 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8297 the parameter. The TREE_PURPOSE is the default value, if any.
8298 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8299 iff this parameter is a non-type parameter. */
8302 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8305 cp_parameter_declarator *parameter_declarator;
8308 /* Assume it is a type parameter or a template parameter. */
8309 *is_non_type = false;
8310 /* Peek at the next token. */
8311 token = cp_lexer_peek_token (parser->lexer);
8312 /* If it is `class' or `template', we have a type-parameter. */
8313 if (token->keyword == RID_TEMPLATE)
8314 return cp_parser_type_parameter (parser);
8315 /* If it is `class' or `typename' we do not know yet whether it is a
8316 type parameter or a non-type parameter. Consider:
8318 template <typename T, typename T::X X> ...
8322 template <class C, class D*> ...
8324 Here, the first parameter is a type parameter, and the second is
8325 a non-type parameter. We can tell by looking at the token after
8326 the identifier -- if it is a `,', `=', or `>' then we have a type
8328 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8330 /* Peek at the token after `class' or `typename'. */
8331 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8332 /* If it's an identifier, skip it. */
8333 if (token->type == CPP_NAME)
8334 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8335 /* Now, see if the token looks like the end of a template
8337 if (token->type == CPP_COMMA
8338 || token->type == CPP_EQ
8339 || token->type == CPP_GREATER)
8340 return cp_parser_type_parameter (parser);
8343 /* Otherwise, it is a non-type parameter.
8347 When parsing a default template-argument for a non-type
8348 template-parameter, the first non-nested `>' is taken as the end
8349 of the template parameter-list rather than a greater-than
8351 *is_non_type = true;
8352 parameter_declarator
8353 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8354 /*parenthesized_p=*/NULL);
8355 parm = grokdeclarator (parameter_declarator->declarator,
8356 ¶meter_declarator->decl_specifiers,
8357 PARM, /*initialized=*/0,
8359 if (parm == error_mark_node)
8360 return error_mark_node;
8361 return build_tree_list (parameter_declarator->default_argument, parm);
8364 /* Parse a type-parameter.
8367 class identifier [opt]
8368 class identifier [opt] = type-id
8369 typename identifier [opt]
8370 typename identifier [opt] = type-id
8371 template < template-parameter-list > class identifier [opt]
8372 template < template-parameter-list > class identifier [opt]
8375 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8376 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8377 the declaration of the parameter. */
8380 cp_parser_type_parameter (cp_parser* parser)
8385 /* Look for a keyword to tell us what kind of parameter this is. */
8386 token = cp_parser_require (parser, CPP_KEYWORD,
8387 "`class', `typename', or `template'");
8389 return error_mark_node;
8391 switch (token->keyword)
8397 tree default_argument;
8399 /* If the next token is an identifier, then it names the
8401 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8402 identifier = cp_parser_identifier (parser);
8404 identifier = NULL_TREE;
8406 /* Create the parameter. */
8407 parameter = finish_template_type_parm (class_type_node, identifier);
8409 /* If the next token is an `=', we have a default argument. */
8410 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8412 /* Consume the `=' token. */
8413 cp_lexer_consume_token (parser->lexer);
8414 /* Parse the default-argument. */
8415 default_argument = cp_parser_type_id (parser);
8418 default_argument = NULL_TREE;
8420 /* Create the combined representation of the parameter and the
8421 default argument. */
8422 parameter = build_tree_list (default_argument, parameter);
8428 tree parameter_list;
8430 tree default_argument;
8432 /* Look for the `<'. */
8433 cp_parser_require (parser, CPP_LESS, "`<'");
8434 /* Parse the template-parameter-list. */
8435 begin_template_parm_list ();
8437 = cp_parser_template_parameter_list (parser);
8438 parameter_list = end_template_parm_list (parameter_list);
8439 /* Look for the `>'. */
8440 cp_parser_require (parser, CPP_GREATER, "`>'");
8441 /* Look for the `class' keyword. */
8442 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8443 /* If the next token is an `=', then there is a
8444 default-argument. If the next token is a `>', we are at
8445 the end of the parameter-list. If the next token is a `,',
8446 then we are at the end of this parameter. */
8447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8448 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8449 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8451 identifier = cp_parser_identifier (parser);
8452 /* Treat invalid names as if the parameter were nameless. */
8453 if (identifier == error_mark_node)
8454 identifier = NULL_TREE;
8457 identifier = NULL_TREE;
8459 /* Create the template parameter. */
8460 parameter = finish_template_template_parm (class_type_node,
8463 /* If the next token is an `=', then there is a
8464 default-argument. */
8465 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8469 /* Consume the `='. */
8470 cp_lexer_consume_token (parser->lexer);
8471 /* Parse the id-expression. */
8473 = cp_parser_id_expression (parser,
8474 /*template_keyword_p=*/false,
8475 /*check_dependency_p=*/true,
8476 /*template_p=*/&is_template,
8477 /*declarator_p=*/false);
8478 if (TREE_CODE (default_argument) == TYPE_DECL)
8479 /* If the id-expression was a template-id that refers to
8480 a template-class, we already have the declaration here,
8481 so no further lookup is needed. */
8484 /* Look up the name. */
8486 = cp_parser_lookup_name (parser, default_argument,
8488 /*is_template=*/is_template,
8489 /*is_namespace=*/false,
8490 /*check_dependency=*/true,
8491 /*ambiguous_p=*/NULL);
8492 /* See if the default argument is valid. */
8494 = check_template_template_default_arg (default_argument);
8497 default_argument = NULL_TREE;
8499 /* Create the combined representation of the parameter and the
8500 default argument. */
8501 parameter = build_tree_list (default_argument, parameter);
8513 /* Parse a template-id.
8516 template-name < template-argument-list [opt] >
8518 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8519 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8520 returned. Otherwise, if the template-name names a function, or set
8521 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8522 names a class, returns a TYPE_DECL for the specialization.
8524 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8525 uninstantiated templates. */
8528 cp_parser_template_id (cp_parser *parser,
8529 bool template_keyword_p,
8530 bool check_dependency_p,
8531 bool is_declaration)
8536 cp_token_position start_of_id = 0;
8537 tree access_check = NULL_TREE;
8538 cp_token *next_token, *next_token_2;
8541 /* If the next token corresponds to a template-id, there is no need
8543 next_token = cp_lexer_peek_token (parser->lexer);
8544 if (next_token->type == CPP_TEMPLATE_ID)
8549 /* Get the stored value. */
8550 value = cp_lexer_consume_token (parser->lexer)->value;
8551 /* Perform any access checks that were deferred. */
8552 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8553 perform_or_defer_access_check (TREE_PURPOSE (check),
8554 TREE_VALUE (check));
8555 /* Return the stored value. */
8556 return TREE_VALUE (value);
8559 /* Avoid performing name lookup if there is no possibility of
8560 finding a template-id. */
8561 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8562 || (next_token->type == CPP_NAME
8563 && !cp_parser_nth_token_starts_template_argument_list_p
8566 cp_parser_error (parser, "expected template-id");
8567 return error_mark_node;
8570 /* Remember where the template-id starts. */
8571 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8572 start_of_id = cp_lexer_token_position (parser->lexer, false);
8574 push_deferring_access_checks (dk_deferred);
8576 /* Parse the template-name. */
8577 is_identifier = false;
8578 template = cp_parser_template_name (parser, template_keyword_p,
8582 if (template == error_mark_node || is_identifier)
8584 pop_deferring_access_checks ();
8588 /* If we find the sequence `[:' after a template-name, it's probably
8589 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8590 parse correctly the argument list. */
8591 next_token = cp_lexer_peek_token (parser->lexer);
8592 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8593 if (next_token->type == CPP_OPEN_SQUARE
8594 && next_token->flags & DIGRAPH
8595 && next_token_2->type == CPP_COLON
8596 && !(next_token_2->flags & PREV_WHITE))
8598 cp_parser_parse_tentatively (parser);
8599 /* Change `:' into `::'. */
8600 next_token_2->type = CPP_SCOPE;
8601 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8603 cp_lexer_consume_token (parser->lexer);
8604 /* Parse the arguments. */
8605 arguments = cp_parser_enclosed_template_argument_list (parser);
8606 if (!cp_parser_parse_definitely (parser))
8608 /* If we couldn't parse an argument list, then we revert our changes
8609 and return simply an error. Maybe this is not a template-id
8611 next_token_2->type = CPP_COLON;
8612 cp_parser_error (parser, "expected %<<%>");
8613 pop_deferring_access_checks ();
8614 return error_mark_node;
8616 /* Otherwise, emit an error about the invalid digraph, but continue
8617 parsing because we got our argument list. */
8618 pedwarn ("%<<::%> cannot begin a template-argument list");
8619 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8620 "between %<<%> and %<::%>");
8621 if (!flag_permissive)
8626 inform ("(if you use -fpermissive G++ will accept your code)");
8633 /* Look for the `<' that starts the template-argument-list. */
8634 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8636 pop_deferring_access_checks ();
8637 return error_mark_node;
8639 /* Parse the arguments. */
8640 arguments = cp_parser_enclosed_template_argument_list (parser);
8643 /* Build a representation of the specialization. */
8644 if (TREE_CODE (template) == IDENTIFIER_NODE)
8645 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8646 else if (DECL_CLASS_TEMPLATE_P (template)
8647 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8649 = finish_template_type (template, arguments,
8650 cp_lexer_next_token_is (parser->lexer,
8654 /* If it's not a class-template or a template-template, it should be
8655 a function-template. */
8656 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8657 || TREE_CODE (template) == OVERLOAD
8658 || BASELINK_P (template)));
8660 template_id = lookup_template_function (template, arguments);
8663 /* Retrieve any deferred checks. Do not pop this access checks yet
8664 so the memory will not be reclaimed during token replacing below. */
8665 access_check = get_deferred_access_checks ();
8667 /* If parsing tentatively, replace the sequence of tokens that makes
8668 up the template-id with a CPP_TEMPLATE_ID token. That way,
8669 should we re-parse the token stream, we will not have to repeat
8670 the effort required to do the parse, nor will we issue duplicate
8671 error messages about problems during instantiation of the
8675 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8677 /* Reset the contents of the START_OF_ID token. */
8678 token->type = CPP_TEMPLATE_ID;
8679 token->value = build_tree_list (access_check, template_id);
8680 token->keyword = RID_MAX;
8682 /* Purge all subsequent tokens. */
8683 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8685 /* ??? Can we actually assume that, if template_id ==
8686 error_mark_node, we will have issued a diagnostic to the
8687 user, as opposed to simply marking the tentative parse as
8689 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8690 error ("parse error in template argument list");
8693 pop_deferring_access_checks ();
8697 /* Parse a template-name.
8702 The standard should actually say:
8706 operator-function-id
8708 A defect report has been filed about this issue.
8710 A conversion-function-id cannot be a template name because they cannot
8711 be part of a template-id. In fact, looking at this code:
8715 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8716 It is impossible to call a templated conversion-function-id with an
8717 explicit argument list, since the only allowed template parameter is
8718 the type to which it is converting.
8720 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8721 `template' keyword, in a construction like:
8725 In that case `f' is taken to be a template-name, even though there
8726 is no way of knowing for sure.
8728 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8729 name refers to a set of overloaded functions, at least one of which
8730 is a template, or an IDENTIFIER_NODE with the name of the template,
8731 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8732 names are looked up inside uninstantiated templates. */
8735 cp_parser_template_name (cp_parser* parser,
8736 bool template_keyword_p,
8737 bool check_dependency_p,
8738 bool is_declaration,
8739 bool *is_identifier)
8745 /* If the next token is `operator', then we have either an
8746 operator-function-id or a conversion-function-id. */
8747 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8749 /* We don't know whether we're looking at an
8750 operator-function-id or a conversion-function-id. */
8751 cp_parser_parse_tentatively (parser);
8752 /* Try an operator-function-id. */
8753 identifier = cp_parser_operator_function_id (parser);
8754 /* If that didn't work, try a conversion-function-id. */
8755 if (!cp_parser_parse_definitely (parser))
8757 cp_parser_error (parser, "expected template-name");
8758 return error_mark_node;
8761 /* Look for the identifier. */
8763 identifier = cp_parser_identifier (parser);
8765 /* If we didn't find an identifier, we don't have a template-id. */
8766 if (identifier == error_mark_node)
8767 return error_mark_node;
8769 /* If the name immediately followed the `template' keyword, then it
8770 is a template-name. However, if the next token is not `<', then
8771 we do not treat it as a template-name, since it is not being used
8772 as part of a template-id. This enables us to handle constructs
8775 template <typename T> struct S { S(); };
8776 template <typename T> S<T>::S();
8778 correctly. We would treat `S' as a template -- if it were `S<T>'
8779 -- but we do not if there is no `<'. */
8781 if (processing_template_decl
8782 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8784 /* In a declaration, in a dependent context, we pretend that the
8785 "template" keyword was present in order to improve error
8786 recovery. For example, given:
8788 template <typename T> void f(T::X<int>);
8790 we want to treat "X<int>" as a template-id. */
8792 && !template_keyword_p
8793 && parser->scope && TYPE_P (parser->scope)
8794 && check_dependency_p
8795 && dependent_type_p (parser->scope)
8796 /* Do not do this for dtors (or ctors), since they never
8797 need the template keyword before their name. */
8798 && !constructor_name_p (identifier, parser->scope))
8800 cp_token_position start = 0;
8802 /* Explain what went wrong. */
8803 error ("non-template %qD used as template", identifier);
8804 inform ("use %<%T::template %D%> to indicate that it is a template",
8805 parser->scope, identifier);
8806 /* If parsing tentatively, find the location of the "<" token. */
8807 if (cp_parser_simulate_error (parser))
8808 start = cp_lexer_token_position (parser->lexer, true);
8809 /* Parse the template arguments so that we can issue error
8810 messages about them. */
8811 cp_lexer_consume_token (parser->lexer);
8812 cp_parser_enclosed_template_argument_list (parser);
8813 /* Skip tokens until we find a good place from which to
8814 continue parsing. */
8815 cp_parser_skip_to_closing_parenthesis (parser,
8816 /*recovering=*/true,
8818 /*consume_paren=*/false);
8819 /* If parsing tentatively, permanently remove the
8820 template argument list. That will prevent duplicate
8821 error messages from being issued about the missing
8822 "template" keyword. */
8824 cp_lexer_purge_tokens_after (parser->lexer, start);
8826 *is_identifier = true;
8830 /* If the "template" keyword is present, then there is generally
8831 no point in doing name-lookup, so we just return IDENTIFIER.
8832 But, if the qualifying scope is non-dependent then we can
8833 (and must) do name-lookup normally. */
8834 if (template_keyword_p
8836 || (TYPE_P (parser->scope)
8837 && dependent_type_p (parser->scope))))
8841 /* Look up the name. */
8842 decl = cp_parser_lookup_name (parser, identifier,
8844 /*is_template=*/false,
8845 /*is_namespace=*/false,
8847 /*ambiguous_p=*/NULL);
8848 decl = maybe_get_template_decl_from_type_decl (decl);
8850 /* If DECL is a template, then the name was a template-name. */
8851 if (TREE_CODE (decl) == TEMPLATE_DECL)
8855 tree fn = NULL_TREE;
8857 /* The standard does not explicitly indicate whether a name that
8858 names a set of overloaded declarations, some of which are
8859 templates, is a template-name. However, such a name should
8860 be a template-name; otherwise, there is no way to form a
8861 template-id for the overloaded templates. */
8862 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8863 if (TREE_CODE (fns) == OVERLOAD)
8864 for (fn = fns; fn; fn = OVL_NEXT (fn))
8865 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8870 /* The name does not name a template. */
8871 cp_parser_error (parser, "expected template-name");
8872 return error_mark_node;
8876 /* If DECL is dependent, and refers to a function, then just return
8877 its name; we will look it up again during template instantiation. */
8878 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8880 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8881 if (TYPE_P (scope) && dependent_type_p (scope))
8888 /* Parse a template-argument-list.
8890 template-argument-list:
8892 template-argument-list , template-argument
8894 Returns a TREE_VEC containing the arguments. */
8897 cp_parser_template_argument_list (cp_parser* parser)
8899 tree fixed_args[10];
8900 unsigned n_args = 0;
8901 unsigned alloced = 10;
8902 tree *arg_ary = fixed_args;
8904 bool saved_in_template_argument_list_p;
8906 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8907 parser->in_template_argument_list_p = true;
8913 /* Consume the comma. */
8914 cp_lexer_consume_token (parser->lexer);
8916 /* Parse the template-argument. */
8917 argument = cp_parser_template_argument (parser);
8918 if (n_args == alloced)
8922 if (arg_ary == fixed_args)
8924 arg_ary = xmalloc (sizeof (tree) * alloced);
8925 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8928 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8930 arg_ary[n_args++] = argument;
8932 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8934 vec = make_tree_vec (n_args);
8937 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8939 if (arg_ary != fixed_args)
8941 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8945 /* Parse a template-argument.
8948 assignment-expression
8952 The representation is that of an assignment-expression, type-id, or
8953 id-expression -- except that the qualified id-expression is
8954 evaluated, so that the value returned is either a DECL or an
8957 Although the standard says "assignment-expression", it forbids
8958 throw-expressions or assignments in the template argument.
8959 Therefore, we use "conditional-expression" instead. */
8962 cp_parser_template_argument (cp_parser* parser)
8967 bool maybe_type_id = false;
8970 tree qualifying_class;
8972 /* There's really no way to know what we're looking at, so we just
8973 try each alternative in order.
8977 In a template-argument, an ambiguity between a type-id and an
8978 expression is resolved to a type-id, regardless of the form of
8979 the corresponding template-parameter.
8981 Therefore, we try a type-id first. */
8982 cp_parser_parse_tentatively (parser);
8983 argument = cp_parser_type_id (parser);
8984 /* If there was no error parsing the type-id but the next token is a '>>',
8985 we probably found a typo for '> >'. But there are type-id which are
8986 also valid expressions. For instance:
8988 struct X { int operator >> (int); };
8989 template <int V> struct Foo {};
8992 Here 'X()' is a valid type-id of a function type, but the user just
8993 wanted to write the expression "X() >> 5". Thus, we remember that we
8994 found a valid type-id, but we still try to parse the argument as an
8995 expression to see what happens. */
8996 if (!cp_parser_error_occurred (parser)
8997 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8999 maybe_type_id = true;
9000 cp_parser_abort_tentative_parse (parser);
9004 /* If the next token isn't a `,' or a `>', then this argument wasn't
9005 really finished. This means that the argument is not a valid
9007 if (!cp_parser_next_token_ends_template_argument_p (parser))
9008 cp_parser_error (parser, "expected template-argument");
9009 /* If that worked, we're done. */
9010 if (cp_parser_parse_definitely (parser))
9013 /* We're still not sure what the argument will be. */
9014 cp_parser_parse_tentatively (parser);
9015 /* Try a template. */
9016 argument = cp_parser_id_expression (parser,
9017 /*template_keyword_p=*/false,
9018 /*check_dependency_p=*/true,
9020 /*declarator_p=*/false);
9021 /* If the next token isn't a `,' or a `>', then this argument wasn't
9023 if (!cp_parser_next_token_ends_template_argument_p (parser))
9024 cp_parser_error (parser, "expected template-argument");
9025 if (!cp_parser_error_occurred (parser))
9027 /* Figure out what is being referred to. If the id-expression
9028 was for a class template specialization, then we will have a
9029 TYPE_DECL at this point. There is no need to do name lookup
9030 at this point in that case. */
9031 if (TREE_CODE (argument) != TYPE_DECL)
9032 argument = cp_parser_lookup_name (parser, argument,
9034 /*is_template=*/template_p,
9035 /*is_namespace=*/false,
9036 /*check_dependency=*/true,
9037 /*ambiguous_p=*/NULL);
9038 if (TREE_CODE (argument) != TEMPLATE_DECL
9039 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9040 cp_parser_error (parser, "expected template-name");
9042 if (cp_parser_parse_definitely (parser))
9044 /* It must be a non-type argument. There permitted cases are given
9045 in [temp.arg.nontype]:
9047 -- an integral constant-expression of integral or enumeration
9050 -- the name of a non-type template-parameter; or
9052 -- the name of an object or function with external linkage...
9054 -- the address of an object or function with external linkage...
9056 -- a pointer to member... */
9057 /* Look for a non-type template parameter. */
9058 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9060 cp_parser_parse_tentatively (parser);
9061 argument = cp_parser_primary_expression (parser,
9065 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9066 || !cp_parser_next_token_ends_template_argument_p (parser))
9067 cp_parser_simulate_error (parser);
9068 if (cp_parser_parse_definitely (parser))
9072 /* If the next token is "&", the argument must be the address of an
9073 object or function with external linkage. */
9074 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9076 cp_lexer_consume_token (parser->lexer);
9077 /* See if we might have an id-expression. */
9078 token = cp_lexer_peek_token (parser->lexer);
9079 if (token->type == CPP_NAME
9080 || token->keyword == RID_OPERATOR
9081 || token->type == CPP_SCOPE
9082 || token->type == CPP_TEMPLATE_ID
9083 || token->type == CPP_NESTED_NAME_SPECIFIER)
9085 cp_parser_parse_tentatively (parser);
9086 argument = cp_parser_primary_expression (parser,
9090 if (cp_parser_error_occurred (parser)
9091 || !cp_parser_next_token_ends_template_argument_p (parser))
9092 cp_parser_abort_tentative_parse (parser);
9095 if (TREE_CODE (argument) == INDIRECT_REF)
9097 gcc_assert (REFERENCE_REF_P (argument));
9098 argument = TREE_OPERAND (argument, 0);
9101 if (qualifying_class)
9102 argument = finish_qualified_id_expr (qualifying_class,
9106 if (TREE_CODE (argument) == VAR_DECL)
9108 /* A variable without external linkage might still be a
9109 valid constant-expression, so no error is issued here
9110 if the external-linkage check fails. */
9111 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9112 cp_parser_simulate_error (parser);
9114 else if (is_overloaded_fn (argument))
9115 /* All overloaded functions are allowed; if the external
9116 linkage test does not pass, an error will be issued
9120 && (TREE_CODE (argument) == OFFSET_REF
9121 || TREE_CODE (argument) == SCOPE_REF))
9122 /* A pointer-to-member. */
9124 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9127 cp_parser_simulate_error (parser);
9129 if (cp_parser_parse_definitely (parser))
9132 argument = build_x_unary_op (ADDR_EXPR, argument);
9137 /* If the argument started with "&", there are no other valid
9138 alternatives at this point. */
9141 cp_parser_error (parser, "invalid non-type template argument");
9142 return error_mark_node;
9145 /* If the argument wasn't successfully parsed as a type-id followed
9146 by '>>', the argument can only be a constant expression now.
9147 Otherwise, we try parsing the constant-expression tentatively,
9148 because the argument could really be a type-id. */
9150 cp_parser_parse_tentatively (parser);
9151 argument = cp_parser_constant_expression (parser,
9152 /*allow_non_constant_p=*/false,
9153 /*non_constant_p=*/NULL);
9154 argument = fold_non_dependent_expr (argument);
9157 if (!cp_parser_next_token_ends_template_argument_p (parser))
9158 cp_parser_error (parser, "expected template-argument");
9159 if (cp_parser_parse_definitely (parser))
9161 /* We did our best to parse the argument as a non type-id, but that
9162 was the only alternative that matched (albeit with a '>' after
9163 it). We can assume it's just a typo from the user, and a
9164 diagnostic will then be issued. */
9165 return cp_parser_type_id (parser);
9168 /* Parse an explicit-instantiation.
9170 explicit-instantiation:
9171 template declaration
9173 Although the standard says `declaration', what it really means is:
9175 explicit-instantiation:
9176 template decl-specifier-seq [opt] declarator [opt] ;
9178 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9179 supposed to be allowed. A defect report has been filed about this
9184 explicit-instantiation:
9185 storage-class-specifier template
9186 decl-specifier-seq [opt] declarator [opt] ;
9187 function-specifier template
9188 decl-specifier-seq [opt] declarator [opt] ; */
9191 cp_parser_explicit_instantiation (cp_parser* parser)
9193 int declares_class_or_enum;
9194 cp_decl_specifier_seq decl_specifiers;
9195 tree extension_specifier = NULL_TREE;
9197 /* Look for an (optional) storage-class-specifier or
9198 function-specifier. */
9199 if (cp_parser_allow_gnu_extensions_p (parser))
9202 = cp_parser_storage_class_specifier_opt (parser);
9203 if (!extension_specifier)
9205 = cp_parser_function_specifier_opt (parser,
9206 /*decl_specs=*/NULL);
9209 /* Look for the `template' keyword. */
9210 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9211 /* Let the front end know that we are processing an explicit
9213 begin_explicit_instantiation ();
9214 /* [temp.explicit] says that we are supposed to ignore access
9215 control while processing explicit instantiation directives. */
9216 push_deferring_access_checks (dk_no_check);
9217 /* Parse a decl-specifier-seq. */
9218 cp_parser_decl_specifier_seq (parser,
9219 CP_PARSER_FLAGS_OPTIONAL,
9221 &declares_class_or_enum);
9222 /* If there was exactly one decl-specifier, and it declared a class,
9223 and there's no declarator, then we have an explicit type
9225 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9229 type = check_tag_decl (&decl_specifiers);
9230 /* Turn access control back on for names used during
9231 template instantiation. */
9232 pop_deferring_access_checks ();
9234 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9238 cp_declarator *declarator;
9241 /* Parse the declarator. */
9243 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9244 /*ctor_dtor_or_conv_p=*/NULL,
9245 /*parenthesized_p=*/NULL,
9246 /*member_p=*/false);
9247 if (declares_class_or_enum & 2)
9248 cp_parser_check_for_definition_in_return_type (declarator,
9249 decl_specifiers.type);
9250 if (declarator != cp_error_declarator)
9252 decl = grokdeclarator (declarator, &decl_specifiers,
9254 /* Turn access control back on for names used during
9255 template instantiation. */
9256 pop_deferring_access_checks ();
9257 /* Do the explicit instantiation. */
9258 do_decl_instantiation (decl, extension_specifier);
9262 pop_deferring_access_checks ();
9263 /* Skip the body of the explicit instantiation. */
9264 cp_parser_skip_to_end_of_statement (parser);
9267 /* We're done with the instantiation. */
9268 end_explicit_instantiation ();
9270 cp_parser_consume_semicolon_at_end_of_statement (parser);
9273 /* Parse an explicit-specialization.
9275 explicit-specialization:
9276 template < > declaration
9278 Although the standard says `declaration', what it really means is:
9280 explicit-specialization:
9281 template <> decl-specifier [opt] init-declarator [opt] ;
9282 template <> function-definition
9283 template <> explicit-specialization
9284 template <> template-declaration */
9287 cp_parser_explicit_specialization (cp_parser* parser)
9289 /* Look for the `template' keyword. */
9290 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9291 /* Look for the `<'. */
9292 cp_parser_require (parser, CPP_LESS, "`<'");
9293 /* Look for the `>'. */
9294 cp_parser_require (parser, CPP_GREATER, "`>'");
9295 /* We have processed another parameter list. */
9296 ++parser->num_template_parameter_lists;
9297 /* Let the front end know that we are beginning a specialization. */
9298 begin_specialization ();
9300 /* If the next keyword is `template', we need to figure out whether
9301 or not we're looking a template-declaration. */
9302 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9304 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9305 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9306 cp_parser_template_declaration_after_export (parser,
9307 /*member_p=*/false);
9309 cp_parser_explicit_specialization (parser);
9312 /* Parse the dependent declaration. */
9313 cp_parser_single_declaration (parser,
9317 /* We're done with the specialization. */
9318 end_specialization ();
9319 /* We're done with this parameter list. */
9320 --parser->num_template_parameter_lists;
9323 /* Parse a type-specifier.
9326 simple-type-specifier
9329 elaborated-type-specifier
9337 Returns a representation of the type-specifier. For a
9338 class-specifier, enum-specifier, or elaborated-type-specifier, a
9339 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9341 The parser flags FLAGS is used to control type-specifier parsing.
9343 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9344 in a decl-specifier-seq.
9346 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9347 class-specifier, enum-specifier, or elaborated-type-specifier, then
9348 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9349 if a type is declared; 2 if it is defined. Otherwise, it is set to
9352 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9353 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9357 cp_parser_type_specifier (cp_parser* parser,
9358 cp_parser_flags flags,
9359 cp_decl_specifier_seq *decl_specs,
9360 bool is_declaration,
9361 int* declares_class_or_enum,
9362 bool* is_cv_qualifier)
9364 tree type_spec = NULL_TREE;
9367 cp_decl_spec ds = ds_last;
9369 /* Assume this type-specifier does not declare a new type. */
9370 if (declares_class_or_enum)
9371 *declares_class_or_enum = 0;
9372 /* And that it does not specify a cv-qualifier. */
9373 if (is_cv_qualifier)
9374 *is_cv_qualifier = false;
9375 /* Peek at the next token. */
9376 token = cp_lexer_peek_token (parser->lexer);
9378 /* If we're looking at a keyword, we can use that to guide the
9379 production we choose. */
9380 keyword = token->keyword;
9384 /* 'enum' [identifier] '{' introduces an enum-specifier;
9385 'enum' <anything else> introduces an elaborated-type-specifier. */
9386 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9387 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9388 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9391 if (parser->num_template_parameter_lists)
9393 error ("template declaration of %qs", "enum");
9394 cp_parser_skip_to_end_of_block_or_statement (parser);
9395 type_spec = error_mark_node;
9398 type_spec = cp_parser_enum_specifier (parser);
9400 if (declares_class_or_enum)
9401 *declares_class_or_enum = 2;
9403 cp_parser_set_decl_spec_type (decl_specs,
9405 /*user_defined_p=*/true);
9409 goto elaborated_type_specifier;
9411 /* Any of these indicate either a class-specifier, or an
9412 elaborated-type-specifier. */
9416 /* Parse tentatively so that we can back up if we don't find a
9418 cp_parser_parse_tentatively (parser);
9419 /* Look for the class-specifier. */
9420 type_spec = cp_parser_class_specifier (parser);
9421 /* If that worked, we're done. */
9422 if (cp_parser_parse_definitely (parser))
9424 if (declares_class_or_enum)
9425 *declares_class_or_enum = 2;
9427 cp_parser_set_decl_spec_type (decl_specs,
9429 /*user_defined_p=*/true);
9434 elaborated_type_specifier:
9435 /* We're declaring (not defining) a class or enum. */
9436 if (declares_class_or_enum)
9437 *declares_class_or_enum = 1;
9441 /* Look for an elaborated-type-specifier. */
9443 = (cp_parser_elaborated_type_specifier
9445 decl_specs && decl_specs->specs[(int) ds_friend],
9448 cp_parser_set_decl_spec_type (decl_specs,
9450 /*user_defined_p=*/true);
9455 if (is_cv_qualifier)
9456 *is_cv_qualifier = true;
9461 if (is_cv_qualifier)
9462 *is_cv_qualifier = true;
9467 if (is_cv_qualifier)
9468 *is_cv_qualifier = true;
9472 /* The `__complex__' keyword is a GNU extension. */
9480 /* Handle simple keywords. */
9485 ++decl_specs->specs[(int)ds];
9486 decl_specs->any_specifiers_p = true;
9488 return cp_lexer_consume_token (parser->lexer)->value;
9491 /* If we do not already have a type-specifier, assume we are looking
9492 at a simple-type-specifier. */
9493 type_spec = cp_parser_simple_type_specifier (parser,
9497 /* If we didn't find a type-specifier, and a type-specifier was not
9498 optional in this context, issue an error message. */
9499 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9501 cp_parser_error (parser, "expected type specifier");
9502 return error_mark_node;
9508 /* Parse a simple-type-specifier.
9510 simple-type-specifier:
9511 :: [opt] nested-name-specifier [opt] type-name
9512 :: [opt] nested-name-specifier template template-id
9527 simple-type-specifier:
9528 __typeof__ unary-expression
9529 __typeof__ ( type-id )
9531 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9532 appropriately updated. */
9535 cp_parser_simple_type_specifier (cp_parser* parser,
9536 cp_decl_specifier_seq *decl_specs,
9537 cp_parser_flags flags)
9539 tree type = NULL_TREE;
9542 /* Peek at the next token. */
9543 token = cp_lexer_peek_token (parser->lexer);
9545 /* If we're looking at a keyword, things are easy. */
9546 switch (token->keyword)
9550 decl_specs->explicit_char_p = true;
9551 type = char_type_node;
9554 type = wchar_type_node;
9557 type = boolean_type_node;
9561 ++decl_specs->specs[(int) ds_short];
9562 type = short_integer_type_node;
9566 decl_specs->explicit_int_p = true;
9567 type = integer_type_node;
9571 ++decl_specs->specs[(int) ds_long];
9572 type = long_integer_type_node;
9576 ++decl_specs->specs[(int) ds_signed];
9577 type = integer_type_node;
9581 ++decl_specs->specs[(int) ds_unsigned];
9582 type = unsigned_type_node;
9585 type = float_type_node;
9588 type = double_type_node;
9591 type = void_type_node;
9595 /* Consume the `typeof' token. */
9596 cp_lexer_consume_token (parser->lexer);
9597 /* Parse the operand to `typeof'. */
9598 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9599 /* If it is not already a TYPE, take its type. */
9601 type = finish_typeof (type);
9604 cp_parser_set_decl_spec_type (decl_specs, type,
9605 /*user_defined_p=*/true);
9613 /* If the type-specifier was for a built-in type, we're done. */
9618 /* Record the type. */
9620 && (token->keyword != RID_SIGNED
9621 && token->keyword != RID_UNSIGNED
9622 && token->keyword != RID_SHORT
9623 && token->keyword != RID_LONG))
9624 cp_parser_set_decl_spec_type (decl_specs,
9626 /*user_defined=*/false);
9628 decl_specs->any_specifiers_p = true;
9630 /* Consume the token. */
9631 id = cp_lexer_consume_token (parser->lexer)->value;
9633 /* There is no valid C++ program where a non-template type is
9634 followed by a "<". That usually indicates that the user thought
9635 that the type was a template. */
9636 cp_parser_check_for_invalid_template_id (parser, type);
9638 return TYPE_NAME (type);
9641 /* The type-specifier must be a user-defined type. */
9642 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9647 /* Don't gobble tokens or issue error messages if this is an
9648 optional type-specifier. */
9649 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9650 cp_parser_parse_tentatively (parser);
9652 /* Look for the optional `::' operator. */
9654 = (cp_parser_global_scope_opt (parser,
9655 /*current_scope_valid_p=*/false)
9657 /* Look for the nested-name specifier. */
9659 = (cp_parser_nested_name_specifier_opt (parser,
9660 /*typename_keyword_p=*/false,
9661 /*check_dependency_p=*/true,
9663 /*is_declaration=*/false)
9665 /* If we have seen a nested-name-specifier, and the next token
9666 is `template', then we are using the template-id production. */
9668 && cp_parser_optional_template_keyword (parser))
9670 /* Look for the template-id. */
9671 type = cp_parser_template_id (parser,
9672 /*template_keyword_p=*/true,
9673 /*check_dependency_p=*/true,
9674 /*is_declaration=*/false);
9675 /* If the template-id did not name a type, we are out of
9677 if (TREE_CODE (type) != TYPE_DECL)
9679 cp_parser_error (parser, "expected template-id for type");
9683 /* Otherwise, look for a type-name. */
9685 type = cp_parser_type_name (parser);
9686 /* Keep track of all name-lookups performed in class scopes. */
9690 && TREE_CODE (type) == TYPE_DECL
9691 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9692 maybe_note_name_used_in_class (DECL_NAME (type), type);
9693 /* If it didn't work out, we don't have a TYPE. */
9694 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9695 && !cp_parser_parse_definitely (parser))
9697 if (type && decl_specs)
9698 cp_parser_set_decl_spec_type (decl_specs, type,
9699 /*user_defined=*/true);
9702 /* If we didn't get a type-name, issue an error message. */
9703 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9705 cp_parser_error (parser, "expected type-name");
9706 return error_mark_node;
9709 /* There is no valid C++ program where a non-template type is
9710 followed by a "<". That usually indicates that the user thought
9711 that the type was a template. */
9712 if (type && type != error_mark_node)
9714 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9715 If it is, then the '<'...'>' enclose protocol names rather than
9716 template arguments, and so everything is fine. */
9717 if (c_dialect_objc ()
9718 && (objc_is_id (type) || objc_is_class_name (type)))
9720 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9721 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9723 /* Clobber the "unqualified" type previously entered into
9724 DECL_SPECS with the new, improved protocol-qualified version. */
9726 decl_specs->type = qual_type;
9731 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9737 /* Parse a type-name.
9750 Returns a TYPE_DECL for the type. */
9753 cp_parser_type_name (cp_parser* parser)
9758 /* We can't know yet whether it is a class-name or not. */
9759 cp_parser_parse_tentatively (parser);
9760 /* Try a class-name. */
9761 type_decl = cp_parser_class_name (parser,
9762 /*typename_keyword_p=*/false,
9763 /*template_keyword_p=*/false,
9765 /*check_dependency_p=*/true,
9766 /*class_head_p=*/false,
9767 /*is_declaration=*/false);
9768 /* If it's not a class-name, keep looking. */
9769 if (!cp_parser_parse_definitely (parser))
9771 /* It must be a typedef-name or an enum-name. */
9772 identifier = cp_parser_identifier (parser);
9773 if (identifier == error_mark_node)
9774 return error_mark_node;
9776 /* Look up the type-name. */
9777 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9779 if (TREE_CODE (type_decl) != TYPE_DECL
9780 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9782 /* See if this is an Objective-C type. */
9783 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9784 tree type = objc_get_protocol_qualified_type (identifier, protos);
9786 type_decl = TYPE_NAME (type);
9789 /* Issue an error if we did not find a type-name. */
9790 if (TREE_CODE (type_decl) != TYPE_DECL)
9792 if (!cp_parser_simulate_error (parser))
9793 cp_parser_name_lookup_error (parser, identifier, type_decl,
9795 type_decl = error_mark_node;
9797 /* Remember that the name was used in the definition of the
9798 current class so that we can check later to see if the
9799 meaning would have been different after the class was
9800 entirely defined. */
9801 else if (type_decl != error_mark_node
9803 maybe_note_name_used_in_class (identifier, type_decl);
9810 /* Parse an elaborated-type-specifier. Note that the grammar given
9811 here incorporates the resolution to DR68.
9813 elaborated-type-specifier:
9814 class-key :: [opt] nested-name-specifier [opt] identifier
9815 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9816 enum :: [opt] nested-name-specifier [opt] identifier
9817 typename :: [opt] nested-name-specifier identifier
9818 typename :: [opt] nested-name-specifier template [opt]
9823 elaborated-type-specifier:
9824 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9825 class-key attributes :: [opt] nested-name-specifier [opt]
9826 template [opt] template-id
9827 enum attributes :: [opt] nested-name-specifier [opt] identifier
9829 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9830 declared `friend'. If IS_DECLARATION is TRUE, then this
9831 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9832 something is being declared.
9834 Returns the TYPE specified. */
9837 cp_parser_elaborated_type_specifier (cp_parser* parser,
9839 bool is_declaration)
9841 enum tag_types tag_type;
9843 tree type = NULL_TREE;
9844 tree attributes = NULL_TREE;
9846 /* See if we're looking at the `enum' keyword. */
9847 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9849 /* Consume the `enum' token. */
9850 cp_lexer_consume_token (parser->lexer);
9851 /* Remember that it's an enumeration type. */
9852 tag_type = enum_type;
9853 /* Parse the attributes. */
9854 attributes = cp_parser_attributes_opt (parser);
9856 /* Or, it might be `typename'. */
9857 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9860 /* Consume the `typename' token. */
9861 cp_lexer_consume_token (parser->lexer);
9862 /* Remember that it's a `typename' type. */
9863 tag_type = typename_type;
9864 /* The `typename' keyword is only allowed in templates. */
9865 if (!processing_template_decl)
9866 pedwarn ("using %<typename%> outside of template");
9868 /* Otherwise it must be a class-key. */
9871 tag_type = cp_parser_class_key (parser);
9872 if (tag_type == none_type)
9873 return error_mark_node;
9874 /* Parse the attributes. */
9875 attributes = cp_parser_attributes_opt (parser);
9878 /* Look for the `::' operator. */
9879 cp_parser_global_scope_opt (parser,
9880 /*current_scope_valid_p=*/false);
9881 /* Look for the nested-name-specifier. */
9882 if (tag_type == typename_type)
9884 if (!cp_parser_nested_name_specifier (parser,
9885 /*typename_keyword_p=*/true,
9886 /*check_dependency_p=*/true,
9889 return error_mark_node;
9892 /* Even though `typename' is not present, the proposed resolution
9893 to Core Issue 180 says that in `class A<T>::B', `B' should be
9894 considered a type-name, even if `A<T>' is dependent. */
9895 cp_parser_nested_name_specifier_opt (parser,
9896 /*typename_keyword_p=*/true,
9897 /*check_dependency_p=*/true,
9900 /* For everything but enumeration types, consider a template-id. */
9901 if (tag_type != enum_type)
9903 bool template_p = false;
9906 /* Allow the `template' keyword. */
9907 template_p = cp_parser_optional_template_keyword (parser);
9908 /* If we didn't see `template', we don't know if there's a
9909 template-id or not. */
9911 cp_parser_parse_tentatively (parser);
9912 /* Parse the template-id. */
9913 decl = cp_parser_template_id (parser, template_p,
9914 /*check_dependency_p=*/true,
9916 /* If we didn't find a template-id, look for an ordinary
9918 if (!template_p && !cp_parser_parse_definitely (parser))
9920 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9921 in effect, then we must assume that, upon instantiation, the
9922 template will correspond to a class. */
9923 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9924 && tag_type == typename_type)
9925 type = make_typename_type (parser->scope, decl,
9929 type = TREE_TYPE (decl);
9932 /* For an enumeration type, consider only a plain identifier. */
9935 identifier = cp_parser_identifier (parser);
9937 if (identifier == error_mark_node)
9939 parser->scope = NULL_TREE;
9940 return error_mark_node;
9943 /* For a `typename', we needn't call xref_tag. */
9944 if (tag_type == typename_type
9945 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9946 return cp_parser_make_typename_type (parser, parser->scope,
9948 /* Look up a qualified name in the usual way. */
9953 decl = cp_parser_lookup_name (parser, identifier,
9955 /*is_template=*/false,
9956 /*is_namespace=*/false,
9957 /*check_dependency=*/true,
9958 /*ambiguous_p=*/NULL);
9960 /* If we are parsing friend declaration, DECL may be a
9961 TEMPLATE_DECL tree node here. However, we need to check
9962 whether this TEMPLATE_DECL results in valid code. Consider
9963 the following example:
9966 template <class T> class C {};
9969 template <class T> friend class N::C; // #1, valid code
9971 template <class T> class Y {
9972 friend class N::C; // #2, invalid code
9975 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9976 name lookup of `N::C'. We see that friend declaration must
9977 be template for the code to be valid. Note that
9978 processing_template_decl does not work here since it is
9979 always 1 for the above two cases. */
9981 decl = (cp_parser_maybe_treat_template_as_class
9982 (decl, /*tag_name_p=*/is_friend
9983 && parser->num_template_parameter_lists));
9985 if (TREE_CODE (decl) != TYPE_DECL)
9987 cp_parser_diagnose_invalid_type_name (parser,
9990 return error_mark_node;
9993 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9994 check_elaborated_type_specifier
9996 (parser->num_template_parameter_lists
9997 || DECL_SELF_REFERENCE_P (decl)));
9999 type = TREE_TYPE (decl);
10003 /* An elaborated-type-specifier sometimes introduces a new type and
10004 sometimes names an existing type. Normally, the rule is that it
10005 introduces a new type only if there is not an existing type of
10006 the same name already in scope. For example, given:
10009 void f() { struct S s; }
10011 the `struct S' in the body of `f' is the same `struct S' as in
10012 the global scope; the existing definition is used. However, if
10013 there were no global declaration, this would introduce a new
10014 local class named `S'.
10016 An exception to this rule applies to the following code:
10018 namespace N { struct S; }
10020 Here, the elaborated-type-specifier names a new type
10021 unconditionally; even if there is already an `S' in the
10022 containing scope this declaration names a new type.
10023 This exception only applies if the elaborated-type-specifier
10024 forms the complete declaration:
10028 A declaration consisting solely of `class-key identifier ;' is
10029 either a redeclaration of the name in the current scope or a
10030 forward declaration of the identifier as a class name. It
10031 introduces the name into the current scope.
10033 We are in this situation precisely when the next token is a `;'.
10035 An exception to the exception is that a `friend' declaration does
10036 *not* name a new type; i.e., given:
10038 struct S { friend struct T; };
10040 `T' is not a new type in the scope of `S'.
10042 Also, `new struct S' or `sizeof (struct S)' never results in the
10043 definition of a new type; a new type can only be declared in a
10044 declaration context. */
10048 /* Friends have special name lookup rules. */
10049 ts = ts_within_enclosing_non_class;
10050 else if (is_declaration
10051 && cp_lexer_next_token_is (parser->lexer,
10053 /* This is a `class-key identifier ;' */
10058 /* Warn about attributes. They are ignored. */
10060 warning (OPT_Wattributes,
10061 "type attributes are honored only at type definition");
10063 type = xref_tag (tag_type, identifier, ts,
10064 parser->num_template_parameter_lists);
10067 if (tag_type != enum_type)
10068 cp_parser_check_class_key (tag_type, type);
10070 /* A "<" cannot follow an elaborated type specifier. If that
10071 happens, the user was probably trying to form a template-id. */
10072 cp_parser_check_for_invalid_template_id (parser, type);
10077 /* Parse an enum-specifier.
10080 enum identifier [opt] { enumerator-list [opt] }
10083 enum identifier [opt] { enumerator-list [opt] } attributes
10085 Returns an ENUM_TYPE representing the enumeration. */
10088 cp_parser_enum_specifier (cp_parser* parser)
10093 /* Caller guarantees that the current token is 'enum', an identifier
10094 possibly follows, and the token after that is an opening brace.
10095 If we don't have an identifier, fabricate an anonymous name for
10096 the enumeration being defined. */
10097 cp_lexer_consume_token (parser->lexer);
10099 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10100 identifier = cp_parser_identifier (parser);
10102 identifier = make_anon_name ();
10104 /* Issue an error message if type-definitions are forbidden here. */
10105 cp_parser_check_type_definition (parser);
10107 /* Create the new type. We do this before consuming the opening brace
10108 so the enum will be recorded as being on the line of its tag (or the
10109 'enum' keyword, if there is no tag). */
10110 type = start_enum (identifier);
10112 /* Consume the opening brace. */
10113 cp_lexer_consume_token (parser->lexer);
10115 /* If the next token is not '}', then there are some enumerators. */
10116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10117 cp_parser_enumerator_list (parser, type);
10119 /* Consume the final '}'. */
10120 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10122 /* Look for trailing attributes to apply to this enumeration, and
10123 apply them if appropriate. */
10124 if (cp_parser_allow_gnu_extensions_p (parser))
10126 tree trailing_attr = cp_parser_attributes_opt (parser);
10127 cplus_decl_attributes (&type,
10129 (int) ATTR_FLAG_TYPE_IN_PLACE);
10132 /* Finish up the enumeration. */
10133 finish_enum (type);
10138 /* Parse an enumerator-list. The enumerators all have the indicated
10142 enumerator-definition
10143 enumerator-list , enumerator-definition */
10146 cp_parser_enumerator_list (cp_parser* parser, tree type)
10150 /* Parse an enumerator-definition. */
10151 cp_parser_enumerator_definition (parser, type);
10153 /* If the next token is not a ',', we've reached the end of
10155 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10157 /* Otherwise, consume the `,' and keep going. */
10158 cp_lexer_consume_token (parser->lexer);
10159 /* If the next token is a `}', there is a trailing comma. */
10160 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10162 if (pedantic && !in_system_header)
10163 pedwarn ("comma at end of enumerator list");
10169 /* Parse an enumerator-definition. The enumerator has the indicated
10172 enumerator-definition:
10174 enumerator = constant-expression
10180 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10185 /* Look for the identifier. */
10186 identifier = cp_parser_identifier (parser);
10187 if (identifier == error_mark_node)
10190 /* If the next token is an '=', then there is an explicit value. */
10191 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10193 /* Consume the `=' token. */
10194 cp_lexer_consume_token (parser->lexer);
10195 /* Parse the value. */
10196 value = cp_parser_constant_expression (parser,
10197 /*allow_non_constant_p=*/false,
10203 /* Create the enumerator. */
10204 build_enumerator (identifier, value, type);
10207 /* Parse a namespace-name.
10210 original-namespace-name
10213 Returns the NAMESPACE_DECL for the namespace. */
10216 cp_parser_namespace_name (cp_parser* parser)
10219 tree namespace_decl;
10221 /* Get the name of the namespace. */
10222 identifier = cp_parser_identifier (parser);
10223 if (identifier == error_mark_node)
10224 return error_mark_node;
10226 /* Look up the identifier in the currently active scope. Look only
10227 for namespaces, due to:
10229 [basic.lookup.udir]
10231 When looking up a namespace-name in a using-directive or alias
10232 definition, only namespace names are considered.
10236 [basic.lookup.qual]
10238 During the lookup of a name preceding the :: scope resolution
10239 operator, object, function, and enumerator names are ignored.
10241 (Note that cp_parser_class_or_namespace_name only calls this
10242 function if the token after the name is the scope resolution
10244 namespace_decl = cp_parser_lookup_name (parser, identifier,
10246 /*is_template=*/false,
10247 /*is_namespace=*/true,
10248 /*check_dependency=*/true,
10249 /*ambiguous_p=*/NULL);
10250 /* If it's not a namespace, issue an error. */
10251 if (namespace_decl == error_mark_node
10252 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10254 cp_parser_error (parser, "expected namespace-name");
10255 namespace_decl = error_mark_node;
10258 return namespace_decl;
10261 /* Parse a namespace-definition.
10263 namespace-definition:
10264 named-namespace-definition
10265 unnamed-namespace-definition
10267 named-namespace-definition:
10268 original-namespace-definition
10269 extension-namespace-definition
10271 original-namespace-definition:
10272 namespace identifier { namespace-body }
10274 extension-namespace-definition:
10275 namespace original-namespace-name { namespace-body }
10277 unnamed-namespace-definition:
10278 namespace { namespace-body } */
10281 cp_parser_namespace_definition (cp_parser* parser)
10285 /* Look for the `namespace' keyword. */
10286 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10288 /* Get the name of the namespace. We do not attempt to distinguish
10289 between an original-namespace-definition and an
10290 extension-namespace-definition at this point. The semantic
10291 analysis routines are responsible for that. */
10292 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10293 identifier = cp_parser_identifier (parser);
10295 identifier = NULL_TREE;
10297 /* Look for the `{' to start the namespace. */
10298 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10299 /* Start the namespace. */
10300 push_namespace (identifier);
10301 /* Parse the body of the namespace. */
10302 cp_parser_namespace_body (parser);
10303 /* Finish the namespace. */
10305 /* Look for the final `}'. */
10306 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10309 /* Parse a namespace-body.
10312 declaration-seq [opt] */
10315 cp_parser_namespace_body (cp_parser* parser)
10317 cp_parser_declaration_seq_opt (parser);
10320 /* Parse a namespace-alias-definition.
10322 namespace-alias-definition:
10323 namespace identifier = qualified-namespace-specifier ; */
10326 cp_parser_namespace_alias_definition (cp_parser* parser)
10329 tree namespace_specifier;
10331 /* Look for the `namespace' keyword. */
10332 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10333 /* Look for the identifier. */
10334 identifier = cp_parser_identifier (parser);
10335 if (identifier == error_mark_node)
10337 /* Look for the `=' token. */
10338 cp_parser_require (parser, CPP_EQ, "`='");
10339 /* Look for the qualified-namespace-specifier. */
10340 namespace_specifier
10341 = cp_parser_qualified_namespace_specifier (parser);
10342 /* Look for the `;' token. */
10343 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10345 /* Register the alias in the symbol table. */
10346 do_namespace_alias (identifier, namespace_specifier);
10349 /* Parse a qualified-namespace-specifier.
10351 qualified-namespace-specifier:
10352 :: [opt] nested-name-specifier [opt] namespace-name
10354 Returns a NAMESPACE_DECL corresponding to the specified
10358 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10360 /* Look for the optional `::'. */
10361 cp_parser_global_scope_opt (parser,
10362 /*current_scope_valid_p=*/false);
10364 /* Look for the optional nested-name-specifier. */
10365 cp_parser_nested_name_specifier_opt (parser,
10366 /*typename_keyword_p=*/false,
10367 /*check_dependency_p=*/true,
10369 /*is_declaration=*/true);
10371 return cp_parser_namespace_name (parser);
10374 /* Parse a using-declaration.
10377 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10378 using :: unqualified-id ; */
10381 cp_parser_using_declaration (cp_parser* parser)
10384 bool typename_p = false;
10385 bool global_scope_p;
10390 /* Look for the `using' keyword. */
10391 cp_parser_require_keyword (parser, RID_USING, "`using'");
10393 /* Peek at the next token. */
10394 token = cp_lexer_peek_token (parser->lexer);
10395 /* See if it's `typename'. */
10396 if (token->keyword == RID_TYPENAME)
10398 /* Remember that we've seen it. */
10400 /* Consume the `typename' token. */
10401 cp_lexer_consume_token (parser->lexer);
10404 /* Look for the optional global scope qualification. */
10406 = (cp_parser_global_scope_opt (parser,
10407 /*current_scope_valid_p=*/false)
10410 /* If we saw `typename', or didn't see `::', then there must be a
10411 nested-name-specifier present. */
10412 if (typename_p || !global_scope_p)
10413 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10414 /*check_dependency_p=*/true,
10416 /*is_declaration=*/true);
10417 /* Otherwise, we could be in either of the two productions. In that
10418 case, treat the nested-name-specifier as optional. */
10420 qscope = cp_parser_nested_name_specifier_opt (parser,
10421 /*typename_keyword_p=*/false,
10422 /*check_dependency_p=*/true,
10424 /*is_declaration=*/true);
10426 qscope = global_namespace;
10428 /* Parse the unqualified-id. */
10429 identifier = cp_parser_unqualified_id (parser,
10430 /*template_keyword_p=*/false,
10431 /*check_dependency_p=*/true,
10432 /*declarator_p=*/true);
10434 /* The function we call to handle a using-declaration is different
10435 depending on what scope we are in. */
10436 if (identifier == error_mark_node)
10438 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10439 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10440 /* [namespace.udecl]
10442 A using declaration shall not name a template-id. */
10443 error ("a template-id may not appear in a using-declaration");
10446 if (at_class_scope_p ())
10448 /* Create the USING_DECL. */
10449 decl = do_class_using_decl (parser->scope, identifier);
10450 /* Add it to the list of members in this class. */
10451 finish_member_declaration (decl);
10455 decl = cp_parser_lookup_name_simple (parser, identifier);
10456 if (decl == error_mark_node)
10457 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10458 else if (!at_namespace_scope_p ())
10459 do_local_using_decl (decl, qscope, identifier);
10461 do_toplevel_using_decl (decl, qscope, identifier);
10465 /* Look for the final `;'. */
10466 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10469 /* Parse a using-directive.
10472 using namespace :: [opt] nested-name-specifier [opt]
10473 namespace-name ; */
10476 cp_parser_using_directive (cp_parser* parser)
10478 tree namespace_decl;
10481 /* Look for the `using' keyword. */
10482 cp_parser_require_keyword (parser, RID_USING, "`using'");
10483 /* And the `namespace' keyword. */
10484 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10485 /* Look for the optional `::' operator. */
10486 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10487 /* And the optional nested-name-specifier. */
10488 cp_parser_nested_name_specifier_opt (parser,
10489 /*typename_keyword_p=*/false,
10490 /*check_dependency_p=*/true,
10492 /*is_declaration=*/true);
10493 /* Get the namespace being used. */
10494 namespace_decl = cp_parser_namespace_name (parser);
10495 /* And any specified attributes. */
10496 attribs = cp_parser_attributes_opt (parser);
10497 /* Update the symbol table. */
10498 parse_using_directive (namespace_decl, attribs);
10499 /* Look for the final `;'. */
10500 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10503 /* Parse an asm-definition.
10506 asm ( string-literal ) ;
10511 asm volatile [opt] ( string-literal ) ;
10512 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10513 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10514 : asm-operand-list [opt] ) ;
10515 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10516 : asm-operand-list [opt]
10517 : asm-operand-list [opt] ) ; */
10520 cp_parser_asm_definition (cp_parser* parser)
10523 tree outputs = NULL_TREE;
10524 tree inputs = NULL_TREE;
10525 tree clobbers = NULL_TREE;
10527 bool volatile_p = false;
10528 bool extended_p = false;
10530 /* Look for the `asm' keyword. */
10531 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10532 /* See if the next token is `volatile'. */
10533 if (cp_parser_allow_gnu_extensions_p (parser)
10534 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10536 /* Remember that we saw the `volatile' keyword. */
10538 /* Consume the token. */
10539 cp_lexer_consume_token (parser->lexer);
10541 /* Look for the opening `('. */
10542 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10544 /* Look for the string. */
10545 string = cp_parser_string_literal (parser, false, false);
10546 if (string == error_mark_node)
10548 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10549 /*consume_paren=*/true);
10553 /* If we're allowing GNU extensions, check for the extended assembly
10554 syntax. Unfortunately, the `:' tokens need not be separated by
10555 a space in C, and so, for compatibility, we tolerate that here
10556 too. Doing that means that we have to treat the `::' operator as
10558 if (cp_parser_allow_gnu_extensions_p (parser)
10559 && at_function_scope_p ()
10560 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10561 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10563 bool inputs_p = false;
10564 bool clobbers_p = false;
10566 /* The extended syntax was used. */
10569 /* Look for outputs. */
10570 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10572 /* Consume the `:'. */
10573 cp_lexer_consume_token (parser->lexer);
10574 /* Parse the output-operands. */
10575 if (cp_lexer_next_token_is_not (parser->lexer,
10577 && cp_lexer_next_token_is_not (parser->lexer,
10579 && cp_lexer_next_token_is_not (parser->lexer,
10581 outputs = cp_parser_asm_operand_list (parser);
10583 /* If the next token is `::', there are no outputs, and the
10584 next token is the beginning of the inputs. */
10585 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10586 /* The inputs are coming next. */
10589 /* Look for inputs. */
10591 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10593 /* Consume the `:' or `::'. */
10594 cp_lexer_consume_token (parser->lexer);
10595 /* Parse the output-operands. */
10596 if (cp_lexer_next_token_is_not (parser->lexer,
10598 && cp_lexer_next_token_is_not (parser->lexer,
10600 inputs = cp_parser_asm_operand_list (parser);
10602 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10603 /* The clobbers are coming next. */
10606 /* Look for clobbers. */
10608 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10610 /* Consume the `:' or `::'. */
10611 cp_lexer_consume_token (parser->lexer);
10612 /* Parse the clobbers. */
10613 if (cp_lexer_next_token_is_not (parser->lexer,
10615 clobbers = cp_parser_asm_clobber_list (parser);
10618 /* Look for the closing `)'. */
10619 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10620 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10621 /*consume_paren=*/true);
10622 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10624 /* Create the ASM_EXPR. */
10625 if (at_function_scope_p ())
10627 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10629 /* If the extended syntax was not used, mark the ASM_EXPR. */
10632 tree temp = asm_stmt;
10633 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10634 temp = TREE_OPERAND (temp, 0);
10636 ASM_INPUT_P (temp) = 1;
10640 assemble_asm (string);
10643 /* Declarators [gram.dcl.decl] */
10645 /* Parse an init-declarator.
10648 declarator initializer [opt]
10653 declarator asm-specification [opt] attributes [opt] initializer [opt]
10655 function-definition:
10656 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10658 decl-specifier-seq [opt] declarator function-try-block
10662 function-definition:
10663 __extension__ function-definition
10665 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10666 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10667 then this declarator appears in a class scope. The new DECL created
10668 by this declarator is returned.
10670 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10671 for a function-definition here as well. If the declarator is a
10672 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10673 be TRUE upon return. By that point, the function-definition will
10674 have been completely parsed.
10676 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10680 cp_parser_init_declarator (cp_parser* parser,
10681 cp_decl_specifier_seq *decl_specifiers,
10682 bool function_definition_allowed_p,
10684 int declares_class_or_enum,
10685 bool* function_definition_p)
10688 cp_declarator *declarator;
10689 tree prefix_attributes;
10691 tree asm_specification;
10693 tree decl = NULL_TREE;
10695 bool is_initialized;
10696 bool is_parenthesized_init;
10697 bool is_non_constant_init;
10698 int ctor_dtor_or_conv_p;
10700 tree pushed_scope = NULL;
10702 /* Gather the attributes that were provided with the
10703 decl-specifiers. */
10704 prefix_attributes = decl_specifiers->attributes;
10706 /* Assume that this is not the declarator for a function
10708 if (function_definition_p)
10709 *function_definition_p = false;
10711 /* Defer access checks while parsing the declarator; we cannot know
10712 what names are accessible until we know what is being
10714 resume_deferring_access_checks ();
10716 /* Parse the declarator. */
10718 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10719 &ctor_dtor_or_conv_p,
10720 /*parenthesized_p=*/NULL,
10721 /*member_p=*/false);
10722 /* Gather up the deferred checks. */
10723 stop_deferring_access_checks ();
10725 /* If the DECLARATOR was erroneous, there's no need to go
10727 if (declarator == cp_error_declarator)
10728 return error_mark_node;
10730 if (declares_class_or_enum & 2)
10731 cp_parser_check_for_definition_in_return_type (declarator,
10732 decl_specifiers->type);
10734 /* Figure out what scope the entity declared by the DECLARATOR is
10735 located in. `grokdeclarator' sometimes changes the scope, so
10736 we compute it now. */
10737 scope = get_scope_of_declarator (declarator);
10739 /* If we're allowing GNU extensions, look for an asm-specification
10741 if (cp_parser_allow_gnu_extensions_p (parser))
10743 /* Look for an asm-specification. */
10744 asm_specification = cp_parser_asm_specification_opt (parser);
10745 /* And attributes. */
10746 attributes = cp_parser_attributes_opt (parser);
10750 asm_specification = NULL_TREE;
10751 attributes = NULL_TREE;
10754 /* Peek at the next token. */
10755 token = cp_lexer_peek_token (parser->lexer);
10756 /* Check to see if the token indicates the start of a
10757 function-definition. */
10758 if (cp_parser_token_starts_function_definition_p (token))
10760 if (!function_definition_allowed_p)
10762 /* If a function-definition should not appear here, issue an
10764 cp_parser_error (parser,
10765 "a function-definition is not allowed here");
10766 return error_mark_node;
10770 /* Neither attributes nor an asm-specification are allowed
10771 on a function-definition. */
10772 if (asm_specification)
10773 error ("an asm-specification is not allowed on a function-definition");
10775 error ("attributes are not allowed on a function-definition");
10776 /* This is a function-definition. */
10777 *function_definition_p = true;
10779 /* Parse the function definition. */
10781 decl = cp_parser_save_member_function_body (parser,
10784 prefix_attributes);
10787 = (cp_parser_function_definition_from_specifiers_and_declarator
10788 (parser, decl_specifiers, prefix_attributes, declarator));
10796 Only in function declarations for constructors, destructors, and
10797 type conversions can the decl-specifier-seq be omitted.
10799 We explicitly postpone this check past the point where we handle
10800 function-definitions because we tolerate function-definitions
10801 that are missing their return types in some modes. */
10802 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10804 cp_parser_error (parser,
10805 "expected constructor, destructor, or type conversion");
10806 return error_mark_node;
10809 /* An `=' or an `(' indicates an initializer. */
10810 is_initialized = (token->type == CPP_EQ
10811 || token->type == CPP_OPEN_PAREN);
10812 /* If the init-declarator isn't initialized and isn't followed by a
10813 `,' or `;', it's not a valid init-declarator. */
10814 if (!is_initialized
10815 && token->type != CPP_COMMA
10816 && token->type != CPP_SEMICOLON)
10818 cp_parser_error (parser, "expected initializer");
10819 return error_mark_node;
10822 /* Because start_decl has side-effects, we should only call it if we
10823 know we're going ahead. By this point, we know that we cannot
10824 possibly be looking at any other construct. */
10825 cp_parser_commit_to_tentative_parse (parser);
10827 /* If the decl specifiers were bad, issue an error now that we're
10828 sure this was intended to be a declarator. Then continue
10829 declaring the variable(s), as int, to try to cut down on further
10831 if (decl_specifiers->any_specifiers_p
10832 && decl_specifiers->type == error_mark_node)
10834 cp_parser_error (parser, "invalid type in declaration");
10835 decl_specifiers->type = integer_type_node;
10838 /* Check to see whether or not this declaration is a friend. */
10839 friend_p = cp_parser_friend_p (decl_specifiers);
10841 /* Check that the number of template-parameter-lists is OK. */
10842 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10843 return error_mark_node;
10845 /* Enter the newly declared entry in the symbol table. If we're
10846 processing a declaration in a class-specifier, we wait until
10847 after processing the initializer. */
10850 if (parser->in_unbraced_linkage_specification_p)
10852 decl_specifiers->storage_class = sc_extern;
10853 have_extern_spec = false;
10855 decl = start_decl (declarator, decl_specifiers,
10856 is_initialized, attributes, prefix_attributes,
10860 /* Enter the SCOPE. That way unqualified names appearing in the
10861 initializer will be looked up in SCOPE. */
10862 pushed_scope = push_scope (scope);
10864 /* Perform deferred access control checks, now that we know in which
10865 SCOPE the declared entity resides. */
10866 if (!member_p && decl)
10868 tree saved_current_function_decl = NULL_TREE;
10870 /* If the entity being declared is a function, pretend that we
10871 are in its scope. If it is a `friend', it may have access to
10872 things that would not otherwise be accessible. */
10873 if (TREE_CODE (decl) == FUNCTION_DECL)
10875 saved_current_function_decl = current_function_decl;
10876 current_function_decl = decl;
10879 /* Perform the access control checks for the declarator and the
10880 the decl-specifiers. */
10881 perform_deferred_access_checks ();
10883 /* Restore the saved value. */
10884 if (TREE_CODE (decl) == FUNCTION_DECL)
10885 current_function_decl = saved_current_function_decl;
10888 /* Parse the initializer. */
10889 if (is_initialized)
10890 initializer = cp_parser_initializer (parser,
10891 &is_parenthesized_init,
10892 &is_non_constant_init);
10895 initializer = NULL_TREE;
10896 is_parenthesized_init = false;
10897 is_non_constant_init = true;
10900 /* The old parser allows attributes to appear after a parenthesized
10901 initializer. Mark Mitchell proposed removing this functionality
10902 on the GCC mailing lists on 2002-08-13. This parser accepts the
10903 attributes -- but ignores them. */
10904 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10905 if (cp_parser_attributes_opt (parser))
10906 warning (OPT_Wattributes,
10907 "attributes after parenthesized initializer ignored");
10909 /* For an in-class declaration, use `grokfield' to create the
10915 pop_scope (pushed_scope);
10916 pushed_scope = false;
10918 decl = grokfield (declarator, decl_specifiers,
10919 initializer, /*asmspec=*/NULL_TREE,
10920 /*attributes=*/NULL_TREE);
10921 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10922 cp_parser_save_default_args (parser, decl);
10925 /* Finish processing the declaration. But, skip friend
10927 if (!friend_p && decl && decl != error_mark_node)
10929 cp_finish_decl (decl,
10932 /* If the initializer is in parentheses, then this is
10933 a direct-initialization, which means that an
10934 `explicit' constructor is OK. Otherwise, an
10935 `explicit' constructor cannot be used. */
10936 ((is_parenthesized_init || !is_initialized)
10937 ? 0 : LOOKUP_ONLYCONVERTING));
10939 if (!friend_p && pushed_scope)
10940 pop_scope (pushed_scope);
10942 /* Remember whether or not variables were initialized by
10943 constant-expressions. */
10944 if (decl && TREE_CODE (decl) == VAR_DECL
10945 && is_initialized && !is_non_constant_init)
10946 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10951 /* Parse a declarator.
10955 ptr-operator declarator
10957 abstract-declarator:
10958 ptr-operator abstract-declarator [opt]
10959 direct-abstract-declarator
10964 attributes [opt] direct-declarator
10965 attributes [opt] ptr-operator declarator
10967 abstract-declarator:
10968 attributes [opt] ptr-operator abstract-declarator [opt]
10969 attributes [opt] direct-abstract-declarator
10971 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10972 detect constructor, destructor or conversion operators. It is set
10973 to -1 if the declarator is a name, and +1 if it is a
10974 function. Otherwise it is set to zero. Usually you just want to
10975 test for >0, but internally the negative value is used.
10977 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10978 a decl-specifier-seq unless it declares a constructor, destructor,
10979 or conversion. It might seem that we could check this condition in
10980 semantic analysis, rather than parsing, but that makes it difficult
10981 to handle something like `f()'. We want to notice that there are
10982 no decl-specifiers, and therefore realize that this is an
10983 expression, not a declaration.)
10985 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10986 the declarator is a direct-declarator of the form "(...)".
10988 MEMBER_P is true iff this declarator is a member-declarator. */
10990 static cp_declarator *
10991 cp_parser_declarator (cp_parser* parser,
10992 cp_parser_declarator_kind dcl_kind,
10993 int* ctor_dtor_or_conv_p,
10994 bool* parenthesized_p,
10998 cp_declarator *declarator;
10999 enum tree_code code;
11000 cp_cv_quals cv_quals;
11002 tree attributes = NULL_TREE;
11004 /* Assume this is not a constructor, destructor, or type-conversion
11006 if (ctor_dtor_or_conv_p)
11007 *ctor_dtor_or_conv_p = 0;
11009 if (cp_parser_allow_gnu_extensions_p (parser))
11010 attributes = cp_parser_attributes_opt (parser);
11012 /* Peek at the next token. */
11013 token = cp_lexer_peek_token (parser->lexer);
11015 /* Check for the ptr-operator production. */
11016 cp_parser_parse_tentatively (parser);
11017 /* Parse the ptr-operator. */
11018 code = cp_parser_ptr_operator (parser,
11021 /* If that worked, then we have a ptr-operator. */
11022 if (cp_parser_parse_definitely (parser))
11024 /* If a ptr-operator was found, then this declarator was not
11026 if (parenthesized_p)
11027 *parenthesized_p = true;
11028 /* The dependent declarator is optional if we are parsing an
11029 abstract-declarator. */
11030 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11031 cp_parser_parse_tentatively (parser);
11033 /* Parse the dependent declarator. */
11034 declarator = cp_parser_declarator (parser, dcl_kind,
11035 /*ctor_dtor_or_conv_p=*/NULL,
11036 /*parenthesized_p=*/NULL,
11037 /*member_p=*/false);
11039 /* If we are parsing an abstract-declarator, we must handle the
11040 case where the dependent declarator is absent. */
11041 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11042 && !cp_parser_parse_definitely (parser))
11045 /* Build the representation of the ptr-operator. */
11047 declarator = make_ptrmem_declarator (cv_quals,
11050 else if (code == INDIRECT_REF)
11051 declarator = make_pointer_declarator (cv_quals, declarator);
11053 declarator = make_reference_declarator (cv_quals, declarator);
11055 /* Everything else is a direct-declarator. */
11058 if (parenthesized_p)
11059 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11061 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11062 ctor_dtor_or_conv_p,
11066 if (attributes && declarator != cp_error_declarator)
11067 declarator->attributes = attributes;
11072 /* Parse a direct-declarator or direct-abstract-declarator.
11076 direct-declarator ( parameter-declaration-clause )
11077 cv-qualifier-seq [opt]
11078 exception-specification [opt]
11079 direct-declarator [ constant-expression [opt] ]
11082 direct-abstract-declarator:
11083 direct-abstract-declarator [opt]
11084 ( parameter-declaration-clause )
11085 cv-qualifier-seq [opt]
11086 exception-specification [opt]
11087 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11088 ( abstract-declarator )
11090 Returns a representation of the declarator. DCL_KIND is
11091 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11092 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11093 we are parsing a direct-declarator. It is
11094 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11095 of ambiguity we prefer an abstract declarator, as per
11096 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11097 cp_parser_declarator. */
11099 static cp_declarator *
11100 cp_parser_direct_declarator (cp_parser* parser,
11101 cp_parser_declarator_kind dcl_kind,
11102 int* ctor_dtor_or_conv_p,
11106 cp_declarator *declarator = NULL;
11107 tree scope = NULL_TREE;
11108 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11109 bool saved_in_declarator_p = parser->in_declarator_p;
11111 tree pushed_scope = NULL_TREE;
11115 /* Peek at the next token. */
11116 token = cp_lexer_peek_token (parser->lexer);
11117 if (token->type == CPP_OPEN_PAREN)
11119 /* This is either a parameter-declaration-clause, or a
11120 parenthesized declarator. When we know we are parsing a
11121 named declarator, it must be a parenthesized declarator
11122 if FIRST is true. For instance, `(int)' is a
11123 parameter-declaration-clause, with an omitted
11124 direct-abstract-declarator. But `((*))', is a
11125 parenthesized abstract declarator. Finally, when T is a
11126 template parameter `(T)' is a
11127 parameter-declaration-clause, and not a parenthesized
11130 We first try and parse a parameter-declaration-clause,
11131 and then try a nested declarator (if FIRST is true).
11133 It is not an error for it not to be a
11134 parameter-declaration-clause, even when FIRST is
11140 The first is the declaration of a function while the
11141 second is a the definition of a variable, including its
11144 Having seen only the parenthesis, we cannot know which of
11145 these two alternatives should be selected. Even more
11146 complex are examples like:
11151 The former is a function-declaration; the latter is a
11152 variable initialization.
11154 Thus again, we try a parameter-declaration-clause, and if
11155 that fails, we back out and return. */
11157 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11159 cp_parameter_declarator *params;
11160 unsigned saved_num_template_parameter_lists;
11162 /* In a member-declarator, the only valid interpretation
11163 of a parenthesis is the start of a
11164 parameter-declaration-clause. (It is invalid to
11165 initialize a static data member with a parenthesized
11166 initializer; only the "=" form of initialization is
11169 cp_parser_parse_tentatively (parser);
11171 /* Consume the `('. */
11172 cp_lexer_consume_token (parser->lexer);
11175 /* If this is going to be an abstract declarator, we're
11176 in a declarator and we can't have default args. */
11177 parser->default_arg_ok_p = false;
11178 parser->in_declarator_p = true;
11181 /* Inside the function parameter list, surrounding
11182 template-parameter-lists do not apply. */
11183 saved_num_template_parameter_lists
11184 = parser->num_template_parameter_lists;
11185 parser->num_template_parameter_lists = 0;
11187 /* Parse the parameter-declaration-clause. */
11188 params = cp_parser_parameter_declaration_clause (parser);
11190 parser->num_template_parameter_lists
11191 = saved_num_template_parameter_lists;
11193 /* If all went well, parse the cv-qualifier-seq and the
11194 exception-specification. */
11195 if (member_p || cp_parser_parse_definitely (parser))
11197 cp_cv_quals cv_quals;
11198 tree exception_specification;
11200 if (ctor_dtor_or_conv_p)
11201 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11203 /* Consume the `)'. */
11204 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11206 /* Parse the cv-qualifier-seq. */
11207 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11208 /* And the exception-specification. */
11209 exception_specification
11210 = cp_parser_exception_specification_opt (parser);
11212 /* Create the function-declarator. */
11213 declarator = make_call_declarator (declarator,
11216 exception_specification);
11217 /* Any subsequent parameter lists are to do with
11218 return type, so are not those of the declared
11220 parser->default_arg_ok_p = false;
11222 /* Repeat the main loop. */
11227 /* If this is the first, we can try a parenthesized
11231 bool saved_in_type_id_in_expr_p;
11233 parser->default_arg_ok_p = saved_default_arg_ok_p;
11234 parser->in_declarator_p = saved_in_declarator_p;
11236 /* Consume the `('. */
11237 cp_lexer_consume_token (parser->lexer);
11238 /* Parse the nested declarator. */
11239 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11240 parser->in_type_id_in_expr_p = true;
11242 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11243 /*parenthesized_p=*/NULL,
11245 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11247 /* Expect a `)'. */
11248 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11249 declarator = cp_error_declarator;
11250 if (declarator == cp_error_declarator)
11253 goto handle_declarator;
11255 /* Otherwise, we must be done. */
11259 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11260 && token->type == CPP_OPEN_SQUARE)
11262 /* Parse an array-declarator. */
11265 if (ctor_dtor_or_conv_p)
11266 *ctor_dtor_or_conv_p = 0;
11269 parser->default_arg_ok_p = false;
11270 parser->in_declarator_p = true;
11271 /* Consume the `['. */
11272 cp_lexer_consume_token (parser->lexer);
11273 /* Peek at the next token. */
11274 token = cp_lexer_peek_token (parser->lexer);
11275 /* If the next token is `]', then there is no
11276 constant-expression. */
11277 if (token->type != CPP_CLOSE_SQUARE)
11279 bool non_constant_p;
11282 = cp_parser_constant_expression (parser,
11283 /*allow_non_constant=*/true,
11285 if (!non_constant_p)
11286 bounds = fold_non_dependent_expr (bounds);
11287 /* Normally, the array bound must be an integral constant
11288 expression. However, as an extension, we allow VLAs
11289 in function scopes. */
11290 else if (!at_function_scope_p ())
11292 error ("array bound is not an integer constant");
11293 bounds = error_mark_node;
11297 bounds = NULL_TREE;
11298 /* Look for the closing `]'. */
11299 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11301 declarator = cp_error_declarator;
11305 declarator = make_array_declarator (declarator, bounds);
11307 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11309 tree qualifying_scope;
11310 tree unqualified_name;
11312 /* Parse a declarator-id */
11313 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11314 cp_parser_parse_tentatively (parser);
11315 unqualified_name = cp_parser_declarator_id (parser);
11316 qualifying_scope = parser->scope;
11317 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11319 if (!cp_parser_parse_definitely (parser))
11320 unqualified_name = error_mark_node;
11321 else if (qualifying_scope
11322 || (TREE_CODE (unqualified_name)
11323 != IDENTIFIER_NODE))
11325 cp_parser_error (parser, "expected unqualified-id");
11326 unqualified_name = error_mark_node;
11330 if (unqualified_name == error_mark_node)
11332 declarator = cp_error_declarator;
11336 if (qualifying_scope && at_namespace_scope_p ()
11337 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11339 /* In the declaration of a member of a template class
11340 outside of the class itself, the SCOPE will sometimes
11341 be a TYPENAME_TYPE. For example, given:
11343 template <typename T>
11344 int S<T>::R::i = 3;
11346 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11347 this context, we must resolve S<T>::R to an ordinary
11348 type, rather than a typename type.
11350 The reason we normally avoid resolving TYPENAME_TYPEs
11351 is that a specialization of `S' might render
11352 `S<T>::R' not a type. However, if `S' is
11353 specialized, then this `i' will not be used, so there
11354 is no harm in resolving the types here. */
11357 /* Resolve the TYPENAME_TYPE. */
11358 type = resolve_typename_type (qualifying_scope,
11359 /*only_current_p=*/false);
11360 /* If that failed, the declarator is invalid. */
11361 if (type == error_mark_node)
11362 error ("%<%T::%D%> is not a type",
11363 TYPE_CONTEXT (qualifying_scope),
11364 TYPE_IDENTIFIER (qualifying_scope));
11365 qualifying_scope = type;
11368 declarator = make_id_declarator (qualifying_scope,
11370 declarator->id_loc = token->location;
11371 if (unqualified_name)
11375 if (qualifying_scope
11376 && CLASS_TYPE_P (qualifying_scope))
11377 class_type = qualifying_scope;
11379 class_type = current_class_type;
11383 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11384 declarator->u.id.sfk = sfk_destructor;
11385 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11386 declarator->u.id.sfk = sfk_conversion;
11387 else if (/* There's no way to declare a constructor
11388 for an anonymous type, even if the type
11389 got a name for linkage purposes. */
11390 !TYPE_WAS_ANONYMOUS (class_type)
11391 && (constructor_name_p (unqualified_name,
11393 || (TREE_CODE (unqualified_name) == TYPE_DECL
11395 (TREE_TYPE (unqualified_name),
11397 declarator->u.id.sfk = sfk_constructor;
11399 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11400 *ctor_dtor_or_conv_p = -1;
11401 if (qualifying_scope
11402 && TREE_CODE (unqualified_name) == TYPE_DECL
11403 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11405 error ("invalid use of constructor as a template");
11406 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11407 "the constructor in a qualified name",
11409 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11410 class_type, class_type);
11415 handle_declarator:;
11416 scope = get_scope_of_declarator (declarator);
11418 /* Any names that appear after the declarator-id for a
11419 member are looked up in the containing scope. */
11420 pushed_scope = push_scope (scope);
11421 parser->in_declarator_p = true;
11422 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11423 || (declarator && declarator->kind == cdk_id))
11424 /* Default args are only allowed on function
11426 parser->default_arg_ok_p = saved_default_arg_ok_p;
11428 parser->default_arg_ok_p = false;
11437 /* For an abstract declarator, we might wind up with nothing at this
11438 point. That's an error; the declarator is not optional. */
11440 cp_parser_error (parser, "expected declarator");
11442 /* If we entered a scope, we must exit it now. */
11444 pop_scope (pushed_scope);
11446 parser->default_arg_ok_p = saved_default_arg_ok_p;
11447 parser->in_declarator_p = saved_in_declarator_p;
11452 /* Parse a ptr-operator.
11455 * cv-qualifier-seq [opt]
11457 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11462 & cv-qualifier-seq [opt]
11464 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11465 Returns ADDR_EXPR if a reference was used. In the case of a
11466 pointer-to-member, *TYPE is filled in with the TYPE containing the
11467 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11468 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11469 ERROR_MARK if an error occurred. */
11471 static enum tree_code
11472 cp_parser_ptr_operator (cp_parser* parser,
11474 cp_cv_quals *cv_quals)
11476 enum tree_code code = ERROR_MARK;
11479 /* Assume that it's not a pointer-to-member. */
11481 /* And that there are no cv-qualifiers. */
11482 *cv_quals = TYPE_UNQUALIFIED;
11484 /* Peek at the next token. */
11485 token = cp_lexer_peek_token (parser->lexer);
11486 /* If it's a `*' or `&' we have a pointer or reference. */
11487 if (token->type == CPP_MULT || token->type == CPP_AND)
11489 /* Remember which ptr-operator we were processing. */
11490 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11492 /* Consume the `*' or `&'. */
11493 cp_lexer_consume_token (parser->lexer);
11495 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11496 `&', if we are allowing GNU extensions. (The only qualifier
11497 that can legally appear after `&' is `restrict', but that is
11498 enforced during semantic analysis. */
11499 if (code == INDIRECT_REF
11500 || cp_parser_allow_gnu_extensions_p (parser))
11501 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11505 /* Try the pointer-to-member case. */
11506 cp_parser_parse_tentatively (parser);
11507 /* Look for the optional `::' operator. */
11508 cp_parser_global_scope_opt (parser,
11509 /*current_scope_valid_p=*/false);
11510 /* Look for the nested-name specifier. */
11511 cp_parser_nested_name_specifier (parser,
11512 /*typename_keyword_p=*/false,
11513 /*check_dependency_p=*/true,
11515 /*is_declaration=*/false);
11516 /* If we found it, and the next token is a `*', then we are
11517 indeed looking at a pointer-to-member operator. */
11518 if (!cp_parser_error_occurred (parser)
11519 && cp_parser_require (parser, CPP_MULT, "`*'"))
11521 /* The type of which the member is a member is given by the
11523 *type = parser->scope;
11524 /* The next name will not be qualified. */
11525 parser->scope = NULL_TREE;
11526 parser->qualifying_scope = NULL_TREE;
11527 parser->object_scope = NULL_TREE;
11528 /* Indicate that the `*' operator was used. */
11529 code = INDIRECT_REF;
11530 /* Look for the optional cv-qualifier-seq. */
11531 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11533 /* If that didn't work we don't have a ptr-operator. */
11534 if (!cp_parser_parse_definitely (parser))
11535 cp_parser_error (parser, "expected ptr-operator");
11541 /* Parse an (optional) cv-qualifier-seq.
11544 cv-qualifier cv-qualifier-seq [opt]
11555 Returns a bitmask representing the cv-qualifiers. */
11558 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11560 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11565 cp_cv_quals cv_qualifier;
11567 /* Peek at the next token. */
11568 token = cp_lexer_peek_token (parser->lexer);
11569 /* See if it's a cv-qualifier. */
11570 switch (token->keyword)
11573 cv_qualifier = TYPE_QUAL_CONST;
11577 cv_qualifier = TYPE_QUAL_VOLATILE;
11581 cv_qualifier = TYPE_QUAL_RESTRICT;
11585 cv_qualifier = TYPE_UNQUALIFIED;
11592 if (cv_quals & cv_qualifier)
11594 error ("duplicate cv-qualifier");
11595 cp_lexer_purge_token (parser->lexer);
11599 cp_lexer_consume_token (parser->lexer);
11600 cv_quals |= cv_qualifier;
11607 /* Parse a declarator-id.
11611 :: [opt] nested-name-specifier [opt] type-name
11613 In the `id-expression' case, the value returned is as for
11614 cp_parser_id_expression if the id-expression was an unqualified-id.
11615 If the id-expression was a qualified-id, then a SCOPE_REF is
11616 returned. The first operand is the scope (either a NAMESPACE_DECL
11617 or TREE_TYPE), but the second is still just a representation of an
11621 cp_parser_declarator_id (cp_parser* parser)
11623 /* The expression must be an id-expression. Assume that qualified
11624 names are the names of types so that:
11627 int S<T>::R::i = 3;
11629 will work; we must treat `S<T>::R' as the name of a type.
11630 Similarly, assume that qualified names are templates, where
11634 int S<T>::R<T>::i = 3;
11637 return cp_parser_id_expression (parser,
11638 /*template_keyword_p=*/false,
11639 /*check_dependency_p=*/false,
11640 /*template_p=*/NULL,
11641 /*declarator_p=*/true);
11644 /* Parse a type-id.
11647 type-specifier-seq abstract-declarator [opt]
11649 Returns the TYPE specified. */
11652 cp_parser_type_id (cp_parser* parser)
11654 cp_decl_specifier_seq type_specifier_seq;
11655 cp_declarator *abstract_declarator;
11657 /* Parse the type-specifier-seq. */
11658 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11659 &type_specifier_seq);
11660 if (type_specifier_seq.type == error_mark_node)
11661 return error_mark_node;
11663 /* There might or might not be an abstract declarator. */
11664 cp_parser_parse_tentatively (parser);
11665 /* Look for the declarator. */
11666 abstract_declarator
11667 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11668 /*parenthesized_p=*/NULL,
11669 /*member_p=*/false);
11670 /* Check to see if there really was a declarator. */
11671 if (!cp_parser_parse_definitely (parser))
11672 abstract_declarator = NULL;
11674 return groktypename (&type_specifier_seq, abstract_declarator);
11677 /* Parse a type-specifier-seq.
11679 type-specifier-seq:
11680 type-specifier type-specifier-seq [opt]
11684 type-specifier-seq:
11685 attributes type-specifier-seq [opt]
11687 If IS_CONDITION is true, we are at the start of a "condition",
11688 e.g., we've just seen "if (".
11690 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11693 cp_parser_type_specifier_seq (cp_parser* parser,
11695 cp_decl_specifier_seq *type_specifier_seq)
11697 bool seen_type_specifier = false;
11698 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11700 /* Clear the TYPE_SPECIFIER_SEQ. */
11701 clear_decl_specs (type_specifier_seq);
11703 /* Parse the type-specifiers and attributes. */
11706 tree type_specifier;
11707 bool is_cv_qualifier;
11709 /* Check for attributes first. */
11710 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11712 type_specifier_seq->attributes =
11713 chainon (type_specifier_seq->attributes,
11714 cp_parser_attributes_opt (parser));
11718 /* Look for the type-specifier. */
11719 type_specifier = cp_parser_type_specifier (parser,
11721 type_specifier_seq,
11722 /*is_declaration=*/false,
11725 if (!type_specifier)
11727 /* If the first type-specifier could not be found, this is not a
11728 type-specifier-seq at all. */
11729 if (!seen_type_specifier)
11731 cp_parser_error (parser, "expected type-specifier");
11732 type_specifier_seq->type = error_mark_node;
11735 /* If subsequent type-specifiers could not be found, the
11736 type-specifier-seq is complete. */
11740 seen_type_specifier = true;
11741 /* The standard says that a condition can be:
11743 type-specifier-seq declarator = assignment-expression
11750 we should treat the "S" as a declarator, not as a
11751 type-specifier. The standard doesn't say that explicitly for
11752 type-specifier-seq, but it does say that for
11753 decl-specifier-seq in an ordinary declaration. Perhaps it
11754 would be clearer just to allow a decl-specifier-seq here, and
11755 then add a semantic restriction that if any decl-specifiers
11756 that are not type-specifiers appear, the program is invalid. */
11757 if (is_condition && !is_cv_qualifier)
11758 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11764 /* Parse a parameter-declaration-clause.
11766 parameter-declaration-clause:
11767 parameter-declaration-list [opt] ... [opt]
11768 parameter-declaration-list , ...
11770 Returns a representation for the parameter declarations. A return
11771 value of NULL indicates a parameter-declaration-clause consisting
11772 only of an ellipsis. */
11774 static cp_parameter_declarator *
11775 cp_parser_parameter_declaration_clause (cp_parser* parser)
11777 cp_parameter_declarator *parameters;
11782 /* Peek at the next token. */
11783 token = cp_lexer_peek_token (parser->lexer);
11784 /* Check for trivial parameter-declaration-clauses. */
11785 if (token->type == CPP_ELLIPSIS)
11787 /* Consume the `...' token. */
11788 cp_lexer_consume_token (parser->lexer);
11791 else if (token->type == CPP_CLOSE_PAREN)
11792 /* There are no parameters. */
11794 #ifndef NO_IMPLICIT_EXTERN_C
11795 if (in_system_header && current_class_type == NULL
11796 && current_lang_name == lang_name_c)
11800 return no_parameters;
11802 /* Check for `(void)', too, which is a special case. */
11803 else if (token->keyword == RID_VOID
11804 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11805 == CPP_CLOSE_PAREN))
11807 /* Consume the `void' token. */
11808 cp_lexer_consume_token (parser->lexer);
11809 /* There are no parameters. */
11810 return no_parameters;
11813 /* Parse the parameter-declaration-list. */
11814 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11815 /* If a parse error occurred while parsing the
11816 parameter-declaration-list, then the entire
11817 parameter-declaration-clause is erroneous. */
11821 /* Peek at the next token. */
11822 token = cp_lexer_peek_token (parser->lexer);
11823 /* If it's a `,', the clause should terminate with an ellipsis. */
11824 if (token->type == CPP_COMMA)
11826 /* Consume the `,'. */
11827 cp_lexer_consume_token (parser->lexer);
11828 /* Expect an ellipsis. */
11830 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11832 /* It might also be `...' if the optional trailing `,' was
11834 else if (token->type == CPP_ELLIPSIS)
11836 /* Consume the `...' token. */
11837 cp_lexer_consume_token (parser->lexer);
11838 /* And remember that we saw it. */
11842 ellipsis_p = false;
11844 /* Finish the parameter list. */
11845 if (parameters && ellipsis_p)
11846 parameters->ellipsis_p = true;
11851 /* Parse a parameter-declaration-list.
11853 parameter-declaration-list:
11854 parameter-declaration
11855 parameter-declaration-list , parameter-declaration
11857 Returns a representation of the parameter-declaration-list, as for
11858 cp_parser_parameter_declaration_clause. However, the
11859 `void_list_node' is never appended to the list. Upon return,
11860 *IS_ERROR will be true iff an error occurred. */
11862 static cp_parameter_declarator *
11863 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11865 cp_parameter_declarator *parameters = NULL;
11866 cp_parameter_declarator **tail = ¶meters;
11868 /* Assume all will go well. */
11871 /* Look for more parameters. */
11874 cp_parameter_declarator *parameter;
11875 bool parenthesized_p;
11876 /* Parse the parameter. */
11878 = cp_parser_parameter_declaration (parser,
11879 /*template_parm_p=*/false,
11882 /* If a parse error occurred parsing the parameter declaration,
11883 then the entire parameter-declaration-list is erroneous. */
11890 /* Add the new parameter to the list. */
11892 tail = ¶meter->next;
11894 /* Peek at the next token. */
11895 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11896 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11897 /* These are for Objective-C++ */
11898 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11899 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11900 /* The parameter-declaration-list is complete. */
11902 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11906 /* Peek at the next token. */
11907 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11908 /* If it's an ellipsis, then the list is complete. */
11909 if (token->type == CPP_ELLIPSIS)
11911 /* Otherwise, there must be more parameters. Consume the
11913 cp_lexer_consume_token (parser->lexer);
11914 /* When parsing something like:
11916 int i(float f, double d)
11918 we can tell after seeing the declaration for "f" that we
11919 are not looking at an initialization of a variable "i",
11920 but rather at the declaration of a function "i".
11922 Due to the fact that the parsing of template arguments
11923 (as specified to a template-id) requires backtracking we
11924 cannot use this technique when inside a template argument
11926 if (!parser->in_template_argument_list_p
11927 && !parser->in_type_id_in_expr_p
11928 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11929 /* However, a parameter-declaration of the form
11930 "foat(f)" (which is a valid declaration of a
11931 parameter "f") can also be interpreted as an
11932 expression (the conversion of "f" to "float"). */
11933 && !parenthesized_p)
11934 cp_parser_commit_to_tentative_parse (parser);
11938 cp_parser_error (parser, "expected %<,%> or %<...%>");
11939 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11940 cp_parser_skip_to_closing_parenthesis (parser,
11941 /*recovering=*/true,
11942 /*or_comma=*/false,
11943 /*consume_paren=*/false);
11951 /* Parse a parameter declaration.
11953 parameter-declaration:
11954 decl-specifier-seq declarator
11955 decl-specifier-seq declarator = assignment-expression
11956 decl-specifier-seq abstract-declarator [opt]
11957 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11959 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11960 declares a template parameter. (In that case, a non-nested `>'
11961 token encountered during the parsing of the assignment-expression
11962 is not interpreted as a greater-than operator.)
11964 Returns a representation of the parameter, or NULL if an error
11965 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11966 true iff the declarator is of the form "(p)". */
11968 static cp_parameter_declarator *
11969 cp_parser_parameter_declaration (cp_parser *parser,
11970 bool template_parm_p,
11971 bool *parenthesized_p)
11973 int declares_class_or_enum;
11974 bool greater_than_is_operator_p;
11975 cp_decl_specifier_seq decl_specifiers;
11976 cp_declarator *declarator;
11977 tree default_argument;
11979 const char *saved_message;
11981 /* In a template parameter, `>' is not an operator.
11985 When parsing a default template-argument for a non-type
11986 template-parameter, the first non-nested `>' is taken as the end
11987 of the template parameter-list rather than a greater-than
11989 greater_than_is_operator_p = !template_parm_p;
11991 /* Type definitions may not appear in parameter types. */
11992 saved_message = parser->type_definition_forbidden_message;
11993 parser->type_definition_forbidden_message
11994 = "types may not be defined in parameter types";
11996 /* Parse the declaration-specifiers. */
11997 cp_parser_decl_specifier_seq (parser,
11998 CP_PARSER_FLAGS_NONE,
12000 &declares_class_or_enum);
12001 /* If an error occurred, there's no reason to attempt to parse the
12002 rest of the declaration. */
12003 if (cp_parser_error_occurred (parser))
12005 parser->type_definition_forbidden_message = saved_message;
12009 /* Peek at the next token. */
12010 token = cp_lexer_peek_token (parser->lexer);
12011 /* If the next token is a `)', `,', `=', `>', or `...', then there
12012 is no declarator. */
12013 if (token->type == CPP_CLOSE_PAREN
12014 || token->type == CPP_COMMA
12015 || token->type == CPP_EQ
12016 || token->type == CPP_ELLIPSIS
12017 || token->type == CPP_GREATER)
12020 if (parenthesized_p)
12021 *parenthesized_p = false;
12023 /* Otherwise, there should be a declarator. */
12026 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12027 parser->default_arg_ok_p = false;
12029 /* After seeing a decl-specifier-seq, if the next token is not a
12030 "(", there is no possibility that the code is a valid
12031 expression. Therefore, if parsing tentatively, we commit at
12033 if (!parser->in_template_argument_list_p
12034 /* In an expression context, having seen:
12038 we cannot be sure whether we are looking at a
12039 function-type (taking a "char" as a parameter) or a cast
12040 of some object of type "char" to "int". */
12041 && !parser->in_type_id_in_expr_p
12042 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12043 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12044 cp_parser_commit_to_tentative_parse (parser);
12045 /* Parse the declarator. */
12046 declarator = cp_parser_declarator (parser,
12047 CP_PARSER_DECLARATOR_EITHER,
12048 /*ctor_dtor_or_conv_p=*/NULL,
12050 /*member_p=*/false);
12051 parser->default_arg_ok_p = saved_default_arg_ok_p;
12052 /* After the declarator, allow more attributes. */
12053 decl_specifiers.attributes
12054 = chainon (decl_specifiers.attributes,
12055 cp_parser_attributes_opt (parser));
12058 /* The restriction on defining new types applies only to the type
12059 of the parameter, not to the default argument. */
12060 parser->type_definition_forbidden_message = saved_message;
12062 /* If the next token is `=', then process a default argument. */
12063 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12065 bool saved_greater_than_is_operator_p;
12066 /* Consume the `='. */
12067 cp_lexer_consume_token (parser->lexer);
12069 /* If we are defining a class, then the tokens that make up the
12070 default argument must be saved and processed later. */
12071 if (!template_parm_p && at_class_scope_p ()
12072 && TYPE_BEING_DEFINED (current_class_type))
12074 unsigned depth = 0;
12075 cp_token *first_token;
12078 /* Add tokens until we have processed the entire default
12079 argument. We add the range [first_token, token). */
12080 first_token = cp_lexer_peek_token (parser->lexer);
12085 /* Peek at the next token. */
12086 token = cp_lexer_peek_token (parser->lexer);
12087 /* What we do depends on what token we have. */
12088 switch (token->type)
12090 /* In valid code, a default argument must be
12091 immediately followed by a `,' `)', or `...'. */
12093 case CPP_CLOSE_PAREN:
12095 /* If we run into a non-nested `;', `}', or `]',
12096 then the code is invalid -- but the default
12097 argument is certainly over. */
12098 case CPP_SEMICOLON:
12099 case CPP_CLOSE_BRACE:
12100 case CPP_CLOSE_SQUARE:
12103 /* Update DEPTH, if necessary. */
12104 else if (token->type == CPP_CLOSE_PAREN
12105 || token->type == CPP_CLOSE_BRACE
12106 || token->type == CPP_CLOSE_SQUARE)
12110 case CPP_OPEN_PAREN:
12111 case CPP_OPEN_SQUARE:
12112 case CPP_OPEN_BRACE:
12117 /* If we see a non-nested `>', and `>' is not an
12118 operator, then it marks the end of the default
12120 if (!depth && !greater_than_is_operator_p)
12124 /* If we run out of tokens, issue an error message. */
12126 error ("file ends in default argument");
12132 /* In these cases, we should look for template-ids.
12133 For example, if the default argument is
12134 `X<int, double>()', we need to do name lookup to
12135 figure out whether or not `X' is a template; if
12136 so, the `,' does not end the default argument.
12138 That is not yet done. */
12145 /* If we've reached the end, stop. */
12149 /* Add the token to the token block. */
12150 token = cp_lexer_consume_token (parser->lexer);
12153 /* Create a DEFAULT_ARG to represented the unparsed default
12155 default_argument = make_node (DEFAULT_ARG);
12156 DEFARG_TOKENS (default_argument)
12157 = cp_token_cache_new (first_token, token);
12158 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12160 /* Outside of a class definition, we can just parse the
12161 assignment-expression. */
12164 bool saved_local_variables_forbidden_p;
12166 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12168 saved_greater_than_is_operator_p
12169 = parser->greater_than_is_operator_p;
12170 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12171 /* Local variable names (and the `this' keyword) may not
12172 appear in a default argument. */
12173 saved_local_variables_forbidden_p
12174 = parser->local_variables_forbidden_p;
12175 parser->local_variables_forbidden_p = true;
12176 /* Parse the assignment-expression. */
12178 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12179 /* Restore saved state. */
12180 parser->greater_than_is_operator_p
12181 = saved_greater_than_is_operator_p;
12182 parser->local_variables_forbidden_p
12183 = saved_local_variables_forbidden_p;
12185 if (!parser->default_arg_ok_p)
12187 if (!flag_pedantic_errors)
12188 warning (0, "deprecated use of default argument for parameter of non-function");
12191 error ("default arguments are only permitted for function parameters");
12192 default_argument = NULL_TREE;
12197 default_argument = NULL_TREE;
12199 return make_parameter_declarator (&decl_specifiers,
12204 /* Parse a function-body.
12207 compound_statement */
12210 cp_parser_function_body (cp_parser *parser)
12212 cp_parser_compound_statement (parser, NULL, false);
12215 /* Parse a ctor-initializer-opt followed by a function-body. Return
12216 true if a ctor-initializer was present. */
12219 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12222 bool ctor_initializer_p;
12224 /* Begin the function body. */
12225 body = begin_function_body ();
12226 /* Parse the optional ctor-initializer. */
12227 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12228 /* Parse the function-body. */
12229 cp_parser_function_body (parser);
12230 /* Finish the function body. */
12231 finish_function_body (body);
12233 return ctor_initializer_p;
12236 /* Parse an initializer.
12239 = initializer-clause
12240 ( expression-list )
12242 Returns a expression representing the initializer. If no
12243 initializer is present, NULL_TREE is returned.
12245 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12246 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12247 set to FALSE if there is no initializer present. If there is an
12248 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12249 is set to true; otherwise it is set to false. */
12252 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12253 bool* non_constant_p)
12258 /* Peek at the next token. */
12259 token = cp_lexer_peek_token (parser->lexer);
12261 /* Let our caller know whether or not this initializer was
12263 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12264 /* Assume that the initializer is constant. */
12265 *non_constant_p = false;
12267 if (token->type == CPP_EQ)
12269 /* Consume the `='. */
12270 cp_lexer_consume_token (parser->lexer);
12271 /* Parse the initializer-clause. */
12272 init = cp_parser_initializer_clause (parser, non_constant_p);
12274 else if (token->type == CPP_OPEN_PAREN)
12275 init = cp_parser_parenthesized_expression_list (parser, false,
12280 /* Anything else is an error. */
12281 cp_parser_error (parser, "expected initializer");
12282 init = error_mark_node;
12288 /* Parse an initializer-clause.
12290 initializer-clause:
12291 assignment-expression
12292 { initializer-list , [opt] }
12295 Returns an expression representing the initializer.
12297 If the `assignment-expression' production is used the value
12298 returned is simply a representation for the expression.
12300 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12301 the elements of the initializer-list (or NULL_TREE, if the last
12302 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12303 NULL_TREE. There is no way to detect whether or not the optional
12304 trailing `,' was provided. NON_CONSTANT_P is as for
12305 cp_parser_initializer. */
12308 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12312 /* Assume the expression is constant. */
12313 *non_constant_p = false;
12315 /* If it is not a `{', then we are looking at an
12316 assignment-expression. */
12317 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12320 = cp_parser_constant_expression (parser,
12321 /*allow_non_constant_p=*/true,
12323 if (!*non_constant_p)
12324 initializer = fold_non_dependent_expr (initializer);
12328 /* Consume the `{' token. */
12329 cp_lexer_consume_token (parser->lexer);
12330 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12331 initializer = make_node (CONSTRUCTOR);
12332 /* If it's not a `}', then there is a non-trivial initializer. */
12333 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12335 /* Parse the initializer list. */
12336 CONSTRUCTOR_ELTS (initializer)
12337 = cp_parser_initializer_list (parser, non_constant_p);
12338 /* A trailing `,' token is allowed. */
12339 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12340 cp_lexer_consume_token (parser->lexer);
12342 /* Now, there should be a trailing `}'. */
12343 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12346 return initializer;
12349 /* Parse an initializer-list.
12353 initializer-list , initializer-clause
12358 identifier : initializer-clause
12359 initializer-list, identifier : initializer-clause
12361 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12362 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12363 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12364 as for cp_parser_initializer. */
12367 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12369 tree initializers = NULL_TREE;
12371 /* Assume all of the expressions are constant. */
12372 *non_constant_p = false;
12374 /* Parse the rest of the list. */
12380 bool clause_non_constant_p;
12382 /* If the next token is an identifier and the following one is a
12383 colon, we are looking at the GNU designated-initializer
12385 if (cp_parser_allow_gnu_extensions_p (parser)
12386 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12387 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12389 /* Consume the identifier. */
12390 identifier = cp_lexer_consume_token (parser->lexer)->value;
12391 /* Consume the `:'. */
12392 cp_lexer_consume_token (parser->lexer);
12395 identifier = NULL_TREE;
12397 /* Parse the initializer. */
12398 initializer = cp_parser_initializer_clause (parser,
12399 &clause_non_constant_p);
12400 /* If any clause is non-constant, so is the entire initializer. */
12401 if (clause_non_constant_p)
12402 *non_constant_p = true;
12403 /* Add it to the list. */
12404 initializers = tree_cons (identifier, initializer, initializers);
12406 /* If the next token is not a comma, we have reached the end of
12408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12411 /* Peek at the next token. */
12412 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12413 /* If the next token is a `}', then we're still done. An
12414 initializer-clause can have a trailing `,' after the
12415 initializer-list and before the closing `}'. */
12416 if (token->type == CPP_CLOSE_BRACE)
12419 /* Consume the `,' token. */
12420 cp_lexer_consume_token (parser->lexer);
12423 /* The initializers were built up in reverse order, so we need to
12424 reverse them now. */
12425 return nreverse (initializers);
12428 /* Classes [gram.class] */
12430 /* Parse a class-name.
12436 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12437 to indicate that names looked up in dependent types should be
12438 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12439 keyword has been used to indicate that the name that appears next
12440 is a template. TAG_TYPE indicates the explicit tag given before
12441 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12442 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12443 is the class being defined in a class-head.
12445 Returns the TYPE_DECL representing the class. */
12448 cp_parser_class_name (cp_parser *parser,
12449 bool typename_keyword_p,
12450 bool template_keyword_p,
12451 enum tag_types tag_type,
12452 bool check_dependency_p,
12454 bool is_declaration)
12461 /* All class-names start with an identifier. */
12462 token = cp_lexer_peek_token (parser->lexer);
12463 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12465 cp_parser_error (parser, "expected class-name");
12466 return error_mark_node;
12469 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12470 to a template-id, so we save it here. */
12471 scope = parser->scope;
12472 if (scope == error_mark_node)
12473 return error_mark_node;
12475 /* Any name names a type if we're following the `typename' keyword
12476 in a qualified name where the enclosing scope is type-dependent. */
12477 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12478 && dependent_type_p (scope));
12479 /* Handle the common case (an identifier, but not a template-id)
12481 if (token->type == CPP_NAME
12482 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12486 /* Look for the identifier. */
12487 identifier = cp_parser_identifier (parser);
12488 /* If the next token isn't an identifier, we are certainly not
12489 looking at a class-name. */
12490 if (identifier == error_mark_node)
12491 decl = error_mark_node;
12492 /* If we know this is a type-name, there's no need to look it
12494 else if (typename_p)
12498 /* If the next token is a `::', then the name must be a type
12501 [basic.lookup.qual]
12503 During the lookup for a name preceding the :: scope
12504 resolution operator, object, function, and enumerator
12505 names are ignored. */
12506 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12507 tag_type = typename_type;
12508 /* Look up the name. */
12509 decl = cp_parser_lookup_name (parser, identifier,
12511 /*is_template=*/false,
12512 /*is_namespace=*/false,
12513 check_dependency_p,
12514 /*ambiguous_p=*/NULL);
12519 /* Try a template-id. */
12520 decl = cp_parser_template_id (parser, template_keyword_p,
12521 check_dependency_p,
12523 if (decl == error_mark_node)
12524 return error_mark_node;
12527 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12529 /* If this is a typename, create a TYPENAME_TYPE. */
12530 if (typename_p && decl != error_mark_node)
12532 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12533 if (decl != error_mark_node)
12534 decl = TYPE_NAME (decl);
12537 /* Check to see that it is really the name of a class. */
12538 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12539 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12540 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12541 /* Situations like this:
12543 template <typename T> struct A {
12544 typename T::template X<int>::I i;
12547 are problematic. Is `T::template X<int>' a class-name? The
12548 standard does not seem to be definitive, but there is no other
12549 valid interpretation of the following `::'. Therefore, those
12550 names are considered class-names. */
12551 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12552 else if (decl == error_mark_node
12553 || TREE_CODE (decl) != TYPE_DECL
12554 || TREE_TYPE (decl) == error_mark_node
12555 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12557 cp_parser_error (parser, "expected class-name");
12558 return error_mark_node;
12564 /* Parse a class-specifier.
12567 class-head { member-specification [opt] }
12569 Returns the TREE_TYPE representing the class. */
12572 cp_parser_class_specifier (cp_parser* parser)
12576 tree attributes = NULL_TREE;
12577 int has_trailing_semicolon;
12578 bool nested_name_specifier_p;
12579 unsigned saved_num_template_parameter_lists;
12580 tree old_scope = NULL_TREE;
12581 tree scope = NULL_TREE;
12583 push_deferring_access_checks (dk_no_deferred);
12585 /* Parse the class-head. */
12586 type = cp_parser_class_head (parser,
12587 &nested_name_specifier_p,
12589 /* If the class-head was a semantic disaster, skip the entire body
12593 cp_parser_skip_to_end_of_block_or_statement (parser);
12594 pop_deferring_access_checks ();
12595 return error_mark_node;
12598 /* Look for the `{'. */
12599 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12601 pop_deferring_access_checks ();
12602 return error_mark_node;
12605 /* Issue an error message if type-definitions are forbidden here. */
12606 cp_parser_check_type_definition (parser);
12607 /* Remember that we are defining one more class. */
12608 ++parser->num_classes_being_defined;
12609 /* Inside the class, surrounding template-parameter-lists do not
12611 saved_num_template_parameter_lists
12612 = parser->num_template_parameter_lists;
12613 parser->num_template_parameter_lists = 0;
12615 /* Start the class. */
12616 if (nested_name_specifier_p)
12618 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12619 old_scope = push_inner_scope (scope);
12621 type = begin_class_definition (type);
12623 if (type == error_mark_node)
12624 /* If the type is erroneous, skip the entire body of the class. */
12625 cp_parser_skip_to_closing_brace (parser);
12627 /* Parse the member-specification. */
12628 cp_parser_member_specification_opt (parser);
12630 /* Look for the trailing `}'. */
12631 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12632 /* We get better error messages by noticing a common problem: a
12633 missing trailing `;'. */
12634 token = cp_lexer_peek_token (parser->lexer);
12635 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12636 /* Look for trailing attributes to apply to this class. */
12637 if (cp_parser_allow_gnu_extensions_p (parser))
12639 tree sub_attr = cp_parser_attributes_opt (parser);
12640 attributes = chainon (attributes, sub_attr);
12642 if (type != error_mark_node)
12643 type = finish_struct (type, attributes);
12644 if (nested_name_specifier_p)
12645 pop_inner_scope (old_scope, scope);
12646 /* If this class is not itself within the scope of another class,
12647 then we need to parse the bodies of all of the queued function
12648 definitions. Note that the queued functions defined in a class
12649 are not always processed immediately following the
12650 class-specifier for that class. Consider:
12653 struct B { void f() { sizeof (A); } };
12656 If `f' were processed before the processing of `A' were
12657 completed, there would be no way to compute the size of `A'.
12658 Note that the nesting we are interested in here is lexical --
12659 not the semantic nesting given by TYPE_CONTEXT. In particular,
12662 struct A { struct B; };
12663 struct A::B { void f() { } };
12665 there is no need to delay the parsing of `A::B::f'. */
12666 if (--parser->num_classes_being_defined == 0)
12670 tree class_type = NULL_TREE;
12671 tree pushed_scope = NULL_TREE;
12673 /* In a first pass, parse default arguments to the functions.
12674 Then, in a second pass, parse the bodies of the functions.
12675 This two-phased approach handles cases like:
12683 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12684 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12685 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12686 TREE_PURPOSE (parser->unparsed_functions_queues)
12687 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12689 fn = TREE_VALUE (queue_entry);
12690 /* If there are default arguments that have not yet been processed,
12691 take care of them now. */
12692 if (class_type != TREE_PURPOSE (queue_entry))
12695 pop_scope (pushed_scope);
12696 class_type = TREE_PURPOSE (queue_entry);
12697 pushed_scope = push_scope (class_type);
12699 /* Make sure that any template parameters are in scope. */
12700 maybe_begin_member_template_processing (fn);
12701 /* Parse the default argument expressions. */
12702 cp_parser_late_parsing_default_args (parser, fn);
12703 /* Remove any template parameters from the symbol table. */
12704 maybe_end_member_template_processing ();
12707 pop_scope (pushed_scope);
12708 /* Now parse the body of the functions. */
12709 for (TREE_VALUE (parser->unparsed_functions_queues)
12710 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12711 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12712 TREE_VALUE (parser->unparsed_functions_queues)
12713 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12715 /* Figure out which function we need to process. */
12716 fn = TREE_VALUE (queue_entry);
12718 /* A hack to prevent garbage collection. */
12721 /* Parse the function. */
12722 cp_parser_late_parsing_for_member (parser, fn);
12727 /* Put back any saved access checks. */
12728 pop_deferring_access_checks ();
12730 /* Restore the count of active template-parameter-lists. */
12731 parser->num_template_parameter_lists
12732 = saved_num_template_parameter_lists;
12737 /* Parse a class-head.
12740 class-key identifier [opt] base-clause [opt]
12741 class-key nested-name-specifier identifier base-clause [opt]
12742 class-key nested-name-specifier [opt] template-id
12746 class-key attributes identifier [opt] base-clause [opt]
12747 class-key attributes nested-name-specifier identifier base-clause [opt]
12748 class-key attributes nested-name-specifier [opt] template-id
12751 Returns the TYPE of the indicated class. Sets
12752 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12753 involving a nested-name-specifier was used, and FALSE otherwise.
12755 Returns error_mark_node if this is not a class-head.
12757 Returns NULL_TREE if the class-head is syntactically valid, but
12758 semantically invalid in a way that means we should skip the entire
12759 body of the class. */
12762 cp_parser_class_head (cp_parser* parser,
12763 bool* nested_name_specifier_p,
12764 tree *attributes_p)
12766 tree nested_name_specifier;
12767 enum tag_types class_key;
12768 tree id = NULL_TREE;
12769 tree type = NULL_TREE;
12771 bool template_id_p = false;
12772 bool qualified_p = false;
12773 bool invalid_nested_name_p = false;
12774 bool invalid_explicit_specialization_p = false;
12775 tree pushed_scope = NULL_TREE;
12776 unsigned num_templates;
12779 /* Assume no nested-name-specifier will be present. */
12780 *nested_name_specifier_p = false;
12781 /* Assume no template parameter lists will be used in defining the
12785 /* Look for the class-key. */
12786 class_key = cp_parser_class_key (parser);
12787 if (class_key == none_type)
12788 return error_mark_node;
12790 /* Parse the attributes. */
12791 attributes = cp_parser_attributes_opt (parser);
12793 /* If the next token is `::', that is invalid -- but sometimes
12794 people do try to write:
12798 Handle this gracefully by accepting the extra qualifier, and then
12799 issuing an error about it later if this really is a
12800 class-head. If it turns out just to be an elaborated type
12801 specifier, remain silent. */
12802 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12803 qualified_p = true;
12805 push_deferring_access_checks (dk_no_check);
12807 /* Determine the name of the class. Begin by looking for an
12808 optional nested-name-specifier. */
12809 nested_name_specifier
12810 = cp_parser_nested_name_specifier_opt (parser,
12811 /*typename_keyword_p=*/false,
12812 /*check_dependency_p=*/false,
12814 /*is_declaration=*/false);
12815 /* If there was a nested-name-specifier, then there *must* be an
12817 if (nested_name_specifier)
12819 /* Although the grammar says `identifier', it really means
12820 `class-name' or `template-name'. You are only allowed to
12821 define a class that has already been declared with this
12824 The proposed resolution for Core Issue 180 says that whever
12825 you see `class T::X' you should treat `X' as a type-name.
12827 It is OK to define an inaccessible class; for example:
12829 class A { class B; };
12832 We do not know if we will see a class-name, or a
12833 template-name. We look for a class-name first, in case the
12834 class-name is a template-id; if we looked for the
12835 template-name first we would stop after the template-name. */
12836 cp_parser_parse_tentatively (parser);
12837 type = cp_parser_class_name (parser,
12838 /*typename_keyword_p=*/false,
12839 /*template_keyword_p=*/false,
12841 /*check_dependency_p=*/false,
12842 /*class_head_p=*/true,
12843 /*is_declaration=*/false);
12844 /* If that didn't work, ignore the nested-name-specifier. */
12845 if (!cp_parser_parse_definitely (parser))
12847 invalid_nested_name_p = true;
12848 id = cp_parser_identifier (parser);
12849 if (id == error_mark_node)
12852 /* If we could not find a corresponding TYPE, treat this
12853 declaration like an unqualified declaration. */
12854 if (type == error_mark_node)
12855 nested_name_specifier = NULL_TREE;
12856 /* Otherwise, count the number of templates used in TYPE and its
12857 containing scopes. */
12862 for (scope = TREE_TYPE (type);
12863 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12864 scope = (TYPE_P (scope)
12865 ? TYPE_CONTEXT (scope)
12866 : DECL_CONTEXT (scope)))
12868 && CLASS_TYPE_P (scope)
12869 && CLASSTYPE_TEMPLATE_INFO (scope)
12870 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12871 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12875 /* Otherwise, the identifier is optional. */
12878 /* We don't know whether what comes next is a template-id,
12879 an identifier, or nothing at all. */
12880 cp_parser_parse_tentatively (parser);
12881 /* Check for a template-id. */
12882 id = cp_parser_template_id (parser,
12883 /*template_keyword_p=*/false,
12884 /*check_dependency_p=*/true,
12885 /*is_declaration=*/true);
12886 /* If that didn't work, it could still be an identifier. */
12887 if (!cp_parser_parse_definitely (parser))
12889 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12890 id = cp_parser_identifier (parser);
12896 template_id_p = true;
12901 pop_deferring_access_checks ();
12904 cp_parser_check_for_invalid_template_id (parser, id);
12906 /* If it's not a `:' or a `{' then we can't really be looking at a
12907 class-head, since a class-head only appears as part of a
12908 class-specifier. We have to detect this situation before calling
12909 xref_tag, since that has irreversible side-effects. */
12910 if (!cp_parser_next_token_starts_class_definition_p (parser))
12912 cp_parser_error (parser, "expected %<{%> or %<:%>");
12913 return error_mark_node;
12916 /* At this point, we're going ahead with the class-specifier, even
12917 if some other problem occurs. */
12918 cp_parser_commit_to_tentative_parse (parser);
12919 /* Issue the error about the overly-qualified name now. */
12921 cp_parser_error (parser,
12922 "global qualification of class name is invalid");
12923 else if (invalid_nested_name_p)
12924 cp_parser_error (parser,
12925 "qualified name does not name a class");
12926 else if (nested_name_specifier)
12930 /* Reject typedef-names in class heads. */
12931 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12933 error ("invalid class name in declaration of %qD", type);
12938 /* Figure out in what scope the declaration is being placed. */
12939 scope = current_scope ();
12940 /* If that scope does not contain the scope in which the
12941 class was originally declared, the program is invalid. */
12942 if (scope && !is_ancestor (scope, nested_name_specifier))
12944 error ("declaration of %qD in %qD which does not enclose %qD",
12945 type, scope, nested_name_specifier);
12951 A declarator-id shall not be qualified exception of the
12952 definition of a ... nested class outside of its class
12953 ... [or] a the definition or explicit instantiation of a
12954 class member of a namespace outside of its namespace. */
12955 if (scope == nested_name_specifier)
12957 pedwarn ("extra qualification ignored");
12958 nested_name_specifier = NULL_TREE;
12962 /* An explicit-specialization must be preceded by "template <>". If
12963 it is not, try to recover gracefully. */
12964 if (at_namespace_scope_p ()
12965 && parser->num_template_parameter_lists == 0
12968 error ("an explicit specialization must be preceded by %<template <>%>");
12969 invalid_explicit_specialization_p = true;
12970 /* Take the same action that would have been taken by
12971 cp_parser_explicit_specialization. */
12972 ++parser->num_template_parameter_lists;
12973 begin_specialization ();
12975 /* There must be no "return" statements between this point and the
12976 end of this function; set "type "to the correct return value and
12977 use "goto done;" to return. */
12978 /* Make sure that the right number of template parameters were
12980 if (!cp_parser_check_template_parameters (parser, num_templates))
12982 /* If something went wrong, there is no point in even trying to
12983 process the class-definition. */
12988 /* Look up the type. */
12991 type = TREE_TYPE (id);
12992 maybe_process_partial_specialization (type);
12993 if (nested_name_specifier)
12994 pushed_scope = push_scope (nested_name_specifier);
12996 else if (nested_name_specifier)
13002 template <typename T> struct S { struct T };
13003 template <typename T> struct S<T>::T { };
13005 we will get a TYPENAME_TYPE when processing the definition of
13006 `S::T'. We need to resolve it to the actual type before we
13007 try to define it. */
13008 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13010 class_type = resolve_typename_type (TREE_TYPE (type),
13011 /*only_current_p=*/false);
13012 if (class_type != error_mark_node)
13013 type = TYPE_NAME (class_type);
13016 cp_parser_error (parser, "could not resolve typename type");
13017 type = error_mark_node;
13021 maybe_process_partial_specialization (TREE_TYPE (type));
13022 class_type = current_class_type;
13023 /* Enter the scope indicated by the nested-name-specifier. */
13024 pushed_scope = push_scope (nested_name_specifier);
13025 /* Get the canonical version of this type. */
13026 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13027 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13028 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13030 type = push_template_decl (type);
13031 if (type == error_mark_node)
13038 type = TREE_TYPE (type);
13039 *nested_name_specifier_p = true;
13041 else /* The name is not a nested name. */
13043 /* If the class was unnamed, create a dummy name. */
13045 id = make_anon_name ();
13046 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13047 parser->num_template_parameter_lists);
13050 /* Indicate whether this class was declared as a `class' or as a
13052 if (TREE_CODE (type) == RECORD_TYPE)
13053 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13054 cp_parser_check_class_key (class_key, type);
13056 /* If this type was already complete, and we see another definition,
13057 that's an error. */
13058 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13060 error ("redefinition of %q#T", type);
13061 error ("previous definition of %q+#T", type);
13066 /* We will have entered the scope containing the class; the names of
13067 base classes should be looked up in that context. For example:
13069 struct A { struct B {}; struct C; };
13070 struct A::C : B {};
13075 /* Get the list of base-classes, if there is one. */
13076 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13077 bases = cp_parser_base_clause (parser);
13079 /* Process the base classes. */
13080 xref_basetypes (type, bases);
13083 /* Leave the scope given by the nested-name-specifier. We will
13084 enter the class scope itself while processing the members. */
13086 pop_scope (pushed_scope);
13088 if (invalid_explicit_specialization_p)
13090 end_specialization ();
13091 --parser->num_template_parameter_lists;
13093 *attributes_p = attributes;
13097 /* Parse a class-key.
13104 Returns the kind of class-key specified, or none_type to indicate
13107 static enum tag_types
13108 cp_parser_class_key (cp_parser* parser)
13111 enum tag_types tag_type;
13113 /* Look for the class-key. */
13114 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13118 /* Check to see if the TOKEN is a class-key. */
13119 tag_type = cp_parser_token_is_class_key (token);
13121 cp_parser_error (parser, "expected class-key");
13125 /* Parse an (optional) member-specification.
13127 member-specification:
13128 member-declaration member-specification [opt]
13129 access-specifier : member-specification [opt] */
13132 cp_parser_member_specification_opt (cp_parser* parser)
13139 /* Peek at the next token. */
13140 token = cp_lexer_peek_token (parser->lexer);
13141 /* If it's a `}', or EOF then we've seen all the members. */
13142 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13145 /* See if this token is a keyword. */
13146 keyword = token->keyword;
13150 case RID_PROTECTED:
13152 /* Consume the access-specifier. */
13153 cp_lexer_consume_token (parser->lexer);
13154 /* Remember which access-specifier is active. */
13155 current_access_specifier = token->value;
13156 /* Look for the `:'. */
13157 cp_parser_require (parser, CPP_COLON, "`:'");
13161 /* Accept #pragmas at class scope. */
13162 if (token->type == CPP_PRAGMA)
13164 cp_lexer_handle_pragma (parser->lexer);
13168 /* Otherwise, the next construction must be a
13169 member-declaration. */
13170 cp_parser_member_declaration (parser);
13175 /* Parse a member-declaration.
13177 member-declaration:
13178 decl-specifier-seq [opt] member-declarator-list [opt] ;
13179 function-definition ; [opt]
13180 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13182 template-declaration
13184 member-declarator-list:
13186 member-declarator-list , member-declarator
13189 declarator pure-specifier [opt]
13190 declarator constant-initializer [opt]
13191 identifier [opt] : constant-expression
13195 member-declaration:
13196 __extension__ member-declaration
13199 declarator attributes [opt] pure-specifier [opt]
13200 declarator attributes [opt] constant-initializer [opt]
13201 identifier [opt] attributes [opt] : constant-expression */
13204 cp_parser_member_declaration (cp_parser* parser)
13206 cp_decl_specifier_seq decl_specifiers;
13207 tree prefix_attributes;
13209 int declares_class_or_enum;
13212 int saved_pedantic;
13214 /* Check for the `__extension__' keyword. */
13215 if (cp_parser_extension_opt (parser, &saved_pedantic))
13218 cp_parser_member_declaration (parser);
13219 /* Restore the old value of the PEDANTIC flag. */
13220 pedantic = saved_pedantic;
13225 /* Check for a template-declaration. */
13226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13228 /* Parse the template-declaration. */
13229 cp_parser_template_declaration (parser, /*member_p=*/true);
13234 /* Check for a using-declaration. */
13235 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13237 /* Parse the using-declaration. */
13238 cp_parser_using_declaration (parser);
13243 /* Check for @defs. */
13244 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13247 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13248 ivar = ivar_chains;
13252 ivar = TREE_CHAIN (member);
13253 TREE_CHAIN (member) = NULL_TREE;
13254 finish_member_declaration (member);
13259 /* Parse the decl-specifier-seq. */
13260 cp_parser_decl_specifier_seq (parser,
13261 CP_PARSER_FLAGS_OPTIONAL,
13263 &declares_class_or_enum);
13264 prefix_attributes = decl_specifiers.attributes;
13265 decl_specifiers.attributes = NULL_TREE;
13266 /* Check for an invalid type-name. */
13267 if (!decl_specifiers.type
13268 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13270 /* If there is no declarator, then the decl-specifier-seq should
13272 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13274 /* If there was no decl-specifier-seq, and the next token is a
13275 `;', then we have something like:
13281 Each member-declaration shall declare at least one member
13282 name of the class. */
13283 if (!decl_specifiers.any_specifiers_p)
13285 cp_token *token = cp_lexer_peek_token (parser->lexer);
13286 if (pedantic && !token->in_system_header)
13287 pedwarn ("%Hextra %<;%>", &token->location);
13293 /* See if this declaration is a friend. */
13294 friend_p = cp_parser_friend_p (&decl_specifiers);
13295 /* If there were decl-specifiers, check to see if there was
13296 a class-declaration. */
13297 type = check_tag_decl (&decl_specifiers);
13298 /* Nested classes have already been added to the class, but
13299 a `friend' needs to be explicitly registered. */
13302 /* If the `friend' keyword was present, the friend must
13303 be introduced with a class-key. */
13304 if (!declares_class_or_enum)
13305 error ("a class-key must be used when declaring a friend");
13308 template <typename T> struct A {
13309 friend struct A<T>::B;
13312 A<T>::B will be represented by a TYPENAME_TYPE, and
13313 therefore not recognized by check_tag_decl. */
13315 && decl_specifiers.type
13316 && TYPE_P (decl_specifiers.type))
13317 type = decl_specifiers.type;
13318 if (!type || !TYPE_P (type))
13319 error ("friend declaration does not name a class or "
13322 make_friend_class (current_class_type, type,
13323 /*complain=*/true);
13325 /* If there is no TYPE, an error message will already have
13327 else if (!type || type == error_mark_node)
13329 /* An anonymous aggregate has to be handled specially; such
13330 a declaration really declares a data member (with a
13331 particular type), as opposed to a nested class. */
13332 else if (ANON_AGGR_TYPE_P (type))
13334 /* Remove constructors and such from TYPE, now that we
13335 know it is an anonymous aggregate. */
13336 fixup_anonymous_aggr (type);
13337 /* And make the corresponding data member. */
13338 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13339 /* Add it to the class. */
13340 finish_member_declaration (decl);
13343 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13348 /* See if these declarations will be friends. */
13349 friend_p = cp_parser_friend_p (&decl_specifiers);
13351 /* Keep going until we hit the `;' at the end of the
13353 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13355 tree attributes = NULL_TREE;
13356 tree first_attribute;
13358 /* Peek at the next token. */
13359 token = cp_lexer_peek_token (parser->lexer);
13361 /* Check for a bitfield declaration. */
13362 if (token->type == CPP_COLON
13363 || (token->type == CPP_NAME
13364 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13370 /* Get the name of the bitfield. Note that we cannot just
13371 check TOKEN here because it may have been invalidated by
13372 the call to cp_lexer_peek_nth_token above. */
13373 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13374 identifier = cp_parser_identifier (parser);
13376 identifier = NULL_TREE;
13378 /* Consume the `:' token. */
13379 cp_lexer_consume_token (parser->lexer);
13380 /* Get the width of the bitfield. */
13382 = cp_parser_constant_expression (parser,
13383 /*allow_non_constant=*/false,
13386 /* Look for attributes that apply to the bitfield. */
13387 attributes = cp_parser_attributes_opt (parser);
13388 /* Remember which attributes are prefix attributes and
13390 first_attribute = attributes;
13391 /* Combine the attributes. */
13392 attributes = chainon (prefix_attributes, attributes);
13394 /* Create the bitfield declaration. */
13395 decl = grokbitfield (identifier
13396 ? make_id_declarator (NULL_TREE,
13401 /* Apply the attributes. */
13402 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13406 cp_declarator *declarator;
13408 tree asm_specification;
13409 int ctor_dtor_or_conv_p;
13411 /* Parse the declarator. */
13413 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13414 &ctor_dtor_or_conv_p,
13415 /*parenthesized_p=*/NULL,
13416 /*member_p=*/true);
13418 /* If something went wrong parsing the declarator, make sure
13419 that we at least consume some tokens. */
13420 if (declarator == cp_error_declarator)
13422 /* Skip to the end of the statement. */
13423 cp_parser_skip_to_end_of_statement (parser);
13424 /* If the next token is not a semicolon, that is
13425 probably because we just skipped over the body of
13426 a function. So, we consume a semicolon if
13427 present, but do not issue an error message if it
13429 if (cp_lexer_next_token_is (parser->lexer,
13431 cp_lexer_consume_token (parser->lexer);
13435 if (declares_class_or_enum & 2)
13436 cp_parser_check_for_definition_in_return_type
13437 (declarator, decl_specifiers.type);
13439 /* Look for an asm-specification. */
13440 asm_specification = cp_parser_asm_specification_opt (parser);
13441 /* Look for attributes that apply to the declaration. */
13442 attributes = cp_parser_attributes_opt (parser);
13443 /* Remember which attributes are prefix attributes and
13445 first_attribute = attributes;
13446 /* Combine the attributes. */
13447 attributes = chainon (prefix_attributes, attributes);
13449 /* If it's an `=', then we have a constant-initializer or a
13450 pure-specifier. It is not correct to parse the
13451 initializer before registering the member declaration
13452 since the member declaration should be in scope while
13453 its initializer is processed. However, the rest of the
13454 front end does not yet provide an interface that allows
13455 us to handle this correctly. */
13456 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13460 A pure-specifier shall be used only in the declaration of
13461 a virtual function.
13463 A member-declarator can contain a constant-initializer
13464 only if it declares a static member of integral or
13467 Therefore, if the DECLARATOR is for a function, we look
13468 for a pure-specifier; otherwise, we look for a
13469 constant-initializer. When we call `grokfield', it will
13470 perform more stringent semantics checks. */
13471 if (declarator->kind == cdk_function)
13472 initializer = cp_parser_pure_specifier (parser);
13474 /* Parse the initializer. */
13475 initializer = cp_parser_constant_initializer (parser);
13477 /* Otherwise, there is no initializer. */
13479 initializer = NULL_TREE;
13481 /* See if we are probably looking at a function
13482 definition. We are certainly not looking at a
13483 member-declarator. Calling `grokfield' has
13484 side-effects, so we must not do it unless we are sure
13485 that we are looking at a member-declarator. */
13486 if (cp_parser_token_starts_function_definition_p
13487 (cp_lexer_peek_token (parser->lexer)))
13489 /* The grammar does not allow a pure-specifier to be
13490 used when a member function is defined. (It is
13491 possible that this fact is an oversight in the
13492 standard, since a pure function may be defined
13493 outside of the class-specifier. */
13495 error ("pure-specifier on function-definition");
13496 decl = cp_parser_save_member_function_body (parser,
13500 /* If the member was not a friend, declare it here. */
13502 finish_member_declaration (decl);
13503 /* Peek at the next token. */
13504 token = cp_lexer_peek_token (parser->lexer);
13505 /* If the next token is a semicolon, consume it. */
13506 if (token->type == CPP_SEMICOLON)
13507 cp_lexer_consume_token (parser->lexer);
13512 /* Create the declaration. */
13513 decl = grokfield (declarator, &decl_specifiers,
13514 initializer, asm_specification,
13516 /* Any initialization must have been from a
13517 constant-expression. */
13518 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13519 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13523 /* Reset PREFIX_ATTRIBUTES. */
13524 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13525 attributes = TREE_CHAIN (attributes);
13527 TREE_CHAIN (attributes) = NULL_TREE;
13529 /* If there is any qualification still in effect, clear it
13530 now; we will be starting fresh with the next declarator. */
13531 parser->scope = NULL_TREE;
13532 parser->qualifying_scope = NULL_TREE;
13533 parser->object_scope = NULL_TREE;
13534 /* If it's a `,', then there are more declarators. */
13535 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13536 cp_lexer_consume_token (parser->lexer);
13537 /* If the next token isn't a `;', then we have a parse error. */
13538 else if (cp_lexer_next_token_is_not (parser->lexer,
13541 cp_parser_error (parser, "expected %<;%>");
13542 /* Skip tokens until we find a `;'. */
13543 cp_parser_skip_to_end_of_statement (parser);
13550 /* Add DECL to the list of members. */
13552 finish_member_declaration (decl);
13554 if (TREE_CODE (decl) == FUNCTION_DECL)
13555 cp_parser_save_default_args (parser, decl);
13560 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13563 /* Parse a pure-specifier.
13568 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13569 Otherwise, ERROR_MARK_NODE is returned. */
13572 cp_parser_pure_specifier (cp_parser* parser)
13576 /* Look for the `=' token. */
13577 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13578 return error_mark_node;
13579 /* Look for the `0' token. */
13580 token = cp_lexer_consume_token (parser->lexer);
13581 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13583 cp_parser_error (parser,
13584 "invalid pure specifier (only `= 0' is allowed)");
13585 cp_parser_skip_to_end_of_statement (parser);
13586 return error_mark_node;
13589 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13590 We need to get information from the lexer about how the number
13591 was spelled in order to fix this problem. */
13592 return integer_zero_node;
13595 /* Parse a constant-initializer.
13597 constant-initializer:
13598 = constant-expression
13600 Returns a representation of the constant-expression. */
13603 cp_parser_constant_initializer (cp_parser* parser)
13605 /* Look for the `=' token. */
13606 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13607 return error_mark_node;
13609 /* It is invalid to write:
13611 struct S { static const int i = { 7 }; };
13614 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13616 cp_parser_error (parser,
13617 "a brace-enclosed initializer is not allowed here");
13618 /* Consume the opening brace. */
13619 cp_lexer_consume_token (parser->lexer);
13620 /* Skip the initializer. */
13621 cp_parser_skip_to_closing_brace (parser);
13622 /* Look for the trailing `}'. */
13623 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13625 return error_mark_node;
13628 return cp_parser_constant_expression (parser,
13629 /*allow_non_constant=*/false,
13633 /* Derived classes [gram.class.derived] */
13635 /* Parse a base-clause.
13638 : base-specifier-list
13640 base-specifier-list:
13642 base-specifier-list , base-specifier
13644 Returns a TREE_LIST representing the base-classes, in the order in
13645 which they were declared. The representation of each node is as
13646 described by cp_parser_base_specifier.
13648 In the case that no bases are specified, this function will return
13649 NULL_TREE, not ERROR_MARK_NODE. */
13652 cp_parser_base_clause (cp_parser* parser)
13654 tree bases = NULL_TREE;
13656 /* Look for the `:' that begins the list. */
13657 cp_parser_require (parser, CPP_COLON, "`:'");
13659 /* Scan the base-specifier-list. */
13665 /* Look for the base-specifier. */
13666 base = cp_parser_base_specifier (parser);
13667 /* Add BASE to the front of the list. */
13668 if (base != error_mark_node)
13670 TREE_CHAIN (base) = bases;
13673 /* Peek at the next token. */
13674 token = cp_lexer_peek_token (parser->lexer);
13675 /* If it's not a comma, then the list is complete. */
13676 if (token->type != CPP_COMMA)
13678 /* Consume the `,'. */
13679 cp_lexer_consume_token (parser->lexer);
13682 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13683 base class had a qualified name. However, the next name that
13684 appears is certainly not qualified. */
13685 parser->scope = NULL_TREE;
13686 parser->qualifying_scope = NULL_TREE;
13687 parser->object_scope = NULL_TREE;
13689 return nreverse (bases);
13692 /* Parse a base-specifier.
13695 :: [opt] nested-name-specifier [opt] class-name
13696 virtual access-specifier [opt] :: [opt] nested-name-specifier
13698 access-specifier virtual [opt] :: [opt] nested-name-specifier
13701 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13702 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13703 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13704 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13707 cp_parser_base_specifier (cp_parser* parser)
13711 bool virtual_p = false;
13712 bool duplicate_virtual_error_issued_p = false;
13713 bool duplicate_access_error_issued_p = false;
13714 bool class_scope_p, template_p;
13715 tree access = access_default_node;
13718 /* Process the optional `virtual' and `access-specifier'. */
13721 /* Peek at the next token. */
13722 token = cp_lexer_peek_token (parser->lexer);
13723 /* Process `virtual'. */
13724 switch (token->keyword)
13727 /* If `virtual' appears more than once, issue an error. */
13728 if (virtual_p && !duplicate_virtual_error_issued_p)
13730 cp_parser_error (parser,
13731 "%<virtual%> specified more than once in base-specified");
13732 duplicate_virtual_error_issued_p = true;
13737 /* Consume the `virtual' token. */
13738 cp_lexer_consume_token (parser->lexer);
13743 case RID_PROTECTED:
13745 /* If more than one access specifier appears, issue an
13747 if (access != access_default_node
13748 && !duplicate_access_error_issued_p)
13750 cp_parser_error (parser,
13751 "more than one access specifier in base-specified");
13752 duplicate_access_error_issued_p = true;
13755 access = ridpointers[(int) token->keyword];
13757 /* Consume the access-specifier. */
13758 cp_lexer_consume_token (parser->lexer);
13767 /* It is not uncommon to see programs mechanically, erroneously, use
13768 the 'typename' keyword to denote (dependent) qualified types
13769 as base classes. */
13770 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13772 if (!processing_template_decl)
13773 error ("keyword %<typename%> not allowed outside of templates");
13775 error ("keyword %<typename%> not allowed in this context "
13776 "(the base class is implicitly a type)");
13777 cp_lexer_consume_token (parser->lexer);
13780 /* Look for the optional `::' operator. */
13781 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13782 /* Look for the nested-name-specifier. The simplest way to
13787 The keyword `typename' is not permitted in a base-specifier or
13788 mem-initializer; in these contexts a qualified name that
13789 depends on a template-parameter is implicitly assumed to be a
13792 is to pretend that we have seen the `typename' keyword at this
13794 cp_parser_nested_name_specifier_opt (parser,
13795 /*typename_keyword_p=*/true,
13796 /*check_dependency_p=*/true,
13798 /*is_declaration=*/true);
13799 /* If the base class is given by a qualified name, assume that names
13800 we see are type names or templates, as appropriate. */
13801 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13802 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13804 /* Finally, look for the class-name. */
13805 type = cp_parser_class_name (parser,
13809 /*check_dependency_p=*/true,
13810 /*class_head_p=*/false,
13811 /*is_declaration=*/true);
13813 if (type == error_mark_node)
13814 return error_mark_node;
13816 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13819 /* Exception handling [gram.exception] */
13821 /* Parse an (optional) exception-specification.
13823 exception-specification:
13824 throw ( type-id-list [opt] )
13826 Returns a TREE_LIST representing the exception-specification. The
13827 TREE_VALUE of each node is a type. */
13830 cp_parser_exception_specification_opt (cp_parser* parser)
13835 /* Peek at the next token. */
13836 token = cp_lexer_peek_token (parser->lexer);
13837 /* If it's not `throw', then there's no exception-specification. */
13838 if (!cp_parser_is_keyword (token, RID_THROW))
13841 /* Consume the `throw'. */
13842 cp_lexer_consume_token (parser->lexer);
13844 /* Look for the `('. */
13845 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13847 /* Peek at the next token. */
13848 token = cp_lexer_peek_token (parser->lexer);
13849 /* If it's not a `)', then there is a type-id-list. */
13850 if (token->type != CPP_CLOSE_PAREN)
13852 const char *saved_message;
13854 /* Types may not be defined in an exception-specification. */
13855 saved_message = parser->type_definition_forbidden_message;
13856 parser->type_definition_forbidden_message
13857 = "types may not be defined in an exception-specification";
13858 /* Parse the type-id-list. */
13859 type_id_list = cp_parser_type_id_list (parser);
13860 /* Restore the saved message. */
13861 parser->type_definition_forbidden_message = saved_message;
13864 type_id_list = empty_except_spec;
13866 /* Look for the `)'. */
13867 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13869 return type_id_list;
13872 /* Parse an (optional) type-id-list.
13876 type-id-list , type-id
13878 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13879 in the order that the types were presented. */
13882 cp_parser_type_id_list (cp_parser* parser)
13884 tree types = NULL_TREE;
13891 /* Get the next type-id. */
13892 type = cp_parser_type_id (parser);
13893 /* Add it to the list. */
13894 types = add_exception_specifier (types, type, /*complain=*/1);
13895 /* Peek at the next token. */
13896 token = cp_lexer_peek_token (parser->lexer);
13897 /* If it is not a `,', we are done. */
13898 if (token->type != CPP_COMMA)
13900 /* Consume the `,'. */
13901 cp_lexer_consume_token (parser->lexer);
13904 return nreverse (types);
13907 /* Parse a try-block.
13910 try compound-statement handler-seq */
13913 cp_parser_try_block (cp_parser* parser)
13917 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13918 try_block = begin_try_block ();
13919 cp_parser_compound_statement (parser, NULL, true);
13920 finish_try_block (try_block);
13921 cp_parser_handler_seq (parser);
13922 finish_handler_sequence (try_block);
13927 /* Parse a function-try-block.
13929 function-try-block:
13930 try ctor-initializer [opt] function-body handler-seq */
13933 cp_parser_function_try_block (cp_parser* parser)
13936 bool ctor_initializer_p;
13938 /* Look for the `try' keyword. */
13939 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13941 /* Let the rest of the front-end know where we are. */
13942 try_block = begin_function_try_block ();
13943 /* Parse the function-body. */
13945 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13946 /* We're done with the `try' part. */
13947 finish_function_try_block (try_block);
13948 /* Parse the handlers. */
13949 cp_parser_handler_seq (parser);
13950 /* We're done with the handlers. */
13951 finish_function_handler_sequence (try_block);
13953 return ctor_initializer_p;
13956 /* Parse a handler-seq.
13959 handler handler-seq [opt] */
13962 cp_parser_handler_seq (cp_parser* parser)
13968 /* Parse the handler. */
13969 cp_parser_handler (parser);
13970 /* Peek at the next token. */
13971 token = cp_lexer_peek_token (parser->lexer);
13972 /* If it's not `catch' then there are no more handlers. */
13973 if (!cp_parser_is_keyword (token, RID_CATCH))
13978 /* Parse a handler.
13981 catch ( exception-declaration ) compound-statement */
13984 cp_parser_handler (cp_parser* parser)
13989 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13990 handler = begin_handler ();
13991 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13992 declaration = cp_parser_exception_declaration (parser);
13993 finish_handler_parms (declaration, handler);
13994 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13995 cp_parser_compound_statement (parser, NULL, false);
13996 finish_handler (handler);
13999 /* Parse an exception-declaration.
14001 exception-declaration:
14002 type-specifier-seq declarator
14003 type-specifier-seq abstract-declarator
14007 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14008 ellipsis variant is used. */
14011 cp_parser_exception_declaration (cp_parser* parser)
14014 cp_decl_specifier_seq type_specifiers;
14015 cp_declarator *declarator;
14016 const char *saved_message;
14018 /* If it's an ellipsis, it's easy to handle. */
14019 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14021 /* Consume the `...' token. */
14022 cp_lexer_consume_token (parser->lexer);
14026 /* Types may not be defined in exception-declarations. */
14027 saved_message = parser->type_definition_forbidden_message;
14028 parser->type_definition_forbidden_message
14029 = "types may not be defined in exception-declarations";
14031 /* Parse the type-specifier-seq. */
14032 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14034 /* If it's a `)', then there is no declarator. */
14035 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14038 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14039 /*ctor_dtor_or_conv_p=*/NULL,
14040 /*parenthesized_p=*/NULL,
14041 /*member_p=*/false);
14043 /* Restore the saved message. */
14044 parser->type_definition_forbidden_message = saved_message;
14046 if (type_specifiers.any_specifiers_p)
14048 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14049 if (decl == NULL_TREE)
14050 error ("invalid catch parameter");
14058 /* Parse a throw-expression.
14061 throw assignment-expression [opt]
14063 Returns a THROW_EXPR representing the throw-expression. */
14066 cp_parser_throw_expression (cp_parser* parser)
14071 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14072 token = cp_lexer_peek_token (parser->lexer);
14073 /* Figure out whether or not there is an assignment-expression
14074 following the "throw" keyword. */
14075 if (token->type == CPP_COMMA
14076 || token->type == CPP_SEMICOLON
14077 || token->type == CPP_CLOSE_PAREN
14078 || token->type == CPP_CLOSE_SQUARE
14079 || token->type == CPP_CLOSE_BRACE
14080 || token->type == CPP_COLON)
14081 expression = NULL_TREE;
14083 expression = cp_parser_assignment_expression (parser,
14086 return build_throw (expression);
14089 /* GNU Extensions */
14091 /* Parse an (optional) asm-specification.
14094 asm ( string-literal )
14096 If the asm-specification is present, returns a STRING_CST
14097 corresponding to the string-literal. Otherwise, returns
14101 cp_parser_asm_specification_opt (cp_parser* parser)
14104 tree asm_specification;
14106 /* Peek at the next token. */
14107 token = cp_lexer_peek_token (parser->lexer);
14108 /* If the next token isn't the `asm' keyword, then there's no
14109 asm-specification. */
14110 if (!cp_parser_is_keyword (token, RID_ASM))
14113 /* Consume the `asm' token. */
14114 cp_lexer_consume_token (parser->lexer);
14115 /* Look for the `('. */
14116 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14118 /* Look for the string-literal. */
14119 asm_specification = cp_parser_string_literal (parser, false, false);
14121 /* Look for the `)'. */
14122 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14124 return asm_specification;
14127 /* Parse an asm-operand-list.
14131 asm-operand-list , asm-operand
14134 string-literal ( expression )
14135 [ string-literal ] string-literal ( expression )
14137 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14138 each node is the expression. The TREE_PURPOSE is itself a
14139 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14140 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14141 is a STRING_CST for the string literal before the parenthesis. */
14144 cp_parser_asm_operand_list (cp_parser* parser)
14146 tree asm_operands = NULL_TREE;
14150 tree string_literal;
14154 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14156 /* Consume the `[' token. */
14157 cp_lexer_consume_token (parser->lexer);
14158 /* Read the operand name. */
14159 name = cp_parser_identifier (parser);
14160 if (name != error_mark_node)
14161 name = build_string (IDENTIFIER_LENGTH (name),
14162 IDENTIFIER_POINTER (name));
14163 /* Look for the closing `]'. */
14164 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14168 /* Look for the string-literal. */
14169 string_literal = cp_parser_string_literal (parser, false, false);
14171 /* Look for the `('. */
14172 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14173 /* Parse the expression. */
14174 expression = cp_parser_expression (parser, /*cast_p=*/false);
14175 /* Look for the `)'. */
14176 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14178 /* Add this operand to the list. */
14179 asm_operands = tree_cons (build_tree_list (name, string_literal),
14182 /* If the next token is not a `,', there are no more
14184 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14186 /* Consume the `,'. */
14187 cp_lexer_consume_token (parser->lexer);
14190 return nreverse (asm_operands);
14193 /* Parse an asm-clobber-list.
14197 asm-clobber-list , string-literal
14199 Returns a TREE_LIST, indicating the clobbers in the order that they
14200 appeared. The TREE_VALUE of each node is a STRING_CST. */
14203 cp_parser_asm_clobber_list (cp_parser* parser)
14205 tree clobbers = NULL_TREE;
14209 tree string_literal;
14211 /* Look for the string literal. */
14212 string_literal = cp_parser_string_literal (parser, false, false);
14213 /* Add it to the list. */
14214 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14215 /* If the next token is not a `,', then the list is
14217 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14219 /* Consume the `,' token. */
14220 cp_lexer_consume_token (parser->lexer);
14226 /* Parse an (optional) series of attributes.
14229 attributes attribute
14232 __attribute__ (( attribute-list [opt] ))
14234 The return value is as for cp_parser_attribute_list. */
14237 cp_parser_attributes_opt (cp_parser* parser)
14239 tree attributes = NULL_TREE;
14244 tree attribute_list;
14246 /* Peek at the next token. */
14247 token = cp_lexer_peek_token (parser->lexer);
14248 /* If it's not `__attribute__', then we're done. */
14249 if (token->keyword != RID_ATTRIBUTE)
14252 /* Consume the `__attribute__' keyword. */
14253 cp_lexer_consume_token (parser->lexer);
14254 /* Look for the two `(' tokens. */
14255 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14256 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14258 /* Peek at the next token. */
14259 token = cp_lexer_peek_token (parser->lexer);
14260 if (token->type != CPP_CLOSE_PAREN)
14261 /* Parse the attribute-list. */
14262 attribute_list = cp_parser_attribute_list (parser);
14264 /* If the next token is a `)', then there is no attribute
14266 attribute_list = NULL;
14268 /* Look for the two `)' tokens. */
14269 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14270 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14272 /* Add these new attributes to the list. */
14273 attributes = chainon (attributes, attribute_list);
14279 /* Parse an attribute-list.
14283 attribute-list , attribute
14287 identifier ( identifier )
14288 identifier ( identifier , expression-list )
14289 identifier ( expression-list )
14291 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14292 to an attribute. The TREE_PURPOSE of each node is the identifier
14293 indicating which attribute is in use. The TREE_VALUE represents
14294 the arguments, if any. */
14297 cp_parser_attribute_list (cp_parser* parser)
14299 tree attribute_list = NULL_TREE;
14300 bool save_translate_strings_p = parser->translate_strings_p;
14302 parser->translate_strings_p = false;
14309 /* Look for the identifier. We also allow keywords here; for
14310 example `__attribute__ ((const))' is legal. */
14311 token = cp_lexer_peek_token (parser->lexer);
14312 if (token->type == CPP_NAME
14313 || token->type == CPP_KEYWORD)
14315 /* Consume the token. */
14316 token = cp_lexer_consume_token (parser->lexer);
14318 /* Save away the identifier that indicates which attribute
14320 identifier = token->value;
14321 attribute = build_tree_list (identifier, NULL_TREE);
14323 /* Peek at the next token. */
14324 token = cp_lexer_peek_token (parser->lexer);
14325 /* If it's an `(', then parse the attribute arguments. */
14326 if (token->type == CPP_OPEN_PAREN)
14330 arguments = (cp_parser_parenthesized_expression_list
14331 (parser, true, /*cast_p=*/false,
14332 /*non_constant_p=*/NULL));
14333 /* Save the identifier and arguments away. */
14334 TREE_VALUE (attribute) = arguments;
14337 /* Add this attribute to the list. */
14338 TREE_CHAIN (attribute) = attribute_list;
14339 attribute_list = attribute;
14341 token = cp_lexer_peek_token (parser->lexer);
14343 /* Now, look for more attributes. If the next token isn't a
14344 `,', we're done. */
14345 if (token->type != CPP_COMMA)
14348 /* Consume the comma and keep going. */
14349 cp_lexer_consume_token (parser->lexer);
14351 parser->translate_strings_p = save_translate_strings_p;
14353 /* We built up the list in reverse order. */
14354 return nreverse (attribute_list);
14357 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14358 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14359 current value of the PEDANTIC flag, regardless of whether or not
14360 the `__extension__' keyword is present. The caller is responsible
14361 for restoring the value of the PEDANTIC flag. */
14364 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14366 /* Save the old value of the PEDANTIC flag. */
14367 *saved_pedantic = pedantic;
14369 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14371 /* Consume the `__extension__' token. */
14372 cp_lexer_consume_token (parser->lexer);
14373 /* We're not being pedantic while the `__extension__' keyword is
14383 /* Parse a label declaration.
14386 __label__ label-declarator-seq ;
14388 label-declarator-seq:
14389 identifier , label-declarator-seq
14393 cp_parser_label_declaration (cp_parser* parser)
14395 /* Look for the `__label__' keyword. */
14396 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14402 /* Look for an identifier. */
14403 identifier = cp_parser_identifier (parser);
14404 /* If we failed, stop. */
14405 if (identifier == error_mark_node)
14407 /* Declare it as a label. */
14408 finish_label_decl (identifier);
14409 /* If the next token is a `;', stop. */
14410 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14412 /* Look for the `,' separating the label declarations. */
14413 cp_parser_require (parser, CPP_COMMA, "`,'");
14416 /* Look for the final `;'. */
14417 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14420 /* Support Functions */
14422 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14423 NAME should have one of the representations used for an
14424 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14425 is returned. If PARSER->SCOPE is a dependent type, then a
14426 SCOPE_REF is returned.
14428 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14429 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14430 was formed. Abstractly, such entities should not be passed to this
14431 function, because they do not need to be looked up, but it is
14432 simpler to check for this special case here, rather than at the
14435 In cases not explicitly covered above, this function returns a
14436 DECL, OVERLOAD, or baselink representing the result of the lookup.
14437 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14440 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14441 (e.g., "struct") that was used. In that case bindings that do not
14442 refer to types are ignored.
14444 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14447 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14450 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14453 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14454 results in an ambiguity, and false otherwise. */
14457 cp_parser_lookup_name (cp_parser *parser, tree name,
14458 enum tag_types tag_type,
14459 bool is_template, bool is_namespace,
14460 bool check_dependency,
14464 tree object_type = parser->context->object_type;
14466 /* Assume that the lookup will be unambiguous. */
14468 *ambiguous_p = false;
14470 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14471 no longer valid. Note that if we are parsing tentatively, and
14472 the parse fails, OBJECT_TYPE will be automatically restored. */
14473 parser->context->object_type = NULL_TREE;
14475 if (name == error_mark_node)
14476 return error_mark_node;
14478 /* A template-id has already been resolved; there is no lookup to
14480 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14482 if (BASELINK_P (name))
14484 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14485 == TEMPLATE_ID_EXPR);
14489 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14490 it should already have been checked to make sure that the name
14491 used matches the type being destroyed. */
14492 if (TREE_CODE (name) == BIT_NOT_EXPR)
14496 /* Figure out to which type this destructor applies. */
14498 type = parser->scope;
14499 else if (object_type)
14500 type = object_type;
14502 type = current_class_type;
14503 /* If that's not a class type, there is no destructor. */
14504 if (!type || !CLASS_TYPE_P (type))
14505 return error_mark_node;
14506 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14507 lazily_declare_fn (sfk_destructor, type);
14508 if (!CLASSTYPE_DESTRUCTORS (type))
14509 return error_mark_node;
14510 /* If it was a class type, return the destructor. */
14511 return CLASSTYPE_DESTRUCTORS (type);
14514 /* By this point, the NAME should be an ordinary identifier. If
14515 the id-expression was a qualified name, the qualifying scope is
14516 stored in PARSER->SCOPE at this point. */
14517 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14519 /* Perform the lookup. */
14524 if (parser->scope == error_mark_node)
14525 return error_mark_node;
14527 /* If the SCOPE is dependent, the lookup must be deferred until
14528 the template is instantiated -- unless we are explicitly
14529 looking up names in uninstantiated templates. Even then, we
14530 cannot look up the name if the scope is not a class type; it
14531 might, for example, be a template type parameter. */
14532 dependent_p = (TYPE_P (parser->scope)
14533 && !(parser->in_declarator_p
14534 && currently_open_class (parser->scope))
14535 && dependent_type_p (parser->scope));
14536 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14543 /* The resolution to Core Issue 180 says that `struct
14544 A::B' should be considered a type-name, even if `A'
14546 type = make_typename_type (parser->scope, name, tag_type,
14548 decl = TYPE_NAME (type);
14550 else if (is_template)
14551 decl = make_unbound_class_template (parser->scope,
14555 decl = build_nt (SCOPE_REF, parser->scope, name);
14559 tree pushed_scope = NULL_TREE;
14561 /* If PARSER->SCOPE is a dependent type, then it must be a
14562 class type, and we must not be checking dependencies;
14563 otherwise, we would have processed this lookup above. So
14564 that PARSER->SCOPE is not considered a dependent base by
14565 lookup_member, we must enter the scope here. */
14567 pushed_scope = push_scope (parser->scope);
14568 /* If the PARSER->SCOPE is a template specialization, it
14569 may be instantiated during name lookup. In that case,
14570 errors may be issued. Even if we rollback the current
14571 tentative parse, those errors are valid. */
14572 decl = lookup_qualified_name (parser->scope, name,
14573 tag_type != none_type,
14574 /*complain=*/true);
14576 pop_scope (pushed_scope);
14578 parser->qualifying_scope = parser->scope;
14579 parser->object_scope = NULL_TREE;
14581 else if (object_type)
14583 tree object_decl = NULL_TREE;
14584 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14585 OBJECT_TYPE is not a class. */
14586 if (CLASS_TYPE_P (object_type))
14587 /* If the OBJECT_TYPE is a template specialization, it may
14588 be instantiated during name lookup. In that case, errors
14589 may be issued. Even if we rollback the current tentative
14590 parse, those errors are valid. */
14591 object_decl = lookup_member (object_type,
14594 tag_type != none_type);
14595 /* Look it up in the enclosing context, too. */
14596 decl = lookup_name_real (name, tag_type != none_type,
14598 /*block_p=*/true, is_namespace,
14600 parser->object_scope = object_type;
14601 parser->qualifying_scope = NULL_TREE;
14603 decl = object_decl;
14607 decl = lookup_name_real (name, tag_type != none_type,
14609 /*block_p=*/true, is_namespace,
14611 parser->qualifying_scope = NULL_TREE;
14612 parser->object_scope = NULL_TREE;
14615 /* If the lookup failed, let our caller know. */
14616 if (!decl || decl == error_mark_node)
14617 return error_mark_node;
14619 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14620 if (TREE_CODE (decl) == TREE_LIST)
14623 *ambiguous_p = true;
14624 /* The error message we have to print is too complicated for
14625 cp_parser_error, so we incorporate its actions directly. */
14626 if (!cp_parser_simulate_error (parser))
14628 error ("reference to %qD is ambiguous", name);
14629 print_candidates (decl);
14631 return error_mark_node;
14634 gcc_assert (DECL_P (decl)
14635 || TREE_CODE (decl) == OVERLOAD
14636 || TREE_CODE (decl) == SCOPE_REF
14637 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14638 || BASELINK_P (decl));
14640 /* If we have resolved the name of a member declaration, check to
14641 see if the declaration is accessible. When the name resolves to
14642 set of overloaded functions, accessibility is checked when
14643 overload resolution is done.
14645 During an explicit instantiation, access is not checked at all,
14646 as per [temp.explicit]. */
14648 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14653 /* Like cp_parser_lookup_name, but for use in the typical case where
14654 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14655 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14658 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14660 return cp_parser_lookup_name (parser, name,
14662 /*is_template=*/false,
14663 /*is_namespace=*/false,
14664 /*check_dependency=*/true,
14665 /*ambiguous_p=*/NULL);
14668 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14669 the current context, return the TYPE_DECL. If TAG_NAME_P is
14670 true, the DECL indicates the class being defined in a class-head,
14671 or declared in an elaborated-type-specifier.
14673 Otherwise, return DECL. */
14676 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14678 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14679 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14682 template <typename T> struct B;
14685 template <typename T> struct A::B {};
14687 Similarly, in a elaborated-type-specifier:
14689 namespace N { struct X{}; }
14692 template <typename T> friend struct N::X;
14695 However, if the DECL refers to a class type, and we are in
14696 the scope of the class, then the name lookup automatically
14697 finds the TYPE_DECL created by build_self_reference rather
14698 than a TEMPLATE_DECL. For example, in:
14700 template <class T> struct S {
14704 there is no need to handle such case. */
14706 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14707 return DECL_TEMPLATE_RESULT (decl);
14712 /* If too many, or too few, template-parameter lists apply to the
14713 declarator, issue an error message. Returns TRUE if all went well,
14714 and FALSE otherwise. */
14717 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14718 cp_declarator *declarator)
14720 unsigned num_templates;
14722 /* We haven't seen any classes that involve template parameters yet. */
14725 switch (declarator->kind)
14728 if (declarator->u.id.qualifying_scope)
14733 scope = declarator->u.id.qualifying_scope;
14734 member = declarator->u.id.unqualified_name;
14736 while (scope && CLASS_TYPE_P (scope))
14738 /* You're supposed to have one `template <...>'
14739 for every template class, but you don't need one
14740 for a full specialization. For example:
14742 template <class T> struct S{};
14743 template <> struct S<int> { void f(); };
14744 void S<int>::f () {}
14746 is correct; there shouldn't be a `template <>' for
14747 the definition of `S<int>::f'. */
14748 if (CLASSTYPE_TEMPLATE_INFO (scope)
14749 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14750 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14751 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14754 scope = TYPE_CONTEXT (scope);
14757 else if (TREE_CODE (declarator->u.id.unqualified_name)
14758 == TEMPLATE_ID_EXPR)
14759 /* If the DECLARATOR has the form `X<y>' then it uses one
14760 additional level of template parameters. */
14763 return cp_parser_check_template_parameters (parser,
14769 case cdk_reference:
14771 return (cp_parser_check_declarator_template_parameters
14772 (parser, declarator->declarator));
14778 gcc_unreachable ();
14783 /* NUM_TEMPLATES were used in the current declaration. If that is
14784 invalid, return FALSE and issue an error messages. Otherwise,
14788 cp_parser_check_template_parameters (cp_parser* parser,
14789 unsigned num_templates)
14791 /* If there are more template classes than parameter lists, we have
14794 template <class T> void S<T>::R<T>::f (); */
14795 if (parser->num_template_parameter_lists < num_templates)
14797 error ("too few template-parameter-lists");
14800 /* If there are the same number of template classes and parameter
14801 lists, that's OK. */
14802 if (parser->num_template_parameter_lists == num_templates)
14804 /* If there are more, but only one more, then we are referring to a
14805 member template. That's OK too. */
14806 if (parser->num_template_parameter_lists == num_templates + 1)
14808 /* Otherwise, there are too many template parameter lists. We have
14811 template <class T> template <class U> void S::f(); */
14812 error ("too many template-parameter-lists");
14816 /* Parse an optional `::' token indicating that the following name is
14817 from the global namespace. If so, PARSER->SCOPE is set to the
14818 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14819 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14820 Returns the new value of PARSER->SCOPE, if the `::' token is
14821 present, and NULL_TREE otherwise. */
14824 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14828 /* Peek at the next token. */
14829 token = cp_lexer_peek_token (parser->lexer);
14830 /* If we're looking at a `::' token then we're starting from the
14831 global namespace, not our current location. */
14832 if (token->type == CPP_SCOPE)
14834 /* Consume the `::' token. */
14835 cp_lexer_consume_token (parser->lexer);
14836 /* Set the SCOPE so that we know where to start the lookup. */
14837 parser->scope = global_namespace;
14838 parser->qualifying_scope = global_namespace;
14839 parser->object_scope = NULL_TREE;
14841 return parser->scope;
14843 else if (!current_scope_valid_p)
14845 parser->scope = NULL_TREE;
14846 parser->qualifying_scope = NULL_TREE;
14847 parser->object_scope = NULL_TREE;
14853 /* Returns TRUE if the upcoming token sequence is the start of a
14854 constructor declarator. If FRIEND_P is true, the declarator is
14855 preceded by the `friend' specifier. */
14858 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14860 bool constructor_p;
14861 tree type_decl = NULL_TREE;
14862 bool nested_name_p;
14863 cp_token *next_token;
14865 /* The common case is that this is not a constructor declarator, so
14866 try to avoid doing lots of work if at all possible. It's not
14867 valid declare a constructor at function scope. */
14868 if (at_function_scope_p ())
14870 /* And only certain tokens can begin a constructor declarator. */
14871 next_token = cp_lexer_peek_token (parser->lexer);
14872 if (next_token->type != CPP_NAME
14873 && next_token->type != CPP_SCOPE
14874 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14875 && next_token->type != CPP_TEMPLATE_ID)
14878 /* Parse tentatively; we are going to roll back all of the tokens
14880 cp_parser_parse_tentatively (parser);
14881 /* Assume that we are looking at a constructor declarator. */
14882 constructor_p = true;
14884 /* Look for the optional `::' operator. */
14885 cp_parser_global_scope_opt (parser,
14886 /*current_scope_valid_p=*/false);
14887 /* Look for the nested-name-specifier. */
14889 = (cp_parser_nested_name_specifier_opt (parser,
14890 /*typename_keyword_p=*/false,
14891 /*check_dependency_p=*/false,
14893 /*is_declaration=*/false)
14895 /* Outside of a class-specifier, there must be a
14896 nested-name-specifier. */
14897 if (!nested_name_p &&
14898 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14900 constructor_p = false;
14901 /* If we still think that this might be a constructor-declarator,
14902 look for a class-name. */
14907 template <typename T> struct S { S(); };
14908 template <typename T> S<T>::S ();
14910 we must recognize that the nested `S' names a class.
14913 template <typename T> S<T>::S<T> ();
14915 we must recognize that the nested `S' names a template. */
14916 type_decl = cp_parser_class_name (parser,
14917 /*typename_keyword_p=*/false,
14918 /*template_keyword_p=*/false,
14920 /*check_dependency_p=*/false,
14921 /*class_head_p=*/false,
14922 /*is_declaration=*/false);
14923 /* If there was no class-name, then this is not a constructor. */
14924 constructor_p = !cp_parser_error_occurred (parser);
14927 /* If we're still considering a constructor, we have to see a `(',
14928 to begin the parameter-declaration-clause, followed by either a
14929 `)', an `...', or a decl-specifier. We need to check for a
14930 type-specifier to avoid being fooled into thinking that:
14934 is a constructor. (It is actually a function named `f' that
14935 takes one parameter (of type `int') and returns a value of type
14938 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14941 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14942 /* A parameter declaration begins with a decl-specifier,
14943 which is either the "attribute" keyword, a storage class
14944 specifier, or (usually) a type-specifier. */
14945 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14946 && !cp_parser_storage_class_specifier_opt (parser))
14949 tree pushed_scope = NULL_TREE;
14950 unsigned saved_num_template_parameter_lists;
14952 /* Names appearing in the type-specifier should be looked up
14953 in the scope of the class. */
14954 if (current_class_type)
14958 type = TREE_TYPE (type_decl);
14959 if (TREE_CODE (type) == TYPENAME_TYPE)
14961 type = resolve_typename_type (type,
14962 /*only_current_p=*/false);
14963 if (type == error_mark_node)
14965 cp_parser_abort_tentative_parse (parser);
14969 pushed_scope = push_scope (type);
14972 /* Inside the constructor parameter list, surrounding
14973 template-parameter-lists do not apply. */
14974 saved_num_template_parameter_lists
14975 = parser->num_template_parameter_lists;
14976 parser->num_template_parameter_lists = 0;
14978 /* Look for the type-specifier. */
14979 cp_parser_type_specifier (parser,
14980 CP_PARSER_FLAGS_NONE,
14981 /*decl_specs=*/NULL,
14982 /*is_declarator=*/true,
14983 /*declares_class_or_enum=*/NULL,
14984 /*is_cv_qualifier=*/NULL);
14986 parser->num_template_parameter_lists
14987 = saved_num_template_parameter_lists;
14989 /* Leave the scope of the class. */
14991 pop_scope (pushed_scope);
14993 constructor_p = !cp_parser_error_occurred (parser);
14997 constructor_p = false;
14998 /* We did not really want to consume any tokens. */
14999 cp_parser_abort_tentative_parse (parser);
15001 return constructor_p;
15004 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15005 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15006 they must be performed once we are in the scope of the function.
15008 Returns the function defined. */
15011 cp_parser_function_definition_from_specifiers_and_declarator
15012 (cp_parser* parser,
15013 cp_decl_specifier_seq *decl_specifiers,
15015 const cp_declarator *declarator)
15020 /* Begin the function-definition. */
15021 success_p = start_function (decl_specifiers, declarator, attributes);
15023 /* The things we're about to see are not directly qualified by any
15024 template headers we've seen thus far. */
15025 reset_specialization ();
15027 /* If there were names looked up in the decl-specifier-seq that we
15028 did not check, check them now. We must wait until we are in the
15029 scope of the function to perform the checks, since the function
15030 might be a friend. */
15031 perform_deferred_access_checks ();
15035 /* Skip the entire function. */
15036 error ("invalid function declaration");
15037 cp_parser_skip_to_end_of_block_or_statement (parser);
15038 fn = error_mark_node;
15041 fn = cp_parser_function_definition_after_declarator (parser,
15042 /*inline_p=*/false);
15047 /* Parse the part of a function-definition that follows the
15048 declarator. INLINE_P is TRUE iff this function is an inline
15049 function defined with a class-specifier.
15051 Returns the function defined. */
15054 cp_parser_function_definition_after_declarator (cp_parser* parser,
15058 bool ctor_initializer_p = false;
15059 bool saved_in_unbraced_linkage_specification_p;
15060 unsigned saved_num_template_parameter_lists;
15062 /* If the next token is `return', then the code may be trying to
15063 make use of the "named return value" extension that G++ used to
15065 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15067 /* Consume the `return' keyword. */
15068 cp_lexer_consume_token (parser->lexer);
15069 /* Look for the identifier that indicates what value is to be
15071 cp_parser_identifier (parser);
15072 /* Issue an error message. */
15073 error ("named return values are no longer supported");
15074 /* Skip tokens until we reach the start of the function body. */
15075 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15076 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15077 cp_lexer_consume_token (parser->lexer);
15079 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15080 anything declared inside `f'. */
15081 saved_in_unbraced_linkage_specification_p
15082 = parser->in_unbraced_linkage_specification_p;
15083 parser->in_unbraced_linkage_specification_p = false;
15084 /* Inside the function, surrounding template-parameter-lists do not
15086 saved_num_template_parameter_lists
15087 = parser->num_template_parameter_lists;
15088 parser->num_template_parameter_lists = 0;
15089 /* If the next token is `try', then we are looking at a
15090 function-try-block. */
15091 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15092 ctor_initializer_p = cp_parser_function_try_block (parser);
15093 /* A function-try-block includes the function-body, so we only do
15094 this next part if we're not processing a function-try-block. */
15097 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15099 /* Finish the function. */
15100 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15101 (inline_p ? 2 : 0));
15102 /* Generate code for it, if necessary. */
15103 expand_or_defer_fn (fn);
15104 /* Restore the saved values. */
15105 parser->in_unbraced_linkage_specification_p
15106 = saved_in_unbraced_linkage_specification_p;
15107 parser->num_template_parameter_lists
15108 = saved_num_template_parameter_lists;
15113 /* Parse a template-declaration, assuming that the `export' (and
15114 `extern') keywords, if present, has already been scanned. MEMBER_P
15115 is as for cp_parser_template_declaration. */
15118 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15120 tree decl = NULL_TREE;
15121 tree parameter_list;
15122 bool friend_p = false;
15124 /* Look for the `template' keyword. */
15125 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15129 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15132 /* If the next token is `>', then we have an invalid
15133 specialization. Rather than complain about an invalid template
15134 parameter, issue an error message here. */
15135 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15137 cp_parser_error (parser, "invalid explicit specialization");
15138 begin_specialization ();
15139 parameter_list = NULL_TREE;
15143 /* Parse the template parameters. */
15144 begin_template_parm_list ();
15145 parameter_list = cp_parser_template_parameter_list (parser);
15146 parameter_list = end_template_parm_list (parameter_list);
15149 /* Look for the `>'. */
15150 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15151 /* We just processed one more parameter list. */
15152 ++parser->num_template_parameter_lists;
15153 /* If the next token is `template', there are more template
15155 if (cp_lexer_next_token_is_keyword (parser->lexer,
15157 cp_parser_template_declaration_after_export (parser, member_p);
15160 /* There are no access checks when parsing a template, as we do not
15161 know if a specialization will be a friend. */
15162 push_deferring_access_checks (dk_no_check);
15164 decl = cp_parser_single_declaration (parser,
15168 pop_deferring_access_checks ();
15170 /* If this is a member template declaration, let the front
15172 if (member_p && !friend_p && decl)
15174 if (TREE_CODE (decl) == TYPE_DECL)
15175 cp_parser_check_access_in_redeclaration (decl);
15177 decl = finish_member_template_decl (decl);
15179 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15180 make_friend_class (current_class_type, TREE_TYPE (decl),
15181 /*complain=*/true);
15183 /* We are done with the current parameter list. */
15184 --parser->num_template_parameter_lists;
15187 finish_template_decl (parameter_list);
15189 /* Register member declarations. */
15190 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15191 finish_member_declaration (decl);
15193 /* If DECL is a function template, we must return to parse it later.
15194 (Even though there is no definition, there might be default
15195 arguments that need handling.) */
15196 if (member_p && decl
15197 && (TREE_CODE (decl) == FUNCTION_DECL
15198 || DECL_FUNCTION_TEMPLATE_P (decl)))
15199 TREE_VALUE (parser->unparsed_functions_queues)
15200 = tree_cons (NULL_TREE, decl,
15201 TREE_VALUE (parser->unparsed_functions_queues));
15204 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15205 `function-definition' sequence. MEMBER_P is true, this declaration
15206 appears in a class scope.
15208 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15209 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15212 cp_parser_single_declaration (cp_parser* parser,
15216 int declares_class_or_enum;
15217 tree decl = NULL_TREE;
15218 cp_decl_specifier_seq decl_specifiers;
15219 bool function_definition_p = false;
15221 /* This function is only used when processing a template
15223 gcc_assert (innermost_scope_kind () == sk_template_parms
15224 || innermost_scope_kind () == sk_template_spec);
15226 /* Defer access checks until we know what is being declared. */
15227 push_deferring_access_checks (dk_deferred);
15229 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15231 cp_parser_decl_specifier_seq (parser,
15232 CP_PARSER_FLAGS_OPTIONAL,
15234 &declares_class_or_enum);
15236 *friend_p = cp_parser_friend_p (&decl_specifiers);
15238 /* There are no template typedefs. */
15239 if (decl_specifiers.specs[(int) ds_typedef])
15241 error ("template declaration of %qs", "typedef");
15242 decl = error_mark_node;
15245 /* Gather up the access checks that occurred the
15246 decl-specifier-seq. */
15247 stop_deferring_access_checks ();
15249 /* Check for the declaration of a template class. */
15250 if (declares_class_or_enum)
15252 if (cp_parser_declares_only_class_p (parser))
15254 decl = shadow_tag (&decl_specifiers);
15259 friend template <typename T> struct A<T>::B;
15262 A<T>::B will be represented by a TYPENAME_TYPE, and
15263 therefore not recognized by shadow_tag. */
15264 if (friend_p && *friend_p
15266 && decl_specifiers.type
15267 && TYPE_P (decl_specifiers.type))
15268 decl = decl_specifiers.type;
15270 if (decl && decl != error_mark_node)
15271 decl = TYPE_NAME (decl);
15273 decl = error_mark_node;
15276 /* If it's not a template class, try for a template function. If
15277 the next token is a `;', then this declaration does not declare
15278 anything. But, if there were errors in the decl-specifiers, then
15279 the error might well have come from an attempted class-specifier.
15280 In that case, there's no need to warn about a missing declarator. */
15282 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15283 || decl_specifiers.type != error_mark_node))
15284 decl = cp_parser_init_declarator (parser,
15286 /*function_definition_allowed_p=*/true,
15288 declares_class_or_enum,
15289 &function_definition_p);
15291 pop_deferring_access_checks ();
15293 /* Clear any current qualification; whatever comes next is the start
15294 of something new. */
15295 parser->scope = NULL_TREE;
15296 parser->qualifying_scope = NULL_TREE;
15297 parser->object_scope = NULL_TREE;
15298 /* Look for a trailing `;' after the declaration. */
15299 if (!function_definition_p
15300 && (decl == error_mark_node
15301 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15302 cp_parser_skip_to_end_of_block_or_statement (parser);
15307 /* Parse a cast-expression that is not the operand of a unary "&". */
15310 cp_parser_simple_cast_expression (cp_parser *parser)
15312 return cp_parser_cast_expression (parser, /*address_p=*/false,
15316 /* Parse a functional cast to TYPE. Returns an expression
15317 representing the cast. */
15320 cp_parser_functional_cast (cp_parser* parser, tree type)
15322 tree expression_list;
15326 = cp_parser_parenthesized_expression_list (parser, false,
15328 /*non_constant_p=*/NULL);
15330 cast = build_functional_cast (type, expression_list);
15331 /* [expr.const]/1: In an integral constant expression "only type
15332 conversions to integral or enumeration type can be used". */
15333 if (cast != error_mark_node && !type_dependent_expression_p (type)
15334 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15336 if (cp_parser_non_integral_constant_expression
15337 (parser, "a call to a constructor"))
15338 return error_mark_node;
15343 /* Save the tokens that make up the body of a member function defined
15344 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15345 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15346 specifiers applied to the declaration. Returns the FUNCTION_DECL
15347 for the member function. */
15350 cp_parser_save_member_function_body (cp_parser* parser,
15351 cp_decl_specifier_seq *decl_specifiers,
15352 cp_declarator *declarator,
15359 /* Create the function-declaration. */
15360 fn = start_method (decl_specifiers, declarator, attributes);
15361 /* If something went badly wrong, bail out now. */
15362 if (fn == error_mark_node)
15364 /* If there's a function-body, skip it. */
15365 if (cp_parser_token_starts_function_definition_p
15366 (cp_lexer_peek_token (parser->lexer)))
15367 cp_parser_skip_to_end_of_block_or_statement (parser);
15368 return error_mark_node;
15371 /* Remember it, if there default args to post process. */
15372 cp_parser_save_default_args (parser, fn);
15374 /* Save away the tokens that make up the body of the
15376 first = parser->lexer->next_token;
15377 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15378 /* Handle function try blocks. */
15379 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15380 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15381 last = parser->lexer->next_token;
15383 /* Save away the inline definition; we will process it when the
15384 class is complete. */
15385 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15386 DECL_PENDING_INLINE_P (fn) = 1;
15388 /* We need to know that this was defined in the class, so that
15389 friend templates are handled correctly. */
15390 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15392 /* We're done with the inline definition. */
15393 finish_method (fn);
15395 /* Add FN to the queue of functions to be parsed later. */
15396 TREE_VALUE (parser->unparsed_functions_queues)
15397 = tree_cons (NULL_TREE, fn,
15398 TREE_VALUE (parser->unparsed_functions_queues));
15403 /* Parse a template-argument-list, as well as the trailing ">" (but
15404 not the opening ">"). See cp_parser_template_argument_list for the
15408 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15412 tree saved_qualifying_scope;
15413 tree saved_object_scope;
15414 bool saved_greater_than_is_operator_p;
15418 When parsing a template-id, the first non-nested `>' is taken as
15419 the end of the template-argument-list rather than a greater-than
15421 saved_greater_than_is_operator_p
15422 = parser->greater_than_is_operator_p;
15423 parser->greater_than_is_operator_p = false;
15424 /* Parsing the argument list may modify SCOPE, so we save it
15426 saved_scope = parser->scope;
15427 saved_qualifying_scope = parser->qualifying_scope;
15428 saved_object_scope = parser->object_scope;
15429 /* Parse the template-argument-list itself. */
15430 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15431 arguments = NULL_TREE;
15433 arguments = cp_parser_template_argument_list (parser);
15434 /* Look for the `>' that ends the template-argument-list. If we find
15435 a '>>' instead, it's probably just a typo. */
15436 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15438 if (!saved_greater_than_is_operator_p)
15440 /* If we're in a nested template argument list, the '>>' has
15441 to be a typo for '> >'. We emit the error message, but we
15442 continue parsing and we push a '>' as next token, so that
15443 the argument list will be parsed correctly. Note that the
15444 global source location is still on the token before the
15445 '>>', so we need to say explicitly where we want it. */
15446 cp_token *token = cp_lexer_peek_token (parser->lexer);
15447 error ("%H%<>>%> should be %<> >%> "
15448 "within a nested template argument list",
15451 /* ??? Proper recovery should terminate two levels of
15452 template argument list here. */
15453 token->type = CPP_GREATER;
15457 /* If this is not a nested template argument list, the '>>'
15458 is a typo for '>'. Emit an error message and continue.
15459 Same deal about the token location, but here we can get it
15460 right by consuming the '>>' before issuing the diagnostic. */
15461 cp_lexer_consume_token (parser->lexer);
15462 error ("spurious %<>>%>, use %<>%> to terminate "
15463 "a template argument list");
15466 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15467 error ("missing %<>%> to terminate the template argument list");
15469 /* It's what we want, a '>'; consume it. */
15470 cp_lexer_consume_token (parser->lexer);
15471 /* The `>' token might be a greater-than operator again now. */
15472 parser->greater_than_is_operator_p
15473 = saved_greater_than_is_operator_p;
15474 /* Restore the SAVED_SCOPE. */
15475 parser->scope = saved_scope;
15476 parser->qualifying_scope = saved_qualifying_scope;
15477 parser->object_scope = saved_object_scope;
15482 /* MEMBER_FUNCTION is a member function, or a friend. If default
15483 arguments, or the body of the function have not yet been parsed,
15487 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15489 /* If this member is a template, get the underlying
15491 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15492 member_function = DECL_TEMPLATE_RESULT (member_function);
15494 /* There should not be any class definitions in progress at this
15495 point; the bodies of members are only parsed outside of all class
15497 gcc_assert (parser->num_classes_being_defined == 0);
15498 /* While we're parsing the member functions we might encounter more
15499 classes. We want to handle them right away, but we don't want
15500 them getting mixed up with functions that are currently in the
15502 parser->unparsed_functions_queues
15503 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15505 /* Make sure that any template parameters are in scope. */
15506 maybe_begin_member_template_processing (member_function);
15508 /* If the body of the function has not yet been parsed, parse it
15510 if (DECL_PENDING_INLINE_P (member_function))
15512 tree function_scope;
15513 cp_token_cache *tokens;
15515 /* The function is no longer pending; we are processing it. */
15516 tokens = DECL_PENDING_INLINE_INFO (member_function);
15517 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15518 DECL_PENDING_INLINE_P (member_function) = 0;
15520 /* If this is a local class, enter the scope of the containing
15522 function_scope = current_function_decl;
15523 if (function_scope)
15524 push_function_context_to (function_scope);
15527 /* Push the body of the function onto the lexer stack. */
15528 cp_parser_push_lexer_for_tokens (parser, tokens);
15530 /* Let the front end know that we going to be defining this
15532 start_preparsed_function (member_function, NULL_TREE,
15533 SF_PRE_PARSED | SF_INCLASS_INLINE);
15535 /* Don't do access checking if it is a templated function. */
15536 if (processing_template_decl)
15537 push_deferring_access_checks (dk_no_check);
15539 /* Now, parse the body of the function. */
15540 cp_parser_function_definition_after_declarator (parser,
15541 /*inline_p=*/true);
15543 if (processing_template_decl)
15544 pop_deferring_access_checks ();
15546 /* Leave the scope of the containing function. */
15547 if (function_scope)
15548 pop_function_context_from (function_scope);
15549 cp_parser_pop_lexer (parser);
15552 /* Remove any template parameters from the symbol table. */
15553 maybe_end_member_template_processing ();
15555 /* Restore the queue. */
15556 parser->unparsed_functions_queues
15557 = TREE_CHAIN (parser->unparsed_functions_queues);
15560 /* If DECL contains any default args, remember it on the unparsed
15561 functions queue. */
15564 cp_parser_save_default_args (cp_parser* parser, tree decl)
15568 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15570 probe = TREE_CHAIN (probe))
15571 if (TREE_PURPOSE (probe))
15573 TREE_PURPOSE (parser->unparsed_functions_queues)
15574 = tree_cons (current_class_type, decl,
15575 TREE_PURPOSE (parser->unparsed_functions_queues));
15581 /* FN is a FUNCTION_DECL which may contains a parameter with an
15582 unparsed DEFAULT_ARG. Parse the default args now. This function
15583 assumes that the current scope is the scope in which the default
15584 argument should be processed. */
15587 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15589 bool saved_local_variables_forbidden_p;
15592 /* While we're parsing the default args, we might (due to the
15593 statement expression extension) encounter more classes. We want
15594 to handle them right away, but we don't want them getting mixed
15595 up with default args that are currently in the queue. */
15596 parser->unparsed_functions_queues
15597 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15599 /* Local variable names (and the `this' keyword) may not appear
15600 in a default argument. */
15601 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15602 parser->local_variables_forbidden_p = true;
15604 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15606 parm = TREE_CHAIN (parm))
15608 cp_token_cache *tokens;
15609 tree default_arg = TREE_PURPOSE (parm);
15611 VEC(tree,gc) *insts;
15618 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15619 /* This can happen for a friend declaration for a function
15620 already declared with default arguments. */
15623 /* Push the saved tokens for the default argument onto the parser's
15625 tokens = DEFARG_TOKENS (default_arg);
15626 cp_parser_push_lexer_for_tokens (parser, tokens);
15628 /* Parse the assignment-expression. */
15629 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15631 TREE_PURPOSE (parm) = parsed_arg;
15633 /* Update any instantiations we've already created. */
15634 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15635 VEC_iterate (tree, insts, ix, copy); ix++)
15636 TREE_PURPOSE (copy) = parsed_arg;
15638 /* If the token stream has not been completely used up, then
15639 there was extra junk after the end of the default
15641 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15642 cp_parser_error (parser, "expected %<,%>");
15644 /* Revert to the main lexer. */
15645 cp_parser_pop_lexer (parser);
15648 /* Restore the state of local_variables_forbidden_p. */
15649 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15651 /* Restore the queue. */
15652 parser->unparsed_functions_queues
15653 = TREE_CHAIN (parser->unparsed_functions_queues);
15656 /* Parse the operand of `sizeof' (or a similar operator). Returns
15657 either a TYPE or an expression, depending on the form of the
15658 input. The KEYWORD indicates which kind of expression we have
15662 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15664 static const char *format;
15665 tree expr = NULL_TREE;
15666 const char *saved_message;
15667 bool saved_integral_constant_expression_p;
15668 bool saved_non_integral_constant_expression_p;
15670 /* Initialize FORMAT the first time we get here. */
15672 format = "types may not be defined in '%s' expressions";
15674 /* Types cannot be defined in a `sizeof' expression. Save away the
15676 saved_message = parser->type_definition_forbidden_message;
15677 /* And create the new one. */
15678 parser->type_definition_forbidden_message
15679 = xmalloc (strlen (format)
15680 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15682 sprintf ((char *) parser->type_definition_forbidden_message,
15683 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15685 /* The restrictions on constant-expressions do not apply inside
15686 sizeof expressions. */
15687 saved_integral_constant_expression_p
15688 = parser->integral_constant_expression_p;
15689 saved_non_integral_constant_expression_p
15690 = parser->non_integral_constant_expression_p;
15691 parser->integral_constant_expression_p = false;
15693 /* Do not actually evaluate the expression. */
15695 /* If it's a `(', then we might be looking at the type-id
15697 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15700 bool saved_in_type_id_in_expr_p;
15702 /* We can't be sure yet whether we're looking at a type-id or an
15704 cp_parser_parse_tentatively (parser);
15705 /* Consume the `('. */
15706 cp_lexer_consume_token (parser->lexer);
15707 /* Parse the type-id. */
15708 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15709 parser->in_type_id_in_expr_p = true;
15710 type = cp_parser_type_id (parser);
15711 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15712 /* Now, look for the trailing `)'. */
15713 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15714 /* If all went well, then we're done. */
15715 if (cp_parser_parse_definitely (parser))
15717 cp_decl_specifier_seq decl_specs;
15719 /* Build a trivial decl-specifier-seq. */
15720 clear_decl_specs (&decl_specs);
15721 decl_specs.type = type;
15723 /* Call grokdeclarator to figure out what type this is. */
15724 expr = grokdeclarator (NULL,
15728 /*attrlist=*/NULL);
15732 /* If the type-id production did not work out, then we must be
15733 looking at the unary-expression production. */
15735 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15737 /* Go back to evaluating expressions. */
15740 /* Free the message we created. */
15741 free ((char *) parser->type_definition_forbidden_message);
15742 /* And restore the old one. */
15743 parser->type_definition_forbidden_message = saved_message;
15744 parser->integral_constant_expression_p
15745 = saved_integral_constant_expression_p;
15746 parser->non_integral_constant_expression_p
15747 = saved_non_integral_constant_expression_p;
15752 /* If the current declaration has no declarator, return true. */
15755 cp_parser_declares_only_class_p (cp_parser *parser)
15757 /* If the next token is a `;' or a `,' then there is no
15759 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15760 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15763 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15766 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15767 cp_storage_class storage_class)
15769 if (decl_specs->storage_class != sc_none)
15770 decl_specs->multiple_storage_classes_p = true;
15772 decl_specs->storage_class = storage_class;
15775 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15776 is true, the type is a user-defined type; otherwise it is a
15777 built-in type specified by a keyword. */
15780 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15782 bool user_defined_p)
15784 decl_specs->any_specifiers_p = true;
15786 /* If the user tries to redeclare bool or wchar_t (with, for
15787 example, in "typedef int wchar_t;") we remember that this is what
15788 happened. In system headers, we ignore these declarations so
15789 that G++ can work with system headers that are not C++-safe. */
15790 if (decl_specs->specs[(int) ds_typedef]
15792 && (type_spec == boolean_type_node
15793 || type_spec == wchar_type_node)
15794 && (decl_specs->type
15795 || decl_specs->specs[(int) ds_long]
15796 || decl_specs->specs[(int) ds_short]
15797 || decl_specs->specs[(int) ds_unsigned]
15798 || decl_specs->specs[(int) ds_signed]))
15800 decl_specs->redefined_builtin_type = type_spec;
15801 if (!decl_specs->type)
15803 decl_specs->type = type_spec;
15804 decl_specs->user_defined_type_p = false;
15807 else if (decl_specs->type)
15808 decl_specs->multiple_types_p = true;
15811 decl_specs->type = type_spec;
15812 decl_specs->user_defined_type_p = user_defined_p;
15813 decl_specs->redefined_builtin_type = NULL_TREE;
15817 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15818 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15821 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15823 return decl_specifiers->specs[(int) ds_friend] != 0;
15826 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15827 issue an error message indicating that TOKEN_DESC was expected.
15829 Returns the token consumed, if the token had the appropriate type.
15830 Otherwise, returns NULL. */
15833 cp_parser_require (cp_parser* parser,
15834 enum cpp_ttype type,
15835 const char* token_desc)
15837 if (cp_lexer_next_token_is (parser->lexer, type))
15838 return cp_lexer_consume_token (parser->lexer);
15841 /* Output the MESSAGE -- unless we're parsing tentatively. */
15842 if (!cp_parser_simulate_error (parser))
15844 char *message = concat ("expected ", token_desc, NULL);
15845 cp_parser_error (parser, message);
15852 /* Like cp_parser_require, except that tokens will be skipped until
15853 the desired token is found. An error message is still produced if
15854 the next token is not as expected. */
15857 cp_parser_skip_until_found (cp_parser* parser,
15858 enum cpp_ttype type,
15859 const char* token_desc)
15862 unsigned nesting_depth = 0;
15864 if (cp_parser_require (parser, type, token_desc))
15867 /* Skip tokens until the desired token is found. */
15870 /* Peek at the next token. */
15871 token = cp_lexer_peek_token (parser->lexer);
15872 /* If we've reached the token we want, consume it and
15874 if (token->type == type && !nesting_depth)
15876 cp_lexer_consume_token (parser->lexer);
15879 /* If we've run out of tokens, stop. */
15880 if (token->type == CPP_EOF)
15882 if (token->type == CPP_OPEN_BRACE
15883 || token->type == CPP_OPEN_PAREN
15884 || token->type == CPP_OPEN_SQUARE)
15886 else if (token->type == CPP_CLOSE_BRACE
15887 || token->type == CPP_CLOSE_PAREN
15888 || token->type == CPP_CLOSE_SQUARE)
15890 if (nesting_depth-- == 0)
15893 /* Consume this token. */
15894 cp_lexer_consume_token (parser->lexer);
15898 /* If the next token is the indicated keyword, consume it. Otherwise,
15899 issue an error message indicating that TOKEN_DESC was expected.
15901 Returns the token consumed, if the token had the appropriate type.
15902 Otherwise, returns NULL. */
15905 cp_parser_require_keyword (cp_parser* parser,
15907 const char* token_desc)
15909 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15911 if (token && token->keyword != keyword)
15913 dyn_string_t error_msg;
15915 /* Format the error message. */
15916 error_msg = dyn_string_new (0);
15917 dyn_string_append_cstr (error_msg, "expected ");
15918 dyn_string_append_cstr (error_msg, token_desc);
15919 cp_parser_error (parser, error_msg->s);
15920 dyn_string_delete (error_msg);
15927 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15928 function-definition. */
15931 cp_parser_token_starts_function_definition_p (cp_token* token)
15933 return (/* An ordinary function-body begins with an `{'. */
15934 token->type == CPP_OPEN_BRACE
15935 /* A ctor-initializer begins with a `:'. */
15936 || token->type == CPP_COLON
15937 /* A function-try-block begins with `try'. */
15938 || token->keyword == RID_TRY
15939 /* The named return value extension begins with `return'. */
15940 || token->keyword == RID_RETURN);
15943 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15947 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15951 token = cp_lexer_peek_token (parser->lexer);
15952 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15955 /* Returns TRUE iff the next token is the "," or ">" ending a
15956 template-argument. */
15959 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15963 token = cp_lexer_peek_token (parser->lexer);
15964 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15967 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15968 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15971 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15976 token = cp_lexer_peek_nth_token (parser->lexer, n);
15977 if (token->type == CPP_LESS)
15979 /* Check for the sequence `<::' in the original code. It would be lexed as
15980 `[:', where `[' is a digraph, and there is no whitespace before
15982 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15985 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15986 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15992 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15993 or none_type otherwise. */
15995 static enum tag_types
15996 cp_parser_token_is_class_key (cp_token* token)
15998 switch (token->keyword)
16003 return record_type;
16012 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16015 cp_parser_check_class_key (enum tag_types class_key, tree type)
16017 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16018 pedwarn ("%qs tag used in naming %q#T",
16019 class_key == union_type ? "union"
16020 : class_key == record_type ? "struct" : "class",
16024 /* Issue an error message if DECL is redeclared with different
16025 access than its original declaration [class.access.spec/3].
16026 This applies to nested classes and nested class templates.
16030 cp_parser_check_access_in_redeclaration (tree decl)
16032 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16035 if ((TREE_PRIVATE (decl)
16036 != (current_access_specifier == access_private_node))
16037 || (TREE_PROTECTED (decl)
16038 != (current_access_specifier == access_protected_node)))
16039 error ("%qD redeclared with different access", decl);
16042 /* Look for the `template' keyword, as a syntactic disambiguator.
16043 Return TRUE iff it is present, in which case it will be
16047 cp_parser_optional_template_keyword (cp_parser *parser)
16049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16051 /* The `template' keyword can only be used within templates;
16052 outside templates the parser can always figure out what is a
16053 template and what is not. */
16054 if (!processing_template_decl)
16056 error ("%<template%> (as a disambiguator) is only allowed "
16057 "within templates");
16058 /* If this part of the token stream is rescanned, the same
16059 error message would be generated. So, we purge the token
16060 from the stream. */
16061 cp_lexer_purge_token (parser->lexer);
16066 /* Consume the `template' keyword. */
16067 cp_lexer_consume_token (parser->lexer);
16075 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16076 set PARSER->SCOPE, and perform other related actions. */
16079 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16084 /* Get the stored value. */
16085 value = cp_lexer_consume_token (parser->lexer)->value;
16086 /* Perform any access checks that were deferred. */
16087 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16088 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16089 /* Set the scope from the stored value. */
16090 parser->scope = TREE_VALUE (value);
16091 parser->qualifying_scope = TREE_TYPE (value);
16092 parser->object_scope = NULL_TREE;
16095 /* Consume tokens up through a non-nested END token. */
16098 cp_parser_cache_group (cp_parser *parser,
16099 enum cpp_ttype end,
16106 /* Abort a parenthesized expression if we encounter a brace. */
16107 if ((end == CPP_CLOSE_PAREN || depth == 0)
16108 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16110 /* If we've reached the end of the file, stop. */
16111 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16113 /* Consume the next token. */
16114 token = cp_lexer_consume_token (parser->lexer);
16115 /* See if it starts a new group. */
16116 if (token->type == CPP_OPEN_BRACE)
16118 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16122 else if (token->type == CPP_OPEN_PAREN)
16123 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16124 else if (token->type == end)
16129 /* Begin parsing tentatively. We always save tokens while parsing
16130 tentatively so that if the tentative parsing fails we can restore the
16134 cp_parser_parse_tentatively (cp_parser* parser)
16136 /* Enter a new parsing context. */
16137 parser->context = cp_parser_context_new (parser->context);
16138 /* Begin saving tokens. */
16139 cp_lexer_save_tokens (parser->lexer);
16140 /* In order to avoid repetitive access control error messages,
16141 access checks are queued up until we are no longer parsing
16143 push_deferring_access_checks (dk_deferred);
16146 /* Commit to the currently active tentative parse. */
16149 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16151 cp_parser_context *context;
16154 /* Mark all of the levels as committed. */
16155 lexer = parser->lexer;
16156 for (context = parser->context; context->next; context = context->next)
16158 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16160 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16161 while (!cp_lexer_saving_tokens (lexer))
16162 lexer = lexer->next;
16163 cp_lexer_commit_tokens (lexer);
16167 /* Abort the currently active tentative parse. All consumed tokens
16168 will be rolled back, and no diagnostics will be issued. */
16171 cp_parser_abort_tentative_parse (cp_parser* parser)
16173 cp_parser_simulate_error (parser);
16174 /* Now, pretend that we want to see if the construct was
16175 successfully parsed. */
16176 cp_parser_parse_definitely (parser);
16179 /* Stop parsing tentatively. If a parse error has occurred, restore the
16180 token stream. Otherwise, commit to the tokens we have consumed.
16181 Returns true if no error occurred; false otherwise. */
16184 cp_parser_parse_definitely (cp_parser* parser)
16186 bool error_occurred;
16187 cp_parser_context *context;
16189 /* Remember whether or not an error occurred, since we are about to
16190 destroy that information. */
16191 error_occurred = cp_parser_error_occurred (parser);
16192 /* Remove the topmost context from the stack. */
16193 context = parser->context;
16194 parser->context = context->next;
16195 /* If no parse errors occurred, commit to the tentative parse. */
16196 if (!error_occurred)
16198 /* Commit to the tokens read tentatively, unless that was
16200 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16201 cp_lexer_commit_tokens (parser->lexer);
16203 pop_to_parent_deferring_access_checks ();
16205 /* Otherwise, if errors occurred, roll back our state so that things
16206 are just as they were before we began the tentative parse. */
16209 cp_lexer_rollback_tokens (parser->lexer);
16210 pop_deferring_access_checks ();
16212 /* Add the context to the front of the free list. */
16213 context->next = cp_parser_context_free_list;
16214 cp_parser_context_free_list = context;
16216 return !error_occurred;
16219 /* Returns true if we are parsing tentatively and are not committed to
16220 this tentative parse. */
16223 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16225 return (cp_parser_parsing_tentatively (parser)
16226 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16229 /* Returns nonzero iff an error has occurred during the most recent
16230 tentative parse. */
16233 cp_parser_error_occurred (cp_parser* parser)
16235 return (cp_parser_parsing_tentatively (parser)
16236 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16239 /* Returns nonzero if GNU extensions are allowed. */
16242 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16244 return parser->allow_gnu_extensions_p;
16247 /* Objective-C++ Productions */
16250 /* Parse an Objective-C expression, which feeds into a primary-expression
16254 objc-message-expression
16255 objc-string-literal
16256 objc-encode-expression
16257 objc-protocol-expression
16258 objc-selector-expression
16260 Returns a tree representation of the expression. */
16263 cp_parser_objc_expression (cp_parser* parser)
16265 /* Try to figure out what kind of declaration is present. */
16266 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16270 case CPP_OPEN_SQUARE:
16271 return cp_parser_objc_message_expression (parser);
16273 case CPP_OBJC_STRING:
16274 kwd = cp_lexer_consume_token (parser->lexer);
16275 return objc_build_string_object (kwd->value);
16278 switch (kwd->keyword)
16280 case RID_AT_ENCODE:
16281 return cp_parser_objc_encode_expression (parser);
16283 case RID_AT_PROTOCOL:
16284 return cp_parser_objc_protocol_expression (parser);
16286 case RID_AT_SELECTOR:
16287 return cp_parser_objc_selector_expression (parser);
16293 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16294 cp_parser_skip_to_end_of_block_or_statement (parser);
16297 return error_mark_node;
16300 /* Parse an Objective-C message expression.
16302 objc-message-expression:
16303 [ objc-message-receiver objc-message-args ]
16305 Returns a representation of an Objective-C message. */
16308 cp_parser_objc_message_expression (cp_parser* parser)
16310 tree receiver, messageargs;
16312 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16313 receiver = cp_parser_objc_message_receiver (parser);
16314 messageargs = cp_parser_objc_message_args (parser);
16315 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16317 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16320 /* Parse an objc-message-receiver.
16322 objc-message-receiver:
16324 simple-type-specifier
16326 Returns a representation of the type or expression. */
16329 cp_parser_objc_message_receiver (cp_parser* parser)
16333 /* An Objective-C message receiver may be either (1) a type
16334 or (2) an expression. */
16335 cp_parser_parse_tentatively (parser);
16336 rcv = cp_parser_expression (parser, false);
16338 if (cp_parser_parse_definitely (parser))
16341 rcv = cp_parser_simple_type_specifier (parser,
16342 /*decl_specs=*/NULL,
16343 CP_PARSER_FLAGS_NONE);
16345 return objc_get_class_reference (rcv);
16348 /* Parse the arguments and selectors comprising an Objective-C message.
16353 objc-selector-args , objc-comma-args
16355 objc-selector-args:
16356 objc-selector [opt] : assignment-expression
16357 objc-selector-args objc-selector [opt] : assignment-expression
16360 assignment-expression
16361 objc-comma-args , assignment-expression
16363 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16364 selector arguments and TREE_VALUE containing a list of comma
16368 cp_parser_objc_message_args (cp_parser* parser)
16370 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16371 bool maybe_unary_selector_p = true;
16372 cp_token *token = cp_lexer_peek_token (parser->lexer);
16374 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16376 tree selector = NULL_TREE, arg;
16378 if (token->type != CPP_COLON)
16379 selector = cp_parser_objc_selector (parser);
16381 /* Detect if we have a unary selector. */
16382 if (maybe_unary_selector_p
16383 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16384 return build_tree_list (selector, NULL_TREE);
16386 maybe_unary_selector_p = false;
16387 cp_parser_require (parser, CPP_COLON, "`:'");
16388 arg = cp_parser_assignment_expression (parser, false);
16391 = chainon (sel_args,
16392 build_tree_list (selector, arg));
16394 token = cp_lexer_peek_token (parser->lexer);
16397 /* Handle non-selector arguments, if any. */
16398 while (token->type == CPP_COMMA)
16402 cp_lexer_consume_token (parser->lexer);
16403 arg = cp_parser_assignment_expression (parser, false);
16406 = chainon (addl_args,
16407 build_tree_list (NULL_TREE, arg));
16409 token = cp_lexer_peek_token (parser->lexer);
16412 return build_tree_list (sel_args, addl_args);
16415 /* Parse an Objective-C encode expression.
16417 objc-encode-expression:
16418 @encode objc-typename
16420 Returns an encoded representation of the type argument. */
16423 cp_parser_objc_encode_expression (cp_parser* parser)
16427 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16428 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16429 type = complete_type (cp_parser_type_id (parser));
16430 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16434 error ("%<@encode%> must specify a type as an argument");
16435 return error_mark_node;
16438 return objc_build_encode_expr (type);
16441 /* Parse an Objective-C @defs expression. */
16444 cp_parser_objc_defs_expression (cp_parser *parser)
16448 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16449 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16450 name = cp_parser_identifier (parser);
16451 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16453 return objc_get_class_ivars (name);
16456 /* Parse an Objective-C protocol expression.
16458 objc-protocol-expression:
16459 @protocol ( identifier )
16461 Returns a representation of the protocol expression. */
16464 cp_parser_objc_protocol_expression (cp_parser* parser)
16468 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16469 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16470 proto = cp_parser_identifier (parser);
16471 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16473 return objc_build_protocol_expr (proto);
16476 /* Parse an Objective-C selector expression.
16478 objc-selector-expression:
16479 @selector ( objc-method-signature )
16481 objc-method-signature:
16487 objc-selector-seq objc-selector :
16489 Returns a representation of the method selector. */
16492 cp_parser_objc_selector_expression (cp_parser* parser)
16494 tree sel_seq = NULL_TREE;
16495 bool maybe_unary_selector_p = true;
16498 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16499 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16500 token = cp_lexer_peek_token (parser->lexer);
16502 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16504 tree selector = NULL_TREE;
16506 if (token->type != CPP_COLON)
16507 selector = cp_parser_objc_selector (parser);
16509 /* Detect if we have a unary selector. */
16510 if (maybe_unary_selector_p
16511 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16513 sel_seq = selector;
16514 goto finish_selector;
16517 maybe_unary_selector_p = false;
16518 cp_parser_require (parser, CPP_COLON, "`:'");
16521 = chainon (sel_seq,
16522 build_tree_list (selector, NULL_TREE));
16524 token = cp_lexer_peek_token (parser->lexer);
16528 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16530 return objc_build_selector_expr (sel_seq);
16533 /* Parse a list of identifiers.
16535 objc-identifier-list:
16537 objc-identifier-list , identifier
16539 Returns a TREE_LIST of identifier nodes. */
16542 cp_parser_objc_identifier_list (cp_parser* parser)
16544 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16545 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16547 while (sep->type == CPP_COMMA)
16549 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16550 list = chainon (list,
16551 build_tree_list (NULL_TREE,
16552 cp_parser_identifier (parser)));
16553 sep = cp_lexer_peek_token (parser->lexer);
16559 /* Parse an Objective-C alias declaration.
16561 objc-alias-declaration:
16562 @compatibility_alias identifier identifier ;
16564 This function registers the alias mapping with the Objective-C front-end.
16565 It returns nothing. */
16568 cp_parser_objc_alias_declaration (cp_parser* parser)
16572 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16573 alias = cp_parser_identifier (parser);
16574 orig = cp_parser_identifier (parser);
16575 objc_declare_alias (alias, orig);
16576 cp_parser_consume_semicolon_at_end_of_statement (parser);
16579 /* Parse an Objective-C class forward-declaration.
16581 objc-class-declaration:
16582 @class objc-identifier-list ;
16584 The function registers the forward declarations with the Objective-C
16585 front-end. It returns nothing. */
16588 cp_parser_objc_class_declaration (cp_parser* parser)
16590 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16591 objc_declare_class (cp_parser_objc_identifier_list (parser));
16592 cp_parser_consume_semicolon_at_end_of_statement (parser);
16595 /* Parse a list of Objective-C protocol references.
16597 objc-protocol-refs-opt:
16598 objc-protocol-refs [opt]
16600 objc-protocol-refs:
16601 < objc-identifier-list >
16603 Returns a TREE_LIST of identifiers, if any. */
16606 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16608 tree protorefs = NULL_TREE;
16610 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16612 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16613 protorefs = cp_parser_objc_identifier_list (parser);
16614 cp_parser_require (parser, CPP_GREATER, "`>'");
16620 /* Parse a Objective-C visibility specification. */
16623 cp_parser_objc_visibility_spec (cp_parser* parser)
16625 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16627 switch (vis->keyword)
16629 case RID_AT_PRIVATE:
16630 objc_set_visibility (2);
16632 case RID_AT_PROTECTED:
16633 objc_set_visibility (0);
16635 case RID_AT_PUBLIC:
16636 objc_set_visibility (1);
16642 /* Eat '@private'/'@protected'/'@public'. */
16643 cp_lexer_consume_token (parser->lexer);
16646 /* Parse an Objective-C method type. */
16649 cp_parser_objc_method_type (cp_parser* parser)
16651 objc_set_method_type
16652 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16657 /* Parse an Objective-C protocol qualifier. */
16660 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16662 tree quals = NULL_TREE, node;
16663 cp_token *token = cp_lexer_peek_token (parser->lexer);
16665 node = token->value;
16667 while (node && TREE_CODE (node) == IDENTIFIER_NODE
16668 && (node == ridpointers [(int) RID_IN]
16669 || node == ridpointers [(int) RID_OUT]
16670 || node == ridpointers [(int) RID_INOUT]
16671 || node == ridpointers [(int) RID_BYCOPY]
16672 || node == ridpointers [(int) RID_BYREF]
16673 || node == ridpointers [(int) RID_ONEWAY]))
16675 quals = tree_cons (NULL_TREE, node, quals);
16676 cp_lexer_consume_token (parser->lexer);
16677 token = cp_lexer_peek_token (parser->lexer);
16678 node = token->value;
16684 /* Parse an Objective-C typename. */
16687 cp_parser_objc_typename (cp_parser* parser)
16689 tree typename = NULL_TREE;
16691 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16693 tree proto_quals, cp_type = NULL_TREE;
16695 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
16696 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16698 /* An ObjC type name may consist of just protocol qualifiers, in which
16699 case the type shall default to 'id'. */
16700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16701 cp_type = cp_parser_type_id (parser);
16703 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16704 typename = build_tree_list (proto_quals, cp_type);
16710 /* Check to see if TYPE refers to an Objective-C selector name. */
16713 cp_parser_objc_selector_p (enum cpp_ttype type)
16715 return (type == CPP_NAME || type == CPP_KEYWORD
16716 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16717 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16718 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16719 || type == CPP_XOR || type == CPP_XOR_EQ);
16722 /* Parse an Objective-C selector. */
16725 cp_parser_objc_selector (cp_parser* parser)
16727 cp_token *token = cp_lexer_consume_token (parser->lexer);
16729 if (!cp_parser_objc_selector_p (token->type))
16731 error ("invalid Objective-C++ selector name");
16732 return error_mark_node;
16735 /* C++ operator names are allowed to appear in ObjC selectors. */
16736 switch (token->type)
16738 case CPP_AND_AND: return get_identifier ("and");
16739 case CPP_AND_EQ: return get_identifier ("and_eq");
16740 case CPP_AND: return get_identifier ("bitand");
16741 case CPP_OR: return get_identifier ("bitor");
16742 case CPP_COMPL: return get_identifier ("compl");
16743 case CPP_NOT: return get_identifier ("not");
16744 case CPP_NOT_EQ: return get_identifier ("not_eq");
16745 case CPP_OR_OR: return get_identifier ("or");
16746 case CPP_OR_EQ: return get_identifier ("or_eq");
16747 case CPP_XOR: return get_identifier ("xor");
16748 case CPP_XOR_EQ: return get_identifier ("xor_eq");
16749 default: return token->value;
16753 /* Parse an Objective-C params list. */
16756 cp_parser_objc_method_keyword_params (cp_parser* parser)
16758 tree params = NULL_TREE;
16759 bool maybe_unary_selector_p = true;
16760 cp_token *token = cp_lexer_peek_token (parser->lexer);
16762 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16764 tree selector = NULL_TREE, typename, identifier;
16766 if (token->type != CPP_COLON)
16767 selector = cp_parser_objc_selector (parser);
16769 /* Detect if we have a unary selector. */
16770 if (maybe_unary_selector_p
16771 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16774 maybe_unary_selector_p = false;
16775 cp_parser_require (parser, CPP_COLON, "`:'");
16776 typename = cp_parser_objc_typename (parser);
16777 identifier = cp_parser_identifier (parser);
16781 objc_build_keyword_decl (selector,
16785 token = cp_lexer_peek_token (parser->lexer);
16791 /* Parse the non-keyword Objective-C params. */
16794 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
16796 tree params = make_node (TREE_LIST);
16797 cp_token *token = cp_lexer_peek_token (parser->lexer);
16798 *ellipsisp = false; /* Initially, assume no ellipsis. */
16800 while (token->type == CPP_COMMA)
16802 cp_parameter_declarator *parmdecl;
16805 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16806 token = cp_lexer_peek_token (parser->lexer);
16808 if (token->type == CPP_ELLIPSIS)
16810 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
16815 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
16816 parm = grokdeclarator (parmdecl->declarator,
16817 &parmdecl->decl_specifiers,
16818 PARM, /*initialized=*/0,
16819 /*attrlist=*/NULL);
16821 chainon (params, build_tree_list (NULL_TREE, parm));
16822 token = cp_lexer_peek_token (parser->lexer);
16828 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
16831 cp_parser_objc_interstitial_code (cp_parser* parser)
16833 cp_token *token = cp_lexer_peek_token (parser->lexer);
16835 /* If the next token is `extern' and the following token is a string
16836 literal, then we have a linkage specification. */
16837 if (token->keyword == RID_EXTERN
16838 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
16839 cp_parser_linkage_specification (parser);
16840 /* Handle #pragma, if any. */
16841 else if (token->type == CPP_PRAGMA)
16842 cp_lexer_handle_pragma (parser->lexer);
16843 /* Allow stray semicolons. */
16844 else if (token->type == CPP_SEMICOLON)
16845 cp_lexer_consume_token (parser->lexer);
16846 /* Finally, try to parse a block-declaration, or a function-definition. */
16848 cp_parser_block_declaration (parser, /*statement_p=*/false);
16851 /* Parse a method signature. */
16854 cp_parser_objc_method_signature (cp_parser* parser)
16856 tree rettype, kwdparms, optparms;
16857 bool ellipsis = false;
16859 cp_parser_objc_method_type (parser);
16860 rettype = cp_parser_objc_typename (parser);
16861 kwdparms = cp_parser_objc_method_keyword_params (parser);
16862 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
16864 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
16867 /* Pars an Objective-C method prototype list. */
16870 cp_parser_objc_method_prototype_list (cp_parser* parser)
16872 cp_token *token = cp_lexer_peek_token (parser->lexer);
16874 while (token->keyword != RID_AT_END)
16876 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16878 objc_add_method_declaration
16879 (cp_parser_objc_method_signature (parser));
16880 cp_parser_consume_semicolon_at_end_of_statement (parser);
16883 /* Allow for interspersed non-ObjC++ code. */
16884 cp_parser_objc_interstitial_code (parser);
16886 token = cp_lexer_peek_token (parser->lexer);
16889 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
16890 objc_finish_interface ();
16893 /* Parse an Objective-C method definition list. */
16896 cp_parser_objc_method_definition_list (cp_parser* parser)
16898 cp_token *token = cp_lexer_peek_token (parser->lexer);
16900 while (token->keyword != RID_AT_END)
16904 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16906 push_deferring_access_checks (dk_deferred);
16907 objc_start_method_definition
16908 (cp_parser_objc_method_signature (parser));
16910 /* For historical reasons, we accept an optional semicolon. */
16911 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16912 cp_lexer_consume_token (parser->lexer);
16914 perform_deferred_access_checks ();
16915 stop_deferring_access_checks ();
16916 meth = cp_parser_function_definition_after_declarator (parser,
16918 pop_deferring_access_checks ();
16919 objc_finish_method_definition (meth);
16922 /* Allow for interspersed non-ObjC++ code. */
16923 cp_parser_objc_interstitial_code (parser);
16925 token = cp_lexer_peek_token (parser->lexer);
16928 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
16929 objc_finish_implementation ();
16932 /* Parse Objective-C ivars. */
16935 cp_parser_objc_class_ivars (cp_parser* parser)
16937 cp_token *token = cp_lexer_peek_token (parser->lexer);
16939 if (token->type != CPP_OPEN_BRACE)
16940 return; /* No ivars specified. */
16942 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
16943 token = cp_lexer_peek_token (parser->lexer);
16945 while (token->type != CPP_CLOSE_BRACE)
16947 cp_decl_specifier_seq declspecs;
16948 int decl_class_or_enum_p;
16949 tree prefix_attributes;
16951 cp_parser_objc_visibility_spec (parser);
16953 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16956 cp_parser_decl_specifier_seq (parser,
16957 CP_PARSER_FLAGS_OPTIONAL,
16959 &decl_class_or_enum_p);
16960 prefix_attributes = declspecs.attributes;
16961 declspecs.attributes = NULL_TREE;
16963 /* Keep going until we hit the `;' at the end of the
16965 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16967 tree width = NULL_TREE, attributes, first_attribute, decl;
16968 cp_declarator *declarator = NULL;
16969 int ctor_dtor_or_conv_p;
16971 /* Check for a (possibly unnamed) bitfield declaration. */
16972 token = cp_lexer_peek_token (parser->lexer);
16973 if (token->type == CPP_COLON)
16976 if (token->type == CPP_NAME
16977 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16980 /* Get the name of the bitfield. */
16981 declarator = make_id_declarator (NULL_TREE,
16982 cp_parser_identifier (parser));
16985 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
16986 /* Get the width of the bitfield. */
16988 = cp_parser_constant_expression (parser,
16989 /*allow_non_constant=*/false,
16994 /* Parse the declarator. */
16996 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16997 &ctor_dtor_or_conv_p,
16998 /*parenthesized_p=*/NULL,
16999 /*member_p=*/false);
17002 /* Look for attributes that apply to the ivar. */
17003 attributes = cp_parser_attributes_opt (parser);
17004 /* Remember which attributes are prefix attributes and
17006 first_attribute = attributes;
17007 /* Combine the attributes. */
17008 attributes = chainon (prefix_attributes, attributes);
17012 /* Create the bitfield declaration. */
17013 decl = grokbitfield (declarator, &declspecs, width);
17014 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17017 decl = grokfield (declarator, &declspecs, NULL_TREE,
17018 NULL_TREE, attributes);
17020 /* Add the instance variable. */
17021 objc_add_instance_variable (decl);
17023 /* Reset PREFIX_ATTRIBUTES. */
17024 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17025 attributes = TREE_CHAIN (attributes);
17027 TREE_CHAIN (attributes) = NULL_TREE;
17029 token = cp_lexer_peek_token (parser->lexer);
17031 if (token->type == CPP_COMMA)
17033 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17039 cp_parser_consume_semicolon_at_end_of_statement (parser);
17040 token = cp_lexer_peek_token (parser->lexer);
17043 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17044 /* For historical reasons, we accept an optional semicolon. */
17045 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17046 cp_lexer_consume_token (parser->lexer);
17049 /* Parse an Objective-C protocol declaration. */
17052 cp_parser_objc_protocol_declaration (cp_parser* parser)
17054 tree proto, protorefs;
17057 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17060 error ("identifier expected after %<@protocol%>");
17064 /* See if we have a forward declaration or a definition. */
17065 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17067 /* Try a forward declaration first. */
17068 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17070 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17072 cp_parser_consume_semicolon_at_end_of_statement (parser);
17075 /* Ok, we got a full-fledged definition (or at least should). */
17078 proto = cp_parser_identifier (parser);
17079 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17080 objc_start_protocol (proto, protorefs);
17081 cp_parser_objc_method_prototype_list (parser);
17085 /* Parse an Objective-C superclass or category. */
17088 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17091 cp_token *next = cp_lexer_peek_token (parser->lexer);
17093 *super = *categ = NULL_TREE;
17094 if (next->type == CPP_COLON)
17096 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17097 *super = cp_parser_identifier (parser);
17099 else if (next->type == CPP_OPEN_PAREN)
17101 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17102 *categ = cp_parser_identifier (parser);
17103 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17107 /* Parse an Objective-C class interface. */
17110 cp_parser_objc_class_interface (cp_parser* parser)
17112 tree name, super, categ, protos;
17114 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17115 name = cp_parser_identifier (parser);
17116 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17117 protos = cp_parser_objc_protocol_refs_opt (parser);
17119 /* We have either a class or a category on our hands. */
17121 objc_start_category_interface (name, categ, protos);
17124 objc_start_class_interface (name, super, protos);
17125 /* Handle instance variable declarations, if any. */
17126 cp_parser_objc_class_ivars (parser);
17127 objc_continue_interface ();
17130 cp_parser_objc_method_prototype_list (parser);
17133 /* Parse an Objective-C class implementation. */
17136 cp_parser_objc_class_implementation (cp_parser* parser)
17138 tree name, super, categ;
17140 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17141 name = cp_parser_identifier (parser);
17142 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17144 /* We have either a class or a category on our hands. */
17146 objc_start_category_implementation (name, categ);
17149 objc_start_class_implementation (name, super);
17150 /* Handle instance variable declarations, if any. */
17151 cp_parser_objc_class_ivars (parser);
17152 objc_continue_implementation ();
17155 cp_parser_objc_method_definition_list (parser);
17158 /* Consume the @end token and finish off the implementation. */
17161 cp_parser_objc_end_implementation (cp_parser* parser)
17163 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17164 objc_finish_implementation ();
17167 /* Parse an Objective-C declaration. */
17170 cp_parser_objc_declaration (cp_parser* parser)
17172 /* Try to figure out what kind of declaration is present. */
17173 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17175 switch (kwd->keyword)
17178 cp_parser_objc_alias_declaration (parser);
17181 cp_parser_objc_class_declaration (parser);
17183 case RID_AT_PROTOCOL:
17184 cp_parser_objc_protocol_declaration (parser);
17186 case RID_AT_INTERFACE:
17187 cp_parser_objc_class_interface (parser);
17189 case RID_AT_IMPLEMENTATION:
17190 cp_parser_objc_class_implementation (parser);
17193 cp_parser_objc_end_implementation (parser);
17196 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17197 cp_parser_skip_to_end_of_block_or_statement (parser);
17201 /* Parse an Objective-C try-catch-finally statement.
17203 objc-try-catch-finally-stmt:
17204 @try compound-statement objc-catch-clause-seq [opt]
17205 objc-finally-clause [opt]
17207 objc-catch-clause-seq:
17208 objc-catch-clause objc-catch-clause-seq [opt]
17211 @catch ( exception-declaration ) compound-statement
17213 objc-finally-clause
17214 @finally compound-statement
17216 Returns NULL_TREE. */
17219 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17220 location_t location;
17223 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17224 location = cp_lexer_peek_token (parser->lexer)->location;
17225 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17226 node, lest it get absorbed into the surrounding block. */
17227 stmt = push_stmt_list ();
17228 cp_parser_compound_statement (parser, NULL, false);
17229 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17231 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17233 cp_parameter_declarator *parmdecl;
17236 cp_lexer_consume_token (parser->lexer);
17237 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17238 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17239 parm = grokdeclarator (parmdecl->declarator,
17240 &parmdecl->decl_specifiers,
17241 PARM, /*initialized=*/0,
17242 /*attrlist=*/NULL);
17243 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17244 objc_begin_catch_clause (parm);
17245 cp_parser_compound_statement (parser, NULL, false);
17246 objc_finish_catch_clause ();
17249 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17251 cp_lexer_consume_token (parser->lexer);
17252 location = cp_lexer_peek_token (parser->lexer)->location;
17253 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17254 node, lest it get absorbed into the surrounding block. */
17255 stmt = push_stmt_list ();
17256 cp_parser_compound_statement (parser, NULL, false);
17257 objc_build_finally_clause (location, pop_stmt_list (stmt));
17260 return objc_finish_try_stmt ();
17263 /* Parse an Objective-C synchronized statement.
17265 objc-synchronized-stmt:
17266 @synchronized ( expression ) compound-statement
17268 Returns NULL_TREE. */
17271 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17272 location_t location;
17275 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17277 location = cp_lexer_peek_token (parser->lexer)->location;
17278 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17279 lock = cp_parser_expression (parser, false);
17280 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17282 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17283 node, lest it get absorbed into the surrounding block. */
17284 stmt = push_stmt_list ();
17285 cp_parser_compound_statement (parser, NULL, false);
17287 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17290 /* Parse an Objective-C throw statement.
17293 @throw assignment-expression [opt] ;
17295 Returns a constructed '@throw' statement. */
17298 cp_parser_objc_throw_statement (cp_parser *parser) {
17299 tree expr = NULL_TREE;
17301 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17304 expr = cp_parser_assignment_expression (parser, false);
17306 cp_parser_consume_semicolon_at_end_of_statement (parser);
17308 return objc_build_throw_stmt (expr);
17311 /* Parse an Objective-C statement. */
17314 cp_parser_objc_statement (cp_parser * parser) {
17315 /* Try to figure out what kind of declaration is present. */
17316 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17318 switch (kwd->keyword)
17321 return cp_parser_objc_try_catch_finally_statement (parser);
17322 case RID_AT_SYNCHRONIZED:
17323 return cp_parser_objc_synchronized_statement (parser);
17325 return cp_parser_objc_throw_statement (parser);
17327 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17328 cp_parser_skip_to_end_of_block_or_statement (parser);
17331 return error_mark_node;
17336 static GTY (()) cp_parser *the_parser;
17338 /* External interface. */
17340 /* Parse one entire translation unit. */
17343 c_parse_file (void)
17345 bool error_occurred;
17346 static bool already_called = false;
17348 if (already_called)
17350 sorry ("inter-module optimizations not implemented for C++");
17353 already_called = true;
17355 the_parser = cp_parser_new ();
17356 push_deferring_access_checks (flag_access_control
17357 ? dk_no_deferred : dk_no_check);
17358 error_occurred = cp_parser_translation_unit (the_parser);
17362 /* This variable must be provided by every front end. */
17366 #include "gt-cp-parser.h"