2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
42 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43 and c-lex.c) and the C++ parser. */
47 typedef struct cp_token GTY (())
49 /* The kind of token. */
50 ENUM_BITFIELD (cpp_ttype) type : 8;
51 /* If this token is a keyword, this value indicates which keyword.
52 Otherwise, this value is RID_MAX. */
53 ENUM_BITFIELD (rid) keyword : 8;
56 /* True if this token is from a system header. */
57 BOOL_BITFIELD in_system_header : 1;
58 /* True if this token is from a context where it is implicitly extern "C" */
59 BOOL_BITFIELD implicit_extern_c : 1;
60 /* The value associated with this token, if any. */
62 /* The location at which this token was found. */
66 /* We use a stack of token pointer for saving token sets. */
67 typedef struct cp_token *cp_token_position;
68 DEF_VEC_MALLOC_P (cp_token_position);
70 static const cp_token eof_token =
72 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
73 #if USE_MAPPED_LOCATION
80 /* The cp_lexer structure represents the C++ lexer. It is responsible
81 for managing the token stream from the preprocessor and supplying
82 it to the parser. Tokens are never added to the cp_lexer after
85 typedef struct cp_lexer GTY (())
87 /* The memory allocated for the buffer. NULL if this lexer does not
88 own the token buffer. */
89 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
90 /* If the lexer owns the buffer, this is the number of tokens in the
94 /* A pointer just past the last available token. The tokens
95 in this lexer are [buffer, last_token). */
96 cp_token_position GTY ((skip)) last_token;
98 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
99 no more available tokens. */
100 cp_token_position GTY ((skip)) next_token;
102 /* A stack indicating positions at which cp_lexer_save_tokens was
103 called. The top entry is the most recent position at which we
104 began saving tokens. If the stack is non-empty, we are saving
106 VEC (cp_token_position) *GTY ((skip)) saved_tokens;
108 /* True if we should output debugging information. */
111 /* The next lexer in a linked list of lexers. */
112 struct cp_lexer *next;
115 /* cp_token_cache is a range of tokens. There is no need to represent
116 allocate heap memory for it, since tokens are never removed from the
117 lexer's array. There is also no need for the GC to walk through
118 a cp_token_cache, since everything in here is referenced through
121 typedef struct cp_token_cache GTY(())
123 /* The beginning of the token range. */
124 cp_token * GTY((skip)) first;
126 /* Points immediately after the last token in the range. */
127 cp_token * GTY ((skip)) last;
132 static cp_lexer *cp_lexer_new_main
134 static cp_lexer *cp_lexer_new_from_tokens
135 (cp_token_cache *tokens);
136 static void cp_lexer_destroy
138 static int cp_lexer_saving_tokens
140 static cp_token_position cp_lexer_token_position
142 static cp_token *cp_lexer_token_at
143 (cp_lexer *, cp_token_position);
144 static void cp_lexer_get_preprocessor_token
145 (cp_lexer *, cp_token *);
146 static inline cp_token *cp_lexer_peek_token
148 static cp_token *cp_lexer_peek_nth_token
149 (cp_lexer *, size_t);
150 static inline bool cp_lexer_next_token_is
151 (cp_lexer *, enum cpp_ttype);
152 static bool cp_lexer_next_token_is_not
153 (cp_lexer *, enum cpp_ttype);
154 static bool cp_lexer_next_token_is_keyword
155 (cp_lexer *, enum rid);
156 static cp_token *cp_lexer_consume_token
158 static void cp_lexer_purge_token
160 static void cp_lexer_purge_tokens_after
161 (cp_lexer *, cp_token_position);
162 static void cp_lexer_handle_pragma
164 static void cp_lexer_save_tokens
166 static void cp_lexer_commit_tokens
168 static void cp_lexer_rollback_tokens
170 #ifdef ENABLE_CHECKING
171 static void cp_lexer_print_token
172 (FILE *, cp_token *);
173 static inline bool cp_lexer_debugging_p
175 static void cp_lexer_start_debugging
176 (cp_lexer *) ATTRIBUTE_UNUSED;
177 static void cp_lexer_stop_debugging
178 (cp_lexer *) ATTRIBUTE_UNUSED;
180 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
181 about passing NULL to functions that require non-NULL arguments
182 (fputs, fprintf). It will never be used, so all we need is a value
183 of the right type that's guaranteed not to be NULL. */
184 #define cp_lexer_debug_stream stdout
185 #define cp_lexer_print_token(str, tok) (void) 0
186 #define cp_lexer_debugging_p(lexer) 0
187 #endif /* ENABLE_CHECKING */
189 static cp_token_cache *cp_token_cache_new
190 (cp_token *, cp_token *);
192 /* Manifest constants. */
193 #define CP_LEXER_BUFFER_SIZE 10000
194 #define CP_SAVED_TOKEN_STACK 5
196 /* A token type for keywords, as opposed to ordinary identifiers. */
197 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
199 /* A token type for template-ids. If a template-id is processed while
200 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
201 the value of the CPP_TEMPLATE_ID is whatever was returned by
202 cp_parser_template_id. */
203 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
205 /* A token type for nested-name-specifiers. If a
206 nested-name-specifier is processed while parsing tentatively, it is
207 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
208 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
209 cp_parser_nested_name_specifier_opt. */
210 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
212 /* A token type for tokens that are not tokens at all; these are used
213 to represent slots in the array where there used to be a token
214 that has now been deleted. */
215 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
217 /* The number of token types, including C++-specific ones. */
218 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
222 #ifdef ENABLE_CHECKING
223 /* The stream to which debugging output should be written. */
224 static FILE *cp_lexer_debug_stream;
225 #endif /* ENABLE_CHECKING */
227 /* Create a new main C++ lexer, the lexer that gets tokens from the
231 cp_lexer_new_main (void)
233 cp_token first_token;
240 /* Tell cpplib we want CPP_PRAGMA tokens. */
241 cpp_get_options (parse_in)->defer_pragmas = true;
243 /* Tell c_lex not to merge string constants. */
244 c_lex_return_raw_strings = true;
246 /* It's possible that lexing the first token will load a PCH file,
247 which is a GC collection point. So we have to grab the first
248 token before allocating any memory. */
249 cp_lexer_get_preprocessor_token (NULL, &first_token);
250 c_common_no_more_pch ();
252 /* Allocate the memory. */
253 lexer = GGC_CNEW (cp_lexer);
255 #ifdef ENABLE_CHECKING
256 /* Initially we are not debugging. */
257 lexer->debugging_p = false;
258 #endif /* ENABLE_CHECKING */
259 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
261 /* Create the buffer. */
262 alloc = CP_LEXER_BUFFER_SIZE;
263 buffer = ggc_alloc (alloc * sizeof (cp_token));
265 /* Put the first token in the buffer. */
270 /* Get the remaining tokens from the preprocessor. */
271 while (pos->type != CPP_EOF)
278 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
279 pos = buffer + space;
281 cp_lexer_get_preprocessor_token (lexer, pos);
283 lexer->buffer = buffer;
284 lexer->buffer_length = alloc - space;
285 lexer->last_token = pos;
286 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
288 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
289 direct calls to c_lex. Those callers all expect c_lex to do
290 string constant concatenation. */
291 c_lex_return_raw_strings = false;
293 gcc_assert (lexer->next_token->type != CPP_PURGED);
297 /* Create a new lexer whose token stream is primed with the tokens in
298 CACHE. When these tokens are exhausted, no new tokens will be read. */
301 cp_lexer_new_from_tokens (cp_token_cache *cache)
303 cp_token *first = cache->first;
304 cp_token *last = cache->last;
305 cp_lexer *lexer = GGC_CNEW (cp_lexer);
307 /* We do not own the buffer. */
308 lexer->buffer = NULL;
309 lexer->buffer_length = 0;
310 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
311 lexer->last_token = last;
313 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
315 #ifdef ENABLE_CHECKING
316 /* Initially we are not debugging. */
317 lexer->debugging_p = false;
320 gcc_assert (lexer->next_token->type != CPP_PURGED);
324 /* Frees all resources associated with LEXER. */
327 cp_lexer_destroy (cp_lexer *lexer)
330 ggc_free (lexer->buffer);
331 VEC_free (cp_token_position, lexer->saved_tokens);
335 /* Returns nonzero if debugging information should be output. */
337 #ifdef ENABLE_CHECKING
340 cp_lexer_debugging_p (cp_lexer *lexer)
342 return lexer->debugging_p;
345 #endif /* ENABLE_CHECKING */
347 static inline cp_token_position
348 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
350 gcc_assert (!previous_p || lexer->next_token != &eof_token);
352 return lexer->next_token - previous_p;
355 static inline cp_token *
356 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
361 /* nonzero if we are presently saving tokens. */
364 cp_lexer_saving_tokens (const cp_lexer* lexer)
366 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
369 /* Store the next token from the preprocessor in *TOKEN. Return true
373 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
376 static int is_extern_c = 0;
380 /* Keep going until we get a token we like. */
383 /* Get a new token from the preprocessor. */
384 token->type = c_lex_with_flags (&token->value, &token->flags);
385 /* Issue messages about tokens we cannot process. */
391 error ("invalid token");
395 /* This is a good token, so we exit the loop. */
400 /* Now we've got our token. */
401 token->location = input_location;
402 token->in_system_header = in_system_header;
404 /* On some systems, some header files are surrounded by an
405 implicit extern "C" block. Set a flag in the token if it
406 comes from such a header. */
407 is_extern_c += pending_lang_change;
408 pending_lang_change = 0;
409 token->implicit_extern_c = is_extern_c > 0;
411 /* Check to see if this token is a keyword. */
412 if (token->type == CPP_NAME
413 && C_IS_RESERVED_WORD (token->value))
415 /* Mark this token as a keyword. */
416 token->type = CPP_KEYWORD;
417 /* Record which keyword. */
418 token->keyword = C_RID_CODE (token->value);
419 /* Update the value. Some keywords are mapped to particular
420 entities, rather than simply having the value of the
421 corresponding IDENTIFIER_NODE. For example, `__const' is
422 mapped to `const'. */
423 token->value = ridpointers[token->keyword];
426 token->keyword = RID_MAX;
429 /* Update the globals input_location and in_system_header from TOKEN. */
431 cp_lexer_set_source_position_from_token (cp_token *token)
433 if (token->type != CPP_EOF)
435 input_location = token->location;
436 in_system_header = token->in_system_header;
440 /* Return a pointer to the next token in the token stream, but do not
443 static inline cp_token *
444 cp_lexer_peek_token (cp_lexer *lexer)
446 if (cp_lexer_debugging_p (lexer))
448 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
449 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
450 putc ('\n', cp_lexer_debug_stream);
452 return lexer->next_token;
455 /* Return true if the next token has the indicated TYPE. */
458 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
460 return cp_lexer_peek_token (lexer)->type == type;
463 /* Return true if the next token does not have the indicated TYPE. */
466 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
468 return !cp_lexer_next_token_is (lexer, type);
471 /* Return true if the next token is the indicated KEYWORD. */
474 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
478 /* Peek at the next token. */
479 token = cp_lexer_peek_token (lexer);
480 /* Check to see if it is the indicated keyword. */
481 return token->keyword == keyword;
484 /* Return a pointer to the Nth token in the token stream. If N is 1,
485 then this is precisely equivalent to cp_lexer_peek_token (except
486 that it is not inline). One would like to disallow that case, but
487 there is one case (cp_parser_nth_token_starts_template_id) where
488 the caller passes a variable for N and it might be 1. */
491 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
495 /* N is 1-based, not zero-based. */
496 gcc_assert (n > 0 && lexer->next_token != &eof_token);
498 if (cp_lexer_debugging_p (lexer))
499 fprintf (cp_lexer_debug_stream,
500 "cp_lexer: peeking ahead %ld at token: ", (long)n);
503 token = lexer->next_token;
507 if (token == lexer->last_token)
509 token = (cp_token *)&eof_token;
513 if (token->type != CPP_PURGED)
517 if (cp_lexer_debugging_p (lexer))
519 cp_lexer_print_token (cp_lexer_debug_stream, token);
520 putc ('\n', cp_lexer_debug_stream);
526 /* Return the next token, and advance the lexer's next_token pointer
527 to point to the next non-purged token. */
530 cp_lexer_consume_token (cp_lexer* lexer)
532 cp_token *token = lexer->next_token;
534 gcc_assert (token != &eof_token);
539 if (lexer->next_token == lexer->last_token)
541 lexer->next_token = (cp_token *)&eof_token;
546 while (lexer->next_token->type == CPP_PURGED);
548 cp_lexer_set_source_position_from_token (token);
550 /* Provide debugging output. */
551 if (cp_lexer_debugging_p (lexer))
553 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
554 cp_lexer_print_token (cp_lexer_debug_stream, token);
555 putc ('\n', cp_lexer_debug_stream);
561 /* Permanently remove the next token from the token stream, and
562 advance the next_token pointer to refer to the next non-purged
566 cp_lexer_purge_token (cp_lexer *lexer)
568 cp_token *tok = lexer->next_token;
570 gcc_assert (tok != &eof_token);
571 tok->type = CPP_PURGED;
572 tok->location = UNKNOWN_LOCATION;
573 tok->value = NULL_TREE;
574 tok->keyword = RID_MAX;
579 if (tok == lexer->last_token)
581 tok = (cp_token *)&eof_token;
585 while (tok->type == CPP_PURGED);
586 lexer->next_token = tok;
589 /* Permanently remove all tokens after TOK, up to, but not
590 including, the token that will be returned next by
591 cp_lexer_peek_token. */
594 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
596 cp_token *peek = lexer->next_token;
598 if (peek == &eof_token)
599 peek = lexer->last_token;
601 gcc_assert (tok < peek);
603 for ( tok += 1; tok != peek; tok += 1)
605 tok->type = CPP_PURGED;
606 tok->location = UNKNOWN_LOCATION;
607 tok->value = NULL_TREE;
608 tok->keyword = RID_MAX;
612 /* Consume and handle a pragma token. */
614 cp_lexer_handle_pragma (cp_lexer *lexer)
617 cp_token *token = cp_lexer_consume_token (lexer);
618 gcc_assert (token->type == CPP_PRAGMA);
619 gcc_assert (token->value);
621 s.len = TREE_STRING_LENGTH (token->value);
622 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
624 cpp_handle_deferred_pragma (parse_in, &s);
626 /* Clearing token->value here means that we will get an ICE if we
627 try to process this #pragma again (which should be impossible). */
631 /* Begin saving tokens. All tokens consumed after this point will be
635 cp_lexer_save_tokens (cp_lexer* lexer)
637 /* Provide debugging output. */
638 if (cp_lexer_debugging_p (lexer))
639 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
641 VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
644 /* Commit to the portion of the token stream most recently saved. */
647 cp_lexer_commit_tokens (cp_lexer* lexer)
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
651 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
653 VEC_pop (cp_token_position, lexer->saved_tokens);
656 /* Return all tokens saved since the last call to cp_lexer_save_tokens
657 to the token stream. Stop saving tokens. */
660 cp_lexer_rollback_tokens (cp_lexer* lexer)
662 /* Provide debugging output. */
663 if (cp_lexer_debugging_p (lexer))
664 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
666 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
669 /* Print a representation of the TOKEN on the STREAM. */
671 #ifdef ENABLE_CHECKING
674 cp_lexer_print_token (FILE * stream, cp_token *token)
676 /* We don't use cpp_type2name here because the parser defines
677 a few tokens of its own. */
678 static const char *const token_names[] = {
679 /* cpplib-defined token types */
685 /* C++ parser token types - see "Manifest constants", above. */
688 "NESTED_NAME_SPECIFIER",
692 /* If we have a name for the token, print it out. Otherwise, we
693 simply give the numeric code. */
694 gcc_assert (token->type < ARRAY_SIZE(token_names));
695 fputs (token_names[token->type], stream);
697 /* For some tokens, print the associated data. */
701 /* Some keywords have a value that is not an IDENTIFIER_NODE.
702 For example, `struct' is mapped to an INTEGER_CST. */
703 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
705 /* else fall through */
707 fputs (IDENTIFIER_POINTER (token->value), stream);
713 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
721 /* Start emitting debugging information. */
724 cp_lexer_start_debugging (cp_lexer* lexer)
726 ++lexer->debugging_p;
729 /* Stop emitting debugging information. */
732 cp_lexer_stop_debugging (cp_lexer* lexer)
734 --lexer->debugging_p;
737 #endif /* ENABLE_CHECKING */
739 /* Create a new cp_token_cache, representing a range of tokens. */
741 static cp_token_cache *
742 cp_token_cache_new (cp_token *first, cp_token *last)
744 cp_token_cache *cache = GGC_NEW (cp_token_cache);
745 cache->first = first;
751 /* Decl-specifiers. */
753 static void clear_decl_specs
754 (cp_decl_specifier_seq *);
756 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
759 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
761 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
766 /* Nothing other than the parser should be creating declarators;
767 declarators are a semi-syntactic representation of C++ entities.
768 Other parts of the front end that need to create entities (like
769 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
771 static cp_declarator *make_id_declarator
773 static cp_declarator *make_call_declarator
774 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
775 static cp_declarator *make_array_declarator
776 (cp_declarator *, tree);
777 static cp_declarator *make_pointer_declarator
778 (cp_cv_quals, cp_declarator *);
779 static cp_declarator *make_reference_declarator
780 (cp_cv_quals, cp_declarator *);
781 static cp_parameter_declarator *make_parameter_declarator
782 (cp_decl_specifier_seq *, cp_declarator *, tree);
783 static cp_declarator *make_ptrmem_declarator
784 (cp_cv_quals, tree, cp_declarator *);
786 cp_declarator *cp_error_declarator;
788 /* The obstack on which declarators and related data structures are
790 static struct obstack declarator_obstack;
792 /* Alloc BYTES from the declarator memory pool. */
795 alloc_declarator (size_t bytes)
797 return obstack_alloc (&declarator_obstack, bytes);
800 /* Allocate a declarator of the indicated KIND. Clear fields that are
801 common to all declarators. */
803 static cp_declarator *
804 make_declarator (cp_declarator_kind kind)
806 cp_declarator *declarator;
808 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
809 declarator->kind = kind;
810 declarator->attributes = NULL_TREE;
811 declarator->declarator = NULL;
816 /* Make a declarator for a generalized identifier. */
819 make_id_declarator (tree id)
821 cp_declarator *declarator;
823 declarator = make_declarator (cdk_id);
824 declarator->u.id.name = id;
825 declarator->u.id.sfk = sfk_none;
830 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
831 of modifiers such as const or volatile to apply to the pointer
832 type, represented as identifiers. */
835 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
837 cp_declarator *declarator;
839 declarator = make_declarator (cdk_pointer);
840 declarator->declarator = target;
841 declarator->u.pointer.qualifiers = cv_qualifiers;
842 declarator->u.pointer.class_type = NULL_TREE;
847 /* Like make_pointer_declarator -- but for references. */
850 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
852 cp_declarator *declarator;
854 declarator = make_declarator (cdk_reference);
855 declarator->declarator = target;
856 declarator->u.pointer.qualifiers = cv_qualifiers;
857 declarator->u.pointer.class_type = NULL_TREE;
862 /* Like make_pointer_declarator -- but for a pointer to a non-static
863 member of CLASS_TYPE. */
866 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
867 cp_declarator *pointee)
869 cp_declarator *declarator;
871 declarator = make_declarator (cdk_ptrmem);
872 declarator->declarator = pointee;
873 declarator->u.pointer.qualifiers = cv_qualifiers;
874 declarator->u.pointer.class_type = class_type;
879 /* Make a declarator for the function given by TARGET, with the
880 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
881 "const"-qualified member function. The EXCEPTION_SPECIFICATION
882 indicates what exceptions can be thrown. */
885 make_call_declarator (cp_declarator *target,
886 cp_parameter_declarator *parms,
887 cp_cv_quals cv_qualifiers,
888 tree exception_specification)
890 cp_declarator *declarator;
892 declarator = make_declarator (cdk_function);
893 declarator->declarator = target;
894 declarator->u.function.parameters = parms;
895 declarator->u.function.qualifiers = cv_qualifiers;
896 declarator->u.function.exception_specification = exception_specification;
901 /* Make a declarator for an array of BOUNDS elements, each of which is
902 defined by ELEMENT. */
905 make_array_declarator (cp_declarator *element, tree bounds)
907 cp_declarator *declarator;
909 declarator = make_declarator (cdk_array);
910 declarator->declarator = element;
911 declarator->u.array.bounds = bounds;
916 cp_parameter_declarator *no_parameters;
918 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
919 DECLARATOR and DEFAULT_ARGUMENT. */
921 cp_parameter_declarator *
922 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
923 cp_declarator *declarator,
924 tree default_argument)
926 cp_parameter_declarator *parameter;
928 parameter = ((cp_parameter_declarator *)
929 alloc_declarator (sizeof (cp_parameter_declarator)));
930 parameter->next = NULL;
932 parameter->decl_specifiers = *decl_specifiers;
934 clear_decl_specs (¶meter->decl_specifiers);
935 parameter->declarator = declarator;
936 parameter->default_argument = default_argument;
937 parameter->ellipsis_p = false;
947 A cp_parser parses the token stream as specified by the C++
948 grammar. Its job is purely parsing, not semantic analysis. For
949 example, the parser breaks the token stream into declarators,
950 expressions, statements, and other similar syntactic constructs.
951 It does not check that the types of the expressions on either side
952 of an assignment-statement are compatible, or that a function is
953 not declared with a parameter of type `void'.
955 The parser invokes routines elsewhere in the compiler to perform
956 semantic analysis and to build up the abstract syntax tree for the
959 The parser (and the template instantiation code, which is, in a
960 way, a close relative of parsing) are the only parts of the
961 compiler that should be calling push_scope and pop_scope, or
962 related functions. The parser (and template instantiation code)
963 keeps track of what scope is presently active; everything else
964 should simply honor that. (The code that generates static
965 initializers may also need to set the scope, in order to check
966 access control correctly when emitting the initializers.)
971 The parser is of the standard recursive-descent variety. Upcoming
972 tokens in the token stream are examined in order to determine which
973 production to use when parsing a non-terminal. Some C++ constructs
974 require arbitrary look ahead to disambiguate. For example, it is
975 impossible, in the general case, to tell whether a statement is an
976 expression or declaration without scanning the entire statement.
977 Therefore, the parser is capable of "parsing tentatively." When the
978 parser is not sure what construct comes next, it enters this mode.
979 Then, while we attempt to parse the construct, the parser queues up
980 error messages, rather than issuing them immediately, and saves the
981 tokens it consumes. If the construct is parsed successfully, the
982 parser "commits", i.e., it issues any queued error messages and
983 the tokens that were being preserved are permanently discarded.
984 If, however, the construct is not parsed successfully, the parser
985 rolls back its state completely so that it can resume parsing using
986 a different alternative.
991 The performance of the parser could probably be improved substantially.
992 We could often eliminate the need to parse tentatively by looking ahead
993 a little bit. In some places, this approach might not entirely eliminate
994 the need to parse tentatively, but it might still speed up the average
997 /* Flags that are passed to some parsing functions. These values can
998 be bitwise-ored together. */
1000 typedef enum cp_parser_flags
1003 CP_PARSER_FLAGS_NONE = 0x0,
1004 /* The construct is optional. If it is not present, then no error
1005 should be issued. */
1006 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1007 /* When parsing a type-specifier, do not allow user-defined types. */
1008 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1011 /* The different kinds of declarators we want to parse. */
1013 typedef enum cp_parser_declarator_kind
1015 /* We want an abstract declarator. */
1016 CP_PARSER_DECLARATOR_ABSTRACT,
1017 /* We want a named declarator. */
1018 CP_PARSER_DECLARATOR_NAMED,
1019 /* We don't mind, but the name must be an unqualified-id. */
1020 CP_PARSER_DECLARATOR_EITHER
1021 } cp_parser_declarator_kind;
1023 /* The precedence values used to parse binary expressions. The minimum value
1024 of PREC must be 1, because zero is reserved to quickly discriminate
1025 binary operators from other tokens. */
1030 PREC_LOGICAL_OR_EXPRESSION,
1031 PREC_LOGICAL_AND_EXPRESSION,
1032 PREC_INCLUSIVE_OR_EXPRESSION,
1033 PREC_EXCLUSIVE_OR_EXPRESSION,
1034 PREC_AND_EXPRESSION,
1035 PREC_EQUALITY_EXPRESSION,
1036 PREC_RELATIONAL_EXPRESSION,
1037 PREC_SHIFT_EXPRESSION,
1038 PREC_ADDITIVE_EXPRESSION,
1039 PREC_MULTIPLICATIVE_EXPRESSION,
1041 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1044 /* A mapping from a token type to a corresponding tree node type, with a
1045 precedence value. */
1047 typedef struct cp_parser_binary_operations_map_node
1049 /* The token type. */
1050 enum cpp_ttype token_type;
1051 /* The corresponding tree code. */
1052 enum tree_code tree_type;
1053 /* The precedence of this operator. */
1054 enum cp_parser_prec prec;
1055 } cp_parser_binary_operations_map_node;
1057 /* The status of a tentative parse. */
1059 typedef enum cp_parser_status_kind
1061 /* No errors have occurred. */
1062 CP_PARSER_STATUS_KIND_NO_ERROR,
1063 /* An error has occurred. */
1064 CP_PARSER_STATUS_KIND_ERROR,
1065 /* We are committed to this tentative parse, whether or not an error
1067 CP_PARSER_STATUS_KIND_COMMITTED
1068 } cp_parser_status_kind;
1070 typedef struct cp_parser_expression_stack_entry
1073 enum tree_code tree_type;
1075 } cp_parser_expression_stack_entry;
1077 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1078 entries because precedence levels on the stack are monotonically
1080 typedef struct cp_parser_expression_stack_entry
1081 cp_parser_expression_stack[NUM_PREC_VALUES];
1083 /* Context that is saved and restored when parsing tentatively. */
1084 typedef struct cp_parser_context GTY (())
1086 /* If this is a tentative parsing context, the status of the
1088 enum cp_parser_status_kind status;
1089 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1090 that are looked up in this context must be looked up both in the
1091 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1092 the context of the containing expression. */
1095 /* The next parsing context in the stack. */
1096 struct cp_parser_context *next;
1097 } cp_parser_context;
1101 /* Constructors and destructors. */
1103 static cp_parser_context *cp_parser_context_new
1104 (cp_parser_context *);
1106 /* Class variables. */
1108 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1110 /* The operator-precedence table used by cp_parser_binary_expression.
1111 Transformed into an associative array (binops_by_token) by
1114 static const cp_parser_binary_operations_map_node binops[] = {
1115 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1116 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1118 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1119 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1120 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1122 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1123 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1125 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1126 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1128 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1129 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1130 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1131 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1132 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1133 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1135 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1136 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1138 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1140 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1142 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1144 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1146 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1149 /* The same as binops, but initialized by cp_parser_new so that
1150 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1152 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1154 /* Constructors and destructors. */
1156 /* Construct a new context. The context below this one on the stack
1157 is given by NEXT. */
1159 static cp_parser_context *
1160 cp_parser_context_new (cp_parser_context* next)
1162 cp_parser_context *context;
1164 /* Allocate the storage. */
1165 if (cp_parser_context_free_list != NULL)
1167 /* Pull the first entry from the free list. */
1168 context = cp_parser_context_free_list;
1169 cp_parser_context_free_list = context->next;
1170 memset (context, 0, sizeof (*context));
1173 context = GGC_CNEW (cp_parser_context);
1175 /* No errors have occurred yet in this context. */
1176 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1177 /* If this is not the bottomost context, copy information that we
1178 need from the previous context. */
1181 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1182 expression, then we are parsing one in this context, too. */
1183 context->object_type = next->object_type;
1184 /* Thread the stack. */
1185 context->next = next;
1191 /* The cp_parser structure represents the C++ parser. */
1193 typedef struct cp_parser GTY(())
1195 /* The lexer from which we are obtaining tokens. */
1198 /* The scope in which names should be looked up. If NULL_TREE, then
1199 we look up names in the scope that is currently open in the
1200 source program. If non-NULL, this is either a TYPE or
1201 NAMESPACE_DECL for the scope in which we should look.
1203 This value is not cleared automatically after a name is looked
1204 up, so we must be careful to clear it before starting a new look
1205 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1206 will look up `Z' in the scope of `X', rather than the current
1207 scope.) Unfortunately, it is difficult to tell when name lookup
1208 is complete, because we sometimes peek at a token, look it up,
1209 and then decide not to consume it. */
1212 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1213 last lookup took place. OBJECT_SCOPE is used if an expression
1214 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1215 respectively. QUALIFYING_SCOPE is used for an expression of the
1216 form "X::Y"; it refers to X. */
1218 tree qualifying_scope;
1220 /* A stack of parsing contexts. All but the bottom entry on the
1221 stack will be tentative contexts.
1223 We parse tentatively in order to determine which construct is in
1224 use in some situations. For example, in order to determine
1225 whether a statement is an expression-statement or a
1226 declaration-statement we parse it tentatively as a
1227 declaration-statement. If that fails, we then reparse the same
1228 token stream as an expression-statement. */
1229 cp_parser_context *context;
1231 /* True if we are parsing GNU C++. If this flag is not set, then
1232 GNU extensions are not recognized. */
1233 bool allow_gnu_extensions_p;
1235 /* TRUE if the `>' token should be interpreted as the greater-than
1236 operator. FALSE if it is the end of a template-id or
1237 template-parameter-list. */
1238 bool greater_than_is_operator_p;
1240 /* TRUE if default arguments are allowed within a parameter list
1241 that starts at this point. FALSE if only a gnu extension makes
1242 them permissible. */
1243 bool default_arg_ok_p;
1245 /* TRUE if we are parsing an integral constant-expression. See
1246 [expr.const] for a precise definition. */
1247 bool integral_constant_expression_p;
1249 /* TRUE if we are parsing an integral constant-expression -- but a
1250 non-constant expression should be permitted as well. This flag
1251 is used when parsing an array bound so that GNU variable-length
1252 arrays are tolerated. */
1253 bool allow_non_integral_constant_expression_p;
1255 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1256 been seen that makes the expression non-constant. */
1257 bool non_integral_constant_expression_p;
1259 /* TRUE if local variable names and `this' are forbidden in the
1261 bool local_variables_forbidden_p;
1263 /* TRUE if the declaration we are parsing is part of a
1264 linkage-specification of the form `extern string-literal
1266 bool in_unbraced_linkage_specification_p;
1268 /* TRUE if we are presently parsing a declarator, after the
1269 direct-declarator. */
1270 bool in_declarator_p;
1272 /* TRUE if we are presently parsing a template-argument-list. */
1273 bool in_template_argument_list_p;
1275 /* TRUE if we are presently parsing the body of an
1276 iteration-statement. */
1277 bool in_iteration_statement_p;
1279 /* TRUE if we are presently parsing the body of a switch
1281 bool in_switch_statement_p;
1283 /* TRUE if we are parsing a type-id in an expression context. In
1284 such a situation, both "type (expr)" and "type (type)" are valid
1286 bool in_type_id_in_expr_p;
1288 /* TRUE if we are currently in a header file where declarations are
1289 implicitly extern "C". */
1290 bool implicit_extern_c;
1292 /* TRUE if strings in expressions should be translated to the execution
1294 bool translate_strings_p;
1296 /* If non-NULL, then we are parsing a construct where new type
1297 definitions are not permitted. The string stored here will be
1298 issued as an error message if a type is defined. */
1299 const char *type_definition_forbidden_message;
1301 /* A list of lists. The outer list is a stack, used for member
1302 functions of local classes. At each level there are two sub-list,
1303 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1304 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1305 TREE_VALUE's. The functions are chained in reverse declaration
1308 The TREE_PURPOSE sublist contains those functions with default
1309 arguments that need post processing, and the TREE_VALUE sublist
1310 contains those functions with definitions that need post
1313 These lists can only be processed once the outermost class being
1314 defined is complete. */
1315 tree unparsed_functions_queues;
1317 /* The number of classes whose definitions are currently in
1319 unsigned num_classes_being_defined;
1321 /* The number of template parameter lists that apply directly to the
1322 current declaration. */
1323 unsigned num_template_parameter_lists;
1326 /* The type of a function that parses some kind of expression. */
1327 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1331 /* Constructors and destructors. */
1333 static cp_parser *cp_parser_new
1336 /* Routines to parse various constructs.
1338 Those that return `tree' will return the error_mark_node (rather
1339 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1340 Sometimes, they will return an ordinary node if error-recovery was
1341 attempted, even though a parse error occurred. So, to check
1342 whether or not a parse error occurred, you should always use
1343 cp_parser_error_occurred. If the construct is optional (indicated
1344 either by an `_opt' in the name of the function that does the
1345 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1346 the construct is not present. */
1348 /* Lexical conventions [gram.lex] */
1350 static tree cp_parser_identifier
1352 static tree cp_parser_string_literal
1353 (cp_parser *, bool, bool);
1355 /* Basic concepts [gram.basic] */
1357 static bool cp_parser_translation_unit
1360 /* Expressions [gram.expr] */
1362 static tree cp_parser_primary_expression
1363 (cp_parser *, cp_id_kind *, tree *);
1364 static tree cp_parser_id_expression
1365 (cp_parser *, bool, bool, bool *, bool);
1366 static tree cp_parser_unqualified_id
1367 (cp_parser *, bool, bool, bool);
1368 static tree cp_parser_nested_name_specifier_opt
1369 (cp_parser *, bool, bool, bool, bool);
1370 static tree cp_parser_nested_name_specifier
1371 (cp_parser *, bool, bool, bool, bool);
1372 static tree cp_parser_class_or_namespace_name
1373 (cp_parser *, bool, bool, bool, bool, bool);
1374 static tree cp_parser_postfix_expression
1375 (cp_parser *, bool);
1376 static tree cp_parser_postfix_open_square_expression
1377 (cp_parser *, tree, bool);
1378 static tree cp_parser_postfix_dot_deref_expression
1379 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1380 static tree cp_parser_parenthesized_expression_list
1381 (cp_parser *, bool, bool *);
1382 static void cp_parser_pseudo_destructor_name
1383 (cp_parser *, tree *, tree *);
1384 static tree cp_parser_unary_expression
1385 (cp_parser *, bool);
1386 static enum tree_code cp_parser_unary_operator
1388 static tree cp_parser_new_expression
1390 static tree cp_parser_new_placement
1392 static tree cp_parser_new_type_id
1393 (cp_parser *, tree *);
1394 static cp_declarator *cp_parser_new_declarator_opt
1396 static cp_declarator *cp_parser_direct_new_declarator
1398 static tree cp_parser_new_initializer
1400 static tree cp_parser_delete_expression
1402 static tree cp_parser_cast_expression
1403 (cp_parser *, bool);
1404 static tree cp_parser_binary_expression
1406 static tree cp_parser_question_colon_clause
1407 (cp_parser *, tree);
1408 static tree cp_parser_assignment_expression
1410 static enum tree_code cp_parser_assignment_operator_opt
1412 static tree cp_parser_expression
1414 static tree cp_parser_constant_expression
1415 (cp_parser *, bool, bool *);
1416 static tree cp_parser_builtin_offsetof
1419 /* Statements [gram.stmt.stmt] */
1421 static void cp_parser_statement
1422 (cp_parser *, tree);
1423 static tree cp_parser_labeled_statement
1424 (cp_parser *, tree);
1425 static tree cp_parser_expression_statement
1426 (cp_parser *, tree);
1427 static tree cp_parser_compound_statement
1428 (cp_parser *, tree, bool);
1429 static void cp_parser_statement_seq_opt
1430 (cp_parser *, tree);
1431 static tree cp_parser_selection_statement
1433 static tree cp_parser_condition
1435 static tree cp_parser_iteration_statement
1437 static void cp_parser_for_init_statement
1439 static tree cp_parser_jump_statement
1441 static void cp_parser_declaration_statement
1444 static tree cp_parser_implicitly_scoped_statement
1446 static void cp_parser_already_scoped_statement
1449 /* Declarations [gram.dcl.dcl] */
1451 static void cp_parser_declaration_seq_opt
1453 static void cp_parser_declaration
1455 static void cp_parser_block_declaration
1456 (cp_parser *, bool);
1457 static void cp_parser_simple_declaration
1458 (cp_parser *, bool);
1459 static void cp_parser_decl_specifier_seq
1460 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1461 static tree cp_parser_storage_class_specifier_opt
1463 static tree cp_parser_function_specifier_opt
1464 (cp_parser *, cp_decl_specifier_seq *);
1465 static tree cp_parser_type_specifier
1466 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1468 static tree cp_parser_simple_type_specifier
1469 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1470 static tree cp_parser_type_name
1472 static tree cp_parser_elaborated_type_specifier
1473 (cp_parser *, bool, bool);
1474 static tree cp_parser_enum_specifier
1476 static void cp_parser_enumerator_list
1477 (cp_parser *, tree);
1478 static void cp_parser_enumerator_definition
1479 (cp_parser *, tree);
1480 static tree cp_parser_namespace_name
1482 static void cp_parser_namespace_definition
1484 static void cp_parser_namespace_body
1486 static tree cp_parser_qualified_namespace_specifier
1488 static void cp_parser_namespace_alias_definition
1490 static void cp_parser_using_declaration
1492 static void cp_parser_using_directive
1494 static void cp_parser_asm_definition
1496 static void cp_parser_linkage_specification
1499 /* Declarators [gram.dcl.decl] */
1501 static tree cp_parser_init_declarator
1502 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1503 static cp_declarator *cp_parser_declarator
1504 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1505 static cp_declarator *cp_parser_direct_declarator
1506 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1507 static enum tree_code cp_parser_ptr_operator
1508 (cp_parser *, tree *, cp_cv_quals *);
1509 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1511 static tree cp_parser_declarator_id
1513 static tree cp_parser_type_id
1515 static void cp_parser_type_specifier_seq
1516 (cp_parser *, cp_decl_specifier_seq *);
1517 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1519 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1520 (cp_parser *, bool *);
1521 static cp_parameter_declarator *cp_parser_parameter_declaration
1522 (cp_parser *, bool, bool *);
1523 static void cp_parser_function_body
1525 static tree cp_parser_initializer
1526 (cp_parser *, bool *, bool *);
1527 static tree cp_parser_initializer_clause
1528 (cp_parser *, bool *);
1529 static tree cp_parser_initializer_list
1530 (cp_parser *, bool *);
1532 static bool cp_parser_ctor_initializer_opt_and_function_body
1535 /* Classes [gram.class] */
1537 static tree cp_parser_class_name
1538 (cp_parser *, bool, bool, bool, bool, bool, bool);
1539 static tree cp_parser_class_specifier
1541 static tree cp_parser_class_head
1542 (cp_parser *, bool *, tree *);
1543 static enum tag_types cp_parser_class_key
1545 static void cp_parser_member_specification_opt
1547 static void cp_parser_member_declaration
1549 static tree cp_parser_pure_specifier
1551 static tree cp_parser_constant_initializer
1554 /* Derived classes [gram.class.derived] */
1556 static tree cp_parser_base_clause
1558 static tree cp_parser_base_specifier
1561 /* Special member functions [gram.special] */
1563 static tree cp_parser_conversion_function_id
1565 static tree cp_parser_conversion_type_id
1567 static cp_declarator *cp_parser_conversion_declarator_opt
1569 static bool cp_parser_ctor_initializer_opt
1571 static void cp_parser_mem_initializer_list
1573 static tree cp_parser_mem_initializer
1575 static tree cp_parser_mem_initializer_id
1578 /* Overloading [gram.over] */
1580 static tree cp_parser_operator_function_id
1582 static tree cp_parser_operator
1585 /* Templates [gram.temp] */
1587 static void cp_parser_template_declaration
1588 (cp_parser *, bool);
1589 static tree cp_parser_template_parameter_list
1591 static tree cp_parser_template_parameter
1592 (cp_parser *, bool *);
1593 static tree cp_parser_type_parameter
1595 static tree cp_parser_template_id
1596 (cp_parser *, bool, bool, bool);
1597 static tree cp_parser_template_name
1598 (cp_parser *, bool, bool, bool, bool *);
1599 static tree cp_parser_template_argument_list
1601 static tree cp_parser_template_argument
1603 static void cp_parser_explicit_instantiation
1605 static void cp_parser_explicit_specialization
1608 /* Exception handling [gram.exception] */
1610 static tree cp_parser_try_block
1612 static bool cp_parser_function_try_block
1614 static void cp_parser_handler_seq
1616 static void cp_parser_handler
1618 static tree cp_parser_exception_declaration
1620 static tree cp_parser_throw_expression
1622 static tree cp_parser_exception_specification_opt
1624 static tree cp_parser_type_id_list
1627 /* GNU Extensions */
1629 static tree cp_parser_asm_specification_opt
1631 static tree cp_parser_asm_operand_list
1633 static tree cp_parser_asm_clobber_list
1635 static tree cp_parser_attributes_opt
1637 static tree cp_parser_attribute_list
1639 static bool cp_parser_extension_opt
1640 (cp_parser *, int *);
1641 static void cp_parser_label_declaration
1644 /* Utility Routines */
1646 static tree cp_parser_lookup_name
1647 (cp_parser *, tree, bool, bool, bool, bool, bool *);
1648 static tree cp_parser_lookup_name_simple
1649 (cp_parser *, tree);
1650 static tree cp_parser_maybe_treat_template_as_class
1652 static bool cp_parser_check_declarator_template_parameters
1653 (cp_parser *, cp_declarator *);
1654 static bool cp_parser_check_template_parameters
1655 (cp_parser *, unsigned);
1656 static tree cp_parser_simple_cast_expression
1658 static tree cp_parser_global_scope_opt
1659 (cp_parser *, bool);
1660 static bool cp_parser_constructor_declarator_p
1661 (cp_parser *, bool);
1662 static tree cp_parser_function_definition_from_specifiers_and_declarator
1663 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1664 static tree cp_parser_function_definition_after_declarator
1665 (cp_parser *, bool);
1666 static void cp_parser_template_declaration_after_export
1667 (cp_parser *, bool);
1668 static tree cp_parser_single_declaration
1669 (cp_parser *, bool, bool *);
1670 static tree cp_parser_functional_cast
1671 (cp_parser *, tree);
1672 static tree cp_parser_save_member_function_body
1673 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1674 static tree cp_parser_enclosed_template_argument_list
1676 static void cp_parser_save_default_args
1677 (cp_parser *, tree);
1678 static void cp_parser_late_parsing_for_member
1679 (cp_parser *, tree);
1680 static void cp_parser_late_parsing_default_args
1681 (cp_parser *, tree);
1682 static tree cp_parser_sizeof_operand
1683 (cp_parser *, enum rid);
1684 static bool cp_parser_declares_only_class_p
1686 static void cp_parser_set_storage_class
1687 (cp_decl_specifier_seq *, cp_storage_class);
1688 static void cp_parser_set_decl_spec_type
1689 (cp_decl_specifier_seq *, tree, bool);
1690 static bool cp_parser_friend_p
1691 (const cp_decl_specifier_seq *);
1692 static cp_token *cp_parser_require
1693 (cp_parser *, enum cpp_ttype, const char *);
1694 static cp_token *cp_parser_require_keyword
1695 (cp_parser *, enum rid, const char *);
1696 static bool cp_parser_token_starts_function_definition_p
1698 static bool cp_parser_next_token_starts_class_definition_p
1700 static bool cp_parser_next_token_ends_template_argument_p
1702 static bool cp_parser_nth_token_starts_template_argument_list_p
1703 (cp_parser *, size_t);
1704 static enum tag_types cp_parser_token_is_class_key
1706 static void cp_parser_check_class_key
1707 (enum tag_types, tree type);
1708 static void cp_parser_check_access_in_redeclaration
1710 static bool cp_parser_optional_template_keyword
1712 static void cp_parser_pre_parsed_nested_name_specifier
1714 static void cp_parser_cache_group
1715 (cp_parser *, enum cpp_ttype, unsigned);
1716 static void cp_parser_parse_tentatively
1718 static void cp_parser_commit_to_tentative_parse
1720 static void cp_parser_abort_tentative_parse
1722 static bool cp_parser_parse_definitely
1724 static inline bool cp_parser_parsing_tentatively
1726 static bool cp_parser_committed_to_tentative_parse
1728 static void cp_parser_error
1729 (cp_parser *, const char *);
1730 static void cp_parser_name_lookup_error
1731 (cp_parser *, tree, tree, const char *);
1732 static bool cp_parser_simulate_error
1734 static void cp_parser_check_type_definition
1736 static void cp_parser_check_for_definition_in_return_type
1737 (cp_declarator *, int);
1738 static void cp_parser_check_for_invalid_template_id
1739 (cp_parser *, tree);
1740 static bool cp_parser_non_integral_constant_expression
1741 (cp_parser *, const char *);
1742 static void cp_parser_diagnose_invalid_type_name
1743 (cp_parser *, tree, tree);
1744 static bool cp_parser_parse_and_diagnose_invalid_type_name
1746 static int cp_parser_skip_to_closing_parenthesis
1747 (cp_parser *, bool, bool, bool);
1748 static void cp_parser_skip_to_end_of_statement
1750 static void cp_parser_consume_semicolon_at_end_of_statement
1752 static void cp_parser_skip_to_end_of_block_or_statement
1754 static void cp_parser_skip_to_closing_brace
1756 static void cp_parser_skip_until_found
1757 (cp_parser *, enum cpp_ttype, const char *);
1758 static bool cp_parser_error_occurred
1760 static bool cp_parser_allow_gnu_extensions_p
1762 static bool cp_parser_is_string_literal
1764 static bool cp_parser_is_keyword
1765 (cp_token *, enum rid);
1766 static tree cp_parser_make_typename_type
1767 (cp_parser *, tree, tree);
1769 /* Returns nonzero if we are parsing tentatively. */
1772 cp_parser_parsing_tentatively (cp_parser* parser)
1774 return parser->context->next != NULL;
1777 /* Returns nonzero if TOKEN is a string literal. */
1780 cp_parser_is_string_literal (cp_token* token)
1782 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1785 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1788 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1790 return token->keyword == keyword;
1793 /* If not parsing tentatively, issue a diagnostic of the form
1794 FILE:LINE: MESSAGE before TOKEN
1795 where TOKEN is the next token in the input stream. MESSAGE
1796 (specified by the caller) is usually of the form "expected
1800 cp_parser_error (cp_parser* parser, const char* message)
1802 if (!cp_parser_simulate_error (parser))
1804 cp_token *token = cp_lexer_peek_token (parser->lexer);
1805 /* This diagnostic makes more sense if it is tagged to the line
1806 of the token we just peeked at. */
1807 cp_lexer_set_source_position_from_token (token);
1808 c_parse_error (message,
1809 /* Because c_parser_error does not understand
1810 CPP_KEYWORD, keywords are treated like
1812 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1817 /* Issue an error about name-lookup failing. NAME is the
1818 IDENTIFIER_NODE DECL is the result of
1819 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1820 the thing that we hoped to find. */
1823 cp_parser_name_lookup_error (cp_parser* parser,
1826 const char* desired)
1828 /* If name lookup completely failed, tell the user that NAME was not
1830 if (decl == error_mark_node)
1832 if (parser->scope && parser->scope != global_namespace)
1833 error ("%<%D::%D%> has not been declared",
1834 parser->scope, name);
1835 else if (parser->scope == global_namespace)
1836 error ("%<::%D%> has not been declared", name);
1837 else if (parser->object_scope
1838 && !CLASS_TYPE_P (parser->object_scope))
1839 error ("request for member %qD in non-class type %qT",
1840 name, parser->object_scope);
1841 else if (parser->object_scope)
1842 error ("%<%T::%D%> has not been declared",
1843 parser->object_scope, name);
1845 error ("`%D' has not been declared", name);
1847 else if (parser->scope && parser->scope != global_namespace)
1848 error ("%<%D::%D%> %s", parser->scope, name, desired);
1849 else if (parser->scope == global_namespace)
1850 error ("%<::%D%> %s", name, desired);
1852 error ("%qD %s", name, desired);
1855 /* If we are parsing tentatively, remember that an error has occurred
1856 during this tentative parse. Returns true if the error was
1857 simulated; false if a message should be issued by the caller. */
1860 cp_parser_simulate_error (cp_parser* parser)
1862 if (cp_parser_parsing_tentatively (parser)
1863 && !cp_parser_committed_to_tentative_parse (parser))
1865 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1871 /* This function is called when a type is defined. If type
1872 definitions are forbidden at this point, an error message is
1876 cp_parser_check_type_definition (cp_parser* parser)
1878 /* If types are forbidden here, issue a message. */
1879 if (parser->type_definition_forbidden_message)
1880 /* Use `%s' to print the string in case there are any escape
1881 characters in the message. */
1882 error ("%s", parser->type_definition_forbidden_message);
1885 /* This function is called when a declaration is parsed. If
1886 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1887 indicates that a type was defined in the decl-specifiers for DECL,
1888 then an error is issued. */
1891 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1892 int declares_class_or_enum)
1894 /* [dcl.fct] forbids type definitions in return types.
1895 Unfortunately, it's not easy to know whether or not we are
1896 processing a return type until after the fact. */
1898 && (declarator->kind == cdk_pointer
1899 || declarator->kind == cdk_reference
1900 || declarator->kind == cdk_ptrmem))
1901 declarator = declarator->declarator;
1903 && declarator->kind == cdk_function
1904 && declares_class_or_enum & 2)
1905 error ("new types may not be defined in a return type");
1908 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1909 "<" in any valid C++ program. If the next token is indeed "<",
1910 issue a message warning the user about what appears to be an
1911 invalid attempt to form a template-id. */
1914 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1917 cp_token_position start = 0;
1919 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1922 error ("%qT is not a template", type);
1923 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1924 error ("%qE is not a template", type);
1926 error ("invalid template-id");
1927 /* Remember the location of the invalid "<". */
1928 if (cp_parser_parsing_tentatively (parser)
1929 && !cp_parser_committed_to_tentative_parse (parser))
1930 start = cp_lexer_token_position (parser->lexer, true);
1931 /* Consume the "<". */
1932 cp_lexer_consume_token (parser->lexer);
1933 /* Parse the template arguments. */
1934 cp_parser_enclosed_template_argument_list (parser);
1935 /* Permanently remove the invalid template arguments so that
1936 this error message is not issued again. */
1938 cp_lexer_purge_tokens_after (parser->lexer, start);
1942 /* If parsing an integral constant-expression, issue an error message
1943 about the fact that THING appeared and return true. Otherwise,
1944 return false, marking the current expression as non-constant. */
1947 cp_parser_non_integral_constant_expression (cp_parser *parser,
1950 if (parser->integral_constant_expression_p)
1952 if (!parser->allow_non_integral_constant_expression_p)
1954 error ("%s cannot appear in a constant-expression", thing);
1957 parser->non_integral_constant_expression_p = true;
1962 /* Emit a diagnostic for an invalid type name. Consider also if it is
1963 qualified or not and the result of a lookup, to provide a better
1967 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1969 tree decl, old_scope;
1970 /* Try to lookup the identifier. */
1971 old_scope = parser->scope;
1972 parser->scope = scope;
1973 decl = cp_parser_lookup_name_simple (parser, id);
1974 parser->scope = old_scope;
1975 /* If the lookup found a template-name, it means that the user forgot
1976 to specify an argument list. Emit an useful error message. */
1977 if (TREE_CODE (decl) == TEMPLATE_DECL)
1978 error ("invalid use of template-name %qE without an argument list",
1980 else if (!parser->scope)
1982 /* Issue an error message. */
1983 error ("%qE does not name a type", id);
1984 /* If we're in a template class, it's possible that the user was
1985 referring to a type from a base class. For example:
1987 template <typename T> struct A { typedef T X; };
1988 template <typename T> struct B : public A<T> { X x; };
1990 The user should have said "typename A<T>::X". */
1991 if (processing_template_decl && current_class_type)
1995 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1999 tree base_type = BINFO_TYPE (b);
2000 if (CLASS_TYPE_P (base_type)
2001 && dependent_type_p (base_type))
2004 /* Go from a particular instantiation of the
2005 template (which will have an empty TYPE_FIELDs),
2006 to the main version. */
2007 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2008 for (field = TYPE_FIELDS (base_type);
2010 field = TREE_CHAIN (field))
2011 if (TREE_CODE (field) == TYPE_DECL
2012 && DECL_NAME (field) == id)
2014 inform ("(perhaps `typename %T::%E' was intended)",
2015 BINFO_TYPE (b), id);
2024 /* Here we diagnose qualified-ids where the scope is actually correct,
2025 but the identifier does not resolve to a valid type name. */
2028 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2029 error ("%qE in namespace %qE does not name a type",
2031 else if (TYPE_P (parser->scope))
2032 error ("q%E in class %qT does not name a type", id, parser->scope);
2038 /* Check for a common situation where a type-name should be present,
2039 but is not, and issue a sensible error message. Returns true if an
2040 invalid type-name was detected.
2042 The situation handled by this function are variable declarations of the
2043 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2044 Usually, `ID' should name a type, but if we got here it means that it
2045 does not. We try to emit the best possible error message depending on
2046 how exactly the id-expression looks like.
2050 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2054 cp_parser_parse_tentatively (parser);
2055 id = cp_parser_id_expression (parser,
2056 /*template_keyword_p=*/false,
2057 /*check_dependency_p=*/true,
2058 /*template_p=*/NULL,
2059 /*declarator_p=*/true);
2060 /* After the id-expression, there should be a plain identifier,
2061 otherwise this is not a simple variable declaration. Also, if
2062 the scope is dependent, we cannot do much. */
2063 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2064 || (parser->scope && TYPE_P (parser->scope)
2065 && dependent_type_p (parser->scope)))
2067 cp_parser_abort_tentative_parse (parser);
2070 if (!cp_parser_parse_definitely (parser)
2071 || TREE_CODE (id) != IDENTIFIER_NODE)
2074 /* Emit a diagnostic for the invalid type. */
2075 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2076 /* Skip to the end of the declaration; there's no point in
2077 trying to process it. */
2078 cp_parser_skip_to_end_of_block_or_statement (parser);
2082 /* Consume tokens up to, and including, the next non-nested closing `)'.
2083 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2084 are doing error recovery. Returns -1 if OR_COMMA is true and we
2085 found an unnested comma. */
2088 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2093 unsigned paren_depth = 0;
2094 unsigned brace_depth = 0;
2097 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2098 && !cp_parser_committed_to_tentative_parse (parser))
2105 /* If we've run out of tokens, then there is no closing `)'. */
2106 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2112 token = cp_lexer_peek_token (parser->lexer);
2114 /* This matches the processing in skip_to_end_of_statement. */
2115 if (token->type == CPP_SEMICOLON && !brace_depth)
2120 if (token->type == CPP_OPEN_BRACE)
2122 if (token->type == CPP_CLOSE_BRACE)
2130 if (recovering && or_comma && token->type == CPP_COMMA
2131 && !brace_depth && !paren_depth)
2139 /* If it is an `(', we have entered another level of nesting. */
2140 if (token->type == CPP_OPEN_PAREN)
2142 /* If it is a `)', then we might be done. */
2143 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2146 cp_lexer_consume_token (parser->lexer);
2154 /* Consume the token. */
2155 cp_lexer_consume_token (parser->lexer);
2161 /* Consume tokens until we reach the end of the current statement.
2162 Normally, that will be just before consuming a `;'. However, if a
2163 non-nested `}' comes first, then we stop before consuming that. */
2166 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2168 unsigned nesting_depth = 0;
2174 /* Peek at the next token. */
2175 token = cp_lexer_peek_token (parser->lexer);
2176 /* If we've run out of tokens, stop. */
2177 if (token->type == CPP_EOF)
2179 /* If the next token is a `;', we have reached the end of the
2181 if (token->type == CPP_SEMICOLON && !nesting_depth)
2183 /* If the next token is a non-nested `}', then we have reached
2184 the end of the current block. */
2185 if (token->type == CPP_CLOSE_BRACE)
2187 /* If this is a non-nested `}', stop before consuming it.
2188 That way, when confronted with something like:
2192 we stop before consuming the closing `}', even though we
2193 have not yet reached a `;'. */
2194 if (nesting_depth == 0)
2196 /* If it is the closing `}' for a block that we have
2197 scanned, stop -- but only after consuming the token.
2203 we will stop after the body of the erroneously declared
2204 function, but before consuming the following `typedef'
2206 if (--nesting_depth == 0)
2208 cp_lexer_consume_token (parser->lexer);
2212 /* If it the next token is a `{', then we are entering a new
2213 block. Consume the entire block. */
2214 else if (token->type == CPP_OPEN_BRACE)
2216 /* Consume the token. */
2217 cp_lexer_consume_token (parser->lexer);
2221 /* This function is called at the end of a statement or declaration.
2222 If the next token is a semicolon, it is consumed; otherwise, error
2223 recovery is attempted. */
2226 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2228 /* Look for the trailing `;'. */
2229 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2231 /* If there is additional (erroneous) input, skip to the end of
2233 cp_parser_skip_to_end_of_statement (parser);
2234 /* If the next token is now a `;', consume it. */
2235 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2236 cp_lexer_consume_token (parser->lexer);
2240 /* Skip tokens until we have consumed an entire block, or until we
2241 have consumed a non-nested `;'. */
2244 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2246 unsigned nesting_depth = 0;
2252 /* Peek at the next token. */
2253 token = cp_lexer_peek_token (parser->lexer);
2254 /* If we've run out of tokens, stop. */
2255 if (token->type == CPP_EOF)
2257 /* If the next token is a `;', we have reached the end of the
2259 if (token->type == CPP_SEMICOLON && !nesting_depth)
2261 /* Consume the `;'. */
2262 cp_lexer_consume_token (parser->lexer);
2265 /* Consume the token. */
2266 token = cp_lexer_consume_token (parser->lexer);
2267 /* If the next token is a non-nested `}', then we have reached
2268 the end of the current block. */
2269 if (token->type == CPP_CLOSE_BRACE
2270 && (nesting_depth == 0 || --nesting_depth == 0))
2272 /* If it the next token is a `{', then we are entering a new
2273 block. Consume the entire block. */
2274 if (token->type == CPP_OPEN_BRACE)
2279 /* Skip tokens until a non-nested closing curly brace is the next
2283 cp_parser_skip_to_closing_brace (cp_parser *parser)
2285 unsigned nesting_depth = 0;
2291 /* Peek at the next token. */
2292 token = cp_lexer_peek_token (parser->lexer);
2293 /* If we've run out of tokens, stop. */
2294 if (token->type == CPP_EOF)
2296 /* If the next token is a non-nested `}', then we have reached
2297 the end of the current block. */
2298 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2300 /* If it the next token is a `{', then we are entering a new
2301 block. Consume the entire block. */
2302 else if (token->type == CPP_OPEN_BRACE)
2304 /* Consume the token. */
2305 cp_lexer_consume_token (parser->lexer);
2309 /* This is a simple wrapper around make_typename_type. When the id is
2310 an unresolved identifier node, we can provide a superior diagnostic
2311 using cp_parser_diagnose_invalid_type_name. */
2314 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2317 if (TREE_CODE (id) == IDENTIFIER_NODE)
2319 result = make_typename_type (scope, id, /*complain=*/0);
2320 if (result == error_mark_node)
2321 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2324 return make_typename_type (scope, id, tf_error);
2328 /* Create a new C++ parser. */
2331 cp_parser_new (void)
2337 /* cp_lexer_new_main is called before calling ggc_alloc because
2338 cp_lexer_new_main might load a PCH file. */
2339 lexer = cp_lexer_new_main ();
2341 /* Initialize the binops_by_token so that we can get the tree
2342 directly from the token. */
2343 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2344 binops_by_token[binops[i].token_type] = binops[i];
2346 parser = GGC_CNEW (cp_parser);
2347 parser->lexer = lexer;
2348 parser->context = cp_parser_context_new (NULL);
2350 /* For now, we always accept GNU extensions. */
2351 parser->allow_gnu_extensions_p = 1;
2353 /* The `>' token is a greater-than operator, not the end of a
2355 parser->greater_than_is_operator_p = true;
2357 parser->default_arg_ok_p = true;
2359 /* We are not parsing a constant-expression. */
2360 parser->integral_constant_expression_p = false;
2361 parser->allow_non_integral_constant_expression_p = false;
2362 parser->non_integral_constant_expression_p = false;
2364 /* Local variable names are not forbidden. */
2365 parser->local_variables_forbidden_p = false;
2367 /* We are not processing an `extern "C"' declaration. */
2368 parser->in_unbraced_linkage_specification_p = false;
2370 /* We are not processing a declarator. */
2371 parser->in_declarator_p = false;
2373 /* We are not processing a template-argument-list. */
2374 parser->in_template_argument_list_p = false;
2376 /* We are not in an iteration statement. */
2377 parser->in_iteration_statement_p = false;
2379 /* We are not in a switch statement. */
2380 parser->in_switch_statement_p = false;
2382 /* We are not parsing a type-id inside an expression. */
2383 parser->in_type_id_in_expr_p = false;
2385 /* Declarations aren't implicitly extern "C". */
2386 parser->implicit_extern_c = false;
2388 /* String literals should be translated to the execution character set. */
2389 parser->translate_strings_p = true;
2391 /* The unparsed function queue is empty. */
2392 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2394 /* There are no classes being defined. */
2395 parser->num_classes_being_defined = 0;
2397 /* No template parameters apply. */
2398 parser->num_template_parameter_lists = 0;
2403 /* Create a cp_lexer structure which will emit the tokens in CACHE
2404 and push it onto the parser's lexer stack. This is used for delayed
2405 parsing of in-class method bodies and default arguments, and should
2406 not be confused with tentative parsing. */
2408 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2410 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2411 lexer->next = parser->lexer;
2412 parser->lexer = lexer;
2414 /* Move the current source position to that of the first token in the
2416 cp_lexer_set_source_position_from_token (lexer->next_token);
2419 /* Pop the top lexer off the parser stack. This is never used for the
2420 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2422 cp_parser_pop_lexer (cp_parser *parser)
2424 cp_lexer *lexer = parser->lexer;
2425 parser->lexer = lexer->next;
2426 cp_lexer_destroy (lexer);
2428 /* Put the current source position back where it was before this
2429 lexer was pushed. */
2430 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2433 /* Lexical conventions [gram.lex] */
2435 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2439 cp_parser_identifier (cp_parser* parser)
2443 /* Look for the identifier. */
2444 token = cp_parser_require (parser, CPP_NAME, "identifier");
2445 /* Return the value. */
2446 return token ? token->value : error_mark_node;
2449 /* Parse a sequence of adjacent string constants. Returns a
2450 TREE_STRING representing the combined, nul-terminated string
2451 constant. If TRANSLATE is true, translate the string to the
2452 execution character set. If WIDE_OK is true, a wide string is
2455 C++98 [lex.string] says that if a narrow string literal token is
2456 adjacent to a wide string literal token, the behavior is undefined.
2457 However, C99 6.4.5p4 says that this results in a wide string literal.
2458 We follow C99 here, for consistency with the C front end.
2460 This code is largely lifted from lex_string() in c-lex.c.
2462 FUTURE: ObjC++ will need to handle @-strings here. */
2464 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2469 struct obstack str_ob;
2470 cpp_string str, istr, *strs;
2473 tok = cp_lexer_peek_token (parser->lexer);
2474 if (!cp_parser_is_string_literal (tok))
2476 cp_parser_error (parser, "expected string-literal");
2477 return error_mark_node;
2480 /* Try to avoid the overhead of creating and destroying an obstack
2481 for the common case of just one string. */
2482 if (!cp_parser_is_string_literal
2483 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2485 cp_lexer_consume_token (parser->lexer);
2487 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2488 str.len = TREE_STRING_LENGTH (tok->value);
2490 if (tok->type == CPP_WSTRING)
2497 gcc_obstack_init (&str_ob);
2502 cp_lexer_consume_token (parser->lexer);
2504 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2505 str.len = TREE_STRING_LENGTH (tok->value);
2506 if (tok->type == CPP_WSTRING)
2509 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2511 tok = cp_lexer_peek_token (parser->lexer);
2513 while (cp_parser_is_string_literal (tok));
2515 strs = (cpp_string *) obstack_finish (&str_ob);
2518 if (wide && !wide_ok)
2520 cp_parser_error (parser, "a wide string is invalid in this context");
2524 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2525 (parse_in, strs, count, &istr, wide))
2527 value = build_string (istr.len, (char *)istr.text);
2528 free ((void *)istr.text);
2530 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2531 value = fix_string_type (value);
2534 /* cpp_interpret_string has issued an error. */
2535 value = error_mark_node;
2538 obstack_free (&str_ob, 0);
2544 /* Basic concepts [gram.basic] */
2546 /* Parse a translation-unit.
2549 declaration-seq [opt]
2551 Returns TRUE if all went well. */
2554 cp_parser_translation_unit (cp_parser* parser)
2556 /* The address of the first non-permanent object on the declarator
2558 static void *declarator_obstack_base;
2562 /* Create the declarator obstack, if necessary. */
2563 if (!cp_error_declarator)
2565 gcc_obstack_init (&declarator_obstack);
2566 /* Create the error declarator. */
2567 cp_error_declarator = make_declarator (cdk_error);
2568 /* Create the empty parameter list. */
2569 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2570 /* Remember where the base of the declarator obstack lies. */
2571 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2576 cp_parser_declaration_seq_opt (parser);
2578 /* If there are no tokens left then all went well. */
2579 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2581 /* Get rid of the token array; we don't need it any more. */
2582 cp_lexer_destroy (parser->lexer);
2583 parser->lexer = NULL;
2585 /* This file might have been a context that's implicitly extern
2586 "C". If so, pop the lang context. (Only relevant for PCH.) */
2587 if (parser->implicit_extern_c)
2589 pop_lang_context ();
2590 parser->implicit_extern_c = false;
2594 finish_translation_unit ();
2601 cp_parser_error (parser, "expected declaration");
2607 /* Make sure the declarator obstack was fully cleaned up. */
2608 gcc_assert (obstack_next_free (&declarator_obstack)
2609 == declarator_obstack_base);
2611 /* All went well. */
2615 /* Expressions [gram.expr] */
2617 /* Parse a primary-expression.
2628 ( compound-statement )
2629 __builtin_va_arg ( assignment-expression , type-id )
2634 Returns a representation of the expression.
2636 *IDK indicates what kind of id-expression (if any) was present.
2638 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2639 used as the operand of a pointer-to-member. In that case,
2640 *QUALIFYING_CLASS gives the class that is used as the qualifying
2641 class in the pointer-to-member. */
2644 cp_parser_primary_expression (cp_parser *parser,
2646 tree *qualifying_class)
2650 /* Assume the primary expression is not an id-expression. */
2651 *idk = CP_ID_KIND_NONE;
2652 /* And that it cannot be used as pointer-to-member. */
2653 *qualifying_class = NULL_TREE;
2655 /* Peek at the next token. */
2656 token = cp_lexer_peek_token (parser->lexer);
2657 switch (token->type)
2668 token = cp_lexer_consume_token (parser->lexer);
2669 return token->value;
2673 /* ??? Should wide strings be allowed when parser->translate_strings_p
2674 is false (i.e. in attributes)? If not, we can kill the third
2675 argument to cp_parser_string_literal. */
2676 return cp_parser_string_literal (parser,
2677 parser->translate_strings_p,
2680 case CPP_OPEN_PAREN:
2683 bool saved_greater_than_is_operator_p;
2685 /* Consume the `('. */
2686 cp_lexer_consume_token (parser->lexer);
2687 /* Within a parenthesized expression, a `>' token is always
2688 the greater-than operator. */
2689 saved_greater_than_is_operator_p
2690 = parser->greater_than_is_operator_p;
2691 parser->greater_than_is_operator_p = true;
2692 /* If we see `( { ' then we are looking at the beginning of
2693 a GNU statement-expression. */
2694 if (cp_parser_allow_gnu_extensions_p (parser)
2695 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2697 /* Statement-expressions are not allowed by the standard. */
2699 pedwarn ("ISO C++ forbids braced-groups within expressions");
2701 /* And they're not allowed outside of a function-body; you
2702 cannot, for example, write:
2704 int i = ({ int j = 3; j + 1; });
2706 at class or namespace scope. */
2707 if (!at_function_scope_p ())
2708 error ("statement-expressions are allowed only inside functions");
2709 /* Start the statement-expression. */
2710 expr = begin_stmt_expr ();
2711 /* Parse the compound-statement. */
2712 cp_parser_compound_statement (parser, expr, false);
2714 expr = finish_stmt_expr (expr, false);
2718 /* Parse the parenthesized expression. */
2719 expr = cp_parser_expression (parser);
2720 /* Let the front end know that this expression was
2721 enclosed in parentheses. This matters in case, for
2722 example, the expression is of the form `A::B', since
2723 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2725 finish_parenthesized_expr (expr);
2727 /* The `>' token might be the end of a template-id or
2728 template-parameter-list now. */
2729 parser->greater_than_is_operator_p
2730 = saved_greater_than_is_operator_p;
2731 /* Consume the `)'. */
2732 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2733 cp_parser_skip_to_end_of_statement (parser);
2739 switch (token->keyword)
2741 /* These two are the boolean literals. */
2743 cp_lexer_consume_token (parser->lexer);
2744 return boolean_true_node;
2746 cp_lexer_consume_token (parser->lexer);
2747 return boolean_false_node;
2749 /* The `__null' literal. */
2751 cp_lexer_consume_token (parser->lexer);
2754 /* Recognize the `this' keyword. */
2756 cp_lexer_consume_token (parser->lexer);
2757 if (parser->local_variables_forbidden_p)
2759 error ("%<this%> may not be used in this context");
2760 return error_mark_node;
2762 /* Pointers cannot appear in constant-expressions. */
2763 if (cp_parser_non_integral_constant_expression (parser,
2765 return error_mark_node;
2766 return finish_this_expr ();
2768 /* The `operator' keyword can be the beginning of an
2773 case RID_FUNCTION_NAME:
2774 case RID_PRETTY_FUNCTION_NAME:
2775 case RID_C99_FUNCTION_NAME:
2776 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2777 __func__ are the names of variables -- but they are
2778 treated specially. Therefore, they are handled here,
2779 rather than relying on the generic id-expression logic
2780 below. Grammatically, these names are id-expressions.
2782 Consume the token. */
2783 token = cp_lexer_consume_token (parser->lexer);
2784 /* Look up the name. */
2785 return finish_fname (token->value);
2792 /* The `__builtin_va_arg' construct is used to handle
2793 `va_arg'. Consume the `__builtin_va_arg' token. */
2794 cp_lexer_consume_token (parser->lexer);
2795 /* Look for the opening `('. */
2796 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2797 /* Now, parse the assignment-expression. */
2798 expression = cp_parser_assignment_expression (parser);
2799 /* Look for the `,'. */
2800 cp_parser_require (parser, CPP_COMMA, "`,'");
2801 /* Parse the type-id. */
2802 type = cp_parser_type_id (parser);
2803 /* Look for the closing `)'. */
2804 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2805 /* Using `va_arg' in a constant-expression is not
2807 if (cp_parser_non_integral_constant_expression (parser,
2809 return error_mark_node;
2810 return build_x_va_arg (expression, type);
2814 return cp_parser_builtin_offsetof (parser);
2817 cp_parser_error (parser, "expected primary-expression");
2818 return error_mark_node;
2821 /* An id-expression can start with either an identifier, a
2822 `::' as the beginning of a qualified-id, or the "operator"
2826 case CPP_TEMPLATE_ID:
2827 case CPP_NESTED_NAME_SPECIFIER:
2831 const char *error_msg;
2834 /* Parse the id-expression. */
2836 = cp_parser_id_expression (parser,
2837 /*template_keyword_p=*/false,
2838 /*check_dependency_p=*/true,
2839 /*template_p=*/NULL,
2840 /*declarator_p=*/false);
2841 if (id_expression == error_mark_node)
2842 return error_mark_node;
2843 /* If we have a template-id, then no further lookup is
2844 required. If the template-id was for a template-class, we
2845 will sometimes have a TYPE_DECL at this point. */
2846 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2847 || TREE_CODE (id_expression) == TYPE_DECL)
2848 decl = id_expression;
2849 /* Look up the name. */
2854 decl = cp_parser_lookup_name (parser, id_expression,
2856 /*is_template=*/false,
2857 /*is_namespace=*/false,
2858 /*check_dependency=*/true,
2860 /* If the lookup was ambiguous, an error will already have
2863 return error_mark_node;
2864 /* If name lookup gives us a SCOPE_REF, then the
2865 qualifying scope was dependent. Just propagate the
2867 if (TREE_CODE (decl) == SCOPE_REF)
2869 if (TYPE_P (TREE_OPERAND (decl, 0)))
2870 *qualifying_class = TREE_OPERAND (decl, 0);
2873 /* Check to see if DECL is a local variable in a context
2874 where that is forbidden. */
2875 if (parser->local_variables_forbidden_p
2876 && local_variable_p (decl))
2878 /* It might be that we only found DECL because we are
2879 trying to be generous with pre-ISO scoping rules.
2880 For example, consider:
2884 for (int i = 0; i < 10; ++i) {}
2885 extern void f(int j = i);
2888 Here, name look up will originally find the out
2889 of scope `i'. We need to issue a warning message,
2890 but then use the global `i'. */
2891 decl = check_for_out_of_scope_variable (decl);
2892 if (local_variable_p (decl))
2894 error ("local variable %qD may not appear in this context",
2896 return error_mark_node;
2901 decl = finish_id_expression (id_expression, decl, parser->scope,
2902 idk, qualifying_class,
2903 parser->integral_constant_expression_p,
2904 parser->allow_non_integral_constant_expression_p,
2905 &parser->non_integral_constant_expression_p,
2908 cp_parser_error (parser, error_msg);
2912 /* Anything else is an error. */
2914 cp_parser_error (parser, "expected primary-expression");
2915 return error_mark_node;
2919 /* Parse an id-expression.
2926 :: [opt] nested-name-specifier template [opt] unqualified-id
2928 :: operator-function-id
2931 Return a representation of the unqualified portion of the
2932 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2933 a `::' or nested-name-specifier.
2935 Often, if the id-expression was a qualified-id, the caller will
2936 want to make a SCOPE_REF to represent the qualified-id. This
2937 function does not do this in order to avoid wastefully creating
2938 SCOPE_REFs when they are not required.
2940 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2943 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2944 uninstantiated templates.
2946 If *TEMPLATE_P is non-NULL, it is set to true iff the
2947 `template' keyword is used to explicitly indicate that the entity
2948 named is a template.
2950 If DECLARATOR_P is true, the id-expression is appearing as part of
2951 a declarator, rather than as part of an expression. */
2954 cp_parser_id_expression (cp_parser *parser,
2955 bool template_keyword_p,
2956 bool check_dependency_p,
2960 bool global_scope_p;
2961 bool nested_name_specifier_p;
2963 /* Assume the `template' keyword was not used. */
2965 *template_p = false;
2967 /* Look for the optional `::' operator. */
2969 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2971 /* Look for the optional nested-name-specifier. */
2972 nested_name_specifier_p
2973 = (cp_parser_nested_name_specifier_opt (parser,
2974 /*typename_keyword_p=*/false,
2979 /* If there is a nested-name-specifier, then we are looking at
2980 the first qualified-id production. */
2981 if (nested_name_specifier_p)
2984 tree saved_object_scope;
2985 tree saved_qualifying_scope;
2986 tree unqualified_id;
2989 /* See if the next token is the `template' keyword. */
2991 template_p = &is_template;
2992 *template_p = cp_parser_optional_template_keyword (parser);
2993 /* Name lookup we do during the processing of the
2994 unqualified-id might obliterate SCOPE. */
2995 saved_scope = parser->scope;
2996 saved_object_scope = parser->object_scope;
2997 saved_qualifying_scope = parser->qualifying_scope;
2998 /* Process the final unqualified-id. */
2999 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3002 /* Restore the SAVED_SCOPE for our caller. */
3003 parser->scope = saved_scope;
3004 parser->object_scope = saved_object_scope;
3005 parser->qualifying_scope = saved_qualifying_scope;
3007 return unqualified_id;
3009 /* Otherwise, if we are in global scope, then we are looking at one
3010 of the other qualified-id productions. */
3011 else if (global_scope_p)
3016 /* Peek at the next token. */
3017 token = cp_lexer_peek_token (parser->lexer);
3019 /* If it's an identifier, and the next token is not a "<", then
3020 we can avoid the template-id case. This is an optimization
3021 for this common case. */
3022 if (token->type == CPP_NAME
3023 && !cp_parser_nth_token_starts_template_argument_list_p
3025 return cp_parser_identifier (parser);
3027 cp_parser_parse_tentatively (parser);
3028 /* Try a template-id. */
3029 id = cp_parser_template_id (parser,
3030 /*template_keyword_p=*/false,
3031 /*check_dependency_p=*/true,
3033 /* If that worked, we're done. */
3034 if (cp_parser_parse_definitely (parser))
3037 /* Peek at the next token. (Changes in the token buffer may
3038 have invalidated the pointer obtained above.) */
3039 token = cp_lexer_peek_token (parser->lexer);
3041 switch (token->type)
3044 return cp_parser_identifier (parser);
3047 if (token->keyword == RID_OPERATOR)
3048 return cp_parser_operator_function_id (parser);
3052 cp_parser_error (parser, "expected id-expression");
3053 return error_mark_node;
3057 return cp_parser_unqualified_id (parser, template_keyword_p,
3058 /*check_dependency_p=*/true,
3062 /* Parse an unqualified-id.
3066 operator-function-id
3067 conversion-function-id
3071 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3072 keyword, in a construct like `A::template ...'.
3074 Returns a representation of unqualified-id. For the `identifier'
3075 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3076 production a BIT_NOT_EXPR is returned; the operand of the
3077 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3078 other productions, see the documentation accompanying the
3079 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3080 names are looked up in uninstantiated templates. If DECLARATOR_P
3081 is true, the unqualified-id is appearing as part of a declarator,
3082 rather than as part of an expression. */
3085 cp_parser_unqualified_id (cp_parser* parser,
3086 bool template_keyword_p,
3087 bool check_dependency_p,
3092 /* Peek at the next token. */
3093 token = cp_lexer_peek_token (parser->lexer);
3095 switch (token->type)
3101 /* We don't know yet whether or not this will be a
3103 cp_parser_parse_tentatively (parser);
3104 /* Try a template-id. */
3105 id = cp_parser_template_id (parser, template_keyword_p,
3108 /* If it worked, we're done. */
3109 if (cp_parser_parse_definitely (parser))
3111 /* Otherwise, it's an ordinary identifier. */
3112 return cp_parser_identifier (parser);
3115 case CPP_TEMPLATE_ID:
3116 return cp_parser_template_id (parser, template_keyword_p,
3123 tree qualifying_scope;
3127 /* Consume the `~' token. */
3128 cp_lexer_consume_token (parser->lexer);
3129 /* Parse the class-name. The standard, as written, seems to
3132 template <typename T> struct S { ~S (); };
3133 template <typename T> S<T>::~S() {}
3135 is invalid, since `~' must be followed by a class-name, but
3136 `S<T>' is dependent, and so not known to be a class.
3137 That's not right; we need to look in uninstantiated
3138 templates. A further complication arises from:
3140 template <typename T> void f(T t) {
3144 Here, it is not possible to look up `T' in the scope of `T'
3145 itself. We must look in both the current scope, and the
3146 scope of the containing complete expression.
3148 Yet another issue is:
3157 The standard does not seem to say that the `S' in `~S'
3158 should refer to the type `S' and not the data member
3161 /* DR 244 says that we look up the name after the "~" in the
3162 same scope as we looked up the qualifying name. That idea
3163 isn't fully worked out; it's more complicated than that. */
3164 scope = parser->scope;
3165 object_scope = parser->object_scope;
3166 qualifying_scope = parser->qualifying_scope;
3168 /* If the name is of the form "X::~X" it's OK. */
3169 if (scope && TYPE_P (scope)
3170 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3171 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3173 && (cp_lexer_peek_token (parser->lexer)->value
3174 == TYPE_IDENTIFIER (scope)))
3176 cp_lexer_consume_token (parser->lexer);
3177 return build_nt (BIT_NOT_EXPR, scope);
3180 /* If there was an explicit qualification (S::~T), first look
3181 in the scope given by the qualification (i.e., S). */
3184 cp_parser_parse_tentatively (parser);
3185 type_decl = cp_parser_class_name (parser,
3186 /*typename_keyword_p=*/false,
3187 /*template_keyword_p=*/false,
3189 /*check_dependency=*/false,
3190 /*class_head_p=*/false,
3192 if (cp_parser_parse_definitely (parser))
3193 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3195 /* In "N::S::~S", look in "N" as well. */
3196 if (scope && qualifying_scope)
3198 cp_parser_parse_tentatively (parser);
3199 parser->scope = qualifying_scope;
3200 parser->object_scope = NULL_TREE;
3201 parser->qualifying_scope = NULL_TREE;
3203 = cp_parser_class_name (parser,
3204 /*typename_keyword_p=*/false,
3205 /*template_keyword_p=*/false,
3207 /*check_dependency=*/false,
3208 /*class_head_p=*/false,
3210 if (cp_parser_parse_definitely (parser))
3211 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3213 /* In "p->S::~T", look in the scope given by "*p" as well. */
3214 else if (object_scope)
3216 cp_parser_parse_tentatively (parser);
3217 parser->scope = object_scope;
3218 parser->object_scope = NULL_TREE;
3219 parser->qualifying_scope = NULL_TREE;
3221 = cp_parser_class_name (parser,
3222 /*typename_keyword_p=*/false,
3223 /*template_keyword_p=*/false,
3225 /*check_dependency=*/false,
3226 /*class_head_p=*/false,
3228 if (cp_parser_parse_definitely (parser))
3229 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3231 /* Look in the surrounding context. */
3232 parser->scope = NULL_TREE;
3233 parser->object_scope = NULL_TREE;
3234 parser->qualifying_scope = NULL_TREE;
3236 = cp_parser_class_name (parser,
3237 /*typename_keyword_p=*/false,
3238 /*template_keyword_p=*/false,
3240 /*check_dependency=*/false,
3241 /*class_head_p=*/false,
3243 /* If an error occurred, assume that the name of the
3244 destructor is the same as the name of the qualifying
3245 class. That allows us to keep parsing after running
3246 into ill-formed destructor names. */
3247 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3248 return build_nt (BIT_NOT_EXPR, scope);
3249 else if (type_decl == error_mark_node)
3250 return error_mark_node;
3254 A typedef-name that names a class shall not be used as the
3255 identifier in the declarator for a destructor declaration. */
3257 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3258 && !DECL_SELF_REFERENCE_P (type_decl))
3259 error ("typedef-name %qD used as destructor declarator",
3262 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3266 if (token->keyword == RID_OPERATOR)
3270 /* This could be a template-id, so we try that first. */
3271 cp_parser_parse_tentatively (parser);
3272 /* Try a template-id. */
3273 id = cp_parser_template_id (parser, template_keyword_p,
3274 /*check_dependency_p=*/true,
3276 /* If that worked, we're done. */
3277 if (cp_parser_parse_definitely (parser))
3279 /* We still don't know whether we're looking at an
3280 operator-function-id or a conversion-function-id. */
3281 cp_parser_parse_tentatively (parser);
3282 /* Try an operator-function-id. */
3283 id = cp_parser_operator_function_id (parser);
3284 /* If that didn't work, try a conversion-function-id. */
3285 if (!cp_parser_parse_definitely (parser))
3286 id = cp_parser_conversion_function_id (parser);
3293 cp_parser_error (parser, "expected unqualified-id");
3294 return error_mark_node;
3298 /* Parse an (optional) nested-name-specifier.
3300 nested-name-specifier:
3301 class-or-namespace-name :: nested-name-specifier [opt]
3302 class-or-namespace-name :: template nested-name-specifier [opt]
3304 PARSER->SCOPE should be set appropriately before this function is
3305 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3306 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3309 Sets PARSER->SCOPE to the class (TYPE) or namespace
3310 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3311 it unchanged if there is no nested-name-specifier. Returns the new
3312 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3314 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3315 part of a declaration and/or decl-specifier. */
3318 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3319 bool typename_keyword_p,
3320 bool check_dependency_p,
3322 bool is_declaration)
3324 bool success = false;
3325 tree access_check = NULL_TREE;
3326 cp_token_position start = 0;
3329 /* If the next token corresponds to a nested name specifier, there
3330 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3331 false, it may have been true before, in which case something
3332 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3333 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3334 CHECK_DEPENDENCY_P is false, we have to fall through into the
3336 if (check_dependency_p
3337 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3339 cp_parser_pre_parsed_nested_name_specifier (parser);
3340 return parser->scope;
3343 /* Remember where the nested-name-specifier starts. */
3344 if (cp_parser_parsing_tentatively (parser)
3345 && !cp_parser_committed_to_tentative_parse (parser))
3346 start = cp_lexer_token_position (parser->lexer, false);
3348 push_deferring_access_checks (dk_deferred);
3354 tree saved_qualifying_scope;
3355 bool template_keyword_p;
3357 /* Spot cases that cannot be the beginning of a
3358 nested-name-specifier. */
3359 token = cp_lexer_peek_token (parser->lexer);
3361 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3362 the already parsed nested-name-specifier. */
3363 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3365 /* Grab the nested-name-specifier and continue the loop. */
3366 cp_parser_pre_parsed_nested_name_specifier (parser);
3371 /* Spot cases that cannot be the beginning of a
3372 nested-name-specifier. On the second and subsequent times
3373 through the loop, we look for the `template' keyword. */
3374 if (success && token->keyword == RID_TEMPLATE)
3376 /* A template-id can start a nested-name-specifier. */
3377 else if (token->type == CPP_TEMPLATE_ID)
3381 /* If the next token is not an identifier, then it is
3382 definitely not a class-or-namespace-name. */
3383 if (token->type != CPP_NAME)
3385 /* If the following token is neither a `<' (to begin a
3386 template-id), nor a `::', then we are not looking at a
3387 nested-name-specifier. */
3388 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3389 if (token->type != CPP_SCOPE
3390 && !cp_parser_nth_token_starts_template_argument_list_p
3395 /* The nested-name-specifier is optional, so we parse
3397 cp_parser_parse_tentatively (parser);
3399 /* Look for the optional `template' keyword, if this isn't the
3400 first time through the loop. */
3402 template_keyword_p = cp_parser_optional_template_keyword (parser);
3404 template_keyword_p = false;
3406 /* Save the old scope since the name lookup we are about to do
3407 might destroy it. */
3408 old_scope = parser->scope;
3409 saved_qualifying_scope = parser->qualifying_scope;
3410 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3411 look up names in "X<T>::I" in order to determine that "Y" is
3412 a template. So, if we have a typename at this point, we make
3413 an effort to look through it. */
3415 && !typename_keyword_p
3417 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3418 parser->scope = resolve_typename_type (parser->scope,
3419 /*only_current_p=*/false);
3420 /* Parse the qualifying entity. */
3422 = cp_parser_class_or_namespace_name (parser,
3428 /* Look for the `::' token. */
3429 cp_parser_require (parser, CPP_SCOPE, "`::'");
3431 /* If we found what we wanted, we keep going; otherwise, we're
3433 if (!cp_parser_parse_definitely (parser))
3435 bool error_p = false;
3437 /* Restore the OLD_SCOPE since it was valid before the
3438 failed attempt at finding the last
3439 class-or-namespace-name. */
3440 parser->scope = old_scope;
3441 parser->qualifying_scope = saved_qualifying_scope;
3442 /* If the next token is an identifier, and the one after
3443 that is a `::', then any valid interpretation would have
3444 found a class-or-namespace-name. */
3445 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3446 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3448 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3451 token = cp_lexer_consume_token (parser->lexer);
3456 decl = cp_parser_lookup_name_simple (parser, token->value);
3457 if (TREE_CODE (decl) == TEMPLATE_DECL)
3458 error ("%qD used without template parameters", decl);
3460 cp_parser_name_lookup_error
3461 (parser, token->value, decl,
3462 "is not a class or namespace");
3463 parser->scope = NULL_TREE;
3465 /* Treat this as a successful nested-name-specifier
3470 If the name found is not a class-name (clause
3471 _class_) or namespace-name (_namespace.def_), the
3472 program is ill-formed. */
3475 cp_lexer_consume_token (parser->lexer);
3480 /* We've found one valid nested-name-specifier. */
3482 /* Make sure we look in the right scope the next time through
3484 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3485 ? TREE_TYPE (new_scope)
3487 /* If it is a class scope, try to complete it; we are about to
3488 be looking up names inside the class. */
3489 if (TYPE_P (parser->scope)
3490 /* Since checking types for dependency can be expensive,
3491 avoid doing it if the type is already complete. */
3492 && !COMPLETE_TYPE_P (parser->scope)
3493 /* Do not try to complete dependent types. */
3494 && !dependent_type_p (parser->scope))
3495 complete_type (parser->scope);
3498 /* Retrieve any deferred checks. Do not pop this access checks yet
3499 so the memory will not be reclaimed during token replacing below. */
3500 access_check = get_deferred_access_checks ();
3502 /* If parsing tentatively, replace the sequence of tokens that makes
3503 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3504 token. That way, should we re-parse the token stream, we will
3505 not have to repeat the effort required to do the parse, nor will
3506 we issue duplicate error messages. */
3507 if (success && start)
3509 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3511 /* Reset the contents of the START token. */
3512 token->type = CPP_NESTED_NAME_SPECIFIER;
3513 token->value = build_tree_list (access_check, parser->scope);
3514 TREE_TYPE (token->value) = parser->qualifying_scope;
3515 token->keyword = RID_MAX;
3517 /* Purge all subsequent tokens. */
3518 cp_lexer_purge_tokens_after (parser->lexer, start);
3521 pop_deferring_access_checks ();
3522 return success ? parser->scope : NULL_TREE;
3525 /* Parse a nested-name-specifier. See
3526 cp_parser_nested_name_specifier_opt for details. This function
3527 behaves identically, except that it will an issue an error if no
3528 nested-name-specifier is present, and it will return
3529 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3533 cp_parser_nested_name_specifier (cp_parser *parser,
3534 bool typename_keyword_p,
3535 bool check_dependency_p,
3537 bool is_declaration)
3541 /* Look for the nested-name-specifier. */
3542 scope = cp_parser_nested_name_specifier_opt (parser,
3547 /* If it was not present, issue an error message. */
3550 cp_parser_error (parser, "expected nested-name-specifier");
3551 parser->scope = NULL_TREE;
3552 return error_mark_node;
3558 /* Parse a class-or-namespace-name.
3560 class-or-namespace-name:
3564 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3565 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3566 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3567 TYPE_P is TRUE iff the next name should be taken as a class-name,
3568 even the same name is declared to be another entity in the same
3571 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3572 specified by the class-or-namespace-name. If neither is found the
3573 ERROR_MARK_NODE is returned. */
3576 cp_parser_class_or_namespace_name (cp_parser *parser,
3577 bool typename_keyword_p,
3578 bool template_keyword_p,
3579 bool check_dependency_p,
3581 bool is_declaration)
3584 tree saved_qualifying_scope;
3585 tree saved_object_scope;
3589 /* Before we try to parse the class-name, we must save away the
3590 current PARSER->SCOPE since cp_parser_class_name will destroy
3592 saved_scope = parser->scope;
3593 saved_qualifying_scope = parser->qualifying_scope;
3594 saved_object_scope = parser->object_scope;
3595 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3596 there is no need to look for a namespace-name. */
3597 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3599 cp_parser_parse_tentatively (parser);
3600 scope = cp_parser_class_name (parser,
3605 /*class_head_p=*/false,
3607 /* If that didn't work, try for a namespace-name. */
3608 if (!only_class_p && !cp_parser_parse_definitely (parser))
3610 /* Restore the saved scope. */
3611 parser->scope = saved_scope;
3612 parser->qualifying_scope = saved_qualifying_scope;
3613 parser->object_scope = saved_object_scope;
3614 /* If we are not looking at an identifier followed by the scope
3615 resolution operator, then this is not part of a
3616 nested-name-specifier. (Note that this function is only used
3617 to parse the components of a nested-name-specifier.) */
3618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3619 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3620 return error_mark_node;
3621 scope = cp_parser_namespace_name (parser);
3627 /* Parse a postfix-expression.
3631 postfix-expression [ expression ]
3632 postfix-expression ( expression-list [opt] )
3633 simple-type-specifier ( expression-list [opt] )
3634 typename :: [opt] nested-name-specifier identifier
3635 ( expression-list [opt] )
3636 typename :: [opt] nested-name-specifier template [opt] template-id
3637 ( expression-list [opt] )
3638 postfix-expression . template [opt] id-expression
3639 postfix-expression -> template [opt] id-expression
3640 postfix-expression . pseudo-destructor-name
3641 postfix-expression -> pseudo-destructor-name
3642 postfix-expression ++
3643 postfix-expression --
3644 dynamic_cast < type-id > ( expression )
3645 static_cast < type-id > ( expression )
3646 reinterpret_cast < type-id > ( expression )
3647 const_cast < type-id > ( expression )
3648 typeid ( expression )
3654 ( type-id ) { initializer-list , [opt] }
3656 This extension is a GNU version of the C99 compound-literal
3657 construct. (The C99 grammar uses `type-name' instead of `type-id',
3658 but they are essentially the same concept.)
3660 If ADDRESS_P is true, the postfix expression is the operand of the
3663 Returns a representation of the expression. */
3666 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3670 cp_id_kind idk = CP_ID_KIND_NONE;
3671 tree postfix_expression = NULL_TREE;
3672 /* Non-NULL only if the current postfix-expression can be used to
3673 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3674 class used to qualify the member. */
3675 tree qualifying_class = NULL_TREE;
3677 /* Peek at the next token. */
3678 token = cp_lexer_peek_token (parser->lexer);
3679 /* Some of the productions are determined by keywords. */
3680 keyword = token->keyword;
3690 const char *saved_message;
3692 /* All of these can be handled in the same way from the point
3693 of view of parsing. Begin by consuming the token
3694 identifying the cast. */
3695 cp_lexer_consume_token (parser->lexer);
3697 /* New types cannot be defined in the cast. */
3698 saved_message = parser->type_definition_forbidden_message;
3699 parser->type_definition_forbidden_message
3700 = "types may not be defined in casts";
3702 /* Look for the opening `<'. */
3703 cp_parser_require (parser, CPP_LESS, "`<'");
3704 /* Parse the type to which we are casting. */
3705 type = cp_parser_type_id (parser);
3706 /* Look for the closing `>'. */
3707 cp_parser_require (parser, CPP_GREATER, "`>'");
3708 /* Restore the old message. */
3709 parser->type_definition_forbidden_message = saved_message;
3711 /* And the expression which is being cast. */
3712 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3713 expression = cp_parser_expression (parser);
3714 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3716 /* Only type conversions to integral or enumeration types
3717 can be used in constant-expressions. */
3718 if (parser->integral_constant_expression_p
3719 && !dependent_type_p (type)
3720 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3721 && (cp_parser_non_integral_constant_expression
3723 "a cast to a type other than an integral or "
3724 "enumeration type")))
3725 return error_mark_node;
3731 = build_dynamic_cast (type, expression);
3735 = build_static_cast (type, expression);
3739 = build_reinterpret_cast (type, expression);
3743 = build_const_cast (type, expression);
3754 const char *saved_message;
3755 bool saved_in_type_id_in_expr_p;
3757 /* Consume the `typeid' token. */
3758 cp_lexer_consume_token (parser->lexer);
3759 /* Look for the `(' token. */
3760 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3761 /* Types cannot be defined in a `typeid' expression. */
3762 saved_message = parser->type_definition_forbidden_message;
3763 parser->type_definition_forbidden_message
3764 = "types may not be defined in a `typeid\' expression";
3765 /* We can't be sure yet whether we're looking at a type-id or an
3767 cp_parser_parse_tentatively (parser);
3768 /* Try a type-id first. */
3769 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3770 parser->in_type_id_in_expr_p = true;
3771 type = cp_parser_type_id (parser);
3772 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3773 /* Look for the `)' token. Otherwise, we can't be sure that
3774 we're not looking at an expression: consider `typeid (int
3775 (3))', for example. */
3776 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3777 /* If all went well, simply lookup the type-id. */
3778 if (cp_parser_parse_definitely (parser))
3779 postfix_expression = get_typeid (type);
3780 /* Otherwise, fall back to the expression variant. */
3785 /* Look for an expression. */
3786 expression = cp_parser_expression (parser);
3787 /* Compute its typeid. */
3788 postfix_expression = build_typeid (expression);
3789 /* Look for the `)' token. */
3790 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3792 /* `typeid' may not appear in an integral constant expression. */
3793 if (cp_parser_non_integral_constant_expression(parser,
3794 "`typeid' operator"))
3795 return error_mark_node;
3796 /* Restore the saved message. */
3797 parser->type_definition_forbidden_message = saved_message;
3803 bool template_p = false;
3807 /* Consume the `typename' token. */
3808 cp_lexer_consume_token (parser->lexer);
3809 /* Look for the optional `::' operator. */
3810 cp_parser_global_scope_opt (parser,
3811 /*current_scope_valid_p=*/false);
3812 /* Look for the nested-name-specifier. */
3813 cp_parser_nested_name_specifier (parser,
3814 /*typename_keyword_p=*/true,
3815 /*check_dependency_p=*/true,
3817 /*is_declaration=*/true);
3818 /* Look for the optional `template' keyword. */
3819 template_p = cp_parser_optional_template_keyword (parser);
3820 /* We don't know whether we're looking at a template-id or an
3822 cp_parser_parse_tentatively (parser);
3823 /* Try a template-id. */
3824 id = cp_parser_template_id (parser, template_p,
3825 /*check_dependency_p=*/true,
3826 /*is_declaration=*/true);
3827 /* If that didn't work, try an identifier. */
3828 if (!cp_parser_parse_definitely (parser))
3829 id = cp_parser_identifier (parser);
3830 /* If we look up a template-id in a non-dependent qualifying
3831 scope, there's no need to create a dependent type. */
3832 if (TREE_CODE (id) == TYPE_DECL
3833 && !dependent_type_p (parser->scope))
3834 type = TREE_TYPE (id);
3835 /* Create a TYPENAME_TYPE to represent the type to which the
3836 functional cast is being performed. */
3838 type = make_typename_type (parser->scope, id,
3841 postfix_expression = cp_parser_functional_cast (parser, type);
3849 /* If the next thing is a simple-type-specifier, we may be
3850 looking at a functional cast. We could also be looking at
3851 an id-expression. So, we try the functional cast, and if
3852 that doesn't work we fall back to the primary-expression. */
3853 cp_parser_parse_tentatively (parser);
3854 /* Look for the simple-type-specifier. */
3855 type = cp_parser_simple_type_specifier (parser,
3856 /*decl_specs=*/NULL,
3857 CP_PARSER_FLAGS_NONE);
3858 /* Parse the cast itself. */
3859 if (!cp_parser_error_occurred (parser))
3861 = cp_parser_functional_cast (parser, type);
3862 /* If that worked, we're done. */
3863 if (cp_parser_parse_definitely (parser))
3866 /* If the functional-cast didn't work out, try a
3867 compound-literal. */
3868 if (cp_parser_allow_gnu_extensions_p (parser)
3869 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3871 tree initializer_list = NULL_TREE;
3872 bool saved_in_type_id_in_expr_p;
3874 cp_parser_parse_tentatively (parser);
3875 /* Consume the `('. */
3876 cp_lexer_consume_token (parser->lexer);
3877 /* Parse the type. */
3878 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3879 parser->in_type_id_in_expr_p = true;
3880 type = cp_parser_type_id (parser);
3881 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3882 /* Look for the `)'. */
3883 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3884 /* Look for the `{'. */
3885 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3886 /* If things aren't going well, there's no need to
3888 if (!cp_parser_error_occurred (parser))
3890 bool non_constant_p;
3891 /* Parse the initializer-list. */
3893 = cp_parser_initializer_list (parser, &non_constant_p);
3894 /* Allow a trailing `,'. */
3895 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3896 cp_lexer_consume_token (parser->lexer);
3897 /* Look for the final `}'. */
3898 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3900 /* If that worked, we're definitely looking at a
3901 compound-literal expression. */
3902 if (cp_parser_parse_definitely (parser))
3904 /* Warn the user that a compound literal is not
3905 allowed in standard C++. */
3907 pedwarn ("ISO C++ forbids compound-literals");
3908 /* Form the representation of the compound-literal. */
3910 = finish_compound_literal (type, initializer_list);
3915 /* It must be a primary-expression. */
3916 postfix_expression = cp_parser_primary_expression (parser,
3923 /* If we were avoiding committing to the processing of a
3924 qualified-id until we knew whether or not we had a
3925 pointer-to-member, we now know. */
3926 if (qualifying_class)
3930 /* Peek at the next token. */
3931 token = cp_lexer_peek_token (parser->lexer);
3932 done = (token->type != CPP_OPEN_SQUARE
3933 && token->type != CPP_OPEN_PAREN
3934 && token->type != CPP_DOT
3935 && token->type != CPP_DEREF
3936 && token->type != CPP_PLUS_PLUS
3937 && token->type != CPP_MINUS_MINUS);
3939 postfix_expression = finish_qualified_id_expr (qualifying_class,
3944 return postfix_expression;
3947 /* Keep looping until the postfix-expression is complete. */
3950 if (idk == CP_ID_KIND_UNQUALIFIED
3951 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3952 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3953 /* It is not a Koenig lookup function call. */
3955 = unqualified_name_lookup_error (postfix_expression);
3957 /* Peek at the next token. */
3958 token = cp_lexer_peek_token (parser->lexer);
3960 switch (token->type)
3962 case CPP_OPEN_SQUARE:
3964 = cp_parser_postfix_open_square_expression (parser,
3967 idk = CP_ID_KIND_NONE;
3970 case CPP_OPEN_PAREN:
3971 /* postfix-expression ( expression-list [opt] ) */
3974 tree args = (cp_parser_parenthesized_expression_list
3975 (parser, false, /*non_constant_p=*/NULL));
3977 if (args == error_mark_node)
3979 postfix_expression = error_mark_node;
3983 /* Function calls are not permitted in
3984 constant-expressions. */
3985 if (cp_parser_non_integral_constant_expression (parser,
3988 postfix_expression = error_mark_node;
3993 if (idk == CP_ID_KIND_UNQUALIFIED)
3995 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4001 = perform_koenig_lookup (postfix_expression, args);
4005 = unqualified_fn_lookup_error (postfix_expression);
4007 /* We do not perform argument-dependent lookup if
4008 normal lookup finds a non-function, in accordance
4009 with the expected resolution of DR 218. */
4010 else if (args && is_overloaded_fn (postfix_expression))
4012 tree fn = get_first_fn (postfix_expression);
4014 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4015 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4017 /* Only do argument dependent lookup if regular
4018 lookup does not find a set of member functions.
4019 [basic.lookup.koenig]/2a */
4020 if (!DECL_FUNCTION_MEMBER_P (fn))
4024 = perform_koenig_lookup (postfix_expression, args);
4029 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4031 tree instance = TREE_OPERAND (postfix_expression, 0);
4032 tree fn = TREE_OPERAND (postfix_expression, 1);
4034 if (processing_template_decl
4035 && (type_dependent_expression_p (instance)
4036 || (!BASELINK_P (fn)
4037 && TREE_CODE (fn) != FIELD_DECL)
4038 || type_dependent_expression_p (fn)
4039 || any_type_dependent_arguments_p (args)))
4042 = build_min_nt (CALL_EXPR, postfix_expression,
4047 if (BASELINK_P (fn))
4049 = (build_new_method_call
4050 (instance, fn, args, NULL_TREE,
4051 (idk == CP_ID_KIND_QUALIFIED
4052 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4055 = finish_call_expr (postfix_expression, args,
4056 /*disallow_virtual=*/false,
4057 /*koenig_p=*/false);
4059 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4060 || TREE_CODE (postfix_expression) == MEMBER_REF
4061 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4062 postfix_expression = (build_offset_ref_call_from_tree
4063 (postfix_expression, args));
4064 else if (idk == CP_ID_KIND_QUALIFIED)
4065 /* A call to a static class member, or a namespace-scope
4068 = finish_call_expr (postfix_expression, args,
4069 /*disallow_virtual=*/true,
4072 /* All other function calls. */
4074 = finish_call_expr (postfix_expression, args,
4075 /*disallow_virtual=*/false,
4078 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4079 idk = CP_ID_KIND_NONE;
4085 /* postfix-expression . template [opt] id-expression
4086 postfix-expression . pseudo-destructor-name
4087 postfix-expression -> template [opt] id-expression
4088 postfix-expression -> pseudo-destructor-name */
4090 /* Consume the `.' or `->' operator. */
4091 cp_lexer_consume_token (parser->lexer);
4094 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4100 /* postfix-expression ++ */
4101 /* Consume the `++' token. */
4102 cp_lexer_consume_token (parser->lexer);
4103 /* Generate a representation for the complete expression. */
4105 = finish_increment_expr (postfix_expression,
4106 POSTINCREMENT_EXPR);
4107 /* Increments may not appear in constant-expressions. */
4108 if (cp_parser_non_integral_constant_expression (parser,
4110 postfix_expression = error_mark_node;
4111 idk = CP_ID_KIND_NONE;
4114 case CPP_MINUS_MINUS:
4115 /* postfix-expression -- */
4116 /* Consume the `--' token. */
4117 cp_lexer_consume_token (parser->lexer);
4118 /* Generate a representation for the complete expression. */
4120 = finish_increment_expr (postfix_expression,
4121 POSTDECREMENT_EXPR);
4122 /* Decrements may not appear in constant-expressions. */
4123 if (cp_parser_non_integral_constant_expression (parser,
4125 postfix_expression = error_mark_node;
4126 idk = CP_ID_KIND_NONE;
4130 return postfix_expression;
4134 /* We should never get here. */
4136 return error_mark_node;
4139 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4140 by cp_parser_builtin_offsetof. We're looking for
4142 postfix-expression [ expression ]
4144 FOR_OFFSETOF is set if we're being called in that context, which
4145 changes how we deal with integer constant expressions. */
4148 cp_parser_postfix_open_square_expression (cp_parser *parser,
4149 tree postfix_expression,
4154 /* Consume the `[' token. */
4155 cp_lexer_consume_token (parser->lexer);
4157 /* Parse the index expression. */
4158 /* ??? For offsetof, there is a question of what to allow here. If
4159 offsetof is not being used in an integral constant expression context,
4160 then we *could* get the right answer by computing the value at runtime.
4161 If we are in an integral constant expression context, then we might
4162 could accept any constant expression; hard to say without analysis.
4163 Rather than open the barn door too wide right away, allow only integer
4164 constant expressions here. */
4166 index = cp_parser_constant_expression (parser, false, NULL);
4168 index = cp_parser_expression (parser);
4170 /* Look for the closing `]'. */
4171 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4173 /* Build the ARRAY_REF. */
4174 postfix_expression = grok_array_decl (postfix_expression, index);
4176 /* When not doing offsetof, array references are not permitted in
4177 constant-expressions. */
4179 && (cp_parser_non_integral_constant_expression
4180 (parser, "an array reference")))
4181 postfix_expression = error_mark_node;
4183 return postfix_expression;
4186 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4187 by cp_parser_builtin_offsetof. We're looking for
4189 postfix-expression . template [opt] id-expression
4190 postfix-expression . pseudo-destructor-name
4191 postfix-expression -> template [opt] id-expression
4192 postfix-expression -> pseudo-destructor-name
4194 FOR_OFFSETOF is set if we're being called in that context. That sorta
4195 limits what of the above we'll actually accept, but nevermind.
4196 TOKEN_TYPE is the "." or "->" token, which will already have been
4197 removed from the stream. */
4200 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4201 enum cpp_ttype token_type,
4202 tree postfix_expression,
4203 bool for_offsetof, cp_id_kind *idk)
4208 bool pseudo_destructor_p;
4209 tree scope = NULL_TREE;
4211 /* If this is a `->' operator, dereference the pointer. */
4212 if (token_type == CPP_DEREF)
4213 postfix_expression = build_x_arrow (postfix_expression);
4214 /* Check to see whether or not the expression is type-dependent. */
4215 dependent_p = type_dependent_expression_p (postfix_expression);
4216 /* The identifier following the `->' or `.' is not qualified. */
4217 parser->scope = NULL_TREE;
4218 parser->qualifying_scope = NULL_TREE;
4219 parser->object_scope = NULL_TREE;
4220 *idk = CP_ID_KIND_NONE;
4221 /* Enter the scope corresponding to the type of the object
4222 given by the POSTFIX_EXPRESSION. */
4223 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4225 scope = TREE_TYPE (postfix_expression);
4226 /* According to the standard, no expression should ever have
4227 reference type. Unfortunately, we do not currently match
4228 the standard in this respect in that our internal representation
4229 of an expression may have reference type even when the standard
4230 says it does not. Therefore, we have to manually obtain the
4231 underlying type here. */
4232 scope = non_reference (scope);
4233 /* The type of the POSTFIX_EXPRESSION must be complete. */
4234 scope = complete_type_or_else (scope, NULL_TREE);
4235 /* Let the name lookup machinery know that we are processing a
4236 class member access expression. */
4237 parser->context->object_type = scope;
4238 /* If something went wrong, we want to be able to discern that case,
4239 as opposed to the case where there was no SCOPE due to the type
4240 of expression being dependent. */
4242 scope = error_mark_node;
4243 /* If the SCOPE was erroneous, make the various semantic analysis
4244 functions exit quickly -- and without issuing additional error
4246 if (scope == error_mark_node)
4247 postfix_expression = error_mark_node;
4250 /* Assume this expression is not a pseudo-destructor access. */
4251 pseudo_destructor_p = false;
4253 /* If the SCOPE is a scalar type, then, if this is a valid program,
4254 we must be looking at a pseudo-destructor-name. */
4255 if (scope && SCALAR_TYPE_P (scope))
4260 cp_parser_parse_tentatively (parser);
4261 /* Parse the pseudo-destructor-name. */
4263 cp_parser_pseudo_destructor_name (parser, &s, &type);
4264 if (cp_parser_parse_definitely (parser))
4266 pseudo_destructor_p = true;
4268 = finish_pseudo_destructor_expr (postfix_expression,
4269 s, TREE_TYPE (type));
4273 if (!pseudo_destructor_p)
4275 /* If the SCOPE is not a scalar type, we are looking at an
4276 ordinary class member access expression, rather than a
4277 pseudo-destructor-name. */
4278 template_p = cp_parser_optional_template_keyword (parser);
4279 /* Parse the id-expression. */
4280 name = cp_parser_id_expression (parser, template_p,
4281 /*check_dependency_p=*/true,
4282 /*template_p=*/NULL,
4283 /*declarator_p=*/false);
4284 /* In general, build a SCOPE_REF if the member name is qualified.
4285 However, if the name was not dependent and has already been
4286 resolved; there is no need to build the SCOPE_REF. For example;
4288 struct X { void f(); };
4289 template <typename T> void f(T* t) { t->X::f(); }
4291 Even though "t" is dependent, "X::f" is not and has been resolved
4292 to a BASELINK; there is no need to include scope information. */
4294 /* But we do need to remember that there was an explicit scope for
4295 virtual function calls. */
4297 *idk = CP_ID_KIND_QUALIFIED;
4299 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4301 name = build_nt (SCOPE_REF, parser->scope, name);
4302 parser->scope = NULL_TREE;
4303 parser->qualifying_scope = NULL_TREE;
4304 parser->object_scope = NULL_TREE;
4306 if (scope && name && BASELINK_P (name))
4307 adjust_result_of_qualified_name_lookup
4308 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4310 = finish_class_member_access_expr (postfix_expression, name);
4313 /* We no longer need to look up names in the scope of the object on
4314 the left-hand side of the `.' or `->' operator. */
4315 parser->context->object_type = NULL_TREE;
4317 /* Outside of offsetof, these operators may not appear in
4318 constant-expressions. */
4320 && (cp_parser_non_integral_constant_expression
4321 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4322 postfix_expression = error_mark_node;
4324 return postfix_expression;
4327 /* Parse a parenthesized expression-list.
4330 assignment-expression
4331 expression-list, assignment-expression
4336 identifier, expression-list
4338 Returns a TREE_LIST. The TREE_VALUE of each node is a
4339 representation of an assignment-expression. Note that a TREE_LIST
4340 is returned even if there is only a single expression in the list.
4341 error_mark_node is returned if the ( and or ) are
4342 missing. NULL_TREE is returned on no expressions. The parentheses
4343 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4344 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4345 indicates whether or not all of the expressions in the list were
4349 cp_parser_parenthesized_expression_list (cp_parser* parser,
4350 bool is_attribute_list,
4351 bool *non_constant_p)
4353 tree expression_list = NULL_TREE;
4354 bool fold_expr_p = is_attribute_list;
4355 tree identifier = NULL_TREE;
4357 /* Assume all the expressions will be constant. */
4359 *non_constant_p = false;
4361 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4362 return error_mark_node;
4364 /* Consume expressions until there are no more. */
4365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4370 /* At the beginning of attribute lists, check to see if the
4371 next token is an identifier. */
4372 if (is_attribute_list
4373 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4377 /* Consume the identifier. */
4378 token = cp_lexer_consume_token (parser->lexer);
4379 /* Save the identifier. */
4380 identifier = token->value;
4384 /* Parse the next assignment-expression. */
4387 bool expr_non_constant_p;
4388 expr = (cp_parser_constant_expression
4389 (parser, /*allow_non_constant_p=*/true,
4390 &expr_non_constant_p));
4391 if (expr_non_constant_p)
4392 *non_constant_p = true;
4395 expr = cp_parser_assignment_expression (parser);
4398 expr = fold_non_dependent_expr (expr);
4400 /* Add it to the list. We add error_mark_node
4401 expressions to the list, so that we can still tell if
4402 the correct form for a parenthesized expression-list
4403 is found. That gives better errors. */
4404 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4406 if (expr == error_mark_node)
4410 /* After the first item, attribute lists look the same as
4411 expression lists. */
4412 is_attribute_list = false;
4415 /* If the next token isn't a `,', then we are done. */
4416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4419 /* Otherwise, consume the `,' and keep going. */
4420 cp_lexer_consume_token (parser->lexer);
4423 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4428 /* We try and resync to an unnested comma, as that will give the
4429 user better diagnostics. */
4430 ending = cp_parser_skip_to_closing_parenthesis (parser,
4431 /*recovering=*/true,
4433 /*consume_paren=*/true);
4437 return error_mark_node;
4440 /* We built up the list in reverse order so we must reverse it now. */
4441 expression_list = nreverse (expression_list);
4443 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4445 return expression_list;
4448 /* Parse a pseudo-destructor-name.
4450 pseudo-destructor-name:
4451 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4452 :: [opt] nested-name-specifier template template-id :: ~ type-name
4453 :: [opt] nested-name-specifier [opt] ~ type-name
4455 If either of the first two productions is used, sets *SCOPE to the
4456 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4457 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4458 or ERROR_MARK_NODE if the parse fails. */
4461 cp_parser_pseudo_destructor_name (cp_parser* parser,
4465 bool nested_name_specifier_p;
4467 /* Assume that things will not work out. */
4468 *type = error_mark_node;
4470 /* Look for the optional `::' operator. */
4471 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4472 /* Look for the optional nested-name-specifier. */
4473 nested_name_specifier_p
4474 = (cp_parser_nested_name_specifier_opt (parser,
4475 /*typename_keyword_p=*/false,
4476 /*check_dependency_p=*/true,
4478 /*is_declaration=*/true)
4480 /* Now, if we saw a nested-name-specifier, we might be doing the
4481 second production. */
4482 if (nested_name_specifier_p
4483 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4485 /* Consume the `template' keyword. */
4486 cp_lexer_consume_token (parser->lexer);
4487 /* Parse the template-id. */
4488 cp_parser_template_id (parser,
4489 /*template_keyword_p=*/true,
4490 /*check_dependency_p=*/false,
4491 /*is_declaration=*/true);
4492 /* Look for the `::' token. */
4493 cp_parser_require (parser, CPP_SCOPE, "`::'");
4495 /* If the next token is not a `~', then there might be some
4496 additional qualification. */
4497 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4499 /* Look for the type-name. */
4500 *scope = TREE_TYPE (cp_parser_type_name (parser));
4502 if (*scope == error_mark_node)
4505 /* If we don't have ::~, then something has gone wrong. Since
4506 the only caller of this function is looking for something
4507 after `.' or `->' after a scalar type, most likely the
4508 program is trying to get a member of a non-aggregate
4510 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4511 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4513 cp_parser_error (parser, "request for member of non-aggregate type");
4517 /* Look for the `::' token. */
4518 cp_parser_require (parser, CPP_SCOPE, "`::'");
4523 /* Look for the `~'. */
4524 cp_parser_require (parser, CPP_COMPL, "`~'");
4525 /* Look for the type-name again. We are not responsible for
4526 checking that it matches the first type-name. */
4527 *type = cp_parser_type_name (parser);
4530 /* Parse a unary-expression.
4536 unary-operator cast-expression
4537 sizeof unary-expression
4545 __extension__ cast-expression
4546 __alignof__ unary-expression
4547 __alignof__ ( type-id )
4548 __real__ cast-expression
4549 __imag__ cast-expression
4552 ADDRESS_P is true iff the unary-expression is appearing as the
4553 operand of the `&' operator.
4555 Returns a representation of the expression. */
4558 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4561 enum tree_code unary_operator;
4563 /* Peek at the next token. */
4564 token = cp_lexer_peek_token (parser->lexer);
4565 /* Some keywords give away the kind of expression. */
4566 if (token->type == CPP_KEYWORD)
4568 enum rid keyword = token->keyword;
4578 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4579 /* Consume the token. */
4580 cp_lexer_consume_token (parser->lexer);
4581 /* Parse the operand. */
4582 operand = cp_parser_sizeof_operand (parser, keyword);
4584 if (TYPE_P (operand))
4585 return cxx_sizeof_or_alignof_type (operand, op, true);
4587 return cxx_sizeof_or_alignof_expr (operand, op);
4591 return cp_parser_new_expression (parser);
4594 return cp_parser_delete_expression (parser);
4598 /* The saved value of the PEDANTIC flag. */
4602 /* Save away the PEDANTIC flag. */
4603 cp_parser_extension_opt (parser, &saved_pedantic);
4604 /* Parse the cast-expression. */
4605 expr = cp_parser_simple_cast_expression (parser);
4606 /* Restore the PEDANTIC flag. */
4607 pedantic = saved_pedantic;
4617 /* Consume the `__real__' or `__imag__' token. */
4618 cp_lexer_consume_token (parser->lexer);
4619 /* Parse the cast-expression. */
4620 expression = cp_parser_simple_cast_expression (parser);
4621 /* Create the complete representation. */
4622 return build_x_unary_op ((keyword == RID_REALPART
4623 ? REALPART_EXPR : IMAGPART_EXPR),
4633 /* Look for the `:: new' and `:: delete', which also signal the
4634 beginning of a new-expression, or delete-expression,
4635 respectively. If the next token is `::', then it might be one of
4637 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4641 /* See if the token after the `::' is one of the keywords in
4642 which we're interested. */
4643 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4644 /* If it's `new', we have a new-expression. */
4645 if (keyword == RID_NEW)
4646 return cp_parser_new_expression (parser);
4647 /* Similarly, for `delete'. */
4648 else if (keyword == RID_DELETE)
4649 return cp_parser_delete_expression (parser);
4652 /* Look for a unary operator. */
4653 unary_operator = cp_parser_unary_operator (token);
4654 /* The `++' and `--' operators can be handled similarly, even though
4655 they are not technically unary-operators in the grammar. */
4656 if (unary_operator == ERROR_MARK)
4658 if (token->type == CPP_PLUS_PLUS)
4659 unary_operator = PREINCREMENT_EXPR;
4660 else if (token->type == CPP_MINUS_MINUS)
4661 unary_operator = PREDECREMENT_EXPR;
4662 /* Handle the GNU address-of-label extension. */
4663 else if (cp_parser_allow_gnu_extensions_p (parser)
4664 && token->type == CPP_AND_AND)
4668 /* Consume the '&&' token. */
4669 cp_lexer_consume_token (parser->lexer);
4670 /* Look for the identifier. */
4671 identifier = cp_parser_identifier (parser);
4672 /* Create an expression representing the address. */
4673 return finish_label_address_expr (identifier);
4676 if (unary_operator != ERROR_MARK)
4678 tree cast_expression;
4679 tree expression = error_mark_node;
4680 const char *non_constant_p = NULL;
4682 /* Consume the operator token. */
4683 token = cp_lexer_consume_token (parser->lexer);
4684 /* Parse the cast-expression. */
4686 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4687 /* Now, build an appropriate representation. */
4688 switch (unary_operator)
4691 non_constant_p = "`*'";
4692 expression = build_x_indirect_ref (cast_expression, "unary *");
4696 non_constant_p = "`&'";
4699 expression = build_x_unary_op (unary_operator, cast_expression);
4702 case PREINCREMENT_EXPR:
4703 case PREDECREMENT_EXPR:
4704 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4709 case TRUTH_NOT_EXPR:
4710 expression = finish_unary_op_expr (unary_operator, cast_expression);
4718 && cp_parser_non_integral_constant_expression (parser,
4720 expression = error_mark_node;
4725 return cp_parser_postfix_expression (parser, address_p);
4728 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4729 unary-operator, the corresponding tree code is returned. */
4731 static enum tree_code
4732 cp_parser_unary_operator (cp_token* token)
4734 switch (token->type)
4737 return INDIRECT_REF;
4743 return CONVERT_EXPR;
4749 return TRUTH_NOT_EXPR;
4752 return BIT_NOT_EXPR;
4759 /* Parse a new-expression.
4762 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4763 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4765 Returns a representation of the expression. */
4768 cp_parser_new_expression (cp_parser* parser)
4770 bool global_scope_p;
4776 /* Look for the optional `::' operator. */
4778 = (cp_parser_global_scope_opt (parser,
4779 /*current_scope_valid_p=*/false)
4781 /* Look for the `new' operator. */
4782 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4783 /* There's no easy way to tell a new-placement from the
4784 `( type-id )' construct. */
4785 cp_parser_parse_tentatively (parser);
4786 /* Look for a new-placement. */
4787 placement = cp_parser_new_placement (parser);
4788 /* If that didn't work out, there's no new-placement. */
4789 if (!cp_parser_parse_definitely (parser))
4790 placement = NULL_TREE;
4792 /* If the next token is a `(', then we have a parenthesized
4794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4796 /* Consume the `('. */
4797 cp_lexer_consume_token (parser->lexer);
4798 /* Parse the type-id. */
4799 type = cp_parser_type_id (parser);
4800 /* Look for the closing `)'. */
4801 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4802 /* There should not be a direct-new-declarator in this production,
4803 but GCC used to allowed this, so we check and emit a sensible error
4804 message for this case. */
4805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4807 error ("array bound forbidden after parenthesized type-id");
4808 inform ("try removing the parentheses around the type-id");
4809 cp_parser_direct_new_declarator (parser);
4813 /* Otherwise, there must be a new-type-id. */
4815 type = cp_parser_new_type_id (parser, &nelts);
4817 /* If the next token is a `(', then we have a new-initializer. */
4818 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4819 initializer = cp_parser_new_initializer (parser);
4821 initializer = NULL_TREE;
4823 /* A new-expression may not appear in an integral constant
4825 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4826 return error_mark_node;
4828 /* Create a representation of the new-expression. */
4829 return build_new (placement, type, nelts, initializer, global_scope_p);
4832 /* Parse a new-placement.
4837 Returns the same representation as for an expression-list. */
4840 cp_parser_new_placement (cp_parser* parser)
4842 tree expression_list;
4844 /* Parse the expression-list. */
4845 expression_list = (cp_parser_parenthesized_expression_list
4846 (parser, false, /*non_constant_p=*/NULL));
4848 return expression_list;
4851 /* Parse a new-type-id.
4854 type-specifier-seq new-declarator [opt]
4856 Returns the TYPE allocated. If the new-type-id indicates an array
4857 type, *NELTS is set to the number of elements in the last array
4858 bound; the TYPE will not include the last array bound. */
4861 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4863 cp_decl_specifier_seq type_specifier_seq;
4864 cp_declarator *new_declarator;
4865 cp_declarator *declarator;
4866 cp_declarator *outer_declarator;
4867 const char *saved_message;
4870 /* The type-specifier sequence must not contain type definitions.
4871 (It cannot contain declarations of new types either, but if they
4872 are not definitions we will catch that because they are not
4874 saved_message = parser->type_definition_forbidden_message;
4875 parser->type_definition_forbidden_message
4876 = "types may not be defined in a new-type-id";
4877 /* Parse the type-specifier-seq. */
4878 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4879 /* Restore the old message. */
4880 parser->type_definition_forbidden_message = saved_message;
4881 /* Parse the new-declarator. */
4882 new_declarator = cp_parser_new_declarator_opt (parser);
4884 /* Determine the number of elements in the last array dimension, if
4887 /* Skip down to the last array dimension. */
4888 declarator = new_declarator;
4889 outer_declarator = NULL;
4890 while (declarator && (declarator->kind == cdk_pointer
4891 || declarator->kind == cdk_ptrmem))
4893 outer_declarator = declarator;
4894 declarator = declarator->declarator;
4897 && declarator->kind == cdk_array
4898 && declarator->declarator
4899 && declarator->declarator->kind == cdk_array)
4901 outer_declarator = declarator;
4902 declarator = declarator->declarator;
4905 if (declarator && declarator->kind == cdk_array)
4907 *nelts = declarator->u.array.bounds;
4908 if (*nelts == error_mark_node)
4909 *nelts = integer_one_node;
4910 else if (!processing_template_decl)
4912 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4914 pedwarn ("size in array new must have integral type");
4915 *nelts = save_expr (cp_convert (sizetype, *nelts));
4916 if (*nelts == integer_zero_node)
4917 warning ("zero size array reserves no space");
4919 if (outer_declarator)
4920 outer_declarator->declarator = declarator->declarator;
4922 new_declarator = NULL;
4925 type = groktypename (&type_specifier_seq, new_declarator);
4926 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4928 *nelts = array_type_nelts_top (type);
4929 type = TREE_TYPE (type);
4934 /* Parse an (optional) new-declarator.
4937 ptr-operator new-declarator [opt]
4938 direct-new-declarator
4940 Returns the declarator. */
4942 static cp_declarator *
4943 cp_parser_new_declarator_opt (cp_parser* parser)
4945 enum tree_code code;
4947 cp_cv_quals cv_quals;
4949 /* We don't know if there's a ptr-operator next, or not. */
4950 cp_parser_parse_tentatively (parser);
4951 /* Look for a ptr-operator. */
4952 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4953 /* If that worked, look for more new-declarators. */
4954 if (cp_parser_parse_definitely (parser))
4956 cp_declarator *declarator;
4958 /* Parse another optional declarator. */
4959 declarator = cp_parser_new_declarator_opt (parser);
4961 /* Create the representation of the declarator. */
4963 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4964 else if (code == INDIRECT_REF)
4965 declarator = make_pointer_declarator (cv_quals, declarator);
4967 declarator = make_reference_declarator (cv_quals, declarator);
4972 /* If the next token is a `[', there is a direct-new-declarator. */
4973 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4974 return cp_parser_direct_new_declarator (parser);
4979 /* Parse a direct-new-declarator.
4981 direct-new-declarator:
4983 direct-new-declarator [constant-expression]
4987 static cp_declarator *
4988 cp_parser_direct_new_declarator (cp_parser* parser)
4990 cp_declarator *declarator = NULL;
4996 /* Look for the opening `['. */
4997 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4998 /* The first expression is not required to be constant. */
5001 expression = cp_parser_expression (parser);
5002 /* The standard requires that the expression have integral
5003 type. DR 74 adds enumeration types. We believe that the
5004 real intent is that these expressions be handled like the
5005 expression in a `switch' condition, which also allows
5006 classes with a single conversion to integral or
5007 enumeration type. */
5008 if (!processing_template_decl)
5011 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5016 error ("expression in new-declarator must have integral "
5017 "or enumeration type");
5018 expression = error_mark_node;
5022 /* But all the other expressions must be. */
5025 = cp_parser_constant_expression (parser,
5026 /*allow_non_constant=*/false,
5028 /* Look for the closing `]'. */
5029 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5031 /* Add this bound to the declarator. */
5032 declarator = make_array_declarator (declarator, expression);
5034 /* If the next token is not a `[', then there are no more
5036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5043 /* Parse a new-initializer.
5046 ( expression-list [opt] )
5048 Returns a representation of the expression-list. If there is no
5049 expression-list, VOID_ZERO_NODE is returned. */
5052 cp_parser_new_initializer (cp_parser* parser)
5054 tree expression_list;
5056 expression_list = (cp_parser_parenthesized_expression_list
5057 (parser, false, /*non_constant_p=*/NULL));
5058 if (!expression_list)
5059 expression_list = void_zero_node;
5061 return expression_list;
5064 /* Parse a delete-expression.
5067 :: [opt] delete cast-expression
5068 :: [opt] delete [ ] cast-expression
5070 Returns a representation of the expression. */
5073 cp_parser_delete_expression (cp_parser* parser)
5075 bool global_scope_p;
5079 /* Look for the optional `::' operator. */
5081 = (cp_parser_global_scope_opt (parser,
5082 /*current_scope_valid_p=*/false)
5084 /* Look for the `delete' keyword. */
5085 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5086 /* See if the array syntax is in use. */
5087 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5089 /* Consume the `[' token. */
5090 cp_lexer_consume_token (parser->lexer);
5091 /* Look for the `]' token. */
5092 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5093 /* Remember that this is the `[]' construct. */
5099 /* Parse the cast-expression. */
5100 expression = cp_parser_simple_cast_expression (parser);
5102 /* A delete-expression may not appear in an integral constant
5104 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5105 return error_mark_node;
5107 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5110 /* Parse a cast-expression.
5114 ( type-id ) cast-expression
5116 Returns a representation of the expression. */
5119 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5121 /* If it's a `(', then we might be looking at a cast. */
5122 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5124 tree type = NULL_TREE;
5125 tree expr = NULL_TREE;
5126 bool compound_literal_p;
5127 const char *saved_message;
5129 /* There's no way to know yet whether or not this is a cast.
5130 For example, `(int (3))' is a unary-expression, while `(int)
5131 3' is a cast. So, we resort to parsing tentatively. */
5132 cp_parser_parse_tentatively (parser);
5133 /* Types may not be defined in a cast. */
5134 saved_message = parser->type_definition_forbidden_message;
5135 parser->type_definition_forbidden_message
5136 = "types may not be defined in casts";
5137 /* Consume the `('. */
5138 cp_lexer_consume_token (parser->lexer);
5139 /* A very tricky bit is that `(struct S) { 3 }' is a
5140 compound-literal (which we permit in C++ as an extension).
5141 But, that construct is not a cast-expression -- it is a
5142 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5143 is legal; if the compound-literal were a cast-expression,
5144 you'd need an extra set of parentheses.) But, if we parse
5145 the type-id, and it happens to be a class-specifier, then we
5146 will commit to the parse at that point, because we cannot
5147 undo the action that is done when creating a new class. So,
5148 then we cannot back up and do a postfix-expression.
5150 Therefore, we scan ahead to the closing `)', and check to see
5151 if the token after the `)' is a `{'. If so, we are not
5152 looking at a cast-expression.
5154 Save tokens so that we can put them back. */
5155 cp_lexer_save_tokens (parser->lexer);
5156 /* Skip tokens until the next token is a closing parenthesis.
5157 If we find the closing `)', and the next token is a `{', then
5158 we are looking at a compound-literal. */
5160 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5161 /*consume_paren=*/true)
5162 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5163 /* Roll back the tokens we skipped. */
5164 cp_lexer_rollback_tokens (parser->lexer);
5165 /* If we were looking at a compound-literal, simulate an error
5166 so that the call to cp_parser_parse_definitely below will
5168 if (compound_literal_p)
5169 cp_parser_simulate_error (parser);
5172 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5173 parser->in_type_id_in_expr_p = true;
5174 /* Look for the type-id. */
5175 type = cp_parser_type_id (parser);
5176 /* Look for the closing `)'. */
5177 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5178 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5181 /* Restore the saved message. */
5182 parser->type_definition_forbidden_message = saved_message;
5184 /* If ok so far, parse the dependent expression. We cannot be
5185 sure it is a cast. Consider `(T ())'. It is a parenthesized
5186 ctor of T, but looks like a cast to function returning T
5187 without a dependent expression. */
5188 if (!cp_parser_error_occurred (parser))
5189 expr = cp_parser_simple_cast_expression (parser);
5191 if (cp_parser_parse_definitely (parser))
5193 /* Warn about old-style casts, if so requested. */
5194 if (warn_old_style_cast
5195 && !in_system_header
5196 && !VOID_TYPE_P (type)
5197 && current_lang_name != lang_name_c)
5198 warning ("use of old-style cast");
5200 /* Only type conversions to integral or enumeration types
5201 can be used in constant-expressions. */
5202 if (parser->integral_constant_expression_p
5203 && !dependent_type_p (type)
5204 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5205 && (cp_parser_non_integral_constant_expression
5207 "a cast to a type other than an integral or "
5208 "enumeration type")))
5209 return error_mark_node;
5211 /* Perform the cast. */
5212 expr = build_c_cast (type, expr);
5217 /* If we get here, then it's not a cast, so it must be a
5218 unary-expression. */
5219 return cp_parser_unary_expression (parser, address_p);
5222 /* Parse a binary expression of the general form:
5226 pm-expression .* cast-expression
5227 pm-expression ->* cast-expression
5229 multiplicative-expression:
5231 multiplicative-expression * pm-expression
5232 multiplicative-expression / pm-expression
5233 multiplicative-expression % pm-expression
5235 additive-expression:
5236 multiplicative-expression
5237 additive-expression + multiplicative-expression
5238 additive-expression - multiplicative-expression
5242 shift-expression << additive-expression
5243 shift-expression >> additive-expression
5245 relational-expression:
5247 relational-expression < shift-expression
5248 relational-expression > shift-expression
5249 relational-expression <= shift-expression
5250 relational-expression >= shift-expression
5254 relational-expression:
5255 relational-expression <? shift-expression
5256 relational-expression >? shift-expression
5258 equality-expression:
5259 relational-expression
5260 equality-expression == relational-expression
5261 equality-expression != relational-expression
5265 and-expression & equality-expression
5267 exclusive-or-expression:
5269 exclusive-or-expression ^ and-expression
5271 inclusive-or-expression:
5272 exclusive-or-expression
5273 inclusive-or-expression | exclusive-or-expression
5275 logical-and-expression:
5276 inclusive-or-expression
5277 logical-and-expression && inclusive-or-expression
5279 logical-or-expression:
5280 logical-and-expression
5281 logical-or-expression || logical-and-expression
5283 All these are implemented with a single function like:
5286 simple-cast-expression
5287 binary-expression <token> binary-expression
5289 The binops_by_token map is used to get the tree codes for each <token> type.
5290 binary-expressions are associated according to a precedence table. */
5292 #define TOKEN_PRECEDENCE(token) \
5293 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5294 ? PREC_NOT_OPERATOR \
5295 : binops_by_token[token->type].prec)
5298 cp_parser_binary_expression (cp_parser* parser)
5300 cp_parser_expression_stack stack;
5301 cp_parser_expression_stack_entry *sp = &stack[0];
5304 enum tree_code tree_type;
5305 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5308 /* Parse the first expression. */
5309 lhs = cp_parser_simple_cast_expression (parser);
5313 /* Get an operator token. */
5314 token = cp_lexer_peek_token (parser->lexer);
5315 new_prec = TOKEN_PRECEDENCE (token);
5317 /* Popping an entry off the stack means we completed a subexpression:
5318 - either we found a token which is not an operator (`>' where it is not
5319 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5320 will happen repeatedly;
5321 - or, we found an operator which has lower priority. This is the case
5322 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5324 if (new_prec <= prec)
5333 tree_type = binops_by_token[token->type].tree_type;
5335 /* We used the operator token. */
5336 cp_lexer_consume_token (parser->lexer);
5338 /* Extract another operand. It may be the RHS of this expression
5339 or the LHS of a new, higher priority expression. */
5340 rhs = cp_parser_simple_cast_expression (parser);
5342 /* Get another operator token. Look up its precedence to avoid
5343 building a useless (immediately popped) stack entry for common
5344 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5345 token = cp_lexer_peek_token (parser->lexer);
5346 lookahead_prec = TOKEN_PRECEDENCE (token);
5347 if (lookahead_prec > new_prec)
5349 /* ... and prepare to parse the RHS of the new, higher priority
5350 expression. Since precedence levels on the stack are
5351 monotonically increasing, we do not have to care about
5354 sp->tree_type = tree_type;
5359 new_prec = lookahead_prec;
5363 /* If the stack is not empty, we have parsed into LHS the right side
5364 (`4' in the example above) of an expression we had suspended.
5365 We can use the information on the stack to recover the LHS (`3')
5366 from the stack together with the tree code (`MULT_EXPR'), and
5367 the precedence of the higher level subexpression
5368 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5369 which will be used to actually build the additive expression. */
5372 tree_type = sp->tree_type;
5377 overloaded_p = false;
5378 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5380 /* If the binary operator required the use of an overloaded operator,
5381 then this expression cannot be an integral constant-expression.
5382 An overloaded operator can be used even if both operands are
5383 otherwise permissible in an integral constant-expression if at
5384 least one of the operands is of enumeration type. */
5387 && (cp_parser_non_integral_constant_expression
5388 (parser, "calls to overloaded operators")))
5389 return error_mark_node;
5396 /* Parse the `? expression : assignment-expression' part of a
5397 conditional-expression. The LOGICAL_OR_EXPR is the
5398 logical-or-expression that started the conditional-expression.
5399 Returns a representation of the entire conditional-expression.
5401 This routine is used by cp_parser_assignment_expression.
5403 ? expression : assignment-expression
5407 ? : assignment-expression */
5410 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5413 tree assignment_expr;
5415 /* Consume the `?' token. */
5416 cp_lexer_consume_token (parser->lexer);
5417 if (cp_parser_allow_gnu_extensions_p (parser)
5418 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5419 /* Implicit true clause. */
5422 /* Parse the expression. */
5423 expr = cp_parser_expression (parser);
5425 /* The next token should be a `:'. */
5426 cp_parser_require (parser, CPP_COLON, "`:'");
5427 /* Parse the assignment-expression. */
5428 assignment_expr = cp_parser_assignment_expression (parser);
5430 /* Build the conditional-expression. */
5431 return build_x_conditional_expr (logical_or_expr,
5436 /* Parse an assignment-expression.
5438 assignment-expression:
5439 conditional-expression
5440 logical-or-expression assignment-operator assignment_expression
5443 Returns a representation for the expression. */
5446 cp_parser_assignment_expression (cp_parser* parser)
5450 /* If the next token is the `throw' keyword, then we're looking at
5451 a throw-expression. */
5452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5453 expr = cp_parser_throw_expression (parser);
5454 /* Otherwise, it must be that we are looking at a
5455 logical-or-expression. */
5458 /* Parse the binary expressions (logical-or-expression). */
5459 expr = cp_parser_binary_expression (parser);
5460 /* If the next token is a `?' then we're actually looking at a
5461 conditional-expression. */
5462 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5463 return cp_parser_question_colon_clause (parser, expr);
5466 enum tree_code assignment_operator;
5468 /* If it's an assignment-operator, we're using the second
5471 = cp_parser_assignment_operator_opt (parser);
5472 if (assignment_operator != ERROR_MARK)
5476 /* Parse the right-hand side of the assignment. */
5477 rhs = cp_parser_assignment_expression (parser);
5478 /* An assignment may not appear in a
5479 constant-expression. */
5480 if (cp_parser_non_integral_constant_expression (parser,
5482 return error_mark_node;
5483 /* Build the assignment expression. */
5484 expr = build_x_modify_expr (expr,
5485 assignment_operator,
5494 /* Parse an (optional) assignment-operator.
5496 assignment-operator: one of
5497 = *= /= %= += -= >>= <<= &= ^= |=
5501 assignment-operator: one of
5504 If the next token is an assignment operator, the corresponding tree
5505 code is returned, and the token is consumed. For example, for
5506 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5507 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5508 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5509 operator, ERROR_MARK is returned. */
5511 static enum tree_code
5512 cp_parser_assignment_operator_opt (cp_parser* parser)
5517 /* Peek at the next toen. */
5518 token = cp_lexer_peek_token (parser->lexer);
5520 switch (token->type)
5531 op = TRUNC_DIV_EXPR;
5535 op = TRUNC_MOD_EXPR;
5575 /* Nothing else is an assignment operator. */
5579 /* If it was an assignment operator, consume it. */
5580 if (op != ERROR_MARK)
5581 cp_lexer_consume_token (parser->lexer);
5586 /* Parse an expression.
5589 assignment-expression
5590 expression , assignment-expression
5592 Returns a representation of the expression. */
5595 cp_parser_expression (cp_parser* parser)
5597 tree expression = NULL_TREE;
5601 tree assignment_expression;
5603 /* Parse the next assignment-expression. */
5604 assignment_expression
5605 = cp_parser_assignment_expression (parser);
5606 /* If this is the first assignment-expression, we can just
5609 expression = assignment_expression;
5611 expression = build_x_compound_expr (expression,
5612 assignment_expression);
5613 /* If the next token is not a comma, then we are done with the
5615 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5617 /* Consume the `,'. */
5618 cp_lexer_consume_token (parser->lexer);
5619 /* A comma operator cannot appear in a constant-expression. */
5620 if (cp_parser_non_integral_constant_expression (parser,
5621 "a comma operator"))
5622 expression = error_mark_node;
5628 /* Parse a constant-expression.
5630 constant-expression:
5631 conditional-expression
5633 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5634 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5635 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5636 is false, NON_CONSTANT_P should be NULL. */
5639 cp_parser_constant_expression (cp_parser* parser,
5640 bool allow_non_constant_p,
5641 bool *non_constant_p)
5643 bool saved_integral_constant_expression_p;
5644 bool saved_allow_non_integral_constant_expression_p;
5645 bool saved_non_integral_constant_expression_p;
5648 /* It might seem that we could simply parse the
5649 conditional-expression, and then check to see if it were
5650 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5651 one that the compiler can figure out is constant, possibly after
5652 doing some simplifications or optimizations. The standard has a
5653 precise definition of constant-expression, and we must honor
5654 that, even though it is somewhat more restrictive.
5660 is not a legal declaration, because `(2, 3)' is not a
5661 constant-expression. The `,' operator is forbidden in a
5662 constant-expression. However, GCC's constant-folding machinery
5663 will fold this operation to an INTEGER_CST for `3'. */
5665 /* Save the old settings. */
5666 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5667 saved_allow_non_integral_constant_expression_p
5668 = parser->allow_non_integral_constant_expression_p;
5669 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5670 /* We are now parsing a constant-expression. */
5671 parser->integral_constant_expression_p = true;
5672 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5673 parser->non_integral_constant_expression_p = false;
5674 /* Although the grammar says "conditional-expression", we parse an
5675 "assignment-expression", which also permits "throw-expression"
5676 and the use of assignment operators. In the case that
5677 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5678 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5679 actually essential that we look for an assignment-expression.
5680 For example, cp_parser_initializer_clauses uses this function to
5681 determine whether a particular assignment-expression is in fact
5683 expression = cp_parser_assignment_expression (parser);
5684 /* Restore the old settings. */
5685 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5686 parser->allow_non_integral_constant_expression_p
5687 = saved_allow_non_integral_constant_expression_p;
5688 if (allow_non_constant_p)
5689 *non_constant_p = parser->non_integral_constant_expression_p;
5690 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5695 /* Parse __builtin_offsetof.
5697 offsetof-expression:
5698 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5700 offsetof-member-designator:
5702 | offsetof-member-designator "." id-expression
5703 | offsetof-member-designator "[" expression "]"
5707 cp_parser_builtin_offsetof (cp_parser *parser)
5709 int save_ice_p, save_non_ice_p;
5713 /* We're about to accept non-integral-constant things, but will
5714 definitely yield an integral constant expression. Save and
5715 restore these values around our local parsing. */
5716 save_ice_p = parser->integral_constant_expression_p;
5717 save_non_ice_p = parser->non_integral_constant_expression_p;
5719 /* Consume the "__builtin_offsetof" token. */
5720 cp_lexer_consume_token (parser->lexer);
5721 /* Consume the opening `('. */
5722 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5723 /* Parse the type-id. */
5724 type = cp_parser_type_id (parser);
5725 /* Look for the `,'. */
5726 cp_parser_require (parser, CPP_COMMA, "`,'");
5728 /* Build the (type *)null that begins the traditional offsetof macro. */
5729 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5731 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5732 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5736 cp_token *token = cp_lexer_peek_token (parser->lexer);
5737 switch (token->type)
5739 case CPP_OPEN_SQUARE:
5740 /* offsetof-member-designator "[" expression "]" */
5741 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5745 /* offsetof-member-designator "." identifier */
5746 cp_lexer_consume_token (parser->lexer);
5747 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5751 case CPP_CLOSE_PAREN:
5752 /* Consume the ")" token. */
5753 cp_lexer_consume_token (parser->lexer);
5757 /* Error. We know the following require will fail, but
5758 that gives the proper error message. */
5759 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5760 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5761 expr = error_mark_node;
5767 /* If we're processing a template, we can't finish the semantics yet.
5768 Otherwise we can fold the entire expression now. */
5769 if (processing_template_decl)
5770 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5772 expr = fold_offsetof (expr);
5775 parser->integral_constant_expression_p = save_ice_p;
5776 parser->non_integral_constant_expression_p = save_non_ice_p;
5781 /* Statements [gram.stmt.stmt] */
5783 /* Parse a statement.
5787 expression-statement
5792 declaration-statement
5796 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5800 location_t statement_location;
5802 /* There is no statement yet. */
5803 statement = NULL_TREE;
5804 /* Peek at the next token. */
5805 token = cp_lexer_peek_token (parser->lexer);
5806 /* Remember the location of the first token in the statement. */
5807 statement_location = token->location;
5808 /* If this is a keyword, then that will often determine what kind of
5809 statement we have. */
5810 if (token->type == CPP_KEYWORD)
5812 enum rid keyword = token->keyword;
5818 statement = cp_parser_labeled_statement (parser,
5824 statement = cp_parser_selection_statement (parser);
5830 statement = cp_parser_iteration_statement (parser);
5837 statement = cp_parser_jump_statement (parser);
5841 statement = cp_parser_try_block (parser);
5845 /* It might be a keyword like `int' that can start a
5846 declaration-statement. */
5850 else if (token->type == CPP_NAME)
5852 /* If the next token is a `:', then we are looking at a
5853 labeled-statement. */
5854 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5855 if (token->type == CPP_COLON)
5856 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5858 /* Anything that starts with a `{' must be a compound-statement. */
5859 else if (token->type == CPP_OPEN_BRACE)
5860 statement = cp_parser_compound_statement (parser, NULL, false);
5861 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5862 a statement all its own. */
5863 else if (token->type == CPP_PRAGMA)
5865 cp_lexer_handle_pragma (parser->lexer);
5869 /* Everything else must be a declaration-statement or an
5870 expression-statement. Try for the declaration-statement
5871 first, unless we are looking at a `;', in which case we know that
5872 we have an expression-statement. */
5875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5877 cp_parser_parse_tentatively (parser);
5878 /* Try to parse the declaration-statement. */
5879 cp_parser_declaration_statement (parser);
5880 /* If that worked, we're done. */
5881 if (cp_parser_parse_definitely (parser))
5884 /* Look for an expression-statement instead. */
5885 statement = cp_parser_expression_statement (parser, in_statement_expr);
5888 /* Set the line number for the statement. */
5889 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5890 SET_EXPR_LOCATION (statement, statement_location);
5893 /* Parse a labeled-statement.
5896 identifier : statement
5897 case constant-expression : statement
5903 case constant-expression ... constant-expression : statement
5905 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5906 For an ordinary label, returns a LABEL_EXPR. */
5909 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5912 tree statement = error_mark_node;
5914 /* The next token should be an identifier. */
5915 token = cp_lexer_peek_token (parser->lexer);
5916 if (token->type != CPP_NAME
5917 && token->type != CPP_KEYWORD)
5919 cp_parser_error (parser, "expected labeled-statement");
5920 return error_mark_node;
5923 switch (token->keyword)
5930 /* Consume the `case' token. */
5931 cp_lexer_consume_token (parser->lexer);
5932 /* Parse the constant-expression. */
5933 expr = cp_parser_constant_expression (parser,
5934 /*allow_non_constant_p=*/false,
5937 ellipsis = cp_lexer_peek_token (parser->lexer);
5938 if (ellipsis->type == CPP_ELLIPSIS)
5940 /* Consume the `...' token. */
5941 cp_lexer_consume_token (parser->lexer);
5943 cp_parser_constant_expression (parser,
5944 /*allow_non_constant_p=*/false,
5946 /* We don't need to emit warnings here, as the common code
5947 will do this for us. */
5950 expr_hi = NULL_TREE;
5952 if (!parser->in_switch_statement_p)
5953 error ("case label %qE not within a switch statement", expr);
5955 statement = finish_case_label (expr, expr_hi);
5960 /* Consume the `default' token. */
5961 cp_lexer_consume_token (parser->lexer);
5962 if (!parser->in_switch_statement_p)
5963 error ("case label not within a switch statement");
5965 statement = finish_case_label (NULL_TREE, NULL_TREE);
5969 /* Anything else must be an ordinary label. */
5970 statement = finish_label_stmt (cp_parser_identifier (parser));
5974 /* Require the `:' token. */
5975 cp_parser_require (parser, CPP_COLON, "`:'");
5976 /* Parse the labeled statement. */
5977 cp_parser_statement (parser, in_statement_expr);
5979 /* Return the label, in the case of a `case' or `default' label. */
5983 /* Parse an expression-statement.
5985 expression-statement:
5988 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5989 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5990 indicates whether this expression-statement is part of an
5991 expression statement. */
5994 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
5996 tree statement = NULL_TREE;
5998 /* If the next token is a ';', then there is no expression
6000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6001 statement = cp_parser_expression (parser);
6003 /* Consume the final `;'. */
6004 cp_parser_consume_semicolon_at_end_of_statement (parser);
6006 if (in_statement_expr
6007 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6009 /* This is the final expression statement of a statement
6011 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6014 statement = finish_expr_stmt (statement);
6021 /* Parse a compound-statement.
6024 { statement-seq [opt] }
6026 Returns a tree representing the statement. */
6029 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6034 /* Consume the `{'. */
6035 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6036 return error_mark_node;
6037 /* Begin the compound-statement. */
6038 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6039 /* Parse an (optional) statement-seq. */
6040 cp_parser_statement_seq_opt (parser, in_statement_expr);
6041 /* Finish the compound-statement. */
6042 finish_compound_stmt (compound_stmt);
6043 /* Consume the `}'. */
6044 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6046 return compound_stmt;
6049 /* Parse an (optional) statement-seq.
6053 statement-seq [opt] statement */
6056 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6058 /* Scan statements until there aren't any more. */
6061 /* If we're looking at a `}', then we've run out of statements. */
6062 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6063 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6066 /* Parse the statement. */
6067 cp_parser_statement (parser, in_statement_expr);
6071 /* Parse a selection-statement.
6073 selection-statement:
6074 if ( condition ) statement
6075 if ( condition ) statement else statement
6076 switch ( condition ) statement
6078 Returns the new IF_STMT or SWITCH_STMT. */
6081 cp_parser_selection_statement (cp_parser* parser)
6086 /* Peek at the next token. */
6087 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6089 /* See what kind of keyword it is. */
6090 keyword = token->keyword;
6099 /* Look for the `('. */
6100 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6102 cp_parser_skip_to_end_of_statement (parser);
6103 return error_mark_node;
6106 /* Begin the selection-statement. */
6107 if (keyword == RID_IF)
6108 statement = begin_if_stmt ();
6110 statement = begin_switch_stmt ();
6112 /* Parse the condition. */
6113 condition = cp_parser_condition (parser);
6114 /* Look for the `)'. */
6115 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6116 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6117 /*consume_paren=*/true);
6119 if (keyword == RID_IF)
6121 /* Add the condition. */
6122 finish_if_stmt_cond (condition, statement);
6124 /* Parse the then-clause. */
6125 cp_parser_implicitly_scoped_statement (parser);
6126 finish_then_clause (statement);
6128 /* If the next token is `else', parse the else-clause. */
6129 if (cp_lexer_next_token_is_keyword (parser->lexer,
6132 /* Consume the `else' keyword. */
6133 cp_lexer_consume_token (parser->lexer);
6134 begin_else_clause (statement);
6135 /* Parse the else-clause. */
6136 cp_parser_implicitly_scoped_statement (parser);
6137 finish_else_clause (statement);
6140 /* Now we're all done with the if-statement. */
6141 finish_if_stmt (statement);
6145 bool in_switch_statement_p;
6147 /* Add the condition. */
6148 finish_switch_cond (condition, statement);
6150 /* Parse the body of the switch-statement. */
6151 in_switch_statement_p = parser->in_switch_statement_p;
6152 parser->in_switch_statement_p = true;
6153 cp_parser_implicitly_scoped_statement (parser);
6154 parser->in_switch_statement_p = in_switch_statement_p;
6156 /* Now we're all done with the switch-statement. */
6157 finish_switch_stmt (statement);
6165 cp_parser_error (parser, "expected selection-statement");
6166 return error_mark_node;
6170 /* Parse a condition.
6174 type-specifier-seq declarator = assignment-expression
6179 type-specifier-seq declarator asm-specification [opt]
6180 attributes [opt] = assignment-expression
6182 Returns the expression that should be tested. */
6185 cp_parser_condition (cp_parser* parser)
6187 cp_decl_specifier_seq type_specifiers;
6188 const char *saved_message;
6190 /* Try the declaration first. */
6191 cp_parser_parse_tentatively (parser);
6192 /* New types are not allowed in the type-specifier-seq for a
6194 saved_message = parser->type_definition_forbidden_message;
6195 parser->type_definition_forbidden_message
6196 = "types may not be defined in conditions";
6197 /* Parse the type-specifier-seq. */
6198 cp_parser_type_specifier_seq (parser, &type_specifiers);
6199 /* Restore the saved message. */
6200 parser->type_definition_forbidden_message = saved_message;
6201 /* If all is well, we might be looking at a declaration. */
6202 if (!cp_parser_error_occurred (parser))
6205 tree asm_specification;
6207 cp_declarator *declarator;
6208 tree initializer = NULL_TREE;
6210 /* Parse the declarator. */
6211 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6212 /*ctor_dtor_or_conv_p=*/NULL,
6213 /*parenthesized_p=*/NULL,
6214 /*member_p=*/false);
6215 /* Parse the attributes. */
6216 attributes = cp_parser_attributes_opt (parser);
6217 /* Parse the asm-specification. */
6218 asm_specification = cp_parser_asm_specification_opt (parser);
6219 /* If the next token is not an `=', then we might still be
6220 looking at an expression. For example:
6224 looks like a decl-specifier-seq and a declarator -- but then
6225 there is no `=', so this is an expression. */
6226 cp_parser_require (parser, CPP_EQ, "`='");
6227 /* If we did see an `=', then we are looking at a declaration
6229 if (cp_parser_parse_definitely (parser))
6233 /* Create the declaration. */
6234 decl = start_decl (declarator, &type_specifiers,
6235 /*initialized_p=*/true,
6236 attributes, /*prefix_attributes=*/NULL_TREE,
6238 /* Parse the assignment-expression. */
6239 initializer = cp_parser_assignment_expression (parser);
6241 /* Process the initializer. */
6242 cp_finish_decl (decl,
6245 LOOKUP_ONLYCONVERTING);
6248 pop_scope (DECL_CONTEXT (decl));
6250 return convert_from_reference (decl);
6253 /* If we didn't even get past the declarator successfully, we are
6254 definitely not looking at a declaration. */
6256 cp_parser_abort_tentative_parse (parser);
6258 /* Otherwise, we are looking at an expression. */
6259 return cp_parser_expression (parser);
6262 /* Parse an iteration-statement.
6264 iteration-statement:
6265 while ( condition ) statement
6266 do statement while ( expression ) ;
6267 for ( for-init-statement condition [opt] ; expression [opt] )
6270 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6273 cp_parser_iteration_statement (cp_parser* parser)
6278 bool in_iteration_statement_p;
6281 /* Peek at the next token. */
6282 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6284 return error_mark_node;
6286 /* Remember whether or not we are already within an iteration
6288 in_iteration_statement_p = parser->in_iteration_statement_p;
6290 /* See what kind of keyword it is. */
6291 keyword = token->keyword;
6298 /* Begin the while-statement. */
6299 statement = begin_while_stmt ();
6300 /* Look for the `('. */
6301 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6302 /* Parse the condition. */
6303 condition = cp_parser_condition (parser);
6304 finish_while_stmt_cond (condition, statement);
6305 /* Look for the `)'. */
6306 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6307 /* Parse the dependent statement. */
6308 parser->in_iteration_statement_p = true;
6309 cp_parser_already_scoped_statement (parser);
6310 parser->in_iteration_statement_p = in_iteration_statement_p;
6311 /* We're done with the while-statement. */
6312 finish_while_stmt (statement);
6320 /* Begin the do-statement. */
6321 statement = begin_do_stmt ();
6322 /* Parse the body of the do-statement. */
6323 parser->in_iteration_statement_p = true;
6324 cp_parser_implicitly_scoped_statement (parser);
6325 parser->in_iteration_statement_p = in_iteration_statement_p;
6326 finish_do_body (statement);
6327 /* Look for the `while' keyword. */
6328 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6329 /* Look for the `('. */
6330 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6331 /* Parse the expression. */
6332 expression = cp_parser_expression (parser);
6333 /* We're done with the do-statement. */
6334 finish_do_stmt (expression, statement);
6335 /* Look for the `)'. */
6336 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6337 /* Look for the `;'. */
6338 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6344 tree condition = NULL_TREE;
6345 tree expression = NULL_TREE;
6347 /* Begin the for-statement. */
6348 statement = begin_for_stmt ();
6349 /* Look for the `('. */
6350 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6351 /* Parse the initialization. */
6352 cp_parser_for_init_statement (parser);
6353 finish_for_init_stmt (statement);
6355 /* If there's a condition, process it. */
6356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6357 condition = cp_parser_condition (parser);
6358 finish_for_cond (condition, statement);
6359 /* Look for the `;'. */
6360 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6362 /* If there's an expression, process it. */
6363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6364 expression = cp_parser_expression (parser);
6365 finish_for_expr (expression, statement);
6366 /* Look for the `)'. */
6367 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6369 /* Parse the body of the for-statement. */
6370 parser->in_iteration_statement_p = true;
6371 cp_parser_already_scoped_statement (parser);
6372 parser->in_iteration_statement_p = in_iteration_statement_p;
6374 /* We're done with the for-statement. */
6375 finish_for_stmt (statement);
6380 cp_parser_error (parser, "expected iteration-statement");
6381 statement = error_mark_node;
6388 /* Parse a for-init-statement.
6391 expression-statement
6392 simple-declaration */
6395 cp_parser_for_init_statement (cp_parser* parser)
6397 /* If the next token is a `;', then we have an empty
6398 expression-statement. Grammatically, this is also a
6399 simple-declaration, but an invalid one, because it does not
6400 declare anything. Therefore, if we did not handle this case
6401 specially, we would issue an error message about an invalid
6403 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6405 /* We're going to speculatively look for a declaration, falling back
6406 to an expression, if necessary. */
6407 cp_parser_parse_tentatively (parser);
6408 /* Parse the declaration. */
6409 cp_parser_simple_declaration (parser,
6410 /*function_definition_allowed_p=*/false);
6411 /* If the tentative parse failed, then we shall need to look for an
6412 expression-statement. */
6413 if (cp_parser_parse_definitely (parser))
6417 cp_parser_expression_statement (parser, false);
6420 /* Parse a jump-statement.
6425 return expression [opt] ;
6433 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6436 cp_parser_jump_statement (cp_parser* parser)
6438 tree statement = error_mark_node;
6442 /* Peek at the next token. */
6443 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6445 return error_mark_node;
6447 /* See what kind of keyword it is. */
6448 keyword = token->keyword;
6452 if (!parser->in_switch_statement_p
6453 && !parser->in_iteration_statement_p)
6455 error ("break statement not within loop or switch");
6456 statement = error_mark_node;
6459 statement = finish_break_stmt ();
6460 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6464 if (!parser->in_iteration_statement_p)
6466 error ("continue statement not within a loop");
6467 statement = error_mark_node;
6470 statement = finish_continue_stmt ();
6471 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6478 /* If the next token is a `;', then there is no
6480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6481 expr = cp_parser_expression (parser);
6484 /* Build the return-statement. */
6485 statement = finish_return_stmt (expr);
6486 /* Look for the final `;'. */
6487 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6492 /* Create the goto-statement. */
6493 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6495 /* Issue a warning about this use of a GNU extension. */
6497 pedwarn ("ISO C++ forbids computed gotos");
6498 /* Consume the '*' token. */
6499 cp_lexer_consume_token (parser->lexer);
6500 /* Parse the dependent expression. */
6501 finish_goto_stmt (cp_parser_expression (parser));
6504 finish_goto_stmt (cp_parser_identifier (parser));
6505 /* Look for the final `;'. */
6506 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6510 cp_parser_error (parser, "expected jump-statement");
6517 /* Parse a declaration-statement.
6519 declaration-statement:
6520 block-declaration */
6523 cp_parser_declaration_statement (cp_parser* parser)
6527 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6528 p = obstack_alloc (&declarator_obstack, 0);
6530 /* Parse the block-declaration. */
6531 cp_parser_block_declaration (parser, /*statement_p=*/true);
6533 /* Free any declarators allocated. */
6534 obstack_free (&declarator_obstack, p);
6536 /* Finish off the statement. */
6540 /* Some dependent statements (like `if (cond) statement'), are
6541 implicitly in their own scope. In other words, if the statement is
6542 a single statement (as opposed to a compound-statement), it is
6543 none-the-less treated as if it were enclosed in braces. Any
6544 declarations appearing in the dependent statement are out of scope
6545 after control passes that point. This function parses a statement,
6546 but ensures that is in its own scope, even if it is not a
6549 Returns the new statement. */
6552 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6556 /* If the token is not a `{', then we must take special action. */
6557 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6559 /* Create a compound-statement. */
6560 statement = begin_compound_stmt (0);
6561 /* Parse the dependent-statement. */
6562 cp_parser_statement (parser, false);
6563 /* Finish the dummy compound-statement. */
6564 finish_compound_stmt (statement);
6566 /* Otherwise, we simply parse the statement directly. */
6568 statement = cp_parser_compound_statement (parser, NULL, false);
6570 /* Return the statement. */
6574 /* For some dependent statements (like `while (cond) statement'), we
6575 have already created a scope. Therefore, even if the dependent
6576 statement is a compound-statement, we do not want to create another
6580 cp_parser_already_scoped_statement (cp_parser* parser)
6582 /* If the token is a `{', then we must take special action. */
6583 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6584 cp_parser_statement (parser, false);
6587 /* Avoid calling cp_parser_compound_statement, so that we
6588 don't create a new scope. Do everything else by hand. */
6589 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6590 cp_parser_statement_seq_opt (parser, false);
6591 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6595 /* Declarations [gram.dcl.dcl] */
6597 /* Parse an optional declaration-sequence.
6601 declaration-seq declaration */
6604 cp_parser_declaration_seq_opt (cp_parser* parser)
6610 token = cp_lexer_peek_token (parser->lexer);
6612 if (token->type == CPP_CLOSE_BRACE
6613 || token->type == CPP_EOF)
6616 if (token->type == CPP_SEMICOLON)
6618 /* A declaration consisting of a single semicolon is
6619 invalid. Allow it unless we're being pedantic. */
6620 cp_lexer_consume_token (parser->lexer);
6621 if (pedantic && !in_system_header)
6622 pedwarn ("extra %<;%>");
6626 /* If we're entering or exiting a region that's implicitly
6627 extern "C", modify the lang context appropriately. */
6628 if (!parser->implicit_extern_c && token->implicit_extern_c)
6630 push_lang_context (lang_name_c);
6631 parser->implicit_extern_c = true;
6633 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6635 pop_lang_context ();
6636 parser->implicit_extern_c = false;
6639 if (token->type == CPP_PRAGMA)
6641 /* A top-level declaration can consist solely of a #pragma.
6642 A nested declaration cannot, so this is done here and not
6643 in cp_parser_declaration. (A #pragma at block scope is
6644 handled in cp_parser_statement.) */
6645 cp_lexer_handle_pragma (parser->lexer);
6649 /* Parse the declaration itself. */
6650 cp_parser_declaration (parser);
6654 /* Parse a declaration.
6659 template-declaration
6660 explicit-instantiation
6661 explicit-specialization
6662 linkage-specification
6663 namespace-definition
6668 __extension__ declaration */
6671 cp_parser_declaration (cp_parser* parser)
6678 /* Check for the `__extension__' keyword. */
6679 if (cp_parser_extension_opt (parser, &saved_pedantic))
6681 /* Parse the qualified declaration. */
6682 cp_parser_declaration (parser);
6683 /* Restore the PEDANTIC flag. */
6684 pedantic = saved_pedantic;
6689 /* Try to figure out what kind of declaration is present. */
6690 token1 = *cp_lexer_peek_token (parser->lexer);
6692 if (token1.type != CPP_EOF)
6693 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6695 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6696 p = obstack_alloc (&declarator_obstack, 0);
6698 /* If the next token is `extern' and the following token is a string
6699 literal, then we have a linkage specification. */
6700 if (token1.keyword == RID_EXTERN
6701 && cp_parser_is_string_literal (&token2))
6702 cp_parser_linkage_specification (parser);
6703 /* If the next token is `template', then we have either a template
6704 declaration, an explicit instantiation, or an explicit
6706 else if (token1.keyword == RID_TEMPLATE)
6708 /* `template <>' indicates a template specialization. */
6709 if (token2.type == CPP_LESS
6710 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6711 cp_parser_explicit_specialization (parser);
6712 /* `template <' indicates a template declaration. */
6713 else if (token2.type == CPP_LESS)
6714 cp_parser_template_declaration (parser, /*member_p=*/false);
6715 /* Anything else must be an explicit instantiation. */
6717 cp_parser_explicit_instantiation (parser);
6719 /* If the next token is `export', then we have a template
6721 else if (token1.keyword == RID_EXPORT)
6722 cp_parser_template_declaration (parser, /*member_p=*/false);
6723 /* If the next token is `extern', 'static' or 'inline' and the one
6724 after that is `template', we have a GNU extended explicit
6725 instantiation directive. */
6726 else if (cp_parser_allow_gnu_extensions_p (parser)
6727 && (token1.keyword == RID_EXTERN
6728 || token1.keyword == RID_STATIC
6729 || token1.keyword == RID_INLINE)
6730 && token2.keyword == RID_TEMPLATE)
6731 cp_parser_explicit_instantiation (parser);
6732 /* If the next token is `namespace', check for a named or unnamed
6733 namespace definition. */
6734 else if (token1.keyword == RID_NAMESPACE
6735 && (/* A named namespace definition. */
6736 (token2.type == CPP_NAME
6737 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6739 /* An unnamed namespace definition. */
6740 || token2.type == CPP_OPEN_BRACE))
6741 cp_parser_namespace_definition (parser);
6742 /* We must have either a block declaration or a function
6745 /* Try to parse a block-declaration, or a function-definition. */
6746 cp_parser_block_declaration (parser, /*statement_p=*/false);
6748 /* Free any declarators allocated. */
6749 obstack_free (&declarator_obstack, p);
6752 /* Parse a block-declaration.
6757 namespace-alias-definition
6764 __extension__ block-declaration
6767 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6768 part of a declaration-statement. */
6771 cp_parser_block_declaration (cp_parser *parser,
6777 /* Check for the `__extension__' keyword. */
6778 if (cp_parser_extension_opt (parser, &saved_pedantic))
6780 /* Parse the qualified declaration. */
6781 cp_parser_block_declaration (parser, statement_p);
6782 /* Restore the PEDANTIC flag. */
6783 pedantic = saved_pedantic;
6788 /* Peek at the next token to figure out which kind of declaration is
6790 token1 = cp_lexer_peek_token (parser->lexer);
6792 /* If the next keyword is `asm', we have an asm-definition. */
6793 if (token1->keyword == RID_ASM)
6796 cp_parser_commit_to_tentative_parse (parser);
6797 cp_parser_asm_definition (parser);
6799 /* If the next keyword is `namespace', we have a
6800 namespace-alias-definition. */
6801 else if (token1->keyword == RID_NAMESPACE)
6802 cp_parser_namespace_alias_definition (parser);
6803 /* If the next keyword is `using', we have either a
6804 using-declaration or a using-directive. */
6805 else if (token1->keyword == RID_USING)
6810 cp_parser_commit_to_tentative_parse (parser);
6811 /* If the token after `using' is `namespace', then we have a
6813 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6814 if (token2->keyword == RID_NAMESPACE)
6815 cp_parser_using_directive (parser);
6816 /* Otherwise, it's a using-declaration. */
6818 cp_parser_using_declaration (parser);
6820 /* If the next keyword is `__label__' we have a label declaration. */
6821 else if (token1->keyword == RID_LABEL)
6824 cp_parser_commit_to_tentative_parse (parser);
6825 cp_parser_label_declaration (parser);
6827 /* Anything else must be a simple-declaration. */
6829 cp_parser_simple_declaration (parser, !statement_p);
6832 /* Parse a simple-declaration.
6835 decl-specifier-seq [opt] init-declarator-list [opt] ;
6837 init-declarator-list:
6839 init-declarator-list , init-declarator
6841 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6842 function-definition as a simple-declaration. */
6845 cp_parser_simple_declaration (cp_parser* parser,
6846 bool function_definition_allowed_p)
6848 cp_decl_specifier_seq decl_specifiers;
6849 int declares_class_or_enum;
6850 bool saw_declarator;
6852 /* Defer access checks until we know what is being declared; the
6853 checks for names appearing in the decl-specifier-seq should be
6854 done as if we were in the scope of the thing being declared. */
6855 push_deferring_access_checks (dk_deferred);
6857 /* Parse the decl-specifier-seq. We have to keep track of whether
6858 or not the decl-specifier-seq declares a named class or
6859 enumeration type, since that is the only case in which the
6860 init-declarator-list is allowed to be empty.
6864 In a simple-declaration, the optional init-declarator-list can be
6865 omitted only when declaring a class or enumeration, that is when
6866 the decl-specifier-seq contains either a class-specifier, an
6867 elaborated-type-specifier, or an enum-specifier. */
6868 cp_parser_decl_specifier_seq (parser,
6869 CP_PARSER_FLAGS_OPTIONAL,
6871 &declares_class_or_enum);
6872 /* We no longer need to defer access checks. */
6873 stop_deferring_access_checks ();
6875 /* In a block scope, a valid declaration must always have a
6876 decl-specifier-seq. By not trying to parse declarators, we can
6877 resolve the declaration/expression ambiguity more quickly. */
6878 if (!function_definition_allowed_p
6879 && !decl_specifiers.any_specifiers_p)
6881 cp_parser_error (parser, "expected declaration");
6885 /* If the next two tokens are both identifiers, the code is
6886 erroneous. The usual cause of this situation is code like:
6890 where "T" should name a type -- but does not. */
6891 if (!decl_specifiers.type
6892 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6894 /* If parsing tentatively, we should commit; we really are
6895 looking at a declaration. */
6896 cp_parser_commit_to_tentative_parse (parser);
6901 /* If we have seen at least one decl-specifier, and the next token
6902 is not a parenthesis, then we must be looking at a declaration.
6903 (After "int (" we might be looking at a functional cast.) */
6904 if (decl_specifiers.any_specifiers_p
6905 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6906 cp_parser_commit_to_tentative_parse (parser);
6908 /* Keep going until we hit the `;' at the end of the simple
6910 saw_declarator = false;
6911 while (cp_lexer_next_token_is_not (parser->lexer,
6915 bool function_definition_p;
6918 saw_declarator = true;
6919 /* Parse the init-declarator. */
6920 decl = cp_parser_init_declarator (parser, &decl_specifiers,
6921 function_definition_allowed_p,
6923 declares_class_or_enum,
6924 &function_definition_p);
6925 /* If an error occurred while parsing tentatively, exit quickly.
6926 (That usually happens when in the body of a function; each
6927 statement is treated as a declaration-statement until proven
6929 if (cp_parser_error_occurred (parser))
6931 /* Handle function definitions specially. */
6932 if (function_definition_p)
6934 /* If the next token is a `,', then we are probably
6935 processing something like:
6939 which is erroneous. */
6940 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6941 error ("mixing declarations and function-definitions is forbidden");
6942 /* Otherwise, we're done with the list of declarators. */
6945 pop_deferring_access_checks ();
6949 /* The next token should be either a `,' or a `;'. */
6950 token = cp_lexer_peek_token (parser->lexer);
6951 /* If it's a `,', there are more declarators to come. */
6952 if (token->type == CPP_COMMA)
6953 cp_lexer_consume_token (parser->lexer);
6954 /* If it's a `;', we are done. */
6955 else if (token->type == CPP_SEMICOLON)
6957 /* Anything else is an error. */
6960 /* If we have already issued an error message we don't need
6961 to issue another one. */
6962 if (decl != error_mark_node
6963 || (cp_parser_parsing_tentatively (parser)
6964 && !cp_parser_committed_to_tentative_parse (parser)))
6965 cp_parser_error (parser, "expected %<,%> or %<;%>");
6966 /* Skip tokens until we reach the end of the statement. */
6967 cp_parser_skip_to_end_of_statement (parser);
6968 /* If the next token is now a `;', consume it. */
6969 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6970 cp_lexer_consume_token (parser->lexer);
6973 /* After the first time around, a function-definition is not
6974 allowed -- even if it was OK at first. For example:
6979 function_definition_allowed_p = false;
6982 /* Issue an error message if no declarators are present, and the
6983 decl-specifier-seq does not itself declare a class or
6985 if (!saw_declarator)
6987 if (cp_parser_declares_only_class_p (parser))
6988 shadow_tag (&decl_specifiers);
6989 /* Perform any deferred access checks. */
6990 perform_deferred_access_checks ();
6993 /* Consume the `;'. */
6994 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6997 pop_deferring_access_checks ();
7000 /* Parse a decl-specifier-seq.
7003 decl-specifier-seq [opt] decl-specifier
7006 storage-class-specifier
7017 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7019 The parser flags FLAGS is used to control type-specifier parsing.
7021 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7024 1: one of the decl-specifiers is an elaborated-type-specifier
7025 (i.e., a type declaration)
7026 2: one of the decl-specifiers is an enum-specifier or a
7027 class-specifier (i.e., a type definition)
7032 cp_parser_decl_specifier_seq (cp_parser* parser,
7033 cp_parser_flags flags,
7034 cp_decl_specifier_seq *decl_specs,
7035 int* declares_class_or_enum)
7037 bool constructor_possible_p = !parser->in_declarator_p;
7039 /* Clear DECL_SPECS. */
7040 clear_decl_specs (decl_specs);
7042 /* Assume no class or enumeration type is declared. */
7043 *declares_class_or_enum = 0;
7045 /* Keep reading specifiers until there are no more to read. */
7049 bool found_decl_spec;
7052 /* Peek at the next token. */
7053 token = cp_lexer_peek_token (parser->lexer);
7054 /* Handle attributes. */
7055 if (token->keyword == RID_ATTRIBUTE)
7057 /* Parse the attributes. */
7058 decl_specs->attributes
7059 = chainon (decl_specs->attributes,
7060 cp_parser_attributes_opt (parser));
7063 /* Assume we will find a decl-specifier keyword. */
7064 found_decl_spec = true;
7065 /* If the next token is an appropriate keyword, we can simply
7066 add it to the list. */
7067 switch (token->keyword)
7072 if (decl_specs->specs[(int) ds_friend]++)
7073 error ("duplicate %<friend%>");
7074 /* Consume the token. */
7075 cp_lexer_consume_token (parser->lexer);
7078 /* function-specifier:
7085 cp_parser_function_specifier_opt (parser, decl_specs);
7091 ++decl_specs->specs[(int) ds_typedef];
7092 /* Consume the token. */
7093 cp_lexer_consume_token (parser->lexer);
7094 /* A constructor declarator cannot appear in a typedef. */
7095 constructor_possible_p = false;
7096 /* The "typedef" keyword can only occur in a declaration; we
7097 may as well commit at this point. */
7098 cp_parser_commit_to_tentative_parse (parser);
7101 /* storage-class-specifier:
7111 /* Consume the token. */
7112 cp_lexer_consume_token (parser->lexer);
7113 cp_parser_set_storage_class (decl_specs, sc_auto);
7116 /* Consume the token. */
7117 cp_lexer_consume_token (parser->lexer);
7118 cp_parser_set_storage_class (decl_specs, sc_register);
7121 /* Consume the token. */
7122 cp_lexer_consume_token (parser->lexer);
7123 if (decl_specs->specs[(int) ds_thread])
7125 error ("%<__thread%> before %<static%>");
7126 decl_specs->specs[(int) ds_thread] = 0;
7128 cp_parser_set_storage_class (decl_specs, sc_static);
7131 /* Consume the token. */
7132 cp_lexer_consume_token (parser->lexer);
7133 if (decl_specs->specs[(int) ds_thread])
7135 error ("%<__thread%> before %<extern%>");
7136 decl_specs->specs[(int) ds_thread] = 0;
7138 cp_parser_set_storage_class (decl_specs, sc_extern);
7141 /* Consume the token. */
7142 cp_lexer_consume_token (parser->lexer);
7143 cp_parser_set_storage_class (decl_specs, sc_mutable);
7146 /* Consume the token. */
7147 cp_lexer_consume_token (parser->lexer);
7148 ++decl_specs->specs[(int) ds_thread];
7152 /* We did not yet find a decl-specifier yet. */
7153 found_decl_spec = false;
7157 /* Constructors are a special case. The `S' in `S()' is not a
7158 decl-specifier; it is the beginning of the declarator. */
7161 && constructor_possible_p
7162 && (cp_parser_constructor_declarator_p
7163 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7165 /* If we don't have a DECL_SPEC yet, then we must be looking at
7166 a type-specifier. */
7167 if (!found_decl_spec && !constructor_p)
7169 int decl_spec_declares_class_or_enum;
7170 bool is_cv_qualifier;
7174 = cp_parser_type_specifier (parser, flags,
7176 /*is_declaration=*/true,
7177 &decl_spec_declares_class_or_enum,
7180 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7182 /* If this type-specifier referenced a user-defined type
7183 (a typedef, class-name, etc.), then we can't allow any
7184 more such type-specifiers henceforth.
7188 The longest sequence of decl-specifiers that could
7189 possibly be a type name is taken as the
7190 decl-specifier-seq of a declaration. The sequence shall
7191 be self-consistent as described below.
7195 As a general rule, at most one type-specifier is allowed
7196 in the complete decl-specifier-seq of a declaration. The
7197 only exceptions are the following:
7199 -- const or volatile can be combined with any other
7202 -- signed or unsigned can be combined with char, long,
7210 void g (const int Pc);
7212 Here, Pc is *not* part of the decl-specifier seq; it's
7213 the declarator. Therefore, once we see a type-specifier
7214 (other than a cv-qualifier), we forbid any additional
7215 user-defined types. We *do* still allow things like `int
7216 int' to be considered a decl-specifier-seq, and issue the
7217 error message later. */
7218 if (type_spec && !is_cv_qualifier)
7219 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7220 /* A constructor declarator cannot follow a type-specifier. */
7223 constructor_possible_p = false;
7224 found_decl_spec = true;
7228 /* If we still do not have a DECL_SPEC, then there are no more
7230 if (!found_decl_spec)
7233 decl_specs->any_specifiers_p = true;
7234 /* After we see one decl-specifier, further decl-specifiers are
7236 flags |= CP_PARSER_FLAGS_OPTIONAL;
7239 /* Don't allow a friend specifier with a class definition. */
7240 if (decl_specs->specs[(int) ds_friend] != 0
7241 && (*declares_class_or_enum & 2))
7242 error ("class definition may not be declared a friend");
7245 /* Parse an (optional) storage-class-specifier.
7247 storage-class-specifier:
7256 storage-class-specifier:
7259 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7262 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7264 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7272 /* Consume the token. */
7273 return cp_lexer_consume_token (parser->lexer)->value;
7280 /* Parse an (optional) function-specifier.
7287 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7288 Updates DECL_SPECS, if it is non-NULL. */
7291 cp_parser_function_specifier_opt (cp_parser* parser,
7292 cp_decl_specifier_seq *decl_specs)
7294 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7298 ++decl_specs->specs[(int) ds_inline];
7303 ++decl_specs->specs[(int) ds_virtual];
7308 ++decl_specs->specs[(int) ds_explicit];
7315 /* Consume the token. */
7316 return cp_lexer_consume_token (parser->lexer)->value;
7319 /* Parse a linkage-specification.
7321 linkage-specification:
7322 extern string-literal { declaration-seq [opt] }
7323 extern string-literal declaration */
7326 cp_parser_linkage_specification (cp_parser* parser)
7330 /* Look for the `extern' keyword. */
7331 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7333 /* Look for the string-literal. */
7334 linkage = cp_parser_string_literal (parser, false, false);
7336 /* Transform the literal into an identifier. If the literal is a
7337 wide-character string, or contains embedded NULs, then we can't
7338 handle it as the user wants. */
7339 if (strlen (TREE_STRING_POINTER (linkage))
7340 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7342 cp_parser_error (parser, "invalid linkage-specification");
7343 /* Assume C++ linkage. */
7344 linkage = lang_name_cplusplus;
7347 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7349 /* We're now using the new linkage. */
7350 push_lang_context (linkage);
7352 /* If the next token is a `{', then we're using the first
7354 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7356 /* Consume the `{' token. */
7357 cp_lexer_consume_token (parser->lexer);
7358 /* Parse the declarations. */
7359 cp_parser_declaration_seq_opt (parser);
7360 /* Look for the closing `}'. */
7361 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7363 /* Otherwise, there's just one declaration. */
7366 bool saved_in_unbraced_linkage_specification_p;
7368 saved_in_unbraced_linkage_specification_p
7369 = parser->in_unbraced_linkage_specification_p;
7370 parser->in_unbraced_linkage_specification_p = true;
7371 have_extern_spec = true;
7372 cp_parser_declaration (parser);
7373 have_extern_spec = false;
7374 parser->in_unbraced_linkage_specification_p
7375 = saved_in_unbraced_linkage_specification_p;
7378 /* We're done with the linkage-specification. */
7379 pop_lang_context ();
7382 /* Special member functions [gram.special] */
7384 /* Parse a conversion-function-id.
7386 conversion-function-id:
7387 operator conversion-type-id
7389 Returns an IDENTIFIER_NODE representing the operator. */
7392 cp_parser_conversion_function_id (cp_parser* parser)
7396 tree saved_qualifying_scope;
7397 tree saved_object_scope;
7400 /* Look for the `operator' token. */
7401 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7402 return error_mark_node;
7403 /* When we parse the conversion-type-id, the current scope will be
7404 reset. However, we need that information in able to look up the
7405 conversion function later, so we save it here. */
7406 saved_scope = parser->scope;
7407 saved_qualifying_scope = parser->qualifying_scope;
7408 saved_object_scope = parser->object_scope;
7409 /* We must enter the scope of the class so that the names of
7410 entities declared within the class are available in the
7411 conversion-type-id. For example, consider:
7418 S::operator I() { ... }
7420 In order to see that `I' is a type-name in the definition, we
7421 must be in the scope of `S'. */
7423 pop_p = push_scope (saved_scope);
7424 /* Parse the conversion-type-id. */
7425 type = cp_parser_conversion_type_id (parser);
7426 /* Leave the scope of the class, if any. */
7428 pop_scope (saved_scope);
7429 /* Restore the saved scope. */
7430 parser->scope = saved_scope;
7431 parser->qualifying_scope = saved_qualifying_scope;
7432 parser->object_scope = saved_object_scope;
7433 /* If the TYPE is invalid, indicate failure. */
7434 if (type == error_mark_node)
7435 return error_mark_node;
7436 return mangle_conv_op_name_for_type (type);
7439 /* Parse a conversion-type-id:
7442 type-specifier-seq conversion-declarator [opt]
7444 Returns the TYPE specified. */
7447 cp_parser_conversion_type_id (cp_parser* parser)
7450 cp_decl_specifier_seq type_specifiers;
7451 cp_declarator *declarator;
7452 tree type_specified;
7454 /* Parse the attributes. */
7455 attributes = cp_parser_attributes_opt (parser);
7456 /* Parse the type-specifiers. */
7457 cp_parser_type_specifier_seq (parser, &type_specifiers);
7458 /* If that didn't work, stop. */
7459 if (type_specifiers.type == error_mark_node)
7460 return error_mark_node;
7461 /* Parse the conversion-declarator. */
7462 declarator = cp_parser_conversion_declarator_opt (parser);
7464 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7465 /*initialized=*/0, &attributes);
7467 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7468 return type_specified;
7471 /* Parse an (optional) conversion-declarator.
7473 conversion-declarator:
7474 ptr-operator conversion-declarator [opt]
7478 static cp_declarator *
7479 cp_parser_conversion_declarator_opt (cp_parser* parser)
7481 enum tree_code code;
7483 cp_cv_quals cv_quals;
7485 /* We don't know if there's a ptr-operator next, or not. */
7486 cp_parser_parse_tentatively (parser);
7487 /* Try the ptr-operator. */
7488 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7489 /* If it worked, look for more conversion-declarators. */
7490 if (cp_parser_parse_definitely (parser))
7492 cp_declarator *declarator;
7494 /* Parse another optional declarator. */
7495 declarator = cp_parser_conversion_declarator_opt (parser);
7497 /* Create the representation of the declarator. */
7499 declarator = make_ptrmem_declarator (cv_quals, class_type,
7501 else if (code == INDIRECT_REF)
7502 declarator = make_pointer_declarator (cv_quals, declarator);
7504 declarator = make_reference_declarator (cv_quals, declarator);
7512 /* Parse an (optional) ctor-initializer.
7515 : mem-initializer-list
7517 Returns TRUE iff the ctor-initializer was actually present. */
7520 cp_parser_ctor_initializer_opt (cp_parser* parser)
7522 /* If the next token is not a `:', then there is no
7523 ctor-initializer. */
7524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7526 /* Do default initialization of any bases and members. */
7527 if (DECL_CONSTRUCTOR_P (current_function_decl))
7528 finish_mem_initializers (NULL_TREE);
7533 /* Consume the `:' token. */
7534 cp_lexer_consume_token (parser->lexer);
7535 /* And the mem-initializer-list. */
7536 cp_parser_mem_initializer_list (parser);
7541 /* Parse a mem-initializer-list.
7543 mem-initializer-list:
7545 mem-initializer , mem-initializer-list */
7548 cp_parser_mem_initializer_list (cp_parser* parser)
7550 tree mem_initializer_list = NULL_TREE;
7552 /* Let the semantic analysis code know that we are starting the
7553 mem-initializer-list. */
7554 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7555 error ("only constructors take base initializers");
7557 /* Loop through the list. */
7560 tree mem_initializer;
7562 /* Parse the mem-initializer. */
7563 mem_initializer = cp_parser_mem_initializer (parser);
7564 /* Add it to the list, unless it was erroneous. */
7565 if (mem_initializer)
7567 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7568 mem_initializer_list = mem_initializer;
7570 /* If the next token is not a `,', we're done. */
7571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7573 /* Consume the `,' token. */
7574 cp_lexer_consume_token (parser->lexer);
7577 /* Perform semantic analysis. */
7578 if (DECL_CONSTRUCTOR_P (current_function_decl))
7579 finish_mem_initializers (mem_initializer_list);
7582 /* Parse a mem-initializer.
7585 mem-initializer-id ( expression-list [opt] )
7590 ( expression-list [opt] )
7592 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7593 class) or FIELD_DECL (for a non-static data member) to initialize;
7594 the TREE_VALUE is the expression-list. */
7597 cp_parser_mem_initializer (cp_parser* parser)
7599 tree mem_initializer_id;
7600 tree expression_list;
7603 /* Find out what is being initialized. */
7604 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7606 pedwarn ("anachronistic old-style base class initializer");
7607 mem_initializer_id = NULL_TREE;
7610 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7611 member = expand_member_init (mem_initializer_id);
7612 if (member && !DECL_P (member))
7613 in_base_initializer = 1;
7616 = cp_parser_parenthesized_expression_list (parser, false,
7617 /*non_constant_p=*/NULL);
7618 if (!expression_list)
7619 expression_list = void_type_node;
7621 in_base_initializer = 0;
7623 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7626 /* Parse a mem-initializer-id.
7629 :: [opt] nested-name-specifier [opt] class-name
7632 Returns a TYPE indicating the class to be initializer for the first
7633 production. Returns an IDENTIFIER_NODE indicating the data member
7634 to be initialized for the second production. */
7637 cp_parser_mem_initializer_id (cp_parser* parser)
7639 bool global_scope_p;
7640 bool nested_name_specifier_p;
7641 bool template_p = false;
7644 /* `typename' is not allowed in this context ([temp.res]). */
7645 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7647 error ("keyword %<typename%> not allowed in this context (a qualified "
7648 "member initializer is implicitly a type)");
7649 cp_lexer_consume_token (parser->lexer);
7651 /* Look for the optional `::' operator. */
7653 = (cp_parser_global_scope_opt (parser,
7654 /*current_scope_valid_p=*/false)
7656 /* Look for the optional nested-name-specifier. The simplest way to
7661 The keyword `typename' is not permitted in a base-specifier or
7662 mem-initializer; in these contexts a qualified name that
7663 depends on a template-parameter is implicitly assumed to be a
7666 is to assume that we have seen the `typename' keyword at this
7668 nested_name_specifier_p
7669 = (cp_parser_nested_name_specifier_opt (parser,
7670 /*typename_keyword_p=*/true,
7671 /*check_dependency_p=*/true,
7673 /*is_declaration=*/true)
7675 if (nested_name_specifier_p)
7676 template_p = cp_parser_optional_template_keyword (parser);
7677 /* If there is a `::' operator or a nested-name-specifier, then we
7678 are definitely looking for a class-name. */
7679 if (global_scope_p || nested_name_specifier_p)
7680 return cp_parser_class_name (parser,
7681 /*typename_keyword_p=*/true,
7682 /*template_keyword_p=*/template_p,
7684 /*check_dependency_p=*/true,
7685 /*class_head_p=*/false,
7686 /*is_declaration=*/true);
7687 /* Otherwise, we could also be looking for an ordinary identifier. */
7688 cp_parser_parse_tentatively (parser);
7689 /* Try a class-name. */
7690 id = cp_parser_class_name (parser,
7691 /*typename_keyword_p=*/true,
7692 /*template_keyword_p=*/false,
7694 /*check_dependency_p=*/true,
7695 /*class_head_p=*/false,
7696 /*is_declaration=*/true);
7697 /* If we found one, we're done. */
7698 if (cp_parser_parse_definitely (parser))
7700 /* Otherwise, look for an ordinary identifier. */
7701 return cp_parser_identifier (parser);
7704 /* Overloading [gram.over] */
7706 /* Parse an operator-function-id.
7708 operator-function-id:
7711 Returns an IDENTIFIER_NODE for the operator which is a
7712 human-readable spelling of the identifier, e.g., `operator +'. */
7715 cp_parser_operator_function_id (cp_parser* parser)
7717 /* Look for the `operator' keyword. */
7718 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7719 return error_mark_node;
7720 /* And then the name of the operator itself. */
7721 return cp_parser_operator (parser);
7724 /* Parse an operator.
7727 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7728 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7729 || ++ -- , ->* -> () []
7736 Returns an IDENTIFIER_NODE for the operator which is a
7737 human-readable spelling of the identifier, e.g., `operator +'. */
7740 cp_parser_operator (cp_parser* parser)
7742 tree id = NULL_TREE;
7745 /* Peek at the next token. */
7746 token = cp_lexer_peek_token (parser->lexer);
7747 /* Figure out which operator we have. */
7748 switch (token->type)
7754 /* The keyword should be either `new' or `delete'. */
7755 if (token->keyword == RID_NEW)
7757 else if (token->keyword == RID_DELETE)
7762 /* Consume the `new' or `delete' token. */
7763 cp_lexer_consume_token (parser->lexer);
7765 /* Peek at the next token. */
7766 token = cp_lexer_peek_token (parser->lexer);
7767 /* If it's a `[' token then this is the array variant of the
7769 if (token->type == CPP_OPEN_SQUARE)
7771 /* Consume the `[' token. */
7772 cp_lexer_consume_token (parser->lexer);
7773 /* Look for the `]' token. */
7774 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7775 id = ansi_opname (op == NEW_EXPR
7776 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7778 /* Otherwise, we have the non-array variant. */
7780 id = ansi_opname (op);
7786 id = ansi_opname (PLUS_EXPR);
7790 id = ansi_opname (MINUS_EXPR);
7794 id = ansi_opname (MULT_EXPR);
7798 id = ansi_opname (TRUNC_DIV_EXPR);
7802 id = ansi_opname (TRUNC_MOD_EXPR);
7806 id = ansi_opname (BIT_XOR_EXPR);
7810 id = ansi_opname (BIT_AND_EXPR);
7814 id = ansi_opname (BIT_IOR_EXPR);
7818 id = ansi_opname (BIT_NOT_EXPR);
7822 id = ansi_opname (TRUTH_NOT_EXPR);
7826 id = ansi_assopname (NOP_EXPR);
7830 id = ansi_opname (LT_EXPR);
7834 id = ansi_opname (GT_EXPR);
7838 id = ansi_assopname (PLUS_EXPR);
7842 id = ansi_assopname (MINUS_EXPR);
7846 id = ansi_assopname (MULT_EXPR);
7850 id = ansi_assopname (TRUNC_DIV_EXPR);
7854 id = ansi_assopname (TRUNC_MOD_EXPR);
7858 id = ansi_assopname (BIT_XOR_EXPR);
7862 id = ansi_assopname (BIT_AND_EXPR);
7866 id = ansi_assopname (BIT_IOR_EXPR);
7870 id = ansi_opname (LSHIFT_EXPR);
7874 id = ansi_opname (RSHIFT_EXPR);
7878 id = ansi_assopname (LSHIFT_EXPR);
7882 id = ansi_assopname (RSHIFT_EXPR);
7886 id = ansi_opname (EQ_EXPR);
7890 id = ansi_opname (NE_EXPR);
7894 id = ansi_opname (LE_EXPR);
7897 case CPP_GREATER_EQ:
7898 id = ansi_opname (GE_EXPR);
7902 id = ansi_opname (TRUTH_ANDIF_EXPR);
7906 id = ansi_opname (TRUTH_ORIF_EXPR);
7910 id = ansi_opname (POSTINCREMENT_EXPR);
7913 case CPP_MINUS_MINUS:
7914 id = ansi_opname (PREDECREMENT_EXPR);
7918 id = ansi_opname (COMPOUND_EXPR);
7921 case CPP_DEREF_STAR:
7922 id = ansi_opname (MEMBER_REF);
7926 id = ansi_opname (COMPONENT_REF);
7929 case CPP_OPEN_PAREN:
7930 /* Consume the `('. */
7931 cp_lexer_consume_token (parser->lexer);
7932 /* Look for the matching `)'. */
7933 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7934 return ansi_opname (CALL_EXPR);
7936 case CPP_OPEN_SQUARE:
7937 /* Consume the `['. */
7938 cp_lexer_consume_token (parser->lexer);
7939 /* Look for the matching `]'. */
7940 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7941 return ansi_opname (ARRAY_REF);
7945 id = ansi_opname (MIN_EXPR);
7949 id = ansi_opname (MAX_EXPR);
7953 id = ansi_assopname (MIN_EXPR);
7957 id = ansi_assopname (MAX_EXPR);
7961 /* Anything else is an error. */
7965 /* If we have selected an identifier, we need to consume the
7968 cp_lexer_consume_token (parser->lexer);
7969 /* Otherwise, no valid operator name was present. */
7972 cp_parser_error (parser, "expected operator");
7973 id = error_mark_node;
7979 /* Parse a template-declaration.
7981 template-declaration:
7982 export [opt] template < template-parameter-list > declaration
7984 If MEMBER_P is TRUE, this template-declaration occurs within a
7987 The grammar rule given by the standard isn't correct. What
7990 template-declaration:
7991 export [opt] template-parameter-list-seq
7992 decl-specifier-seq [opt] init-declarator [opt] ;
7993 export [opt] template-parameter-list-seq
7996 template-parameter-list-seq:
7997 template-parameter-list-seq [opt]
7998 template < template-parameter-list > */
8001 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8003 /* Check for `export'. */
8004 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8006 /* Consume the `export' token. */
8007 cp_lexer_consume_token (parser->lexer);
8008 /* Warn that we do not support `export'. */
8009 warning ("keyword %<export%> not implemented, and will be ignored");
8012 cp_parser_template_declaration_after_export (parser, member_p);
8015 /* Parse a template-parameter-list.
8017 template-parameter-list:
8019 template-parameter-list , template-parameter
8021 Returns a TREE_LIST. Each node represents a template parameter.
8022 The nodes are connected via their TREE_CHAINs. */
8025 cp_parser_template_parameter_list (cp_parser* parser)
8027 tree parameter_list = NULL_TREE;
8035 /* Parse the template-parameter. */
8036 parameter = cp_parser_template_parameter (parser, &is_non_type);
8037 /* Add it to the list. */
8038 parameter_list = process_template_parm (parameter_list,
8041 /* Peek at the next token. */
8042 token = cp_lexer_peek_token (parser->lexer);
8043 /* If it's not a `,', we're done. */
8044 if (token->type != CPP_COMMA)
8046 /* Otherwise, consume the `,' token. */
8047 cp_lexer_consume_token (parser->lexer);
8050 return parameter_list;
8053 /* Parse a template-parameter.
8057 parameter-declaration
8059 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8060 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8061 true iff this parameter is a non-type parameter. */
8064 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8067 cp_parameter_declarator *parameter_declarator;
8069 /* Assume it is a type parameter or a template parameter. */
8070 *is_non_type = false;
8071 /* Peek at the next token. */
8072 token = cp_lexer_peek_token (parser->lexer);
8073 /* If it is `class' or `template', we have a type-parameter. */
8074 if (token->keyword == RID_TEMPLATE)
8075 return cp_parser_type_parameter (parser);
8076 /* If it is `class' or `typename' we do not know yet whether it is a
8077 type parameter or a non-type parameter. Consider:
8079 template <typename T, typename T::X X> ...
8083 template <class C, class D*> ...
8085 Here, the first parameter is a type parameter, and the second is
8086 a non-type parameter. We can tell by looking at the token after
8087 the identifier -- if it is a `,', `=', or `>' then we have a type
8089 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8091 /* Peek at the token after `class' or `typename'. */
8092 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8093 /* If it's an identifier, skip it. */
8094 if (token->type == CPP_NAME)
8095 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8096 /* Now, see if the token looks like the end of a template
8098 if (token->type == CPP_COMMA
8099 || token->type == CPP_EQ
8100 || token->type == CPP_GREATER)
8101 return cp_parser_type_parameter (parser);
8104 /* Otherwise, it is a non-type parameter.
8108 When parsing a default template-argument for a non-type
8109 template-parameter, the first non-nested `>' is taken as the end
8110 of the template parameter-list rather than a greater-than
8112 *is_non_type = true;
8113 parameter_declarator
8114 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8115 /*parenthesized_p=*/NULL);
8116 return (build_tree_list
8117 (parameter_declarator->default_argument,
8118 grokdeclarator (parameter_declarator->declarator,
8119 ¶meter_declarator->decl_specifiers,
8120 PARM, /*initialized=*/0,
8121 /*attrlist=*/NULL)));
8124 /* Parse a type-parameter.
8127 class identifier [opt]
8128 class identifier [opt] = type-id
8129 typename identifier [opt]
8130 typename identifier [opt] = type-id
8131 template < template-parameter-list > class identifier [opt]
8132 template < template-parameter-list > class identifier [opt]
8135 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8136 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8137 the declaration of the parameter. */
8140 cp_parser_type_parameter (cp_parser* parser)
8145 /* Look for a keyword to tell us what kind of parameter this is. */
8146 token = cp_parser_require (parser, CPP_KEYWORD,
8147 "`class', `typename', or `template'");
8149 return error_mark_node;
8151 switch (token->keyword)
8157 tree default_argument;
8159 /* If the next token is an identifier, then it names the
8161 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8162 identifier = cp_parser_identifier (parser);
8164 identifier = NULL_TREE;
8166 /* Create the parameter. */
8167 parameter = finish_template_type_parm (class_type_node, identifier);
8169 /* If the next token is an `=', we have a default argument. */
8170 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8172 /* Consume the `=' token. */
8173 cp_lexer_consume_token (parser->lexer);
8174 /* Parse the default-argument. */
8175 default_argument = cp_parser_type_id (parser);
8178 default_argument = NULL_TREE;
8180 /* Create the combined representation of the parameter and the
8181 default argument. */
8182 parameter = build_tree_list (default_argument, parameter);
8188 tree parameter_list;
8190 tree default_argument;
8192 /* Look for the `<'. */
8193 cp_parser_require (parser, CPP_LESS, "`<'");
8194 /* Parse the template-parameter-list. */
8195 begin_template_parm_list ();
8197 = cp_parser_template_parameter_list (parser);
8198 parameter_list = end_template_parm_list (parameter_list);
8199 /* Look for the `>'. */
8200 cp_parser_require (parser, CPP_GREATER, "`>'");
8201 /* Look for the `class' keyword. */
8202 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8203 /* If the next token is an `=', then there is a
8204 default-argument. If the next token is a `>', we are at
8205 the end of the parameter-list. If the next token is a `,',
8206 then we are at the end of this parameter. */
8207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8208 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8209 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8210 identifier = cp_parser_identifier (parser);
8212 identifier = NULL_TREE;
8213 /* Create the template parameter. */
8214 parameter = finish_template_template_parm (class_type_node,
8217 /* If the next token is an `=', then there is a
8218 default-argument. */
8219 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8223 /* Consume the `='. */
8224 cp_lexer_consume_token (parser->lexer);
8225 /* Parse the id-expression. */
8227 = cp_parser_id_expression (parser,
8228 /*template_keyword_p=*/false,
8229 /*check_dependency_p=*/true,
8230 /*template_p=*/&is_template,
8231 /*declarator_p=*/false);
8232 if (TREE_CODE (default_argument) == TYPE_DECL)
8233 /* If the id-expression was a template-id that refers to
8234 a template-class, we already have the declaration here,
8235 so no further lookup is needed. */
8238 /* Look up the name. */
8240 = cp_parser_lookup_name (parser, default_argument,
8242 /*is_template=*/is_template,
8243 /*is_namespace=*/false,
8244 /*check_dependency=*/true,
8245 /*ambiguous_p=*/NULL);
8246 /* See if the default argument is valid. */
8248 = check_template_template_default_arg (default_argument);
8251 default_argument = NULL_TREE;
8253 /* Create the combined representation of the parameter and the
8254 default argument. */
8255 parameter = build_tree_list (default_argument, parameter);
8260 /* Anything else is an error. */
8261 cp_parser_error (parser,
8262 "expected %<class%>, %<typename%>, or %<template%>");
8263 parameter = error_mark_node;
8269 /* Parse a template-id.
8272 template-name < template-argument-list [opt] >
8274 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8275 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8276 returned. Otherwise, if the template-name names a function, or set
8277 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8278 names a class, returns a TYPE_DECL for the specialization.
8280 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8281 uninstantiated templates. */
8284 cp_parser_template_id (cp_parser *parser,
8285 bool template_keyword_p,
8286 bool check_dependency_p,
8287 bool is_declaration)
8292 cp_token_position start_of_id = 0;
8293 tree access_check = NULL_TREE;
8294 cp_token *next_token, *next_token_2;
8297 /* If the next token corresponds to a template-id, there is no need
8299 next_token = cp_lexer_peek_token (parser->lexer);
8300 if (next_token->type == CPP_TEMPLATE_ID)
8305 /* Get the stored value. */
8306 value = cp_lexer_consume_token (parser->lexer)->value;
8307 /* Perform any access checks that were deferred. */
8308 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8309 perform_or_defer_access_check (TREE_PURPOSE (check),
8310 TREE_VALUE (check));
8311 /* Return the stored value. */
8312 return TREE_VALUE (value);
8315 /* Avoid performing name lookup if there is no possibility of
8316 finding a template-id. */
8317 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8318 || (next_token->type == CPP_NAME
8319 && !cp_parser_nth_token_starts_template_argument_list_p
8322 cp_parser_error (parser, "expected template-id");
8323 return error_mark_node;
8326 /* Remember where the template-id starts. */
8327 if (cp_parser_parsing_tentatively (parser)
8328 && !cp_parser_committed_to_tentative_parse (parser))
8329 start_of_id = cp_lexer_token_position (parser->lexer, false);
8331 push_deferring_access_checks (dk_deferred);
8333 /* Parse the template-name. */
8334 is_identifier = false;
8335 template = cp_parser_template_name (parser, template_keyword_p,
8339 if (template == error_mark_node || is_identifier)
8341 pop_deferring_access_checks ();
8345 /* If we find the sequence `[:' after a template-name, it's probably
8346 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8347 parse correctly the argument list. */
8348 next_token = cp_lexer_peek_token (parser->lexer);
8349 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8350 if (next_token->type == CPP_OPEN_SQUARE
8351 && next_token->flags & DIGRAPH
8352 && next_token_2->type == CPP_COLON
8353 && !(next_token_2->flags & PREV_WHITE))
8355 cp_parser_parse_tentatively (parser);
8356 /* Change `:' into `::'. */
8357 next_token_2->type = CPP_SCOPE;
8358 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8360 cp_lexer_consume_token (parser->lexer);
8361 /* Parse the arguments. */
8362 arguments = cp_parser_enclosed_template_argument_list (parser);
8363 if (!cp_parser_parse_definitely (parser))
8365 /* If we couldn't parse an argument list, then we revert our changes
8366 and return simply an error. Maybe this is not a template-id
8368 next_token_2->type = CPP_COLON;
8369 cp_parser_error (parser, "expected %<<%>");
8370 pop_deferring_access_checks ();
8371 return error_mark_node;
8373 /* Otherwise, emit an error about the invalid digraph, but continue
8374 parsing because we got our argument list. */
8375 pedwarn ("%<<::%> cannot begin a template-argument list");
8376 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8377 "between %<<%> and %<::%>");
8378 if (!flag_permissive)
8383 inform ("(if you use -fpermissive G++ will accept your code)");
8390 /* Look for the `<' that starts the template-argument-list. */
8391 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8393 pop_deferring_access_checks ();
8394 return error_mark_node;
8396 /* Parse the arguments. */
8397 arguments = cp_parser_enclosed_template_argument_list (parser);
8400 /* Build a representation of the specialization. */
8401 if (TREE_CODE (template) == IDENTIFIER_NODE)
8402 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8403 else if (DECL_CLASS_TEMPLATE_P (template)
8404 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8406 = finish_template_type (template, arguments,
8407 cp_lexer_next_token_is (parser->lexer,
8411 /* If it's not a class-template or a template-template, it should be
8412 a function-template. */
8413 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8414 || TREE_CODE (template) == OVERLOAD
8415 || BASELINK_P (template)));
8417 template_id = lookup_template_function (template, arguments);
8420 /* Retrieve any deferred checks. Do not pop this access checks yet
8421 so the memory will not be reclaimed during token replacing below. */
8422 access_check = get_deferred_access_checks ();
8424 /* If parsing tentatively, replace the sequence of tokens that makes
8425 up the template-id with a CPP_TEMPLATE_ID token. That way,
8426 should we re-parse the token stream, we will not have to repeat
8427 the effort required to do the parse, nor will we issue duplicate
8428 error messages about problems during instantiation of the
8432 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8434 /* Reset the contents of the START_OF_ID token. */
8435 token->type = CPP_TEMPLATE_ID;
8436 token->value = build_tree_list (access_check, template_id);
8437 token->keyword = RID_MAX;
8439 /* Purge all subsequent tokens. */
8440 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8443 pop_deferring_access_checks ();
8447 /* Parse a template-name.
8452 The standard should actually say:
8456 operator-function-id
8458 A defect report has been filed about this issue.
8460 A conversion-function-id cannot be a template name because they cannot
8461 be part of a template-id. In fact, looking at this code:
8465 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8466 It is impossible to call a templated conversion-function-id with an
8467 explicit argument list, since the only allowed template parameter is
8468 the type to which it is converting.
8470 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8471 `template' keyword, in a construction like:
8475 In that case `f' is taken to be a template-name, even though there
8476 is no way of knowing for sure.
8478 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8479 name refers to a set of overloaded functions, at least one of which
8480 is a template, or an IDENTIFIER_NODE with the name of the template,
8481 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8482 names are looked up inside uninstantiated templates. */
8485 cp_parser_template_name (cp_parser* parser,
8486 bool template_keyword_p,
8487 bool check_dependency_p,
8488 bool is_declaration,
8489 bool *is_identifier)
8495 /* If the next token is `operator', then we have either an
8496 operator-function-id or a conversion-function-id. */
8497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8499 /* We don't know whether we're looking at an
8500 operator-function-id or a conversion-function-id. */
8501 cp_parser_parse_tentatively (parser);
8502 /* Try an operator-function-id. */
8503 identifier = cp_parser_operator_function_id (parser);
8504 /* If that didn't work, try a conversion-function-id. */
8505 if (!cp_parser_parse_definitely (parser))
8507 cp_parser_error (parser, "expected template-name");
8508 return error_mark_node;
8511 /* Look for the identifier. */
8513 identifier = cp_parser_identifier (parser);
8515 /* If we didn't find an identifier, we don't have a template-id. */
8516 if (identifier == error_mark_node)
8517 return error_mark_node;
8519 /* If the name immediately followed the `template' keyword, then it
8520 is a template-name. However, if the next token is not `<', then
8521 we do not treat it as a template-name, since it is not being used
8522 as part of a template-id. This enables us to handle constructs
8525 template <typename T> struct S { S(); };
8526 template <typename T> S<T>::S();
8528 correctly. We would treat `S' as a template -- if it were `S<T>'
8529 -- but we do not if there is no `<'. */
8531 if (processing_template_decl
8532 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8534 /* In a declaration, in a dependent context, we pretend that the
8535 "template" keyword was present in order to improve error
8536 recovery. For example, given:
8538 template <typename T> void f(T::X<int>);
8540 we want to treat "X<int>" as a template-id. */
8542 && !template_keyword_p
8543 && parser->scope && TYPE_P (parser->scope)
8544 && check_dependency_p
8545 && dependent_type_p (parser->scope)
8546 /* Do not do this for dtors (or ctors), since they never
8547 need the template keyword before their name. */
8548 && !constructor_name_p (identifier, parser->scope))
8550 cp_token_position start = 0;
8552 /* Explain what went wrong. */
8553 error ("non-template %qD used as template", identifier);
8554 inform ("use %<%T::template %D%> to indicate that it is a template",
8555 parser->scope, identifier);
8556 /* If parsing tentatively, find the location of the "<"
8558 if (cp_parser_parsing_tentatively (parser)
8559 && !cp_parser_committed_to_tentative_parse (parser))
8561 cp_parser_simulate_error (parser);
8562 start = cp_lexer_token_position (parser->lexer, true);
8564 /* Parse the template arguments so that we can issue error
8565 messages about them. */
8566 cp_lexer_consume_token (parser->lexer);
8567 cp_parser_enclosed_template_argument_list (parser);
8568 /* Skip tokens until we find a good place from which to
8569 continue parsing. */
8570 cp_parser_skip_to_closing_parenthesis (parser,
8571 /*recovering=*/true,
8573 /*consume_paren=*/false);
8574 /* If parsing tentatively, permanently remove the
8575 template argument list. That will prevent duplicate
8576 error messages from being issued about the missing
8577 "template" keyword. */
8579 cp_lexer_purge_tokens_after (parser->lexer, start);
8581 *is_identifier = true;
8585 /* If the "template" keyword is present, then there is generally
8586 no point in doing name-lookup, so we just return IDENTIFIER.
8587 But, if the qualifying scope is non-dependent then we can
8588 (and must) do name-lookup normally. */
8589 if (template_keyword_p
8591 || (TYPE_P (parser->scope)
8592 && dependent_type_p (parser->scope))))
8596 /* Look up the name. */
8597 decl = cp_parser_lookup_name (parser, identifier,
8599 /*is_template=*/false,
8600 /*is_namespace=*/false,
8602 /*ambiguous_p=*/NULL);
8603 decl = maybe_get_template_decl_from_type_decl (decl);
8605 /* If DECL is a template, then the name was a template-name. */
8606 if (TREE_CODE (decl) == TEMPLATE_DECL)
8610 /* The standard does not explicitly indicate whether a name that
8611 names a set of overloaded declarations, some of which are
8612 templates, is a template-name. However, such a name should
8613 be a template-name; otherwise, there is no way to form a
8614 template-id for the overloaded templates. */
8615 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8616 if (TREE_CODE (fns) == OVERLOAD)
8620 for (fn = fns; fn; fn = OVL_NEXT (fn))
8621 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8626 /* Otherwise, the name does not name a template. */
8627 cp_parser_error (parser, "expected template-name");
8628 return error_mark_node;
8632 /* If DECL is dependent, and refers to a function, then just return
8633 its name; we will look it up again during template instantiation. */
8634 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8636 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8637 if (TYPE_P (scope) && dependent_type_p (scope))
8644 /* Parse a template-argument-list.
8646 template-argument-list:
8648 template-argument-list , template-argument
8650 Returns a TREE_VEC containing the arguments. */
8653 cp_parser_template_argument_list (cp_parser* parser)
8655 tree fixed_args[10];
8656 unsigned n_args = 0;
8657 unsigned alloced = 10;
8658 tree *arg_ary = fixed_args;
8660 bool saved_in_template_argument_list_p;
8662 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8663 parser->in_template_argument_list_p = true;
8669 /* Consume the comma. */
8670 cp_lexer_consume_token (parser->lexer);
8672 /* Parse the template-argument. */
8673 argument = cp_parser_template_argument (parser);
8674 if (n_args == alloced)
8678 if (arg_ary == fixed_args)
8680 arg_ary = xmalloc (sizeof (tree) * alloced);
8681 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8684 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8686 arg_ary[n_args++] = argument;
8688 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8690 vec = make_tree_vec (n_args);
8693 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8695 if (arg_ary != fixed_args)
8697 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8701 /* Parse a template-argument.
8704 assignment-expression
8708 The representation is that of an assignment-expression, type-id, or
8709 id-expression -- except that the qualified id-expression is
8710 evaluated, so that the value returned is either a DECL or an
8713 Although the standard says "assignment-expression", it forbids
8714 throw-expressions or assignments in the template argument.
8715 Therefore, we use "conditional-expression" instead. */
8718 cp_parser_template_argument (cp_parser* parser)
8723 bool maybe_type_id = false;
8726 tree qualifying_class;
8728 /* There's really no way to know what we're looking at, so we just
8729 try each alternative in order.
8733 In a template-argument, an ambiguity between a type-id and an
8734 expression is resolved to a type-id, regardless of the form of
8735 the corresponding template-parameter.
8737 Therefore, we try a type-id first. */
8738 cp_parser_parse_tentatively (parser);
8739 argument = cp_parser_type_id (parser);
8740 /* If there was no error parsing the type-id but the next token is a '>>',
8741 we probably found a typo for '> >'. But there are type-id which are
8742 also valid expressions. For instance:
8744 struct X { int operator >> (int); };
8745 template <int V> struct Foo {};
8748 Here 'X()' is a valid type-id of a function type, but the user just
8749 wanted to write the expression "X() >> 5". Thus, we remember that we
8750 found a valid type-id, but we still try to parse the argument as an
8751 expression to see what happens. */
8752 if (!cp_parser_error_occurred (parser)
8753 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8755 maybe_type_id = true;
8756 cp_parser_abort_tentative_parse (parser);
8760 /* If the next token isn't a `,' or a `>', then this argument wasn't
8761 really finished. This means that the argument is not a valid
8763 if (!cp_parser_next_token_ends_template_argument_p (parser))
8764 cp_parser_error (parser, "expected template-argument");
8765 /* If that worked, we're done. */
8766 if (cp_parser_parse_definitely (parser))
8769 /* We're still not sure what the argument will be. */
8770 cp_parser_parse_tentatively (parser);
8771 /* Try a template. */
8772 argument = cp_parser_id_expression (parser,
8773 /*template_keyword_p=*/false,
8774 /*check_dependency_p=*/true,
8776 /*declarator_p=*/false);
8777 /* If the next token isn't a `,' or a `>', then this argument wasn't
8779 if (!cp_parser_next_token_ends_template_argument_p (parser))
8780 cp_parser_error (parser, "expected template-argument");
8781 if (!cp_parser_error_occurred (parser))
8783 /* Figure out what is being referred to. If the id-expression
8784 was for a class template specialization, then we will have a
8785 TYPE_DECL at this point. There is no need to do name lookup
8786 at this point in that case. */
8787 if (TREE_CODE (argument) != TYPE_DECL)
8788 argument = cp_parser_lookup_name (parser, argument,
8790 /*is_template=*/template_p,
8791 /*is_namespace=*/false,
8792 /*check_dependency=*/true,
8793 /*ambiguous_p=*/NULL);
8794 if (TREE_CODE (argument) != TEMPLATE_DECL
8795 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8796 cp_parser_error (parser, "expected template-name");
8798 if (cp_parser_parse_definitely (parser))
8800 /* It must be a non-type argument. There permitted cases are given
8801 in [temp.arg.nontype]:
8803 -- an integral constant-expression of integral or enumeration
8806 -- the name of a non-type template-parameter; or
8808 -- the name of an object or function with external linkage...
8810 -- the address of an object or function with external linkage...
8812 -- a pointer to member... */
8813 /* Look for a non-type template parameter. */
8814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8816 cp_parser_parse_tentatively (parser);
8817 argument = cp_parser_primary_expression (parser,
8820 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8821 || !cp_parser_next_token_ends_template_argument_p (parser))
8822 cp_parser_simulate_error (parser);
8823 if (cp_parser_parse_definitely (parser))
8826 /* If the next token is "&", the argument must be the address of an
8827 object or function with external linkage. */
8828 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8830 cp_lexer_consume_token (parser->lexer);
8831 /* See if we might have an id-expression. */
8832 token = cp_lexer_peek_token (parser->lexer);
8833 if (token->type == CPP_NAME
8834 || token->keyword == RID_OPERATOR
8835 || token->type == CPP_SCOPE
8836 || token->type == CPP_TEMPLATE_ID
8837 || token->type == CPP_NESTED_NAME_SPECIFIER)
8839 cp_parser_parse_tentatively (parser);
8840 argument = cp_parser_primary_expression (parser,
8843 if (cp_parser_error_occurred (parser)
8844 || !cp_parser_next_token_ends_template_argument_p (parser))
8845 cp_parser_abort_tentative_parse (parser);
8848 if (qualifying_class)
8849 argument = finish_qualified_id_expr (qualifying_class,
8853 if (TREE_CODE (argument) == VAR_DECL)
8855 /* A variable without external linkage might still be a
8856 valid constant-expression, so no error is issued here
8857 if the external-linkage check fails. */
8858 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8859 cp_parser_simulate_error (parser);
8861 else if (is_overloaded_fn (argument))
8862 /* All overloaded functions are allowed; if the external
8863 linkage test does not pass, an error will be issued
8867 && (TREE_CODE (argument) == OFFSET_REF
8868 || TREE_CODE (argument) == SCOPE_REF))
8869 /* A pointer-to-member. */
8872 cp_parser_simulate_error (parser);
8874 if (cp_parser_parse_definitely (parser))
8877 argument = build_x_unary_op (ADDR_EXPR, argument);
8882 /* If the argument started with "&", there are no other valid
8883 alternatives at this point. */
8886 cp_parser_error (parser, "invalid non-type template argument");
8887 return error_mark_node;
8889 /* If the argument wasn't successfully parsed as a type-id followed
8890 by '>>', the argument can only be a constant expression now.
8891 Otherwise, we try parsing the constant-expression tentatively,
8892 because the argument could really be a type-id. */
8894 cp_parser_parse_tentatively (parser);
8895 argument = cp_parser_constant_expression (parser,
8896 /*allow_non_constant_p=*/false,
8897 /*non_constant_p=*/NULL);
8898 argument = fold_non_dependent_expr (argument);
8901 if (!cp_parser_next_token_ends_template_argument_p (parser))
8902 cp_parser_error (parser, "expected template-argument");
8903 if (cp_parser_parse_definitely (parser))
8905 /* We did our best to parse the argument as a non type-id, but that
8906 was the only alternative that matched (albeit with a '>' after
8907 it). We can assume it's just a typo from the user, and a
8908 diagnostic will then be issued. */
8909 return cp_parser_type_id (parser);
8912 /* Parse an explicit-instantiation.
8914 explicit-instantiation:
8915 template declaration
8917 Although the standard says `declaration', what it really means is:
8919 explicit-instantiation:
8920 template decl-specifier-seq [opt] declarator [opt] ;
8922 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8923 supposed to be allowed. A defect report has been filed about this
8928 explicit-instantiation:
8929 storage-class-specifier template
8930 decl-specifier-seq [opt] declarator [opt] ;
8931 function-specifier template
8932 decl-specifier-seq [opt] declarator [opt] ; */
8935 cp_parser_explicit_instantiation (cp_parser* parser)
8937 int declares_class_or_enum;
8938 cp_decl_specifier_seq decl_specifiers;
8939 tree extension_specifier = NULL_TREE;
8941 /* Look for an (optional) storage-class-specifier or
8942 function-specifier. */
8943 if (cp_parser_allow_gnu_extensions_p (parser))
8946 = cp_parser_storage_class_specifier_opt (parser);
8947 if (!extension_specifier)
8949 = cp_parser_function_specifier_opt (parser,
8950 /*decl_specs=*/NULL);
8953 /* Look for the `template' keyword. */
8954 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8955 /* Let the front end know that we are processing an explicit
8957 begin_explicit_instantiation ();
8958 /* [temp.explicit] says that we are supposed to ignore access
8959 control while processing explicit instantiation directives. */
8960 push_deferring_access_checks (dk_no_check);
8961 /* Parse a decl-specifier-seq. */
8962 cp_parser_decl_specifier_seq (parser,
8963 CP_PARSER_FLAGS_OPTIONAL,
8965 &declares_class_or_enum);
8966 /* If there was exactly one decl-specifier, and it declared a class,
8967 and there's no declarator, then we have an explicit type
8969 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8973 type = check_tag_decl (&decl_specifiers);
8974 /* Turn access control back on for names used during
8975 template instantiation. */
8976 pop_deferring_access_checks ();
8978 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8982 cp_declarator *declarator;
8985 /* Parse the declarator. */
8987 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8988 /*ctor_dtor_or_conv_p=*/NULL,
8989 /*parenthesized_p=*/NULL,
8990 /*member_p=*/false);
8991 cp_parser_check_for_definition_in_return_type (declarator,
8992 declares_class_or_enum);
8993 if (declarator != cp_error_declarator)
8995 decl = grokdeclarator (declarator, &decl_specifiers,
8997 /* Turn access control back on for names used during
8998 template instantiation. */
8999 pop_deferring_access_checks ();
9000 /* Do the explicit instantiation. */
9001 do_decl_instantiation (decl, extension_specifier);
9005 pop_deferring_access_checks ();
9006 /* Skip the body of the explicit instantiation. */
9007 cp_parser_skip_to_end_of_statement (parser);
9010 /* We're done with the instantiation. */
9011 end_explicit_instantiation ();
9013 cp_parser_consume_semicolon_at_end_of_statement (parser);
9016 /* Parse an explicit-specialization.
9018 explicit-specialization:
9019 template < > declaration
9021 Although the standard says `declaration', what it really means is:
9023 explicit-specialization:
9024 template <> decl-specifier [opt] init-declarator [opt] ;
9025 template <> function-definition
9026 template <> explicit-specialization
9027 template <> template-declaration */
9030 cp_parser_explicit_specialization (cp_parser* parser)
9032 /* Look for the `template' keyword. */
9033 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9034 /* Look for the `<'. */
9035 cp_parser_require (parser, CPP_LESS, "`<'");
9036 /* Look for the `>'. */
9037 cp_parser_require (parser, CPP_GREATER, "`>'");
9038 /* We have processed another parameter list. */
9039 ++parser->num_template_parameter_lists;
9040 /* Let the front end know that we are beginning a specialization. */
9041 begin_specialization ();
9043 /* If the next keyword is `template', we need to figure out whether
9044 or not we're looking a template-declaration. */
9045 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9047 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9048 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9049 cp_parser_template_declaration_after_export (parser,
9050 /*member_p=*/false);
9052 cp_parser_explicit_specialization (parser);
9055 /* Parse the dependent declaration. */
9056 cp_parser_single_declaration (parser,
9060 /* We're done with the specialization. */
9061 end_specialization ();
9062 /* We're done with this parameter list. */
9063 --parser->num_template_parameter_lists;
9066 /* Parse a type-specifier.
9069 simple-type-specifier
9072 elaborated-type-specifier
9080 Returns a representation of the type-specifier. For a
9081 class-specifier, enum-specifier, or elaborated-type-specifier, a
9082 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9084 The parser flags FLAGS is used to control type-specifier parsing.
9086 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9087 in a decl-specifier-seq.
9089 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9090 class-specifier, enum-specifier, or elaborated-type-specifier, then
9091 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9092 if a type is declared; 2 if it is defined. Otherwise, it is set to
9095 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9096 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9100 cp_parser_type_specifier (cp_parser* parser,
9101 cp_parser_flags flags,
9102 cp_decl_specifier_seq *decl_specs,
9103 bool is_declaration,
9104 int* declares_class_or_enum,
9105 bool* is_cv_qualifier)
9107 tree type_spec = NULL_TREE;
9110 cp_decl_spec ds = ds_last;
9112 /* Assume this type-specifier does not declare a new type. */
9113 if (declares_class_or_enum)
9114 *declares_class_or_enum = 0;
9115 /* And that it does not specify a cv-qualifier. */
9116 if (is_cv_qualifier)
9117 *is_cv_qualifier = false;
9118 /* Peek at the next token. */
9119 token = cp_lexer_peek_token (parser->lexer);
9121 /* If we're looking at a keyword, we can use that to guide the
9122 production we choose. */
9123 keyword = token->keyword;
9127 /* 'enum' [identifier] '{' introduces an enum-specifier;
9128 'enum' <anything else> introduces an elaborated-type-specifier. */
9129 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9130 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9131 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9134 type_spec = cp_parser_enum_specifier (parser);
9135 if (declares_class_or_enum)
9136 *declares_class_or_enum = 2;
9138 cp_parser_set_decl_spec_type (decl_specs,
9140 /*user_defined_p=*/true);
9144 goto elaborated_type_specifier;
9146 /* Any of these indicate either a class-specifier, or an
9147 elaborated-type-specifier. */
9151 /* Parse tentatively so that we can back up if we don't find a
9153 cp_parser_parse_tentatively (parser);
9154 /* Look for the class-specifier. */
9155 type_spec = cp_parser_class_specifier (parser);
9156 /* If that worked, we're done. */
9157 if (cp_parser_parse_definitely (parser))
9159 if (declares_class_or_enum)
9160 *declares_class_or_enum = 2;
9162 cp_parser_set_decl_spec_type (decl_specs,
9164 /*user_defined_p=*/true);
9169 elaborated_type_specifier:
9170 /* We're declaring (not defining) a class or enum. */
9171 if (declares_class_or_enum)
9172 *declares_class_or_enum = 1;
9176 /* Look for an elaborated-type-specifier. */
9178 = (cp_parser_elaborated_type_specifier
9180 decl_specs && decl_specs->specs[(int) ds_friend],
9183 cp_parser_set_decl_spec_type (decl_specs,
9185 /*user_defined_p=*/true);
9190 if (is_cv_qualifier)
9191 *is_cv_qualifier = true;
9196 if (is_cv_qualifier)
9197 *is_cv_qualifier = true;
9202 if (is_cv_qualifier)
9203 *is_cv_qualifier = true;
9207 /* The `__complex__' keyword is a GNU extension. */
9215 /* Handle simple keywords. */
9220 ++decl_specs->specs[(int)ds];
9221 decl_specs->any_specifiers_p = true;
9223 return cp_lexer_consume_token (parser->lexer)->value;
9226 /* If we do not already have a type-specifier, assume we are looking
9227 at a simple-type-specifier. */
9228 type_spec = cp_parser_simple_type_specifier (parser,
9232 /* If we didn't find a type-specifier, and a type-specifier was not
9233 optional in this context, issue an error message. */
9234 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9236 cp_parser_error (parser, "expected type specifier");
9237 return error_mark_node;
9243 /* Parse a simple-type-specifier.
9245 simple-type-specifier:
9246 :: [opt] nested-name-specifier [opt] type-name
9247 :: [opt] nested-name-specifier template template-id
9262 simple-type-specifier:
9263 __typeof__ unary-expression
9264 __typeof__ ( type-id )
9266 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9267 appropriately updated. */
9270 cp_parser_simple_type_specifier (cp_parser* parser,
9271 cp_decl_specifier_seq *decl_specs,
9272 cp_parser_flags flags)
9274 tree type = NULL_TREE;
9277 /* Peek at the next token. */
9278 token = cp_lexer_peek_token (parser->lexer);
9280 /* If we're looking at a keyword, things are easy. */
9281 switch (token->keyword)
9285 decl_specs->explicit_char_p = true;
9286 type = char_type_node;
9289 type = wchar_type_node;
9292 type = boolean_type_node;
9296 ++decl_specs->specs[(int) ds_short];
9297 type = short_integer_type_node;
9301 decl_specs->explicit_int_p = true;
9302 type = integer_type_node;
9306 ++decl_specs->specs[(int) ds_long];
9307 type = long_integer_type_node;
9311 ++decl_specs->specs[(int) ds_signed];
9312 type = integer_type_node;
9316 ++decl_specs->specs[(int) ds_unsigned];
9317 type = unsigned_type_node;
9320 type = float_type_node;
9323 type = double_type_node;
9326 type = void_type_node;
9330 /* Consume the `typeof' token. */
9331 cp_lexer_consume_token (parser->lexer);
9332 /* Parse the operand to `typeof'. */
9333 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9334 /* If it is not already a TYPE, take its type. */
9336 type = finish_typeof (type);
9339 cp_parser_set_decl_spec_type (decl_specs, type,
9340 /*user_defined_p=*/true);
9348 /* If the type-specifier was for a built-in type, we're done. */
9353 /* Record the type. */
9355 && (token->keyword != RID_SIGNED
9356 && token->keyword != RID_UNSIGNED
9357 && token->keyword != RID_SHORT
9358 && token->keyword != RID_LONG))
9359 cp_parser_set_decl_spec_type (decl_specs,
9361 /*user_defined=*/false);
9363 decl_specs->any_specifiers_p = true;
9365 /* Consume the token. */
9366 id = cp_lexer_consume_token (parser->lexer)->value;
9368 /* There is no valid C++ program where a non-template type is
9369 followed by a "<". That usually indicates that the user thought
9370 that the type was a template. */
9371 cp_parser_check_for_invalid_template_id (parser, type);
9373 return TYPE_NAME (type);
9376 /* The type-specifier must be a user-defined type. */
9377 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9382 /* Don't gobble tokens or issue error messages if this is an
9383 optional type-specifier. */
9384 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9385 cp_parser_parse_tentatively (parser);
9387 /* Look for the optional `::' operator. */
9389 = (cp_parser_global_scope_opt (parser,
9390 /*current_scope_valid_p=*/false)
9392 /* Look for the nested-name specifier. */
9394 = (cp_parser_nested_name_specifier_opt (parser,
9395 /*typename_keyword_p=*/false,
9396 /*check_dependency_p=*/true,
9398 /*is_declaration=*/false)
9400 /* If we have seen a nested-name-specifier, and the next token
9401 is `template', then we are using the template-id production. */
9403 && cp_parser_optional_template_keyword (parser))
9405 /* Look for the template-id. */
9406 type = cp_parser_template_id (parser,
9407 /*template_keyword_p=*/true,
9408 /*check_dependency_p=*/true,
9409 /*is_declaration=*/false);
9410 /* If the template-id did not name a type, we are out of
9412 if (TREE_CODE (type) != TYPE_DECL)
9414 cp_parser_error (parser, "expected template-id for type");
9418 /* Otherwise, look for a type-name. */
9420 type = cp_parser_type_name (parser);
9421 /* Keep track of all name-lookups performed in class scopes. */
9425 && TREE_CODE (type) == TYPE_DECL
9426 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9427 maybe_note_name_used_in_class (DECL_NAME (type), type);
9428 /* If it didn't work out, we don't have a TYPE. */
9429 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9430 && !cp_parser_parse_definitely (parser))
9432 if (type && decl_specs)
9433 cp_parser_set_decl_spec_type (decl_specs, type,
9434 /*user_defined=*/true);
9437 /* If we didn't get a type-name, issue an error message. */
9438 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9440 cp_parser_error (parser, "expected type-name");
9441 return error_mark_node;
9444 /* There is no valid C++ program where a non-template type is
9445 followed by a "<". That usually indicates that the user thought
9446 that the type was a template. */
9447 if (type && type != error_mark_node)
9448 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9453 /* Parse a type-name.
9466 Returns a TYPE_DECL for the the type. */
9469 cp_parser_type_name (cp_parser* parser)
9474 /* We can't know yet whether it is a class-name or not. */
9475 cp_parser_parse_tentatively (parser);
9476 /* Try a class-name. */
9477 type_decl = cp_parser_class_name (parser,
9478 /*typename_keyword_p=*/false,
9479 /*template_keyword_p=*/false,
9481 /*check_dependency_p=*/true,
9482 /*class_head_p=*/false,
9483 /*is_declaration=*/false);
9484 /* If it's not a class-name, keep looking. */
9485 if (!cp_parser_parse_definitely (parser))
9487 /* It must be a typedef-name or an enum-name. */
9488 identifier = cp_parser_identifier (parser);
9489 if (identifier == error_mark_node)
9490 return error_mark_node;
9492 /* Look up the type-name. */
9493 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9494 /* Issue an error if we did not find a type-name. */
9495 if (TREE_CODE (type_decl) != TYPE_DECL)
9497 if (!cp_parser_simulate_error (parser))
9498 cp_parser_name_lookup_error (parser, identifier, type_decl,
9500 type_decl = error_mark_node;
9502 /* Remember that the name was used in the definition of the
9503 current class so that we can check later to see if the
9504 meaning would have been different after the class was
9505 entirely defined. */
9506 else if (type_decl != error_mark_node
9508 maybe_note_name_used_in_class (identifier, type_decl);
9515 /* Parse an elaborated-type-specifier. Note that the grammar given
9516 here incorporates the resolution to DR68.
9518 elaborated-type-specifier:
9519 class-key :: [opt] nested-name-specifier [opt] identifier
9520 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9521 enum :: [opt] nested-name-specifier [opt] identifier
9522 typename :: [opt] nested-name-specifier identifier
9523 typename :: [opt] nested-name-specifier template [opt]
9528 elaborated-type-specifier:
9529 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9530 class-key attributes :: [opt] nested-name-specifier [opt]
9531 template [opt] template-id
9532 enum attributes :: [opt] nested-name-specifier [opt] identifier
9534 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9535 declared `friend'. If IS_DECLARATION is TRUE, then this
9536 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9537 something is being declared.
9539 Returns the TYPE specified. */
9542 cp_parser_elaborated_type_specifier (cp_parser* parser,
9544 bool is_declaration)
9546 enum tag_types tag_type;
9548 tree type = NULL_TREE;
9549 tree attributes = NULL_TREE;
9551 /* See if we're looking at the `enum' keyword. */
9552 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9554 /* Consume the `enum' token. */
9555 cp_lexer_consume_token (parser->lexer);
9556 /* Remember that it's an enumeration type. */
9557 tag_type = enum_type;
9558 /* Parse the attributes. */
9559 attributes = cp_parser_attributes_opt (parser);
9561 /* Or, it might be `typename'. */
9562 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9565 /* Consume the `typename' token. */
9566 cp_lexer_consume_token (parser->lexer);
9567 /* Remember that it's a `typename' type. */
9568 tag_type = typename_type;
9569 /* The `typename' keyword is only allowed in templates. */
9570 if (!processing_template_decl)
9571 pedwarn ("using %<typename%> outside of template");
9573 /* Otherwise it must be a class-key. */
9576 tag_type = cp_parser_class_key (parser);
9577 if (tag_type == none_type)
9578 return error_mark_node;
9579 /* Parse the attributes. */
9580 attributes = cp_parser_attributes_opt (parser);
9583 /* Look for the `::' operator. */
9584 cp_parser_global_scope_opt (parser,
9585 /*current_scope_valid_p=*/false);
9586 /* Look for the nested-name-specifier. */
9587 if (tag_type == typename_type)
9589 if (cp_parser_nested_name_specifier (parser,
9590 /*typename_keyword_p=*/true,
9591 /*check_dependency_p=*/true,
9595 return error_mark_node;
9598 /* Even though `typename' is not present, the proposed resolution
9599 to Core Issue 180 says that in `class A<T>::B', `B' should be
9600 considered a type-name, even if `A<T>' is dependent. */
9601 cp_parser_nested_name_specifier_opt (parser,
9602 /*typename_keyword_p=*/true,
9603 /*check_dependency_p=*/true,
9606 /* For everything but enumeration types, consider a template-id. */
9607 if (tag_type != enum_type)
9609 bool template_p = false;
9612 /* Allow the `template' keyword. */
9613 template_p = cp_parser_optional_template_keyword (parser);
9614 /* If we didn't see `template', we don't know if there's a
9615 template-id or not. */
9617 cp_parser_parse_tentatively (parser);
9618 /* Parse the template-id. */
9619 decl = cp_parser_template_id (parser, template_p,
9620 /*check_dependency_p=*/true,
9622 /* If we didn't find a template-id, look for an ordinary
9624 if (!template_p && !cp_parser_parse_definitely (parser))
9626 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9627 in effect, then we must assume that, upon instantiation, the
9628 template will correspond to a class. */
9629 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9630 && tag_type == typename_type)
9631 type = make_typename_type (parser->scope, decl,
9634 type = TREE_TYPE (decl);
9637 /* For an enumeration type, consider only a plain identifier. */
9640 identifier = cp_parser_identifier (parser);
9642 if (identifier == error_mark_node)
9644 parser->scope = NULL_TREE;
9645 return error_mark_node;
9648 /* For a `typename', we needn't call xref_tag. */
9649 if (tag_type == typename_type)
9650 return cp_parser_make_typename_type (parser, parser->scope,
9652 /* Look up a qualified name in the usual way. */
9657 /* In an elaborated-type-specifier, names are assumed to name
9658 types, so we set IS_TYPE to TRUE when calling
9659 cp_parser_lookup_name. */
9660 decl = cp_parser_lookup_name (parser, identifier,
9662 /*is_template=*/false,
9663 /*is_namespace=*/false,
9664 /*check_dependency=*/true,
9665 /*ambiguous_p=*/NULL);
9667 /* If we are parsing friend declaration, DECL may be a
9668 TEMPLATE_DECL tree node here. However, we need to check
9669 whether this TEMPLATE_DECL results in valid code. Consider
9670 the following example:
9673 template <class T> class C {};
9676 template <class T> friend class N::C; // #1, valid code
9678 template <class T> class Y {
9679 friend class N::C; // #2, invalid code
9682 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9683 name lookup of `N::C'. We see that friend declaration must
9684 be template for the code to be valid. Note that
9685 processing_template_decl does not work here since it is
9686 always 1 for the above two cases. */
9688 decl = (cp_parser_maybe_treat_template_as_class
9689 (decl, /*tag_name_p=*/is_friend
9690 && parser->num_template_parameter_lists));
9692 if (TREE_CODE (decl) != TYPE_DECL)
9694 error ("expected type-name");
9695 return error_mark_node;
9698 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9699 check_elaborated_type_specifier
9701 (parser->num_template_parameter_lists
9702 || DECL_SELF_REFERENCE_P (decl)));
9704 type = TREE_TYPE (decl);
9708 /* An elaborated-type-specifier sometimes introduces a new type and
9709 sometimes names an existing type. Normally, the rule is that it
9710 introduces a new type only if there is not an existing type of
9711 the same name already in scope. For example, given:
9714 void f() { struct S s; }
9716 the `struct S' in the body of `f' is the same `struct S' as in
9717 the global scope; the existing definition is used. However, if
9718 there were no global declaration, this would introduce a new
9719 local class named `S'.
9721 An exception to this rule applies to the following code:
9723 namespace N { struct S; }
9725 Here, the elaborated-type-specifier names a new type
9726 unconditionally; even if there is already an `S' in the
9727 containing scope this declaration names a new type.
9728 This exception only applies if the elaborated-type-specifier
9729 forms the complete declaration:
9733 A declaration consisting solely of `class-key identifier ;' is
9734 either a redeclaration of the name in the current scope or a
9735 forward declaration of the identifier as a class name. It
9736 introduces the name into the current scope.
9738 We are in this situation precisely when the next token is a `;'.
9740 An exception to the exception is that a `friend' declaration does
9741 *not* name a new type; i.e., given:
9743 struct S { friend struct T; };
9745 `T' is not a new type in the scope of `S'.
9747 Also, `new struct S' or `sizeof (struct S)' never results in the
9748 definition of a new type; a new type can only be declared in a
9749 declaration context. */
9751 /* Warn about attributes. They are ignored. */
9753 warning ("type attributes are honored only at type definition");
9755 type = xref_tag (tag_type, identifier,
9758 || cp_lexer_next_token_is_not (parser->lexer,
9760 parser->num_template_parameter_lists);
9763 if (tag_type != enum_type)
9764 cp_parser_check_class_key (tag_type, type);
9766 /* A "<" cannot follow an elaborated type specifier. If that
9767 happens, the user was probably trying to form a template-id. */
9768 cp_parser_check_for_invalid_template_id (parser, type);
9773 /* Parse an enum-specifier.
9776 enum identifier [opt] { enumerator-list [opt] }
9778 Returns an ENUM_TYPE representing the enumeration. */
9781 cp_parser_enum_specifier (cp_parser* parser)
9786 /* Caller guarantees that the current token is 'enum', an identifier
9787 possibly follows, and the token after that is an opening brace.
9788 If we don't have an identifier, fabricate an anonymous name for
9789 the enumeration being defined. */
9790 cp_lexer_consume_token (parser->lexer);
9792 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9793 identifier = cp_parser_identifier (parser);
9795 identifier = make_anon_name ();
9797 /* Issue an error message if type-definitions are forbidden here. */
9798 cp_parser_check_type_definition (parser);
9800 /* Create the new type. We do this before consuming the opening brace
9801 so the enum will be recorded as being on the line of its tag (or the
9802 'enum' keyword, if there is no tag). */
9803 type = start_enum (identifier);
9805 /* Consume the opening brace. */
9806 cp_lexer_consume_token (parser->lexer);
9808 /* If the next token is not '}', then there are some enumerators. */
9809 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9810 cp_parser_enumerator_list (parser, type);
9812 /* Consume the final '}'. */
9813 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9815 /* Finish up the enumeration. */
9821 /* Parse an enumerator-list. The enumerators all have the indicated
9825 enumerator-definition
9826 enumerator-list , enumerator-definition */
9829 cp_parser_enumerator_list (cp_parser* parser, tree type)
9833 /* Parse an enumerator-definition. */
9834 cp_parser_enumerator_definition (parser, type);
9836 /* If the next token is not a ',', we've reached the end of
9838 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9840 /* Otherwise, consume the `,' and keep going. */
9841 cp_lexer_consume_token (parser->lexer);
9842 /* If the next token is a `}', there is a trailing comma. */
9843 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9845 if (pedantic && !in_system_header)
9846 pedwarn ("comma at end of enumerator list");
9852 /* Parse an enumerator-definition. The enumerator has the indicated
9855 enumerator-definition:
9857 enumerator = constant-expression
9863 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9868 /* Look for the identifier. */
9869 identifier = cp_parser_identifier (parser);
9870 if (identifier == error_mark_node)
9873 /* If the next token is an '=', then there is an explicit value. */
9874 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9876 /* Consume the `=' token. */
9877 cp_lexer_consume_token (parser->lexer);
9878 /* Parse the value. */
9879 value = cp_parser_constant_expression (parser,
9880 /*allow_non_constant_p=*/false,
9886 /* Create the enumerator. */
9887 build_enumerator (identifier, value, type);
9890 /* Parse a namespace-name.
9893 original-namespace-name
9896 Returns the NAMESPACE_DECL for the namespace. */
9899 cp_parser_namespace_name (cp_parser* parser)
9902 tree namespace_decl;
9904 /* Get the name of the namespace. */
9905 identifier = cp_parser_identifier (parser);
9906 if (identifier == error_mark_node)
9907 return error_mark_node;
9909 /* Look up the identifier in the currently active scope. Look only
9910 for namespaces, due to:
9914 When looking up a namespace-name in a using-directive or alias
9915 definition, only namespace names are considered.
9921 During the lookup of a name preceding the :: scope resolution
9922 operator, object, function, and enumerator names are ignored.
9924 (Note that cp_parser_class_or_namespace_name only calls this
9925 function if the token after the name is the scope resolution
9927 namespace_decl = cp_parser_lookup_name (parser, identifier,
9929 /*is_template=*/false,
9930 /*is_namespace=*/true,
9931 /*check_dependency=*/true,
9932 /*ambiguous_p=*/NULL);
9933 /* If it's not a namespace, issue an error. */
9934 if (namespace_decl == error_mark_node
9935 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9937 cp_parser_error (parser, "expected namespace-name");
9938 namespace_decl = error_mark_node;
9941 return namespace_decl;
9944 /* Parse a namespace-definition.
9946 namespace-definition:
9947 named-namespace-definition
9948 unnamed-namespace-definition
9950 named-namespace-definition:
9951 original-namespace-definition
9952 extension-namespace-definition
9954 original-namespace-definition:
9955 namespace identifier { namespace-body }
9957 extension-namespace-definition:
9958 namespace original-namespace-name { namespace-body }
9960 unnamed-namespace-definition:
9961 namespace { namespace-body } */
9964 cp_parser_namespace_definition (cp_parser* parser)
9968 /* Look for the `namespace' keyword. */
9969 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9971 /* Get the name of the namespace. We do not attempt to distinguish
9972 between an original-namespace-definition and an
9973 extension-namespace-definition at this point. The semantic
9974 analysis routines are responsible for that. */
9975 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9976 identifier = cp_parser_identifier (parser);
9978 identifier = NULL_TREE;
9980 /* Look for the `{' to start the namespace. */
9981 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9982 /* Start the namespace. */
9983 push_namespace (identifier);
9984 /* Parse the body of the namespace. */
9985 cp_parser_namespace_body (parser);
9986 /* Finish the namespace. */
9988 /* Look for the final `}'. */
9989 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9992 /* Parse a namespace-body.
9995 declaration-seq [opt] */
9998 cp_parser_namespace_body (cp_parser* parser)
10000 cp_parser_declaration_seq_opt (parser);
10003 /* Parse a namespace-alias-definition.
10005 namespace-alias-definition:
10006 namespace identifier = qualified-namespace-specifier ; */
10009 cp_parser_namespace_alias_definition (cp_parser* parser)
10012 tree namespace_specifier;
10014 /* Look for the `namespace' keyword. */
10015 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10016 /* Look for the identifier. */
10017 identifier = cp_parser_identifier (parser);
10018 if (identifier == error_mark_node)
10020 /* Look for the `=' token. */
10021 cp_parser_require (parser, CPP_EQ, "`='");
10022 /* Look for the qualified-namespace-specifier. */
10023 namespace_specifier
10024 = cp_parser_qualified_namespace_specifier (parser);
10025 /* Look for the `;' token. */
10026 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10028 /* Register the alias in the symbol table. */
10029 do_namespace_alias (identifier, namespace_specifier);
10032 /* Parse a qualified-namespace-specifier.
10034 qualified-namespace-specifier:
10035 :: [opt] nested-name-specifier [opt] namespace-name
10037 Returns a NAMESPACE_DECL corresponding to the specified
10041 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10043 /* Look for the optional `::'. */
10044 cp_parser_global_scope_opt (parser,
10045 /*current_scope_valid_p=*/false);
10047 /* Look for the optional nested-name-specifier. */
10048 cp_parser_nested_name_specifier_opt (parser,
10049 /*typename_keyword_p=*/false,
10050 /*check_dependency_p=*/true,
10052 /*is_declaration=*/true);
10054 return cp_parser_namespace_name (parser);
10057 /* Parse a using-declaration.
10060 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10061 using :: unqualified-id ; */
10064 cp_parser_using_declaration (cp_parser* parser)
10067 bool typename_p = false;
10068 bool global_scope_p;
10074 /* Look for the `using' keyword. */
10075 cp_parser_require_keyword (parser, RID_USING, "`using'");
10077 /* Peek at the next token. */
10078 token = cp_lexer_peek_token (parser->lexer);
10079 /* See if it's `typename'. */
10080 if (token->keyword == RID_TYPENAME)
10082 /* Remember that we've seen it. */
10084 /* Consume the `typename' token. */
10085 cp_lexer_consume_token (parser->lexer);
10088 /* Look for the optional global scope qualification. */
10090 = (cp_parser_global_scope_opt (parser,
10091 /*current_scope_valid_p=*/false)
10094 /* If we saw `typename', or didn't see `::', then there must be a
10095 nested-name-specifier present. */
10096 if (typename_p || !global_scope_p)
10097 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10098 /*check_dependency_p=*/true,
10100 /*is_declaration=*/true);
10101 /* Otherwise, we could be in either of the two productions. In that
10102 case, treat the nested-name-specifier as optional. */
10104 qscope = cp_parser_nested_name_specifier_opt (parser,
10105 /*typename_keyword_p=*/false,
10106 /*check_dependency_p=*/true,
10108 /*is_declaration=*/true);
10110 qscope = global_namespace;
10112 /* Parse the unqualified-id. */
10113 identifier = cp_parser_unqualified_id (parser,
10114 /*template_keyword_p=*/false,
10115 /*check_dependency_p=*/true,
10116 /*declarator_p=*/true);
10118 /* The function we call to handle a using-declaration is different
10119 depending on what scope we are in. */
10120 if (identifier == error_mark_node)
10122 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10123 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10124 /* [namespace.udecl]
10126 A using declaration shall not name a template-id. */
10127 error ("a template-id may not appear in a using-declaration");
10130 scope = current_scope ();
10131 if (scope && TYPE_P (scope))
10133 /* Create the USING_DECL. */
10134 decl = do_class_using_decl (build_nt (SCOPE_REF,
10137 /* Add it to the list of members in this class. */
10138 finish_member_declaration (decl);
10142 decl = cp_parser_lookup_name_simple (parser, identifier);
10143 if (decl == error_mark_node)
10144 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10146 do_local_using_decl (decl, qscope, identifier);
10148 do_toplevel_using_decl (decl, qscope, identifier);
10152 /* Look for the final `;'. */
10153 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10156 /* Parse a using-directive.
10159 using namespace :: [opt] nested-name-specifier [opt]
10160 namespace-name ; */
10163 cp_parser_using_directive (cp_parser* parser)
10165 tree namespace_decl;
10168 /* Look for the `using' keyword. */
10169 cp_parser_require_keyword (parser, RID_USING, "`using'");
10170 /* And the `namespace' keyword. */
10171 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10172 /* Look for the optional `::' operator. */
10173 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10174 /* And the optional nested-name-specifier. */
10175 cp_parser_nested_name_specifier_opt (parser,
10176 /*typename_keyword_p=*/false,
10177 /*check_dependency_p=*/true,
10179 /*is_declaration=*/true);
10180 /* Get the namespace being used. */
10181 namespace_decl = cp_parser_namespace_name (parser);
10182 /* And any specified attributes. */
10183 attribs = cp_parser_attributes_opt (parser);
10184 /* Update the symbol table. */
10185 parse_using_directive (namespace_decl, attribs);
10186 /* Look for the final `;'. */
10187 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10190 /* Parse an asm-definition.
10193 asm ( string-literal ) ;
10198 asm volatile [opt] ( string-literal ) ;
10199 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10200 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10201 : asm-operand-list [opt] ) ;
10202 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10203 : asm-operand-list [opt]
10204 : asm-operand-list [opt] ) ; */
10207 cp_parser_asm_definition (cp_parser* parser)
10210 tree outputs = NULL_TREE;
10211 tree inputs = NULL_TREE;
10212 tree clobbers = NULL_TREE;
10214 bool volatile_p = false;
10215 bool extended_p = false;
10217 /* Look for the `asm' keyword. */
10218 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10219 /* See if the next token is `volatile'. */
10220 if (cp_parser_allow_gnu_extensions_p (parser)
10221 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10223 /* Remember that we saw the `volatile' keyword. */
10225 /* Consume the token. */
10226 cp_lexer_consume_token (parser->lexer);
10228 /* Look for the opening `('. */
10229 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10231 /* Look for the string. */
10232 string = cp_parser_string_literal (parser, false, false);
10233 if (string == error_mark_node)
10235 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10236 /*consume_paren=*/true);
10240 /* If we're allowing GNU extensions, check for the extended assembly
10241 syntax. Unfortunately, the `:' tokens need not be separated by
10242 a space in C, and so, for compatibility, we tolerate that here
10243 too. Doing that means that we have to treat the `::' operator as
10245 if (cp_parser_allow_gnu_extensions_p (parser)
10246 && at_function_scope_p ()
10247 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10248 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10250 bool inputs_p = false;
10251 bool clobbers_p = false;
10253 /* The extended syntax was used. */
10256 /* Look for outputs. */
10257 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10259 /* Consume the `:'. */
10260 cp_lexer_consume_token (parser->lexer);
10261 /* Parse the output-operands. */
10262 if (cp_lexer_next_token_is_not (parser->lexer,
10264 && cp_lexer_next_token_is_not (parser->lexer,
10266 && cp_lexer_next_token_is_not (parser->lexer,
10268 outputs = cp_parser_asm_operand_list (parser);
10270 /* If the next token is `::', there are no outputs, and the
10271 next token is the beginning of the inputs. */
10272 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10273 /* The inputs are coming next. */
10276 /* Look for inputs. */
10278 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10280 /* Consume the `:' or `::'. */
10281 cp_lexer_consume_token (parser->lexer);
10282 /* Parse the output-operands. */
10283 if (cp_lexer_next_token_is_not (parser->lexer,
10285 && cp_lexer_next_token_is_not (parser->lexer,
10287 inputs = cp_parser_asm_operand_list (parser);
10289 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10290 /* The clobbers are coming next. */
10293 /* Look for clobbers. */
10295 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10297 /* Consume the `:' or `::'. */
10298 cp_lexer_consume_token (parser->lexer);
10299 /* Parse the clobbers. */
10300 if (cp_lexer_next_token_is_not (parser->lexer,
10302 clobbers = cp_parser_asm_clobber_list (parser);
10305 /* Look for the closing `)'. */
10306 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10307 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10308 /*consume_paren=*/true);
10309 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10311 /* Create the ASM_EXPR. */
10312 if (at_function_scope_p ())
10314 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10316 /* If the extended syntax was not used, mark the ASM_EXPR. */
10319 tree temp = asm_stmt;
10320 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10321 temp = TREE_OPERAND (temp, 0);
10323 ASM_INPUT_P (temp) = 1;
10327 assemble_asm (string);
10330 /* Declarators [gram.dcl.decl] */
10332 /* Parse an init-declarator.
10335 declarator initializer [opt]
10340 declarator asm-specification [opt] attributes [opt] initializer [opt]
10342 function-definition:
10343 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10345 decl-specifier-seq [opt] declarator function-try-block
10349 function-definition:
10350 __extension__ function-definition
10352 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10353 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10354 then this declarator appears in a class scope. The new DECL created
10355 by this declarator is returned.
10357 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10358 for a function-definition here as well. If the declarator is a
10359 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10360 be TRUE upon return. By that point, the function-definition will
10361 have been completely parsed.
10363 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10367 cp_parser_init_declarator (cp_parser* parser,
10368 cp_decl_specifier_seq *decl_specifiers,
10369 bool function_definition_allowed_p,
10371 int declares_class_or_enum,
10372 bool* function_definition_p)
10375 cp_declarator *declarator;
10376 tree prefix_attributes;
10378 tree asm_specification;
10380 tree decl = NULL_TREE;
10382 bool is_initialized;
10383 bool is_parenthesized_init;
10384 bool is_non_constant_init;
10385 int ctor_dtor_or_conv_p;
10387 bool pop_p = false;
10389 /* Gather the attributes that were provided with the
10390 decl-specifiers. */
10391 prefix_attributes = decl_specifiers->attributes;
10393 /* Assume that this is not the declarator for a function
10395 if (function_definition_p)
10396 *function_definition_p = false;
10398 /* Defer access checks while parsing the declarator; we cannot know
10399 what names are accessible until we know what is being
10401 resume_deferring_access_checks ();
10403 /* Parse the declarator. */
10405 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10406 &ctor_dtor_or_conv_p,
10407 /*parenthesized_p=*/NULL,
10408 /*member_p=*/false);
10409 /* Gather up the deferred checks. */
10410 stop_deferring_access_checks ();
10412 /* If the DECLARATOR was erroneous, there's no need to go
10414 if (declarator == cp_error_declarator)
10415 return error_mark_node;
10417 cp_parser_check_for_definition_in_return_type (declarator,
10418 declares_class_or_enum);
10420 /* Figure out what scope the entity declared by the DECLARATOR is
10421 located in. `grokdeclarator' sometimes changes the scope, so
10422 we compute it now. */
10423 scope = get_scope_of_declarator (declarator);
10425 /* If we're allowing GNU extensions, look for an asm-specification
10427 if (cp_parser_allow_gnu_extensions_p (parser))
10429 /* Look for an asm-specification. */
10430 asm_specification = cp_parser_asm_specification_opt (parser);
10431 /* And attributes. */
10432 attributes = cp_parser_attributes_opt (parser);
10436 asm_specification = NULL_TREE;
10437 attributes = NULL_TREE;
10440 /* Peek at the next token. */
10441 token = cp_lexer_peek_token (parser->lexer);
10442 /* Check to see if the token indicates the start of a
10443 function-definition. */
10444 if (cp_parser_token_starts_function_definition_p (token))
10446 if (!function_definition_allowed_p)
10448 /* If a function-definition should not appear here, issue an
10450 cp_parser_error (parser,
10451 "a function-definition is not allowed here");
10452 return error_mark_node;
10456 /* Neither attributes nor an asm-specification are allowed
10457 on a function-definition. */
10458 if (asm_specification)
10459 error ("an asm-specification is not allowed on a function-definition");
10461 error ("attributes are not allowed on a function-definition");
10462 /* This is a function-definition. */
10463 *function_definition_p = true;
10465 /* Parse the function definition. */
10467 decl = cp_parser_save_member_function_body (parser,
10470 prefix_attributes);
10473 = (cp_parser_function_definition_from_specifiers_and_declarator
10474 (parser, decl_specifiers, prefix_attributes, declarator));
10482 Only in function declarations for constructors, destructors, and
10483 type conversions can the decl-specifier-seq be omitted.
10485 We explicitly postpone this check past the point where we handle
10486 function-definitions because we tolerate function-definitions
10487 that are missing their return types in some modes. */
10488 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10490 cp_parser_error (parser,
10491 "expected constructor, destructor, or type conversion");
10492 return error_mark_node;
10495 /* An `=' or an `(' indicates an initializer. */
10496 is_initialized = (token->type == CPP_EQ
10497 || token->type == CPP_OPEN_PAREN);
10498 /* If the init-declarator isn't initialized and isn't followed by a
10499 `,' or `;', it's not a valid init-declarator. */
10500 if (!is_initialized
10501 && token->type != CPP_COMMA
10502 && token->type != CPP_SEMICOLON)
10504 cp_parser_error (parser, "expected initializer");
10505 return error_mark_node;
10508 /* Because start_decl has side-effects, we should only call it if we
10509 know we're going ahead. By this point, we know that we cannot
10510 possibly be looking at any other construct. */
10511 cp_parser_commit_to_tentative_parse (parser);
10513 /* If the decl specifiers were bad, issue an error now that we're
10514 sure this was intended to be a declarator. Then continue
10515 declaring the variable(s), as int, to try to cut down on further
10517 if (decl_specifiers->any_specifiers_p
10518 && decl_specifiers->type == error_mark_node)
10520 cp_parser_error (parser, "invalid type in declaration");
10521 decl_specifiers->type = integer_type_node;
10524 /* Check to see whether or not this declaration is a friend. */
10525 friend_p = cp_parser_friend_p (decl_specifiers);
10527 /* Check that the number of template-parameter-lists is OK. */
10528 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10529 return error_mark_node;
10531 /* Enter the newly declared entry in the symbol table. If we're
10532 processing a declaration in a class-specifier, we wait until
10533 after processing the initializer. */
10536 if (parser->in_unbraced_linkage_specification_p)
10538 decl_specifiers->storage_class = sc_extern;
10539 have_extern_spec = false;
10541 decl = start_decl (declarator, decl_specifiers,
10542 is_initialized, attributes, prefix_attributes,
10546 /* Enter the SCOPE. That way unqualified names appearing in the
10547 initializer will be looked up in SCOPE. */
10548 pop_p = push_scope (scope);
10550 /* Perform deferred access control checks, now that we know in which
10551 SCOPE the declared entity resides. */
10552 if (!member_p && decl)
10554 tree saved_current_function_decl = NULL_TREE;
10556 /* If the entity being declared is a function, pretend that we
10557 are in its scope. If it is a `friend', it may have access to
10558 things that would not otherwise be accessible. */
10559 if (TREE_CODE (decl) == FUNCTION_DECL)
10561 saved_current_function_decl = current_function_decl;
10562 current_function_decl = decl;
10565 /* Perform the access control checks for the declarator and the
10566 the decl-specifiers. */
10567 perform_deferred_access_checks ();
10569 /* Restore the saved value. */
10570 if (TREE_CODE (decl) == FUNCTION_DECL)
10571 current_function_decl = saved_current_function_decl;
10574 /* Parse the initializer. */
10575 if (is_initialized)
10576 initializer = cp_parser_initializer (parser,
10577 &is_parenthesized_init,
10578 &is_non_constant_init);
10581 initializer = NULL_TREE;
10582 is_parenthesized_init = false;
10583 is_non_constant_init = true;
10586 /* The old parser allows attributes to appear after a parenthesized
10587 initializer. Mark Mitchell proposed removing this functionality
10588 on the GCC mailing lists on 2002-08-13. This parser accepts the
10589 attributes -- but ignores them. */
10590 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10591 if (cp_parser_attributes_opt (parser))
10592 warning ("attributes after parenthesized initializer ignored");
10594 /* For an in-class declaration, use `grokfield' to create the
10600 decl = grokfield (declarator, decl_specifiers,
10601 initializer, /*asmspec=*/NULL_TREE,
10602 /*attributes=*/NULL_TREE);
10603 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10604 cp_parser_save_default_args (parser, decl);
10607 /* Finish processing the declaration. But, skip friend
10609 if (!friend_p && decl && decl != error_mark_node)
10611 cp_finish_decl (decl,
10614 /* If the initializer is in parentheses, then this is
10615 a direct-initialization, which means that an
10616 `explicit' constructor is OK. Otherwise, an
10617 `explicit' constructor cannot be used. */
10618 ((is_parenthesized_init || !is_initialized)
10619 ? 0 : LOOKUP_ONLYCONVERTING));
10621 pop_scope (DECL_CONTEXT (decl));
10624 /* Remember whether or not variables were initialized by
10625 constant-expressions. */
10626 if (decl && TREE_CODE (decl) == VAR_DECL
10627 && is_initialized && !is_non_constant_init)
10628 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10633 /* Parse a declarator.
10637 ptr-operator declarator
10639 abstract-declarator:
10640 ptr-operator abstract-declarator [opt]
10641 direct-abstract-declarator
10646 attributes [opt] direct-declarator
10647 attributes [opt] ptr-operator declarator
10649 abstract-declarator:
10650 attributes [opt] ptr-operator abstract-declarator [opt]
10651 attributes [opt] direct-abstract-declarator
10653 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10654 detect constructor, destructor or conversion operators. It is set
10655 to -1 if the declarator is a name, and +1 if it is a
10656 function. Otherwise it is set to zero. Usually you just want to
10657 test for >0, but internally the negative value is used.
10659 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10660 a decl-specifier-seq unless it declares a constructor, destructor,
10661 or conversion. It might seem that we could check this condition in
10662 semantic analysis, rather than parsing, but that makes it difficult
10663 to handle something like `f()'. We want to notice that there are
10664 no decl-specifiers, and therefore realize that this is an
10665 expression, not a declaration.)
10667 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10668 the declarator is a direct-declarator of the form "(...)".
10670 MEMBER_P is true iff this declarator is a member-declarator. */
10672 static cp_declarator *
10673 cp_parser_declarator (cp_parser* parser,
10674 cp_parser_declarator_kind dcl_kind,
10675 int* ctor_dtor_or_conv_p,
10676 bool* parenthesized_p,
10680 cp_declarator *declarator;
10681 enum tree_code code;
10682 cp_cv_quals cv_quals;
10684 tree attributes = NULL_TREE;
10686 /* Assume this is not a constructor, destructor, or type-conversion
10688 if (ctor_dtor_or_conv_p)
10689 *ctor_dtor_or_conv_p = 0;
10691 if (cp_parser_allow_gnu_extensions_p (parser))
10692 attributes = cp_parser_attributes_opt (parser);
10694 /* Peek at the next token. */
10695 token = cp_lexer_peek_token (parser->lexer);
10697 /* Check for the ptr-operator production. */
10698 cp_parser_parse_tentatively (parser);
10699 /* Parse the ptr-operator. */
10700 code = cp_parser_ptr_operator (parser,
10703 /* If that worked, then we have a ptr-operator. */
10704 if (cp_parser_parse_definitely (parser))
10706 /* If a ptr-operator was found, then this declarator was not
10708 if (parenthesized_p)
10709 *parenthesized_p = true;
10710 /* The dependent declarator is optional if we are parsing an
10711 abstract-declarator. */
10712 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10713 cp_parser_parse_tentatively (parser);
10715 /* Parse the dependent declarator. */
10716 declarator = cp_parser_declarator (parser, dcl_kind,
10717 /*ctor_dtor_or_conv_p=*/NULL,
10718 /*parenthesized_p=*/NULL,
10719 /*member_p=*/false);
10721 /* If we are parsing an abstract-declarator, we must handle the
10722 case where the dependent declarator is absent. */
10723 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10724 && !cp_parser_parse_definitely (parser))
10727 /* Build the representation of the ptr-operator. */
10729 declarator = make_ptrmem_declarator (cv_quals,
10732 else if (code == INDIRECT_REF)
10733 declarator = make_pointer_declarator (cv_quals, declarator);
10735 declarator = make_reference_declarator (cv_quals, declarator);
10737 /* Everything else is a direct-declarator. */
10740 if (parenthesized_p)
10741 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10743 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10744 ctor_dtor_or_conv_p,
10748 if (attributes && declarator != cp_error_declarator)
10749 declarator->attributes = attributes;
10754 /* Parse a direct-declarator or direct-abstract-declarator.
10758 direct-declarator ( parameter-declaration-clause )
10759 cv-qualifier-seq [opt]
10760 exception-specification [opt]
10761 direct-declarator [ constant-expression [opt] ]
10764 direct-abstract-declarator:
10765 direct-abstract-declarator [opt]
10766 ( parameter-declaration-clause )
10767 cv-qualifier-seq [opt]
10768 exception-specification [opt]
10769 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10770 ( abstract-declarator )
10772 Returns a representation of the declarator. DCL_KIND is
10773 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10774 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10775 we are parsing a direct-declarator. It is
10776 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10777 of ambiguity we prefer an abstract declarator, as per
10778 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10779 cp_parser_declarator. */
10781 static cp_declarator *
10782 cp_parser_direct_declarator (cp_parser* parser,
10783 cp_parser_declarator_kind dcl_kind,
10784 int* ctor_dtor_or_conv_p,
10788 cp_declarator *declarator = NULL;
10789 tree scope = NULL_TREE;
10790 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10791 bool saved_in_declarator_p = parser->in_declarator_p;
10793 bool pop_p = false;
10797 /* Peek at the next token. */
10798 token = cp_lexer_peek_token (parser->lexer);
10799 if (token->type == CPP_OPEN_PAREN)
10801 /* This is either a parameter-declaration-clause, or a
10802 parenthesized declarator. When we know we are parsing a
10803 named declarator, it must be a parenthesized declarator
10804 if FIRST is true. For instance, `(int)' is a
10805 parameter-declaration-clause, with an omitted
10806 direct-abstract-declarator. But `((*))', is a
10807 parenthesized abstract declarator. Finally, when T is a
10808 template parameter `(T)' is a
10809 parameter-declaration-clause, and not a parenthesized
10812 We first try and parse a parameter-declaration-clause,
10813 and then try a nested declarator (if FIRST is true).
10815 It is not an error for it not to be a
10816 parameter-declaration-clause, even when FIRST is
10822 The first is the declaration of a function while the
10823 second is a the definition of a variable, including its
10826 Having seen only the parenthesis, we cannot know which of
10827 these two alternatives should be selected. Even more
10828 complex are examples like:
10833 The former is a function-declaration; the latter is a
10834 variable initialization.
10836 Thus again, we try a parameter-declaration-clause, and if
10837 that fails, we back out and return. */
10839 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10841 cp_parameter_declarator *params;
10842 unsigned saved_num_template_parameter_lists;
10844 /* In a member-declarator, the only valid interpretation
10845 of a parenthesis is the start of a
10846 parameter-declaration-clause. (It is invalid to
10847 initialize a static data member with a parenthesized
10848 initializer; only the "=" form of initialization is
10851 cp_parser_parse_tentatively (parser);
10853 /* Consume the `('. */
10854 cp_lexer_consume_token (parser->lexer);
10857 /* If this is going to be an abstract declarator, we're
10858 in a declarator and we can't have default args. */
10859 parser->default_arg_ok_p = false;
10860 parser->in_declarator_p = true;
10863 /* Inside the function parameter list, surrounding
10864 template-parameter-lists do not apply. */
10865 saved_num_template_parameter_lists
10866 = parser->num_template_parameter_lists;
10867 parser->num_template_parameter_lists = 0;
10869 /* Parse the parameter-declaration-clause. */
10870 params = cp_parser_parameter_declaration_clause (parser);
10872 parser->num_template_parameter_lists
10873 = saved_num_template_parameter_lists;
10875 /* If all went well, parse the cv-qualifier-seq and the
10876 exception-specification. */
10877 if (member_p || cp_parser_parse_definitely (parser))
10879 cp_cv_quals cv_quals;
10880 tree exception_specification;
10882 if (ctor_dtor_or_conv_p)
10883 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10885 /* Consume the `)'. */
10886 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10888 /* Parse the cv-qualifier-seq. */
10889 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10890 /* And the exception-specification. */
10891 exception_specification
10892 = cp_parser_exception_specification_opt (parser);
10894 /* Create the function-declarator. */
10895 declarator = make_call_declarator (declarator,
10898 exception_specification);
10899 /* Any subsequent parameter lists are to do with
10900 return type, so are not those of the declared
10902 parser->default_arg_ok_p = false;
10904 /* Repeat the main loop. */
10909 /* If this is the first, we can try a parenthesized
10913 bool saved_in_type_id_in_expr_p;
10915 parser->default_arg_ok_p = saved_default_arg_ok_p;
10916 parser->in_declarator_p = saved_in_declarator_p;
10918 /* Consume the `('. */
10919 cp_lexer_consume_token (parser->lexer);
10920 /* Parse the nested declarator. */
10921 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10922 parser->in_type_id_in_expr_p = true;
10924 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10925 /*parenthesized_p=*/NULL,
10927 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10929 /* Expect a `)'. */
10930 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10931 declarator = cp_error_declarator;
10932 if (declarator == cp_error_declarator)
10935 goto handle_declarator;
10937 /* Otherwise, we must be done. */
10941 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10942 && token->type == CPP_OPEN_SQUARE)
10944 /* Parse an array-declarator. */
10947 if (ctor_dtor_or_conv_p)
10948 *ctor_dtor_or_conv_p = 0;
10951 parser->default_arg_ok_p = false;
10952 parser->in_declarator_p = true;
10953 /* Consume the `['. */
10954 cp_lexer_consume_token (parser->lexer);
10955 /* Peek at the next token. */
10956 token = cp_lexer_peek_token (parser->lexer);
10957 /* If the next token is `]', then there is no
10958 constant-expression. */
10959 if (token->type != CPP_CLOSE_SQUARE)
10961 bool non_constant_p;
10964 = cp_parser_constant_expression (parser,
10965 /*allow_non_constant=*/true,
10967 if (!non_constant_p)
10968 bounds = fold_non_dependent_expr (bounds);
10971 bounds = NULL_TREE;
10972 /* Look for the closing `]'. */
10973 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10975 declarator = cp_error_declarator;
10979 declarator = make_array_declarator (declarator, bounds);
10981 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10985 /* Parse a declarator-id */
10986 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10987 cp_parser_parse_tentatively (parser);
10988 id = cp_parser_declarator_id (parser);
10989 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10991 if (!cp_parser_parse_definitely (parser))
10992 id = error_mark_node;
10993 else if (TREE_CODE (id) != IDENTIFIER_NODE)
10995 cp_parser_error (parser, "expected unqualified-id");
10996 id = error_mark_node;
11000 if (id == error_mark_node)
11002 declarator = cp_error_declarator;
11006 if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11008 tree scope = TREE_OPERAND (id, 0);
11010 /* In the declaration of a member of a template class
11011 outside of the class itself, the SCOPE will sometimes
11012 be a TYPENAME_TYPE. For example, given:
11014 template <typename T>
11015 int S<T>::R::i = 3;
11017 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11018 this context, we must resolve S<T>::R to an ordinary
11019 type, rather than a typename type.
11021 The reason we normally avoid resolving TYPENAME_TYPEs
11022 is that a specialization of `S' might render
11023 `S<T>::R' not a type. However, if `S' is
11024 specialized, then this `i' will not be used, so there
11025 is no harm in resolving the types here. */
11026 if (TREE_CODE (scope) == TYPENAME_TYPE)
11030 /* Resolve the TYPENAME_TYPE. */
11031 type = resolve_typename_type (scope,
11032 /*only_current_p=*/false);
11033 /* If that failed, the declarator is invalid. */
11034 if (type == error_mark_node)
11035 error ("%<%T::%D%> is not a type",
11036 TYPE_CONTEXT (scope),
11037 TYPE_IDENTIFIER (scope));
11038 /* Build a new DECLARATOR. */
11039 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11043 declarator = make_id_declarator (id);
11047 tree unqualified_name;
11049 if (TREE_CODE (id) == SCOPE_REF
11050 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11052 class_type = TREE_OPERAND (id, 0);
11053 unqualified_name = TREE_OPERAND (id, 1);
11057 class_type = current_class_type;
11058 unqualified_name = id;
11063 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11064 declarator->u.id.sfk = sfk_destructor;
11065 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11066 declarator->u.id.sfk = sfk_conversion;
11067 else if (constructor_name_p (unqualified_name,
11069 || (TREE_CODE (unqualified_name) == TYPE_DECL
11070 && same_type_p (TREE_TYPE (unqualified_name),
11072 declarator->u.id.sfk = sfk_constructor;
11074 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11075 *ctor_dtor_or_conv_p = -1;
11076 if (TREE_CODE (id) == SCOPE_REF
11077 && TREE_CODE (unqualified_name) == TYPE_DECL
11078 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11080 error ("invalid use of constructor as a template");
11081 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11082 "the constructor in a qualified name",
11084 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11085 class_type, class_type);
11090 handle_declarator:;
11091 scope = get_scope_of_declarator (declarator);
11093 /* Any names that appear after the declarator-id for a
11094 member are looked up in the containing scope. */
11095 pop_p = push_scope (scope);
11096 parser->in_declarator_p = true;
11097 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11098 || (declarator && declarator->kind == cdk_id))
11099 /* Default args are only allowed on function
11101 parser->default_arg_ok_p = saved_default_arg_ok_p;
11103 parser->default_arg_ok_p = false;
11112 /* For an abstract declarator, we might wind up with nothing at this
11113 point. That's an error; the declarator is not optional. */
11115 cp_parser_error (parser, "expected declarator");
11117 /* If we entered a scope, we must exit it now. */
11121 parser->default_arg_ok_p = saved_default_arg_ok_p;
11122 parser->in_declarator_p = saved_in_declarator_p;
11127 /* Parse a ptr-operator.
11130 * cv-qualifier-seq [opt]
11132 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11137 & cv-qualifier-seq [opt]
11139 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11140 Returns ADDR_EXPR if a reference was used. In the case of a
11141 pointer-to-member, *TYPE is filled in with the TYPE containing the
11142 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11143 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11144 ERROR_MARK if an error occurred. */
11146 static enum tree_code
11147 cp_parser_ptr_operator (cp_parser* parser,
11149 cp_cv_quals *cv_quals)
11151 enum tree_code code = ERROR_MARK;
11154 /* Assume that it's not a pointer-to-member. */
11156 /* And that there are no cv-qualifiers. */
11157 *cv_quals = TYPE_UNQUALIFIED;
11159 /* Peek at the next token. */
11160 token = cp_lexer_peek_token (parser->lexer);
11161 /* If it's a `*' or `&' we have a pointer or reference. */
11162 if (token->type == CPP_MULT || token->type == CPP_AND)
11164 /* Remember which ptr-operator we were processing. */
11165 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11167 /* Consume the `*' or `&'. */
11168 cp_lexer_consume_token (parser->lexer);
11170 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11171 `&', if we are allowing GNU extensions. (The only qualifier
11172 that can legally appear after `&' is `restrict', but that is
11173 enforced during semantic analysis. */
11174 if (code == INDIRECT_REF
11175 || cp_parser_allow_gnu_extensions_p (parser))
11176 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11180 /* Try the pointer-to-member case. */
11181 cp_parser_parse_tentatively (parser);
11182 /* Look for the optional `::' operator. */
11183 cp_parser_global_scope_opt (parser,
11184 /*current_scope_valid_p=*/false);
11185 /* Look for the nested-name specifier. */
11186 cp_parser_nested_name_specifier (parser,
11187 /*typename_keyword_p=*/false,
11188 /*check_dependency_p=*/true,
11190 /*is_declaration=*/false);
11191 /* If we found it, and the next token is a `*', then we are
11192 indeed looking at a pointer-to-member operator. */
11193 if (!cp_parser_error_occurred (parser)
11194 && cp_parser_require (parser, CPP_MULT, "`*'"))
11196 /* The type of which the member is a member is given by the
11198 *type = parser->scope;
11199 /* The next name will not be qualified. */
11200 parser->scope = NULL_TREE;
11201 parser->qualifying_scope = NULL_TREE;
11202 parser->object_scope = NULL_TREE;
11203 /* Indicate that the `*' operator was used. */
11204 code = INDIRECT_REF;
11205 /* Look for the optional cv-qualifier-seq. */
11206 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11208 /* If that didn't work we don't have a ptr-operator. */
11209 if (!cp_parser_parse_definitely (parser))
11210 cp_parser_error (parser, "expected ptr-operator");
11216 /* Parse an (optional) cv-qualifier-seq.
11219 cv-qualifier cv-qualifier-seq [opt]
11230 Returns a bitmask representing the cv-qualifiers. */
11233 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11235 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11240 cp_cv_quals cv_qualifier;
11242 /* Peek at the next token. */
11243 token = cp_lexer_peek_token (parser->lexer);
11244 /* See if it's a cv-qualifier. */
11245 switch (token->keyword)
11248 cv_qualifier = TYPE_QUAL_CONST;
11252 cv_qualifier = TYPE_QUAL_VOLATILE;
11256 cv_qualifier = TYPE_QUAL_RESTRICT;
11260 cv_qualifier = TYPE_UNQUALIFIED;
11267 if (cv_quals & cv_qualifier)
11269 error ("duplicate cv-qualifier");
11270 cp_lexer_purge_token (parser->lexer);
11274 cp_lexer_consume_token (parser->lexer);
11275 cv_quals |= cv_qualifier;
11282 /* Parse a declarator-id.
11286 :: [opt] nested-name-specifier [opt] type-name
11288 In the `id-expression' case, the value returned is as for
11289 cp_parser_id_expression if the id-expression was an unqualified-id.
11290 If the id-expression was a qualified-id, then a SCOPE_REF is
11291 returned. The first operand is the scope (either a NAMESPACE_DECL
11292 or TREE_TYPE), but the second is still just a representation of an
11296 cp_parser_declarator_id (cp_parser* parser)
11298 tree id_expression;
11300 /* The expression must be an id-expression. Assume that qualified
11301 names are the names of types so that:
11304 int S<T>::R::i = 3;
11306 will work; we must treat `S<T>::R' as the name of a type.
11307 Similarly, assume that qualified names are templates, where
11311 int S<T>::R<T>::i = 3;
11314 id_expression = cp_parser_id_expression (parser,
11315 /*template_keyword_p=*/false,
11316 /*check_dependency_p=*/false,
11317 /*template_p=*/NULL,
11318 /*declarator_p=*/true);
11319 /* If the name was qualified, create a SCOPE_REF to represent
11323 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11324 parser->scope = NULL_TREE;
11327 return id_expression;
11330 /* Parse a type-id.
11333 type-specifier-seq abstract-declarator [opt]
11335 Returns the TYPE specified. */
11338 cp_parser_type_id (cp_parser* parser)
11340 cp_decl_specifier_seq type_specifier_seq;
11341 cp_declarator *abstract_declarator;
11343 /* Parse the type-specifier-seq. */
11344 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11345 if (type_specifier_seq.type == error_mark_node)
11346 return error_mark_node;
11348 /* There might or might not be an abstract declarator. */
11349 cp_parser_parse_tentatively (parser);
11350 /* Look for the declarator. */
11351 abstract_declarator
11352 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11353 /*parenthesized_p=*/NULL,
11354 /*member_p=*/false);
11355 /* Check to see if there really was a declarator. */
11356 if (!cp_parser_parse_definitely (parser))
11357 abstract_declarator = NULL;
11359 return groktypename (&type_specifier_seq, abstract_declarator);
11362 /* Parse a type-specifier-seq.
11364 type-specifier-seq:
11365 type-specifier type-specifier-seq [opt]
11369 type-specifier-seq:
11370 attributes type-specifier-seq [opt]
11372 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11375 cp_parser_type_specifier_seq (cp_parser* parser,
11376 cp_decl_specifier_seq *type_specifier_seq)
11378 bool seen_type_specifier = false;
11380 /* Clear the TYPE_SPECIFIER_SEQ. */
11381 clear_decl_specs (type_specifier_seq);
11383 /* Parse the type-specifiers and attributes. */
11386 tree type_specifier;
11388 /* Check for attributes first. */
11389 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11391 type_specifier_seq->attributes =
11392 chainon (type_specifier_seq->attributes,
11393 cp_parser_attributes_opt (parser));
11397 /* Look for the type-specifier. */
11398 type_specifier = cp_parser_type_specifier (parser,
11399 CP_PARSER_FLAGS_OPTIONAL,
11400 type_specifier_seq,
11401 /*is_declaration=*/false,
11404 /* If the first type-specifier could not be found, this is not a
11405 type-specifier-seq at all. */
11406 if (!seen_type_specifier && !type_specifier)
11408 cp_parser_error (parser, "expected type-specifier");
11409 type_specifier_seq->type = error_mark_node;
11412 /* If subsequent type-specifiers could not be found, the
11413 type-specifier-seq is complete. */
11414 else if (seen_type_specifier && !type_specifier)
11417 seen_type_specifier = true;
11423 /* Parse a parameter-declaration-clause.
11425 parameter-declaration-clause:
11426 parameter-declaration-list [opt] ... [opt]
11427 parameter-declaration-list , ...
11429 Returns a representation for the parameter declarations. A return
11430 value of NULL indicates a parameter-declaration-clause consisting
11431 only of an ellipsis. */
11433 static cp_parameter_declarator *
11434 cp_parser_parameter_declaration_clause (cp_parser* parser)
11436 cp_parameter_declarator *parameters;
11441 /* Peek at the next token. */
11442 token = cp_lexer_peek_token (parser->lexer);
11443 /* Check for trivial parameter-declaration-clauses. */
11444 if (token->type == CPP_ELLIPSIS)
11446 /* Consume the `...' token. */
11447 cp_lexer_consume_token (parser->lexer);
11450 else if (token->type == CPP_CLOSE_PAREN)
11451 /* There are no parameters. */
11453 #ifndef NO_IMPLICIT_EXTERN_C
11454 if (in_system_header && current_class_type == NULL
11455 && current_lang_name == lang_name_c)
11459 return no_parameters;
11461 /* Check for `(void)', too, which is a special case. */
11462 else if (token->keyword == RID_VOID
11463 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11464 == CPP_CLOSE_PAREN))
11466 /* Consume the `void' token. */
11467 cp_lexer_consume_token (parser->lexer);
11468 /* There are no parameters. */
11469 return no_parameters;
11472 /* Parse the parameter-declaration-list. */
11473 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11474 /* If a parse error occurred while parsing the
11475 parameter-declaration-list, then the entire
11476 parameter-declaration-clause is erroneous. */
11480 /* Peek at the next token. */
11481 token = cp_lexer_peek_token (parser->lexer);
11482 /* If it's a `,', the clause should terminate with an ellipsis. */
11483 if (token->type == CPP_COMMA)
11485 /* Consume the `,'. */
11486 cp_lexer_consume_token (parser->lexer);
11487 /* Expect an ellipsis. */
11489 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11491 /* It might also be `...' if the optional trailing `,' was
11493 else if (token->type == CPP_ELLIPSIS)
11495 /* Consume the `...' token. */
11496 cp_lexer_consume_token (parser->lexer);
11497 /* And remember that we saw it. */
11501 ellipsis_p = false;
11503 /* Finish the parameter list. */
11504 if (parameters && ellipsis_p)
11505 parameters->ellipsis_p = true;
11510 /* Parse a parameter-declaration-list.
11512 parameter-declaration-list:
11513 parameter-declaration
11514 parameter-declaration-list , parameter-declaration
11516 Returns a representation of the parameter-declaration-list, as for
11517 cp_parser_parameter_declaration_clause. However, the
11518 `void_list_node' is never appended to the list. Upon return,
11519 *IS_ERROR will be true iff an error occurred. */
11521 static cp_parameter_declarator *
11522 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11524 cp_parameter_declarator *parameters = NULL;
11525 cp_parameter_declarator **tail = ¶meters;
11527 /* Assume all will go well. */
11530 /* Look for more parameters. */
11533 cp_parameter_declarator *parameter;
11534 bool parenthesized_p;
11535 /* Parse the parameter. */
11537 = cp_parser_parameter_declaration (parser,
11538 /*template_parm_p=*/false,
11541 /* If a parse error occurred parsing the parameter declaration,
11542 then the entire parameter-declaration-list is erroneous. */
11549 /* Add the new parameter to the list. */
11551 tail = ¶meter->next;
11553 /* Peek at the next token. */
11554 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11555 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11556 /* The parameter-declaration-list is complete. */
11558 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11562 /* Peek at the next token. */
11563 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11564 /* If it's an ellipsis, then the list is complete. */
11565 if (token->type == CPP_ELLIPSIS)
11567 /* Otherwise, there must be more parameters. Consume the
11569 cp_lexer_consume_token (parser->lexer);
11570 /* When parsing something like:
11572 int i(float f, double d)
11574 we can tell after seeing the declaration for "f" that we
11575 are not looking at an initialization of a variable "i",
11576 but rather at the declaration of a function "i".
11578 Due to the fact that the parsing of template arguments
11579 (as specified to a template-id) requires backtracking we
11580 cannot use this technique when inside a template argument
11582 if (!parser->in_template_argument_list_p
11583 && !parser->in_type_id_in_expr_p
11584 && cp_parser_parsing_tentatively (parser)
11585 && !cp_parser_committed_to_tentative_parse (parser)
11586 /* However, a parameter-declaration of the form
11587 "foat(f)" (which is a valid declaration of a
11588 parameter "f") can also be interpreted as an
11589 expression (the conversion of "f" to "float"). */
11590 && !parenthesized_p)
11591 cp_parser_commit_to_tentative_parse (parser);
11595 cp_parser_error (parser, "expected %<,%> or %<...%>");
11596 if (!cp_parser_parsing_tentatively (parser)
11597 || cp_parser_committed_to_tentative_parse (parser))
11598 cp_parser_skip_to_closing_parenthesis (parser,
11599 /*recovering=*/true,
11600 /*or_comma=*/false,
11601 /*consume_paren=*/false);
11609 /* Parse a parameter declaration.
11611 parameter-declaration:
11612 decl-specifier-seq declarator
11613 decl-specifier-seq declarator = assignment-expression
11614 decl-specifier-seq abstract-declarator [opt]
11615 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11617 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11618 declares a template parameter. (In that case, a non-nested `>'
11619 token encountered during the parsing of the assignment-expression
11620 is not interpreted as a greater-than operator.)
11622 Returns a representation of the parameter, or NULL if an error
11623 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11624 true iff the declarator is of the form "(p)". */
11626 static cp_parameter_declarator *
11627 cp_parser_parameter_declaration (cp_parser *parser,
11628 bool template_parm_p,
11629 bool *parenthesized_p)
11631 int declares_class_or_enum;
11632 bool greater_than_is_operator_p;
11633 cp_decl_specifier_seq decl_specifiers;
11634 cp_declarator *declarator;
11635 tree default_argument;
11637 const char *saved_message;
11639 /* In a template parameter, `>' is not an operator.
11643 When parsing a default template-argument for a non-type
11644 template-parameter, the first non-nested `>' is taken as the end
11645 of the template parameter-list rather than a greater-than
11647 greater_than_is_operator_p = !template_parm_p;
11649 /* Type definitions may not appear in parameter types. */
11650 saved_message = parser->type_definition_forbidden_message;
11651 parser->type_definition_forbidden_message
11652 = "types may not be defined in parameter types";
11654 /* Parse the declaration-specifiers. */
11655 cp_parser_decl_specifier_seq (parser,
11656 CP_PARSER_FLAGS_NONE,
11658 &declares_class_or_enum);
11659 /* If an error occurred, there's no reason to attempt to parse the
11660 rest of the declaration. */
11661 if (cp_parser_error_occurred (parser))
11663 parser->type_definition_forbidden_message = saved_message;
11667 /* Peek at the next token. */
11668 token = cp_lexer_peek_token (parser->lexer);
11669 /* If the next token is a `)', `,', `=', `>', or `...', then there
11670 is no declarator. */
11671 if (token->type == CPP_CLOSE_PAREN
11672 || token->type == CPP_COMMA
11673 || token->type == CPP_EQ
11674 || token->type == CPP_ELLIPSIS
11675 || token->type == CPP_GREATER)
11678 if (parenthesized_p)
11679 *parenthesized_p = false;
11681 /* Otherwise, there should be a declarator. */
11684 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11685 parser->default_arg_ok_p = false;
11687 /* After seeing a decl-specifier-seq, if the next token is not a
11688 "(", there is no possibility that the code is a valid
11689 expression. Therefore, if parsing tentatively, we commit at
11691 if (!parser->in_template_argument_list_p
11692 /* In an expression context, having seen:
11696 we cannot be sure whether we are looking at a
11697 function-type (taking a "char" as a parameter) or a cast
11698 of some object of type "char" to "int". */
11699 && !parser->in_type_id_in_expr_p
11700 && cp_parser_parsing_tentatively (parser)
11701 && !cp_parser_committed_to_tentative_parse (parser)
11702 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11703 cp_parser_commit_to_tentative_parse (parser);
11704 /* Parse the declarator. */
11705 declarator = cp_parser_declarator (parser,
11706 CP_PARSER_DECLARATOR_EITHER,
11707 /*ctor_dtor_or_conv_p=*/NULL,
11709 /*member_p=*/false);
11710 parser->default_arg_ok_p = saved_default_arg_ok_p;
11711 /* After the declarator, allow more attributes. */
11712 decl_specifiers.attributes
11713 = chainon (decl_specifiers.attributes,
11714 cp_parser_attributes_opt (parser));
11717 /* The restriction on defining new types applies only to the type
11718 of the parameter, not to the default argument. */
11719 parser->type_definition_forbidden_message = saved_message;
11721 /* If the next token is `=', then process a default argument. */
11722 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11724 bool saved_greater_than_is_operator_p;
11725 /* Consume the `='. */
11726 cp_lexer_consume_token (parser->lexer);
11728 /* If we are defining a class, then the tokens that make up the
11729 default argument must be saved and processed later. */
11730 if (!template_parm_p && at_class_scope_p ()
11731 && TYPE_BEING_DEFINED (current_class_type))
11733 unsigned depth = 0;
11734 cp_token *first_token;
11737 /* Add tokens until we have processed the entire default
11738 argument. We add the range [first_token, token). */
11739 first_token = cp_lexer_peek_token (parser->lexer);
11744 /* Peek at the next token. */
11745 token = cp_lexer_peek_token (parser->lexer);
11746 /* What we do depends on what token we have. */
11747 switch (token->type)
11749 /* In valid code, a default argument must be
11750 immediately followed by a `,' `)', or `...'. */
11752 case CPP_CLOSE_PAREN:
11754 /* If we run into a non-nested `;', `}', or `]',
11755 then the code is invalid -- but the default
11756 argument is certainly over. */
11757 case CPP_SEMICOLON:
11758 case CPP_CLOSE_BRACE:
11759 case CPP_CLOSE_SQUARE:
11762 /* Update DEPTH, if necessary. */
11763 else if (token->type == CPP_CLOSE_PAREN
11764 || token->type == CPP_CLOSE_BRACE
11765 || token->type == CPP_CLOSE_SQUARE)
11769 case CPP_OPEN_PAREN:
11770 case CPP_OPEN_SQUARE:
11771 case CPP_OPEN_BRACE:
11776 /* If we see a non-nested `>', and `>' is not an
11777 operator, then it marks the end of the default
11779 if (!depth && !greater_than_is_operator_p)
11783 /* If we run out of tokens, issue an error message. */
11785 error ("file ends in default argument");
11791 /* In these cases, we should look for template-ids.
11792 For example, if the default argument is
11793 `X<int, double>()', we need to do name lookup to
11794 figure out whether or not `X' is a template; if
11795 so, the `,' does not end the default argument.
11797 That is not yet done. */
11804 /* If we've reached the end, stop. */
11808 /* Add the token to the token block. */
11809 token = cp_lexer_consume_token (parser->lexer);
11812 /* Create a DEFAULT_ARG to represented the unparsed default
11814 default_argument = make_node (DEFAULT_ARG);
11815 DEFARG_TOKENS (default_argument)
11816 = cp_token_cache_new (first_token, token);
11818 /* Outside of a class definition, we can just parse the
11819 assignment-expression. */
11822 bool saved_local_variables_forbidden_p;
11824 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11826 saved_greater_than_is_operator_p
11827 = parser->greater_than_is_operator_p;
11828 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11829 /* Local variable names (and the `this' keyword) may not
11830 appear in a default argument. */
11831 saved_local_variables_forbidden_p
11832 = parser->local_variables_forbidden_p;
11833 parser->local_variables_forbidden_p = true;
11834 /* Parse the assignment-expression. */
11835 default_argument = cp_parser_assignment_expression (parser);
11836 /* Restore saved state. */
11837 parser->greater_than_is_operator_p
11838 = saved_greater_than_is_operator_p;
11839 parser->local_variables_forbidden_p
11840 = saved_local_variables_forbidden_p;
11842 if (!parser->default_arg_ok_p)
11844 if (!flag_pedantic_errors)
11845 warning ("deprecated use of default argument for parameter of non-function");
11848 error ("default arguments are only permitted for function parameters");
11849 default_argument = NULL_TREE;
11854 default_argument = NULL_TREE;
11856 return make_parameter_declarator (&decl_specifiers,
11861 /* Parse a function-body.
11864 compound_statement */
11867 cp_parser_function_body (cp_parser *parser)
11869 cp_parser_compound_statement (parser, NULL, false);
11872 /* Parse a ctor-initializer-opt followed by a function-body. Return
11873 true if a ctor-initializer was present. */
11876 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11879 bool ctor_initializer_p;
11881 /* Begin the function body. */
11882 body = begin_function_body ();
11883 /* Parse the optional ctor-initializer. */
11884 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11885 /* Parse the function-body. */
11886 cp_parser_function_body (parser);
11887 /* Finish the function body. */
11888 finish_function_body (body);
11890 return ctor_initializer_p;
11893 /* Parse an initializer.
11896 = initializer-clause
11897 ( expression-list )
11899 Returns a expression representing the initializer. If no
11900 initializer is present, NULL_TREE is returned.
11902 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11903 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11904 set to FALSE if there is no initializer present. If there is an
11905 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11906 is set to true; otherwise it is set to false. */
11909 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11910 bool* non_constant_p)
11915 /* Peek at the next token. */
11916 token = cp_lexer_peek_token (parser->lexer);
11918 /* Let our caller know whether or not this initializer was
11920 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11921 /* Assume that the initializer is constant. */
11922 *non_constant_p = false;
11924 if (token->type == CPP_EQ)
11926 /* Consume the `='. */
11927 cp_lexer_consume_token (parser->lexer);
11928 /* Parse the initializer-clause. */
11929 init = cp_parser_initializer_clause (parser, non_constant_p);
11931 else if (token->type == CPP_OPEN_PAREN)
11932 init = cp_parser_parenthesized_expression_list (parser, false,
11936 /* Anything else is an error. */
11937 cp_parser_error (parser, "expected initializer");
11938 init = error_mark_node;
11944 /* Parse an initializer-clause.
11946 initializer-clause:
11947 assignment-expression
11948 { initializer-list , [opt] }
11951 Returns an expression representing the initializer.
11953 If the `assignment-expression' production is used the value
11954 returned is simply a representation for the expression.
11956 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11957 the elements of the initializer-list (or NULL_TREE, if the last
11958 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11959 NULL_TREE. There is no way to detect whether or not the optional
11960 trailing `,' was provided. NON_CONSTANT_P is as for
11961 cp_parser_initializer. */
11964 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11968 /* If it is not a `{', then we are looking at an
11969 assignment-expression. */
11970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11973 = cp_parser_constant_expression (parser,
11974 /*allow_non_constant_p=*/true,
11976 if (!*non_constant_p)
11977 initializer = fold_non_dependent_expr (initializer);
11981 /* Consume the `{' token. */
11982 cp_lexer_consume_token (parser->lexer);
11983 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11984 initializer = make_node (CONSTRUCTOR);
11985 /* If it's not a `}', then there is a non-trivial initializer. */
11986 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11988 /* Parse the initializer list. */
11989 CONSTRUCTOR_ELTS (initializer)
11990 = cp_parser_initializer_list (parser, non_constant_p);
11991 /* A trailing `,' token is allowed. */
11992 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11993 cp_lexer_consume_token (parser->lexer);
11995 /* Now, there should be a trailing `}'. */
11996 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11999 return initializer;
12002 /* Parse an initializer-list.
12006 initializer-list , initializer-clause
12011 identifier : initializer-clause
12012 initializer-list, identifier : initializer-clause
12014 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12015 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12016 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12017 as for cp_parser_initializer. */
12020 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12022 tree initializers = NULL_TREE;
12024 /* Assume all of the expressions are constant. */
12025 *non_constant_p = false;
12027 /* Parse the rest of the list. */
12033 bool clause_non_constant_p;
12035 /* If the next token is an identifier and the following one is a
12036 colon, we are looking at the GNU designated-initializer
12038 if (cp_parser_allow_gnu_extensions_p (parser)
12039 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12040 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12042 /* Consume the identifier. */
12043 identifier = cp_lexer_consume_token (parser->lexer)->value;
12044 /* Consume the `:'. */
12045 cp_lexer_consume_token (parser->lexer);
12048 identifier = NULL_TREE;
12050 /* Parse the initializer. */
12051 initializer = cp_parser_initializer_clause (parser,
12052 &clause_non_constant_p);
12053 /* If any clause is non-constant, so is the entire initializer. */
12054 if (clause_non_constant_p)
12055 *non_constant_p = true;
12056 /* Add it to the list. */
12057 initializers = tree_cons (identifier, initializer, initializers);
12059 /* If the next token is not a comma, we have reached the end of
12061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12064 /* Peek at the next token. */
12065 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12066 /* If the next token is a `}', then we're still done. An
12067 initializer-clause can have a trailing `,' after the
12068 initializer-list and before the closing `}'. */
12069 if (token->type == CPP_CLOSE_BRACE)
12072 /* Consume the `,' token. */
12073 cp_lexer_consume_token (parser->lexer);
12076 /* The initializers were built up in reverse order, so we need to
12077 reverse them now. */
12078 return nreverse (initializers);
12081 /* Classes [gram.class] */
12083 /* Parse a class-name.
12089 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12090 to indicate that names looked up in dependent types should be
12091 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12092 keyword has been used to indicate that the name that appears next
12093 is a template. TYPE_P is true iff the next name should be treated
12094 as class-name, even if it is declared to be some other kind of name
12095 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12096 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
12097 being defined in a class-head.
12099 Returns the TYPE_DECL representing the class. */
12102 cp_parser_class_name (cp_parser *parser,
12103 bool typename_keyword_p,
12104 bool template_keyword_p,
12106 bool check_dependency_p,
12108 bool is_declaration)
12115 /* All class-names start with an identifier. */
12116 token = cp_lexer_peek_token (parser->lexer);
12117 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12119 cp_parser_error (parser, "expected class-name");
12120 return error_mark_node;
12123 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12124 to a template-id, so we save it here. */
12125 scope = parser->scope;
12126 if (scope == error_mark_node)
12127 return error_mark_node;
12129 /* Any name names a type if we're following the `typename' keyword
12130 in a qualified name where the enclosing scope is type-dependent. */
12131 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12132 && dependent_type_p (scope));
12133 /* Handle the common case (an identifier, but not a template-id)
12135 if (token->type == CPP_NAME
12136 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12140 /* Look for the identifier. */
12141 identifier = cp_parser_identifier (parser);
12142 /* If the next token isn't an identifier, we are certainly not
12143 looking at a class-name. */
12144 if (identifier == error_mark_node)
12145 decl = error_mark_node;
12146 /* If we know this is a type-name, there's no need to look it
12148 else if (typename_p)
12152 /* If the next token is a `::', then the name must be a type
12155 [basic.lookup.qual]
12157 During the lookup for a name preceding the :: scope
12158 resolution operator, object, function, and enumerator
12159 names are ignored. */
12160 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12162 /* Look up the name. */
12163 decl = cp_parser_lookup_name (parser, identifier,
12165 /*is_template=*/false,
12166 /*is_namespace=*/false,
12167 check_dependency_p,
12168 /*ambiguous_p=*/NULL);
12173 /* Try a template-id. */
12174 decl = cp_parser_template_id (parser, template_keyword_p,
12175 check_dependency_p,
12177 if (decl == error_mark_node)
12178 return error_mark_node;
12181 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12183 /* If this is a typename, create a TYPENAME_TYPE. */
12184 if (typename_p && decl != error_mark_node)
12186 decl = make_typename_type (scope, decl, /*complain=*/1);
12187 if (decl != error_mark_node)
12188 decl = TYPE_NAME (decl);
12191 /* Check to see that it is really the name of a class. */
12192 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12193 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12194 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12195 /* Situations like this:
12197 template <typename T> struct A {
12198 typename T::template X<int>::I i;
12201 are problematic. Is `T::template X<int>' a class-name? The
12202 standard does not seem to be definitive, but there is no other
12203 valid interpretation of the following `::'. Therefore, those
12204 names are considered class-names. */
12205 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12206 else if (decl == error_mark_node
12207 || TREE_CODE (decl) != TYPE_DECL
12208 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12210 cp_parser_error (parser, "expected class-name");
12211 return error_mark_node;
12217 /* Parse a class-specifier.
12220 class-head { member-specification [opt] }
12222 Returns the TREE_TYPE representing the class. */
12225 cp_parser_class_specifier (cp_parser* parser)
12229 tree attributes = NULL_TREE;
12230 int has_trailing_semicolon;
12231 bool nested_name_specifier_p;
12232 unsigned saved_num_template_parameter_lists;
12233 bool pop_p = false;
12234 tree scope = NULL_TREE;
12236 push_deferring_access_checks (dk_no_deferred);
12238 /* Parse the class-head. */
12239 type = cp_parser_class_head (parser,
12240 &nested_name_specifier_p,
12242 /* If the class-head was a semantic disaster, skip the entire body
12246 cp_parser_skip_to_end_of_block_or_statement (parser);
12247 pop_deferring_access_checks ();
12248 return error_mark_node;
12251 /* Look for the `{'. */
12252 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12254 pop_deferring_access_checks ();
12255 return error_mark_node;
12258 /* Issue an error message if type-definitions are forbidden here. */
12259 cp_parser_check_type_definition (parser);
12260 /* Remember that we are defining one more class. */
12261 ++parser->num_classes_being_defined;
12262 /* Inside the class, surrounding template-parameter-lists do not
12264 saved_num_template_parameter_lists
12265 = parser->num_template_parameter_lists;
12266 parser->num_template_parameter_lists = 0;
12268 /* Start the class. */
12269 if (nested_name_specifier_p)
12271 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12272 pop_p = push_scope (scope);
12274 type = begin_class_definition (type);
12276 if (type == error_mark_node)
12277 /* If the type is erroneous, skip the entire body of the class. */
12278 cp_parser_skip_to_closing_brace (parser);
12280 /* Parse the member-specification. */
12281 cp_parser_member_specification_opt (parser);
12283 /* Look for the trailing `}'. */
12284 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12285 /* We get better error messages by noticing a common problem: a
12286 missing trailing `;'. */
12287 token = cp_lexer_peek_token (parser->lexer);
12288 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12289 /* Look for trailing attributes to apply to this class. */
12290 if (cp_parser_allow_gnu_extensions_p (parser))
12292 tree sub_attr = cp_parser_attributes_opt (parser);
12293 attributes = chainon (attributes, sub_attr);
12295 if (type != error_mark_node)
12296 type = finish_struct (type, attributes);
12299 /* If this class is not itself within the scope of another class,
12300 then we need to parse the bodies of all of the queued function
12301 definitions. Note that the queued functions defined in a class
12302 are not always processed immediately following the
12303 class-specifier for that class. Consider:
12306 struct B { void f() { sizeof (A); } };
12309 If `f' were processed before the processing of `A' were
12310 completed, there would be no way to compute the size of `A'.
12311 Note that the nesting we are interested in here is lexical --
12312 not the semantic nesting given by TYPE_CONTEXT. In particular,
12315 struct A { struct B; };
12316 struct A::B { void f() { } };
12318 there is no need to delay the parsing of `A::B::f'. */
12319 if (--parser->num_classes_being_defined == 0)
12326 /* In a first pass, parse default arguments to the functions.
12327 Then, in a second pass, parse the bodies of the functions.
12328 This two-phased approach handles cases like:
12336 class_type = NULL_TREE;
12338 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12339 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12340 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12341 TREE_PURPOSE (parser->unparsed_functions_queues)
12342 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12344 fn = TREE_VALUE (queue_entry);
12345 /* If there are default arguments that have not yet been processed,
12346 take care of them now. */
12347 if (class_type != TREE_PURPOSE (queue_entry))
12350 pop_scope (class_type);
12351 class_type = TREE_PURPOSE (queue_entry);
12352 pop_p = push_scope (class_type);
12354 /* Make sure that any template parameters are in scope. */
12355 maybe_begin_member_template_processing (fn);
12356 /* Parse the default argument expressions. */
12357 cp_parser_late_parsing_default_args (parser, fn);
12358 /* Remove any template parameters from the symbol table. */
12359 maybe_end_member_template_processing ();
12362 pop_scope (class_type);
12363 /* Now parse the body of the functions. */
12364 for (TREE_VALUE (parser->unparsed_functions_queues)
12365 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12366 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12367 TREE_VALUE (parser->unparsed_functions_queues)
12368 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12370 /* Figure out which function we need to process. */
12371 fn = TREE_VALUE (queue_entry);
12373 /* A hack to prevent garbage collection. */
12376 /* Parse the function. */
12377 cp_parser_late_parsing_for_member (parser, fn);
12382 /* Put back any saved access checks. */
12383 pop_deferring_access_checks ();
12385 /* Restore the count of active template-parameter-lists. */
12386 parser->num_template_parameter_lists
12387 = saved_num_template_parameter_lists;
12392 /* Parse a class-head.
12395 class-key identifier [opt] base-clause [opt]
12396 class-key nested-name-specifier identifier base-clause [opt]
12397 class-key nested-name-specifier [opt] template-id
12401 class-key attributes identifier [opt] base-clause [opt]
12402 class-key attributes nested-name-specifier identifier base-clause [opt]
12403 class-key attributes nested-name-specifier [opt] template-id
12406 Returns the TYPE of the indicated class. Sets
12407 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12408 involving a nested-name-specifier was used, and FALSE otherwise.
12410 Returns NULL_TREE if the class-head is syntactically valid, but
12411 semantically invalid in a way that means we should skip the entire
12412 body of the class. */
12415 cp_parser_class_head (cp_parser* parser,
12416 bool* nested_name_specifier_p,
12417 tree *attributes_p)
12419 tree nested_name_specifier;
12420 enum tag_types class_key;
12421 tree id = NULL_TREE;
12422 tree type = NULL_TREE;
12424 bool template_id_p = false;
12425 bool qualified_p = false;
12426 bool invalid_nested_name_p = false;
12427 bool invalid_explicit_specialization_p = false;
12428 bool pop_p = false;
12429 unsigned num_templates;
12432 /* Assume no nested-name-specifier will be present. */
12433 *nested_name_specifier_p = false;
12434 /* Assume no template parameter lists will be used in defining the
12438 /* Look for the class-key. */
12439 class_key = cp_parser_class_key (parser);
12440 if (class_key == none_type)
12441 return error_mark_node;
12443 /* Parse the attributes. */
12444 attributes = cp_parser_attributes_opt (parser);
12446 /* If the next token is `::', that is invalid -- but sometimes
12447 people do try to write:
12451 Handle this gracefully by accepting the extra qualifier, and then
12452 issuing an error about it later if this really is a
12453 class-head. If it turns out just to be an elaborated type
12454 specifier, remain silent. */
12455 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12456 qualified_p = true;
12458 push_deferring_access_checks (dk_no_check);
12460 /* Determine the name of the class. Begin by looking for an
12461 optional nested-name-specifier. */
12462 nested_name_specifier
12463 = cp_parser_nested_name_specifier_opt (parser,
12464 /*typename_keyword_p=*/false,
12465 /*check_dependency_p=*/false,
12467 /*is_declaration=*/false);
12468 /* If there was a nested-name-specifier, then there *must* be an
12470 if (nested_name_specifier)
12472 /* Although the grammar says `identifier', it really means
12473 `class-name' or `template-name'. You are only allowed to
12474 define a class that has already been declared with this
12477 The proposed resolution for Core Issue 180 says that whever
12478 you see `class T::X' you should treat `X' as a type-name.
12480 It is OK to define an inaccessible class; for example:
12482 class A { class B; };
12485 We do not know if we will see a class-name, or a
12486 template-name. We look for a class-name first, in case the
12487 class-name is a template-id; if we looked for the
12488 template-name first we would stop after the template-name. */
12489 cp_parser_parse_tentatively (parser);
12490 type = cp_parser_class_name (parser,
12491 /*typename_keyword_p=*/false,
12492 /*template_keyword_p=*/false,
12494 /*check_dependency_p=*/false,
12495 /*class_head_p=*/true,
12496 /*is_declaration=*/false);
12497 /* If that didn't work, ignore the nested-name-specifier. */
12498 if (!cp_parser_parse_definitely (parser))
12500 invalid_nested_name_p = true;
12501 id = cp_parser_identifier (parser);
12502 if (id == error_mark_node)
12505 /* If we could not find a corresponding TYPE, treat this
12506 declaration like an unqualified declaration. */
12507 if (type == error_mark_node)
12508 nested_name_specifier = NULL_TREE;
12509 /* Otherwise, count the number of templates used in TYPE and its
12510 containing scopes. */
12515 for (scope = TREE_TYPE (type);
12516 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12517 scope = (TYPE_P (scope)
12518 ? TYPE_CONTEXT (scope)
12519 : DECL_CONTEXT (scope)))
12521 && CLASS_TYPE_P (scope)
12522 && CLASSTYPE_TEMPLATE_INFO (scope)
12523 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12524 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12528 /* Otherwise, the identifier is optional. */
12531 /* We don't know whether what comes next is a template-id,
12532 an identifier, or nothing at all. */
12533 cp_parser_parse_tentatively (parser);
12534 /* Check for a template-id. */
12535 id = cp_parser_template_id (parser,
12536 /*template_keyword_p=*/false,
12537 /*check_dependency_p=*/true,
12538 /*is_declaration=*/true);
12539 /* If that didn't work, it could still be an identifier. */
12540 if (!cp_parser_parse_definitely (parser))
12542 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12543 id = cp_parser_identifier (parser);
12549 template_id_p = true;
12554 pop_deferring_access_checks ();
12557 cp_parser_check_for_invalid_template_id (parser, id);
12559 /* If it's not a `:' or a `{' then we can't really be looking at a
12560 class-head, since a class-head only appears as part of a
12561 class-specifier. We have to detect this situation before calling
12562 xref_tag, since that has irreversible side-effects. */
12563 if (!cp_parser_next_token_starts_class_definition_p (parser))
12565 cp_parser_error (parser, "expected %<{%> or %<:%>");
12566 return error_mark_node;
12569 /* At this point, we're going ahead with the class-specifier, even
12570 if some other problem occurs. */
12571 cp_parser_commit_to_tentative_parse (parser);
12572 /* Issue the error about the overly-qualified name now. */
12574 cp_parser_error (parser,
12575 "global qualification of class name is invalid");
12576 else if (invalid_nested_name_p)
12577 cp_parser_error (parser,
12578 "qualified name does not name a class");
12579 else if (nested_name_specifier)
12582 /* Figure out in what scope the declaration is being placed. */
12583 scope = current_scope ();
12585 scope = current_namespace;
12586 /* If that scope does not contain the scope in which the
12587 class was originally declared, the program is invalid. */
12588 if (scope && !is_ancestor (scope, nested_name_specifier))
12590 error ("declaration of %qD in %qD which does not enclose %qD",
12591 type, scope, nested_name_specifier);
12597 A declarator-id shall not be qualified exception of the
12598 definition of a ... nested class outside of its class
12599 ... [or] a the definition or explicit instantiation of a
12600 class member of a namespace outside of its namespace. */
12601 if (scope == nested_name_specifier)
12603 pedwarn ("extra qualification ignored");
12604 nested_name_specifier = NULL_TREE;
12608 /* An explicit-specialization must be preceded by "template <>". If
12609 it is not, try to recover gracefully. */
12610 if (at_namespace_scope_p ()
12611 && parser->num_template_parameter_lists == 0
12614 error ("an explicit specialization must be preceded by %<template <>%>");
12615 invalid_explicit_specialization_p = true;
12616 /* Take the same action that would have been taken by
12617 cp_parser_explicit_specialization. */
12618 ++parser->num_template_parameter_lists;
12619 begin_specialization ();
12621 /* There must be no "return" statements between this point and the
12622 end of this function; set "type "to the correct return value and
12623 use "goto done;" to return. */
12624 /* Make sure that the right number of template parameters were
12626 if (!cp_parser_check_template_parameters (parser, num_templates))
12628 /* If something went wrong, there is no point in even trying to
12629 process the class-definition. */
12634 /* Look up the type. */
12637 type = TREE_TYPE (id);
12638 maybe_process_partial_specialization (type);
12640 else if (!nested_name_specifier)
12642 /* If the class was unnamed, create a dummy name. */
12644 id = make_anon_name ();
12645 type = xref_tag (class_key, id, /*globalize=*/false,
12646 parser->num_template_parameter_lists);
12651 bool pop_p = false;
12655 template <typename T> struct S { struct T };
12656 template <typename T> struct S<T>::T { };
12658 we will get a TYPENAME_TYPE when processing the definition of
12659 `S::T'. We need to resolve it to the actual type before we
12660 try to define it. */
12661 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12663 class_type = resolve_typename_type (TREE_TYPE (type),
12664 /*only_current_p=*/false);
12665 if (class_type != error_mark_node)
12666 type = TYPE_NAME (class_type);
12669 cp_parser_error (parser, "could not resolve typename type");
12670 type = error_mark_node;
12674 maybe_process_partial_specialization (TREE_TYPE (type));
12675 class_type = current_class_type;
12676 /* Enter the scope indicated by the nested-name-specifier. */
12677 if (nested_name_specifier)
12678 pop_p = push_scope (nested_name_specifier);
12679 /* Get the canonical version of this type. */
12680 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12681 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12682 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12683 type = push_template_decl (type);
12684 type = TREE_TYPE (type);
12685 if (nested_name_specifier)
12687 *nested_name_specifier_p = true;
12689 pop_scope (nested_name_specifier);
12692 /* Indicate whether this class was declared as a `class' or as a
12694 if (TREE_CODE (type) == RECORD_TYPE)
12695 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12696 cp_parser_check_class_key (class_key, type);
12698 /* Enter the scope containing the class; the names of base classes
12699 should be looked up in that context. For example, given:
12701 struct A { struct B {}; struct C; };
12702 struct A::C : B {};
12705 if (nested_name_specifier)
12706 pop_p = push_scope (nested_name_specifier);
12710 /* Get the list of base-classes, if there is one. */
12711 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12712 bases = cp_parser_base_clause (parser);
12714 /* Process the base classes. */
12715 xref_basetypes (type, bases);
12717 /* Leave the scope given by the nested-name-specifier. We will
12718 enter the class scope itself while processing the members. */
12720 pop_scope (nested_name_specifier);
12723 if (invalid_explicit_specialization_p)
12725 end_specialization ();
12726 --parser->num_template_parameter_lists;
12728 *attributes_p = attributes;
12732 /* Parse a class-key.
12739 Returns the kind of class-key specified, or none_type to indicate
12742 static enum tag_types
12743 cp_parser_class_key (cp_parser* parser)
12746 enum tag_types tag_type;
12748 /* Look for the class-key. */
12749 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12753 /* Check to see if the TOKEN is a class-key. */
12754 tag_type = cp_parser_token_is_class_key (token);
12756 cp_parser_error (parser, "expected class-key");
12760 /* Parse an (optional) member-specification.
12762 member-specification:
12763 member-declaration member-specification [opt]
12764 access-specifier : member-specification [opt] */
12767 cp_parser_member_specification_opt (cp_parser* parser)
12774 /* Peek at the next token. */
12775 token = cp_lexer_peek_token (parser->lexer);
12776 /* If it's a `}', or EOF then we've seen all the members. */
12777 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12780 /* See if this token is a keyword. */
12781 keyword = token->keyword;
12785 case RID_PROTECTED:
12787 /* Consume the access-specifier. */
12788 cp_lexer_consume_token (parser->lexer);
12789 /* Remember which access-specifier is active. */
12790 current_access_specifier = token->value;
12791 /* Look for the `:'. */
12792 cp_parser_require (parser, CPP_COLON, "`:'");
12796 /* Accept #pragmas at class scope. */
12797 if (token->type == CPP_PRAGMA)
12799 cp_lexer_handle_pragma (parser->lexer);
12803 /* Otherwise, the next construction must be a
12804 member-declaration. */
12805 cp_parser_member_declaration (parser);
12810 /* Parse a member-declaration.
12812 member-declaration:
12813 decl-specifier-seq [opt] member-declarator-list [opt] ;
12814 function-definition ; [opt]
12815 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12817 template-declaration
12819 member-declarator-list:
12821 member-declarator-list , member-declarator
12824 declarator pure-specifier [opt]
12825 declarator constant-initializer [opt]
12826 identifier [opt] : constant-expression
12830 member-declaration:
12831 __extension__ member-declaration
12834 declarator attributes [opt] pure-specifier [opt]
12835 declarator attributes [opt] constant-initializer [opt]
12836 identifier [opt] attributes [opt] : constant-expression */
12839 cp_parser_member_declaration (cp_parser* parser)
12841 cp_decl_specifier_seq decl_specifiers;
12842 tree prefix_attributes;
12844 int declares_class_or_enum;
12847 int saved_pedantic;
12849 /* Check for the `__extension__' keyword. */
12850 if (cp_parser_extension_opt (parser, &saved_pedantic))
12853 cp_parser_member_declaration (parser);
12854 /* Restore the old value of the PEDANTIC flag. */
12855 pedantic = saved_pedantic;
12860 /* Check for a template-declaration. */
12861 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12863 /* Parse the template-declaration. */
12864 cp_parser_template_declaration (parser, /*member_p=*/true);
12869 /* Check for a using-declaration. */
12870 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12872 /* Parse the using-declaration. */
12873 cp_parser_using_declaration (parser);
12878 /* Parse the decl-specifier-seq. */
12879 cp_parser_decl_specifier_seq (parser,
12880 CP_PARSER_FLAGS_OPTIONAL,
12882 &declares_class_or_enum);
12883 prefix_attributes = decl_specifiers.attributes;
12884 decl_specifiers.attributes = NULL_TREE;
12885 /* Check for an invalid type-name. */
12886 if (!decl_specifiers.type
12887 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12889 /* If there is no declarator, then the decl-specifier-seq should
12891 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12893 /* If there was no decl-specifier-seq, and the next token is a
12894 `;', then we have something like:
12900 Each member-declaration shall declare at least one member
12901 name of the class. */
12902 if (!decl_specifiers.any_specifiers_p)
12904 cp_token *token = cp_lexer_peek_token (parser->lexer);
12905 if (pedantic && !token->in_system_header)
12906 pedwarn ("%Hextra %<;%>", &token->location);
12912 /* See if this declaration is a friend. */
12913 friend_p = cp_parser_friend_p (&decl_specifiers);
12914 /* If there were decl-specifiers, check to see if there was
12915 a class-declaration. */
12916 type = check_tag_decl (&decl_specifiers);
12917 /* Nested classes have already been added to the class, but
12918 a `friend' needs to be explicitly registered. */
12921 /* If the `friend' keyword was present, the friend must
12922 be introduced with a class-key. */
12923 if (!declares_class_or_enum)
12924 error ("a class-key must be used when declaring a friend");
12927 template <typename T> struct A {
12928 friend struct A<T>::B;
12931 A<T>::B will be represented by a TYPENAME_TYPE, and
12932 therefore not recognized by check_tag_decl. */
12934 && decl_specifiers.type
12935 && TYPE_P (decl_specifiers.type))
12936 type = decl_specifiers.type;
12937 if (!type || !TYPE_P (type))
12938 error ("friend declaration does not name a class or "
12941 make_friend_class (current_class_type, type,
12942 /*complain=*/true);
12944 /* If there is no TYPE, an error message will already have
12946 else if (!type || type == error_mark_node)
12948 /* An anonymous aggregate has to be handled specially; such
12949 a declaration really declares a data member (with a
12950 particular type), as opposed to a nested class. */
12951 else if (ANON_AGGR_TYPE_P (type))
12953 /* Remove constructors and such from TYPE, now that we
12954 know it is an anonymous aggregate. */
12955 fixup_anonymous_aggr (type);
12956 /* And make the corresponding data member. */
12957 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12958 /* Add it to the class. */
12959 finish_member_declaration (decl);
12962 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12967 /* See if these declarations will be friends. */
12968 friend_p = cp_parser_friend_p (&decl_specifiers);
12970 /* Keep going until we hit the `;' at the end of the
12972 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12974 tree attributes = NULL_TREE;
12975 tree first_attribute;
12977 /* Peek at the next token. */
12978 token = cp_lexer_peek_token (parser->lexer);
12980 /* Check for a bitfield declaration. */
12981 if (token->type == CPP_COLON
12982 || (token->type == CPP_NAME
12983 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12989 /* Get the name of the bitfield. Note that we cannot just
12990 check TOKEN here because it may have been invalidated by
12991 the call to cp_lexer_peek_nth_token above. */
12992 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12993 identifier = cp_parser_identifier (parser);
12995 identifier = NULL_TREE;
12997 /* Consume the `:' token. */
12998 cp_lexer_consume_token (parser->lexer);
12999 /* Get the width of the bitfield. */
13001 = cp_parser_constant_expression (parser,
13002 /*allow_non_constant=*/false,
13005 /* Look for attributes that apply to the bitfield. */
13006 attributes = cp_parser_attributes_opt (parser);
13007 /* Remember which attributes are prefix attributes and
13009 first_attribute = attributes;
13010 /* Combine the attributes. */
13011 attributes = chainon (prefix_attributes, attributes);
13013 /* Create the bitfield declaration. */
13014 decl = grokbitfield (identifier
13015 ? make_id_declarator (identifier)
13019 /* Apply the attributes. */
13020 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13024 cp_declarator *declarator;
13026 tree asm_specification;
13027 int ctor_dtor_or_conv_p;
13029 /* Parse the declarator. */
13031 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13032 &ctor_dtor_or_conv_p,
13033 /*parenthesized_p=*/NULL,
13034 /*member_p=*/true);
13036 /* If something went wrong parsing the declarator, make sure
13037 that we at least consume some tokens. */
13038 if (declarator == cp_error_declarator)
13040 /* Skip to the end of the statement. */
13041 cp_parser_skip_to_end_of_statement (parser);
13042 /* If the next token is not a semicolon, that is
13043 probably because we just skipped over the body of
13044 a function. So, we consume a semicolon if
13045 present, but do not issue an error message if it
13047 if (cp_lexer_next_token_is (parser->lexer,
13049 cp_lexer_consume_token (parser->lexer);
13053 cp_parser_check_for_definition_in_return_type
13054 (declarator, declares_class_or_enum);
13056 /* Look for an asm-specification. */
13057 asm_specification = cp_parser_asm_specification_opt (parser);
13058 /* Look for attributes that apply to the declaration. */
13059 attributes = cp_parser_attributes_opt (parser);
13060 /* Remember which attributes are prefix attributes and
13062 first_attribute = attributes;
13063 /* Combine the attributes. */
13064 attributes = chainon (prefix_attributes, attributes);
13066 /* If it's an `=', then we have a constant-initializer or a
13067 pure-specifier. It is not correct to parse the
13068 initializer before registering the member declaration
13069 since the member declaration should be in scope while
13070 its initializer is processed. However, the rest of the
13071 front end does not yet provide an interface that allows
13072 us to handle this correctly. */
13073 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13077 A pure-specifier shall be used only in the declaration of
13078 a virtual function.
13080 A member-declarator can contain a constant-initializer
13081 only if it declares a static member of integral or
13084 Therefore, if the DECLARATOR is for a function, we look
13085 for a pure-specifier; otherwise, we look for a
13086 constant-initializer. When we call `grokfield', it will
13087 perform more stringent semantics checks. */
13088 if (declarator->kind == cdk_function)
13089 initializer = cp_parser_pure_specifier (parser);
13091 /* Parse the initializer. */
13092 initializer = cp_parser_constant_initializer (parser);
13094 /* Otherwise, there is no initializer. */
13096 initializer = NULL_TREE;
13098 /* See if we are probably looking at a function
13099 definition. We are certainly not looking at at a
13100 member-declarator. Calling `grokfield' has
13101 side-effects, so we must not do it unless we are sure
13102 that we are looking at a member-declarator. */
13103 if (cp_parser_token_starts_function_definition_p
13104 (cp_lexer_peek_token (parser->lexer)))
13106 /* The grammar does not allow a pure-specifier to be
13107 used when a member function is defined. (It is
13108 possible that this fact is an oversight in the
13109 standard, since a pure function may be defined
13110 outside of the class-specifier. */
13112 error ("pure-specifier on function-definition");
13113 decl = cp_parser_save_member_function_body (parser,
13117 /* If the member was not a friend, declare it here. */
13119 finish_member_declaration (decl);
13120 /* Peek at the next token. */
13121 token = cp_lexer_peek_token (parser->lexer);
13122 /* If the next token is a semicolon, consume it. */
13123 if (token->type == CPP_SEMICOLON)
13124 cp_lexer_consume_token (parser->lexer);
13129 /* Create the declaration. */
13130 decl = grokfield (declarator, &decl_specifiers,
13131 initializer, asm_specification,
13133 /* Any initialization must have been from a
13134 constant-expression. */
13135 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13136 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13140 /* Reset PREFIX_ATTRIBUTES. */
13141 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13142 attributes = TREE_CHAIN (attributes);
13144 TREE_CHAIN (attributes) = NULL_TREE;
13146 /* If there is any qualification still in effect, clear it
13147 now; we will be starting fresh with the next declarator. */
13148 parser->scope = NULL_TREE;
13149 parser->qualifying_scope = NULL_TREE;
13150 parser->object_scope = NULL_TREE;
13151 /* If it's a `,', then there are more declarators. */
13152 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13153 cp_lexer_consume_token (parser->lexer);
13154 /* If the next token isn't a `;', then we have a parse error. */
13155 else if (cp_lexer_next_token_is_not (parser->lexer,
13158 cp_parser_error (parser, "expected %<;%>");
13159 /* Skip tokens until we find a `;'. */
13160 cp_parser_skip_to_end_of_statement (parser);
13167 /* Add DECL to the list of members. */
13169 finish_member_declaration (decl);
13171 if (TREE_CODE (decl) == FUNCTION_DECL)
13172 cp_parser_save_default_args (parser, decl);
13177 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13180 /* Parse a pure-specifier.
13185 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13186 Otherwise, ERROR_MARK_NODE is returned. */
13189 cp_parser_pure_specifier (cp_parser* parser)
13193 /* Look for the `=' token. */
13194 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13195 return error_mark_node;
13196 /* Look for the `0' token. */
13197 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13198 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13199 to get information from the lexer about how the number was
13200 spelled in order to fix this problem. */
13201 if (!token || !integer_zerop (token->value))
13202 return error_mark_node;
13204 return integer_zero_node;
13207 /* Parse a constant-initializer.
13209 constant-initializer:
13210 = constant-expression
13212 Returns a representation of the constant-expression. */
13215 cp_parser_constant_initializer (cp_parser* parser)
13217 /* Look for the `=' token. */
13218 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13219 return error_mark_node;
13221 /* It is invalid to write:
13223 struct S { static const int i = { 7 }; };
13226 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13228 cp_parser_error (parser,
13229 "a brace-enclosed initializer is not allowed here");
13230 /* Consume the opening brace. */
13231 cp_lexer_consume_token (parser->lexer);
13232 /* Skip the initializer. */
13233 cp_parser_skip_to_closing_brace (parser);
13234 /* Look for the trailing `}'. */
13235 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13237 return error_mark_node;
13240 return cp_parser_constant_expression (parser,
13241 /*allow_non_constant=*/false,
13245 /* Derived classes [gram.class.derived] */
13247 /* Parse a base-clause.
13250 : base-specifier-list
13252 base-specifier-list:
13254 base-specifier-list , base-specifier
13256 Returns a TREE_LIST representing the base-classes, in the order in
13257 which they were declared. The representation of each node is as
13258 described by cp_parser_base_specifier.
13260 In the case that no bases are specified, this function will return
13261 NULL_TREE, not ERROR_MARK_NODE. */
13264 cp_parser_base_clause (cp_parser* parser)
13266 tree bases = NULL_TREE;
13268 /* Look for the `:' that begins the list. */
13269 cp_parser_require (parser, CPP_COLON, "`:'");
13271 /* Scan the base-specifier-list. */
13277 /* Look for the base-specifier. */
13278 base = cp_parser_base_specifier (parser);
13279 /* Add BASE to the front of the list. */
13280 if (base != error_mark_node)
13282 TREE_CHAIN (base) = bases;
13285 /* Peek at the next token. */
13286 token = cp_lexer_peek_token (parser->lexer);
13287 /* If it's not a comma, then the list is complete. */
13288 if (token->type != CPP_COMMA)
13290 /* Consume the `,'. */
13291 cp_lexer_consume_token (parser->lexer);
13294 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13295 base class had a qualified name. However, the next name that
13296 appears is certainly not qualified. */
13297 parser->scope = NULL_TREE;
13298 parser->qualifying_scope = NULL_TREE;
13299 parser->object_scope = NULL_TREE;
13301 return nreverse (bases);
13304 /* Parse a base-specifier.
13307 :: [opt] nested-name-specifier [opt] class-name
13308 virtual access-specifier [opt] :: [opt] nested-name-specifier
13310 access-specifier virtual [opt] :: [opt] nested-name-specifier
13313 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13314 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13315 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13316 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13319 cp_parser_base_specifier (cp_parser* parser)
13323 bool virtual_p = false;
13324 bool duplicate_virtual_error_issued_p = false;
13325 bool duplicate_access_error_issued_p = false;
13326 bool class_scope_p, template_p;
13327 tree access = access_default_node;
13330 /* Process the optional `virtual' and `access-specifier'. */
13333 /* Peek at the next token. */
13334 token = cp_lexer_peek_token (parser->lexer);
13335 /* Process `virtual'. */
13336 switch (token->keyword)
13339 /* If `virtual' appears more than once, issue an error. */
13340 if (virtual_p && !duplicate_virtual_error_issued_p)
13342 cp_parser_error (parser,
13343 "%<virtual%> specified more than once in base-specified");
13344 duplicate_virtual_error_issued_p = true;
13349 /* Consume the `virtual' token. */
13350 cp_lexer_consume_token (parser->lexer);
13355 case RID_PROTECTED:
13357 /* If more than one access specifier appears, issue an
13359 if (access != access_default_node
13360 && !duplicate_access_error_issued_p)
13362 cp_parser_error (parser,
13363 "more than one access specifier in base-specified");
13364 duplicate_access_error_issued_p = true;
13367 access = ridpointers[(int) token->keyword];
13369 /* Consume the access-specifier. */
13370 cp_lexer_consume_token (parser->lexer);
13379 /* It is not uncommon to see programs mechanically, erroneously, use
13380 the 'typename' keyword to denote (dependent) qualified types
13381 as base classes. */
13382 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13384 if (!processing_template_decl)
13385 error ("keyword %<typename%> not allowed outside of templates");
13387 error ("keyword %<typename%> not allowed in this context "
13388 "(the base class is implicitly a type)");
13389 cp_lexer_consume_token (parser->lexer);
13392 /* Look for the optional `::' operator. */
13393 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13394 /* Look for the nested-name-specifier. The simplest way to
13399 The keyword `typename' is not permitted in a base-specifier or
13400 mem-initializer; in these contexts a qualified name that
13401 depends on a template-parameter is implicitly assumed to be a
13404 is to pretend that we have seen the `typename' keyword at this
13406 cp_parser_nested_name_specifier_opt (parser,
13407 /*typename_keyword_p=*/true,
13408 /*check_dependency_p=*/true,
13410 /*is_declaration=*/true);
13411 /* If the base class is given by a qualified name, assume that names
13412 we see are type names or templates, as appropriate. */
13413 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13414 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13416 /* Finally, look for the class-name. */
13417 type = cp_parser_class_name (parser,
13421 /*check_dependency_p=*/true,
13422 /*class_head_p=*/false,
13423 /*is_declaration=*/true);
13425 if (type == error_mark_node)
13426 return error_mark_node;
13428 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13431 /* Exception handling [gram.exception] */
13433 /* Parse an (optional) exception-specification.
13435 exception-specification:
13436 throw ( type-id-list [opt] )
13438 Returns a TREE_LIST representing the exception-specification. The
13439 TREE_VALUE of each node is a type. */
13442 cp_parser_exception_specification_opt (cp_parser* parser)
13447 /* Peek at the next token. */
13448 token = cp_lexer_peek_token (parser->lexer);
13449 /* If it's not `throw', then there's no exception-specification. */
13450 if (!cp_parser_is_keyword (token, RID_THROW))
13453 /* Consume the `throw'. */
13454 cp_lexer_consume_token (parser->lexer);
13456 /* Look for the `('. */
13457 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13459 /* Peek at the next token. */
13460 token = cp_lexer_peek_token (parser->lexer);
13461 /* If it's not a `)', then there is a type-id-list. */
13462 if (token->type != CPP_CLOSE_PAREN)
13464 const char *saved_message;
13466 /* Types may not be defined in an exception-specification. */
13467 saved_message = parser->type_definition_forbidden_message;
13468 parser->type_definition_forbidden_message
13469 = "types may not be defined in an exception-specification";
13470 /* Parse the type-id-list. */
13471 type_id_list = cp_parser_type_id_list (parser);
13472 /* Restore the saved message. */
13473 parser->type_definition_forbidden_message = saved_message;
13476 type_id_list = empty_except_spec;
13478 /* Look for the `)'. */
13479 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13481 return type_id_list;
13484 /* Parse an (optional) type-id-list.
13488 type-id-list , type-id
13490 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13491 in the order that the types were presented. */
13494 cp_parser_type_id_list (cp_parser* parser)
13496 tree types = NULL_TREE;
13503 /* Get the next type-id. */
13504 type = cp_parser_type_id (parser);
13505 /* Add it to the list. */
13506 types = add_exception_specifier (types, type, /*complain=*/1);
13507 /* Peek at the next token. */
13508 token = cp_lexer_peek_token (parser->lexer);
13509 /* If it is not a `,', we are done. */
13510 if (token->type != CPP_COMMA)
13512 /* Consume the `,'. */
13513 cp_lexer_consume_token (parser->lexer);
13516 return nreverse (types);
13519 /* Parse a try-block.
13522 try compound-statement handler-seq */
13525 cp_parser_try_block (cp_parser* parser)
13529 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13530 try_block = begin_try_block ();
13531 cp_parser_compound_statement (parser, NULL, true);
13532 finish_try_block (try_block);
13533 cp_parser_handler_seq (parser);
13534 finish_handler_sequence (try_block);
13539 /* Parse a function-try-block.
13541 function-try-block:
13542 try ctor-initializer [opt] function-body handler-seq */
13545 cp_parser_function_try_block (cp_parser* parser)
13548 bool ctor_initializer_p;
13550 /* Look for the `try' keyword. */
13551 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13553 /* Let the rest of the front-end know where we are. */
13554 try_block = begin_function_try_block ();
13555 /* Parse the function-body. */
13557 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13558 /* We're done with the `try' part. */
13559 finish_function_try_block (try_block);
13560 /* Parse the handlers. */
13561 cp_parser_handler_seq (parser);
13562 /* We're done with the handlers. */
13563 finish_function_handler_sequence (try_block);
13565 return ctor_initializer_p;
13568 /* Parse a handler-seq.
13571 handler handler-seq [opt] */
13574 cp_parser_handler_seq (cp_parser* parser)
13580 /* Parse the handler. */
13581 cp_parser_handler (parser);
13582 /* Peek at the next token. */
13583 token = cp_lexer_peek_token (parser->lexer);
13584 /* If it's not `catch' then there are no more handlers. */
13585 if (!cp_parser_is_keyword (token, RID_CATCH))
13590 /* Parse a handler.
13593 catch ( exception-declaration ) compound-statement */
13596 cp_parser_handler (cp_parser* parser)
13601 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13602 handler = begin_handler ();
13603 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13604 declaration = cp_parser_exception_declaration (parser);
13605 finish_handler_parms (declaration, handler);
13606 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13607 cp_parser_compound_statement (parser, NULL, false);
13608 finish_handler (handler);
13611 /* Parse an exception-declaration.
13613 exception-declaration:
13614 type-specifier-seq declarator
13615 type-specifier-seq abstract-declarator
13619 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13620 ellipsis variant is used. */
13623 cp_parser_exception_declaration (cp_parser* parser)
13626 cp_decl_specifier_seq type_specifiers;
13627 cp_declarator *declarator;
13628 const char *saved_message;
13630 /* If it's an ellipsis, it's easy to handle. */
13631 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13633 /* Consume the `...' token. */
13634 cp_lexer_consume_token (parser->lexer);
13638 /* Types may not be defined in exception-declarations. */
13639 saved_message = parser->type_definition_forbidden_message;
13640 parser->type_definition_forbidden_message
13641 = "types may not be defined in exception-declarations";
13643 /* Parse the type-specifier-seq. */
13644 cp_parser_type_specifier_seq (parser, &type_specifiers);
13645 /* If it's a `)', then there is no declarator. */
13646 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13649 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13650 /*ctor_dtor_or_conv_p=*/NULL,
13651 /*parenthesized_p=*/NULL,
13652 /*member_p=*/false);
13654 /* Restore the saved message. */
13655 parser->type_definition_forbidden_message = saved_message;
13657 if (type_specifiers.any_specifiers_p)
13659 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13660 if (decl == NULL_TREE)
13661 error ("invalid catch parameter");
13669 /* Parse a throw-expression.
13672 throw assignment-expression [opt]
13674 Returns a THROW_EXPR representing the throw-expression. */
13677 cp_parser_throw_expression (cp_parser* parser)
13682 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13683 token = cp_lexer_peek_token (parser->lexer);
13684 /* Figure out whether or not there is an assignment-expression
13685 following the "throw" keyword. */
13686 if (token->type == CPP_COMMA
13687 || token->type == CPP_SEMICOLON
13688 || token->type == CPP_CLOSE_PAREN
13689 || token->type == CPP_CLOSE_SQUARE
13690 || token->type == CPP_CLOSE_BRACE
13691 || token->type == CPP_COLON)
13692 expression = NULL_TREE;
13694 expression = cp_parser_assignment_expression (parser);
13696 return build_throw (expression);
13699 /* GNU Extensions */
13701 /* Parse an (optional) asm-specification.
13704 asm ( string-literal )
13706 If the asm-specification is present, returns a STRING_CST
13707 corresponding to the string-literal. Otherwise, returns
13711 cp_parser_asm_specification_opt (cp_parser* parser)
13714 tree asm_specification;
13716 /* Peek at the next token. */
13717 token = cp_lexer_peek_token (parser->lexer);
13718 /* If the next token isn't the `asm' keyword, then there's no
13719 asm-specification. */
13720 if (!cp_parser_is_keyword (token, RID_ASM))
13723 /* Consume the `asm' token. */
13724 cp_lexer_consume_token (parser->lexer);
13725 /* Look for the `('. */
13726 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13728 /* Look for the string-literal. */
13729 asm_specification = cp_parser_string_literal (parser, false, false);
13731 /* Look for the `)'. */
13732 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13734 return asm_specification;
13737 /* Parse an asm-operand-list.
13741 asm-operand-list , asm-operand
13744 string-literal ( expression )
13745 [ string-literal ] string-literal ( expression )
13747 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13748 each node is the expression. The TREE_PURPOSE is itself a
13749 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13750 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13751 is a STRING_CST for the string literal before the parenthesis. */
13754 cp_parser_asm_operand_list (cp_parser* parser)
13756 tree asm_operands = NULL_TREE;
13760 tree string_literal;
13764 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13766 /* Consume the `[' token. */
13767 cp_lexer_consume_token (parser->lexer);
13768 /* Read the operand name. */
13769 name = cp_parser_identifier (parser);
13770 if (name != error_mark_node)
13771 name = build_string (IDENTIFIER_LENGTH (name),
13772 IDENTIFIER_POINTER (name));
13773 /* Look for the closing `]'. */
13774 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13778 /* Look for the string-literal. */
13779 string_literal = cp_parser_string_literal (parser, false, false);
13781 /* Look for the `('. */
13782 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13783 /* Parse the expression. */
13784 expression = cp_parser_expression (parser);
13785 /* Look for the `)'. */
13786 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13788 /* Add this operand to the list. */
13789 asm_operands = tree_cons (build_tree_list (name, string_literal),
13792 /* If the next token is not a `,', there are no more
13794 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13796 /* Consume the `,'. */
13797 cp_lexer_consume_token (parser->lexer);
13800 return nreverse (asm_operands);
13803 /* Parse an asm-clobber-list.
13807 asm-clobber-list , string-literal
13809 Returns a TREE_LIST, indicating the clobbers in the order that they
13810 appeared. The TREE_VALUE of each node is a STRING_CST. */
13813 cp_parser_asm_clobber_list (cp_parser* parser)
13815 tree clobbers = NULL_TREE;
13819 tree string_literal;
13821 /* Look for the string literal. */
13822 string_literal = cp_parser_string_literal (parser, false, false);
13823 /* Add it to the list. */
13824 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13825 /* If the next token is not a `,', then the list is
13827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13829 /* Consume the `,' token. */
13830 cp_lexer_consume_token (parser->lexer);
13836 /* Parse an (optional) series of attributes.
13839 attributes attribute
13842 __attribute__ (( attribute-list [opt] ))
13844 The return value is as for cp_parser_attribute_list. */
13847 cp_parser_attributes_opt (cp_parser* parser)
13849 tree attributes = NULL_TREE;
13854 tree attribute_list;
13856 /* Peek at the next token. */
13857 token = cp_lexer_peek_token (parser->lexer);
13858 /* If it's not `__attribute__', then we're done. */
13859 if (token->keyword != RID_ATTRIBUTE)
13862 /* Consume the `__attribute__' keyword. */
13863 cp_lexer_consume_token (parser->lexer);
13864 /* Look for the two `(' tokens. */
13865 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13866 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13868 /* Peek at the next token. */
13869 token = cp_lexer_peek_token (parser->lexer);
13870 if (token->type != CPP_CLOSE_PAREN)
13871 /* Parse the attribute-list. */
13872 attribute_list = cp_parser_attribute_list (parser);
13874 /* If the next token is a `)', then there is no attribute
13876 attribute_list = NULL;
13878 /* Look for the two `)' tokens. */
13879 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13880 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13882 /* Add these new attributes to the list. */
13883 attributes = chainon (attributes, attribute_list);
13889 /* Parse an attribute-list.
13893 attribute-list , attribute
13897 identifier ( identifier )
13898 identifier ( identifier , expression-list )
13899 identifier ( expression-list )
13901 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13902 TREE_PURPOSE of each node is the identifier indicating which
13903 attribute is in use. The TREE_VALUE represents the arguments, if
13907 cp_parser_attribute_list (cp_parser* parser)
13909 tree attribute_list = NULL_TREE;
13910 bool save_translate_strings_p = parser->translate_strings_p;
13912 parser->translate_strings_p = false;
13919 /* Look for the identifier. We also allow keywords here; for
13920 example `__attribute__ ((const))' is legal. */
13921 token = cp_lexer_peek_token (parser->lexer);
13922 if (token->type != CPP_NAME
13923 && token->type != CPP_KEYWORD)
13924 return error_mark_node;
13925 /* Consume the token. */
13926 token = cp_lexer_consume_token (parser->lexer);
13928 /* Save away the identifier that indicates which attribute this is. */
13929 identifier = token->value;
13930 attribute = build_tree_list (identifier, NULL_TREE);
13932 /* Peek at the next token. */
13933 token = cp_lexer_peek_token (parser->lexer);
13934 /* If it's an `(', then parse the attribute arguments. */
13935 if (token->type == CPP_OPEN_PAREN)
13939 arguments = (cp_parser_parenthesized_expression_list
13940 (parser, true, /*non_constant_p=*/NULL));
13941 /* Save the identifier and arguments away. */
13942 TREE_VALUE (attribute) = arguments;
13945 /* Add this attribute to the list. */
13946 TREE_CHAIN (attribute) = attribute_list;
13947 attribute_list = attribute;
13949 /* Now, look for more attributes. */
13950 token = cp_lexer_peek_token (parser->lexer);
13951 /* If the next token isn't a `,', we're done. */
13952 if (token->type != CPP_COMMA)
13955 /* Consume the comma and keep going. */
13956 cp_lexer_consume_token (parser->lexer);
13958 parser->translate_strings_p = save_translate_strings_p;
13960 /* We built up the list in reverse order. */
13961 return nreverse (attribute_list);
13964 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13965 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13966 current value of the PEDANTIC flag, regardless of whether or not
13967 the `__extension__' keyword is present. The caller is responsible
13968 for restoring the value of the PEDANTIC flag. */
13971 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13973 /* Save the old value of the PEDANTIC flag. */
13974 *saved_pedantic = pedantic;
13976 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13978 /* Consume the `__extension__' token. */
13979 cp_lexer_consume_token (parser->lexer);
13980 /* We're not being pedantic while the `__extension__' keyword is
13990 /* Parse a label declaration.
13993 __label__ label-declarator-seq ;
13995 label-declarator-seq:
13996 identifier , label-declarator-seq
14000 cp_parser_label_declaration (cp_parser* parser)
14002 /* Look for the `__label__' keyword. */
14003 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14009 /* Look for an identifier. */
14010 identifier = cp_parser_identifier (parser);
14011 /* Declare it as a lobel. */
14012 finish_label_decl (identifier);
14013 /* If the next token is a `;', stop. */
14014 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14016 /* Look for the `,' separating the label declarations. */
14017 cp_parser_require (parser, CPP_COMMA, "`,'");
14020 /* Look for the final `;'. */
14021 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14024 /* Support Functions */
14026 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14027 NAME should have one of the representations used for an
14028 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14029 is returned. If PARSER->SCOPE is a dependent type, then a
14030 SCOPE_REF is returned.
14032 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14033 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14034 was formed. Abstractly, such entities should not be passed to this
14035 function, because they do not need to be looked up, but it is
14036 simpler to check for this special case here, rather than at the
14039 In cases not explicitly covered above, this function returns a
14040 DECL, OVERLOAD, or baselink representing the result of the lookup.
14041 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14044 If IS_TYPE is TRUE, bindings that do not refer to types are
14047 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14050 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14053 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14056 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14057 results in an ambiguity, and false otherwise. */
14060 cp_parser_lookup_name (cp_parser *parser, tree name,
14061 bool is_type, bool is_template, bool is_namespace,
14062 bool check_dependency,
14066 tree object_type = parser->context->object_type;
14068 /* Assume that the lookup will be unambiguous. */
14070 *ambiguous_p = false;
14072 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14073 no longer valid. Note that if we are parsing tentatively, and
14074 the parse fails, OBJECT_TYPE will be automatically restored. */
14075 parser->context->object_type = NULL_TREE;
14077 if (name == error_mark_node)
14078 return error_mark_node;
14080 /* A template-id has already been resolved; there is no lookup to
14082 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14084 if (BASELINK_P (name))
14086 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14087 == TEMPLATE_ID_EXPR);
14091 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14092 it should already have been checked to make sure that the name
14093 used matches the type being destroyed. */
14094 if (TREE_CODE (name) == BIT_NOT_EXPR)
14098 /* Figure out to which type this destructor applies. */
14100 type = parser->scope;
14101 else if (object_type)
14102 type = object_type;
14104 type = current_class_type;
14105 /* If that's not a class type, there is no destructor. */
14106 if (!type || !CLASS_TYPE_P (type))
14107 return error_mark_node;
14108 if (!CLASSTYPE_DESTRUCTORS (type))
14109 return error_mark_node;
14110 /* If it was a class type, return the destructor. */
14111 return CLASSTYPE_DESTRUCTORS (type);
14114 /* By this point, the NAME should be an ordinary identifier. If
14115 the id-expression was a qualified name, the qualifying scope is
14116 stored in PARSER->SCOPE at this point. */
14117 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14119 /* Perform the lookup. */
14124 if (parser->scope == error_mark_node)
14125 return error_mark_node;
14127 /* If the SCOPE is dependent, the lookup must be deferred until
14128 the template is instantiated -- unless we are explicitly
14129 looking up names in uninstantiated templates. Even then, we
14130 cannot look up the name if the scope is not a class type; it
14131 might, for example, be a template type parameter. */
14132 dependent_p = (TYPE_P (parser->scope)
14133 && !(parser->in_declarator_p
14134 && currently_open_class (parser->scope))
14135 && dependent_type_p (parser->scope));
14136 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14140 /* The resolution to Core Issue 180 says that `struct A::B'
14141 should be considered a type-name, even if `A' is
14143 decl = TYPE_NAME (make_typename_type (parser->scope,
14146 else if (is_template)
14147 decl = make_unbound_class_template (parser->scope,
14151 decl = build_nt (SCOPE_REF, parser->scope, name);
14155 bool pop_p = false;
14157 /* If PARSER->SCOPE is a dependent type, then it must be a
14158 class type, and we must not be checking dependencies;
14159 otherwise, we would have processed this lookup above. So
14160 that PARSER->SCOPE is not considered a dependent base by
14161 lookup_member, we must enter the scope here. */
14163 pop_p = push_scope (parser->scope);
14164 /* If the PARSER->SCOPE is a a template specialization, it
14165 may be instantiated during name lookup. In that case,
14166 errors may be issued. Even if we rollback the current
14167 tentative parse, those errors are valid. */
14168 decl = lookup_qualified_name (parser->scope, name, is_type,
14169 /*complain=*/true);
14171 pop_scope (parser->scope);
14173 parser->qualifying_scope = parser->scope;
14174 parser->object_scope = NULL_TREE;
14176 else if (object_type)
14178 tree object_decl = NULL_TREE;
14179 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14180 OBJECT_TYPE is not a class. */
14181 if (CLASS_TYPE_P (object_type))
14182 /* If the OBJECT_TYPE is a template specialization, it may
14183 be instantiated during name lookup. In that case, errors
14184 may be issued. Even if we rollback the current tentative
14185 parse, those errors are valid. */
14186 object_decl = lookup_member (object_type,
14188 /*protect=*/0, is_type);
14189 /* Look it up in the enclosing context, too. */
14190 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14191 /*block_p=*/true, is_namespace,
14193 parser->object_scope = object_type;
14194 parser->qualifying_scope = NULL_TREE;
14196 decl = object_decl;
14200 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14201 /*block_p=*/true, is_namespace,
14203 parser->qualifying_scope = NULL_TREE;
14204 parser->object_scope = NULL_TREE;
14207 /* If the lookup failed, let our caller know. */
14209 || decl == error_mark_node
14210 || (TREE_CODE (decl) == FUNCTION_DECL
14211 && DECL_ANTICIPATED (decl)))
14212 return error_mark_node;
14214 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14215 if (TREE_CODE (decl) == TREE_LIST)
14218 *ambiguous_p = true;
14219 /* The error message we have to print is too complicated for
14220 cp_parser_error, so we incorporate its actions directly. */
14221 if (!cp_parser_simulate_error (parser))
14223 error ("reference to %qD is ambiguous", name);
14224 print_candidates (decl);
14226 return error_mark_node;
14229 gcc_assert (DECL_P (decl)
14230 || TREE_CODE (decl) == OVERLOAD
14231 || TREE_CODE (decl) == SCOPE_REF
14232 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14233 || BASELINK_P (decl));
14235 /* If we have resolved the name of a member declaration, check to
14236 see if the declaration is accessible. When the name resolves to
14237 set of overloaded functions, accessibility is checked when
14238 overload resolution is done.
14240 During an explicit instantiation, access is not checked at all,
14241 as per [temp.explicit]. */
14243 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14248 /* Like cp_parser_lookup_name, but for use in the typical case where
14249 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14250 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14253 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14255 return cp_parser_lookup_name (parser, name,
14257 /*is_template=*/false,
14258 /*is_namespace=*/false,
14259 /*check_dependency=*/true,
14260 /*ambiguous_p=*/NULL);
14263 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14264 the current context, return the TYPE_DECL. If TAG_NAME_P is
14265 true, the DECL indicates the class being defined in a class-head,
14266 or declared in an elaborated-type-specifier.
14268 Otherwise, return DECL. */
14271 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14273 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14274 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14277 template <typename T> struct B;
14280 template <typename T> struct A::B {};
14282 Similarly, in a elaborated-type-specifier:
14284 namespace N { struct X{}; }
14287 template <typename T> friend struct N::X;
14290 However, if the DECL refers to a class type, and we are in
14291 the scope of the class, then the name lookup automatically
14292 finds the TYPE_DECL created by build_self_reference rather
14293 than a TEMPLATE_DECL. For example, in:
14295 template <class T> struct S {
14299 there is no need to handle such case. */
14301 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14302 return DECL_TEMPLATE_RESULT (decl);
14307 /* If too many, or too few, template-parameter lists apply to the
14308 declarator, issue an error message. Returns TRUE if all went well,
14309 and FALSE otherwise. */
14312 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14313 cp_declarator *declarator)
14315 unsigned num_templates;
14317 /* We haven't seen any classes that involve template parameters yet. */
14320 switch (declarator->kind)
14323 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14328 scope = TREE_OPERAND (declarator->u.id.name, 0);
14329 member = TREE_OPERAND (declarator->u.id.name, 1);
14331 while (scope && CLASS_TYPE_P (scope))
14333 /* You're supposed to have one `template <...>'
14334 for every template class, but you don't need one
14335 for a full specialization. For example:
14337 template <class T> struct S{};
14338 template <> struct S<int> { void f(); };
14339 void S<int>::f () {}
14341 is correct; there shouldn't be a `template <>' for
14342 the definition of `S<int>::f'. */
14343 if (CLASSTYPE_TEMPLATE_INFO (scope)
14344 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14345 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14346 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14349 scope = TYPE_CONTEXT (scope);
14353 /* If the DECLARATOR has the form `X<y>' then it uses one
14354 additional level of template parameters. */
14355 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14358 return cp_parser_check_template_parameters (parser,
14364 case cdk_reference:
14366 return (cp_parser_check_declarator_template_parameters
14367 (parser, declarator->declarator));
14373 gcc_unreachable ();
14378 /* NUM_TEMPLATES were used in the current declaration. If that is
14379 invalid, return FALSE and issue an error messages. Otherwise,
14383 cp_parser_check_template_parameters (cp_parser* parser,
14384 unsigned num_templates)
14386 /* If there are more template classes than parameter lists, we have
14389 template <class T> void S<T>::R<T>::f (); */
14390 if (parser->num_template_parameter_lists < num_templates)
14392 error ("too few template-parameter-lists");
14395 /* If there are the same number of template classes and parameter
14396 lists, that's OK. */
14397 if (parser->num_template_parameter_lists == num_templates)
14399 /* If there are more, but only one more, then we are referring to a
14400 member template. That's OK too. */
14401 if (parser->num_template_parameter_lists == num_templates + 1)
14403 /* Otherwise, there are too many template parameter lists. We have
14406 template <class T> template <class U> void S::f(); */
14407 error ("too many template-parameter-lists");
14411 /* Parse an optional `::' token indicating that the following name is
14412 from the global namespace. If so, PARSER->SCOPE is set to the
14413 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14414 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14415 Returns the new value of PARSER->SCOPE, if the `::' token is
14416 present, and NULL_TREE otherwise. */
14419 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14423 /* Peek at the next token. */
14424 token = cp_lexer_peek_token (parser->lexer);
14425 /* If we're looking at a `::' token then we're starting from the
14426 global namespace, not our current location. */
14427 if (token->type == CPP_SCOPE)
14429 /* Consume the `::' token. */
14430 cp_lexer_consume_token (parser->lexer);
14431 /* Set the SCOPE so that we know where to start the lookup. */
14432 parser->scope = global_namespace;
14433 parser->qualifying_scope = global_namespace;
14434 parser->object_scope = NULL_TREE;
14436 return parser->scope;
14438 else if (!current_scope_valid_p)
14440 parser->scope = NULL_TREE;
14441 parser->qualifying_scope = NULL_TREE;
14442 parser->object_scope = NULL_TREE;
14448 /* Returns TRUE if the upcoming token sequence is the start of a
14449 constructor declarator. If FRIEND_P is true, the declarator is
14450 preceded by the `friend' specifier. */
14453 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14455 bool constructor_p;
14456 tree type_decl = NULL_TREE;
14457 bool nested_name_p;
14458 cp_token *next_token;
14460 /* The common case is that this is not a constructor declarator, so
14461 try to avoid doing lots of work if at all possible. It's not
14462 valid declare a constructor at function scope. */
14463 if (at_function_scope_p ())
14465 /* And only certain tokens can begin a constructor declarator. */
14466 next_token = cp_lexer_peek_token (parser->lexer);
14467 if (next_token->type != CPP_NAME
14468 && next_token->type != CPP_SCOPE
14469 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14470 && next_token->type != CPP_TEMPLATE_ID)
14473 /* Parse tentatively; we are going to roll back all of the tokens
14475 cp_parser_parse_tentatively (parser);
14476 /* Assume that we are looking at a constructor declarator. */
14477 constructor_p = true;
14479 /* Look for the optional `::' operator. */
14480 cp_parser_global_scope_opt (parser,
14481 /*current_scope_valid_p=*/false);
14482 /* Look for the nested-name-specifier. */
14484 = (cp_parser_nested_name_specifier_opt (parser,
14485 /*typename_keyword_p=*/false,
14486 /*check_dependency_p=*/false,
14488 /*is_declaration=*/false)
14490 /* Outside of a class-specifier, there must be a
14491 nested-name-specifier. */
14492 if (!nested_name_p &&
14493 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14495 constructor_p = false;
14496 /* If we still think that this might be a constructor-declarator,
14497 look for a class-name. */
14502 template <typename T> struct S { S(); };
14503 template <typename T> S<T>::S ();
14505 we must recognize that the nested `S' names a class.
14508 template <typename T> S<T>::S<T> ();
14510 we must recognize that the nested `S' names a template. */
14511 type_decl = cp_parser_class_name (parser,
14512 /*typename_keyword_p=*/false,
14513 /*template_keyword_p=*/false,
14515 /*check_dependency_p=*/false,
14516 /*class_head_p=*/false,
14517 /*is_declaration=*/false);
14518 /* If there was no class-name, then this is not a constructor. */
14519 constructor_p = !cp_parser_error_occurred (parser);
14522 /* If we're still considering a constructor, we have to see a `(',
14523 to begin the parameter-declaration-clause, followed by either a
14524 `)', an `...', or a decl-specifier. We need to check for a
14525 type-specifier to avoid being fooled into thinking that:
14529 is a constructor. (It is actually a function named `f' that
14530 takes one parameter (of type `int') and returns a value of type
14533 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14535 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14536 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14537 /* A parameter declaration begins with a decl-specifier,
14538 which is either the "attribute" keyword, a storage class
14539 specifier, or (usually) a type-specifier. */
14540 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14541 && !cp_parser_storage_class_specifier_opt (parser))
14544 bool pop_p = false;
14545 unsigned saved_num_template_parameter_lists;
14547 /* Names appearing in the type-specifier should be looked up
14548 in the scope of the class. */
14549 if (current_class_type)
14553 type = TREE_TYPE (type_decl);
14554 if (TREE_CODE (type) == TYPENAME_TYPE)
14556 type = resolve_typename_type (type,
14557 /*only_current_p=*/false);
14558 if (type == error_mark_node)
14560 cp_parser_abort_tentative_parse (parser);
14564 pop_p = push_scope (type);
14567 /* Inside the constructor parameter list, surrounding
14568 template-parameter-lists do not apply. */
14569 saved_num_template_parameter_lists
14570 = parser->num_template_parameter_lists;
14571 parser->num_template_parameter_lists = 0;
14573 /* Look for the type-specifier. */
14574 cp_parser_type_specifier (parser,
14575 CP_PARSER_FLAGS_NONE,
14576 /*decl_specs=*/NULL,
14577 /*is_declarator=*/true,
14578 /*declares_class_or_enum=*/NULL,
14579 /*is_cv_qualifier=*/NULL);
14581 parser->num_template_parameter_lists
14582 = saved_num_template_parameter_lists;
14584 /* Leave the scope of the class. */
14588 constructor_p = !cp_parser_error_occurred (parser);
14592 constructor_p = false;
14593 /* We did not really want to consume any tokens. */
14594 cp_parser_abort_tentative_parse (parser);
14596 return constructor_p;
14599 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14600 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14601 they must be performed once we are in the scope of the function.
14603 Returns the function defined. */
14606 cp_parser_function_definition_from_specifiers_and_declarator
14607 (cp_parser* parser,
14608 cp_decl_specifier_seq *decl_specifiers,
14610 const cp_declarator *declarator)
14615 /* Begin the function-definition. */
14616 success_p = start_function (decl_specifiers, declarator, attributes);
14618 /* The things we're about to see are not directly qualified by any
14619 template headers we've seen thus far. */
14620 reset_specialization ();
14622 /* If there were names looked up in the decl-specifier-seq that we
14623 did not check, check them now. We must wait until we are in the
14624 scope of the function to perform the checks, since the function
14625 might be a friend. */
14626 perform_deferred_access_checks ();
14630 /* Skip the entire function. */
14631 error ("invalid function declaration");
14632 cp_parser_skip_to_end_of_block_or_statement (parser);
14633 fn = error_mark_node;
14636 fn = cp_parser_function_definition_after_declarator (parser,
14637 /*inline_p=*/false);
14642 /* Parse the part of a function-definition that follows the
14643 declarator. INLINE_P is TRUE iff this function is an inline
14644 function defined with a class-specifier.
14646 Returns the function defined. */
14649 cp_parser_function_definition_after_declarator (cp_parser* parser,
14653 bool ctor_initializer_p = false;
14654 bool saved_in_unbraced_linkage_specification_p;
14655 unsigned saved_num_template_parameter_lists;
14657 /* If the next token is `return', then the code may be trying to
14658 make use of the "named return value" extension that G++ used to
14660 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14662 /* Consume the `return' keyword. */
14663 cp_lexer_consume_token (parser->lexer);
14664 /* Look for the identifier that indicates what value is to be
14666 cp_parser_identifier (parser);
14667 /* Issue an error message. */
14668 error ("named return values are no longer supported");
14669 /* Skip tokens until we reach the start of the function body. */
14670 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14671 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14672 cp_lexer_consume_token (parser->lexer);
14674 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14675 anything declared inside `f'. */
14676 saved_in_unbraced_linkage_specification_p
14677 = parser->in_unbraced_linkage_specification_p;
14678 parser->in_unbraced_linkage_specification_p = false;
14679 /* Inside the function, surrounding template-parameter-lists do not
14681 saved_num_template_parameter_lists
14682 = parser->num_template_parameter_lists;
14683 parser->num_template_parameter_lists = 0;
14684 /* If the next token is `try', then we are looking at a
14685 function-try-block. */
14686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14687 ctor_initializer_p = cp_parser_function_try_block (parser);
14688 /* A function-try-block includes the function-body, so we only do
14689 this next part if we're not processing a function-try-block. */
14692 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14694 /* Finish the function. */
14695 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14696 (inline_p ? 2 : 0));
14697 /* Generate code for it, if necessary. */
14698 expand_or_defer_fn (fn);
14699 /* Restore the saved values. */
14700 parser->in_unbraced_linkage_specification_p
14701 = saved_in_unbraced_linkage_specification_p;
14702 parser->num_template_parameter_lists
14703 = saved_num_template_parameter_lists;
14708 /* Parse a template-declaration, assuming that the `export' (and
14709 `extern') keywords, if present, has already been scanned. MEMBER_P
14710 is as for cp_parser_template_declaration. */
14713 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14715 tree decl = NULL_TREE;
14716 tree parameter_list;
14717 bool friend_p = false;
14719 /* Look for the `template' keyword. */
14720 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14724 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14727 /* If the next token is `>', then we have an invalid
14728 specialization. Rather than complain about an invalid template
14729 parameter, issue an error message here. */
14730 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14732 cp_parser_error (parser, "invalid explicit specialization");
14733 begin_specialization ();
14734 parameter_list = NULL_TREE;
14738 /* Parse the template parameters. */
14739 begin_template_parm_list ();
14740 parameter_list = cp_parser_template_parameter_list (parser);
14741 parameter_list = end_template_parm_list (parameter_list);
14744 /* Look for the `>'. */
14745 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14746 /* We just processed one more parameter list. */
14747 ++parser->num_template_parameter_lists;
14748 /* If the next token is `template', there are more template
14750 if (cp_lexer_next_token_is_keyword (parser->lexer,
14752 cp_parser_template_declaration_after_export (parser, member_p);
14755 /* There are no access checks when parsing a template, as we do not
14756 know if a specialization will be a friend. */
14757 push_deferring_access_checks (dk_no_check);
14759 decl = cp_parser_single_declaration (parser,
14763 pop_deferring_access_checks ();
14765 /* If this is a member template declaration, let the front
14767 if (member_p && !friend_p && decl)
14769 if (TREE_CODE (decl) == TYPE_DECL)
14770 cp_parser_check_access_in_redeclaration (decl);
14772 decl = finish_member_template_decl (decl);
14774 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14775 make_friend_class (current_class_type, TREE_TYPE (decl),
14776 /*complain=*/true);
14778 /* We are done with the current parameter list. */
14779 --parser->num_template_parameter_lists;
14782 finish_template_decl (parameter_list);
14784 /* Register member declarations. */
14785 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14786 finish_member_declaration (decl);
14788 /* If DECL is a function template, we must return to parse it later.
14789 (Even though there is no definition, there might be default
14790 arguments that need handling.) */
14791 if (member_p && decl
14792 && (TREE_CODE (decl) == FUNCTION_DECL
14793 || DECL_FUNCTION_TEMPLATE_P (decl)))
14794 TREE_VALUE (parser->unparsed_functions_queues)
14795 = tree_cons (NULL_TREE, decl,
14796 TREE_VALUE (parser->unparsed_functions_queues));
14799 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14800 `function-definition' sequence. MEMBER_P is true, this declaration
14801 appears in a class scope.
14803 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14804 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14807 cp_parser_single_declaration (cp_parser* parser,
14811 int declares_class_or_enum;
14812 tree decl = NULL_TREE;
14813 cp_decl_specifier_seq decl_specifiers;
14814 bool function_definition_p = false;
14816 /* Defer access checks until we know what is being declared. */
14817 push_deferring_access_checks (dk_deferred);
14819 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14821 cp_parser_decl_specifier_seq (parser,
14822 CP_PARSER_FLAGS_OPTIONAL,
14824 &declares_class_or_enum);
14826 *friend_p = cp_parser_friend_p (&decl_specifiers);
14827 /* Gather up the access checks that occurred the
14828 decl-specifier-seq. */
14829 stop_deferring_access_checks ();
14831 /* Check for the declaration of a template class. */
14832 if (declares_class_or_enum)
14834 if (cp_parser_declares_only_class_p (parser))
14836 decl = shadow_tag (&decl_specifiers);
14841 friend template <typename T> struct A<T>::B;
14844 A<T>::B will be represented by a TYPENAME_TYPE, and
14845 therefore not recognized by shadow_tag. */
14846 if (friend_p && *friend_p
14848 && decl_specifiers.type
14849 && TYPE_P (decl_specifiers.type))
14850 decl = decl_specifiers.type;
14852 if (decl && decl != error_mark_node)
14853 decl = TYPE_NAME (decl);
14855 decl = error_mark_node;
14860 /* If it's not a template class, try for a template function. If
14861 the next token is a `;', then this declaration does not declare
14862 anything. But, if there were errors in the decl-specifiers, then
14863 the error might well have come from an attempted class-specifier.
14864 In that case, there's no need to warn about a missing declarator. */
14866 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14867 || decl_specifiers.type != error_mark_node))
14868 decl = cp_parser_init_declarator (parser,
14870 /*function_definition_allowed_p=*/true,
14872 declares_class_or_enum,
14873 &function_definition_p);
14875 pop_deferring_access_checks ();
14877 /* Clear any current qualification; whatever comes next is the start
14878 of something new. */
14879 parser->scope = NULL_TREE;
14880 parser->qualifying_scope = NULL_TREE;
14881 parser->object_scope = NULL_TREE;
14882 /* Look for a trailing `;' after the declaration. */
14883 if (!function_definition_p
14884 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14885 cp_parser_skip_to_end_of_block_or_statement (parser);
14890 /* Parse a cast-expression that is not the operand of a unary "&". */
14893 cp_parser_simple_cast_expression (cp_parser *parser)
14895 return cp_parser_cast_expression (parser, /*address_p=*/false);
14898 /* Parse a functional cast to TYPE. Returns an expression
14899 representing the cast. */
14902 cp_parser_functional_cast (cp_parser* parser, tree type)
14904 tree expression_list;
14908 = cp_parser_parenthesized_expression_list (parser, false,
14909 /*non_constant_p=*/NULL);
14911 cast = build_functional_cast (type, expression_list);
14912 /* [expr.const]/1: In an integral constant expression "only type
14913 conversions to integral or enumeration type can be used". */
14914 if (cast != error_mark_node && !type_dependent_expression_p (type)
14915 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14917 if (cp_parser_non_integral_constant_expression
14918 (parser, "a call to a constructor"))
14919 return error_mark_node;
14924 /* Save the tokens that make up the body of a member function defined
14925 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14926 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14927 specifiers applied to the declaration. Returns the FUNCTION_DECL
14928 for the member function. */
14931 cp_parser_save_member_function_body (cp_parser* parser,
14932 cp_decl_specifier_seq *decl_specifiers,
14933 cp_declarator *declarator,
14940 /* Create the function-declaration. */
14941 fn = start_method (decl_specifiers, declarator, attributes);
14942 /* If something went badly wrong, bail out now. */
14943 if (fn == error_mark_node)
14945 /* If there's a function-body, skip it. */
14946 if (cp_parser_token_starts_function_definition_p
14947 (cp_lexer_peek_token (parser->lexer)))
14948 cp_parser_skip_to_end_of_block_or_statement (parser);
14949 return error_mark_node;
14952 /* Remember it, if there default args to post process. */
14953 cp_parser_save_default_args (parser, fn);
14955 /* Save away the tokens that make up the body of the
14957 first = parser->lexer->next_token;
14958 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14959 /* Handle function try blocks. */
14960 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14961 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14962 last = parser->lexer->next_token;
14964 /* Save away the inline definition; we will process it when the
14965 class is complete. */
14966 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
14967 DECL_PENDING_INLINE_P (fn) = 1;
14969 /* We need to know that this was defined in the class, so that
14970 friend templates are handled correctly. */
14971 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14973 /* We're done with the inline definition. */
14974 finish_method (fn);
14976 /* Add FN to the queue of functions to be parsed later. */
14977 TREE_VALUE (parser->unparsed_functions_queues)
14978 = tree_cons (NULL_TREE, fn,
14979 TREE_VALUE (parser->unparsed_functions_queues));
14984 /* Parse a template-argument-list, as well as the trailing ">" (but
14985 not the opening ">"). See cp_parser_template_argument_list for the
14989 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14993 tree saved_qualifying_scope;
14994 tree saved_object_scope;
14995 bool saved_greater_than_is_operator_p;
14999 When parsing a template-id, the first non-nested `>' is taken as
15000 the end of the template-argument-list rather than a greater-than
15002 saved_greater_than_is_operator_p
15003 = parser->greater_than_is_operator_p;
15004 parser->greater_than_is_operator_p = false;
15005 /* Parsing the argument list may modify SCOPE, so we save it
15007 saved_scope = parser->scope;
15008 saved_qualifying_scope = parser->qualifying_scope;
15009 saved_object_scope = parser->object_scope;
15010 /* Parse the template-argument-list itself. */
15011 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15012 arguments = NULL_TREE;
15014 arguments = cp_parser_template_argument_list (parser);
15015 /* Look for the `>' that ends the template-argument-list. If we find
15016 a '>>' instead, it's probably just a typo. */
15017 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15019 if (!saved_greater_than_is_operator_p)
15021 /* If we're in a nested template argument list, the '>>' has
15022 to be a typo for '> >'. We emit the error message, but we
15023 continue parsing and we push a '>' as next token, so that
15024 the argument list will be parsed correctly. Note that the
15025 global source location is still on the token before the
15026 '>>', so we need to say explicitly where we want it. */
15027 cp_token *token = cp_lexer_peek_token (parser->lexer);
15028 error ("%H%<>>%> should be %<> >%> "
15029 "within a nested template argument list",
15032 /* ??? Proper recovery should terminate two levels of
15033 template argument list here. */
15034 token->type = CPP_GREATER;
15038 /* If this is not a nested template argument list, the '>>'
15039 is a typo for '>'. Emit an error message and continue.
15040 Same deal about the token location, but here we can get it
15041 right by consuming the '>>' before issuing the diagnostic. */
15042 cp_lexer_consume_token (parser->lexer);
15043 error ("spurious %<>>%>, use %<>%> to terminate "
15044 "a template argument list");
15047 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15048 error ("missing %<>%> to terminate the template argument list");
15050 /* It's what we want, a '>'; consume it. */
15051 cp_lexer_consume_token (parser->lexer);
15052 /* The `>' token might be a greater-than operator again now. */
15053 parser->greater_than_is_operator_p
15054 = saved_greater_than_is_operator_p;
15055 /* Restore the SAVED_SCOPE. */
15056 parser->scope = saved_scope;
15057 parser->qualifying_scope = saved_qualifying_scope;
15058 parser->object_scope = saved_object_scope;
15063 /* MEMBER_FUNCTION is a member function, or a friend. If default
15064 arguments, or the body of the function have not yet been parsed,
15068 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15070 /* If this member is a template, get the underlying
15072 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15073 member_function = DECL_TEMPLATE_RESULT (member_function);
15075 /* There should not be any class definitions in progress at this
15076 point; the bodies of members are only parsed outside of all class
15078 gcc_assert (parser->num_classes_being_defined == 0);
15079 /* While we're parsing the member functions we might encounter more
15080 classes. We want to handle them right away, but we don't want
15081 them getting mixed up with functions that are currently in the
15083 parser->unparsed_functions_queues
15084 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15086 /* Make sure that any template parameters are in scope. */
15087 maybe_begin_member_template_processing (member_function);
15089 /* If the body of the function has not yet been parsed, parse it
15091 if (DECL_PENDING_INLINE_P (member_function))
15093 tree function_scope;
15094 cp_token_cache *tokens;
15096 /* The function is no longer pending; we are processing it. */
15097 tokens = DECL_PENDING_INLINE_INFO (member_function);
15098 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15099 DECL_PENDING_INLINE_P (member_function) = 0;
15100 /* If this was an inline function in a local class, enter the scope
15101 of the containing function. */
15102 function_scope = decl_function_context (member_function);
15103 if (function_scope)
15104 push_function_context_to (function_scope);
15106 /* Push the body of the function onto the lexer stack. */
15107 cp_parser_push_lexer_for_tokens (parser, tokens);
15109 /* Let the front end know that we going to be defining this
15111 start_preparsed_function (member_function, NULL_TREE,
15112 SF_PRE_PARSED | SF_INCLASS_INLINE);
15114 /* Now, parse the body of the function. */
15115 cp_parser_function_definition_after_declarator (parser,
15116 /*inline_p=*/true);
15118 /* Leave the scope of the containing function. */
15119 if (function_scope)
15120 pop_function_context_from (function_scope);
15121 cp_parser_pop_lexer (parser);
15124 /* Remove any template parameters from the symbol table. */
15125 maybe_end_member_template_processing ();
15127 /* Restore the queue. */
15128 parser->unparsed_functions_queues
15129 = TREE_CHAIN (parser->unparsed_functions_queues);
15132 /* If DECL contains any default args, remember it on the unparsed
15133 functions queue. */
15136 cp_parser_save_default_args (cp_parser* parser, tree decl)
15140 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15142 probe = TREE_CHAIN (probe))
15143 if (TREE_PURPOSE (probe))
15145 TREE_PURPOSE (parser->unparsed_functions_queues)
15146 = tree_cons (current_class_type, decl,
15147 TREE_PURPOSE (parser->unparsed_functions_queues));
15153 /* FN is a FUNCTION_DECL which may contains a parameter with an
15154 unparsed DEFAULT_ARG. Parse the default args now. This function
15155 assumes that the current scope is the scope in which the default
15156 argument should be processed. */
15159 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15161 bool saved_local_variables_forbidden_p;
15164 /* While we're parsing the default args, we might (due to the
15165 statement expression extension) encounter more classes. We want
15166 to handle them right away, but we don't want them getting mixed
15167 up with default args that are currently in the queue. */
15168 parser->unparsed_functions_queues
15169 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15171 /* Local variable names (and the `this' keyword) may not appear
15172 in a default argument. */
15173 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15174 parser->local_variables_forbidden_p = true;
15176 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15178 parm = TREE_CHAIN (parm))
15180 cp_token_cache *tokens;
15182 if (!TREE_PURPOSE (parm)
15183 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15186 /* Push the saved tokens for the default argument onto the parser's
15188 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15189 cp_parser_push_lexer_for_tokens (parser, tokens);
15191 /* Parse the assignment-expression. */
15192 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15194 /* If the token stream has not been completely used up, then
15195 there was extra junk after the end of the default
15197 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15198 cp_parser_error (parser, "expected %<,%>");
15200 /* Revert to the main lexer. */
15201 cp_parser_pop_lexer (parser);
15204 /* Restore the state of local_variables_forbidden_p. */
15205 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15207 /* Restore the queue. */
15208 parser->unparsed_functions_queues
15209 = TREE_CHAIN (parser->unparsed_functions_queues);
15212 /* Parse the operand of `sizeof' (or a similar operator). Returns
15213 either a TYPE or an expression, depending on the form of the
15214 input. The KEYWORD indicates which kind of expression we have
15218 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15220 static const char *format;
15221 tree expr = NULL_TREE;
15222 const char *saved_message;
15223 bool saved_integral_constant_expression_p;
15225 /* Initialize FORMAT the first time we get here. */
15227 format = "types may not be defined in `%s' expressions";
15229 /* Types cannot be defined in a `sizeof' expression. Save away the
15231 saved_message = parser->type_definition_forbidden_message;
15232 /* And create the new one. */
15233 parser->type_definition_forbidden_message
15234 = xmalloc (strlen (format)
15235 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15237 sprintf ((char *) parser->type_definition_forbidden_message,
15238 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15240 /* The restrictions on constant-expressions do not apply inside
15241 sizeof expressions. */
15242 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15243 parser->integral_constant_expression_p = false;
15245 /* Do not actually evaluate the expression. */
15247 /* If it's a `(', then we might be looking at the type-id
15249 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15252 bool saved_in_type_id_in_expr_p;
15254 /* We can't be sure yet whether we're looking at a type-id or an
15256 cp_parser_parse_tentatively (parser);
15257 /* Consume the `('. */
15258 cp_lexer_consume_token (parser->lexer);
15259 /* Parse the type-id. */
15260 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15261 parser->in_type_id_in_expr_p = true;
15262 type = cp_parser_type_id (parser);
15263 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15264 /* Now, look for the trailing `)'. */
15265 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15266 /* If all went well, then we're done. */
15267 if (cp_parser_parse_definitely (parser))
15269 cp_decl_specifier_seq decl_specs;
15271 /* Build a trivial decl-specifier-seq. */
15272 clear_decl_specs (&decl_specs);
15273 decl_specs.type = type;
15275 /* Call grokdeclarator to figure out what type this is. */
15276 expr = grokdeclarator (NULL,
15280 /*attrlist=*/NULL);
15284 /* If the type-id production did not work out, then we must be
15285 looking at the unary-expression production. */
15287 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15288 /* Go back to evaluating expressions. */
15291 /* Free the message we created. */
15292 free ((char *) parser->type_definition_forbidden_message);
15293 /* And restore the old one. */
15294 parser->type_definition_forbidden_message = saved_message;
15295 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15300 /* If the current declaration has no declarator, return true. */
15303 cp_parser_declares_only_class_p (cp_parser *parser)
15305 /* If the next token is a `;' or a `,' then there is no
15307 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15308 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15311 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15314 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15315 cp_storage_class storage_class)
15317 if (decl_specs->storage_class != sc_none)
15318 decl_specs->multiple_storage_classes_p = true;
15320 decl_specs->storage_class = storage_class;
15323 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15324 is true, the type is a user-defined type; otherwise it is a
15325 built-in type specified by a keyword. */
15328 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15330 bool user_defined_p)
15332 decl_specs->any_specifiers_p = true;
15334 /* If the user tries to redeclare a built-in type (with, for example,
15335 in "typedef int wchar_t;") we remember that this is what
15336 happened. In system headers, we ignore these declarations so
15337 that G++ can work with system headers that are not C++-safe. */
15338 if (decl_specs->specs[(int) ds_typedef]
15340 && (decl_specs->type
15341 || decl_specs->specs[(int) ds_long]
15342 || decl_specs->specs[(int) ds_short]
15343 || decl_specs->specs[(int) ds_unsigned]
15344 || decl_specs->specs[(int) ds_signed]))
15346 decl_specs->redefined_builtin_type = type_spec;
15347 if (!decl_specs->type)
15349 decl_specs->type = type_spec;
15350 decl_specs->user_defined_type_p = false;
15353 else if (decl_specs->type)
15354 decl_specs->multiple_types_p = true;
15357 decl_specs->type = type_spec;
15358 decl_specs->user_defined_type_p = user_defined_p;
15359 decl_specs->redefined_builtin_type = NULL_TREE;
15363 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15364 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15367 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15369 return decl_specifiers->specs[(int) ds_friend] != 0;
15372 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15373 issue an error message indicating that TOKEN_DESC was expected.
15375 Returns the token consumed, if the token had the appropriate type.
15376 Otherwise, returns NULL. */
15379 cp_parser_require (cp_parser* parser,
15380 enum cpp_ttype type,
15381 const char* token_desc)
15383 if (cp_lexer_next_token_is (parser->lexer, type))
15384 return cp_lexer_consume_token (parser->lexer);
15387 /* Output the MESSAGE -- unless we're parsing tentatively. */
15388 if (!cp_parser_simulate_error (parser))
15390 char *message = concat ("expected ", token_desc, NULL);
15391 cp_parser_error (parser, message);
15398 /* Like cp_parser_require, except that tokens will be skipped until
15399 the desired token is found. An error message is still produced if
15400 the next token is not as expected. */
15403 cp_parser_skip_until_found (cp_parser* parser,
15404 enum cpp_ttype type,
15405 const char* token_desc)
15408 unsigned nesting_depth = 0;
15410 if (cp_parser_require (parser, type, token_desc))
15413 /* Skip tokens until the desired token is found. */
15416 /* Peek at the next token. */
15417 token = cp_lexer_peek_token (parser->lexer);
15418 /* If we've reached the token we want, consume it and
15420 if (token->type == type && !nesting_depth)
15422 cp_lexer_consume_token (parser->lexer);
15425 /* If we've run out of tokens, stop. */
15426 if (token->type == CPP_EOF)
15428 if (token->type == CPP_OPEN_BRACE
15429 || token->type == CPP_OPEN_PAREN
15430 || token->type == CPP_OPEN_SQUARE)
15432 else if (token->type == CPP_CLOSE_BRACE
15433 || token->type == CPP_CLOSE_PAREN
15434 || token->type == CPP_CLOSE_SQUARE)
15436 if (nesting_depth-- == 0)
15439 /* Consume this token. */
15440 cp_lexer_consume_token (parser->lexer);
15444 /* If the next token is the indicated keyword, consume it. Otherwise,
15445 issue an error message indicating that TOKEN_DESC was expected.
15447 Returns the token consumed, if the token had the appropriate type.
15448 Otherwise, returns NULL. */
15451 cp_parser_require_keyword (cp_parser* parser,
15453 const char* token_desc)
15455 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15457 if (token && token->keyword != keyword)
15459 dyn_string_t error_msg;
15461 /* Format the error message. */
15462 error_msg = dyn_string_new (0);
15463 dyn_string_append_cstr (error_msg, "expected ");
15464 dyn_string_append_cstr (error_msg, token_desc);
15465 cp_parser_error (parser, error_msg->s);
15466 dyn_string_delete (error_msg);
15473 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15474 function-definition. */
15477 cp_parser_token_starts_function_definition_p (cp_token* token)
15479 return (/* An ordinary function-body begins with an `{'. */
15480 token->type == CPP_OPEN_BRACE
15481 /* A ctor-initializer begins with a `:'. */
15482 || token->type == CPP_COLON
15483 /* A function-try-block begins with `try'. */
15484 || token->keyword == RID_TRY
15485 /* The named return value extension begins with `return'. */
15486 || token->keyword == RID_RETURN);
15489 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15493 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15497 token = cp_lexer_peek_token (parser->lexer);
15498 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15501 /* Returns TRUE iff the next token is the "," or ">" ending a
15502 template-argument. ">>" is also accepted (after the full
15503 argument was parsed) because it's probably a typo for "> >",
15504 and there is a specific diagnostic for this. */
15507 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15511 token = cp_lexer_peek_token (parser->lexer);
15512 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15513 || token->type == CPP_RSHIFT);
15516 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15517 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15520 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15525 token = cp_lexer_peek_nth_token (parser->lexer, n);
15526 if (token->type == CPP_LESS)
15528 /* Check for the sequence `<::' in the original code. It would be lexed as
15529 `[:', where `[' is a digraph, and there is no whitespace before
15531 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15534 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15535 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15541 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15542 or none_type otherwise. */
15544 static enum tag_types
15545 cp_parser_token_is_class_key (cp_token* token)
15547 switch (token->keyword)
15552 return record_type;
15561 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15564 cp_parser_check_class_key (enum tag_types class_key, tree type)
15566 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15567 pedwarn ("%qs tag used in naming %q#T",
15568 class_key == union_type ? "union"
15569 : class_key == record_type ? "struct" : "class",
15573 /* Issue an error message if DECL is redeclared with different
15574 access than its original declaration [class.access.spec/3].
15575 This applies to nested classes and nested class templates.
15579 cp_parser_check_access_in_redeclaration (tree decl)
15581 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15584 if ((TREE_PRIVATE (decl)
15585 != (current_access_specifier == access_private_node))
15586 || (TREE_PROTECTED (decl)
15587 != (current_access_specifier == access_protected_node)))
15588 error ("%qD redeclared with different access", decl);
15591 /* Look for the `template' keyword, as a syntactic disambiguator.
15592 Return TRUE iff it is present, in which case it will be
15596 cp_parser_optional_template_keyword (cp_parser *parser)
15598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15600 /* The `template' keyword can only be used within templates;
15601 outside templates the parser can always figure out what is a
15602 template and what is not. */
15603 if (!processing_template_decl)
15605 error ("%<template%> (as a disambiguator) is only allowed "
15606 "within templates");
15607 /* If this part of the token stream is rescanned, the same
15608 error message would be generated. So, we purge the token
15609 from the stream. */
15610 cp_lexer_purge_token (parser->lexer);
15615 /* Consume the `template' keyword. */
15616 cp_lexer_consume_token (parser->lexer);
15624 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15625 set PARSER->SCOPE, and perform other related actions. */
15628 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15633 /* Get the stored value. */
15634 value = cp_lexer_consume_token (parser->lexer)->value;
15635 /* Perform any access checks that were deferred. */
15636 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15637 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15638 /* Set the scope from the stored value. */
15639 parser->scope = TREE_VALUE (value);
15640 parser->qualifying_scope = TREE_TYPE (value);
15641 parser->object_scope = NULL_TREE;
15644 /* Consume tokens up through a non-nested END token. */
15647 cp_parser_cache_group (cp_parser *parser,
15648 enum cpp_ttype end,
15655 /* Abort a parenthesized expression if we encounter a brace. */
15656 if ((end == CPP_CLOSE_PAREN || depth == 0)
15657 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15659 /* If we've reached the end of the file, stop. */
15660 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15662 /* Consume the next token. */
15663 token = cp_lexer_consume_token (parser->lexer);
15664 /* See if it starts a new group. */
15665 if (token->type == CPP_OPEN_BRACE)
15667 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15671 else if (token->type == CPP_OPEN_PAREN)
15672 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15673 else if (token->type == end)
15678 /* Begin parsing tentatively. We always save tokens while parsing
15679 tentatively so that if the tentative parsing fails we can restore the
15683 cp_parser_parse_tentatively (cp_parser* parser)
15685 /* Enter a new parsing context. */
15686 parser->context = cp_parser_context_new (parser->context);
15687 /* Begin saving tokens. */
15688 cp_lexer_save_tokens (parser->lexer);
15689 /* In order to avoid repetitive access control error messages,
15690 access checks are queued up until we are no longer parsing
15692 push_deferring_access_checks (dk_deferred);
15695 /* Commit to the currently active tentative parse. */
15698 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15700 cp_parser_context *context;
15703 /* Mark all of the levels as committed. */
15704 lexer = parser->lexer;
15705 for (context = parser->context; context->next; context = context->next)
15707 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15709 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15710 while (!cp_lexer_saving_tokens (lexer))
15711 lexer = lexer->next;
15712 cp_lexer_commit_tokens (lexer);
15716 /* Abort the currently active tentative parse. All consumed tokens
15717 will be rolled back, and no diagnostics will be issued. */
15720 cp_parser_abort_tentative_parse (cp_parser* parser)
15722 cp_parser_simulate_error (parser);
15723 /* Now, pretend that we want to see if the construct was
15724 successfully parsed. */
15725 cp_parser_parse_definitely (parser);
15728 /* Stop parsing tentatively. If a parse error has occurred, restore the
15729 token stream. Otherwise, commit to the tokens we have consumed.
15730 Returns true if no error occurred; false otherwise. */
15733 cp_parser_parse_definitely (cp_parser* parser)
15735 bool error_occurred;
15736 cp_parser_context *context;
15738 /* Remember whether or not an error occurred, since we are about to
15739 destroy that information. */
15740 error_occurred = cp_parser_error_occurred (parser);
15741 /* Remove the topmost context from the stack. */
15742 context = parser->context;
15743 parser->context = context->next;
15744 /* If no parse errors occurred, commit to the tentative parse. */
15745 if (!error_occurred)
15747 /* Commit to the tokens read tentatively, unless that was
15749 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15750 cp_lexer_commit_tokens (parser->lexer);
15752 pop_to_parent_deferring_access_checks ();
15754 /* Otherwise, if errors occurred, roll back our state so that things
15755 are just as they were before we began the tentative parse. */
15758 cp_lexer_rollback_tokens (parser->lexer);
15759 pop_deferring_access_checks ();
15761 /* Add the context to the front of the free list. */
15762 context->next = cp_parser_context_free_list;
15763 cp_parser_context_free_list = context;
15765 return !error_occurred;
15768 /* Returns true if we are parsing tentatively -- but have decided that
15769 we will stick with this tentative parse, even if errors occur. */
15772 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15774 return (cp_parser_parsing_tentatively (parser)
15775 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15778 /* Returns nonzero iff an error has occurred during the most recent
15779 tentative parse. */
15782 cp_parser_error_occurred (cp_parser* parser)
15784 return (cp_parser_parsing_tentatively (parser)
15785 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15788 /* Returns nonzero if GNU extensions are allowed. */
15791 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15793 return parser->allow_gnu_extensions_p;
15799 static GTY (()) cp_parser *the_parser;
15801 /* External interface. */
15803 /* Parse one entire translation unit. */
15806 c_parse_file (void)
15808 bool error_occurred;
15809 static bool already_called = false;
15811 if (already_called)
15813 sorry ("inter-module optimizations not implemented for C++");
15816 already_called = true;
15818 the_parser = cp_parser_new ();
15819 push_deferring_access_checks (flag_access_control
15820 ? dk_no_deferred : dk_no_check);
15821 error_occurred = cp_parser_translation_unit (the_parser);
15825 /* This variable must be provided by every front end. */
15829 #include "gt-cp-parser.h"