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 /* The value associated with this token, if any. */
60 /* The location at which this token was found. */
64 /* The cp_lexer structure represents the C++ lexer. It is responsible
65 for managing the token stream from the preprocessor and supplying
66 it to the parser. Tokens are never added to the cp_lexer after
69 typedef struct cp_lexer GTY (())
71 /* The memory allocated for the buffer. Never NULL. */
72 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
73 /* A pointer just past the end of the memory allocated for the buffer. */
74 cp_token * GTY ((skip)) buffer_end;
75 /* A pointer just past the last available token. The tokens
76 in this lexer are [buffer, last_token). */
77 cp_token * GTY ((skip)) last_token;
79 /* The next available token. If NEXT_TOKEN is NULL, then there are
80 no more available tokens. */
81 cp_token * GTY ((skip)) next_token;
83 /* A stack indicating positions at which cp_lexer_save_tokens was
84 called. The top entry is the most recent position at which we
85 began saving tokens. The entries are differences in token
86 position between BUFFER and the first saved token.
87 If the stack is non-empty, we are saving tokens. */
88 varray_type saved_tokens;
90 /* True if we should output debugging information. */
93 /* The next lexer in a linked list of lexers. */
94 struct cp_lexer *next;
97 /* cp_token_cache is a range of tokens. There is no need to represent
98 allocate heap memory for it, since tokens are never removed from the
99 lexer's array. There is also no need for the GC to walk through
100 a cp_token_cache, since everything in here is referenced through
103 typedef struct cp_token_cache GTY(())
105 /* The beginning of the token range. */
106 cp_token * GTY((skip)) first;
108 /* Points immediately after the last token in the range. */
109 cp_token * GTY ((skip)) last;
114 static cp_lexer *cp_lexer_new_main
116 static cp_lexer *cp_lexer_new_from_tokens
117 (cp_token_cache *tokens);
118 static void cp_lexer_destroy
120 static int cp_lexer_saving_tokens
122 static cp_token *cp_lexer_next_token
123 (cp_lexer *, cp_token *);
124 static cp_token *cp_lexer_prev_token
125 (cp_lexer *, cp_token *);
126 static ptrdiff_t cp_lexer_token_difference
127 (cp_lexer *, cp_token *, cp_token *);
128 static void cp_lexer_grow_buffer
130 static void cp_lexer_get_preprocessor_token
131 (cp_lexer *, cp_token *);
132 static inline cp_token *cp_lexer_peek_token
134 static cp_token *cp_lexer_peek_nth_token
135 (cp_lexer *, size_t);
136 static inline bool cp_lexer_next_token_is
137 (cp_lexer *, enum cpp_ttype);
138 static bool cp_lexer_next_token_is_not
139 (cp_lexer *, enum cpp_ttype);
140 static bool cp_lexer_next_token_is_keyword
141 (cp_lexer *, enum rid);
142 static cp_token *cp_lexer_consume_token
144 static void cp_lexer_purge_token
146 static void cp_lexer_purge_tokens_after
147 (cp_lexer *, cp_token *);
148 static void cp_lexer_handle_pragma
150 static void cp_lexer_save_tokens
152 static void cp_lexer_commit_tokens
154 static void cp_lexer_rollback_tokens
156 #ifdef ENABLE_CHECKING
157 static void cp_lexer_print_token
158 (FILE *, cp_token *);
159 static inline bool cp_lexer_debugging_p
161 static void cp_lexer_start_debugging
162 (cp_lexer *) ATTRIBUTE_UNUSED;
163 static void cp_lexer_stop_debugging
164 (cp_lexer *) ATTRIBUTE_UNUSED;
165 static void cp_lexer_peek_token_emit_debug_info
166 (cp_lexer *, cp_token *);
168 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
169 about passing NULL to functions that require non-NULL arguments
170 (fputs, fprintf). It will never be used, so all we need is a value
171 of the right type that's guaranteed not to be NULL. */
172 #define cp_lexer_debug_stream stdout
173 #define cp_lexer_print_token(str, tok) (void) 0
174 #define cp_lexer_debugging_p(lexer) 0
175 #define cp_lexer_peek_token_emit_debug_info(lexer, tok) (void) 0
176 #endif /* ENABLE_CHECKING */
178 static cp_token_cache *cp_token_cache_new
179 (cp_token *, cp_token *);
181 /* Manifest constants. */
183 #define CP_LEXER_BUFFER_SIZE 10000
184 #define CP_SAVED_TOKENS_SIZE 5
186 /* A token type for keywords, as opposed to ordinary identifiers. */
187 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
189 /* A token type for template-ids. If a template-id is processed while
190 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
191 the value of the CPP_TEMPLATE_ID is whatever was returned by
192 cp_parser_template_id. */
193 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
195 /* A token type for nested-name-specifiers. If a
196 nested-name-specifier is processed while parsing tentatively, it is
197 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
198 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
199 cp_parser_nested_name_specifier_opt. */
200 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
202 /* A token type for tokens that are not tokens at all; these are used
203 to represent slots in the array where there used to be a token
204 that has now been deleted. */
205 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
207 /* The number of token types, including C++-specific ones. */
208 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
212 #ifdef ENABLE_CHECKING
213 /* The stream to which debugging output should be written. */
214 static FILE *cp_lexer_debug_stream;
215 #endif /* ENABLE_CHECKING */
217 /* Create a new main C++ lexer, the lexer that gets tokens from the
221 cp_lexer_new_main (void)
224 cp_token first_token;
226 /* Tell cpplib we want CPP_PRAGMA tokens. */
227 cpp_get_options (parse_in)->defer_pragmas = true;
229 /* Tell c_lex not to merge string constants. */
230 c_lex_return_raw_strings = true;
232 /* It's possible that lexing the first token will load a PCH file,
233 which is a GC collection point. So we have to grab the first
234 token before allocating any memory. */
235 cp_lexer_get_preprocessor_token (NULL, &first_token);
236 c_common_no_more_pch ();
238 /* Allocate the memory. */
239 lexer = GGC_CNEW (cp_lexer);
241 /* Create the buffer. */
242 lexer->buffer = ggc_calloc (CP_LEXER_BUFFER_SIZE, sizeof (cp_token));
243 lexer->buffer_end = lexer->buffer + CP_LEXER_BUFFER_SIZE;
245 /* There is one token in the buffer. */
246 lexer->last_token = lexer->buffer + 1;
247 lexer->next_token = lexer->buffer;
248 *lexer->next_token = first_token;
250 /* Create the SAVED_TOKENS stack. */
251 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
253 #ifdef ENABLE_CHECKING
254 /* Initially we are not debugging. */
255 lexer->debugging_p = false;
256 #endif /* ENABLE_CHECKING */
258 /* Get the rest of the tokens from the preprocessor. */
259 while (lexer->last_token[-1].type != CPP_EOF)
261 if (lexer->last_token == lexer->buffer_end)
262 cp_lexer_grow_buffer (lexer);
263 cp_lexer_get_preprocessor_token (lexer, lexer->last_token++);
266 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
267 direct calls to c_lex. Those callers all expect c_lex to do
268 string constant concatenation. */
269 c_lex_return_raw_strings = false;
271 gcc_assert (lexer->next_token->type != CPP_PURGED);
275 /* Create a new lexer whose token stream is primed with the tokens in
276 CACHE. When these tokens are exhausted, no new tokens will be read. */
279 cp_lexer_new_from_tokens (cp_token_cache *cache)
281 cp_token *first = cache->first;
282 cp_token *last = cache->last;
283 cp_lexer *lexer = GGC_CNEW (cp_lexer);
286 /* Allocate a new buffer. The reason we do this is to make sure
287 there's a CPP_EOF token at the end. An alternative would be to
288 modify cp_lexer_peek_token so that it checks for end-of-buffer
289 and returns a CPP_EOF when appropriate. */
291 lexer->buffer = GGC_NEWVEC (cp_token, (last - first) + 1);
292 memcpy (lexer->buffer, first, sizeof (cp_token) * (last - first));
293 lexer->next_token = lexer->buffer;
294 lexer->buffer_end = lexer->last_token = lexer->buffer + (last - first);
296 eof = lexer->buffer + (last - first);
298 eof->location = UNKNOWN_LOCATION;
299 eof->value = NULL_TREE;
300 eof->keyword = RID_MAX;
302 /* Create the SAVED_TOKENS stack. */
303 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
305 #ifdef ENABLE_CHECKING
306 /* Initially we are not debugging. */
307 lexer->debugging_p = false;
310 gcc_assert (lexer->next_token->type != CPP_PURGED);
314 /* Frees all resources associated with LEXER. */
317 cp_lexer_destroy (cp_lexer *lexer)
319 ggc_free (lexer->buffer);
323 /* Returns nonzero if debugging information should be output. */
325 #ifdef ENABLE_CHECKING
328 cp_lexer_debugging_p (cp_lexer *lexer)
330 return lexer->debugging_p;
333 #endif /* ENABLE_CHECKING */
335 /* TOKEN points into the circular token buffer. Return a pointer to
336 the next token in the buffer. */
338 static inline cp_token *
339 cp_lexer_next_token (cp_lexer* lexer ATTRIBUTE_UNUSED, cp_token* token)
345 /* TOKEN points into the circular token buffer. Return a pointer to
346 the previous token in the buffer. */
348 static inline cp_token *
349 cp_lexer_prev_token (cp_lexer* lexer ATTRIBUTE_UNUSED, cp_token* token)
354 /* nonzero if we are presently saving tokens. */
357 cp_lexer_saving_tokens (const cp_lexer* lexer)
359 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
362 /* Return a pointer to the token that is N tokens beyond TOKEN in the
365 static inline cp_token *
366 cp_lexer_advance_token (cp_lexer *lexer ATTRIBUTE_UNUSED,
367 cp_token *token, ptrdiff_t n)
372 /* Returns the number of times that START would have to be incremented
373 to reach FINISH. If START and FINISH are the same, returns zero. */
375 static inline ptrdiff_t
376 cp_lexer_token_difference (cp_lexer* lexer ATTRIBUTE_UNUSED,
377 cp_token* start, cp_token* finish)
379 return finish - start;
382 /* If the buffer is full, make it bigger. */
384 cp_lexer_grow_buffer (cp_lexer* lexer)
386 cp_token *old_buffer;
387 cp_token *new_buffer;
388 ptrdiff_t buffer_length;
390 /* This function should only be called when buffer is full. */
391 gcc_assert (lexer->last_token == lexer->buffer_end);
393 /* Remember the current buffer pointer. It will become invalid,
394 but we will need to do pointer arithmetic involving this
396 old_buffer = lexer->buffer;
397 /* Compute the current buffer size. */
398 buffer_length = lexer->buffer_end - lexer->buffer;
399 /* Allocate a buffer twice as big. */
400 new_buffer = ggc_realloc (lexer->buffer,
401 2 * buffer_length * sizeof (cp_token));
403 /* Recompute buffer positions. */
404 lexer->buffer = new_buffer;
405 lexer->buffer_end = new_buffer + 2 * buffer_length;
406 lexer->last_token = new_buffer + (lexer->last_token - old_buffer);
407 lexer->next_token = new_buffer + (lexer->next_token - old_buffer);
409 /* Clear the rest of the buffer. We never look at this storage,
410 but the garbage collector may. */
411 memset (lexer->last_token, 0,
412 (lexer->buffer_end - lexer->last_token) * sizeof(cp_token));
415 /* Store the next token from the preprocessor in *TOKEN. */
418 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
424 /* Keep going until we get a token we like. */
427 /* Get a new token from the preprocessor. */
428 token->type = c_lex_with_flags (&token->value, &token->flags);
429 /* Issue messages about tokens we cannot process. */
435 error ("invalid token");
439 /* This is a good token, so we exit the loop. */
444 /* Now we've got our token. */
445 token->location = input_location;
446 token->in_system_header = in_system_header;
448 /* Check to see if this token is a keyword. */
449 if (token->type == CPP_NAME
450 && C_IS_RESERVED_WORD (token->value))
452 /* Mark this token as a keyword. */
453 token->type = CPP_KEYWORD;
454 /* Record which keyword. */
455 token->keyword = C_RID_CODE (token->value);
456 /* Update the value. Some keywords are mapped to particular
457 entities, rather than simply having the value of the
458 corresponding IDENTIFIER_NODE. For example, `__const' is
459 mapped to `const'. */
460 token->value = ridpointers[token->keyword];
463 token->keyword = RID_MAX;
466 /* Update the globals input_location and in_system_header from TOKEN. */
468 cp_lexer_set_source_position_from_token (cp_token *token)
470 if (token->type != CPP_EOF)
472 input_location = token->location;
473 in_system_header = token->in_system_header;
477 /* Return a pointer to the next token in the token stream, but do not
480 static inline cp_token *
481 cp_lexer_peek_token (cp_lexer *lexer)
483 if (cp_lexer_debugging_p (lexer))
484 cp_lexer_peek_token_emit_debug_info (lexer, lexer->next_token);
485 return lexer->next_token;
488 #ifdef ENABLE_CHECKING
489 /* Emit debug output for cp_lexer_peek_token. Split out into a
490 separate function so that cp_lexer_peek_token can be small and
494 cp_lexer_peek_token_emit_debug_info (cp_lexer *lexer ATTRIBUTE_UNUSED,
495 cp_token *token ATTRIBUTE_UNUSED)
497 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
498 cp_lexer_print_token (cp_lexer_debug_stream, token);
499 putc ('\n', cp_lexer_debug_stream);
503 /* Return true if the next token has the indicated TYPE. */
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
508 return cp_lexer_peek_token (lexer)->type == type;
511 /* Return true if the next token does not have the indicated TYPE. */
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
516 return !cp_lexer_next_token_is (lexer, type);
519 /* Return true if the next token is the indicated KEYWORD. */
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
526 /* Peek at the next token. */
527 token = cp_lexer_peek_token (lexer);
528 /* Check to see if it is the indicated keyword. */
529 return token->keyword == keyword;
532 /* Return a pointer to the Nth token in the token stream. If N is 1,
533 then this is precisely equivalent to cp_lexer_peek_token (except
534 that it is not inline). One would like to disallow that case, but
535 there is one case (cp_parser_nth_token_starts_template_id) where
536 the caller passes a variable for N and it might be 1. */
539 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
543 /* N is 1-based, not zero-based. */
546 if (cp_lexer_debugging_p (lexer))
547 fprintf (cp_lexer_debug_stream,
548 "cp_lexer: peeking ahead %ld at token: ", (long)n);
551 token = lexer->next_token;
555 if (token->type != CPP_PURGED)
559 if (cp_lexer_debugging_p (lexer))
561 cp_lexer_print_token (cp_lexer_debug_stream, token);
562 putc ('\n', cp_lexer_debug_stream);
568 /* Return the next token, and advance the lexer's next_token pointer
569 to point to the next non-purged token. */
572 cp_lexer_consume_token (cp_lexer* lexer)
574 cp_token *token = lexer->next_token;
578 while (lexer->next_token->type == CPP_PURGED);
580 cp_lexer_set_source_position_from_token (token);
582 /* Provide debugging output. */
583 if (cp_lexer_debugging_p (lexer))
585 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
586 cp_lexer_print_token (cp_lexer_debug_stream, token);
587 putc ('\n', cp_lexer_debug_stream);
593 /* Permanently remove the next token from the token stream, and
594 advance the next_token pointer to refer to the next non-purged
598 cp_lexer_purge_token (cp_lexer *lexer)
600 cp_token *tok = lexer->next_token;
601 tok->type = CPP_PURGED;
602 tok->location = UNKNOWN_LOCATION;
603 tok->value = NULL_TREE;
604 tok->keyword = RID_MAX;
608 while (lexer->next_token->type == CPP_PURGED);
611 /* Permanently remove all tokens after TOK, up to, but not
612 including, the token that will be returned next by
613 cp_lexer_peek_token. */
616 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
620 peek = cp_lexer_peek_token (lexer);
621 gcc_assert (tok < peek);
623 for ( tok += 1; tok != peek; tok += 1)
625 tok->type = CPP_PURGED;
626 tok->location = UNKNOWN_LOCATION;
627 tok->value = NULL_TREE;
628 tok->keyword = RID_MAX;
632 /* Consume and handle a pragma token. */
634 cp_lexer_handle_pragma (cp_lexer *lexer)
637 cp_token *token = cp_lexer_consume_token (lexer);
638 gcc_assert (token->type == CPP_PRAGMA);
639 gcc_assert (token->value);
641 s.len = TREE_STRING_LENGTH (token->value);
642 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
644 cpp_handle_deferred_pragma (parse_in, &s);
646 /* Clearing token->value here means that we will get an ICE if we
647 try to process this #pragma again (which should be impossible). */
651 /* Begin saving tokens. All tokens consumed after this point will be
655 cp_lexer_save_tokens (cp_lexer* lexer)
657 /* Provide debugging output. */
658 if (cp_lexer_debugging_p (lexer))
659 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
661 VARRAY_PUSH_INT (lexer->saved_tokens,
662 cp_lexer_token_difference (lexer,
667 /* Commit to the portion of the token stream most recently saved. */
670 cp_lexer_commit_tokens (cp_lexer* lexer)
672 /* Provide debugging output. */
673 if (cp_lexer_debugging_p (lexer))
674 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
676 VARRAY_POP (lexer->saved_tokens);
679 /* Return all tokens saved since the last call to cp_lexer_save_tokens
680 to the token stream. Stop saving tokens. */
683 cp_lexer_rollback_tokens (cp_lexer* lexer)
687 /* Provide debugging output. */
688 if (cp_lexer_debugging_p (lexer))
689 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
691 /* Find the token that was the NEXT_TOKEN when we started saving
693 delta = VARRAY_TOP_INT(lexer->saved_tokens);
694 /* Make it the next token again now. */
695 lexer->next_token = cp_lexer_advance_token (lexer, lexer->buffer, delta);
697 /* Stop saving tokens. */
698 VARRAY_POP (lexer->saved_tokens);
701 /* Print a representation of the TOKEN on the STREAM. */
703 #ifdef ENABLE_CHECKING
706 cp_lexer_print_token (FILE * stream, cp_token *token)
708 /* We don't use cpp_type2name here because the parser defines
709 a few tokens of its own. */
710 static const char *const token_names[] = {
711 /* cpplib-defined token types */
717 /* C++ parser token types - see "Manifest constants", above. */
720 "NESTED_NAME_SPECIFIER",
724 /* If we have a name for the token, print it out. Otherwise, we
725 simply give the numeric code. */
726 gcc_assert (token->type < ARRAY_SIZE(token_names));
727 fputs (token_names[token->type], stream);
729 /* For some tokens, print the associated data. */
733 /* Some keywords have a value that is not an IDENTIFIER_NODE.
734 For example, `struct' is mapped to an INTEGER_CST. */
735 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
737 /* else fall through */
739 fputs (IDENTIFIER_POINTER (token->value), stream);
745 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
753 /* Start emitting debugging information. */
756 cp_lexer_start_debugging (cp_lexer* lexer)
758 ++lexer->debugging_p;
761 /* Stop emitting debugging information. */
764 cp_lexer_stop_debugging (cp_lexer* lexer)
766 --lexer->debugging_p;
769 #endif /* ENABLE_CHECKING */
771 /* Create a new cp_token_cache, representing a range of tokens. */
773 static cp_token_cache *
774 cp_token_cache_new (cp_token *first, cp_token *last)
776 cp_token_cache *cache = GGC_NEW (cp_token_cache);
777 cache->first = first;
783 /* Decl-specifiers. */
785 static void clear_decl_specs
786 (cp_decl_specifier_seq *);
788 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
791 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
793 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
798 /* Nothing other than the parser should be creating declarators;
799 declarators are a semi-syntactic representation of C++ entities.
800 Other parts of the front end that need to create entities (like
801 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
803 static cp_declarator *make_id_declarator
805 static cp_declarator *make_call_declarator
806 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
807 static cp_declarator *make_array_declarator
808 (cp_declarator *, tree);
809 static cp_declarator *make_pointer_declarator
810 (cp_cv_quals, cp_declarator *);
811 static cp_declarator *make_reference_declarator
812 (cp_cv_quals, cp_declarator *);
813 static cp_parameter_declarator *make_parameter_declarator
814 (cp_decl_specifier_seq *, cp_declarator *, tree);
815 static cp_declarator *make_ptrmem_declarator
816 (cp_cv_quals, tree, cp_declarator *);
818 cp_declarator *cp_error_declarator;
820 /* The obstack on which declarators and related data structures are
822 static struct obstack declarator_obstack;
824 /* Alloc BYTES from the declarator memory pool. */
827 alloc_declarator (size_t bytes)
829 return obstack_alloc (&declarator_obstack, bytes);
832 /* Allocate a declarator of the indicated KIND. Clear fields that are
833 common to all declarators. */
835 static cp_declarator *
836 make_declarator (cp_declarator_kind kind)
838 cp_declarator *declarator;
840 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
841 declarator->kind = kind;
842 declarator->attributes = NULL_TREE;
843 declarator->declarator = NULL;
848 /* Make a declarator for a generalized identifier. */
851 make_id_declarator (tree id)
853 cp_declarator *declarator;
855 declarator = make_declarator (cdk_id);
856 declarator->u.id.name = id;
857 declarator->u.id.sfk = sfk_none;
862 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
863 of modifiers such as const or volatile to apply to the pointer
864 type, represented as identifiers. */
867 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
869 cp_declarator *declarator;
871 declarator = make_declarator (cdk_pointer);
872 declarator->declarator = target;
873 declarator->u.pointer.qualifiers = cv_qualifiers;
874 declarator->u.pointer.class_type = NULL_TREE;
879 /* Like make_pointer_declarator -- but for references. */
882 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
884 cp_declarator *declarator;
886 declarator = make_declarator (cdk_reference);
887 declarator->declarator = target;
888 declarator->u.pointer.qualifiers = cv_qualifiers;
889 declarator->u.pointer.class_type = NULL_TREE;
894 /* Like make_pointer_declarator -- but for a pointer to a non-static
895 member of CLASS_TYPE. */
898 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
899 cp_declarator *pointee)
901 cp_declarator *declarator;
903 declarator = make_declarator (cdk_ptrmem);
904 declarator->declarator = pointee;
905 declarator->u.pointer.qualifiers = cv_qualifiers;
906 declarator->u.pointer.class_type = class_type;
911 /* Make a declarator for the function given by TARGET, with the
912 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
913 "const"-qualified member function. The EXCEPTION_SPECIFICATION
914 indicates what exceptions can be thrown. */
917 make_call_declarator (cp_declarator *target,
918 cp_parameter_declarator *parms,
919 cp_cv_quals cv_qualifiers,
920 tree exception_specification)
922 cp_declarator *declarator;
924 declarator = make_declarator (cdk_function);
925 declarator->declarator = target;
926 declarator->u.function.parameters = parms;
927 declarator->u.function.qualifiers = cv_qualifiers;
928 declarator->u.function.exception_specification = exception_specification;
933 /* Make a declarator for an array of BOUNDS elements, each of which is
934 defined by ELEMENT. */
937 make_array_declarator (cp_declarator *element, tree bounds)
939 cp_declarator *declarator;
941 declarator = make_declarator (cdk_array);
942 declarator->declarator = element;
943 declarator->u.array.bounds = bounds;
948 cp_parameter_declarator *no_parameters;
950 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
951 DECLARATOR and DEFAULT_ARGUMENT. */
953 cp_parameter_declarator *
954 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
955 cp_declarator *declarator,
956 tree default_argument)
958 cp_parameter_declarator *parameter;
960 parameter = ((cp_parameter_declarator *)
961 alloc_declarator (sizeof (cp_parameter_declarator)));
962 parameter->next = NULL;
964 parameter->decl_specifiers = *decl_specifiers;
966 clear_decl_specs (¶meter->decl_specifiers);
967 parameter->declarator = declarator;
968 parameter->default_argument = default_argument;
969 parameter->ellipsis_p = false;
979 A cp_parser parses the token stream as specified by the C++
980 grammar. Its job is purely parsing, not semantic analysis. For
981 example, the parser breaks the token stream into declarators,
982 expressions, statements, and other similar syntactic constructs.
983 It does not check that the types of the expressions on either side
984 of an assignment-statement are compatible, or that a function is
985 not declared with a parameter of type `void'.
987 The parser invokes routines elsewhere in the compiler to perform
988 semantic analysis and to build up the abstract syntax tree for the
991 The parser (and the template instantiation code, which is, in a
992 way, a close relative of parsing) are the only parts of the
993 compiler that should be calling push_scope and pop_scope, or
994 related functions. The parser (and template instantiation code)
995 keeps track of what scope is presently active; everything else
996 should simply honor that. (The code that generates static
997 initializers may also need to set the scope, in order to check
998 access control correctly when emitting the initializers.)
1003 The parser is of the standard recursive-descent variety. Upcoming
1004 tokens in the token stream are examined in order to determine which
1005 production to use when parsing a non-terminal. Some C++ constructs
1006 require arbitrary look ahead to disambiguate. For example, it is
1007 impossible, in the general case, to tell whether a statement is an
1008 expression or declaration without scanning the entire statement.
1009 Therefore, the parser is capable of "parsing tentatively." When the
1010 parser is not sure what construct comes next, it enters this mode.
1011 Then, while we attempt to parse the construct, the parser queues up
1012 error messages, rather than issuing them immediately, and saves the
1013 tokens it consumes. If the construct is parsed successfully, the
1014 parser "commits", i.e., it issues any queued error messages and
1015 the tokens that were being preserved are permanently discarded.
1016 If, however, the construct is not parsed successfully, the parser
1017 rolls back its state completely so that it can resume parsing using
1018 a different alternative.
1023 The performance of the parser could probably be improved substantially.
1024 We could often eliminate the need to parse tentatively by looking ahead
1025 a little bit. In some places, this approach might not entirely eliminate
1026 the need to parse tentatively, but it might still speed up the average
1029 /* Flags that are passed to some parsing functions. These values can
1030 be bitwise-ored together. */
1032 typedef enum cp_parser_flags
1035 CP_PARSER_FLAGS_NONE = 0x0,
1036 /* The construct is optional. If it is not present, then no error
1037 should be issued. */
1038 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1039 /* When parsing a type-specifier, do not allow user-defined types. */
1040 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1043 /* The different kinds of declarators we want to parse. */
1045 typedef enum cp_parser_declarator_kind
1047 /* We want an abstract declarator. */
1048 CP_PARSER_DECLARATOR_ABSTRACT,
1049 /* We want a named declarator. */
1050 CP_PARSER_DECLARATOR_NAMED,
1051 /* We don't mind, but the name must be an unqualified-id. */
1052 CP_PARSER_DECLARATOR_EITHER
1053 } cp_parser_declarator_kind;
1055 /* The precedence values used to parse binary expressions. The minimum value
1056 of PREC must be 1, because zero is reserved to quickly discriminate
1057 binary operators from other tokens. */
1062 PREC_LOGICAL_OR_EXPRESSION,
1063 PREC_LOGICAL_AND_EXPRESSION,
1064 PREC_INCLUSIVE_OR_EXPRESSION,
1065 PREC_EXCLUSIVE_OR_EXPRESSION,
1066 PREC_AND_EXPRESSION,
1067 PREC_RELATIONAL_EXPRESSION,
1068 PREC_EQUALITY_EXPRESSION,
1069 PREC_SHIFT_EXPRESSION,
1070 PREC_ADDITIVE_EXPRESSION,
1071 PREC_MULTIPLICATIVE_EXPRESSION,
1073 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1076 /* A mapping from a token type to a corresponding tree node type, with a
1077 precedence value. */
1079 typedef struct cp_parser_binary_operations_map_node
1081 /* The token type. */
1082 enum cpp_ttype token_type;
1083 /* The corresponding tree code. */
1084 enum tree_code tree_type;
1085 /* The precedence of this operator. */
1086 enum cp_parser_prec prec;
1087 } cp_parser_binary_operations_map_node;
1089 /* The status of a tentative parse. */
1091 typedef enum cp_parser_status_kind
1093 /* No errors have occurred. */
1094 CP_PARSER_STATUS_KIND_NO_ERROR,
1095 /* An error has occurred. */
1096 CP_PARSER_STATUS_KIND_ERROR,
1097 /* We are committed to this tentative parse, whether or not an error
1099 CP_PARSER_STATUS_KIND_COMMITTED
1100 } cp_parser_status_kind;
1102 typedef struct cp_parser_expression_stack_entry
1105 enum tree_code tree_type;
1107 } cp_parser_expression_stack_entry;
1109 typedef struct cp_parser_expression_stack_entry
1110 cp_parser_expression_stack[NUM_PREC_VALUES];
1112 /* Context that is saved and restored when parsing tentatively. */
1113 typedef struct cp_parser_context GTY (())
1115 /* If this is a tentative parsing context, the status of the
1117 enum cp_parser_status_kind status;
1118 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1119 that are looked up in this context must be looked up both in the
1120 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1121 the context of the containing expression. */
1124 /* The next parsing context in the stack. */
1125 struct cp_parser_context *next;
1126 } cp_parser_context;
1130 /* Constructors and destructors. */
1132 static cp_parser_context *cp_parser_context_new
1133 (cp_parser_context *);
1135 /* Class variables. */
1137 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1139 /* The operator-precedence table used by cp_parser_binary_expression.
1140 Transformed into an associative array (binops_by_token) by
1143 static const cp_parser_binary_operations_map_node binops[] = {
1144 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1145 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1147 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1148 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1149 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1151 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1152 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1154 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1155 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1157 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1158 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1159 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1160 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1161 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1162 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1164 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1165 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1167 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1169 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1171 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1173 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1175 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1178 /* The same as binops, but initialized by cp_parser_new so that
1179 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1181 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1183 /* Constructors and destructors. */
1185 /* Construct a new context. The context below this one on the stack
1186 is given by NEXT. */
1188 static cp_parser_context *
1189 cp_parser_context_new (cp_parser_context* next)
1191 cp_parser_context *context;
1193 /* Allocate the storage. */
1194 if (cp_parser_context_free_list != NULL)
1196 /* Pull the first entry from the free list. */
1197 context = cp_parser_context_free_list;
1198 cp_parser_context_free_list = context->next;
1199 memset (context, 0, sizeof (*context));
1202 context = GGC_CNEW (cp_parser_context);
1204 /* No errors have occurred yet in this context. */
1205 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1206 /* If this is not the bottomost context, copy information that we
1207 need from the previous context. */
1210 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1211 expression, then we are parsing one in this context, too. */
1212 context->object_type = next->object_type;
1213 /* Thread the stack. */
1214 context->next = next;
1220 /* The cp_parser structure represents the C++ parser. */
1222 typedef struct cp_parser GTY(())
1224 /* The lexer from which we are obtaining tokens. */
1227 /* The scope in which names should be looked up. If NULL_TREE, then
1228 we look up names in the scope that is currently open in the
1229 source program. If non-NULL, this is either a TYPE or
1230 NAMESPACE_DECL for the scope in which we should look.
1232 This value is not cleared automatically after a name is looked
1233 up, so we must be careful to clear it before starting a new look
1234 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1235 will look up `Z' in the scope of `X', rather than the current
1236 scope.) Unfortunately, it is difficult to tell when name lookup
1237 is complete, because we sometimes peek at a token, look it up,
1238 and then decide not to consume it. */
1241 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1242 last lookup took place. OBJECT_SCOPE is used if an expression
1243 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1244 respectively. QUALIFYING_SCOPE is used for an expression of the
1245 form "X::Y"; it refers to X. */
1247 tree qualifying_scope;
1249 /* A stack of parsing contexts. All but the bottom entry on the
1250 stack will be tentative contexts.
1252 We parse tentatively in order to determine which construct is in
1253 use in some situations. For example, in order to determine
1254 whether a statement is an expression-statement or a
1255 declaration-statement we parse it tentatively as a
1256 declaration-statement. If that fails, we then reparse the same
1257 token stream as an expression-statement. */
1258 cp_parser_context *context;
1260 /* True if we are parsing GNU C++. If this flag is not set, then
1261 GNU extensions are not recognized. */
1262 bool allow_gnu_extensions_p;
1264 /* TRUE if the `>' token should be interpreted as the greater-than
1265 operator. FALSE if it is the end of a template-id or
1266 template-parameter-list. */
1267 bool greater_than_is_operator_p;
1269 /* TRUE if default arguments are allowed within a parameter list
1270 that starts at this point. FALSE if only a gnu extension makes
1271 them permissible. */
1272 bool default_arg_ok_p;
1274 /* TRUE if we are parsing an integral constant-expression. See
1275 [expr.const] for a precise definition. */
1276 bool integral_constant_expression_p;
1278 /* TRUE if we are parsing an integral constant-expression -- but a
1279 non-constant expression should be permitted as well. This flag
1280 is used when parsing an array bound so that GNU variable-length
1281 arrays are tolerated. */
1282 bool allow_non_integral_constant_expression_p;
1284 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1285 been seen that makes the expression non-constant. */
1286 bool non_integral_constant_expression_p;
1288 /* TRUE if local variable names and `this' are forbidden in the
1290 bool local_variables_forbidden_p;
1292 /* TRUE if the declaration we are parsing is part of a
1293 linkage-specification of the form `extern string-literal
1295 bool in_unbraced_linkage_specification_p;
1297 /* TRUE if we are presently parsing a declarator, after the
1298 direct-declarator. */
1299 bool in_declarator_p;
1301 /* TRUE if we are presently parsing a template-argument-list. */
1302 bool in_template_argument_list_p;
1304 /* TRUE if we are presently parsing the body of an
1305 iteration-statement. */
1306 bool in_iteration_statement_p;
1308 /* TRUE if we are presently parsing the body of a switch
1310 bool in_switch_statement_p;
1312 /* TRUE if we are parsing a type-id in an expression context. In
1313 such a situation, both "type (expr)" and "type (type)" are valid
1315 bool in_type_id_in_expr_p;
1317 /* TRUE if strings in expressions should be translated to the execution
1319 bool translate_strings_p;
1321 /* If non-NULL, then we are parsing a construct where new type
1322 definitions are not permitted. The string stored here will be
1323 issued as an error message if a type is defined. */
1324 const char *type_definition_forbidden_message;
1326 /* A list of lists. The outer list is a stack, used for member
1327 functions of local classes. At each level there are two sub-list,
1328 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1329 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1330 TREE_VALUE's. The functions are chained in reverse declaration
1333 The TREE_PURPOSE sublist contains those functions with default
1334 arguments that need post processing, and the TREE_VALUE sublist
1335 contains those functions with definitions that need post
1338 These lists can only be processed once the outermost class being
1339 defined is complete. */
1340 tree unparsed_functions_queues;
1342 /* The number of classes whose definitions are currently in
1344 unsigned num_classes_being_defined;
1346 /* The number of template parameter lists that apply directly to the
1347 current declaration. */
1348 unsigned num_template_parameter_lists;
1351 /* The type of a function that parses some kind of expression. */
1352 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1356 /* Constructors and destructors. */
1358 static cp_parser *cp_parser_new
1361 /* Routines to parse various constructs.
1363 Those that return `tree' will return the error_mark_node (rather
1364 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1365 Sometimes, they will return an ordinary node if error-recovery was
1366 attempted, even though a parse error occurred. So, to check
1367 whether or not a parse error occurred, you should always use
1368 cp_parser_error_occurred. If the construct is optional (indicated
1369 either by an `_opt' in the name of the function that does the
1370 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1371 the construct is not present. */
1373 /* Lexical conventions [gram.lex] */
1375 static tree cp_parser_identifier
1377 static tree cp_parser_string_literal
1378 (cp_parser *, bool, bool);
1380 /* Basic concepts [gram.basic] */
1382 static bool cp_parser_translation_unit
1385 /* Expressions [gram.expr] */
1387 static tree cp_parser_primary_expression
1388 (cp_parser *, cp_id_kind *, tree *);
1389 static tree cp_parser_id_expression
1390 (cp_parser *, bool, bool, bool *, bool);
1391 static tree cp_parser_unqualified_id
1392 (cp_parser *, bool, bool, bool);
1393 static tree cp_parser_nested_name_specifier_opt
1394 (cp_parser *, bool, bool, bool, bool);
1395 static tree cp_parser_nested_name_specifier
1396 (cp_parser *, bool, bool, bool, bool);
1397 static tree cp_parser_class_or_namespace_name
1398 (cp_parser *, bool, bool, bool, bool, bool);
1399 static tree cp_parser_postfix_expression
1400 (cp_parser *, bool);
1401 static tree cp_parser_postfix_open_square_expression
1402 (cp_parser *, tree, bool);
1403 static tree cp_parser_postfix_dot_deref_expression
1404 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1405 static tree cp_parser_parenthesized_expression_list
1406 (cp_parser *, bool, bool *);
1407 static void cp_parser_pseudo_destructor_name
1408 (cp_parser *, tree *, tree *);
1409 static tree cp_parser_unary_expression
1410 (cp_parser *, bool);
1411 static enum tree_code cp_parser_unary_operator
1413 static tree cp_parser_new_expression
1415 static tree cp_parser_new_placement
1417 static tree cp_parser_new_type_id
1418 (cp_parser *, tree *);
1419 static cp_declarator *cp_parser_new_declarator_opt
1421 static cp_declarator *cp_parser_direct_new_declarator
1423 static tree cp_parser_new_initializer
1425 static tree cp_parser_delete_expression
1427 static tree cp_parser_cast_expression
1428 (cp_parser *, bool);
1429 static tree cp_parser_binary_expression
1431 static tree cp_parser_question_colon_clause
1432 (cp_parser *, tree);
1433 static tree cp_parser_assignment_expression
1435 static enum tree_code cp_parser_assignment_operator_opt
1437 static tree cp_parser_expression
1439 static tree cp_parser_constant_expression
1440 (cp_parser *, bool, bool *);
1441 static tree cp_parser_builtin_offsetof
1444 /* Statements [gram.stmt.stmt] */
1446 static void cp_parser_statement
1447 (cp_parser *, tree);
1448 static tree cp_parser_labeled_statement
1449 (cp_parser *, tree);
1450 static tree cp_parser_expression_statement
1451 (cp_parser *, tree);
1452 static tree cp_parser_compound_statement
1453 (cp_parser *, tree, bool);
1454 static void cp_parser_statement_seq_opt
1455 (cp_parser *, tree);
1456 static tree cp_parser_selection_statement
1458 static tree cp_parser_condition
1460 static tree cp_parser_iteration_statement
1462 static void cp_parser_for_init_statement
1464 static tree cp_parser_jump_statement
1466 static void cp_parser_declaration_statement
1469 static tree cp_parser_implicitly_scoped_statement
1471 static void cp_parser_already_scoped_statement
1474 /* Declarations [gram.dcl.dcl] */
1476 static void cp_parser_declaration_seq_opt
1478 static void cp_parser_declaration
1480 static void cp_parser_block_declaration
1481 (cp_parser *, bool);
1482 static void cp_parser_simple_declaration
1483 (cp_parser *, bool);
1484 static void cp_parser_decl_specifier_seq
1485 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1486 static tree cp_parser_storage_class_specifier_opt
1488 static tree cp_parser_function_specifier_opt
1489 (cp_parser *, cp_decl_specifier_seq *);
1490 static tree cp_parser_type_specifier
1491 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1493 static tree cp_parser_simple_type_specifier
1494 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1495 static tree cp_parser_type_name
1497 static tree cp_parser_elaborated_type_specifier
1498 (cp_parser *, bool, bool);
1499 static tree cp_parser_enum_specifier
1501 static void cp_parser_enumerator_list
1502 (cp_parser *, tree);
1503 static void cp_parser_enumerator_definition
1504 (cp_parser *, tree);
1505 static tree cp_parser_namespace_name
1507 static void cp_parser_namespace_definition
1509 static void cp_parser_namespace_body
1511 static tree cp_parser_qualified_namespace_specifier
1513 static void cp_parser_namespace_alias_definition
1515 static void cp_parser_using_declaration
1517 static void cp_parser_using_directive
1519 static void cp_parser_asm_definition
1521 static void cp_parser_linkage_specification
1524 /* Declarators [gram.dcl.decl] */
1526 static tree cp_parser_init_declarator
1527 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1528 static cp_declarator *cp_parser_declarator
1529 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1530 static cp_declarator *cp_parser_direct_declarator
1531 (cp_parser *, cp_parser_declarator_kind, int *);
1532 static enum tree_code cp_parser_ptr_operator
1533 (cp_parser *, tree *, cp_cv_quals *);
1534 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1536 static tree cp_parser_declarator_id
1538 static tree cp_parser_type_id
1540 static void cp_parser_type_specifier_seq
1541 (cp_parser *, cp_decl_specifier_seq *);
1542 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1544 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1545 (cp_parser *, bool *);
1546 static cp_parameter_declarator *cp_parser_parameter_declaration
1547 (cp_parser *, bool, bool *);
1548 static void cp_parser_function_body
1550 static tree cp_parser_initializer
1551 (cp_parser *, bool *, bool *);
1552 static tree cp_parser_initializer_clause
1553 (cp_parser *, bool *);
1554 static tree cp_parser_initializer_list
1555 (cp_parser *, bool *);
1557 static bool cp_parser_ctor_initializer_opt_and_function_body
1560 /* Classes [gram.class] */
1562 static tree cp_parser_class_name
1563 (cp_parser *, bool, bool, bool, bool, bool, bool);
1564 static tree cp_parser_class_specifier
1566 static tree cp_parser_class_head
1567 (cp_parser *, bool *, tree *);
1568 static enum tag_types cp_parser_class_key
1570 static void cp_parser_member_specification_opt
1572 static void cp_parser_member_declaration
1574 static tree cp_parser_pure_specifier
1576 static tree cp_parser_constant_initializer
1579 /* Derived classes [gram.class.derived] */
1581 static tree cp_parser_base_clause
1583 static tree cp_parser_base_specifier
1586 /* Special member functions [gram.special] */
1588 static tree cp_parser_conversion_function_id
1590 static tree cp_parser_conversion_type_id
1592 static cp_declarator *cp_parser_conversion_declarator_opt
1594 static bool cp_parser_ctor_initializer_opt
1596 static void cp_parser_mem_initializer_list
1598 static tree cp_parser_mem_initializer
1600 static tree cp_parser_mem_initializer_id
1603 /* Overloading [gram.over] */
1605 static tree cp_parser_operator_function_id
1607 static tree cp_parser_operator
1610 /* Templates [gram.temp] */
1612 static void cp_parser_template_declaration
1613 (cp_parser *, bool);
1614 static tree cp_parser_template_parameter_list
1616 static tree cp_parser_template_parameter
1617 (cp_parser *, bool *);
1618 static tree cp_parser_type_parameter
1620 static tree cp_parser_template_id
1621 (cp_parser *, bool, bool, bool);
1622 static tree cp_parser_template_name
1623 (cp_parser *, bool, bool, bool, bool *);
1624 static tree cp_parser_template_argument_list
1626 static tree cp_parser_template_argument
1628 static void cp_parser_explicit_instantiation
1630 static void cp_parser_explicit_specialization
1633 /* Exception handling [gram.exception] */
1635 static tree cp_parser_try_block
1637 static bool cp_parser_function_try_block
1639 static void cp_parser_handler_seq
1641 static void cp_parser_handler
1643 static tree cp_parser_exception_declaration
1645 static tree cp_parser_throw_expression
1647 static tree cp_parser_exception_specification_opt
1649 static tree cp_parser_type_id_list
1652 /* GNU Extensions */
1654 static tree cp_parser_asm_specification_opt
1656 static tree cp_parser_asm_operand_list
1658 static tree cp_parser_asm_clobber_list
1660 static tree cp_parser_attributes_opt
1662 static tree cp_parser_attribute_list
1664 static bool cp_parser_extension_opt
1665 (cp_parser *, int *);
1666 static void cp_parser_label_declaration
1669 /* Utility Routines */
1671 static tree cp_parser_lookup_name
1672 (cp_parser *, tree, bool, bool, bool, bool, bool *);
1673 static tree cp_parser_lookup_name_simple
1674 (cp_parser *, tree);
1675 static tree cp_parser_maybe_treat_template_as_class
1677 static bool cp_parser_check_declarator_template_parameters
1678 (cp_parser *, cp_declarator *);
1679 static bool cp_parser_check_template_parameters
1680 (cp_parser *, unsigned);
1681 static tree cp_parser_simple_cast_expression
1683 static tree cp_parser_global_scope_opt
1684 (cp_parser *, bool);
1685 static bool cp_parser_constructor_declarator_p
1686 (cp_parser *, bool);
1687 static tree cp_parser_function_definition_from_specifiers_and_declarator
1688 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1689 static tree cp_parser_function_definition_after_declarator
1690 (cp_parser *, bool);
1691 static void cp_parser_template_declaration_after_export
1692 (cp_parser *, bool);
1693 static tree cp_parser_single_declaration
1694 (cp_parser *, bool, bool *);
1695 static tree cp_parser_functional_cast
1696 (cp_parser *, tree);
1697 static tree cp_parser_save_member_function_body
1698 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1699 static tree cp_parser_enclosed_template_argument_list
1701 static void cp_parser_save_default_args
1702 (cp_parser *, tree);
1703 static void cp_parser_late_parsing_for_member
1704 (cp_parser *, tree);
1705 static void cp_parser_late_parsing_default_args
1706 (cp_parser *, tree);
1707 static tree cp_parser_sizeof_operand
1708 (cp_parser *, enum rid);
1709 static bool cp_parser_declares_only_class_p
1711 static void cp_parser_set_storage_class
1712 (cp_decl_specifier_seq *, cp_storage_class);
1713 static void cp_parser_set_decl_spec_type
1714 (cp_decl_specifier_seq *, tree, bool);
1715 static bool cp_parser_friend_p
1716 (const cp_decl_specifier_seq *);
1717 static cp_token *cp_parser_require
1718 (cp_parser *, enum cpp_ttype, const char *);
1719 static cp_token *cp_parser_require_keyword
1720 (cp_parser *, enum rid, const char *);
1721 static bool cp_parser_token_starts_function_definition_p
1723 static bool cp_parser_next_token_starts_class_definition_p
1725 static bool cp_parser_next_token_ends_template_argument_p
1727 static bool cp_parser_nth_token_starts_template_argument_list_p
1728 (cp_parser *, size_t);
1729 static enum tag_types cp_parser_token_is_class_key
1731 static void cp_parser_check_class_key
1732 (enum tag_types, tree type);
1733 static void cp_parser_check_access_in_redeclaration
1735 static bool cp_parser_optional_template_keyword
1737 static void cp_parser_pre_parsed_nested_name_specifier
1739 static void cp_parser_cache_group
1740 (cp_parser *, enum cpp_ttype, unsigned);
1741 static void cp_parser_parse_tentatively
1743 static void cp_parser_commit_to_tentative_parse
1745 static void cp_parser_abort_tentative_parse
1747 static bool cp_parser_parse_definitely
1749 static inline bool cp_parser_parsing_tentatively
1751 static bool cp_parser_committed_to_tentative_parse
1753 static void cp_parser_error
1754 (cp_parser *, const char *);
1755 static void cp_parser_name_lookup_error
1756 (cp_parser *, tree, tree, const char *);
1757 static bool cp_parser_simulate_error
1759 static void cp_parser_check_type_definition
1761 static void cp_parser_check_for_definition_in_return_type
1762 (cp_declarator *, int);
1763 static void cp_parser_check_for_invalid_template_id
1764 (cp_parser *, tree);
1765 static bool cp_parser_non_integral_constant_expression
1766 (cp_parser *, const char *);
1767 static void cp_parser_diagnose_invalid_type_name
1768 (cp_parser *, tree, tree);
1769 static bool cp_parser_parse_and_diagnose_invalid_type_name
1771 static int cp_parser_skip_to_closing_parenthesis
1772 (cp_parser *, bool, bool, bool);
1773 static void cp_parser_skip_to_end_of_statement
1775 static void cp_parser_consume_semicolon_at_end_of_statement
1777 static void cp_parser_skip_to_end_of_block_or_statement
1779 static void cp_parser_skip_to_closing_brace
1781 static void cp_parser_skip_until_found
1782 (cp_parser *, enum cpp_ttype, const char *);
1783 static bool cp_parser_error_occurred
1785 static bool cp_parser_allow_gnu_extensions_p
1787 static bool cp_parser_is_string_literal
1789 static bool cp_parser_is_keyword
1790 (cp_token *, enum rid);
1791 static tree cp_parser_make_typename_type
1792 (cp_parser *, tree, tree);
1794 /* Returns nonzero if we are parsing tentatively. */
1797 cp_parser_parsing_tentatively (cp_parser* parser)
1799 return parser->context->next != NULL;
1802 /* Returns nonzero if TOKEN is a string literal. */
1805 cp_parser_is_string_literal (cp_token* token)
1807 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1810 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1813 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1815 return token->keyword == keyword;
1818 /* If not parsing tentatively, issue a diagnostic of the form
1819 FILE:LINE: MESSAGE before TOKEN
1820 where TOKEN is the next token in the input stream. MESSAGE
1821 (specified by the caller) is usually of the form "expected
1825 cp_parser_error (cp_parser* parser, const char* message)
1827 if (!cp_parser_simulate_error (parser))
1829 cp_token *token = cp_lexer_peek_token (parser->lexer);
1830 /* This diagnostic makes more sense if it is tagged to the line
1831 of the token we just peeked at. */
1832 cp_lexer_set_source_position_from_token (token);
1833 c_parse_error (message,
1834 /* Because c_parser_error does not understand
1835 CPP_KEYWORD, keywords are treated like
1837 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1842 /* Issue an error about name-lookup failing. NAME is the
1843 IDENTIFIER_NODE DECL is the result of
1844 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1845 the thing that we hoped to find. */
1848 cp_parser_name_lookup_error (cp_parser* parser,
1851 const char* desired)
1853 /* If name lookup completely failed, tell the user that NAME was not
1855 if (decl == error_mark_node)
1857 if (parser->scope && parser->scope != global_namespace)
1858 error ("`%D::%D' has not been declared",
1859 parser->scope, name);
1860 else if (parser->scope == global_namespace)
1861 error ("`::%D' has not been declared", name);
1862 else if (parser->object_scope
1863 && !CLASS_TYPE_P (parser->object_scope))
1864 error ("request for member `%D' in non-class type `%T'",
1865 name, parser->object_scope);
1866 else if (parser->object_scope)
1867 error ("`%T::%D' has not been declared",
1868 parser->object_scope, name);
1870 error ("`%D' has not been declared", name);
1872 else if (parser->scope && parser->scope != global_namespace)
1873 error ("`%D::%D' %s", parser->scope, name, desired);
1874 else if (parser->scope == global_namespace)
1875 error ("`::%D' %s", name, desired);
1877 error ("`%D' %s", name, desired);
1880 /* If we are parsing tentatively, remember that an error has occurred
1881 during this tentative parse. Returns true if the error was
1882 simulated; false if a message should be issued by the caller. */
1885 cp_parser_simulate_error (cp_parser* parser)
1887 if (cp_parser_parsing_tentatively (parser)
1888 && !cp_parser_committed_to_tentative_parse (parser))
1890 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1896 /* This function is called when a type is defined. If type
1897 definitions are forbidden at this point, an error message is
1901 cp_parser_check_type_definition (cp_parser* parser)
1903 /* If types are forbidden here, issue a message. */
1904 if (parser->type_definition_forbidden_message)
1905 /* Use `%s' to print the string in case there are any escape
1906 characters in the message. */
1907 error ("%s", parser->type_definition_forbidden_message);
1910 /* This function is called when a declaration is parsed. If
1911 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1912 indicates that a type was defined in the decl-specifiers for DECL,
1913 then an error is issued. */
1916 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1917 int declares_class_or_enum)
1919 /* [dcl.fct] forbids type definitions in return types.
1920 Unfortunately, it's not easy to know whether or not we are
1921 processing a return type until after the fact. */
1923 && (declarator->kind == cdk_pointer
1924 || declarator->kind == cdk_reference
1925 || declarator->kind == cdk_ptrmem))
1926 declarator = declarator->declarator;
1928 && declarator->kind == cdk_function
1929 && declares_class_or_enum & 2)
1930 error ("new types may not be defined in a return type");
1933 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1934 "<" in any valid C++ program. If the next token is indeed "<",
1935 issue a message warning the user about what appears to be an
1936 invalid attempt to form a template-id. */
1939 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1945 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1948 error ("`%T' is not a template", type);
1949 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1950 error ("`%E' is not a template", type);
1952 error ("invalid template-id");
1953 /* Remember the location of the invalid "<". */
1954 if (cp_parser_parsing_tentatively (parser)
1955 && !cp_parser_committed_to_tentative_parse (parser))
1957 token = cp_lexer_peek_token (parser->lexer);
1958 token = cp_lexer_prev_token (parser->lexer, token);
1959 start = cp_lexer_token_difference (parser->lexer,
1960 parser->lexer->buffer,
1965 /* Consume the "<". */
1966 cp_lexer_consume_token (parser->lexer);
1967 /* Parse the template arguments. */
1968 cp_parser_enclosed_template_argument_list (parser);
1969 /* Permanently remove the invalid template arguments so that
1970 this error message is not issued again. */
1973 token = cp_lexer_advance_token (parser->lexer,
1974 parser->lexer->buffer,
1976 cp_lexer_purge_tokens_after (parser->lexer, token);
1981 /* If parsing an integral constant-expression, issue an error message
1982 about the fact that THING appeared and return true. Otherwise,
1983 return false, marking the current expression as non-constant. */
1986 cp_parser_non_integral_constant_expression (cp_parser *parser,
1989 if (parser->integral_constant_expression_p)
1991 if (!parser->allow_non_integral_constant_expression_p)
1993 error ("%s cannot appear in a constant-expression", thing);
1996 parser->non_integral_constant_expression_p = true;
2001 /* Emit a diagnostic for an invalid type name. Consider also if it is
2002 qualified or not and the result of a lookup, to provide a better
2006 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2008 tree decl, old_scope;
2009 /* Try to lookup the identifier. */
2010 old_scope = parser->scope;
2011 parser->scope = scope;
2012 decl = cp_parser_lookup_name_simple (parser, id);
2013 parser->scope = old_scope;
2014 /* If the lookup found a template-name, it means that the user forgot
2015 to specify an argument list. Emit an useful error message. */
2016 if (TREE_CODE (decl) == TEMPLATE_DECL)
2017 error ("invalid use of template-name `%E' without an argument list",
2019 else if (!parser->scope)
2021 /* Issue an error message. */
2022 error ("`%E' does not name a type", id);
2023 /* If we're in a template class, it's possible that the user was
2024 referring to a type from a base class. For example:
2026 template <typename T> struct A { typedef T X; };
2027 template <typename T> struct B : public A<T> { X x; };
2029 The user should have said "typename A<T>::X". */
2030 if (processing_template_decl && current_class_type)
2034 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2038 tree base_type = BINFO_TYPE (b);
2039 if (CLASS_TYPE_P (base_type)
2040 && dependent_type_p (base_type))
2043 /* Go from a particular instantiation of the
2044 template (which will have an empty TYPE_FIELDs),
2045 to the main version. */
2046 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2047 for (field = TYPE_FIELDS (base_type);
2049 field = TREE_CHAIN (field))
2050 if (TREE_CODE (field) == TYPE_DECL
2051 && DECL_NAME (field) == id)
2053 inform ("(perhaps `typename %T::%E' was intended)",
2054 BINFO_TYPE (b), id);
2063 /* Here we diagnose qualified-ids where the scope is actually correct,
2064 but the identifier does not resolve to a valid type name. */
2067 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2068 error ("`%E' in namespace `%E' does not name a type",
2070 else if (TYPE_P (parser->scope))
2071 error ("`%E' in class `%T' does not name a type",
2078 /* Check for a common situation where a type-name should be present,
2079 but is not, and issue a sensible error message. Returns true if an
2080 invalid type-name was detected.
2082 The situation handled by this function are variable declarations of the
2083 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2084 Usually, `ID' should name a type, but if we got here it means that it
2085 does not. We try to emit the best possible error message depending on
2086 how exactly the id-expression looks like.
2090 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2094 cp_parser_parse_tentatively (parser);
2095 id = cp_parser_id_expression (parser,
2096 /*template_keyword_p=*/false,
2097 /*check_dependency_p=*/true,
2098 /*template_p=*/NULL,
2099 /*declarator_p=*/true);
2100 /* After the id-expression, there should be a plain identifier,
2101 otherwise this is not a simple variable declaration. Also, if
2102 the scope is dependent, we cannot do much. */
2103 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2104 || (parser->scope && TYPE_P (parser->scope)
2105 && dependent_type_p (parser->scope)))
2107 cp_parser_abort_tentative_parse (parser);
2110 if (!cp_parser_parse_definitely (parser)
2111 || TREE_CODE (id) != IDENTIFIER_NODE)
2114 /* Emit a diagnostic for the invalid type. */
2115 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2116 /* Skip to the end of the declaration; there's no point in
2117 trying to process it. */
2118 cp_parser_skip_to_end_of_block_or_statement (parser);
2122 /* Consume tokens up to, and including, the next non-nested closing `)'.
2123 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2124 are doing error recovery. Returns -1 if OR_COMMA is true and we
2125 found an unnested comma. */
2128 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2133 unsigned paren_depth = 0;
2134 unsigned brace_depth = 0;
2137 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2138 && !cp_parser_committed_to_tentative_parse (parser))
2145 /* If we've run out of tokens, then there is no closing `)'. */
2146 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2152 token = cp_lexer_peek_token (parser->lexer);
2154 /* This matches the processing in skip_to_end_of_statement. */
2155 if (token->type == CPP_SEMICOLON && !brace_depth)
2160 if (token->type == CPP_OPEN_BRACE)
2162 if (token->type == CPP_CLOSE_BRACE)
2170 if (recovering && or_comma && token->type == CPP_COMMA
2171 && !brace_depth && !paren_depth)
2179 /* If it is an `(', we have entered another level of nesting. */
2180 if (token->type == CPP_OPEN_PAREN)
2182 /* If it is a `)', then we might be done. */
2183 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2186 cp_lexer_consume_token (parser->lexer);
2194 /* Consume the token. */
2195 cp_lexer_consume_token (parser->lexer);
2201 /* Consume tokens until we reach the end of the current statement.
2202 Normally, that will be just before consuming a `;'. However, if a
2203 non-nested `}' comes first, then we stop before consuming that. */
2206 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2208 unsigned nesting_depth = 0;
2214 /* Peek at the next token. */
2215 token = cp_lexer_peek_token (parser->lexer);
2216 /* If we've run out of tokens, stop. */
2217 if (token->type == CPP_EOF)
2219 /* If the next token is a `;', we have reached the end of the
2221 if (token->type == CPP_SEMICOLON && !nesting_depth)
2223 /* If the next token is a non-nested `}', then we have reached
2224 the end of the current block. */
2225 if (token->type == CPP_CLOSE_BRACE)
2227 /* If this is a non-nested `}', stop before consuming it.
2228 That way, when confronted with something like:
2232 we stop before consuming the closing `}', even though we
2233 have not yet reached a `;'. */
2234 if (nesting_depth == 0)
2236 /* If it is the closing `}' for a block that we have
2237 scanned, stop -- but only after consuming the token.
2243 we will stop after the body of the erroneously declared
2244 function, but before consuming the following `typedef'
2246 if (--nesting_depth == 0)
2248 cp_lexer_consume_token (parser->lexer);
2252 /* If it the next token is a `{', then we are entering a new
2253 block. Consume the entire block. */
2254 else if (token->type == CPP_OPEN_BRACE)
2256 /* Consume the token. */
2257 cp_lexer_consume_token (parser->lexer);
2261 /* This function is called at the end of a statement or declaration.
2262 If the next token is a semicolon, it is consumed; otherwise, error
2263 recovery is attempted. */
2266 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2268 /* Look for the trailing `;'. */
2269 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2271 /* If there is additional (erroneous) input, skip to the end of
2273 cp_parser_skip_to_end_of_statement (parser);
2274 /* If the next token is now a `;', consume it. */
2275 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2276 cp_lexer_consume_token (parser->lexer);
2280 /* Skip tokens until we have consumed an entire block, or until we
2281 have consumed a non-nested `;'. */
2284 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2286 unsigned nesting_depth = 0;
2292 /* Peek at the next token. */
2293 token = cp_lexer_peek_token (parser->lexer);
2294 /* If we've run out of tokens, stop. */
2295 if (token->type == CPP_EOF)
2297 /* If the next token is a `;', we have reached the end of the
2299 if (token->type == CPP_SEMICOLON && !nesting_depth)
2301 /* Consume the `;'. */
2302 cp_lexer_consume_token (parser->lexer);
2305 /* Consume the token. */
2306 token = cp_lexer_consume_token (parser->lexer);
2307 /* If the next token is a non-nested `}', then we have reached
2308 the end of the current block. */
2309 if (token->type == CPP_CLOSE_BRACE
2310 && (nesting_depth == 0 || --nesting_depth == 0))
2312 /* If it the next token is a `{', then we are entering a new
2313 block. Consume the entire block. */
2314 if (token->type == CPP_OPEN_BRACE)
2319 /* Skip tokens until a non-nested closing curly brace is the next
2323 cp_parser_skip_to_closing_brace (cp_parser *parser)
2325 unsigned nesting_depth = 0;
2331 /* Peek at the next token. */
2332 token = cp_lexer_peek_token (parser->lexer);
2333 /* If we've run out of tokens, stop. */
2334 if (token->type == CPP_EOF)
2336 /* If the next token is a non-nested `}', then we have reached
2337 the end of the current block. */
2338 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2340 /* If it the next token is a `{', then we are entering a new
2341 block. Consume the entire block. */
2342 else if (token->type == CPP_OPEN_BRACE)
2344 /* Consume the token. */
2345 cp_lexer_consume_token (parser->lexer);
2349 /* This is a simple wrapper around make_typename_type. When the id is
2350 an unresolved identifier node, we can provide a superior diagnostic
2351 using cp_parser_diagnose_invalid_type_name. */
2354 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2357 if (TREE_CODE (id) == IDENTIFIER_NODE)
2359 result = make_typename_type (scope, id, /*complain=*/0);
2360 if (result == error_mark_node)
2361 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2364 return make_typename_type (scope, id, tf_error);
2368 /* Create a new C++ parser. */
2371 cp_parser_new (void)
2377 /* cp_lexer_new_main is called before calling ggc_alloc because
2378 cp_lexer_new_main might load a PCH file. */
2379 lexer = cp_lexer_new_main ();
2381 /* Initialize the binops_by_token so that we can get the tree
2382 directly from the token. */
2383 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2384 binops_by_token[binops[i].token_type] = binops[i];
2386 parser = GGC_CNEW (cp_parser);
2387 parser->lexer = lexer;
2388 parser->context = cp_parser_context_new (NULL);
2390 /* For now, we always accept GNU extensions. */
2391 parser->allow_gnu_extensions_p = 1;
2393 /* The `>' token is a greater-than operator, not the end of a
2395 parser->greater_than_is_operator_p = true;
2397 parser->default_arg_ok_p = true;
2399 /* We are not parsing a constant-expression. */
2400 parser->integral_constant_expression_p = false;
2401 parser->allow_non_integral_constant_expression_p = false;
2402 parser->non_integral_constant_expression_p = false;
2404 /* Local variable names are not forbidden. */
2405 parser->local_variables_forbidden_p = false;
2407 /* We are not processing an `extern "C"' declaration. */
2408 parser->in_unbraced_linkage_specification_p = false;
2410 /* We are not processing a declarator. */
2411 parser->in_declarator_p = false;
2413 /* We are not processing a template-argument-list. */
2414 parser->in_template_argument_list_p = false;
2416 /* We are not in an iteration statement. */
2417 parser->in_iteration_statement_p = false;
2419 /* We are not in a switch statement. */
2420 parser->in_switch_statement_p = false;
2422 /* We are not parsing a type-id inside an expression. */
2423 parser->in_type_id_in_expr_p = false;
2425 /* String literals should be translated to the execution character set. */
2426 parser->translate_strings_p = true;
2428 /* The unparsed function queue is empty. */
2429 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2431 /* There are no classes being defined. */
2432 parser->num_classes_being_defined = 0;
2434 /* No template parameters apply. */
2435 parser->num_template_parameter_lists = 0;
2440 /* Create a cp_lexer structure which will emit the tokens in CACHE
2441 and push it onto the parser's lexer stack. This is used for delayed
2442 parsing of in-class method bodies and default arguments, and should
2443 not be confused with tentative parsing. */
2445 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2447 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2448 lexer->next = parser->lexer;
2449 parser->lexer = lexer;
2451 /* Move the current source position to that of the first token in the
2453 cp_lexer_set_source_position_from_token (lexer->next_token);
2456 /* Pop the top lexer off the parser stack. This is never used for the
2457 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2459 cp_parser_pop_lexer (cp_parser *parser)
2461 cp_lexer *lexer = parser->lexer;
2462 parser->lexer = lexer->next;
2463 cp_lexer_destroy (lexer);
2465 /* Put the current source position back where it was before this
2466 lexer was pushed. */
2467 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2470 /* Lexical conventions [gram.lex] */
2472 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2476 cp_parser_identifier (cp_parser* parser)
2480 /* Look for the identifier. */
2481 token = cp_parser_require (parser, CPP_NAME, "identifier");
2482 /* Return the value. */
2483 return token ? token->value : error_mark_node;
2486 /* Parse a sequence of adjacent string constants. Returns a
2487 TREE_STRING representing the combined, nul-terminated string
2488 constant. If TRANSLATE is true, translate the string to the
2489 execution character set. If WIDE_OK is true, a wide string is
2492 C++98 [lex.string] says that if a narrow string literal token is
2493 adjacent to a wide string literal token, the behavior is undefined.
2494 However, C99 6.4.5p4 says that this results in a wide string literal.
2495 We follow C99 here, for consistency with the C front end.
2497 This code is largely lifted from lex_string() in c-lex.c.
2499 FUTURE: ObjC++ will need to handle @-strings here. */
2501 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2506 struct obstack str_ob;
2507 cpp_string str, istr, *strs;
2510 tok = cp_lexer_peek_token (parser->lexer);
2511 if (!cp_parser_is_string_literal (tok))
2513 cp_parser_error (parser, "expected string-literal");
2514 return error_mark_node;
2517 /* Try to avoid the overhead of creating and destroying an obstack
2518 for the common case of just one string. */
2519 if (!cp_parser_is_string_literal
2520 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2522 cp_lexer_consume_token (parser->lexer);
2524 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2525 str.len = TREE_STRING_LENGTH (tok->value);
2527 if (tok->type == CPP_WSTRING)
2534 gcc_obstack_init (&str_ob);
2539 cp_lexer_consume_token (parser->lexer);
2541 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2542 str.len = TREE_STRING_LENGTH (tok->value);
2543 if (tok->type == CPP_WSTRING)
2546 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2548 tok = cp_lexer_peek_token (parser->lexer);
2550 while (cp_parser_is_string_literal (tok));
2552 strs = (cpp_string *) obstack_finish (&str_ob);
2555 if (wide && !wide_ok)
2557 cp_parser_error (parser, "a wide string is invalid in this context");
2561 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2562 (parse_in, strs, count, &istr, wide))
2564 value = build_string (istr.len, (char *)istr.text);
2565 free ((void *)istr.text);
2567 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2568 value = fix_string_type (value);
2571 /* cpp_interpret_string has issued an error. */
2572 value = error_mark_node;
2575 obstack_free (&str_ob, 0);
2581 /* Basic concepts [gram.basic] */
2583 /* Parse a translation-unit.
2586 declaration-seq [opt]
2588 Returns TRUE if all went well. */
2591 cp_parser_translation_unit (cp_parser* parser)
2593 /* The address of the first non-permanent object on the declarator
2595 static void *declarator_obstack_base;
2599 /* Create the declarator obstack, if necessary. */
2600 if (!cp_error_declarator)
2602 gcc_obstack_init (&declarator_obstack);
2603 /* Create the error declarator. */
2604 cp_error_declarator = make_declarator (cdk_error);
2605 /* Create the empty parameter list. */
2606 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2607 /* Remember where the base of the declarator obstack lies. */
2608 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2613 cp_parser_declaration_seq_opt (parser);
2615 /* If there are no tokens left then all went well. */
2616 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2618 /* Consume the EOF token. */
2619 cp_parser_require (parser, CPP_EOF, "end-of-file");
2621 /* Get rid of the token array; we don't need it any more. */
2622 cp_lexer_destroy (parser->lexer);
2623 parser->lexer = NULL;
2626 finish_translation_unit ();
2633 cp_parser_error (parser, "expected declaration");
2639 /* Make sure the declarator obstack was fully cleaned up. */
2640 gcc_assert (obstack_next_free (&declarator_obstack)
2641 == declarator_obstack_base);
2643 /* All went well. */
2647 /* Expressions [gram.expr] */
2649 /* Parse a primary-expression.
2660 ( compound-statement )
2661 __builtin_va_arg ( assignment-expression , type-id )
2666 Returns a representation of the expression.
2668 *IDK indicates what kind of id-expression (if any) was present.
2670 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2671 used as the operand of a pointer-to-member. In that case,
2672 *QUALIFYING_CLASS gives the class that is used as the qualifying
2673 class in the pointer-to-member. */
2676 cp_parser_primary_expression (cp_parser *parser,
2678 tree *qualifying_class)
2682 /* Assume the primary expression is not an id-expression. */
2683 *idk = CP_ID_KIND_NONE;
2684 /* And that it cannot be used as pointer-to-member. */
2685 *qualifying_class = NULL_TREE;
2687 /* Peek at the next token. */
2688 token = cp_lexer_peek_token (parser->lexer);
2689 switch (token->type)
2700 token = cp_lexer_consume_token (parser->lexer);
2701 return token->value;
2705 /* ??? Should wide strings be allowed when parser->translate_strings_p
2706 is false (i.e. in attributes)? If not, we can kill the third
2707 argument to cp_parser_string_literal. */
2708 return cp_parser_string_literal (parser,
2709 parser->translate_strings_p,
2712 case CPP_OPEN_PAREN:
2715 bool saved_greater_than_is_operator_p;
2717 /* Consume the `('. */
2718 cp_lexer_consume_token (parser->lexer);
2719 /* Within a parenthesized expression, a `>' token is always
2720 the greater-than operator. */
2721 saved_greater_than_is_operator_p
2722 = parser->greater_than_is_operator_p;
2723 parser->greater_than_is_operator_p = true;
2724 /* If we see `( { ' then we are looking at the beginning of
2725 a GNU statement-expression. */
2726 if (cp_parser_allow_gnu_extensions_p (parser)
2727 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2729 /* Statement-expressions are not allowed by the standard. */
2731 pedwarn ("ISO C++ forbids braced-groups within expressions");
2733 /* And they're not allowed outside of a function-body; you
2734 cannot, for example, write:
2736 int i = ({ int j = 3; j + 1; });
2738 at class or namespace scope. */
2739 if (!at_function_scope_p ())
2740 error ("statement-expressions are allowed only inside functions");
2741 /* Start the statement-expression. */
2742 expr = begin_stmt_expr ();
2743 /* Parse the compound-statement. */
2744 cp_parser_compound_statement (parser, expr, false);
2746 expr = finish_stmt_expr (expr, false);
2750 /* Parse the parenthesized expression. */
2751 expr = cp_parser_expression (parser);
2752 /* Let the front end know that this expression was
2753 enclosed in parentheses. This matters in case, for
2754 example, the expression is of the form `A::B', since
2755 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2757 finish_parenthesized_expr (expr);
2759 /* The `>' token might be the end of a template-id or
2760 template-parameter-list now. */
2761 parser->greater_than_is_operator_p
2762 = saved_greater_than_is_operator_p;
2763 /* Consume the `)'. */
2764 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2765 cp_parser_skip_to_end_of_statement (parser);
2771 switch (token->keyword)
2773 /* These two are the boolean literals. */
2775 cp_lexer_consume_token (parser->lexer);
2776 return boolean_true_node;
2778 cp_lexer_consume_token (parser->lexer);
2779 return boolean_false_node;
2781 /* The `__null' literal. */
2783 cp_lexer_consume_token (parser->lexer);
2786 /* Recognize the `this' keyword. */
2788 cp_lexer_consume_token (parser->lexer);
2789 if (parser->local_variables_forbidden_p)
2791 error ("`this' may not be used in this context");
2792 return error_mark_node;
2794 /* Pointers cannot appear in constant-expressions. */
2795 if (cp_parser_non_integral_constant_expression (parser,
2797 return error_mark_node;
2798 return finish_this_expr ();
2800 /* The `operator' keyword can be the beginning of an
2805 case RID_FUNCTION_NAME:
2806 case RID_PRETTY_FUNCTION_NAME:
2807 case RID_C99_FUNCTION_NAME:
2808 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2809 __func__ are the names of variables -- but they are
2810 treated specially. Therefore, they are handled here,
2811 rather than relying on the generic id-expression logic
2812 below. Grammatically, these names are id-expressions.
2814 Consume the token. */
2815 token = cp_lexer_consume_token (parser->lexer);
2816 /* Look up the name. */
2817 return finish_fname (token->value);
2824 /* The `__builtin_va_arg' construct is used to handle
2825 `va_arg'. Consume the `__builtin_va_arg' token. */
2826 cp_lexer_consume_token (parser->lexer);
2827 /* Look for the opening `('. */
2828 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2829 /* Now, parse the assignment-expression. */
2830 expression = cp_parser_assignment_expression (parser);
2831 /* Look for the `,'. */
2832 cp_parser_require (parser, CPP_COMMA, "`,'");
2833 /* Parse the type-id. */
2834 type = cp_parser_type_id (parser);
2835 /* Look for the closing `)'. */
2836 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2837 /* Using `va_arg' in a constant-expression is not
2839 if (cp_parser_non_integral_constant_expression (parser,
2841 return error_mark_node;
2842 return build_x_va_arg (expression, type);
2846 return cp_parser_builtin_offsetof (parser);
2849 cp_parser_error (parser, "expected primary-expression");
2850 return error_mark_node;
2853 /* An id-expression can start with either an identifier, a
2854 `::' as the beginning of a qualified-id, or the "operator"
2858 case CPP_TEMPLATE_ID:
2859 case CPP_NESTED_NAME_SPECIFIER:
2863 const char *error_msg;
2866 /* Parse the id-expression. */
2868 = cp_parser_id_expression (parser,
2869 /*template_keyword_p=*/false,
2870 /*check_dependency_p=*/true,
2871 /*template_p=*/NULL,
2872 /*declarator_p=*/false);
2873 if (id_expression == error_mark_node)
2874 return error_mark_node;
2875 /* If we have a template-id, then no further lookup is
2876 required. If the template-id was for a template-class, we
2877 will sometimes have a TYPE_DECL at this point. */
2878 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2879 || TREE_CODE (id_expression) == TYPE_DECL)
2880 decl = id_expression;
2881 /* Look up the name. */
2886 decl = cp_parser_lookup_name (parser, id_expression,
2888 /*is_template=*/false,
2889 /*is_namespace=*/false,
2890 /*check_dependency=*/true,
2892 /* If the lookup was ambiguous, an error will already have
2895 return error_mark_node;
2896 /* If name lookup gives us a SCOPE_REF, then the
2897 qualifying scope was dependent. Just propagate the
2899 if (TREE_CODE (decl) == SCOPE_REF)
2901 if (TYPE_P (TREE_OPERAND (decl, 0)))
2902 *qualifying_class = TREE_OPERAND (decl, 0);
2905 /* Check to see if DECL is a local variable in a context
2906 where that is forbidden. */
2907 if (parser->local_variables_forbidden_p
2908 && local_variable_p (decl))
2910 /* It might be that we only found DECL because we are
2911 trying to be generous with pre-ISO scoping rules.
2912 For example, consider:
2916 for (int i = 0; i < 10; ++i) {}
2917 extern void f(int j = i);
2920 Here, name look up will originally find the out
2921 of scope `i'. We need to issue a warning message,
2922 but then use the global `i'. */
2923 decl = check_for_out_of_scope_variable (decl);
2924 if (local_variable_p (decl))
2926 error ("local variable `%D' may not appear in this context",
2928 return error_mark_node;
2933 decl = finish_id_expression (id_expression, decl, parser->scope,
2934 idk, qualifying_class,
2935 parser->integral_constant_expression_p,
2936 parser->allow_non_integral_constant_expression_p,
2937 &parser->non_integral_constant_expression_p,
2940 cp_parser_error (parser, error_msg);
2944 /* Anything else is an error. */
2946 cp_parser_error (parser, "expected primary-expression");
2947 return error_mark_node;
2951 /* Parse an id-expression.
2958 :: [opt] nested-name-specifier template [opt] unqualified-id
2960 :: operator-function-id
2963 Return a representation of the unqualified portion of the
2964 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2965 a `::' or nested-name-specifier.
2967 Often, if the id-expression was a qualified-id, the caller will
2968 want to make a SCOPE_REF to represent the qualified-id. This
2969 function does not do this in order to avoid wastefully creating
2970 SCOPE_REFs when they are not required.
2972 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2975 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2976 uninstantiated templates.
2978 If *TEMPLATE_P is non-NULL, it is set to true iff the
2979 `template' keyword is used to explicitly indicate that the entity
2980 named is a template.
2982 If DECLARATOR_P is true, the id-expression is appearing as part of
2983 a declarator, rather than as part of an expression. */
2986 cp_parser_id_expression (cp_parser *parser,
2987 bool template_keyword_p,
2988 bool check_dependency_p,
2992 bool global_scope_p;
2993 bool nested_name_specifier_p;
2995 /* Assume the `template' keyword was not used. */
2997 *template_p = false;
2999 /* Look for the optional `::' operator. */
3001 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3003 /* Look for the optional nested-name-specifier. */
3004 nested_name_specifier_p
3005 = (cp_parser_nested_name_specifier_opt (parser,
3006 /*typename_keyword_p=*/false,
3011 /* If there is a nested-name-specifier, then we are looking at
3012 the first qualified-id production. */
3013 if (nested_name_specifier_p)
3016 tree saved_object_scope;
3017 tree saved_qualifying_scope;
3018 tree unqualified_id;
3021 /* See if the next token is the `template' keyword. */
3023 template_p = &is_template;
3024 *template_p = cp_parser_optional_template_keyword (parser);
3025 /* Name lookup we do during the processing of the
3026 unqualified-id might obliterate SCOPE. */
3027 saved_scope = parser->scope;
3028 saved_object_scope = parser->object_scope;
3029 saved_qualifying_scope = parser->qualifying_scope;
3030 /* Process the final unqualified-id. */
3031 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3034 /* Restore the SAVED_SCOPE for our caller. */
3035 parser->scope = saved_scope;
3036 parser->object_scope = saved_object_scope;
3037 parser->qualifying_scope = saved_qualifying_scope;
3039 return unqualified_id;
3041 /* Otherwise, if we are in global scope, then we are looking at one
3042 of the other qualified-id productions. */
3043 else if (global_scope_p)
3048 /* Peek at the next token. */
3049 token = cp_lexer_peek_token (parser->lexer);
3051 /* If it's an identifier, and the next token is not a "<", then
3052 we can avoid the template-id case. This is an optimization
3053 for this common case. */
3054 if (token->type == CPP_NAME
3055 && !cp_parser_nth_token_starts_template_argument_list_p
3057 return cp_parser_identifier (parser);
3059 cp_parser_parse_tentatively (parser);
3060 /* Try a template-id. */
3061 id = cp_parser_template_id (parser,
3062 /*template_keyword_p=*/false,
3063 /*check_dependency_p=*/true,
3065 /* If that worked, we're done. */
3066 if (cp_parser_parse_definitely (parser))
3069 /* Peek at the next token. (Changes in the token buffer may
3070 have invalidated the pointer obtained above.) */
3071 token = cp_lexer_peek_token (parser->lexer);
3073 switch (token->type)
3076 return cp_parser_identifier (parser);
3079 if (token->keyword == RID_OPERATOR)
3080 return cp_parser_operator_function_id (parser);
3084 cp_parser_error (parser, "expected id-expression");
3085 return error_mark_node;
3089 return cp_parser_unqualified_id (parser, template_keyword_p,
3090 /*check_dependency_p=*/true,
3094 /* Parse an unqualified-id.
3098 operator-function-id
3099 conversion-function-id
3103 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3104 keyword, in a construct like `A::template ...'.
3106 Returns a representation of unqualified-id. For the `identifier'
3107 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3108 production a BIT_NOT_EXPR is returned; the operand of the
3109 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3110 other productions, see the documentation accompanying the
3111 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3112 names are looked up in uninstantiated templates. If DECLARATOR_P
3113 is true, the unqualified-id is appearing as part of a declarator,
3114 rather than as part of an expression. */
3117 cp_parser_unqualified_id (cp_parser* parser,
3118 bool template_keyword_p,
3119 bool check_dependency_p,
3124 /* Peek at the next token. */
3125 token = cp_lexer_peek_token (parser->lexer);
3127 switch (token->type)
3133 /* We don't know yet whether or not this will be a
3135 cp_parser_parse_tentatively (parser);
3136 /* Try a template-id. */
3137 id = cp_parser_template_id (parser, template_keyword_p,
3140 /* If it worked, we're done. */
3141 if (cp_parser_parse_definitely (parser))
3143 /* Otherwise, it's an ordinary identifier. */
3144 return cp_parser_identifier (parser);
3147 case CPP_TEMPLATE_ID:
3148 return cp_parser_template_id (parser, template_keyword_p,
3155 tree qualifying_scope;
3159 /* Consume the `~' token. */
3160 cp_lexer_consume_token (parser->lexer);
3161 /* Parse the class-name. The standard, as written, seems to
3164 template <typename T> struct S { ~S (); };
3165 template <typename T> S<T>::~S() {}
3167 is invalid, since `~' must be followed by a class-name, but
3168 `S<T>' is dependent, and so not known to be a class.
3169 That's not right; we need to look in uninstantiated
3170 templates. A further complication arises from:
3172 template <typename T> void f(T t) {
3176 Here, it is not possible to look up `T' in the scope of `T'
3177 itself. We must look in both the current scope, and the
3178 scope of the containing complete expression.
3180 Yet another issue is:
3189 The standard does not seem to say that the `S' in `~S'
3190 should refer to the type `S' and not the data member
3193 /* DR 244 says that we look up the name after the "~" in the
3194 same scope as we looked up the qualifying name. That idea
3195 isn't fully worked out; it's more complicated than that. */
3196 scope = parser->scope;
3197 object_scope = parser->object_scope;
3198 qualifying_scope = parser->qualifying_scope;
3200 /* If the name is of the form "X::~X" it's OK. */
3201 if (scope && TYPE_P (scope)
3202 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3203 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3205 && (cp_lexer_peek_token (parser->lexer)->value
3206 == TYPE_IDENTIFIER (scope)))
3208 cp_lexer_consume_token (parser->lexer);
3209 return build_nt (BIT_NOT_EXPR, scope);
3212 /* If there was an explicit qualification (S::~T), first look
3213 in the scope given by the qualification (i.e., S). */
3216 cp_parser_parse_tentatively (parser);
3217 type_decl = cp_parser_class_name (parser,
3218 /*typename_keyword_p=*/false,
3219 /*template_keyword_p=*/false,
3221 /*check_dependency=*/false,
3222 /*class_head_p=*/false,
3224 if (cp_parser_parse_definitely (parser))
3225 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3227 /* In "N::S::~S", look in "N" as well. */
3228 if (scope && qualifying_scope)
3230 cp_parser_parse_tentatively (parser);
3231 parser->scope = qualifying_scope;
3232 parser->object_scope = NULL_TREE;
3233 parser->qualifying_scope = NULL_TREE;
3235 = cp_parser_class_name (parser,
3236 /*typename_keyword_p=*/false,
3237 /*template_keyword_p=*/false,
3239 /*check_dependency=*/false,
3240 /*class_head_p=*/false,
3242 if (cp_parser_parse_definitely (parser))
3243 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3245 /* In "p->S::~T", look in the scope given by "*p" as well. */
3246 else if (object_scope)
3248 cp_parser_parse_tentatively (parser);
3249 parser->scope = object_scope;
3250 parser->object_scope = NULL_TREE;
3251 parser->qualifying_scope = NULL_TREE;
3253 = cp_parser_class_name (parser,
3254 /*typename_keyword_p=*/false,
3255 /*template_keyword_p=*/false,
3257 /*check_dependency=*/false,
3258 /*class_head_p=*/false,
3260 if (cp_parser_parse_definitely (parser))
3261 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3263 /* Look in the surrounding context. */
3264 parser->scope = NULL_TREE;
3265 parser->object_scope = NULL_TREE;
3266 parser->qualifying_scope = NULL_TREE;
3268 = cp_parser_class_name (parser,
3269 /*typename_keyword_p=*/false,
3270 /*template_keyword_p=*/false,
3272 /*check_dependency=*/false,
3273 /*class_head_p=*/false,
3275 /* If an error occurred, assume that the name of the
3276 destructor is the same as the name of the qualifying
3277 class. That allows us to keep parsing after running
3278 into ill-formed destructor names. */
3279 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3280 return build_nt (BIT_NOT_EXPR, scope);
3281 else if (type_decl == error_mark_node)
3282 return error_mark_node;
3286 A typedef-name that names a class shall not be used as the
3287 identifier in the declarator for a destructor declaration. */
3289 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3290 && !DECL_SELF_REFERENCE_P (type_decl))
3291 error ("typedef-name `%D' used as destructor declarator",
3294 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3298 if (token->keyword == RID_OPERATOR)
3302 /* This could be a template-id, so we try that first. */
3303 cp_parser_parse_tentatively (parser);
3304 /* Try a template-id. */
3305 id = cp_parser_template_id (parser, template_keyword_p,
3306 /*check_dependency_p=*/true,
3308 /* If that worked, we're done. */
3309 if (cp_parser_parse_definitely (parser))
3311 /* We still don't know whether we're looking at an
3312 operator-function-id or a conversion-function-id. */
3313 cp_parser_parse_tentatively (parser);
3314 /* Try an operator-function-id. */
3315 id = cp_parser_operator_function_id (parser);
3316 /* If that didn't work, try a conversion-function-id. */
3317 if (!cp_parser_parse_definitely (parser))
3318 id = cp_parser_conversion_function_id (parser);
3325 cp_parser_error (parser, "expected unqualified-id");
3326 return error_mark_node;
3330 /* Parse an (optional) nested-name-specifier.
3332 nested-name-specifier:
3333 class-or-namespace-name :: nested-name-specifier [opt]
3334 class-or-namespace-name :: template nested-name-specifier [opt]
3336 PARSER->SCOPE should be set appropriately before this function is
3337 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3338 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3341 Sets PARSER->SCOPE to the class (TYPE) or namespace
3342 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3343 it unchanged if there is no nested-name-specifier. Returns the new
3344 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3346 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3347 part of a declaration and/or decl-specifier. */
3350 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3351 bool typename_keyword_p,
3352 bool check_dependency_p,
3354 bool is_declaration)
3356 bool success = false;
3357 tree access_check = NULL_TREE;
3361 /* If the next token corresponds to a nested name specifier, there
3362 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3363 false, it may have been true before, in which case something
3364 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3365 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3366 CHECK_DEPENDENCY_P is false, we have to fall through into the
3368 if (check_dependency_p
3369 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3371 cp_parser_pre_parsed_nested_name_specifier (parser);
3372 return parser->scope;
3375 /* Remember where the nested-name-specifier starts. */
3376 if (cp_parser_parsing_tentatively (parser)
3377 && !cp_parser_committed_to_tentative_parse (parser))
3379 token = cp_lexer_peek_token (parser->lexer);
3380 start = cp_lexer_token_difference (parser->lexer,
3381 parser->lexer->buffer,
3387 push_deferring_access_checks (dk_deferred);
3393 tree saved_qualifying_scope;
3394 bool template_keyword_p;
3396 /* Spot cases that cannot be the beginning of a
3397 nested-name-specifier. */
3398 token = cp_lexer_peek_token (parser->lexer);
3400 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3401 the already parsed nested-name-specifier. */
3402 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3404 /* Grab the nested-name-specifier and continue the loop. */
3405 cp_parser_pre_parsed_nested_name_specifier (parser);
3410 /* Spot cases that cannot be the beginning of a
3411 nested-name-specifier. On the second and subsequent times
3412 through the loop, we look for the `template' keyword. */
3413 if (success && token->keyword == RID_TEMPLATE)
3415 /* A template-id can start a nested-name-specifier. */
3416 else if (token->type == CPP_TEMPLATE_ID)
3420 /* If the next token is not an identifier, then it is
3421 definitely not a class-or-namespace-name. */
3422 if (token->type != CPP_NAME)
3424 /* If the following token is neither a `<' (to begin a
3425 template-id), nor a `::', then we are not looking at a
3426 nested-name-specifier. */
3427 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3428 if (token->type != CPP_SCOPE
3429 && !cp_parser_nth_token_starts_template_argument_list_p
3434 /* The nested-name-specifier is optional, so we parse
3436 cp_parser_parse_tentatively (parser);
3438 /* Look for the optional `template' keyword, if this isn't the
3439 first time through the loop. */
3441 template_keyword_p = cp_parser_optional_template_keyword (parser);
3443 template_keyword_p = false;
3445 /* Save the old scope since the name lookup we are about to do
3446 might destroy it. */
3447 old_scope = parser->scope;
3448 saved_qualifying_scope = parser->qualifying_scope;
3449 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3450 look up names in "X<T>::I" in order to determine that "Y" is
3451 a template. So, if we have a typename at this point, we make
3452 an effort to look through it. */
3454 && !typename_keyword_p
3456 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3457 parser->scope = resolve_typename_type (parser->scope,
3458 /*only_current_p=*/false);
3459 /* Parse the qualifying entity. */
3461 = cp_parser_class_or_namespace_name (parser,
3467 /* Look for the `::' token. */
3468 cp_parser_require (parser, CPP_SCOPE, "`::'");
3470 /* If we found what we wanted, we keep going; otherwise, we're
3472 if (!cp_parser_parse_definitely (parser))
3474 bool error_p = false;
3476 /* Restore the OLD_SCOPE since it was valid before the
3477 failed attempt at finding the last
3478 class-or-namespace-name. */
3479 parser->scope = old_scope;
3480 parser->qualifying_scope = saved_qualifying_scope;
3481 /* If the next token is an identifier, and the one after
3482 that is a `::', then any valid interpretation would have
3483 found a class-or-namespace-name. */
3484 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3485 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3487 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3490 token = cp_lexer_consume_token (parser->lexer);
3495 decl = cp_parser_lookup_name_simple (parser, token->value);
3496 if (TREE_CODE (decl) == TEMPLATE_DECL)
3497 error ("`%D' used without template parameters",
3500 cp_parser_name_lookup_error
3501 (parser, token->value, decl,
3502 "is not a class or namespace");
3503 parser->scope = NULL_TREE;
3505 /* Treat this as a successful nested-name-specifier
3510 If the name found is not a class-name (clause
3511 _class_) or namespace-name (_namespace.def_), the
3512 program is ill-formed. */
3515 cp_lexer_consume_token (parser->lexer);
3520 /* We've found one valid nested-name-specifier. */
3522 /* Make sure we look in the right scope the next time through
3524 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3525 ? TREE_TYPE (new_scope)
3527 /* If it is a class scope, try to complete it; we are about to
3528 be looking up names inside the class. */
3529 if (TYPE_P (parser->scope)
3530 /* Since checking types for dependency can be expensive,
3531 avoid doing it if the type is already complete. */
3532 && !COMPLETE_TYPE_P (parser->scope)
3533 /* Do not try to complete dependent types. */
3534 && !dependent_type_p (parser->scope))
3535 complete_type (parser->scope);
3538 /* Retrieve any deferred checks. Do not pop this access checks yet
3539 so the memory will not be reclaimed during token replacing below. */
3540 access_check = get_deferred_access_checks ();
3542 /* If parsing tentatively, replace the sequence of tokens that makes
3543 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3544 token. That way, should we re-parse the token stream, we will
3545 not have to repeat the effort required to do the parse, nor will
3546 we issue duplicate error messages. */
3547 if (success && start >= 0)
3549 /* Find the token that corresponds to the start of the
3551 token = cp_lexer_advance_token (parser->lexer,
3552 parser->lexer->buffer,
3555 /* Reset the contents of the START token. */
3556 token->type = CPP_NESTED_NAME_SPECIFIER;
3557 token->value = build_tree_list (access_check, parser->scope);
3558 TREE_TYPE (token->value) = parser->qualifying_scope;
3559 token->keyword = RID_MAX;
3560 /* Purge all subsequent tokens. */
3561 cp_lexer_purge_tokens_after (parser->lexer, token);
3564 pop_deferring_access_checks ();
3565 return success ? parser->scope : NULL_TREE;
3568 /* Parse a nested-name-specifier. See
3569 cp_parser_nested_name_specifier_opt for details. This function
3570 behaves identically, except that it will an issue an error if no
3571 nested-name-specifier is present, and it will return
3572 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3576 cp_parser_nested_name_specifier (cp_parser *parser,
3577 bool typename_keyword_p,
3578 bool check_dependency_p,
3580 bool is_declaration)
3584 /* Look for the nested-name-specifier. */
3585 scope = cp_parser_nested_name_specifier_opt (parser,
3590 /* If it was not present, issue an error message. */
3593 cp_parser_error (parser, "expected nested-name-specifier");
3594 parser->scope = NULL_TREE;
3595 return error_mark_node;
3601 /* Parse a class-or-namespace-name.
3603 class-or-namespace-name:
3607 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3608 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3609 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3610 TYPE_P is TRUE iff the next name should be taken as a class-name,
3611 even the same name is declared to be another entity in the same
3614 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3615 specified by the class-or-namespace-name. If neither is found the
3616 ERROR_MARK_NODE is returned. */
3619 cp_parser_class_or_namespace_name (cp_parser *parser,
3620 bool typename_keyword_p,
3621 bool template_keyword_p,
3622 bool check_dependency_p,
3624 bool is_declaration)
3627 tree saved_qualifying_scope;
3628 tree saved_object_scope;
3632 /* Before we try to parse the class-name, we must save away the
3633 current PARSER->SCOPE since cp_parser_class_name will destroy
3635 saved_scope = parser->scope;
3636 saved_qualifying_scope = parser->qualifying_scope;
3637 saved_object_scope = parser->object_scope;
3638 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3639 there is no need to look for a namespace-name. */
3640 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3642 cp_parser_parse_tentatively (parser);
3643 scope = cp_parser_class_name (parser,
3648 /*class_head_p=*/false,
3650 /* If that didn't work, try for a namespace-name. */
3651 if (!only_class_p && !cp_parser_parse_definitely (parser))
3653 /* Restore the saved scope. */
3654 parser->scope = saved_scope;
3655 parser->qualifying_scope = saved_qualifying_scope;
3656 parser->object_scope = saved_object_scope;
3657 /* If we are not looking at an identifier followed by the scope
3658 resolution operator, then this is not part of a
3659 nested-name-specifier. (Note that this function is only used
3660 to parse the components of a nested-name-specifier.) */
3661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3662 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3663 return error_mark_node;
3664 scope = cp_parser_namespace_name (parser);
3670 /* Parse a postfix-expression.
3674 postfix-expression [ expression ]
3675 postfix-expression ( expression-list [opt] )
3676 simple-type-specifier ( expression-list [opt] )
3677 typename :: [opt] nested-name-specifier identifier
3678 ( expression-list [opt] )
3679 typename :: [opt] nested-name-specifier template [opt] template-id
3680 ( expression-list [opt] )
3681 postfix-expression . template [opt] id-expression
3682 postfix-expression -> template [opt] id-expression
3683 postfix-expression . pseudo-destructor-name
3684 postfix-expression -> pseudo-destructor-name
3685 postfix-expression ++
3686 postfix-expression --
3687 dynamic_cast < type-id > ( expression )
3688 static_cast < type-id > ( expression )
3689 reinterpret_cast < type-id > ( expression )
3690 const_cast < type-id > ( expression )
3691 typeid ( expression )
3697 ( type-id ) { initializer-list , [opt] }
3699 This extension is a GNU version of the C99 compound-literal
3700 construct. (The C99 grammar uses `type-name' instead of `type-id',
3701 but they are essentially the same concept.)
3703 If ADDRESS_P is true, the postfix expression is the operand of the
3706 Returns a representation of the expression. */
3709 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3713 cp_id_kind idk = CP_ID_KIND_NONE;
3714 tree postfix_expression = NULL_TREE;
3715 /* Non-NULL only if the current postfix-expression can be used to
3716 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3717 class used to qualify the member. */
3718 tree qualifying_class = NULL_TREE;
3720 /* Peek at the next token. */
3721 token = cp_lexer_peek_token (parser->lexer);
3722 /* Some of the productions are determined by keywords. */
3723 keyword = token->keyword;
3733 const char *saved_message;
3735 /* All of these can be handled in the same way from the point
3736 of view of parsing. Begin by consuming the token
3737 identifying the cast. */
3738 cp_lexer_consume_token (parser->lexer);
3740 /* New types cannot be defined in the cast. */
3741 saved_message = parser->type_definition_forbidden_message;
3742 parser->type_definition_forbidden_message
3743 = "types may not be defined in casts";
3745 /* Look for the opening `<'. */
3746 cp_parser_require (parser, CPP_LESS, "`<'");
3747 /* Parse the type to which we are casting. */
3748 type = cp_parser_type_id (parser);
3749 /* Look for the closing `>'. */
3750 cp_parser_require (parser, CPP_GREATER, "`>'");
3751 /* Restore the old message. */
3752 parser->type_definition_forbidden_message = saved_message;
3754 /* And the expression which is being cast. */
3755 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3756 expression = cp_parser_expression (parser);
3757 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3759 /* Only type conversions to integral or enumeration types
3760 can be used in constant-expressions. */
3761 if (parser->integral_constant_expression_p
3762 && !dependent_type_p (type)
3763 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3764 && (cp_parser_non_integral_constant_expression
3766 "a cast to a type other than an integral or "
3767 "enumeration type")))
3768 return error_mark_node;
3774 = build_dynamic_cast (type, expression);
3778 = build_static_cast (type, expression);
3782 = build_reinterpret_cast (type, expression);
3786 = build_const_cast (type, expression);
3797 const char *saved_message;
3798 bool saved_in_type_id_in_expr_p;
3800 /* Consume the `typeid' token. */
3801 cp_lexer_consume_token (parser->lexer);
3802 /* Look for the `(' token. */
3803 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3804 /* Types cannot be defined in a `typeid' expression. */
3805 saved_message = parser->type_definition_forbidden_message;
3806 parser->type_definition_forbidden_message
3807 = "types may not be defined in a `typeid\' expression";
3808 /* We can't be sure yet whether we're looking at a type-id or an
3810 cp_parser_parse_tentatively (parser);
3811 /* Try a type-id first. */
3812 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3813 parser->in_type_id_in_expr_p = true;
3814 type = cp_parser_type_id (parser);
3815 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3816 /* Look for the `)' token. Otherwise, we can't be sure that
3817 we're not looking at an expression: consider `typeid (int
3818 (3))', for example. */
3819 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3820 /* If all went well, simply lookup the type-id. */
3821 if (cp_parser_parse_definitely (parser))
3822 postfix_expression = get_typeid (type);
3823 /* Otherwise, fall back to the expression variant. */
3828 /* Look for an expression. */
3829 expression = cp_parser_expression (parser);
3830 /* Compute its typeid. */
3831 postfix_expression = build_typeid (expression);
3832 /* Look for the `)' token. */
3833 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3835 /* `typeid' may not appear in an integral constant expression. */
3836 if (cp_parser_non_integral_constant_expression(parser,
3837 "`typeid' operator"))
3838 return error_mark_node;
3839 /* Restore the saved message. */
3840 parser->type_definition_forbidden_message = saved_message;
3846 bool template_p = false;
3850 /* Consume the `typename' token. */
3851 cp_lexer_consume_token (parser->lexer);
3852 /* Look for the optional `::' operator. */
3853 cp_parser_global_scope_opt (parser,
3854 /*current_scope_valid_p=*/false);
3855 /* Look for the nested-name-specifier. */
3856 cp_parser_nested_name_specifier (parser,
3857 /*typename_keyword_p=*/true,
3858 /*check_dependency_p=*/true,
3860 /*is_declaration=*/true);
3861 /* Look for the optional `template' keyword. */
3862 template_p = cp_parser_optional_template_keyword (parser);
3863 /* We don't know whether we're looking at a template-id or an
3865 cp_parser_parse_tentatively (parser);
3866 /* Try a template-id. */
3867 id = cp_parser_template_id (parser, template_p,
3868 /*check_dependency_p=*/true,
3869 /*is_declaration=*/true);
3870 /* If that didn't work, try an identifier. */
3871 if (!cp_parser_parse_definitely (parser))
3872 id = cp_parser_identifier (parser);
3873 /* If we look up a template-id in a non-dependent qualifying
3874 scope, there's no need to create a dependent type. */
3875 if (TREE_CODE (id) == TYPE_DECL
3876 && !dependent_type_p (parser->scope))
3877 type = TREE_TYPE (id);
3878 /* Create a TYPENAME_TYPE to represent the type to which the
3879 functional cast is being performed. */
3881 type = make_typename_type (parser->scope, id,
3884 postfix_expression = cp_parser_functional_cast (parser, type);
3892 /* If the next thing is a simple-type-specifier, we may be
3893 looking at a functional cast. We could also be looking at
3894 an id-expression. So, we try the functional cast, and if
3895 that doesn't work we fall back to the primary-expression. */
3896 cp_parser_parse_tentatively (parser);
3897 /* Look for the simple-type-specifier. */
3898 type = cp_parser_simple_type_specifier (parser,
3899 /*decl_specs=*/NULL,
3900 CP_PARSER_FLAGS_NONE);
3901 /* Parse the cast itself. */
3902 if (!cp_parser_error_occurred (parser))
3904 = cp_parser_functional_cast (parser, type);
3905 /* If that worked, we're done. */
3906 if (cp_parser_parse_definitely (parser))
3909 /* If the functional-cast didn't work out, try a
3910 compound-literal. */
3911 if (cp_parser_allow_gnu_extensions_p (parser)
3912 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3914 tree initializer_list = NULL_TREE;
3915 bool saved_in_type_id_in_expr_p;
3917 cp_parser_parse_tentatively (parser);
3918 /* Consume the `('. */
3919 cp_lexer_consume_token (parser->lexer);
3920 /* Parse the type. */
3921 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3922 parser->in_type_id_in_expr_p = true;
3923 type = cp_parser_type_id (parser);
3924 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3925 /* Look for the `)'. */
3926 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3927 /* Look for the `{'. */
3928 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3929 /* If things aren't going well, there's no need to
3931 if (!cp_parser_error_occurred (parser))
3933 bool non_constant_p;
3934 /* Parse the initializer-list. */
3936 = cp_parser_initializer_list (parser, &non_constant_p);
3937 /* Allow a trailing `,'. */
3938 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3939 cp_lexer_consume_token (parser->lexer);
3940 /* Look for the final `}'. */
3941 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3943 /* If that worked, we're definitely looking at a
3944 compound-literal expression. */
3945 if (cp_parser_parse_definitely (parser))
3947 /* Warn the user that a compound literal is not
3948 allowed in standard C++. */
3950 pedwarn ("ISO C++ forbids compound-literals");
3951 /* Form the representation of the compound-literal. */
3953 = finish_compound_literal (type, initializer_list);
3958 /* It must be a primary-expression. */
3959 postfix_expression = cp_parser_primary_expression (parser,
3966 /* If we were avoiding committing to the processing of a
3967 qualified-id until we knew whether or not we had a
3968 pointer-to-member, we now know. */
3969 if (qualifying_class)
3973 /* Peek at the next token. */
3974 token = cp_lexer_peek_token (parser->lexer);
3975 done = (token->type != CPP_OPEN_SQUARE
3976 && token->type != CPP_OPEN_PAREN
3977 && token->type != CPP_DOT
3978 && token->type != CPP_DEREF
3979 && token->type != CPP_PLUS_PLUS
3980 && token->type != CPP_MINUS_MINUS);
3982 postfix_expression = finish_qualified_id_expr (qualifying_class,
3987 return postfix_expression;
3990 /* Keep looping until the postfix-expression is complete. */
3993 if (idk == CP_ID_KIND_UNQUALIFIED
3994 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3995 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3996 /* It is not a Koenig lookup function call. */
3998 = unqualified_name_lookup_error (postfix_expression);
4000 /* Peek at the next token. */
4001 token = cp_lexer_peek_token (parser->lexer);
4003 switch (token->type)
4005 case CPP_OPEN_SQUARE:
4007 = cp_parser_postfix_open_square_expression (parser,
4010 idk = CP_ID_KIND_NONE;
4013 case CPP_OPEN_PAREN:
4014 /* postfix-expression ( expression-list [opt] ) */
4017 tree args = (cp_parser_parenthesized_expression_list
4018 (parser, false, /*non_constant_p=*/NULL));
4020 if (args == error_mark_node)
4022 postfix_expression = error_mark_node;
4026 /* Function calls are not permitted in
4027 constant-expressions. */
4028 if (cp_parser_non_integral_constant_expression (parser,
4031 postfix_expression = error_mark_node;
4036 if (idk == CP_ID_KIND_UNQUALIFIED)
4038 /* We do not perform argument-dependent lookup if
4039 normal lookup finds a non-function, in accordance
4040 with the expected resolution of DR 218. */
4042 && (is_overloaded_fn (postfix_expression)
4043 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
4047 = perform_koenig_lookup (postfix_expression, args);
4049 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4051 = unqualified_fn_lookup_error (postfix_expression);
4054 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4056 tree instance = TREE_OPERAND (postfix_expression, 0);
4057 tree fn = TREE_OPERAND (postfix_expression, 1);
4059 if (processing_template_decl
4060 && (type_dependent_expression_p (instance)
4061 || (!BASELINK_P (fn)
4062 && TREE_CODE (fn) != FIELD_DECL)
4063 || type_dependent_expression_p (fn)
4064 || any_type_dependent_arguments_p (args)))
4067 = build_min_nt (CALL_EXPR, postfix_expression,
4072 if (BASELINK_P (fn))
4074 = (build_new_method_call
4075 (instance, fn, args, NULL_TREE,
4076 (idk == CP_ID_KIND_QUALIFIED
4077 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4080 = finish_call_expr (postfix_expression, args,
4081 /*disallow_virtual=*/false,
4082 /*koenig_p=*/false);
4084 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4085 || TREE_CODE (postfix_expression) == MEMBER_REF
4086 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4087 postfix_expression = (build_offset_ref_call_from_tree
4088 (postfix_expression, args));
4089 else if (idk == CP_ID_KIND_QUALIFIED)
4090 /* A call to a static class member, or a namespace-scope
4093 = finish_call_expr (postfix_expression, args,
4094 /*disallow_virtual=*/true,
4097 /* All other function calls. */
4099 = finish_call_expr (postfix_expression, args,
4100 /*disallow_virtual=*/false,
4103 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4104 idk = CP_ID_KIND_NONE;
4110 /* postfix-expression . template [opt] id-expression
4111 postfix-expression . pseudo-destructor-name
4112 postfix-expression -> template [opt] id-expression
4113 postfix-expression -> pseudo-destructor-name */
4115 /* Consume the `.' or `->' operator. */
4116 cp_lexer_consume_token (parser->lexer);
4119 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4125 /* postfix-expression ++ */
4126 /* Consume the `++' token. */
4127 cp_lexer_consume_token (parser->lexer);
4128 /* Generate a representation for the complete expression. */
4130 = finish_increment_expr (postfix_expression,
4131 POSTINCREMENT_EXPR);
4132 /* Increments may not appear in constant-expressions. */
4133 if (cp_parser_non_integral_constant_expression (parser,
4135 postfix_expression = error_mark_node;
4136 idk = CP_ID_KIND_NONE;
4139 case CPP_MINUS_MINUS:
4140 /* postfix-expression -- */
4141 /* Consume the `--' token. */
4142 cp_lexer_consume_token (parser->lexer);
4143 /* Generate a representation for the complete expression. */
4145 = finish_increment_expr (postfix_expression,
4146 POSTDECREMENT_EXPR);
4147 /* Decrements may not appear in constant-expressions. */
4148 if (cp_parser_non_integral_constant_expression (parser,
4150 postfix_expression = error_mark_node;
4151 idk = CP_ID_KIND_NONE;
4155 return postfix_expression;
4159 /* We should never get here. */
4161 return error_mark_node;
4164 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4165 by cp_parser_builtin_offsetof. We're looking for
4167 postfix-expression [ expression ]
4169 FOR_OFFSETOF is set if we're being called in that context, which
4170 changes how we deal with integer constant expressions. */
4173 cp_parser_postfix_open_square_expression (cp_parser *parser,
4174 tree postfix_expression,
4179 /* Consume the `[' token. */
4180 cp_lexer_consume_token (parser->lexer);
4182 /* Parse the index expression. */
4183 /* ??? For offsetof, there is a question of what to allow here. If
4184 offsetof is not being used in an integral constant expression context,
4185 then we *could* get the right answer by computing the value at runtime.
4186 If we are in an integral constant expression context, then we might
4187 could accept any constant expression; hard to say without analysis.
4188 Rather than open the barn door too wide right away, allow only integer
4189 constant expressions here. */
4191 index = cp_parser_constant_expression (parser, false, NULL);
4193 index = cp_parser_expression (parser);
4195 /* Look for the closing `]'. */
4196 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4198 /* Build the ARRAY_REF. */
4199 postfix_expression = grok_array_decl (postfix_expression, index);
4201 /* When not doing offsetof, array references are not permitted in
4202 constant-expressions. */
4204 && (cp_parser_non_integral_constant_expression
4205 (parser, "an array reference")))
4206 postfix_expression = error_mark_node;
4208 return postfix_expression;
4211 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4212 by cp_parser_builtin_offsetof. We're looking for
4214 postfix-expression . template [opt] id-expression
4215 postfix-expression . pseudo-destructor-name
4216 postfix-expression -> template [opt] id-expression
4217 postfix-expression -> pseudo-destructor-name
4219 FOR_OFFSETOF is set if we're being called in that context. That sorta
4220 limits what of the above we'll actually accept, but nevermind.
4221 TOKEN_TYPE is the "." or "->" token, which will already have been
4222 removed from the stream. */
4225 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4226 enum cpp_ttype token_type,
4227 tree postfix_expression,
4228 bool for_offsetof, cp_id_kind *idk)
4233 tree scope = NULL_TREE;
4235 /* If this is a `->' operator, dereference the pointer. */
4236 if (token_type == CPP_DEREF)
4237 postfix_expression = build_x_arrow (postfix_expression);
4238 /* Check to see whether or not the expression is type-dependent. */
4239 dependent_p = type_dependent_expression_p (postfix_expression);
4240 /* The identifier following the `->' or `.' is not qualified. */
4241 parser->scope = NULL_TREE;
4242 parser->qualifying_scope = NULL_TREE;
4243 parser->object_scope = NULL_TREE;
4244 *idk = CP_ID_KIND_NONE;
4245 /* Enter the scope corresponding to the type of the object
4246 given by the POSTFIX_EXPRESSION. */
4247 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4249 scope = TREE_TYPE (postfix_expression);
4250 /* According to the standard, no expression should ever have
4251 reference type. Unfortunately, we do not currently match
4252 the standard in this respect in that our internal representation
4253 of an expression may have reference type even when the standard
4254 says it does not. Therefore, we have to manually obtain the
4255 underlying type here. */
4256 scope = non_reference (scope);
4257 /* The type of the POSTFIX_EXPRESSION must be complete. */
4258 scope = complete_type_or_else (scope, NULL_TREE);
4259 /* Let the name lookup machinery know that we are processing a
4260 class member access expression. */
4261 parser->context->object_type = scope;
4262 /* If something went wrong, we want to be able to discern that case,
4263 as opposed to the case where there was no SCOPE due to the type
4264 of expression being dependent. */
4266 scope = error_mark_node;
4267 /* If the SCOPE was erroneous, make the various semantic analysis
4268 functions exit quickly -- and without issuing additional error
4270 if (scope == error_mark_node)
4271 postfix_expression = error_mark_node;
4274 /* If the SCOPE is not a scalar type, we are looking at an
4275 ordinary class member access expression, rather than a
4276 pseudo-destructor-name. */
4277 if (!scope || !SCALAR_TYPE_P (scope))
4279 template_p = cp_parser_optional_template_keyword (parser);
4280 /* Parse the id-expression. */
4281 name = cp_parser_id_expression (parser, template_p,
4282 /*check_dependency_p=*/true,
4283 /*template_p=*/NULL,
4284 /*declarator_p=*/false);
4285 /* In general, build a SCOPE_REF if the member name is qualified.
4286 However, if the name was not dependent and has already been
4287 resolved; there is no need to build the SCOPE_REF. For example;
4289 struct X { void f(); };
4290 template <typename T> void f(T* t) { t->X::f(); }
4292 Even though "t" is dependent, "X::f" is not and has been resolved
4293 to a BASELINK; there is no need to include scope information. */
4295 /* But we do need to remember that there was an explicit scope for
4296 virtual function calls. */
4298 *idk = CP_ID_KIND_QUALIFIED;
4300 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4302 name = build_nt (SCOPE_REF, parser->scope, name);
4303 parser->scope = NULL_TREE;
4304 parser->qualifying_scope = NULL_TREE;
4305 parser->object_scope = NULL_TREE;
4307 if (scope && name && BASELINK_P (name))
4308 adjust_result_of_qualified_name_lookup
4309 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4311 = finish_class_member_access_expr (postfix_expression, name);
4313 /* Otherwise, try the pseudo-destructor-name production. */
4319 /* Parse the pseudo-destructor-name. */
4320 cp_parser_pseudo_destructor_name (parser, &s, &type);
4321 /* Form the call. */
4323 = finish_pseudo_destructor_expr (postfix_expression,
4324 s, TREE_TYPE (type));
4327 /* We no longer need to look up names in the scope of the object on
4328 the left-hand side of the `.' or `->' operator. */
4329 parser->context->object_type = NULL_TREE;
4331 /* Outside of offsetof, these operators may not appear in
4332 constant-expressions. */
4334 && (cp_parser_non_integral_constant_expression
4335 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4336 postfix_expression = error_mark_node;
4338 return postfix_expression;
4341 /* Parse a parenthesized expression-list.
4344 assignment-expression
4345 expression-list, assignment-expression
4350 identifier, expression-list
4352 Returns a TREE_LIST. The TREE_VALUE of each node is a
4353 representation of an assignment-expression. Note that a TREE_LIST
4354 is returned even if there is only a single expression in the list.
4355 error_mark_node is returned if the ( and or ) are
4356 missing. NULL_TREE is returned on no expressions. The parentheses
4357 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4358 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4359 indicates whether or not all of the expressions in the list were
4363 cp_parser_parenthesized_expression_list (cp_parser* parser,
4364 bool is_attribute_list,
4365 bool *non_constant_p)
4367 tree expression_list = NULL_TREE;
4368 tree identifier = NULL_TREE;
4370 /* Assume all the expressions will be constant. */
4372 *non_constant_p = false;
4374 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4375 return error_mark_node;
4377 /* Consume expressions until there are no more. */
4378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4383 /* At the beginning of attribute lists, check to see if the
4384 next token is an identifier. */
4385 if (is_attribute_list
4386 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4390 /* Consume the identifier. */
4391 token = cp_lexer_consume_token (parser->lexer);
4392 /* Save the identifier. */
4393 identifier = token->value;
4397 /* Parse the next assignment-expression. */
4400 bool expr_non_constant_p;
4401 expr = (cp_parser_constant_expression
4402 (parser, /*allow_non_constant_p=*/true,
4403 &expr_non_constant_p));
4404 if (expr_non_constant_p)
4405 *non_constant_p = true;
4408 expr = cp_parser_assignment_expression (parser);
4410 /* Add it to the list. We add error_mark_node
4411 expressions to the list, so that we can still tell if
4412 the correct form for a parenthesized expression-list
4413 is found. That gives better errors. */
4414 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4416 if (expr == error_mark_node)
4420 /* After the first item, attribute lists look the same as
4421 expression lists. */
4422 is_attribute_list = false;
4425 /* If the next token isn't a `,', then we are done. */
4426 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4429 /* Otherwise, consume the `,' and keep going. */
4430 cp_lexer_consume_token (parser->lexer);
4433 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4438 /* We try and resync to an unnested comma, as that will give the
4439 user better diagnostics. */
4440 ending = cp_parser_skip_to_closing_parenthesis (parser,
4441 /*recovering=*/true,
4443 /*consume_paren=*/true);
4447 return error_mark_node;
4450 /* We built up the list in reverse order so we must reverse it now. */
4451 expression_list = nreverse (expression_list);
4453 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4455 return expression_list;
4458 /* Parse a pseudo-destructor-name.
4460 pseudo-destructor-name:
4461 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4462 :: [opt] nested-name-specifier template template-id :: ~ type-name
4463 :: [opt] nested-name-specifier [opt] ~ type-name
4465 If either of the first two productions is used, sets *SCOPE to the
4466 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4467 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4468 or ERROR_MARK_NODE if the parse fails. */
4471 cp_parser_pseudo_destructor_name (cp_parser* parser,
4475 bool nested_name_specifier_p;
4477 /* Assume that things will not work out. */
4478 *type = error_mark_node;
4480 /* Look for the optional `::' operator. */
4481 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4482 /* Look for the optional nested-name-specifier. */
4483 nested_name_specifier_p
4484 = (cp_parser_nested_name_specifier_opt (parser,
4485 /*typename_keyword_p=*/false,
4486 /*check_dependency_p=*/true,
4488 /*is_declaration=*/true)
4490 /* Now, if we saw a nested-name-specifier, we might be doing the
4491 second production. */
4492 if (nested_name_specifier_p
4493 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4495 /* Consume the `template' keyword. */
4496 cp_lexer_consume_token (parser->lexer);
4497 /* Parse the template-id. */
4498 cp_parser_template_id (parser,
4499 /*template_keyword_p=*/true,
4500 /*check_dependency_p=*/false,
4501 /*is_declaration=*/true);
4502 /* Look for the `::' token. */
4503 cp_parser_require (parser, CPP_SCOPE, "`::'");
4505 /* If the next token is not a `~', then there might be some
4506 additional qualification. */
4507 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4509 /* Look for the type-name. */
4510 *scope = TREE_TYPE (cp_parser_type_name (parser));
4512 if (*scope == error_mark_node)
4515 /* If we don't have ::~, then something has gone wrong. Since
4516 the only caller of this function is looking for something
4517 after `.' or `->' after a scalar type, most likely the
4518 program is trying to get a member of a non-aggregate
4520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4521 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4523 cp_parser_error (parser, "request for member of non-aggregate type");
4527 /* Look for the `::' token. */
4528 cp_parser_require (parser, CPP_SCOPE, "`::'");
4533 /* Look for the `~'. */
4534 cp_parser_require (parser, CPP_COMPL, "`~'");
4535 /* Look for the type-name again. We are not responsible for
4536 checking that it matches the first type-name. */
4537 *type = cp_parser_type_name (parser);
4540 /* Parse a unary-expression.
4546 unary-operator cast-expression
4547 sizeof unary-expression
4555 __extension__ cast-expression
4556 __alignof__ unary-expression
4557 __alignof__ ( type-id )
4558 __real__ cast-expression
4559 __imag__ cast-expression
4562 ADDRESS_P is true iff the unary-expression is appearing as the
4563 operand of the `&' operator.
4565 Returns a representation of the expression. */
4568 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4571 enum tree_code unary_operator;
4573 /* Peek at the next token. */
4574 token = cp_lexer_peek_token (parser->lexer);
4575 /* Some keywords give away the kind of expression. */
4576 if (token->type == CPP_KEYWORD)
4578 enum rid keyword = token->keyword;
4588 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4589 /* Consume the token. */
4590 cp_lexer_consume_token (parser->lexer);
4591 /* Parse the operand. */
4592 operand = cp_parser_sizeof_operand (parser, keyword);
4594 if (TYPE_P (operand))
4595 return cxx_sizeof_or_alignof_type (operand, op, true);
4597 return cxx_sizeof_or_alignof_expr (operand, op);
4601 return cp_parser_new_expression (parser);
4604 return cp_parser_delete_expression (parser);
4608 /* The saved value of the PEDANTIC flag. */
4612 /* Save away the PEDANTIC flag. */
4613 cp_parser_extension_opt (parser, &saved_pedantic);
4614 /* Parse the cast-expression. */
4615 expr = cp_parser_simple_cast_expression (parser);
4616 /* Restore the PEDANTIC flag. */
4617 pedantic = saved_pedantic;
4627 /* Consume the `__real__' or `__imag__' token. */
4628 cp_lexer_consume_token (parser->lexer);
4629 /* Parse the cast-expression. */
4630 expression = cp_parser_simple_cast_expression (parser);
4631 /* Create the complete representation. */
4632 return build_x_unary_op ((keyword == RID_REALPART
4633 ? REALPART_EXPR : IMAGPART_EXPR),
4643 /* Look for the `:: new' and `:: delete', which also signal the
4644 beginning of a new-expression, or delete-expression,
4645 respectively. If the next token is `::', then it might be one of
4647 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4651 /* See if the token after the `::' is one of the keywords in
4652 which we're interested. */
4653 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4654 /* If it's `new', we have a new-expression. */
4655 if (keyword == RID_NEW)
4656 return cp_parser_new_expression (parser);
4657 /* Similarly, for `delete'. */
4658 else if (keyword == RID_DELETE)
4659 return cp_parser_delete_expression (parser);
4662 /* Look for a unary operator. */
4663 unary_operator = cp_parser_unary_operator (token);
4664 /* The `++' and `--' operators can be handled similarly, even though
4665 they are not technically unary-operators in the grammar. */
4666 if (unary_operator == ERROR_MARK)
4668 if (token->type == CPP_PLUS_PLUS)
4669 unary_operator = PREINCREMENT_EXPR;
4670 else if (token->type == CPP_MINUS_MINUS)
4671 unary_operator = PREDECREMENT_EXPR;
4672 /* Handle the GNU address-of-label extension. */
4673 else if (cp_parser_allow_gnu_extensions_p (parser)
4674 && token->type == CPP_AND_AND)
4678 /* Consume the '&&' token. */
4679 cp_lexer_consume_token (parser->lexer);
4680 /* Look for the identifier. */
4681 identifier = cp_parser_identifier (parser);
4682 /* Create an expression representing the address. */
4683 return finish_label_address_expr (identifier);
4686 if (unary_operator != ERROR_MARK)
4688 tree cast_expression;
4689 tree expression = error_mark_node;
4690 const char *non_constant_p = NULL;
4692 /* Consume the operator token. */
4693 token = cp_lexer_consume_token (parser->lexer);
4694 /* Parse the cast-expression. */
4696 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4697 /* Now, build an appropriate representation. */
4698 switch (unary_operator)
4701 non_constant_p = "`*'";
4702 expression = build_x_indirect_ref (cast_expression, "unary *");
4706 non_constant_p = "`&'";
4709 expression = build_x_unary_op (unary_operator, cast_expression);
4712 case PREINCREMENT_EXPR:
4713 case PREDECREMENT_EXPR:
4714 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4719 case TRUTH_NOT_EXPR:
4720 expression = finish_unary_op_expr (unary_operator, cast_expression);
4728 && cp_parser_non_integral_constant_expression (parser,
4730 expression = error_mark_node;
4735 return cp_parser_postfix_expression (parser, address_p);
4738 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4739 unary-operator, the corresponding tree code is returned. */
4741 static enum tree_code
4742 cp_parser_unary_operator (cp_token* token)
4744 switch (token->type)
4747 return INDIRECT_REF;
4753 return CONVERT_EXPR;
4759 return TRUTH_NOT_EXPR;
4762 return BIT_NOT_EXPR;
4769 /* Parse a new-expression.
4772 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4773 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4775 Returns a representation of the expression. */
4778 cp_parser_new_expression (cp_parser* parser)
4780 bool global_scope_p;
4786 /* Look for the optional `::' operator. */
4788 = (cp_parser_global_scope_opt (parser,
4789 /*current_scope_valid_p=*/false)
4791 /* Look for the `new' operator. */
4792 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4793 /* There's no easy way to tell a new-placement from the
4794 `( type-id )' construct. */
4795 cp_parser_parse_tentatively (parser);
4796 /* Look for a new-placement. */
4797 placement = cp_parser_new_placement (parser);
4798 /* If that didn't work out, there's no new-placement. */
4799 if (!cp_parser_parse_definitely (parser))
4800 placement = NULL_TREE;
4802 /* If the next token is a `(', then we have a parenthesized
4804 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4806 /* Consume the `('. */
4807 cp_lexer_consume_token (parser->lexer);
4808 /* Parse the type-id. */
4809 type = cp_parser_type_id (parser);
4810 /* Look for the closing `)'. */
4811 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4812 /* There should not be a direct-new-declarator in this production,
4813 but GCC used to allowed this, so we check and emit a sensible error
4814 message for this case. */
4815 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4817 error ("array bound forbidden after parenthesized type-id");
4818 inform ("try removing the parentheses around the type-id");
4819 cp_parser_direct_new_declarator (parser);
4821 nelts = integer_one_node;
4823 /* Otherwise, there must be a new-type-id. */
4825 type = cp_parser_new_type_id (parser, &nelts);
4827 /* If the next token is a `(', then we have a new-initializer. */
4828 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4829 initializer = cp_parser_new_initializer (parser);
4831 initializer = NULL_TREE;
4833 /* A new-expression may not appear in an integral constant
4835 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4836 return error_mark_node;
4838 /* Create a representation of the new-expression. */
4839 return build_new (placement, type, nelts, initializer, global_scope_p);
4842 /* Parse a new-placement.
4847 Returns the same representation as for an expression-list. */
4850 cp_parser_new_placement (cp_parser* parser)
4852 tree expression_list;
4854 /* Parse the expression-list. */
4855 expression_list = (cp_parser_parenthesized_expression_list
4856 (parser, false, /*non_constant_p=*/NULL));
4858 return expression_list;
4861 /* Parse a new-type-id.
4864 type-specifier-seq new-declarator [opt]
4866 Returns the TYPE allocated. If the new-type-id indicates an array
4867 type, *NELTS is set to the number of elements in the last array
4868 bound; the TYPE will not include the last array bound. */
4871 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4873 cp_decl_specifier_seq type_specifier_seq;
4874 cp_declarator *new_declarator;
4875 cp_declarator *declarator;
4876 cp_declarator *outer_declarator;
4877 const char *saved_message;
4880 /* The type-specifier sequence must not contain type definitions.
4881 (It cannot contain declarations of new types either, but if they
4882 are not definitions we will catch that because they are not
4884 saved_message = parser->type_definition_forbidden_message;
4885 parser->type_definition_forbidden_message
4886 = "types may not be defined in a new-type-id";
4887 /* Parse the type-specifier-seq. */
4888 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4889 /* Restore the old message. */
4890 parser->type_definition_forbidden_message = saved_message;
4891 /* Parse the new-declarator. */
4892 new_declarator = cp_parser_new_declarator_opt (parser);
4894 /* Determine the number of elements in the last array dimension, if
4897 /* Skip down to the last array dimension. */
4898 declarator = new_declarator;
4899 outer_declarator = NULL;
4900 while (declarator && (declarator->kind == cdk_pointer
4901 || declarator->kind == cdk_ptrmem))
4903 outer_declarator = declarator;
4904 declarator = declarator->declarator;
4907 && declarator->kind == cdk_array
4908 && declarator->declarator
4909 && declarator->declarator->kind == cdk_array)
4911 outer_declarator = declarator;
4912 declarator = declarator->declarator;
4915 if (declarator && declarator->kind == cdk_array)
4917 *nelts = declarator->u.array.bounds;
4918 if (*nelts == error_mark_node)
4919 *nelts = integer_one_node;
4920 else if (!processing_template_decl)
4922 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4924 pedwarn ("size in array new must have integral type");
4925 *nelts = save_expr (cp_convert (sizetype, *nelts));
4926 if (*nelts == integer_zero_node)
4927 warning ("zero size array reserves no space");
4929 if (outer_declarator)
4930 outer_declarator->declarator = declarator->declarator;
4932 new_declarator = NULL;
4935 type = groktypename (&type_specifier_seq, new_declarator);
4936 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4938 *nelts = array_type_nelts_top (type);
4939 type = TREE_TYPE (type);
4944 /* Parse an (optional) new-declarator.
4947 ptr-operator new-declarator [opt]
4948 direct-new-declarator
4950 Returns the declarator. */
4952 static cp_declarator *
4953 cp_parser_new_declarator_opt (cp_parser* parser)
4955 enum tree_code code;
4957 cp_cv_quals cv_quals;
4959 /* We don't know if there's a ptr-operator next, or not. */
4960 cp_parser_parse_tentatively (parser);
4961 /* Look for a ptr-operator. */
4962 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4963 /* If that worked, look for more new-declarators. */
4964 if (cp_parser_parse_definitely (parser))
4966 cp_declarator *declarator;
4968 /* Parse another optional declarator. */
4969 declarator = cp_parser_new_declarator_opt (parser);
4971 /* Create the representation of the declarator. */
4973 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4974 else if (code == INDIRECT_REF)
4975 declarator = make_pointer_declarator (cv_quals, declarator);
4977 declarator = make_reference_declarator (cv_quals, declarator);
4982 /* If the next token is a `[', there is a direct-new-declarator. */
4983 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4984 return cp_parser_direct_new_declarator (parser);
4989 /* Parse a direct-new-declarator.
4991 direct-new-declarator:
4993 direct-new-declarator [constant-expression]
4997 static cp_declarator *
4998 cp_parser_direct_new_declarator (cp_parser* parser)
5000 cp_declarator *declarator = NULL;
5006 /* Look for the opening `['. */
5007 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5008 /* The first expression is not required to be constant. */
5011 expression = cp_parser_expression (parser);
5012 /* The standard requires that the expression have integral
5013 type. DR 74 adds enumeration types. We believe that the
5014 real intent is that these expressions be handled like the
5015 expression in a `switch' condition, which also allows
5016 classes with a single conversion to integral or
5017 enumeration type. */
5018 if (!processing_template_decl)
5021 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5026 error ("expression in new-declarator must have integral or enumeration type");
5027 expression = error_mark_node;
5031 /* But all the other expressions must be. */
5034 = cp_parser_constant_expression (parser,
5035 /*allow_non_constant=*/false,
5037 /* Look for the closing `]'. */
5038 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5040 /* Add this bound to the declarator. */
5041 declarator = make_array_declarator (declarator, expression);
5043 /* If the next token is not a `[', then there are no more
5045 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5052 /* Parse a new-initializer.
5055 ( expression-list [opt] )
5057 Returns a representation of the expression-list. If there is no
5058 expression-list, VOID_ZERO_NODE is returned. */
5061 cp_parser_new_initializer (cp_parser* parser)
5063 tree expression_list;
5065 expression_list = (cp_parser_parenthesized_expression_list
5066 (parser, false, /*non_constant_p=*/NULL));
5067 if (!expression_list)
5068 expression_list = void_zero_node;
5070 return expression_list;
5073 /* Parse a delete-expression.
5076 :: [opt] delete cast-expression
5077 :: [opt] delete [ ] cast-expression
5079 Returns a representation of the expression. */
5082 cp_parser_delete_expression (cp_parser* parser)
5084 bool global_scope_p;
5088 /* Look for the optional `::' operator. */
5090 = (cp_parser_global_scope_opt (parser,
5091 /*current_scope_valid_p=*/false)
5093 /* Look for the `delete' keyword. */
5094 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5095 /* See if the array syntax is in use. */
5096 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5098 /* Consume the `[' token. */
5099 cp_lexer_consume_token (parser->lexer);
5100 /* Look for the `]' token. */
5101 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5102 /* Remember that this is the `[]' construct. */
5108 /* Parse the cast-expression. */
5109 expression = cp_parser_simple_cast_expression (parser);
5111 /* A delete-expression may not appear in an integral constant
5113 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5114 return error_mark_node;
5116 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5119 /* Parse a cast-expression.
5123 ( type-id ) cast-expression
5125 Returns a representation of the expression. */
5128 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5130 /* If it's a `(', then we might be looking at a cast. */
5131 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5133 tree type = NULL_TREE;
5134 tree expr = NULL_TREE;
5135 bool compound_literal_p;
5136 const char *saved_message;
5138 /* There's no way to know yet whether or not this is a cast.
5139 For example, `(int (3))' is a unary-expression, while `(int)
5140 3' is a cast. So, we resort to parsing tentatively. */
5141 cp_parser_parse_tentatively (parser);
5142 /* Types may not be defined in a cast. */
5143 saved_message = parser->type_definition_forbidden_message;
5144 parser->type_definition_forbidden_message
5145 = "types may not be defined in casts";
5146 /* Consume the `('. */
5147 cp_lexer_consume_token (parser->lexer);
5148 /* A very tricky bit is that `(struct S) { 3 }' is a
5149 compound-literal (which we permit in C++ as an extension).
5150 But, that construct is not a cast-expression -- it is a
5151 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5152 is legal; if the compound-literal were a cast-expression,
5153 you'd need an extra set of parentheses.) But, if we parse
5154 the type-id, and it happens to be a class-specifier, then we
5155 will commit to the parse at that point, because we cannot
5156 undo the action that is done when creating a new class. So,
5157 then we cannot back up and do a postfix-expression.
5159 Therefore, we scan ahead to the closing `)', and check to see
5160 if the token after the `)' is a `{'. If so, we are not
5161 looking at a cast-expression.
5163 Save tokens so that we can put them back. */
5164 cp_lexer_save_tokens (parser->lexer);
5165 /* Skip tokens until the next token is a closing parenthesis.
5166 If we find the closing `)', and the next token is a `{', then
5167 we are looking at a compound-literal. */
5169 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5170 /*consume_paren=*/true)
5171 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5172 /* Roll back the tokens we skipped. */
5173 cp_lexer_rollback_tokens (parser->lexer);
5174 /* If we were looking at a compound-literal, simulate an error
5175 so that the call to cp_parser_parse_definitely below will
5177 if (compound_literal_p)
5178 cp_parser_simulate_error (parser);
5181 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5182 parser->in_type_id_in_expr_p = true;
5183 /* Look for the type-id. */
5184 type = cp_parser_type_id (parser);
5185 /* Look for the closing `)'. */
5186 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5187 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5190 /* Restore the saved message. */
5191 parser->type_definition_forbidden_message = saved_message;
5193 /* If ok so far, parse the dependent expression. We cannot be
5194 sure it is a cast. Consider `(T ())'. It is a parenthesized
5195 ctor of T, but looks like a cast to function returning T
5196 without a dependent expression. */
5197 if (!cp_parser_error_occurred (parser))
5198 expr = cp_parser_simple_cast_expression (parser);
5200 if (cp_parser_parse_definitely (parser))
5202 /* Warn about old-style casts, if so requested. */
5203 if (warn_old_style_cast
5204 && !in_system_header
5205 && !VOID_TYPE_P (type)
5206 && current_lang_name != lang_name_c)
5207 warning ("use of old-style cast");
5209 /* Only type conversions to integral or enumeration types
5210 can be used in constant-expressions. */
5211 if (parser->integral_constant_expression_p
5212 && !dependent_type_p (type)
5213 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5214 && (cp_parser_non_integral_constant_expression
5216 "a cast to a type other than an integral or "
5217 "enumeration type")))
5218 return error_mark_node;
5220 /* Perform the cast. */
5221 expr = build_c_cast (type, expr);
5226 /* If we get here, then it's not a cast, so it must be a
5227 unary-expression. */
5228 return cp_parser_unary_expression (parser, address_p);
5231 /* Parse a binary expression of the general form:
5235 pm-expression .* cast-expression
5236 pm-expression ->* cast-expression
5238 multiplicative-expression:
5240 multiplicative-expression * pm-expression
5241 multiplicative-expression / pm-expression
5242 multiplicative-expression % pm-expression
5244 additive-expression:
5245 multiplicative-expression
5246 additive-expression + multiplicative-expression
5247 additive-expression - multiplicative-expression
5251 shift-expression << additive-expression
5252 shift-expression >> additive-expression
5254 relational-expression:
5256 relational-expression < shift-expression
5257 relational-expression > shift-expression
5258 relational-expression <= shift-expression
5259 relational-expression >= shift-expression
5263 relational-expression:
5264 relational-expression <? shift-expression
5265 relational-expression >? shift-expression
5267 equality-expression:
5268 relational-expression
5269 equality-expression == relational-expression
5270 equality-expression != relational-expression
5274 and-expression & equality-expression
5276 exclusive-or-expression:
5278 exclusive-or-expression ^ and-expression
5280 inclusive-or-expression:
5281 exclusive-or-expression
5282 inclusive-or-expression | exclusive-or-expression
5284 logical-and-expression:
5285 inclusive-or-expression
5286 logical-and-expression && inclusive-or-expression
5288 logical-or-expression:
5289 logical-and-expression
5290 logical-or-expression || logical-and-expression
5292 All these are implemented with a single function like:
5295 simple-cast-expression
5296 binary-expression <token> binary-expression
5298 The binops_by_token map is used to get the tree codes for each <token> type.
5299 binary-expressions are associated according to a precedence table. */
5301 #define TOKEN_PRECEDENCE(token) \
5302 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5303 ? PREC_NOT_OPERATOR \
5304 : binops_by_token[token->type].prec)
5307 cp_parser_binary_expression (cp_parser* parser)
5309 cp_parser_expression_stack stack;
5310 cp_parser_expression_stack_entry *sp = &stack[0];
5313 enum tree_code tree_type;
5314 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5317 /* Parse the first expression. */
5318 lhs = cp_parser_simple_cast_expression (parser);
5322 /* Get an operator token. */
5323 token = cp_lexer_peek_token (parser->lexer);
5324 new_prec = TOKEN_PRECEDENCE (token);
5326 /* Popping an entry off the stack means we completed a subexpression:
5327 - either we found a token which is not an operator (`>' where it is not
5328 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5329 will happen repeatedly;
5330 - or, we found an operator which has lower priority. This is the case
5331 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5333 if (new_prec <= prec)
5342 tree_type = binops_by_token[token->type].tree_type;
5344 /* We used the operator token. */
5345 cp_lexer_consume_token (parser->lexer);
5347 /* Extract another operand. It may be the RHS of this expression
5348 or the LHS of a new, higher priority expression. */
5349 rhs = cp_parser_simple_cast_expression (parser);
5351 /* Get another operator token. Look up its precedence to avoid
5352 building a useless (immediately popped) stack entry for common
5353 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5354 token = cp_lexer_peek_token (parser->lexer);
5355 lookahead_prec = TOKEN_PRECEDENCE (token);
5356 if (lookahead_prec > new_prec)
5358 /* ... and prepare to parse the RHS of the new, higher priority
5361 sp->tree_type = tree_type;
5366 new_prec = lookahead_prec;
5370 /* If the stack is not empty, we have parsed into LHS the right side
5371 (`4' in the example above) of an expression we had suspended.
5372 We can use the information on the stack to recover the LHS (`3')
5373 from the stack together with the tree code (`MULT_EXPR'), and
5374 the precedence of the higher level subexpression
5375 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5376 which will be used to actually build the additive expression. */
5379 tree_type = sp->tree_type;
5384 overloaded_p = false;
5385 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5387 /* If the binary operator required the use of an overloaded operator,
5388 then this expression cannot be an integral constant-expression.
5389 An overloaded operator can be used even if both operands are
5390 otherwise permissible in an integral constant-expression if at
5391 least one of the operands is of enumeration type. */
5394 && (cp_parser_non_integral_constant_expression
5395 (parser, "calls to overloaded operators")))
5396 return error_mark_node;
5403 /* Parse the `? expression : assignment-expression' part of a
5404 conditional-expression. The LOGICAL_OR_EXPR is the
5405 logical-or-expression that started the conditional-expression.
5406 Returns a representation of the entire conditional-expression.
5408 This routine is used by cp_parser_assignment_expression.
5410 ? expression : assignment-expression
5414 ? : assignment-expression */
5417 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5420 tree assignment_expr;
5422 /* Consume the `?' token. */
5423 cp_lexer_consume_token (parser->lexer);
5424 if (cp_parser_allow_gnu_extensions_p (parser)
5425 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5426 /* Implicit true clause. */
5429 /* Parse the expression. */
5430 expr = cp_parser_expression (parser);
5432 /* The next token should be a `:'. */
5433 cp_parser_require (parser, CPP_COLON, "`:'");
5434 /* Parse the assignment-expression. */
5435 assignment_expr = cp_parser_assignment_expression (parser);
5437 /* Build the conditional-expression. */
5438 return build_x_conditional_expr (logical_or_expr,
5443 /* Parse an assignment-expression.
5445 assignment-expression:
5446 conditional-expression
5447 logical-or-expression assignment-operator assignment_expression
5450 Returns a representation for the expression. */
5453 cp_parser_assignment_expression (cp_parser* parser)
5457 /* If the next token is the `throw' keyword, then we're looking at
5458 a throw-expression. */
5459 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5460 expr = cp_parser_throw_expression (parser);
5461 /* Otherwise, it must be that we are looking at a
5462 logical-or-expression. */
5465 /* Parse the binary expressions (logical-or-expression). */
5466 expr = cp_parser_binary_expression (parser);
5467 /* If the next token is a `?' then we're actually looking at a
5468 conditional-expression. */
5469 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5470 return cp_parser_question_colon_clause (parser, expr);
5473 enum tree_code assignment_operator;
5475 /* If it's an assignment-operator, we're using the second
5478 = cp_parser_assignment_operator_opt (parser);
5479 if (assignment_operator != ERROR_MARK)
5483 /* Parse the right-hand side of the assignment. */
5484 rhs = cp_parser_assignment_expression (parser);
5485 /* An assignment may not appear in a
5486 constant-expression. */
5487 if (cp_parser_non_integral_constant_expression (parser,
5489 return error_mark_node;
5490 /* Build the assignment expression. */
5491 expr = build_x_modify_expr (expr,
5492 assignment_operator,
5501 /* Parse an (optional) assignment-operator.
5503 assignment-operator: one of
5504 = *= /= %= += -= >>= <<= &= ^= |=
5508 assignment-operator: one of
5511 If the next token is an assignment operator, the corresponding tree
5512 code is returned, and the token is consumed. For example, for
5513 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5514 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5515 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5516 operator, ERROR_MARK is returned. */
5518 static enum tree_code
5519 cp_parser_assignment_operator_opt (cp_parser* parser)
5524 /* Peek at the next toen. */
5525 token = cp_lexer_peek_token (parser->lexer);
5527 switch (token->type)
5538 op = TRUNC_DIV_EXPR;
5542 op = TRUNC_MOD_EXPR;
5582 /* Nothing else is an assignment operator. */
5586 /* If it was an assignment operator, consume it. */
5587 if (op != ERROR_MARK)
5588 cp_lexer_consume_token (parser->lexer);
5593 /* Parse an expression.
5596 assignment-expression
5597 expression , assignment-expression
5599 Returns a representation of the expression. */
5602 cp_parser_expression (cp_parser* parser)
5604 tree expression = NULL_TREE;
5608 tree assignment_expression;
5610 /* Parse the next assignment-expression. */
5611 assignment_expression
5612 = cp_parser_assignment_expression (parser);
5613 /* If this is the first assignment-expression, we can just
5616 expression = assignment_expression;
5618 expression = build_x_compound_expr (expression,
5619 assignment_expression);
5620 /* If the next token is not a comma, then we are done with the
5622 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5624 /* Consume the `,'. */
5625 cp_lexer_consume_token (parser->lexer);
5626 /* A comma operator cannot appear in a constant-expression. */
5627 if (cp_parser_non_integral_constant_expression (parser,
5628 "a comma operator"))
5629 expression = error_mark_node;
5635 /* Parse a constant-expression.
5637 constant-expression:
5638 conditional-expression
5640 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5641 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5642 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5643 is false, NON_CONSTANT_P should be NULL. */
5646 cp_parser_constant_expression (cp_parser* parser,
5647 bool allow_non_constant_p,
5648 bool *non_constant_p)
5650 bool saved_integral_constant_expression_p;
5651 bool saved_allow_non_integral_constant_expression_p;
5652 bool saved_non_integral_constant_expression_p;
5655 /* It might seem that we could simply parse the
5656 conditional-expression, and then check to see if it were
5657 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5658 one that the compiler can figure out is constant, possibly after
5659 doing some simplifications or optimizations. The standard has a
5660 precise definition of constant-expression, and we must honor
5661 that, even though it is somewhat more restrictive.
5667 is not a legal declaration, because `(2, 3)' is not a
5668 constant-expression. The `,' operator is forbidden in a
5669 constant-expression. However, GCC's constant-folding machinery
5670 will fold this operation to an INTEGER_CST for `3'. */
5672 /* Save the old settings. */
5673 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5674 saved_allow_non_integral_constant_expression_p
5675 = parser->allow_non_integral_constant_expression_p;
5676 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5677 /* We are now parsing a constant-expression. */
5678 parser->integral_constant_expression_p = true;
5679 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5680 parser->non_integral_constant_expression_p = false;
5681 /* Although the grammar says "conditional-expression", we parse an
5682 "assignment-expression", which also permits "throw-expression"
5683 and the use of assignment operators. In the case that
5684 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5685 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5686 actually essential that we look for an assignment-expression.
5687 For example, cp_parser_initializer_clauses uses this function to
5688 determine whether a particular assignment-expression is in fact
5690 expression = cp_parser_assignment_expression (parser);
5691 /* Restore the old settings. */
5692 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5693 parser->allow_non_integral_constant_expression_p
5694 = saved_allow_non_integral_constant_expression_p;
5695 if (allow_non_constant_p)
5696 *non_constant_p = parser->non_integral_constant_expression_p;
5697 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5702 /* Parse __builtin_offsetof.
5704 offsetof-expression:
5705 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5707 offsetof-member-designator:
5709 | offsetof-member-designator "." id-expression
5710 | offsetof-member-designator "[" expression "]"
5714 cp_parser_builtin_offsetof (cp_parser *parser)
5716 int save_ice_p, save_non_ice_p;
5720 /* We're about to accept non-integral-constant things, but will
5721 definitely yield an integral constant expression. Save and
5722 restore these values around our local parsing. */
5723 save_ice_p = parser->integral_constant_expression_p;
5724 save_non_ice_p = parser->non_integral_constant_expression_p;
5726 /* Consume the "__builtin_offsetof" token. */
5727 cp_lexer_consume_token (parser->lexer);
5728 /* Consume the opening `('. */
5729 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5730 /* Parse the type-id. */
5731 type = cp_parser_type_id (parser);
5732 /* Look for the `,'. */
5733 cp_parser_require (parser, CPP_COMMA, "`,'");
5735 /* Build the (type *)null that begins the traditional offsetof macro. */
5736 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5738 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5739 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5743 cp_token *token = cp_lexer_peek_token (parser->lexer);
5744 switch (token->type)
5746 case CPP_OPEN_SQUARE:
5747 /* offsetof-member-designator "[" expression "]" */
5748 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5752 /* offsetof-member-designator "." identifier */
5753 cp_lexer_consume_token (parser->lexer);
5754 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5758 case CPP_CLOSE_PAREN:
5759 /* Consume the ")" token. */
5760 cp_lexer_consume_token (parser->lexer);
5764 /* Error. We know the following require will fail, but
5765 that gives the proper error message. */
5766 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5767 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5768 expr = error_mark_node;
5774 /* If we're processing a template, we can't finish the semantics yet.
5775 Otherwise we can fold the entire expression now. */
5776 if (processing_template_decl)
5777 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5779 expr = fold_offsetof (expr);
5782 parser->integral_constant_expression_p = save_ice_p;
5783 parser->non_integral_constant_expression_p = save_non_ice_p;
5788 /* Statements [gram.stmt.stmt] */
5790 /* Parse a statement.
5794 expression-statement
5799 declaration-statement
5803 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5807 location_t statement_location;
5809 /* There is no statement yet. */
5810 statement = NULL_TREE;
5811 /* Peek at the next token. */
5812 token = cp_lexer_peek_token (parser->lexer);
5813 /* Remember the location of the first token in the statement. */
5814 statement_location = token->location;
5815 /* If this is a keyword, then that will often determine what kind of
5816 statement we have. */
5817 if (token->type == CPP_KEYWORD)
5819 enum rid keyword = token->keyword;
5825 statement = cp_parser_labeled_statement (parser,
5831 statement = cp_parser_selection_statement (parser);
5837 statement = cp_parser_iteration_statement (parser);
5844 statement = cp_parser_jump_statement (parser);
5848 statement = cp_parser_try_block (parser);
5852 /* It might be a keyword like `int' that can start a
5853 declaration-statement. */
5857 else if (token->type == CPP_NAME)
5859 /* If the next token is a `:', then we are looking at a
5860 labeled-statement. */
5861 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5862 if (token->type == CPP_COLON)
5863 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5865 /* Anything that starts with a `{' must be a compound-statement. */
5866 else if (token->type == CPP_OPEN_BRACE)
5867 statement = cp_parser_compound_statement (parser, NULL, false);
5868 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5869 a statement all its own. */
5870 else if (token->type == CPP_PRAGMA)
5872 cp_lexer_handle_pragma (parser->lexer);
5876 /* Everything else must be a declaration-statement or an
5877 expression-statement. Try for the declaration-statement
5878 first, unless we are looking at a `;', in which case we know that
5879 we have an expression-statement. */
5882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5884 cp_parser_parse_tentatively (parser);
5885 /* Try to parse the declaration-statement. */
5886 cp_parser_declaration_statement (parser);
5887 /* If that worked, we're done. */
5888 if (cp_parser_parse_definitely (parser))
5891 /* Look for an expression-statement instead. */
5892 statement = cp_parser_expression_statement (parser, in_statement_expr);
5895 /* Set the line number for the statement. */
5896 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5897 SET_EXPR_LOCATION (statement, statement_location);
5900 /* Parse a labeled-statement.
5903 identifier : statement
5904 case constant-expression : statement
5910 case constant-expression ... constant-expression : statement
5912 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5913 For an ordinary label, returns a LABEL_EXPR. */
5916 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5919 tree statement = error_mark_node;
5921 /* The next token should be an identifier. */
5922 token = cp_lexer_peek_token (parser->lexer);
5923 if (token->type != CPP_NAME
5924 && token->type != CPP_KEYWORD)
5926 cp_parser_error (parser, "expected labeled-statement");
5927 return error_mark_node;
5930 switch (token->keyword)
5937 /* Consume the `case' token. */
5938 cp_lexer_consume_token (parser->lexer);
5939 /* Parse the constant-expression. */
5940 expr = cp_parser_constant_expression (parser,
5941 /*allow_non_constant_p=*/false,
5944 ellipsis = cp_lexer_peek_token (parser->lexer);
5945 if (ellipsis->type == CPP_ELLIPSIS)
5947 /* Consume the `...' token. */
5948 cp_lexer_consume_token (parser->lexer);
5950 cp_parser_constant_expression (parser,
5951 /*allow_non_constant_p=*/false,
5953 /* We don't need to emit warnings here, as the common code
5954 will do this for us. */
5957 expr_hi = NULL_TREE;
5959 if (!parser->in_switch_statement_p)
5960 error ("case label `%E' not within a switch statement", expr);
5962 statement = finish_case_label (expr, expr_hi);
5967 /* Consume the `default' token. */
5968 cp_lexer_consume_token (parser->lexer);
5969 if (!parser->in_switch_statement_p)
5970 error ("case label not within a switch statement");
5972 statement = finish_case_label (NULL_TREE, NULL_TREE);
5976 /* Anything else must be an ordinary label. */
5977 statement = finish_label_stmt (cp_parser_identifier (parser));
5981 /* Require the `:' token. */
5982 cp_parser_require (parser, CPP_COLON, "`:'");
5983 /* Parse the labeled statement. */
5984 cp_parser_statement (parser, in_statement_expr);
5986 /* Return the label, in the case of a `case' or `default' label. */
5990 /* Parse an expression-statement.
5992 expression-statement:
5995 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5996 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5997 indicates whether this expression-statement is part of an
5998 expression statement. */
6001 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6003 tree statement = NULL_TREE;
6005 /* If the next token is a ';', then there is no expression
6007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6008 statement = cp_parser_expression (parser);
6010 /* Consume the final `;'. */
6011 cp_parser_consume_semicolon_at_end_of_statement (parser);
6013 if (in_statement_expr
6014 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6016 /* This is the final expression statement of a statement
6018 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6021 statement = finish_expr_stmt (statement);
6028 /* Parse a compound-statement.
6031 { statement-seq [opt] }
6033 Returns a tree representing the statement. */
6036 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6041 /* Consume the `{'. */
6042 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6043 return error_mark_node;
6044 /* Begin the compound-statement. */
6045 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6046 /* Parse an (optional) statement-seq. */
6047 cp_parser_statement_seq_opt (parser, in_statement_expr);
6048 /* Finish the compound-statement. */
6049 finish_compound_stmt (compound_stmt);
6050 /* Consume the `}'. */
6051 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6053 return compound_stmt;
6056 /* Parse an (optional) statement-seq.
6060 statement-seq [opt] statement */
6063 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6065 /* Scan statements until there aren't any more. */
6068 /* If we're looking at a `}', then we've run out of statements. */
6069 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6070 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6073 /* Parse the statement. */
6074 cp_parser_statement (parser, in_statement_expr);
6078 /* Parse a selection-statement.
6080 selection-statement:
6081 if ( condition ) statement
6082 if ( condition ) statement else statement
6083 switch ( condition ) statement
6085 Returns the new IF_STMT or SWITCH_STMT. */
6088 cp_parser_selection_statement (cp_parser* parser)
6093 /* Peek at the next token. */
6094 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6096 /* See what kind of keyword it is. */
6097 keyword = token->keyword;
6106 /* Look for the `('. */
6107 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6109 cp_parser_skip_to_end_of_statement (parser);
6110 return error_mark_node;
6113 /* Begin the selection-statement. */
6114 if (keyword == RID_IF)
6115 statement = begin_if_stmt ();
6117 statement = begin_switch_stmt ();
6119 /* Parse the condition. */
6120 condition = cp_parser_condition (parser);
6121 /* Look for the `)'. */
6122 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6123 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6124 /*consume_paren=*/true);
6126 if (keyword == RID_IF)
6128 /* Add the condition. */
6129 finish_if_stmt_cond (condition, statement);
6131 /* Parse the then-clause. */
6132 cp_parser_implicitly_scoped_statement (parser);
6133 finish_then_clause (statement);
6135 /* If the next token is `else', parse the else-clause. */
6136 if (cp_lexer_next_token_is_keyword (parser->lexer,
6139 /* Consume the `else' keyword. */
6140 cp_lexer_consume_token (parser->lexer);
6141 begin_else_clause (statement);
6142 /* Parse the else-clause. */
6143 cp_parser_implicitly_scoped_statement (parser);
6144 finish_else_clause (statement);
6147 /* Now we're all done with the if-statement. */
6148 finish_if_stmt (statement);
6152 bool in_switch_statement_p;
6154 /* Add the condition. */
6155 finish_switch_cond (condition, statement);
6157 /* Parse the body of the switch-statement. */
6158 in_switch_statement_p = parser->in_switch_statement_p;
6159 parser->in_switch_statement_p = true;
6160 cp_parser_implicitly_scoped_statement (parser);
6161 parser->in_switch_statement_p = in_switch_statement_p;
6163 /* Now we're all done with the switch-statement. */
6164 finish_switch_stmt (statement);
6172 cp_parser_error (parser, "expected selection-statement");
6173 return error_mark_node;
6177 /* Parse a condition.
6181 type-specifier-seq declarator = assignment-expression
6186 type-specifier-seq declarator asm-specification [opt]
6187 attributes [opt] = assignment-expression
6189 Returns the expression that should be tested. */
6192 cp_parser_condition (cp_parser* parser)
6194 cp_decl_specifier_seq type_specifiers;
6195 const char *saved_message;
6197 /* Try the declaration first. */
6198 cp_parser_parse_tentatively (parser);
6199 /* New types are not allowed in the type-specifier-seq for a
6201 saved_message = parser->type_definition_forbidden_message;
6202 parser->type_definition_forbidden_message
6203 = "types may not be defined in conditions";
6204 /* Parse the type-specifier-seq. */
6205 cp_parser_type_specifier_seq (parser, &type_specifiers);
6206 /* Restore the saved message. */
6207 parser->type_definition_forbidden_message = saved_message;
6208 /* If all is well, we might be looking at a declaration. */
6209 if (!cp_parser_error_occurred (parser))
6212 tree asm_specification;
6214 cp_declarator *declarator;
6215 tree initializer = NULL_TREE;
6217 /* Parse the declarator. */
6218 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6219 /*ctor_dtor_or_conv_p=*/NULL,
6220 /*parenthesized_p=*/NULL);
6221 /* Parse the attributes. */
6222 attributes = cp_parser_attributes_opt (parser);
6223 /* Parse the asm-specification. */
6224 asm_specification = cp_parser_asm_specification_opt (parser);
6225 /* If the next token is not an `=', then we might still be
6226 looking at an expression. For example:
6230 looks like a decl-specifier-seq and a declarator -- but then
6231 there is no `=', so this is an expression. */
6232 cp_parser_require (parser, CPP_EQ, "`='");
6233 /* If we did see an `=', then we are looking at a declaration
6235 if (cp_parser_parse_definitely (parser))
6239 /* Create the declaration. */
6240 decl = start_decl (declarator, &type_specifiers,
6241 /*initialized_p=*/true,
6242 attributes, /*prefix_attributes=*/NULL_TREE,
6244 /* Parse the assignment-expression. */
6245 initializer = cp_parser_assignment_expression (parser);
6247 /* Process the initializer. */
6248 cp_finish_decl (decl,
6251 LOOKUP_ONLYCONVERTING);
6254 pop_scope (DECL_CONTEXT (decl));
6256 return convert_from_reference (decl);
6259 /* If we didn't even get past the declarator successfully, we are
6260 definitely not looking at a declaration. */
6262 cp_parser_abort_tentative_parse (parser);
6264 /* Otherwise, we are looking at an expression. */
6265 return cp_parser_expression (parser);
6268 /* Parse an iteration-statement.
6270 iteration-statement:
6271 while ( condition ) statement
6272 do statement while ( expression ) ;
6273 for ( for-init-statement condition [opt] ; expression [opt] )
6276 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6279 cp_parser_iteration_statement (cp_parser* parser)
6284 bool in_iteration_statement_p;
6287 /* Peek at the next token. */
6288 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6290 return error_mark_node;
6292 /* Remember whether or not we are already within an iteration
6294 in_iteration_statement_p = parser->in_iteration_statement_p;
6296 /* See what kind of keyword it is. */
6297 keyword = token->keyword;
6304 /* Begin the while-statement. */
6305 statement = begin_while_stmt ();
6306 /* Look for the `('. */
6307 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6308 /* Parse the condition. */
6309 condition = cp_parser_condition (parser);
6310 finish_while_stmt_cond (condition, statement);
6311 /* Look for the `)'. */
6312 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6313 /* Parse the dependent statement. */
6314 parser->in_iteration_statement_p = true;
6315 cp_parser_already_scoped_statement (parser);
6316 parser->in_iteration_statement_p = in_iteration_statement_p;
6317 /* We're done with the while-statement. */
6318 finish_while_stmt (statement);
6326 /* Begin the do-statement. */
6327 statement = begin_do_stmt ();
6328 /* Parse the body of the do-statement. */
6329 parser->in_iteration_statement_p = true;
6330 cp_parser_implicitly_scoped_statement (parser);
6331 parser->in_iteration_statement_p = in_iteration_statement_p;
6332 finish_do_body (statement);
6333 /* Look for the `while' keyword. */
6334 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6335 /* Look for the `('. */
6336 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6337 /* Parse the expression. */
6338 expression = cp_parser_expression (parser);
6339 /* We're done with the do-statement. */
6340 finish_do_stmt (expression, statement);
6341 /* Look for the `)'. */
6342 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6343 /* Look for the `;'. */
6344 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6350 tree condition = NULL_TREE;
6351 tree expression = NULL_TREE;
6353 /* Begin the for-statement. */
6354 statement = begin_for_stmt ();
6355 /* Look for the `('. */
6356 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6357 /* Parse the initialization. */
6358 cp_parser_for_init_statement (parser);
6359 finish_for_init_stmt (statement);
6361 /* If there's a condition, process it. */
6362 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6363 condition = cp_parser_condition (parser);
6364 finish_for_cond (condition, statement);
6365 /* Look for the `;'. */
6366 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6368 /* If there's an expression, process it. */
6369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6370 expression = cp_parser_expression (parser);
6371 finish_for_expr (expression, statement);
6372 /* Look for the `)'. */
6373 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6375 /* Parse the body of the for-statement. */
6376 parser->in_iteration_statement_p = true;
6377 cp_parser_already_scoped_statement (parser);
6378 parser->in_iteration_statement_p = in_iteration_statement_p;
6380 /* We're done with the for-statement. */
6381 finish_for_stmt (statement);
6386 cp_parser_error (parser, "expected iteration-statement");
6387 statement = error_mark_node;
6394 /* Parse a for-init-statement.
6397 expression-statement
6398 simple-declaration */
6401 cp_parser_for_init_statement (cp_parser* parser)
6403 /* If the next token is a `;', then we have an empty
6404 expression-statement. Grammatically, this is also a
6405 simple-declaration, but an invalid one, because it does not
6406 declare anything. Therefore, if we did not handle this case
6407 specially, we would issue an error message about an invalid
6409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6411 /* We're going to speculatively look for a declaration, falling back
6412 to an expression, if necessary. */
6413 cp_parser_parse_tentatively (parser);
6414 /* Parse the declaration. */
6415 cp_parser_simple_declaration (parser,
6416 /*function_definition_allowed_p=*/false);
6417 /* If the tentative parse failed, then we shall need to look for an
6418 expression-statement. */
6419 if (cp_parser_parse_definitely (parser))
6423 cp_parser_expression_statement (parser, false);
6426 /* Parse a jump-statement.
6431 return expression [opt] ;
6439 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6442 cp_parser_jump_statement (cp_parser* parser)
6444 tree statement = error_mark_node;
6448 /* Peek at the next token. */
6449 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6451 return error_mark_node;
6453 /* See what kind of keyword it is. */
6454 keyword = token->keyword;
6458 if (!parser->in_switch_statement_p
6459 && !parser->in_iteration_statement_p)
6461 error ("break statement not within loop or switch");
6462 statement = error_mark_node;
6465 statement = finish_break_stmt ();
6466 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6470 if (!parser->in_iteration_statement_p)
6472 error ("continue statement not within a loop");
6473 statement = error_mark_node;
6476 statement = finish_continue_stmt ();
6477 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6484 /* If the next token is a `;', then there is no
6486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6487 expr = cp_parser_expression (parser);
6490 /* Build the return-statement. */
6491 statement = finish_return_stmt (expr);
6492 /* Look for the final `;'. */
6493 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6498 /* Create the goto-statement. */
6499 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6501 /* Issue a warning about this use of a GNU extension. */
6503 pedwarn ("ISO C++ forbids computed gotos");
6504 /* Consume the '*' token. */
6505 cp_lexer_consume_token (parser->lexer);
6506 /* Parse the dependent expression. */
6507 finish_goto_stmt (cp_parser_expression (parser));
6510 finish_goto_stmt (cp_parser_identifier (parser));
6511 /* Look for the final `;'. */
6512 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6516 cp_parser_error (parser, "expected jump-statement");
6523 /* Parse a declaration-statement.
6525 declaration-statement:
6526 block-declaration */
6529 cp_parser_declaration_statement (cp_parser* parser)
6533 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6534 p = obstack_alloc (&declarator_obstack, 0);
6536 /* Parse the block-declaration. */
6537 cp_parser_block_declaration (parser, /*statement_p=*/true);
6539 /* Free any declarators allocated. */
6540 obstack_free (&declarator_obstack, p);
6542 /* Finish off the statement. */
6546 /* Some dependent statements (like `if (cond) statement'), are
6547 implicitly in their own scope. In other words, if the statement is
6548 a single statement (as opposed to a compound-statement), it is
6549 none-the-less treated as if it were enclosed in braces. Any
6550 declarations appearing in the dependent statement are out of scope
6551 after control passes that point. This function parses a statement,
6552 but ensures that is in its own scope, even if it is not a
6555 Returns the new statement. */
6558 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6562 /* If the token is not a `{', then we must take special action. */
6563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6565 /* Create a compound-statement. */
6566 statement = begin_compound_stmt (0);
6567 /* Parse the dependent-statement. */
6568 cp_parser_statement (parser, false);
6569 /* Finish the dummy compound-statement. */
6570 finish_compound_stmt (statement);
6572 /* Otherwise, we simply parse the statement directly. */
6574 statement = cp_parser_compound_statement (parser, NULL, false);
6576 /* Return the statement. */
6580 /* For some dependent statements (like `while (cond) statement'), we
6581 have already created a scope. Therefore, even if the dependent
6582 statement is a compound-statement, we do not want to create another
6586 cp_parser_already_scoped_statement (cp_parser* parser)
6588 /* If the token is a `{', then we must take special action. */
6589 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6590 cp_parser_statement (parser, false);
6593 /* Avoid calling cp_parser_compound_statement, so that we
6594 don't create a new scope. Do everything else by hand. */
6595 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6596 cp_parser_statement_seq_opt (parser, false);
6597 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6601 /* Declarations [gram.dcl.dcl] */
6603 /* Parse an optional declaration-sequence.
6607 declaration-seq declaration */
6610 cp_parser_declaration_seq_opt (cp_parser* parser)
6616 token = cp_lexer_peek_token (parser->lexer);
6618 if (token->type == CPP_CLOSE_BRACE
6619 || token->type == CPP_EOF)
6622 if (token->type == CPP_SEMICOLON)
6624 /* A declaration consisting of a single semicolon is
6625 invalid. Allow it unless we're being pedantic. */
6626 cp_lexer_consume_token (parser->lexer);
6627 if (pedantic && !in_system_header)
6628 pedwarn ("extra %<;%>");
6632 if (token->type == CPP_PRAGMA)
6634 /* A top-level declaration can consist solely of a #pragma.
6635 A nested declaration cannot, so this is done here and not
6636 in cp_parser_declaration. (A #pragma at block scope is
6637 handled in cp_parser_statement.) */
6638 cp_lexer_handle_pragma (parser->lexer);
6642 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6643 parser to enter or exit implicit `extern "C"' blocks. */
6644 while (pending_lang_change > 0)
6646 push_lang_context (lang_name_c);
6647 --pending_lang_change;
6649 while (pending_lang_change < 0)
6651 pop_lang_context ();
6652 ++pending_lang_change;
6655 /* Parse the declaration itself. */
6656 cp_parser_declaration (parser);
6660 /* Parse a declaration.
6665 template-declaration
6666 explicit-instantiation
6667 explicit-specialization
6668 linkage-specification
6669 namespace-definition
6674 __extension__ declaration */
6677 cp_parser_declaration (cp_parser* parser)
6684 /* Check for the `__extension__' keyword. */
6685 if (cp_parser_extension_opt (parser, &saved_pedantic))
6687 /* Parse the qualified declaration. */
6688 cp_parser_declaration (parser);
6689 /* Restore the PEDANTIC flag. */
6690 pedantic = saved_pedantic;
6695 /* Try to figure out what kind of declaration is present. */
6696 token1 = *cp_lexer_peek_token (parser->lexer);
6698 if (token1.type != CPP_EOF)
6699 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6701 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6702 p = obstack_alloc (&declarator_obstack, 0);
6704 /* If the next token is `extern' and the following token is a string
6705 literal, then we have a linkage specification. */
6706 if (token1.keyword == RID_EXTERN
6707 && cp_parser_is_string_literal (&token2))
6708 cp_parser_linkage_specification (parser);
6709 /* If the next token is `template', then we have either a template
6710 declaration, an explicit instantiation, or an explicit
6712 else if (token1.keyword == RID_TEMPLATE)
6714 /* `template <>' indicates a template specialization. */
6715 if (token2.type == CPP_LESS
6716 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6717 cp_parser_explicit_specialization (parser);
6718 /* `template <' indicates a template declaration. */
6719 else if (token2.type == CPP_LESS)
6720 cp_parser_template_declaration (parser, /*member_p=*/false);
6721 /* Anything else must be an explicit instantiation. */
6723 cp_parser_explicit_instantiation (parser);
6725 /* If the next token is `export', then we have a template
6727 else if (token1.keyword == RID_EXPORT)
6728 cp_parser_template_declaration (parser, /*member_p=*/false);
6729 /* If the next token is `extern', 'static' or 'inline' and the one
6730 after that is `template', we have a GNU extended explicit
6731 instantiation directive. */
6732 else if (cp_parser_allow_gnu_extensions_p (parser)
6733 && (token1.keyword == RID_EXTERN
6734 || token1.keyword == RID_STATIC
6735 || token1.keyword == RID_INLINE)
6736 && token2.keyword == RID_TEMPLATE)
6737 cp_parser_explicit_instantiation (parser);
6738 /* If the next token is `namespace', check for a named or unnamed
6739 namespace definition. */
6740 else if (token1.keyword == RID_NAMESPACE
6741 && (/* A named namespace definition. */
6742 (token2.type == CPP_NAME
6743 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6745 /* An unnamed namespace definition. */
6746 || token2.type == CPP_OPEN_BRACE))
6747 cp_parser_namespace_definition (parser);
6748 /* We must have either a block declaration or a function
6751 /* Try to parse a block-declaration, or a function-definition. */
6752 cp_parser_block_declaration (parser, /*statement_p=*/false);
6754 /* Free any declarators allocated. */
6755 obstack_free (&declarator_obstack, p);
6758 /* Parse a block-declaration.
6763 namespace-alias-definition
6770 __extension__ block-declaration
6773 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6774 part of a declaration-statement. */
6777 cp_parser_block_declaration (cp_parser *parser,
6783 /* Check for the `__extension__' keyword. */
6784 if (cp_parser_extension_opt (parser, &saved_pedantic))
6786 /* Parse the qualified declaration. */
6787 cp_parser_block_declaration (parser, statement_p);
6788 /* Restore the PEDANTIC flag. */
6789 pedantic = saved_pedantic;
6794 /* Peek at the next token to figure out which kind of declaration is
6796 token1 = cp_lexer_peek_token (parser->lexer);
6798 /* If the next keyword is `asm', we have an asm-definition. */
6799 if (token1->keyword == RID_ASM)
6802 cp_parser_commit_to_tentative_parse (parser);
6803 cp_parser_asm_definition (parser);
6805 /* If the next keyword is `namespace', we have a
6806 namespace-alias-definition. */
6807 else if (token1->keyword == RID_NAMESPACE)
6808 cp_parser_namespace_alias_definition (parser);
6809 /* If the next keyword is `using', we have either a
6810 using-declaration or a using-directive. */
6811 else if (token1->keyword == RID_USING)
6816 cp_parser_commit_to_tentative_parse (parser);
6817 /* If the token after `using' is `namespace', then we have a
6819 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6820 if (token2->keyword == RID_NAMESPACE)
6821 cp_parser_using_directive (parser);
6822 /* Otherwise, it's a using-declaration. */
6824 cp_parser_using_declaration (parser);
6826 /* If the next keyword is `__label__' we have a label declaration. */
6827 else if (token1->keyword == RID_LABEL)
6830 cp_parser_commit_to_tentative_parse (parser);
6831 cp_parser_label_declaration (parser);
6833 /* Anything else must be a simple-declaration. */
6835 cp_parser_simple_declaration (parser, !statement_p);
6838 /* Parse a simple-declaration.
6841 decl-specifier-seq [opt] init-declarator-list [opt] ;
6843 init-declarator-list:
6845 init-declarator-list , init-declarator
6847 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6848 function-definition as a simple-declaration. */
6851 cp_parser_simple_declaration (cp_parser* parser,
6852 bool function_definition_allowed_p)
6854 cp_decl_specifier_seq decl_specifiers;
6855 int declares_class_or_enum;
6856 bool saw_declarator;
6858 /* Defer access checks until we know what is being declared; the
6859 checks for names appearing in the decl-specifier-seq should be
6860 done as if we were in the scope of the thing being declared. */
6861 push_deferring_access_checks (dk_deferred);
6863 /* Parse the decl-specifier-seq. We have to keep track of whether
6864 or not the decl-specifier-seq declares a named class or
6865 enumeration type, since that is the only case in which the
6866 init-declarator-list is allowed to be empty.
6870 In a simple-declaration, the optional init-declarator-list can be
6871 omitted only when declaring a class or enumeration, that is when
6872 the decl-specifier-seq contains either a class-specifier, an
6873 elaborated-type-specifier, or an enum-specifier. */
6874 cp_parser_decl_specifier_seq (parser,
6875 CP_PARSER_FLAGS_OPTIONAL,
6877 &declares_class_or_enum);
6878 /* We no longer need to defer access checks. */
6879 stop_deferring_access_checks ();
6881 /* In a block scope, a valid declaration must always have a
6882 decl-specifier-seq. By not trying to parse declarators, we can
6883 resolve the declaration/expression ambiguity more quickly. */
6884 if (!function_definition_allowed_p
6885 && !decl_specifiers.any_specifiers_p)
6887 cp_parser_error (parser, "expected declaration");
6891 /* If the next two tokens are both identifiers, the code is
6892 erroneous. The usual cause of this situation is code like:
6896 where "T" should name a type -- but does not. */
6897 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6899 /* If parsing tentatively, we should commit; we really are
6900 looking at a declaration. */
6901 cp_parser_commit_to_tentative_parse (parser);
6906 /* If we have seen at least one decl-specifier, and the next token
6907 is not a parenthesis, then we must be looking at a declaration.
6908 (After "int (" we might be looking at a functional cast.) */
6909 if (decl_specifiers.any_specifiers_p
6910 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6911 cp_parser_commit_to_tentative_parse (parser);
6913 /* Keep going until we hit the `;' at the end of the simple
6915 saw_declarator = false;
6916 while (cp_lexer_next_token_is_not (parser->lexer,
6920 bool function_definition_p;
6923 saw_declarator = true;
6924 /* Parse the init-declarator. */
6925 decl = cp_parser_init_declarator (parser, &decl_specifiers,
6926 function_definition_allowed_p,
6928 declares_class_or_enum,
6929 &function_definition_p);
6930 /* If an error occurred while parsing tentatively, exit quickly.
6931 (That usually happens when in the body of a function; each
6932 statement is treated as a declaration-statement until proven
6934 if (cp_parser_error_occurred (parser))
6936 /* Handle function definitions specially. */
6937 if (function_definition_p)
6939 /* If the next token is a `,', then we are probably
6940 processing something like:
6944 which is erroneous. */
6945 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6946 error ("mixing declarations and function-definitions is forbidden");
6947 /* Otherwise, we're done with the list of declarators. */
6950 pop_deferring_access_checks ();
6954 /* The next token should be either a `,' or a `;'. */
6955 token = cp_lexer_peek_token (parser->lexer);
6956 /* If it's a `,', there are more declarators to come. */
6957 if (token->type == CPP_COMMA)
6958 cp_lexer_consume_token (parser->lexer);
6959 /* If it's a `;', we are done. */
6960 else if (token->type == CPP_SEMICOLON)
6962 /* Anything else is an error. */
6965 /* If we have already issued an error message we don't need
6966 to issue another one. */
6967 if (decl != error_mark_node
6968 || (cp_parser_parsing_tentatively (parser)
6969 && !cp_parser_committed_to_tentative_parse (parser)))
6970 cp_parser_error (parser, "expected `,' or `;'");
6971 /* Skip tokens until we reach the end of the statement. */
6972 cp_parser_skip_to_end_of_statement (parser);
6973 /* If the next token is now a `;', consume it. */
6974 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6975 cp_lexer_consume_token (parser->lexer);
6978 /* After the first time around, a function-definition is not
6979 allowed -- even if it was OK at first. For example:
6984 function_definition_allowed_p = false;
6987 /* Issue an error message if no declarators are present, and the
6988 decl-specifier-seq does not itself declare a class or
6990 if (!saw_declarator)
6992 if (cp_parser_declares_only_class_p (parser))
6993 shadow_tag (&decl_specifiers);
6994 /* Perform any deferred access checks. */
6995 perform_deferred_access_checks ();
6998 /* Consume the `;'. */
6999 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7002 pop_deferring_access_checks ();
7005 /* Parse a decl-specifier-seq.
7008 decl-specifier-seq [opt] decl-specifier
7011 storage-class-specifier
7022 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7024 The parser flags FLAGS is used to control type-specifier parsing.
7026 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7029 1: one of the decl-specifiers is an elaborated-type-specifier
7030 (i.e., a type declaration)
7031 2: one of the decl-specifiers is an enum-specifier or a
7032 class-specifier (i.e., a type definition)
7037 cp_parser_decl_specifier_seq (cp_parser* parser,
7038 cp_parser_flags flags,
7039 cp_decl_specifier_seq *decl_specs,
7040 int* declares_class_or_enum)
7042 bool constructor_possible_p = !parser->in_declarator_p;
7044 /* Clear DECL_SPECS. */
7045 clear_decl_specs (decl_specs);
7047 /* Assume no class or enumeration type is declared. */
7048 *declares_class_or_enum = 0;
7050 /* Keep reading specifiers until there are no more to read. */
7054 bool found_decl_spec;
7057 /* Peek at the next token. */
7058 token = cp_lexer_peek_token (parser->lexer);
7059 /* Handle attributes. */
7060 if (token->keyword == RID_ATTRIBUTE)
7062 /* Parse the attributes. */
7063 decl_specs->attributes
7064 = chainon (decl_specs->attributes,
7065 cp_parser_attributes_opt (parser));
7068 /* Assume we will find a decl-specifier keyword. */
7069 found_decl_spec = true;
7070 /* If the next token is an appropriate keyword, we can simply
7071 add it to the list. */
7072 switch (token->keyword)
7077 if (decl_specs->specs[(int) ds_friend]++)
7078 error ("duplicate `friend'");
7079 /* Consume the token. */
7080 cp_lexer_consume_token (parser->lexer);
7083 /* function-specifier:
7090 cp_parser_function_specifier_opt (parser, decl_specs);
7096 ++decl_specs->specs[(int) ds_typedef];
7097 /* Consume the token. */
7098 cp_lexer_consume_token (parser->lexer);
7099 /* A constructor declarator cannot appear in a typedef. */
7100 constructor_possible_p = false;
7101 /* The "typedef" keyword can only occur in a declaration; we
7102 may as well commit at this point. */
7103 cp_parser_commit_to_tentative_parse (parser);
7106 /* storage-class-specifier:
7116 /* Consume the token. */
7117 cp_lexer_consume_token (parser->lexer);
7118 cp_parser_set_storage_class (decl_specs, sc_auto);
7121 /* Consume the token. */
7122 cp_lexer_consume_token (parser->lexer);
7123 cp_parser_set_storage_class (decl_specs, sc_register);
7126 /* Consume the token. */
7127 cp_lexer_consume_token (parser->lexer);
7128 if (decl_specs->specs[(int) ds_thread])
7130 error ("`__thread' before `static'");
7131 decl_specs->specs[(int) ds_thread] = 0;
7133 cp_parser_set_storage_class (decl_specs, sc_static);
7136 /* Consume the token. */
7137 cp_lexer_consume_token (parser->lexer);
7138 if (decl_specs->specs[(int) ds_thread])
7140 error ("`__thread' before `extern'");
7141 decl_specs->specs[(int) ds_thread] = 0;
7143 cp_parser_set_storage_class (decl_specs, sc_extern);
7146 /* Consume the token. */
7147 cp_lexer_consume_token (parser->lexer);
7148 cp_parser_set_storage_class (decl_specs, sc_mutable);
7151 /* Consume the token. */
7152 cp_lexer_consume_token (parser->lexer);
7153 ++decl_specs->specs[(int) ds_thread];
7157 /* We did not yet find a decl-specifier yet. */
7158 found_decl_spec = false;
7162 /* Constructors are a special case. The `S' in `S()' is not a
7163 decl-specifier; it is the beginning of the declarator. */
7166 && constructor_possible_p
7167 && (cp_parser_constructor_declarator_p
7168 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7170 /* If we don't have a DECL_SPEC yet, then we must be looking at
7171 a type-specifier. */
7172 if (!found_decl_spec && !constructor_p)
7174 int decl_spec_declares_class_or_enum;
7175 bool is_cv_qualifier;
7179 = cp_parser_type_specifier (parser, flags,
7181 /*is_declaration=*/true,
7182 &decl_spec_declares_class_or_enum,
7185 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7187 /* If this type-specifier referenced a user-defined type
7188 (a typedef, class-name, etc.), then we can't allow any
7189 more such type-specifiers henceforth.
7193 The longest sequence of decl-specifiers that could
7194 possibly be a type name is taken as the
7195 decl-specifier-seq of a declaration. The sequence shall
7196 be self-consistent as described below.
7200 As a general rule, at most one type-specifier is allowed
7201 in the complete decl-specifier-seq of a declaration. The
7202 only exceptions are the following:
7204 -- const or volatile can be combined with any other
7207 -- signed or unsigned can be combined with char, long,
7215 void g (const int Pc);
7217 Here, Pc is *not* part of the decl-specifier seq; it's
7218 the declarator. Therefore, once we see a type-specifier
7219 (other than a cv-qualifier), we forbid any additional
7220 user-defined types. We *do* still allow things like `int
7221 int' to be considered a decl-specifier-seq, and issue the
7222 error message later. */
7223 if (type_spec && !is_cv_qualifier)
7224 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7225 /* A constructor declarator cannot follow a type-specifier. */
7228 constructor_possible_p = false;
7229 found_decl_spec = true;
7233 /* If we still do not have a DECL_SPEC, then there are no more
7235 if (!found_decl_spec)
7238 decl_specs->any_specifiers_p = true;
7239 /* After we see one decl-specifier, further decl-specifiers are
7241 flags |= CP_PARSER_FLAGS_OPTIONAL;
7244 /* Don't allow a friend specifier with a class definition. */
7245 if (decl_specs->specs[(int) ds_friend] != 0
7246 && (*declares_class_or_enum & 2))
7247 error ("class definition may not be declared a friend");
7250 /* Parse an (optional) storage-class-specifier.
7252 storage-class-specifier:
7261 storage-class-specifier:
7264 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7267 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7269 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7277 /* Consume the token. */
7278 return cp_lexer_consume_token (parser->lexer)->value;
7285 /* Parse an (optional) function-specifier.
7292 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7293 Updates DECL_SPECS, if it is non-NULL. */
7296 cp_parser_function_specifier_opt (cp_parser* parser,
7297 cp_decl_specifier_seq *decl_specs)
7299 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7303 ++decl_specs->specs[(int) ds_inline];
7308 ++decl_specs->specs[(int) ds_virtual];
7313 ++decl_specs->specs[(int) ds_explicit];
7320 /* Consume the token. */
7321 return cp_lexer_consume_token (parser->lexer)->value;
7324 /* Parse a linkage-specification.
7326 linkage-specification:
7327 extern string-literal { declaration-seq [opt] }
7328 extern string-literal declaration */
7331 cp_parser_linkage_specification (cp_parser* parser)
7335 /* Look for the `extern' keyword. */
7336 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7338 /* Look for the string-literal. */
7339 linkage = cp_parser_string_literal (parser, false, false);
7341 /* Transform the literal into an identifier. If the literal is a
7342 wide-character string, or contains embedded NULs, then we can't
7343 handle it as the user wants. */
7344 if (strlen (TREE_STRING_POINTER (linkage))
7345 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7347 cp_parser_error (parser, "invalid linkage-specification");
7348 /* Assume C++ linkage. */
7349 linkage = lang_name_cplusplus;
7352 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7354 /* We're now using the new linkage. */
7355 push_lang_context (linkage);
7357 /* If the next token is a `{', then we're using the first
7359 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7361 /* Consume the `{' token. */
7362 cp_lexer_consume_token (parser->lexer);
7363 /* Parse the declarations. */
7364 cp_parser_declaration_seq_opt (parser);
7365 /* Look for the closing `}'. */
7366 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7368 /* Otherwise, there's just one declaration. */
7371 bool saved_in_unbraced_linkage_specification_p;
7373 saved_in_unbraced_linkage_specification_p
7374 = parser->in_unbraced_linkage_specification_p;
7375 parser->in_unbraced_linkage_specification_p = true;
7376 have_extern_spec = true;
7377 cp_parser_declaration (parser);
7378 have_extern_spec = false;
7379 parser->in_unbraced_linkage_specification_p
7380 = saved_in_unbraced_linkage_specification_p;
7383 /* We're done with the linkage-specification. */
7384 pop_lang_context ();
7387 /* Special member functions [gram.special] */
7389 /* Parse a conversion-function-id.
7391 conversion-function-id:
7392 operator conversion-type-id
7394 Returns an IDENTIFIER_NODE representing the operator. */
7397 cp_parser_conversion_function_id (cp_parser* parser)
7401 tree saved_qualifying_scope;
7402 tree saved_object_scope;
7405 /* Look for the `operator' token. */
7406 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7407 return error_mark_node;
7408 /* When we parse the conversion-type-id, the current scope will be
7409 reset. However, we need that information in able to look up the
7410 conversion function later, so we save it here. */
7411 saved_scope = parser->scope;
7412 saved_qualifying_scope = parser->qualifying_scope;
7413 saved_object_scope = parser->object_scope;
7414 /* We must enter the scope of the class so that the names of
7415 entities declared within the class are available in the
7416 conversion-type-id. For example, consider:
7423 S::operator I() { ... }
7425 In order to see that `I' is a type-name in the definition, we
7426 must be in the scope of `S'. */
7428 pop_p = push_scope (saved_scope);
7429 /* Parse the conversion-type-id. */
7430 type = cp_parser_conversion_type_id (parser);
7431 /* Leave the scope of the class, if any. */
7433 pop_scope (saved_scope);
7434 /* Restore the saved scope. */
7435 parser->scope = saved_scope;
7436 parser->qualifying_scope = saved_qualifying_scope;
7437 parser->object_scope = saved_object_scope;
7438 /* If the TYPE is invalid, indicate failure. */
7439 if (type == error_mark_node)
7440 return error_mark_node;
7441 return mangle_conv_op_name_for_type (type);
7444 /* Parse a conversion-type-id:
7447 type-specifier-seq conversion-declarator [opt]
7449 Returns the TYPE specified. */
7452 cp_parser_conversion_type_id (cp_parser* parser)
7455 cp_decl_specifier_seq type_specifiers;
7456 cp_declarator *declarator;
7457 tree type_specified;
7459 /* Parse the attributes. */
7460 attributes = cp_parser_attributes_opt (parser);
7461 /* Parse the type-specifiers. */
7462 cp_parser_type_specifier_seq (parser, &type_specifiers);
7463 /* If that didn't work, stop. */
7464 if (type_specifiers.type == error_mark_node)
7465 return error_mark_node;
7466 /* Parse the conversion-declarator. */
7467 declarator = cp_parser_conversion_declarator_opt (parser);
7469 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7470 /*initialized=*/0, &attributes);
7472 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7473 return type_specified;
7476 /* Parse an (optional) conversion-declarator.
7478 conversion-declarator:
7479 ptr-operator conversion-declarator [opt]
7483 static cp_declarator *
7484 cp_parser_conversion_declarator_opt (cp_parser* parser)
7486 enum tree_code code;
7488 cp_cv_quals cv_quals;
7490 /* We don't know if there's a ptr-operator next, or not. */
7491 cp_parser_parse_tentatively (parser);
7492 /* Try the ptr-operator. */
7493 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7494 /* If it worked, look for more conversion-declarators. */
7495 if (cp_parser_parse_definitely (parser))
7497 cp_declarator *declarator;
7499 /* Parse another optional declarator. */
7500 declarator = cp_parser_conversion_declarator_opt (parser);
7502 /* Create the representation of the declarator. */
7504 declarator = make_ptrmem_declarator (cv_quals, class_type,
7506 else if (code == INDIRECT_REF)
7507 declarator = make_pointer_declarator (cv_quals, declarator);
7509 declarator = make_reference_declarator (cv_quals, declarator);
7517 /* Parse an (optional) ctor-initializer.
7520 : mem-initializer-list
7522 Returns TRUE iff the ctor-initializer was actually present. */
7525 cp_parser_ctor_initializer_opt (cp_parser* parser)
7527 /* If the next token is not a `:', then there is no
7528 ctor-initializer. */
7529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7531 /* Do default initialization of any bases and members. */
7532 if (DECL_CONSTRUCTOR_P (current_function_decl))
7533 finish_mem_initializers (NULL_TREE);
7538 /* Consume the `:' token. */
7539 cp_lexer_consume_token (parser->lexer);
7540 /* And the mem-initializer-list. */
7541 cp_parser_mem_initializer_list (parser);
7546 /* Parse a mem-initializer-list.
7548 mem-initializer-list:
7550 mem-initializer , mem-initializer-list */
7553 cp_parser_mem_initializer_list (cp_parser* parser)
7555 tree mem_initializer_list = NULL_TREE;
7557 /* Let the semantic analysis code know that we are starting the
7558 mem-initializer-list. */
7559 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7560 error ("only constructors take base initializers");
7562 /* Loop through the list. */
7565 tree mem_initializer;
7567 /* Parse the mem-initializer. */
7568 mem_initializer = cp_parser_mem_initializer (parser);
7569 /* Add it to the list, unless it was erroneous. */
7570 if (mem_initializer)
7572 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7573 mem_initializer_list = mem_initializer;
7575 /* If the next token is not a `,', we're done. */
7576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7578 /* Consume the `,' token. */
7579 cp_lexer_consume_token (parser->lexer);
7582 /* Perform semantic analysis. */
7583 if (DECL_CONSTRUCTOR_P (current_function_decl))
7584 finish_mem_initializers (mem_initializer_list);
7587 /* Parse a mem-initializer.
7590 mem-initializer-id ( expression-list [opt] )
7595 ( expression-list [opt] )
7597 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7598 class) or FIELD_DECL (for a non-static data member) to initialize;
7599 the TREE_VALUE is the expression-list. */
7602 cp_parser_mem_initializer (cp_parser* parser)
7604 tree mem_initializer_id;
7605 tree expression_list;
7608 /* Find out what is being initialized. */
7609 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7611 pedwarn ("anachronistic old-style base class initializer");
7612 mem_initializer_id = NULL_TREE;
7615 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7616 member = expand_member_init (mem_initializer_id);
7617 if (member && !DECL_P (member))
7618 in_base_initializer = 1;
7621 = cp_parser_parenthesized_expression_list (parser, false,
7622 /*non_constant_p=*/NULL);
7623 if (!expression_list)
7624 expression_list = void_type_node;
7626 in_base_initializer = 0;
7628 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7631 /* Parse a mem-initializer-id.
7634 :: [opt] nested-name-specifier [opt] class-name
7637 Returns a TYPE indicating the class to be initializer for the first
7638 production. Returns an IDENTIFIER_NODE indicating the data member
7639 to be initialized for the second production. */
7642 cp_parser_mem_initializer_id (cp_parser* parser)
7644 bool global_scope_p;
7645 bool nested_name_specifier_p;
7646 bool template_p = false;
7649 /* `typename' is not allowed in this context ([temp.res]). */
7650 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7652 error ("keyword `typename' not allowed in this context (a qualified "
7653 "member initializer is implicitly a type)");
7654 cp_lexer_consume_token (parser->lexer);
7656 /* Look for the optional `::' operator. */
7658 = (cp_parser_global_scope_opt (parser,
7659 /*current_scope_valid_p=*/false)
7661 /* Look for the optional nested-name-specifier. The simplest way to
7666 The keyword `typename' is not permitted in a base-specifier or
7667 mem-initializer; in these contexts a qualified name that
7668 depends on a template-parameter is implicitly assumed to be a
7671 is to assume that we have seen the `typename' keyword at this
7673 nested_name_specifier_p
7674 = (cp_parser_nested_name_specifier_opt (parser,
7675 /*typename_keyword_p=*/true,
7676 /*check_dependency_p=*/true,
7678 /*is_declaration=*/true)
7680 if (nested_name_specifier_p)
7681 template_p = cp_parser_optional_template_keyword (parser);
7682 /* If there is a `::' operator or a nested-name-specifier, then we
7683 are definitely looking for a class-name. */
7684 if (global_scope_p || nested_name_specifier_p)
7685 return cp_parser_class_name (parser,
7686 /*typename_keyword_p=*/true,
7687 /*template_keyword_p=*/template_p,
7689 /*check_dependency_p=*/true,
7690 /*class_head_p=*/false,
7691 /*is_declaration=*/true);
7692 /* Otherwise, we could also be looking for an ordinary identifier. */
7693 cp_parser_parse_tentatively (parser);
7694 /* Try a class-name. */
7695 id = cp_parser_class_name (parser,
7696 /*typename_keyword_p=*/true,
7697 /*template_keyword_p=*/false,
7699 /*check_dependency_p=*/true,
7700 /*class_head_p=*/false,
7701 /*is_declaration=*/true);
7702 /* If we found one, we're done. */
7703 if (cp_parser_parse_definitely (parser))
7705 /* Otherwise, look for an ordinary identifier. */
7706 return cp_parser_identifier (parser);
7709 /* Overloading [gram.over] */
7711 /* Parse an operator-function-id.
7713 operator-function-id:
7716 Returns an IDENTIFIER_NODE for the operator which is a
7717 human-readable spelling of the identifier, e.g., `operator +'. */
7720 cp_parser_operator_function_id (cp_parser* parser)
7722 /* Look for the `operator' keyword. */
7723 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7724 return error_mark_node;
7725 /* And then the name of the operator itself. */
7726 return cp_parser_operator (parser);
7729 /* Parse an operator.
7732 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7733 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7734 || ++ -- , ->* -> () []
7741 Returns an IDENTIFIER_NODE for the operator which is a
7742 human-readable spelling of the identifier, e.g., `operator +'. */
7745 cp_parser_operator (cp_parser* parser)
7747 tree id = NULL_TREE;
7750 /* Peek at the next token. */
7751 token = cp_lexer_peek_token (parser->lexer);
7752 /* Figure out which operator we have. */
7753 switch (token->type)
7759 /* The keyword should be either `new' or `delete'. */
7760 if (token->keyword == RID_NEW)
7762 else if (token->keyword == RID_DELETE)
7767 /* Consume the `new' or `delete' token. */
7768 cp_lexer_consume_token (parser->lexer);
7770 /* Peek at the next token. */
7771 token = cp_lexer_peek_token (parser->lexer);
7772 /* If it's a `[' token then this is the array variant of the
7774 if (token->type == CPP_OPEN_SQUARE)
7776 /* Consume the `[' token. */
7777 cp_lexer_consume_token (parser->lexer);
7778 /* Look for the `]' token. */
7779 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7780 id = ansi_opname (op == NEW_EXPR
7781 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7783 /* Otherwise, we have the non-array variant. */
7785 id = ansi_opname (op);
7791 id = ansi_opname (PLUS_EXPR);
7795 id = ansi_opname (MINUS_EXPR);
7799 id = ansi_opname (MULT_EXPR);
7803 id = ansi_opname (TRUNC_DIV_EXPR);
7807 id = ansi_opname (TRUNC_MOD_EXPR);
7811 id = ansi_opname (BIT_XOR_EXPR);
7815 id = ansi_opname (BIT_AND_EXPR);
7819 id = ansi_opname (BIT_IOR_EXPR);
7823 id = ansi_opname (BIT_NOT_EXPR);
7827 id = ansi_opname (TRUTH_NOT_EXPR);
7831 id = ansi_assopname (NOP_EXPR);
7835 id = ansi_opname (LT_EXPR);
7839 id = ansi_opname (GT_EXPR);
7843 id = ansi_assopname (PLUS_EXPR);
7847 id = ansi_assopname (MINUS_EXPR);
7851 id = ansi_assopname (MULT_EXPR);
7855 id = ansi_assopname (TRUNC_DIV_EXPR);
7859 id = ansi_assopname (TRUNC_MOD_EXPR);
7863 id = ansi_assopname (BIT_XOR_EXPR);
7867 id = ansi_assopname (BIT_AND_EXPR);
7871 id = ansi_assopname (BIT_IOR_EXPR);
7875 id = ansi_opname (LSHIFT_EXPR);
7879 id = ansi_opname (RSHIFT_EXPR);
7883 id = ansi_assopname (LSHIFT_EXPR);
7887 id = ansi_assopname (RSHIFT_EXPR);
7891 id = ansi_opname (EQ_EXPR);
7895 id = ansi_opname (NE_EXPR);
7899 id = ansi_opname (LE_EXPR);
7902 case CPP_GREATER_EQ:
7903 id = ansi_opname (GE_EXPR);
7907 id = ansi_opname (TRUTH_ANDIF_EXPR);
7911 id = ansi_opname (TRUTH_ORIF_EXPR);
7915 id = ansi_opname (POSTINCREMENT_EXPR);
7918 case CPP_MINUS_MINUS:
7919 id = ansi_opname (PREDECREMENT_EXPR);
7923 id = ansi_opname (COMPOUND_EXPR);
7926 case CPP_DEREF_STAR:
7927 id = ansi_opname (MEMBER_REF);
7931 id = ansi_opname (COMPONENT_REF);
7934 case CPP_OPEN_PAREN:
7935 /* Consume the `('. */
7936 cp_lexer_consume_token (parser->lexer);
7937 /* Look for the matching `)'. */
7938 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7939 return ansi_opname (CALL_EXPR);
7941 case CPP_OPEN_SQUARE:
7942 /* Consume the `['. */
7943 cp_lexer_consume_token (parser->lexer);
7944 /* Look for the matching `]'. */
7945 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7946 return ansi_opname (ARRAY_REF);
7950 id = ansi_opname (MIN_EXPR);
7954 id = ansi_opname (MAX_EXPR);
7958 id = ansi_assopname (MIN_EXPR);
7962 id = ansi_assopname (MAX_EXPR);
7966 /* Anything else is an error. */
7970 /* If we have selected an identifier, we need to consume the
7973 cp_lexer_consume_token (parser->lexer);
7974 /* Otherwise, no valid operator name was present. */
7977 cp_parser_error (parser, "expected operator");
7978 id = error_mark_node;
7984 /* Parse a template-declaration.
7986 template-declaration:
7987 export [opt] template < template-parameter-list > declaration
7989 If MEMBER_P is TRUE, this template-declaration occurs within a
7992 The grammar rule given by the standard isn't correct. What
7995 template-declaration:
7996 export [opt] template-parameter-list-seq
7997 decl-specifier-seq [opt] init-declarator [opt] ;
7998 export [opt] template-parameter-list-seq
8001 template-parameter-list-seq:
8002 template-parameter-list-seq [opt]
8003 template < template-parameter-list > */
8006 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8008 /* Check for `export'. */
8009 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8011 /* Consume the `export' token. */
8012 cp_lexer_consume_token (parser->lexer);
8013 /* Warn that we do not support `export'. */
8014 warning ("keyword `export' not implemented, and will be ignored");
8017 cp_parser_template_declaration_after_export (parser, member_p);
8020 /* Parse a template-parameter-list.
8022 template-parameter-list:
8024 template-parameter-list , template-parameter
8026 Returns a TREE_LIST. Each node represents a template parameter.
8027 The nodes are connected via their TREE_CHAINs. */
8030 cp_parser_template_parameter_list (cp_parser* parser)
8032 tree parameter_list = NULL_TREE;
8040 /* Parse the template-parameter. */
8041 parameter = cp_parser_template_parameter (parser, &is_non_type);
8042 /* Add it to the list. */
8043 parameter_list = process_template_parm (parameter_list,
8046 /* Peek at the next token. */
8047 token = cp_lexer_peek_token (parser->lexer);
8048 /* If it's not a `,', we're done. */
8049 if (token->type != CPP_COMMA)
8051 /* Otherwise, consume the `,' token. */
8052 cp_lexer_consume_token (parser->lexer);
8055 return parameter_list;
8058 /* Parse a template-parameter.
8062 parameter-declaration
8064 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8065 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8066 true iff this parameter is a non-type parameter. */
8069 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8072 cp_parameter_declarator *parameter_declarator;
8074 /* Assume it is a type parameter or a template parameter. */
8075 *is_non_type = false;
8076 /* Peek at the next token. */
8077 token = cp_lexer_peek_token (parser->lexer);
8078 /* If it is `class' or `template', we have a type-parameter. */
8079 if (token->keyword == RID_TEMPLATE)
8080 return cp_parser_type_parameter (parser);
8081 /* If it is `class' or `typename' we do not know yet whether it is a
8082 type parameter or a non-type parameter. Consider:
8084 template <typename T, typename T::X X> ...
8088 template <class C, class D*> ...
8090 Here, the first parameter is a type parameter, and the second is
8091 a non-type parameter. We can tell by looking at the token after
8092 the identifier -- if it is a `,', `=', or `>' then we have a type
8094 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8096 /* Peek at the token after `class' or `typename'. */
8097 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8098 /* If it's an identifier, skip it. */
8099 if (token->type == CPP_NAME)
8100 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8101 /* Now, see if the token looks like the end of a template
8103 if (token->type == CPP_COMMA
8104 || token->type == CPP_EQ
8105 || token->type == CPP_GREATER)
8106 return cp_parser_type_parameter (parser);
8109 /* Otherwise, it is a non-type parameter.
8113 When parsing a default template-argument for a non-type
8114 template-parameter, the first non-nested `>' is taken as the end
8115 of the template parameter-list rather than a greater-than
8117 *is_non_type = true;
8118 parameter_declarator
8119 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8120 /*parenthesized_p=*/NULL);
8121 return (build_tree_list
8122 (parameter_declarator->default_argument,
8123 grokdeclarator (parameter_declarator->declarator,
8124 ¶meter_declarator->decl_specifiers,
8125 PARM, /*initialized=*/0,
8126 /*attrlist=*/NULL)));
8129 /* Parse a type-parameter.
8132 class identifier [opt]
8133 class identifier [opt] = type-id
8134 typename identifier [opt]
8135 typename identifier [opt] = type-id
8136 template < template-parameter-list > class identifier [opt]
8137 template < template-parameter-list > class identifier [opt]
8140 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8141 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8142 the declaration of the parameter. */
8145 cp_parser_type_parameter (cp_parser* parser)
8150 /* Look for a keyword to tell us what kind of parameter this is. */
8151 token = cp_parser_require (parser, CPP_KEYWORD,
8152 "`class', `typename', or `template'");
8154 return error_mark_node;
8156 switch (token->keyword)
8162 tree default_argument;
8164 /* If the next token is an identifier, then it names the
8166 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8167 identifier = cp_parser_identifier (parser);
8169 identifier = NULL_TREE;
8171 /* Create the parameter. */
8172 parameter = finish_template_type_parm (class_type_node, identifier);
8174 /* If the next token is an `=', we have a default argument. */
8175 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8177 /* Consume the `=' token. */
8178 cp_lexer_consume_token (parser->lexer);
8179 /* Parse the default-argument. */
8180 default_argument = cp_parser_type_id (parser);
8183 default_argument = NULL_TREE;
8185 /* Create the combined representation of the parameter and the
8186 default argument. */
8187 parameter = build_tree_list (default_argument, parameter);
8193 tree parameter_list;
8195 tree default_argument;
8197 /* Look for the `<'. */
8198 cp_parser_require (parser, CPP_LESS, "`<'");
8199 /* Parse the template-parameter-list. */
8200 begin_template_parm_list ();
8202 = cp_parser_template_parameter_list (parser);
8203 parameter_list = end_template_parm_list (parameter_list);
8204 /* Look for the `>'. */
8205 cp_parser_require (parser, CPP_GREATER, "`>'");
8206 /* Look for the `class' keyword. */
8207 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8208 /* If the next token is an `=', then there is a
8209 default-argument. If the next token is a `>', we are at
8210 the end of the parameter-list. If the next token is a `,',
8211 then we are at the end of this parameter. */
8212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8213 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8214 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8215 identifier = cp_parser_identifier (parser);
8217 identifier = NULL_TREE;
8218 /* Create the template parameter. */
8219 parameter = finish_template_template_parm (class_type_node,
8222 /* If the next token is an `=', then there is a
8223 default-argument. */
8224 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8228 /* Consume the `='. */
8229 cp_lexer_consume_token (parser->lexer);
8230 /* Parse the id-expression. */
8232 = cp_parser_id_expression (parser,
8233 /*template_keyword_p=*/false,
8234 /*check_dependency_p=*/true,
8235 /*template_p=*/&is_template,
8236 /*declarator_p=*/false);
8237 if (TREE_CODE (default_argument) == TYPE_DECL)
8238 /* If the id-expression was a template-id that refers to
8239 a template-class, we already have the declaration here,
8240 so no further lookup is needed. */
8243 /* Look up the name. */
8245 = cp_parser_lookup_name (parser, default_argument,
8247 /*is_template=*/is_template,
8248 /*is_namespace=*/false,
8249 /*check_dependency=*/true,
8250 /*ambiguous_p=*/NULL);
8251 /* See if the default argument is valid. */
8253 = check_template_template_default_arg (default_argument);
8256 default_argument = NULL_TREE;
8258 /* Create the combined representation of the parameter and the
8259 default argument. */
8260 parameter = build_tree_list (default_argument, parameter);
8265 /* Anything else is an error. */
8266 cp_parser_error (parser,
8267 "expected `class', `typename', or `template'");
8268 parameter = error_mark_node;
8274 /* Parse a template-id.
8277 template-name < template-argument-list [opt] >
8279 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8280 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8281 returned. Otherwise, if the template-name names a function, or set
8282 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8283 names a class, returns a TYPE_DECL for the specialization.
8285 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8286 uninstantiated templates. */
8289 cp_parser_template_id (cp_parser *parser,
8290 bool template_keyword_p,
8291 bool check_dependency_p,
8292 bool is_declaration)
8297 ptrdiff_t start_of_id;
8298 tree access_check = NULL_TREE;
8299 cp_token *next_token, *next_token_2;
8302 /* If the next token corresponds to a template-id, there is no need
8304 next_token = cp_lexer_peek_token (parser->lexer);
8305 if (next_token->type == CPP_TEMPLATE_ID)
8310 /* Get the stored value. */
8311 value = cp_lexer_consume_token (parser->lexer)->value;
8312 /* Perform any access checks that were deferred. */
8313 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8314 perform_or_defer_access_check (TREE_PURPOSE (check),
8315 TREE_VALUE (check));
8316 /* Return the stored value. */
8317 return TREE_VALUE (value);
8320 /* Avoid performing name lookup if there is no possibility of
8321 finding a template-id. */
8322 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8323 || (next_token->type == CPP_NAME
8324 && !cp_parser_nth_token_starts_template_argument_list_p
8327 cp_parser_error (parser, "expected template-id");
8328 return error_mark_node;
8331 /* Remember where the template-id starts. */
8332 if (cp_parser_parsing_tentatively (parser)
8333 && !cp_parser_committed_to_tentative_parse (parser))
8335 next_token = cp_lexer_peek_token (parser->lexer);
8336 start_of_id = cp_lexer_token_difference (parser->lexer,
8337 parser->lexer->buffer,
8343 push_deferring_access_checks (dk_deferred);
8345 /* Parse the template-name. */
8346 is_identifier = false;
8347 template = cp_parser_template_name (parser, template_keyword_p,
8351 if (template == error_mark_node || is_identifier)
8353 pop_deferring_access_checks ();
8357 /* If we find the sequence `[:' after a template-name, it's probably
8358 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8359 parse correctly the argument list. */
8360 next_token = cp_lexer_peek_token (parser->lexer);
8361 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8362 if (next_token->type == CPP_OPEN_SQUARE
8363 && next_token->flags & DIGRAPH
8364 && next_token_2->type == CPP_COLON
8365 && !(next_token_2->flags & PREV_WHITE))
8367 cp_parser_parse_tentatively (parser);
8368 /* Change `:' into `::'. */
8369 next_token_2->type = CPP_SCOPE;
8370 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8372 cp_lexer_consume_token (parser->lexer);
8373 /* Parse the arguments. */
8374 arguments = cp_parser_enclosed_template_argument_list (parser);
8375 if (!cp_parser_parse_definitely (parser))
8377 /* If we couldn't parse an argument list, then we revert our changes
8378 and return simply an error. Maybe this is not a template-id
8380 next_token_2->type = CPP_COLON;
8381 cp_parser_error (parser, "expected `<'");
8382 pop_deferring_access_checks ();
8383 return error_mark_node;
8385 /* Otherwise, emit an error about the invalid digraph, but continue
8386 parsing because we got our argument list. */
8387 pedwarn ("`<::' cannot begin a template-argument list");
8388 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8389 "between `<' and `::'");
8390 if (!flag_permissive)
8395 inform ("(if you use `-fpermissive' G++ will accept your code)");
8402 /* Look for the `<' that starts the template-argument-list. */
8403 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8405 pop_deferring_access_checks ();
8406 return error_mark_node;
8408 /* Parse the arguments. */
8409 arguments = cp_parser_enclosed_template_argument_list (parser);
8412 /* Build a representation of the specialization. */
8413 if (TREE_CODE (template) == IDENTIFIER_NODE)
8414 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8415 else if (DECL_CLASS_TEMPLATE_P (template)
8416 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8418 = finish_template_type (template, arguments,
8419 cp_lexer_next_token_is (parser->lexer,
8423 /* If it's not a class-template or a template-template, it should be
8424 a function-template. */
8425 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8426 || TREE_CODE (template) == OVERLOAD
8427 || BASELINK_P (template)));
8429 template_id = lookup_template_function (template, arguments);
8432 /* Retrieve any deferred checks. Do not pop this access checks yet
8433 so the memory will not be reclaimed during token replacing below. */
8434 access_check = get_deferred_access_checks ();
8436 /* If parsing tentatively, replace the sequence of tokens that makes
8437 up the template-id with a CPP_TEMPLATE_ID token. That way,
8438 should we re-parse the token stream, we will not have to repeat
8439 the effort required to do the parse, nor will we issue duplicate
8440 error messages about problems during instantiation of the
8442 if (start_of_id >= 0)
8446 /* Find the token that corresponds to the start of the
8448 token = cp_lexer_advance_token (parser->lexer,
8449 parser->lexer->buffer,
8452 /* Reset the contents of the START_OF_ID token. */
8453 token->type = CPP_TEMPLATE_ID;
8454 token->value = build_tree_list (access_check, template_id);
8455 token->keyword = RID_MAX;
8456 /* Purge all subsequent tokens. */
8457 cp_lexer_purge_tokens_after (parser->lexer, token);
8460 pop_deferring_access_checks ();
8464 /* Parse a template-name.
8469 The standard should actually say:
8473 operator-function-id
8475 A defect report has been filed about this issue.
8477 A conversion-function-id cannot be a template name because they cannot
8478 be part of a template-id. In fact, looking at this code:
8482 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8483 It is impossible to call a templated conversion-function-id with an
8484 explicit argument list, since the only allowed template parameter is
8485 the type to which it is converting.
8487 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8488 `template' keyword, in a construction like:
8492 In that case `f' is taken to be a template-name, even though there
8493 is no way of knowing for sure.
8495 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8496 name refers to a set of overloaded functions, at least one of which
8497 is a template, or an IDENTIFIER_NODE with the name of the template,
8498 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8499 names are looked up inside uninstantiated templates. */
8502 cp_parser_template_name (cp_parser* parser,
8503 bool template_keyword_p,
8504 bool check_dependency_p,
8505 bool is_declaration,
8506 bool *is_identifier)
8512 /* If the next token is `operator', then we have either an
8513 operator-function-id or a conversion-function-id. */
8514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8516 /* We don't know whether we're looking at an
8517 operator-function-id or a conversion-function-id. */
8518 cp_parser_parse_tentatively (parser);
8519 /* Try an operator-function-id. */
8520 identifier = cp_parser_operator_function_id (parser);
8521 /* If that didn't work, try a conversion-function-id. */
8522 if (!cp_parser_parse_definitely (parser))
8524 cp_parser_error (parser, "expected template-name");
8525 return error_mark_node;
8528 /* Look for the identifier. */
8530 identifier = cp_parser_identifier (parser);
8532 /* If we didn't find an identifier, we don't have a template-id. */
8533 if (identifier == error_mark_node)
8534 return error_mark_node;
8536 /* If the name immediately followed the `template' keyword, then it
8537 is a template-name. However, if the next token is not `<', then
8538 we do not treat it as a template-name, since it is not being used
8539 as part of a template-id. This enables us to handle constructs
8542 template <typename T> struct S { S(); };
8543 template <typename T> S<T>::S();
8545 correctly. We would treat `S' as a template -- if it were `S<T>'
8546 -- but we do not if there is no `<'. */
8548 if (processing_template_decl
8549 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8551 /* In a declaration, in a dependent context, we pretend that the
8552 "template" keyword was present in order to improve error
8553 recovery. For example, given:
8555 template <typename T> void f(T::X<int>);
8557 we want to treat "X<int>" as a template-id. */
8559 && !template_keyword_p
8560 && parser->scope && TYPE_P (parser->scope)
8561 && check_dependency_p
8562 && dependent_type_p (parser->scope)
8563 /* Do not do this for dtors (or ctors), since they never
8564 need the template keyword before their name. */
8565 && !constructor_name_p (identifier, parser->scope))
8569 /* Explain what went wrong. */
8570 error ("non-template `%D' used as template", identifier);
8571 inform ("use `%T::template %D' to indicate that it is a template",
8572 parser->scope, identifier);
8573 /* If parsing tentatively, find the location of the "<"
8575 if (cp_parser_parsing_tentatively (parser)
8576 && !cp_parser_committed_to_tentative_parse (parser))
8578 cp_parser_simulate_error (parser);
8579 token = cp_lexer_peek_token (parser->lexer);
8580 token = cp_lexer_prev_token (parser->lexer, token);
8581 start = cp_lexer_token_difference (parser->lexer,
8582 parser->lexer->buffer,
8587 /* Parse the template arguments so that we can issue error
8588 messages about them. */
8589 cp_lexer_consume_token (parser->lexer);
8590 cp_parser_enclosed_template_argument_list (parser);
8591 /* Skip tokens until we find a good place from which to
8592 continue parsing. */
8593 cp_parser_skip_to_closing_parenthesis (parser,
8594 /*recovering=*/true,
8596 /*consume_paren=*/false);
8597 /* If parsing tentatively, permanently remove the
8598 template argument list. That will prevent duplicate
8599 error messages from being issued about the missing
8600 "template" keyword. */
8603 token = cp_lexer_advance_token (parser->lexer,
8604 parser->lexer->buffer,
8606 cp_lexer_purge_tokens_after (parser->lexer, token);
8609 *is_identifier = true;
8613 /* If the "template" keyword is present, then there is generally
8614 no point in doing name-lookup, so we just return IDENTIFIER.
8615 But, if the qualifying scope is non-dependent then we can
8616 (and must) do name-lookup normally. */
8617 if (template_keyword_p
8619 || (TYPE_P (parser->scope)
8620 && dependent_type_p (parser->scope))))
8624 /* Look up the name. */
8625 decl = cp_parser_lookup_name (parser, identifier,
8627 /*is_template=*/false,
8628 /*is_namespace=*/false,
8630 /*ambiguous_p=*/NULL);
8631 decl = maybe_get_template_decl_from_type_decl (decl);
8633 /* If DECL is a template, then the name was a template-name. */
8634 if (TREE_CODE (decl) == TEMPLATE_DECL)
8638 /* The standard does not explicitly indicate whether a name that
8639 names a set of overloaded declarations, some of which are
8640 templates, is a template-name. However, such a name should
8641 be a template-name; otherwise, there is no way to form a
8642 template-id for the overloaded templates. */
8643 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8644 if (TREE_CODE (fns) == OVERLOAD)
8648 for (fn = fns; fn; fn = OVL_NEXT (fn))
8649 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8654 /* Otherwise, the name does not name a template. */
8655 cp_parser_error (parser, "expected template-name");
8656 return error_mark_node;
8660 /* If DECL is dependent, and refers to a function, then just return
8661 its name; we will look it up again during template instantiation. */
8662 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8664 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8665 if (TYPE_P (scope) && dependent_type_p (scope))
8672 /* Parse a template-argument-list.
8674 template-argument-list:
8676 template-argument-list , template-argument
8678 Returns a TREE_VEC containing the arguments. */
8681 cp_parser_template_argument_list (cp_parser* parser)
8683 tree fixed_args[10];
8684 unsigned n_args = 0;
8685 unsigned alloced = 10;
8686 tree *arg_ary = fixed_args;
8688 bool saved_in_template_argument_list_p;
8690 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8691 parser->in_template_argument_list_p = true;
8697 /* Consume the comma. */
8698 cp_lexer_consume_token (parser->lexer);
8700 /* Parse the template-argument. */
8701 argument = cp_parser_template_argument (parser);
8702 if (n_args == alloced)
8706 if (arg_ary == fixed_args)
8708 arg_ary = xmalloc (sizeof (tree) * alloced);
8709 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8712 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8714 arg_ary[n_args++] = argument;
8716 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8718 vec = make_tree_vec (n_args);
8721 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8723 if (arg_ary != fixed_args)
8725 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8729 /* Parse a template-argument.
8732 assignment-expression
8736 The representation is that of an assignment-expression, type-id, or
8737 id-expression -- except that the qualified id-expression is
8738 evaluated, so that the value returned is either a DECL or an
8741 Although the standard says "assignment-expression", it forbids
8742 throw-expressions or assignments in the template argument.
8743 Therefore, we use "conditional-expression" instead. */
8746 cp_parser_template_argument (cp_parser* parser)
8751 bool maybe_type_id = false;
8754 tree qualifying_class;
8756 /* There's really no way to know what we're looking at, so we just
8757 try each alternative in order.
8761 In a template-argument, an ambiguity between a type-id and an
8762 expression is resolved to a type-id, regardless of the form of
8763 the corresponding template-parameter.
8765 Therefore, we try a type-id first. */
8766 cp_parser_parse_tentatively (parser);
8767 argument = cp_parser_type_id (parser);
8768 /* If there was no error parsing the type-id but the next token is a '>>',
8769 we probably found a typo for '> >'. But there are type-id which are
8770 also valid expressions. For instance:
8772 struct X { int operator >> (int); };
8773 template <int V> struct Foo {};
8776 Here 'X()' is a valid type-id of a function type, but the user just
8777 wanted to write the expression "X() >> 5". Thus, we remember that we
8778 found a valid type-id, but we still try to parse the argument as an
8779 expression to see what happens. */
8780 if (!cp_parser_error_occurred (parser)
8781 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8783 maybe_type_id = true;
8784 cp_parser_abort_tentative_parse (parser);
8788 /* If the next token isn't a `,' or a `>', then this argument wasn't
8789 really finished. This means that the argument is not a valid
8791 if (!cp_parser_next_token_ends_template_argument_p (parser))
8792 cp_parser_error (parser, "expected template-argument");
8793 /* If that worked, we're done. */
8794 if (cp_parser_parse_definitely (parser))
8797 /* We're still not sure what the argument will be. */
8798 cp_parser_parse_tentatively (parser);
8799 /* Try a template. */
8800 argument = cp_parser_id_expression (parser,
8801 /*template_keyword_p=*/false,
8802 /*check_dependency_p=*/true,
8804 /*declarator_p=*/false);
8805 /* If the next token isn't a `,' or a `>', then this argument wasn't
8807 if (!cp_parser_next_token_ends_template_argument_p (parser))
8808 cp_parser_error (parser, "expected template-argument");
8809 if (!cp_parser_error_occurred (parser))
8811 /* Figure out what is being referred to. If the id-expression
8812 was for a class template specialization, then we will have a
8813 TYPE_DECL at this point. There is no need to do name lookup
8814 at this point in that case. */
8815 if (TREE_CODE (argument) != TYPE_DECL)
8816 argument = cp_parser_lookup_name (parser, argument,
8818 /*is_template=*/template_p,
8819 /*is_namespace=*/false,
8820 /*check_dependency=*/true,
8821 /*ambiguous_p=*/NULL);
8822 if (TREE_CODE (argument) != TEMPLATE_DECL
8823 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8824 cp_parser_error (parser, "expected template-name");
8826 if (cp_parser_parse_definitely (parser))
8828 /* It must be a non-type argument. There permitted cases are given
8829 in [temp.arg.nontype]:
8831 -- an integral constant-expression of integral or enumeration
8834 -- the name of a non-type template-parameter; or
8836 -- the name of an object or function with external linkage...
8838 -- the address of an object or function with external linkage...
8840 -- a pointer to member... */
8841 /* Look for a non-type template parameter. */
8842 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8844 cp_parser_parse_tentatively (parser);
8845 argument = cp_parser_primary_expression (parser,
8848 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8849 || !cp_parser_next_token_ends_template_argument_p (parser))
8850 cp_parser_simulate_error (parser);
8851 if (cp_parser_parse_definitely (parser))
8854 /* If the next token is "&", the argument must be the address of an
8855 object or function with external linkage. */
8856 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8858 cp_lexer_consume_token (parser->lexer);
8859 /* See if we might have an id-expression. */
8860 token = cp_lexer_peek_token (parser->lexer);
8861 if (token->type == CPP_NAME
8862 || token->keyword == RID_OPERATOR
8863 || token->type == CPP_SCOPE
8864 || token->type == CPP_TEMPLATE_ID
8865 || token->type == CPP_NESTED_NAME_SPECIFIER)
8867 cp_parser_parse_tentatively (parser);
8868 argument = cp_parser_primary_expression (parser,
8871 if (cp_parser_error_occurred (parser)
8872 || !cp_parser_next_token_ends_template_argument_p (parser))
8873 cp_parser_abort_tentative_parse (parser);
8876 if (qualifying_class)
8877 argument = finish_qualified_id_expr (qualifying_class,
8881 if (TREE_CODE (argument) == VAR_DECL)
8883 /* A variable without external linkage might still be a
8884 valid constant-expression, so no error is issued here
8885 if the external-linkage check fails. */
8886 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8887 cp_parser_simulate_error (parser);
8889 else if (is_overloaded_fn (argument))
8890 /* All overloaded functions are allowed; if the external
8891 linkage test does not pass, an error will be issued
8895 && (TREE_CODE (argument) == OFFSET_REF
8896 || TREE_CODE (argument) == SCOPE_REF))
8897 /* A pointer-to-member. */
8900 cp_parser_simulate_error (parser);
8902 if (cp_parser_parse_definitely (parser))
8905 argument = build_x_unary_op (ADDR_EXPR, argument);
8910 /* If the argument started with "&", there are no other valid
8911 alternatives at this point. */
8914 cp_parser_error (parser, "invalid non-type template argument");
8915 return error_mark_node;
8917 /* If the argument wasn't successfully parsed as a type-id followed
8918 by '>>', the argument can only be a constant expression now.
8919 Otherwise, we try parsing the constant-expression tentatively,
8920 because the argument could really be a type-id. */
8922 cp_parser_parse_tentatively (parser);
8923 argument = cp_parser_constant_expression (parser,
8924 /*allow_non_constant_p=*/false,
8925 /*non_constant_p=*/NULL);
8926 argument = fold_non_dependent_expr (argument);
8929 if (!cp_parser_next_token_ends_template_argument_p (parser))
8930 cp_parser_error (parser, "expected template-argument");
8931 if (cp_parser_parse_definitely (parser))
8933 /* We did our best to parse the argument as a non type-id, but that
8934 was the only alternative that matched (albeit with a '>' after
8935 it). We can assume it's just a typo from the user, and a
8936 diagnostic will then be issued. */
8937 return cp_parser_type_id (parser);
8940 /* Parse an explicit-instantiation.
8942 explicit-instantiation:
8943 template declaration
8945 Although the standard says `declaration', what it really means is:
8947 explicit-instantiation:
8948 template decl-specifier-seq [opt] declarator [opt] ;
8950 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8951 supposed to be allowed. A defect report has been filed about this
8956 explicit-instantiation:
8957 storage-class-specifier template
8958 decl-specifier-seq [opt] declarator [opt] ;
8959 function-specifier template
8960 decl-specifier-seq [opt] declarator [opt] ; */
8963 cp_parser_explicit_instantiation (cp_parser* parser)
8965 int declares_class_or_enum;
8966 cp_decl_specifier_seq decl_specifiers;
8967 tree extension_specifier = NULL_TREE;
8969 /* Look for an (optional) storage-class-specifier or
8970 function-specifier. */
8971 if (cp_parser_allow_gnu_extensions_p (parser))
8974 = cp_parser_storage_class_specifier_opt (parser);
8975 if (!extension_specifier)
8977 = cp_parser_function_specifier_opt (parser,
8978 /*decl_specs=*/NULL);
8981 /* Look for the `template' keyword. */
8982 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8983 /* Let the front end know that we are processing an explicit
8985 begin_explicit_instantiation ();
8986 /* [temp.explicit] says that we are supposed to ignore access
8987 control while processing explicit instantiation directives. */
8988 push_deferring_access_checks (dk_no_check);
8989 /* Parse a decl-specifier-seq. */
8990 cp_parser_decl_specifier_seq (parser,
8991 CP_PARSER_FLAGS_OPTIONAL,
8993 &declares_class_or_enum);
8994 /* If there was exactly one decl-specifier, and it declared a class,
8995 and there's no declarator, then we have an explicit type
8997 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9001 type = check_tag_decl (&decl_specifiers);
9002 /* Turn access control back on for names used during
9003 template instantiation. */
9004 pop_deferring_access_checks ();
9006 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9010 cp_declarator *declarator;
9013 /* Parse the declarator. */
9015 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9016 /*ctor_dtor_or_conv_p=*/NULL,
9017 /*parenthesized_p=*/NULL);
9018 cp_parser_check_for_definition_in_return_type (declarator,
9019 declares_class_or_enum);
9020 if (declarator != cp_error_declarator)
9022 decl = grokdeclarator (declarator, &decl_specifiers,
9024 /* Turn access control back on for names used during
9025 template instantiation. */
9026 pop_deferring_access_checks ();
9027 /* Do the explicit instantiation. */
9028 do_decl_instantiation (decl, extension_specifier);
9032 pop_deferring_access_checks ();
9033 /* Skip the body of the explicit instantiation. */
9034 cp_parser_skip_to_end_of_statement (parser);
9037 /* We're done with the instantiation. */
9038 end_explicit_instantiation ();
9040 cp_parser_consume_semicolon_at_end_of_statement (parser);
9043 /* Parse an explicit-specialization.
9045 explicit-specialization:
9046 template < > declaration
9048 Although the standard says `declaration', what it really means is:
9050 explicit-specialization:
9051 template <> decl-specifier [opt] init-declarator [opt] ;
9052 template <> function-definition
9053 template <> explicit-specialization
9054 template <> template-declaration */
9057 cp_parser_explicit_specialization (cp_parser* parser)
9059 /* Look for the `template' keyword. */
9060 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9061 /* Look for the `<'. */
9062 cp_parser_require (parser, CPP_LESS, "`<'");
9063 /* Look for the `>'. */
9064 cp_parser_require (parser, CPP_GREATER, "`>'");
9065 /* We have processed another parameter list. */
9066 ++parser->num_template_parameter_lists;
9067 /* Let the front end know that we are beginning a specialization. */
9068 begin_specialization ();
9070 /* If the next keyword is `template', we need to figure out whether
9071 or not we're looking a template-declaration. */
9072 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9074 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9075 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9076 cp_parser_template_declaration_after_export (parser,
9077 /*member_p=*/false);
9079 cp_parser_explicit_specialization (parser);
9082 /* Parse the dependent declaration. */
9083 cp_parser_single_declaration (parser,
9087 /* We're done with the specialization. */
9088 end_specialization ();
9089 /* We're done with this parameter list. */
9090 --parser->num_template_parameter_lists;
9093 /* Parse a type-specifier.
9096 simple-type-specifier
9099 elaborated-type-specifier
9107 Returns a representation of the type-specifier. For a
9108 class-specifier, enum-specifier, or elaborated-type-specifier, a
9109 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9111 The parser flags FLAGS is used to control type-specifier parsing.
9113 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9114 in a decl-specifier-seq.
9116 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9117 class-specifier, enum-specifier, or elaborated-type-specifier, then
9118 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9119 if a type is declared; 2 if it is defined. Otherwise, it is set to
9122 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9123 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9127 cp_parser_type_specifier (cp_parser* parser,
9128 cp_parser_flags flags,
9129 cp_decl_specifier_seq *decl_specs,
9130 bool is_declaration,
9131 int* declares_class_or_enum,
9132 bool* is_cv_qualifier)
9134 tree type_spec = NULL_TREE;
9137 cp_decl_spec ds = ds_last;
9139 /* Assume this type-specifier does not declare a new type. */
9140 if (declares_class_or_enum)
9141 *declares_class_or_enum = 0;
9142 /* And that it does not specify a cv-qualifier. */
9143 if (is_cv_qualifier)
9144 *is_cv_qualifier = false;
9145 /* Peek at the next token. */
9146 token = cp_lexer_peek_token (parser->lexer);
9148 /* If we're looking at a keyword, we can use that to guide the
9149 production we choose. */
9150 keyword = token->keyword;
9154 /* 'enum' [identifier] '{' introduces an enum-specifier;
9155 'enum' <anything else> introduces an elaborated-type-specifier. */
9156 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9157 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9158 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9161 type_spec = cp_parser_enum_specifier (parser);
9162 if (declares_class_or_enum)
9163 *declares_class_or_enum = 2;
9165 cp_parser_set_decl_spec_type (decl_specs,
9167 /*user_defined_p=*/true);
9171 goto elaborated_type_specifier;
9173 /* Any of these indicate either a class-specifier, or an
9174 elaborated-type-specifier. */
9178 /* Parse tentatively so that we can back up if we don't find a
9180 cp_parser_parse_tentatively (parser);
9181 /* Look for the class-specifier. */
9182 type_spec = cp_parser_class_specifier (parser);
9183 /* If that worked, we're done. */
9184 if (cp_parser_parse_definitely (parser))
9186 if (declares_class_or_enum)
9187 *declares_class_or_enum = 2;
9189 cp_parser_set_decl_spec_type (decl_specs,
9191 /*user_defined_p=*/true);
9196 elaborated_type_specifier:
9197 /* We're declaring (not defining) a class or enum. */
9198 if (declares_class_or_enum)
9199 *declares_class_or_enum = 1;
9203 /* Look for an elaborated-type-specifier. */
9205 = (cp_parser_elaborated_type_specifier
9207 decl_specs && decl_specs->specs[(int) ds_friend],
9210 cp_parser_set_decl_spec_type (decl_specs,
9212 /*user_defined_p=*/true);
9217 if (is_cv_qualifier)
9218 *is_cv_qualifier = true;
9223 if (is_cv_qualifier)
9224 *is_cv_qualifier = true;
9229 if (is_cv_qualifier)
9230 *is_cv_qualifier = true;
9234 /* The `__complex__' keyword is a GNU extension. */
9242 /* Handle simple keywords. */
9247 ++decl_specs->specs[(int)ds];
9248 decl_specs->any_specifiers_p = true;
9250 return cp_lexer_consume_token (parser->lexer)->value;
9253 /* If we do not already have a type-specifier, assume we are looking
9254 at a simple-type-specifier. */
9255 type_spec = cp_parser_simple_type_specifier (parser,
9259 /* If we didn't find a type-specifier, and a type-specifier was not
9260 optional in this context, issue an error message. */
9261 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9263 cp_parser_error (parser, "expected type specifier");
9264 return error_mark_node;
9270 /* Parse a simple-type-specifier.
9272 simple-type-specifier:
9273 :: [opt] nested-name-specifier [opt] type-name
9274 :: [opt] nested-name-specifier template template-id
9289 simple-type-specifier:
9290 __typeof__ unary-expression
9291 __typeof__ ( type-id )
9293 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9294 appropriately updated. */
9297 cp_parser_simple_type_specifier (cp_parser* parser,
9298 cp_decl_specifier_seq *decl_specs,
9299 cp_parser_flags flags)
9301 tree type = NULL_TREE;
9304 /* Peek at the next token. */
9305 token = cp_lexer_peek_token (parser->lexer);
9307 /* If we're looking at a keyword, things are easy. */
9308 switch (token->keyword)
9312 decl_specs->explicit_char_p = true;
9313 type = char_type_node;
9316 type = wchar_type_node;
9319 type = boolean_type_node;
9323 ++decl_specs->specs[(int) ds_short];
9324 type = short_integer_type_node;
9328 decl_specs->explicit_int_p = true;
9329 type = integer_type_node;
9333 ++decl_specs->specs[(int) ds_long];
9334 type = long_integer_type_node;
9338 ++decl_specs->specs[(int) ds_signed];
9339 type = integer_type_node;
9343 ++decl_specs->specs[(int) ds_unsigned];
9344 type = unsigned_type_node;
9347 type = float_type_node;
9350 type = double_type_node;
9353 type = void_type_node;
9357 /* Consume the `typeof' token. */
9358 cp_lexer_consume_token (parser->lexer);
9359 /* Parse the operand to `typeof'. */
9360 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9361 /* If it is not already a TYPE, take its type. */
9363 type = finish_typeof (type);
9366 cp_parser_set_decl_spec_type (decl_specs, type,
9367 /*user_defined_p=*/true);
9375 /* If the type-specifier was for a built-in type, we're done. */
9380 /* Record the type. */
9382 && (token->keyword != RID_SIGNED
9383 && token->keyword != RID_UNSIGNED
9384 && token->keyword != RID_SHORT
9385 && token->keyword != RID_LONG))
9386 cp_parser_set_decl_spec_type (decl_specs,
9388 /*user_defined=*/false);
9390 decl_specs->any_specifiers_p = true;
9392 /* Consume the token. */
9393 id = cp_lexer_consume_token (parser->lexer)->value;
9395 /* There is no valid C++ program where a non-template type is
9396 followed by a "<". That usually indicates that the user thought
9397 that the type was a template. */
9398 cp_parser_check_for_invalid_template_id (parser, type);
9400 return TYPE_NAME (type);
9403 /* The type-specifier must be a user-defined type. */
9404 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9409 /* Don't gobble tokens or issue error messages if this is an
9410 optional type-specifier. */
9411 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9412 cp_parser_parse_tentatively (parser);
9414 /* Look for the optional `::' operator. */
9416 = (cp_parser_global_scope_opt (parser,
9417 /*current_scope_valid_p=*/false)
9419 /* Look for the nested-name specifier. */
9421 = (cp_parser_nested_name_specifier_opt (parser,
9422 /*typename_keyword_p=*/false,
9423 /*check_dependency_p=*/true,
9425 /*is_declaration=*/false)
9427 /* If we have seen a nested-name-specifier, and the next token
9428 is `template', then we are using the template-id production. */
9430 && cp_parser_optional_template_keyword (parser))
9432 /* Look for the template-id. */
9433 type = cp_parser_template_id (parser,
9434 /*template_keyword_p=*/true,
9435 /*check_dependency_p=*/true,
9436 /*is_declaration=*/false);
9437 /* If the template-id did not name a type, we are out of
9439 if (TREE_CODE (type) != TYPE_DECL)
9441 cp_parser_error (parser, "expected template-id for type");
9445 /* Otherwise, look for a type-name. */
9447 type = cp_parser_type_name (parser);
9448 /* Keep track of all name-lookups performed in class scopes. */
9452 && TREE_CODE (type) == TYPE_DECL
9453 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9454 maybe_note_name_used_in_class (DECL_NAME (type), type);
9455 /* If it didn't work out, we don't have a TYPE. */
9456 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9457 && !cp_parser_parse_definitely (parser))
9459 if (type && decl_specs)
9460 cp_parser_set_decl_spec_type (decl_specs, type,
9461 /*user_defined=*/true);
9464 /* If we didn't get a type-name, issue an error message. */
9465 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9467 cp_parser_error (parser, "expected type-name");
9468 return error_mark_node;
9471 /* There is no valid C++ program where a non-template type is
9472 followed by a "<". That usually indicates that the user thought
9473 that the type was a template. */
9474 if (type && type != error_mark_node)
9475 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9480 /* Parse a type-name.
9493 Returns a TYPE_DECL for the the type. */
9496 cp_parser_type_name (cp_parser* parser)
9501 /* We can't know yet whether it is a class-name or not. */
9502 cp_parser_parse_tentatively (parser);
9503 /* Try a class-name. */
9504 type_decl = cp_parser_class_name (parser,
9505 /*typename_keyword_p=*/false,
9506 /*template_keyword_p=*/false,
9508 /*check_dependency_p=*/true,
9509 /*class_head_p=*/false,
9510 /*is_declaration=*/false);
9511 /* If it's not a class-name, keep looking. */
9512 if (!cp_parser_parse_definitely (parser))
9514 /* It must be a typedef-name or an enum-name. */
9515 identifier = cp_parser_identifier (parser);
9516 if (identifier == error_mark_node)
9517 return error_mark_node;
9519 /* Look up the type-name. */
9520 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9521 /* Issue an error if we did not find a type-name. */
9522 if (TREE_CODE (type_decl) != TYPE_DECL)
9524 if (!cp_parser_simulate_error (parser))
9525 cp_parser_name_lookup_error (parser, identifier, type_decl,
9527 type_decl = error_mark_node;
9529 /* Remember that the name was used in the definition of the
9530 current class so that we can check later to see if the
9531 meaning would have been different after the class was
9532 entirely defined. */
9533 else if (type_decl != error_mark_node
9535 maybe_note_name_used_in_class (identifier, type_decl);
9542 /* Parse an elaborated-type-specifier. Note that the grammar given
9543 here incorporates the resolution to DR68.
9545 elaborated-type-specifier:
9546 class-key :: [opt] nested-name-specifier [opt] identifier
9547 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9548 enum :: [opt] nested-name-specifier [opt] identifier
9549 typename :: [opt] nested-name-specifier identifier
9550 typename :: [opt] nested-name-specifier template [opt]
9555 elaborated-type-specifier:
9556 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9557 class-key attributes :: [opt] nested-name-specifier [opt]
9558 template [opt] template-id
9559 enum attributes :: [opt] nested-name-specifier [opt] identifier
9561 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9562 declared `friend'. If IS_DECLARATION is TRUE, then this
9563 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9564 something is being declared.
9566 Returns the TYPE specified. */
9569 cp_parser_elaborated_type_specifier (cp_parser* parser,
9571 bool is_declaration)
9573 enum tag_types tag_type;
9575 tree type = NULL_TREE;
9576 tree attributes = NULL_TREE;
9578 /* See if we're looking at the `enum' keyword. */
9579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9581 /* Consume the `enum' token. */
9582 cp_lexer_consume_token (parser->lexer);
9583 /* Remember that it's an enumeration type. */
9584 tag_type = enum_type;
9585 /* Parse the attributes. */
9586 attributes = cp_parser_attributes_opt (parser);
9588 /* Or, it might be `typename'. */
9589 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9592 /* Consume the `typename' token. */
9593 cp_lexer_consume_token (parser->lexer);
9594 /* Remember that it's a `typename' type. */
9595 tag_type = typename_type;
9596 /* The `typename' keyword is only allowed in templates. */
9597 if (!processing_template_decl)
9598 pedwarn ("using `typename' outside of template");
9600 /* Otherwise it must be a class-key. */
9603 tag_type = cp_parser_class_key (parser);
9604 if (tag_type == none_type)
9605 return error_mark_node;
9606 /* Parse the attributes. */
9607 attributes = cp_parser_attributes_opt (parser);
9610 /* Look for the `::' operator. */
9611 cp_parser_global_scope_opt (parser,
9612 /*current_scope_valid_p=*/false);
9613 /* Look for the nested-name-specifier. */
9614 if (tag_type == typename_type)
9616 if (cp_parser_nested_name_specifier (parser,
9617 /*typename_keyword_p=*/true,
9618 /*check_dependency_p=*/true,
9622 return error_mark_node;
9625 /* Even though `typename' is not present, the proposed resolution
9626 to Core Issue 180 says that in `class A<T>::B', `B' should be
9627 considered a type-name, even if `A<T>' is dependent. */
9628 cp_parser_nested_name_specifier_opt (parser,
9629 /*typename_keyword_p=*/true,
9630 /*check_dependency_p=*/true,
9633 /* For everything but enumeration types, consider a template-id. */
9634 if (tag_type != enum_type)
9636 bool template_p = false;
9639 /* Allow the `template' keyword. */
9640 template_p = cp_parser_optional_template_keyword (parser);
9641 /* If we didn't see `template', we don't know if there's a
9642 template-id or not. */
9644 cp_parser_parse_tentatively (parser);
9645 /* Parse the template-id. */
9646 decl = cp_parser_template_id (parser, template_p,
9647 /*check_dependency_p=*/true,
9649 /* If we didn't find a template-id, look for an ordinary
9651 if (!template_p && !cp_parser_parse_definitely (parser))
9653 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9654 in effect, then we must assume that, upon instantiation, the
9655 template will correspond to a class. */
9656 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9657 && tag_type == typename_type)
9658 type = make_typename_type (parser->scope, decl,
9661 type = TREE_TYPE (decl);
9664 /* For an enumeration type, consider only a plain identifier. */
9667 identifier = cp_parser_identifier (parser);
9669 if (identifier == error_mark_node)
9671 parser->scope = NULL_TREE;
9672 return error_mark_node;
9675 /* For a `typename', we needn't call xref_tag. */
9676 if (tag_type == typename_type)
9677 return cp_parser_make_typename_type (parser, parser->scope,
9679 /* Look up a qualified name in the usual way. */
9684 /* In an elaborated-type-specifier, names are assumed to name
9685 types, so we set IS_TYPE to TRUE when calling
9686 cp_parser_lookup_name. */
9687 decl = cp_parser_lookup_name (parser, identifier,
9689 /*is_template=*/false,
9690 /*is_namespace=*/false,
9691 /*check_dependency=*/true,
9692 /*ambiguous_p=*/NULL);
9694 /* If we are parsing friend declaration, DECL may be a
9695 TEMPLATE_DECL tree node here. However, we need to check
9696 whether this TEMPLATE_DECL results in valid code. Consider
9697 the following example:
9700 template <class T> class C {};
9703 template <class T> friend class N::C; // #1, valid code
9705 template <class T> class Y {
9706 friend class N::C; // #2, invalid code
9709 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9710 name lookup of `N::C'. We see that friend declaration must
9711 be template for the code to be valid. Note that
9712 processing_template_decl does not work here since it is
9713 always 1 for the above two cases. */
9715 decl = (cp_parser_maybe_treat_template_as_class
9716 (decl, /*tag_name_p=*/is_friend
9717 && parser->num_template_parameter_lists));
9719 if (TREE_CODE (decl) != TYPE_DECL)
9721 error ("expected type-name");
9722 return error_mark_node;
9725 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9726 check_elaborated_type_specifier
9728 (parser->num_template_parameter_lists
9729 || DECL_SELF_REFERENCE_P (decl)));
9731 type = TREE_TYPE (decl);
9735 /* An elaborated-type-specifier sometimes introduces a new type and
9736 sometimes names an existing type. Normally, the rule is that it
9737 introduces a new type only if there is not an existing type of
9738 the same name already in scope. For example, given:
9741 void f() { struct S s; }
9743 the `struct S' in the body of `f' is the same `struct S' as in
9744 the global scope; the existing definition is used. However, if
9745 there were no global declaration, this would introduce a new
9746 local class named `S'.
9748 An exception to this rule applies to the following code:
9750 namespace N { struct S; }
9752 Here, the elaborated-type-specifier names a new type
9753 unconditionally; even if there is already an `S' in the
9754 containing scope this declaration names a new type.
9755 This exception only applies if the elaborated-type-specifier
9756 forms the complete declaration:
9760 A declaration consisting solely of `class-key identifier ;' is
9761 either a redeclaration of the name in the current scope or a
9762 forward declaration of the identifier as a class name. It
9763 introduces the name into the current scope.
9765 We are in this situation precisely when the next token is a `;'.
9767 An exception to the exception is that a `friend' declaration does
9768 *not* name a new type; i.e., given:
9770 struct S { friend struct T; };
9772 `T' is not a new type in the scope of `S'.
9774 Also, `new struct S' or `sizeof (struct S)' never results in the
9775 definition of a new type; a new type can only be declared in a
9776 declaration context. */
9778 /* Warn about attributes. They are ignored. */
9780 warning ("type attributes are honored only at type definition");
9782 type = xref_tag (tag_type, identifier,
9785 || cp_lexer_next_token_is_not (parser->lexer,
9787 parser->num_template_parameter_lists);
9790 if (tag_type != enum_type)
9791 cp_parser_check_class_key (tag_type, type);
9793 /* A "<" cannot follow an elaborated type specifier. If that
9794 happens, the user was probably trying to form a template-id. */
9795 cp_parser_check_for_invalid_template_id (parser, type);
9800 /* Parse an enum-specifier.
9803 enum identifier [opt] { enumerator-list [opt] }
9805 Returns an ENUM_TYPE representing the enumeration. */
9808 cp_parser_enum_specifier (cp_parser* parser)
9813 /* Caller guarantees that the current token is 'enum', an identifier
9814 possibly follows, and the token after that is an opening brace.
9815 If we don't have an identifier, fabricate an anonymous name for
9816 the enumeration being defined. */
9817 cp_lexer_consume_token (parser->lexer);
9819 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9820 identifier = cp_parser_identifier (parser);
9822 identifier = make_anon_name ();
9824 /* Issue an error message if type-definitions are forbidden here. */
9825 cp_parser_check_type_definition (parser);
9827 /* Create the new type. We do this before consuming the opening brace
9828 so the enum will be recorded as being on the line of its tag (or the
9829 'enum' keyword, if there is no tag). */
9830 type = start_enum (identifier);
9832 /* Consume the opening brace. */
9833 cp_lexer_consume_token (parser->lexer);
9835 /* If the next token is not '}', then there are some enumerators. */
9836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9837 cp_parser_enumerator_list (parser, type);
9839 /* Consume the final '}'. */
9840 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9842 /* Finish up the enumeration. */
9848 /* Parse an enumerator-list. The enumerators all have the indicated
9852 enumerator-definition
9853 enumerator-list , enumerator-definition */
9856 cp_parser_enumerator_list (cp_parser* parser, tree type)
9860 /* Parse an enumerator-definition. */
9861 cp_parser_enumerator_definition (parser, type);
9863 /* If the next token is not a ',', we've reached the end of
9865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9867 /* Otherwise, consume the `,' and keep going. */
9868 cp_lexer_consume_token (parser->lexer);
9869 /* If the next token is a `}', there is a trailing comma. */
9870 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9872 if (pedantic && !in_system_header)
9873 pedwarn ("comma at end of enumerator list");
9879 /* Parse an enumerator-definition. The enumerator has the indicated
9882 enumerator-definition:
9884 enumerator = constant-expression
9890 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9895 /* Look for the identifier. */
9896 identifier = cp_parser_identifier (parser);
9897 if (identifier == error_mark_node)
9900 /* If the next token is an '=', then there is an explicit value. */
9901 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9903 /* Consume the `=' token. */
9904 cp_lexer_consume_token (parser->lexer);
9905 /* Parse the value. */
9906 value = cp_parser_constant_expression (parser,
9907 /*allow_non_constant_p=*/false,
9913 /* Create the enumerator. */
9914 build_enumerator (identifier, value, type);
9917 /* Parse a namespace-name.
9920 original-namespace-name
9923 Returns the NAMESPACE_DECL for the namespace. */
9926 cp_parser_namespace_name (cp_parser* parser)
9929 tree namespace_decl;
9931 /* Get the name of the namespace. */
9932 identifier = cp_parser_identifier (parser);
9933 if (identifier == error_mark_node)
9934 return error_mark_node;
9936 /* Look up the identifier in the currently active scope. Look only
9937 for namespaces, due to:
9941 When looking up a namespace-name in a using-directive or alias
9942 definition, only namespace names are considered.
9948 During the lookup of a name preceding the :: scope resolution
9949 operator, object, function, and enumerator names are ignored.
9951 (Note that cp_parser_class_or_namespace_name only calls this
9952 function if the token after the name is the scope resolution
9954 namespace_decl = cp_parser_lookup_name (parser, identifier,
9956 /*is_template=*/false,
9957 /*is_namespace=*/true,
9958 /*check_dependency=*/true,
9959 /*ambiguous_p=*/NULL);
9960 /* If it's not a namespace, issue an error. */
9961 if (namespace_decl == error_mark_node
9962 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9964 cp_parser_error (parser, "expected namespace-name");
9965 namespace_decl = error_mark_node;
9968 return namespace_decl;
9971 /* Parse a namespace-definition.
9973 namespace-definition:
9974 named-namespace-definition
9975 unnamed-namespace-definition
9977 named-namespace-definition:
9978 original-namespace-definition
9979 extension-namespace-definition
9981 original-namespace-definition:
9982 namespace identifier { namespace-body }
9984 extension-namespace-definition:
9985 namespace original-namespace-name { namespace-body }
9987 unnamed-namespace-definition:
9988 namespace { namespace-body } */
9991 cp_parser_namespace_definition (cp_parser* parser)
9995 /* Look for the `namespace' keyword. */
9996 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9998 /* Get the name of the namespace. We do not attempt to distinguish
9999 between an original-namespace-definition and an
10000 extension-namespace-definition at this point. The semantic
10001 analysis routines are responsible for that. */
10002 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10003 identifier = cp_parser_identifier (parser);
10005 identifier = NULL_TREE;
10007 /* Look for the `{' to start the namespace. */
10008 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10009 /* Start the namespace. */
10010 push_namespace (identifier);
10011 /* Parse the body of the namespace. */
10012 cp_parser_namespace_body (parser);
10013 /* Finish the namespace. */
10015 /* Look for the final `}'. */
10016 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10019 /* Parse a namespace-body.
10022 declaration-seq [opt] */
10025 cp_parser_namespace_body (cp_parser* parser)
10027 cp_parser_declaration_seq_opt (parser);
10030 /* Parse a namespace-alias-definition.
10032 namespace-alias-definition:
10033 namespace identifier = qualified-namespace-specifier ; */
10036 cp_parser_namespace_alias_definition (cp_parser* parser)
10039 tree namespace_specifier;
10041 /* Look for the `namespace' keyword. */
10042 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10043 /* Look for the identifier. */
10044 identifier = cp_parser_identifier (parser);
10045 if (identifier == error_mark_node)
10047 /* Look for the `=' token. */
10048 cp_parser_require (parser, CPP_EQ, "`='");
10049 /* Look for the qualified-namespace-specifier. */
10050 namespace_specifier
10051 = cp_parser_qualified_namespace_specifier (parser);
10052 /* Look for the `;' token. */
10053 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10055 /* Register the alias in the symbol table. */
10056 do_namespace_alias (identifier, namespace_specifier);
10059 /* Parse a qualified-namespace-specifier.
10061 qualified-namespace-specifier:
10062 :: [opt] nested-name-specifier [opt] namespace-name
10064 Returns a NAMESPACE_DECL corresponding to the specified
10068 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10070 /* Look for the optional `::'. */
10071 cp_parser_global_scope_opt (parser,
10072 /*current_scope_valid_p=*/false);
10074 /* Look for the optional nested-name-specifier. */
10075 cp_parser_nested_name_specifier_opt (parser,
10076 /*typename_keyword_p=*/false,
10077 /*check_dependency_p=*/true,
10079 /*is_declaration=*/true);
10081 return cp_parser_namespace_name (parser);
10084 /* Parse a using-declaration.
10087 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10088 using :: unqualified-id ; */
10091 cp_parser_using_declaration (cp_parser* parser)
10094 bool typename_p = false;
10095 bool global_scope_p;
10101 /* Look for the `using' keyword. */
10102 cp_parser_require_keyword (parser, RID_USING, "`using'");
10104 /* Peek at the next token. */
10105 token = cp_lexer_peek_token (parser->lexer);
10106 /* See if it's `typename'. */
10107 if (token->keyword == RID_TYPENAME)
10109 /* Remember that we've seen it. */
10111 /* Consume the `typename' token. */
10112 cp_lexer_consume_token (parser->lexer);
10115 /* Look for the optional global scope qualification. */
10117 = (cp_parser_global_scope_opt (parser,
10118 /*current_scope_valid_p=*/false)
10121 /* If we saw `typename', or didn't see `::', then there must be a
10122 nested-name-specifier present. */
10123 if (typename_p || !global_scope_p)
10124 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10125 /*check_dependency_p=*/true,
10127 /*is_declaration=*/true);
10128 /* Otherwise, we could be in either of the two productions. In that
10129 case, treat the nested-name-specifier as optional. */
10131 qscope = cp_parser_nested_name_specifier_opt (parser,
10132 /*typename_keyword_p=*/false,
10133 /*check_dependency_p=*/true,
10135 /*is_declaration=*/true);
10137 qscope = global_namespace;
10139 /* Parse the unqualified-id. */
10140 identifier = cp_parser_unqualified_id (parser,
10141 /*template_keyword_p=*/false,
10142 /*check_dependency_p=*/true,
10143 /*declarator_p=*/true);
10145 /* The function we call to handle a using-declaration is different
10146 depending on what scope we are in. */
10147 if (identifier == error_mark_node)
10149 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10150 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10151 /* [namespace.udecl]
10153 A using declaration shall not name a template-id. */
10154 error ("a template-id may not appear in a using-declaration");
10157 scope = current_scope ();
10158 if (scope && TYPE_P (scope))
10160 /* Create the USING_DECL. */
10161 decl = do_class_using_decl (build_nt (SCOPE_REF,
10164 /* Add it to the list of members in this class. */
10165 finish_member_declaration (decl);
10169 decl = cp_parser_lookup_name_simple (parser, identifier);
10170 if (decl == error_mark_node)
10171 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10173 do_local_using_decl (decl, qscope, identifier);
10175 do_toplevel_using_decl (decl, qscope, identifier);
10179 /* Look for the final `;'. */
10180 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10183 /* Parse a using-directive.
10186 using namespace :: [opt] nested-name-specifier [opt]
10187 namespace-name ; */
10190 cp_parser_using_directive (cp_parser* parser)
10192 tree namespace_decl;
10195 /* Look for the `using' keyword. */
10196 cp_parser_require_keyword (parser, RID_USING, "`using'");
10197 /* And the `namespace' keyword. */
10198 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10199 /* Look for the optional `::' operator. */
10200 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10201 /* And the optional nested-name-specifier. */
10202 cp_parser_nested_name_specifier_opt (parser,
10203 /*typename_keyword_p=*/false,
10204 /*check_dependency_p=*/true,
10206 /*is_declaration=*/true);
10207 /* Get the namespace being used. */
10208 namespace_decl = cp_parser_namespace_name (parser);
10209 /* And any specified attributes. */
10210 attribs = cp_parser_attributes_opt (parser);
10211 /* Update the symbol table. */
10212 parse_using_directive (namespace_decl, attribs);
10213 /* Look for the final `;'. */
10214 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10217 /* Parse an asm-definition.
10220 asm ( string-literal ) ;
10225 asm volatile [opt] ( string-literal ) ;
10226 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10227 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10228 : asm-operand-list [opt] ) ;
10229 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10230 : asm-operand-list [opt]
10231 : asm-operand-list [opt] ) ; */
10234 cp_parser_asm_definition (cp_parser* parser)
10237 tree outputs = NULL_TREE;
10238 tree inputs = NULL_TREE;
10239 tree clobbers = NULL_TREE;
10241 bool volatile_p = false;
10242 bool extended_p = false;
10244 /* Look for the `asm' keyword. */
10245 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10246 /* See if the next token is `volatile'. */
10247 if (cp_parser_allow_gnu_extensions_p (parser)
10248 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10250 /* Remember that we saw the `volatile' keyword. */
10252 /* Consume the token. */
10253 cp_lexer_consume_token (parser->lexer);
10255 /* Look for the opening `('. */
10256 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10258 /* Look for the string. */
10259 string = cp_parser_string_literal (parser, false, false);
10260 if (string == error_mark_node)
10262 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10263 /*consume_paren=*/true);
10267 /* If we're allowing GNU extensions, check for the extended assembly
10268 syntax. Unfortunately, the `:' tokens need not be separated by
10269 a space in C, and so, for compatibility, we tolerate that here
10270 too. Doing that means that we have to treat the `::' operator as
10272 if (cp_parser_allow_gnu_extensions_p (parser)
10273 && at_function_scope_p ()
10274 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10275 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10277 bool inputs_p = false;
10278 bool clobbers_p = false;
10280 /* The extended syntax was used. */
10283 /* Look for outputs. */
10284 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10286 /* Consume the `:'. */
10287 cp_lexer_consume_token (parser->lexer);
10288 /* Parse the output-operands. */
10289 if (cp_lexer_next_token_is_not (parser->lexer,
10291 && cp_lexer_next_token_is_not (parser->lexer,
10293 && cp_lexer_next_token_is_not (parser->lexer,
10295 outputs = cp_parser_asm_operand_list (parser);
10297 /* If the next token is `::', there are no outputs, and the
10298 next token is the beginning of the inputs. */
10299 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10300 /* The inputs are coming next. */
10303 /* Look for inputs. */
10305 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10307 /* Consume the `:' or `::'. */
10308 cp_lexer_consume_token (parser->lexer);
10309 /* Parse the output-operands. */
10310 if (cp_lexer_next_token_is_not (parser->lexer,
10312 && cp_lexer_next_token_is_not (parser->lexer,
10314 inputs = cp_parser_asm_operand_list (parser);
10316 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10317 /* The clobbers are coming next. */
10320 /* Look for clobbers. */
10322 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10324 /* Consume the `:' or `::'. */
10325 cp_lexer_consume_token (parser->lexer);
10326 /* Parse the clobbers. */
10327 if (cp_lexer_next_token_is_not (parser->lexer,
10329 clobbers = cp_parser_asm_clobber_list (parser);
10332 /* Look for the closing `)'. */
10333 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10334 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10335 /*consume_paren=*/true);
10336 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10338 /* Create the ASM_EXPR. */
10339 if (at_function_scope_p ())
10341 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10343 /* If the extended syntax was not used, mark the ASM_EXPR. */
10345 ASM_INPUT_P (asm_stmt) = 1;
10348 assemble_asm (string);
10351 /* Declarators [gram.dcl.decl] */
10353 /* Parse an init-declarator.
10356 declarator initializer [opt]
10361 declarator asm-specification [opt] attributes [opt] initializer [opt]
10363 function-definition:
10364 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10366 decl-specifier-seq [opt] declarator function-try-block
10370 function-definition:
10371 __extension__ function-definition
10373 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10374 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10375 then this declarator appears in a class scope. The new DECL created
10376 by this declarator is returned.
10378 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10379 for a function-definition here as well. If the declarator is a
10380 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10381 be TRUE upon return. By that point, the function-definition will
10382 have been completely parsed.
10384 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10388 cp_parser_init_declarator (cp_parser* parser,
10389 cp_decl_specifier_seq *decl_specifiers,
10390 bool function_definition_allowed_p,
10392 int declares_class_or_enum,
10393 bool* function_definition_p)
10396 cp_declarator *declarator;
10397 tree prefix_attributes;
10399 tree asm_specification;
10401 tree decl = NULL_TREE;
10403 bool is_initialized;
10404 bool is_parenthesized_init;
10405 bool is_non_constant_init;
10406 int ctor_dtor_or_conv_p;
10408 bool pop_p = false;
10410 /* Gather the attributes that were provided with the
10411 decl-specifiers. */
10412 prefix_attributes = decl_specifiers->attributes;
10414 /* Assume that this is not the declarator for a function
10416 if (function_definition_p)
10417 *function_definition_p = false;
10419 /* Defer access checks while parsing the declarator; we cannot know
10420 what names are accessible until we know what is being
10422 resume_deferring_access_checks ();
10424 /* Parse the declarator. */
10426 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10427 &ctor_dtor_or_conv_p,
10428 /*parenthesized_p=*/NULL);
10429 /* Gather up the deferred checks. */
10430 stop_deferring_access_checks ();
10432 /* If the DECLARATOR was erroneous, there's no need to go
10434 if (declarator == cp_error_declarator)
10435 return error_mark_node;
10437 cp_parser_check_for_definition_in_return_type (declarator,
10438 declares_class_or_enum);
10440 /* Figure out what scope the entity declared by the DECLARATOR is
10441 located in. `grokdeclarator' sometimes changes the scope, so
10442 we compute it now. */
10443 scope = get_scope_of_declarator (declarator);
10445 /* If we're allowing GNU extensions, look for an asm-specification
10447 if (cp_parser_allow_gnu_extensions_p (parser))
10449 /* Look for an asm-specification. */
10450 asm_specification = cp_parser_asm_specification_opt (parser);
10451 /* And attributes. */
10452 attributes = cp_parser_attributes_opt (parser);
10456 asm_specification = NULL_TREE;
10457 attributes = NULL_TREE;
10460 /* Peek at the next token. */
10461 token = cp_lexer_peek_token (parser->lexer);
10462 /* Check to see if the token indicates the start of a
10463 function-definition. */
10464 if (cp_parser_token_starts_function_definition_p (token))
10466 if (!function_definition_allowed_p)
10468 /* If a function-definition should not appear here, issue an
10470 cp_parser_error (parser,
10471 "a function-definition is not allowed here");
10472 return error_mark_node;
10476 /* Neither attributes nor an asm-specification are allowed
10477 on a function-definition. */
10478 if (asm_specification)
10479 error ("an asm-specification is not allowed on a function-definition");
10481 error ("attributes are not allowed on a function-definition");
10482 /* This is a function-definition. */
10483 *function_definition_p = true;
10485 /* Parse the function definition. */
10487 decl = cp_parser_save_member_function_body (parser,
10490 prefix_attributes);
10493 = (cp_parser_function_definition_from_specifiers_and_declarator
10494 (parser, decl_specifiers, prefix_attributes, declarator));
10502 Only in function declarations for constructors, destructors, and
10503 type conversions can the decl-specifier-seq be omitted.
10505 We explicitly postpone this check past the point where we handle
10506 function-definitions because we tolerate function-definitions
10507 that are missing their return types in some modes. */
10508 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10510 cp_parser_error (parser,
10511 "expected constructor, destructor, or type conversion");
10512 return error_mark_node;
10515 /* An `=' or an `(' indicates an initializer. */
10516 is_initialized = (token->type == CPP_EQ
10517 || token->type == CPP_OPEN_PAREN);
10518 /* If the init-declarator isn't initialized and isn't followed by a
10519 `,' or `;', it's not a valid init-declarator. */
10520 if (!is_initialized
10521 && token->type != CPP_COMMA
10522 && token->type != CPP_SEMICOLON)
10524 cp_parser_error (parser, "expected initializer");
10525 return error_mark_node;
10528 /* Because start_decl has side-effects, we should only call it if we
10529 know we're going ahead. By this point, we know that we cannot
10530 possibly be looking at any other construct. */
10531 cp_parser_commit_to_tentative_parse (parser);
10533 /* If the decl specifiers were bad, issue an error now that we're
10534 sure this was intended to be a declarator. Then continue
10535 declaring the variable(s), as int, to try to cut down on further
10537 if (decl_specifiers->any_specifiers_p
10538 && decl_specifiers->type == error_mark_node)
10540 cp_parser_error (parser, "invalid type in declaration");
10541 decl_specifiers->type = integer_type_node;
10544 /* Check to see whether or not this declaration is a friend. */
10545 friend_p = cp_parser_friend_p (decl_specifiers);
10547 /* Check that the number of template-parameter-lists is OK. */
10548 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10549 return error_mark_node;
10551 /* Enter the newly declared entry in the symbol table. If we're
10552 processing a declaration in a class-specifier, we wait until
10553 after processing the initializer. */
10556 if (parser->in_unbraced_linkage_specification_p)
10558 decl_specifiers->storage_class = sc_extern;
10559 have_extern_spec = false;
10561 decl = start_decl (declarator, decl_specifiers,
10562 is_initialized, attributes, prefix_attributes,
10566 /* Enter the SCOPE. That way unqualified names appearing in the
10567 initializer will be looked up in SCOPE. */
10568 pop_p = push_scope (scope);
10570 /* Perform deferred access control checks, now that we know in which
10571 SCOPE the declared entity resides. */
10572 if (!member_p && decl)
10574 tree saved_current_function_decl = NULL_TREE;
10576 /* If the entity being declared is a function, pretend that we
10577 are in its scope. If it is a `friend', it may have access to
10578 things that would not otherwise be accessible. */
10579 if (TREE_CODE (decl) == FUNCTION_DECL)
10581 saved_current_function_decl = current_function_decl;
10582 current_function_decl = decl;
10585 /* Perform the access control checks for the declarator and the
10586 the decl-specifiers. */
10587 perform_deferred_access_checks ();
10589 /* Restore the saved value. */
10590 if (TREE_CODE (decl) == FUNCTION_DECL)
10591 current_function_decl = saved_current_function_decl;
10594 /* Parse the initializer. */
10595 if (is_initialized)
10596 initializer = cp_parser_initializer (parser,
10597 &is_parenthesized_init,
10598 &is_non_constant_init);
10601 initializer = NULL_TREE;
10602 is_parenthesized_init = false;
10603 is_non_constant_init = true;
10606 /* The old parser allows attributes to appear after a parenthesized
10607 initializer. Mark Mitchell proposed removing this functionality
10608 on the GCC mailing lists on 2002-08-13. This parser accepts the
10609 attributes -- but ignores them. */
10610 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10611 if (cp_parser_attributes_opt (parser))
10612 warning ("attributes after parenthesized initializer ignored");
10614 /* For an in-class declaration, use `grokfield' to create the
10620 decl = grokfield (declarator, decl_specifiers,
10621 initializer, /*asmspec=*/NULL_TREE,
10622 /*attributes=*/NULL_TREE);
10623 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10624 cp_parser_save_default_args (parser, decl);
10627 /* Finish processing the declaration. But, skip friend
10629 if (!friend_p && decl && decl != error_mark_node)
10631 cp_finish_decl (decl,
10634 /* If the initializer is in parentheses, then this is
10635 a direct-initialization, which means that an
10636 `explicit' constructor is OK. Otherwise, an
10637 `explicit' constructor cannot be used. */
10638 ((is_parenthesized_init || !is_initialized)
10639 ? 0 : LOOKUP_ONLYCONVERTING));
10641 pop_scope (DECL_CONTEXT (decl));
10644 /* Remember whether or not variables were initialized by
10645 constant-expressions. */
10646 if (decl && TREE_CODE (decl) == VAR_DECL
10647 && is_initialized && !is_non_constant_init)
10648 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10653 /* Parse a declarator.
10657 ptr-operator declarator
10659 abstract-declarator:
10660 ptr-operator abstract-declarator [opt]
10661 direct-abstract-declarator
10666 attributes [opt] direct-declarator
10667 attributes [opt] ptr-operator declarator
10669 abstract-declarator:
10670 attributes [opt] ptr-operator abstract-declarator [opt]
10671 attributes [opt] direct-abstract-declarator
10673 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10674 detect constructor, destructor or conversion operators. It is set
10675 to -1 if the declarator is a name, and +1 if it is a
10676 function. Otherwise it is set to zero. Usually you just want to
10677 test for >0, but internally the negative value is used.
10679 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10680 a decl-specifier-seq unless it declares a constructor, destructor,
10681 or conversion. It might seem that we could check this condition in
10682 semantic analysis, rather than parsing, but that makes it difficult
10683 to handle something like `f()'. We want to notice that there are
10684 no decl-specifiers, and therefore realize that this is an
10685 expression, not a declaration.)
10687 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10688 the declarator is a direct-declarator of the form "(...)". */
10690 static cp_declarator *
10691 cp_parser_declarator (cp_parser* parser,
10692 cp_parser_declarator_kind dcl_kind,
10693 int* ctor_dtor_or_conv_p,
10694 bool* parenthesized_p)
10697 cp_declarator *declarator;
10698 enum tree_code code;
10699 cp_cv_quals cv_quals;
10701 tree attributes = NULL_TREE;
10703 /* Assume this is not a constructor, destructor, or type-conversion
10705 if (ctor_dtor_or_conv_p)
10706 *ctor_dtor_or_conv_p = 0;
10708 if (cp_parser_allow_gnu_extensions_p (parser))
10709 attributes = cp_parser_attributes_opt (parser);
10711 /* Peek at the next token. */
10712 token = cp_lexer_peek_token (parser->lexer);
10714 /* Check for the ptr-operator production. */
10715 cp_parser_parse_tentatively (parser);
10716 /* Parse the ptr-operator. */
10717 code = cp_parser_ptr_operator (parser,
10720 /* If that worked, then we have a ptr-operator. */
10721 if (cp_parser_parse_definitely (parser))
10723 /* If a ptr-operator was found, then this declarator was not
10725 if (parenthesized_p)
10726 *parenthesized_p = true;
10727 /* The dependent declarator is optional if we are parsing an
10728 abstract-declarator. */
10729 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10730 cp_parser_parse_tentatively (parser);
10732 /* Parse the dependent declarator. */
10733 declarator = cp_parser_declarator (parser, dcl_kind,
10734 /*ctor_dtor_or_conv_p=*/NULL,
10735 /*parenthesized_p=*/NULL);
10737 /* If we are parsing an abstract-declarator, we must handle the
10738 case where the dependent declarator is absent. */
10739 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10740 && !cp_parser_parse_definitely (parser))
10743 /* Build the representation of the ptr-operator. */
10745 declarator = make_ptrmem_declarator (cv_quals,
10748 else if (code == INDIRECT_REF)
10749 declarator = make_pointer_declarator (cv_quals, declarator);
10751 declarator = make_reference_declarator (cv_quals, declarator);
10753 /* Everything else is a direct-declarator. */
10756 if (parenthesized_p)
10757 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10759 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10760 ctor_dtor_or_conv_p);
10763 if (attributes && declarator != cp_error_declarator)
10764 declarator->attributes = attributes;
10769 /* Parse a direct-declarator or direct-abstract-declarator.
10773 direct-declarator ( parameter-declaration-clause )
10774 cv-qualifier-seq [opt]
10775 exception-specification [opt]
10776 direct-declarator [ constant-expression [opt] ]
10779 direct-abstract-declarator:
10780 direct-abstract-declarator [opt]
10781 ( parameter-declaration-clause )
10782 cv-qualifier-seq [opt]
10783 exception-specification [opt]
10784 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10785 ( abstract-declarator )
10787 Returns a representation of the declarator. DCL_KIND is
10788 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10789 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10790 we are parsing a direct-declarator. It is
10791 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10792 of ambiguity we prefer an abstract declarator, as per
10793 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10794 cp_parser_declarator. */
10796 static cp_declarator *
10797 cp_parser_direct_declarator (cp_parser* parser,
10798 cp_parser_declarator_kind dcl_kind,
10799 int* ctor_dtor_or_conv_p)
10802 cp_declarator *declarator = NULL;
10803 tree scope = NULL_TREE;
10804 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10805 bool saved_in_declarator_p = parser->in_declarator_p;
10807 bool pop_p = false;
10811 /* Peek at the next token. */
10812 token = cp_lexer_peek_token (parser->lexer);
10813 if (token->type == CPP_OPEN_PAREN)
10815 /* This is either a parameter-declaration-clause, or a
10816 parenthesized declarator. When we know we are parsing a
10817 named declarator, it must be a parenthesized declarator
10818 if FIRST is true. For instance, `(int)' is a
10819 parameter-declaration-clause, with an omitted
10820 direct-abstract-declarator. But `((*))', is a
10821 parenthesized abstract declarator. Finally, when T is a
10822 template parameter `(T)' is a
10823 parameter-declaration-clause, and not a parenthesized
10826 We first try and parse a parameter-declaration-clause,
10827 and then try a nested declarator (if FIRST is true).
10829 It is not an error for it not to be a
10830 parameter-declaration-clause, even when FIRST is
10836 The first is the declaration of a function while the
10837 second is a the definition of a variable, including its
10840 Having seen only the parenthesis, we cannot know which of
10841 these two alternatives should be selected. Even more
10842 complex are examples like:
10847 The former is a function-declaration; the latter is a
10848 variable initialization.
10850 Thus again, we try a parameter-declaration-clause, and if
10851 that fails, we back out and return. */
10853 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10855 cp_parameter_declarator *params;
10856 unsigned saved_num_template_parameter_lists;
10858 cp_parser_parse_tentatively (parser);
10860 /* Consume the `('. */
10861 cp_lexer_consume_token (parser->lexer);
10864 /* If this is going to be an abstract declarator, we're
10865 in a declarator and we can't have default args. */
10866 parser->default_arg_ok_p = false;
10867 parser->in_declarator_p = true;
10870 /* Inside the function parameter list, surrounding
10871 template-parameter-lists do not apply. */
10872 saved_num_template_parameter_lists
10873 = parser->num_template_parameter_lists;
10874 parser->num_template_parameter_lists = 0;
10876 /* Parse the parameter-declaration-clause. */
10877 params = cp_parser_parameter_declaration_clause (parser);
10879 parser->num_template_parameter_lists
10880 = saved_num_template_parameter_lists;
10882 /* If all went well, parse the cv-qualifier-seq and the
10883 exception-specification. */
10884 if (cp_parser_parse_definitely (parser))
10886 cp_cv_quals cv_quals;
10887 tree exception_specification;
10889 if (ctor_dtor_or_conv_p)
10890 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10892 /* Consume the `)'. */
10893 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10895 /* Parse the cv-qualifier-seq. */
10896 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10897 /* And the exception-specification. */
10898 exception_specification
10899 = cp_parser_exception_specification_opt (parser);
10901 /* Create the function-declarator. */
10902 declarator = make_call_declarator (declarator,
10905 exception_specification);
10906 /* Any subsequent parameter lists are to do with
10907 return type, so are not those of the declared
10909 parser->default_arg_ok_p = false;
10911 /* Repeat the main loop. */
10916 /* If this is the first, we can try a parenthesized
10920 bool saved_in_type_id_in_expr_p;
10922 parser->default_arg_ok_p = saved_default_arg_ok_p;
10923 parser->in_declarator_p = saved_in_declarator_p;
10925 /* Consume the `('. */
10926 cp_lexer_consume_token (parser->lexer);
10927 /* Parse the nested declarator. */
10928 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10929 parser->in_type_id_in_expr_p = true;
10931 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10932 /*parenthesized_p=*/NULL);
10933 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10935 /* Expect a `)'. */
10936 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10937 declarator = cp_error_declarator;
10938 if (declarator == cp_error_declarator)
10941 goto handle_declarator;
10943 /* Otherwise, we must be done. */
10947 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10948 && token->type == CPP_OPEN_SQUARE)
10950 /* Parse an array-declarator. */
10953 if (ctor_dtor_or_conv_p)
10954 *ctor_dtor_or_conv_p = 0;
10957 parser->default_arg_ok_p = false;
10958 parser->in_declarator_p = true;
10959 /* Consume the `['. */
10960 cp_lexer_consume_token (parser->lexer);
10961 /* Peek at the next token. */
10962 token = cp_lexer_peek_token (parser->lexer);
10963 /* If the next token is `]', then there is no
10964 constant-expression. */
10965 if (token->type != CPP_CLOSE_SQUARE)
10967 bool non_constant_p;
10970 = cp_parser_constant_expression (parser,
10971 /*allow_non_constant=*/true,
10973 if (!non_constant_p)
10974 bounds = fold_non_dependent_expr (bounds);
10977 bounds = NULL_TREE;
10978 /* Look for the closing `]'. */
10979 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10981 declarator = cp_error_declarator;
10985 declarator = make_array_declarator (declarator, bounds);
10987 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10991 /* Parse a declarator-id */
10992 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10993 cp_parser_parse_tentatively (parser);
10994 id = cp_parser_declarator_id (parser);
10995 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10997 if (!cp_parser_parse_definitely (parser))
10998 id = error_mark_node;
10999 else if (TREE_CODE (id) != IDENTIFIER_NODE)
11001 cp_parser_error (parser, "expected unqualified-id");
11002 id = error_mark_node;
11006 if (id == error_mark_node)
11008 declarator = cp_error_declarator;
11012 if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11014 tree scope = TREE_OPERAND (id, 0);
11016 /* In the declaration of a member of a template class
11017 outside of the class itself, the SCOPE will sometimes
11018 be a TYPENAME_TYPE. For example, given:
11020 template <typename T>
11021 int S<T>::R::i = 3;
11023 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11024 this context, we must resolve S<T>::R to an ordinary
11025 type, rather than a typename type.
11027 The reason we normally avoid resolving TYPENAME_TYPEs
11028 is that a specialization of `S' might render
11029 `S<T>::R' not a type. However, if `S' is
11030 specialized, then this `i' will not be used, so there
11031 is no harm in resolving the types here. */
11032 if (TREE_CODE (scope) == TYPENAME_TYPE)
11036 /* Resolve the TYPENAME_TYPE. */
11037 type = resolve_typename_type (scope,
11038 /*only_current_p=*/false);
11039 /* If that failed, the declarator is invalid. */
11040 if (type == error_mark_node)
11041 error ("`%T::%D' is not a type",
11042 TYPE_CONTEXT (scope),
11043 TYPE_IDENTIFIER (scope));
11044 /* Build a new DECLARATOR. */
11045 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11049 declarator = make_id_declarator (id);
11053 tree unqualified_name;
11055 if (TREE_CODE (id) == SCOPE_REF
11056 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11058 class_type = TREE_OPERAND (id, 0);
11059 unqualified_name = TREE_OPERAND (id, 1);
11063 class_type = current_class_type;
11064 unqualified_name = id;
11069 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11070 declarator->u.id.sfk = sfk_destructor;
11071 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11072 declarator->u.id.sfk = sfk_conversion;
11073 else if (constructor_name_p (unqualified_name,
11075 || (TREE_CODE (unqualified_name) == TYPE_DECL
11076 && same_type_p (TREE_TYPE (unqualified_name),
11078 declarator->u.id.sfk = sfk_constructor;
11080 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11081 *ctor_dtor_or_conv_p = -1;
11082 if (TREE_CODE (id) == SCOPE_REF
11083 && TREE_CODE (unqualified_name) == TYPE_DECL
11084 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11086 error ("invalid use of constructor as a template");
11087 inform ("use `%T::%D' instead of `%T::%T' to name the "
11088 "constructor in a qualified name", class_type,
11089 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11090 class_type, class_type);
11095 handle_declarator:;
11096 scope = get_scope_of_declarator (declarator);
11098 /* Any names that appear after the declarator-id for a
11099 member are looked up in the containing scope. */
11100 pop_p = push_scope (scope);
11101 parser->in_declarator_p = true;
11102 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11103 || (declarator && declarator->kind == cdk_id))
11104 /* Default args are only allowed on function
11106 parser->default_arg_ok_p = saved_default_arg_ok_p;
11108 parser->default_arg_ok_p = false;
11117 /* For an abstract declarator, we might wind up with nothing at this
11118 point. That's an error; the declarator is not optional. */
11120 cp_parser_error (parser, "expected declarator");
11122 /* If we entered a scope, we must exit it now. */
11126 parser->default_arg_ok_p = saved_default_arg_ok_p;
11127 parser->in_declarator_p = saved_in_declarator_p;
11132 /* Parse a ptr-operator.
11135 * cv-qualifier-seq [opt]
11137 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11142 & cv-qualifier-seq [opt]
11144 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11145 Returns ADDR_EXPR if a reference was used. In the case of a
11146 pointer-to-member, *TYPE is filled in with the TYPE containing the
11147 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11148 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11149 ERROR_MARK if an error occurred. */
11151 static enum tree_code
11152 cp_parser_ptr_operator (cp_parser* parser,
11154 cp_cv_quals *cv_quals)
11156 enum tree_code code = ERROR_MARK;
11159 /* Assume that it's not a pointer-to-member. */
11161 /* And that there are no cv-qualifiers. */
11162 *cv_quals = TYPE_UNQUALIFIED;
11164 /* Peek at the next token. */
11165 token = cp_lexer_peek_token (parser->lexer);
11166 /* If it's a `*' or `&' we have a pointer or reference. */
11167 if (token->type == CPP_MULT || token->type == CPP_AND)
11169 /* Remember which ptr-operator we were processing. */
11170 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11172 /* Consume the `*' or `&'. */
11173 cp_lexer_consume_token (parser->lexer);
11175 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11176 `&', if we are allowing GNU extensions. (The only qualifier
11177 that can legally appear after `&' is `restrict', but that is
11178 enforced during semantic analysis. */
11179 if (code == INDIRECT_REF
11180 || cp_parser_allow_gnu_extensions_p (parser))
11181 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11185 /* Try the pointer-to-member case. */
11186 cp_parser_parse_tentatively (parser);
11187 /* Look for the optional `::' operator. */
11188 cp_parser_global_scope_opt (parser,
11189 /*current_scope_valid_p=*/false);
11190 /* Look for the nested-name specifier. */
11191 cp_parser_nested_name_specifier (parser,
11192 /*typename_keyword_p=*/false,
11193 /*check_dependency_p=*/true,
11195 /*is_declaration=*/false);
11196 /* If we found it, and the next token is a `*', then we are
11197 indeed looking at a pointer-to-member operator. */
11198 if (!cp_parser_error_occurred (parser)
11199 && cp_parser_require (parser, CPP_MULT, "`*'"))
11201 /* The type of which the member is a member is given by the
11203 *type = parser->scope;
11204 /* The next name will not be qualified. */
11205 parser->scope = NULL_TREE;
11206 parser->qualifying_scope = NULL_TREE;
11207 parser->object_scope = NULL_TREE;
11208 /* Indicate that the `*' operator was used. */
11209 code = INDIRECT_REF;
11210 /* Look for the optional cv-qualifier-seq. */
11211 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11213 /* If that didn't work we don't have a ptr-operator. */
11214 if (!cp_parser_parse_definitely (parser))
11215 cp_parser_error (parser, "expected ptr-operator");
11221 /* Parse an (optional) cv-qualifier-seq.
11224 cv-qualifier cv-qualifier-seq [opt]
11235 Returns a bitmask representing the cv-qualifiers. */
11238 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11240 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11245 cp_cv_quals cv_qualifier;
11247 /* Peek at the next token. */
11248 token = cp_lexer_peek_token (parser->lexer);
11249 /* See if it's a cv-qualifier. */
11250 switch (token->keyword)
11253 cv_qualifier = TYPE_QUAL_CONST;
11257 cv_qualifier = TYPE_QUAL_VOLATILE;
11261 cv_qualifier = TYPE_QUAL_RESTRICT;
11265 cv_qualifier = TYPE_UNQUALIFIED;
11272 if (cv_quals & cv_qualifier)
11274 error ("duplicate cv-qualifier");
11275 cp_lexer_purge_token (parser->lexer);
11279 cp_lexer_consume_token (parser->lexer);
11280 cv_quals |= cv_qualifier;
11287 /* Parse a declarator-id.
11291 :: [opt] nested-name-specifier [opt] type-name
11293 In the `id-expression' case, the value returned is as for
11294 cp_parser_id_expression if the id-expression was an unqualified-id.
11295 If the id-expression was a qualified-id, then a SCOPE_REF is
11296 returned. The first operand is the scope (either a NAMESPACE_DECL
11297 or TREE_TYPE), but the second is still just a representation of an
11301 cp_parser_declarator_id (cp_parser* parser)
11303 tree id_expression;
11305 /* The expression must be an id-expression. Assume that qualified
11306 names are the names of types so that:
11309 int S<T>::R::i = 3;
11311 will work; we must treat `S<T>::R' as the name of a type.
11312 Similarly, assume that qualified names are templates, where
11316 int S<T>::R<T>::i = 3;
11319 id_expression = cp_parser_id_expression (parser,
11320 /*template_keyword_p=*/false,
11321 /*check_dependency_p=*/false,
11322 /*template_p=*/NULL,
11323 /*declarator_p=*/true);
11324 /* If the name was qualified, create a SCOPE_REF to represent
11328 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11329 parser->scope = NULL_TREE;
11332 return id_expression;
11335 /* Parse a type-id.
11338 type-specifier-seq abstract-declarator [opt]
11340 Returns the TYPE specified. */
11343 cp_parser_type_id (cp_parser* parser)
11345 cp_decl_specifier_seq type_specifier_seq;
11346 cp_declarator *abstract_declarator;
11348 /* Parse the type-specifier-seq. */
11349 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11350 if (type_specifier_seq.type == error_mark_node)
11351 return error_mark_node;
11353 /* There might or might not be an abstract declarator. */
11354 cp_parser_parse_tentatively (parser);
11355 /* Look for the declarator. */
11356 abstract_declarator
11357 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11358 /*parenthesized_p=*/NULL);
11359 /* Check to see if there really was a declarator. */
11360 if (!cp_parser_parse_definitely (parser))
11361 abstract_declarator = NULL;
11363 return groktypename (&type_specifier_seq, abstract_declarator);
11366 /* Parse a type-specifier-seq.
11368 type-specifier-seq:
11369 type-specifier type-specifier-seq [opt]
11373 type-specifier-seq:
11374 attributes type-specifier-seq [opt]
11376 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11379 cp_parser_type_specifier_seq (cp_parser* parser,
11380 cp_decl_specifier_seq *type_specifier_seq)
11382 bool seen_type_specifier = false;
11384 /* Clear the TYPE_SPECIFIER_SEQ. */
11385 clear_decl_specs (type_specifier_seq);
11387 /* Parse the type-specifiers and attributes. */
11390 tree type_specifier;
11392 /* Check for attributes first. */
11393 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11395 type_specifier_seq->attributes =
11396 chainon (type_specifier_seq->attributes,
11397 cp_parser_attributes_opt (parser));
11401 /* Look for the type-specifier. */
11402 type_specifier = cp_parser_type_specifier (parser,
11403 CP_PARSER_FLAGS_OPTIONAL,
11404 type_specifier_seq,
11405 /*is_declaration=*/false,
11408 /* If the first type-specifier could not be found, this is not a
11409 type-specifier-seq at all. */
11410 if (!seen_type_specifier && !type_specifier)
11412 cp_parser_error (parser, "expected type-specifier");
11413 type_specifier_seq->type = error_mark_node;
11416 /* If subsequent type-specifiers could not be found, the
11417 type-specifier-seq is complete. */
11418 else if (seen_type_specifier && !type_specifier)
11421 seen_type_specifier = true;
11427 /* Parse a parameter-declaration-clause.
11429 parameter-declaration-clause:
11430 parameter-declaration-list [opt] ... [opt]
11431 parameter-declaration-list , ...
11433 Returns a representation for the parameter declarations. A return
11434 value of NULL indicates a parameter-declaration-clause consisting
11435 only of an ellipsis. */
11437 static cp_parameter_declarator *
11438 cp_parser_parameter_declaration_clause (cp_parser* parser)
11440 cp_parameter_declarator *parameters;
11445 /* Peek at the next token. */
11446 token = cp_lexer_peek_token (parser->lexer);
11447 /* Check for trivial parameter-declaration-clauses. */
11448 if (token->type == CPP_ELLIPSIS)
11450 /* Consume the `...' token. */
11451 cp_lexer_consume_token (parser->lexer);
11454 else if (token->type == CPP_CLOSE_PAREN)
11455 /* There are no parameters. */
11457 #ifndef NO_IMPLICIT_EXTERN_C
11458 if (in_system_header && current_class_type == NULL
11459 && current_lang_name == lang_name_c)
11463 return no_parameters;
11465 /* Check for `(void)', too, which is a special case. */
11466 else if (token->keyword == RID_VOID
11467 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11468 == CPP_CLOSE_PAREN))
11470 /* Consume the `void' token. */
11471 cp_lexer_consume_token (parser->lexer);
11472 /* There are no parameters. */
11473 return no_parameters;
11476 /* Parse the parameter-declaration-list. */
11477 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11478 /* If a parse error occurred while parsing the
11479 parameter-declaration-list, then the entire
11480 parameter-declaration-clause is erroneous. */
11484 /* Peek at the next token. */
11485 token = cp_lexer_peek_token (parser->lexer);
11486 /* If it's a `,', the clause should terminate with an ellipsis. */
11487 if (token->type == CPP_COMMA)
11489 /* Consume the `,'. */
11490 cp_lexer_consume_token (parser->lexer);
11491 /* Expect an ellipsis. */
11493 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11495 /* It might also be `...' if the optional trailing `,' was
11497 else if (token->type == CPP_ELLIPSIS)
11499 /* Consume the `...' token. */
11500 cp_lexer_consume_token (parser->lexer);
11501 /* And remember that we saw it. */
11505 ellipsis_p = false;
11507 /* Finish the parameter list. */
11508 if (parameters && ellipsis_p)
11509 parameters->ellipsis_p = true;
11514 /* Parse a parameter-declaration-list.
11516 parameter-declaration-list:
11517 parameter-declaration
11518 parameter-declaration-list , parameter-declaration
11520 Returns a representation of the parameter-declaration-list, as for
11521 cp_parser_parameter_declaration_clause. However, the
11522 `void_list_node' is never appended to the list. Upon return,
11523 *IS_ERROR will be true iff an error occurred. */
11525 static cp_parameter_declarator *
11526 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11528 cp_parameter_declarator *parameters = NULL;
11529 cp_parameter_declarator **tail = ¶meters;
11531 /* Assume all will go well. */
11534 /* Look for more parameters. */
11537 cp_parameter_declarator *parameter;
11538 bool parenthesized_p;
11539 /* Parse the parameter. */
11541 = cp_parser_parameter_declaration (parser,
11542 /*template_parm_p=*/false,
11545 /* If a parse error occurred parsing the parameter declaration,
11546 then the entire parameter-declaration-list is erroneous. */
11553 /* Add the new parameter to the list. */
11555 tail = ¶meter->next;
11557 /* Peek at the next token. */
11558 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11559 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11560 /* The parameter-declaration-list is complete. */
11562 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11566 /* Peek at the next token. */
11567 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11568 /* If it's an ellipsis, then the list is complete. */
11569 if (token->type == CPP_ELLIPSIS)
11571 /* Otherwise, there must be more parameters. Consume the
11573 cp_lexer_consume_token (parser->lexer);
11574 /* When parsing something like:
11576 int i(float f, double d)
11578 we can tell after seeing the declaration for "f" that we
11579 are not looking at an initialization of a variable "i",
11580 but rather at the declaration of a function "i".
11582 Due to the fact that the parsing of template arguments
11583 (as specified to a template-id) requires backtracking we
11584 cannot use this technique when inside a template argument
11586 if (!parser->in_template_argument_list_p
11587 && !parser->in_type_id_in_expr_p
11588 && cp_parser_parsing_tentatively (parser)
11589 && !cp_parser_committed_to_tentative_parse (parser)
11590 /* However, a parameter-declaration of the form
11591 "foat(f)" (which is a valid declaration of a
11592 parameter "f") can also be interpreted as an
11593 expression (the conversion of "f" to "float"). */
11594 && !parenthesized_p)
11595 cp_parser_commit_to_tentative_parse (parser);
11599 cp_parser_error (parser, "expected `,' or `...'");
11600 if (!cp_parser_parsing_tentatively (parser)
11601 || cp_parser_committed_to_tentative_parse (parser))
11602 cp_parser_skip_to_closing_parenthesis (parser,
11603 /*recovering=*/true,
11604 /*or_comma=*/false,
11605 /*consume_paren=*/false);
11613 /* Parse a parameter declaration.
11615 parameter-declaration:
11616 decl-specifier-seq declarator
11617 decl-specifier-seq declarator = assignment-expression
11618 decl-specifier-seq abstract-declarator [opt]
11619 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11621 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11622 declares a template parameter. (In that case, a non-nested `>'
11623 token encountered during the parsing of the assignment-expression
11624 is not interpreted as a greater-than operator.)
11626 Returns a representation of the parameter, or NULL if an error
11627 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11628 true iff the declarator is of the form "(p)". */
11630 static cp_parameter_declarator *
11631 cp_parser_parameter_declaration (cp_parser *parser,
11632 bool template_parm_p,
11633 bool *parenthesized_p)
11635 int declares_class_or_enum;
11636 bool greater_than_is_operator_p;
11637 cp_decl_specifier_seq decl_specifiers;
11638 cp_declarator *declarator;
11639 tree default_argument;
11641 const char *saved_message;
11643 /* In a template parameter, `>' is not an operator.
11647 When parsing a default template-argument for a non-type
11648 template-parameter, the first non-nested `>' is taken as the end
11649 of the template parameter-list rather than a greater-than
11651 greater_than_is_operator_p = !template_parm_p;
11653 /* Type definitions may not appear in parameter types. */
11654 saved_message = parser->type_definition_forbidden_message;
11655 parser->type_definition_forbidden_message
11656 = "types may not be defined in parameter types";
11658 /* Parse the declaration-specifiers. */
11659 cp_parser_decl_specifier_seq (parser,
11660 CP_PARSER_FLAGS_NONE,
11662 &declares_class_or_enum);
11663 /* If an error occurred, there's no reason to attempt to parse the
11664 rest of the declaration. */
11665 if (cp_parser_error_occurred (parser))
11667 parser->type_definition_forbidden_message = saved_message;
11671 /* Peek at the next token. */
11672 token = cp_lexer_peek_token (parser->lexer);
11673 /* If the next token is a `)', `,', `=', `>', or `...', then there
11674 is no declarator. */
11675 if (token->type == CPP_CLOSE_PAREN
11676 || token->type == CPP_COMMA
11677 || token->type == CPP_EQ
11678 || token->type == CPP_ELLIPSIS
11679 || token->type == CPP_GREATER)
11682 if (parenthesized_p)
11683 *parenthesized_p = false;
11685 /* Otherwise, there should be a declarator. */
11688 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11689 parser->default_arg_ok_p = false;
11691 /* After seeing a decl-specifier-seq, if the next token is not a
11692 "(", there is no possibility that the code is a valid
11693 expression. Therefore, if parsing tentatively, we commit at
11695 if (!parser->in_template_argument_list_p
11696 /* In an expression context, having seen:
11700 we cannot be sure whether we are looking at a
11701 function-type (taking a "char" as a parameter) or a cast
11702 of some object of type "char" to "int". */
11703 && !parser->in_type_id_in_expr_p
11704 && cp_parser_parsing_tentatively (parser)
11705 && !cp_parser_committed_to_tentative_parse (parser)
11706 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11707 cp_parser_commit_to_tentative_parse (parser);
11708 /* Parse the declarator. */
11709 declarator = cp_parser_declarator (parser,
11710 CP_PARSER_DECLARATOR_EITHER,
11711 /*ctor_dtor_or_conv_p=*/NULL,
11713 parser->default_arg_ok_p = saved_default_arg_ok_p;
11714 /* After the declarator, allow more attributes. */
11715 decl_specifiers.attributes
11716 = chainon (decl_specifiers.attributes,
11717 cp_parser_attributes_opt (parser));
11720 /* The restriction on defining new types applies only to the type
11721 of the parameter, not to the default argument. */
11722 parser->type_definition_forbidden_message = saved_message;
11724 /* If the next token is `=', then process a default argument. */
11725 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11727 bool saved_greater_than_is_operator_p;
11728 /* Consume the `='. */
11729 cp_lexer_consume_token (parser->lexer);
11731 /* If we are defining a class, then the tokens that make up the
11732 default argument must be saved and processed later. */
11733 if (!template_parm_p && at_class_scope_p ()
11734 && TYPE_BEING_DEFINED (current_class_type))
11736 unsigned depth = 0;
11737 cp_token *first_token;
11740 /* Add tokens until we have processed the entire default
11741 argument. We add the range [first_token, token). */
11742 first_token = cp_lexer_peek_token (parser->lexer);
11747 /* Peek at the next token. */
11748 token = cp_lexer_peek_token (parser->lexer);
11749 /* What we do depends on what token we have. */
11750 switch (token->type)
11752 /* In valid code, a default argument must be
11753 immediately followed by a `,' `)', or `...'. */
11755 case CPP_CLOSE_PAREN:
11757 /* If we run into a non-nested `;', `}', or `]',
11758 then the code is invalid -- but the default
11759 argument is certainly over. */
11760 case CPP_SEMICOLON:
11761 case CPP_CLOSE_BRACE:
11762 case CPP_CLOSE_SQUARE:
11765 /* Update DEPTH, if necessary. */
11766 else if (token->type == CPP_CLOSE_PAREN
11767 || token->type == CPP_CLOSE_BRACE
11768 || token->type == CPP_CLOSE_SQUARE)
11772 case CPP_OPEN_PAREN:
11773 case CPP_OPEN_SQUARE:
11774 case CPP_OPEN_BRACE:
11779 /* If we see a non-nested `>', and `>' is not an
11780 operator, then it marks the end of the default
11782 if (!depth && !greater_than_is_operator_p)
11786 /* If we run out of tokens, issue an error message. */
11788 error ("file ends in default argument");
11794 /* In these cases, we should look for template-ids.
11795 For example, if the default argument is
11796 `X<int, double>()', we need to do name lookup to
11797 figure out whether or not `X' is a template; if
11798 so, the `,' does not end the default argument.
11800 That is not yet done. */
11807 /* If we've reached the end, stop. */
11811 /* Add the token to the token block. */
11812 token = cp_lexer_consume_token (parser->lexer);
11815 /* Create a DEFAULT_ARG to represented the unparsed default
11817 default_argument = make_node (DEFAULT_ARG);
11818 DEFARG_TOKENS (default_argument)
11819 = cp_token_cache_new (first_token, token);
11821 /* Outside of a class definition, we can just parse the
11822 assignment-expression. */
11825 bool saved_local_variables_forbidden_p;
11827 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11829 saved_greater_than_is_operator_p
11830 = parser->greater_than_is_operator_p;
11831 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11832 /* Local variable names (and the `this' keyword) may not
11833 appear in a default argument. */
11834 saved_local_variables_forbidden_p
11835 = parser->local_variables_forbidden_p;
11836 parser->local_variables_forbidden_p = true;
11837 /* Parse the assignment-expression. */
11838 default_argument = cp_parser_assignment_expression (parser);
11839 /* Restore saved state. */
11840 parser->greater_than_is_operator_p
11841 = saved_greater_than_is_operator_p;
11842 parser->local_variables_forbidden_p
11843 = saved_local_variables_forbidden_p;
11845 if (!parser->default_arg_ok_p)
11847 if (!flag_pedantic_errors)
11848 warning ("deprecated use of default argument for parameter of non-function");
11851 error ("default arguments are only permitted for function parameters");
11852 default_argument = NULL_TREE;
11857 default_argument = NULL_TREE;
11859 return make_parameter_declarator (&decl_specifiers,
11864 /* Parse a function-body.
11867 compound_statement */
11870 cp_parser_function_body (cp_parser *parser)
11872 cp_parser_compound_statement (parser, NULL, false);
11875 /* Parse a ctor-initializer-opt followed by a function-body. Return
11876 true if a ctor-initializer was present. */
11879 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11882 bool ctor_initializer_p;
11884 /* Begin the function body. */
11885 body = begin_function_body ();
11886 /* Parse the optional ctor-initializer. */
11887 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11888 /* Parse the function-body. */
11889 cp_parser_function_body (parser);
11890 /* Finish the function body. */
11891 finish_function_body (body);
11893 return ctor_initializer_p;
11896 /* Parse an initializer.
11899 = initializer-clause
11900 ( expression-list )
11902 Returns a expression representing the initializer. If no
11903 initializer is present, NULL_TREE is returned.
11905 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11906 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11907 set to FALSE if there is no initializer present. If there is an
11908 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11909 is set to true; otherwise it is set to false. */
11912 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11913 bool* non_constant_p)
11918 /* Peek at the next token. */
11919 token = cp_lexer_peek_token (parser->lexer);
11921 /* Let our caller know whether or not this initializer was
11923 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11924 /* Assume that the initializer is constant. */
11925 *non_constant_p = false;
11927 if (token->type == CPP_EQ)
11929 /* Consume the `='. */
11930 cp_lexer_consume_token (parser->lexer);
11931 /* Parse the initializer-clause. */
11932 init = cp_parser_initializer_clause (parser, non_constant_p);
11934 else if (token->type == CPP_OPEN_PAREN)
11935 init = cp_parser_parenthesized_expression_list (parser, false,
11939 /* Anything else is an error. */
11940 cp_parser_error (parser, "expected initializer");
11941 init = error_mark_node;
11947 /* Parse an initializer-clause.
11949 initializer-clause:
11950 assignment-expression
11951 { initializer-list , [opt] }
11954 Returns an expression representing the initializer.
11956 If the `assignment-expression' production is used the value
11957 returned is simply a representation for the expression.
11959 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11960 the elements of the initializer-list (or NULL_TREE, if the last
11961 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11962 NULL_TREE. There is no way to detect whether or not the optional
11963 trailing `,' was provided. NON_CONSTANT_P is as for
11964 cp_parser_initializer. */
11967 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11971 /* If it is not a `{', then we are looking at an
11972 assignment-expression. */
11973 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11976 = cp_parser_constant_expression (parser,
11977 /*allow_non_constant_p=*/true,
11979 if (!*non_constant_p)
11980 initializer = fold_non_dependent_expr (initializer);
11984 /* Consume the `{' token. */
11985 cp_lexer_consume_token (parser->lexer);
11986 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11987 initializer = make_node (CONSTRUCTOR);
11988 /* If it's not a `}', then there is a non-trivial initializer. */
11989 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11991 /* Parse the initializer list. */
11992 CONSTRUCTOR_ELTS (initializer)
11993 = cp_parser_initializer_list (parser, non_constant_p);
11994 /* A trailing `,' token is allowed. */
11995 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11996 cp_lexer_consume_token (parser->lexer);
11998 /* Now, there should be a trailing `}'. */
11999 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12002 return initializer;
12005 /* Parse an initializer-list.
12009 initializer-list , initializer-clause
12014 identifier : initializer-clause
12015 initializer-list, identifier : initializer-clause
12017 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12018 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12019 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12020 as for cp_parser_initializer. */
12023 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12025 tree initializers = NULL_TREE;
12027 /* Assume all of the expressions are constant. */
12028 *non_constant_p = false;
12030 /* Parse the rest of the list. */
12036 bool clause_non_constant_p;
12038 /* If the next token is an identifier and the following one is a
12039 colon, we are looking at the GNU designated-initializer
12041 if (cp_parser_allow_gnu_extensions_p (parser)
12042 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12043 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12045 /* Consume the identifier. */
12046 identifier = cp_lexer_consume_token (parser->lexer)->value;
12047 /* Consume the `:'. */
12048 cp_lexer_consume_token (parser->lexer);
12051 identifier = NULL_TREE;
12053 /* Parse the initializer. */
12054 initializer = cp_parser_initializer_clause (parser,
12055 &clause_non_constant_p);
12056 /* If any clause is non-constant, so is the entire initializer. */
12057 if (clause_non_constant_p)
12058 *non_constant_p = true;
12059 /* Add it to the list. */
12060 initializers = tree_cons (identifier, initializer, initializers);
12062 /* If the next token is not a comma, we have reached the end of
12064 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12067 /* Peek at the next token. */
12068 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12069 /* If the next token is a `}', then we're still done. An
12070 initializer-clause can have a trailing `,' after the
12071 initializer-list and before the closing `}'. */
12072 if (token->type == CPP_CLOSE_BRACE)
12075 /* Consume the `,' token. */
12076 cp_lexer_consume_token (parser->lexer);
12079 /* The initializers were built up in reverse order, so we need to
12080 reverse them now. */
12081 return nreverse (initializers);
12084 /* Classes [gram.class] */
12086 /* Parse a class-name.
12092 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12093 to indicate that names looked up in dependent types should be
12094 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12095 keyword has been used to indicate that the name that appears next
12096 is a template. TYPE_P is true iff the next name should be treated
12097 as class-name, even if it is declared to be some other kind of name
12098 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12099 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
12100 being defined in a class-head.
12102 Returns the TYPE_DECL representing the class. */
12105 cp_parser_class_name (cp_parser *parser,
12106 bool typename_keyword_p,
12107 bool template_keyword_p,
12109 bool check_dependency_p,
12111 bool is_declaration)
12118 /* All class-names start with an identifier. */
12119 token = cp_lexer_peek_token (parser->lexer);
12120 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12122 cp_parser_error (parser, "expected class-name");
12123 return error_mark_node;
12126 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12127 to a template-id, so we save it here. */
12128 scope = parser->scope;
12129 if (scope == error_mark_node)
12130 return error_mark_node;
12132 /* Any name names a type if we're following the `typename' keyword
12133 in a qualified name where the enclosing scope is type-dependent. */
12134 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12135 && dependent_type_p (scope));
12136 /* Handle the common case (an identifier, but not a template-id)
12138 if (token->type == CPP_NAME
12139 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12143 /* Look for the identifier. */
12144 identifier = cp_parser_identifier (parser);
12145 /* If the next token isn't an identifier, we are certainly not
12146 looking at a class-name. */
12147 if (identifier == error_mark_node)
12148 decl = error_mark_node;
12149 /* If we know this is a type-name, there's no need to look it
12151 else if (typename_p)
12155 /* If the next token is a `::', then the name must be a type
12158 [basic.lookup.qual]
12160 During the lookup for a name preceding the :: scope
12161 resolution operator, object, function, and enumerator
12162 names are ignored. */
12163 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12165 /* Look up the name. */
12166 decl = cp_parser_lookup_name (parser, identifier,
12168 /*is_template=*/false,
12169 /*is_namespace=*/false,
12170 check_dependency_p,
12171 /*ambiguous_p=*/NULL);
12176 /* Try a template-id. */
12177 decl = cp_parser_template_id (parser, template_keyword_p,
12178 check_dependency_p,
12180 if (decl == error_mark_node)
12181 return error_mark_node;
12184 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12186 /* If this is a typename, create a TYPENAME_TYPE. */
12187 if (typename_p && decl != error_mark_node)
12189 decl = make_typename_type (scope, decl, /*complain=*/1);
12190 if (decl != error_mark_node)
12191 decl = TYPE_NAME (decl);
12194 /* Check to see that it is really the name of a class. */
12195 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12196 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12197 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12198 /* Situations like this:
12200 template <typename T> struct A {
12201 typename T::template X<int>::I i;
12204 are problematic. Is `T::template X<int>' a class-name? The
12205 standard does not seem to be definitive, but there is no other
12206 valid interpretation of the following `::'. Therefore, those
12207 names are considered class-names. */
12208 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12209 else if (decl == error_mark_node
12210 || TREE_CODE (decl) != TYPE_DECL
12211 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12213 cp_parser_error (parser, "expected class-name");
12214 return error_mark_node;
12220 /* Parse a class-specifier.
12223 class-head { member-specification [opt] }
12225 Returns the TREE_TYPE representing the class. */
12228 cp_parser_class_specifier (cp_parser* parser)
12232 tree attributes = NULL_TREE;
12233 int has_trailing_semicolon;
12234 bool nested_name_specifier_p;
12235 unsigned saved_num_template_parameter_lists;
12236 bool pop_p = false;
12237 tree scope = NULL_TREE;
12239 push_deferring_access_checks (dk_no_deferred);
12241 /* Parse the class-head. */
12242 type = cp_parser_class_head (parser,
12243 &nested_name_specifier_p,
12245 /* If the class-head was a semantic disaster, skip the entire body
12249 cp_parser_skip_to_end_of_block_or_statement (parser);
12250 pop_deferring_access_checks ();
12251 return error_mark_node;
12254 /* Look for the `{'. */
12255 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12257 pop_deferring_access_checks ();
12258 return error_mark_node;
12261 /* Issue an error message if type-definitions are forbidden here. */
12262 cp_parser_check_type_definition (parser);
12263 /* Remember that we are defining one more class. */
12264 ++parser->num_classes_being_defined;
12265 /* Inside the class, surrounding template-parameter-lists do not
12267 saved_num_template_parameter_lists
12268 = parser->num_template_parameter_lists;
12269 parser->num_template_parameter_lists = 0;
12271 /* Start the class. */
12272 if (nested_name_specifier_p)
12274 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12275 pop_p = push_scope (scope);
12277 type = begin_class_definition (type);
12279 if (type == error_mark_node)
12280 /* If the type is erroneous, skip the entire body of the class. */
12281 cp_parser_skip_to_closing_brace (parser);
12283 /* Parse the member-specification. */
12284 cp_parser_member_specification_opt (parser);
12286 /* Look for the trailing `}'. */
12287 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12288 /* We get better error messages by noticing a common problem: a
12289 missing trailing `;'. */
12290 token = cp_lexer_peek_token (parser->lexer);
12291 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12292 /* Look for trailing attributes to apply to this class. */
12293 if (cp_parser_allow_gnu_extensions_p (parser))
12295 tree sub_attr = cp_parser_attributes_opt (parser);
12296 attributes = chainon (attributes, sub_attr);
12298 if (type != error_mark_node)
12299 type = finish_struct (type, attributes);
12302 /* If this class is not itself within the scope of another class,
12303 then we need to parse the bodies of all of the queued function
12304 definitions. Note that the queued functions defined in a class
12305 are not always processed immediately following the
12306 class-specifier for that class. Consider:
12309 struct B { void f() { sizeof (A); } };
12312 If `f' were processed before the processing of `A' were
12313 completed, there would be no way to compute the size of `A'.
12314 Note that the nesting we are interested in here is lexical --
12315 not the semantic nesting given by TYPE_CONTEXT. In particular,
12318 struct A { struct B; };
12319 struct A::B { void f() { } };
12321 there is no need to delay the parsing of `A::B::f'. */
12322 if (--parser->num_classes_being_defined == 0)
12329 /* In a first pass, parse default arguments to the functions.
12330 Then, in a second pass, parse the bodies of the functions.
12331 This two-phased approach handles cases like:
12339 class_type = NULL_TREE;
12341 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12342 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12343 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12344 TREE_PURPOSE (parser->unparsed_functions_queues)
12345 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12347 fn = TREE_VALUE (queue_entry);
12348 /* If there are default arguments that have not yet been processed,
12349 take care of them now. */
12350 if (class_type != TREE_PURPOSE (queue_entry))
12353 pop_scope (class_type);
12354 class_type = TREE_PURPOSE (queue_entry);
12355 pop_p = push_scope (class_type);
12357 /* Make sure that any template parameters are in scope. */
12358 maybe_begin_member_template_processing (fn);
12359 /* Parse the default argument expressions. */
12360 cp_parser_late_parsing_default_args (parser, fn);
12361 /* Remove any template parameters from the symbol table. */
12362 maybe_end_member_template_processing ();
12365 pop_scope (class_type);
12366 /* Now parse the body of the functions. */
12367 for (TREE_VALUE (parser->unparsed_functions_queues)
12368 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12369 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12370 TREE_VALUE (parser->unparsed_functions_queues)
12371 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12373 /* Figure out which function we need to process. */
12374 fn = TREE_VALUE (queue_entry);
12376 /* A hack to prevent garbage collection. */
12379 /* Parse the function. */
12380 cp_parser_late_parsing_for_member (parser, fn);
12385 /* Put back any saved access checks. */
12386 pop_deferring_access_checks ();
12388 /* Restore the count of active template-parameter-lists. */
12389 parser->num_template_parameter_lists
12390 = saved_num_template_parameter_lists;
12395 /* Parse a class-head.
12398 class-key identifier [opt] base-clause [opt]
12399 class-key nested-name-specifier identifier base-clause [opt]
12400 class-key nested-name-specifier [opt] template-id
12404 class-key attributes identifier [opt] base-clause [opt]
12405 class-key attributes nested-name-specifier identifier base-clause [opt]
12406 class-key attributes nested-name-specifier [opt] template-id
12409 Returns the TYPE of the indicated class. Sets
12410 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12411 involving a nested-name-specifier was used, and FALSE otherwise.
12413 Returns NULL_TREE if the class-head is syntactically valid, but
12414 semantically invalid in a way that means we should skip the entire
12415 body of the class. */
12418 cp_parser_class_head (cp_parser* parser,
12419 bool* nested_name_specifier_p,
12420 tree *attributes_p)
12422 tree nested_name_specifier;
12423 enum tag_types class_key;
12424 tree id = NULL_TREE;
12425 tree type = NULL_TREE;
12427 bool template_id_p = false;
12428 bool qualified_p = false;
12429 bool invalid_nested_name_p = false;
12430 bool invalid_explicit_specialization_p = false;
12431 bool pop_p = false;
12432 unsigned num_templates;
12435 /* Assume no nested-name-specifier will be present. */
12436 *nested_name_specifier_p = false;
12437 /* Assume no template parameter lists will be used in defining the
12441 /* Look for the class-key. */
12442 class_key = cp_parser_class_key (parser);
12443 if (class_key == none_type)
12444 return error_mark_node;
12446 /* Parse the attributes. */
12447 attributes = cp_parser_attributes_opt (parser);
12449 /* If the next token is `::', that is invalid -- but sometimes
12450 people do try to write:
12454 Handle this gracefully by accepting the extra qualifier, and then
12455 issuing an error about it later if this really is a
12456 class-head. If it turns out just to be an elaborated type
12457 specifier, remain silent. */
12458 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12459 qualified_p = true;
12461 push_deferring_access_checks (dk_no_check);
12463 /* Determine the name of the class. Begin by looking for an
12464 optional nested-name-specifier. */
12465 nested_name_specifier
12466 = cp_parser_nested_name_specifier_opt (parser,
12467 /*typename_keyword_p=*/false,
12468 /*check_dependency_p=*/false,
12470 /*is_declaration=*/false);
12471 /* If there was a nested-name-specifier, then there *must* be an
12473 if (nested_name_specifier)
12475 /* Although the grammar says `identifier', it really means
12476 `class-name' or `template-name'. You are only allowed to
12477 define a class that has already been declared with this
12480 The proposed resolution for Core Issue 180 says that whever
12481 you see `class T::X' you should treat `X' as a type-name.
12483 It is OK to define an inaccessible class; for example:
12485 class A { class B; };
12488 We do not know if we will see a class-name, or a
12489 template-name. We look for a class-name first, in case the
12490 class-name is a template-id; if we looked for the
12491 template-name first we would stop after the template-name. */
12492 cp_parser_parse_tentatively (parser);
12493 type = cp_parser_class_name (parser,
12494 /*typename_keyword_p=*/false,
12495 /*template_keyword_p=*/false,
12497 /*check_dependency_p=*/false,
12498 /*class_head_p=*/true,
12499 /*is_declaration=*/false);
12500 /* If that didn't work, ignore the nested-name-specifier. */
12501 if (!cp_parser_parse_definitely (parser))
12503 invalid_nested_name_p = true;
12504 id = cp_parser_identifier (parser);
12505 if (id == error_mark_node)
12508 /* If we could not find a corresponding TYPE, treat this
12509 declaration like an unqualified declaration. */
12510 if (type == error_mark_node)
12511 nested_name_specifier = NULL_TREE;
12512 /* Otherwise, count the number of templates used in TYPE and its
12513 containing scopes. */
12518 for (scope = TREE_TYPE (type);
12519 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12520 scope = (TYPE_P (scope)
12521 ? TYPE_CONTEXT (scope)
12522 : DECL_CONTEXT (scope)))
12524 && CLASS_TYPE_P (scope)
12525 && CLASSTYPE_TEMPLATE_INFO (scope)
12526 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12527 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12531 /* Otherwise, the identifier is optional. */
12534 /* We don't know whether what comes next is a template-id,
12535 an identifier, or nothing at all. */
12536 cp_parser_parse_tentatively (parser);
12537 /* Check for a template-id. */
12538 id = cp_parser_template_id (parser,
12539 /*template_keyword_p=*/false,
12540 /*check_dependency_p=*/true,
12541 /*is_declaration=*/true);
12542 /* If that didn't work, it could still be an identifier. */
12543 if (!cp_parser_parse_definitely (parser))
12545 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12546 id = cp_parser_identifier (parser);
12552 template_id_p = true;
12557 pop_deferring_access_checks ();
12560 cp_parser_check_for_invalid_template_id (parser, id);
12562 /* If it's not a `:' or a `{' then we can't really be looking at a
12563 class-head, since a class-head only appears as part of a
12564 class-specifier. We have to detect this situation before calling
12565 xref_tag, since that has irreversible side-effects. */
12566 if (!cp_parser_next_token_starts_class_definition_p (parser))
12568 cp_parser_error (parser, "expected `{' or `:'");
12569 return error_mark_node;
12572 /* At this point, we're going ahead with the class-specifier, even
12573 if some other problem occurs. */
12574 cp_parser_commit_to_tentative_parse (parser);
12575 /* Issue the error about the overly-qualified name now. */
12577 cp_parser_error (parser,
12578 "global qualification of class name is invalid");
12579 else if (invalid_nested_name_p)
12580 cp_parser_error (parser,
12581 "qualified name does not name a class");
12582 else if (nested_name_specifier)
12585 /* Figure out in what scope the declaration is being placed. */
12586 scope = current_scope ();
12588 scope = current_namespace;
12589 /* If that scope does not contain the scope in which the
12590 class was originally declared, the program is invalid. */
12591 if (scope && !is_ancestor (scope, nested_name_specifier))
12593 error ("declaration of `%D' in `%D' which does not "
12594 "enclose `%D'", type, scope, nested_name_specifier);
12600 A declarator-id shall not be qualified exception of the
12601 definition of a ... nested class outside of its class
12602 ... [or] a the definition or explicit instantiation of a
12603 class member of a namespace outside of its namespace. */
12604 if (scope == nested_name_specifier)
12606 pedwarn ("extra qualification ignored");
12607 nested_name_specifier = NULL_TREE;
12611 /* An explicit-specialization must be preceded by "template <>". If
12612 it is not, try to recover gracefully. */
12613 if (at_namespace_scope_p ()
12614 && parser->num_template_parameter_lists == 0
12617 error ("an explicit specialization must be preceded by 'template <>'");
12618 invalid_explicit_specialization_p = true;
12619 /* Take the same action that would have been taken by
12620 cp_parser_explicit_specialization. */
12621 ++parser->num_template_parameter_lists;
12622 begin_specialization ();
12624 /* There must be no "return" statements between this point and the
12625 end of this function; set "type "to the correct return value and
12626 use "goto done;" to return. */
12627 /* Make sure that the right number of template parameters were
12629 if (!cp_parser_check_template_parameters (parser, num_templates))
12631 /* If something went wrong, there is no point in even trying to
12632 process the class-definition. */
12637 /* Look up the type. */
12640 type = TREE_TYPE (id);
12641 maybe_process_partial_specialization (type);
12643 else if (!nested_name_specifier)
12645 /* If the class was unnamed, create a dummy name. */
12647 id = make_anon_name ();
12648 type = xref_tag (class_key, id, /*globalize=*/false,
12649 parser->num_template_parameter_lists);
12654 bool pop_p = false;
12658 template <typename T> struct S { struct T };
12659 template <typename T> struct S<T>::T { };
12661 we will get a TYPENAME_TYPE when processing the definition of
12662 `S::T'. We need to resolve it to the actual type before we
12663 try to define it. */
12664 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12666 class_type = resolve_typename_type (TREE_TYPE (type),
12667 /*only_current_p=*/false);
12668 if (class_type != error_mark_node)
12669 type = TYPE_NAME (class_type);
12672 cp_parser_error (parser, "could not resolve typename type");
12673 type = error_mark_node;
12677 maybe_process_partial_specialization (TREE_TYPE (type));
12678 class_type = current_class_type;
12679 /* Enter the scope indicated by the nested-name-specifier. */
12680 if (nested_name_specifier)
12681 pop_p = push_scope (nested_name_specifier);
12682 /* Get the canonical version of this type. */
12683 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12684 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12685 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12686 type = push_template_decl (type);
12687 type = TREE_TYPE (type);
12688 if (nested_name_specifier)
12690 *nested_name_specifier_p = true;
12692 pop_scope (nested_name_specifier);
12695 /* Indicate whether this class was declared as a `class' or as a
12697 if (TREE_CODE (type) == RECORD_TYPE)
12698 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12699 cp_parser_check_class_key (class_key, type);
12701 /* Enter the scope containing the class; the names of base classes
12702 should be looked up in that context. For example, given:
12704 struct A { struct B {}; struct C; };
12705 struct A::C : B {};
12708 if (nested_name_specifier)
12709 pop_p = push_scope (nested_name_specifier);
12713 /* Get the list of base-classes, if there is one. */
12714 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12715 bases = cp_parser_base_clause (parser);
12717 /* Process the base classes. */
12718 xref_basetypes (type, bases);
12720 /* Leave the scope given by the nested-name-specifier. We will
12721 enter the class scope itself while processing the members. */
12723 pop_scope (nested_name_specifier);
12726 if (invalid_explicit_specialization_p)
12728 end_specialization ();
12729 --parser->num_template_parameter_lists;
12731 *attributes_p = attributes;
12735 /* Parse a class-key.
12742 Returns the kind of class-key specified, or none_type to indicate
12745 static enum tag_types
12746 cp_parser_class_key (cp_parser* parser)
12749 enum tag_types tag_type;
12751 /* Look for the class-key. */
12752 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12756 /* Check to see if the TOKEN is a class-key. */
12757 tag_type = cp_parser_token_is_class_key (token);
12759 cp_parser_error (parser, "expected class-key");
12763 /* Parse an (optional) member-specification.
12765 member-specification:
12766 member-declaration member-specification [opt]
12767 access-specifier : member-specification [opt] */
12770 cp_parser_member_specification_opt (cp_parser* parser)
12777 /* Peek at the next token. */
12778 token = cp_lexer_peek_token (parser->lexer);
12779 /* If it's a `}', or EOF then we've seen all the members. */
12780 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12783 /* See if this token is a keyword. */
12784 keyword = token->keyword;
12788 case RID_PROTECTED:
12790 /* Consume the access-specifier. */
12791 cp_lexer_consume_token (parser->lexer);
12792 /* Remember which access-specifier is active. */
12793 current_access_specifier = token->value;
12794 /* Look for the `:'. */
12795 cp_parser_require (parser, CPP_COLON, "`:'");
12799 /* Otherwise, the next construction must be a
12800 member-declaration. */
12801 cp_parser_member_declaration (parser);
12806 /* Parse a member-declaration.
12808 member-declaration:
12809 decl-specifier-seq [opt] member-declarator-list [opt] ;
12810 function-definition ; [opt]
12811 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12813 template-declaration
12815 member-declarator-list:
12817 member-declarator-list , member-declarator
12820 declarator pure-specifier [opt]
12821 declarator constant-initializer [opt]
12822 identifier [opt] : constant-expression
12826 member-declaration:
12827 __extension__ member-declaration
12830 declarator attributes [opt] pure-specifier [opt]
12831 declarator attributes [opt] constant-initializer [opt]
12832 identifier [opt] attributes [opt] : constant-expression */
12835 cp_parser_member_declaration (cp_parser* parser)
12837 cp_decl_specifier_seq decl_specifiers;
12838 tree prefix_attributes;
12840 int declares_class_or_enum;
12843 int saved_pedantic;
12845 /* Check for the `__extension__' keyword. */
12846 if (cp_parser_extension_opt (parser, &saved_pedantic))
12849 cp_parser_member_declaration (parser);
12850 /* Restore the old value of the PEDANTIC flag. */
12851 pedantic = saved_pedantic;
12856 /* Check for a template-declaration. */
12857 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12859 /* Parse the template-declaration. */
12860 cp_parser_template_declaration (parser, /*member_p=*/true);
12865 /* Check for a using-declaration. */
12866 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12868 /* Parse the using-declaration. */
12869 cp_parser_using_declaration (parser);
12874 /* Parse the decl-specifier-seq. */
12875 cp_parser_decl_specifier_seq (parser,
12876 CP_PARSER_FLAGS_OPTIONAL,
12878 &declares_class_or_enum);
12879 prefix_attributes = decl_specifiers.attributes;
12880 decl_specifiers.attributes = NULL_TREE;
12881 /* Check for an invalid type-name. */
12882 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12884 /* If there is no declarator, then the decl-specifier-seq should
12886 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12888 /* If there was no decl-specifier-seq, and the next token is a
12889 `;', then we have something like:
12895 Each member-declaration shall declare at least one member
12896 name of the class. */
12897 if (!decl_specifiers.any_specifiers_p)
12899 cp_token *token = cp_lexer_peek_token (parser->lexer);
12900 if (pedantic && !token->in_system_header)
12901 pedwarn ("%Hextra %<;%>", &token->location);
12907 /* See if this declaration is a friend. */
12908 friend_p = cp_parser_friend_p (&decl_specifiers);
12909 /* If there were decl-specifiers, check to see if there was
12910 a class-declaration. */
12911 type = check_tag_decl (&decl_specifiers);
12912 /* Nested classes have already been added to the class, but
12913 a `friend' needs to be explicitly registered. */
12916 /* If the `friend' keyword was present, the friend must
12917 be introduced with a class-key. */
12918 if (!declares_class_or_enum)
12919 error ("a class-key must be used when declaring a friend");
12922 template <typename T> struct A {
12923 friend struct A<T>::B;
12926 A<T>::B will be represented by a TYPENAME_TYPE, and
12927 therefore not recognized by check_tag_decl. */
12929 && decl_specifiers.type
12930 && TYPE_P (decl_specifiers.type))
12931 type = decl_specifiers.type;
12932 if (!type || !TYPE_P (type))
12933 error ("friend declaration does not name a class or "
12936 make_friend_class (current_class_type, type,
12937 /*complain=*/true);
12939 /* If there is no TYPE, an error message will already have
12941 else if (!type || type == error_mark_node)
12943 /* An anonymous aggregate has to be handled specially; such
12944 a declaration really declares a data member (with a
12945 particular type), as opposed to a nested class. */
12946 else if (ANON_AGGR_TYPE_P (type))
12948 /* Remove constructors and such from TYPE, now that we
12949 know it is an anonymous aggregate. */
12950 fixup_anonymous_aggr (type);
12951 /* And make the corresponding data member. */
12952 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12953 /* Add it to the class. */
12954 finish_member_declaration (decl);
12957 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12962 /* See if these declarations will be friends. */
12963 friend_p = cp_parser_friend_p (&decl_specifiers);
12965 /* Keep going until we hit the `;' at the end of the
12967 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12969 tree attributes = NULL_TREE;
12970 tree first_attribute;
12972 /* Peek at the next token. */
12973 token = cp_lexer_peek_token (parser->lexer);
12975 /* Check for a bitfield declaration. */
12976 if (token->type == CPP_COLON
12977 || (token->type == CPP_NAME
12978 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12984 /* Get the name of the bitfield. Note that we cannot just
12985 check TOKEN here because it may have been invalidated by
12986 the call to cp_lexer_peek_nth_token above. */
12987 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12988 identifier = cp_parser_identifier (parser);
12990 identifier = NULL_TREE;
12992 /* Consume the `:' token. */
12993 cp_lexer_consume_token (parser->lexer);
12994 /* Get the width of the bitfield. */
12996 = cp_parser_constant_expression (parser,
12997 /*allow_non_constant=*/false,
13000 /* Look for attributes that apply to the bitfield. */
13001 attributes = cp_parser_attributes_opt (parser);
13002 /* Remember which attributes are prefix attributes and
13004 first_attribute = attributes;
13005 /* Combine the attributes. */
13006 attributes = chainon (prefix_attributes, attributes);
13008 /* Create the bitfield declaration. */
13009 decl = grokbitfield (identifier
13010 ? make_id_declarator (identifier)
13014 /* Apply the attributes. */
13015 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13019 cp_declarator *declarator;
13021 tree asm_specification;
13022 int ctor_dtor_or_conv_p;
13024 /* Parse the declarator. */
13026 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13027 &ctor_dtor_or_conv_p,
13028 /*parenthesized_p=*/NULL);
13030 /* If something went wrong parsing the declarator, make sure
13031 that we at least consume some tokens. */
13032 if (declarator == cp_error_declarator)
13034 /* Skip to the end of the statement. */
13035 cp_parser_skip_to_end_of_statement (parser);
13036 /* If the next token is not a semicolon, that is
13037 probably because we just skipped over the body of
13038 a function. So, we consume a semicolon if
13039 present, but do not issue an error message if it
13041 if (cp_lexer_next_token_is (parser->lexer,
13043 cp_lexer_consume_token (parser->lexer);
13047 cp_parser_check_for_definition_in_return_type
13048 (declarator, declares_class_or_enum);
13050 /* Look for an asm-specification. */
13051 asm_specification = cp_parser_asm_specification_opt (parser);
13052 /* Look for attributes that apply to the declaration. */
13053 attributes = cp_parser_attributes_opt (parser);
13054 /* Remember which attributes are prefix attributes and
13056 first_attribute = attributes;
13057 /* Combine the attributes. */
13058 attributes = chainon (prefix_attributes, attributes);
13060 /* If it's an `=', then we have a constant-initializer or a
13061 pure-specifier. It is not correct to parse the
13062 initializer before registering the member declaration
13063 since the member declaration should be in scope while
13064 its initializer is processed. However, the rest of the
13065 front end does not yet provide an interface that allows
13066 us to handle this correctly. */
13067 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13071 A pure-specifier shall be used only in the declaration of
13072 a virtual function.
13074 A member-declarator can contain a constant-initializer
13075 only if it declares a static member of integral or
13078 Therefore, if the DECLARATOR is for a function, we look
13079 for a pure-specifier; otherwise, we look for a
13080 constant-initializer. When we call `grokfield', it will
13081 perform more stringent semantics checks. */
13082 if (declarator->kind == cdk_function)
13083 initializer = cp_parser_pure_specifier (parser);
13085 /* Parse the initializer. */
13086 initializer = cp_parser_constant_initializer (parser);
13088 /* Otherwise, there is no initializer. */
13090 initializer = NULL_TREE;
13092 /* See if we are probably looking at a function
13093 definition. We are certainly not looking at at a
13094 member-declarator. Calling `grokfield' has
13095 side-effects, so we must not do it unless we are sure
13096 that we are looking at a member-declarator. */
13097 if (cp_parser_token_starts_function_definition_p
13098 (cp_lexer_peek_token (parser->lexer)))
13100 /* The grammar does not allow a pure-specifier to be
13101 used when a member function is defined. (It is
13102 possible that this fact is an oversight in the
13103 standard, since a pure function may be defined
13104 outside of the class-specifier. */
13106 error ("pure-specifier on function-definition");
13107 decl = cp_parser_save_member_function_body (parser,
13111 /* If the member was not a friend, declare it here. */
13113 finish_member_declaration (decl);
13114 /* Peek at the next token. */
13115 token = cp_lexer_peek_token (parser->lexer);
13116 /* If the next token is a semicolon, consume it. */
13117 if (token->type == CPP_SEMICOLON)
13118 cp_lexer_consume_token (parser->lexer);
13123 /* Create the declaration. */
13124 decl = grokfield (declarator, &decl_specifiers,
13125 initializer, asm_specification,
13127 /* Any initialization must have been from a
13128 constant-expression. */
13129 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13130 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13134 /* Reset PREFIX_ATTRIBUTES. */
13135 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13136 attributes = TREE_CHAIN (attributes);
13138 TREE_CHAIN (attributes) = NULL_TREE;
13140 /* If there is any qualification still in effect, clear it
13141 now; we will be starting fresh with the next declarator. */
13142 parser->scope = NULL_TREE;
13143 parser->qualifying_scope = NULL_TREE;
13144 parser->object_scope = NULL_TREE;
13145 /* If it's a `,', then there are more declarators. */
13146 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13147 cp_lexer_consume_token (parser->lexer);
13148 /* If the next token isn't a `;', then we have a parse error. */
13149 else if (cp_lexer_next_token_is_not (parser->lexer,
13152 cp_parser_error (parser, "expected `;'");
13153 /* Skip tokens until we find a `;'. */
13154 cp_parser_skip_to_end_of_statement (parser);
13161 /* Add DECL to the list of members. */
13163 finish_member_declaration (decl);
13165 if (TREE_CODE (decl) == FUNCTION_DECL)
13166 cp_parser_save_default_args (parser, decl);
13171 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13174 /* Parse a pure-specifier.
13179 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13180 Otherwise, ERROR_MARK_NODE is returned. */
13183 cp_parser_pure_specifier (cp_parser* parser)
13187 /* Look for the `=' token. */
13188 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13189 return error_mark_node;
13190 /* Look for the `0' token. */
13191 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13192 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13193 to get information from the lexer about how the number was
13194 spelled in order to fix this problem. */
13195 if (!token || !integer_zerop (token->value))
13196 return error_mark_node;
13198 return integer_zero_node;
13201 /* Parse a constant-initializer.
13203 constant-initializer:
13204 = constant-expression
13206 Returns a representation of the constant-expression. */
13209 cp_parser_constant_initializer (cp_parser* parser)
13211 /* Look for the `=' token. */
13212 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13213 return error_mark_node;
13215 /* It is invalid to write:
13217 struct S { static const int i = { 7 }; };
13220 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13222 cp_parser_error (parser,
13223 "a brace-enclosed initializer is not allowed here");
13224 /* Consume the opening brace. */
13225 cp_lexer_consume_token (parser->lexer);
13226 /* Skip the initializer. */
13227 cp_parser_skip_to_closing_brace (parser);
13228 /* Look for the trailing `}'. */
13229 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13231 return error_mark_node;
13234 return cp_parser_constant_expression (parser,
13235 /*allow_non_constant=*/false,
13239 /* Derived classes [gram.class.derived] */
13241 /* Parse a base-clause.
13244 : base-specifier-list
13246 base-specifier-list:
13248 base-specifier-list , base-specifier
13250 Returns a TREE_LIST representing the base-classes, in the order in
13251 which they were declared. The representation of each node is as
13252 described by cp_parser_base_specifier.
13254 In the case that no bases are specified, this function will return
13255 NULL_TREE, not ERROR_MARK_NODE. */
13258 cp_parser_base_clause (cp_parser* parser)
13260 tree bases = NULL_TREE;
13262 /* Look for the `:' that begins the list. */
13263 cp_parser_require (parser, CPP_COLON, "`:'");
13265 /* Scan the base-specifier-list. */
13271 /* Look for the base-specifier. */
13272 base = cp_parser_base_specifier (parser);
13273 /* Add BASE to the front of the list. */
13274 if (base != error_mark_node)
13276 TREE_CHAIN (base) = bases;
13279 /* Peek at the next token. */
13280 token = cp_lexer_peek_token (parser->lexer);
13281 /* If it's not a comma, then the list is complete. */
13282 if (token->type != CPP_COMMA)
13284 /* Consume the `,'. */
13285 cp_lexer_consume_token (parser->lexer);
13288 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13289 base class had a qualified name. However, the next name that
13290 appears is certainly not qualified. */
13291 parser->scope = NULL_TREE;
13292 parser->qualifying_scope = NULL_TREE;
13293 parser->object_scope = NULL_TREE;
13295 return nreverse (bases);
13298 /* Parse a base-specifier.
13301 :: [opt] nested-name-specifier [opt] class-name
13302 virtual access-specifier [opt] :: [opt] nested-name-specifier
13304 access-specifier virtual [opt] :: [opt] nested-name-specifier
13307 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13308 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13309 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13310 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13313 cp_parser_base_specifier (cp_parser* parser)
13317 bool virtual_p = false;
13318 bool duplicate_virtual_error_issued_p = false;
13319 bool duplicate_access_error_issued_p = false;
13320 bool class_scope_p, template_p;
13321 tree access = access_default_node;
13324 /* Process the optional `virtual' and `access-specifier'. */
13327 /* Peek at the next token. */
13328 token = cp_lexer_peek_token (parser->lexer);
13329 /* Process `virtual'. */
13330 switch (token->keyword)
13333 /* If `virtual' appears more than once, issue an error. */
13334 if (virtual_p && !duplicate_virtual_error_issued_p)
13336 cp_parser_error (parser,
13337 "`virtual' specified more than once in base-specified");
13338 duplicate_virtual_error_issued_p = true;
13343 /* Consume the `virtual' token. */
13344 cp_lexer_consume_token (parser->lexer);
13349 case RID_PROTECTED:
13351 /* If more than one access specifier appears, issue an
13353 if (access != access_default_node
13354 && !duplicate_access_error_issued_p)
13356 cp_parser_error (parser,
13357 "more than one access specifier in base-specified");
13358 duplicate_access_error_issued_p = true;
13361 access = ridpointers[(int) token->keyword];
13363 /* Consume the access-specifier. */
13364 cp_lexer_consume_token (parser->lexer);
13373 /* It is not uncommon to see programs mechanically, erroneously, use
13374 the 'typename' keyword to denote (dependent) qualified types
13375 as base classes. */
13376 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13378 if (!processing_template_decl)
13379 error ("keyword `typename' not allowed outside of templates");
13381 error ("keyword `typename' not allowed in this context "
13382 "(the base class is implicitly a type)");
13383 cp_lexer_consume_token (parser->lexer);
13386 /* Look for the optional `::' operator. */
13387 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13388 /* Look for the nested-name-specifier. The simplest way to
13393 The keyword `typename' is not permitted in a base-specifier or
13394 mem-initializer; in these contexts a qualified name that
13395 depends on a template-parameter is implicitly assumed to be a
13398 is to pretend that we have seen the `typename' keyword at this
13400 cp_parser_nested_name_specifier_opt (parser,
13401 /*typename_keyword_p=*/true,
13402 /*check_dependency_p=*/true,
13404 /*is_declaration=*/true);
13405 /* If the base class is given by a qualified name, assume that names
13406 we see are type names or templates, as appropriate. */
13407 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13408 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13410 /* Finally, look for the class-name. */
13411 type = cp_parser_class_name (parser,
13415 /*check_dependency_p=*/true,
13416 /*class_head_p=*/false,
13417 /*is_declaration=*/true);
13419 if (type == error_mark_node)
13420 return error_mark_node;
13422 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13425 /* Exception handling [gram.exception] */
13427 /* Parse an (optional) exception-specification.
13429 exception-specification:
13430 throw ( type-id-list [opt] )
13432 Returns a TREE_LIST representing the exception-specification. The
13433 TREE_VALUE of each node is a type. */
13436 cp_parser_exception_specification_opt (cp_parser* parser)
13441 /* Peek at the next token. */
13442 token = cp_lexer_peek_token (parser->lexer);
13443 /* If it's not `throw', then there's no exception-specification. */
13444 if (!cp_parser_is_keyword (token, RID_THROW))
13447 /* Consume the `throw'. */
13448 cp_lexer_consume_token (parser->lexer);
13450 /* Look for the `('. */
13451 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13453 /* Peek at the next token. */
13454 token = cp_lexer_peek_token (parser->lexer);
13455 /* If it's not a `)', then there is a type-id-list. */
13456 if (token->type != CPP_CLOSE_PAREN)
13458 const char *saved_message;
13460 /* Types may not be defined in an exception-specification. */
13461 saved_message = parser->type_definition_forbidden_message;
13462 parser->type_definition_forbidden_message
13463 = "types may not be defined in an exception-specification";
13464 /* Parse the type-id-list. */
13465 type_id_list = cp_parser_type_id_list (parser);
13466 /* Restore the saved message. */
13467 parser->type_definition_forbidden_message = saved_message;
13470 type_id_list = empty_except_spec;
13472 /* Look for the `)'. */
13473 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13475 return type_id_list;
13478 /* Parse an (optional) type-id-list.
13482 type-id-list , type-id
13484 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13485 in the order that the types were presented. */
13488 cp_parser_type_id_list (cp_parser* parser)
13490 tree types = NULL_TREE;
13497 /* Get the next type-id. */
13498 type = cp_parser_type_id (parser);
13499 /* Add it to the list. */
13500 types = add_exception_specifier (types, type, /*complain=*/1);
13501 /* Peek at the next token. */
13502 token = cp_lexer_peek_token (parser->lexer);
13503 /* If it is not a `,', we are done. */
13504 if (token->type != CPP_COMMA)
13506 /* Consume the `,'. */
13507 cp_lexer_consume_token (parser->lexer);
13510 return nreverse (types);
13513 /* Parse a try-block.
13516 try compound-statement handler-seq */
13519 cp_parser_try_block (cp_parser* parser)
13523 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13524 try_block = begin_try_block ();
13525 cp_parser_compound_statement (parser, NULL, true);
13526 finish_try_block (try_block);
13527 cp_parser_handler_seq (parser);
13528 finish_handler_sequence (try_block);
13533 /* Parse a function-try-block.
13535 function-try-block:
13536 try ctor-initializer [opt] function-body handler-seq */
13539 cp_parser_function_try_block (cp_parser* parser)
13542 bool ctor_initializer_p;
13544 /* Look for the `try' keyword. */
13545 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13547 /* Let the rest of the front-end know where we are. */
13548 try_block = begin_function_try_block ();
13549 /* Parse the function-body. */
13551 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13552 /* We're done with the `try' part. */
13553 finish_function_try_block (try_block);
13554 /* Parse the handlers. */
13555 cp_parser_handler_seq (parser);
13556 /* We're done with the handlers. */
13557 finish_function_handler_sequence (try_block);
13559 return ctor_initializer_p;
13562 /* Parse a handler-seq.
13565 handler handler-seq [opt] */
13568 cp_parser_handler_seq (cp_parser* parser)
13574 /* Parse the handler. */
13575 cp_parser_handler (parser);
13576 /* Peek at the next token. */
13577 token = cp_lexer_peek_token (parser->lexer);
13578 /* If it's not `catch' then there are no more handlers. */
13579 if (!cp_parser_is_keyword (token, RID_CATCH))
13584 /* Parse a handler.
13587 catch ( exception-declaration ) compound-statement */
13590 cp_parser_handler (cp_parser* parser)
13595 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13596 handler = begin_handler ();
13597 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13598 declaration = cp_parser_exception_declaration (parser);
13599 finish_handler_parms (declaration, handler);
13600 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13601 cp_parser_compound_statement (parser, NULL, false);
13602 finish_handler (handler);
13605 /* Parse an exception-declaration.
13607 exception-declaration:
13608 type-specifier-seq declarator
13609 type-specifier-seq abstract-declarator
13613 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13614 ellipsis variant is used. */
13617 cp_parser_exception_declaration (cp_parser* parser)
13620 cp_decl_specifier_seq type_specifiers;
13621 cp_declarator *declarator;
13622 const char *saved_message;
13624 /* If it's an ellipsis, it's easy to handle. */
13625 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13627 /* Consume the `...' token. */
13628 cp_lexer_consume_token (parser->lexer);
13632 /* Types may not be defined in exception-declarations. */
13633 saved_message = parser->type_definition_forbidden_message;
13634 parser->type_definition_forbidden_message
13635 = "types may not be defined in exception-declarations";
13637 /* Parse the type-specifier-seq. */
13638 cp_parser_type_specifier_seq (parser, &type_specifiers);
13639 /* If it's a `)', then there is no declarator. */
13640 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13643 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13644 /*ctor_dtor_or_conv_p=*/NULL,
13645 /*parenthesized_p=*/NULL);
13647 /* Restore the saved message. */
13648 parser->type_definition_forbidden_message = saved_message;
13650 if (type_specifiers.any_specifiers_p)
13652 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13653 if (decl == NULL_TREE)
13654 error ("invalid catch parameter");
13662 /* Parse a throw-expression.
13665 throw assignment-expression [opt]
13667 Returns a THROW_EXPR representing the throw-expression. */
13670 cp_parser_throw_expression (cp_parser* parser)
13675 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13676 token = cp_lexer_peek_token (parser->lexer);
13677 /* Figure out whether or not there is an assignment-expression
13678 following the "throw" keyword. */
13679 if (token->type == CPP_COMMA
13680 || token->type == CPP_SEMICOLON
13681 || token->type == CPP_CLOSE_PAREN
13682 || token->type == CPP_CLOSE_SQUARE
13683 || token->type == CPP_CLOSE_BRACE
13684 || token->type == CPP_COLON)
13685 expression = NULL_TREE;
13687 expression = cp_parser_assignment_expression (parser);
13689 return build_throw (expression);
13692 /* GNU Extensions */
13694 /* Parse an (optional) asm-specification.
13697 asm ( string-literal )
13699 If the asm-specification is present, returns a STRING_CST
13700 corresponding to the string-literal. Otherwise, returns
13704 cp_parser_asm_specification_opt (cp_parser* parser)
13707 tree asm_specification;
13709 /* Peek at the next token. */
13710 token = cp_lexer_peek_token (parser->lexer);
13711 /* If the next token isn't the `asm' keyword, then there's no
13712 asm-specification. */
13713 if (!cp_parser_is_keyword (token, RID_ASM))
13716 /* Consume the `asm' token. */
13717 cp_lexer_consume_token (parser->lexer);
13718 /* Look for the `('. */
13719 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13721 /* Look for the string-literal. */
13722 asm_specification = cp_parser_string_literal (parser, false, false);
13724 /* Look for the `)'. */
13725 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13727 return asm_specification;
13730 /* Parse an asm-operand-list.
13734 asm-operand-list , asm-operand
13737 string-literal ( expression )
13738 [ string-literal ] string-literal ( expression )
13740 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13741 each node is the expression. The TREE_PURPOSE is itself a
13742 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13743 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13744 is a STRING_CST for the string literal before the parenthesis. */
13747 cp_parser_asm_operand_list (cp_parser* parser)
13749 tree asm_operands = NULL_TREE;
13753 tree string_literal;
13757 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13759 /* Consume the `[' token. */
13760 cp_lexer_consume_token (parser->lexer);
13761 /* Read the operand name. */
13762 name = cp_parser_identifier (parser);
13763 if (name != error_mark_node)
13764 name = build_string (IDENTIFIER_LENGTH (name),
13765 IDENTIFIER_POINTER (name));
13766 /* Look for the closing `]'. */
13767 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13771 /* Look for the string-literal. */
13772 string_literal = cp_parser_string_literal (parser, false, false);
13774 /* Look for the `('. */
13775 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13776 /* Parse the expression. */
13777 expression = cp_parser_expression (parser);
13778 /* Look for the `)'. */
13779 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13781 /* Add this operand to the list. */
13782 asm_operands = tree_cons (build_tree_list (name, string_literal),
13785 /* If the next token is not a `,', there are no more
13787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13789 /* Consume the `,'. */
13790 cp_lexer_consume_token (parser->lexer);
13793 return nreverse (asm_operands);
13796 /* Parse an asm-clobber-list.
13800 asm-clobber-list , string-literal
13802 Returns a TREE_LIST, indicating the clobbers in the order that they
13803 appeared. The TREE_VALUE of each node is a STRING_CST. */
13806 cp_parser_asm_clobber_list (cp_parser* parser)
13808 tree clobbers = NULL_TREE;
13812 tree string_literal;
13814 /* Look for the string literal. */
13815 string_literal = cp_parser_string_literal (parser, false, false);
13816 /* Add it to the list. */
13817 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13818 /* If the next token is not a `,', then the list is
13820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13822 /* Consume the `,' token. */
13823 cp_lexer_consume_token (parser->lexer);
13829 /* Parse an (optional) series of attributes.
13832 attributes attribute
13835 __attribute__ (( attribute-list [opt] ))
13837 The return value is as for cp_parser_attribute_list. */
13840 cp_parser_attributes_opt (cp_parser* parser)
13842 tree attributes = NULL_TREE;
13847 tree attribute_list;
13849 /* Peek at the next token. */
13850 token = cp_lexer_peek_token (parser->lexer);
13851 /* If it's not `__attribute__', then we're done. */
13852 if (token->keyword != RID_ATTRIBUTE)
13855 /* Consume the `__attribute__' keyword. */
13856 cp_lexer_consume_token (parser->lexer);
13857 /* Look for the two `(' tokens. */
13858 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13859 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13861 /* Peek at the next token. */
13862 token = cp_lexer_peek_token (parser->lexer);
13863 if (token->type != CPP_CLOSE_PAREN)
13864 /* Parse the attribute-list. */
13865 attribute_list = cp_parser_attribute_list (parser);
13867 /* If the next token is a `)', then there is no attribute
13869 attribute_list = NULL;
13871 /* Look for the two `)' tokens. */
13872 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13873 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13875 /* Add these new attributes to the list. */
13876 attributes = chainon (attributes, attribute_list);
13882 /* Parse an attribute-list.
13886 attribute-list , attribute
13890 identifier ( identifier )
13891 identifier ( identifier , expression-list )
13892 identifier ( expression-list )
13894 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13895 TREE_PURPOSE of each node is the identifier indicating which
13896 attribute is in use. The TREE_VALUE represents the arguments, if
13900 cp_parser_attribute_list (cp_parser* parser)
13902 tree attribute_list = NULL_TREE;
13903 bool save_translate_strings_p = parser->translate_strings_p;
13905 parser->translate_strings_p = false;
13912 /* Look for the identifier. We also allow keywords here; for
13913 example `__attribute__ ((const))' is legal. */
13914 token = cp_lexer_peek_token (parser->lexer);
13915 if (token->type != CPP_NAME
13916 && token->type != CPP_KEYWORD)
13917 return error_mark_node;
13918 /* Consume the token. */
13919 token = cp_lexer_consume_token (parser->lexer);
13921 /* Save away the identifier that indicates which attribute this is. */
13922 identifier = token->value;
13923 attribute = build_tree_list (identifier, NULL_TREE);
13925 /* Peek at the next token. */
13926 token = cp_lexer_peek_token (parser->lexer);
13927 /* If it's an `(', then parse the attribute arguments. */
13928 if (token->type == CPP_OPEN_PAREN)
13932 arguments = (cp_parser_parenthesized_expression_list
13933 (parser, true, /*non_constant_p=*/NULL));
13934 /* Save the identifier and arguments away. */
13935 TREE_VALUE (attribute) = arguments;
13938 /* Add this attribute to the list. */
13939 TREE_CHAIN (attribute) = attribute_list;
13940 attribute_list = attribute;
13942 /* Now, look for more attributes. */
13943 token = cp_lexer_peek_token (parser->lexer);
13944 /* If the next token isn't a `,', we're done. */
13945 if (token->type != CPP_COMMA)
13948 /* Consume the comma and keep going. */
13949 cp_lexer_consume_token (parser->lexer);
13951 parser->translate_strings_p = save_translate_strings_p;
13953 /* We built up the list in reverse order. */
13954 return nreverse (attribute_list);
13957 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13958 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13959 current value of the PEDANTIC flag, regardless of whether or not
13960 the `__extension__' keyword is present. The caller is responsible
13961 for restoring the value of the PEDANTIC flag. */
13964 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13966 /* Save the old value of the PEDANTIC flag. */
13967 *saved_pedantic = pedantic;
13969 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13971 /* Consume the `__extension__' token. */
13972 cp_lexer_consume_token (parser->lexer);
13973 /* We're not being pedantic while the `__extension__' keyword is
13983 /* Parse a label declaration.
13986 __label__ label-declarator-seq ;
13988 label-declarator-seq:
13989 identifier , label-declarator-seq
13993 cp_parser_label_declaration (cp_parser* parser)
13995 /* Look for the `__label__' keyword. */
13996 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14002 /* Look for an identifier. */
14003 identifier = cp_parser_identifier (parser);
14004 /* Declare it as a lobel. */
14005 finish_label_decl (identifier);
14006 /* If the next token is a `;', stop. */
14007 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14009 /* Look for the `,' separating the label declarations. */
14010 cp_parser_require (parser, CPP_COMMA, "`,'");
14013 /* Look for the final `;'. */
14014 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14017 /* Support Functions */
14019 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14020 NAME should have one of the representations used for an
14021 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14022 is returned. If PARSER->SCOPE is a dependent type, then a
14023 SCOPE_REF is returned.
14025 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14026 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14027 was formed. Abstractly, such entities should not be passed to this
14028 function, because they do not need to be looked up, but it is
14029 simpler to check for this special case here, rather than at the
14032 In cases not explicitly covered above, this function returns a
14033 DECL, OVERLOAD, or baselink representing the result of the lookup.
14034 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14037 If IS_TYPE is TRUE, bindings that do not refer to types are
14040 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14043 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14046 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14049 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14050 results in an ambiguity, and false otherwise. */
14053 cp_parser_lookup_name (cp_parser *parser, tree name,
14054 bool is_type, bool is_template, bool is_namespace,
14055 bool check_dependency,
14059 tree object_type = parser->context->object_type;
14061 /* Assume that the lookup will be unambiguous. */
14063 *ambiguous_p = false;
14065 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14066 no longer valid. Note that if we are parsing tentatively, and
14067 the parse fails, OBJECT_TYPE will be automatically restored. */
14068 parser->context->object_type = NULL_TREE;
14070 if (name == error_mark_node)
14071 return error_mark_node;
14073 /* A template-id has already been resolved; there is no lookup to
14075 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14077 if (BASELINK_P (name))
14079 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14080 == TEMPLATE_ID_EXPR);
14084 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14085 it should already have been checked to make sure that the name
14086 used matches the type being destroyed. */
14087 if (TREE_CODE (name) == BIT_NOT_EXPR)
14091 /* Figure out to which type this destructor applies. */
14093 type = parser->scope;
14094 else if (object_type)
14095 type = object_type;
14097 type = current_class_type;
14098 /* If that's not a class type, there is no destructor. */
14099 if (!type || !CLASS_TYPE_P (type))
14100 return error_mark_node;
14101 if (!CLASSTYPE_DESTRUCTORS (type))
14102 return error_mark_node;
14103 /* If it was a class type, return the destructor. */
14104 return CLASSTYPE_DESTRUCTORS (type);
14107 /* By this point, the NAME should be an ordinary identifier. If
14108 the id-expression was a qualified name, the qualifying scope is
14109 stored in PARSER->SCOPE at this point. */
14110 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14112 /* Perform the lookup. */
14117 if (parser->scope == error_mark_node)
14118 return error_mark_node;
14120 /* If the SCOPE is dependent, the lookup must be deferred until
14121 the template is instantiated -- unless we are explicitly
14122 looking up names in uninstantiated templates. Even then, we
14123 cannot look up the name if the scope is not a class type; it
14124 might, for example, be a template type parameter. */
14125 dependent_p = (TYPE_P (parser->scope)
14126 && !(parser->in_declarator_p
14127 && currently_open_class (parser->scope))
14128 && dependent_type_p (parser->scope));
14129 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14133 /* The resolution to Core Issue 180 says that `struct A::B'
14134 should be considered a type-name, even if `A' is
14136 decl = TYPE_NAME (make_typename_type (parser->scope,
14139 else if (is_template)
14140 decl = make_unbound_class_template (parser->scope,
14144 decl = build_nt (SCOPE_REF, parser->scope, name);
14148 bool pop_p = false;
14150 /* If PARSER->SCOPE is a dependent type, then it must be a
14151 class type, and we must not be checking dependencies;
14152 otherwise, we would have processed this lookup above. So
14153 that PARSER->SCOPE is not considered a dependent base by
14154 lookup_member, we must enter the scope here. */
14156 pop_p = push_scope (parser->scope);
14157 /* If the PARSER->SCOPE is a a template specialization, it
14158 may be instantiated during name lookup. In that case,
14159 errors may be issued. Even if we rollback the current
14160 tentative parse, those errors are valid. */
14161 decl = lookup_qualified_name (parser->scope, name, is_type,
14162 /*complain=*/true);
14164 pop_scope (parser->scope);
14166 parser->qualifying_scope = parser->scope;
14167 parser->object_scope = NULL_TREE;
14169 else if (object_type)
14171 tree object_decl = NULL_TREE;
14172 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14173 OBJECT_TYPE is not a class. */
14174 if (CLASS_TYPE_P (object_type))
14175 /* If the OBJECT_TYPE is a template specialization, it may
14176 be instantiated during name lookup. In that case, errors
14177 may be issued. Even if we rollback the current tentative
14178 parse, those errors are valid. */
14179 object_decl = lookup_member (object_type,
14181 /*protect=*/0, is_type);
14182 /* Look it up in the enclosing context, too. */
14183 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14184 /*block_p=*/true, is_namespace,
14186 parser->object_scope = object_type;
14187 parser->qualifying_scope = NULL_TREE;
14189 decl = object_decl;
14193 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14194 /*block_p=*/true, is_namespace,
14196 parser->qualifying_scope = NULL_TREE;
14197 parser->object_scope = NULL_TREE;
14200 /* If the lookup failed, let our caller know. */
14202 || decl == error_mark_node
14203 || (TREE_CODE (decl) == FUNCTION_DECL
14204 && DECL_ANTICIPATED (decl)))
14205 return error_mark_node;
14207 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14208 if (TREE_CODE (decl) == TREE_LIST)
14211 *ambiguous_p = true;
14212 /* The error message we have to print is too complicated for
14213 cp_parser_error, so we incorporate its actions directly. */
14214 if (!cp_parser_simulate_error (parser))
14216 error ("reference to `%D' is ambiguous", name);
14217 print_candidates (decl);
14219 return error_mark_node;
14222 gcc_assert (DECL_P (decl)
14223 || TREE_CODE (decl) == OVERLOAD
14224 || TREE_CODE (decl) == SCOPE_REF
14225 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14226 || BASELINK_P (decl));
14228 /* If we have resolved the name of a member declaration, check to
14229 see if the declaration is accessible. When the name resolves to
14230 set of overloaded functions, accessibility is checked when
14231 overload resolution is done.
14233 During an explicit instantiation, access is not checked at all,
14234 as per [temp.explicit]. */
14236 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14241 /* Like cp_parser_lookup_name, but for use in the typical case where
14242 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14243 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14246 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14248 return cp_parser_lookup_name (parser, name,
14250 /*is_template=*/false,
14251 /*is_namespace=*/false,
14252 /*check_dependency=*/true,
14253 /*ambiguous_p=*/NULL);
14256 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14257 the current context, return the TYPE_DECL. If TAG_NAME_P is
14258 true, the DECL indicates the class being defined in a class-head,
14259 or declared in an elaborated-type-specifier.
14261 Otherwise, return DECL. */
14264 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14266 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14267 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14270 template <typename T> struct B;
14273 template <typename T> struct A::B {};
14275 Similarly, in a elaborated-type-specifier:
14277 namespace N { struct X{}; }
14280 template <typename T> friend struct N::X;
14283 However, if the DECL refers to a class type, and we are in
14284 the scope of the class, then the name lookup automatically
14285 finds the TYPE_DECL created by build_self_reference rather
14286 than a TEMPLATE_DECL. For example, in:
14288 template <class T> struct S {
14292 there is no need to handle such case. */
14294 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14295 return DECL_TEMPLATE_RESULT (decl);
14300 /* If too many, or too few, template-parameter lists apply to the
14301 declarator, issue an error message. Returns TRUE if all went well,
14302 and FALSE otherwise. */
14305 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14306 cp_declarator *declarator)
14308 unsigned num_templates;
14310 /* We haven't seen any classes that involve template parameters yet. */
14313 switch (declarator->kind)
14316 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14321 scope = TREE_OPERAND (declarator->u.id.name, 0);
14322 member = TREE_OPERAND (declarator->u.id.name, 1);
14324 while (scope && CLASS_TYPE_P (scope))
14326 /* You're supposed to have one `template <...>'
14327 for every template class, but you don't need one
14328 for a full specialization. For example:
14330 template <class T> struct S{};
14331 template <> struct S<int> { void f(); };
14332 void S<int>::f () {}
14334 is correct; there shouldn't be a `template <>' for
14335 the definition of `S<int>::f'. */
14336 if (CLASSTYPE_TEMPLATE_INFO (scope)
14337 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14338 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14339 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14342 scope = TYPE_CONTEXT (scope);
14346 /* If the DECLARATOR has the form `X<y>' then it uses one
14347 additional level of template parameters. */
14348 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14351 return cp_parser_check_template_parameters (parser,
14357 case cdk_reference:
14359 return (cp_parser_check_declarator_template_parameters
14360 (parser, declarator->declarator));
14366 gcc_unreachable ();
14371 /* NUM_TEMPLATES were used in the current declaration. If that is
14372 invalid, return FALSE and issue an error messages. Otherwise,
14376 cp_parser_check_template_parameters (cp_parser* parser,
14377 unsigned num_templates)
14379 /* If there are more template classes than parameter lists, we have
14382 template <class T> void S<T>::R<T>::f (); */
14383 if (parser->num_template_parameter_lists < num_templates)
14385 error ("too few template-parameter-lists");
14388 /* If there are the same number of template classes and parameter
14389 lists, that's OK. */
14390 if (parser->num_template_parameter_lists == num_templates)
14392 /* If there are more, but only one more, then we are referring to a
14393 member template. That's OK too. */
14394 if (parser->num_template_parameter_lists == num_templates + 1)
14396 /* Otherwise, there are too many template parameter lists. We have
14399 template <class T> template <class U> void S::f(); */
14400 error ("too many template-parameter-lists");
14404 /* Parse an optional `::' token indicating that the following name is
14405 from the global namespace. If so, PARSER->SCOPE is set to the
14406 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14407 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14408 Returns the new value of PARSER->SCOPE, if the `::' token is
14409 present, and NULL_TREE otherwise. */
14412 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14416 /* Peek at the next token. */
14417 token = cp_lexer_peek_token (parser->lexer);
14418 /* If we're looking at a `::' token then we're starting from the
14419 global namespace, not our current location. */
14420 if (token->type == CPP_SCOPE)
14422 /* Consume the `::' token. */
14423 cp_lexer_consume_token (parser->lexer);
14424 /* Set the SCOPE so that we know where to start the lookup. */
14425 parser->scope = global_namespace;
14426 parser->qualifying_scope = global_namespace;
14427 parser->object_scope = NULL_TREE;
14429 return parser->scope;
14431 else if (!current_scope_valid_p)
14433 parser->scope = NULL_TREE;
14434 parser->qualifying_scope = NULL_TREE;
14435 parser->object_scope = NULL_TREE;
14441 /* Returns TRUE if the upcoming token sequence is the start of a
14442 constructor declarator. If FRIEND_P is true, the declarator is
14443 preceded by the `friend' specifier. */
14446 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14448 bool constructor_p;
14449 tree type_decl = NULL_TREE;
14450 bool nested_name_p;
14451 cp_token *next_token;
14453 /* The common case is that this is not a constructor declarator, so
14454 try to avoid doing lots of work if at all possible. It's not
14455 valid declare a constructor at function scope. */
14456 if (at_function_scope_p ())
14458 /* And only certain tokens can begin a constructor declarator. */
14459 next_token = cp_lexer_peek_token (parser->lexer);
14460 if (next_token->type != CPP_NAME
14461 && next_token->type != CPP_SCOPE
14462 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14463 && next_token->type != CPP_TEMPLATE_ID)
14466 /* Parse tentatively; we are going to roll back all of the tokens
14468 cp_parser_parse_tentatively (parser);
14469 /* Assume that we are looking at a constructor declarator. */
14470 constructor_p = true;
14472 /* Look for the optional `::' operator. */
14473 cp_parser_global_scope_opt (parser,
14474 /*current_scope_valid_p=*/false);
14475 /* Look for the nested-name-specifier. */
14477 = (cp_parser_nested_name_specifier_opt (parser,
14478 /*typename_keyword_p=*/false,
14479 /*check_dependency_p=*/false,
14481 /*is_declaration=*/false)
14483 /* Outside of a class-specifier, there must be a
14484 nested-name-specifier. */
14485 if (!nested_name_p &&
14486 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14488 constructor_p = false;
14489 /* If we still think that this might be a constructor-declarator,
14490 look for a class-name. */
14495 template <typename T> struct S { S(); };
14496 template <typename T> S<T>::S ();
14498 we must recognize that the nested `S' names a class.
14501 template <typename T> S<T>::S<T> ();
14503 we must recognize that the nested `S' names a template. */
14504 type_decl = cp_parser_class_name (parser,
14505 /*typename_keyword_p=*/false,
14506 /*template_keyword_p=*/false,
14508 /*check_dependency_p=*/false,
14509 /*class_head_p=*/false,
14510 /*is_declaration=*/false);
14511 /* If there was no class-name, then this is not a constructor. */
14512 constructor_p = !cp_parser_error_occurred (parser);
14515 /* If we're still considering a constructor, we have to see a `(',
14516 to begin the parameter-declaration-clause, followed by either a
14517 `)', an `...', or a decl-specifier. We need to check for a
14518 type-specifier to avoid being fooled into thinking that:
14522 is a constructor. (It is actually a function named `f' that
14523 takes one parameter (of type `int') and returns a value of type
14526 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14529 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14530 /* A parameter declaration begins with a decl-specifier,
14531 which is either the "attribute" keyword, a storage class
14532 specifier, or (usually) a type-specifier. */
14533 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14534 && !cp_parser_storage_class_specifier_opt (parser))
14537 bool pop_p = false;
14538 unsigned saved_num_template_parameter_lists;
14540 /* Names appearing in the type-specifier should be looked up
14541 in the scope of the class. */
14542 if (current_class_type)
14546 type = TREE_TYPE (type_decl);
14547 if (TREE_CODE (type) == TYPENAME_TYPE)
14549 type = resolve_typename_type (type,
14550 /*only_current_p=*/false);
14551 if (type == error_mark_node)
14553 cp_parser_abort_tentative_parse (parser);
14557 pop_p = push_scope (type);
14560 /* Inside the constructor parameter list, surrounding
14561 template-parameter-lists do not apply. */
14562 saved_num_template_parameter_lists
14563 = parser->num_template_parameter_lists;
14564 parser->num_template_parameter_lists = 0;
14566 /* Look for the type-specifier. */
14567 cp_parser_type_specifier (parser,
14568 CP_PARSER_FLAGS_NONE,
14569 /*decl_specs=*/NULL,
14570 /*is_declarator=*/true,
14571 /*declares_class_or_enum=*/NULL,
14572 /*is_cv_qualifier=*/NULL);
14574 parser->num_template_parameter_lists
14575 = saved_num_template_parameter_lists;
14577 /* Leave the scope of the class. */
14581 constructor_p = !cp_parser_error_occurred (parser);
14585 constructor_p = false;
14586 /* We did not really want to consume any tokens. */
14587 cp_parser_abort_tentative_parse (parser);
14589 return constructor_p;
14592 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14593 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14594 they must be performed once we are in the scope of the function.
14596 Returns the function defined. */
14599 cp_parser_function_definition_from_specifiers_and_declarator
14600 (cp_parser* parser,
14601 cp_decl_specifier_seq *decl_specifiers,
14603 const cp_declarator *declarator)
14608 /* Begin the function-definition. */
14609 success_p = start_function (decl_specifiers, declarator, attributes);
14611 /* The things we're about to see are not directly qualified by any
14612 template headers we've seen thus far. */
14613 reset_specialization ();
14615 /* If there were names looked up in the decl-specifier-seq that we
14616 did not check, check them now. We must wait until we are in the
14617 scope of the function to perform the checks, since the function
14618 might be a friend. */
14619 perform_deferred_access_checks ();
14623 /* Skip the entire function. */
14624 error ("invalid function declaration");
14625 cp_parser_skip_to_end_of_block_or_statement (parser);
14626 fn = error_mark_node;
14629 fn = cp_parser_function_definition_after_declarator (parser,
14630 /*inline_p=*/false);
14635 /* Parse the part of a function-definition that follows the
14636 declarator. INLINE_P is TRUE iff this function is an inline
14637 function defined with a class-specifier.
14639 Returns the function defined. */
14642 cp_parser_function_definition_after_declarator (cp_parser* parser,
14646 bool ctor_initializer_p = false;
14647 bool saved_in_unbraced_linkage_specification_p;
14648 unsigned saved_num_template_parameter_lists;
14650 /* If the next token is `return', then the code may be trying to
14651 make use of the "named return value" extension that G++ used to
14653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14655 /* Consume the `return' keyword. */
14656 cp_lexer_consume_token (parser->lexer);
14657 /* Look for the identifier that indicates what value is to be
14659 cp_parser_identifier (parser);
14660 /* Issue an error message. */
14661 error ("named return values are no longer supported");
14662 /* Skip tokens until we reach the start of the function body. */
14663 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14664 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14665 cp_lexer_consume_token (parser->lexer);
14667 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14668 anything declared inside `f'. */
14669 saved_in_unbraced_linkage_specification_p
14670 = parser->in_unbraced_linkage_specification_p;
14671 parser->in_unbraced_linkage_specification_p = false;
14672 /* Inside the function, surrounding template-parameter-lists do not
14674 saved_num_template_parameter_lists
14675 = parser->num_template_parameter_lists;
14676 parser->num_template_parameter_lists = 0;
14677 /* If the next token is `try', then we are looking at a
14678 function-try-block. */
14679 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14680 ctor_initializer_p = cp_parser_function_try_block (parser);
14681 /* A function-try-block includes the function-body, so we only do
14682 this next part if we're not processing a function-try-block. */
14685 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14687 /* Finish the function. */
14688 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14689 (inline_p ? 2 : 0));
14690 /* Generate code for it, if necessary. */
14691 expand_or_defer_fn (fn);
14692 /* Restore the saved values. */
14693 parser->in_unbraced_linkage_specification_p
14694 = saved_in_unbraced_linkage_specification_p;
14695 parser->num_template_parameter_lists
14696 = saved_num_template_parameter_lists;
14701 /* Parse a template-declaration, assuming that the `export' (and
14702 `extern') keywords, if present, has already been scanned. MEMBER_P
14703 is as for cp_parser_template_declaration. */
14706 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14708 tree decl = NULL_TREE;
14709 tree parameter_list;
14710 bool friend_p = false;
14712 /* Look for the `template' keyword. */
14713 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14717 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14720 /* If the next token is `>', then we have an invalid
14721 specialization. Rather than complain about an invalid template
14722 parameter, issue an error message here. */
14723 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14725 cp_parser_error (parser, "invalid explicit specialization");
14726 begin_specialization ();
14727 parameter_list = NULL_TREE;
14731 /* Parse the template parameters. */
14732 begin_template_parm_list ();
14733 parameter_list = cp_parser_template_parameter_list (parser);
14734 parameter_list = end_template_parm_list (parameter_list);
14737 /* Look for the `>'. */
14738 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14739 /* We just processed one more parameter list. */
14740 ++parser->num_template_parameter_lists;
14741 /* If the next token is `template', there are more template
14743 if (cp_lexer_next_token_is_keyword (parser->lexer,
14745 cp_parser_template_declaration_after_export (parser, member_p);
14748 /* There are no access checks when parsing a template, as we do not
14749 know if a specialization will be a friend. */
14750 push_deferring_access_checks (dk_no_check);
14752 decl = cp_parser_single_declaration (parser,
14756 pop_deferring_access_checks ();
14758 /* If this is a member template declaration, let the front
14760 if (member_p && !friend_p && decl)
14762 if (TREE_CODE (decl) == TYPE_DECL)
14763 cp_parser_check_access_in_redeclaration (decl);
14765 decl = finish_member_template_decl (decl);
14767 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14768 make_friend_class (current_class_type, TREE_TYPE (decl),
14769 /*complain=*/true);
14771 /* We are done with the current parameter list. */
14772 --parser->num_template_parameter_lists;
14775 finish_template_decl (parameter_list);
14777 /* Register member declarations. */
14778 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14779 finish_member_declaration (decl);
14781 /* If DECL is a function template, we must return to parse it later.
14782 (Even though there is no definition, there might be default
14783 arguments that need handling.) */
14784 if (member_p && decl
14785 && (TREE_CODE (decl) == FUNCTION_DECL
14786 || DECL_FUNCTION_TEMPLATE_P (decl)))
14787 TREE_VALUE (parser->unparsed_functions_queues)
14788 = tree_cons (NULL_TREE, decl,
14789 TREE_VALUE (parser->unparsed_functions_queues));
14792 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14793 `function-definition' sequence. MEMBER_P is true, this declaration
14794 appears in a class scope.
14796 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14797 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14800 cp_parser_single_declaration (cp_parser* parser,
14804 int declares_class_or_enum;
14805 tree decl = NULL_TREE;
14806 cp_decl_specifier_seq decl_specifiers;
14807 bool function_definition_p = false;
14809 /* Defer access checks until we know what is being declared. */
14810 push_deferring_access_checks (dk_deferred);
14812 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14814 cp_parser_decl_specifier_seq (parser,
14815 CP_PARSER_FLAGS_OPTIONAL,
14817 &declares_class_or_enum);
14819 *friend_p = cp_parser_friend_p (&decl_specifiers);
14820 /* Gather up the access checks that occurred the
14821 decl-specifier-seq. */
14822 stop_deferring_access_checks ();
14824 /* Check for the declaration of a template class. */
14825 if (declares_class_or_enum)
14827 if (cp_parser_declares_only_class_p (parser))
14829 decl = shadow_tag (&decl_specifiers);
14830 if (decl && decl != error_mark_node)
14831 decl = TYPE_NAME (decl);
14833 decl = error_mark_node;
14838 /* If it's not a template class, try for a template function. If
14839 the next token is a `;', then this declaration does not declare
14840 anything. But, if there were errors in the decl-specifiers, then
14841 the error might well have come from an attempted class-specifier.
14842 In that case, there's no need to warn about a missing declarator. */
14844 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14845 || decl_specifiers.type != error_mark_node))
14846 decl = cp_parser_init_declarator (parser,
14848 /*function_definition_allowed_p=*/true,
14850 declares_class_or_enum,
14851 &function_definition_p);
14853 pop_deferring_access_checks ();
14855 /* Clear any current qualification; whatever comes next is the start
14856 of something new. */
14857 parser->scope = NULL_TREE;
14858 parser->qualifying_scope = NULL_TREE;
14859 parser->object_scope = NULL_TREE;
14860 /* Look for a trailing `;' after the declaration. */
14861 if (!function_definition_p
14862 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14863 cp_parser_skip_to_end_of_block_or_statement (parser);
14868 /* Parse a cast-expression that is not the operand of a unary "&". */
14871 cp_parser_simple_cast_expression (cp_parser *parser)
14873 return cp_parser_cast_expression (parser, /*address_p=*/false);
14876 /* Parse a functional cast to TYPE. Returns an expression
14877 representing the cast. */
14880 cp_parser_functional_cast (cp_parser* parser, tree type)
14882 tree expression_list;
14886 = cp_parser_parenthesized_expression_list (parser, false,
14887 /*non_constant_p=*/NULL);
14889 cast = build_functional_cast (type, expression_list);
14890 /* [expr.const]/1: In an integral constant expression "only type
14891 conversions to integral or enumeration type can be used". */
14892 if (cast != error_mark_node && !type_dependent_expression_p (type)
14893 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14895 if (cp_parser_non_integral_constant_expression
14896 (parser, "a call to a constructor"))
14897 return error_mark_node;
14902 /* Save the tokens that make up the body of a member function defined
14903 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14904 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14905 specifiers applied to the declaration. Returns the FUNCTION_DECL
14906 for the member function. */
14909 cp_parser_save_member_function_body (cp_parser* parser,
14910 cp_decl_specifier_seq *decl_specifiers,
14911 cp_declarator *declarator,
14918 /* Create the function-declaration. */
14919 fn = start_method (decl_specifiers, declarator, attributes);
14920 /* If something went badly wrong, bail out now. */
14921 if (fn == error_mark_node)
14923 /* If there's a function-body, skip it. */
14924 if (cp_parser_token_starts_function_definition_p
14925 (cp_lexer_peek_token (parser->lexer)))
14926 cp_parser_skip_to_end_of_block_or_statement (parser);
14927 return error_mark_node;
14930 /* Remember it, if there default args to post process. */
14931 cp_parser_save_default_args (parser, fn);
14933 /* Save away the tokens that make up the body of the
14935 first = parser->lexer->next_token;
14936 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14937 /* Handle function try blocks. */
14938 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14939 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14940 last = parser->lexer->next_token;
14942 /* Save away the inline definition; we will process it when the
14943 class is complete. */
14944 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
14945 DECL_PENDING_INLINE_P (fn) = 1;
14947 /* We need to know that this was defined in the class, so that
14948 friend templates are handled correctly. */
14949 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14951 /* We're done with the inline definition. */
14952 finish_method (fn);
14954 /* Add FN to the queue of functions to be parsed later. */
14955 TREE_VALUE (parser->unparsed_functions_queues)
14956 = tree_cons (NULL_TREE, fn,
14957 TREE_VALUE (parser->unparsed_functions_queues));
14962 /* Parse a template-argument-list, as well as the trailing ">" (but
14963 not the opening ">"). See cp_parser_template_argument_list for the
14967 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14971 tree saved_qualifying_scope;
14972 tree saved_object_scope;
14973 bool saved_greater_than_is_operator_p;
14977 When parsing a template-id, the first non-nested `>' is taken as
14978 the end of the template-argument-list rather than a greater-than
14980 saved_greater_than_is_operator_p
14981 = parser->greater_than_is_operator_p;
14982 parser->greater_than_is_operator_p = false;
14983 /* Parsing the argument list may modify SCOPE, so we save it
14985 saved_scope = parser->scope;
14986 saved_qualifying_scope = parser->qualifying_scope;
14987 saved_object_scope = parser->object_scope;
14988 /* Parse the template-argument-list itself. */
14989 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14990 arguments = NULL_TREE;
14992 arguments = cp_parser_template_argument_list (parser);
14993 /* Look for the `>' that ends the template-argument-list. If we find
14994 a '>>' instead, it's probably just a typo. */
14995 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14997 if (!saved_greater_than_is_operator_p)
14999 /* If we're in a nested template argument list, the '>>' has
15000 to be a typo for '> >'. We emit the error message, but we
15001 continue parsing and we push a '>' as next token, so that
15002 the argument list will be parsed correctly. Note that the
15003 global source location is still on the token before the
15004 '>>', so we need to say explicitly where we want it. */
15005 cp_token *token = cp_lexer_peek_token (parser->lexer);
15006 error ("%H%<>>%> should be %<> >%> "
15007 "within a nested template argument list",
15010 /* ??? Proper recovery should terminate two levels of
15011 template argument list here. */
15012 token->type = CPP_GREATER;
15016 /* If this is not a nested template argument list, the '>>'
15017 is a typo for '>'. Emit an error message and continue.
15018 Same deal about the token location, but here we can get it
15019 right by consuming the '>>' before issuing the diagnostic. */
15020 cp_lexer_consume_token (parser->lexer);
15021 error ("spurious %<>>%>, use %<>%> to terminate "
15022 "a template argument list");
15025 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15026 error ("missing %<>%> to terminate the template argument list");
15028 /* It's what we want, a '>'; consume it. */
15029 cp_lexer_consume_token (parser->lexer);
15030 /* The `>' token might be a greater-than operator again now. */
15031 parser->greater_than_is_operator_p
15032 = saved_greater_than_is_operator_p;
15033 /* Restore the SAVED_SCOPE. */
15034 parser->scope = saved_scope;
15035 parser->qualifying_scope = saved_qualifying_scope;
15036 parser->object_scope = saved_object_scope;
15041 /* MEMBER_FUNCTION is a member function, or a friend. If default
15042 arguments, or the body of the function have not yet been parsed,
15046 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15048 /* If this member is a template, get the underlying
15050 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15051 member_function = DECL_TEMPLATE_RESULT (member_function);
15053 /* There should not be any class definitions in progress at this
15054 point; the bodies of members are only parsed outside of all class
15056 gcc_assert (parser->num_classes_being_defined == 0);
15057 /* While we're parsing the member functions we might encounter more
15058 classes. We want to handle them right away, but we don't want
15059 them getting mixed up with functions that are currently in the
15061 parser->unparsed_functions_queues
15062 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15064 /* Make sure that any template parameters are in scope. */
15065 maybe_begin_member_template_processing (member_function);
15067 /* If the body of the function has not yet been parsed, parse it
15069 if (DECL_PENDING_INLINE_P (member_function))
15071 tree function_scope;
15072 cp_token_cache *tokens;
15074 /* The function is no longer pending; we are processing it. */
15075 tokens = DECL_PENDING_INLINE_INFO (member_function);
15076 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15077 DECL_PENDING_INLINE_P (member_function) = 0;
15078 /* If this was an inline function in a local class, enter the scope
15079 of the containing function. */
15080 function_scope = decl_function_context (member_function);
15081 if (function_scope)
15082 push_function_context_to (function_scope);
15084 /* Push the body of the function onto the lexer stack. */
15085 cp_parser_push_lexer_for_tokens (parser, tokens);
15087 /* Let the front end know that we going to be defining this
15089 start_preparsed_function (member_function, NULL_TREE,
15090 SF_PRE_PARSED | SF_INCLASS_INLINE);
15092 /* Now, parse the body of the function. */
15093 cp_parser_function_definition_after_declarator (parser,
15094 /*inline_p=*/true);
15096 /* Leave the scope of the containing function. */
15097 if (function_scope)
15098 pop_function_context_from (function_scope);
15099 cp_parser_pop_lexer (parser);
15102 /* Remove any template parameters from the symbol table. */
15103 maybe_end_member_template_processing ();
15105 /* Restore the queue. */
15106 parser->unparsed_functions_queues
15107 = TREE_CHAIN (parser->unparsed_functions_queues);
15110 /* If DECL contains any default args, remember it on the unparsed
15111 functions queue. */
15114 cp_parser_save_default_args (cp_parser* parser, tree decl)
15118 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15120 probe = TREE_CHAIN (probe))
15121 if (TREE_PURPOSE (probe))
15123 TREE_PURPOSE (parser->unparsed_functions_queues)
15124 = tree_cons (current_class_type, decl,
15125 TREE_PURPOSE (parser->unparsed_functions_queues));
15131 /* FN is a FUNCTION_DECL which may contains a parameter with an
15132 unparsed DEFAULT_ARG. Parse the default args now. This function
15133 assumes that the current scope is the scope in which the default
15134 argument should be processed. */
15137 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15139 bool saved_local_variables_forbidden_p;
15142 /* While we're parsing the default args, we might (due to the
15143 statement expression extension) encounter more classes. We want
15144 to handle them right away, but we don't want them getting mixed
15145 up with default args that are currently in the queue. */
15146 parser->unparsed_functions_queues
15147 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15149 /* Local variable names (and the `this' keyword) may not appear
15150 in a default argument. */
15151 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15152 parser->local_variables_forbidden_p = true;
15154 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15156 parm = TREE_CHAIN (parm))
15158 cp_token_cache *tokens;
15160 if (!TREE_PURPOSE (parm)
15161 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15164 /* Push the saved tokens for the default argument onto the parser's
15166 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15167 cp_parser_push_lexer_for_tokens (parser, tokens);
15169 /* Parse the assignment-expression. */
15170 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15172 /* If the token stream has not been completely used up, then
15173 there was extra junk after the end of the default
15175 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15176 cp_parser_error (parser, "expected `,'");
15178 /* Revert to the main lexer. */
15179 cp_parser_pop_lexer (parser);
15182 /* Restore the state of local_variables_forbidden_p. */
15183 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15185 /* Restore the queue. */
15186 parser->unparsed_functions_queues
15187 = TREE_CHAIN (parser->unparsed_functions_queues);
15190 /* Parse the operand of `sizeof' (or a similar operator). Returns
15191 either a TYPE or an expression, depending on the form of the
15192 input. The KEYWORD indicates which kind of expression we have
15196 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15198 static const char *format;
15199 tree expr = NULL_TREE;
15200 const char *saved_message;
15201 bool saved_integral_constant_expression_p;
15203 /* Initialize FORMAT the first time we get here. */
15205 format = "types may not be defined in `%s' expressions";
15207 /* Types cannot be defined in a `sizeof' expression. Save away the
15209 saved_message = parser->type_definition_forbidden_message;
15210 /* And create the new one. */
15211 parser->type_definition_forbidden_message
15212 = xmalloc (strlen (format)
15213 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15215 sprintf ((char *) parser->type_definition_forbidden_message,
15216 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15218 /* The restrictions on constant-expressions do not apply inside
15219 sizeof expressions. */
15220 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15221 parser->integral_constant_expression_p = false;
15223 /* Do not actually evaluate the expression. */
15225 /* If it's a `(', then we might be looking at the type-id
15227 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15230 bool saved_in_type_id_in_expr_p;
15232 /* We can't be sure yet whether we're looking at a type-id or an
15234 cp_parser_parse_tentatively (parser);
15235 /* Consume the `('. */
15236 cp_lexer_consume_token (parser->lexer);
15237 /* Parse the type-id. */
15238 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15239 parser->in_type_id_in_expr_p = true;
15240 type = cp_parser_type_id (parser);
15241 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15242 /* Now, look for the trailing `)'. */
15243 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15244 /* If all went well, then we're done. */
15245 if (cp_parser_parse_definitely (parser))
15247 cp_decl_specifier_seq decl_specs;
15249 /* Build a trivial decl-specifier-seq. */
15250 clear_decl_specs (&decl_specs);
15251 decl_specs.type = type;
15253 /* Call grokdeclarator to figure out what type this is. */
15254 expr = grokdeclarator (NULL,
15258 /*attrlist=*/NULL);
15262 /* If the type-id production did not work out, then we must be
15263 looking at the unary-expression production. */
15265 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15266 /* Go back to evaluating expressions. */
15269 /* Free the message we created. */
15270 free ((char *) parser->type_definition_forbidden_message);
15271 /* And restore the old one. */
15272 parser->type_definition_forbidden_message = saved_message;
15273 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15278 /* If the current declaration has no declarator, return true. */
15281 cp_parser_declares_only_class_p (cp_parser *parser)
15283 /* If the next token is a `;' or a `,' then there is no
15285 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15286 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15289 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15292 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15293 cp_storage_class storage_class)
15295 if (decl_specs->storage_class != sc_none)
15296 decl_specs->multiple_storage_classes_p = true;
15298 decl_specs->storage_class = storage_class;
15301 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15302 is true, the type is a user-defined type; otherwise it is a
15303 built-in type specified by a keyword. */
15306 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15308 bool user_defined_p)
15310 decl_specs->any_specifiers_p = true;
15312 /* If the user tries to redeclare a built-in type (with, for example,
15313 in "typedef int wchar_t;") we remember that this is what
15314 happened. In system headers, we ignore these declarations so
15315 that G++ can work with system headers that are not C++-safe. */
15316 if (decl_specs->specs[(int) ds_typedef]
15318 && (decl_specs->type
15319 || decl_specs->specs[(int) ds_long]
15320 || decl_specs->specs[(int) ds_short]
15321 || decl_specs->specs[(int) ds_unsigned]
15322 || decl_specs->specs[(int) ds_signed]))
15324 decl_specs->redefined_builtin_type = type_spec;
15325 if (!decl_specs->type)
15327 decl_specs->type = type_spec;
15328 decl_specs->user_defined_type_p = false;
15331 else if (decl_specs->type)
15332 decl_specs->multiple_types_p = true;
15335 decl_specs->type = type_spec;
15336 decl_specs->user_defined_type_p = user_defined_p;
15337 decl_specs->redefined_builtin_type = NULL_TREE;
15341 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15342 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15345 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15347 return decl_specifiers->specs[(int) ds_friend] != 0;
15350 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15351 issue an error message indicating that TOKEN_DESC was expected.
15353 Returns the token consumed, if the token had the appropriate type.
15354 Otherwise, returns NULL. */
15357 cp_parser_require (cp_parser* parser,
15358 enum cpp_ttype type,
15359 const char* token_desc)
15361 if (cp_lexer_next_token_is (parser->lexer, type))
15362 return cp_lexer_consume_token (parser->lexer);
15365 /* Output the MESSAGE -- unless we're parsing tentatively. */
15366 if (!cp_parser_simulate_error (parser))
15368 char *message = concat ("expected ", token_desc, NULL);
15369 cp_parser_error (parser, message);
15376 /* Like cp_parser_require, except that tokens will be skipped until
15377 the desired token is found. An error message is still produced if
15378 the next token is not as expected. */
15381 cp_parser_skip_until_found (cp_parser* parser,
15382 enum cpp_ttype type,
15383 const char* token_desc)
15386 unsigned nesting_depth = 0;
15388 if (cp_parser_require (parser, type, token_desc))
15391 /* Skip tokens until the desired token is found. */
15394 /* Peek at the next token. */
15395 token = cp_lexer_peek_token (parser->lexer);
15396 /* If we've reached the token we want, consume it and
15398 if (token->type == type && !nesting_depth)
15400 cp_lexer_consume_token (parser->lexer);
15403 /* If we've run out of tokens, stop. */
15404 if (token->type == CPP_EOF)
15406 if (token->type == CPP_OPEN_BRACE
15407 || token->type == CPP_OPEN_PAREN
15408 || token->type == CPP_OPEN_SQUARE)
15410 else if (token->type == CPP_CLOSE_BRACE
15411 || token->type == CPP_CLOSE_PAREN
15412 || token->type == CPP_CLOSE_SQUARE)
15414 if (nesting_depth-- == 0)
15417 /* Consume this token. */
15418 cp_lexer_consume_token (parser->lexer);
15422 /* If the next token is the indicated keyword, consume it. Otherwise,
15423 issue an error message indicating that TOKEN_DESC was expected.
15425 Returns the token consumed, if the token had the appropriate type.
15426 Otherwise, returns NULL. */
15429 cp_parser_require_keyword (cp_parser* parser,
15431 const char* token_desc)
15433 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15435 if (token && token->keyword != keyword)
15437 dyn_string_t error_msg;
15439 /* Format the error message. */
15440 error_msg = dyn_string_new (0);
15441 dyn_string_append_cstr (error_msg, "expected ");
15442 dyn_string_append_cstr (error_msg, token_desc);
15443 cp_parser_error (parser, error_msg->s);
15444 dyn_string_delete (error_msg);
15451 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15452 function-definition. */
15455 cp_parser_token_starts_function_definition_p (cp_token* token)
15457 return (/* An ordinary function-body begins with an `{'. */
15458 token->type == CPP_OPEN_BRACE
15459 /* A ctor-initializer begins with a `:'. */
15460 || token->type == CPP_COLON
15461 /* A function-try-block begins with `try'. */
15462 || token->keyword == RID_TRY
15463 /* The named return value extension begins with `return'. */
15464 || token->keyword == RID_RETURN);
15467 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15471 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15475 token = cp_lexer_peek_token (parser->lexer);
15476 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15479 /* Returns TRUE iff the next token is the "," or ">" ending a
15480 template-argument. ">>" is also accepted (after the full
15481 argument was parsed) because it's probably a typo for "> >",
15482 and there is a specific diagnostic for this. */
15485 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15489 token = cp_lexer_peek_token (parser->lexer);
15490 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15491 || token->type == CPP_RSHIFT);
15494 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15495 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15498 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15503 token = cp_lexer_peek_nth_token (parser->lexer, n);
15504 if (token->type == CPP_LESS)
15506 /* Check for the sequence `<::' in the original code. It would be lexed as
15507 `[:', where `[' is a digraph, and there is no whitespace before
15509 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15512 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15513 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15519 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15520 or none_type otherwise. */
15522 static enum tag_types
15523 cp_parser_token_is_class_key (cp_token* token)
15525 switch (token->keyword)
15530 return record_type;
15539 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15542 cp_parser_check_class_key (enum tag_types class_key, tree type)
15544 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15545 pedwarn ("`%s' tag used in naming `%#T'",
15546 class_key == union_type ? "union"
15547 : class_key == record_type ? "struct" : "class",
15551 /* Issue an error message if DECL is redeclared with different
15552 access than its original declaration [class.access.spec/3].
15553 This applies to nested classes and nested class templates.
15556 static void cp_parser_check_access_in_redeclaration (tree decl)
15558 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15561 if ((TREE_PRIVATE (decl)
15562 != (current_access_specifier == access_private_node))
15563 || (TREE_PROTECTED (decl)
15564 != (current_access_specifier == access_protected_node)))
15565 error ("%D redeclared with different access", decl);
15568 /* Look for the `template' keyword, as a syntactic disambiguator.
15569 Return TRUE iff it is present, in which case it will be
15573 cp_parser_optional_template_keyword (cp_parser *parser)
15575 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15577 /* The `template' keyword can only be used within templates;
15578 outside templates the parser can always figure out what is a
15579 template and what is not. */
15580 if (!processing_template_decl)
15582 error ("`template' (as a disambiguator) is only allowed "
15583 "within templates");
15584 /* If this part of the token stream is rescanned, the same
15585 error message would be generated. So, we purge the token
15586 from the stream. */
15587 cp_lexer_purge_token (parser->lexer);
15592 /* Consume the `template' keyword. */
15593 cp_lexer_consume_token (parser->lexer);
15601 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15602 set PARSER->SCOPE, and perform other related actions. */
15605 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15610 /* Get the stored value. */
15611 value = cp_lexer_consume_token (parser->lexer)->value;
15612 /* Perform any access checks that were deferred. */
15613 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15614 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15615 /* Set the scope from the stored value. */
15616 parser->scope = TREE_VALUE (value);
15617 parser->qualifying_scope = TREE_TYPE (value);
15618 parser->object_scope = NULL_TREE;
15621 /* Consume tokens up through a non-nested END token. */
15624 cp_parser_cache_group (cp_parser *parser,
15625 enum cpp_ttype end,
15632 /* Abort a parenthesized expression if we encounter a brace. */
15633 if ((end == CPP_CLOSE_PAREN || depth == 0)
15634 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15636 /* If we've reached the end of the file, stop. */
15637 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15639 /* Consume the next token. */
15640 token = cp_lexer_consume_token (parser->lexer);
15641 /* See if it starts a new group. */
15642 if (token->type == CPP_OPEN_BRACE)
15644 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15648 else if (token->type == CPP_OPEN_PAREN)
15649 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15650 else if (token->type == end)
15655 /* Begin parsing tentatively. We always save tokens while parsing
15656 tentatively so that if the tentative parsing fails we can restore the
15660 cp_parser_parse_tentatively (cp_parser* parser)
15662 /* Enter a new parsing context. */
15663 parser->context = cp_parser_context_new (parser->context);
15664 /* Begin saving tokens. */
15665 cp_lexer_save_tokens (parser->lexer);
15666 /* In order to avoid repetitive access control error messages,
15667 access checks are queued up until we are no longer parsing
15669 push_deferring_access_checks (dk_deferred);
15672 /* Commit to the currently active tentative parse. */
15675 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15677 cp_parser_context *context;
15680 /* Mark all of the levels as committed. */
15681 lexer = parser->lexer;
15682 for (context = parser->context; context->next; context = context->next)
15684 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15686 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15687 while (!cp_lexer_saving_tokens (lexer))
15688 lexer = lexer->next;
15689 cp_lexer_commit_tokens (lexer);
15693 /* Abort the currently active tentative parse. All consumed tokens
15694 will be rolled back, and no diagnostics will be issued. */
15697 cp_parser_abort_tentative_parse (cp_parser* parser)
15699 cp_parser_simulate_error (parser);
15700 /* Now, pretend that we want to see if the construct was
15701 successfully parsed. */
15702 cp_parser_parse_definitely (parser);
15705 /* Stop parsing tentatively. If a parse error has occurred, restore the
15706 token stream. Otherwise, commit to the tokens we have consumed.
15707 Returns true if no error occurred; false otherwise. */
15710 cp_parser_parse_definitely (cp_parser* parser)
15712 bool error_occurred;
15713 cp_parser_context *context;
15715 /* Remember whether or not an error occurred, since we are about to
15716 destroy that information. */
15717 error_occurred = cp_parser_error_occurred (parser);
15718 /* Remove the topmost context from the stack. */
15719 context = parser->context;
15720 parser->context = context->next;
15721 /* If no parse errors occurred, commit to the tentative parse. */
15722 if (!error_occurred)
15724 /* Commit to the tokens read tentatively, unless that was
15726 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15727 cp_lexer_commit_tokens (parser->lexer);
15729 pop_to_parent_deferring_access_checks ();
15731 /* Otherwise, if errors occurred, roll back our state so that things
15732 are just as they were before we began the tentative parse. */
15735 cp_lexer_rollback_tokens (parser->lexer);
15736 pop_deferring_access_checks ();
15738 /* Add the context to the front of the free list. */
15739 context->next = cp_parser_context_free_list;
15740 cp_parser_context_free_list = context;
15742 return !error_occurred;
15745 /* Returns true if we are parsing tentatively -- but have decided that
15746 we will stick with this tentative parse, even if errors occur. */
15749 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15751 return (cp_parser_parsing_tentatively (parser)
15752 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15755 /* Returns nonzero iff an error has occurred during the most recent
15756 tentative parse. */
15759 cp_parser_error_occurred (cp_parser* parser)
15761 return (cp_parser_parsing_tentatively (parser)
15762 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15765 /* Returns nonzero if GNU extensions are allowed. */
15768 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15770 return parser->allow_gnu_extensions_p;
15776 static GTY (()) cp_parser *the_parser;
15778 /* External interface. */
15780 /* Parse one entire translation unit. */
15783 c_parse_file (void)
15785 bool error_occurred;
15786 static bool already_called = false;
15788 if (already_called)
15790 sorry ("inter-module optimizations not implemented for C++");
15793 already_called = true;
15795 the_parser = cp_parser_new ();
15796 push_deferring_access_checks (flag_access_control
15797 ? dk_no_deferred : dk_no_check);
15798 error_occurred = cp_parser_translation_unit (the_parser);
15802 /* This variable must be provided by every front end. */
15806 #include "gt-cp-parser.h"