2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
42 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43 and c-lex.c) and the C++ parser. */
47 typedef struct cp_token GTY (())
49 /* The kind of token. */
50 ENUM_BITFIELD (cpp_ttype) type : 8;
51 /* If this token is a keyword, this value indicates which keyword.
52 Otherwise, this value is RID_MAX. */
53 ENUM_BITFIELD (rid) keyword : 8;
56 /* True if this token is from a system header. */
57 BOOL_BITFIELD in_system_header : 1;
58 /* True if this token is from a context where it is implicitly extern "C" */
59 BOOL_BITFIELD implicit_extern_c : 1;
60 /* The value associated with this token, if any. */
62 /* The location at which this token was found. */
66 /* The cp_lexer structure represents the C++ lexer. It is responsible
67 for managing the token stream from the preprocessor and supplying
68 it to the parser. Tokens are never added to the cp_lexer after
71 typedef struct cp_lexer GTY (())
73 /* The memory allocated for the buffer. Never NULL. */
74 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
75 /* A pointer just past the end of the memory allocated for the buffer. */
76 cp_token * GTY ((skip)) buffer_end;
77 /* A pointer just past the last available token. The tokens
78 in this lexer are [buffer, last_token). */
79 cp_token * GTY ((skip)) last_token;
81 /* The next available token. If NEXT_TOKEN is NULL, then there are
82 no more available tokens. */
83 cp_token * GTY ((skip)) next_token;
85 /* A stack indicating positions at which cp_lexer_save_tokens was
86 called. The top entry is the most recent position at which we
87 began saving tokens. The entries are differences in token
88 position between BUFFER and the first saved token.
89 If the stack is non-empty, we are saving tokens. */
90 varray_type saved_tokens;
92 /* True if we should output debugging information. */
95 /* The next lexer in a linked list of lexers. */
96 struct cp_lexer *next;
99 /* cp_token_cache is a range of tokens. There is no need to represent
100 allocate heap memory for it, since tokens are never removed from the
101 lexer's array. There is also no need for the GC to walk through
102 a cp_token_cache, since everything in here is referenced through
105 typedef struct cp_token_cache GTY(())
107 /* The beginning of the token range. */
108 cp_token * GTY((skip)) first;
110 /* Points immediately after the last token in the range. */
111 cp_token * GTY ((skip)) last;
116 static cp_lexer *cp_lexer_new_main
118 static cp_lexer *cp_lexer_new_from_tokens
119 (cp_token_cache *tokens);
120 static void cp_lexer_destroy
122 static int cp_lexer_saving_tokens
124 static cp_token *cp_lexer_next_token
125 (cp_lexer *, cp_token *);
126 static cp_token *cp_lexer_prev_token
127 (cp_lexer *, cp_token *);
128 static ptrdiff_t cp_lexer_token_difference
129 (cp_lexer *, cp_token *, cp_token *);
130 static void cp_lexer_grow_buffer
132 static void cp_lexer_get_preprocessor_token
133 (cp_lexer *, cp_token *);
134 static inline cp_token *cp_lexer_peek_token
136 static cp_token *cp_lexer_peek_nth_token
137 (cp_lexer *, size_t);
138 static inline bool cp_lexer_next_token_is
139 (cp_lexer *, enum cpp_ttype);
140 static bool cp_lexer_next_token_is_not
141 (cp_lexer *, enum cpp_ttype);
142 static bool cp_lexer_next_token_is_keyword
143 (cp_lexer *, enum rid);
144 static cp_token *cp_lexer_consume_token
146 static void cp_lexer_purge_token
148 static void cp_lexer_purge_tokens_after
149 (cp_lexer *, cp_token *);
150 static void cp_lexer_handle_pragma
152 static void cp_lexer_save_tokens
154 static void cp_lexer_commit_tokens
156 static void cp_lexer_rollback_tokens
158 #ifdef ENABLE_CHECKING
159 static void cp_lexer_print_token
160 (FILE *, cp_token *);
161 static inline bool cp_lexer_debugging_p
163 static void cp_lexer_start_debugging
164 (cp_lexer *) ATTRIBUTE_UNUSED;
165 static void cp_lexer_stop_debugging
166 (cp_lexer *) ATTRIBUTE_UNUSED;
167 static void cp_lexer_peek_token_emit_debug_info
168 (cp_lexer *, cp_token *);
170 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
171 about passing NULL to functions that require non-NULL arguments
172 (fputs, fprintf). It will never be used, so all we need is a value
173 of the right type that's guaranteed not to be NULL. */
174 #define cp_lexer_debug_stream stdout
175 #define cp_lexer_print_token(str, tok) (void) 0
176 #define cp_lexer_debugging_p(lexer) 0
177 #define cp_lexer_peek_token_emit_debug_info(lexer, tok) (void) 0
178 #endif /* ENABLE_CHECKING */
180 static cp_token_cache *cp_token_cache_new
181 (cp_token *, cp_token *);
183 /* Manifest constants. */
185 #define CP_LEXER_BUFFER_SIZE 10000
186 #define CP_SAVED_TOKENS_SIZE 5
188 /* A token type for keywords, as opposed to ordinary identifiers. */
189 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
191 /* A token type for template-ids. If a template-id is processed while
192 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
193 the value of the CPP_TEMPLATE_ID is whatever was returned by
194 cp_parser_template_id. */
195 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
197 /* A token type for nested-name-specifiers. If a
198 nested-name-specifier is processed while parsing tentatively, it is
199 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
200 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
201 cp_parser_nested_name_specifier_opt. */
202 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
204 /* A token type for tokens that are not tokens at all; these are used
205 to represent slots in the array where there used to be a token
206 that has now been deleted. */
207 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
209 /* The number of token types, including C++-specific ones. */
210 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
214 #ifdef ENABLE_CHECKING
215 /* The stream to which debugging output should be written. */
216 static FILE *cp_lexer_debug_stream;
217 #endif /* ENABLE_CHECKING */
219 /* Create a new main C++ lexer, the lexer that gets tokens from the
223 cp_lexer_new_main (void)
226 cp_token first_token;
228 /* Tell cpplib we want CPP_PRAGMA tokens. */
229 cpp_get_options (parse_in)->defer_pragmas = true;
231 /* Tell c_lex not to merge string constants. */
232 c_lex_return_raw_strings = true;
234 /* It's possible that lexing the first token will load a PCH file,
235 which is a GC collection point. So we have to grab the first
236 token before allocating any memory. */
237 cp_lexer_get_preprocessor_token (NULL, &first_token);
238 c_common_no_more_pch ();
240 /* Allocate the memory. */
241 lexer = GGC_CNEW (cp_lexer);
243 /* Create the buffer. */
244 lexer->buffer = ggc_calloc (CP_LEXER_BUFFER_SIZE, sizeof (cp_token));
245 lexer->buffer_end = lexer->buffer + CP_LEXER_BUFFER_SIZE;
247 /* There is one token in the buffer. */
248 lexer->last_token = lexer->buffer + 1;
249 lexer->next_token = lexer->buffer;
250 *lexer->next_token = first_token;
252 /* Create the SAVED_TOKENS stack. */
253 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
255 #ifdef ENABLE_CHECKING
256 /* Initially we are not debugging. */
257 lexer->debugging_p = false;
258 #endif /* ENABLE_CHECKING */
260 /* Get the rest of the tokens from the preprocessor. */
261 while (lexer->last_token[-1].type != CPP_EOF)
263 if (lexer->last_token == lexer->buffer_end)
264 cp_lexer_grow_buffer (lexer);
265 cp_lexer_get_preprocessor_token (lexer, lexer->last_token++);
268 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
269 direct calls to c_lex. Those callers all expect c_lex to do
270 string constant concatenation. */
271 c_lex_return_raw_strings = false;
273 gcc_assert (lexer->next_token->type != CPP_PURGED);
277 /* Create a new lexer whose token stream is primed with the tokens in
278 CACHE. When these tokens are exhausted, no new tokens will be read. */
281 cp_lexer_new_from_tokens (cp_token_cache *cache)
283 cp_token *first = cache->first;
284 cp_token *last = cache->last;
285 cp_lexer *lexer = GGC_CNEW (cp_lexer);
288 /* Allocate a new buffer. The reason we do this is to make sure
289 there's a CPP_EOF token at the end. An alternative would be to
290 modify cp_lexer_peek_token so that it checks for end-of-buffer
291 and returns a CPP_EOF when appropriate. */
293 lexer->buffer = GGC_NEWVEC (cp_token, (last - first) + 1);
294 memcpy (lexer->buffer, first, sizeof (cp_token) * (last - first));
295 lexer->next_token = lexer->buffer;
296 lexer->buffer_end = lexer->last_token = lexer->buffer + (last - first);
298 eof = lexer->buffer + (last - first);
300 eof->location = UNKNOWN_LOCATION;
301 eof->value = NULL_TREE;
302 eof->keyword = RID_MAX;
304 /* Create the SAVED_TOKENS stack. */
305 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
307 #ifdef ENABLE_CHECKING
308 /* Initially we are not debugging. */
309 lexer->debugging_p = false;
312 gcc_assert (lexer->next_token->type != CPP_PURGED);
316 /* Frees all resources associated with LEXER. */
319 cp_lexer_destroy (cp_lexer *lexer)
321 ggc_free (lexer->buffer);
325 /* Returns nonzero if debugging information should be output. */
327 #ifdef ENABLE_CHECKING
330 cp_lexer_debugging_p (cp_lexer *lexer)
332 return lexer->debugging_p;
335 #endif /* ENABLE_CHECKING */
337 /* TOKEN points into the circular token buffer. Return a pointer to
338 the next token in the buffer. */
340 static inline cp_token *
341 cp_lexer_next_token (cp_lexer* lexer ATTRIBUTE_UNUSED, cp_token* token)
347 /* TOKEN points into the circular token buffer. Return a pointer to
348 the previous token in the buffer. */
350 static inline cp_token *
351 cp_lexer_prev_token (cp_lexer* lexer ATTRIBUTE_UNUSED, cp_token* token)
356 /* nonzero if we are presently saving tokens. */
359 cp_lexer_saving_tokens (const cp_lexer* lexer)
361 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
364 /* Return a pointer to the token that is N tokens beyond TOKEN in the
367 static inline cp_token *
368 cp_lexer_advance_token (cp_lexer *lexer ATTRIBUTE_UNUSED,
369 cp_token *token, ptrdiff_t n)
374 /* Returns the number of times that START would have to be incremented
375 to reach FINISH. If START and FINISH are the same, returns zero. */
377 static inline ptrdiff_t
378 cp_lexer_token_difference (cp_lexer* lexer ATTRIBUTE_UNUSED,
379 cp_token* start, cp_token* finish)
381 return finish - start;
384 /* If the buffer is full, make it bigger. */
386 cp_lexer_grow_buffer (cp_lexer* lexer)
388 cp_token *old_buffer;
389 cp_token *new_buffer;
390 ptrdiff_t buffer_length;
392 /* This function should only be called when buffer is full. */
393 gcc_assert (lexer->last_token == lexer->buffer_end);
395 /* Remember the current buffer pointer. It will become invalid,
396 but we will need to do pointer arithmetic involving this
398 old_buffer = lexer->buffer;
399 /* Compute the current buffer size. */
400 buffer_length = lexer->buffer_end - lexer->buffer;
401 /* Allocate a buffer twice as big. */
402 new_buffer = ggc_realloc (lexer->buffer,
403 2 * buffer_length * sizeof (cp_token));
405 /* Recompute buffer positions. */
406 lexer->buffer = new_buffer;
407 lexer->buffer_end = new_buffer + 2 * buffer_length;
408 lexer->last_token = new_buffer + (lexer->last_token - old_buffer);
409 lexer->next_token = new_buffer + (lexer->next_token - old_buffer);
411 /* Clear the rest of the buffer. We never look at this storage,
412 but the garbage collector may. */
413 memset (lexer->last_token, 0,
414 (lexer->buffer_end - lexer->last_token) * sizeof(cp_token));
417 /* Store the next token from the preprocessor in *TOKEN. */
420 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
423 static int is_extern_c = 0;
427 /* Keep going until we get a token we like. */
430 /* Get a new token from the preprocessor. */
431 token->type = c_lex_with_flags (&token->value, &token->flags);
432 /* Issue messages about tokens we cannot process. */
438 error ("invalid token");
442 /* This is a good token, so we exit the loop. */
447 /* Now we've got our token. */
448 token->location = input_location;
449 token->in_system_header = in_system_header;
451 /* On some systems, some header files are surrounded by an
452 implicit extern "C" block. Set a flag in the token if it
453 comes from such a header. */
454 is_extern_c += pending_lang_change;
455 pending_lang_change = 0;
456 token->implicit_extern_c = is_extern_c > 0;
458 /* Check to see if this token is a keyword. */
459 if (token->type == CPP_NAME
460 && C_IS_RESERVED_WORD (token->value))
462 /* Mark this token as a keyword. */
463 token->type = CPP_KEYWORD;
464 /* Record which keyword. */
465 token->keyword = C_RID_CODE (token->value);
466 /* Update the value. Some keywords are mapped to particular
467 entities, rather than simply having the value of the
468 corresponding IDENTIFIER_NODE. For example, `__const' is
469 mapped to `const'. */
470 token->value = ridpointers[token->keyword];
473 token->keyword = RID_MAX;
476 /* Update the globals input_location and in_system_header from TOKEN. */
478 cp_lexer_set_source_position_from_token (cp_token *token)
480 if (token->type != CPP_EOF)
482 input_location = token->location;
483 in_system_header = token->in_system_header;
487 /* Return a pointer to the next token in the token stream, but do not
490 static inline cp_token *
491 cp_lexer_peek_token (cp_lexer *lexer)
493 if (cp_lexer_debugging_p (lexer))
494 cp_lexer_peek_token_emit_debug_info (lexer, lexer->next_token);
495 return lexer->next_token;
498 #ifdef ENABLE_CHECKING
499 /* Emit debug output for cp_lexer_peek_token. Split out into a
500 separate function so that cp_lexer_peek_token can be small and
504 cp_lexer_peek_token_emit_debug_info (cp_lexer *lexer ATTRIBUTE_UNUSED,
505 cp_token *token ATTRIBUTE_UNUSED)
507 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
508 cp_lexer_print_token (cp_lexer_debug_stream, token);
509 putc ('\n', cp_lexer_debug_stream);
513 /* Return true if the next token has the indicated TYPE. */
516 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
518 return cp_lexer_peek_token (lexer)->type == type;
521 /* Return true if the next token does not have the indicated TYPE. */
524 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
526 return !cp_lexer_next_token_is (lexer, type);
529 /* Return true if the next token is the indicated KEYWORD. */
532 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
536 /* Peek at the next token. */
537 token = cp_lexer_peek_token (lexer);
538 /* Check to see if it is the indicated keyword. */
539 return token->keyword == keyword;
542 /* Return a pointer to the Nth token in the token stream. If N is 1,
543 then this is precisely equivalent to cp_lexer_peek_token (except
544 that it is not inline). One would like to disallow that case, but
545 there is one case (cp_parser_nth_token_starts_template_id) where
546 the caller passes a variable for N and it might be 1. */
549 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
553 /* N is 1-based, not zero-based. */
556 if (cp_lexer_debugging_p (lexer))
557 fprintf (cp_lexer_debug_stream,
558 "cp_lexer: peeking ahead %ld at token: ", (long)n);
561 token = lexer->next_token;
565 if (token->type != CPP_PURGED)
569 if (cp_lexer_debugging_p (lexer))
571 cp_lexer_print_token (cp_lexer_debug_stream, token);
572 putc ('\n', cp_lexer_debug_stream);
578 /* Return the next token, and advance the lexer's next_token pointer
579 to point to the next non-purged token. */
582 cp_lexer_consume_token (cp_lexer* lexer)
584 cp_token *token = lexer->next_token;
588 while (lexer->next_token->type == CPP_PURGED);
590 cp_lexer_set_source_position_from_token (token);
592 /* Provide debugging output. */
593 if (cp_lexer_debugging_p (lexer))
595 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
596 cp_lexer_print_token (cp_lexer_debug_stream, token);
597 putc ('\n', cp_lexer_debug_stream);
603 /* Permanently remove the next token from the token stream, and
604 advance the next_token pointer to refer to the next non-purged
608 cp_lexer_purge_token (cp_lexer *lexer)
610 cp_token *tok = lexer->next_token;
611 tok->type = CPP_PURGED;
612 tok->location = UNKNOWN_LOCATION;
613 tok->value = NULL_TREE;
614 tok->keyword = RID_MAX;
618 while (lexer->next_token->type == CPP_PURGED);
621 /* Permanently remove all tokens after TOK, up to, but not
622 including, the token that will be returned next by
623 cp_lexer_peek_token. */
626 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
630 peek = cp_lexer_peek_token (lexer);
631 gcc_assert (tok < peek);
633 for ( tok += 1; tok != peek; tok += 1)
635 tok->type = CPP_PURGED;
636 tok->location = UNKNOWN_LOCATION;
637 tok->value = NULL_TREE;
638 tok->keyword = RID_MAX;
642 /* Consume and handle a pragma token. */
644 cp_lexer_handle_pragma (cp_lexer *lexer)
647 cp_token *token = cp_lexer_consume_token (lexer);
648 gcc_assert (token->type == CPP_PRAGMA);
649 gcc_assert (token->value);
651 s.len = TREE_STRING_LENGTH (token->value);
652 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
654 cpp_handle_deferred_pragma (parse_in, &s);
656 /* Clearing token->value here means that we will get an ICE if we
657 try to process this #pragma again (which should be impossible). */
661 /* Begin saving tokens. All tokens consumed after this point will be
665 cp_lexer_save_tokens (cp_lexer* lexer)
667 /* Provide debugging output. */
668 if (cp_lexer_debugging_p (lexer))
669 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
671 VARRAY_PUSH_INT (lexer->saved_tokens,
672 cp_lexer_token_difference (lexer,
677 /* Commit to the portion of the token stream most recently saved. */
680 cp_lexer_commit_tokens (cp_lexer* lexer)
682 /* Provide debugging output. */
683 if (cp_lexer_debugging_p (lexer))
684 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
686 VARRAY_POP (lexer->saved_tokens);
689 /* Return all tokens saved since the last call to cp_lexer_save_tokens
690 to the token stream. Stop saving tokens. */
693 cp_lexer_rollback_tokens (cp_lexer* lexer)
697 /* Provide debugging output. */
698 if (cp_lexer_debugging_p (lexer))
699 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
701 /* Find the token that was the NEXT_TOKEN when we started saving
703 delta = VARRAY_TOP_INT(lexer->saved_tokens);
704 /* Make it the next token again now. */
705 lexer->next_token = cp_lexer_advance_token (lexer, lexer->buffer, delta);
707 /* Stop saving tokens. */
708 VARRAY_POP (lexer->saved_tokens);
711 /* Print a representation of the TOKEN on the STREAM. */
713 #ifdef ENABLE_CHECKING
716 cp_lexer_print_token (FILE * stream, cp_token *token)
718 /* We don't use cpp_type2name here because the parser defines
719 a few tokens of its own. */
720 static const char *const token_names[] = {
721 /* cpplib-defined token types */
727 /* C++ parser token types - see "Manifest constants", above. */
730 "NESTED_NAME_SPECIFIER",
734 /* If we have a name for the token, print it out. Otherwise, we
735 simply give the numeric code. */
736 gcc_assert (token->type < ARRAY_SIZE(token_names));
737 fputs (token_names[token->type], stream);
739 /* For some tokens, print the associated data. */
743 /* Some keywords have a value that is not an IDENTIFIER_NODE.
744 For example, `struct' is mapped to an INTEGER_CST. */
745 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
747 /* else fall through */
749 fputs (IDENTIFIER_POINTER (token->value), stream);
755 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
763 /* Start emitting debugging information. */
766 cp_lexer_start_debugging (cp_lexer* lexer)
768 ++lexer->debugging_p;
771 /* Stop emitting debugging information. */
774 cp_lexer_stop_debugging (cp_lexer* lexer)
776 --lexer->debugging_p;
779 #endif /* ENABLE_CHECKING */
781 /* Create a new cp_token_cache, representing a range of tokens. */
783 static cp_token_cache *
784 cp_token_cache_new (cp_token *first, cp_token *last)
786 cp_token_cache *cache = GGC_NEW (cp_token_cache);
787 cache->first = first;
793 /* Decl-specifiers. */
795 static void clear_decl_specs
796 (cp_decl_specifier_seq *);
798 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
801 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
803 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
808 /* Nothing other than the parser should be creating declarators;
809 declarators are a semi-syntactic representation of C++ entities.
810 Other parts of the front end that need to create entities (like
811 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
813 static cp_declarator *make_id_declarator
815 static cp_declarator *make_call_declarator
816 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
817 static cp_declarator *make_array_declarator
818 (cp_declarator *, tree);
819 static cp_declarator *make_pointer_declarator
820 (cp_cv_quals, cp_declarator *);
821 static cp_declarator *make_reference_declarator
822 (cp_cv_quals, cp_declarator *);
823 static cp_parameter_declarator *make_parameter_declarator
824 (cp_decl_specifier_seq *, cp_declarator *, tree);
825 static cp_declarator *make_ptrmem_declarator
826 (cp_cv_quals, tree, cp_declarator *);
828 cp_declarator *cp_error_declarator;
830 /* The obstack on which declarators and related data structures are
832 static struct obstack declarator_obstack;
834 /* Alloc BYTES from the declarator memory pool. */
837 alloc_declarator (size_t bytes)
839 return obstack_alloc (&declarator_obstack, bytes);
842 /* Allocate a declarator of the indicated KIND. Clear fields that are
843 common to all declarators. */
845 static cp_declarator *
846 make_declarator (cp_declarator_kind kind)
848 cp_declarator *declarator;
850 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
851 declarator->kind = kind;
852 declarator->attributes = NULL_TREE;
853 declarator->declarator = NULL;
858 /* Make a declarator for a generalized identifier. */
861 make_id_declarator (tree id)
863 cp_declarator *declarator;
865 declarator = make_declarator (cdk_id);
866 declarator->u.id.name = id;
867 declarator->u.id.sfk = sfk_none;
872 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
873 of modifiers such as const or volatile to apply to the pointer
874 type, represented as identifiers. */
877 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
879 cp_declarator *declarator;
881 declarator = make_declarator (cdk_pointer);
882 declarator->declarator = target;
883 declarator->u.pointer.qualifiers = cv_qualifiers;
884 declarator->u.pointer.class_type = NULL_TREE;
889 /* Like make_pointer_declarator -- but for references. */
892 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
894 cp_declarator *declarator;
896 declarator = make_declarator (cdk_reference);
897 declarator->declarator = target;
898 declarator->u.pointer.qualifiers = cv_qualifiers;
899 declarator->u.pointer.class_type = NULL_TREE;
904 /* Like make_pointer_declarator -- but for a pointer to a non-static
905 member of CLASS_TYPE. */
908 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
909 cp_declarator *pointee)
911 cp_declarator *declarator;
913 declarator = make_declarator (cdk_ptrmem);
914 declarator->declarator = pointee;
915 declarator->u.pointer.qualifiers = cv_qualifiers;
916 declarator->u.pointer.class_type = class_type;
921 /* Make a declarator for the function given by TARGET, with the
922 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
923 "const"-qualified member function. The EXCEPTION_SPECIFICATION
924 indicates what exceptions can be thrown. */
927 make_call_declarator (cp_declarator *target,
928 cp_parameter_declarator *parms,
929 cp_cv_quals cv_qualifiers,
930 tree exception_specification)
932 cp_declarator *declarator;
934 declarator = make_declarator (cdk_function);
935 declarator->declarator = target;
936 declarator->u.function.parameters = parms;
937 declarator->u.function.qualifiers = cv_qualifiers;
938 declarator->u.function.exception_specification = exception_specification;
943 /* Make a declarator for an array of BOUNDS elements, each of which is
944 defined by ELEMENT. */
947 make_array_declarator (cp_declarator *element, tree bounds)
949 cp_declarator *declarator;
951 declarator = make_declarator (cdk_array);
952 declarator->declarator = element;
953 declarator->u.array.bounds = bounds;
958 cp_parameter_declarator *no_parameters;
960 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
961 DECLARATOR and DEFAULT_ARGUMENT. */
963 cp_parameter_declarator *
964 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
965 cp_declarator *declarator,
966 tree default_argument)
968 cp_parameter_declarator *parameter;
970 parameter = ((cp_parameter_declarator *)
971 alloc_declarator (sizeof (cp_parameter_declarator)));
972 parameter->next = NULL;
974 parameter->decl_specifiers = *decl_specifiers;
976 clear_decl_specs (¶meter->decl_specifiers);
977 parameter->declarator = declarator;
978 parameter->default_argument = default_argument;
979 parameter->ellipsis_p = false;
989 A cp_parser parses the token stream as specified by the C++
990 grammar. Its job is purely parsing, not semantic analysis. For
991 example, the parser breaks the token stream into declarators,
992 expressions, statements, and other similar syntactic constructs.
993 It does not check that the types of the expressions on either side
994 of an assignment-statement are compatible, or that a function is
995 not declared with a parameter of type `void'.
997 The parser invokes routines elsewhere in the compiler to perform
998 semantic analysis and to build up the abstract syntax tree for the
1001 The parser (and the template instantiation code, which is, in a
1002 way, a close relative of parsing) are the only parts of the
1003 compiler that should be calling push_scope and pop_scope, or
1004 related functions. The parser (and template instantiation code)
1005 keeps track of what scope is presently active; everything else
1006 should simply honor that. (The code that generates static
1007 initializers may also need to set the scope, in order to check
1008 access control correctly when emitting the initializers.)
1013 The parser is of the standard recursive-descent variety. Upcoming
1014 tokens in the token stream are examined in order to determine which
1015 production to use when parsing a non-terminal. Some C++ constructs
1016 require arbitrary look ahead to disambiguate. For example, it is
1017 impossible, in the general case, to tell whether a statement is an
1018 expression or declaration without scanning the entire statement.
1019 Therefore, the parser is capable of "parsing tentatively." When the
1020 parser is not sure what construct comes next, it enters this mode.
1021 Then, while we attempt to parse the construct, the parser queues up
1022 error messages, rather than issuing them immediately, and saves the
1023 tokens it consumes. If the construct is parsed successfully, the
1024 parser "commits", i.e., it issues any queued error messages and
1025 the tokens that were being preserved are permanently discarded.
1026 If, however, the construct is not parsed successfully, the parser
1027 rolls back its state completely so that it can resume parsing using
1028 a different alternative.
1033 The performance of the parser could probably be improved substantially.
1034 We could often eliminate the need to parse tentatively by looking ahead
1035 a little bit. In some places, this approach might not entirely eliminate
1036 the need to parse tentatively, but it might still speed up the average
1039 /* Flags that are passed to some parsing functions. These values can
1040 be bitwise-ored together. */
1042 typedef enum cp_parser_flags
1045 CP_PARSER_FLAGS_NONE = 0x0,
1046 /* The construct is optional. If it is not present, then no error
1047 should be issued. */
1048 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1049 /* When parsing a type-specifier, do not allow user-defined types. */
1050 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1053 /* The different kinds of declarators we want to parse. */
1055 typedef enum cp_parser_declarator_kind
1057 /* We want an abstract declarator. */
1058 CP_PARSER_DECLARATOR_ABSTRACT,
1059 /* We want a named declarator. */
1060 CP_PARSER_DECLARATOR_NAMED,
1061 /* We don't mind, but the name must be an unqualified-id. */
1062 CP_PARSER_DECLARATOR_EITHER
1063 } cp_parser_declarator_kind;
1065 /* The precedence values used to parse binary expressions. The minimum value
1066 of PREC must be 1, because zero is reserved to quickly discriminate
1067 binary operators from other tokens. */
1072 PREC_LOGICAL_OR_EXPRESSION,
1073 PREC_LOGICAL_AND_EXPRESSION,
1074 PREC_INCLUSIVE_OR_EXPRESSION,
1075 PREC_EXCLUSIVE_OR_EXPRESSION,
1076 PREC_AND_EXPRESSION,
1077 PREC_RELATIONAL_EXPRESSION,
1078 PREC_EQUALITY_EXPRESSION,
1079 PREC_SHIFT_EXPRESSION,
1080 PREC_ADDITIVE_EXPRESSION,
1081 PREC_MULTIPLICATIVE_EXPRESSION,
1083 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1086 /* A mapping from a token type to a corresponding tree node type, with a
1087 precedence value. */
1089 typedef struct cp_parser_binary_operations_map_node
1091 /* The token type. */
1092 enum cpp_ttype token_type;
1093 /* The corresponding tree code. */
1094 enum tree_code tree_type;
1095 /* The precedence of this operator. */
1096 enum cp_parser_prec prec;
1097 } cp_parser_binary_operations_map_node;
1099 /* The status of a tentative parse. */
1101 typedef enum cp_parser_status_kind
1103 /* No errors have occurred. */
1104 CP_PARSER_STATUS_KIND_NO_ERROR,
1105 /* An error has occurred. */
1106 CP_PARSER_STATUS_KIND_ERROR,
1107 /* We are committed to this tentative parse, whether or not an error
1109 CP_PARSER_STATUS_KIND_COMMITTED
1110 } cp_parser_status_kind;
1112 typedef struct cp_parser_expression_stack_entry
1115 enum tree_code tree_type;
1117 } cp_parser_expression_stack_entry;
1119 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1120 entries because precedence levels on the stack are monotonically
1122 typedef struct cp_parser_expression_stack_entry
1123 cp_parser_expression_stack[NUM_PREC_VALUES];
1125 /* Context that is saved and restored when parsing tentatively. */
1126 typedef struct cp_parser_context GTY (())
1128 /* If this is a tentative parsing context, the status of the
1130 enum cp_parser_status_kind status;
1131 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1132 that are looked up in this context must be looked up both in the
1133 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1134 the context of the containing expression. */
1137 /* The next parsing context in the stack. */
1138 struct cp_parser_context *next;
1139 } cp_parser_context;
1143 /* Constructors and destructors. */
1145 static cp_parser_context *cp_parser_context_new
1146 (cp_parser_context *);
1148 /* Class variables. */
1150 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1152 /* The operator-precedence table used by cp_parser_binary_expression.
1153 Transformed into an associative array (binops_by_token) by
1156 static const cp_parser_binary_operations_map_node binops[] = {
1157 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1158 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1160 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1161 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1162 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1164 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1165 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1167 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1168 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1170 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1171 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1172 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1173 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1174 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1175 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1177 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1178 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1180 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1182 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1184 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1186 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1188 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1191 /* The same as binops, but initialized by cp_parser_new so that
1192 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1194 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1196 /* Constructors and destructors. */
1198 /* Construct a new context. The context below this one on the stack
1199 is given by NEXT. */
1201 static cp_parser_context *
1202 cp_parser_context_new (cp_parser_context* next)
1204 cp_parser_context *context;
1206 /* Allocate the storage. */
1207 if (cp_parser_context_free_list != NULL)
1209 /* Pull the first entry from the free list. */
1210 context = cp_parser_context_free_list;
1211 cp_parser_context_free_list = context->next;
1212 memset (context, 0, sizeof (*context));
1215 context = GGC_CNEW (cp_parser_context);
1217 /* No errors have occurred yet in this context. */
1218 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1219 /* If this is not the bottomost context, copy information that we
1220 need from the previous context. */
1223 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1224 expression, then we are parsing one in this context, too. */
1225 context->object_type = next->object_type;
1226 /* Thread the stack. */
1227 context->next = next;
1233 /* The cp_parser structure represents the C++ parser. */
1235 typedef struct cp_parser GTY(())
1237 /* The lexer from which we are obtaining tokens. */
1240 /* The scope in which names should be looked up. If NULL_TREE, then
1241 we look up names in the scope that is currently open in the
1242 source program. If non-NULL, this is either a TYPE or
1243 NAMESPACE_DECL for the scope in which we should look.
1245 This value is not cleared automatically after a name is looked
1246 up, so we must be careful to clear it before starting a new look
1247 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1248 will look up `Z' in the scope of `X', rather than the current
1249 scope.) Unfortunately, it is difficult to tell when name lookup
1250 is complete, because we sometimes peek at a token, look it up,
1251 and then decide not to consume it. */
1254 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1255 last lookup took place. OBJECT_SCOPE is used if an expression
1256 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1257 respectively. QUALIFYING_SCOPE is used for an expression of the
1258 form "X::Y"; it refers to X. */
1260 tree qualifying_scope;
1262 /* A stack of parsing contexts. All but the bottom entry on the
1263 stack will be tentative contexts.
1265 We parse tentatively in order to determine which construct is in
1266 use in some situations. For example, in order to determine
1267 whether a statement is an expression-statement or a
1268 declaration-statement we parse it tentatively as a
1269 declaration-statement. If that fails, we then reparse the same
1270 token stream as an expression-statement. */
1271 cp_parser_context *context;
1273 /* True if we are parsing GNU C++. If this flag is not set, then
1274 GNU extensions are not recognized. */
1275 bool allow_gnu_extensions_p;
1277 /* TRUE if the `>' token should be interpreted as the greater-than
1278 operator. FALSE if it is the end of a template-id or
1279 template-parameter-list. */
1280 bool greater_than_is_operator_p;
1282 /* TRUE if default arguments are allowed within a parameter list
1283 that starts at this point. FALSE if only a gnu extension makes
1284 them permissible. */
1285 bool default_arg_ok_p;
1287 /* TRUE if we are parsing an integral constant-expression. See
1288 [expr.const] for a precise definition. */
1289 bool integral_constant_expression_p;
1291 /* TRUE if we are parsing an integral constant-expression -- but a
1292 non-constant expression should be permitted as well. This flag
1293 is used when parsing an array bound so that GNU variable-length
1294 arrays are tolerated. */
1295 bool allow_non_integral_constant_expression_p;
1297 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1298 been seen that makes the expression non-constant. */
1299 bool non_integral_constant_expression_p;
1301 /* TRUE if local variable names and `this' are forbidden in the
1303 bool local_variables_forbidden_p;
1305 /* TRUE if the declaration we are parsing is part of a
1306 linkage-specification of the form `extern string-literal
1308 bool in_unbraced_linkage_specification_p;
1310 /* TRUE if we are presently parsing a declarator, after the
1311 direct-declarator. */
1312 bool in_declarator_p;
1314 /* TRUE if we are presently parsing a template-argument-list. */
1315 bool in_template_argument_list_p;
1317 /* TRUE if we are presently parsing the body of an
1318 iteration-statement. */
1319 bool in_iteration_statement_p;
1321 /* TRUE if we are presently parsing the body of a switch
1323 bool in_switch_statement_p;
1325 /* TRUE if we are parsing a type-id in an expression context. In
1326 such a situation, both "type (expr)" and "type (type)" are valid
1328 bool in_type_id_in_expr_p;
1330 /* TRUE if we are currently in a header file where declarations are
1331 implicitly extern "C". */
1332 bool implicit_extern_c;
1334 /* TRUE if strings in expressions should be translated to the execution
1336 bool translate_strings_p;
1338 /* If non-NULL, then we are parsing a construct where new type
1339 definitions are not permitted. The string stored here will be
1340 issued as an error message if a type is defined. */
1341 const char *type_definition_forbidden_message;
1343 /* A list of lists. The outer list is a stack, used for member
1344 functions of local classes. At each level there are two sub-list,
1345 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1346 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1347 TREE_VALUE's. The functions are chained in reverse declaration
1350 The TREE_PURPOSE sublist contains those functions with default
1351 arguments that need post processing, and the TREE_VALUE sublist
1352 contains those functions with definitions that need post
1355 These lists can only be processed once the outermost class being
1356 defined is complete. */
1357 tree unparsed_functions_queues;
1359 /* The number of classes whose definitions are currently in
1361 unsigned num_classes_being_defined;
1363 /* The number of template parameter lists that apply directly to the
1364 current declaration. */
1365 unsigned num_template_parameter_lists;
1368 /* The type of a function that parses some kind of expression. */
1369 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1373 /* Constructors and destructors. */
1375 static cp_parser *cp_parser_new
1378 /* Routines to parse various constructs.
1380 Those that return `tree' will return the error_mark_node (rather
1381 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1382 Sometimes, they will return an ordinary node if error-recovery was
1383 attempted, even though a parse error occurred. So, to check
1384 whether or not a parse error occurred, you should always use
1385 cp_parser_error_occurred. If the construct is optional (indicated
1386 either by an `_opt' in the name of the function that does the
1387 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1388 the construct is not present. */
1390 /* Lexical conventions [gram.lex] */
1392 static tree cp_parser_identifier
1394 static tree cp_parser_string_literal
1395 (cp_parser *, bool, bool);
1397 /* Basic concepts [gram.basic] */
1399 static bool cp_parser_translation_unit
1402 /* Expressions [gram.expr] */
1404 static tree cp_parser_primary_expression
1405 (cp_parser *, cp_id_kind *, tree *);
1406 static tree cp_parser_id_expression
1407 (cp_parser *, bool, bool, bool *, bool);
1408 static tree cp_parser_unqualified_id
1409 (cp_parser *, bool, bool, bool);
1410 static tree cp_parser_nested_name_specifier_opt
1411 (cp_parser *, bool, bool, bool, bool);
1412 static tree cp_parser_nested_name_specifier
1413 (cp_parser *, bool, bool, bool, bool);
1414 static tree cp_parser_class_or_namespace_name
1415 (cp_parser *, bool, bool, bool, bool, bool);
1416 static tree cp_parser_postfix_expression
1417 (cp_parser *, bool);
1418 static tree cp_parser_postfix_open_square_expression
1419 (cp_parser *, tree, bool);
1420 static tree cp_parser_postfix_dot_deref_expression
1421 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1422 static tree cp_parser_parenthesized_expression_list
1423 (cp_parser *, bool, bool *);
1424 static void cp_parser_pseudo_destructor_name
1425 (cp_parser *, tree *, tree *);
1426 static tree cp_parser_unary_expression
1427 (cp_parser *, bool);
1428 static enum tree_code cp_parser_unary_operator
1430 static tree cp_parser_new_expression
1432 static tree cp_parser_new_placement
1434 static tree cp_parser_new_type_id
1435 (cp_parser *, tree *);
1436 static cp_declarator *cp_parser_new_declarator_opt
1438 static cp_declarator *cp_parser_direct_new_declarator
1440 static tree cp_parser_new_initializer
1442 static tree cp_parser_delete_expression
1444 static tree cp_parser_cast_expression
1445 (cp_parser *, bool);
1446 static tree cp_parser_binary_expression
1448 static tree cp_parser_question_colon_clause
1449 (cp_parser *, tree);
1450 static tree cp_parser_assignment_expression
1452 static enum tree_code cp_parser_assignment_operator_opt
1454 static tree cp_parser_expression
1456 static tree cp_parser_constant_expression
1457 (cp_parser *, bool, bool *);
1458 static tree cp_parser_builtin_offsetof
1461 /* Statements [gram.stmt.stmt] */
1463 static void cp_parser_statement
1464 (cp_parser *, tree);
1465 static tree cp_parser_labeled_statement
1466 (cp_parser *, tree);
1467 static tree cp_parser_expression_statement
1468 (cp_parser *, tree);
1469 static tree cp_parser_compound_statement
1470 (cp_parser *, tree, bool);
1471 static void cp_parser_statement_seq_opt
1472 (cp_parser *, tree);
1473 static tree cp_parser_selection_statement
1475 static tree cp_parser_condition
1477 static tree cp_parser_iteration_statement
1479 static void cp_parser_for_init_statement
1481 static tree cp_parser_jump_statement
1483 static void cp_parser_declaration_statement
1486 static tree cp_parser_implicitly_scoped_statement
1488 static void cp_parser_already_scoped_statement
1491 /* Declarations [gram.dcl.dcl] */
1493 static void cp_parser_declaration_seq_opt
1495 static void cp_parser_declaration
1497 static void cp_parser_block_declaration
1498 (cp_parser *, bool);
1499 static void cp_parser_simple_declaration
1500 (cp_parser *, bool);
1501 static void cp_parser_decl_specifier_seq
1502 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1503 static tree cp_parser_storage_class_specifier_opt
1505 static tree cp_parser_function_specifier_opt
1506 (cp_parser *, cp_decl_specifier_seq *);
1507 static tree cp_parser_type_specifier
1508 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1510 static tree cp_parser_simple_type_specifier
1511 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1512 static tree cp_parser_type_name
1514 static tree cp_parser_elaborated_type_specifier
1515 (cp_parser *, bool, bool);
1516 static tree cp_parser_enum_specifier
1518 static void cp_parser_enumerator_list
1519 (cp_parser *, tree);
1520 static void cp_parser_enumerator_definition
1521 (cp_parser *, tree);
1522 static tree cp_parser_namespace_name
1524 static void cp_parser_namespace_definition
1526 static void cp_parser_namespace_body
1528 static tree cp_parser_qualified_namespace_specifier
1530 static void cp_parser_namespace_alias_definition
1532 static void cp_parser_using_declaration
1534 static void cp_parser_using_directive
1536 static void cp_parser_asm_definition
1538 static void cp_parser_linkage_specification
1541 /* Declarators [gram.dcl.decl] */
1543 static tree cp_parser_init_declarator
1544 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1545 static cp_declarator *cp_parser_declarator
1546 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1547 static cp_declarator *cp_parser_direct_declarator
1548 (cp_parser *, cp_parser_declarator_kind, int *);
1549 static enum tree_code cp_parser_ptr_operator
1550 (cp_parser *, tree *, cp_cv_quals *);
1551 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1553 static tree cp_parser_declarator_id
1555 static tree cp_parser_type_id
1557 static void cp_parser_type_specifier_seq
1558 (cp_parser *, cp_decl_specifier_seq *);
1559 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1561 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1562 (cp_parser *, bool *);
1563 static cp_parameter_declarator *cp_parser_parameter_declaration
1564 (cp_parser *, bool, bool *);
1565 static void cp_parser_function_body
1567 static tree cp_parser_initializer
1568 (cp_parser *, bool *, bool *);
1569 static tree cp_parser_initializer_clause
1570 (cp_parser *, bool *);
1571 static tree cp_parser_initializer_list
1572 (cp_parser *, bool *);
1574 static bool cp_parser_ctor_initializer_opt_and_function_body
1577 /* Classes [gram.class] */
1579 static tree cp_parser_class_name
1580 (cp_parser *, bool, bool, bool, bool, bool, bool);
1581 static tree cp_parser_class_specifier
1583 static tree cp_parser_class_head
1584 (cp_parser *, bool *, tree *);
1585 static enum tag_types cp_parser_class_key
1587 static void cp_parser_member_specification_opt
1589 static void cp_parser_member_declaration
1591 static tree cp_parser_pure_specifier
1593 static tree cp_parser_constant_initializer
1596 /* Derived classes [gram.class.derived] */
1598 static tree cp_parser_base_clause
1600 static tree cp_parser_base_specifier
1603 /* Special member functions [gram.special] */
1605 static tree cp_parser_conversion_function_id
1607 static tree cp_parser_conversion_type_id
1609 static cp_declarator *cp_parser_conversion_declarator_opt
1611 static bool cp_parser_ctor_initializer_opt
1613 static void cp_parser_mem_initializer_list
1615 static tree cp_parser_mem_initializer
1617 static tree cp_parser_mem_initializer_id
1620 /* Overloading [gram.over] */
1622 static tree cp_parser_operator_function_id
1624 static tree cp_parser_operator
1627 /* Templates [gram.temp] */
1629 static void cp_parser_template_declaration
1630 (cp_parser *, bool);
1631 static tree cp_parser_template_parameter_list
1633 static tree cp_parser_template_parameter
1634 (cp_parser *, bool *);
1635 static tree cp_parser_type_parameter
1637 static tree cp_parser_template_id
1638 (cp_parser *, bool, bool, bool);
1639 static tree cp_parser_template_name
1640 (cp_parser *, bool, bool, bool, bool *);
1641 static tree cp_parser_template_argument_list
1643 static tree cp_parser_template_argument
1645 static void cp_parser_explicit_instantiation
1647 static void cp_parser_explicit_specialization
1650 /* Exception handling [gram.exception] */
1652 static tree cp_parser_try_block
1654 static bool cp_parser_function_try_block
1656 static void cp_parser_handler_seq
1658 static void cp_parser_handler
1660 static tree cp_parser_exception_declaration
1662 static tree cp_parser_throw_expression
1664 static tree cp_parser_exception_specification_opt
1666 static tree cp_parser_type_id_list
1669 /* GNU Extensions */
1671 static tree cp_parser_asm_specification_opt
1673 static tree cp_parser_asm_operand_list
1675 static tree cp_parser_asm_clobber_list
1677 static tree cp_parser_attributes_opt
1679 static tree cp_parser_attribute_list
1681 static bool cp_parser_extension_opt
1682 (cp_parser *, int *);
1683 static void cp_parser_label_declaration
1686 /* Utility Routines */
1688 static tree cp_parser_lookup_name
1689 (cp_parser *, tree, bool, bool, bool, bool, bool *);
1690 static tree cp_parser_lookup_name_simple
1691 (cp_parser *, tree);
1692 static tree cp_parser_maybe_treat_template_as_class
1694 static bool cp_parser_check_declarator_template_parameters
1695 (cp_parser *, cp_declarator *);
1696 static bool cp_parser_check_template_parameters
1697 (cp_parser *, unsigned);
1698 static tree cp_parser_simple_cast_expression
1700 static tree cp_parser_global_scope_opt
1701 (cp_parser *, bool);
1702 static bool cp_parser_constructor_declarator_p
1703 (cp_parser *, bool);
1704 static tree cp_parser_function_definition_from_specifiers_and_declarator
1705 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1706 static tree cp_parser_function_definition_after_declarator
1707 (cp_parser *, bool);
1708 static void cp_parser_template_declaration_after_export
1709 (cp_parser *, bool);
1710 static tree cp_parser_single_declaration
1711 (cp_parser *, bool, bool *);
1712 static tree cp_parser_functional_cast
1713 (cp_parser *, tree);
1714 static tree cp_parser_save_member_function_body
1715 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1716 static tree cp_parser_enclosed_template_argument_list
1718 static void cp_parser_save_default_args
1719 (cp_parser *, tree);
1720 static void cp_parser_late_parsing_for_member
1721 (cp_parser *, tree);
1722 static void cp_parser_late_parsing_default_args
1723 (cp_parser *, tree);
1724 static tree cp_parser_sizeof_operand
1725 (cp_parser *, enum rid);
1726 static bool cp_parser_declares_only_class_p
1728 static void cp_parser_set_storage_class
1729 (cp_decl_specifier_seq *, cp_storage_class);
1730 static void cp_parser_set_decl_spec_type
1731 (cp_decl_specifier_seq *, tree, bool);
1732 static bool cp_parser_friend_p
1733 (const cp_decl_specifier_seq *);
1734 static cp_token *cp_parser_require
1735 (cp_parser *, enum cpp_ttype, const char *);
1736 static cp_token *cp_parser_require_keyword
1737 (cp_parser *, enum rid, const char *);
1738 static bool cp_parser_token_starts_function_definition_p
1740 static bool cp_parser_next_token_starts_class_definition_p
1742 static bool cp_parser_next_token_ends_template_argument_p
1744 static bool cp_parser_nth_token_starts_template_argument_list_p
1745 (cp_parser *, size_t);
1746 static enum tag_types cp_parser_token_is_class_key
1748 static void cp_parser_check_class_key
1749 (enum tag_types, tree type);
1750 static void cp_parser_check_access_in_redeclaration
1752 static bool cp_parser_optional_template_keyword
1754 static void cp_parser_pre_parsed_nested_name_specifier
1756 static void cp_parser_cache_group
1757 (cp_parser *, enum cpp_ttype, unsigned);
1758 static void cp_parser_parse_tentatively
1760 static void cp_parser_commit_to_tentative_parse
1762 static void cp_parser_abort_tentative_parse
1764 static bool cp_parser_parse_definitely
1766 static inline bool cp_parser_parsing_tentatively
1768 static bool cp_parser_committed_to_tentative_parse
1770 static void cp_parser_error
1771 (cp_parser *, const char *);
1772 static void cp_parser_name_lookup_error
1773 (cp_parser *, tree, tree, const char *);
1774 static bool cp_parser_simulate_error
1776 static void cp_parser_check_type_definition
1778 static void cp_parser_check_for_definition_in_return_type
1779 (cp_declarator *, int);
1780 static void cp_parser_check_for_invalid_template_id
1781 (cp_parser *, tree);
1782 static bool cp_parser_non_integral_constant_expression
1783 (cp_parser *, const char *);
1784 static void cp_parser_diagnose_invalid_type_name
1785 (cp_parser *, tree, tree);
1786 static bool cp_parser_parse_and_diagnose_invalid_type_name
1788 static int cp_parser_skip_to_closing_parenthesis
1789 (cp_parser *, bool, bool, bool);
1790 static void cp_parser_skip_to_end_of_statement
1792 static void cp_parser_consume_semicolon_at_end_of_statement
1794 static void cp_parser_skip_to_end_of_block_or_statement
1796 static void cp_parser_skip_to_closing_brace
1798 static void cp_parser_skip_until_found
1799 (cp_parser *, enum cpp_ttype, const char *);
1800 static bool cp_parser_error_occurred
1802 static bool cp_parser_allow_gnu_extensions_p
1804 static bool cp_parser_is_string_literal
1806 static bool cp_parser_is_keyword
1807 (cp_token *, enum rid);
1808 static tree cp_parser_make_typename_type
1809 (cp_parser *, tree, tree);
1811 /* Returns nonzero if we are parsing tentatively. */
1814 cp_parser_parsing_tentatively (cp_parser* parser)
1816 return parser->context->next != NULL;
1819 /* Returns nonzero if TOKEN is a string literal. */
1822 cp_parser_is_string_literal (cp_token* token)
1824 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1827 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1830 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1832 return token->keyword == keyword;
1835 /* If not parsing tentatively, issue a diagnostic of the form
1836 FILE:LINE: MESSAGE before TOKEN
1837 where TOKEN is the next token in the input stream. MESSAGE
1838 (specified by the caller) is usually of the form "expected
1842 cp_parser_error (cp_parser* parser, const char* message)
1844 if (!cp_parser_simulate_error (parser))
1846 cp_token *token = cp_lexer_peek_token (parser->lexer);
1847 /* This diagnostic makes more sense if it is tagged to the line
1848 of the token we just peeked at. */
1849 cp_lexer_set_source_position_from_token (token);
1850 c_parse_error (message,
1851 /* Because c_parser_error does not understand
1852 CPP_KEYWORD, keywords are treated like
1854 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1859 /* Issue an error about name-lookup failing. NAME is the
1860 IDENTIFIER_NODE DECL is the result of
1861 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1862 the thing that we hoped to find. */
1865 cp_parser_name_lookup_error (cp_parser* parser,
1868 const char* desired)
1870 /* If name lookup completely failed, tell the user that NAME was not
1872 if (decl == error_mark_node)
1874 if (parser->scope && parser->scope != global_namespace)
1875 error ("%<%D::%D%> has not been declared",
1876 parser->scope, name);
1877 else if (parser->scope == global_namespace)
1878 error ("%<::%D%> has not been declared", name);
1879 else if (parser->object_scope
1880 && !CLASS_TYPE_P (parser->object_scope))
1881 error ("request for member %qD in non-class type %qT",
1882 name, parser->object_scope);
1883 else if (parser->object_scope)
1884 error ("%<%T::%D%> has not been declared",
1885 parser->object_scope, name);
1887 error ("`%D' has not been declared", name);
1889 else if (parser->scope && parser->scope != global_namespace)
1890 error ("%<%D::%D%> %s", parser->scope, name, desired);
1891 else if (parser->scope == global_namespace)
1892 error ("%<::%D%> %s", name, desired);
1894 error ("%qD %s", name, desired);
1897 /* If we are parsing tentatively, remember that an error has occurred
1898 during this tentative parse. Returns true if the error was
1899 simulated; false if a message should be issued by the caller. */
1902 cp_parser_simulate_error (cp_parser* parser)
1904 if (cp_parser_parsing_tentatively (parser)
1905 && !cp_parser_committed_to_tentative_parse (parser))
1907 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1913 /* This function is called when a type is defined. If type
1914 definitions are forbidden at this point, an error message is
1918 cp_parser_check_type_definition (cp_parser* parser)
1920 /* If types are forbidden here, issue a message. */
1921 if (parser->type_definition_forbidden_message)
1922 /* Use `%s' to print the string in case there are any escape
1923 characters in the message. */
1924 error ("%s", parser->type_definition_forbidden_message);
1927 /* This function is called when a declaration is parsed. If
1928 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1929 indicates that a type was defined in the decl-specifiers for DECL,
1930 then an error is issued. */
1933 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1934 int declares_class_or_enum)
1936 /* [dcl.fct] forbids type definitions in return types.
1937 Unfortunately, it's not easy to know whether or not we are
1938 processing a return type until after the fact. */
1940 && (declarator->kind == cdk_pointer
1941 || declarator->kind == cdk_reference
1942 || declarator->kind == cdk_ptrmem))
1943 declarator = declarator->declarator;
1945 && declarator->kind == cdk_function
1946 && declares_class_or_enum & 2)
1947 error ("new types may not be defined in a return type");
1950 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1951 "<" in any valid C++ program. If the next token is indeed "<",
1952 issue a message warning the user about what appears to be an
1953 invalid attempt to form a template-id. */
1956 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1962 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1965 error ("%qT is not a template", type);
1966 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1967 error ("%qE is not a template", type);
1969 error ("invalid template-id");
1970 /* Remember the location of the invalid "<". */
1971 if (cp_parser_parsing_tentatively (parser)
1972 && !cp_parser_committed_to_tentative_parse (parser))
1974 token = cp_lexer_peek_token (parser->lexer);
1975 token = cp_lexer_prev_token (parser->lexer, token);
1976 start = cp_lexer_token_difference (parser->lexer,
1977 parser->lexer->buffer,
1982 /* Consume the "<". */
1983 cp_lexer_consume_token (parser->lexer);
1984 /* Parse the template arguments. */
1985 cp_parser_enclosed_template_argument_list (parser);
1986 /* Permanently remove the invalid template arguments so that
1987 this error message is not issued again. */
1990 token = cp_lexer_advance_token (parser->lexer,
1991 parser->lexer->buffer,
1993 cp_lexer_purge_tokens_after (parser->lexer, token);
1998 /* If parsing an integral constant-expression, issue an error message
1999 about the fact that THING appeared and return true. Otherwise,
2000 return false, marking the current expression as non-constant. */
2003 cp_parser_non_integral_constant_expression (cp_parser *parser,
2006 if (parser->integral_constant_expression_p)
2008 if (!parser->allow_non_integral_constant_expression_p)
2010 error ("%s cannot appear in a constant-expression", thing);
2013 parser->non_integral_constant_expression_p = true;
2018 /* Emit a diagnostic for an invalid type name. Consider also if it is
2019 qualified or not and the result of a lookup, to provide a better
2023 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2025 tree decl, old_scope;
2026 /* Try to lookup the identifier. */
2027 old_scope = parser->scope;
2028 parser->scope = scope;
2029 decl = cp_parser_lookup_name_simple (parser, id);
2030 parser->scope = old_scope;
2031 /* If the lookup found a template-name, it means that the user forgot
2032 to specify an argument list. Emit an useful error message. */
2033 if (TREE_CODE (decl) == TEMPLATE_DECL)
2034 error ("invalid use of template-name %qE without an argument list",
2036 else if (!parser->scope)
2038 /* Issue an error message. */
2039 error ("%qE does not name a type", id);
2040 /* If we're in a template class, it's possible that the user was
2041 referring to a type from a base class. For example:
2043 template <typename T> struct A { typedef T X; };
2044 template <typename T> struct B : public A<T> { X x; };
2046 The user should have said "typename A<T>::X". */
2047 if (processing_template_decl && current_class_type)
2051 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2055 tree base_type = BINFO_TYPE (b);
2056 if (CLASS_TYPE_P (base_type)
2057 && dependent_type_p (base_type))
2060 /* Go from a particular instantiation of the
2061 template (which will have an empty TYPE_FIELDs),
2062 to the main version. */
2063 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2064 for (field = TYPE_FIELDS (base_type);
2066 field = TREE_CHAIN (field))
2067 if (TREE_CODE (field) == TYPE_DECL
2068 && DECL_NAME (field) == id)
2070 inform ("(perhaps `typename %T::%E' was intended)",
2071 BINFO_TYPE (b), id);
2080 /* Here we diagnose qualified-ids where the scope is actually correct,
2081 but the identifier does not resolve to a valid type name. */
2084 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2085 error ("%qE in namespace %qE does not name a type",
2087 else if (TYPE_P (parser->scope))
2088 error ("q%E in class %qT does not name a type", id, parser->scope);
2094 /* Check for a common situation where a type-name should be present,
2095 but is not, and issue a sensible error message. Returns true if an
2096 invalid type-name was detected.
2098 The situation handled by this function are variable declarations of the
2099 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2100 Usually, `ID' should name a type, but if we got here it means that it
2101 does not. We try to emit the best possible error message depending on
2102 how exactly the id-expression looks like.
2106 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2110 cp_parser_parse_tentatively (parser);
2111 id = cp_parser_id_expression (parser,
2112 /*template_keyword_p=*/false,
2113 /*check_dependency_p=*/true,
2114 /*template_p=*/NULL,
2115 /*declarator_p=*/true);
2116 /* After the id-expression, there should be a plain identifier,
2117 otherwise this is not a simple variable declaration. Also, if
2118 the scope is dependent, we cannot do much. */
2119 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2120 || (parser->scope && TYPE_P (parser->scope)
2121 && dependent_type_p (parser->scope)))
2123 cp_parser_abort_tentative_parse (parser);
2126 if (!cp_parser_parse_definitely (parser)
2127 || TREE_CODE (id) != IDENTIFIER_NODE)
2130 /* Emit a diagnostic for the invalid type. */
2131 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2132 /* Skip to the end of the declaration; there's no point in
2133 trying to process it. */
2134 cp_parser_skip_to_end_of_block_or_statement (parser);
2138 /* Consume tokens up to, and including, the next non-nested closing `)'.
2139 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2140 are doing error recovery. Returns -1 if OR_COMMA is true and we
2141 found an unnested comma. */
2144 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2149 unsigned paren_depth = 0;
2150 unsigned brace_depth = 0;
2153 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2154 && !cp_parser_committed_to_tentative_parse (parser))
2161 /* If we've run out of tokens, then there is no closing `)'. */
2162 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2168 token = cp_lexer_peek_token (parser->lexer);
2170 /* This matches the processing in skip_to_end_of_statement. */
2171 if (token->type == CPP_SEMICOLON && !brace_depth)
2176 if (token->type == CPP_OPEN_BRACE)
2178 if (token->type == CPP_CLOSE_BRACE)
2186 if (recovering && or_comma && token->type == CPP_COMMA
2187 && !brace_depth && !paren_depth)
2195 /* If it is an `(', we have entered another level of nesting. */
2196 if (token->type == CPP_OPEN_PAREN)
2198 /* If it is a `)', then we might be done. */
2199 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2202 cp_lexer_consume_token (parser->lexer);
2210 /* Consume the token. */
2211 cp_lexer_consume_token (parser->lexer);
2217 /* Consume tokens until we reach the end of the current statement.
2218 Normally, that will be just before consuming a `;'. However, if a
2219 non-nested `}' comes first, then we stop before consuming that. */
2222 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2224 unsigned nesting_depth = 0;
2230 /* Peek at the next token. */
2231 token = cp_lexer_peek_token (parser->lexer);
2232 /* If we've run out of tokens, stop. */
2233 if (token->type == CPP_EOF)
2235 /* If the next token is a `;', we have reached the end of the
2237 if (token->type == CPP_SEMICOLON && !nesting_depth)
2239 /* If the next token is a non-nested `}', then we have reached
2240 the end of the current block. */
2241 if (token->type == CPP_CLOSE_BRACE)
2243 /* If this is a non-nested `}', stop before consuming it.
2244 That way, when confronted with something like:
2248 we stop before consuming the closing `}', even though we
2249 have not yet reached a `;'. */
2250 if (nesting_depth == 0)
2252 /* If it is the closing `}' for a block that we have
2253 scanned, stop -- but only after consuming the token.
2259 we will stop after the body of the erroneously declared
2260 function, but before consuming the following `typedef'
2262 if (--nesting_depth == 0)
2264 cp_lexer_consume_token (parser->lexer);
2268 /* If it the next token is a `{', then we are entering a new
2269 block. Consume the entire block. */
2270 else if (token->type == CPP_OPEN_BRACE)
2272 /* Consume the token. */
2273 cp_lexer_consume_token (parser->lexer);
2277 /* This function is called at the end of a statement or declaration.
2278 If the next token is a semicolon, it is consumed; otherwise, error
2279 recovery is attempted. */
2282 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2284 /* Look for the trailing `;'. */
2285 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2287 /* If there is additional (erroneous) input, skip to the end of
2289 cp_parser_skip_to_end_of_statement (parser);
2290 /* If the next token is now a `;', consume it. */
2291 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2292 cp_lexer_consume_token (parser->lexer);
2296 /* Skip tokens until we have consumed an entire block, or until we
2297 have consumed a non-nested `;'. */
2300 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2302 unsigned nesting_depth = 0;
2308 /* Peek at the next token. */
2309 token = cp_lexer_peek_token (parser->lexer);
2310 /* If we've run out of tokens, stop. */
2311 if (token->type == CPP_EOF)
2313 /* If the next token is a `;', we have reached the end of the
2315 if (token->type == CPP_SEMICOLON && !nesting_depth)
2317 /* Consume the `;'. */
2318 cp_lexer_consume_token (parser->lexer);
2321 /* Consume the token. */
2322 token = cp_lexer_consume_token (parser->lexer);
2323 /* If the next token is a non-nested `}', then we have reached
2324 the end of the current block. */
2325 if (token->type == CPP_CLOSE_BRACE
2326 && (nesting_depth == 0 || --nesting_depth == 0))
2328 /* If it the next token is a `{', then we are entering a new
2329 block. Consume the entire block. */
2330 if (token->type == CPP_OPEN_BRACE)
2335 /* Skip tokens until a non-nested closing curly brace is the next
2339 cp_parser_skip_to_closing_brace (cp_parser *parser)
2341 unsigned nesting_depth = 0;
2347 /* Peek at the next token. */
2348 token = cp_lexer_peek_token (parser->lexer);
2349 /* If we've run out of tokens, stop. */
2350 if (token->type == CPP_EOF)
2352 /* If the next token is a non-nested `}', then we have reached
2353 the end of the current block. */
2354 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2356 /* If it the next token is a `{', then we are entering a new
2357 block. Consume the entire block. */
2358 else if (token->type == CPP_OPEN_BRACE)
2360 /* Consume the token. */
2361 cp_lexer_consume_token (parser->lexer);
2365 /* This is a simple wrapper around make_typename_type. When the id is
2366 an unresolved identifier node, we can provide a superior diagnostic
2367 using cp_parser_diagnose_invalid_type_name. */
2370 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2373 if (TREE_CODE (id) == IDENTIFIER_NODE)
2375 result = make_typename_type (scope, id, /*complain=*/0);
2376 if (result == error_mark_node)
2377 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2380 return make_typename_type (scope, id, tf_error);
2384 /* Create a new C++ parser. */
2387 cp_parser_new (void)
2393 /* cp_lexer_new_main is called before calling ggc_alloc because
2394 cp_lexer_new_main might load a PCH file. */
2395 lexer = cp_lexer_new_main ();
2397 /* Initialize the binops_by_token so that we can get the tree
2398 directly from the token. */
2399 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2400 binops_by_token[binops[i].token_type] = binops[i];
2402 parser = GGC_CNEW (cp_parser);
2403 parser->lexer = lexer;
2404 parser->context = cp_parser_context_new (NULL);
2406 /* For now, we always accept GNU extensions. */
2407 parser->allow_gnu_extensions_p = 1;
2409 /* The `>' token is a greater-than operator, not the end of a
2411 parser->greater_than_is_operator_p = true;
2413 parser->default_arg_ok_p = true;
2415 /* We are not parsing a constant-expression. */
2416 parser->integral_constant_expression_p = false;
2417 parser->allow_non_integral_constant_expression_p = false;
2418 parser->non_integral_constant_expression_p = false;
2420 /* Local variable names are not forbidden. */
2421 parser->local_variables_forbidden_p = false;
2423 /* We are not processing an `extern "C"' declaration. */
2424 parser->in_unbraced_linkage_specification_p = false;
2426 /* We are not processing a declarator. */
2427 parser->in_declarator_p = false;
2429 /* We are not processing a template-argument-list. */
2430 parser->in_template_argument_list_p = false;
2432 /* We are not in an iteration statement. */
2433 parser->in_iteration_statement_p = false;
2435 /* We are not in a switch statement. */
2436 parser->in_switch_statement_p = false;
2438 /* We are not parsing a type-id inside an expression. */
2439 parser->in_type_id_in_expr_p = false;
2441 /* Declarations aren't implicitly extern "C". */
2442 parser->implicit_extern_c = false;
2444 /* String literals should be translated to the execution character set. */
2445 parser->translate_strings_p = true;
2447 /* The unparsed function queue is empty. */
2448 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2450 /* There are no classes being defined. */
2451 parser->num_classes_being_defined = 0;
2453 /* No template parameters apply. */
2454 parser->num_template_parameter_lists = 0;
2459 /* Create a cp_lexer structure which will emit the tokens in CACHE
2460 and push it onto the parser's lexer stack. This is used for delayed
2461 parsing of in-class method bodies and default arguments, and should
2462 not be confused with tentative parsing. */
2464 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2466 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2467 lexer->next = parser->lexer;
2468 parser->lexer = lexer;
2470 /* Move the current source position to that of the first token in the
2472 cp_lexer_set_source_position_from_token (lexer->next_token);
2475 /* Pop the top lexer off the parser stack. This is never used for the
2476 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2478 cp_parser_pop_lexer (cp_parser *parser)
2480 cp_lexer *lexer = parser->lexer;
2481 parser->lexer = lexer->next;
2482 cp_lexer_destroy (lexer);
2484 /* Put the current source position back where it was before this
2485 lexer was pushed. */
2486 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2489 /* Lexical conventions [gram.lex] */
2491 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2495 cp_parser_identifier (cp_parser* parser)
2499 /* Look for the identifier. */
2500 token = cp_parser_require (parser, CPP_NAME, "identifier");
2501 /* Return the value. */
2502 return token ? token->value : error_mark_node;
2505 /* Parse a sequence of adjacent string constants. Returns a
2506 TREE_STRING representing the combined, nul-terminated string
2507 constant. If TRANSLATE is true, translate the string to the
2508 execution character set. If WIDE_OK is true, a wide string is
2511 C++98 [lex.string] says that if a narrow string literal token is
2512 adjacent to a wide string literal token, the behavior is undefined.
2513 However, C99 6.4.5p4 says that this results in a wide string literal.
2514 We follow C99 here, for consistency with the C front end.
2516 This code is largely lifted from lex_string() in c-lex.c.
2518 FUTURE: ObjC++ will need to handle @-strings here. */
2520 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2525 struct obstack str_ob;
2526 cpp_string str, istr, *strs;
2529 tok = cp_lexer_peek_token (parser->lexer);
2530 if (!cp_parser_is_string_literal (tok))
2532 cp_parser_error (parser, "expected string-literal");
2533 return error_mark_node;
2536 /* Try to avoid the overhead of creating and destroying an obstack
2537 for the common case of just one string. */
2538 if (!cp_parser_is_string_literal
2539 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2541 cp_lexer_consume_token (parser->lexer);
2543 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2544 str.len = TREE_STRING_LENGTH (tok->value);
2546 if (tok->type == CPP_WSTRING)
2553 gcc_obstack_init (&str_ob);
2558 cp_lexer_consume_token (parser->lexer);
2560 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2561 str.len = TREE_STRING_LENGTH (tok->value);
2562 if (tok->type == CPP_WSTRING)
2565 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2567 tok = cp_lexer_peek_token (parser->lexer);
2569 while (cp_parser_is_string_literal (tok));
2571 strs = (cpp_string *) obstack_finish (&str_ob);
2574 if (wide && !wide_ok)
2576 cp_parser_error (parser, "a wide string is invalid in this context");
2580 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2581 (parse_in, strs, count, &istr, wide))
2583 value = build_string (istr.len, (char *)istr.text);
2584 free ((void *)istr.text);
2586 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2587 value = fix_string_type (value);
2590 /* cpp_interpret_string has issued an error. */
2591 value = error_mark_node;
2594 obstack_free (&str_ob, 0);
2600 /* Basic concepts [gram.basic] */
2602 /* Parse a translation-unit.
2605 declaration-seq [opt]
2607 Returns TRUE if all went well. */
2610 cp_parser_translation_unit (cp_parser* parser)
2612 /* The address of the first non-permanent object on the declarator
2614 static void *declarator_obstack_base;
2618 /* Create the declarator obstack, if necessary. */
2619 if (!cp_error_declarator)
2621 gcc_obstack_init (&declarator_obstack);
2622 /* Create the error declarator. */
2623 cp_error_declarator = make_declarator (cdk_error);
2624 /* Create the empty parameter list. */
2625 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2626 /* Remember where the base of the declarator obstack lies. */
2627 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2632 cp_parser_declaration_seq_opt (parser);
2634 /* If there are no tokens left then all went well. */
2635 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2637 /* Consume the EOF token. */
2638 cp_parser_require (parser, CPP_EOF, "end-of-file");
2640 /* Get rid of the token array; we don't need it any more. */
2641 cp_lexer_destroy (parser->lexer);
2642 parser->lexer = NULL;
2644 /* This file might have been a context that's implicitly extern
2645 "C". If so, pop the lang context. (Only relevant for PCH.) */
2646 if (parser->implicit_extern_c)
2648 pop_lang_context ();
2649 parser->implicit_extern_c = false;
2653 finish_translation_unit ();
2660 cp_parser_error (parser, "expected declaration");
2666 /* Make sure the declarator obstack was fully cleaned up. */
2667 gcc_assert (obstack_next_free (&declarator_obstack)
2668 == declarator_obstack_base);
2670 /* All went well. */
2674 /* Expressions [gram.expr] */
2676 /* Parse a primary-expression.
2687 ( compound-statement )
2688 __builtin_va_arg ( assignment-expression , type-id )
2693 Returns a representation of the expression.
2695 *IDK indicates what kind of id-expression (if any) was present.
2697 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2698 used as the operand of a pointer-to-member. In that case,
2699 *QUALIFYING_CLASS gives the class that is used as the qualifying
2700 class in the pointer-to-member. */
2703 cp_parser_primary_expression (cp_parser *parser,
2705 tree *qualifying_class)
2709 /* Assume the primary expression is not an id-expression. */
2710 *idk = CP_ID_KIND_NONE;
2711 /* And that it cannot be used as pointer-to-member. */
2712 *qualifying_class = NULL_TREE;
2714 /* Peek at the next token. */
2715 token = cp_lexer_peek_token (parser->lexer);
2716 switch (token->type)
2727 token = cp_lexer_consume_token (parser->lexer);
2728 return token->value;
2732 /* ??? Should wide strings be allowed when parser->translate_strings_p
2733 is false (i.e. in attributes)? If not, we can kill the third
2734 argument to cp_parser_string_literal. */
2735 return cp_parser_string_literal (parser,
2736 parser->translate_strings_p,
2739 case CPP_OPEN_PAREN:
2742 bool saved_greater_than_is_operator_p;
2744 /* Consume the `('. */
2745 cp_lexer_consume_token (parser->lexer);
2746 /* Within a parenthesized expression, a `>' token is always
2747 the greater-than operator. */
2748 saved_greater_than_is_operator_p
2749 = parser->greater_than_is_operator_p;
2750 parser->greater_than_is_operator_p = true;
2751 /* If we see `( { ' then we are looking at the beginning of
2752 a GNU statement-expression. */
2753 if (cp_parser_allow_gnu_extensions_p (parser)
2754 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2756 /* Statement-expressions are not allowed by the standard. */
2758 pedwarn ("ISO C++ forbids braced-groups within expressions");
2760 /* And they're not allowed outside of a function-body; you
2761 cannot, for example, write:
2763 int i = ({ int j = 3; j + 1; });
2765 at class or namespace scope. */
2766 if (!at_function_scope_p ())
2767 error ("statement-expressions are allowed only inside functions");
2768 /* Start the statement-expression. */
2769 expr = begin_stmt_expr ();
2770 /* Parse the compound-statement. */
2771 cp_parser_compound_statement (parser, expr, false);
2773 expr = finish_stmt_expr (expr, false);
2777 /* Parse the parenthesized expression. */
2778 expr = cp_parser_expression (parser);
2779 /* Let the front end know that this expression was
2780 enclosed in parentheses. This matters in case, for
2781 example, the expression is of the form `A::B', since
2782 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2784 finish_parenthesized_expr (expr);
2786 /* The `>' token might be the end of a template-id or
2787 template-parameter-list now. */
2788 parser->greater_than_is_operator_p
2789 = saved_greater_than_is_operator_p;
2790 /* Consume the `)'. */
2791 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2792 cp_parser_skip_to_end_of_statement (parser);
2798 switch (token->keyword)
2800 /* These two are the boolean literals. */
2802 cp_lexer_consume_token (parser->lexer);
2803 return boolean_true_node;
2805 cp_lexer_consume_token (parser->lexer);
2806 return boolean_false_node;
2808 /* The `__null' literal. */
2810 cp_lexer_consume_token (parser->lexer);
2813 /* Recognize the `this' keyword. */
2815 cp_lexer_consume_token (parser->lexer);
2816 if (parser->local_variables_forbidden_p)
2818 error ("%<this%> may not be used in this context");
2819 return error_mark_node;
2821 /* Pointers cannot appear in constant-expressions. */
2822 if (cp_parser_non_integral_constant_expression (parser,
2824 return error_mark_node;
2825 return finish_this_expr ();
2827 /* The `operator' keyword can be the beginning of an
2832 case RID_FUNCTION_NAME:
2833 case RID_PRETTY_FUNCTION_NAME:
2834 case RID_C99_FUNCTION_NAME:
2835 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2836 __func__ are the names of variables -- but they are
2837 treated specially. Therefore, they are handled here,
2838 rather than relying on the generic id-expression logic
2839 below. Grammatically, these names are id-expressions.
2841 Consume the token. */
2842 token = cp_lexer_consume_token (parser->lexer);
2843 /* Look up the name. */
2844 return finish_fname (token->value);
2851 /* The `__builtin_va_arg' construct is used to handle
2852 `va_arg'. Consume the `__builtin_va_arg' token. */
2853 cp_lexer_consume_token (parser->lexer);
2854 /* Look for the opening `('. */
2855 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2856 /* Now, parse the assignment-expression. */
2857 expression = cp_parser_assignment_expression (parser);
2858 /* Look for the `,'. */
2859 cp_parser_require (parser, CPP_COMMA, "`,'");
2860 /* Parse the type-id. */
2861 type = cp_parser_type_id (parser);
2862 /* Look for the closing `)'. */
2863 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2864 /* Using `va_arg' in a constant-expression is not
2866 if (cp_parser_non_integral_constant_expression (parser,
2868 return error_mark_node;
2869 return build_x_va_arg (expression, type);
2873 return cp_parser_builtin_offsetof (parser);
2876 cp_parser_error (parser, "expected primary-expression");
2877 return error_mark_node;
2880 /* An id-expression can start with either an identifier, a
2881 `::' as the beginning of a qualified-id, or the "operator"
2885 case CPP_TEMPLATE_ID:
2886 case CPP_NESTED_NAME_SPECIFIER:
2890 const char *error_msg;
2893 /* Parse the id-expression. */
2895 = cp_parser_id_expression (parser,
2896 /*template_keyword_p=*/false,
2897 /*check_dependency_p=*/true,
2898 /*template_p=*/NULL,
2899 /*declarator_p=*/false);
2900 if (id_expression == error_mark_node)
2901 return error_mark_node;
2902 /* If we have a template-id, then no further lookup is
2903 required. If the template-id was for a template-class, we
2904 will sometimes have a TYPE_DECL at this point. */
2905 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2906 || TREE_CODE (id_expression) == TYPE_DECL)
2907 decl = id_expression;
2908 /* Look up the name. */
2913 decl = cp_parser_lookup_name (parser, id_expression,
2915 /*is_template=*/false,
2916 /*is_namespace=*/false,
2917 /*check_dependency=*/true,
2919 /* If the lookup was ambiguous, an error will already have
2922 return error_mark_node;
2923 /* If name lookup gives us a SCOPE_REF, then the
2924 qualifying scope was dependent. Just propagate the
2926 if (TREE_CODE (decl) == SCOPE_REF)
2928 if (TYPE_P (TREE_OPERAND (decl, 0)))
2929 *qualifying_class = TREE_OPERAND (decl, 0);
2932 /* Check to see if DECL is a local variable in a context
2933 where that is forbidden. */
2934 if (parser->local_variables_forbidden_p
2935 && local_variable_p (decl))
2937 /* It might be that we only found DECL because we are
2938 trying to be generous with pre-ISO scoping rules.
2939 For example, consider:
2943 for (int i = 0; i < 10; ++i) {}
2944 extern void f(int j = i);
2947 Here, name look up will originally find the out
2948 of scope `i'. We need to issue a warning message,
2949 but then use the global `i'. */
2950 decl = check_for_out_of_scope_variable (decl);
2951 if (local_variable_p (decl))
2953 error ("local variable %qD may not appear in this context",
2955 return error_mark_node;
2960 decl = finish_id_expression (id_expression, decl, parser->scope,
2961 idk, qualifying_class,
2962 parser->integral_constant_expression_p,
2963 parser->allow_non_integral_constant_expression_p,
2964 &parser->non_integral_constant_expression_p,
2967 cp_parser_error (parser, error_msg);
2971 /* Anything else is an error. */
2973 cp_parser_error (parser, "expected primary-expression");
2974 return error_mark_node;
2978 /* Parse an id-expression.
2985 :: [opt] nested-name-specifier template [opt] unqualified-id
2987 :: operator-function-id
2990 Return a representation of the unqualified portion of the
2991 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2992 a `::' or nested-name-specifier.
2994 Often, if the id-expression was a qualified-id, the caller will
2995 want to make a SCOPE_REF to represent the qualified-id. This
2996 function does not do this in order to avoid wastefully creating
2997 SCOPE_REFs when they are not required.
2999 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3002 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3003 uninstantiated templates.
3005 If *TEMPLATE_P is non-NULL, it is set to true iff the
3006 `template' keyword is used to explicitly indicate that the entity
3007 named is a template.
3009 If DECLARATOR_P is true, the id-expression is appearing as part of
3010 a declarator, rather than as part of an expression. */
3013 cp_parser_id_expression (cp_parser *parser,
3014 bool template_keyword_p,
3015 bool check_dependency_p,
3019 bool global_scope_p;
3020 bool nested_name_specifier_p;
3022 /* Assume the `template' keyword was not used. */
3024 *template_p = false;
3026 /* Look for the optional `::' operator. */
3028 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3030 /* Look for the optional nested-name-specifier. */
3031 nested_name_specifier_p
3032 = (cp_parser_nested_name_specifier_opt (parser,
3033 /*typename_keyword_p=*/false,
3038 /* If there is a nested-name-specifier, then we are looking at
3039 the first qualified-id production. */
3040 if (nested_name_specifier_p)
3043 tree saved_object_scope;
3044 tree saved_qualifying_scope;
3045 tree unqualified_id;
3048 /* See if the next token is the `template' keyword. */
3050 template_p = &is_template;
3051 *template_p = cp_parser_optional_template_keyword (parser);
3052 /* Name lookup we do during the processing of the
3053 unqualified-id might obliterate SCOPE. */
3054 saved_scope = parser->scope;
3055 saved_object_scope = parser->object_scope;
3056 saved_qualifying_scope = parser->qualifying_scope;
3057 /* Process the final unqualified-id. */
3058 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3061 /* Restore the SAVED_SCOPE for our caller. */
3062 parser->scope = saved_scope;
3063 parser->object_scope = saved_object_scope;
3064 parser->qualifying_scope = saved_qualifying_scope;
3066 return unqualified_id;
3068 /* Otherwise, if we are in global scope, then we are looking at one
3069 of the other qualified-id productions. */
3070 else if (global_scope_p)
3075 /* Peek at the next token. */
3076 token = cp_lexer_peek_token (parser->lexer);
3078 /* If it's an identifier, and the next token is not a "<", then
3079 we can avoid the template-id case. This is an optimization
3080 for this common case. */
3081 if (token->type == CPP_NAME
3082 && !cp_parser_nth_token_starts_template_argument_list_p
3084 return cp_parser_identifier (parser);
3086 cp_parser_parse_tentatively (parser);
3087 /* Try a template-id. */
3088 id = cp_parser_template_id (parser,
3089 /*template_keyword_p=*/false,
3090 /*check_dependency_p=*/true,
3092 /* If that worked, we're done. */
3093 if (cp_parser_parse_definitely (parser))
3096 /* Peek at the next token. (Changes in the token buffer may
3097 have invalidated the pointer obtained above.) */
3098 token = cp_lexer_peek_token (parser->lexer);
3100 switch (token->type)
3103 return cp_parser_identifier (parser);
3106 if (token->keyword == RID_OPERATOR)
3107 return cp_parser_operator_function_id (parser);
3111 cp_parser_error (parser, "expected id-expression");
3112 return error_mark_node;
3116 return cp_parser_unqualified_id (parser, template_keyword_p,
3117 /*check_dependency_p=*/true,
3121 /* Parse an unqualified-id.
3125 operator-function-id
3126 conversion-function-id
3130 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3131 keyword, in a construct like `A::template ...'.
3133 Returns a representation of unqualified-id. For the `identifier'
3134 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3135 production a BIT_NOT_EXPR is returned; the operand of the
3136 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3137 other productions, see the documentation accompanying the
3138 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3139 names are looked up in uninstantiated templates. If DECLARATOR_P
3140 is true, the unqualified-id is appearing as part of a declarator,
3141 rather than as part of an expression. */
3144 cp_parser_unqualified_id (cp_parser* parser,
3145 bool template_keyword_p,
3146 bool check_dependency_p,
3151 /* Peek at the next token. */
3152 token = cp_lexer_peek_token (parser->lexer);
3154 switch (token->type)
3160 /* We don't know yet whether or not this will be a
3162 cp_parser_parse_tentatively (parser);
3163 /* Try a template-id. */
3164 id = cp_parser_template_id (parser, template_keyword_p,
3167 /* If it worked, we're done. */
3168 if (cp_parser_parse_definitely (parser))
3170 /* Otherwise, it's an ordinary identifier. */
3171 return cp_parser_identifier (parser);
3174 case CPP_TEMPLATE_ID:
3175 return cp_parser_template_id (parser, template_keyword_p,
3182 tree qualifying_scope;
3186 /* Consume the `~' token. */
3187 cp_lexer_consume_token (parser->lexer);
3188 /* Parse the class-name. The standard, as written, seems to
3191 template <typename T> struct S { ~S (); };
3192 template <typename T> S<T>::~S() {}
3194 is invalid, since `~' must be followed by a class-name, but
3195 `S<T>' is dependent, and so not known to be a class.
3196 That's not right; we need to look in uninstantiated
3197 templates. A further complication arises from:
3199 template <typename T> void f(T t) {
3203 Here, it is not possible to look up `T' in the scope of `T'
3204 itself. We must look in both the current scope, and the
3205 scope of the containing complete expression.
3207 Yet another issue is:
3216 The standard does not seem to say that the `S' in `~S'
3217 should refer to the type `S' and not the data member
3220 /* DR 244 says that we look up the name after the "~" in the
3221 same scope as we looked up the qualifying name. That idea
3222 isn't fully worked out; it's more complicated than that. */
3223 scope = parser->scope;
3224 object_scope = parser->object_scope;
3225 qualifying_scope = parser->qualifying_scope;
3227 /* If the name is of the form "X::~X" it's OK. */
3228 if (scope && TYPE_P (scope)
3229 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3230 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3232 && (cp_lexer_peek_token (parser->lexer)->value
3233 == TYPE_IDENTIFIER (scope)))
3235 cp_lexer_consume_token (parser->lexer);
3236 return build_nt (BIT_NOT_EXPR, scope);
3239 /* If there was an explicit qualification (S::~T), first look
3240 in the scope given by the qualification (i.e., S). */
3243 cp_parser_parse_tentatively (parser);
3244 type_decl = cp_parser_class_name (parser,
3245 /*typename_keyword_p=*/false,
3246 /*template_keyword_p=*/false,
3248 /*check_dependency=*/false,
3249 /*class_head_p=*/false,
3251 if (cp_parser_parse_definitely (parser))
3252 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3254 /* In "N::S::~S", look in "N" as well. */
3255 if (scope && qualifying_scope)
3257 cp_parser_parse_tentatively (parser);
3258 parser->scope = qualifying_scope;
3259 parser->object_scope = NULL_TREE;
3260 parser->qualifying_scope = NULL_TREE;
3262 = cp_parser_class_name (parser,
3263 /*typename_keyword_p=*/false,
3264 /*template_keyword_p=*/false,
3266 /*check_dependency=*/false,
3267 /*class_head_p=*/false,
3269 if (cp_parser_parse_definitely (parser))
3270 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3272 /* In "p->S::~T", look in the scope given by "*p" as well. */
3273 else if (object_scope)
3275 cp_parser_parse_tentatively (parser);
3276 parser->scope = object_scope;
3277 parser->object_scope = NULL_TREE;
3278 parser->qualifying_scope = NULL_TREE;
3280 = cp_parser_class_name (parser,
3281 /*typename_keyword_p=*/false,
3282 /*template_keyword_p=*/false,
3284 /*check_dependency=*/false,
3285 /*class_head_p=*/false,
3287 if (cp_parser_parse_definitely (parser))
3288 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3290 /* Look in the surrounding context. */
3291 parser->scope = NULL_TREE;
3292 parser->object_scope = NULL_TREE;
3293 parser->qualifying_scope = NULL_TREE;
3295 = cp_parser_class_name (parser,
3296 /*typename_keyword_p=*/false,
3297 /*template_keyword_p=*/false,
3299 /*check_dependency=*/false,
3300 /*class_head_p=*/false,
3302 /* If an error occurred, assume that the name of the
3303 destructor is the same as the name of the qualifying
3304 class. That allows us to keep parsing after running
3305 into ill-formed destructor names. */
3306 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3307 return build_nt (BIT_NOT_EXPR, scope);
3308 else if (type_decl == error_mark_node)
3309 return error_mark_node;
3313 A typedef-name that names a class shall not be used as the
3314 identifier in the declarator for a destructor declaration. */
3316 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3317 && !DECL_SELF_REFERENCE_P (type_decl))
3318 error ("typedef-name %qD used as destructor declarator",
3321 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3325 if (token->keyword == RID_OPERATOR)
3329 /* This could be a template-id, so we try that first. */
3330 cp_parser_parse_tentatively (parser);
3331 /* Try a template-id. */
3332 id = cp_parser_template_id (parser, template_keyword_p,
3333 /*check_dependency_p=*/true,
3335 /* If that worked, we're done. */
3336 if (cp_parser_parse_definitely (parser))
3338 /* We still don't know whether we're looking at an
3339 operator-function-id or a conversion-function-id. */
3340 cp_parser_parse_tentatively (parser);
3341 /* Try an operator-function-id. */
3342 id = cp_parser_operator_function_id (parser);
3343 /* If that didn't work, try a conversion-function-id. */
3344 if (!cp_parser_parse_definitely (parser))
3345 id = cp_parser_conversion_function_id (parser);
3352 cp_parser_error (parser, "expected unqualified-id");
3353 return error_mark_node;
3357 /* Parse an (optional) nested-name-specifier.
3359 nested-name-specifier:
3360 class-or-namespace-name :: nested-name-specifier [opt]
3361 class-or-namespace-name :: template nested-name-specifier [opt]
3363 PARSER->SCOPE should be set appropriately before this function is
3364 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3365 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3368 Sets PARSER->SCOPE to the class (TYPE) or namespace
3369 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3370 it unchanged if there is no nested-name-specifier. Returns the new
3371 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3373 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3374 part of a declaration and/or decl-specifier. */
3377 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3378 bool typename_keyword_p,
3379 bool check_dependency_p,
3381 bool is_declaration)
3383 bool success = false;
3384 tree access_check = NULL_TREE;
3388 /* If the next token corresponds to a nested name specifier, there
3389 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3390 false, it may have been true before, in which case something
3391 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3392 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3393 CHECK_DEPENDENCY_P is false, we have to fall through into the
3395 if (check_dependency_p
3396 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3398 cp_parser_pre_parsed_nested_name_specifier (parser);
3399 return parser->scope;
3402 /* Remember where the nested-name-specifier starts. */
3403 if (cp_parser_parsing_tentatively (parser)
3404 && !cp_parser_committed_to_tentative_parse (parser))
3406 token = cp_lexer_peek_token (parser->lexer);
3407 start = cp_lexer_token_difference (parser->lexer,
3408 parser->lexer->buffer,
3414 push_deferring_access_checks (dk_deferred);
3420 tree saved_qualifying_scope;
3421 bool template_keyword_p;
3423 /* Spot cases that cannot be the beginning of a
3424 nested-name-specifier. */
3425 token = cp_lexer_peek_token (parser->lexer);
3427 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3428 the already parsed nested-name-specifier. */
3429 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3431 /* Grab the nested-name-specifier and continue the loop. */
3432 cp_parser_pre_parsed_nested_name_specifier (parser);
3437 /* Spot cases that cannot be the beginning of a
3438 nested-name-specifier. On the second and subsequent times
3439 through the loop, we look for the `template' keyword. */
3440 if (success && token->keyword == RID_TEMPLATE)
3442 /* A template-id can start a nested-name-specifier. */
3443 else if (token->type == CPP_TEMPLATE_ID)
3447 /* If the next token is not an identifier, then it is
3448 definitely not a class-or-namespace-name. */
3449 if (token->type != CPP_NAME)
3451 /* If the following token is neither a `<' (to begin a
3452 template-id), nor a `::', then we are not looking at a
3453 nested-name-specifier. */
3454 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3455 if (token->type != CPP_SCOPE
3456 && !cp_parser_nth_token_starts_template_argument_list_p
3461 /* The nested-name-specifier is optional, so we parse
3463 cp_parser_parse_tentatively (parser);
3465 /* Look for the optional `template' keyword, if this isn't the
3466 first time through the loop. */
3468 template_keyword_p = cp_parser_optional_template_keyword (parser);
3470 template_keyword_p = false;
3472 /* Save the old scope since the name lookup we are about to do
3473 might destroy it. */
3474 old_scope = parser->scope;
3475 saved_qualifying_scope = parser->qualifying_scope;
3476 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3477 look up names in "X<T>::I" in order to determine that "Y" is
3478 a template. So, if we have a typename at this point, we make
3479 an effort to look through it. */
3481 && !typename_keyword_p
3483 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3484 parser->scope = resolve_typename_type (parser->scope,
3485 /*only_current_p=*/false);
3486 /* Parse the qualifying entity. */
3488 = cp_parser_class_or_namespace_name (parser,
3494 /* Look for the `::' token. */
3495 cp_parser_require (parser, CPP_SCOPE, "`::'");
3497 /* If we found what we wanted, we keep going; otherwise, we're
3499 if (!cp_parser_parse_definitely (parser))
3501 bool error_p = false;
3503 /* Restore the OLD_SCOPE since it was valid before the
3504 failed attempt at finding the last
3505 class-or-namespace-name. */
3506 parser->scope = old_scope;
3507 parser->qualifying_scope = saved_qualifying_scope;
3508 /* If the next token is an identifier, and the one after
3509 that is a `::', then any valid interpretation would have
3510 found a class-or-namespace-name. */
3511 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3512 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3514 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3517 token = cp_lexer_consume_token (parser->lexer);
3522 decl = cp_parser_lookup_name_simple (parser, token->value);
3523 if (TREE_CODE (decl) == TEMPLATE_DECL)
3524 error ("%qD used without template parameters", decl);
3526 cp_parser_name_lookup_error
3527 (parser, token->value, decl,
3528 "is not a class or namespace");
3529 parser->scope = NULL_TREE;
3531 /* Treat this as a successful nested-name-specifier
3536 If the name found is not a class-name (clause
3537 _class_) or namespace-name (_namespace.def_), the
3538 program is ill-formed. */
3541 cp_lexer_consume_token (parser->lexer);
3546 /* We've found one valid nested-name-specifier. */
3548 /* Make sure we look in the right scope the next time through
3550 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3551 ? TREE_TYPE (new_scope)
3553 /* If it is a class scope, try to complete it; we are about to
3554 be looking up names inside the class. */
3555 if (TYPE_P (parser->scope)
3556 /* Since checking types for dependency can be expensive,
3557 avoid doing it if the type is already complete. */
3558 && !COMPLETE_TYPE_P (parser->scope)
3559 /* Do not try to complete dependent types. */
3560 && !dependent_type_p (parser->scope))
3561 complete_type (parser->scope);
3564 /* Retrieve any deferred checks. Do not pop this access checks yet
3565 so the memory will not be reclaimed during token replacing below. */
3566 access_check = get_deferred_access_checks ();
3568 /* If parsing tentatively, replace the sequence of tokens that makes
3569 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3570 token. That way, should we re-parse the token stream, we will
3571 not have to repeat the effort required to do the parse, nor will
3572 we issue duplicate error messages. */
3573 if (success && start >= 0)
3575 /* Find the token that corresponds to the start of the
3577 token = cp_lexer_advance_token (parser->lexer,
3578 parser->lexer->buffer,
3581 /* Reset the contents of the START token. */
3582 token->type = CPP_NESTED_NAME_SPECIFIER;
3583 token->value = build_tree_list (access_check, parser->scope);
3584 TREE_TYPE (token->value) = parser->qualifying_scope;
3585 token->keyword = RID_MAX;
3586 /* Purge all subsequent tokens. */
3587 cp_lexer_purge_tokens_after (parser->lexer, token);
3590 pop_deferring_access_checks ();
3591 return success ? parser->scope : NULL_TREE;
3594 /* Parse a nested-name-specifier. See
3595 cp_parser_nested_name_specifier_opt for details. This function
3596 behaves identically, except that it will an issue an error if no
3597 nested-name-specifier is present, and it will return
3598 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3602 cp_parser_nested_name_specifier (cp_parser *parser,
3603 bool typename_keyword_p,
3604 bool check_dependency_p,
3606 bool is_declaration)
3610 /* Look for the nested-name-specifier. */
3611 scope = cp_parser_nested_name_specifier_opt (parser,
3616 /* If it was not present, issue an error message. */
3619 cp_parser_error (parser, "expected nested-name-specifier");
3620 parser->scope = NULL_TREE;
3621 return error_mark_node;
3627 /* Parse a class-or-namespace-name.
3629 class-or-namespace-name:
3633 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3634 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3635 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3636 TYPE_P is TRUE iff the next name should be taken as a class-name,
3637 even the same name is declared to be another entity in the same
3640 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3641 specified by the class-or-namespace-name. If neither is found the
3642 ERROR_MARK_NODE is returned. */
3645 cp_parser_class_or_namespace_name (cp_parser *parser,
3646 bool typename_keyword_p,
3647 bool template_keyword_p,
3648 bool check_dependency_p,
3650 bool is_declaration)
3653 tree saved_qualifying_scope;
3654 tree saved_object_scope;
3658 /* Before we try to parse the class-name, we must save away the
3659 current PARSER->SCOPE since cp_parser_class_name will destroy
3661 saved_scope = parser->scope;
3662 saved_qualifying_scope = parser->qualifying_scope;
3663 saved_object_scope = parser->object_scope;
3664 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3665 there is no need to look for a namespace-name. */
3666 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3668 cp_parser_parse_tentatively (parser);
3669 scope = cp_parser_class_name (parser,
3674 /*class_head_p=*/false,
3676 /* If that didn't work, try for a namespace-name. */
3677 if (!only_class_p && !cp_parser_parse_definitely (parser))
3679 /* Restore the saved scope. */
3680 parser->scope = saved_scope;
3681 parser->qualifying_scope = saved_qualifying_scope;
3682 parser->object_scope = saved_object_scope;
3683 /* If we are not looking at an identifier followed by the scope
3684 resolution operator, then this is not part of a
3685 nested-name-specifier. (Note that this function is only used
3686 to parse the components of a nested-name-specifier.) */
3687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3688 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3689 return error_mark_node;
3690 scope = cp_parser_namespace_name (parser);
3696 /* Parse a postfix-expression.
3700 postfix-expression [ expression ]
3701 postfix-expression ( expression-list [opt] )
3702 simple-type-specifier ( expression-list [opt] )
3703 typename :: [opt] nested-name-specifier identifier
3704 ( expression-list [opt] )
3705 typename :: [opt] nested-name-specifier template [opt] template-id
3706 ( expression-list [opt] )
3707 postfix-expression . template [opt] id-expression
3708 postfix-expression -> template [opt] id-expression
3709 postfix-expression . pseudo-destructor-name
3710 postfix-expression -> pseudo-destructor-name
3711 postfix-expression ++
3712 postfix-expression --
3713 dynamic_cast < type-id > ( expression )
3714 static_cast < type-id > ( expression )
3715 reinterpret_cast < type-id > ( expression )
3716 const_cast < type-id > ( expression )
3717 typeid ( expression )
3723 ( type-id ) { initializer-list , [opt] }
3725 This extension is a GNU version of the C99 compound-literal
3726 construct. (The C99 grammar uses `type-name' instead of `type-id',
3727 but they are essentially the same concept.)
3729 If ADDRESS_P is true, the postfix expression is the operand of the
3732 Returns a representation of the expression. */
3735 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3739 cp_id_kind idk = CP_ID_KIND_NONE;
3740 tree postfix_expression = NULL_TREE;
3741 /* Non-NULL only if the current postfix-expression can be used to
3742 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3743 class used to qualify the member. */
3744 tree qualifying_class = NULL_TREE;
3746 /* Peek at the next token. */
3747 token = cp_lexer_peek_token (parser->lexer);
3748 /* Some of the productions are determined by keywords. */
3749 keyword = token->keyword;
3759 const char *saved_message;
3761 /* All of these can be handled in the same way from the point
3762 of view of parsing. Begin by consuming the token
3763 identifying the cast. */
3764 cp_lexer_consume_token (parser->lexer);
3766 /* New types cannot be defined in the cast. */
3767 saved_message = parser->type_definition_forbidden_message;
3768 parser->type_definition_forbidden_message
3769 = "types may not be defined in casts";
3771 /* Look for the opening `<'. */
3772 cp_parser_require (parser, CPP_LESS, "`<'");
3773 /* Parse the type to which we are casting. */
3774 type = cp_parser_type_id (parser);
3775 /* Look for the closing `>'. */
3776 cp_parser_require (parser, CPP_GREATER, "`>'");
3777 /* Restore the old message. */
3778 parser->type_definition_forbidden_message = saved_message;
3780 /* And the expression which is being cast. */
3781 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3782 expression = cp_parser_expression (parser);
3783 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3785 /* Only type conversions to integral or enumeration types
3786 can be used in constant-expressions. */
3787 if (parser->integral_constant_expression_p
3788 && !dependent_type_p (type)
3789 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3790 && (cp_parser_non_integral_constant_expression
3792 "a cast to a type other than an integral or "
3793 "enumeration type")))
3794 return error_mark_node;
3800 = build_dynamic_cast (type, expression);
3804 = build_static_cast (type, expression);
3808 = build_reinterpret_cast (type, expression);
3812 = build_const_cast (type, expression);
3823 const char *saved_message;
3824 bool saved_in_type_id_in_expr_p;
3826 /* Consume the `typeid' token. */
3827 cp_lexer_consume_token (parser->lexer);
3828 /* Look for the `(' token. */
3829 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3830 /* Types cannot be defined in a `typeid' expression. */
3831 saved_message = parser->type_definition_forbidden_message;
3832 parser->type_definition_forbidden_message
3833 = "types may not be defined in a `typeid\' expression";
3834 /* We can't be sure yet whether we're looking at a type-id or an
3836 cp_parser_parse_tentatively (parser);
3837 /* Try a type-id first. */
3838 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3839 parser->in_type_id_in_expr_p = true;
3840 type = cp_parser_type_id (parser);
3841 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3842 /* Look for the `)' token. Otherwise, we can't be sure that
3843 we're not looking at an expression: consider `typeid (int
3844 (3))', for example. */
3845 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3846 /* If all went well, simply lookup the type-id. */
3847 if (cp_parser_parse_definitely (parser))
3848 postfix_expression = get_typeid (type);
3849 /* Otherwise, fall back to the expression variant. */
3854 /* Look for an expression. */
3855 expression = cp_parser_expression (parser);
3856 /* Compute its typeid. */
3857 postfix_expression = build_typeid (expression);
3858 /* Look for the `)' token. */
3859 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3861 /* `typeid' may not appear in an integral constant expression. */
3862 if (cp_parser_non_integral_constant_expression(parser,
3863 "`typeid' operator"))
3864 return error_mark_node;
3865 /* Restore the saved message. */
3866 parser->type_definition_forbidden_message = saved_message;
3872 bool template_p = false;
3876 /* Consume the `typename' token. */
3877 cp_lexer_consume_token (parser->lexer);
3878 /* Look for the optional `::' operator. */
3879 cp_parser_global_scope_opt (parser,
3880 /*current_scope_valid_p=*/false);
3881 /* Look for the nested-name-specifier. */
3882 cp_parser_nested_name_specifier (parser,
3883 /*typename_keyword_p=*/true,
3884 /*check_dependency_p=*/true,
3886 /*is_declaration=*/true);
3887 /* Look for the optional `template' keyword. */
3888 template_p = cp_parser_optional_template_keyword (parser);
3889 /* We don't know whether we're looking at a template-id or an
3891 cp_parser_parse_tentatively (parser);
3892 /* Try a template-id. */
3893 id = cp_parser_template_id (parser, template_p,
3894 /*check_dependency_p=*/true,
3895 /*is_declaration=*/true);
3896 /* If that didn't work, try an identifier. */
3897 if (!cp_parser_parse_definitely (parser))
3898 id = cp_parser_identifier (parser);
3899 /* If we look up a template-id in a non-dependent qualifying
3900 scope, there's no need to create a dependent type. */
3901 if (TREE_CODE (id) == TYPE_DECL
3902 && !dependent_type_p (parser->scope))
3903 type = TREE_TYPE (id);
3904 /* Create a TYPENAME_TYPE to represent the type to which the
3905 functional cast is being performed. */
3907 type = make_typename_type (parser->scope, id,
3910 postfix_expression = cp_parser_functional_cast (parser, type);
3918 /* If the next thing is a simple-type-specifier, we may be
3919 looking at a functional cast. We could also be looking at
3920 an id-expression. So, we try the functional cast, and if
3921 that doesn't work we fall back to the primary-expression. */
3922 cp_parser_parse_tentatively (parser);
3923 /* Look for the simple-type-specifier. */
3924 type = cp_parser_simple_type_specifier (parser,
3925 /*decl_specs=*/NULL,
3926 CP_PARSER_FLAGS_NONE);
3927 /* Parse the cast itself. */
3928 if (!cp_parser_error_occurred (parser))
3930 = cp_parser_functional_cast (parser, type);
3931 /* If that worked, we're done. */
3932 if (cp_parser_parse_definitely (parser))
3935 /* If the functional-cast didn't work out, try a
3936 compound-literal. */
3937 if (cp_parser_allow_gnu_extensions_p (parser)
3938 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3940 tree initializer_list = NULL_TREE;
3941 bool saved_in_type_id_in_expr_p;
3943 cp_parser_parse_tentatively (parser);
3944 /* Consume the `('. */
3945 cp_lexer_consume_token (parser->lexer);
3946 /* Parse the type. */
3947 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3948 parser->in_type_id_in_expr_p = true;
3949 type = cp_parser_type_id (parser);
3950 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3951 /* Look for the `)'. */
3952 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3953 /* Look for the `{'. */
3954 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3955 /* If things aren't going well, there's no need to
3957 if (!cp_parser_error_occurred (parser))
3959 bool non_constant_p;
3960 /* Parse the initializer-list. */
3962 = cp_parser_initializer_list (parser, &non_constant_p);
3963 /* Allow a trailing `,'. */
3964 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3965 cp_lexer_consume_token (parser->lexer);
3966 /* Look for the final `}'. */
3967 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3969 /* If that worked, we're definitely looking at a
3970 compound-literal expression. */
3971 if (cp_parser_parse_definitely (parser))
3973 /* Warn the user that a compound literal is not
3974 allowed in standard C++. */
3976 pedwarn ("ISO C++ forbids compound-literals");
3977 /* Form the representation of the compound-literal. */
3979 = finish_compound_literal (type, initializer_list);
3984 /* It must be a primary-expression. */
3985 postfix_expression = cp_parser_primary_expression (parser,
3992 /* If we were avoiding committing to the processing of a
3993 qualified-id until we knew whether or not we had a
3994 pointer-to-member, we now know. */
3995 if (qualifying_class)
3999 /* Peek at the next token. */
4000 token = cp_lexer_peek_token (parser->lexer);
4001 done = (token->type != CPP_OPEN_SQUARE
4002 && token->type != CPP_OPEN_PAREN
4003 && token->type != CPP_DOT
4004 && token->type != CPP_DEREF
4005 && token->type != CPP_PLUS_PLUS
4006 && token->type != CPP_MINUS_MINUS);
4008 postfix_expression = finish_qualified_id_expr (qualifying_class,
4013 return postfix_expression;
4016 /* Keep looping until the postfix-expression is complete. */
4019 if (idk == CP_ID_KIND_UNQUALIFIED
4020 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4021 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4022 /* It is not a Koenig lookup function call. */
4024 = unqualified_name_lookup_error (postfix_expression);
4026 /* Peek at the next token. */
4027 token = cp_lexer_peek_token (parser->lexer);
4029 switch (token->type)
4031 case CPP_OPEN_SQUARE:
4033 = cp_parser_postfix_open_square_expression (parser,
4036 idk = CP_ID_KIND_NONE;
4039 case CPP_OPEN_PAREN:
4040 /* postfix-expression ( expression-list [opt] ) */
4043 tree args = (cp_parser_parenthesized_expression_list
4044 (parser, false, /*non_constant_p=*/NULL));
4046 if (args == error_mark_node)
4048 postfix_expression = error_mark_node;
4052 /* Function calls are not permitted in
4053 constant-expressions. */
4054 if (cp_parser_non_integral_constant_expression (parser,
4057 postfix_expression = error_mark_node;
4062 if (idk == CP_ID_KIND_UNQUALIFIED)
4064 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4070 = perform_koenig_lookup (postfix_expression, args);
4074 = unqualified_fn_lookup_error (postfix_expression);
4076 /* We do not perform argument-dependent lookup if
4077 normal lookup finds a non-function, in accordance
4078 with the expected resolution of DR 218. */
4079 else if (args && is_overloaded_fn (postfix_expression))
4081 tree fn = get_first_fn (postfix_expression);
4083 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4084 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4086 /* Only do argument dependent lookup if regular
4087 lookup does not find a set of member functions.
4088 [basic.lookup.koenig]/2a */
4089 if (!DECL_FUNCTION_MEMBER_P (fn))
4093 = perform_koenig_lookup (postfix_expression, args);
4098 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4100 tree instance = TREE_OPERAND (postfix_expression, 0);
4101 tree fn = TREE_OPERAND (postfix_expression, 1);
4103 if (processing_template_decl
4104 && (type_dependent_expression_p (instance)
4105 || (!BASELINK_P (fn)
4106 && TREE_CODE (fn) != FIELD_DECL)
4107 || type_dependent_expression_p (fn)
4108 || any_type_dependent_arguments_p (args)))
4111 = build_min_nt (CALL_EXPR, postfix_expression,
4116 if (BASELINK_P (fn))
4118 = (build_new_method_call
4119 (instance, fn, args, NULL_TREE,
4120 (idk == CP_ID_KIND_QUALIFIED
4121 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4124 = finish_call_expr (postfix_expression, args,
4125 /*disallow_virtual=*/false,
4126 /*koenig_p=*/false);