2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
43 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
44 and c-lex.c) and the C++ parser. */
48 typedef struct cp_token GTY (())
50 /* The kind of token. */
51 ENUM_BITFIELD (cpp_ttype) type : 8;
52 /* If this token is a keyword, this value indicates which keyword.
53 Otherwise, this value is RID_MAX. */
54 ENUM_BITFIELD (rid) keyword : 8;
57 /* True if this token is from a system header. */
58 BOOL_BITFIELD in_system_header : 1;
59 /* True if this token is from a context where it is implicitly extern "C" */
60 BOOL_BITFIELD implicit_extern_c : 1;
61 /* The value associated with this token, if any. */
63 /* The location at which this token was found. */
67 /* We use a stack of token pointer for saving token sets. */
68 typedef struct cp_token *cp_token_position;
69 DEF_VEC_MALLOC_P (cp_token_position);
71 static const cp_token eof_token =
73 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
74 #if USE_MAPPED_LOCATION
81 /* The cp_lexer structure represents the C++ lexer. It is responsible
82 for managing the token stream from the preprocessor and supplying
83 it to the parser. Tokens are never added to the cp_lexer after
86 typedef struct cp_lexer GTY (())
88 /* The memory allocated for the buffer. NULL if this lexer does not
89 own the token buffer. */
90 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
91 /* If the lexer owns the buffer, this is the number of tokens in the
95 /* A pointer just past the last available token. The tokens
96 in this lexer are [buffer, last_token). */
97 cp_token_position GTY ((skip)) last_token;
99 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
100 no more available tokens. */
101 cp_token_position GTY ((skip)) next_token;
103 /* A stack indicating positions at which cp_lexer_save_tokens was
104 called. The top entry is the most recent position at which we
105 began saving tokens. If the stack is non-empty, we are saving
107 VEC (cp_token_position) *GTY ((skip)) saved_tokens;
109 /* True if we should output debugging information. */
112 /* The next lexer in a linked list of lexers. */
113 struct cp_lexer *next;
116 /* cp_token_cache is a range of tokens. There is no need to represent
117 allocate heap memory for it, since tokens are never removed from the
118 lexer's array. There is also no need for the GC to walk through
119 a cp_token_cache, since everything in here is referenced through
122 typedef struct cp_token_cache GTY(())
124 /* The beginning of the token range. */
125 cp_token * GTY((skip)) first;
127 /* Points immediately after the last token in the range. */
128 cp_token * GTY ((skip)) last;
133 static cp_lexer *cp_lexer_new_main
135 static cp_lexer *cp_lexer_new_from_tokens
136 (cp_token_cache *tokens);
137 static void cp_lexer_destroy
139 static int cp_lexer_saving_tokens
141 static cp_token_position cp_lexer_token_position
143 static cp_token *cp_lexer_token_at
144 (cp_lexer *, cp_token_position);
145 static void cp_lexer_get_preprocessor_token
146 (cp_lexer *, cp_token *);
147 static inline cp_token *cp_lexer_peek_token
149 static cp_token *cp_lexer_peek_nth_token
150 (cp_lexer *, size_t);
151 static inline bool cp_lexer_next_token_is
152 (cp_lexer *, enum cpp_ttype);
153 static bool cp_lexer_next_token_is_not
154 (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_keyword
156 (cp_lexer *, enum rid);
157 static cp_token *cp_lexer_consume_token
159 static void cp_lexer_purge_token
161 static void cp_lexer_purge_tokens_after
162 (cp_lexer *, cp_token_position);
163 static void cp_lexer_handle_pragma
165 static void cp_lexer_save_tokens
167 static void cp_lexer_commit_tokens
169 static void cp_lexer_rollback_tokens
171 #ifdef ENABLE_CHECKING
172 static void cp_lexer_print_token
173 (FILE *, cp_token *);
174 static inline bool cp_lexer_debugging_p
176 static void cp_lexer_start_debugging
177 (cp_lexer *) ATTRIBUTE_UNUSED;
178 static void cp_lexer_stop_debugging
179 (cp_lexer *) ATTRIBUTE_UNUSED;
181 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
182 about passing NULL to functions that require non-NULL arguments
183 (fputs, fprintf). It will never be used, so all we need is a value
184 of the right type that's guaranteed not to be NULL. */
185 #define cp_lexer_debug_stream stdout
186 #define cp_lexer_print_token(str, tok) (void) 0
187 #define cp_lexer_debugging_p(lexer) 0
188 #endif /* ENABLE_CHECKING */
190 static cp_token_cache *cp_token_cache_new
191 (cp_token *, cp_token *);
193 /* Manifest constants. */
194 #define CP_LEXER_BUFFER_SIZE 10000
195 #define CP_SAVED_TOKEN_STACK 5
197 /* A token type for keywords, as opposed to ordinary identifiers. */
198 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
200 /* A token type for template-ids. If a template-id is processed while
201 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
202 the value of the CPP_TEMPLATE_ID is whatever was returned by
203 cp_parser_template_id. */
204 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
206 /* A token type for nested-name-specifiers. If a
207 nested-name-specifier is processed while parsing tentatively, it is
208 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
209 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
210 cp_parser_nested_name_specifier_opt. */
211 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
213 /* A token type for tokens that are not tokens at all; these are used
214 to represent slots in the array where there used to be a token
215 that has now been deleted. */
216 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
218 /* The number of token types, including C++-specific ones. */
219 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
223 #ifdef ENABLE_CHECKING
224 /* The stream to which debugging output should be written. */
225 static FILE *cp_lexer_debug_stream;
226 #endif /* ENABLE_CHECKING */
228 /* Create a new main C++ lexer, the lexer that gets tokens from the
232 cp_lexer_new_main (void)
234 cp_token first_token;
241 /* It's possible that lexing the first token will load a PCH file,
242 which is a GC collection point. So we have to grab the first
243 token before allocating any memory. Pragmas must not be deferred
244 as -fpch-preprocess can generate a pragma to load the PCH file in
245 the preprocessed output used by -save-temps. */
246 cp_lexer_get_preprocessor_token (NULL, &first_token);
248 /* Tell cpplib we want CPP_PRAGMA tokens. */
249 cpp_get_options (parse_in)->defer_pragmas = true;
251 /* Tell c_lex not to merge string constants. */
252 c_lex_return_raw_strings = true;
254 c_common_no_more_pch ();
256 /* Allocate the memory. */
257 lexer = GGC_CNEW (cp_lexer);
259 #ifdef ENABLE_CHECKING
260 /* Initially we are not debugging. */
261 lexer->debugging_p = false;
262 #endif /* ENABLE_CHECKING */
263 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
265 /* Create the buffer. */
266 alloc = CP_LEXER_BUFFER_SIZE;
267 buffer = ggc_alloc (alloc * sizeof (cp_token));
269 /* Put the first token in the buffer. */
274 /* Get the remaining tokens from the preprocessor. */
275 while (pos->type != CPP_EOF)
282 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
283 pos = buffer + space;
285 cp_lexer_get_preprocessor_token (lexer, pos);
287 lexer->buffer = buffer;
288 lexer->buffer_length = alloc - space;
289 lexer->last_token = pos;
290 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
292 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
293 direct calls to c_lex. Those callers all expect c_lex to do
294 string constant concatenation. */
295 c_lex_return_raw_strings = false;
297 gcc_assert (lexer->next_token->type != CPP_PURGED);
301 /* Create a new lexer whose token stream is primed with the tokens in
302 CACHE. When these tokens are exhausted, no new tokens will be read. */
305 cp_lexer_new_from_tokens (cp_token_cache *cache)
307 cp_token *first = cache->first;
308 cp_token *last = cache->last;
309 cp_lexer *lexer = GGC_CNEW (cp_lexer);
311 /* We do not own the buffer. */
312 lexer->buffer = NULL;
313 lexer->buffer_length = 0;
314 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
315 lexer->last_token = last;
317 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
319 #ifdef ENABLE_CHECKING
320 /* Initially we are not debugging. */
321 lexer->debugging_p = false;
324 gcc_assert (lexer->next_token->type != CPP_PURGED);
328 /* Frees all resources associated with LEXER. */
331 cp_lexer_destroy (cp_lexer *lexer)
334 ggc_free (lexer->buffer);
335 VEC_free (cp_token_position, lexer->saved_tokens);
339 /* Returns nonzero if debugging information should be output. */
341 #ifdef ENABLE_CHECKING
344 cp_lexer_debugging_p (cp_lexer *lexer)
346 return lexer->debugging_p;
349 #endif /* ENABLE_CHECKING */
351 static inline cp_token_position
352 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
354 gcc_assert (!previous_p || lexer->next_token != &eof_token);
356 return lexer->next_token - previous_p;
359 static inline cp_token *
360 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
365 /* nonzero if we are presently saving tokens. */
368 cp_lexer_saving_tokens (const cp_lexer* lexer)
370 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
373 /* Store the next token from the preprocessor in *TOKEN. Return true
377 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
380 static int is_extern_c = 0;
382 /* Get a new token from the preprocessor. */
384 = c_lex_with_flags (&token->value, &token->location, &token->flags);
385 token->in_system_header = in_system_header;
387 /* On some systems, some header files are surrounded by an
388 implicit extern "C" block. Set a flag in the token if it
389 comes from such a header. */
390 is_extern_c += pending_lang_change;
391 pending_lang_change = 0;
392 token->implicit_extern_c = is_extern_c > 0;
394 /* Check to see if this token is a keyword. */
395 if (token->type == CPP_NAME
396 && C_IS_RESERVED_WORD (token->value))
398 /* Mark this token as a keyword. */
399 token->type = CPP_KEYWORD;
400 /* Record which keyword. */
401 token->keyword = C_RID_CODE (token->value);
402 /* Update the value. Some keywords are mapped to particular
403 entities, rather than simply having the value of the
404 corresponding IDENTIFIER_NODE. For example, `__const' is
405 mapped to `const'. */
406 token->value = ridpointers[token->keyword];
409 token->keyword = RID_MAX;
412 /* Update the globals input_location and in_system_header from TOKEN. */
414 cp_lexer_set_source_position_from_token (cp_token *token)
416 if (token->type != CPP_EOF)
418 input_location = token->location;
419 in_system_header = token->in_system_header;
423 /* Return a pointer to the next token in the token stream, but do not
426 static inline cp_token *
427 cp_lexer_peek_token (cp_lexer *lexer)
429 if (cp_lexer_debugging_p (lexer))
431 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
432 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
433 putc ('\n', cp_lexer_debug_stream);
435 return lexer->next_token;
438 /* Return true if the next token has the indicated TYPE. */
441 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
443 return cp_lexer_peek_token (lexer)->type == type;
446 /* Return true if the next token does not have the indicated TYPE. */
449 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
451 return !cp_lexer_next_token_is (lexer, type);
454 /* Return true if the next token is the indicated KEYWORD. */
457 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
461 /* Peek at the next token. */
462 token = cp_lexer_peek_token (lexer);
463 /* Check to see if it is the indicated keyword. */
464 return token->keyword == keyword;
467 /* Return a pointer to the Nth token in the token stream. If N is 1,
468 then this is precisely equivalent to cp_lexer_peek_token (except
469 that it is not inline). One would like to disallow that case, but
470 there is one case (cp_parser_nth_token_starts_template_id) where
471 the caller passes a variable for N and it might be 1. */
474 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
478 /* N is 1-based, not zero-based. */
479 gcc_assert (n > 0 && lexer->next_token != &eof_token);
481 if (cp_lexer_debugging_p (lexer))
482 fprintf (cp_lexer_debug_stream,
483 "cp_lexer: peeking ahead %ld at token: ", (long)n);
486 token = lexer->next_token;
490 if (token == lexer->last_token)
492 token = (cp_token *)&eof_token;
496 if (token->type != CPP_PURGED)
500 if (cp_lexer_debugging_p (lexer))
502 cp_lexer_print_token (cp_lexer_debug_stream, token);
503 putc ('\n', cp_lexer_debug_stream);
509 /* Return the next token, and advance the lexer's next_token pointer
510 to point to the next non-purged token. */
513 cp_lexer_consume_token (cp_lexer* lexer)
515 cp_token *token = lexer->next_token;
517 gcc_assert (token != &eof_token);
522 if (lexer->next_token == lexer->last_token)
524 lexer->next_token = (cp_token *)&eof_token;
529 while (lexer->next_token->type == CPP_PURGED);
531 cp_lexer_set_source_position_from_token (token);
533 /* Provide debugging output. */
534 if (cp_lexer_debugging_p (lexer))
536 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
537 cp_lexer_print_token (cp_lexer_debug_stream, token);
538 putc ('\n', cp_lexer_debug_stream);
544 /* Permanently remove the next token from the token stream, and
545 advance the next_token pointer to refer to the next non-purged
549 cp_lexer_purge_token (cp_lexer *lexer)
551 cp_token *tok = lexer->next_token;
553 gcc_assert (tok != &eof_token);
554 tok->type = CPP_PURGED;
555 tok->location = UNKNOWN_LOCATION;
556 tok->value = NULL_TREE;
557 tok->keyword = RID_MAX;
562 if (tok == lexer->last_token)
564 tok = (cp_token *)&eof_token;
568 while (tok->type == CPP_PURGED);
569 lexer->next_token = tok;
572 /* Permanently remove all tokens after TOK, up to, but not
573 including, the token that will be returned next by
574 cp_lexer_peek_token. */
577 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
579 cp_token *peek = lexer->next_token;
581 if (peek == &eof_token)
582 peek = lexer->last_token;
584 gcc_assert (tok < peek);
586 for ( tok += 1; tok != peek; tok += 1)
588 tok->type = CPP_PURGED;
589 tok->location = UNKNOWN_LOCATION;
590 tok->value = NULL_TREE;
591 tok->keyword = RID_MAX;
595 /* Consume and handle a pragma token. */
597 cp_lexer_handle_pragma (cp_lexer *lexer)
600 cp_token *token = cp_lexer_consume_token (lexer);
601 gcc_assert (token->type == CPP_PRAGMA);
602 gcc_assert (token->value);
604 s.len = TREE_STRING_LENGTH (token->value);
605 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
607 cpp_handle_deferred_pragma (parse_in, &s);
609 /* Clearing token->value here means that we will get an ICE if we
610 try to process this #pragma again (which should be impossible). */
614 /* Begin saving tokens. All tokens consumed after this point will be
618 cp_lexer_save_tokens (cp_lexer* lexer)
620 /* Provide debugging output. */
621 if (cp_lexer_debugging_p (lexer))
622 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
624 VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
627 /* Commit to the portion of the token stream most recently saved. */
630 cp_lexer_commit_tokens (cp_lexer* lexer)
632 /* Provide debugging output. */
633 if (cp_lexer_debugging_p (lexer))
634 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
636 VEC_pop (cp_token_position, lexer->saved_tokens);
639 /* Return all tokens saved since the last call to cp_lexer_save_tokens
640 to the token stream. Stop saving tokens. */
643 cp_lexer_rollback_tokens (cp_lexer* lexer)
645 /* Provide debugging output. */
646 if (cp_lexer_debugging_p (lexer))
647 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
649 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
652 /* Print a representation of the TOKEN on the STREAM. */
654 #ifdef ENABLE_CHECKING
657 cp_lexer_print_token (FILE * stream, cp_token *token)
659 /* We don't use cpp_type2name here because the parser defines
660 a few tokens of its own. */
661 static const char *const token_names[] = {
662 /* cpplib-defined token types */
668 /* C++ parser token types - see "Manifest constants", above. */
671 "NESTED_NAME_SPECIFIER",
675 /* If we have a name for the token, print it out. Otherwise, we
676 simply give the numeric code. */
677 gcc_assert (token->type < ARRAY_SIZE(token_names));
678 fputs (token_names[token->type], stream);
680 /* For some tokens, print the associated data. */
684 /* Some keywords have a value that is not an IDENTIFIER_NODE.
685 For example, `struct' is mapped to an INTEGER_CST. */
686 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
688 /* else fall through */
690 fputs (IDENTIFIER_POINTER (token->value), stream);
696 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
704 /* Start emitting debugging information. */
707 cp_lexer_start_debugging (cp_lexer* lexer)
709 lexer->debugging_p = true;
712 /* Stop emitting debugging information. */
715 cp_lexer_stop_debugging (cp_lexer* lexer)
717 lexer->debugging_p = false;
720 #endif /* ENABLE_CHECKING */
722 /* Create a new cp_token_cache, representing a range of tokens. */
724 static cp_token_cache *
725 cp_token_cache_new (cp_token *first, cp_token *last)
727 cp_token_cache *cache = GGC_NEW (cp_token_cache);
728 cache->first = first;
734 /* Decl-specifiers. */
736 static void clear_decl_specs
737 (cp_decl_specifier_seq *);
739 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
742 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
744 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
749 /* Nothing other than the parser should be creating declarators;
750 declarators are a semi-syntactic representation of C++ entities.
751 Other parts of the front end that need to create entities (like
752 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
754 static cp_declarator *make_call_declarator
755 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
756 static cp_declarator *make_array_declarator
757 (cp_declarator *, tree);
758 static cp_declarator *make_pointer_declarator
759 (cp_cv_quals, cp_declarator *);
760 static cp_declarator *make_reference_declarator
761 (cp_cv_quals, cp_declarator *);
762 static cp_parameter_declarator *make_parameter_declarator
763 (cp_decl_specifier_seq *, cp_declarator *, tree);
764 static cp_declarator *make_ptrmem_declarator
765 (cp_cv_quals, tree, cp_declarator *);
767 cp_declarator *cp_error_declarator;
769 /* The obstack on which declarators and related data structures are
771 static struct obstack declarator_obstack;
773 /* Alloc BYTES from the declarator memory pool. */
776 alloc_declarator (size_t bytes)
778 return obstack_alloc (&declarator_obstack, bytes);
781 /* Allocate a declarator of the indicated KIND. Clear fields that are
782 common to all declarators. */
784 static cp_declarator *
785 make_declarator (cp_declarator_kind kind)
787 cp_declarator *declarator;
789 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
790 declarator->kind = kind;
791 declarator->attributes = NULL_TREE;
792 declarator->declarator = NULL;
797 /* Make a declarator for a generalized identifier. If non-NULL, the
798 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
799 just UNQUALIFIED_NAME. */
801 static cp_declarator *
802 make_id_declarator (tree qualifying_scope, tree unqualified_name)
804 cp_declarator *declarator;
806 /* It is valid to write:
808 class C { void f(); };
812 The standard is not clear about whether `typedef const C D' is
813 legal; as of 2002-09-15 the committee is considering that
814 question. EDG 3.0 allows that syntax. Therefore, we do as
816 if (qualifying_scope && TYPE_P (qualifying_scope))
817 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
819 declarator = make_declarator (cdk_id);
820 declarator->u.id.qualifying_scope = qualifying_scope;
821 declarator->u.id.unqualified_name = unqualified_name;
822 declarator->u.id.sfk = sfk_none;
827 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
828 of modifiers such as const or volatile to apply to the pointer
829 type, represented as identifiers. */
832 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
834 cp_declarator *declarator;
836 declarator = make_declarator (cdk_pointer);
837 declarator->declarator = target;
838 declarator->u.pointer.qualifiers = cv_qualifiers;
839 declarator->u.pointer.class_type = NULL_TREE;
844 /* Like make_pointer_declarator -- but for references. */
847 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
849 cp_declarator *declarator;
851 declarator = make_declarator (cdk_reference);
852 declarator->declarator = target;
853 declarator->u.pointer.qualifiers = cv_qualifiers;
854 declarator->u.pointer.class_type = NULL_TREE;
859 /* Like make_pointer_declarator -- but for a pointer to a non-static
860 member of CLASS_TYPE. */
863 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
864 cp_declarator *pointee)
866 cp_declarator *declarator;
868 declarator = make_declarator (cdk_ptrmem);
869 declarator->declarator = pointee;
870 declarator->u.pointer.qualifiers = cv_qualifiers;
871 declarator->u.pointer.class_type = class_type;
876 /* Make a declarator for the function given by TARGET, with the
877 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
878 "const"-qualified member function. The EXCEPTION_SPECIFICATION
879 indicates what exceptions can be thrown. */
882 make_call_declarator (cp_declarator *target,
883 cp_parameter_declarator *parms,
884 cp_cv_quals cv_qualifiers,
885 tree exception_specification)
887 cp_declarator *declarator;
889 declarator = make_declarator (cdk_function);
890 declarator->declarator = target;
891 declarator->u.function.parameters = parms;
892 declarator->u.function.qualifiers = cv_qualifiers;
893 declarator->u.function.exception_specification = exception_specification;
898 /* Make a declarator for an array of BOUNDS elements, each of which is
899 defined by ELEMENT. */
902 make_array_declarator (cp_declarator *element, tree bounds)
904 cp_declarator *declarator;
906 declarator = make_declarator (cdk_array);
907 declarator->declarator = element;
908 declarator->u.array.bounds = bounds;
913 cp_parameter_declarator *no_parameters;
915 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
916 DECLARATOR and DEFAULT_ARGUMENT. */
918 cp_parameter_declarator *
919 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
920 cp_declarator *declarator,
921 tree default_argument)
923 cp_parameter_declarator *parameter;
925 parameter = ((cp_parameter_declarator *)
926 alloc_declarator (sizeof (cp_parameter_declarator)));
927 parameter->next = NULL;
929 parameter->decl_specifiers = *decl_specifiers;
931 clear_decl_specs (¶meter->decl_specifiers);
932 parameter->declarator = declarator;
933 parameter->default_argument = default_argument;
934 parameter->ellipsis_p = false;
944 A cp_parser parses the token stream as specified by the C++
945 grammar. Its job is purely parsing, not semantic analysis. For
946 example, the parser breaks the token stream into declarators,
947 expressions, statements, and other similar syntactic constructs.
948 It does not check that the types of the expressions on either side
949 of an assignment-statement are compatible, or that a function is
950 not declared with a parameter of type `void'.
952 The parser invokes routines elsewhere in the compiler to perform
953 semantic analysis and to build up the abstract syntax tree for the
956 The parser (and the template instantiation code, which is, in a
957 way, a close relative of parsing) are the only parts of the
958 compiler that should be calling push_scope and pop_scope, or
959 related functions. The parser (and template instantiation code)
960 keeps track of what scope is presently active; everything else
961 should simply honor that. (The code that generates static
962 initializers may also need to set the scope, in order to check
963 access control correctly when emitting the initializers.)
968 The parser is of the standard recursive-descent variety. Upcoming
969 tokens in the token stream are examined in order to determine which
970 production to use when parsing a non-terminal. Some C++ constructs
971 require arbitrary look ahead to disambiguate. For example, it is
972 impossible, in the general case, to tell whether a statement is an
973 expression or declaration without scanning the entire statement.
974 Therefore, the parser is capable of "parsing tentatively." When the
975 parser is not sure what construct comes next, it enters this mode.
976 Then, while we attempt to parse the construct, the parser queues up
977 error messages, rather than issuing them immediately, and saves the
978 tokens it consumes. If the construct is parsed successfully, the
979 parser "commits", i.e., it issues any queued error messages and
980 the tokens that were being preserved are permanently discarded.
981 If, however, the construct is not parsed successfully, the parser
982 rolls back its state completely so that it can resume parsing using
983 a different alternative.
988 The performance of the parser could probably be improved substantially.
989 We could often eliminate the need to parse tentatively by looking ahead
990 a little bit. In some places, this approach might not entirely eliminate
991 the need to parse tentatively, but it might still speed up the average
994 /* Flags that are passed to some parsing functions. These values can
995 be bitwise-ored together. */
997 typedef enum cp_parser_flags
1000 CP_PARSER_FLAGS_NONE = 0x0,
1001 /* The construct is optional. If it is not present, then no error
1002 should be issued. */
1003 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1004 /* When parsing a type-specifier, do not allow user-defined types. */
1005 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1008 /* The different kinds of declarators we want to parse. */
1010 typedef enum cp_parser_declarator_kind
1012 /* We want an abstract declarator. */
1013 CP_PARSER_DECLARATOR_ABSTRACT,
1014 /* We want a named declarator. */
1015 CP_PARSER_DECLARATOR_NAMED,
1016 /* We don't mind, but the name must be an unqualified-id. */
1017 CP_PARSER_DECLARATOR_EITHER
1018 } cp_parser_declarator_kind;
1020 /* The precedence values used to parse binary expressions. The minimum value
1021 of PREC must be 1, because zero is reserved to quickly discriminate
1022 binary operators from other tokens. */
1027 PREC_LOGICAL_OR_EXPRESSION,
1028 PREC_LOGICAL_AND_EXPRESSION,
1029 PREC_INCLUSIVE_OR_EXPRESSION,
1030 PREC_EXCLUSIVE_OR_EXPRESSION,
1031 PREC_AND_EXPRESSION,
1032 PREC_EQUALITY_EXPRESSION,
1033 PREC_RELATIONAL_EXPRESSION,
1034 PREC_SHIFT_EXPRESSION,
1035 PREC_ADDITIVE_EXPRESSION,
1036 PREC_MULTIPLICATIVE_EXPRESSION,
1038 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1041 /* A mapping from a token type to a corresponding tree node type, with a
1042 precedence value. */
1044 typedef struct cp_parser_binary_operations_map_node
1046 /* The token type. */
1047 enum cpp_ttype token_type;
1048 /* The corresponding tree code. */
1049 enum tree_code tree_type;
1050 /* The precedence of this operator. */
1051 enum cp_parser_prec prec;
1052 } cp_parser_binary_operations_map_node;
1054 /* The status of a tentative parse. */
1056 typedef enum cp_parser_status_kind
1058 /* No errors have occurred. */
1059 CP_PARSER_STATUS_KIND_NO_ERROR,
1060 /* An error has occurred. */
1061 CP_PARSER_STATUS_KIND_ERROR,
1062 /* We are committed to this tentative parse, whether or not an error
1064 CP_PARSER_STATUS_KIND_COMMITTED
1065 } cp_parser_status_kind;
1067 typedef struct cp_parser_expression_stack_entry
1070 enum tree_code tree_type;
1072 } cp_parser_expression_stack_entry;
1074 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1075 entries because precedence levels on the stack are monotonically
1077 typedef struct cp_parser_expression_stack_entry
1078 cp_parser_expression_stack[NUM_PREC_VALUES];
1080 /* Context that is saved and restored when parsing tentatively. */
1081 typedef struct cp_parser_context GTY (())
1083 /* If this is a tentative parsing context, the status of the
1085 enum cp_parser_status_kind status;
1086 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1087 that are looked up in this context must be looked up both in the
1088 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1089 the context of the containing expression. */
1092 /* The next parsing context in the stack. */
1093 struct cp_parser_context *next;
1094 } cp_parser_context;
1098 /* Constructors and destructors. */
1100 static cp_parser_context *cp_parser_context_new
1101 (cp_parser_context *);
1103 /* Class variables. */
1105 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1107 /* The operator-precedence table used by cp_parser_binary_expression.
1108 Transformed into an associative array (binops_by_token) by
1111 static const cp_parser_binary_operations_map_node binops[] = {
1112 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1113 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1115 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1116 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1117 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1119 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1120 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1122 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1123 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1125 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1126 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1127 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1128 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1129 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1130 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1132 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1133 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1135 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1137 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1139 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1141 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1143 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1146 /* The same as binops, but initialized by cp_parser_new so that
1147 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1149 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1151 /* Constructors and destructors. */
1153 /* Construct a new context. The context below this one on the stack
1154 is given by NEXT. */
1156 static cp_parser_context *
1157 cp_parser_context_new (cp_parser_context* next)
1159 cp_parser_context *context;
1161 /* Allocate the storage. */
1162 if (cp_parser_context_free_list != NULL)
1164 /* Pull the first entry from the free list. */
1165 context = cp_parser_context_free_list;
1166 cp_parser_context_free_list = context->next;
1167 memset (context, 0, sizeof (*context));
1170 context = GGC_CNEW (cp_parser_context);
1172 /* No errors have occurred yet in this context. */
1173 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1174 /* If this is not the bottomost context, copy information that we
1175 need from the previous context. */
1178 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1179 expression, then we are parsing one in this context, too. */
1180 context->object_type = next->object_type;
1181 /* Thread the stack. */
1182 context->next = next;
1188 /* The cp_parser structure represents the C++ parser. */
1190 typedef struct cp_parser GTY(())
1192 /* The lexer from which we are obtaining tokens. */
1195 /* The scope in which names should be looked up. If NULL_TREE, then
1196 we look up names in the scope that is currently open in the
1197 source program. If non-NULL, this is either a TYPE or
1198 NAMESPACE_DECL for the scope in which we should look.
1200 This value is not cleared automatically after a name is looked
1201 up, so we must be careful to clear it before starting a new look
1202 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1203 will look up `Z' in the scope of `X', rather than the current
1204 scope.) Unfortunately, it is difficult to tell when name lookup
1205 is complete, because we sometimes peek at a token, look it up,
1206 and then decide not to consume it. */
1209 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1210 last lookup took place. OBJECT_SCOPE is used if an expression
1211 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1212 respectively. QUALIFYING_SCOPE is used for an expression of the
1213 form "X::Y"; it refers to X. */
1215 tree qualifying_scope;
1217 /* A stack of parsing contexts. All but the bottom entry on the
1218 stack will be tentative contexts.
1220 We parse tentatively in order to determine which construct is in
1221 use in some situations. For example, in order to determine
1222 whether a statement is an expression-statement or a
1223 declaration-statement we parse it tentatively as a
1224 declaration-statement. If that fails, we then reparse the same
1225 token stream as an expression-statement. */
1226 cp_parser_context *context;
1228 /* True if we are parsing GNU C++. If this flag is not set, then
1229 GNU extensions are not recognized. */
1230 bool allow_gnu_extensions_p;
1232 /* TRUE if the `>' token should be interpreted as the greater-than
1233 operator. FALSE if it is the end of a template-id or
1234 template-parameter-list. */
1235 bool greater_than_is_operator_p;
1237 /* TRUE if default arguments are allowed within a parameter list
1238 that starts at this point. FALSE if only a gnu extension makes
1239 them permissible. */
1240 bool default_arg_ok_p;
1242 /* TRUE if we are parsing an integral constant-expression. See
1243 [expr.const] for a precise definition. */
1244 bool integral_constant_expression_p;
1246 /* TRUE if we are parsing an integral constant-expression -- but a
1247 non-constant expression should be permitted as well. This flag
1248 is used when parsing an array bound so that GNU variable-length
1249 arrays are tolerated. */
1250 bool allow_non_integral_constant_expression_p;
1252 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1253 been seen that makes the expression non-constant. */
1254 bool non_integral_constant_expression_p;
1256 /* TRUE if local variable names and `this' are forbidden in the
1258 bool local_variables_forbidden_p;
1260 /* TRUE if the declaration we are parsing is part of a
1261 linkage-specification of the form `extern string-literal
1263 bool in_unbraced_linkage_specification_p;
1265 /* TRUE if we are presently parsing a declarator, after the
1266 direct-declarator. */
1267 bool in_declarator_p;
1269 /* TRUE if we are presently parsing a template-argument-list. */
1270 bool in_template_argument_list_p;
1272 /* TRUE if we are presently parsing the body of an
1273 iteration-statement. */
1274 bool in_iteration_statement_p;
1276 /* TRUE if we are presently parsing the body of a switch
1278 bool in_switch_statement_p;
1280 /* TRUE if we are parsing a type-id in an expression context. In
1281 such a situation, both "type (expr)" and "type (type)" are valid
1283 bool in_type_id_in_expr_p;
1285 /* TRUE if we are currently in a header file where declarations are
1286 implicitly extern "C". */
1287 bool implicit_extern_c;
1289 /* TRUE if strings in expressions should be translated to the execution
1291 bool translate_strings_p;
1293 /* If non-NULL, then we are parsing a construct where new type
1294 definitions are not permitted. The string stored here will be
1295 issued as an error message if a type is defined. */
1296 const char *type_definition_forbidden_message;
1298 /* A list of lists. The outer list is a stack, used for member
1299 functions of local classes. At each level there are two sub-list,
1300 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1301 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1302 TREE_VALUE's. The functions are chained in reverse declaration
1305 The TREE_PURPOSE sublist contains those functions with default
1306 arguments that need post processing, and the TREE_VALUE sublist
1307 contains those functions with definitions that need post
1310 These lists can only be processed once the outermost class being
1311 defined is complete. */
1312 tree unparsed_functions_queues;
1314 /* The number of classes whose definitions are currently in
1316 unsigned num_classes_being_defined;
1318 /* The number of template parameter lists that apply directly to the
1319 current declaration. */
1320 unsigned num_template_parameter_lists;
1323 /* The type of a function that parses some kind of expression. */
1324 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1328 /* Constructors and destructors. */
1330 static cp_parser *cp_parser_new
1333 /* Routines to parse various constructs.
1335 Those that return `tree' will return the error_mark_node (rather
1336 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1337 Sometimes, they will return an ordinary node if error-recovery was
1338 attempted, even though a parse error occurred. So, to check
1339 whether or not a parse error occurred, you should always use
1340 cp_parser_error_occurred. If the construct is optional (indicated
1341 either by an `_opt' in the name of the function that does the
1342 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1343 the construct is not present. */
1345 /* Lexical conventions [gram.lex] */
1347 static tree cp_parser_identifier
1349 static tree cp_parser_string_literal
1350 (cp_parser *, bool, bool);
1352 /* Basic concepts [gram.basic] */
1354 static bool cp_parser_translation_unit
1357 /* Expressions [gram.expr] */
1359 static tree cp_parser_primary_expression
1360 (cp_parser *, bool, cp_id_kind *, tree *);
1361 static tree cp_parser_id_expression
1362 (cp_parser *, bool, bool, bool *, bool);
1363 static tree cp_parser_unqualified_id
1364 (cp_parser *, bool, bool, bool);
1365 static tree cp_parser_nested_name_specifier_opt
1366 (cp_parser *, bool, bool, bool, bool);
1367 static tree cp_parser_nested_name_specifier
1368 (cp_parser *, bool, bool, bool, bool);
1369 static tree cp_parser_class_or_namespace_name
1370 (cp_parser *, bool, bool, bool, bool, bool);
1371 static tree cp_parser_postfix_expression
1372 (cp_parser *, bool, bool);
1373 static tree cp_parser_postfix_open_square_expression
1374 (cp_parser *, tree, bool);
1375 static tree cp_parser_postfix_dot_deref_expression
1376 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1377 static tree cp_parser_parenthesized_expression_list
1378 (cp_parser *, bool, bool, bool *);
1379 static void cp_parser_pseudo_destructor_name
1380 (cp_parser *, tree *, tree *);
1381 static tree cp_parser_unary_expression
1382 (cp_parser *, bool, bool);
1383 static enum tree_code cp_parser_unary_operator
1385 static tree cp_parser_new_expression
1387 static tree cp_parser_new_placement
1389 static tree cp_parser_new_type_id
1390 (cp_parser *, tree *);
1391 static cp_declarator *cp_parser_new_declarator_opt
1393 static cp_declarator *cp_parser_direct_new_declarator
1395 static tree cp_parser_new_initializer
1397 static tree cp_parser_delete_expression
1399 static tree cp_parser_cast_expression
1400 (cp_parser *, bool, bool);
1401 static tree cp_parser_binary_expression
1402 (cp_parser *, bool);
1403 static tree cp_parser_question_colon_clause
1404 (cp_parser *, tree);
1405 static tree cp_parser_assignment_expression
1406 (cp_parser *, bool);
1407 static enum tree_code cp_parser_assignment_operator_opt
1409 static tree cp_parser_expression
1410 (cp_parser *, bool);
1411 static tree cp_parser_constant_expression
1412 (cp_parser *, bool, bool *);
1413 static tree cp_parser_builtin_offsetof
1416 /* Statements [gram.stmt.stmt] */
1418 static void cp_parser_statement
1419 (cp_parser *, tree);
1420 static tree cp_parser_labeled_statement
1421 (cp_parser *, tree);
1422 static tree cp_parser_expression_statement
1423 (cp_parser *, tree);
1424 static tree cp_parser_compound_statement
1425 (cp_parser *, tree, bool);
1426 static void cp_parser_statement_seq_opt
1427 (cp_parser *, tree);
1428 static tree cp_parser_selection_statement
1430 static tree cp_parser_condition
1432 static tree cp_parser_iteration_statement
1434 static void cp_parser_for_init_statement
1436 static tree cp_parser_jump_statement
1438 static void cp_parser_declaration_statement
1441 static tree cp_parser_implicitly_scoped_statement
1443 static void cp_parser_already_scoped_statement
1446 /* Declarations [gram.dcl.dcl] */
1448 static void cp_parser_declaration_seq_opt
1450 static void cp_parser_declaration
1452 static void cp_parser_block_declaration
1453 (cp_parser *, bool);
1454 static void cp_parser_simple_declaration
1455 (cp_parser *, bool);
1456 static void cp_parser_decl_specifier_seq
1457 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1458 static tree cp_parser_storage_class_specifier_opt
1460 static tree cp_parser_function_specifier_opt
1461 (cp_parser *, cp_decl_specifier_seq *);
1462 static tree cp_parser_type_specifier
1463 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1465 static tree cp_parser_simple_type_specifier
1466 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1467 static tree cp_parser_type_name
1469 static tree cp_parser_elaborated_type_specifier
1470 (cp_parser *, bool, bool);
1471 static tree cp_parser_enum_specifier
1473 static void cp_parser_enumerator_list
1474 (cp_parser *, tree);
1475 static void cp_parser_enumerator_definition
1476 (cp_parser *, tree);
1477 static tree cp_parser_namespace_name
1479 static void cp_parser_namespace_definition
1481 static void cp_parser_namespace_body
1483 static tree cp_parser_qualified_namespace_specifier
1485 static void cp_parser_namespace_alias_definition
1487 static void cp_parser_using_declaration
1489 static void cp_parser_using_directive
1491 static void cp_parser_asm_definition
1493 static void cp_parser_linkage_specification
1496 /* Declarators [gram.dcl.decl] */
1498 static tree cp_parser_init_declarator
1499 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1500 static cp_declarator *cp_parser_declarator
1501 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1502 static cp_declarator *cp_parser_direct_declarator
1503 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1504 static enum tree_code cp_parser_ptr_operator
1505 (cp_parser *, tree *, cp_cv_quals *);
1506 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1508 static tree cp_parser_declarator_id
1510 static tree cp_parser_type_id
1512 static void cp_parser_type_specifier_seq
1513 (cp_parser *, cp_decl_specifier_seq *);
1514 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1516 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1517 (cp_parser *, bool *);
1518 static cp_parameter_declarator *cp_parser_parameter_declaration
1519 (cp_parser *, bool, bool *);
1520 static void cp_parser_function_body
1522 static tree cp_parser_initializer
1523 (cp_parser *, bool *, bool *);
1524 static tree cp_parser_initializer_clause
1525 (cp_parser *, bool *);
1526 static tree cp_parser_initializer_list
1527 (cp_parser *, bool *);
1529 static bool cp_parser_ctor_initializer_opt_and_function_body
1532 /* Classes [gram.class] */
1534 static tree cp_parser_class_name
1535 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1536 static tree cp_parser_class_specifier
1538 static tree cp_parser_class_head
1539 (cp_parser *, bool *, tree *);
1540 static enum tag_types cp_parser_class_key
1542 static void cp_parser_member_specification_opt
1544 static void cp_parser_member_declaration
1546 static tree cp_parser_pure_specifier
1548 static tree cp_parser_constant_initializer
1551 /* Derived classes [gram.class.derived] */
1553 static tree cp_parser_base_clause
1555 static tree cp_parser_base_specifier
1558 /* Special member functions [gram.special] */
1560 static tree cp_parser_conversion_function_id
1562 static tree cp_parser_conversion_type_id
1564 static cp_declarator *cp_parser_conversion_declarator_opt
1566 static bool cp_parser_ctor_initializer_opt
1568 static void cp_parser_mem_initializer_list
1570 static tree cp_parser_mem_initializer
1572 static tree cp_parser_mem_initializer_id
1575 /* Overloading [gram.over] */
1577 static tree cp_parser_operator_function_id
1579 static tree cp_parser_operator
1582 /* Templates [gram.temp] */
1584 static void cp_parser_template_declaration
1585 (cp_parser *, bool);
1586 static tree cp_parser_template_parameter_list
1588 static tree cp_parser_template_parameter
1589 (cp_parser *, bool *);
1590 static tree cp_parser_type_parameter
1592 static tree cp_parser_template_id
1593 (cp_parser *, bool, bool, bool);
1594 static tree cp_parser_template_name
1595 (cp_parser *, bool, bool, bool, bool *);
1596 static tree cp_parser_template_argument_list
1598 static tree cp_parser_template_argument
1600 static void cp_parser_explicit_instantiation
1602 static void cp_parser_explicit_specialization
1605 /* Exception handling [gram.exception] */
1607 static tree cp_parser_try_block
1609 static bool cp_parser_function_try_block
1611 static void cp_parser_handler_seq
1613 static void cp_parser_handler
1615 static tree cp_parser_exception_declaration
1617 static tree cp_parser_throw_expression
1619 static tree cp_parser_exception_specification_opt
1621 static tree cp_parser_type_id_list
1624 /* GNU Extensions */
1626 static tree cp_parser_asm_specification_opt
1628 static tree cp_parser_asm_operand_list
1630 static tree cp_parser_asm_clobber_list
1632 static tree cp_parser_attributes_opt
1634 static tree cp_parser_attribute_list
1636 static bool cp_parser_extension_opt
1637 (cp_parser *, int *);
1638 static void cp_parser_label_declaration
1641 /* Utility Routines */
1643 static tree cp_parser_lookup_name
1644 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1645 static tree cp_parser_lookup_name_simple
1646 (cp_parser *, tree);
1647 static tree cp_parser_maybe_treat_template_as_class
1649 static bool cp_parser_check_declarator_template_parameters
1650 (cp_parser *, cp_declarator *);
1651 static bool cp_parser_check_template_parameters
1652 (cp_parser *, unsigned);
1653 static tree cp_parser_simple_cast_expression
1655 static tree cp_parser_global_scope_opt
1656 (cp_parser *, bool);
1657 static bool cp_parser_constructor_declarator_p
1658 (cp_parser *, bool);
1659 static tree cp_parser_function_definition_from_specifiers_and_declarator
1660 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1661 static tree cp_parser_function_definition_after_declarator
1662 (cp_parser *, bool);
1663 static void cp_parser_template_declaration_after_export
1664 (cp_parser *, bool);
1665 static tree cp_parser_single_declaration
1666 (cp_parser *, bool, bool *);
1667 static tree cp_parser_functional_cast
1668 (cp_parser *, tree);
1669 static tree cp_parser_save_member_function_body
1670 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1671 static tree cp_parser_enclosed_template_argument_list
1673 static void cp_parser_save_default_args
1674 (cp_parser *, tree);
1675 static void cp_parser_late_parsing_for_member
1676 (cp_parser *, tree);
1677 static void cp_parser_late_parsing_default_args
1678 (cp_parser *, tree);
1679 static tree cp_parser_sizeof_operand
1680 (cp_parser *, enum rid);
1681 static bool cp_parser_declares_only_class_p
1683 static void cp_parser_set_storage_class
1684 (cp_decl_specifier_seq *, cp_storage_class);
1685 static void cp_parser_set_decl_spec_type
1686 (cp_decl_specifier_seq *, tree, bool);
1687 static bool cp_parser_friend_p
1688 (const cp_decl_specifier_seq *);
1689 static cp_token *cp_parser_require
1690 (cp_parser *, enum cpp_ttype, const char *);
1691 static cp_token *cp_parser_require_keyword
1692 (cp_parser *, enum rid, const char *);
1693 static bool cp_parser_token_starts_function_definition_p
1695 static bool cp_parser_next_token_starts_class_definition_p
1697 static bool cp_parser_next_token_ends_template_argument_p
1699 static bool cp_parser_nth_token_starts_template_argument_list_p
1700 (cp_parser *, size_t);
1701 static enum tag_types cp_parser_token_is_class_key
1703 static void cp_parser_check_class_key
1704 (enum tag_types, tree type);
1705 static void cp_parser_check_access_in_redeclaration
1707 static bool cp_parser_optional_template_keyword
1709 static void cp_parser_pre_parsed_nested_name_specifier
1711 static void cp_parser_cache_group
1712 (cp_parser *, enum cpp_ttype, unsigned);
1713 static void cp_parser_parse_tentatively
1715 static void cp_parser_commit_to_tentative_parse
1717 static void cp_parser_abort_tentative_parse
1719 static bool cp_parser_parse_definitely
1721 static inline bool cp_parser_parsing_tentatively
1723 static bool cp_parser_uncommitted_to_tentative_parse_p
1725 static void cp_parser_error
1726 (cp_parser *, const char *);
1727 static void cp_parser_name_lookup_error
1728 (cp_parser *, tree, tree, const char *);
1729 static bool cp_parser_simulate_error
1731 static void cp_parser_check_type_definition
1733 static void cp_parser_check_for_definition_in_return_type
1734 (cp_declarator *, tree);
1735 static void cp_parser_check_for_invalid_template_id
1736 (cp_parser *, tree);
1737 static bool cp_parser_non_integral_constant_expression
1738 (cp_parser *, const char *);
1739 static void cp_parser_diagnose_invalid_type_name
1740 (cp_parser *, tree, tree);
1741 static bool cp_parser_parse_and_diagnose_invalid_type_name
1743 static int cp_parser_skip_to_closing_parenthesis
1744 (cp_parser *, bool, bool, bool);
1745 static void cp_parser_skip_to_end_of_statement
1747 static void cp_parser_consume_semicolon_at_end_of_statement
1749 static void cp_parser_skip_to_end_of_block_or_statement
1751 static void cp_parser_skip_to_closing_brace
1753 static void cp_parser_skip_until_found
1754 (cp_parser *, enum cpp_ttype, const char *);
1755 static bool cp_parser_error_occurred
1757 static bool cp_parser_allow_gnu_extensions_p
1759 static bool cp_parser_is_string_literal
1761 static bool cp_parser_is_keyword
1762 (cp_token *, enum rid);
1763 static tree cp_parser_make_typename_type
1764 (cp_parser *, tree, tree);
1766 /* Returns nonzero if we are parsing tentatively. */
1769 cp_parser_parsing_tentatively (cp_parser* parser)
1771 return parser->context->next != NULL;
1774 /* Returns nonzero if TOKEN is a string literal. */
1777 cp_parser_is_string_literal (cp_token* token)
1779 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1782 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1785 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1787 return token->keyword == keyword;
1790 /* If not parsing tentatively, issue a diagnostic of the form
1791 FILE:LINE: MESSAGE before TOKEN
1792 where TOKEN is the next token in the input stream. MESSAGE
1793 (specified by the caller) is usually of the form "expected
1797 cp_parser_error (cp_parser* parser, const char* message)
1799 if (!cp_parser_simulate_error (parser))
1801 cp_token *token = cp_lexer_peek_token (parser->lexer);
1802 /* This diagnostic makes more sense if it is tagged to the line
1803 of the token we just peeked at. */
1804 cp_lexer_set_source_position_from_token (token);
1805 if (token->type == CPP_PRAGMA)
1807 error ("%<#pragma%> is not allowed here");
1808 cp_lexer_purge_token (parser->lexer);
1811 c_parse_error (message,
1812 /* Because c_parser_error does not understand
1813 CPP_KEYWORD, keywords are treated like
1815 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1820 /* Issue an error about name-lookup failing. NAME is the
1821 IDENTIFIER_NODE DECL is the result of
1822 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1823 the thing that we hoped to find. */
1826 cp_parser_name_lookup_error (cp_parser* parser,
1829 const char* desired)
1831 /* If name lookup completely failed, tell the user that NAME was not
1833 if (decl == error_mark_node)
1835 if (parser->scope && parser->scope != global_namespace)
1836 error ("%<%D::%D%> has not been declared",
1837 parser->scope, name);
1838 else if (parser->scope == global_namespace)
1839 error ("%<::%D%> has not been declared", name);
1840 else if (parser->object_scope
1841 && !CLASS_TYPE_P (parser->object_scope))
1842 error ("request for member %qD in non-class type %qT",
1843 name, parser->object_scope);
1844 else if (parser->object_scope)
1845 error ("%<%T::%D%> has not been declared",
1846 parser->object_scope, name);
1848 error ("%qD has not been declared", name);
1850 else if (parser->scope && parser->scope != global_namespace)
1851 error ("%<%D::%D%> %s", parser->scope, name, desired);
1852 else if (parser->scope == global_namespace)
1853 error ("%<::%D%> %s", name, desired);
1855 error ("%qD %s", name, desired);
1858 /* If we are parsing tentatively, remember that an error has occurred
1859 during this tentative parse. Returns true if the error was
1860 simulated; false if a message should be issued by the caller. */
1863 cp_parser_simulate_error (cp_parser* parser)
1865 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1867 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1873 /* This function is called when a type is defined. If type
1874 definitions are forbidden at this point, an error message is
1878 cp_parser_check_type_definition (cp_parser* parser)
1880 /* If types are forbidden here, issue a message. */
1881 if (parser->type_definition_forbidden_message)
1882 /* Use `%s' to print the string in case there are any escape
1883 characters in the message. */
1884 error ("%s", parser->type_definition_forbidden_message);
1887 /* This function is called when the DECLARATOR is processed. The TYPE
1888 was a type defined in the decl-specifiers. If it is invalid to
1889 define a type in the decl-specifiers for DECLARATOR, an error is
1893 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1896 /* [dcl.fct] forbids type definitions in return types.
1897 Unfortunately, it's not easy to know whether or not we are
1898 processing a return type until after the fact. */
1900 && (declarator->kind == cdk_pointer
1901 || declarator->kind == cdk_reference
1902 || declarator->kind == cdk_ptrmem))
1903 declarator = declarator->declarator;
1905 && declarator->kind == cdk_function)
1907 error ("new types may not be defined in a return type");
1908 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1913 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1914 "<" in any valid C++ program. If the next token is indeed "<",
1915 issue a message warning the user about what appears to be an
1916 invalid attempt to form a template-id. */
1919 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1922 cp_token_position start = 0;
1924 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1927 error ("%qT is not a template", type);
1928 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1929 error ("%qE is not a template", type);
1931 error ("invalid template-id");
1932 /* Remember the location of the invalid "<". */
1933 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1934 start = cp_lexer_token_position (parser->lexer, true);
1935 /* Consume the "<". */
1936 cp_lexer_consume_token (parser->lexer);
1937 /* Parse the template arguments. */
1938 cp_parser_enclosed_template_argument_list (parser);
1939 /* Permanently remove the invalid template arguments so that
1940 this error message is not issued again. */
1942 cp_lexer_purge_tokens_after (parser->lexer, start);
1946 /* If parsing an integral constant-expression, issue an error message
1947 about the fact that THING appeared and return true. Otherwise,
1948 return false. In either case, set
1949 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
1952 cp_parser_non_integral_constant_expression (cp_parser *parser,
1955 parser->non_integral_constant_expression_p = true;
1956 if (parser->integral_constant_expression_p)
1958 if (!parser->allow_non_integral_constant_expression_p)
1960 error ("%s cannot appear in a constant-expression", thing);
1967 /* Emit a diagnostic for an invalid type name. SCOPE is the
1968 qualifying scope (or NULL, if none) for ID. This function commits
1969 to the current active tentative parse, if any. (Otherwise, the
1970 problematic construct might be encountered again later, resulting
1971 in duplicate error messages.) */
1974 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1976 tree decl, old_scope;
1977 /* Try to lookup the identifier. */
1978 old_scope = parser->scope;
1979 parser->scope = scope;
1980 decl = cp_parser_lookup_name_simple (parser, id);
1981 parser->scope = old_scope;
1982 /* If the lookup found a template-name, it means that the user forgot
1983 to specify an argument list. Emit an useful error message. */
1984 if (TREE_CODE (decl) == TEMPLATE_DECL)
1985 error ("invalid use of template-name %qE without an argument list",
1987 else if (!parser->scope)
1989 /* Issue an error message. */
1990 error ("%qE does not name a type", id);
1991 /* If we're in a template class, it's possible that the user was
1992 referring to a type from a base class. For example:
1994 template <typename T> struct A { typedef T X; };
1995 template <typename T> struct B : public A<T> { X x; };
1997 The user should have said "typename A<T>::X". */
1998 if (processing_template_decl && current_class_type
1999 && TYPE_BINFO (current_class_type))
2003 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2007 tree base_type = BINFO_TYPE (b);
2008 if (CLASS_TYPE_P (base_type)
2009 && dependent_type_p (base_type))
2012 /* Go from a particular instantiation of the
2013 template (which will have an empty TYPE_FIELDs),
2014 to the main version. */
2015 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2016 for (field = TYPE_FIELDS (base_type);
2018 field = TREE_CHAIN (field))
2019 if (TREE_CODE (field) == TYPE_DECL
2020 && DECL_NAME (field) == id)
2022 inform ("(perhaps %<typename %T::%E%> was intended)",
2023 BINFO_TYPE (b), id);
2032 /* Here we diagnose qualified-ids where the scope is actually correct,
2033 but the identifier does not resolve to a valid type name. */
2036 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2037 error ("%qE in namespace %qE does not name a type",
2039 else if (TYPE_P (parser->scope))
2040 error ("%qE in class %qT does not name a type", id, parser->scope);
2044 cp_parser_commit_to_tentative_parse (parser);
2047 /* Check for a common situation where a type-name should be present,
2048 but is not, and issue a sensible error message. Returns true if an
2049 invalid type-name was detected.
2051 The situation handled by this function are variable declarations of the
2052 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2053 Usually, `ID' should name a type, but if we got here it means that it
2054 does not. We try to emit the best possible error message depending on
2055 how exactly the id-expression looks like.
2059 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2063 cp_parser_parse_tentatively (parser);
2064 id = cp_parser_id_expression (parser,
2065 /*template_keyword_p=*/false,
2066 /*check_dependency_p=*/true,
2067 /*template_p=*/NULL,
2068 /*declarator_p=*/true);
2069 /* After the id-expression, there should be a plain identifier,
2070 otherwise this is not a simple variable declaration. Also, if
2071 the scope is dependent, we cannot do much. */
2072 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2073 || (parser->scope && TYPE_P (parser->scope)
2074 && dependent_type_p (parser->scope)))
2076 cp_parser_abort_tentative_parse (parser);
2079 if (!cp_parser_parse_definitely (parser)
2080 || TREE_CODE (id) != IDENTIFIER_NODE)
2083 /* Emit a diagnostic for the invalid type. */
2084 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2085 /* Skip to the end of the declaration; there's no point in
2086 trying to process it. */
2087 cp_parser_skip_to_end_of_block_or_statement (parser);
2091 /* Consume tokens up to, and including, the next non-nested closing `)'.
2092 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2093 are doing error recovery. Returns -1 if OR_COMMA is true and we
2094 found an unnested comma. */
2097 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2102 unsigned paren_depth = 0;
2103 unsigned brace_depth = 0;
2106 if (recovering && !or_comma
2107 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2114 /* If we've run out of tokens, then there is no closing `)'. */
2115 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2121 token = cp_lexer_peek_token (parser->lexer);
2123 /* This matches the processing in skip_to_end_of_statement. */
2124 if (token->type == CPP_SEMICOLON && !brace_depth)
2129 if (token->type == CPP_OPEN_BRACE)
2131 if (token->type == CPP_CLOSE_BRACE)
2139 if (recovering && or_comma && token->type == CPP_COMMA
2140 && !brace_depth && !paren_depth)
2148 /* If it is an `(', we have entered another level of nesting. */
2149 if (token->type == CPP_OPEN_PAREN)
2151 /* If it is a `)', then we might be done. */
2152 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2155 cp_lexer_consume_token (parser->lexer);
2163 /* Consume the token. */
2164 cp_lexer_consume_token (parser->lexer);
2170 /* Consume tokens until we reach the end of the current statement.
2171 Normally, that will be just before consuming a `;'. However, if a
2172 non-nested `}' comes first, then we stop before consuming that. */
2175 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2177 unsigned nesting_depth = 0;
2183 /* Peek at the next token. */
2184 token = cp_lexer_peek_token (parser->lexer);
2185 /* If we've run out of tokens, stop. */
2186 if (token->type == CPP_EOF)
2188 /* If the next token is a `;', we have reached the end of the
2190 if (token->type == CPP_SEMICOLON && !nesting_depth)
2192 /* If the next token is a non-nested `}', then we have reached
2193 the end of the current block. */
2194 if (token->type == CPP_CLOSE_BRACE)
2196 /* If this is a non-nested `}', stop before consuming it.
2197 That way, when confronted with something like:
2201 we stop before consuming the closing `}', even though we
2202 have not yet reached a `;'. */
2203 if (nesting_depth == 0)
2205 /* If it is the closing `}' for a block that we have
2206 scanned, stop -- but only after consuming the token.
2212 we will stop after the body of the erroneously declared
2213 function, but before consuming the following `typedef'
2215 if (--nesting_depth == 0)
2217 cp_lexer_consume_token (parser->lexer);
2221 /* If it the next token is a `{', then we are entering a new
2222 block. Consume the entire block. */
2223 else if (token->type == CPP_OPEN_BRACE)
2225 /* Consume the token. */
2226 cp_lexer_consume_token (parser->lexer);
2230 /* This function is called at the end of a statement or declaration.
2231 If the next token is a semicolon, it is consumed; otherwise, error
2232 recovery is attempted. */
2235 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2237 /* Look for the trailing `;'. */
2238 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2240 /* If there is additional (erroneous) input, skip to the end of
2242 cp_parser_skip_to_end_of_statement (parser);
2243 /* If the next token is now a `;', consume it. */
2244 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2245 cp_lexer_consume_token (parser->lexer);
2249 /* Skip tokens until we have consumed an entire block, or until we
2250 have consumed a non-nested `;'. */
2253 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2255 unsigned nesting_depth = 0;
2261 /* Peek at the next token. */
2262 token = cp_lexer_peek_token (parser->lexer);
2263 /* If we've run out of tokens, stop. */
2264 if (token->type == CPP_EOF)
2266 /* If the next token is a `;', we have reached the end of the
2268 if (token->type == CPP_SEMICOLON && !nesting_depth)
2270 /* Consume the `;'. */
2271 cp_lexer_consume_token (parser->lexer);
2274 /* Consume the token. */
2275 token = cp_lexer_consume_token (parser->lexer);
2276 /* If the next token is a non-nested `}', then we have reached
2277 the end of the current block. */
2278 if (token->type == CPP_CLOSE_BRACE
2279 && (nesting_depth == 0 || --nesting_depth == 0))
2281 /* If it the next token is a `{', then we are entering a new
2282 block. Consume the entire block. */
2283 if (token->type == CPP_OPEN_BRACE)
2288 /* Skip tokens until a non-nested closing curly brace is the next
2292 cp_parser_skip_to_closing_brace (cp_parser *parser)
2294 unsigned nesting_depth = 0;
2300 /* Peek at the next token. */
2301 token = cp_lexer_peek_token (parser->lexer);
2302 /* If we've run out of tokens, stop. */
2303 if (token->type == CPP_EOF)
2305 /* If the next token is a non-nested `}', then we have reached
2306 the end of the current block. */
2307 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2309 /* If it the next token is a `{', then we are entering a new
2310 block. Consume the entire block. */
2311 else if (token->type == CPP_OPEN_BRACE)
2313 /* Consume the token. */
2314 cp_lexer_consume_token (parser->lexer);
2318 /* This is a simple wrapper around make_typename_type. When the id is
2319 an unresolved identifier node, we can provide a superior diagnostic
2320 using cp_parser_diagnose_invalid_type_name. */
2323 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2326 if (TREE_CODE (id) == IDENTIFIER_NODE)
2328 result = make_typename_type (scope, id, typename_type,
2330 if (result == error_mark_node)
2331 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2334 return make_typename_type (scope, id, typename_type, tf_error);
2338 /* Create a new C++ parser. */
2341 cp_parser_new (void)
2347 /* cp_lexer_new_main is called before calling ggc_alloc because
2348 cp_lexer_new_main might load a PCH file. */
2349 lexer = cp_lexer_new_main ();
2351 /* Initialize the binops_by_token so that we can get the tree
2352 directly from the token. */
2353 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2354 binops_by_token[binops[i].token_type] = binops[i];
2356 parser = GGC_CNEW (cp_parser);
2357 parser->lexer = lexer;
2358 parser->context = cp_parser_context_new (NULL);
2360 /* For now, we always accept GNU extensions. */
2361 parser->allow_gnu_extensions_p = 1;
2363 /* The `>' token is a greater-than operator, not the end of a
2365 parser->greater_than_is_operator_p = true;
2367 parser->default_arg_ok_p = true;
2369 /* We are not parsing a constant-expression. */
2370 parser->integral_constant_expression_p = false;
2371 parser->allow_non_integral_constant_expression_p = false;
2372 parser->non_integral_constant_expression_p = false;
2374 /* Local variable names are not forbidden. */
2375 parser->local_variables_forbidden_p = false;
2377 /* We are not processing an `extern "C"' declaration. */
2378 parser->in_unbraced_linkage_specification_p = false;
2380 /* We are not processing a declarator. */
2381 parser->in_declarator_p = false;
2383 /* We are not processing a template-argument-list. */
2384 parser->in_template_argument_list_p = false;
2386 /* We are not in an iteration statement. */
2387 parser->in_iteration_statement_p = false;
2389 /* We are not in a switch statement. */
2390 parser->in_switch_statement_p = false;
2392 /* We are not parsing a type-id inside an expression. */
2393 parser->in_type_id_in_expr_p = false;
2395 /* Declarations aren't implicitly extern "C". */
2396 parser->implicit_extern_c = false;
2398 /* String literals should be translated to the execution character set. */
2399 parser->translate_strings_p = true;
2401 /* The unparsed function queue is empty. */
2402 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2404 /* There are no classes being defined. */
2405 parser->num_classes_being_defined = 0;
2407 /* No template parameters apply. */
2408 parser->num_template_parameter_lists = 0;
2413 /* Create a cp_lexer structure which will emit the tokens in CACHE
2414 and push it onto the parser's lexer stack. This is used for delayed
2415 parsing of in-class method bodies and default arguments, and should
2416 not be confused with tentative parsing. */
2418 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2420 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2421 lexer->next = parser->lexer;
2422 parser->lexer = lexer;
2424 /* Move the current source position to that of the first token in the
2426 cp_lexer_set_source_position_from_token (lexer->next_token);
2429 /* Pop the top lexer off the parser stack. This is never used for the
2430 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2432 cp_parser_pop_lexer (cp_parser *parser)
2434 cp_lexer *lexer = parser->lexer;
2435 parser->lexer = lexer->next;
2436 cp_lexer_destroy (lexer);
2438 /* Put the current source position back where it was before this
2439 lexer was pushed. */
2440 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2443 /* Lexical conventions [gram.lex] */
2445 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2449 cp_parser_identifier (cp_parser* parser)
2453 /* Look for the identifier. */
2454 token = cp_parser_require (parser, CPP_NAME, "identifier");
2455 /* Return the value. */
2456 return token ? token->value : error_mark_node;
2459 /* Parse a sequence of adjacent string constants. Returns a
2460 TREE_STRING representing the combined, nul-terminated string
2461 constant. If TRANSLATE is true, translate the string to the
2462 execution character set. If WIDE_OK is true, a wide string is
2465 C++98 [lex.string] says that if a narrow string literal token is
2466 adjacent to a wide string literal token, the behavior is undefined.
2467 However, C99 6.4.5p4 says that this results in a wide string literal.
2468 We follow C99 here, for consistency with the C front end.
2470 This code is largely lifted from lex_string() in c-lex.c.
2472 FUTURE: ObjC++ will need to handle @-strings here. */
2474 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2479 struct obstack str_ob;
2480 cpp_string str, istr, *strs;
2483 tok = cp_lexer_peek_token (parser->lexer);
2484 if (!cp_parser_is_string_literal (tok))
2486 cp_parser_error (parser, "expected string-literal");
2487 return error_mark_node;
2490 /* Try to avoid the overhead of creating and destroying an obstack
2491 for the common case of just one string. */
2492 if (!cp_parser_is_string_literal
2493 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2495 cp_lexer_consume_token (parser->lexer);
2497 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2498 str.len = TREE_STRING_LENGTH (tok->value);
2500 if (tok->type == CPP_WSTRING)
2507 gcc_obstack_init (&str_ob);
2512 cp_lexer_consume_token (parser->lexer);
2514 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2515 str.len = TREE_STRING_LENGTH (tok->value);
2516 if (tok->type == CPP_WSTRING)
2519 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2521 tok = cp_lexer_peek_token (parser->lexer);
2523 while (cp_parser_is_string_literal (tok));
2525 strs = (cpp_string *) obstack_finish (&str_ob);
2528 if (wide && !wide_ok)
2530 cp_parser_error (parser, "a wide string is invalid in this context");
2534 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2535 (parse_in, strs, count, &istr, wide))
2537 value = build_string (istr.len, (char *)istr.text);
2538 free ((void *)istr.text);
2540 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2541 value = fix_string_type (value);
2544 /* cpp_interpret_string has issued an error. */
2545 value = error_mark_node;
2548 obstack_free (&str_ob, 0);
2554 /* Basic concepts [gram.basic] */
2556 /* Parse a translation-unit.
2559 declaration-seq [opt]
2561 Returns TRUE if all went well. */
2564 cp_parser_translation_unit (cp_parser* parser)
2566 /* The address of the first non-permanent object on the declarator
2568 static void *declarator_obstack_base;
2572 /* Create the declarator obstack, if necessary. */
2573 if (!cp_error_declarator)
2575 gcc_obstack_init (&declarator_obstack);
2576 /* Create the error declarator. */
2577 cp_error_declarator = make_declarator (cdk_error);
2578 /* Create the empty parameter list. */
2579 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2580 /* Remember where the base of the declarator obstack lies. */
2581 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2586 cp_parser_declaration_seq_opt (parser);
2588 /* If there are no tokens left then all went well. */
2589 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2591 /* Get rid of the token array; we don't need it any more. */
2592 cp_lexer_destroy (parser->lexer);
2593 parser->lexer = NULL;
2595 /* This file might have been a context that's implicitly extern
2596 "C". If so, pop the lang context. (Only relevant for PCH.) */
2597 if (parser->implicit_extern_c)
2599 pop_lang_context ();
2600 parser->implicit_extern_c = false;
2604 finish_translation_unit ();
2611 cp_parser_error (parser, "expected declaration");
2617 /* Make sure the declarator obstack was fully cleaned up. */
2618 gcc_assert (obstack_next_free (&declarator_obstack)
2619 == declarator_obstack_base);
2621 /* All went well. */
2625 /* Expressions [gram.expr] */
2627 /* Parse a primary-expression.
2638 ( compound-statement )
2639 __builtin_va_arg ( assignment-expression , type-id )
2644 CAST_P is true if this primary expression is the target of a cast.
2646 Returns a representation of the expression.
2648 *IDK indicates what kind of id-expression (if any) was present.
2650 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2651 used as the operand of a pointer-to-member. In that case,
2652 *QUALIFYING_CLASS gives the class that is used as the qualifying
2653 class in the pointer-to-member. */
2656 cp_parser_primary_expression (cp_parser *parser,
2659 tree *qualifying_class)
2663 /* Assume the primary expression is not an id-expression. */
2664 *idk = CP_ID_KIND_NONE;
2665 /* And that it cannot be used as pointer-to-member. */
2666 *qualifying_class = NULL_TREE;
2668 /* Peek at the next token. */
2669 token = cp_lexer_peek_token (parser->lexer);
2670 switch (token->type)
2681 token = cp_lexer_consume_token (parser->lexer);
2682 /* Floating-point literals are only allowed in an integral
2683 constant expression if they are cast to an integral or
2684 enumeration type. */
2685 if (TREE_CODE (token->value) == REAL_CST
2686 && parser->integral_constant_expression_p
2689 /* CAST_P will be set even in invalid code like "int(2.7 +
2690 ...)". Therefore, we have to check that the next token
2691 is sure to end the cast. */
2694 cp_token *next_token;
2696 next_token = cp_lexer_peek_token (parser->lexer);
2697 if (/* The comma at the end of an
2698 enumerator-definition. */
2699 next_token->type != CPP_COMMA
2700 /* The curly brace at the end of an enum-specifier. */
2701 && next_token->type != CPP_CLOSE_BRACE
2702 /* The end of a statement. */
2703 && next_token->type != CPP_SEMICOLON
2704 /* The end of the cast-expression. */
2705 && next_token->type != CPP_CLOSE_PAREN
2706 /* The end of an array bound. */
2707 && next_token->type != CPP_CLOSE_SQUARE)
2711 /* If we are within a cast, then the constraint that the
2712 cast is to an integral or enumeration type will be
2713 checked at that point. If we are not within a cast, then
2714 this code is invalid. */
2716 cp_parser_non_integral_constant_expression
2717 (parser, "floating-point literal");
2719 return token->value;
2723 /* ??? Should wide strings be allowed when parser->translate_strings_p
2724 is false (i.e. in attributes)? If not, we can kill the third
2725 argument to cp_parser_string_literal. */
2726 return cp_parser_string_literal (parser,
2727 parser->translate_strings_p,
2730 case CPP_OPEN_PAREN:
2733 bool saved_greater_than_is_operator_p;
2735 /* Consume the `('. */
2736 cp_lexer_consume_token (parser->lexer);
2737 /* Within a parenthesized expression, a `>' token is always
2738 the greater-than operator. */
2739 saved_greater_than_is_operator_p
2740 = parser->greater_than_is_operator_p;
2741 parser->greater_than_is_operator_p = true;
2742 /* If we see `( { ' then we are looking at the beginning of
2743 a GNU statement-expression. */
2744 if (cp_parser_allow_gnu_extensions_p (parser)
2745 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2747 /* Statement-expressions are not allowed by the standard. */
2749 pedwarn ("ISO C++ forbids braced-groups within expressions");
2751 /* And they're not allowed outside of a function-body; you
2752 cannot, for example, write:
2754 int i = ({ int j = 3; j + 1; });
2756 at class or namespace scope. */
2757 if (!at_function_scope_p ())
2758 error ("statement-expressions are allowed only inside functions");
2759 /* Start the statement-expression. */
2760 expr = begin_stmt_expr ();
2761 /* Parse the compound-statement. */
2762 cp_parser_compound_statement (parser, expr, false);
2764 expr = finish_stmt_expr (expr, false);
2768 /* Parse the parenthesized expression. */
2769 expr = cp_parser_expression (parser, cast_p);
2770 /* Let the front end know that this expression was
2771 enclosed in parentheses. This matters in case, for
2772 example, the expression is of the form `A::B', since
2773 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2775 finish_parenthesized_expr (expr);
2777 /* The `>' token might be the end of a template-id or
2778 template-parameter-list now. */
2779 parser->greater_than_is_operator_p
2780 = saved_greater_than_is_operator_p;
2781 /* Consume the `)'. */
2782 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2783 cp_parser_skip_to_end_of_statement (parser);
2789 switch (token->keyword)
2791 /* These two are the boolean literals. */
2793 cp_lexer_consume_token (parser->lexer);
2794 return boolean_true_node;
2796 cp_lexer_consume_token (parser->lexer);
2797 return boolean_false_node;
2799 /* The `__null' literal. */
2801 cp_lexer_consume_token (parser->lexer);
2804 /* Recognize the `this' keyword. */
2806 cp_lexer_consume_token (parser->lexer);
2807 if (parser->local_variables_forbidden_p)
2809 error ("%<this%> may not be used in this context");
2810 return error_mark_node;
2812 /* Pointers cannot appear in constant-expressions. */
2813 if (cp_parser_non_integral_constant_expression (parser,
2815 return error_mark_node;
2816 return finish_this_expr ();
2818 /* The `operator' keyword can be the beginning of an
2823 case RID_FUNCTION_NAME:
2824 case RID_PRETTY_FUNCTION_NAME:
2825 case RID_C99_FUNCTION_NAME:
2826 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2827 __func__ are the names of variables -- but they are
2828 treated specially. Therefore, they are handled here,
2829 rather than relying on the generic id-expression logic
2830 below. Grammatically, these names are id-expressions.
2832 Consume the token. */
2833 token = cp_lexer_consume_token (parser->lexer);
2834 /* Look up the name. */
2835 return finish_fname (token->value);
2842 /* The `__builtin_va_arg' construct is used to handle
2843 `va_arg'. Consume the `__builtin_va_arg' token. */
2844 cp_lexer_consume_token (parser->lexer);
2845 /* Look for the opening `('. */
2846 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2847 /* Now, parse the assignment-expression. */
2848 expression = cp_parser_assignment_expression (parser,
2850 /* Look for the `,'. */
2851 cp_parser_require (parser, CPP_COMMA, "`,'");
2852 /* Parse the type-id. */
2853 type = cp_parser_type_id (parser);
2854 /* Look for the closing `)'. */
2855 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2856 /* Using `va_arg' in a constant-expression is not
2858 if (cp_parser_non_integral_constant_expression (parser,
2860 return error_mark_node;
2861 return build_x_va_arg (expression, type);
2865 return cp_parser_builtin_offsetof (parser);
2868 cp_parser_error (parser, "expected primary-expression");
2869 return error_mark_node;
2872 /* An id-expression can start with either an identifier, a
2873 `::' as the beginning of a qualified-id, or the "operator"
2877 case CPP_TEMPLATE_ID:
2878 case CPP_NESTED_NAME_SPECIFIER:
2882 const char *error_msg;
2885 /* Parse the id-expression. */
2887 = cp_parser_id_expression (parser,
2888 /*template_keyword_p=*/false,
2889 /*check_dependency_p=*/true,
2890 /*template_p=*/NULL,
2891 /*declarator_p=*/false);
2892 if (id_expression == error_mark_node)
2893 return error_mark_node;
2894 /* If we have a template-id, then no further lookup is
2895 required. If the template-id was for a template-class, we
2896 will sometimes have a TYPE_DECL at this point. */
2897 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2898 || TREE_CODE (id_expression) == TYPE_DECL)
2899 decl = id_expression;
2900 /* Look up the name. */
2905 decl = cp_parser_lookup_name (parser, id_expression,
2907 /*is_template=*/false,
2908 /*is_namespace=*/false,
2909 /*check_dependency=*/true,
2911 /* If the lookup was ambiguous, an error will already have
2914 return error_mark_node;
2915 /* If name lookup gives us a SCOPE_REF, then the
2916 qualifying scope was dependent. Just propagate the
2918 if (TREE_CODE (decl) == SCOPE_REF)
2920 if (TYPE_P (TREE_OPERAND (decl, 0)))
2921 *qualifying_class = TREE_OPERAND (decl, 0);
2924 /* Check to see if DECL is a local variable in a context
2925 where that is forbidden. */
2926 if (parser->local_variables_forbidden_p
2927 && local_variable_p (decl))
2929 /* It might be that we only found DECL because we are
2930 trying to be generous with pre-ISO scoping rules.
2931 For example, consider:
2935 for (int i = 0; i < 10; ++i) {}
2936 extern void f(int j = i);
2939 Here, name look up will originally find the out
2940 of scope `i'. We need to issue a warning message,
2941 but then use the global `i'. */
2942 decl = check_for_out_of_scope_variable (decl);
2943 if (local_variable_p (decl))
2945 error ("local variable %qD may not appear in this context",
2947 return error_mark_node;
2952 decl = finish_id_expression (id_expression, decl, parser->scope,
2953 idk, qualifying_class,
2954 parser->integral_constant_expression_p,
2955 parser->allow_non_integral_constant_expression_p,
2956 &parser->non_integral_constant_expression_p,
2959 cp_parser_error (parser, error_msg);
2963 /* Anything else is an error. */
2965 cp_parser_error (parser, "expected primary-expression");
2966 return error_mark_node;
2970 /* Parse an id-expression.
2977 :: [opt] nested-name-specifier template [opt] unqualified-id
2979 :: operator-function-id
2982 Return a representation of the unqualified portion of the
2983 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2984 a `::' or nested-name-specifier.
2986 Often, if the id-expression was a qualified-id, the caller will
2987 want to make a SCOPE_REF to represent the qualified-id. This
2988 function does not do this in order to avoid wastefully creating
2989 SCOPE_REFs when they are not required.
2991 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2994 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2995 uninstantiated templates.
2997 If *TEMPLATE_P is non-NULL, it is set to true iff the
2998 `template' keyword is used to explicitly indicate that the entity
2999 named is a template.
3001 If DECLARATOR_P is true, the id-expression is appearing as part of
3002 a declarator, rather than as part of an expression. */
3005 cp_parser_id_expression (cp_parser *parser,
3006 bool template_keyword_p,
3007 bool check_dependency_p,
3011 bool global_scope_p;
3012 bool nested_name_specifier_p;
3014 /* Assume the `template' keyword was not used. */
3016 *template_p = false;
3018 /* Look for the optional `::' operator. */
3020 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3022 /* Look for the optional nested-name-specifier. */
3023 nested_name_specifier_p
3024 = (cp_parser_nested_name_specifier_opt (parser,
3025 /*typename_keyword_p=*/false,
3030 /* If there is a nested-name-specifier, then we are looking at
3031 the first qualified-id production. */
3032 if (nested_name_specifier_p)
3035 tree saved_object_scope;
3036 tree saved_qualifying_scope;
3037 tree unqualified_id;
3040 /* See if the next token is the `template' keyword. */
3042 template_p = &is_template;
3043 *template_p = cp_parser_optional_template_keyword (parser);
3044 /* Name lookup we do during the processing of the
3045 unqualified-id might obliterate SCOPE. */
3046 saved_scope = parser->scope;
3047 saved_object_scope = parser->object_scope;
3048 saved_qualifying_scope = parser->qualifying_scope;
3049 /* Process the final unqualified-id. */
3050 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3053 /* Restore the SAVED_SCOPE for our caller. */
3054 parser->scope = saved_scope;
3055 parser->object_scope = saved_object_scope;
3056 parser->qualifying_scope = saved_qualifying_scope;
3058 return unqualified_id;
3060 /* Otherwise, if we are in global scope, then we are looking at one
3061 of the other qualified-id productions. */
3062 else if (global_scope_p)
3067 /* Peek at the next token. */
3068 token = cp_lexer_peek_token (parser->lexer);
3070 /* If it's an identifier, and the next token is not a "<", then
3071 we can avoid the template-id case. This is an optimization
3072 for this common case. */
3073 if (token->type == CPP_NAME
3074 && !cp_parser_nth_token_starts_template_argument_list_p
3076 return cp_parser_identifier (parser);
3078 cp_parser_parse_tentatively (parser);
3079 /* Try a template-id. */
3080 id = cp_parser_template_id (parser,
3081 /*template_keyword_p=*/false,
3082 /*check_dependency_p=*/true,
3084 /* If that worked, we're done. */
3085 if (cp_parser_parse_definitely (parser))
3088 /* Peek at the next token. (Changes in the token buffer may
3089 have invalidated the pointer obtained above.) */
3090 token = cp_lexer_peek_token (parser->lexer);
3092 switch (token->type)
3095 return cp_parser_identifier (parser);
3098 if (token->keyword == RID_OPERATOR)
3099 return cp_parser_operator_function_id (parser);
3103 cp_parser_error (parser, "expected id-expression");
3104 return error_mark_node;
3108 return cp_parser_unqualified_id (parser, template_keyword_p,
3109 /*check_dependency_p=*/true,
3113 /* Parse an unqualified-id.
3117 operator-function-id
3118 conversion-function-id
3122 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3123 keyword, in a construct like `A::template ...'.
3125 Returns a representation of unqualified-id. For the `identifier'
3126 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3127 production a BIT_NOT_EXPR is returned; the operand of the
3128 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3129 other productions, see the documentation accompanying the
3130 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3131 names are looked up in uninstantiated templates. If DECLARATOR_P
3132 is true, the unqualified-id is appearing as part of a declarator,
3133 rather than as part of an expression. */
3136 cp_parser_unqualified_id (cp_parser* parser,
3137 bool template_keyword_p,
3138 bool check_dependency_p,
3143 /* Peek at the next token. */
3144 token = cp_lexer_peek_token (parser->lexer);
3146 switch (token->type)
3152 /* We don't know yet whether or not this will be a
3154 cp_parser_parse_tentatively (parser);
3155 /* Try a template-id. */
3156 id = cp_parser_template_id (parser, template_keyword_p,
3159 /* If it worked, we're done. */
3160 if (cp_parser_parse_definitely (parser))
3162 /* Otherwise, it's an ordinary identifier. */
3163 return cp_parser_identifier (parser);
3166 case CPP_TEMPLATE_ID:
3167 return cp_parser_template_id (parser, template_keyword_p,
3174 tree qualifying_scope;
3179 /* Consume the `~' token. */
3180 cp_lexer_consume_token (parser->lexer);
3181 /* Parse the class-name. The standard, as written, seems to
3184 template <typename T> struct S { ~S (); };
3185 template <typename T> S<T>::~S() {}
3187 is invalid, since `~' must be followed by a class-name, but
3188 `S<T>' is dependent, and so not known to be a class.
3189 That's not right; we need to look in uninstantiated
3190 templates. A further complication arises from:
3192 template <typename T> void f(T t) {
3196 Here, it is not possible to look up `T' in the scope of `T'
3197 itself. We must look in both the current scope, and the
3198 scope of the containing complete expression.
3200 Yet another issue is:
3209 The standard does not seem to say that the `S' in `~S'
3210 should refer to the type `S' and not the data member
3213 /* DR 244 says that we look up the name after the "~" in the
3214 same scope as we looked up the qualifying name. That idea
3215 isn't fully worked out; it's more complicated than that. */
3216 scope = parser->scope;
3217 object_scope = parser->object_scope;
3218 qualifying_scope = parser->qualifying_scope;
3220 /* If the name is of the form "X::~X" it's OK. */
3221 if (scope && TYPE_P (scope)
3222 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3223 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3225 && (cp_lexer_peek_token (parser->lexer)->value
3226 == TYPE_IDENTIFIER (scope)))
3228 cp_lexer_consume_token (parser->lexer);
3229 return build_nt (BIT_NOT_EXPR, scope);
3232 /* If there was an explicit qualification (S::~T), first look
3233 in the scope given by the qualification (i.e., S). */
3235 type_decl = NULL_TREE;
3238 cp_parser_parse_tentatively (parser);
3239 type_decl = cp_parser_class_name (parser,
3240 /*typename_keyword_p=*/false,
3241 /*template_keyword_p=*/false,
3243 /*check_dependency=*/false,
3244 /*class_head_p=*/false,
3246 if (cp_parser_parse_definitely (parser))
3249 /* In "N::S::~S", look in "N" as well. */
3250 if (!done && scope && qualifying_scope)
3252 cp_parser_parse_tentatively (parser);
3253 parser->scope = qualifying_scope;
3254 parser->object_scope = NULL_TREE;
3255 parser->qualifying_scope = NULL_TREE;
3257 = cp_parser_class_name (parser,
3258 /*typename_keyword_p=*/false,
3259 /*template_keyword_p=*/false,
3261 /*check_dependency=*/false,
3262 /*class_head_p=*/false,
3264 if (cp_parser_parse_definitely (parser))
3267 /* In "p->S::~T", look in the scope given by "*p" as well. */
3268 else if (!done && object_scope)
3270 cp_parser_parse_tentatively (parser);
3271 parser->scope = object_scope;
3272 parser->object_scope = NULL_TREE;
3273 parser->qualifying_scope = NULL_TREE;
3275 = cp_parser_class_name (parser,
3276 /*typename_keyword_p=*/false,
3277 /*template_keyword_p=*/false,
3279 /*check_dependency=*/false,
3280 /*class_head_p=*/false,
3282 if (cp_parser_parse_definitely (parser))
3285 /* Look in the surrounding context. */
3288 parser->scope = NULL_TREE;
3289 parser->object_scope = NULL_TREE;
3290 parser->qualifying_scope = NULL_TREE;
3292 = cp_parser_class_name (parser,
3293 /*typename_keyword_p=*/false,
3294 /*template_keyword_p=*/false,
3296 /*check_dependency=*/false,
3297 /*class_head_p=*/false,
3300 /* If an error occurred, assume that the name of the
3301 destructor is the same as the name of the qualifying
3302 class. That allows us to keep parsing after running
3303 into ill-formed destructor names. */
3304 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3305 return build_nt (BIT_NOT_EXPR, scope);
3306 else if (type_decl == error_mark_node)
3307 return error_mark_node;
3311 A typedef-name that names a class shall not be used as the
3312 identifier in the declarator for a destructor declaration. */
3314 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3315 && !DECL_SELF_REFERENCE_P (type_decl)
3316 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3317 error ("typedef-name %qD used as destructor declarator",
3320 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3324 if (token->keyword == RID_OPERATOR)
3328 /* This could be a template-id, so we try that first. */
3329 cp_parser_parse_tentatively (parser);
3330 /* Try a template-id. */
3331 id = cp_parser_template_id (parser, template_keyword_p,
3332 /*check_dependency_p=*/true,
3334 /* If that worked, we're done. */
3335 if (cp_parser_parse_definitely (parser))
3337 /* We still don't know whether we're looking at an
3338 operator-function-id or a conversion-function-id. */
3339 cp_parser_parse_tentatively (parser);
3340 /* Try an operator-function-id. */
3341 id = cp_parser_operator_function_id (parser);
3342 /* If that didn't work, try a conversion-function-id. */
3343 if (!cp_parser_parse_definitely (parser))
3344 id = cp_parser_conversion_function_id (parser);
3351 cp_parser_error (parser, "expected unqualified-id");
3352 return error_mark_node;
3356 /* Parse an (optional) nested-name-specifier.
3358 nested-name-specifier:
3359 class-or-namespace-name :: nested-name-specifier [opt]
3360 class-or-namespace-name :: template nested-name-specifier [opt]
3362 PARSER->SCOPE should be set appropriately before this function is
3363 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3364 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3367 Sets PARSER->SCOPE to the class (TYPE) or namespace
3368 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3369 it unchanged if there is no nested-name-specifier. Returns the new
3370 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3372 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3373 part of a declaration and/or decl-specifier. */
3376 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3377 bool typename_keyword_p,
3378 bool check_dependency_p,
3380 bool is_declaration)
3382 bool success = false;
3383 tree access_check = NULL_TREE;
3384 cp_token_position start = 0;
3387 /* If the next token corresponds to a nested name specifier, there
3388 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3389 false, it may have been true before, in which case something
3390 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3391 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3392 CHECK_DEPENDENCY_P is false, we have to fall through into the
3394 if (check_dependency_p
3395 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3397 cp_parser_pre_parsed_nested_name_specifier (parser);
3398 return parser->scope;
3401 /* Remember where the nested-name-specifier starts. */
3402 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3403 start = cp_lexer_token_position (parser->lexer, false);
3405 push_deferring_access_checks (dk_deferred);
3411 tree saved_qualifying_scope;
3412 bool template_keyword_p;
3414 /* Spot cases that cannot be the beginning of a
3415 nested-name-specifier. */
3416 token = cp_lexer_peek_token (parser->lexer);
3418 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3419 the already parsed nested-name-specifier. */
3420 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3422 /* Grab the nested-name-specifier and continue the loop. */
3423 cp_parser_pre_parsed_nested_name_specifier (parser);
3428 /* Spot cases that cannot be the beginning of a
3429 nested-name-specifier. On the second and subsequent times
3430 through the loop, we look for the `template' keyword. */
3431 if (success && token->keyword == RID_TEMPLATE)
3433 /* A template-id can start a nested-name-specifier. */
3434 else if (token->type == CPP_TEMPLATE_ID)
3438 /* If the next token is not an identifier, then it is
3439 definitely not a class-or-namespace-name. */
3440 if (token->type != CPP_NAME)
3442 /* If the following token is neither a `<' (to begin a
3443 template-id), nor a `::', then we are not looking at a
3444 nested-name-specifier. */
3445 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3446 if (token->type != CPP_SCOPE
3447 && !cp_parser_nth_token_starts_template_argument_list_p
3452 /* The nested-name-specifier is optional, so we parse
3454 cp_parser_parse_tentatively (parser);
3456 /* Look for the optional `template' keyword, if this isn't the
3457 first time through the loop. */
3459 template_keyword_p = cp_parser_optional_template_keyword (parser);
3461 template_keyword_p = false;
3463 /* Save the old scope since the name lookup we are about to do
3464 might destroy it. */
3465 old_scope = parser->scope;
3466 saved_qualifying_scope = parser->qualifying_scope;
3467 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3468 look up names in "X<T>::I" in order to determine that "Y" is
3469 a template. So, if we have a typename at this point, we make
3470 an effort to look through it. */
3472 && !typename_keyword_p
3474 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3475 parser->scope = resolve_typename_type (parser->scope,
3476 /*only_current_p=*/false);
3477 /* Parse the qualifying entity. */
3479 = cp_parser_class_or_namespace_name (parser,
3485 /* Look for the `::' token. */
3486 cp_parser_require (parser, CPP_SCOPE, "`::'");
3488 /* If we found what we wanted, we keep going; otherwise, we're
3490 if (!cp_parser_parse_definitely (parser))
3492 bool error_p = false;
3494 /* Restore the OLD_SCOPE since it was valid before the
3495 failed attempt at finding the last
3496 class-or-namespace-name. */
3497 parser->scope = old_scope;
3498 parser->qualifying_scope = saved_qualifying_scope;
3499 /* If the next token is an identifier, and the one after
3500 that is a `::', then any valid interpretation would have
3501 found a class-or-namespace-name. */
3502 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3503 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3505 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3508 token = cp_lexer_consume_token (parser->lexer);
3513 decl = cp_parser_lookup_name_simple (parser, token->value);
3514 if (TREE_CODE (decl) == TEMPLATE_DECL)
3515 error ("%qD used without template parameters", decl);
3517 cp_parser_name_lookup_error
3518 (parser, token->value, decl,
3519 "is not a class or namespace");
3520 parser->scope = NULL_TREE;
3522 /* Treat this as a successful nested-name-specifier
3527 If the name found is not a class-name (clause
3528 _class_) or namespace-name (_namespace.def_), the
3529 program is ill-formed. */
3532 cp_lexer_consume_token (parser->lexer);
3537 /* We've found one valid nested-name-specifier. */
3539 /* Make sure we look in the right scope the next time through
3541 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3542 ? TREE_TYPE (new_scope)
3544 /* If it is a class scope, try to complete it; we are about to
3545 be looking up names inside the class. */
3546 if (TYPE_P (parser->scope)
3547 /* Since checking types for dependency can be expensive,
3548 avoid doing it if the type is already complete. */
3549 && !COMPLETE_TYPE_P (parser->scope)
3550 /* Do not try to complete dependent types. */
3551 && !dependent_type_p (parser->scope))
3552 complete_type (parser->scope);
3555 /* Retrieve any deferred checks. Do not pop this access checks yet
3556 so the memory will not be reclaimed during token replacing below. */
3557 access_check = get_deferred_access_checks ();
3559 /* If parsing tentatively, replace the sequence of tokens that makes
3560 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3561 token. That way, should we re-parse the token stream, we will
3562 not have to repeat the effort required to do the parse, nor will
3563 we issue duplicate error messages. */
3564 if (success && start)
3566 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3568 /* Reset the contents of the START token. */
3569 token->type = CPP_NESTED_NAME_SPECIFIER;
3570 token->value = build_tree_list (access_check, parser->scope);
3571 TREE_TYPE (token->value) = parser->qualifying_scope;
3572 token->keyword = RID_MAX;
3574 /* Purge all subsequent tokens. */
3575 cp_lexer_purge_tokens_after (parser->lexer, start);
3578 pop_deferring_access_checks ();
3579 return success ? parser->scope : NULL_TREE;
3582 /* Parse a nested-name-specifier. See
3583 cp_parser_nested_name_specifier_opt for details. This function
3584 behaves identically, except that it will an issue an error if no
3585 nested-name-specifier is present, and it will return
3586 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3590 cp_parser_nested_name_specifier (cp_parser *parser,
3591 bool typename_keyword_p,
3592 bool check_dependency_p,
3594 bool is_declaration)
3598 /* Look for the nested-name-specifier. */
3599 scope = cp_parser_nested_name_specifier_opt (parser,
3604 /* If it was not present, issue an error message. */
3607 cp_parser_error (parser, "expected nested-name-specifier");
3608 parser->scope = NULL_TREE;
3609 return error_mark_node;
3615 /* Parse a class-or-namespace-name.
3617 class-or-namespace-name:
3621 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3622 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3623 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3624 TYPE_P is TRUE iff the next name should be taken as a class-name,
3625 even the same name is declared to be another entity in the same
3628 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3629 specified by the class-or-namespace-name. If neither is found the
3630 ERROR_MARK_NODE is returned. */
3633 cp_parser_class_or_namespace_name (cp_parser *parser,
3634 bool typename_keyword_p,
3635 bool template_keyword_p,
3636 bool check_dependency_p,
3638 bool is_declaration)
3641 tree saved_qualifying_scope;
3642 tree saved_object_scope;
3646 /* Before we try to parse the class-name, we must save away the
3647 current PARSER->SCOPE since cp_parser_class_name will destroy
3649 saved_scope = parser->scope;
3650 saved_qualifying_scope = parser->qualifying_scope;
3651 saved_object_scope = parser->object_scope;
3652 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3653 there is no need to look for a namespace-name. */
3654 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3656 cp_parser_parse_tentatively (parser);
3657 scope = cp_parser_class_name (parser,
3660 type_p ? class_type : none_type,
3662 /*class_head_p=*/false,
3664 /* If that didn't work, try for a namespace-name. */
3665 if (!only_class_p && !cp_parser_parse_definitely (parser))
3667 /* Restore the saved scope. */
3668 parser->scope = saved_scope;
3669 parser->qualifying_scope = saved_qualifying_scope;
3670 parser->object_scope = saved_object_scope;
3671 /* If we are not looking at an identifier followed by the scope
3672 resolution operator, then this is not part of a
3673 nested-name-specifier. (Note that this function is only used
3674 to parse the components of a nested-name-specifier.) */
3675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3676 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3677 return error_mark_node;
3678 scope = cp_parser_namespace_name (parser);
3684 /* Parse a postfix-expression.
3688 postfix-expression [ expression ]
3689 postfix-expression ( expression-list [opt] )
3690 simple-type-specifier ( expression-list [opt] )
3691 typename :: [opt] nested-name-specifier identifier
3692 ( expression-list [opt] )
3693 typename :: [opt] nested-name-specifier template [opt] template-id
3694 ( expression-list [opt] )
3695 postfix-expression . template [opt] id-expression
3696 postfix-expression -> template [opt] id-expression
3697 postfix-expression . pseudo-destructor-name
3698 postfix-expression -> pseudo-destructor-name
3699 postfix-expression ++
3700 postfix-expression --
3701 dynamic_cast < type-id > ( expression )
3702 static_cast < type-id > ( expression )
3703 reinterpret_cast < type-id > ( expression )
3704 const_cast < type-id > ( expression )
3705 typeid ( expression )
3711 ( type-id ) { initializer-list , [opt] }
3713 This extension is a GNU version of the C99 compound-literal
3714 construct. (The C99 grammar uses `type-name' instead of `type-id',
3715 but they are essentially the same concept.)
3717 If ADDRESS_P is true, the postfix expression is the operand of the
3718 `&' operator. CAST_P is true if this expression is the target of a
3721 Returns a representation of the expression. */
3724 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3728 cp_id_kind idk = CP_ID_KIND_NONE;
3729 tree postfix_expression = NULL_TREE;
3730 /* Non-NULL only if the current postfix-expression can be used to
3731 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3732 class used to qualify the member. */
3733 tree qualifying_class = NULL_TREE;
3735 /* Peek at the next token. */
3736 token = cp_lexer_peek_token (parser->lexer);
3737 /* Some of the productions are determined by keywords. */
3738 keyword = token->keyword;
3748 const char *saved_message;
3750 /* All of these can be handled in the same way from the point
3751 of view of parsing. Begin by consuming the token
3752 identifying the cast. */
3753 cp_lexer_consume_token (parser->lexer);
3755 /* New types cannot be defined in the cast. */
3756 saved_message = parser->type_definition_forbidden_message;
3757 parser->type_definition_forbidden_message
3758 = "types may not be defined in casts";
3760 /* Look for the opening `<'. */
3761 cp_parser_require (parser, CPP_LESS, "`<'");
3762 /* Parse the type to which we are casting. */
3763 type = cp_parser_type_id (parser);
3764 /* Look for the closing `>'. */
3765 cp_parser_require (parser, CPP_GREATER, "`>'");
3766 /* Restore the old message. */
3767 parser->type_definition_forbidden_message = saved_message;
3769 /* And the expression which is being cast. */
3770 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3771 expression = cp_parser_expression (parser, /*cast_p=*/true);
3772 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3774 /* Only type conversions to integral or enumeration types
3775 can be used in constant-expressions. */
3776 if (parser->integral_constant_expression_p
3777 && !dependent_type_p (type)
3778 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3779 && (cp_parser_non_integral_constant_expression
3781 "a cast to a type other than an integral or "
3782 "enumeration type")))
3783 return error_mark_node;
3789 = build_dynamic_cast (type, expression);
3793 = build_static_cast (type, expression);
3797 = build_reinterpret_cast (type, expression);
3801 = build_const_cast (type, expression);
3812 const char *saved_message;
3813 bool saved_in_type_id_in_expr_p;
3815 /* Consume the `typeid' token. */
3816 cp_lexer_consume_token (parser->lexer);
3817 /* Look for the `(' token. */
3818 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3819 /* Types cannot be defined in a `typeid' expression. */
3820 saved_message = parser->type_definition_forbidden_message;
3821 parser->type_definition_forbidden_message
3822 = "types may not be defined in a `typeid\' expression";
3823 /* We can't be sure yet whether we're looking at a type-id or an
3825 cp_parser_parse_tentatively (parser);
3826 /* Try a type-id first. */
3827 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3828 parser->in_type_id_in_expr_p = true;
3829 type = cp_parser_type_id (parser);
3830 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3831 /* Look for the `)' token. Otherwise, we can't be sure that
3832 we're not looking at an expression: consider `typeid (int
3833 (3))', for example. */
3834 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3835 /* If all went well, simply lookup the type-id. */
3836 if (cp_parser_parse_definitely (parser))
3837 postfix_expression = get_typeid (type);
3838 /* Otherwise, fall back to the expression variant. */
3843 /* Look for an expression. */
3844 expression = cp_parser_expression (parser, /*cast_p=*/false);
3845 /* Compute its typeid. */
3846 postfix_expression = build_typeid (expression);
3847 /* Look for the `)' token. */
3848 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3850 /* `typeid' may not appear in an integral constant expression. */
3851 if (cp_parser_non_integral_constant_expression(parser,
3852 "`typeid' operator"))
3853 return error_mark_node;
3854 /* Restore the saved message. */
3855 parser->type_definition_forbidden_message = saved_message;
3861 bool template_p = false;
3865 /* Consume the `typename' token. */
3866 cp_lexer_consume_token (parser->lexer);
3867 /* Look for the optional `::' operator. */
3868 cp_parser_global_scope_opt (parser,
3869 /*current_scope_valid_p=*/false);
3870 /* Look for the nested-name-specifier. */
3871 cp_parser_nested_name_specifier (parser,
3872 /*typename_keyword_p=*/true,
3873 /*check_dependency_p=*/true,
3875 /*is_declaration=*/true);
3876 /* Look for the optional `template' keyword. */
3877 template_p = cp_parser_optional_template_keyword (parser);
3878 /* We don't know whether we're looking at a template-id or an
3880 cp_parser_parse_tentatively (parser);
3881 /* Try a template-id. */
3882 id = cp_parser_template_id (parser, template_p,
3883 /*check_dependency_p=*/true,
3884 /*is_declaration=*/true);
3885 /* If that didn't work, try an identifier. */
3886 if (!cp_parser_parse_definitely (parser))
3887 id = cp_parser_identifier (parser);
3888 /* If we look up a template-id in a non-dependent qualifying
3889 scope, there's no need to create a dependent type. */
3890 if (TREE_CODE (id) == TYPE_DECL
3891 && !dependent_type_p (parser->scope))
3892 type = TREE_TYPE (id);
3893 /* Create a TYPENAME_TYPE to represent the type to which the
3894 functional cast is being performed. */
3896 type = make_typename_type (parser->scope, id,
3900 postfix_expression = cp_parser_functional_cast (parser, type);
3908 /* If the next thing is a simple-type-specifier, we may be
3909 looking at a functional cast. We could also be looking at
3910 an id-expression. So, we try the functional cast, and if
3911 that doesn't work we fall back to the primary-expression. */
3912 cp_parser_parse_tentatively (parser);
3913 /* Look for the simple-type-specifier. */
3914 type = cp_parser_simple_type_specifier (parser,
3915 /*decl_specs=*/NULL,
3916 CP_PARSER_FLAGS_NONE);
3917 /* Parse the cast itself. */
3918 if (!cp_parser_error_occurred (parser))
3920 = cp_parser_functional_cast (parser, type);
3921 /* If that worked, we're done. */
3922 if (cp_parser_parse_definitely (parser))
3925 /* If the functional-cast didn't work out, try a
3926 compound-literal. */
3927 if (cp_parser_allow_gnu_extensions_p (parser)
3928 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3930 tree initializer_list = NULL_TREE;
3931 bool saved_in_type_id_in_expr_p;
3933 cp_parser_parse_tentatively (parser);
3934 /* Consume the `('. */
3935 cp_lexer_consume_token (parser->lexer);
3936 /* Parse the type. */
3937 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3938 parser->in_type_id_in_expr_p = true;
3939 type = cp_parser_type_id (parser);
3940 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3941 /* Look for the `)'. */
3942 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3943 /* Look for the `{'. */
3944 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3945 /* If things aren't going well, there's no need to
3947 if (!cp_parser_error_occurred (parser))
3949 bool non_constant_p;
3950 /* Parse the initializer-list. */
3952 = cp_parser_initializer_list (parser, &non_constant_p);
3953 /* Allow a trailing `,'. */
3954 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3955 cp_lexer_consume_token (parser->lexer);
3956 /* Look for the final `}'. */
3957 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3959 /* If that worked, we're definitely looking at a
3960 compound-literal expression. */
3961 if (cp_parser_parse_definitely (parser))
3963 /* Warn the user that a compound literal is not
3964 allowed in standard C++. */
3966 pedwarn ("ISO C++ forbids compound-literals");
3967 /* Form the representation of the compound-literal. */
3969 = finish_compound_literal (type, initializer_list);
3974 /* It must be a primary-expression. */
3975 postfix_expression = cp_parser_primary_expression (parser,
3983 /* If we were avoiding committing to the processing of a
3984 qualified-id until we knew whether or not we had a
3985 pointer-to-member, we now know. */
3986 if (qualifying_class)
3990 /* Peek at the next token. */
3991 token = cp_lexer_peek_token (parser->lexer);
3992 done = (token->type != CPP_OPEN_SQUARE
3993 && token->type != CPP_OPEN_PAREN
3994 && token->type != CPP_DOT
3995 && token->type != CPP_DEREF
3996 && token->type != CPP_PLUS_PLUS
3997 && token->type != CPP_MINUS_MINUS);
3999 postfix_expression = finish_qualified_id_expr (qualifying_class,
4004 return postfix_expression;
4007 /* Keep looping until the postfix-expression is complete. */
4010 if (idk == CP_ID_KIND_UNQUALIFIED
4011 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4012 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4013 /* It is not a Koenig lookup function call. */
4015 = unqualified_name_lookup_error (postfix_expression);
4017 /* Peek at the next token. */
4018 token = cp_lexer_peek_token (parser->lexer);
4020 switch (token->type)
4022 case CPP_OPEN_SQUARE:
4024 = cp_parser_postfix_open_square_expression (parser,
4027 idk = CP_ID_KIND_NONE;
4030 case CPP_OPEN_PAREN:
4031 /* postfix-expression ( expression-list [opt] ) */
4034 tree args = (cp_parser_parenthesized_expression_list
4037 /*non_constant_p=*/NULL));
4039 if (args == error_mark_node)
4041 postfix_expression = error_mark_node;
4045 /* Function calls are not permitted in
4046 constant-expressions. */
4047 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4048 && cp_parser_non_integral_constant_expression (parser,
4051 postfix_expression = error_mark_node;
4056 if (idk == CP_ID_KIND_UNQUALIFIED)
4058 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4064 = perform_koenig_lookup (postfix_expression, args);
4068 = unqualified_fn_lookup_error (postfix_expression);
4070 /* We do not perform argument-dependent lookup if
4071 normal lookup finds a non-function, in accordance
4072 with the expected resolution of DR 218. */
4073 else if (args && is_overloaded_fn (postfix_expression))
4075 tree fn = get_first_fn (postfix_expression);
4077 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4078 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4080 /* Only do argument dependent lookup if regular
4081 lookup does not find a set of member functions.
4082 [basic.lookup.koenig]/2a */
4083 if (!DECL_FUNCTION_MEMBER_P (fn))
4087 = perform_koenig_lookup (postfix_expression, args);
4092 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4094 tree instance = TREE_OPERAND (postfix_expression, 0);
4095 tree fn = TREE_OPERAND (postfix_expression, 1);
4097 if (processing_template_decl
4098 && (type_dependent_expression_p (instance)
4099 || (!BASELINK_P (fn)
4100 && TREE_CODE (fn) != FIELD_DECL)
4101 || type_dependent_expression_p (fn)
4102 || any_type_dependent_arguments_p (args)))
4105 = build_min_nt (CALL_EXPR, postfix_expression,
4110 if (BASELINK_P (fn))
4112 = (build_new_method_call
4113 (instance, fn, args, NULL_TREE,
4114 (idk == CP_ID_KIND_QUALIFIED
4115 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4118 = finish_call_expr (postfix_expression, args,
4119 /*disallow_virtual=*/false,
4120 /*koenig_p=*/false);
4122 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4123 || TREE_CODE (postfix_expression) == MEMBER_REF
4124 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4125 postfix_expression = (build_offset_ref_call_from_tree
4126 (postfix_expression, args));
4127 else if (idk == CP_ID_KIND_QUALIFIED)
4128 /* A call to a static class member, or a namespace-scope
4131 = finish_call_expr (postfix_expression, args,
4132 /*disallow_virtual=*/true,
4135 /* All other function calls. */
4137 = finish_call_expr (postfix_expression, args,
4138 /*disallow_virtual=*/false,
4141 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4142 idk = CP_ID_KIND_NONE;
4148 /* postfix-expression . template [opt] id-expression
4149 postfix-expression . pseudo-destructor-name
4150 postfix-expression -> template [opt] id-expression
4151 postfix-expression -> pseudo-destructor-name */
4153 /* Consume the `.' or `->' operator. */
4154 cp_lexer_consume_token (parser->lexer);
4157 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4163 /* postfix-expression ++ */
4164 /* Consume the `++' token. */
4165 cp_lexer_consume_token (parser->lexer);
4166 /* Generate a representation for the complete expression. */
4168 = finish_increment_expr (postfix_expression,
4169 POSTINCREMENT_EXPR);
4170 /* Increments may not appear in constant-expressions. */
4171 if (cp_parser_non_integral_constant_expression (parser,
4173 postfix_expression = error_mark_node;
4174 idk = CP_ID_KIND_NONE;
4177 case CPP_MINUS_MINUS:
4178 /* postfix-expression -- */
4179 /* Consume the `--' token. */
4180 cp_lexer_consume_token (parser->lexer);
4181 /* Generate a representation for the complete expression. */
4183 = finish_increment_expr (postfix_expression,
4184 POSTDECREMENT_EXPR);
4185 /* Decrements may not appear in constant-expressions. */
4186 if (cp_parser_non_integral_constant_expression (parser,
4188 postfix_expression = error_mark_node;
4189 idk = CP_ID_KIND_NONE;
4193 return postfix_expression;
4197 /* We should never get here. */
4199 return error_mark_node;
4202 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4203 by cp_parser_builtin_offsetof. We're looking for
4205 postfix-expression [ expression ]
4207 FOR_OFFSETOF is set if we're being called in that context, which
4208 changes how we deal with integer constant expressions. */
4211 cp_parser_postfix_open_square_expression (cp_parser *parser,
4212 tree postfix_expression,
4217 /* Consume the `[' token. */
4218 cp_lexer_consume_token (parser->lexer);
4220 /* Parse the index expression. */
4221 /* ??? For offsetof, there is a question of what to allow here. If
4222 offsetof is not being used in an integral constant expression context,
4223 then we *could* get the right answer by computing the value at runtime.
4224 If we are in an integral constant expression context, then we might
4225 could accept any constant expression; hard to say without analysis.
4226 Rather than open the barn door too wide right away, allow only integer
4227 constant expressions here. */
4229 index = cp_parser_constant_expression (parser, false, NULL);
4231 index = cp_parser_expression (parser, /*cast_p=*/false);
4233 /* Look for the closing `]'. */
4234 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4236 /* Build the ARRAY_REF. */
4237 postfix_expression = grok_array_decl (postfix_expression, index);
4239 /* When not doing offsetof, array references are not permitted in
4240 constant-expressions. */
4242 && (cp_parser_non_integral_constant_expression
4243 (parser, "an array reference")))
4244 postfix_expression = error_mark_node;
4246 return postfix_expression;
4249 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4250 by cp_parser_builtin_offsetof. We're looking for
4252 postfix-expression . template [opt] id-expression
4253 postfix-expression . pseudo-destructor-name
4254 postfix-expression -> template [opt] id-expression
4255 postfix-expression -> pseudo-destructor-name
4257 FOR_OFFSETOF is set if we're being called in that context. That sorta
4258 limits what of the above we'll actually accept, but nevermind.
4259 TOKEN_TYPE is the "." or "->" token, which will already have been
4260 removed from the stream. */
4263 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4264 enum cpp_ttype token_type,
4265 tree postfix_expression,
4266 bool for_offsetof, cp_id_kind *idk)
4271 bool pseudo_destructor_p;
4272 tree scope = NULL_TREE;
4274 /* If this is a `->' operator, dereference the pointer. */
4275 if (token_type == CPP_DEREF)
4276 postfix_expression = build_x_arrow (postfix_expression);
4277 /* Check to see whether or not the expression is type-dependent. */
4278 dependent_p = type_dependent_expression_p (postfix_expression);
4279 /* The identifier following the `->' or `.' is not qualified. */
4280 parser->scope = NULL_TREE;
4281 parser->qualifying_scope = NULL_TREE;
4282 parser->object_scope = NULL_TREE;
4283 *idk = CP_ID_KIND_NONE;
4284 /* Enter the scope corresponding to the type of the object
4285 given by the POSTFIX_EXPRESSION. */
4286 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4288 scope = TREE_TYPE (postfix_expression);
4289 /* According to the standard, no expression should ever have
4290 reference type. Unfortunately, we do not currently match
4291 the standard in this respect in that our internal representation
4292 of an expression may have reference type even when the standard
4293 says it does not. Therefore, we have to manually obtain the
4294 underlying type here. */
4295 scope = non_reference (scope);
4296 /* The type of the POSTFIX_EXPRESSION must be complete. */
4297 scope = complete_type_or_else (scope, NULL_TREE);
4298 /* Let the name lookup machinery know that we are processing a
4299 class member access expression. */
4300 parser->context->object_type = scope;
4301 /* If something went wrong, we want to be able to discern that case,
4302 as opposed to the case where there was no SCOPE due to the type
4303 of expression being dependent. */
4305 scope = error_mark_node;
4306 /* If the SCOPE was erroneous, make the various semantic analysis
4307 functions exit quickly -- and without issuing additional error
4309 if (scope == error_mark_node)
4310 postfix_expression = error_mark_node;
4313 /* Assume this expression is not a pseudo-destructor access. */
4314 pseudo_destructor_p = false;
4316 /* If the SCOPE is a scalar type, then, if this is a valid program,
4317 we must be looking at a pseudo-destructor-name. */
4318 if (scope && SCALAR_TYPE_P (scope))
4323 cp_parser_parse_tentatively (parser);
4324 /* Parse the pseudo-destructor-name. */
4326 cp_parser_pseudo_destructor_name (parser, &s, &type);
4327 if (cp_parser_parse_definitely (parser))
4329 pseudo_destructor_p = true;
4331 = finish_pseudo_destructor_expr (postfix_expression,
4332 s, TREE_TYPE (type));
4336 if (!pseudo_destructor_p)
4338 /* If the SCOPE is not a scalar type, we are looking at an
4339 ordinary class member access expression, rather than a
4340 pseudo-destructor-name. */
4341 template_p = cp_parser_optional_template_keyword (parser);
4342 /* Parse the id-expression. */
4343 name = cp_parser_id_expression (parser, template_p,
4344 /*check_dependency_p=*/true,
4345 /*template_p=*/NULL,
4346 /*declarator_p=*/false);
4347 /* In general, build a SCOPE_REF if the member name is qualified.
4348 However, if the name was not dependent and has already been
4349 resolved; there is no need to build the SCOPE_REF. For example;
4351 struct X { void f(); };
4352 template <typename T> void f(T* t) { t->X::f(); }
4354 Even though "t" is dependent, "X::f" is not and has been resolved
4355 to a BASELINK; there is no need to include scope information. */
4357 /* But we do need to remember that there was an explicit scope for
4358 virtual function calls. */
4360 *idk = CP_ID_KIND_QUALIFIED;
4362 /* If the name is a template-id that names a type, we will get a
4363 TYPE_DECL here. That is invalid code. */
4364 if (TREE_CODE (name) == TYPE_DECL)
4366 error ("invalid use of %qD", name);
4367 postfix_expression = error_mark_node;
4371 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4373 name = build_nt (SCOPE_REF, parser->scope, name);
4374 parser->scope = NULL_TREE;
4375 parser->qualifying_scope = NULL_TREE;
4376 parser->object_scope = NULL_TREE;
4378 if (scope && name && BASELINK_P (name))
4379 adjust_result_of_qualified_name_lookup
4380 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4382 = finish_class_member_access_expr (postfix_expression, name);
4386 /* We no longer need to look up names in the scope of the object on
4387 the left-hand side of the `.' or `->' operator. */
4388 parser->context->object_type = NULL_TREE;
4390 /* Outside of offsetof, these operators may not appear in
4391 constant-expressions. */
4393 && (cp_parser_non_integral_constant_expression
4394 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4395 postfix_expression = error_mark_node;
4397 return postfix_expression;
4400 /* Parse a parenthesized expression-list.
4403 assignment-expression
4404 expression-list, assignment-expression
4409 identifier, expression-list
4411 CAST_P is true if this expression is the target of a cast.
4413 Returns a TREE_LIST. The TREE_VALUE of each node is a
4414 representation of an assignment-expression. Note that a TREE_LIST
4415 is returned even if there is only a single expression in the list.
4416 error_mark_node is returned if the ( and or ) are
4417 missing. NULL_TREE is returned on no expressions. The parentheses
4418 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4419 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4420 indicates whether or not all of the expressions in the list were
4424 cp_parser_parenthesized_expression_list (cp_parser* parser,
4425 bool is_attribute_list,
4427 bool *non_constant_p)
4429 tree expression_list = NULL_TREE;
4430 bool fold_expr_p = is_attribute_list;
4431 tree identifier = NULL_TREE;
4433 /* Assume all the expressions will be constant. */
4435 *non_constant_p = false;
4437 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4438 return error_mark_node;
4440 /* Consume expressions until there are no more. */
4441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4446 /* At the beginning of attribute lists, check to see if the
4447 next token is an identifier. */
4448 if (is_attribute_list
4449 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4453 /* Consume the identifier. */
4454 token = cp_lexer_consume_token (parser->lexer);
4455 /* Save the identifier. */
4456 identifier = token->value;
4460 /* Parse the next assignment-expression. */
4463 bool expr_non_constant_p;
4464 expr = (cp_parser_constant_expression
4465 (parser, /*allow_non_constant_p=*/true,
4466 &expr_non_constant_p));
4467 if (expr_non_constant_p)
4468 *non_constant_p = true;
4471 expr = cp_parser_assignment_expression (parser, cast_p);
4474 expr = fold_non_dependent_expr (expr);
4476 /* Add it to the list. We add error_mark_node
4477 expressions to the list, so that we can still tell if
4478 the correct form for a parenthesized expression-list
4479 is found. That gives better errors. */
4480 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4482 if (expr == error_mark_node)
4486 /* After the first item, attribute lists look the same as
4487 expression lists. */
4488 is_attribute_list = false;
4491 /* If the next token isn't a `,', then we are done. */
4492 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4495 /* Otherwise, consume the `,' and keep going. */
4496 cp_lexer_consume_token (parser->lexer);
4499 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4504 /* We try and resync to an unnested comma, as that will give the
4505 user better diagnostics. */
4506 ending = cp_parser_skip_to_closing_parenthesis (parser,
4507 /*recovering=*/true,
4509 /*consume_paren=*/true);
4513 return error_mark_node;
4516 /* We built up the list in reverse order so we must reverse it now. */
4517 expression_list = nreverse (expression_list);
4519 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4521 return expression_list;
4524 /* Parse a pseudo-destructor-name.
4526 pseudo-destructor-name:
4527 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4528 :: [opt] nested-name-specifier template template-id :: ~ type-name
4529 :: [opt] nested-name-specifier [opt] ~ type-name
4531 If either of the first two productions is used, sets *SCOPE to the
4532 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4533 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4534 or ERROR_MARK_NODE if the parse fails. */
4537 cp_parser_pseudo_destructor_name (cp_parser* parser,
4541 bool nested_name_specifier_p;
4543 /* Assume that things will not work out. */
4544 *type = error_mark_node;
4546 /* Look for the optional `::' operator. */
4547 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4548 /* Look for the optional nested-name-specifier. */
4549 nested_name_specifier_p
4550 = (cp_parser_nested_name_specifier_opt (parser,
4551 /*typename_keyword_p=*/false,
4552 /*check_dependency_p=*/true,
4554 /*is_declaration=*/true)
4556 /* Now, if we saw a nested-name-specifier, we might be doing the
4557 second production. */
4558 if (nested_name_specifier_p
4559 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4561 /* Consume the `template' keyword. */
4562 cp_lexer_consume_token (parser->lexer);
4563 /* Parse the template-id. */
4564 cp_parser_template_id (parser,
4565 /*template_keyword_p=*/true,
4566 /*check_dependency_p=*/false,
4567 /*is_declaration=*/true);
4568 /* Look for the `::' token. */
4569 cp_parser_require (parser, CPP_SCOPE, "`::'");
4571 /* If the next token is not a `~', then there might be some
4572 additional qualification. */
4573 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4575 /* Look for the type-name. */
4576 *scope = TREE_TYPE (cp_parser_type_name (parser));
4578 if (*scope == error_mark_node)
4581 /* If we don't have ::~, then something has gone wrong. Since
4582 the only caller of this function is looking for something
4583 after `.' or `->' after a scalar type, most likely the
4584 program is trying to get a member of a non-aggregate
4586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4587 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4589 cp_parser_error (parser, "request for member of non-aggregate type");
4593 /* Look for the `::' token. */
4594 cp_parser_require (parser, CPP_SCOPE, "`::'");
4599 /* Look for the `~'. */
4600 cp_parser_require (parser, CPP_COMPL, "`~'");
4601 /* Look for the type-name again. We are not responsible for
4602 checking that it matches the first type-name. */
4603 *type = cp_parser_type_name (parser);
4606 /* Parse a unary-expression.
4612 unary-operator cast-expression
4613 sizeof unary-expression
4621 __extension__ cast-expression
4622 __alignof__ unary-expression
4623 __alignof__ ( type-id )
4624 __real__ cast-expression
4625 __imag__ cast-expression
4628 ADDRESS_P is true iff the unary-expression is appearing as the
4629 operand of the `&' operator. CAST_P is true if this expression is
4630 the target of a cast.
4632 Returns a representation of the expression. */
4635 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4638 enum tree_code unary_operator;
4640 /* Peek at the next token. */
4641 token = cp_lexer_peek_token (parser->lexer);
4642 /* Some keywords give away the kind of expression. */
4643 if (token->type == CPP_KEYWORD)
4645 enum rid keyword = token->keyword;
4655 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4656 /* Consume the token. */
4657 cp_lexer_consume_token (parser->lexer);
4658 /* Parse the operand. */
4659 operand = cp_parser_sizeof_operand (parser, keyword);
4661 if (TYPE_P (operand))
4662 return cxx_sizeof_or_alignof_type (operand, op, true);
4664 return cxx_sizeof_or_alignof_expr (operand, op);
4668 return cp_parser_new_expression (parser);
4671 return cp_parser_delete_expression (parser);
4675 /* The saved value of the PEDANTIC flag. */
4679 /* Save away the PEDANTIC flag. */
4680 cp_parser_extension_opt (parser, &saved_pedantic);
4681 /* Parse the cast-expression. */
4682 expr = cp_parser_simple_cast_expression (parser);
4683 /* Restore the PEDANTIC flag. */
4684 pedantic = saved_pedantic;
4694 /* Consume the `__real__' or `__imag__' token. */
4695 cp_lexer_consume_token (parser->lexer);
4696 /* Parse the cast-expression. */
4697 expression = cp_parser_simple_cast_expression (parser);
4698 /* Create the complete representation. */
4699 return build_x_unary_op ((keyword == RID_REALPART
4700 ? REALPART_EXPR : IMAGPART_EXPR),
4710 /* Look for the `:: new' and `:: delete', which also signal the
4711 beginning of a new-expression, or delete-expression,
4712 respectively. If the next token is `::', then it might be one of
4714 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4718 /* See if the token after the `::' is one of the keywords in
4719 which we're interested. */
4720 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4721 /* If it's `new', we have a new-expression. */
4722 if (keyword == RID_NEW)
4723 return cp_parser_new_expression (parser);
4724 /* Similarly, for `delete'. */
4725 else if (keyword == RID_DELETE)
4726 return cp_parser_delete_expression (parser);
4729 /* Look for a unary operator. */
4730 unary_operator = cp_parser_unary_operator (token);
4731 /* The `++' and `--' operators can be handled similarly, even though
4732 they are not technically unary-operators in the grammar. */
4733 if (unary_operator == ERROR_MARK)
4735 if (token->type == CPP_PLUS_PLUS)
4736 unary_operator = PREINCREMENT_EXPR;
4737 else if (token->type == CPP_MINUS_MINUS)
4738 unary_operator = PREDECREMENT_EXPR;
4739 /* Handle the GNU address-of-label extension. */
4740 else if (cp_parser_allow_gnu_extensions_p (parser)
4741 && token->type == CPP_AND_AND)
4745 /* Consume the '&&' token. */
4746 cp_lexer_consume_token (parser->lexer);
4747 /* Look for the identifier. */
4748 identifier = cp_parser_identifier (parser);
4749 /* Create an expression representing the address. */
4750 return finish_label_address_expr (identifier);
4753 if (unary_operator != ERROR_MARK)
4755 tree cast_expression;
4756 tree expression = error_mark_node;
4757 const char *non_constant_p = NULL;
4759 /* Consume the operator token. */
4760 token = cp_lexer_consume_token (parser->lexer);
4761 /* Parse the cast-expression. */
4763 = cp_parser_cast_expression (parser,
4764 unary_operator == ADDR_EXPR,
4766 /* Now, build an appropriate representation. */
4767 switch (unary_operator)
4770 non_constant_p = "`*'";
4771 expression = build_x_indirect_ref (cast_expression, "unary *");
4775 non_constant_p = "`&'";
4778 expression = build_x_unary_op (unary_operator, cast_expression);
4781 case PREINCREMENT_EXPR:
4782 case PREDECREMENT_EXPR:
4783 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4788 case TRUTH_NOT_EXPR:
4789 expression = finish_unary_op_expr (unary_operator, cast_expression);
4797 && cp_parser_non_integral_constant_expression (parser,
4799 expression = error_mark_node;
4804 return cp_parser_postfix_expression (parser, address_p, cast_p);
4807 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4808 unary-operator, the corresponding tree code is returned. */
4810 static enum tree_code
4811 cp_parser_unary_operator (cp_token* token)
4813 switch (token->type)
4816 return INDIRECT_REF;
4822 return CONVERT_EXPR;
4828 return TRUTH_NOT_EXPR;
4831 return BIT_NOT_EXPR;
4838 /* Parse a new-expression.
4841 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4842 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4844 Returns a representation of the expression. */
4847 cp_parser_new_expression (cp_parser* parser)
4849 bool global_scope_p;
4855 /* Look for the optional `::' operator. */
4857 = (cp_parser_global_scope_opt (parser,
4858 /*current_scope_valid_p=*/false)
4860 /* Look for the `new' operator. */
4861 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4862 /* There's no easy way to tell a new-placement from the
4863 `( type-id )' construct. */
4864 cp_parser_parse_tentatively (parser);
4865 /* Look for a new-placement. */
4866 placement = cp_parser_new_placement (parser);
4867 /* If that didn't work out, there's no new-placement. */
4868 if (!cp_parser_parse_definitely (parser))
4869 placement = NULL_TREE;
4871 /* If the next token is a `(', then we have a parenthesized
4873 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4875 /* Consume the `('. */
4876 cp_lexer_consume_token (parser->lexer);
4877 /* Parse the type-id. */
4878 type = cp_parser_type_id (parser);
4879 /* Look for the closing `)'. */
4880 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4881 /* There should not be a direct-new-declarator in this production,
4882 but GCC used to allowed this, so we check and emit a sensible error
4883 message for this case. */
4884 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4886 error ("array bound forbidden after parenthesized type-id");
4887 inform ("try removing the parentheses around the type-id");
4888 cp_parser_direct_new_declarator (parser);
4892 /* Otherwise, there must be a new-type-id. */
4894 type = cp_parser_new_type_id (parser, &nelts);
4896 /* If the next token is a `(', then we have a new-initializer. */
4897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4898 initializer = cp_parser_new_initializer (parser);
4900 initializer = NULL_TREE;
4902 /* A new-expression may not appear in an integral constant
4904 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4905 return error_mark_node;
4907 /* Create a representation of the new-expression. */
4908 return build_new (placement, type, nelts, initializer, global_scope_p);
4911 /* Parse a new-placement.
4916 Returns the same representation as for an expression-list. */
4919 cp_parser_new_placement (cp_parser* parser)
4921 tree expression_list;
4923 /* Parse the expression-list. */
4924 expression_list = (cp_parser_parenthesized_expression_list
4925 (parser, false, /*cast_p=*/false,
4926 /*non_constant_p=*/NULL));
4928 return expression_list;
4931 /* Parse a new-type-id.
4934 type-specifier-seq new-declarator [opt]
4936 Returns the TYPE allocated. If the new-type-id indicates an array
4937 type, *NELTS is set to the number of elements in the last array
4938 bound; the TYPE will not include the last array bound. */
4941 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4943 cp_decl_specifier_seq type_specifier_seq;
4944 cp_declarator *new_declarator;
4945 cp_declarator *declarator;
4946 cp_declarator *outer_declarator;
4947 const char *saved_message;
4950 /* The type-specifier sequence must not contain type definitions.
4951 (It cannot contain declarations of new types either, but if they
4952 are not definitions we will catch that because they are not
4954 saved_message = parser->type_definition_forbidden_message;
4955 parser->type_definition_forbidden_message
4956 = "types may not be defined in a new-type-id";
4957 /* Parse the type-specifier-seq. */
4958 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4959 /* Restore the old message. */
4960 parser->type_definition_forbidden_message = saved_message;
4961 /* Parse the new-declarator. */
4962 new_declarator = cp_parser_new_declarator_opt (parser);
4964 /* Determine the number of elements in the last array dimension, if
4967 /* Skip down to the last array dimension. */
4968 declarator = new_declarator;
4969 outer_declarator = NULL;
4970 while (declarator && (declarator->kind == cdk_pointer
4971 || declarator->kind == cdk_ptrmem))
4973 outer_declarator = declarator;
4974 declarator = declarator->declarator;
4977 && declarator->kind == cdk_array
4978 && declarator->declarator
4979 && declarator->declarator->kind == cdk_array)
4981 outer_declarator = declarator;
4982 declarator = declarator->declarator;
4985 if (declarator && declarator->kind == cdk_array)
4987 *nelts = declarator->u.array.bounds;
4988 if (*nelts == error_mark_node)
4989 *nelts = integer_one_node;
4991 if (outer_declarator)
4992 outer_declarator->declarator = declarator->declarator;
4994 new_declarator = NULL;
4997 type = groktypename (&type_specifier_seq, new_declarator);
4998 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5000 *nelts = array_type_nelts_top (type);
5001 type = TREE_TYPE (type);
5006 /* Parse an (optional) new-declarator.
5009 ptr-operator new-declarator [opt]
5010 direct-new-declarator
5012 Returns the declarator. */
5014 static cp_declarator *
5015 cp_parser_new_declarator_opt (cp_parser* parser)
5017 enum tree_code code;
5019 cp_cv_quals cv_quals;
5021 /* We don't know if there's a ptr-operator next, or not. */
5022 cp_parser_parse_tentatively (parser);
5023 /* Look for a ptr-operator. */
5024 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5025 /* If that worked, look for more new-declarators. */
5026 if (cp_parser_parse_definitely (parser))
5028 cp_declarator *declarator;
5030 /* Parse another optional declarator. */
5031 declarator = cp_parser_new_declarator_opt (parser);
5033 /* Create the representation of the declarator. */
5035 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5036 else if (code == INDIRECT_REF)
5037 declarator = make_pointer_declarator (cv_quals, declarator);
5039 declarator = make_reference_declarator (cv_quals, declarator);
5044 /* If the next token is a `[', there is a direct-new-declarator. */
5045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5046 return cp_parser_direct_new_declarator (parser);
5051 /* Parse a direct-new-declarator.
5053 direct-new-declarator:
5055 direct-new-declarator [constant-expression]
5059 static cp_declarator *
5060 cp_parser_direct_new_declarator (cp_parser* parser)
5062 cp_declarator *declarator = NULL;
5068 /* Look for the opening `['. */
5069 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5070 /* The first expression is not required to be constant. */
5073 expression = cp_parser_expression (parser, /*cast_p=*/false);
5074 /* The standard requires that the expression have integral
5075 type. DR 74 adds enumeration types. We believe that the
5076 real intent is that these expressions be handled like the
5077 expression in a `switch' condition, which also allows
5078 classes with a single conversion to integral or
5079 enumeration type. */
5080 if (!processing_template_decl)
5083 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5088 error ("expression in new-declarator must have integral "
5089 "or enumeration type");
5090 expression = error_mark_node;
5094 /* But all the other expressions must be. */
5097 = cp_parser_constant_expression (parser,
5098 /*allow_non_constant=*/false,
5100 /* Look for the closing `]'. */
5101 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5103 /* Add this bound to the declarator. */
5104 declarator = make_array_declarator (declarator, expression);
5106 /* If the next token is not a `[', then there are no more
5108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5115 /* Parse a new-initializer.
5118 ( expression-list [opt] )
5120 Returns a representation of the expression-list. If there is no
5121 expression-list, VOID_ZERO_NODE is returned. */
5124 cp_parser_new_initializer (cp_parser* parser)
5126 tree expression_list;
5128 expression_list = (cp_parser_parenthesized_expression_list
5129 (parser, false, /*cast_p=*/false,
5130 /*non_constant_p=*/NULL));
5131 if (!expression_list)
5132 expression_list = void_zero_node;
5134 return expression_list;
5137 /* Parse a delete-expression.
5140 :: [opt] delete cast-expression
5141 :: [opt] delete [ ] cast-expression
5143 Returns a representation of the expression. */
5146 cp_parser_delete_expression (cp_parser* parser)
5148 bool global_scope_p;
5152 /* Look for the optional `::' operator. */
5154 = (cp_parser_global_scope_opt (parser,
5155 /*current_scope_valid_p=*/false)
5157 /* Look for the `delete' keyword. */
5158 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5159 /* See if the array syntax is in use. */
5160 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5162 /* Consume the `[' token. */
5163 cp_lexer_consume_token (parser->lexer);
5164 /* Look for the `]' token. */
5165 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5166 /* Remember that this is the `[]' construct. */
5172 /* Parse the cast-expression. */
5173 expression = cp_parser_simple_cast_expression (parser);
5175 /* A delete-expression may not appear in an integral constant
5177 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5178 return error_mark_node;
5180 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5183 /* Parse a cast-expression.
5187 ( type-id ) cast-expression
5189 ADDRESS_P is true iff the unary-expression is appearing as the
5190 operand of the `&' operator. CAST_P is true if this expression is
5191 the target of a cast.
5193 Returns a representation of the expression. */
5196 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5198 /* If it's a `(', then we might be looking at a cast. */
5199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5201 tree type = NULL_TREE;
5202 tree expr = NULL_TREE;
5203 bool compound_literal_p;
5204 const char *saved_message;
5206 /* There's no way to know yet whether or not this is a cast.
5207 For example, `(int (3))' is a unary-expression, while `(int)
5208 3' is a cast. So, we resort to parsing tentatively. */
5209 cp_parser_parse_tentatively (parser);
5210 /* Types may not be defined in a cast. */
5211 saved_message = parser->type_definition_forbidden_message;
5212 parser->type_definition_forbidden_message
5213 = "types may not be defined in casts";
5214 /* Consume the `('. */
5215 cp_lexer_consume_token (parser->lexer);
5216 /* A very tricky bit is that `(struct S) { 3 }' is a
5217 compound-literal (which we permit in C++ as an extension).
5218 But, that construct is not a cast-expression -- it is a
5219 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5220 is legal; if the compound-literal were a cast-expression,
5221 you'd need an extra set of parentheses.) But, if we parse
5222 the type-id, and it happens to be a class-specifier, then we
5223 will commit to the parse at that point, because we cannot
5224 undo the action that is done when creating a new class. So,
5225 then we cannot back up and do a postfix-expression.
5227 Therefore, we scan ahead to the closing `)', and check to see
5228 if the token after the `)' is a `{'. If so, we are not
5229 looking at a cast-expression.
5231 Save tokens so that we can put them back. */
5232 cp_lexer_save_tokens (parser->lexer);
5233 /* Skip tokens until the next token is a closing parenthesis.
5234 If we find the closing `)', and the next token is a `{', then
5235 we are looking at a compound-literal. */
5237 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5238 /*consume_paren=*/true)
5239 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5240 /* Roll back the tokens we skipped. */
5241 cp_lexer_rollback_tokens (parser->lexer);
5242 /* If we were looking at a compound-literal, simulate an error
5243 so that the call to cp_parser_parse_definitely below will
5245 if (compound_literal_p)
5246 cp_parser_simulate_error (parser);
5249 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5250 parser->in_type_id_in_expr_p = true;
5251 /* Look for the type-id. */
5252 type = cp_parser_type_id (parser);
5253 /* Look for the closing `)'. */
5254 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5255 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5258 /* Restore the saved message. */
5259 parser->type_definition_forbidden_message = saved_message;
5261 /* If ok so far, parse the dependent expression. We cannot be
5262 sure it is a cast. Consider `(T ())'. It is a parenthesized
5263 ctor of T, but looks like a cast to function returning T
5264 without a dependent expression. */
5265 if (!cp_parser_error_occurred (parser))
5266 expr = cp_parser_cast_expression (parser,
5267 /*address_p=*/false,
5270 if (cp_parser_parse_definitely (parser))
5272 /* Warn about old-style casts, if so requested. */
5273 if (warn_old_style_cast
5274 && !in_system_header
5275 && !VOID_TYPE_P (type)
5276 && current_lang_name != lang_name_c)
5277 warning ("use of old-style cast");
5279 /* Only type conversions to integral or enumeration types
5280 can be used in constant-expressions. */
5281 if (parser->integral_constant_expression_p
5282 && !dependent_type_p (type)
5283 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5284 && (cp_parser_non_integral_constant_expression
5286 "a cast to a type other than an integral or "
5287 "enumeration type")))
5288 return error_mark_node;
5290 /* Perform the cast. */
5291 expr = build_c_cast (type, expr);
5296 /* If we get here, then it's not a cast, so it must be a
5297 unary-expression. */
5298 return cp_parser_unary_expression (parser, address_p, cast_p);
5301 /* Parse a binary expression of the general form:
5305 pm-expression .* cast-expression
5306 pm-expression ->* cast-expression
5308 multiplicative-expression:
5310 multiplicative-expression * pm-expression
5311 multiplicative-expression / pm-expression
5312 multiplicative-expression % pm-expression
5314 additive-expression:
5315 multiplicative-expression
5316 additive-expression + multiplicative-expression
5317 additive-expression - multiplicative-expression
5321 shift-expression << additive-expression
5322 shift-expression >> additive-expression
5324 relational-expression:
5326 relational-expression < shift-expression
5327 relational-expression > shift-expression
5328 relational-expression <= shift-expression
5329 relational-expression >= shift-expression
5333 relational-expression:
5334 relational-expression <? shift-expression
5335 relational-expression >? shift-expression
5337 equality-expression:
5338 relational-expression
5339 equality-expression == relational-expression
5340 equality-expression != relational-expression
5344 and-expression & equality-expression
5346 exclusive-or-expression:
5348 exclusive-or-expression ^ and-expression
5350 inclusive-or-expression:
5351 exclusive-or-expression
5352 inclusive-or-expression | exclusive-or-expression
5354 logical-and-expression:
5355 inclusive-or-expression
5356 logical-and-expression && inclusive-or-expression
5358 logical-or-expression:
5359 logical-and-expression
5360 logical-or-expression || logical-and-expression
5362 All these are implemented with a single function like:
5365 simple-cast-expression
5366 binary-expression <token> binary-expression
5368 CAST_P is true if this expression is the target of a cast.
5370 The binops_by_token map is used to get the tree codes for each <token> type.
5371 binary-expressions are associated according to a precedence table. */
5373 #define TOKEN_PRECEDENCE(token) \
5374 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5375 ? PREC_NOT_OPERATOR \
5376 : binops_by_token[token->type].prec)
5379 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5381 cp_parser_expression_stack stack;
5382 cp_parser_expression_stack_entry *sp = &stack[0];
5385 enum tree_code tree_type;
5386 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5389 /* Parse the first expression. */
5390 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5394 /* Get an operator token. */
5395 token = cp_lexer_peek_token (parser->lexer);
5396 new_prec = TOKEN_PRECEDENCE (token);
5398 /* Popping an entry off the stack means we completed a subexpression:
5399 - either we found a token which is not an operator (`>' where it is not
5400 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5401 will happen repeatedly;
5402 - or, we found an operator which has lower priority. This is the case
5403 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5405 if (new_prec <= prec)
5414 tree_type = binops_by_token[token->type].tree_type;
5416 /* We used the operator token. */
5417 cp_lexer_consume_token (parser->lexer);
5419 /* Extract another operand. It may be the RHS of this expression
5420 or the LHS of a new, higher priority expression. */
5421 rhs = cp_parser_simple_cast_expression (parser);
5423 /* Get another operator token. Look up its precedence to avoid
5424 building a useless (immediately popped) stack entry for common
5425 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5426 token = cp_lexer_peek_token (parser->lexer);
5427 lookahead_prec = TOKEN_PRECEDENCE (token);
5428 if (lookahead_prec > new_prec)
5430 /* ... and prepare to parse the RHS of the new, higher priority
5431 expression. Since precedence levels on the stack are
5432 monotonically increasing, we do not have to care about
5435 sp->tree_type = tree_type;
5440 new_prec = lookahead_prec;
5444 /* If the stack is not empty, we have parsed into LHS the right side
5445 (`4' in the example above) of an expression we had suspended.
5446 We can use the information on the stack to recover the LHS (`3')
5447 from the stack together with the tree code (`MULT_EXPR'), and
5448 the precedence of the higher level subexpression
5449 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5450 which will be used to actually build the additive expression. */
5453 tree_type = sp->tree_type;
5458 overloaded_p = false;
5459 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5461 /* If the binary operator required the use of an overloaded operator,
5462 then this expression cannot be an integral constant-expression.
5463 An overloaded operator can be used even if both operands are
5464 otherwise permissible in an integral constant-expression if at
5465 least one of the operands is of enumeration type. */
5468 && (cp_parser_non_integral_constant_expression
5469 (parser, "calls to overloaded operators")))
5470 return error_mark_node;
5477 /* Parse the `? expression : assignment-expression' part of a
5478 conditional-expression. The LOGICAL_OR_EXPR is the
5479 logical-or-expression that started the conditional-expression.
5480 Returns a representation of the entire conditional-expression.
5482 This routine is used by cp_parser_assignment_expression.
5484 ? expression : assignment-expression
5488 ? : assignment-expression */
5491 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5494 tree assignment_expr;
5496 /* Consume the `?' token. */
5497 cp_lexer_consume_token (parser->lexer);
5498 if (cp_parser_allow_gnu_extensions_p (parser)
5499 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5500 /* Implicit true clause. */
5503 /* Parse the expression. */
5504 expr = cp_parser_expression (parser, /*cast_p=*/false);
5506 /* The next token should be a `:'. */
5507 cp_parser_require (parser, CPP_COLON, "`:'");
5508 /* Parse the assignment-expression. */
5509 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5511 /* Build the conditional-expression. */
5512 return build_x_conditional_expr (logical_or_expr,
5517 /* Parse an assignment-expression.
5519 assignment-expression:
5520 conditional-expression
5521 logical-or-expression assignment-operator assignment_expression
5524 CAST_P is true if this expression is the target of a cast.
5526 Returns a representation for the expression. */
5529 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5533 /* If the next token is the `throw' keyword, then we're looking at
5534 a throw-expression. */
5535 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5536 expr = cp_parser_throw_expression (parser);
5537 /* Otherwise, it must be that we are looking at a
5538 logical-or-expression. */
5541 /* Parse the binary expressions (logical-or-expression). */
5542 expr = cp_parser_binary_expression (parser, cast_p);
5543 /* If the next token is a `?' then we're actually looking at a
5544 conditional-expression. */
5545 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5546 return cp_parser_question_colon_clause (parser, expr);
5549 enum tree_code assignment_operator;
5551 /* If it's an assignment-operator, we're using the second
5554 = cp_parser_assignment_operator_opt (parser);
5555 if (assignment_operator != ERROR_MARK)
5559 /* Parse the right-hand side of the assignment. */
5560 rhs = cp_parser_assignment_expression (parser, cast_p);
5561 /* An assignment may not appear in a
5562 constant-expression. */
5563 if (cp_parser_non_integral_constant_expression (parser,
5565 return error_mark_node;
5566 /* Build the assignment expression. */
5567 expr = build_x_modify_expr (expr,
5568 assignment_operator,
5577 /* Parse an (optional) assignment-operator.
5579 assignment-operator: one of
5580 = *= /= %= += -= >>= <<= &= ^= |=
5584 assignment-operator: one of
5587 If the next token is an assignment operator, the corresponding tree
5588 code is returned, and the token is consumed. For example, for
5589 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5590 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5591 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5592 operator, ERROR_MARK is returned. */
5594 static enum tree_code
5595 cp_parser_assignment_operator_opt (cp_parser* parser)
5600 /* Peek at the next toen. */
5601 token = cp_lexer_peek_token (parser->lexer);
5603 switch (token->type)
5614 op = TRUNC_DIV_EXPR;
5618 op = TRUNC_MOD_EXPR;
5658 /* Nothing else is an assignment operator. */
5662 /* If it was an assignment operator, consume it. */
5663 if (op != ERROR_MARK)
5664 cp_lexer_consume_token (parser->lexer);
5669 /* Parse an expression.
5672 assignment-expression
5673 expression , assignment-expression
5675 CAST_P is true if this expression is the target of a cast.
5677 Returns a representation of the expression. */
5680 cp_parser_expression (cp_parser* parser, bool cast_p)
5682 tree expression = NULL_TREE;
5686 tree assignment_expression;
5688 /* Parse the next assignment-expression. */
5689 assignment_expression
5690 = cp_parser_assignment_expression (parser, cast_p);
5691 /* If this is the first assignment-expression, we can just
5694 expression = assignment_expression;
5696 expression = build_x_compound_expr (expression,
5697 assignment_expression);
5698 /* If the next token is not a comma, then we are done with the
5700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5702 /* Consume the `,'. */
5703 cp_lexer_consume_token (parser->lexer);
5704 /* A comma operator cannot appear in a constant-expression. */
5705 if (cp_parser_non_integral_constant_expression (parser,
5706 "a comma operator"))
5707 expression = error_mark_node;
5713 /* Parse a constant-expression.
5715 constant-expression:
5716 conditional-expression
5718 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5719 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5720 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5721 is false, NON_CONSTANT_P should be NULL. */
5724 cp_parser_constant_expression (cp_parser* parser,
5725 bool allow_non_constant_p,
5726 bool *non_constant_p)
5728 bool saved_integral_constant_expression_p;
5729 bool saved_allow_non_integral_constant_expression_p;
5730 bool saved_non_integral_constant_expression_p;
5733 /* It might seem that we could simply parse the
5734 conditional-expression, and then check to see if it were
5735 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5736 one that the compiler can figure out is constant, possibly after
5737 doing some simplifications or optimizations. The standard has a
5738 precise definition of constant-expression, and we must honor
5739 that, even though it is somewhat more restrictive.
5745 is not a legal declaration, because `(2, 3)' is not a
5746 constant-expression. The `,' operator is forbidden in a
5747 constant-expression. However, GCC's constant-folding machinery
5748 will fold this operation to an INTEGER_CST for `3'. */
5750 /* Save the old settings. */
5751 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5752 saved_allow_non_integral_constant_expression_p
5753 = parser->allow_non_integral_constant_expression_p;
5754 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5755 /* We are now parsing a constant-expression. */
5756 parser->integral_constant_expression_p = true;
5757 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5758 parser->non_integral_constant_expression_p = false;
5759 /* Although the grammar says "conditional-expression", we parse an
5760 "assignment-expression", which also permits "throw-expression"
5761 and the use of assignment operators. In the case that
5762 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5763 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5764 actually essential that we look for an assignment-expression.
5765 For example, cp_parser_initializer_clauses uses this function to
5766 determine whether a particular assignment-expression is in fact
5768 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5769 /* Restore the old settings. */
5770 parser->integral_constant_expression_p
5771 = saved_integral_constant_expression_p;
5772 parser->allow_non_integral_constant_expression_p
5773 = saved_allow_non_integral_constant_expression_p;
5774 if (allow_non_constant_p)
5775 *non_constant_p = parser->non_integral_constant_expression_p;
5776 else if (parser->non_integral_constant_expression_p)
5777 expression = error_mark_node;
5778 parser->non_integral_constant_expression_p
5779 = saved_non_integral_constant_expression_p;
5784 /* Parse __builtin_offsetof.
5786 offsetof-expression:
5787 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5789 offsetof-member-designator:
5791 | offsetof-member-designator "." id-expression
5792 | offsetof-member-designator "[" expression "]"
5796 cp_parser_builtin_offsetof (cp_parser *parser)
5798 int save_ice_p, save_non_ice_p;
5802 /* We're about to accept non-integral-constant things, but will
5803 definitely yield an integral constant expression. Save and
5804 restore these values around our local parsing. */
5805 save_ice_p = parser->integral_constant_expression_p;
5806 save_non_ice_p = parser->non_integral_constant_expression_p;
5808 /* Consume the "__builtin_offsetof" token. */
5809 cp_lexer_consume_token (parser->lexer);
5810 /* Consume the opening `('. */
5811 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5812 /* Parse the type-id. */
5813 type = cp_parser_type_id (parser);
5814 /* Look for the `,'. */
5815 cp_parser_require (parser, CPP_COMMA, "`,'");
5817 /* Build the (type *)null that begins the traditional offsetof macro. */
5818 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5820 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5821 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5825 cp_token *token = cp_lexer_peek_token (parser->lexer);
5826 switch (token->type)
5828 case CPP_OPEN_SQUARE:
5829 /* offsetof-member-designator "[" expression "]" */
5830 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5834 /* offsetof-member-designator "." identifier */
5835 cp_lexer_consume_token (parser->lexer);
5836 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5840 case CPP_CLOSE_PAREN:
5841 /* Consume the ")" token. */
5842 cp_lexer_consume_token (parser->lexer);
5846 /* Error. We know the following require will fail, but
5847 that gives the proper error message. */
5848 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5849 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5850 expr = error_mark_node;
5856 /* If we're processing a template, we can't finish the semantics yet.
5857 Otherwise we can fold the entire expression now. */
5858 if (processing_template_decl)
5859 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5861 expr = fold_offsetof (expr);
5864 parser->integral_constant_expression_p = save_ice_p;
5865 parser->non_integral_constant_expression_p = save_non_ice_p;
5870 /* Statements [gram.stmt.stmt] */
5872 /* Parse a statement.
5876 expression-statement
5881 declaration-statement
5885 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5889 location_t statement_location;
5891 /* There is no statement yet. */
5892 statement = NULL_TREE;
5893 /* Peek at the next token. */
5894 token = cp_lexer_peek_token (parser->lexer);
5895 /* Remember the location of the first token in the statement. */
5896 statement_location = token->location;
5897 /* If this is a keyword, then that will often determine what kind of
5898 statement we have. */
5899 if (token->type == CPP_KEYWORD)
5901 enum rid keyword = token->keyword;
5907 statement = cp_parser_labeled_statement (parser,
5913 statement = cp_parser_selection_statement (parser);
5919 statement = cp_parser_iteration_statement (parser);
5926 statement = cp_parser_jump_statement (parser);
5930 statement = cp_parser_try_block (parser);
5934 /* It might be a keyword like `int' that can start a
5935 declaration-statement. */
5939 else if (token->type == CPP_NAME)
5941 /* If the next token is a `:', then we are looking at a
5942 labeled-statement. */
5943 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5944 if (token->type == CPP_COLON)
5945 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5947 /* Anything that starts with a `{' must be a compound-statement. */
5948 else if (token->type == CPP_OPEN_BRACE)
5949 statement = cp_parser_compound_statement (parser, NULL, false);
5950 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5951 a statement all its own. */
5952 else if (token->type == CPP_PRAGMA)
5954 cp_lexer_handle_pragma (parser->lexer);
5958 /* Everything else must be a declaration-statement or an
5959 expression-statement. Try for the declaration-statement
5960 first, unless we are looking at a `;', in which case we know that
5961 we have an expression-statement. */
5964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5966 cp_parser_parse_tentatively (parser);
5967 /* Try to parse the declaration-statement. */
5968 cp_parser_declaration_statement (parser);
5969 /* If that worked, we're done. */
5970 if (cp_parser_parse_definitely (parser))
5973 /* Look for an expression-statement instead. */
5974 statement = cp_parser_expression_statement (parser, in_statement_expr);
5977 /* Set the line number for the statement. */
5978 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5979 SET_EXPR_LOCATION (statement, statement_location);
5982 /* Parse a labeled-statement.
5985 identifier : statement
5986 case constant-expression : statement
5992 case constant-expression ... constant-expression : statement
5994 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5995 For an ordinary label, returns a LABEL_EXPR. */
5998 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6001 tree statement = error_mark_node;
6003 /* The next token should be an identifier. */
6004 token = cp_lexer_peek_token (parser->lexer);
6005 if (token->type != CPP_NAME
6006 && token->type != CPP_KEYWORD)
6008 cp_parser_error (parser, "expected labeled-statement");
6009 return error_mark_node;
6012 switch (token->keyword)
6019 /* Consume the `case' token. */
6020 cp_lexer_consume_token (parser->lexer);
6021 /* Parse the constant-expression. */
6022 expr = cp_parser_constant_expression (parser,
6023 /*allow_non_constant_p=*/false,
6026 ellipsis = cp_lexer_peek_token (parser->lexer);
6027 if (ellipsis->type == CPP_ELLIPSIS)
6029 /* Consume the `...' token. */
6030 cp_lexer_consume_token (parser->lexer);
6032 cp_parser_constant_expression (parser,
6033 /*allow_non_constant_p=*/false,
6035 /* We don't need to emit warnings here, as the common code
6036 will do this for us. */
6039 expr_hi = NULL_TREE;
6041 if (!parser->in_switch_statement_p)
6042 error ("case label %qE not within a switch statement", expr);
6044 statement = finish_case_label (expr, expr_hi);
6049 /* Consume the `default' token. */
6050 cp_lexer_consume_token (parser->lexer);
6051 if (!parser->in_switch_statement_p)
6052 error ("case label not within a switch statement");
6054 statement = finish_case_label (NULL_TREE, NULL_TREE);
6058 /* Anything else must be an ordinary label. */
6059 statement = finish_label_stmt (cp_parser_identifier (parser));
6063 /* Require the `:' token. */
6064 cp_parser_require (parser, CPP_COLON, "`:'");
6065 /* Parse the labeled statement. */
6066 cp_parser_statement (parser, in_statement_expr);
6068 /* Return the label, in the case of a `case' or `default' label. */
6072 /* Parse an expression-statement.
6074 expression-statement:
6077 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6078 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6079 indicates whether this expression-statement is part of an
6080 expression statement. */
6083 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6085 tree statement = NULL_TREE;
6087 /* If the next token is a ';', then there is no expression
6089 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6090 statement = cp_parser_expression (parser, /*cast_p=*/false);
6092 /* Consume the final `;'. */
6093 cp_parser_consume_semicolon_at_end_of_statement (parser);
6095 if (in_statement_expr
6096 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6097 /* This is the final expression statement of a statement
6099 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6101 statement = finish_expr_stmt (statement);
6108 /* Parse a compound-statement.
6111 { statement-seq [opt] }
6113 Returns a tree representing the statement. */
6116 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6121 /* Consume the `{'. */
6122 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6123 return error_mark_node;
6124 /* Begin the compound-statement. */
6125 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6126 /* Parse an (optional) statement-seq. */
6127 cp_parser_statement_seq_opt (parser, in_statement_expr);
6128 /* Finish the compound-statement. */
6129 finish_compound_stmt (compound_stmt);
6130 /* Consume the `}'. */
6131 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6133 return compound_stmt;
6136 /* Parse an (optional) statement-seq.
6140 statement-seq [opt] statement */
6143 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6145 /* Scan statements until there aren't any more. */
6148 /* If we're looking at a `}', then we've run out of statements. */
6149 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6150 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6153 /* Parse the statement. */
6154 cp_parser_statement (parser, in_statement_expr);
6158 /* Parse a selection-statement.
6160 selection-statement:
6161 if ( condition ) statement
6162 if ( condition ) statement else statement
6163 switch ( condition ) statement
6165 Returns the new IF_STMT or SWITCH_STMT. */
6168 cp_parser_selection_statement (cp_parser* parser)
6173 /* Peek at the next token. */
6174 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6176 /* See what kind of keyword it is. */
6177 keyword = token->keyword;
6186 /* Look for the `('. */
6187 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6189 cp_parser_skip_to_end_of_statement (parser);
6190 return error_mark_node;
6193 /* Begin the selection-statement. */
6194 if (keyword == RID_IF)
6195 statement = begin_if_stmt ();
6197 statement = begin_switch_stmt ();
6199 /* Parse the condition. */
6200 condition = cp_parser_condition (parser);
6201 /* Look for the `)'. */
6202 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6203 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6204 /*consume_paren=*/true);
6206 if (keyword == RID_IF)
6208 /* Add the condition. */
6209 finish_if_stmt_cond (condition, statement);
6211 /* Parse the then-clause. */
6212 cp_parser_implicitly_scoped_statement (parser);
6213 finish_then_clause (statement);
6215 /* If the next token is `else', parse the else-clause. */
6216 if (cp_lexer_next_token_is_keyword (parser->lexer,
6219 /* Consume the `else' keyword. */
6220 cp_lexer_consume_token (parser->lexer);
6221 begin_else_clause (statement);
6222 /* Parse the else-clause. */
6223 cp_parser_implicitly_scoped_statement (parser);
6224 finish_else_clause (statement);
6227 /* Now we're all done with the if-statement. */
6228 finish_if_stmt (statement);
6232 bool in_switch_statement_p;
6234 /* Add the condition. */
6235 finish_switch_cond (condition, statement);
6237 /* Parse the body of the switch-statement. */
6238 in_switch_statement_p = parser->in_switch_statement_p;
6239 parser->in_switch_statement_p = true;
6240 cp_parser_implicitly_scoped_statement (parser);
6241 parser->in_switch_statement_p = in_switch_statement_p;
6243 /* Now we're all done with the switch-statement. */
6244 finish_switch_stmt (statement);
6252 cp_parser_error (parser, "expected selection-statement");
6253 return error_mark_node;
6257 /* Parse a condition.
6261 type-specifier-seq declarator = assignment-expression
6266 type-specifier-seq declarator asm-specification [opt]
6267 attributes [opt] = assignment-expression
6269 Returns the expression that should be tested. */
6272 cp_parser_condition (cp_parser* parser)
6274 cp_decl_specifier_seq type_specifiers;
6275 const char *saved_message;
6277 /* Try the declaration first. */
6278 cp_parser_parse_tentatively (parser);
6279 /* New types are not allowed in the type-specifier-seq for a
6281 saved_message = parser->type_definition_forbidden_message;
6282 parser->type_definition_forbidden_message
6283 = "types may not be defined in conditions";
6284 /* Parse the type-specifier-seq. */
6285 cp_parser_type_specifier_seq (parser, &type_specifiers);
6286 /* Restore the saved message. */
6287 parser->type_definition_forbidden_message = saved_message;
6288 /* If all is well, we might be looking at a declaration. */
6289 if (!cp_parser_error_occurred (parser))
6292 tree asm_specification;
6294 cp_declarator *declarator;
6295 tree initializer = NULL_TREE;
6297 /* Parse the declarator. */
6298 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6299 /*ctor_dtor_or_conv_p=*/NULL,
6300 /*parenthesized_p=*/NULL,
6301 /*member_p=*/false);
6302 /* Parse the attributes. */
6303 attributes = cp_parser_attributes_opt (parser);
6304 /* Parse the asm-specification. */
6305 asm_specification = cp_parser_asm_specification_opt (parser);
6306 /* If the next token is not an `=', then we might still be
6307 looking at an expression. For example:
6311 looks like a decl-specifier-seq and a declarator -- but then
6312 there is no `=', so this is an expression. */
6313 cp_parser_require (parser, CPP_EQ, "`='");
6314 /* If we did see an `=', then we are looking at a declaration
6316 if (cp_parser_parse_definitely (parser))
6320 /* Create the declaration. */
6321 decl = start_decl (declarator, &type_specifiers,
6322 /*initialized_p=*/true,
6323 attributes, /*prefix_attributes=*/NULL_TREE,
6325 /* Parse the assignment-expression. */
6326 initializer = cp_parser_assignment_expression (parser,
6329 /* Process the initializer. */
6330 cp_finish_decl (decl,
6333 LOOKUP_ONLYCONVERTING);
6336 pop_scope (pushed_scope);
6338 return convert_from_reference (decl);
6341 /* If we didn't even get past the declarator successfully, we are
6342 definitely not looking at a declaration. */
6344 cp_parser_abort_tentative_parse (parser);
6346 /* Otherwise, we are looking at an expression. */
6347 return cp_parser_expression (parser, /*cast_p=*/false);
6350 /* Parse an iteration-statement.
6352 iteration-statement:
6353 while ( condition ) statement
6354 do statement while ( expression ) ;
6355 for ( for-init-statement condition [opt] ; expression [opt] )
6358 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6361 cp_parser_iteration_statement (cp_parser* parser)
6366 bool in_iteration_statement_p;
6369 /* Peek at the next token. */
6370 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6372 return error_mark_node;
6374 /* Remember whether or not we are already within an iteration
6376 in_iteration_statement_p = parser->in_iteration_statement_p;
6378 /* See what kind of keyword it is. */
6379 keyword = token->keyword;
6386 /* Begin the while-statement. */
6387 statement = begin_while_stmt ();
6388 /* Look for the `('. */
6389 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6390 /* Parse the condition. */
6391 condition = cp_parser_condition (parser);
6392 finish_while_stmt_cond (condition, statement);
6393 /* Look for the `)'. */
6394 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6395 /* Parse the dependent statement. */
6396 parser->in_iteration_statement_p = true;
6397 cp_parser_already_scoped_statement (parser);
6398 parser->in_iteration_statement_p = in_iteration_statement_p;
6399 /* We're done with the while-statement. */
6400 finish_while_stmt (statement);
6408 /* Begin the do-statement. */
6409 statement = begin_do_stmt ();
6410 /* Parse the body of the do-statement. */
6411 parser->in_iteration_statement_p = true;
6412 cp_parser_implicitly_scoped_statement (parser);
6413 parser->in_iteration_statement_p = in_iteration_statement_p;
6414 finish_do_body (statement);
6415 /* Look for the `while' keyword. */
6416 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6417 /* Look for the `('. */
6418 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6419 /* Parse the expression. */
6420 expression = cp_parser_expression (parser, /*cast_p=*/false);
6421 /* We're done with the do-statement. */
6422 finish_do_stmt (expression, statement);
6423 /* Look for the `)'. */
6424 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6425 /* Look for the `;'. */
6426 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6432 tree condition = NULL_TREE;
6433 tree expression = NULL_TREE;
6435 /* Begin the for-statement. */
6436 statement = begin_for_stmt ();
6437 /* Look for the `('. */
6438 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6439 /* Parse the initialization. */
6440 cp_parser_for_init_statement (parser);
6441 finish_for_init_stmt (statement);
6443 /* If there's a condition, process it. */
6444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6445 condition = cp_parser_condition (parser);
6446 finish_for_cond (condition, statement);
6447 /* Look for the `;'. */
6448 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6450 /* If there's an expression, process it. */
6451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6452 expression = cp_parser_expression (parser, /*cast_p=*/false);
6453 finish_for_expr (expression, statement);
6454 /* Look for the `)'. */
6455 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6457 /* Parse the body of the for-statement. */
6458 parser->in_iteration_statement_p = true;
6459 cp_parser_already_scoped_statement (parser);
6460 parser->in_iteration_statement_p = in_iteration_statement_p;
6462 /* We're done with the for-statement. */
6463 finish_for_stmt (statement);
6468 cp_parser_error (parser, "expected iteration-statement");
6469 statement = error_mark_node;
6476 /* Parse a for-init-statement.
6479 expression-statement
6480 simple-declaration */
6483 cp_parser_for_init_statement (cp_parser* parser)
6485 /* If the next token is a `;', then we have an empty
6486 expression-statement. Grammatically, this is also a
6487 simple-declaration, but an invalid one, because it does not
6488 declare anything. Therefore, if we did not handle this case
6489 specially, we would issue an error message about an invalid
6491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6493 /* We're going to speculatively look for a declaration, falling back
6494 to an expression, if necessary. */
6495 cp_parser_parse_tentatively (parser);
6496 /* Parse the declaration. */
6497 cp_parser_simple_declaration (parser,
6498 /*function_definition_allowed_p=*/false);
6499 /* If the tentative parse failed, then we shall need to look for an
6500 expression-statement. */
6501 if (cp_parser_parse_definitely (parser))
6505 cp_parser_expression_statement (parser, false);
6508 /* Parse a jump-statement.
6513 return expression [opt] ;
6521 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6524 cp_parser_jump_statement (cp_parser* parser)
6526 tree statement = error_mark_node;
6530 /* Peek at the next token. */
6531 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6533 return error_mark_node;
6535 /* See what kind of keyword it is. */
6536 keyword = token->keyword;
6540 if (!parser->in_switch_statement_p
6541 && !parser->in_iteration_statement_p)
6543 error ("break statement not within loop or switch");
6544 statement = error_mark_node;
6547 statement = finish_break_stmt ();
6548 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6552 if (!parser->in_iteration_statement_p)
6554 error ("continue statement not within a loop");
6555 statement = error_mark_node;
6558 statement = finish_continue_stmt ();
6559 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6566 /* If the next token is a `;', then there is no
6568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6569 expr = cp_parser_expression (parser, /*cast_p=*/false);
6572 /* Build the return-statement. */
6573 statement = finish_return_stmt (expr);
6574 /* Look for the final `;'. */
6575 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6580 /* Create the goto-statement. */
6581 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6583 /* Issue a warning about this use of a GNU extension. */
6585 pedwarn ("ISO C++ forbids computed gotos");
6586 /* Consume the '*' token. */
6587 cp_lexer_consume_token (parser->lexer);
6588 /* Parse the dependent expression. */
6589 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6592 finish_goto_stmt (cp_parser_identifier (parser));
6593 /* Look for the final `;'. */
6594 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6598 cp_parser_error (parser, "expected jump-statement");
6605 /* Parse a declaration-statement.
6607 declaration-statement:
6608 block-declaration */
6611 cp_parser_declaration_statement (cp_parser* parser)
6615 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6616 p = obstack_alloc (&declarator_obstack, 0);
6618 /* Parse the block-declaration. */
6619 cp_parser_block_declaration (parser, /*statement_p=*/true);
6621 /* Free any declarators allocated. */
6622 obstack_free (&declarator_obstack, p);
6624 /* Finish off the statement. */
6628 /* Some dependent statements (like `if (cond) statement'), are
6629 implicitly in their own scope. In other words, if the statement is
6630 a single statement (as opposed to a compound-statement), it is
6631 none-the-less treated as if it were enclosed in braces. Any
6632 declarations appearing in the dependent statement are out of scope
6633 after control passes that point. This function parses a statement,
6634 but ensures that is in its own scope, even if it is not a
6637 Returns the new statement. */
6640 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6644 /* If the token is not a `{', then we must take special action. */
6645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6647 /* Create a compound-statement. */
6648 statement = begin_compound_stmt (0);
6649 /* Parse the dependent-statement. */
6650 cp_parser_statement (parser, false);
6651 /* Finish the dummy compound-statement. */
6652 finish_compound_stmt (statement);
6654 /* Otherwise, we simply parse the statement directly. */
6656 statement = cp_parser_compound_statement (parser, NULL, false);
6658 /* Return the statement. */
6662 /* For some dependent statements (like `while (cond) statement'), we
6663 have already created a scope. Therefore, even if the dependent
6664 statement is a compound-statement, we do not want to create another
6668 cp_parser_already_scoped_statement (cp_parser* parser)
6670 /* If the token is a `{', then we must take special action. */
6671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6672 cp_parser_statement (parser, false);
6675 /* Avoid calling cp_parser_compound_statement, so that we
6676 don't create a new scope. Do everything else by hand. */
6677 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6678 cp_parser_statement_seq_opt (parser, false);
6679 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6683 /* Declarations [gram.dcl.dcl] */
6685 /* Parse an optional declaration-sequence.
6689 declaration-seq declaration */
6692 cp_parser_declaration_seq_opt (cp_parser* parser)
6698 token = cp_lexer_peek_token (parser->lexer);
6700 if (token->type == CPP_CLOSE_BRACE
6701 || token->type == CPP_EOF)
6704 if (token->type == CPP_SEMICOLON)
6706 /* A declaration consisting of a single semicolon is
6707 invalid. Allow it unless we're being pedantic. */
6708 cp_lexer_consume_token (parser->lexer);
6709 if (pedantic && !in_system_header)
6710 pedwarn ("extra %<;%>");
6714 /* If we're entering or exiting a region that's implicitly
6715 extern "C", modify the lang context appropriately. */
6716 if (!parser->implicit_extern_c && token->implicit_extern_c)
6718 push_lang_context (lang_name_c);
6719 parser->implicit_extern_c = true;
6721 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6723 pop_lang_context ();
6724 parser->implicit_extern_c = false;
6727 if (token->type == CPP_PRAGMA)
6729 /* A top-level declaration can consist solely of a #pragma.
6730 A nested declaration cannot, so this is done here and not
6731 in cp_parser_declaration. (A #pragma at block scope is
6732 handled in cp_parser_statement.) */
6733 cp_lexer_handle_pragma (parser->lexer);
6737 /* Parse the declaration itself. */
6738 cp_parser_declaration (parser);
6742 /* Parse a declaration.
6747 template-declaration
6748 explicit-instantiation
6749 explicit-specialization
6750 linkage-specification
6751 namespace-definition
6756 __extension__ declaration */
6759 cp_parser_declaration (cp_parser* parser)
6766 /* Check for the `__extension__' keyword. */
6767 if (cp_parser_extension_opt (parser, &saved_pedantic))
6769 /* Parse the qualified declaration. */
6770 cp_parser_declaration (parser);
6771 /* Restore the PEDANTIC flag. */
6772 pedantic = saved_pedantic;
6777 /* Try to figure out what kind of declaration is present. */
6778 token1 = *cp_lexer_peek_token (parser->lexer);
6780 if (token1.type != CPP_EOF)
6781 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6783 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6784 p = obstack_alloc (&declarator_obstack, 0);
6786 /* If the next token is `extern' and the following token is a string
6787 literal, then we have a linkage specification. */
6788 if (token1.keyword == RID_EXTERN
6789 && cp_parser_is_string_literal (&token2))
6790 cp_parser_linkage_specification (parser);
6791 /* If the next token is `template', then we have either a template
6792 declaration, an explicit instantiation, or an explicit
6794 else if (token1.keyword == RID_TEMPLATE)
6796 /* `template <>' indicates a template specialization. */
6797 if (token2.type == CPP_LESS
6798 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6799 cp_parser_explicit_specialization (parser);
6800 /* `template <' indicates a template declaration. */
6801 else if (token2.type == CPP_LESS)
6802 cp_parser_template_declaration (parser, /*member_p=*/false);
6803 /* Anything else must be an explicit instantiation. */
6805 cp_parser_explicit_instantiation (parser);
6807 /* If the next token is `export', then we have a template
6809 else if (token1.keyword == RID_EXPORT)
6810 cp_parser_template_declaration (parser, /*member_p=*/false);
6811 /* If the next token is `extern', 'static' or 'inline' and the one
6812 after that is `template', we have a GNU extended explicit
6813 instantiation directive. */
6814 else if (cp_parser_allow_gnu_extensions_p (parser)
6815 && (token1.keyword == RID_EXTERN
6816 || token1.keyword == RID_STATIC
6817 || token1.keyword == RID_INLINE)
6818 && token2.keyword == RID_TEMPLATE)
6819 cp_parser_explicit_instantiation (parser);
6820 /* If the next token is `namespace', check for a named or unnamed
6821 namespace definition. */
6822 else if (token1.keyword == RID_NAMESPACE
6823 && (/* A named namespace definition. */
6824 (token2.type == CPP_NAME
6825 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6827 /* An unnamed namespace definition. */
6828 || token2.type == CPP_OPEN_BRACE))
6829 cp_parser_namespace_definition (parser);
6830 /* We must have either a block declaration or a function
6833 /* Try to parse a block-declaration, or a function-definition. */
6834 cp_parser_block_declaration (parser, /*statement_p=*/false);
6836 /* Free any declarators allocated. */
6837 obstack_free (&declarator_obstack, p);
6840 /* Parse a block-declaration.
6845 namespace-alias-definition
6852 __extension__ block-declaration
6855 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6856 part of a declaration-statement. */
6859 cp_parser_block_declaration (cp_parser *parser,
6865 /* Check for the `__extension__' keyword. */
6866 if (cp_parser_extension_opt (parser, &saved_pedantic))
6868 /* Parse the qualified declaration. */
6869 cp_parser_block_declaration (parser, statement_p);
6870 /* Restore the PEDANTIC flag. */
6871 pedantic = saved_pedantic;
6876 /* Peek at the next token to figure out which kind of declaration is
6878 token1 = cp_lexer_peek_token (parser->lexer);
6880 /* If the next keyword is `asm', we have an asm-definition. */
6881 if (token1->keyword == RID_ASM)
6884 cp_parser_commit_to_tentative_parse (parser);
6885 cp_parser_asm_definition (parser);
6887 /* If the next keyword is `namespace', we have a
6888 namespace-alias-definition. */
6889 else if (token1->keyword == RID_NAMESPACE)
6890 cp_parser_namespace_alias_definition (parser);
6891 /* If the next keyword is `using', we have either a
6892 using-declaration or a using-directive. */
6893 else if (token1->keyword == RID_USING)
6898 cp_parser_commit_to_tentative_parse (parser);
6899 /* If the token after `using' is `namespace', then we have a
6901 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6902 if (token2->keyword == RID_NAMESPACE)
6903 cp_parser_using_directive (parser);
6904 /* Otherwise, it's a using-declaration. */
6906 cp_parser_using_declaration (parser);
6908 /* If the next keyword is `__label__' we have a label declaration. */
6909 else if (token1->keyword == RID_LABEL)
6912 cp_parser_commit_to_tentative_parse (parser);
6913 cp_parser_label_declaration (parser);
6915 /* Anything else must be a simple-declaration. */
6917 cp_parser_simple_declaration (parser, !statement_p);
6920 /* Parse a simple-declaration.
6923 decl-specifier-seq [opt] init-declarator-list [opt] ;
6925 init-declarator-list:
6927 init-declarator-list , init-declarator
6929 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6930 function-definition as a simple-declaration. */
6933 cp_parser_simple_declaration (cp_parser* parser,
6934 bool function_definition_allowed_p)
6936 cp_decl_specifier_seq decl_specifiers;
6937 int declares_class_or_enum;
6938 bool saw_declarator;
6940 /* Defer access checks until we know what is being declared; the
6941 checks for names appearing in the decl-specifier-seq should be
6942 done as if we were in the scope of the thing being declared. */
6943 push_deferring_access_checks (dk_deferred);
6945 /* Parse the decl-specifier-seq. We have to keep track of whether
6946 or not the decl-specifier-seq declares a named class or
6947 enumeration type, since that is the only case in which the
6948 init-declarator-list is allowed to be empty.
6952 In a simple-declaration, the optional init-declarator-list can be
6953 omitted only when declaring a class or enumeration, that is when
6954 the decl-specifier-seq contains either a class-specifier, an
6955 elaborated-type-specifier, or an enum-specifier. */
6956 cp_parser_decl_specifier_seq (parser,
6957 CP_PARSER_FLAGS_OPTIONAL,
6959 &declares_class_or_enum);
6960 /* We no longer need to defer access checks. */
6961 stop_deferring_access_checks ();
6963 /* In a block scope, a valid declaration must always have a
6964 decl-specifier-seq. By not trying to parse declarators, we can
6965 resolve the declaration/expression ambiguity more quickly. */
6966 if (!function_definition_allowed_p
6967 && !decl_specifiers.any_specifiers_p)
6969 cp_parser_error (parser, "expected declaration");
6973 /* If the next two tokens are both identifiers, the code is
6974 erroneous. The usual cause of this situation is code like:
6978 where "T" should name a type -- but does not. */
6979 if (!decl_specifiers.type
6980 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6982 /* If parsing tentatively, we should commit; we really are
6983 looking at a declaration. */
6984 cp_parser_commit_to_tentative_parse (parser);
6989 /* If we have seen at least one decl-specifier, and the next token
6990 is not a parenthesis, then we must be looking at a declaration.
6991 (After "int (" we might be looking at a functional cast.) */
6992 if (decl_specifiers.any_specifiers_p
6993 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6994 cp_parser_commit_to_tentative_parse (parser);
6996 /* Keep going until we hit the `;' at the end of the simple
6998 saw_declarator = false;
6999 while (cp_lexer_next_token_is_not (parser->lexer,
7003 bool function_definition_p;
7006 saw_declarator = true;
7007 /* Parse the init-declarator. */
7008 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7009 function_definition_allowed_p,
7011 declares_class_or_enum,
7012 &function_definition_p);
7013 /* If an error occurred while parsing tentatively, exit quickly.
7014 (That usually happens when in the body of a function; each
7015 statement is treated as a declaration-statement until proven
7017 if (cp_parser_error_occurred (parser))
7019 /* Handle function definitions specially. */
7020 if (function_definition_p)
7022 /* If the next token is a `,', then we are probably
7023 processing something like:
7027 which is erroneous. */
7028 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7029 error ("mixing declarations and function-definitions is forbidden");
7030 /* Otherwise, we're done with the list of declarators. */
7033 pop_deferring_access_checks ();
7037 /* The next token should be either a `,' or a `;'. */
7038 token = cp_lexer_peek_token (parser->lexer);
7039 /* If it's a `,', there are more declarators to come. */
7040 if (token->type == CPP_COMMA)
7041 cp_lexer_consume_token (parser->lexer);
7042 /* If it's a `;', we are done. */
7043 else if (token->type == CPP_SEMICOLON)
7045 /* Anything else is an error. */
7048 /* If we have already issued an error message we don't need
7049 to issue another one. */
7050 if (decl != error_mark_node
7051 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7052 cp_parser_error (parser, "expected %<,%> or %<;%>");
7053 /* Skip tokens until we reach the end of the statement. */
7054 cp_parser_skip_to_end_of_statement (parser);
7055 /* If the next token is now a `;', consume it. */
7056 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7057 cp_lexer_consume_token (parser->lexer);
7060 /* After the first time around, a function-definition is not
7061 allowed -- even if it was OK at first. For example:
7066 function_definition_allowed_p = false;
7069 /* Issue an error message if no declarators are present, and the
7070 decl-specifier-seq does not itself declare a class or
7072 if (!saw_declarator)
7074 if (cp_parser_declares_only_class_p (parser))
7075 shadow_tag (&decl_specifiers);
7076 /* Perform any deferred access checks. */
7077 perform_deferred_access_checks ();
7080 /* Consume the `;'. */
7081 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7084 pop_deferring_access_checks ();
7087 /* Parse a decl-specifier-seq.
7090 decl-specifier-seq [opt] decl-specifier
7093 storage-class-specifier
7104 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7106 The parser flags FLAGS is used to control type-specifier parsing.
7108 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7111 1: one of the decl-specifiers is an elaborated-type-specifier
7112 (i.e., a type declaration)
7113 2: one of the decl-specifiers is an enum-specifier or a
7114 class-specifier (i.e., a type definition)
7119 cp_parser_decl_specifier_seq (cp_parser* parser,
7120 cp_parser_flags flags,
7121 cp_decl_specifier_seq *decl_specs,
7122 int* declares_class_or_enum)
7124 bool constructor_possible_p = !parser->in_declarator_p;
7126 /* Clear DECL_SPECS. */
7127 clear_decl_specs (decl_specs);
7129 /* Assume no class or enumeration type is declared. */
7130 *declares_class_or_enum = 0;
7132 /* Keep reading specifiers until there are no more to read. */
7136 bool found_decl_spec;
7139 /* Peek at the next token. */
7140 token = cp_lexer_peek_token (parser->lexer);
7141 /* Handle attributes. */
7142 if (token->keyword == RID_ATTRIBUTE)
7144 /* Parse the attributes. */
7145 decl_specs->attributes
7146 = chainon (decl_specs->attributes,
7147 cp_parser_attributes_opt (parser));
7150 /* Assume we will find a decl-specifier keyword. */
7151 found_decl_spec = true;
7152 /* If the next token is an appropriate keyword, we can simply
7153 add it to the list. */
7154 switch (token->keyword)
7159 if (decl_specs->specs[(int) ds_friend]++)
7160 error ("duplicate %<friend%>");
7161 /* Consume the token. */
7162 cp_lexer_consume_token (parser->lexer);
7165 /* function-specifier:
7172 cp_parser_function_specifier_opt (parser, decl_specs);
7178 ++decl_specs->specs[(int) ds_typedef];
7179 /* Consume the token. */
7180 cp_lexer_consume_token (parser->lexer);
7181 /* A constructor declarator cannot appear in a typedef. */
7182 constructor_possible_p = false;
7183 /* The "typedef" keyword can only occur in a declaration; we
7184 may as well commit at this point. */
7185 cp_parser_commit_to_tentative_parse (parser);
7188 /* storage-class-specifier:
7198 /* Consume the token. */
7199 cp_lexer_consume_token (parser->lexer);
7200 cp_parser_set_storage_class (decl_specs, sc_auto);
7203 /* Consume the token. */
7204 cp_lexer_consume_token (parser->lexer);
7205 cp_parser_set_storage_class (decl_specs, sc_register);
7208 /* Consume the token. */
7209 cp_lexer_consume_token (parser->lexer);
7210 if (decl_specs->specs[(int) ds_thread])
7212 error ("%<__thread%> before %<static%>");
7213 decl_specs->specs[(int) ds_thread] = 0;
7215 cp_parser_set_storage_class (decl_specs, sc_static);
7218 /* Consume the token. */
7219 cp_lexer_consume_token (parser->lexer);
7220 if (decl_specs->specs[(int) ds_thread])
7222 error ("%<__thread%> before %<extern%>");
7223 decl_specs->specs[(int) ds_thread] = 0;
7225 cp_parser_set_storage_class (decl_specs, sc_extern);
7228 /* Consume the token. */
7229 cp_lexer_consume_token (parser->lexer);
7230 cp_parser_set_storage_class (decl_specs, sc_mutable);
7233 /* Consume the token. */
7234 cp_lexer_consume_token (parser->lexer);
7235 ++decl_specs->specs[(int) ds_thread];
7239 /* We did not yet find a decl-specifier yet. */
7240 found_decl_spec = false;
7244 /* Constructors are a special case. The `S' in `S()' is not a
7245 decl-specifier; it is the beginning of the declarator. */
7248 && constructor_possible_p
7249 && (cp_parser_constructor_declarator_p
7250 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7252 /* If we don't have a DECL_SPEC yet, then we must be looking at
7253 a type-specifier. */
7254 if (!found_decl_spec && !constructor_p)
7256 int decl_spec_declares_class_or_enum;
7257 bool is_cv_qualifier;
7261 = cp_parser_type_specifier (parser, flags,
7263 /*is_declaration=*/true,
7264 &decl_spec_declares_class_or_enum,
7267 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7269 /* If this type-specifier referenced a user-defined type
7270 (a typedef, class-name, etc.), then we can't allow any
7271 more such type-specifiers henceforth.
7275 The longest sequence of decl-specifiers that could
7276 possibly be a type name is taken as the
7277 decl-specifier-seq of a declaration. The sequence shall
7278 be self-consistent as described below.
7282 As a general rule, at most one type-specifier is allowed
7283 in the complete decl-specifier-seq of a declaration. The
7284 only exceptions are the following:
7286 -- const or volatile can be combined with any other
7289 -- signed or unsigned can be combined with char, long,
7297 void g (const int Pc);
7299 Here, Pc is *not* part of the decl-specifier seq; it's
7300 the declarator. Therefore, once we see a type-specifier
7301 (other than a cv-qualifier), we forbid any additional
7302 user-defined types. We *do* still allow things like `int
7303 int' to be considered a decl-specifier-seq, and issue the
7304 error message later. */
7305 if (type_spec && !is_cv_qualifier)
7306 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7307 /* A constructor declarator cannot follow a type-specifier. */
7310 constructor_possible_p = false;
7311 found_decl_spec = true;
7315 /* If we still do not have a DECL_SPEC, then there are no more
7317 if (!found_decl_spec)
7320 decl_specs->any_specifiers_p = true;
7321 /* After we see one decl-specifier, further decl-specifiers are
7323 flags |= CP_PARSER_FLAGS_OPTIONAL;
7326 /* Don't allow a friend specifier with a class definition. */
7327 if (decl_specs->specs[(int) ds_friend] != 0
7328 && (*declares_class_or_enum & 2))
7329 error ("class definition may not be declared a friend");
7332 /* Parse an (optional) storage-class-specifier.
7334 storage-class-specifier:
7343 storage-class-specifier:
7346 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7349 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7351 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7359 /* Consume the token. */
7360 return cp_lexer_consume_token (parser->lexer)->value;
7367 /* Parse an (optional) function-specifier.
7374 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7375 Updates DECL_SPECS, if it is non-NULL. */
7378 cp_parser_function_specifier_opt (cp_parser* parser,
7379 cp_decl_specifier_seq *decl_specs)
7381 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7385 ++decl_specs->specs[(int) ds_inline];
7390 ++decl_specs->specs[(int) ds_virtual];
7395 ++decl_specs->specs[(int) ds_explicit];
7402 /* Consume the token. */
7403 return cp_lexer_consume_token (parser->lexer)->value;
7406 /* Parse a linkage-specification.
7408 linkage-specification:
7409 extern string-literal { declaration-seq [opt] }
7410 extern string-literal declaration */
7413 cp_parser_linkage_specification (cp_parser* parser)
7417 /* Look for the `extern' keyword. */
7418 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7420 /* Look for the string-literal. */
7421 linkage = cp_parser_string_literal (parser, false, false);
7423 /* Transform the literal into an identifier. If the literal is a
7424 wide-character string, or contains embedded NULs, then we can't
7425 handle it as the user wants. */
7426 if (strlen (TREE_STRING_POINTER (linkage))
7427 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7429 cp_parser_error (parser, "invalid linkage-specification");
7430 /* Assume C++ linkage. */
7431 linkage = lang_name_cplusplus;
7434 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7436 /* We're now using the new linkage. */
7437 push_lang_context (linkage);
7439 /* If the next token is a `{', then we're using the first
7441 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7443 /* Consume the `{' token. */
7444 cp_lexer_consume_token (parser->lexer);
7445 /* Parse the declarations. */
7446 cp_parser_declaration_seq_opt (parser);
7447 /* Look for the closing `}'. */
7448 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7450 /* Otherwise, there's just one declaration. */
7453 bool saved_in_unbraced_linkage_specification_p;
7455 saved_in_unbraced_linkage_specification_p
7456 = parser->in_unbraced_linkage_specification_p;
7457 parser->in_unbraced_linkage_specification_p = true;
7458 have_extern_spec = true;
7459 cp_parser_declaration (parser);
7460 have_extern_spec = false;
7461 parser->in_unbraced_linkage_specification_p
7462 = saved_in_unbraced_linkage_specification_p;
7465 /* We're done with the linkage-specification. */
7466 pop_lang_context ();
7469 /* Special member functions [gram.special] */
7471 /* Parse a conversion-function-id.
7473 conversion-function-id:
7474 operator conversion-type-id
7476 Returns an IDENTIFIER_NODE representing the operator. */
7479 cp_parser_conversion_function_id (cp_parser* parser)
7483 tree saved_qualifying_scope;
7484 tree saved_object_scope;
7485 tree pushed_scope = NULL_TREE;
7487 /* Look for the `operator' token. */
7488 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7489 return error_mark_node;
7490 /* When we parse the conversion-type-id, the current scope will be
7491 reset. However, we need that information in able to look up the
7492 conversion function later, so we save it here. */
7493 saved_scope = parser->scope;
7494 saved_qualifying_scope = parser->qualifying_scope;
7495 saved_object_scope = parser->object_scope;
7496 /* We must enter the scope of the class so that the names of
7497 entities declared within the class are available in the
7498 conversion-type-id. For example, consider:
7505 S::operator I() { ... }
7507 In order to see that `I' is a type-name in the definition, we
7508 must be in the scope of `S'. */
7510 pushed_scope = push_scope (saved_scope);
7511 /* Parse the conversion-type-id. */
7512 type = cp_parser_conversion_type_id (parser);
7513 /* Leave the scope of the class, if any. */
7515 pop_scope (pushed_scope);
7516 /* Restore the saved scope. */
7517 parser->scope = saved_scope;
7518 parser->qualifying_scope = saved_qualifying_scope;
7519 parser->object_scope = saved_object_scope;
7520 /* If the TYPE is invalid, indicate failure. */
7521 if (type == error_mark_node)
7522 return error_mark_node;
7523 return mangle_conv_op_name_for_type (type);
7526 /* Parse a conversion-type-id:
7529 type-specifier-seq conversion-declarator [opt]
7531 Returns the TYPE specified. */
7534 cp_parser_conversion_type_id (cp_parser* parser)
7537 cp_decl_specifier_seq type_specifiers;
7538 cp_declarator *declarator;
7539 tree type_specified;
7541 /* Parse the attributes. */
7542 attributes = cp_parser_attributes_opt (parser);
7543 /* Parse the type-specifiers. */
7544 cp_parser_type_specifier_seq (parser, &type_specifiers);
7545 /* If that didn't work, stop. */
7546 if (type_specifiers.type == error_mark_node)
7547 return error_mark_node;
7548 /* Parse the conversion-declarator. */
7549 declarator = cp_parser_conversion_declarator_opt (parser);
7551 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7552 /*initialized=*/0, &attributes);
7554 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7555 return type_specified;
7558 /* Parse an (optional) conversion-declarator.
7560 conversion-declarator:
7561 ptr-operator conversion-declarator [opt]
7565 static cp_declarator *
7566 cp_parser_conversion_declarator_opt (cp_parser* parser)
7568 enum tree_code code;
7570 cp_cv_quals cv_quals;
7572 /* We don't know if there's a ptr-operator next, or not. */
7573 cp_parser_parse_tentatively (parser);
7574 /* Try the ptr-operator. */
7575 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7576 /* If it worked, look for more conversion-declarators. */
7577 if (cp_parser_parse_definitely (parser))
7579 cp_declarator *declarator;
7581 /* Parse another optional declarator. */
7582 declarator = cp_parser_conversion_declarator_opt (parser);
7584 /* Create the representation of the declarator. */
7586 declarator = make_ptrmem_declarator (cv_quals, class_type,
7588 else if (code == INDIRECT_REF)
7589 declarator = make_pointer_declarator (cv_quals, declarator);
7591 declarator = make_reference_declarator (cv_quals, declarator);
7599 /* Parse an (optional) ctor-initializer.
7602 : mem-initializer-list
7604 Returns TRUE iff the ctor-initializer was actually present. */
7607 cp_parser_ctor_initializer_opt (cp_parser* parser)
7609 /* If the next token is not a `:', then there is no
7610 ctor-initializer. */
7611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7613 /* Do default initialization of any bases and members. */
7614 if (DECL_CONSTRUCTOR_P (current_function_decl))
7615 finish_mem_initializers (NULL_TREE);
7620 /* Consume the `:' token. */
7621 cp_lexer_consume_token (parser->lexer);
7622 /* And the mem-initializer-list. */
7623 cp_parser_mem_initializer_list (parser);
7628 /* Parse a mem-initializer-list.
7630 mem-initializer-list:
7632 mem-initializer , mem-initializer-list */
7635 cp_parser_mem_initializer_list (cp_parser* parser)
7637 tree mem_initializer_list = NULL_TREE;
7639 /* Let the semantic analysis code know that we are starting the
7640 mem-initializer-list. */
7641 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7642 error ("only constructors take base initializers");
7644 /* Loop through the list. */
7647 tree mem_initializer;
7649 /* Parse the mem-initializer. */
7650 mem_initializer = cp_parser_mem_initializer (parser);
7651 /* Add it to the list, unless it was erroneous. */
7652 if (mem_initializer)
7654 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7655 mem_initializer_list = mem_initializer;
7657 /* If the next token is not a `,', we're done. */
7658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7660 /* Consume the `,' token. */
7661 cp_lexer_consume_token (parser->lexer);
7664 /* Perform semantic analysis. */
7665 if (DECL_CONSTRUCTOR_P (current_function_decl))
7666 finish_mem_initializers (mem_initializer_list);
7669 /* Parse a mem-initializer.
7672 mem-initializer-id ( expression-list [opt] )
7677 ( expression-list [opt] )
7679 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7680 class) or FIELD_DECL (for a non-static data member) to initialize;
7681 the TREE_VALUE is the expression-list. */
7684 cp_parser_mem_initializer (cp_parser* parser)
7686 tree mem_initializer_id;
7687 tree expression_list;
7690 /* Find out what is being initialized. */
7691 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7693 pedwarn ("anachronistic old-style base class initializer");
7694 mem_initializer_id = NULL_TREE;
7697 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7698 member = expand_member_init (mem_initializer_id);
7699 if (member && !DECL_P (member))
7700 in_base_initializer = 1;
7703 = cp_parser_parenthesized_expression_list (parser, false,
7705 /*non_constant_p=*/NULL);
7706 if (!expression_list)
7707 expression_list = void_type_node;
7709 in_base_initializer = 0;
7711 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7714 /* Parse a mem-initializer-id.
7717 :: [opt] nested-name-specifier [opt] class-name
7720 Returns a TYPE indicating the class to be initializer for the first
7721 production. Returns an IDENTIFIER_NODE indicating the data member
7722 to be initialized for the second production. */
7725 cp_parser_mem_initializer_id (cp_parser* parser)
7727 bool global_scope_p;
7728 bool nested_name_specifier_p;
7729 bool template_p = false;
7732 /* `typename' is not allowed in this context ([temp.res]). */
7733 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7735 error ("keyword %<typename%> not allowed in this context (a qualified "
7736 "member initializer is implicitly a type)");
7737 cp_lexer_consume_token (parser->lexer);
7739 /* Look for the optional `::' operator. */
7741 = (cp_parser_global_scope_opt (parser,
7742 /*current_scope_valid_p=*/false)
7744 /* Look for the optional nested-name-specifier. The simplest way to
7749 The keyword `typename' is not permitted in a base-specifier or
7750 mem-initializer; in these contexts a qualified name that
7751 depends on a template-parameter is implicitly assumed to be a
7754 is to assume that we have seen the `typename' keyword at this
7756 nested_name_specifier_p
7757 = (cp_parser_nested_name_specifier_opt (parser,
7758 /*typename_keyword_p=*/true,
7759 /*check_dependency_p=*/true,
7761 /*is_declaration=*/true)
7763 if (nested_name_specifier_p)
7764 template_p = cp_parser_optional_template_keyword (parser);
7765 /* If there is a `::' operator or a nested-name-specifier, then we
7766 are definitely looking for a class-name. */
7767 if (global_scope_p || nested_name_specifier_p)
7768 return cp_parser_class_name (parser,
7769 /*typename_keyword_p=*/true,
7770 /*template_keyword_p=*/template_p,
7772 /*check_dependency_p=*/true,
7773 /*class_head_p=*/false,
7774 /*is_declaration=*/true);
7775 /* Otherwise, we could also be looking for an ordinary identifier. */
7776 cp_parser_parse_tentatively (parser);
7777 /* Try a class-name. */
7778 id = cp_parser_class_name (parser,
7779 /*typename_keyword_p=*/true,
7780 /*template_keyword_p=*/false,
7782 /*check_dependency_p=*/true,
7783 /*class_head_p=*/false,
7784 /*is_declaration=*/true);
7785 /* If we found one, we're done. */
7786 if (cp_parser_parse_definitely (parser))
7788 /* Otherwise, look for an ordinary identifier. */
7789 return cp_parser_identifier (parser);
7792 /* Overloading [gram.over] */
7794 /* Parse an operator-function-id.
7796 operator-function-id:
7799 Returns an IDENTIFIER_NODE for the operator which is a
7800 human-readable spelling of the identifier, e.g., `operator +'. */
7803 cp_parser_operator_function_id (cp_parser* parser)
7805 /* Look for the `operator' keyword. */
7806 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7807 return error_mark_node;
7808 /* And then the name of the operator itself. */
7809 return cp_parser_operator (parser);
7812 /* Parse an operator.
7815 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7816 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7817 || ++ -- , ->* -> () []
7824 Returns an IDENTIFIER_NODE for the operator which is a
7825 human-readable spelling of the identifier, e.g., `operator +'. */
7828 cp_parser_operator (cp_parser* parser)
7830 tree id = NULL_TREE;
7833 /* Peek at the next token. */
7834 token = cp_lexer_peek_token (parser->lexer);
7835 /* Figure out which operator we have. */
7836 switch (token->type)
7842 /* The keyword should be either `new' or `delete'. */
7843 if (token->keyword == RID_NEW)
7845 else if (token->keyword == RID_DELETE)
7850 /* Consume the `new' or `delete' token. */
7851 cp_lexer_consume_token (parser->lexer);
7853 /* Peek at the next token. */
7854 token = cp_lexer_peek_token (parser->lexer);
7855 /* If it's a `[' token then this is the array variant of the
7857 if (token->type == CPP_OPEN_SQUARE)
7859 /* Consume the `[' token. */
7860 cp_lexer_consume_token (parser->lexer);
7861 /* Look for the `]' token. */
7862 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7863 id = ansi_opname (op == NEW_EXPR
7864 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7866 /* Otherwise, we have the non-array variant. */
7868 id = ansi_opname (op);
7874 id = ansi_opname (PLUS_EXPR);
7878 id = ansi_opname (MINUS_EXPR);
7882 id = ansi_opname (MULT_EXPR);
7886 id = ansi_opname (TRUNC_DIV_EXPR);
7890 id = ansi_opname (TRUNC_MOD_EXPR);
7894 id = ansi_opname (BIT_XOR_EXPR);
7898 id = ansi_opname (BIT_AND_EXPR);
7902 id = ansi_opname (BIT_IOR_EXPR);
7906 id = ansi_opname (BIT_NOT_EXPR);
7910 id = ansi_opname (TRUTH_NOT_EXPR);
7914 id = ansi_assopname (NOP_EXPR);
7918 id = ansi_opname (LT_EXPR);
7922 id = ansi_opname (GT_EXPR);
7926 id = ansi_assopname (PLUS_EXPR);
7930 id = ansi_assopname (MINUS_EXPR);
7934 id = ansi_assopname (MULT_EXPR);
7938 id = ansi_assopname (TRUNC_DIV_EXPR);
7942 id = ansi_assopname (TRUNC_MOD_EXPR);
7946 id = ansi_assopname (BIT_XOR_EXPR);
7950 id = ansi_assopname (BIT_AND_EXPR);
7954 id = ansi_assopname (BIT_IOR_EXPR);
7958 id = ansi_opname (LSHIFT_EXPR);
7962 id = ansi_opname (RSHIFT_EXPR);
7966 id = ansi_assopname (LSHIFT_EXPR);
7970 id = ansi_assopname (RSHIFT_EXPR);
7974 id = ansi_opname (EQ_EXPR);
7978 id = ansi_opname (NE_EXPR);
7982 id = ansi_opname (LE_EXPR);
7985 case CPP_GREATER_EQ:
7986 id = ansi_opname (GE_EXPR);
7990 id = ansi_opname (TRUTH_ANDIF_EXPR);
7994 id = ansi_opname (TRUTH_ORIF_EXPR);
7998 id = ansi_opname (POSTINCREMENT_EXPR);
8001 case CPP_MINUS_MINUS:
8002 id = ansi_opname (PREDECREMENT_EXPR);
8006 id = ansi_opname (COMPOUND_EXPR);
8009 case CPP_DEREF_STAR:
8010 id = ansi_opname (MEMBER_REF);
8014 id = ansi_opname (COMPONENT_REF);
8017 case CPP_OPEN_PAREN:
8018 /* Consume the `('. */
8019 cp_lexer_consume_token (parser->lexer);
8020 /* Look for the matching `)'. */
8021 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8022 return ansi_opname (CALL_EXPR);
8024 case CPP_OPEN_SQUARE:
8025 /* Consume the `['. */
8026 cp_lexer_consume_token (parser->lexer);
8027 /* Look for the matching `]'. */
8028 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8029 return ansi_opname (ARRAY_REF);
8033 id = ansi_opname (MIN_EXPR);
8037 id = ansi_opname (MAX_EXPR);
8041 id = ansi_assopname (MIN_EXPR);
8045 id = ansi_assopname (MAX_EXPR);
8049 /* Anything else is an error. */
8053 /* If we have selected an identifier, we need to consume the
8056 cp_lexer_consume_token (parser->lexer);
8057 /* Otherwise, no valid operator name was present. */
8060 cp_parser_error (parser, "expected operator");
8061 id = error_mark_node;
8067 /* Parse a template-declaration.
8069 template-declaration:
8070 export [opt] template < template-parameter-list > declaration
8072 If MEMBER_P is TRUE, this template-declaration occurs within a
8075 The grammar rule given by the standard isn't correct. What
8078 template-declaration:
8079 export [opt] template-parameter-list-seq
8080 decl-specifier-seq [opt] init-declarator [opt] ;
8081 export [opt] template-parameter-list-seq
8084 template-parameter-list-seq:
8085 template-parameter-list-seq [opt]
8086 template < template-parameter-list > */
8089 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8091 /* Check for `export'. */
8092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8094 /* Consume the `export' token. */
8095 cp_lexer_consume_token (parser->lexer);
8096 /* Warn that we do not support `export'. */
8097 warning ("keyword %<export%> not implemented, and will be ignored");
8100 cp_parser_template_declaration_after_export (parser, member_p);
8103 /* Parse a template-parameter-list.
8105 template-parameter-list:
8107 template-parameter-list , template-parameter
8109 Returns a TREE_LIST. Each node represents a template parameter.
8110 The nodes are connected via their TREE_CHAINs. */
8113 cp_parser_template_parameter_list (cp_parser* parser)
8115 tree parameter_list = NULL_TREE;
8123 /* Parse the template-parameter. */
8124 parameter = cp_parser_template_parameter (parser, &is_non_type);
8125 /* Add it to the list. */
8126 if (parameter != error_mark_node)
8127 parameter_list = process_template_parm (parameter_list,
8130 /* Peek at the next token. */
8131 token = cp_lexer_peek_token (parser->lexer);
8132 /* If it's not a `,', we're done. */
8133 if (token->type != CPP_COMMA)
8135 /* Otherwise, consume the `,' token. */
8136 cp_lexer_consume_token (parser->lexer);
8139 return parameter_list;
8142 /* Parse a template-parameter.
8146 parameter-declaration
8148 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8149 the parameter. The TREE_PURPOSE is the default value, if any.
8150 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8151 iff this parameter is a non-type parameter. */
8154 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8157 cp_parameter_declarator *parameter_declarator;
8160 /* Assume it is a type parameter or a template parameter. */
8161 *is_non_type = false;
8162 /* Peek at the next token. */
8163 token = cp_lexer_peek_token (parser->lexer);
8164 /* If it is `class' or `template', we have a type-parameter. */
8165 if (token->keyword == RID_TEMPLATE)
8166 return cp_parser_type_parameter (parser);
8167 /* If it is `class' or `typename' we do not know yet whether it is a
8168 type parameter or a non-type parameter. Consider:
8170 template <typename T, typename T::X X> ...
8174 template <class C, class D*> ...
8176 Here, the first parameter is a type parameter, and the second is
8177 a non-type parameter. We can tell by looking at the token after
8178 the identifier -- if it is a `,', `=', or `>' then we have a type
8180 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8182 /* Peek at the token after `class' or `typename'. */
8183 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8184 /* If it's an identifier, skip it. */
8185 if (token->type == CPP_NAME)
8186 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8187 /* Now, see if the token looks like the end of a template
8189 if (token->type == CPP_COMMA
8190 || token->type == CPP_EQ
8191 || token->type == CPP_GREATER)
8192 return cp_parser_type_parameter (parser);
8195 /* Otherwise, it is a non-type parameter.
8199 When parsing a default template-argument for a non-type
8200 template-parameter, the first non-nested `>' is taken as the end
8201 of the template parameter-list rather than a greater-than
8203 *is_non_type = true;
8204 parameter_declarator
8205 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8206 /*parenthesized_p=*/NULL);
8207 parm = grokdeclarator (parameter_declarator->declarator,
8208 ¶meter_declarator->decl_specifiers,
8209 PARM, /*initialized=*/0,
8211 if (parm == error_mark_node)
8212 return error_mark_node;
8213 return build_tree_list (parameter_declarator->default_argument, parm);
8216 /* Parse a type-parameter.
8219 class identifier [opt]
8220 class identifier [opt] = type-id
8221 typename identifier [opt]
8222 typename identifier [opt] = type-id
8223 template < template-parameter-list > class identifier [opt]
8224 template < template-parameter-list > class identifier [opt]
8227 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8228 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8229 the declaration of the parameter. */
8232 cp_parser_type_parameter (cp_parser* parser)
8237 /* Look for a keyword to tell us what kind of parameter this is. */
8238 token = cp_parser_require (parser, CPP_KEYWORD,
8239 "`class', `typename', or `template'");
8241 return error_mark_node;
8243 switch (token->keyword)
8249 tree default_argument;
8251 /* If the next token is an identifier, then it names the
8253 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8254 identifier = cp_parser_identifier (parser);
8256 identifier = NULL_TREE;
8258 /* Create the parameter. */
8259 parameter = finish_template_type_parm (class_type_node, identifier);
8261 /* If the next token is an `=', we have a default argument. */
8262 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8264 /* Consume the `=' token. */
8265 cp_lexer_consume_token (parser->lexer);
8266 /* Parse the default-argument. */
8267 default_argument = cp_parser_type_id (parser);
8270 default_argument = NULL_TREE;
8272 /* Create the combined representation of the parameter and the
8273 default argument. */
8274 parameter = build_tree_list (default_argument, parameter);
8280 tree parameter_list;
8282 tree default_argument;
8284 /* Look for the `<'. */
8285 cp_parser_require (parser, CPP_LESS, "`<'");
8286 /* Parse the template-parameter-list. */
8287 begin_template_parm_list ();
8289 = cp_parser_template_parameter_list (parser);
8290 parameter_list = end_template_parm_list (parameter_list);
8291 /* Look for the `>'. */
8292 cp_parser_require (parser, CPP_GREATER, "`>'");
8293 /* Look for the `class' keyword. */
8294 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8295 /* If the next token is an `=', then there is a
8296 default-argument. If the next token is a `>', we are at
8297 the end of the parameter-list. If the next token is a `,',
8298 then we are at the end of this parameter. */
8299 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8300 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8301 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8303 identifier = cp_parser_identifier (parser);
8304 /* Treat invalid names as if the parameter were nameless. */
8305 if (identifier == error_mark_node)
8306 identifier = NULL_TREE;
8309 identifier = NULL_TREE;
8311 /* Create the template parameter. */
8312 parameter = finish_template_template_parm (class_type_node,
8315 /* If the next token is an `=', then there is a
8316 default-argument. */
8317 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8321 /* Consume the `='. */
8322 cp_lexer_consume_token (parser->lexer);
8323 /* Parse the id-expression. */
8325 = cp_parser_id_expression (parser,
8326 /*template_keyword_p=*/false,
8327 /*check_dependency_p=*/true,
8328 /*template_p=*/&is_template,
8329 /*declarator_p=*/false);
8330 if (TREE_CODE (default_argument) == TYPE_DECL)
8331 /* If the id-expression was a template-id that refers to
8332 a template-class, we already have the declaration here,
8333 so no further lookup is needed. */
8336 /* Look up the name. */
8338 = cp_parser_lookup_name (parser, default_argument,
8340 /*is_template=*/is_template,
8341 /*is_namespace=*/false,
8342 /*check_dependency=*/true,
8343 /*ambiguous_p=*/NULL);
8344 /* See if the default argument is valid. */
8346 = check_template_template_default_arg (default_argument);
8349 default_argument = NULL_TREE;
8351 /* Create the combined representation of the parameter and the
8352 default argument. */
8353 parameter = build_tree_list (default_argument, parameter);
8365 /* Parse a template-id.
8368 template-name < template-argument-list [opt] >
8370 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8371 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8372 returned. Otherwise, if the template-name names a function, or set
8373 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8374 names a class, returns a TYPE_DECL for the specialization.
8376 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8377 uninstantiated templates. */
8380 cp_parser_template_id (cp_parser *parser,
8381 bool template_keyword_p,
8382 bool check_dependency_p,
8383 bool is_declaration)
8388 cp_token_position start_of_id = 0;
8389 tree access_check = NULL_TREE;
8390 cp_token *next_token, *next_token_2;
8393 /* If the next token corresponds to a template-id, there is no need
8395 next_token = cp_lexer_peek_token (parser->lexer);
8396 if (next_token->type == CPP_TEMPLATE_ID)
8401 /* Get the stored value. */
8402 value = cp_lexer_consume_token (parser->lexer)->value;
8403 /* Perform any access checks that were deferred. */
8404 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8405 perform_or_defer_access_check (TREE_PURPOSE (check),
8406 TREE_VALUE (check));
8407 /* Return the stored value. */
8408 return TREE_VALUE (value);
8411 /* Avoid performing name lookup if there is no possibility of
8412 finding a template-id. */
8413 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8414 || (next_token->type == CPP_NAME
8415 && !cp_parser_nth_token_starts_template_argument_list_p
8418 cp_parser_error (parser, "expected template-id");
8419 return error_mark_node;
8422 /* Remember where the template-id starts. */
8423 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8424 start_of_id = cp_lexer_token_position (parser->lexer, false);
8426 push_deferring_access_checks (dk_deferred);
8428 /* Parse the template-name. */
8429 is_identifier = false;
8430 template = cp_parser_template_name (parser, template_keyword_p,
8434 if (template == error_mark_node || is_identifier)
8436 pop_deferring_access_checks ();
8440 /* If we find the sequence `[:' after a template-name, it's probably
8441 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8442 parse correctly the argument list. */
8443 next_token = cp_lexer_peek_token (parser->lexer);
8444 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8445 if (next_token->type == CPP_OPEN_SQUARE
8446 && next_token->flags & DIGRAPH
8447 && next_token_2->type == CPP_COLON
8448 && !(next_token_2->flags & PREV_WHITE))
8450 cp_parser_parse_tentatively (parser);
8451 /* Change `:' into `::'. */
8452 next_token_2->type = CPP_SCOPE;
8453 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8455 cp_lexer_consume_token (parser->lexer);
8456 /* Parse the arguments. */
8457 arguments = cp_parser_enclosed_template_argument_list (parser);
8458 if (!cp_parser_parse_definitely (parser))
8460 /* If we couldn't parse an argument list, then we revert our changes
8461 and return simply an error. Maybe this is not a template-id
8463 next_token_2->type = CPP_COLON;
8464 cp_parser_error (parser, "expected %<<%>");
8465 pop_deferring_access_checks ();
8466 return error_mark_node;
8468 /* Otherwise, emit an error about the invalid digraph, but continue
8469 parsing because we got our argument list. */
8470 pedwarn ("%<<::%> cannot begin a template-argument list");
8471 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8472 "between %<<%> and %<::%>");
8473 if (!flag_permissive)
8478 inform ("(if you use -fpermissive G++ will accept your code)");
8485 /* Look for the `<' that starts the template-argument-list. */
8486 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8488 pop_deferring_access_checks ();
8489 return error_mark_node;
8491 /* Parse the arguments. */
8492 arguments = cp_parser_enclosed_template_argument_list (parser);
8495 /* Build a representation of the specialization. */
8496 if (TREE_CODE (template) == IDENTIFIER_NODE)
8497 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8498 else if (DECL_CLASS_TEMPLATE_P (template)
8499 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8501 = finish_template_type (template, arguments,
8502 cp_lexer_next_token_is (parser->lexer,
8506 /* If it's not a class-template or a template-template, it should be
8507 a function-template. */
8508 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8509 || TREE_CODE (template) == OVERLOAD
8510 || BASELINK_P (template)));
8512 template_id = lookup_template_function (template, arguments);
8515 /* Retrieve any deferred checks. Do not pop this access checks yet
8516 so the memory will not be reclaimed during token replacing below. */
8517 access_check = get_deferred_access_checks ();
8519 /* If parsing tentatively, replace the sequence of tokens that makes
8520 up the template-id with a CPP_TEMPLATE_ID token. That way,
8521 should we re-parse the token stream, we will not have to repeat
8522 the effort required to do the parse, nor will we issue duplicate
8523 error messages about problems during instantiation of the
8527 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8529 /* Reset the contents of the START_OF_ID token. */
8530 token->type = CPP_TEMPLATE_ID;
8531 token->value = build_tree_list (access_check, template_id);
8532 token->keyword = RID_MAX;
8534 /* Purge all subsequent tokens. */
8535 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8537 /* ??? Can we actually assume that, if template_id ==
8538 error_mark_node, we will have issued a diagnostic to the
8539 user, as opposed to simply marking the tentative parse as
8541 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8542 error ("parse error in template argument list");
8545 pop_deferring_access_checks ();
8549 /* Parse a template-name.
8554 The standard should actually say:
8558 operator-function-id
8560 A defect report has been filed about this issue.
8562 A conversion-function-id cannot be a template name because they cannot
8563 be part of a template-id. In fact, looking at this code:
8567 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8568 It is impossible to call a templated conversion-function-id with an
8569 explicit argument list, since the only allowed template parameter is
8570 the type to which it is converting.
8572 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8573 `template' keyword, in a construction like:
8577 In that case `f' is taken to be a template-name, even though there
8578 is no way of knowing for sure.
8580 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8581 name refers to a set of overloaded functions, at least one of which
8582 is a template, or an IDENTIFIER_NODE with the name of the template,
8583 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8584 names are looked up inside uninstantiated templates. */
8587 cp_parser_template_name (cp_parser* parser,
8588 bool template_keyword_p,
8589 bool check_dependency_p,
8590 bool is_declaration,
8591 bool *is_identifier)
8597 /* If the next token is `operator', then we have either an
8598 operator-function-id or a conversion-function-id. */
8599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8601 /* We don't know whether we're looking at an
8602 operator-function-id or a conversion-function-id. */
8603 cp_parser_parse_tentatively (parser);
8604 /* Try an operator-function-id. */
8605 identifier = cp_parser_operator_function_id (parser);
8606 /* If that didn't work, try a conversion-function-id. */
8607 if (!cp_parser_parse_definitely (parser))
8609 cp_parser_error (parser, "expected template-name");
8610 return error_mark_node;
8613 /* Look for the identifier. */
8615 identifier = cp_parser_identifier (parser);
8617 /* If we didn't find an identifier, we don't have a template-id. */
8618 if (identifier == error_mark_node)
8619 return error_mark_node;
8621 /* If the name immediately followed the `template' keyword, then it
8622 is a template-name. However, if the next token is not `<', then
8623 we do not treat it as a template-name, since it is not being used
8624 as part of a template-id. This enables us to handle constructs
8627 template <typename T> struct S { S(); };
8628 template <typename T> S<T>::S();
8630 correctly. We would treat `S' as a template -- if it were `S<T>'
8631 -- but we do not if there is no `<'. */
8633 if (processing_template_decl
8634 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8636 /* In a declaration, in a dependent context, we pretend that the
8637 "template" keyword was present in order to improve error
8638 recovery. For example, given:
8640 template <typename T> void f(T::X<int>);
8642 we want to treat "X<int>" as a template-id. */
8644 && !template_keyword_p
8645 && parser->scope && TYPE_P (parser->scope)
8646 && check_dependency_p
8647 && dependent_type_p (parser->scope)
8648 /* Do not do this for dtors (or ctors), since they never
8649 need the template keyword before their name. */
8650 && !constructor_name_p (identifier, parser->scope))
8652 cp_token_position start = 0;
8654 /* Explain what went wrong. */
8655 error ("non-template %qD used as template", identifier);
8656 inform ("use %<%T::template %D%> to indicate that it is a template",
8657 parser->scope, identifier);
8658 /* If parsing tentatively, find the location of the "<" token. */
8659 if (cp_parser_simulate_error (parser))
8660 start = cp_lexer_token_position (parser->lexer, true);
8661 /* Parse the template arguments so that we can issue error
8662 messages about them. */
8663 cp_lexer_consume_token (parser->lexer);
8664 cp_parser_enclosed_template_argument_list (parser);
8665 /* Skip tokens until we find a good place from which to
8666 continue parsing. */
8667 cp_parser_skip_to_closing_parenthesis (parser,
8668 /*recovering=*/true,
8670 /*consume_paren=*/false);
8671 /* If parsing tentatively, permanently remove the
8672 template argument list. That will prevent duplicate
8673 error messages from being issued about the missing
8674 "template" keyword. */
8676 cp_lexer_purge_tokens_after (parser->lexer, start);
8678 *is_identifier = true;
8682 /* If the "template" keyword is present, then there is generally
8683 no point in doing name-lookup, so we just return IDENTIFIER.
8684 But, if the qualifying scope is non-dependent then we can
8685 (and must) do name-lookup normally. */
8686 if (template_keyword_p
8688 || (TYPE_P (parser->scope)
8689 && dependent_type_p (parser->scope))))
8693 /* Look up the name. */
8694 decl = cp_parser_lookup_name (parser, identifier,
8696 /*is_template=*/false,
8697 /*is_namespace=*/false,
8699 /*ambiguous_p=*/NULL);
8700 decl = maybe_get_template_decl_from_type_decl (decl);
8702 /* If DECL is a template, then the name was a template-name. */
8703 if (TREE_CODE (decl) == TEMPLATE_DECL)
8707 /* The standard does not explicitly indicate whether a name that
8708 names a set of overloaded declarations, some of which are
8709 templates, is a template-name. However, such a name should
8710 be a template-name; otherwise, there is no way to form a
8711 template-id for the overloaded templates. */
8712 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8713 if (TREE_CODE (fns) == OVERLOAD)
8717 for (fn = fns; fn; fn = OVL_NEXT (fn))
8718 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8723 /* Otherwise, the name does not name a template. */
8724 cp_parser_error (parser, "expected template-name");
8725 return error_mark_node;
8729 /* If DECL is dependent, and refers to a function, then just return
8730 its name; we will look it up again during template instantiation. */
8731 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8733 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8734 if (TYPE_P (scope) && dependent_type_p (scope))
8741 /* Parse a template-argument-list.
8743 template-argument-list:
8745 template-argument-list , template-argument
8747 Returns a TREE_VEC containing the arguments. */
8750 cp_parser_template_argument_list (cp_parser* parser)
8752 tree fixed_args[10];
8753 unsigned n_args = 0;
8754 unsigned alloced = 10;
8755 tree *arg_ary = fixed_args;
8757 bool saved_in_template_argument_list_p;
8759 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8760 parser->in_template_argument_list_p = true;
8766 /* Consume the comma. */
8767 cp_lexer_consume_token (parser->lexer);
8769 /* Parse the template-argument. */
8770 argument = cp_parser_template_argument (parser);
8771 if (n_args == alloced)
8775 if (arg_ary == fixed_args)
8777 arg_ary = xmalloc (sizeof (tree) * alloced);
8778 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8781 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8783 arg_ary[n_args++] = argument;
8785 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8787 vec = make_tree_vec (n_args);
8790 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8792 if (arg_ary != fixed_args)
8794 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8798 /* Parse a template-argument.
8801 assignment-expression
8805 The representation is that of an assignment-expression, type-id, or
8806 id-expression -- except that the qualified id-expression is
8807 evaluated, so that the value returned is either a DECL or an
8810 Although the standard says "assignment-expression", it forbids
8811 throw-expressions or assignments in the template argument.
8812 Therefore, we use "conditional-expression" instead. */
8815 cp_parser_template_argument (cp_parser* parser)
8820 bool maybe_type_id = false;
8823 tree qualifying_class;
8825 /* There's really no way to know what we're looking at, so we just
8826 try each alternative in order.
8830 In a template-argument, an ambiguity between a type-id and an
8831 expression is resolved to a type-id, regardless of the form of
8832 the corresponding template-parameter.
8834 Therefore, we try a type-id first. */
8835 cp_parser_parse_tentatively (parser);
8836 argument = cp_parser_type_id (parser);
8837 /* If there was no error parsing the type-id but the next token is a '>>',
8838 we probably found a typo for '> >'. But there are type-id which are
8839 also valid expressions. For instance:
8841 struct X { int operator >> (int); };
8842 template <int V> struct Foo {};
8845 Here 'X()' is a valid type-id of a function type, but the user just
8846 wanted to write the expression "X() >> 5". Thus, we remember that we
8847 found a valid type-id, but we still try to parse the argument as an
8848 expression to see what happens. */
8849 if (!cp_parser_error_occurred (parser)
8850 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8852 maybe_type_id = true;
8853 cp_parser_abort_tentative_parse (parser);
8857 /* If the next token isn't a `,' or a `>', then this argument wasn't
8858 really finished. This means that the argument is not a valid
8860 if (!cp_parser_next_token_ends_template_argument_p (parser))
8861 cp_parser_error (parser, "expected template-argument");
8862 /* If that worked, we're done. */
8863 if (cp_parser_parse_definitely (parser))
8866 /* We're still not sure what the argument will be. */
8867 cp_parser_parse_tentatively (parser);
8868 /* Try a template. */
8869 argument = cp_parser_id_expression (parser,
8870 /*template_keyword_p=*/false,
8871 /*check_dependency_p=*/true,
8873 /*declarator_p=*/false);
8874 /* If the next token isn't a `,' or a `>', then this argument wasn't
8876 if (!cp_parser_next_token_ends_template_argument_p (parser))
8877 cp_parser_error (parser, "expected template-argument");
8878 if (!cp_parser_error_occurred (parser))
8880 /* Figure out what is being referred to. If the id-expression
8881 was for a class template specialization, then we will have a
8882 TYPE_DECL at this point. There is no need to do name lookup
8883 at this point in that case. */
8884 if (TREE_CODE (argument) != TYPE_DECL)
8885 argument = cp_parser_lookup_name (parser, argument,
8887 /*is_template=*/template_p,
8888 /*is_namespace=*/false,
8889 /*check_dependency=*/true,
8890 /*ambiguous_p=*/NULL);
8891 if (TREE_CODE (argument) != TEMPLATE_DECL
8892 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8893 cp_parser_error (parser, "expected template-name");
8895 if (cp_parser_parse_definitely (parser))
8897 /* It must be a non-type argument. There permitted cases are given
8898 in [temp.arg.nontype]:
8900 -- an integral constant-expression of integral or enumeration
8903 -- the name of a non-type template-parameter; or
8905 -- the name of an object or function with external linkage...
8907 -- the address of an object or function with external linkage...
8909 -- a pointer to member... */
8910 /* Look for a non-type template parameter. */
8911 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8913 cp_parser_parse_tentatively (parser);
8914 argument = cp_parser_primary_expression (parser,
8918 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8919 || !cp_parser_next_token_ends_template_argument_p (parser))
8920 cp_parser_simulate_error (parser);
8921 if (cp_parser_parse_definitely (parser))
8925 /* If the next token is "&", the argument must be the address of an
8926 object or function with external linkage. */
8927 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8929 cp_lexer_consume_token (parser->lexer);
8930 /* See if we might have an id-expression. */
8931 token = cp_lexer_peek_token (parser->lexer);
8932 if (token->type == CPP_NAME
8933 || token->keyword == RID_OPERATOR
8934 || token->type == CPP_SCOPE
8935 || token->type == CPP_TEMPLATE_ID
8936 || token->type == CPP_NESTED_NAME_SPECIFIER)
8938 cp_parser_parse_tentatively (parser);
8939 argument = cp_parser_primary_expression (parser,
8943 if (cp_parser_error_occurred (parser)
8944 || !cp_parser_next_token_ends_template_argument_p (parser))
8945 cp_parser_abort_tentative_parse (parser);
8948 if (TREE_CODE (argument) == INDIRECT_REF)
8950 gcc_assert (REFERENCE_REF_P (argument));
8951 argument = TREE_OPERAND (argument, 0);
8954 if (qualifying_class)
8955 argument = finish_qualified_id_expr (qualifying_class,
8959 if (TREE_CODE (argument) == VAR_DECL)
8961 /* A variable without external linkage might still be a
8962 valid constant-expression, so no error is issued here
8963 if the external-linkage check fails. */
8964 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8965 cp_parser_simulate_error (parser);
8967 else if (is_overloaded_fn (argument))
8968 /* All overloaded functions are allowed; if the external
8969 linkage test does not pass, an error will be issued
8973 && (TREE_CODE (argument) == OFFSET_REF
8974 || TREE_CODE (argument) == SCOPE_REF))
8975 /* A pointer-to-member. */
8977 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
8980 cp_parser_simulate_error (parser);
8982 if (cp_parser_parse_definitely (parser))
8985 argument = build_x_unary_op (ADDR_EXPR, argument);
8990 /* If the argument started with "&", there are no other valid
8991 alternatives at this point. */
8994 cp_parser_error (parser, "invalid non-type template argument");
8995 return error_mark_node;
8998 /* If the argument wasn't successfully parsed as a type-id followed
8999 by '>>', the argument can only be a constant expression now.
9000 Otherwise, we try parsing the constant-expression tentatively,
9001 because the argument could really be a type-id. */
9003 cp_parser_parse_tentatively (parser);
9004 argument = cp_parser_constant_expression (parser,
9005 /*allow_non_constant_p=*/false,
9006 /*non_constant_p=*/NULL);
9007 argument = fold_non_dependent_expr (argument);
9010 if (!cp_parser_next_token_ends_template_argument_p (parser))
9011 cp_parser_error (parser, "expected template-argument");
9012 if (cp_parser_parse_definitely (parser))
9014 /* We did our best to parse the argument as a non type-id, but that
9015 was the only alternative that matched (albeit with a '>' after
9016 it). We can assume it's just a typo from the user, and a
9017 diagnostic will then be issued. */
9018 return cp_parser_type_id (parser);
9021 /* Parse an explicit-instantiation.
9023 explicit-instantiation:
9024 template declaration
9026 Although the standard says `declaration', what it really means is:
9028 explicit-instantiation:
9029 template decl-specifier-seq [opt] declarator [opt] ;
9031 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9032 supposed to be allowed. A defect report has been filed about this
9037 explicit-instantiation:
9038 storage-class-specifier template
9039 decl-specifier-seq [opt] declarator [opt] ;
9040 function-specifier template
9041 decl-specifier-seq [opt] declarator [opt] ; */
9044 cp_parser_explicit_instantiation (cp_parser* parser)
9046 int declares_class_or_enum;
9047 cp_decl_specifier_seq decl_specifiers;
9048 tree extension_specifier = NULL_TREE;
9050 /* Look for an (optional) storage-class-specifier or
9051 function-specifier. */
9052 if (cp_parser_allow_gnu_extensions_p (parser))
9055 = cp_parser_storage_class_specifier_opt (parser);
9056 if (!extension_specifier)
9058 = cp_parser_function_specifier_opt (parser,
9059 /*decl_specs=*/NULL);
9062 /* Look for the `template' keyword. */
9063 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9064 /* Let the front end know that we are processing an explicit
9066 begin_explicit_instantiation ();
9067 /* [temp.explicit] says that we are supposed to ignore access
9068 control while processing explicit instantiation directives. */
9069 push_deferring_access_checks (dk_no_check);
9070 /* Parse a decl-specifier-seq. */
9071 cp_parser_decl_specifier_seq (parser,
9072 CP_PARSER_FLAGS_OPTIONAL,
9074 &declares_class_or_enum);
9075 /* If there was exactly one decl-specifier, and it declared a class,
9076 and there's no declarator, then we have an explicit type
9078 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9082 type = check_tag_decl (&decl_specifiers);
9083 /* Turn access control back on for names used during
9084 template instantiation. */
9085 pop_deferring_access_checks ();
9087 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9091 cp_declarator *declarator;
9094 /* Parse the declarator. */
9096 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9097 /*ctor_dtor_or_conv_p=*/NULL,
9098 /*parenthesized_p=*/NULL,
9099 /*member_p=*/false);
9100 if (declares_class_or_enum & 2)
9101 cp_parser_check_for_definition_in_return_type (declarator,
9102 decl_specifiers.type);
9103 if (declarator != cp_error_declarator)
9105 decl = grokdeclarator (declarator, &decl_specifiers,
9107 /* Turn access control back on for names used during
9108 template instantiation. */
9109 pop_deferring_access_checks ();
9110 /* Do the explicit instantiation. */
9111 do_decl_instantiation (decl, extension_specifier);
9115 pop_deferring_access_checks ();
9116 /* Skip the body of the explicit instantiation. */
9117 cp_parser_skip_to_end_of_statement (parser);
9120 /* We're done with the instantiation. */
9121 end_explicit_instantiation ();
9123 cp_parser_consume_semicolon_at_end_of_statement (parser);
9126 /* Parse an explicit-specialization.
9128 explicit-specialization:
9129 template < > declaration
9131 Although the standard says `declaration', what it really means is:
9133 explicit-specialization:
9134 template <> decl-specifier [opt] init-declarator [opt] ;
9135 template <> function-definition
9136 template <> explicit-specialization
9137 template <> template-declaration */
9140 cp_parser_explicit_specialization (cp_parser* parser)
9142 /* Look for the `template' keyword. */
9143 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9144 /* Look for the `<'. */
9145 cp_parser_require (parser, CPP_LESS, "`<'");
9146 /* Look for the `>'. */
9147 cp_parser_require (parser, CPP_GREATER, "`>'");
9148 /* We have processed another parameter list. */
9149 ++parser->num_template_parameter_lists;
9150 /* Let the front end know that we are beginning a specialization. */
9151 begin_specialization ();
9153 /* If the next keyword is `template', we need to figure out whether
9154 or not we're looking a template-declaration. */
9155 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9157 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9158 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9159 cp_parser_template_declaration_after_export (parser,
9160 /*member_p=*/false);
9162 cp_parser_explicit_specialization (parser);
9165 /* Parse the dependent declaration. */
9166 cp_parser_single_declaration (parser,
9170 /* We're done with the specialization. */
9171 end_specialization ();
9172 /* We're done with this parameter list. */
9173 --parser->num_template_parameter_lists;
9176 /* Parse a type-specifier.
9179 simple-type-specifier
9182 elaborated-type-specifier
9190 Returns a representation of the type-specifier. For a
9191 class-specifier, enum-specifier, or elaborated-type-specifier, a
9192 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9194 The parser flags FLAGS is used to control type-specifier parsing.
9196 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9197 in a decl-specifier-seq.
9199 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9200 class-specifier, enum-specifier, or elaborated-type-specifier, then
9201 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9202 if a type is declared; 2 if it is defined. Otherwise, it is set to
9205 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9206 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9210 cp_parser_type_specifier (cp_parser* parser,
9211 cp_parser_flags flags,
9212 cp_decl_specifier_seq *decl_specs,
9213 bool is_declaration,
9214 int* declares_class_or_enum,
9215 bool* is_cv_qualifier)
9217 tree type_spec = NULL_TREE;
9220 cp_decl_spec ds = ds_last;
9222 /* Assume this type-specifier does not declare a new type. */
9223 if (declares_class_or_enum)
9224 *declares_class_or_enum = 0;
9225 /* And that it does not specify a cv-qualifier. */
9226 if (is_cv_qualifier)
9227 *is_cv_qualifier = false;
9228 /* Peek at the next token. */
9229 token = cp_lexer_peek_token (parser->lexer);
9231 /* If we're looking at a keyword, we can use that to guide the
9232 production we choose. */
9233 keyword = token->keyword;
9237 /* 'enum' [identifier] '{' introduces an enum-specifier;
9238 'enum' <anything else> introduces an elaborated-type-specifier. */
9239 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9240 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9241 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9244 if (parser->num_template_parameter_lists)
9246 error ("template declaration of %qs", "enum");
9247 cp_parser_skip_to_end_of_block_or_statement (parser);
9248 type_spec = error_mark_node;
9251 type_spec = cp_parser_enum_specifier (parser);
9253 if (declares_class_or_enum)
9254 *declares_class_or_enum = 2;
9256 cp_parser_set_decl_spec_type (decl_specs,
9258 /*user_defined_p=*/true);
9262 goto elaborated_type_specifier;
9264 /* Any of these indicate either a class-specifier, or an
9265 elaborated-type-specifier. */
9269 /* Parse tentatively so that we can back up if we don't find a
9271 cp_parser_parse_tentatively (parser);
9272 /* Look for the class-specifier. */
9273 type_spec = cp_parser_class_specifier (parser);
9274 /* If that worked, we're done. */
9275 if (cp_parser_parse_definitely (parser))
9277 if (declares_class_or_enum)
9278 *declares_class_or_enum = 2;
9280 cp_parser_set_decl_spec_type (decl_specs,
9282 /*user_defined_p=*/true);
9287 elaborated_type_specifier:
9288 /* We're declaring (not defining) a class or enum. */
9289 if (declares_class_or_enum)
9290 *declares_class_or_enum = 1;
9294 /* Look for an elaborated-type-specifier. */
9296 = (cp_parser_elaborated_type_specifier
9298 decl_specs && decl_specs->specs[(int) ds_friend],
9301 cp_parser_set_decl_spec_type (decl_specs,
9303 /*user_defined_p=*/true);
9308 if (is_cv_qualifier)
9309 *is_cv_qualifier = true;
9314 if (is_cv_qualifier)
9315 *is_cv_qualifier = true;
9320 if (is_cv_qualifier)
9321 *is_cv_qualifier = true;
9325 /* The `__complex__' keyword is a GNU extension. */
9333 /* Handle simple keywords. */
9338 ++decl_specs->specs[(int)ds];
9339 decl_specs->any_specifiers_p = true;
9341 return cp_lexer_consume_token (parser->lexer)->value;
9344 /* If we do not already have a type-specifier, assume we are looking
9345 at a simple-type-specifier. */
9346 type_spec = cp_parser_simple_type_specifier (parser,
9350 /* If we didn't find a type-specifier, and a type-specifier was not
9351 optional in this context, issue an error message. */
9352 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9354 cp_parser_error (parser, "expected type specifier");
9355 return error_mark_node;
9361 /* Parse a simple-type-specifier.
9363 simple-type-specifier:
9364 :: [opt] nested-name-specifier [opt] type-name
9365 :: [opt] nested-name-specifier template template-id
9380 simple-type-specifier:
9381 __typeof__ unary-expression
9382 __typeof__ ( type-id )
9384 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9385 appropriately updated. */
9388 cp_parser_simple_type_specifier (cp_parser* parser,
9389 cp_decl_specifier_seq *decl_specs,
9390 cp_parser_flags flags)
9392 tree type = NULL_TREE;
9395 /* Peek at the next token. */
9396 token = cp_lexer_peek_token (parser->lexer);
9398 /* If we're looking at a keyword, things are easy. */
9399 switch (token->keyword)
9403 decl_specs->explicit_char_p = true;
9404 type = char_type_node;
9407 type = wchar_type_node;
9410 type = boolean_type_node;
9414 ++decl_specs->specs[(int) ds_short];
9415 type = short_integer_type_node;
9419 decl_specs->explicit_int_p = true;
9420 type = integer_type_node;
9424 ++decl_specs->specs[(int) ds_long];
9425 type = long_integer_type_node;
9429 ++decl_specs->specs[(int) ds_signed];
9430 type = integer_type_node;
9434 ++decl_specs->specs[(int) ds_unsigned];
9435 type = unsigned_type_node;
9438 type = float_type_node;
9441 type = double_type_node;
9444 type = void_type_node;
9448 /* Consume the `typeof' token. */
9449 cp_lexer_consume_token (parser->lexer);
9450 /* Parse the operand to `typeof'. */
9451 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9452 /* If it is not already a TYPE, take its type. */
9454 type = finish_typeof (type);
9457 cp_parser_set_decl_spec_type (decl_specs, type,
9458 /*user_defined_p=*/true);
9466 /* If the type-specifier was for a built-in type, we're done. */
9471 /* Record the type. */
9473 && (token->keyword != RID_SIGNED
9474 && token->keyword != RID_UNSIGNED
9475 && token->keyword != RID_SHORT
9476 && token->keyword != RID_LONG))
9477 cp_parser_set_decl_spec_type (decl_specs,
9479 /*user_defined=*/false);
9481 decl_specs->any_specifiers_p = true;
9483 /* Consume the token. */
9484 id = cp_lexer_consume_token (parser->lexer)->value;
9486 /* There is no valid C++ program where a non-template type is
9487 followed by a "<". That usually indicates that the user thought
9488 that the type was a template. */
9489 cp_parser_check_for_invalid_template_id (parser, type);
9491 return TYPE_NAME (type);
9494 /* The type-specifier must be a user-defined type. */
9495 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9500 /* Don't gobble tokens or issue error messages if this is an
9501 optional type-specifier. */
9502 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9503 cp_parser_parse_tentatively (parser);
9505 /* Look for the optional `::' operator. */
9507 = (cp_parser_global_scope_opt (parser,
9508 /*current_scope_valid_p=*/false)
9510 /* Look for the nested-name specifier. */
9512 = (cp_parser_nested_name_specifier_opt (parser,
9513 /*typename_keyword_p=*/false,
9514 /*check_dependency_p=*/true,
9516 /*is_declaration=*/false)
9518 /* If we have seen a nested-name-specifier, and the next token
9519 is `template', then we are using the template-id production. */
9521 && cp_parser_optional_template_keyword (parser))
9523 /* Look for the template-id. */
9524 type = cp_parser_template_id (parser,
9525 /*template_keyword_p=*/true,
9526 /*check_dependency_p=*/true,
9527 /*is_declaration=*/false);
9528 /* If the template-id did not name a type, we are out of
9530 if (TREE_CODE (type) != TYPE_DECL)
9532 cp_parser_error (parser, "expected template-id for type");
9536 /* Otherwise, look for a type-name. */
9538 type = cp_parser_type_name (parser);
9539 /* Keep track of all name-lookups performed in class scopes. */
9543 && TREE_CODE (type) == TYPE_DECL
9544 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9545 maybe_note_name_used_in_class (DECL_NAME (type), type);
9546 /* If it didn't work out, we don't have a TYPE. */
9547 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9548 && !cp_parser_parse_definitely (parser))
9550 if (type && decl_specs)
9551 cp_parser_set_decl_spec_type (decl_specs, type,
9552 /*user_defined=*/true);
9555 /* If we didn't get a type-name, issue an error message. */
9556 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9558 cp_parser_error (parser, "expected type-name");
9559 return error_mark_node;
9562 /* There is no valid C++ program where a non-template type is
9563 followed by a "<". That usually indicates that the user thought
9564 that the type was a template. */
9565 if (type && type != error_mark_node)
9566 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9571 /* Parse a type-name.
9584 Returns a TYPE_DECL for the type. */
9587 cp_parser_type_name (cp_parser* parser)
9592 /* We can't know yet whether it is a class-name or not. */
9593 cp_parser_parse_tentatively (parser);
9594 /* Try a class-name. */
9595 type_decl = cp_parser_class_name (parser,
9596 /*typename_keyword_p=*/false,
9597 /*template_keyword_p=*/false,
9599 /*check_dependency_p=*/true,
9600 /*class_head_p=*/false,
9601 /*is_declaration=*/false);
9602 /* If it's not a class-name, keep looking. */
9603 if (!cp_parser_parse_definitely (parser))
9605 /* It must be a typedef-name or an enum-name. */
9606 identifier = cp_parser_identifier (parser);
9607 if (identifier == error_mark_node)
9608 return error_mark_node;
9610 /* Look up the type-name. */
9611 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9612 /* Issue an error if we did not find a type-name. */
9613 if (TREE_CODE (type_decl) != TYPE_DECL)
9615 if (!cp_parser_simulate_error (parser))
9616 cp_parser_name_lookup_error (parser, identifier, type_decl,
9618 type_decl = error_mark_node;
9620 /* Remember that the name was used in the definition of the
9621 current class so that we can check later to see if the
9622 meaning would have been different after the class was
9623 entirely defined. */
9624 else if (type_decl != error_mark_node
9626 maybe_note_name_used_in_class (identifier, type_decl);
9633 /* Parse an elaborated-type-specifier. Note that the grammar given
9634 here incorporates the resolution to DR68.
9636 elaborated-type-specifier:
9637 class-key :: [opt] nested-name-specifier [opt] identifier
9638 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9639 enum :: [opt] nested-name-specifier [opt] identifier
9640 typename :: [opt] nested-name-specifier identifier
9641 typename :: [opt] nested-name-specifier template [opt]
9646 elaborated-type-specifier:
9647 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9648 class-key attributes :: [opt] nested-name-specifier [opt]
9649 template [opt] template-id
9650 enum attributes :: [opt] nested-name-specifier [opt] identifier
9652 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9653 declared `friend'. If IS_DECLARATION is TRUE, then this
9654 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9655 something is being declared.
9657 Returns the TYPE specified. */
9660 cp_parser_elaborated_type_specifier (cp_parser* parser,
9662 bool is_declaration)
9664 enum tag_types tag_type;
9666 tree type = NULL_TREE;
9667 tree attributes = NULL_TREE;
9669 /* See if we're looking at the `enum' keyword. */
9670 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9672 /* Consume the `enum' token. */
9673 cp_lexer_consume_token (parser->lexer);
9674 /* Remember that it's an enumeration type. */
9675 tag_type = enum_type;
9676 /* Parse the attributes. */
9677 attributes = cp_parser_attributes_opt (parser);
9679 /* Or, it might be `typename'. */
9680 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9683 /* Consume the `typename' token. */
9684 cp_lexer_consume_token (parser->lexer);
9685 /* Remember that it's a `typename' type. */
9686 tag_type = typename_type;
9687 /* The `typename' keyword is only allowed in templates. */
9688 if (!processing_template_decl)
9689 pedwarn ("using %<typename%> outside of template");
9691 /* Otherwise it must be a class-key. */
9694 tag_type = cp_parser_class_key (parser);
9695 if (tag_type == none_type)
9696 return error_mark_node;
9697 /* Parse the attributes. */
9698 attributes = cp_parser_attributes_opt (parser);
9701 /* Look for the `::' operator. */
9702 cp_parser_global_scope_opt (parser,
9703 /*current_scope_valid_p=*/false);
9704 /* Look for the nested-name-specifier. */
9705 if (tag_type == typename_type)
9707 if (cp_parser_nested_name_specifier (parser,
9708 /*typename_keyword_p=*/true,
9709 /*check_dependency_p=*/true,
9713 return error_mark_node;
9716 /* Even though `typename' is not present, the proposed resolution
9717 to Core Issue 180 says that in `class A<T>::B', `B' should be
9718 considered a type-name, even if `A<T>' is dependent. */
9719 cp_parser_nested_name_specifier_opt (parser,
9720 /*typename_keyword_p=*/true,
9721 /*check_dependency_p=*/true,
9724 /* For everything but enumeration types, consider a template-id. */
9725 if (tag_type != enum_type)
9727 bool template_p = false;
9730 /* Allow the `template' keyword. */
9731 template_p = cp_parser_optional_template_keyword (parser);
9732 /* If we didn't see `template', we don't know if there's a
9733 template-id or not. */
9735 cp_parser_parse_tentatively (parser);
9736 /* Parse the template-id. */
9737 decl = cp_parser_template_id (parser, template_p,
9738 /*check_dependency_p=*/true,
9740 /* If we didn't find a template-id, look for an ordinary
9742 if (!template_p && !cp_parser_parse_definitely (parser))
9744 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9745 in effect, then we must assume that, upon instantiation, the
9746 template will correspond to a class. */
9747 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9748 && tag_type == typename_type)
9749 type = make_typename_type (parser->scope, decl,
9753 type = TREE_TYPE (decl);
9756 /* For an enumeration type, consider only a plain identifier. */
9759 identifier = cp_parser_identifier (parser);
9761 if (identifier == error_mark_node)
9763 parser->scope = NULL_TREE;
9764 return error_mark_node;
9767 /* For a `typename', we needn't call xref_tag. */
9768 if (tag_type == typename_type
9769 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9770 return cp_parser_make_typename_type (parser, parser->scope,
9772 /* Look up a qualified name in the usual way. */
9777 decl = cp_parser_lookup_name (parser, identifier,
9779 /*is_template=*/false,
9780 /*is_namespace=*/false,
9781 /*check_dependency=*/true,
9782 /*ambiguous_p=*/NULL);
9784 /* If we are parsing friend declaration, DECL may be a
9785 TEMPLATE_DECL tree node here. However, we need to check
9786 whether this TEMPLATE_DECL results in valid code. Consider
9787 the following example:
9790 template <class T> class C {};
9793 template <class T> friend class N::C; // #1, valid code
9795 template <class T> class Y {
9796 friend class N::C; // #2, invalid code
9799 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9800 name lookup of `N::C'. We see that friend declaration must
9801 be template for the code to be valid. Note that
9802 processing_template_decl does not work here since it is
9803 always 1 for the above two cases. */
9805 decl = (cp_parser_maybe_treat_template_as_class
9806 (decl, /*tag_name_p=*/is_friend
9807 && parser->num_template_parameter_lists));
9809 if (TREE_CODE (decl) != TYPE_DECL)
9811 cp_parser_diagnose_invalid_type_name (parser,
9814 return error_mark_node;
9817 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9818 check_elaborated_type_specifier
9820 (parser->num_template_parameter_lists
9821 || DECL_SELF_REFERENCE_P (decl)));
9823 type = TREE_TYPE (decl);
9827 /* An elaborated-type-specifier sometimes introduces a new type and
9828 sometimes names an existing type. Normally, the rule is that it
9829 introduces a new type only if there is not an existing type of
9830 the same name already in scope. For example, given:
9833 void f() { struct S s; }
9835 the `struct S' in the body of `f' is the same `struct S' as in
9836 the global scope; the existing definition is used. However, if
9837 there were no global declaration, this would introduce a new
9838 local class named `S'.
9840 An exception to this rule applies to the following code:
9842 namespace N { struct S; }
9844 Here, the elaborated-type-specifier names a new type
9845 unconditionally; even if there is already an `S' in the
9846 containing scope this declaration names a new type.
9847 This exception only applies if the elaborated-type-specifier
9848 forms the complete declaration:
9852 A declaration consisting solely of `class-key identifier ;' is
9853 either a redeclaration of the name in the current scope or a
9854 forward declaration of the identifier as a class name. It
9855 introduces the name into the current scope.
9857 We are in this situation precisely when the next token is a `;'.
9859 An exception to the exception is that a `friend' declaration does
9860 *not* name a new type; i.e., given:
9862 struct S { friend struct T; };
9864 `T' is not a new type in the scope of `S'.
9866 Also, `new struct S' or `sizeof (struct S)' never results in the
9867 definition of a new type; a new type can only be declared in a
9868 declaration context. */
9872 /* Friends have special name lookup rules. */
9873 ts = ts_within_enclosing_non_class;
9874 else if (is_declaration
9875 && cp_lexer_next_token_is (parser->lexer,
9877 /* This is a `class-key identifier ;' */
9882 /* Warn about attributes. They are ignored. */
9884 warning ("type attributes are honored only at type definition");
9886 type = xref_tag (tag_type, identifier, ts,
9887 parser->num_template_parameter_lists);
9890 if (tag_type != enum_type)
9891 cp_parser_check_class_key (tag_type, type);
9893 /* A "<" cannot follow an elaborated type specifier. If that
9894 happens, the user was probably trying to form a template-id. */
9895 cp_parser_check_for_invalid_template_id (parser, type);
9900 /* Parse an enum-specifier.
9903 enum identifier [opt] { enumerator-list [opt] }
9906 enum identifier [opt] { enumerator-list [opt] } attributes
9908 Returns an ENUM_TYPE representing the enumeration. */
9911 cp_parser_enum_specifier (cp_parser* parser)
9916 /* Caller guarantees that the current token is 'enum', an identifier
9917 possibly follows, and the token after that is an opening brace.
9918 If we don't have an identifier, fabricate an anonymous name for
9919 the enumeration being defined. */
9920 cp_lexer_consume_token (parser->lexer);
9922 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9923 identifier = cp_parser_identifier (parser);
9925 identifier = make_anon_name ();
9927 /* Issue an error message if type-definitions are forbidden here. */
9928 cp_parser_check_type_definition (parser);
9930 /* Create the new type. We do this before consuming the opening brace
9931 so the enum will be recorded as being on the line of its tag (or the
9932 'enum' keyword, if there is no tag). */
9933 type = start_enum (identifier);
9935 /* Consume the opening brace. */
9936 cp_lexer_consume_token (parser->lexer);
9938 /* If the next token is not '}', then there are some enumerators. */
9939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9940 cp_parser_enumerator_list (parser, type);
9942 /* Consume the final '}'. */
9943 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9945 /* Look for trailing attributes to apply to this enumeration, and
9946 apply them if appropriate. */
9947 if (cp_parser_allow_gnu_extensions_p (parser))
9949 tree trailing_attr = cp_parser_attributes_opt (parser);
9950 cplus_decl_attributes (&type,
9952 (int) ATTR_FLAG_TYPE_IN_PLACE);
9955 /* Finish up the enumeration. */
9961 /* Parse an enumerator-list. The enumerators all have the indicated
9965 enumerator-definition
9966 enumerator-list , enumerator-definition */
9969 cp_parser_enumerator_list (cp_parser* parser, tree type)
9973 /* Parse an enumerator-definition. */
9974 cp_parser_enumerator_definition (parser, type);
9976 /* If the next token is not a ',', we've reached the end of
9978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9980 /* Otherwise, consume the `,' and keep going. */
9981 cp_lexer_consume_token (parser->lexer);
9982 /* If the next token is a `}', there is a trailing comma. */
9983 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9985 if (pedantic && !in_system_header)
9986 pedwarn ("comma at end of enumerator list");
9992 /* Parse an enumerator-definition. The enumerator has the indicated
9995 enumerator-definition:
9997 enumerator = constant-expression
10003 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10008 /* Look for the identifier. */
10009 identifier = cp_parser_identifier (parser);
10010 if (identifier == error_mark_node)
10013 /* If the next token is an '=', then there is an explicit value. */
10014 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10016 /* Consume the `=' token. */
10017 cp_lexer_consume_token (parser->lexer);
10018 /* Parse the value. */
10019 value = cp_parser_constant_expression (parser,
10020 /*allow_non_constant_p=*/false,
10026 /* Create the enumerator. */
10027 build_enumerator (identifier, value, type);
10030 /* Parse a namespace-name.
10033 original-namespace-name
10036 Returns the NAMESPACE_DECL for the namespace. */
10039 cp_parser_namespace_name (cp_parser* parser)
10042 tree namespace_decl;
10044 /* Get the name of the namespace. */
10045 identifier = cp_parser_identifier (parser);
10046 if (identifier == error_mark_node)
10047 return error_mark_node;
10049 /* Look up the identifier in the currently active scope. Look only
10050 for namespaces, due to:
10052 [basic.lookup.udir]
10054 When looking up a namespace-name in a using-directive or alias
10055 definition, only namespace names are considered.
10059 [basic.lookup.qual]
10061 During the lookup of a name preceding the :: scope resolution
10062 operator, object, function, and enumerator names are ignored.
10064 (Note that cp_parser_class_or_namespace_name only calls this
10065 function if the token after the name is the scope resolution
10067 namespace_decl = cp_parser_lookup_name (parser, identifier,
10069 /*is_template=*/false,
10070 /*is_namespace=*/true,
10071 /*check_dependency=*/true,
10072 /*ambiguous_p=*/NULL);
10073 /* If it's not a namespace, issue an error. */
10074 if (namespace_decl == error_mark_node
10075 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10077 cp_parser_error (parser, "expected namespace-name");
10078 namespace_decl = error_mark_node;
10081 return namespace_decl;
10084 /* Parse a namespace-definition.
10086 namespace-definition:
10087 named-namespace-definition
10088 unnamed-namespace-definition
10090 named-namespace-definition:
10091 original-namespace-definition
10092 extension-namespace-definition
10094 original-namespace-definition:
10095 namespace identifier { namespace-body }
10097 extension-namespace-definition:
10098 namespace original-namespace-name { namespace-body }
10100 unnamed-namespace-definition:
10101 namespace { namespace-body } */
10104 cp_parser_namespace_definition (cp_parser* parser)
10108 /* Look for the `namespace' keyword. */
10109 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10111 /* Get the name of the namespace. We do not attempt to distinguish
10112 between an original-namespace-definition and an
10113 extension-namespace-definition at this point. The semantic
10114 analysis routines are responsible for that. */
10115 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10116 identifier = cp_parser_identifier (parser);
10118 identifier = NULL_TREE;
10120 /* Look for the `{' to start the namespace. */
10121 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10122 /* Start the namespace. */
10123 push_namespace (identifier);
10124 /* Parse the body of the namespace. */
10125 cp_parser_namespace_body (parser);
10126 /* Finish the namespace. */
10128 /* Look for the final `}'. */
10129 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10132 /* Parse a namespace-body.
10135 declaration-seq [opt] */
10138 cp_parser_namespace_body (cp_parser* parser)
10140 cp_parser_declaration_seq_opt (parser);
10143 /* Parse a namespace-alias-definition.
10145 namespace-alias-definition:
10146 namespace identifier = qualified-namespace-specifier ; */
10149 cp_parser_namespace_alias_definition (cp_parser* parser)
10152 tree namespace_specifier;
10154 /* Look for the `namespace' keyword. */
10155 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10156 /* Look for the identifier. */
10157 identifier = cp_parser_identifier (parser);
10158 if (identifier == error_mark_node)
10160 /* Look for the `=' token. */
10161 cp_parser_require (parser, CPP_EQ, "`='");
10162 /* Look for the qualified-namespace-specifier. */
10163 namespace_specifier
10164 = cp_parser_qualified_namespace_specifier (parser);
10165 /* Look for the `;' token. */
10166 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10168 /* Register the alias in the symbol table. */
10169 do_namespace_alias (identifier, namespace_specifier);
10172 /* Parse a qualified-namespace-specifier.
10174 qualified-namespace-specifier:
10175 :: [opt] nested-name-specifier [opt] namespace-name
10177 Returns a NAMESPACE_DECL corresponding to the specified
10181 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10183 /* Look for the optional `::'. */
10184 cp_parser_global_scope_opt (parser,
10185 /*current_scope_valid_p=*/false);
10187 /* Look for the optional nested-name-specifier. */
10188 cp_parser_nested_name_specifier_opt (parser,
10189 /*typename_keyword_p=*/false,
10190 /*check_dependency_p=*/true,
10192 /*is_declaration=*/true);
10194 return cp_parser_namespace_name (parser);
10197 /* Parse a using-declaration.
10200 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10201 using :: unqualified-id ; */
10204 cp_parser_using_declaration (cp_parser* parser)
10207 bool typename_p = false;
10208 bool global_scope_p;
10213 /* Look for the `using' keyword. */
10214 cp_parser_require_keyword (parser, RID_USING, "`using'");
10216 /* Peek at the next token. */
10217 token = cp_lexer_peek_token (parser->lexer);
10218 /* See if it's `typename'. */
10219 if (token->keyword == RID_TYPENAME)
10221 /* Remember that we've seen it. */
10223 /* Consume the `typename' token. */
10224 cp_lexer_consume_token (parser->lexer);
10227 /* Look for the optional global scope qualification. */
10229 = (cp_parser_global_scope_opt (parser,
10230 /*current_scope_valid_p=*/false)
10233 /* If we saw `typename', or didn't see `::', then there must be a
10234 nested-name-specifier present. */
10235 if (typename_p || !global_scope_p)
10236 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10237 /*check_dependency_p=*/true,
10239 /*is_declaration=*/true);
10240 /* Otherwise, we could be in either of the two productions. In that
10241 case, treat the nested-name-specifier as optional. */
10243 qscope = cp_parser_nested_name_specifier_opt (parser,
10244 /*typename_keyword_p=*/false,
10245 /*check_dependency_p=*/true,
10247 /*is_declaration=*/true);
10249 qscope = global_namespace;
10251 /* Parse the unqualified-id. */
10252 identifier = cp_parser_unqualified_id (parser,
10253 /*template_keyword_p=*/false,
10254 /*check_dependency_p=*/true,
10255 /*declarator_p=*/true);
10257 /* The function we call to handle a using-declaration is different
10258 depending on what scope we are in. */
10259 if (identifier == error_mark_node)
10261 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10262 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10263 /* [namespace.udecl]
10265 A using declaration shall not name a template-id. */
10266 error ("a template-id may not appear in a using-declaration");
10269 if (at_class_scope_p ())
10271 /* Create the USING_DECL. */
10272 decl = do_class_using_decl (parser->scope, identifier);
10273 /* Add it to the list of members in this class. */
10274 finish_member_declaration (decl);
10278 decl = cp_parser_lookup_name_simple (parser, identifier);
10279 if (decl == error_mark_node)
10280 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10281 else if (!at_namespace_scope_p ())
10282 do_local_using_decl (decl, qscope, identifier);
10284 do_toplevel_using_decl (decl, qscope, identifier);
10288 /* Look for the final `;'. */
10289 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10292 /* Parse a using-directive.
10295 using namespace :: [opt] nested-name-specifier [opt]
10296 namespace-name ; */
10299 cp_parser_using_directive (cp_parser* parser)
10301 tree namespace_decl;
10304 /* Look for the `using' keyword. */
10305 cp_parser_require_keyword (parser, RID_USING, "`using'");
10306 /* And the `namespace' keyword. */
10307 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10308 /* Look for the optional `::' operator. */
10309 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10310 /* And the optional nested-name-specifier. */
10311 cp_parser_nested_name_specifier_opt (parser,
10312 /*typename_keyword_p=*/false,
10313 /*check_dependency_p=*/true,
10315 /*is_declaration=*/true);
10316 /* Get the namespace being used. */
10317 namespace_decl = cp_parser_namespace_name (parser);
10318 /* And any specified attributes. */
10319 attribs = cp_parser_attributes_opt (parser);
10320 /* Update the symbol table. */
10321 parse_using_directive (namespace_decl, attribs);
10322 /* Look for the final `;'. */
10323 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10326 /* Parse an asm-definition.
10329 asm ( string-literal ) ;
10334 asm volatile [opt] ( string-literal ) ;
10335 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10336 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10337 : asm-operand-list [opt] ) ;
10338 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10339 : asm-operand-list [opt]
10340 : asm-operand-list [opt] ) ; */
10343 cp_parser_asm_definition (cp_parser* parser)
10346 tree outputs = NULL_TREE;
10347 tree inputs = NULL_TREE;
10348 tree clobbers = NULL_TREE;
10350 bool volatile_p = false;
10351 bool extended_p = false;
10353 /* Look for the `asm' keyword. */
10354 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10355 /* See if the next token is `volatile'. */
10356 if (cp_parser_allow_gnu_extensions_p (parser)
10357 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10359 /* Remember that we saw the `volatile' keyword. */
10361 /* Consume the token. */
10362 cp_lexer_consume_token (parser->lexer);
10364 /* Look for the opening `('. */
10365 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10367 /* Look for the string. */
10368 string = cp_parser_string_literal (parser, false, false);
10369 if (string == error_mark_node)
10371 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10372 /*consume_paren=*/true);
10376 /* If we're allowing GNU extensions, check for the extended assembly
10377 syntax. Unfortunately, the `:' tokens need not be separated by
10378 a space in C, and so, for compatibility, we tolerate that here
10379 too. Doing that means that we have to treat the `::' operator as
10381 if (cp_parser_allow_gnu_extensions_p (parser)
10382 && at_function_scope_p ()
10383 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10384 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10386 bool inputs_p = false;
10387 bool clobbers_p = false;
10389 /* The extended syntax was used. */
10392 /* Look for outputs. */
10393 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10395 /* Consume the `:'. */
10396 cp_lexer_consume_token (parser->lexer);
10397 /* Parse the output-operands. */
10398 if (cp_lexer_next_token_is_not (parser->lexer,
10400 && cp_lexer_next_token_is_not (parser->lexer,
10402 && cp_lexer_next_token_is_not (parser->lexer,
10404 outputs = cp_parser_asm_operand_list (parser);
10406 /* If the next token is `::', there are no outputs, and the
10407 next token is the beginning of the inputs. */
10408 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10409 /* The inputs are coming next. */
10412 /* Look for inputs. */
10414 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10416 /* Consume the `:' or `::'. */
10417 cp_lexer_consume_token (parser->lexer);
10418 /* Parse the output-operands. */
10419 if (cp_lexer_next_token_is_not (parser->lexer,
10421 && cp_lexer_next_token_is_not (parser->lexer,
10423 inputs = cp_parser_asm_operand_list (parser);
10425 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10426 /* The clobbers are coming next. */
10429 /* Look for clobbers. */
10431 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10433 /* Consume the `:' or `::'. */
10434 cp_lexer_consume_token (parser->lexer);
10435 /* Parse the clobbers. */
10436 if (cp_lexer_next_token_is_not (parser->lexer,
10438 clobbers = cp_parser_asm_clobber_list (parser);
10441 /* Look for the closing `)'. */
10442 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10443 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10444 /*consume_paren=*/true);
10445 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10447 /* Create the ASM_EXPR. */
10448 if (at_function_scope_p ())
10450 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10452 /* If the extended syntax was not used, mark the ASM_EXPR. */
10455 tree temp = asm_stmt;
10456 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10457 temp = TREE_OPERAND (temp, 0);
10459 ASM_INPUT_P (temp) = 1;
10463 assemble_asm (string);
10466 /* Declarators [gram.dcl.decl] */
10468 /* Parse an init-declarator.
10471 declarator initializer [opt]
10476 declarator asm-specification [opt] attributes [opt] initializer [opt]
10478 function-definition:
10479 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10481 decl-specifier-seq [opt] declarator function-try-block
10485 function-definition:
10486 __extension__ function-definition
10488 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10489 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10490 then this declarator appears in a class scope. The new DECL created
10491 by this declarator is returned.
10493 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10494 for a function-definition here as well. If the declarator is a
10495 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10496 be TRUE upon return. By that point, the function-definition will
10497 have been completely parsed.
10499 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10503 cp_parser_init_declarator (cp_parser* parser,
10504 cp_decl_specifier_seq *decl_specifiers,
10505 bool function_definition_allowed_p,
10507 int declares_class_or_enum,
10508 bool* function_definition_p)
10511 cp_declarator *declarator;
10512 tree prefix_attributes;
10514 tree asm_specification;
10516 tree decl = NULL_TREE;
10518 bool is_initialized;
10519 bool is_parenthesized_init;
10520 bool is_non_constant_init;
10521 int ctor_dtor_or_conv_p;
10523 tree pushed_scope = NULL;
10525 /* Gather the attributes that were provided with the
10526 decl-specifiers. */
10527 prefix_attributes = decl_specifiers->attributes;
10529 /* Assume that this is not the declarator for a function
10531 if (function_definition_p)
10532 *function_definition_p = false;
10534 /* Defer access checks while parsing the declarator; we cannot know
10535 what names are accessible until we know what is being
10537 resume_deferring_access_checks ();
10539 /* Parse the declarator. */
10541 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10542 &ctor_dtor_or_conv_p,
10543 /*parenthesized_p=*/NULL,
10544 /*member_p=*/false);
10545 /* Gather up the deferred checks. */
10546 stop_deferring_access_checks ();
10548 /* If the DECLARATOR was erroneous, there's no need to go
10550 if (declarator == cp_error_declarator)
10551 return error_mark_node;
10553 if (declares_class_or_enum & 2)
10554 cp_parser_check_for_definition_in_return_type (declarator,
10555 decl_specifiers->type);
10557 /* Figure out what scope the entity declared by the DECLARATOR is
10558 located in. `grokdeclarator' sometimes changes the scope, so
10559 we compute it now. */
10560 scope = get_scope_of_declarator (declarator);
10562 /* If we're allowing GNU extensions, look for an asm-specification
10564 if (cp_parser_allow_gnu_extensions_p (parser))
10566 /* Look for an asm-specification. */
10567 asm_specification = cp_parser_asm_specification_opt (parser);
10568 /* And attributes. */
10569 attributes = cp_parser_attributes_opt (parser);
10573 asm_specification = NULL_TREE;
10574 attributes = NULL_TREE;
10577 /* Peek at the next token. */
10578 token = cp_lexer_peek_token (parser->lexer);
10579 /* Check to see if the token indicates the start of a
10580 function-definition. */
10581 if (cp_parser_token_starts_function_definition_p (token))
10583 if (!function_definition_allowed_p)
10585 /* If a function-definition should not appear here, issue an
10587 cp_parser_error (parser,
10588 "a function-definition is not allowed here");
10589 return error_mark_node;
10593 /* Neither attributes nor an asm-specification are allowed
10594 on a function-definition. */
10595 if (asm_specification)
10596 error ("an asm-specification is not allowed on a function-definition");
10598 error ("attributes are not allowed on a function-definition");
10599 /* This is a function-definition. */
10600 *function_definition_p = true;
10602 /* Parse the function definition. */
10604 decl = cp_parser_save_member_function_body (parser,
10607 prefix_attributes);
10610 = (cp_parser_function_definition_from_specifiers_and_declarator
10611 (parser, decl_specifiers, prefix_attributes, declarator));
10619 Only in function declarations for constructors, destructors, and
10620 type conversions can the decl-specifier-seq be omitted.
10622 We explicitly postpone this check past the point where we handle
10623 function-definitions because we tolerate function-definitions
10624 that are missing their return types in some modes. */
10625 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10627 cp_parser_error (parser,
10628 "expected constructor, destructor, or type conversion");
10629 return error_mark_node;
10632 /* An `=' or an `(' indicates an initializer. */
10633 is_initialized = (token->type == CPP_EQ
10634 || token->type == CPP_OPEN_PAREN);
10635 /* If the init-declarator isn't initialized and isn't followed by a
10636 `,' or `;', it's not a valid init-declarator. */
10637 if (!is_initialized
10638 && token->type != CPP_COMMA
10639 && token->type != CPP_SEMICOLON)
10641 cp_parser_error (parser, "expected initializer");
10642 return error_mark_node;
10645 /* Because start_decl has side-effects, we should only call it if we
10646 know we're going ahead. By this point, we know that we cannot
10647 possibly be looking at any other construct. */
10648 cp_parser_commit_to_tentative_parse (parser);
10650 /* If the decl specifiers were bad, issue an error now that we're
10651 sure this was intended to be a declarator. Then continue
10652 declaring the variable(s), as int, to try to cut down on further
10654 if (decl_specifiers->any_specifiers_p
10655 && decl_specifiers->type == error_mark_node)
10657 cp_parser_error (parser, "invalid type in declaration");
10658 decl_specifiers->type = integer_type_node;
10661 /* Check to see whether or not this declaration is a friend. */
10662 friend_p = cp_parser_friend_p (decl_specifiers);
10664 /* Check that the number of template-parameter-lists is OK. */
10665 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10666 return error_mark_node;
10668 /* Enter the newly declared entry in the symbol table. If we're
10669 processing a declaration in a class-specifier, we wait until
10670 after processing the initializer. */
10673 if (parser->in_unbraced_linkage_specification_p)
10675 decl_specifiers->storage_class = sc_extern;
10676 have_extern_spec = false;
10678 decl = start_decl (declarator, decl_specifiers,
10679 is_initialized, attributes, prefix_attributes,
10683 /* Enter the SCOPE. That way unqualified names appearing in the
10684 initializer will be looked up in SCOPE. */
10685 pushed_scope = push_scope (scope);
10687 /* Perform deferred access control checks, now that we know in which
10688 SCOPE the declared entity resides. */
10689 if (!member_p && decl)
10691 tree saved_current_function_decl = NULL_TREE;
10693 /* If the entity being declared is a function, pretend that we
10694 are in its scope. If it is a `friend', it may have access to
10695 things that would not otherwise be accessible. */
10696 if (TREE_CODE (decl) == FUNCTION_DECL)
10698 saved_current_function_decl = current_function_decl;
10699 current_function_decl = decl;
10702 /* Perform the access control checks for the declarator and the
10703 the decl-specifiers. */
10704 perform_deferred_access_checks ();
10706 /* Restore the saved value. */
10707 if (TREE_CODE (decl) == FUNCTION_DECL)
10708 current_function_decl = saved_current_function_decl;
10711 /* Parse the initializer. */
10712 if (is_initialized)
10713 initializer = cp_parser_initializer (parser,
10714 &is_parenthesized_init,
10715 &is_non_constant_init);
10718 initializer = NULL_TREE;
10719 is_parenthesized_init = false;
10720 is_non_constant_init = true;
10723 /* The old parser allows attributes to appear after a parenthesized
10724 initializer. Mark Mitchell proposed removing this functionality
10725 on the GCC mailing lists on 2002-08-13. This parser accepts the
10726 attributes -- but ignores them. */
10727 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10728 if (cp_parser_attributes_opt (parser))
10729 warning ("attributes after parenthesized initializer ignored");
10731 /* For an in-class declaration, use `grokfield' to create the
10737 pop_scope (pushed_scope);
10738 pushed_scope = false;
10740 decl = grokfield (declarator, decl_specifiers,
10741 initializer, /*asmspec=*/NULL_TREE,
10742 /*attributes=*/NULL_TREE);
10743 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10744 cp_parser_save_default_args (parser, decl);
10747 /* Finish processing the declaration. But, skip friend
10749 if (!friend_p && decl && decl != error_mark_node)
10751 cp_finish_decl (decl,
10754 /* If the initializer is in parentheses, then this is
10755 a direct-initialization, which means that an
10756 `explicit' constructor is OK. Otherwise, an
10757 `explicit' constructor cannot be used. */
10758 ((is_parenthesized_init || !is_initialized)
10759 ? 0 : LOOKUP_ONLYCONVERTING));
10761 if (!friend_p && pushed_scope)
10762 pop_scope (pushed_scope);
10764 /* Remember whether or not variables were initialized by
10765 constant-expressions. */
10766 if (decl && TREE_CODE (decl) == VAR_DECL
10767 && is_initialized && !is_non_constant_init)
10768 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10773 /* Parse a declarator.
10777 ptr-operator declarator
10779 abstract-declarator:
10780 ptr-operator abstract-declarator [opt]
10781 direct-abstract-declarator
10786 attributes [opt] direct-declarator
10787 attributes [opt] ptr-operator declarator
10789 abstract-declarator:
10790 attributes [opt] ptr-operator abstract-declarator [opt]
10791 attributes [opt] direct-abstract-declarator
10793 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10794 detect constructor, destructor or conversion operators. It is set
10795 to -1 if the declarator is a name, and +1 if it is a
10796 function. Otherwise it is set to zero. Usually you just want to
10797 test for >0, but internally the negative value is used.
10799 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10800 a decl-specifier-seq unless it declares a constructor, destructor,
10801 or conversion. It might seem that we could check this condition in
10802 semantic analysis, rather than parsing, but that makes it difficult
10803 to handle something like `f()'. We want to notice that there are
10804 no decl-specifiers, and therefore realize that this is an
10805 expression, not a declaration.)
10807 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10808 the declarator is a direct-declarator of the form "(...)".
10810 MEMBER_P is true iff this declarator is a member-declarator. */
10812 static cp_declarator *
10813 cp_parser_declarator (cp_parser* parser,
10814 cp_parser_declarator_kind dcl_kind,
10815 int* ctor_dtor_or_conv_p,
10816 bool* parenthesized_p,
10820 cp_declarator *declarator;
10821 enum tree_code code;
10822 cp_cv_quals cv_quals;
10824 tree attributes = NULL_TREE;
10826 /* Assume this is not a constructor, destructor, or type-conversion
10828 if (ctor_dtor_or_conv_p)
10829 *ctor_dtor_or_conv_p = 0;
10831 if (cp_parser_allow_gnu_extensions_p (parser))
10832 attributes = cp_parser_attributes_opt (parser);
10834 /* Peek at the next token. */
10835 token = cp_lexer_peek_token (parser->lexer);
10837 /* Check for the ptr-operator production. */
10838 cp_parser_parse_tentatively (parser);
10839 /* Parse the ptr-operator. */
10840 code = cp_parser_ptr_operator (parser,
10843 /* If that worked, then we have a ptr-operator. */
10844 if (cp_parser_parse_definitely (parser))
10846 /* If a ptr-operator was found, then this declarator was not
10848 if (parenthesized_p)
10849 *parenthesized_p = true;
10850 /* The dependent declarator is optional if we are parsing an
10851 abstract-declarator. */
10852 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10853 cp_parser_parse_tentatively (parser);
10855 /* Parse the dependent declarator. */
10856 declarator = cp_parser_declarator (parser, dcl_kind,
10857 /*ctor_dtor_or_conv_p=*/NULL,
10858 /*parenthesized_p=*/NULL,
10859 /*member_p=*/false);
10861 /* If we are parsing an abstract-declarator, we must handle the
10862 case where the dependent declarator is absent. */
10863 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10864 && !cp_parser_parse_definitely (parser))
10867 /* Build the representation of the ptr-operator. */
10869 declarator = make_ptrmem_declarator (cv_quals,
10872 else if (code == INDIRECT_REF)
10873 declarator = make_pointer_declarator (cv_quals, declarator);
10875 declarator = make_reference_declarator (cv_quals, declarator);
10877 /* Everything else is a direct-declarator. */
10880 if (parenthesized_p)
10881 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10883 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10884 ctor_dtor_or_conv_p,
10888 if (attributes && declarator != cp_error_declarator)
10889 declarator->attributes = attributes;
10894 /* Parse a direct-declarator or direct-abstract-declarator.
10898 direct-declarator ( parameter-declaration-clause )
10899 cv-qualifier-seq [opt]
10900 exception-specification [opt]
10901 direct-declarator [ constant-expression [opt] ]
10904 direct-abstract-declarator:
10905 direct-abstract-declarator [opt]
10906 ( parameter-declaration-clause )
10907 cv-qualifier-seq [opt]
10908 exception-specification [opt]
10909 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10910 ( abstract-declarator )
10912 Returns a representation of the declarator. DCL_KIND is
10913 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10914 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10915 we are parsing a direct-declarator. It is
10916 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10917 of ambiguity we prefer an abstract declarator, as per
10918 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10919 cp_parser_declarator. */
10921 static cp_declarator *
10922 cp_parser_direct_declarator (cp_parser* parser,
10923 cp_parser_declarator_kind dcl_kind,
10924 int* ctor_dtor_or_conv_p,
10928 cp_declarator *declarator = NULL;
10929 tree scope = NULL_TREE;
10930 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10931 bool saved_in_declarator_p = parser->in_declarator_p;
10933 tree pushed_scope = NULL_TREE;
10937 /* Peek at the next token. */
10938 token = cp_lexer_peek_token (parser->lexer);
10939 if (token->type == CPP_OPEN_PAREN)
10941 /* This is either a parameter-declaration-clause, or a
10942 parenthesized declarator. When we know we are parsing a
10943 named declarator, it must be a parenthesized declarator
10944 if FIRST is true. For instance, `(int)' is a
10945 parameter-declaration-clause, with an omitted
10946 direct-abstract-declarator. But `((*))', is a
10947 parenthesized abstract declarator. Finally, when T is a
10948 template parameter `(T)' is a
10949 parameter-declaration-clause, and not a parenthesized
10952 We first try and parse a parameter-declaration-clause,
10953 and then try a nested declarator (if FIRST is true).
10955 It is not an error for it not to be a
10956 parameter-declaration-clause, even when FIRST is
10962 The first is the declaration of a function while the
10963 second is a the definition of a variable, including its
10966 Having seen only the parenthesis, we cannot know which of
10967 these two alternatives should be selected. Even more
10968 complex are examples like:
10973 The former is a function-declaration; the latter is a
10974 variable initialization.
10976 Thus again, we try a parameter-declaration-clause, and if
10977 that fails, we back out and return. */
10979 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10981 cp_parameter_declarator *params;
10982 unsigned saved_num_template_parameter_lists;
10984 /* In a member-declarator, the only valid interpretation
10985 of a parenthesis is the start of a
10986 parameter-declaration-clause. (It is invalid to
10987 initialize a static data member with a parenthesized
10988 initializer; only the "=" form of initialization is
10991 cp_parser_parse_tentatively (parser);
10993 /* Consume the `('. */
10994 cp_lexer_consume_token (parser->lexer);
10997 /* If this is going to be an abstract declarator, we're
10998 in a declarator and we can't have default args. */
10999 parser->default_arg_ok_p = false;
11000 parser->in_declarator_p = true;
11003 /* Inside the function parameter list, surrounding
11004 template-parameter-lists do not apply. */
11005 saved_num_template_parameter_lists
11006 = parser->num_template_parameter_lists;
11007 parser->num_template_parameter_lists = 0;
11009 /* Parse the parameter-declaration-clause. */
11010 params = cp_parser_parameter_declaration_clause (parser);
11012 parser->num_template_parameter_lists
11013 = saved_num_template_parameter_lists;
11015 /* If all went well, parse the cv-qualifier-seq and the
11016 exception-specification. */
11017 if (member_p || cp_parser_parse_definitely (parser))
11019 cp_cv_quals cv_quals;
11020 tree exception_specification;
11022 if (ctor_dtor_or_conv_p)
11023 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11025 /* Consume the `)'. */
11026 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11028 /* Parse the cv-qualifier-seq. */
11029 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11030 /* And the exception-specification. */
11031 exception_specification
11032 = cp_parser_exception_specification_opt (parser);
11034 /* Create the function-declarator. */
11035 declarator = make_call_declarator (declarator,
11038 exception_specification);
11039 /* Any subsequent parameter lists are to do with
11040 return type, so are not those of the declared
11042 parser->default_arg_ok_p = false;
11044 /* Repeat the main loop. */
11049 /* If this is the first, we can try a parenthesized
11053 bool saved_in_type_id_in_expr_p;
11055 parser->default_arg_ok_p = saved_default_arg_ok_p;
11056 parser->in_declarator_p = saved_in_declarator_p;
11058 /* Consume the `('. */
11059 cp_lexer_consume_token (parser->lexer);
11060 /* Parse the nested declarator. */
11061 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11062 parser->in_type_id_in_expr_p = true;
11064 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11065 /*parenthesized_p=*/NULL,
11067 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11069 /* Expect a `)'. */
11070 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11071 declarator = cp_error_declarator;
11072 if (declarator == cp_error_declarator)
11075 goto handle_declarator;
11077 /* Otherwise, we must be done. */
11081 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11082 && token->type == CPP_OPEN_SQUARE)
11084 /* Parse an array-declarator. */
11087 if (ctor_dtor_or_conv_p)
11088 *ctor_dtor_or_conv_p = 0;
11091 parser->default_arg_ok_p = false;
11092 parser->in_declarator_p = true;
11093 /* Consume the `['. */
11094 cp_lexer_consume_token (parser->lexer);
11095 /* Peek at the next token. */
11096 token = cp_lexer_peek_token (parser->lexer);
11097 /* If the next token is `]', then there is no
11098 constant-expression. */
11099 if (token->type != CPP_CLOSE_SQUARE)
11101 bool non_constant_p;
11104 = cp_parser_constant_expression (parser,
11105 /*allow_non_constant=*/true,
11107 if (!non_constant_p)
11108 bounds = fold_non_dependent_expr (bounds);
11109 /* Normally, the array bound must be an integral constant
11110 expression. However, as an extension, we allow VLAs
11111 in function scopes. */
11112 else if (!at_function_scope_p ())
11114 error ("array bound is not an integer constant");
11115 bounds = error_mark_node;
11119 bounds = NULL_TREE;
11120 /* Look for the closing `]'. */
11121 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11123 declarator = cp_error_declarator;
11127 declarator = make_array_declarator (declarator, bounds);
11129 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11131 tree qualifying_scope;
11132 tree unqualified_name;
11134 /* Parse a declarator-id */
11135 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11136 cp_parser_parse_tentatively (parser);
11137 unqualified_name = cp_parser_declarator_id (parser);
11138 qualifying_scope = parser->scope;
11139 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11141 if (!cp_parser_parse_definitely (parser))
11142 unqualified_name = error_mark_node;
11143 else if (qualifying_scope
11144 || (TREE_CODE (unqualified_name)
11145 != IDENTIFIER_NODE))
11147 cp_parser_error (parser, "expected unqualified-id");
11148 unqualified_name = error_mark_node;
11152 if (unqualified_name == error_mark_node)
11154 declarator = cp_error_declarator;
11158 if (qualifying_scope && at_namespace_scope_p ()
11159 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11161 /* In the declaration of a member of a template class
11162 outside of the class itself, the SCOPE will sometimes
11163 be a TYPENAME_TYPE. For example, given:
11165 template <typename T>
11166 int S<T>::R::i = 3;
11168 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11169 this context, we must resolve S<T>::R to an ordinary
11170 type, rather than a typename type.
11172 The reason we normally avoid resolving TYPENAME_TYPEs
11173 is that a specialization of `S' might render
11174 `S<T>::R' not a type. However, if `S' is
11175 specialized, then this `i' will not be used, so there
11176 is no harm in resolving the types here. */
11179 /* Resolve the TYPENAME_TYPE. */
11180 type = resolve_typename_type (qualifying_scope,
11181 /*only_current_p=*/false);
11182 /* If that failed, the declarator is invalid. */
11183 if (type == error_mark_node)
11184 error ("%<%T::%D%> is not a type",
11185 TYPE_CONTEXT (qualifying_scope),
11186 TYPE_IDENTIFIER (qualifying_scope));
11187 qualifying_scope = type;
11190 declarator = make_id_declarator (qualifying_scope,
11192 declarator->id_loc = token->location;
11193 if (unqualified_name)
11197 if (qualifying_scope
11198 && CLASS_TYPE_P (qualifying_scope))
11199 class_type = qualifying_scope;
11201 class_type = current_class_type;
11205 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11206 declarator->u.id.sfk = sfk_destructor;
11207 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11208 declarator->u.id.sfk = sfk_conversion;
11209 else if (/* There's no way to declare a constructor
11210 for an anonymous type, even if the type
11211 got a name for linkage purposes. */
11212 !TYPE_WAS_ANONYMOUS (class_type)
11213 && (constructor_name_p (unqualified_name,
11215 || (TREE_CODE (unqualified_name) == TYPE_DECL
11217 (TREE_TYPE (unqualified_name),
11219 declarator->u.id.sfk = sfk_constructor;
11221 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11222 *ctor_dtor_or_conv_p = -1;
11223 if (qualifying_scope
11224 && TREE_CODE (unqualified_name) == TYPE_DECL
11225 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11227 error ("invalid use of constructor as a template");
11228 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11229 "the constructor in a qualified name",
11231 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11232 class_type, class_type);
11237 handle_declarator:;
11238 scope = get_scope_of_declarator (declarator);
11240 /* Any names that appear after the declarator-id for a
11241 member are looked up in the containing scope. */
11242 pushed_scope = push_scope (scope);
11243 parser->in_declarator_p = true;
11244 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11245 || (declarator && declarator->kind == cdk_id))
11246 /* Default args are only allowed on function
11248 parser->default_arg_ok_p = saved_default_arg_ok_p;
11250 parser->default_arg_ok_p = false;
11259 /* For an abstract declarator, we might wind up with nothing at this
11260 point. That's an error; the declarator is not optional. */
11262 cp_parser_error (parser, "expected declarator");
11264 /* If we entered a scope, we must exit it now. */
11266 pop_scope (pushed_scope);
11268 parser->default_arg_ok_p = saved_default_arg_ok_p;
11269 parser->in_declarator_p = saved_in_declarator_p;
11274 /* Parse a ptr-operator.
11277 * cv-qualifier-seq [opt]
11279 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11284 & cv-qualifier-seq [opt]
11286 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11287 Returns ADDR_EXPR if a reference was used. In the case of a
11288 pointer-to-member, *TYPE is filled in with the TYPE containing the
11289 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11290 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11291 ERROR_MARK if an error occurred. */
11293 static enum tree_code
11294 cp_parser_ptr_operator (cp_parser* parser,
11296 cp_cv_quals *cv_quals)
11298 enum tree_code code = ERROR_MARK;
11301 /* Assume that it's not a pointer-to-member. */
11303 /* And that there are no cv-qualifiers. */
11304 *cv_quals = TYPE_UNQUALIFIED;
11306 /* Peek at the next token. */
11307 token = cp_lexer_peek_token (parser->lexer);
11308 /* If it's a `*' or `&' we have a pointer or reference. */
11309 if (token->type == CPP_MULT || token->type == CPP_AND)
11311 /* Remember which ptr-operator we were processing. */
11312 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11314 /* Consume the `*' or `&'. */
11315 cp_lexer_consume_token (parser->lexer);
11317 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11318 `&', if we are allowing GNU extensions. (The only qualifier
11319 that can legally appear after `&' is `restrict', but that is
11320 enforced during semantic analysis. */
11321 if (code == INDIRECT_REF
11322 || cp_parser_allow_gnu_extensions_p (parser))
11323 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11327 /* Try the pointer-to-member case. */
11328 cp_parser_parse_tentatively (parser);
11329 /* Look for the optional `::' operator. */
11330 cp_parser_global_scope_opt (parser,
11331 /*current_scope_valid_p=*/false);
11332 /* Look for the nested-name specifier. */
11333 cp_parser_nested_name_specifier (parser,
11334 /*typename_keyword_p=*/false,
11335 /*check_dependency_p=*/true,
11337 /*is_declaration=*/false);
11338 /* If we found it, and the next token is a `*', then we are
11339 indeed looking at a pointer-to-member operator. */
11340 if (!cp_parser_error_occurred (parser)
11341 && cp_parser_require (parser, CPP_MULT, "`*'"))
11343 /* The type of which the member is a member is given by the
11345 *type = parser->scope;
11346 /* The next name will not be qualified. */
11347 parser->scope = NULL_TREE;
11348 parser->qualifying_scope = NULL_TREE;
11349 parser->object_scope = NULL_TREE;
11350 /* Indicate that the `*' operator was used. */
11351 code = INDIRECT_REF;
11352 /* Look for the optional cv-qualifier-seq. */
11353 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11355 /* If that didn't work we don't have a ptr-operator. */
11356 if (!cp_parser_parse_definitely (parser))
11357 cp_parser_error (parser, "expected ptr-operator");
11363 /* Parse an (optional) cv-qualifier-seq.
11366 cv-qualifier cv-qualifier-seq [opt]
11377 Returns a bitmask representing the cv-qualifiers. */
11380 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11382 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11387 cp_cv_quals cv_qualifier;
11389 /* Peek at the next token. */
11390 token = cp_lexer_peek_token (parser->lexer);
11391 /* See if it's a cv-qualifier. */
11392 switch (token->keyword)
11395 cv_qualifier = TYPE_QUAL_CONST;
11399 cv_qualifier = TYPE_QUAL_VOLATILE;
11403 cv_qualifier = TYPE_QUAL_RESTRICT;
11407 cv_qualifier = TYPE_UNQUALIFIED;
11414 if (cv_quals & cv_qualifier)
11416 error ("duplicate cv-qualifier");
11417 cp_lexer_purge_token (parser->lexer);
11421 cp_lexer_consume_token (parser->lexer);
11422 cv_quals |= cv_qualifier;
11429 /* Parse a declarator-id.
11433 :: [opt] nested-name-specifier [opt] type-name
11435 In the `id-expression' case, the value returned is as for
11436 cp_parser_id_expression if the id-expression was an unqualified-id.
11437 If the id-expression was a qualified-id, then a SCOPE_REF is
11438 returned. The first operand is the scope (either a NAMESPACE_DECL
11439 or TREE_TYPE), but the second is still just a representation of an
11443 cp_parser_declarator_id (cp_parser* parser)
11445 /* The expression must be an id-expression. Assume that qualified
11446 names are the names of types so that:
11449 int S<T>::R::i = 3;
11451 will work; we must treat `S<T>::R' as the name of a type.
11452 Similarly, assume that qualified names are templates, where
11456 int S<T>::R<T>::i = 3;
11459 return cp_parser_id_expression (parser,
11460 /*template_keyword_p=*/false,
11461 /*check_dependency_p=*/false,
11462 /*template_p=*/NULL,
11463 /*declarator_p=*/true);
11466 /* Parse a type-id.
11469 type-specifier-seq abstract-declarator [opt]
11471 Returns the TYPE specified. */
11474 cp_parser_type_id (cp_parser* parser)
11476 cp_decl_specifier_seq type_specifier_seq;
11477 cp_declarator *abstract_declarator;
11479 /* Parse the type-specifier-seq. */
11480 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11481 if (type_specifier_seq.type == error_mark_node)
11482 return error_mark_node;
11484 /* There might or might not be an abstract declarator. */
11485 cp_parser_parse_tentatively (parser);
11486 /* Look for the declarator. */
11487 abstract_declarator
11488 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11489 /*parenthesized_p=*/NULL,
11490 /*member_p=*/false);
11491 /* Check to see if there really was a declarator. */
11492 if (!cp_parser_parse_definitely (parser))
11493 abstract_declarator = NULL;
11495 return groktypename (&type_specifier_seq, abstract_declarator);
11498 /* Parse a type-specifier-seq.
11500 type-specifier-seq:
11501 type-specifier type-specifier-seq [opt]
11505 type-specifier-seq:
11506 attributes type-specifier-seq [opt]
11508 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11511 cp_parser_type_specifier_seq (cp_parser* parser,
11512 cp_decl_specifier_seq *type_specifier_seq)
11514 bool seen_type_specifier = false;
11516 /* Clear the TYPE_SPECIFIER_SEQ. */
11517 clear_decl_specs (type_specifier_seq);
11519 /* Parse the type-specifiers and attributes. */
11522 tree type_specifier;
11524 /* Check for attributes first. */
11525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11527 type_specifier_seq->attributes =
11528 chainon (type_specifier_seq->attributes,
11529 cp_parser_attributes_opt (parser));
11533 /* Look for the type-specifier. */
11534 type_specifier = cp_parser_type_specifier (parser,
11535 CP_PARSER_FLAGS_OPTIONAL,
11536 type_specifier_seq,
11537 /*is_declaration=*/false,
11540 /* If the first type-specifier could not be found, this is not a
11541 type-specifier-seq at all. */
11542 if (!seen_type_specifier && !type_specifier)
11544 cp_parser_error (parser, "expected type-specifier");
11545 type_specifier_seq->type = error_mark_node;
11548 /* If subsequent type-specifiers could not be found, the
11549 type-specifier-seq is complete. */
11550 else if (seen_type_specifier && !type_specifier)
11553 seen_type_specifier = true;
11559 /* Parse a parameter-declaration-clause.
11561 parameter-declaration-clause:
11562 parameter-declaration-list [opt] ... [opt]
11563 parameter-declaration-list , ...
11565 Returns a representation for the parameter declarations. A return
11566 value of NULL indicates a parameter-declaration-clause consisting
11567 only of an ellipsis. */
11569 static cp_parameter_declarator *
11570 cp_parser_parameter_declaration_clause (cp_parser* parser)
11572 cp_parameter_declarator *parameters;
11577 /* Peek at the next token. */
11578 token = cp_lexer_peek_token (parser->lexer);
11579 /* Check for trivial parameter-declaration-clauses. */
11580 if (token->type == CPP_ELLIPSIS)
11582 /* Consume the `...' token. */
11583 cp_lexer_consume_token (parser->lexer);
11586 else if (token->type == CPP_CLOSE_PAREN)
11587 /* There are no parameters. */
11589 #ifndef NO_IMPLICIT_EXTERN_C
11590 if (in_system_header && current_class_type == NULL
11591 && current_lang_name == lang_name_c)
11595 return no_parameters;
11597 /* Check for `(void)', too, which is a special case. */
11598 else if (token->keyword == RID_VOID
11599 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11600 == CPP_CLOSE_PAREN))
11602 /* Consume the `void' token. */
11603 cp_lexer_consume_token (parser->lexer);
11604 /* There are no parameters. */
11605 return no_parameters;
11608 /* Parse the parameter-declaration-list. */
11609 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11610 /* If a parse error occurred while parsing the
11611 parameter-declaration-list, then the entire
11612 parameter-declaration-clause is erroneous. */
11616 /* Peek at the next token. */
11617 token = cp_lexer_peek_token (parser->lexer);
11618 /* If it's a `,', the clause should terminate with an ellipsis. */
11619 if (token->type == CPP_COMMA)
11621 /* Consume the `,'. */
11622 cp_lexer_consume_token (parser->lexer);
11623 /* Expect an ellipsis. */
11625 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11627 /* It might also be `...' if the optional trailing `,' was
11629 else if (token->type == CPP_ELLIPSIS)
11631 /* Consume the `...' token. */
11632 cp_lexer_consume_token (parser->lexer);
11633 /* And remember that we saw it. */
11637 ellipsis_p = false;
11639 /* Finish the parameter list. */
11640 if (parameters && ellipsis_p)
11641 parameters->ellipsis_p = true;
11646 /* Parse a parameter-declaration-list.
11648 parameter-declaration-list:
11649 parameter-declaration
11650 parameter-declaration-list , parameter-declaration
11652 Returns a representation of the parameter-declaration-list, as for
11653 cp_parser_parameter_declaration_clause. However, the
11654 `void_list_node' is never appended to the list. Upon return,
11655 *IS_ERROR will be true iff an error occurred. */
11657 static cp_parameter_declarator *
11658 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11660 cp_parameter_declarator *parameters = NULL;
11661 cp_parameter_declarator **tail = ¶meters;
11663 /* Assume all will go well. */
11666 /* Look for more parameters. */
11669 cp_parameter_declarator *parameter;
11670 bool parenthesized_p;
11671 /* Parse the parameter. */
11673 = cp_parser_parameter_declaration (parser,
11674 /*template_parm_p=*/false,
11677 /* If a parse error occurred parsing the parameter declaration,
11678 then the entire parameter-declaration-list is erroneous. */
11685 /* Add the new parameter to the list. */
11687 tail = ¶meter->next;
11689 /* Peek at the next token. */
11690 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11691 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11692 /* The parameter-declaration-list is complete. */
11694 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11698 /* Peek at the next token. */
11699 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11700 /* If it's an ellipsis, then the list is complete. */
11701 if (token->type == CPP_ELLIPSIS)
11703 /* Otherwise, there must be more parameters. Consume the
11705 cp_lexer_consume_token (parser->lexer);
11706 /* When parsing something like:
11708 int i(float f, double d)
11710 we can tell after seeing the declaration for "f" that we
11711 are not looking at an initialization of a variable "i",
11712 but rather at the declaration of a function "i".
11714 Due to the fact that the parsing of template arguments
11715 (as specified to a template-id) requires backtracking we
11716 cannot use this technique when inside a template argument
11718 if (!parser->in_template_argument_list_p
11719 && !parser->in_type_id_in_expr_p
11720 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11721 /* However, a parameter-declaration of the form
11722 "foat(f)" (which is a valid declaration of a
11723 parameter "f") can also be interpreted as an
11724 expression (the conversion of "f" to "float"). */
11725 && !parenthesized_p)
11726 cp_parser_commit_to_tentative_parse (parser);
11730 cp_parser_error (parser, "expected %<,%> or %<...%>");
11731 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11732 cp_parser_skip_to_closing_parenthesis (parser,
11733 /*recovering=*/true,
11734 /*or_comma=*/false,
11735 /*consume_paren=*/false);
11743 /* Parse a parameter declaration.
11745 parameter-declaration:
11746 decl-specifier-seq declarator
11747 decl-specifier-seq declarator = assignment-expression
11748 decl-specifier-seq abstract-declarator [opt]
11749 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11751 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11752 declares a template parameter. (In that case, a non-nested `>'
11753 token encountered during the parsing of the assignment-expression
11754 is not interpreted as a greater-than operator.)
11756 Returns a representation of the parameter, or NULL if an error
11757 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11758 true iff the declarator is of the form "(p)". */
11760 static cp_parameter_declarator *
11761 cp_parser_parameter_declaration (cp_parser *parser,
11762 bool template_parm_p,
11763 bool *parenthesized_p)
11765 int declares_class_or_enum;
11766 bool greater_than_is_operator_p;
11767 cp_decl_specifier_seq decl_specifiers;
11768 cp_declarator *declarator;
11769 tree default_argument;
11771 const char *saved_message;
11773 /* In a template parameter, `>' is not an operator.
11777 When parsing a default template-argument for a non-type
11778 template-parameter, the first non-nested `>' is taken as the end
11779 of the template parameter-list rather than a greater-than
11781 greater_than_is_operator_p = !template_parm_p;
11783 /* Type definitions may not appear in parameter types. */
11784 saved_message = parser->type_definition_forbidden_message;
11785 parser->type_definition_forbidden_message
11786 = "types may not be defined in parameter types";
11788 /* Parse the declaration-specifiers. */
11789 cp_parser_decl_specifier_seq (parser,
11790 CP_PARSER_FLAGS_NONE,
11792 &declares_class_or_enum);
11793 /* If an error occurred, there's no reason to attempt to parse the
11794 rest of the declaration. */
11795 if (cp_parser_error_occurred (parser))
11797 parser->type_definition_forbidden_message = saved_message;
11801 /* Peek at the next token. */
11802 token = cp_lexer_peek_token (parser->lexer);
11803 /* If the next token is a `)', `,', `=', `>', or `...', then there
11804 is no declarator. */
11805 if (token->type == CPP_CLOSE_PAREN
11806 || token->type == CPP_COMMA
11807 || token->type == CPP_EQ
11808 || token->type == CPP_ELLIPSIS
11809 || token->type == CPP_GREATER)
11812 if (parenthesized_p)
11813 *parenthesized_p = false;
11815 /* Otherwise, there should be a declarator. */
11818 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11819 parser->default_arg_ok_p = false;
11821 /* After seeing a decl-specifier-seq, if the next token is not a
11822 "(", there is no possibility that the code is a valid
11823 expression. Therefore, if parsing tentatively, we commit at
11825 if (!parser->in_template_argument_list_p
11826 /* In an expression context, having seen:
11830 we cannot be sure whether we are looking at a
11831 function-type (taking a "char" as a parameter) or a cast
11832 of some object of type "char" to "int". */
11833 && !parser->in_type_id_in_expr_p
11834 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11835 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11836 cp_parser_commit_to_tentative_parse (parser);
11837 /* Parse the declarator. */
11838 declarator = cp_parser_declarator (parser,
11839 CP_PARSER_DECLARATOR_EITHER,
11840 /*ctor_dtor_or_conv_p=*/NULL,
11842 /*member_p=*/false);
11843 parser->default_arg_ok_p = saved_default_arg_ok_p;
11844 /* After the declarator, allow more attributes. */
11845 decl_specifiers.attributes
11846 = chainon (decl_specifiers.attributes,
11847 cp_parser_attributes_opt (parser));
11850 /* The restriction on defining new types applies only to the type
11851 of the parameter, not to the default argument. */
11852 parser->type_definition_forbidden_message = saved_message;
11854 /* If the next token is `=', then process a default argument. */
11855 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11857 bool saved_greater_than_is_operator_p;
11858 /* Consume the `='. */
11859 cp_lexer_consume_token (parser->lexer);
11861 /* If we are defining a class, then the tokens that make up the
11862 default argument must be saved and processed later. */
11863 if (!template_parm_p && at_class_scope_p ()
11864 && TYPE_BEING_DEFINED (current_class_type))
11866 unsigned depth = 0;
11867 cp_token *first_token;
11870 /* Add tokens until we have processed the entire default
11871 argument. We add the range [first_token, token). */
11872 first_token = cp_lexer_peek_token (parser->lexer);
11877 /* Peek at the next token. */
11878 token = cp_lexer_peek_token (parser->lexer);
11879 /* What we do depends on what token we have. */
11880 switch (token->type)
11882 /* In valid code, a default argument must be
11883 immediately followed by a `,' `)', or `...'. */
11885 case CPP_CLOSE_PAREN:
11887 /* If we run into a non-nested `;', `}', or `]',
11888 then the code is invalid -- but the default
11889 argument is certainly over. */
11890 case CPP_SEMICOLON:
11891 case CPP_CLOSE_BRACE:
11892 case CPP_CLOSE_SQUARE:
11895 /* Update DEPTH, if necessary. */
11896 else if (token->type == CPP_CLOSE_PAREN
11897 || token->type == CPP_CLOSE_BRACE
11898 || token->type == CPP_CLOSE_SQUARE)
11902 case CPP_OPEN_PAREN:
11903 case CPP_OPEN_SQUARE:
11904 case CPP_OPEN_BRACE:
11909 /* If we see a non-nested `>', and `>' is not an
11910 operator, then it marks the end of the default
11912 if (!depth && !greater_than_is_operator_p)
11916 /* If we run out of tokens, issue an error message. */
11918 error ("file ends in default argument");
11924 /* In these cases, we should look for template-ids.
11925 For example, if the default argument is
11926 `X<int, double>()', we need to do name lookup to
11927 figure out whether or not `X' is a template; if
11928 so, the `,' does not end the default argument.
11930 That is not yet done. */
11937 /* If we've reached the end, stop. */
11941 /* Add the token to the token block. */
11942 token = cp_lexer_consume_token (parser->lexer);
11945 /* Create a DEFAULT_ARG to represented the unparsed default
11947 default_argument = make_node (DEFAULT_ARG);
11948 DEFARG_TOKENS (default_argument)
11949 = cp_token_cache_new (first_token, token);
11951 /* Outside of a class definition, we can just parse the
11952 assignment-expression. */
11955 bool saved_local_variables_forbidden_p;
11957 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11959 saved_greater_than_is_operator_p
11960 = parser->greater_than_is_operator_p;
11961 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11962 /* Local variable names (and the `this' keyword) may not
11963 appear in a default argument. */
11964 saved_local_variables_forbidden_p
11965 = parser->local_variables_forbidden_p;
11966 parser->local_variables_forbidden_p = true;
11967 /* Parse the assignment-expression. */
11969 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
11970 /* Restore saved state. */
11971 parser->greater_than_is_operator_p
11972 = saved_greater_than_is_operator_p;
11973 parser->local_variables_forbidden_p
11974 = saved_local_variables_forbidden_p;
11976 if (!parser->default_arg_ok_p)
11978 if (!flag_pedantic_errors)
11979 warning ("deprecated use of default argument for parameter of non-function");
11982 error ("default arguments are only permitted for function parameters");
11983 default_argument = NULL_TREE;
11988 default_argument = NULL_TREE;
11990 return make_parameter_declarator (&decl_specifiers,
11995 /* Parse a function-body.
11998 compound_statement */
12001 cp_parser_function_body (cp_parser *parser)
12003 cp_parser_compound_statement (parser, NULL, false);
12006 /* Parse a ctor-initializer-opt followed by a function-body. Return
12007 true if a ctor-initializer was present. */
12010 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12013 bool ctor_initializer_p;
12015 /* Begin the function body. */
12016 body = begin_function_body ();
12017 /* Parse the optional ctor-initializer. */
12018 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12019 /* Parse the function-body. */
12020 cp_parser_function_body (parser);
12021 /* Finish the function body. */
12022 finish_function_body (body);
12024 return ctor_initializer_p;
12027 /* Parse an initializer.
12030 = initializer-clause
12031 ( expression-list )
12033 Returns a expression representing the initializer. If no
12034 initializer is present, NULL_TREE is returned.
12036 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12037 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12038 set to FALSE if there is no initializer present. If there is an
12039 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12040 is set to true; otherwise it is set to false. */
12043 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12044 bool* non_constant_p)
12049 /* Peek at the next token. */
12050 token = cp_lexer_peek_token (parser->lexer);
12052 /* Let our caller know whether or not this initializer was
12054 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12055 /* Assume that the initializer is constant. */
12056 *non_constant_p = false;
12058 if (token->type == CPP_EQ)
12060 /* Consume the `='. */
12061 cp_lexer_consume_token (parser->lexer);
12062 /* Parse the initializer-clause. */
12063 init = cp_parser_initializer_clause (parser, non_constant_p);
12065 else if (token->type == CPP_OPEN_PAREN)
12066 init = cp_parser_parenthesized_expression_list (parser, false,
12071 /* Anything else is an error. */
12072 cp_parser_error (parser, "expected initializer");
12073 init = error_mark_node;
12079 /* Parse an initializer-clause.
12081 initializer-clause:
12082 assignment-expression
12083 { initializer-list , [opt] }
12086 Returns an expression representing the initializer.
12088 If the `assignment-expression' production is used the value
12089 returned is simply a representation for the expression.
12091 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12092 the elements of the initializer-list (or NULL_TREE, if the last
12093 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12094 NULL_TREE. There is no way to detect whether or not the optional
12095 trailing `,' was provided. NON_CONSTANT_P is as for
12096 cp_parser_initializer. */
12099 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12103 /* Assume the expression is constant. */
12104 *non_constant_p = false;
12106 /* If it is not a `{', then we are looking at an
12107 assignment-expression. */
12108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12111 = cp_parser_constant_expression (parser,
12112 /*allow_non_constant_p=*/true,
12114 if (!*non_constant_p)
12115 initializer = fold_non_dependent_expr (initializer);
12119 /* Consume the `{' token. */
12120 cp_lexer_consume_token (parser->lexer);
12121 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12122 initializer = make_node (CONSTRUCTOR);
12123 /* If it's not a `}', then there is a non-trivial initializer. */
12124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12126 /* Parse the initializer list. */
12127 CONSTRUCTOR_ELTS (initializer)
12128 = cp_parser_initializer_list (parser, non_constant_p);
12129 /* A trailing `,' token is allowed. */
12130 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12131 cp_lexer_consume_token (parser->lexer);
12133 /* Now, there should be a trailing `}'. */
12134 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12137 return initializer;
12140 /* Parse an initializer-list.
12144 initializer-list , initializer-clause
12149 identifier : initializer-clause
12150 initializer-list, identifier : initializer-clause
12152 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12153 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12154 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12155 as for cp_parser_initializer. */
12158 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12160 tree initializers = NULL_TREE;
12162 /* Assume all of the expressions are constant. */
12163 *non_constant_p = false;
12165 /* Parse the rest of the list. */
12171 bool clause_non_constant_p;
12173 /* If the next token is an identifier and the following one is a
12174 colon, we are looking at the GNU designated-initializer
12176 if (cp_parser_allow_gnu_extensions_p (parser)
12177 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12178 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12180 /* Consume the identifier. */
12181 identifier = cp_lexer_consume_token (parser->lexer)->value;
12182 /* Consume the `:'. */
12183 cp_lexer_consume_token (parser->lexer);
12186 identifier = NULL_TREE;
12188 /* Parse the initializer. */
12189 initializer = cp_parser_initializer_clause (parser,
12190 &clause_non_constant_p);
12191 /* If any clause is non-constant, so is the entire initializer. */
12192 if (clause_non_constant_p)
12193 *non_constant_p = true;
12194 /* Add it to the list. */
12195 initializers = tree_cons (identifier, initializer, initializers);
12197 /* If the next token is not a comma, we have reached the end of
12199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12202 /* Peek at the next token. */
12203 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12204 /* If the next token is a `}', then we're still done. An
12205 initializer-clause can have a trailing `,' after the
12206 initializer-list and before the closing `}'. */
12207 if (token->type == CPP_CLOSE_BRACE)
12210 /* Consume the `,' token. */
12211 cp_lexer_consume_token (parser->lexer);
12214 /* The initializers were built up in reverse order, so we need to
12215 reverse them now. */
12216 return nreverse (initializers);
12219 /* Classes [gram.class] */
12221 /* Parse a class-name.
12227 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12228 to indicate that names looked up in dependent types should be
12229 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12230 keyword has been used to indicate that the name that appears next
12231 is a template. TAG_TYPE indicates the explicit tag given before
12232 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12233 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12234 is the class being defined in a class-head.
12236 Returns the TYPE_DECL representing the class. */
12239 cp_parser_class_name (cp_parser *parser,
12240 bool typename_keyword_p,
12241 bool template_keyword_p,
12242 enum tag_types tag_type,
12243 bool check_dependency_p,
12245 bool is_declaration)
12252 /* All class-names start with an identifier. */
12253 token = cp_lexer_peek_token (parser->lexer);
12254 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12256 cp_parser_error (parser, "expected class-name");
12257 return error_mark_node;
12260 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12261 to a template-id, so we save it here. */
12262 scope = parser->scope;
12263 if (scope == error_mark_node)
12264 return error_mark_node;
12266 /* Any name names a type if we're following the `typename' keyword
12267 in a qualified name where the enclosing scope is type-dependent. */
12268 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12269 && dependent_type_p (scope));
12270 /* Handle the common case (an identifier, but not a template-id)
12272 if (token->type == CPP_NAME
12273 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12277 /* Look for the identifier. */
12278 identifier = cp_parser_identifier (parser);
12279 /* If the next token isn't an identifier, we are certainly not
12280 looking at a class-name. */
12281 if (identifier == error_mark_node)
12282 decl = error_mark_node;
12283 /* If we know this is a type-name, there's no need to look it
12285 else if (typename_p)
12289 /* If the next token is a `::', then the name must be a type
12292 [basic.lookup.qual]
12294 During the lookup for a name preceding the :: scope
12295 resolution operator, object, function, and enumerator
12296 names are ignored. */
12297 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12298 tag_type = typename_type;
12299 /* Look up the name. */
12300 decl = cp_parser_lookup_name (parser, identifier,
12302 /*is_template=*/false,
12303 /*is_namespace=*/false,
12304 check_dependency_p,
12305 /*ambiguous_p=*/NULL);
12310 /* Try a template-id. */
12311 decl = cp_parser_template_id (parser, template_keyword_p,
12312 check_dependency_p,
12314 if (decl == error_mark_node)
12315 return error_mark_node;
12318 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12320 /* If this is a typename, create a TYPENAME_TYPE. */
12321 if (typename_p && decl != error_mark_node)
12323 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12324 if (decl != error_mark_node)
12325 decl = TYPE_NAME (decl);
12328 /* Check to see that it is really the name of a class. */
12329 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12330 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12331 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12332 /* Situations like this:
12334 template <typename T> struct A {
12335 typename T::template X<int>::I i;
12338 are problematic. Is `T::template X<int>' a class-name? The
12339 standard does not seem to be definitive, but there is no other
12340 valid interpretation of the following `::'. Therefore, those
12341 names are considered class-names. */
12342 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12343 else if (decl == error_mark_node
12344 || TREE_CODE (decl) != TYPE_DECL
12345 || TREE_TYPE (decl) == error_mark_node
12346 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12348 cp_parser_error (parser, "expected class-name");
12349 return error_mark_node;
12355 /* Parse a class-specifier.
12358 class-head { member-specification [opt] }
12360 Returns the TREE_TYPE representing the class. */
12363 cp_parser_class_specifier (cp_parser* parser)
12367 tree attributes = NULL_TREE;
12368 int has_trailing_semicolon;
12369 bool nested_name_specifier_p;
12370 unsigned saved_num_template_parameter_lists;
12371 tree old_scope = NULL_TREE;
12372 tree scope = NULL_TREE;
12374 push_deferring_access_checks (dk_no_deferred);
12376 /* Parse the class-head. */
12377 type = cp_parser_class_head (parser,
12378 &nested_name_specifier_p,
12380 /* If the class-head was a semantic disaster, skip the entire body
12384 cp_parser_skip_to_end_of_block_or_statement (parser);
12385 pop_deferring_access_checks ();
12386 return error_mark_node;
12389 /* Look for the `{'. */
12390 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12392 pop_deferring_access_checks ();
12393 return error_mark_node;
12396 /* Issue an error message if type-definitions are forbidden here. */
12397 cp_parser_check_type_definition (parser);
12398 /* Remember that we are defining one more class. */
12399 ++parser->num_classes_being_defined;
12400 /* Inside the class, surrounding template-parameter-lists do not
12402 saved_num_template_parameter_lists
12403 = parser->num_template_parameter_lists;
12404 parser->num_template_parameter_lists = 0;
12406 /* Start the class. */
12407 if (nested_name_specifier_p)
12409 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12410 old_scope = push_inner_scope (scope);
12412 type = begin_class_definition (type);
12414 if (type == error_mark_node)
12415 /* If the type is erroneous, skip the entire body of the class. */
12416 cp_parser_skip_to_closing_brace (parser);
12418 /* Parse the member-specification. */
12419 cp_parser_member_specification_opt (parser);
12421 /* Look for the trailing `}'. */
12422 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12423 /* We get better error messages by noticing a common problem: a
12424 missing trailing `;'. */
12425 token = cp_lexer_peek_token (parser->lexer);
12426 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12427 /* Look for trailing attributes to apply to this class. */
12428 if (cp_parser_allow_gnu_extensions_p (parser))
12430 tree sub_attr = cp_parser_attributes_opt (parser);
12431 attributes = chainon (attributes, sub_attr);
12433 if (type != error_mark_node)
12434 type = finish_struct (type, attributes);
12435 if (nested_name_specifier_p)
12436 pop_inner_scope (old_scope, scope);
12437 /* If this class is not itself within the scope of another class,
12438 then we need to parse the bodies of all of the queued function
12439 definitions. Note that the queued functions defined in a class
12440 are not always processed immediately following the
12441 class-specifier for that class. Consider:
12444 struct B { void f() { sizeof (A); } };
12447 If `f' were processed before the processing of `A' were
12448 completed, there would be no way to compute the size of `A'.
12449 Note that the nesting we are interested in here is lexical --
12450 not the semantic nesting given by TYPE_CONTEXT. In particular,
12453 struct A { struct B; };
12454 struct A::B { void f() { } };
12456 there is no need to delay the parsing of `A::B::f'. */
12457 if (--parser->num_classes_being_defined == 0)
12461 tree class_type = NULL_TREE;
12462 tree pushed_scope = NULL_TREE;
12464 /* In a first pass, parse default arguments to the functions.
12465 Then, in a second pass, parse the bodies of the functions.
12466 This two-phased approach handles cases like:
12474 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12475 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12476 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12477 TREE_PURPOSE (parser->unparsed_functions_queues)
12478 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12480 fn = TREE_VALUE (queue_entry);
12481 /* If there are default arguments that have not yet been processed,
12482 take care of them now. */
12483 if (class_type != TREE_PURPOSE (queue_entry))
12486 pop_scope (pushed_scope);
12487 class_type = TREE_PURPOSE (queue_entry);
12488 pushed_scope = push_scope (class_type);
12490 /* Make sure that any template parameters are in scope. */
12491 maybe_begin_member_template_processing (fn);
12492 /* Parse the default argument expressions. */
12493 cp_parser_late_parsing_default_args (parser, fn);
12494 /* Remove any template parameters from the symbol table. */
12495 maybe_end_member_template_processing ();
12498 pop_scope (pushed_scope);
12499 /* Now parse the body of the functions. */
12500 for (TREE_VALUE (parser->unparsed_functions_queues)
12501 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12502 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12503 TREE_VALUE (parser->unparsed_functions_queues)
12504 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12506 /* Figure out which function we need to process. */
12507 fn = TREE_VALUE (queue_entry);
12509 /* A hack to prevent garbage collection. */
12512 /* Parse the function. */
12513 cp_parser_late_parsing_for_member (parser, fn);
12518 /* Put back any saved access checks. */
12519 pop_deferring_access_checks ();
12521 /* Restore the count of active template-parameter-lists. */
12522 parser->num_template_parameter_lists
12523 = saved_num_template_parameter_lists;
12528 /* Parse a class-head.
12531 class-key identifier [opt] base-clause [opt]
12532 class-key nested-name-specifier identifier base-clause [opt]
12533 class-key nested-name-specifier [opt] template-id
12537 class-key attributes identifier [opt] base-clause [opt]
12538 class-key attributes nested-name-specifier identifier base-clause [opt]
12539 class-key attributes nested-name-specifier [opt] template-id
12542 Returns the TYPE of the indicated class. Sets
12543 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12544 involving a nested-name-specifier was used, and FALSE otherwise.
12546 Returns error_mark_node if this is not a class-head.
12548 Returns NULL_TREE if the class-head is syntactically valid, but
12549 semantically invalid in a way that means we should skip the entire
12550 body of the class. */
12553 cp_parser_class_head (cp_parser* parser,
12554 bool* nested_name_specifier_p,
12555 tree *attributes_p)
12557 tree nested_name_specifier;
12558 enum tag_types class_key;
12559 tree id = NULL_TREE;
12560 tree type = NULL_TREE;
12562 bool template_id_p = false;
12563 bool qualified_p = false;
12564 bool invalid_nested_name_p = false;
12565 bool invalid_explicit_specialization_p = false;
12566 tree pushed_scope = NULL_TREE;
12567 unsigned num_templates;
12570 /* Assume no nested-name-specifier will be present. */
12571 *nested_name_specifier_p = false;
12572 /* Assume no template parameter lists will be used in defining the
12576 /* Look for the class-key. */
12577 class_key = cp_parser_class_key (parser);
12578 if (class_key == none_type)
12579 return error_mark_node;
12581 /* Parse the attributes. */
12582 attributes = cp_parser_attributes_opt (parser);
12584 /* If the next token is `::', that is invalid -- but sometimes
12585 people do try to write:
12589 Handle this gracefully by accepting the extra qualifier, and then
12590 issuing an error about it later if this really is a
12591 class-head. If it turns out just to be an elaborated type
12592 specifier, remain silent. */
12593 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12594 qualified_p = true;
12596 push_deferring_access_checks (dk_no_check);
12598 /* Determine the name of the class. Begin by looking for an
12599 optional nested-name-specifier. */
12600 nested_name_specifier
12601 = cp_parser_nested_name_specifier_opt (parser,
12602 /*typename_keyword_p=*/false,
12603 /*check_dependency_p=*/false,
12605 /*is_declaration=*/false);
12606 /* If there was a nested-name-specifier, then there *must* be an
12608 if (nested_name_specifier)
12610 /* Although the grammar says `identifier', it really means
12611 `class-name' or `template-name'. You are only allowed to
12612 define a class that has already been declared with this
12615 The proposed resolution for Core Issue 180 says that whever
12616 you see `class T::X' you should treat `X' as a type-name.
12618 It is OK to define an inaccessible class; for example:
12620 class A { class B; };
12623 We do not know if we will see a class-name, or a
12624 template-name. We look for a class-name first, in case the
12625 class-name is a template-id; if we looked for the
12626 template-name first we would stop after the template-name. */
12627 cp_parser_parse_tentatively (parser);
12628 type = cp_parser_class_name (parser,
12629 /*typename_keyword_p=*/false,
12630 /*template_keyword_p=*/false,
12632 /*check_dependency_p=*/false,
12633 /*class_head_p=*/true,
12634 /*is_declaration=*/false);
12635 /* If that didn't work, ignore the nested-name-specifier. */
12636 if (!cp_parser_parse_definitely (parser))
12638 invalid_nested_name_p = true;
12639 id = cp_parser_identifier (parser);
12640 if (id == error_mark_node)
12643 /* If we could not find a corresponding TYPE, treat this
12644 declaration like an unqualified declaration. */
12645 if (type == error_mark_node)
12646 nested_name_specifier = NULL_TREE;
12647 /* Otherwise, count the number of templates used in TYPE and its
12648 containing scopes. */
12653 for (scope = TREE_TYPE (type);
12654 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12655 scope = (TYPE_P (scope)
12656 ? TYPE_CONTEXT (scope)
12657 : DECL_CONTEXT (scope)))
12659 && CLASS_TYPE_P (scope)
12660 && CLASSTYPE_TEMPLATE_INFO (scope)
12661 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12662 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12666 /* Otherwise, the identifier is optional. */
12669 /* We don't know whether what comes next is a template-id,
12670 an identifier, or nothing at all. */
12671 cp_parser_parse_tentatively (parser);
12672 /* Check for a template-id. */
12673 id = cp_parser_template_id (parser,
12674 /*template_keyword_p=*/false,
12675 /*check_dependency_p=*/true,
12676 /*is_declaration=*/true);
12677 /* If that didn't work, it could still be an identifier. */
12678 if (!cp_parser_parse_definitely (parser))
12680 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12681 id = cp_parser_identifier (parser);
12687 template_id_p = true;
12692 pop_deferring_access_checks ();
12695 cp_parser_check_for_invalid_template_id (parser, id);
12697 /* If it's not a `:' or a `{' then we can't really be looking at a
12698 class-head, since a class-head only appears as part of a
12699 class-specifier. We have to detect this situation before calling
12700 xref_tag, since that has irreversible side-effects. */
12701 if (!cp_parser_next_token_starts_class_definition_p (parser))
12703 cp_parser_error (parser, "expected %<{%> or %<:%>");
12704 return error_mark_node;
12707 /* At this point, we're going ahead with the class-specifier, even
12708 if some other problem occurs. */
12709 cp_parser_commit_to_tentative_parse (parser);
12710 /* Issue the error about the overly-qualified name now. */
12712 cp_parser_error (parser,
12713 "global qualification of class name is invalid");
12714 else if (invalid_nested_name_p)
12715 cp_parser_error (parser,
12716 "qualified name does not name a class");
12717 else if (nested_name_specifier)
12721 /* Reject typedef-names in class heads. */
12722 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12724 error ("invalid class name in declaration of %qD", type);
12729 /* Figure out in what scope the declaration is being placed. */
12730 scope = current_scope ();
12731 /* If that scope does not contain the scope in which the
12732 class was originally declared, the program is invalid. */
12733 if (scope && !is_ancestor (scope, nested_name_specifier))
12735 error ("declaration of %qD in %qD which does not enclose %qD",
12736 type, scope, nested_name_specifier);
12742 A declarator-id shall not be qualified exception of the
12743 definition of a ... nested class outside of its class
12744 ... [or] a the definition or explicit instantiation of a
12745 class member of a namespace outside of its namespace. */
12746 if (scope == nested_name_specifier)
12748 pedwarn ("extra qualification ignored");
12749 nested_name_specifier = NULL_TREE;
12753 /* An explicit-specialization must be preceded by "template <>". If
12754 it is not, try to recover gracefully. */
12755 if (at_namespace_scope_p ()
12756 && parser->num_template_parameter_lists == 0
12759 error ("an explicit specialization must be preceded by %<template <>%>");
12760 invalid_explicit_specialization_p = true;
12761 /* Take the same action that would have been taken by
12762 cp_parser_explicit_specialization. */
12763 ++parser->num_template_parameter_lists;
12764 begin_specialization ();
12766 /* There must be no "return" statements between this point and the
12767 end of this function; set "type "to the correct return value and
12768 use "goto done;" to return. */
12769 /* Make sure that the right number of template parameters were
12771 if (!cp_parser_check_template_parameters (parser, num_templates))
12773 /* If something went wrong, there is no point in even trying to
12774 process the class-definition. */
12779 /* Look up the type. */
12782 type = TREE_TYPE (id);
12783 maybe_process_partial_specialization (type);
12784 if (nested_name_specifier)
12785 pushed_scope = push_scope (nested_name_specifier);
12787 else if (nested_name_specifier)
12793 template <typename T> struct S { struct T };
12794 template <typename T> struct S<T>::T { };
12796 we will get a TYPENAME_TYPE when processing the definition of
12797 `S::T'. We need to resolve it to the actual type before we
12798 try to define it. */
12799 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12801 class_type = resolve_typename_type (TREE_TYPE (type),
12802 /*only_current_p=*/false);
12803 if (class_type != error_mark_node)
12804 type = TYPE_NAME (class_type);
12807 cp_parser_error (parser, "could not resolve typename type");
12808 type = error_mark_node;
12812 maybe_process_partial_specialization (TREE_TYPE (type));
12813 class_type = current_class_type;
12814 /* Enter the scope indicated by the nested-name-specifier. */
12815 pushed_scope = push_scope (nested_name_specifier);
12816 /* Get the canonical version of this type. */
12817 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12818 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12819 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12821 type = push_template_decl (type);
12822 if (type == error_mark_node)
12829 type = TREE_TYPE (type);
12830 *nested_name_specifier_p = true;
12832 else /* The name is not a nested name. */
12834 /* If the class was unnamed, create a dummy name. */
12836 id = make_anon_name ();
12837 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12838 parser->num_template_parameter_lists);
12841 /* Indicate whether this class was declared as a `class' or as a
12843 if (TREE_CODE (type) == RECORD_TYPE)
12844 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12845 cp_parser_check_class_key (class_key, type);
12847 /* If this type was already complete, and we see another definition,
12848 that's an error. */
12849 if (type != error_mark_node && COMPLETE_TYPE_P (type))
12851 error ("redefinition of %q#T", type);
12852 cp_error_at ("previous definition of %q#T", type);
12853 type = error_mark_node;
12856 /* We will have entered the scope containing the class; the names of
12857 base classes should be looked up in that context. For example:
12859 struct A { struct B {}; struct C; };
12860 struct A::C : B {};
12865 /* Get the list of base-classes, if there is one. */
12866 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12867 bases = cp_parser_base_clause (parser);
12869 /* Process the base classes. */
12870 xref_basetypes (type, bases);
12873 /* Leave the scope given by the nested-name-specifier. We will
12874 enter the class scope itself while processing the members. */
12876 pop_scope (pushed_scope);
12878 if (invalid_explicit_specialization_p)
12880 end_specialization ();
12881 --parser->num_template_parameter_lists;
12883 *attributes_p = attributes;
12887 /* Parse a class-key.
12894 Returns the kind of class-key specified, or none_type to indicate
12897 static enum tag_types
12898 cp_parser_class_key (cp_parser* parser)
12901 enum tag_types tag_type;
12903 /* Look for the class-key. */
12904 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12908 /* Check to see if the TOKEN is a class-key. */
12909 tag_type = cp_parser_token_is_class_key (token);
12911 cp_parser_error (parser, "expected class-key");
12915 /* Parse an (optional) member-specification.
12917 member-specification:
12918 member-declaration member-specification [opt]
12919 access-specifier : member-specification [opt] */
12922 cp_parser_member_specification_opt (cp_parser* parser)
12929 /* Peek at the next token. */
12930 token = cp_lexer_peek_token (parser->lexer);
12931 /* If it's a `}', or EOF then we've seen all the members. */
12932 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12935 /* See if this token is a keyword. */
12936 keyword = token->keyword;
12940 case RID_PROTECTED:
12942 /* Consume the access-specifier. */
12943 cp_lexer_consume_token (parser->lexer);
12944 /* Remember which access-specifier is active. */
12945 current_access_specifier = token->value;
12946 /* Look for the `:'. */
12947 cp_parser_require (parser, CPP_COLON, "`:'");
12951 /* Accept #pragmas at class scope. */
12952 if (token->type == CPP_PRAGMA)
12954 cp_lexer_handle_pragma (parser->lexer);
12958 /* Otherwise, the next construction must be a
12959 member-declaration. */
12960 cp_parser_member_declaration (parser);
12965 /* Parse a member-declaration.
12967 member-declaration:
12968 decl-specifier-seq [opt] member-declarator-list [opt] ;
12969 function-definition ; [opt]
12970 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12972 template-declaration
12974 member-declarator-list:
12976 member-declarator-list , member-declarator
12979 declarator pure-specifier [opt]
12980 declarator constant-initializer [opt]
12981 identifier [opt] : constant-expression
12985 member-declaration:
12986 __extension__ member-declaration
12989 declarator attributes [opt] pure-specifier [opt]
12990 declarator attributes [opt] constant-initializer [opt]
12991 identifier [opt] attributes [opt] : constant-expression */
12994 cp_parser_member_declaration (cp_parser* parser)
12996 cp_decl_specifier_seq decl_specifiers;
12997 tree prefix_attributes;
12999 int declares_class_or_enum;
13002 int saved_pedantic;
13004 /* Check for the `__extension__' keyword. */
13005 if (cp_parser_extension_opt (parser, &saved_pedantic))
13008 cp_parser_member_declaration (parser);
13009 /* Restore the old value of the PEDANTIC flag. */
13010 pedantic = saved_pedantic;
13015 /* Check for a template-declaration. */
13016 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13018 /* Parse the template-declaration. */
13019 cp_parser_template_declaration (parser, /*member_p=*/true);
13024 /* Check for a using-declaration. */
13025 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13027 /* Parse the using-declaration. */
13028 cp_parser_using_declaration (parser);
13033 /* Parse the decl-specifier-seq. */
13034 cp_parser_decl_specifier_seq (parser,
13035 CP_PARSER_FLAGS_OPTIONAL,
13037 &declares_class_or_enum);
13038 prefix_attributes = decl_specifiers.attributes;
13039 decl_specifiers.attributes = NULL_TREE;
13040 /* Check for an invalid type-name. */
13041 if (!decl_specifiers.type
13042 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13044 /* If there is no declarator, then the decl-specifier-seq should
13046 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13048 /* If there was no decl-specifier-seq, and the next token is a
13049 `;', then we have something like:
13055 Each member-declaration shall declare at least one member
13056 name of the class. */
13057 if (!decl_specifiers.any_specifiers_p)
13059 cp_token *token = cp_lexer_peek_token (parser->lexer);
13060 if (pedantic && !token->in_system_header)
13061 pedwarn ("%Hextra %<;%>", &token->location);
13067 /* See if this declaration is a friend. */
13068 friend_p = cp_parser_friend_p (&decl_specifiers);
13069 /* If there were decl-specifiers, check to see if there was
13070 a class-declaration. */
13071 type = check_tag_decl (&decl_specifiers);
13072 /* Nested classes have already been added to the class, but
13073 a `friend' needs to be explicitly registered. */
13076 /* If the `friend' keyword was present, the friend must
13077 be introduced with a class-key. */
13078 if (!declares_class_or_enum)
13079 error ("a class-key must be used when declaring a friend");
13082 template <typename T> struct A {
13083 friend struct A<T>::B;
13086 A<T>::B will be represented by a TYPENAME_TYPE, and
13087 therefore not recognized by check_tag_decl. */
13089 && decl_specifiers.type
13090 && TYPE_P (decl_specifiers.type))
13091 type = decl_specifiers.type;
13092 if (!type || !TYPE_P (type))
13093 error ("friend declaration does not name a class or "
13096 make_friend_class (current_class_type, type,
13097 /*complain=*/true);
13099 /* If there is no TYPE, an error message will already have
13101 else if (!type || type == error_mark_node)
13103 /* An anonymous aggregate has to be handled specially; such
13104 a declaration really declares a data member (with a
13105 particular type), as opposed to a nested class. */
13106 else if (ANON_AGGR_TYPE_P (type))
13108 /* Remove constructors and such from TYPE, now that we
13109 know it is an anonymous aggregate. */
13110 fixup_anonymous_aggr (type);
13111 /* And make the corresponding data member. */
13112 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13113 /* Add it to the class. */
13114 finish_member_declaration (decl);
13117 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13122 /* See if these declarations will be friends. */
13123 friend_p = cp_parser_friend_p (&decl_specifiers);
13125 /* Keep going until we hit the `;' at the end of the
13127 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13129 tree attributes = NULL_TREE;
13130 tree first_attribute;
13132 /* Peek at the next token. */
13133 token = cp_lexer_peek_token (parser->lexer);
13135 /* Check for a bitfield declaration. */
13136 if (token->type == CPP_COLON
13137 || (token->type == CPP_NAME
13138 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13144 /* Get the name of the bitfield. Note that we cannot just
13145 check TOKEN here because it may have been invalidated by
13146 the call to cp_lexer_peek_nth_token above. */
13147 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13148 identifier = cp_parser_identifier (parser);
13150 identifier = NULL_TREE;
13152 /* Consume the `:' token. */
13153 cp_lexer_consume_token (parser->lexer);
13154 /* Get the width of the bitfield. */
13156 = cp_parser_constant_expression (parser,
13157 /*allow_non_constant=*/false,
13160 /* Look for attributes that apply to the bitfield. */
13161 attributes = cp_parser_attributes_opt (parser);
13162 /* Remember which attributes are prefix attributes and
13164 first_attribute = attributes;
13165 /* Combine the attributes. */
13166 attributes = chainon (prefix_attributes, attributes);
13168 /* Create the bitfield declaration. */
13169 decl = grokbitfield (identifier
13170 ? make_id_declarator (NULL_TREE,
13175 /* Apply the attributes. */
13176 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13180 cp_declarator *declarator;
13182 tree asm_specification;
13183 int ctor_dtor_or_conv_p;
13185 /* Parse the declarator. */
13187 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13188 &ctor_dtor_or_conv_p,
13189 /*parenthesized_p=*/NULL,
13190 /*member_p=*/true);
13192 /* If something went wrong parsing the declarator, make sure
13193 that we at least consume some tokens. */
13194 if (declarator == cp_error_declarator)
13196 /* Skip to the end of the statement. */
13197 cp_parser_skip_to_end_of_statement (parser);
13198 /* If the next token is not a semicolon, that is
13199 probably because we just skipped over the body of
13200 a function. So, we consume a semicolon if
13201 present, but do not issue an error message if it
13203 if (cp_lexer_next_token_is (parser->lexer,
13205 cp_lexer_consume_token (parser->lexer);
13209 if (declares_class_or_enum & 2)
13210 cp_parser_check_for_definition_in_return_type
13211 (declarator, decl_specifiers.type);
13213 /* Look for an asm-specification. */
13214 asm_specification = cp_parser_asm_specification_opt (parser);
13215 /* Look for attributes that apply to the declaration. */
13216 attributes = cp_parser_attributes_opt (parser);
13217 /* Remember which attributes are prefix attributes and
13219 first_attribute = attributes;
13220 /* Combine the attributes. */
13221 attributes = chainon (prefix_attributes, attributes);
13223 /* If it's an `=', then we have a constant-initializer or a
13224 pure-specifier. It is not correct to parse the
13225 initializer before registering the member declaration
13226 since the member declaration should be in scope while
13227 its initializer is processed. However, the rest of the
13228 front end does not yet provide an interface that allows
13229 us to handle this correctly. */
13230 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13234 A pure-specifier shall be used only in the declaration of
13235 a virtual function.
13237 A member-declarator can contain a constant-initializer
13238 only if it declares a static member of integral or
13241 Therefore, if the DECLARATOR is for a function, we look
13242 for a pure-specifier; otherwise, we look for a
13243 constant-initializer. When we call `grokfield', it will
13244 perform more stringent semantics checks. */
13245 if (declarator->kind == cdk_function)
13246 initializer = cp_parser_pure_specifier (parser);
13248 /* Parse the initializer. */
13249 initializer = cp_parser_constant_initializer (parser);
13251 /* Otherwise, there is no initializer. */
13253 initializer = NULL_TREE;
13255 /* See if we are probably looking at a function
13256 definition. We are certainly not looking at a
13257 member-declarator. Calling `grokfield' has
13258 side-effects, so we must not do it unless we are sure
13259 that we are looking at a member-declarator. */
13260 if (cp_parser_token_starts_function_definition_p
13261 (cp_lexer_peek_token (parser->lexer)))
13263 /* The grammar does not allow a pure-specifier to be
13264 used when a member function is defined. (It is
13265 possible that this fact is an oversight in the
13266 standard, since a pure function may be defined
13267 outside of the class-specifier. */
13269 error ("pure-specifier on function-definition");
13270 decl = cp_parser_save_member_function_body (parser,
13274 /* If the member was not a friend, declare it here. */
13276 finish_member_declaration (decl);
13277 /* Peek at the next token. */
13278 token = cp_lexer_peek_token (parser->lexer);
13279 /* If the next token is a semicolon, consume it. */
13280 if (token->type == CPP_SEMICOLON)
13281 cp_lexer_consume_token (parser->lexer);
13286 /* Create the declaration. */
13287 decl = grokfield (declarator, &decl_specifiers,
13288 initializer, asm_specification,
13290 /* Any initialization must have been from a
13291 constant-expression. */
13292 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13293 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13297 /* Reset PREFIX_ATTRIBUTES. */
13298 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13299 attributes = TREE_CHAIN (attributes);
13301 TREE_CHAIN (attributes) = NULL_TREE;
13303 /* If there is any qualification still in effect, clear it
13304 now; we will be starting fresh with the next declarator. */
13305 parser->scope = NULL_TREE;
13306 parser->qualifying_scope = NULL_TREE;
13307 parser->object_scope = NULL_TREE;
13308 /* If it's a `,', then there are more declarators. */
13309 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13310 cp_lexer_consume_token (parser->lexer);
13311 /* If the next token isn't a `;', then we have a parse error. */
13312 else if (cp_lexer_next_token_is_not (parser->lexer,
13315 cp_parser_error (parser, "expected %<;%>");
13316 /* Skip tokens until we find a `;'. */
13317 cp_parser_skip_to_end_of_statement (parser);
13324 /* Add DECL to the list of members. */
13326 finish_member_declaration (decl);
13328 if (TREE_CODE (decl) == FUNCTION_DECL)
13329 cp_parser_save_default_args (parser, decl);
13334 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13337 /* Parse a pure-specifier.
13342 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13343 Otherwise, ERROR_MARK_NODE is returned. */
13346 cp_parser_pure_specifier (cp_parser* parser)
13350 /* Look for the `=' token. */
13351 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13352 return error_mark_node;
13353 /* Look for the `0' token. */
13354 token = cp_lexer_consume_token (parser->lexer);
13355 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13357 cp_parser_error (parser,
13358 "invalid pure specifier (only `= 0' is allowed)");
13359 cp_parser_skip_to_end_of_statement (parser);
13360 return error_mark_node;
13363 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13364 We need to get information from the lexer about how the number
13365 was spelled in order to fix this problem. */
13366 return integer_zero_node;
13369 /* Parse a constant-initializer.
13371 constant-initializer:
13372 = constant-expression
13374 Returns a representation of the constant-expression. */
13377 cp_parser_constant_initializer (cp_parser* parser)
13379 /* Look for the `=' token. */
13380 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13381 return error_mark_node;
13383 /* It is invalid to write:
13385 struct S { static const int i = { 7 }; };
13388 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13390 cp_parser_error (parser,
13391 "a brace-enclosed initializer is not allowed here");
13392 /* Consume the opening brace. */
13393 cp_lexer_consume_token (parser->lexer);
13394 /* Skip the initializer. */
13395 cp_parser_skip_to_closing_brace (parser);
13396 /* Look for the trailing `}'. */
13397 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13399 return error_mark_node;
13402 return cp_parser_constant_expression (parser,
13403 /*allow_non_constant=*/false,
13407 /* Derived classes [gram.class.derived] */
13409 /* Parse a base-clause.
13412 : base-specifier-list
13414 base-specifier-list:
13416 base-specifier-list , base-specifier
13418 Returns a TREE_LIST representing the base-classes, in the order in
13419 which they were declared. The representation of each node is as
13420 described by cp_parser_base_specifier.
13422 In the case that no bases are specified, this function will return
13423 NULL_TREE, not ERROR_MARK_NODE. */
13426 cp_parser_base_clause (cp_parser* parser)
13428 tree bases = NULL_TREE;
13430 /* Look for the `:' that begins the list. */
13431 cp_parser_require (parser, CPP_COLON, "`:'");
13433 /* Scan the base-specifier-list. */
13439 /* Look for the base-specifier. */
13440 base = cp_parser_base_specifier (parser);
13441 /* Add BASE to the front of the list. */
13442 if (base != error_mark_node)
13444 TREE_CHAIN (base) = bases;
13447 /* Peek at the next token. */
13448 token = cp_lexer_peek_token (parser->lexer);
13449 /* If it's not a comma, then the list is complete. */
13450 if (token->type != CPP_COMMA)
13452 /* Consume the `,'. */
13453 cp_lexer_consume_token (parser->lexer);
13456 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13457 base class had a qualified name. However, the next name that
13458 appears is certainly not qualified. */
13459 parser->scope = NULL_TREE;
13460 parser->qualifying_scope = NULL_TREE;
13461 parser->object_scope = NULL_TREE;
13463 return nreverse (bases);
13466 /* Parse a base-specifier.
13469 :: [opt] nested-name-specifier [opt] class-name
13470 virtual access-specifier [opt] :: [opt] nested-name-specifier
13472 access-specifier virtual [opt] :: [opt] nested-name-specifier
13475 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13476 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13477 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13478 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13481 cp_parser_base_specifier (cp_parser* parser)
13485 bool virtual_p = false;
13486 bool duplicate_virtual_error_issued_p = false;
13487 bool duplicate_access_error_issued_p = false;
13488 bool class_scope_p, template_p;
13489 tree access = access_default_node;
13492 /* Process the optional `virtual' and `access-specifier'. */
13495 /* Peek at the next token. */
13496 token = cp_lexer_peek_token (parser->lexer);
13497 /* Process `virtual'. */
13498 switch (token->keyword)
13501 /* If `virtual' appears more than once, issue an error. */
13502 if (virtual_p && !duplicate_virtual_error_issued_p)
13504 cp_parser_error (parser,
13505 "%<virtual%> specified more than once in base-specified");
13506 duplicate_virtual_error_issued_p = true;
13511 /* Consume the `virtual' token. */
13512 cp_lexer_consume_token (parser->lexer);
13517 case RID_PROTECTED:
13519 /* If more than one access specifier appears, issue an
13521 if (access != access_default_node
13522 && !duplicate_access_error_issued_p)
13524 cp_parser_error (parser,
13525 "more than one access specifier in base-specified");
13526 duplicate_access_error_issued_p = true;
13529 access = ridpointers[(int) token->keyword];
13531 /* Consume the access-specifier. */
13532 cp_lexer_consume_token (parser->lexer);
13541 /* It is not uncommon to see programs mechanically, erroneously, use
13542 the 'typename' keyword to denote (dependent) qualified types
13543 as base classes. */
13544 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13546 if (!processing_template_decl)
13547 error ("keyword %<typename%> not allowed outside of templates");
13549 error ("keyword %<typename%> not allowed in this context "
13550 "(the base class is implicitly a type)");
13551 cp_lexer_consume_token (parser->lexer);
13554 /* Look for the optional `::' operator. */
13555 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13556 /* Look for the nested-name-specifier. The simplest way to
13561 The keyword `typename' is not permitted in a base-specifier or
13562 mem-initializer; in these contexts a qualified name that
13563 depends on a template-parameter is implicitly assumed to be a
13566 is to pretend that we have seen the `typename' keyword at this
13568 cp_parser_nested_name_specifier_opt (parser,
13569 /*typename_keyword_p=*/true,
13570 /*check_dependency_p=*/true,
13572 /*is_declaration=*/true);
13573 /* If the base class is given by a qualified name, assume that names
13574 we see are type names or templates, as appropriate. */
13575 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13576 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13578 /* Finally, look for the class-name. */
13579 type = cp_parser_class_name (parser,
13583 /*check_dependency_p=*/true,
13584 /*class_head_p=*/false,
13585 /*is_declaration=*/true);
13587 if (type == error_mark_node)
13588 return error_mark_node;
13590 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13593 /* Exception handling [gram.exception] */
13595 /* Parse an (optional) exception-specification.
13597 exception-specification:
13598 throw ( type-id-list [opt] )
13600 Returns a TREE_LIST representing the exception-specification. The
13601 TREE_VALUE of each node is a type. */
13604 cp_parser_exception_specification_opt (cp_parser* parser)
13609 /* Peek at the next token. */
13610 token = cp_lexer_peek_token (parser->lexer);
13611 /* If it's not `throw', then there's no exception-specification. */
13612 if (!cp_parser_is_keyword (token, RID_THROW))
13615 /* Consume the `throw'. */
13616 cp_lexer_consume_token (parser->lexer);
13618 /* Look for the `('. */
13619 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13621 /* Peek at the next token. */
13622 token = cp_lexer_peek_token (parser->lexer);
13623 /* If it's not a `)', then there is a type-id-list. */
13624 if (token->type != CPP_CLOSE_PAREN)
13626 const char *saved_message;
13628 /* Types may not be defined in an exception-specification. */
13629 saved_message = parser->type_definition_forbidden_message;
13630 parser->type_definition_forbidden_message
13631 = "types may not be defined in an exception-specification";
13632 /* Parse the type-id-list. */
13633 type_id_list = cp_parser_type_id_list (parser);
13634 /* Restore the saved message. */
13635 parser->type_definition_forbidden_message = saved_message;
13638 type_id_list = empty_except_spec;
13640 /* Look for the `)'. */
13641 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13643 return type_id_list;
13646 /* Parse an (optional) type-id-list.
13650 type-id-list , type-id
13652 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13653 in the order that the types were presented. */
13656 cp_parser_type_id_list (cp_parser* parser)
13658 tree types = NULL_TREE;
13665 /* Get the next type-id. */
13666 type = cp_parser_type_id (parser);
13667 /* Add it to the list. */
13668 types = add_exception_specifier (types, type, /*complain=*/1);
13669 /* Peek at the next token. */
13670 token = cp_lexer_peek_token (parser->lexer);
13671 /* If it is not a `,', we are done. */
13672 if (token->type != CPP_COMMA)
13674 /* Consume the `,'. */
13675 cp_lexer_consume_token (parser->lexer);
13678 return nreverse (types);
13681 /* Parse a try-block.
13684 try compound-statement handler-seq */
13687 cp_parser_try_block (cp_parser* parser)
13691 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13692 try_block = begin_try_block ();
13693 cp_parser_compound_statement (parser, NULL, true);
13694 finish_try_block (try_block);
13695 cp_parser_handler_seq (parser);
13696 finish_handler_sequence (try_block);
13701 /* Parse a function-try-block.
13703 function-try-block:
13704 try ctor-initializer [opt] function-body handler-seq */
13707 cp_parser_function_try_block (cp_parser* parser)
13710 bool ctor_initializer_p;
13712 /* Look for the `try' keyword. */
13713 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13715 /* Let the rest of the front-end know where we are. */
13716 try_block = begin_function_try_block ();
13717 /* Parse the function-body. */
13719 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13720 /* We're done with the `try' part. */
13721 finish_function_try_block (try_block);
13722 /* Parse the handlers. */
13723 cp_parser_handler_seq (parser);
13724 /* We're done with the handlers. */
13725 finish_function_handler_sequence (try_block);
13727 return ctor_initializer_p;
13730 /* Parse a handler-seq.
13733 handler handler-seq [opt] */
13736 cp_parser_handler_seq (cp_parser* parser)
13742 /* Parse the handler. */
13743 cp_parser_handler (parser);
13744 /* Peek at the next token. */
13745 token = cp_lexer_peek_token (parser->lexer);
13746 /* If it's not `catch' then there are no more handlers. */
13747 if (!cp_parser_is_keyword (token, RID_CATCH))
13752 /* Parse a handler.
13755 catch ( exception-declaration ) compound-statement */
13758 cp_parser_handler (cp_parser* parser)
13763 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13764 handler = begin_handler ();
13765 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13766 declaration = cp_parser_exception_declaration (parser);
13767 finish_handler_parms (declaration, handler);
13768 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13769 cp_parser_compound_statement (parser, NULL, false);
13770 finish_handler (handler);
13773 /* Parse an exception-declaration.
13775 exception-declaration:
13776 type-specifier-seq declarator
13777 type-specifier-seq abstract-declarator
13781 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13782 ellipsis variant is used. */
13785 cp_parser_exception_declaration (cp_parser* parser)
13788 cp_decl_specifier_seq type_specifiers;
13789 cp_declarator *declarator;
13790 const char *saved_message;
13792 /* If it's an ellipsis, it's easy to handle. */
13793 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13795 /* Consume the `...' token. */
13796 cp_lexer_consume_token (parser->lexer);
13800 /* Types may not be defined in exception-declarations. */
13801 saved_message = parser->type_definition_forbidden_message;
13802 parser->type_definition_forbidden_message
13803 = "types may not be defined in exception-declarations";
13805 /* Parse the type-specifier-seq. */
13806 cp_parser_type_specifier_seq (parser, &type_specifiers);
13807 /* If it's a `)', then there is no declarator. */
13808 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13811 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13812 /*ctor_dtor_or_conv_p=*/NULL,
13813 /*parenthesized_p=*/NULL,
13814 /*member_p=*/false);
13816 /* Restore the saved message. */
13817 parser->type_definition_forbidden_message = saved_message;
13819 if (type_specifiers.any_specifiers_p)
13821 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13822 if (decl == NULL_TREE)
13823 error ("invalid catch parameter");
13831 /* Parse a throw-expression.
13834 throw assignment-expression [opt]
13836 Returns a THROW_EXPR representing the throw-expression. */
13839 cp_parser_throw_expression (cp_parser* parser)
13844 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13845 token = cp_lexer_peek_token (parser->lexer);
13846 /* Figure out whether or not there is an assignment-expression
13847 following the "throw" keyword. */
13848 if (token->type == CPP_COMMA
13849 || token->type == CPP_SEMICOLON
13850 || token->type == CPP_CLOSE_PAREN
13851 || token->type == CPP_CLOSE_SQUARE
13852 || token->type == CPP_CLOSE_BRACE
13853 || token->type == CPP_COLON)
13854 expression = NULL_TREE;
13856 expression = cp_parser_assignment_expression (parser,
13859 return build_throw (expression);
13862 /* GNU Extensions */
13864 /* Parse an (optional) asm-specification.
13867 asm ( string-literal )
13869 If the asm-specification is present, returns a STRING_CST
13870 corresponding to the string-literal. Otherwise, returns
13874 cp_parser_asm_specification_opt (cp_parser* parser)
13877 tree asm_specification;
13879 /* Peek at the next token. */
13880 token = cp_lexer_peek_token (parser->lexer);
13881 /* If the next token isn't the `asm' keyword, then there's no
13882 asm-specification. */
13883 if (!cp_parser_is_keyword (token, RID_ASM))
13886 /* Consume the `asm' token. */
13887 cp_lexer_consume_token (parser->lexer);
13888 /* Look for the `('. */
13889 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13891 /* Look for the string-literal. */
13892 asm_specification = cp_parser_string_literal (parser, false, false);
13894 /* Look for the `)'. */
13895 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13897 return asm_specification;
13900 /* Parse an asm-operand-list.
13904 asm-operand-list , asm-operand
13907 string-literal ( expression )
13908 [ string-literal ] string-literal ( expression )
13910 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13911 each node is the expression. The TREE_PURPOSE is itself a
13912 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13913 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13914 is a STRING_CST for the string literal before the parenthesis. */
13917 cp_parser_asm_operand_list (cp_parser* parser)
13919 tree asm_operands = NULL_TREE;
13923 tree string_literal;
13927 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13929 /* Consume the `[' token. */
13930 cp_lexer_consume_token (parser->lexer);
13931 /* Read the operand name. */
13932 name = cp_parser_identifier (parser);
13933 if (name != error_mark_node)
13934 name = build_string (IDENTIFIER_LENGTH (name),
13935 IDENTIFIER_POINTER (name));
13936 /* Look for the closing `]'. */
13937 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13941 /* Look for the string-literal. */
13942 string_literal = cp_parser_string_literal (parser, false, false);
13944 /* Look for the `('. */
13945 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13946 /* Parse the expression. */
13947 expression = cp_parser_expression (parser, /*cast_p=*/false);
13948 /* Look for the `)'. */
13949 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13951 /* Add this operand to the list. */
13952 asm_operands = tree_cons (build_tree_list (name, string_literal),
13955 /* If the next token is not a `,', there are no more
13957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13959 /* Consume the `,'. */
13960 cp_lexer_consume_token (parser->lexer);
13963 return nreverse (asm_operands);
13966 /* Parse an asm-clobber-list.
13970 asm-clobber-list , string-literal
13972 Returns a TREE_LIST, indicating the clobbers in the order that they
13973 appeared. The TREE_VALUE of each node is a STRING_CST. */
13976 cp_parser_asm_clobber_list (cp_parser* parser)
13978 tree clobbers = NULL_TREE;
13982 tree string_literal;
13984 /* Look for the string literal. */
13985 string_literal = cp_parser_string_literal (parser, false, false);
13986 /* Add it to the list. */
13987 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13988 /* If the next token is not a `,', then the list is
13990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13992 /* Consume the `,' token. */
13993 cp_lexer_consume_token (parser->lexer);
13999 /* Parse an (optional) series of attributes.
14002 attributes attribute
14005 __attribute__ (( attribute-list [opt] ))
14007 The return value is as for cp_parser_attribute_list. */
14010 cp_parser_attributes_opt (cp_parser* parser)
14012 tree attributes = NULL_TREE;
14017 tree attribute_list;
14019 /* Peek at the next token. */
14020 token = cp_lexer_peek_token (parser->lexer);
14021 /* If it's not `__attribute__', then we're done. */
14022 if (token->keyword != RID_ATTRIBUTE)
14025 /* Consume the `__attribute__' keyword. */
14026 cp_lexer_consume_token (parser->lexer);
14027 /* Look for the two `(' tokens. */
14028 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14029 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14031 /* Peek at the next token. */
14032 token = cp_lexer_peek_token (parser->lexer);
14033 if (token->type != CPP_CLOSE_PAREN)
14034 /* Parse the attribute-list. */
14035 attribute_list = cp_parser_attribute_list (parser);
14037 /* If the next token is a `)', then there is no attribute
14039 attribute_list = NULL;
14041 /* Look for the two `)' tokens. */
14042 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14043 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14045 /* Add these new attributes to the list. */
14046 attributes = chainon (attributes, attribute_list);
14052 /* Parse an attribute-list.
14056 attribute-list , attribute
14060 identifier ( identifier )
14061 identifier ( identifier , expression-list )
14062 identifier ( expression-list )
14064 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14065 to an attribute. The TREE_PURPOSE of each node is the identifier
14066 indicating which attribute is in use. The TREE_VALUE represents
14067 the arguments, if any. */
14070 cp_parser_attribute_list (cp_parser* parser)
14072 tree attribute_list = NULL_TREE;
14073 bool save_translate_strings_p = parser->translate_strings_p;
14075 parser->translate_strings_p = false;
14082 /* Look for the identifier. We also allow keywords here; for
14083 example `__attribute__ ((const))' is legal. */
14084 token = cp_lexer_peek_token (parser->lexer);
14085 if (token->type == CPP_NAME
14086 || token->type == CPP_KEYWORD)
14088 /* Consume the token. */
14089 token = cp_lexer_consume_token (parser->lexer);
14091 /* Save away the identifier that indicates which attribute
14093 identifier = token->value;
14094 attribute = build_tree_list (identifier, NULL_TREE);
14096 /* Peek at the next token. */
14097 token = cp_lexer_peek_token (parser->lexer);
14098 /* If it's an `(', then parse the attribute arguments. */
14099 if (token->type == CPP_OPEN_PAREN)
14103 arguments = (cp_parser_parenthesized_expression_list
14104 (parser, true, /*cast_p=*/false,
14105 /*non_constant_p=*/NULL));
14106 /* Save the identifier and arguments away. */
14107 TREE_VALUE (attribute) = arguments;
14110 /* Add this attribute to the list. */
14111 TREE_CHAIN (attribute) = attribute_list;
14112 attribute_list = attribute;
14114 token = cp_lexer_peek_token (parser->lexer);
14116 /* Now, look for more attributes. If the next token isn't a
14117 `,', we're done. */
14118 if (token->type != CPP_COMMA)
14121 /* Consume the comma and keep going. */
14122 cp_lexer_consume_token (parser->lexer);
14124 parser->translate_strings_p = save_translate_strings_p;
14126 /* We built up the list in reverse order. */
14127 return nreverse (attribute_list);
14130 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14131 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14132 current value of the PEDANTIC flag, regardless of whether or not
14133 the `__extension__' keyword is present. The caller is responsible
14134 for restoring the value of the PEDANTIC flag. */
14137 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14139 /* Save the old value of the PEDANTIC flag. */
14140 *saved_pedantic = pedantic;
14142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14144 /* Consume the `__extension__' token. */
14145 cp_lexer_consume_token (parser->lexer);
14146 /* We're not being pedantic while the `__extension__' keyword is
14156 /* Parse a label declaration.
14159 __label__ label-declarator-seq ;
14161 label-declarator-seq:
14162 identifier , label-declarator-seq
14166 cp_parser_label_declaration (cp_parser* parser)
14168 /* Look for the `__label__' keyword. */
14169 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14175 /* Look for an identifier. */
14176 identifier = cp_parser_identifier (parser);
14177 /* Declare it as a lobel. */
14178 finish_label_decl (identifier);
14179 /* If the next token is a `;', stop. */
14180 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14182 /* Look for the `,' separating the label declarations. */
14183 cp_parser_require (parser, CPP_COMMA, "`,'");
14186 /* Look for the final `;'. */
14187 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14190 /* Support Functions */
14192 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14193 NAME should have one of the representations used for an
14194 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14195 is returned. If PARSER->SCOPE is a dependent type, then a
14196 SCOPE_REF is returned.
14198 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14199 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14200 was formed. Abstractly, such entities should not be passed to this
14201 function, because they do not need to be looked up, but it is
14202 simpler to check for this special case here, rather than at the
14205 In cases not explicitly covered above, this function returns a
14206 DECL, OVERLOAD, or baselink representing the result of the lookup.
14207 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14210 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14211 (e.g., "struct") that was used. In that case bindings that do not
14212 refer to types are ignored.
14214 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14217 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14220 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14223 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14224 results in an ambiguity, and false otherwise. */
14227 cp_parser_lookup_name (cp_parser *parser, tree name,
14228 enum tag_types tag_type,
14229 bool is_template, bool is_namespace,
14230 bool check_dependency,
14234 tree object_type = parser->context->object_type;
14236 /* Assume that the lookup will be unambiguous. */
14238 *ambiguous_p = false;
14240 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14241 no longer valid. Note that if we are parsing tentatively, and
14242 the parse fails, OBJECT_TYPE will be automatically restored. */
14243 parser->context->object_type = NULL_TREE;
14245 if (name == error_mark_node)
14246 return error_mark_node;
14248 /* A template-id has already been resolved; there is no lookup to
14250 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14252 if (BASELINK_P (name))
14254 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14255 == TEMPLATE_ID_EXPR);
14259 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14260 it should already have been checked to make sure that the name
14261 used matches the type being destroyed. */
14262 if (TREE_CODE (name) == BIT_NOT_EXPR)
14266 /* Figure out to which type this destructor applies. */
14268 type = parser->scope;
14269 else if (object_type)
14270 type = object_type;
14272 type = current_class_type;
14273 /* If that's not a class type, there is no destructor. */
14274 if (!type || !CLASS_TYPE_P (type))
14275 return error_mark_node;
14276 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14277 lazily_declare_fn (sfk_destructor, type);
14278 if (!CLASSTYPE_DESTRUCTORS (type))
14279 return error_mark_node;
14280 /* If it was a class type, return the destructor. */
14281 return CLASSTYPE_DESTRUCTORS (type);
14284 /* By this point, the NAME should be an ordinary identifier. If
14285 the id-expression was a qualified name, the qualifying scope is
14286 stored in PARSER->SCOPE at this point. */
14287 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14289 /* Perform the lookup. */
14294 if (parser->scope == error_mark_node)
14295 return error_mark_node;
14297 /* If the SCOPE is dependent, the lookup must be deferred until
14298 the template is instantiated -- unless we are explicitly
14299 looking up names in uninstantiated templates. Even then, we
14300 cannot look up the name if the scope is not a class type; it
14301 might, for example, be a template type parameter. */
14302 dependent_p = (TYPE_P (parser->scope)
14303 && !(parser->in_declarator_p
14304 && currently_open_class (parser->scope))
14305 && dependent_type_p (parser->scope));
14306 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14313 /* The resolution to Core Issue 180 says that `struct
14314 A::B' should be considered a type-name, even if `A'
14316 type = make_typename_type (parser->scope, name, tag_type,
14318 decl = TYPE_NAME (type);
14320 else if (is_template)
14321 decl = make_unbound_class_template (parser->scope,
14325 decl = build_nt (SCOPE_REF, parser->scope, name);
14329 tree pushed_scope = NULL_TREE;
14331 /* If PARSER->SCOPE is a dependent type, then it must be a
14332 class type, and we must not be checking dependencies;
14333 otherwise, we would have processed this lookup above. So
14334 that PARSER->SCOPE is not considered a dependent base by
14335 lookup_member, we must enter the scope here. */
14337 pushed_scope = push_scope (parser->scope);
14338 /* If the PARSER->SCOPE is a template specialization, it
14339 may be instantiated during name lookup. In that case,
14340 errors may be issued. Even if we rollback the current
14341 tentative parse, those errors are valid. */
14342 decl = lookup_qualified_name (parser->scope, name,
14343 tag_type != none_type,
14344 /*complain=*/true);
14346 pop_scope (pushed_scope);
14348 parser->qualifying_scope = parser->scope;
14349 parser->object_scope = NULL_TREE;
14351 else if (object_type)
14353 tree object_decl = NULL_TREE;
14354 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14355 OBJECT_TYPE is not a class. */
14356 if (CLASS_TYPE_P (object_type))
14357 /* If the OBJECT_TYPE is a template specialization, it may
14358 be instantiated during name lookup. In that case, errors
14359 may be issued. Even if we rollback the current tentative
14360 parse, those errors are valid. */
14361 object_decl = lookup_member (object_type,
14364 tag_type != none_type);
14365 /* Look it up in the enclosing context, too. */
14366 decl = lookup_name_real (name, tag_type != none_type,
14368 /*block_p=*/true, is_namespace,
14370 parser->object_scope = object_type;
14371 parser->qualifying_scope = NULL_TREE;
14373 decl = object_decl;
14377 decl = lookup_name_real (name, tag_type != none_type,
14379 /*block_p=*/true, is_namespace,
14381 parser->qualifying_scope = NULL_TREE;
14382 parser->object_scope = NULL_TREE;
14385 /* If the lookup failed, let our caller know. */
14386 if (!decl || decl == error_mark_node)
14387 return error_mark_node;
14389 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14390 if (TREE_CODE (decl) == TREE_LIST)
14393 *ambiguous_p = true;
14394 /* The error message we have to print is too complicated for
14395 cp_parser_error, so we incorporate its actions directly. */
14396 if (!cp_parser_simulate_error (parser))
14398 error ("reference to %qD is ambiguous", name);
14399 print_candidates (decl);
14401 return error_mark_node;
14404 gcc_assert (DECL_P (decl)
14405 || TREE_CODE (decl) == OVERLOAD
14406 || TREE_CODE (decl) == SCOPE_REF
14407 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14408 || BASELINK_P (decl));
14410 /* If we have resolved the name of a member declaration, check to
14411 see if the declaration is accessible. When the name resolves to
14412 set of overloaded functions, accessibility is checked when
14413 overload resolution is done.
14415 During an explicit instantiation, access is not checked at all,
14416 as per [temp.explicit]. */
14418 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14423 /* Like cp_parser_lookup_name, but for use in the typical case where
14424 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14425 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14428 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14430 return cp_parser_lookup_name (parser, name,
14432 /*is_template=*/false,
14433 /*is_namespace=*/false,
14434 /*check_dependency=*/true,
14435 /*ambiguous_p=*/NULL);
14438 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14439 the current context, return the TYPE_DECL. If TAG_NAME_P is
14440 true, the DECL indicates the class being defined in a class-head,
14441 or declared in an elaborated-type-specifier.
14443 Otherwise, return DECL. */
14446 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14448 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14449 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14452 template <typename T> struct B;
14455 template <typename T> struct A::B {};
14457 Similarly, in a elaborated-type-specifier:
14459 namespace N { struct X{}; }
14462 template <typename T> friend struct N::X;
14465 However, if the DECL refers to a class type, and we are in
14466 the scope of the class, then the name lookup automatically
14467 finds the TYPE_DECL created by build_self_reference rather
14468 than a TEMPLATE_DECL. For example, in:
14470 template <class T> struct S {
14474 there is no need to handle such case. */
14476 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14477 return DECL_TEMPLATE_RESULT (decl);
14482 /* If too many, or too few, template-parameter lists apply to the
14483 declarator, issue an error message. Returns TRUE if all went well,
14484 and FALSE otherwise. */
14487 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14488 cp_declarator *declarator)
14490 unsigned num_templates;
14492 /* We haven't seen any classes that involve template parameters yet. */
14495 switch (declarator->kind)
14498 if (declarator->u.id.qualifying_scope)
14503 scope = declarator->u.id.qualifying_scope;
14504 member = declarator->u.id.unqualified_name;
14506 while (scope && CLASS_TYPE_P (scope))
14508 /* You're supposed to have one `template <...>'
14509 for every template class, but you don't need one
14510 for a full specialization. For example:
14512 template <class T> struct S{};
14513 template <> struct S<int> { void f(); };
14514 void S<int>::f () {}
14516 is correct; there shouldn't be a `template <>' for
14517 the definition of `S<int>::f'. */
14518 if (CLASSTYPE_TEMPLATE_INFO (scope)
14519 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14520 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14521 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14524 scope = TYPE_CONTEXT (scope);
14527 else if (TREE_CODE (declarator->u.id.unqualified_name)
14528 == TEMPLATE_ID_EXPR)
14529 /* If the DECLARATOR has the form `X<y>' then it uses one
14530 additional level of template parameters. */
14533 return cp_parser_check_template_parameters (parser,
14539 case cdk_reference:
14541 return (cp_parser_check_declarator_template_parameters
14542 (parser, declarator->declarator));
14548 gcc_unreachable ();
14553 /* NUM_TEMPLATES were used in the current declaration. If that is
14554 invalid, return FALSE and issue an error messages. Otherwise,
14558 cp_parser_check_template_parameters (cp_parser* parser,
14559 unsigned num_templates)
14561 /* If there are more template classes than parameter lists, we have
14564 template <class T> void S<T>::R<T>::f (); */
14565 if (parser->num_template_parameter_lists < num_templates)
14567 error ("too few template-parameter-lists");
14570 /* If there are the same number of template classes and parameter
14571 lists, that's OK. */
14572 if (parser->num_template_parameter_lists == num_templates)
14574 /* If there are more, but only one more, then we are referring to a
14575 member template. That's OK too. */
14576 if (parser->num_template_parameter_lists == num_templates + 1)
14578 /* Otherwise, there are too many template parameter lists. We have
14581 template <class T> template <class U> void S::f(); */
14582 error ("too many template-parameter-lists");
14586 /* Parse an optional `::' token indicating that the following name is
14587 from the global namespace. If so, PARSER->SCOPE is set to the
14588 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14589 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14590 Returns the new value of PARSER->SCOPE, if the `::' token is
14591 present, and NULL_TREE otherwise. */
14594 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14598 /* Peek at the next token. */
14599 token = cp_lexer_peek_token (parser->lexer);
14600 /* If we're looking at a `::' token then we're starting from the
14601 global namespace, not our current location. */
14602 if (token->type == CPP_SCOPE)
14604 /* Consume the `::' token. */
14605 cp_lexer_consume_token (parser->lexer);
14606 /* Set the SCOPE so that we know where to start the lookup. */
14607 parser->scope = global_namespace;
14608 parser->qualifying_scope = global_namespace;
14609 parser->object_scope = NULL_TREE;
14611 return parser->scope;
14613 else if (!current_scope_valid_p)
14615 parser->scope = NULL_TREE;
14616 parser->qualifying_scope = NULL_TREE;
14617 parser->object_scope = NULL_TREE;
14623 /* Returns TRUE if the upcoming token sequence is the start of a
14624 constructor declarator. If FRIEND_P is true, the declarator is
14625 preceded by the `friend' specifier. */
14628 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14630 bool constructor_p;
14631 tree type_decl = NULL_TREE;
14632 bool nested_name_p;
14633 cp_token *next_token;
14635 /* The common case is that this is not a constructor declarator, so
14636 try to avoid doing lots of work if at all possible. It's not
14637 valid declare a constructor at function scope. */
14638 if (at_function_scope_p ())
14640 /* And only certain tokens can begin a constructor declarator. */
14641 next_token = cp_lexer_peek_token (parser->lexer);
14642 if (next_token->type != CPP_NAME
14643 && next_token->type != CPP_SCOPE
14644 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14645 && next_token->type != CPP_TEMPLATE_ID)
14648 /* Parse tentatively; we are going to roll back all of the tokens
14650 cp_parser_parse_tentatively (parser);
14651 /* Assume that we are looking at a constructor declarator. */
14652 constructor_p = true;
14654 /* Look for the optional `::' operator. */
14655 cp_parser_global_scope_opt (parser,
14656 /*current_scope_valid_p=*/false);
14657 /* Look for the nested-name-specifier. */
14659 = (cp_parser_nested_name_specifier_opt (parser,
14660 /*typename_keyword_p=*/false,
14661 /*check_dependency_p=*/false,
14663 /*is_declaration=*/false)
14665 /* Outside of a class-specifier, there must be a
14666 nested-name-specifier. */
14667 if (!nested_name_p &&
14668 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14670 constructor_p = false;
14671 /* If we still think that this might be a constructor-declarator,
14672 look for a class-name. */
14677 template <typename T> struct S { S(); };
14678 template <typename T> S<T>::S ();
14680 we must recognize that the nested `S' names a class.
14683 template <typename T> S<T>::S<T> ();
14685 we must recognize that the nested `S' names a template. */
14686 type_decl = cp_parser_class_name (parser,
14687 /*typename_keyword_p=*/false,
14688 /*template_keyword_p=*/false,
14690 /*check_dependency_p=*/false,
14691 /*class_head_p=*/false,
14692 /*is_declaration=*/false);
14693 /* If there was no class-name, then this is not a constructor. */
14694 constructor_p = !cp_parser_error_occurred (parser);
14697 /* If we're still considering a constructor, we have to see a `(',
14698 to begin the parameter-declaration-clause, followed by either a
14699 `)', an `...', or a decl-specifier. We need to check for a
14700 type-specifier to avoid being fooled into thinking that:
14704 is a constructor. (It is actually a function named `f' that
14705 takes one parameter (of type `int') and returns a value of type
14708 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14711 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14712 /* A parameter declaration begins with a decl-specifier,
14713 which is either the "attribute" keyword, a storage class
14714 specifier, or (usually) a type-specifier. */
14715 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14716 && !cp_parser_storage_class_specifier_opt (parser))
14719 tree pushed_scope = NULL_TREE;
14720 unsigned saved_num_template_parameter_lists;
14722 /* Names appearing in the type-specifier should be looked up
14723 in the scope of the class. */
14724 if (current_class_type)
14728 type = TREE_TYPE (type_decl);
14729 if (TREE_CODE (type) == TYPENAME_TYPE)
14731 type = resolve_typename_type (type,
14732 /*only_current_p=*/false);
14733 if (type == error_mark_node)
14735 cp_parser_abort_tentative_parse (parser);
14739 pushed_scope = push_scope (type);
14742 /* Inside the constructor parameter list, surrounding
14743 template-parameter-lists do not apply. */
14744 saved_num_template_parameter_lists
14745 = parser->num_template_parameter_lists;
14746 parser->num_template_parameter_lists = 0;
14748 /* Look for the type-specifier. */
14749 cp_parser_type_specifier (parser,
14750 CP_PARSER_FLAGS_NONE,
14751 /*decl_specs=*/NULL,
14752 /*is_declarator=*/true,
14753 /*declares_class_or_enum=*/NULL,
14754 /*is_cv_qualifier=*/NULL);
14756 parser->num_template_parameter_lists
14757 = saved_num_template_parameter_lists;
14759 /* Leave the scope of the class. */
14761 pop_scope (pushed_scope);
14763 constructor_p = !cp_parser_error_occurred (parser);
14767 constructor_p = false;
14768 /* We did not really want to consume any tokens. */
14769 cp_parser_abort_tentative_parse (parser);
14771 return constructor_p;
14774 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14775 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14776 they must be performed once we are in the scope of the function.
14778 Returns the function defined. */
14781 cp_parser_function_definition_from_specifiers_and_declarator
14782 (cp_parser* parser,
14783 cp_decl_specifier_seq *decl_specifiers,
14785 const cp_declarator *declarator)
14790 /* Begin the function-definition. */
14791 success_p = start_function (decl_specifiers, declarator, attributes);
14793 /* The things we're about to see are not directly qualified by any
14794 template headers we've seen thus far. */
14795 reset_specialization ();
14797 /* If there were names looked up in the decl-specifier-seq that we
14798 did not check, check them now. We must wait until we are in the
14799 scope of the function to perform the checks, since the function
14800 might be a friend. */
14801 perform_deferred_access_checks ();
14805 /* Skip the entire function. */
14806 error ("invalid function declaration");
14807 cp_parser_skip_to_end_of_block_or_statement (parser);
14808 fn = error_mark_node;
14811 fn = cp_parser_function_definition_after_declarator (parser,
14812 /*inline_p=*/false);
14817 /* Parse the part of a function-definition that follows the
14818 declarator. INLINE_P is TRUE iff this function is an inline
14819 function defined with a class-specifier.
14821 Returns the function defined. */
14824 cp_parser_function_definition_after_declarator (cp_parser* parser,
14828 bool ctor_initializer_p = false;
14829 bool saved_in_unbraced_linkage_specification_p;
14830 unsigned saved_num_template_parameter_lists;
14832 /* If the next token is `return', then the code may be trying to
14833 make use of the "named return value" extension that G++ used to
14835 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14837 /* Consume the `return' keyword. */
14838 cp_lexer_consume_token (parser->lexer);
14839 /* Look for the identifier that indicates what value is to be
14841 cp_parser_identifier (parser);
14842 /* Issue an error message. */
14843 error ("named return values are no longer supported");
14844 /* Skip tokens until we reach the start of the function body. */
14845 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14846 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14847 cp_lexer_consume_token (parser->lexer);
14849 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14850 anything declared inside `f'. */
14851 saved_in_unbraced_linkage_specification_p
14852 = parser->in_unbraced_linkage_specification_p;
14853 parser->in_unbraced_linkage_specification_p = false;
14854 /* Inside the function, surrounding template-parameter-lists do not
14856 saved_num_template_parameter_lists
14857 = parser->num_template_parameter_lists;
14858 parser->num_template_parameter_lists = 0;
14859 /* If the next token is `try', then we are looking at a
14860 function-try-block. */
14861 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14862 ctor_initializer_p = cp_parser_function_try_block (parser);
14863 /* A function-try-block includes the function-body, so we only do
14864 this next part if we're not processing a function-try-block. */
14867 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14869 /* Finish the function. */
14870 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14871 (inline_p ? 2 : 0));
14872 /* Generate code for it, if necessary. */
14873 expand_or_defer_fn (fn);
14874 /* Restore the saved values. */
14875 parser->in_unbraced_linkage_specification_p
14876 = saved_in_unbraced_linkage_specification_p;
14877 parser->num_template_parameter_lists
14878 = saved_num_template_parameter_lists;
14883 /* Parse a template-declaration, assuming that the `export' (and
14884 `extern') keywords, if present, has already been scanned. MEMBER_P
14885 is as for cp_parser_template_declaration. */
14888 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14890 tree decl = NULL_TREE;
14891 tree parameter_list;
14892 bool friend_p = false;
14894 /* Look for the `template' keyword. */
14895 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14899 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14902 /* If the next token is `>', then we have an invalid
14903 specialization. Rather than complain about an invalid template
14904 parameter, issue an error message here. */
14905 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14907 cp_parser_error (parser, "invalid explicit specialization");
14908 begin_specialization ();
14909 parameter_list = NULL_TREE;
14913 /* Parse the template parameters. */
14914 begin_template_parm_list ();
14915 parameter_list = cp_parser_template_parameter_list (parser);
14916 parameter_list = end_template_parm_list (parameter_list);
14919 /* Look for the `>'. */
14920 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14921 /* We just processed one more parameter list. */
14922 ++parser->num_template_parameter_lists;
14923 /* If the next token is `template', there are more template
14925 if (cp_lexer_next_token_is_keyword (parser->lexer,
14927 cp_parser_template_declaration_after_export (parser, member_p);
14930 /* There are no access checks when parsing a template, as we do not
14931 know if a specialization will be a friend. */
14932 push_deferring_access_checks (dk_no_check);
14934 decl = cp_parser_single_declaration (parser,
14938 pop_deferring_access_checks ();
14940 /* If this is a member template declaration, let the front
14942 if (member_p && !friend_p && decl)
14944 if (TREE_CODE (decl) == TYPE_DECL)
14945 cp_parser_check_access_in_redeclaration (decl);
14947 decl = finish_member_template_decl (decl);
14949 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14950 make_friend_class (current_class_type, TREE_TYPE (decl),
14951 /*complain=*/true);
14953 /* We are done with the current parameter list. */
14954 --parser->num_template_parameter_lists;
14957 finish_template_decl (parameter_list);
14959 /* Register member declarations. */
14960 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14961 finish_member_declaration (decl);
14963 /* If DECL is a function template, we must return to parse it later.
14964 (Even though there is no definition, there might be default
14965 arguments that need handling.) */
14966 if (member_p && decl
14967 && (TREE_CODE (decl) == FUNCTION_DECL
14968 || DECL_FUNCTION_TEMPLATE_P (decl)))
14969 TREE_VALUE (parser->unparsed_functions_queues)
14970 = tree_cons (NULL_TREE, decl,
14971 TREE_VALUE (parser->unparsed_functions_queues));
14974 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14975 `function-definition' sequence. MEMBER_P is true, this declaration
14976 appears in a class scope.
14978 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14979 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14982 cp_parser_single_declaration (cp_parser* parser,
14986 int declares_class_or_enum;
14987 tree decl = NULL_TREE;
14988 cp_decl_specifier_seq decl_specifiers;
14989 bool function_definition_p = false;
14991 /* This function is only used when processing a template
14993 gcc_assert (innermost_scope_kind () == sk_template_parms
14994 || innermost_scope_kind () == sk_template_spec);
14996 /* Defer access checks until we know what is being declared. */
14997 push_deferring_access_checks (dk_deferred);
14999 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15001 cp_parser_decl_specifier_seq (parser,
15002 CP_PARSER_FLAGS_OPTIONAL,
15004 &declares_class_or_enum);
15006 *friend_p = cp_parser_friend_p (&decl_specifiers);
15008 /* There are no template typedefs. */
15009 if (decl_specifiers.specs[(int) ds_typedef])
15011 error ("template declaration of %qs", "typedef");
15012 decl = error_mark_node;
15015 /* Gather up the access checks that occurred the
15016 decl-specifier-seq. */
15017 stop_deferring_access_checks ();
15019 /* Check for the declaration of a template class. */
15020 if (declares_class_or_enum)
15022 if (cp_parser_declares_only_class_p (parser))
15024 decl = shadow_tag (&decl_specifiers);
15029 friend template <typename T> struct A<T>::B;
15032 A<T>::B will be represented by a TYPENAME_TYPE, and
15033 therefore not recognized by shadow_tag. */
15034 if (friend_p && *friend_p
15036 && decl_specifiers.type
15037 && TYPE_P (decl_specifiers.type))
15038 decl = decl_specifiers.type;
15040 if (decl && decl != error_mark_node)
15041 decl = TYPE_NAME (decl);
15043 decl = error_mark_node;
15046 /* If it's not a template class, try for a template function. If
15047 the next token is a `;', then this declaration does not declare
15048 anything. But, if there were errors in the decl-specifiers, then
15049 the error might well have come from an attempted class-specifier.
15050 In that case, there's no need to warn about a missing declarator. */
15052 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15053 || decl_specifiers.type != error_mark_node))
15054 decl = cp_parser_init_declarator (parser,
15056 /*function_definition_allowed_p=*/true,
15058 declares_class_or_enum,
15059 &function_definition_p);
15061 pop_deferring_access_checks ();
15063 /* Clear any current qualification; whatever comes next is the start
15064 of something new. */
15065 parser->scope = NULL_TREE;
15066 parser->qualifying_scope = NULL_TREE;
15067 parser->object_scope = NULL_TREE;
15068 /* Look for a trailing `;' after the declaration. */
15069 if (!function_definition_p
15070 && (decl == error_mark_node
15071 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15072 cp_parser_skip_to_end_of_block_or_statement (parser);
15077 /* Parse a cast-expression that is not the operand of a unary "&". */
15080 cp_parser_simple_cast_expression (cp_parser *parser)
15082 return cp_parser_cast_expression (parser, /*address_p=*/false,
15086 /* Parse a functional cast to TYPE. Returns an expression
15087 representing the cast. */
15090 cp_parser_functional_cast (cp_parser* parser, tree type)
15092 tree expression_list;
15096 = cp_parser_parenthesized_expression_list (parser, false,
15098 /*non_constant_p=*/NULL);
15100 cast = build_functional_cast (type, expression_list);
15101 /* [expr.const]/1: In an integral constant expression "only type
15102 conversions to integral or enumeration type can be used". */
15103 if (cast != error_mark_node && !type_dependent_expression_p (type)
15104 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15106 if (cp_parser_non_integral_constant_expression
15107 (parser, "a call to a constructor"))
15108 return error_mark_node;
15113 /* Save the tokens that make up the body of a member function defined
15114 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15115 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15116 specifiers applied to the declaration. Returns the FUNCTION_DECL
15117 for the member function. */
15120 cp_parser_save_member_function_body (cp_parser* parser,
15121 cp_decl_specifier_seq *decl_specifiers,
15122 cp_declarator *declarator,
15129 /* Create the function-declaration. */
15130 fn = start_method (decl_specifiers, declarator, attributes);
15131 /* If something went badly wrong, bail out now. */
15132 if (fn == error_mark_node)
15134 /* If there's a function-body, skip it. */
15135 if (cp_parser_token_starts_function_definition_p
15136 (cp_lexer_peek_token (parser->lexer)))
15137 cp_parser_skip_to_end_of_block_or_statement (parser);
15138 return error_mark_node;
15141 /* Remember it, if there default args to post process. */
15142 cp_parser_save_default_args (parser, fn);
15144 /* Save away the tokens that make up the body of the
15146 first = parser->lexer->next_token;
15147 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15148 /* Handle function try blocks. */
15149 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15150 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15151 last = parser->lexer->next_token;
15153 /* Save away the inline definition; we will process it when the
15154 class is complete. */
15155 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15156 DECL_PENDING_INLINE_P (fn) = 1;
15158 /* We need to know that this was defined in the class, so that
15159 friend templates are handled correctly. */
15160 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15162 /* We're done with the inline definition. */
15163 finish_method (fn);
15165 /* Add FN to the queue of functions to be parsed later. */
15166 TREE_VALUE (parser->unparsed_functions_queues)
15167 = tree_cons (NULL_TREE, fn,
15168 TREE_VALUE (parser->unparsed_functions_queues));
15173 /* Parse a template-argument-list, as well as the trailing ">" (but
15174 not the opening ">"). See cp_parser_template_argument_list for the
15178 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15182 tree saved_qualifying_scope;
15183 tree saved_object_scope;
15184 bool saved_greater_than_is_operator_p;
15188 When parsing a template-id, the first non-nested `>' is taken as
15189 the end of the template-argument-list rather than a greater-than
15191 saved_greater_than_is_operator_p
15192 = parser->greater_than_is_operator_p;
15193 parser->greater_than_is_operator_p = false;
15194 /* Parsing the argument list may modify SCOPE, so we save it
15196 saved_scope = parser->scope;
15197 saved_qualifying_scope = parser->qualifying_scope;
15198 saved_object_scope = parser->object_scope;
15199 /* Parse the template-argument-list itself. */
15200 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15201 arguments = NULL_TREE;
15203 arguments = cp_parser_template_argument_list (parser);
15204 /* Look for the `>' that ends the template-argument-list. If we find
15205 a '>>' instead, it's probably just a typo. */
15206 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15208 if (!saved_greater_than_is_operator_p)
15210 /* If we're in a nested template argument list, the '>>' has
15211 to be a typo for '> >'. We emit the error message, but we
15212 continue parsing and we push a '>' as next token, so that
15213 the argument list will be parsed correctly. Note that the
15214 global source location is still on the token before the
15215 '>>', so we need to say explicitly where we want it. */
15216 cp_token *token = cp_lexer_peek_token (parser->lexer);
15217 error ("%H%<>>%> should be %<> >%> "
15218 "within a nested template argument list",
15221 /* ??? Proper recovery should terminate two levels of
15222 template argument list here. */
15223 token->type = CPP_GREATER;
15227 /* If this is not a nested template argument list, the '>>'
15228 is a typo for '>'. Emit an error message and continue.
15229 Same deal about the token location, but here we can get it
15230 right by consuming the '>>' before issuing the diagnostic. */
15231 cp_lexer_consume_token (parser->lexer);
15232 error ("spurious %<>>%>, use %<>%> to terminate "
15233 "a template argument list");
15236 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15237 error ("missing %<>%> to terminate the template argument list");
15239 /* It's what we want, a '>'; consume it. */
15240 cp_lexer_consume_token (parser->lexer);
15241 /* The `>' token might be a greater-than operator again now. */
15242 parser->greater_than_is_operator_p
15243 = saved_greater_than_is_operator_p;
15244 /* Restore the SAVED_SCOPE. */
15245 parser->scope = saved_scope;
15246 parser->qualifying_scope = saved_qualifying_scope;
15247 parser->object_scope = saved_object_scope;
15252 /* MEMBER_FUNCTION is a member function, or a friend. If default
15253 arguments, or the body of the function have not yet been parsed,
15257 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15259 /* If this member is a template, get the underlying
15261 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15262 member_function = DECL_TEMPLATE_RESULT (member_function);
15264 /* There should not be any class definitions in progress at this
15265 point; the bodies of members are only parsed outside of all class
15267 gcc_assert (parser->num_classes_being_defined == 0);
15268 /* While we're parsing the member functions we might encounter more
15269 classes. We want to handle them right away, but we don't want
15270 them getting mixed up with functions that are currently in the
15272 parser->unparsed_functions_queues
15273 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15275 /* Make sure that any template parameters are in scope. */
15276 maybe_begin_member_template_processing (member_function);
15278 /* If the body of the function has not yet been parsed, parse it
15280 if (DECL_PENDING_INLINE_P (member_function))
15282 tree function_scope;
15283 cp_token_cache *tokens;
15285 /* The function is no longer pending; we are processing it. */
15286 tokens = DECL_PENDING_INLINE_INFO (member_function);
15287 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15288 DECL_PENDING_INLINE_P (member_function) = 0;
15290 /* If this is a local class, enter the scope of the containing
15292 function_scope = current_function_decl;
15293 if (function_scope)
15294 push_function_context_to (function_scope);
15296 /* Push the body of the function onto the lexer stack. */
15297 cp_parser_push_lexer_for_tokens (parser, tokens);
15299 /* Let the front end know that we going to be defining this
15301 start_preparsed_function (member_function, NULL_TREE,
15302 SF_PRE_PARSED | SF_INCLASS_INLINE);
15304 /* Now, parse the body of the function. */
15305 cp_parser_function_definition_after_declarator (parser,
15306 /*inline_p=*/true);
15308 /* Leave the scope of the containing function. */
15309 if (function_scope)
15310 pop_function_context_from (function_scope);
15311 cp_parser_pop_lexer (parser);
15314 /* Remove any template parameters from the symbol table. */
15315 maybe_end_member_template_processing ();
15317 /* Restore the queue. */
15318 parser->unparsed_functions_queues
15319 = TREE_CHAIN (parser->unparsed_functions_queues);
15322 /* If DECL contains any default args, remember it on the unparsed
15323 functions queue. */
15326 cp_parser_save_default_args (cp_parser* parser, tree decl)
15330 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15332 probe = TREE_CHAIN (probe))
15333 if (TREE_PURPOSE (probe))
15335 TREE_PURPOSE (parser->unparsed_functions_queues)
15336 = tree_cons (current_class_type, decl,
15337 TREE_PURPOSE (parser->unparsed_functions_queues));
15343 /* FN is a FUNCTION_DECL which may contains a parameter with an
15344 unparsed DEFAULT_ARG. Parse the default args now. This function
15345 assumes that the current scope is the scope in which the default
15346 argument should be processed. */
15349 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15351 bool saved_local_variables_forbidden_p;
15354 /* While we're parsing the default args, we might (due to the
15355 statement expression extension) encounter more classes. We want
15356 to handle them right away, but we don't want them getting mixed
15357 up with default args that are currently in the queue. */
15358 parser->unparsed_functions_queues
15359 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15361 /* Local variable names (and the `this' keyword) may not appear
15362 in a default argument. */
15363 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15364 parser->local_variables_forbidden_p = true;
15366 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15368 parm = TREE_CHAIN (parm))
15370 cp_token_cache *tokens;
15372 if (!TREE_PURPOSE (parm)
15373 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15376 /* Push the saved tokens for the default argument onto the parser's
15378 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15379 cp_parser_push_lexer_for_tokens (parser, tokens);
15381 /* Parse the assignment-expression. */
15382 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser,
15385 /* If the token stream has not been completely used up, then
15386 there was extra junk after the end of the default
15388 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15389 cp_parser_error (parser, "expected %<,%>");
15391 /* Revert to the main lexer. */
15392 cp_parser_pop_lexer (parser);
15395 /* Restore the state of local_variables_forbidden_p. */
15396 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15398 /* Restore the queue. */
15399 parser->unparsed_functions_queues
15400 = TREE_CHAIN (parser->unparsed_functions_queues);
15403 /* Parse the operand of `sizeof' (or a similar operator). Returns
15404 either a TYPE or an expression, depending on the form of the
15405 input. The KEYWORD indicates which kind of expression we have
15409 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15411 static const char *format;
15412 tree expr = NULL_TREE;
15413 const char *saved_message;
15414 bool saved_integral_constant_expression_p;
15415 bool saved_non_integral_constant_expression_p;
15417 /* Initialize FORMAT the first time we get here. */
15419 format = "types may not be defined in '%s' expressions";
15421 /* Types cannot be defined in a `sizeof' expression. Save away the
15423 saved_message = parser->type_definition_forbidden_message;
15424 /* And create the new one. */
15425 parser->type_definition_forbidden_message
15426 = xmalloc (strlen (format)
15427 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15429 sprintf ((char *) parser->type_definition_forbidden_message,
15430 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15432 /* The restrictions on constant-expressions do not apply inside
15433 sizeof expressions. */
15434 saved_integral_constant_expression_p
15435 = parser->integral_constant_expression_p;
15436 saved_non_integral_constant_expression_p
15437 = parser->non_integral_constant_expression_p;
15438 parser->integral_constant_expression_p = false;
15440 /* Do not actually evaluate the expression. */
15442 /* If it's a `(', then we might be looking at the type-id
15444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15447 bool saved_in_type_id_in_expr_p;
15449 /* We can't be sure yet whether we're looking at a type-id or an
15451 cp_parser_parse_tentatively (parser);
15452 /* Consume the `('. */
15453 cp_lexer_consume_token (parser->lexer);
15454 /* Parse the type-id. */
15455 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15456 parser->in_type_id_in_expr_p = true;
15457 type = cp_parser_type_id (parser);
15458 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15459 /* Now, look for the trailing `)'. */
15460 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15461 /* If all went well, then we're done. */
15462 if (cp_parser_parse_definitely (parser))
15464 cp_decl_specifier_seq decl_specs;
15466 /* Build a trivial decl-specifier-seq. */
15467 clear_decl_specs (&decl_specs);
15468 decl_specs.type = type;
15470 /* Call grokdeclarator to figure out what type this is. */
15471 expr = grokdeclarator (NULL,
15475 /*attrlist=*/NULL);
15479 /* If the type-id production did not work out, then we must be
15480 looking at the unary-expression production. */
15482 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15484 /* Go back to evaluating expressions. */
15487 /* Free the message we created. */
15488 free ((char *) parser->type_definition_forbidden_message);
15489 /* And restore the old one. */
15490 parser->type_definition_forbidden_message = saved_message;
15491 parser->integral_constant_expression_p
15492 = saved_integral_constant_expression_p;
15493 parser->non_integral_constant_expression_p
15494 = saved_non_integral_constant_expression_p;
15499 /* If the current declaration has no declarator, return true. */
15502 cp_parser_declares_only_class_p (cp_parser *parser)
15504 /* If the next token is a `;' or a `,' then there is no
15506 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15507 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15510 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15513 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15514 cp_storage_class storage_class)
15516 if (decl_specs->storage_class != sc_none)
15517 decl_specs->multiple_storage_classes_p = true;
15519 decl_specs->storage_class = storage_class;
15522 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15523 is true, the type is a user-defined type; otherwise it is a
15524 built-in type specified by a keyword. */
15527 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15529 bool user_defined_p)
15531 decl_specs->any_specifiers_p = true;
15533 /* If the user tries to redeclare bool or wchar_t (with, for
15534 example, in "typedef int wchar_t;") we remember that this is what
15535 happened. In system headers, we ignore these declarations so
15536 that G++ can work with system headers that are not C++-safe. */
15537 if (decl_specs->specs[(int) ds_typedef]
15539 && (type_spec == boolean_type_node
15540 || type_spec == wchar_type_node)
15541 && (decl_specs->type
15542 || decl_specs->specs[(int) ds_long]
15543 || decl_specs->specs[(int) ds_short]
15544 || decl_specs->specs[(int) ds_unsigned]
15545 || decl_specs->specs[(int) ds_signed]))
15547 decl_specs->redefined_builtin_type = type_spec;
15548 if (!decl_specs->type)
15550 decl_specs->type = type_spec;
15551 decl_specs->user_defined_type_p = false;
15554 else if (decl_specs->type)
15555 decl_specs->multiple_types_p = true;
15558 decl_specs->type = type_spec;
15559 decl_specs->user_defined_type_p = user_defined_p;
15560 decl_specs->redefined_builtin_type = NULL_TREE;
15564 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15565 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15568 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15570 return decl_specifiers->specs[(int) ds_friend] != 0;
15573 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15574 issue an error message indicating that TOKEN_DESC was expected.
15576 Returns the token consumed, if the token had the appropriate type.
15577 Otherwise, returns NULL. */
15580 cp_parser_require (cp_parser* parser,
15581 enum cpp_ttype type,
15582 const char* token_desc)
15584 if (cp_lexer_next_token_is (parser->lexer, type))
15585 return cp_lexer_consume_token (parser->lexer);
15588 /* Output the MESSAGE -- unless we're parsing tentatively. */
15589 if (!cp_parser_simulate_error (parser))
15591 char *message = concat ("expected ", token_desc, NULL);
15592 cp_parser_error (parser, message);
15599 /* Like cp_parser_require, except that tokens will be skipped until
15600 the desired token is found. An error message is still produced if
15601 the next token is not as expected. */
15604 cp_parser_skip_until_found (cp_parser* parser,
15605 enum cpp_ttype type,
15606 const char* token_desc)
15609 unsigned nesting_depth = 0;
15611 if (cp_parser_require (parser, type, token_desc))
15614 /* Skip tokens until the desired token is found. */
15617 /* Peek at the next token. */
15618 token = cp_lexer_peek_token (parser->lexer);
15619 /* If we've reached the token we want, consume it and
15621 if (token->type == type && !nesting_depth)
15623 cp_lexer_consume_token (parser->lexer);
15626 /* If we've run out of tokens, stop. */
15627 if (token->type == CPP_EOF)
15629 if (token->type == CPP_OPEN_BRACE
15630 || token->type == CPP_OPEN_PAREN
15631 || token->type == CPP_OPEN_SQUARE)
15633 else if (token->type == CPP_CLOSE_BRACE
15634 || token->type == CPP_CLOSE_PAREN
15635 || token->type == CPP_CLOSE_SQUARE)
15637 if (nesting_depth-- == 0)
15640 /* Consume this token. */
15641 cp_lexer_consume_token (parser->lexer);
15645 /* If the next token is the indicated keyword, consume it. Otherwise,
15646 issue an error message indicating that TOKEN_DESC was expected.
15648 Returns the token consumed, if the token had the appropriate type.
15649 Otherwise, returns NULL. */
15652 cp_parser_require_keyword (cp_parser* parser,
15654 const char* token_desc)
15656 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15658 if (token && token->keyword != keyword)
15660 dyn_string_t error_msg;
15662 /* Format the error message. */
15663 error_msg = dyn_string_new (0);
15664 dyn_string_append_cstr (error_msg, "expected ");
15665 dyn_string_append_cstr (error_msg, token_desc);
15666 cp_parser_error (parser, error_msg->s);
15667 dyn_string_delete (error_msg);
15674 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15675 function-definition. */
15678 cp_parser_token_starts_function_definition_p (cp_token* token)
15680 return (/* An ordinary function-body begins with an `{'. */
15681 token->type == CPP_OPEN_BRACE
15682 /* A ctor-initializer begins with a `:'. */
15683 || token->type == CPP_COLON
15684 /* A function-try-block begins with `try'. */
15685 || token->keyword == RID_TRY
15686 /* The named return value extension begins with `return'. */
15687 || token->keyword == RID_RETURN);
15690 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15694 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15698 token = cp_lexer_peek_token (parser->lexer);
15699 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15702 /* Returns TRUE iff the next token is the "," or ">" ending a
15703 template-argument. */
15706 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15710 token = cp_lexer_peek_token (parser->lexer);
15711 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15714 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15715 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15718 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15723 token = cp_lexer_peek_nth_token (parser->lexer, n);
15724 if (token->type == CPP_LESS)
15726 /* Check for the sequence `<::' in the original code. It would be lexed as
15727 `[:', where `[' is a digraph, and there is no whitespace before
15729 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15732 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15733 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15739 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15740 or none_type otherwise. */
15742 static enum tag_types
15743 cp_parser_token_is_class_key (cp_token* token)
15745 switch (token->keyword)
15750 return record_type;
15759 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15762 cp_parser_check_class_key (enum tag_types class_key, tree type)
15764 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15765 pedwarn ("%qs tag used in naming %q#T",
15766 class_key == union_type ? "union"
15767 : class_key == record_type ? "struct" : "class",
15771 /* Issue an error message if DECL is redeclared with different
15772 access than its original declaration [class.access.spec/3].
15773 This applies to nested classes and nested class templates.
15777 cp_parser_check_access_in_redeclaration (tree decl)
15779 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15782 if ((TREE_PRIVATE (decl)
15783 != (current_access_specifier == access_private_node))
15784 || (TREE_PROTECTED (decl)
15785 != (current_access_specifier == access_protected_node)))
15786 error ("%qD redeclared with different access", decl);
15789 /* Look for the `template' keyword, as a syntactic disambiguator.
15790 Return TRUE iff it is present, in which case it will be
15794 cp_parser_optional_template_keyword (cp_parser *parser)
15796 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15798 /* The `template' keyword can only be used within templates;
15799 outside templates the parser can always figure out what is a
15800 template and what is not. */
15801 if (!processing_template_decl)
15803 error ("%<template%> (as a disambiguator) is only allowed "
15804 "within templates");
15805 /* If this part of the token stream is rescanned, the same
15806 error message would be generated. So, we purge the token
15807 from the stream. */
15808 cp_lexer_purge_token (parser->lexer);
15813 /* Consume the `template' keyword. */
15814 cp_lexer_consume_token (parser->lexer);
15822 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15823 set PARSER->SCOPE, and perform other related actions. */
15826 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15831 /* Get the stored value. */
15832 value = cp_lexer_consume_token (parser->lexer)->value;
15833 /* Perform any access checks that were deferred. */
15834 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15835 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15836 /* Set the scope from the stored value. */
15837 parser->scope = TREE_VALUE (value);
15838 parser->qualifying_scope = TREE_TYPE (value);
15839 parser->object_scope = NULL_TREE;
15842 /* Consume tokens up through a non-nested END token. */
15845 cp_parser_cache_group (cp_parser *parser,
15846 enum cpp_ttype end,
15853 /* Abort a parenthesized expression if we encounter a brace. */
15854 if ((end == CPP_CLOSE_PAREN || depth == 0)
15855 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15857 /* If we've reached the end of the file, stop. */
15858 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15860 /* Consume the next token. */
15861 token = cp_lexer_consume_token (parser->lexer);
15862 /* See if it starts a new group. */
15863 if (token->type == CPP_OPEN_BRACE)
15865 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15869 else if (token->type == CPP_OPEN_PAREN)
15870 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15871 else if (token->type == end)
15876 /* Begin parsing tentatively. We always save tokens while parsing
15877 tentatively so that if the tentative parsing fails we can restore the
15881 cp_parser_parse_tentatively (cp_parser* parser)
15883 /* Enter a new parsing context. */
15884 parser->context = cp_parser_context_new (parser->context);
15885 /* Begin saving tokens. */
15886 cp_lexer_save_tokens (parser->lexer);
15887 /* In order to avoid repetitive access control error messages,
15888 access checks are queued up until we are no longer parsing
15890 push_deferring_access_checks (dk_deferred);
15893 /* Commit to the currently active tentative parse. */
15896 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15898 cp_parser_context *context;
15901 /* Mark all of the levels as committed. */
15902 lexer = parser->lexer;
15903 for (context = parser->context; context->next; context = context->next)
15905 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15907 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15908 while (!cp_lexer_saving_tokens (lexer))
15909 lexer = lexer->next;
15910 cp_lexer_commit_tokens (lexer);
15914 /* Abort the currently active tentative parse. All consumed tokens
15915 will be rolled back, and no diagnostics will be issued. */
15918 cp_parser_abort_tentative_parse (cp_parser* parser)
15920 cp_parser_simulate_error (parser);
15921 /* Now, pretend that we want to see if the construct was
15922 successfully parsed. */
15923 cp_parser_parse_definitely (parser);
15926 /* Stop parsing tentatively. If a parse error has occurred, restore the
15927 token stream. Otherwise, commit to the tokens we have consumed.
15928 Returns true if no error occurred; false otherwise. */
15931 cp_parser_parse_definitely (cp_parser* parser)
15933 bool error_occurred;
15934 cp_parser_context *context;
15936 /* Remember whether or not an error occurred, since we are about to
15937 destroy that information. */
15938 error_occurred = cp_parser_error_occurred (parser);
15939 /* Remove the topmost context from the stack. */
15940 context = parser->context;
15941 parser->context = context->next;
15942 /* If no parse errors occurred, commit to the tentative parse. */
15943 if (!error_occurred)
15945 /* Commit to the tokens read tentatively, unless that was
15947 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15948 cp_lexer_commit_tokens (parser->lexer);
15950 pop_to_parent_deferring_access_checks ();
15952 /* Otherwise, if errors occurred, roll back our state so that things
15953 are just as they were before we began the tentative parse. */
15956 cp_lexer_rollback_tokens (parser->lexer);
15957 pop_deferring_access_checks ();
15959 /* Add the context to the front of the free list. */
15960 context->next = cp_parser_context_free_list;
15961 cp_parser_context_free_list = context;
15963 return !error_occurred;
15966 /* Returns true if we are parsing tentatively and are not committed to
15967 this tentative parse. */
15970 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
15972 return (cp_parser_parsing_tentatively (parser)
15973 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
15976 /* Returns nonzero iff an error has occurred during the most recent
15977 tentative parse. */
15980 cp_parser_error_occurred (cp_parser* parser)
15982 return (cp_parser_parsing_tentatively (parser)
15983 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15986 /* Returns nonzero if GNU extensions are allowed. */
15989 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15991 return parser->allow_gnu_extensions_p;
15997 static GTY (()) cp_parser *the_parser;
15999 /* External interface. */
16001 /* Parse one entire translation unit. */
16004 c_parse_file (void)
16006 bool error_occurred;
16007 static bool already_called = false;
16009 if (already_called)
16011 sorry ("inter-module optimizations not implemented for C++");
16014 already_called = true;
16016 the_parser = cp_parser_new ();
16017 push_deferring_access_checks (flag_access_control
16018 ? dk_no_deferred : dk_no_check);
16019 error_occurred = cp_parser_translation_unit (the_parser);
16023 /* This variable must be provided by every front end. */
16027 #include "gt-cp-parser.h"