2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
42 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43 and c-lex.c) and the C++ parser. */
47 typedef struct cp_token GTY (())
49 /* The kind of token. */
50 ENUM_BITFIELD (cpp_ttype) type : 8;
51 /* If this token is a keyword, this value indicates which keyword.
52 Otherwise, this value is RID_MAX. */
53 ENUM_BITFIELD (rid) keyword : 8;
56 /* True if this token is from a system header. */
57 BOOL_BITFIELD in_system_header : 1;
58 /* True if this token is from a context where it is implicitly extern "C" */
59 BOOL_BITFIELD implicit_extern_c : 1;
60 /* The value associated with this token, if any. */
62 /* The location at which this token was found. */
66 /* We use a stack of token pointer for saving token sets. */
67 typedef struct cp_token *cp_token_position;
68 DEF_VEC_MALLOC_P (cp_token_position);
70 static const cp_token eof_token =
72 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
73 #if USE_MAPPED_LOCATION
80 /* The cp_lexer structure represents the C++ lexer. It is responsible
81 for managing the token stream from the preprocessor and supplying
82 it to the parser. Tokens are never added to the cp_lexer after
85 typedef struct cp_lexer GTY (())
87 /* The memory allocated for the buffer. NULL if this lexer does not
88 own the token buffer. */
89 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
90 /* If the lexer owns the buffer, this is the number of tokens in the
94 /* A pointer just past the last available token. The tokens
95 in this lexer are [buffer, last_token). */
96 cp_token_position GTY ((skip)) last_token;
98 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
99 no more available tokens. */
100 cp_token_position GTY ((skip)) next_token;
102 /* A stack indicating positions at which cp_lexer_save_tokens was
103 called. The top entry is the most recent position at which we
104 began saving tokens. If the stack is non-empty, we are saving
106 VEC (cp_token_position) *GTY ((skip)) saved_tokens;
108 /* True if we should output debugging information. */
111 /* The next lexer in a linked list of lexers. */
112 struct cp_lexer *next;
115 /* cp_token_cache is a range of tokens. There is no need to represent
116 allocate heap memory for it, since tokens are never removed from the
117 lexer's array. There is also no need for the GC to walk through
118 a cp_token_cache, since everything in here is referenced through
121 typedef struct cp_token_cache GTY(())
123 /* The beginning of the token range. */
124 cp_token * GTY((skip)) first;
126 /* Points immediately after the last token in the range. */
127 cp_token * GTY ((skip)) last;
132 static cp_lexer *cp_lexer_new_main
134 static cp_lexer *cp_lexer_new_from_tokens
135 (cp_token_cache *tokens);
136 static void cp_lexer_destroy
138 static int cp_lexer_saving_tokens
140 static cp_token_position cp_lexer_token_position
142 static cp_token *cp_lexer_token_at
143 (cp_lexer *, cp_token_position);
144 static void cp_lexer_get_preprocessor_token
145 (cp_lexer *, cp_token *);
146 static inline cp_token *cp_lexer_peek_token
148 static cp_token *cp_lexer_peek_nth_token
149 (cp_lexer *, size_t);
150 static inline bool cp_lexer_next_token_is
151 (cp_lexer *, enum cpp_ttype);
152 static bool cp_lexer_next_token_is_not
153 (cp_lexer *, enum cpp_ttype);
154 static bool cp_lexer_next_token_is_keyword
155 (cp_lexer *, enum rid);
156 static cp_token *cp_lexer_consume_token
158 static void cp_lexer_purge_token
160 static void cp_lexer_purge_tokens_after
161 (cp_lexer *, cp_token_position);
162 static void cp_lexer_handle_pragma
164 static void cp_lexer_save_tokens
166 static void cp_lexer_commit_tokens
168 static void cp_lexer_rollback_tokens
170 #ifdef ENABLE_CHECKING
171 static void cp_lexer_print_token
172 (FILE *, cp_token *);
173 static inline bool cp_lexer_debugging_p
175 static void cp_lexer_start_debugging
176 (cp_lexer *) ATTRIBUTE_UNUSED;
177 static void cp_lexer_stop_debugging
178 (cp_lexer *) ATTRIBUTE_UNUSED;
180 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
181 about passing NULL to functions that require non-NULL arguments
182 (fputs, fprintf). It will never be used, so all we need is a value
183 of the right type that's guaranteed not to be NULL. */
184 #define cp_lexer_debug_stream stdout
185 #define cp_lexer_print_token(str, tok) (void) 0
186 #define cp_lexer_debugging_p(lexer) 0
187 #endif /* ENABLE_CHECKING */
189 static cp_token_cache *cp_token_cache_new
190 (cp_token *, cp_token *);
192 /* Manifest constants. */
193 #define CP_LEXER_BUFFER_SIZE 10000
194 #define CP_SAVED_TOKEN_STACK 5
196 /* A token type for keywords, as opposed to ordinary identifiers. */
197 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
199 /* A token type for template-ids. If a template-id is processed while
200 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
201 the value of the CPP_TEMPLATE_ID is whatever was returned by
202 cp_parser_template_id. */
203 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
205 /* A token type for nested-name-specifiers. If a
206 nested-name-specifier is processed while parsing tentatively, it is
207 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
208 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
209 cp_parser_nested_name_specifier_opt. */
210 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
212 /* A token type for tokens that are not tokens at all; these are used
213 to represent slots in the array where there used to be a token
214 that has now been deleted. */
215 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
217 /* The number of token types, including C++-specific ones. */
218 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
222 #ifdef ENABLE_CHECKING
223 /* The stream to which debugging output should be written. */
224 static FILE *cp_lexer_debug_stream;
225 #endif /* ENABLE_CHECKING */
227 /* Create a new main C++ lexer, the lexer that gets tokens from the
231 cp_lexer_new_main (void)
233 cp_token first_token;
240 /* Tell cpplib we want CPP_PRAGMA tokens. */
241 cpp_get_options (parse_in)->defer_pragmas = true;
243 /* Tell c_lex not to merge string constants. */
244 c_lex_return_raw_strings = true;
246 /* It's possible that lexing the first token will load a PCH file,
247 which is a GC collection point. So we have to grab the first
248 token before allocating any memory. */
249 cp_lexer_get_preprocessor_token (NULL, &first_token);
250 c_common_no_more_pch ();
252 /* Allocate the memory. */
253 lexer = GGC_CNEW (cp_lexer);
255 #ifdef ENABLE_CHECKING
256 /* Initially we are not debugging. */
257 lexer->debugging_p = false;
258 #endif /* ENABLE_CHECKING */
259 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
261 /* Create the buffer. */
262 alloc = CP_LEXER_BUFFER_SIZE;
263 buffer = ggc_alloc (alloc * sizeof (cp_token));
265 /* Put the first token in the buffer. */
270 /* Get the remaining tokens from the preprocessor. */
271 while (pos->type != CPP_EOF)
278 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
279 pos = buffer + space;
281 cp_lexer_get_preprocessor_token (lexer, pos);
283 lexer->buffer = buffer;
284 lexer->buffer_length = alloc - space;
285 lexer->last_token = pos;
286 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
288 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
289 direct calls to c_lex. Those callers all expect c_lex to do
290 string constant concatenation. */
291 c_lex_return_raw_strings = false;
293 gcc_assert (lexer->next_token->type != CPP_PURGED);
297 /* Create a new lexer whose token stream is primed with the tokens in
298 CACHE. When these tokens are exhausted, no new tokens will be read. */
301 cp_lexer_new_from_tokens (cp_token_cache *cache)
303 cp_token *first = cache->first;
304 cp_token *last = cache->last;
305 cp_lexer *lexer = GGC_CNEW (cp_lexer);
307 /* We do not own the buffer. */
308 lexer->buffer = NULL;
309 lexer->buffer_length = 0;
310 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
311 lexer->last_token = last;
313 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
315 #ifdef ENABLE_CHECKING
316 /* Initially we are not debugging. */
317 lexer->debugging_p = false;
320 gcc_assert (lexer->next_token->type != CPP_PURGED);
324 /* Frees all resources associated with LEXER. */
327 cp_lexer_destroy (cp_lexer *lexer)
330 ggc_free (lexer->buffer);
331 VEC_free (cp_token_position, lexer->saved_tokens);
335 /* Returns nonzero if debugging information should be output. */
337 #ifdef ENABLE_CHECKING
340 cp_lexer_debugging_p (cp_lexer *lexer)
342 return lexer->debugging_p;
345 #endif /* ENABLE_CHECKING */
347 static inline cp_token_position
348 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
350 gcc_assert (!previous_p || lexer->next_token != &eof_token);
352 return lexer->next_token - previous_p;
355 static inline cp_token *
356 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
361 /* nonzero if we are presently saving tokens. */
364 cp_lexer_saving_tokens (const cp_lexer* lexer)
366 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
369 /* Store the next token from the preprocessor in *TOKEN. Return true
373 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
376 static int is_extern_c = 0;
378 /* Get a new token from the preprocessor. */
379 token->type = c_lex_with_flags (&token->value, &token->flags);
380 token->location = input_location;
381 token->in_system_header = in_system_header;
383 /* On some systems, some header files are surrounded by an
384 implicit extern "C" block. Set a flag in the token if it
385 comes from such a header. */
386 is_extern_c += pending_lang_change;
387 pending_lang_change = 0;
388 token->implicit_extern_c = is_extern_c > 0;
390 /* Check to see if this token is a keyword. */
391 if (token->type == CPP_NAME
392 && C_IS_RESERVED_WORD (token->value))
394 /* Mark this token as a keyword. */
395 token->type = CPP_KEYWORD;
396 /* Record which keyword. */
397 token->keyword = C_RID_CODE (token->value);
398 /* Update the value. Some keywords are mapped to particular
399 entities, rather than simply having the value of the
400 corresponding IDENTIFIER_NODE. For example, `__const' is
401 mapped to `const'. */
402 token->value = ridpointers[token->keyword];
405 token->keyword = RID_MAX;
408 /* Update the globals input_location and in_system_header from TOKEN. */
410 cp_lexer_set_source_position_from_token (cp_token *token)
412 if (token->type != CPP_EOF)
414 input_location = token->location;
415 in_system_header = token->in_system_header;
419 /* Return a pointer to the next token in the token stream, but do not
422 static inline cp_token *
423 cp_lexer_peek_token (cp_lexer *lexer)
425 if (cp_lexer_debugging_p (lexer))
427 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
428 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
429 putc ('\n', cp_lexer_debug_stream);
431 return lexer->next_token;
434 /* Return true if the next token has the indicated TYPE. */
437 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
439 return cp_lexer_peek_token (lexer)->type == type;
442 /* Return true if the next token does not have the indicated TYPE. */
445 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
447 return !cp_lexer_next_token_is (lexer, type);
450 /* Return true if the next token is the indicated KEYWORD. */
453 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
457 /* Peek at the next token. */
458 token = cp_lexer_peek_token (lexer);
459 /* Check to see if it is the indicated keyword. */
460 return token->keyword == keyword;
463 /* Return a pointer to the Nth token in the token stream. If N is 1,
464 then this is precisely equivalent to cp_lexer_peek_token (except
465 that it is not inline). One would like to disallow that case, but
466 there is one case (cp_parser_nth_token_starts_template_id) where
467 the caller passes a variable for N and it might be 1. */
470 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
474 /* N is 1-based, not zero-based. */
475 gcc_assert (n > 0 && lexer->next_token != &eof_token);
477 if (cp_lexer_debugging_p (lexer))
478 fprintf (cp_lexer_debug_stream,
479 "cp_lexer: peeking ahead %ld at token: ", (long)n);
482 token = lexer->next_token;
486 if (token == lexer->last_token)
488 token = (cp_token *)&eof_token;
492 if (token->type != CPP_PURGED)
496 if (cp_lexer_debugging_p (lexer))
498 cp_lexer_print_token (cp_lexer_debug_stream, token);
499 putc ('\n', cp_lexer_debug_stream);
505 /* Return the next token, and advance the lexer's next_token pointer
506 to point to the next non-purged token. */
509 cp_lexer_consume_token (cp_lexer* lexer)
511 cp_token *token = lexer->next_token;
513 gcc_assert (token != &eof_token);
518 if (lexer->next_token == lexer->last_token)
520 lexer->next_token = (cp_token *)&eof_token;
525 while (lexer->next_token->type == CPP_PURGED);
527 cp_lexer_set_source_position_from_token (token);
529 /* Provide debugging output. */
530 if (cp_lexer_debugging_p (lexer))
532 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
533 cp_lexer_print_token (cp_lexer_debug_stream, token);
534 putc ('\n', cp_lexer_debug_stream);
540 /* Permanently remove the next token from the token stream, and
541 advance the next_token pointer to refer to the next non-purged
545 cp_lexer_purge_token (cp_lexer *lexer)
547 cp_token *tok = lexer->next_token;
549 gcc_assert (tok != &eof_token);
550 tok->type = CPP_PURGED;
551 tok->location = UNKNOWN_LOCATION;
552 tok->value = NULL_TREE;
553 tok->keyword = RID_MAX;
558 if (tok == lexer->last_token)
560 tok = (cp_token *)&eof_token;
564 while (tok->type == CPP_PURGED);
565 lexer->next_token = tok;
568 /* Permanently remove all tokens after TOK, up to, but not
569 including, the token that will be returned next by
570 cp_lexer_peek_token. */
573 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
575 cp_token *peek = lexer->next_token;
577 if (peek == &eof_token)
578 peek = lexer->last_token;
580 gcc_assert (tok < peek);
582 for ( tok += 1; tok != peek; tok += 1)
584 tok->type = CPP_PURGED;
585 tok->location = UNKNOWN_LOCATION;
586 tok->value = NULL_TREE;
587 tok->keyword = RID_MAX;
591 /* Consume and handle a pragma token. */
593 cp_lexer_handle_pragma (cp_lexer *lexer)
596 cp_token *token = cp_lexer_consume_token (lexer);
597 gcc_assert (token->type == CPP_PRAGMA);
598 gcc_assert (token->value);
600 s.len = TREE_STRING_LENGTH (token->value);
601 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
603 cpp_handle_deferred_pragma (parse_in, &s);
605 /* Clearing token->value here means that we will get an ICE if we
606 try to process this #pragma again (which should be impossible). */
610 /* Begin saving tokens. All tokens consumed after this point will be
614 cp_lexer_save_tokens (cp_lexer* lexer)
616 /* Provide debugging output. */
617 if (cp_lexer_debugging_p (lexer))
618 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
620 VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
623 /* Commit to the portion of the token stream most recently saved. */
626 cp_lexer_commit_tokens (cp_lexer* lexer)
628 /* Provide debugging output. */
629 if (cp_lexer_debugging_p (lexer))
630 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
632 VEC_pop (cp_token_position, lexer->saved_tokens);
635 /* Return all tokens saved since the last call to cp_lexer_save_tokens
636 to the token stream. Stop saving tokens. */
639 cp_lexer_rollback_tokens (cp_lexer* lexer)
641 /* Provide debugging output. */
642 if (cp_lexer_debugging_p (lexer))
643 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
645 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
648 /* Print a representation of the TOKEN on the STREAM. */
650 #ifdef ENABLE_CHECKING
653 cp_lexer_print_token (FILE * stream, cp_token *token)
655 /* We don't use cpp_type2name here because the parser defines
656 a few tokens of its own. */
657 static const char *const token_names[] = {
658 /* cpplib-defined token types */
664 /* C++ parser token types - see "Manifest constants", above. */
667 "NESTED_NAME_SPECIFIER",
671 /* If we have a name for the token, print it out. Otherwise, we
672 simply give the numeric code. */
673 gcc_assert (token->type < ARRAY_SIZE(token_names));
674 fputs (token_names[token->type], stream);
676 /* For some tokens, print the associated data. */
680 /* Some keywords have a value that is not an IDENTIFIER_NODE.
681 For example, `struct' is mapped to an INTEGER_CST. */
682 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
684 /* else fall through */
686 fputs (IDENTIFIER_POINTER (token->value), stream);
692 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
700 /* Start emitting debugging information. */
703 cp_lexer_start_debugging (cp_lexer* lexer)
705 ++lexer->debugging_p;
708 /* Stop emitting debugging information. */
711 cp_lexer_stop_debugging (cp_lexer* lexer)
713 --lexer->debugging_p;
716 #endif /* ENABLE_CHECKING */
718 /* Create a new cp_token_cache, representing a range of tokens. */
720 static cp_token_cache *
721 cp_token_cache_new (cp_token *first, cp_token *last)
723 cp_token_cache *cache = GGC_NEW (cp_token_cache);
724 cache->first = first;
730 /* Decl-specifiers. */
732 static void clear_decl_specs
733 (cp_decl_specifier_seq *);
735 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
738 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
740 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
745 /* Nothing other than the parser should be creating declarators;
746 declarators are a semi-syntactic representation of C++ entities.
747 Other parts of the front end that need to create entities (like
748 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
750 static cp_declarator *make_id_declarator
752 static cp_declarator *make_call_declarator
753 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
754 static cp_declarator *make_array_declarator
755 (cp_declarator *, tree);
756 static cp_declarator *make_pointer_declarator
757 (cp_cv_quals, cp_declarator *);
758 static cp_declarator *make_reference_declarator
759 (cp_cv_quals, cp_declarator *);
760 static cp_parameter_declarator *make_parameter_declarator
761 (cp_decl_specifier_seq *, cp_declarator *, tree);
762 static cp_declarator *make_ptrmem_declarator
763 (cp_cv_quals, tree, cp_declarator *);
765 cp_declarator *cp_error_declarator;
767 /* The obstack on which declarators and related data structures are
769 static struct obstack declarator_obstack;
771 /* Alloc BYTES from the declarator memory pool. */
774 alloc_declarator (size_t bytes)
776 return obstack_alloc (&declarator_obstack, bytes);
779 /* Allocate a declarator of the indicated KIND. Clear fields that are
780 common to all declarators. */
782 static cp_declarator *
783 make_declarator (cp_declarator_kind kind)
785 cp_declarator *declarator;
787 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
788 declarator->kind = kind;
789 declarator->attributes = NULL_TREE;
790 declarator->declarator = NULL;
795 /* Make a declarator for a generalized identifier. */
798 make_id_declarator (tree id)
800 cp_declarator *declarator;
802 declarator = make_declarator (cdk_id);
803 declarator->u.id.name = id;
804 declarator->u.id.sfk = sfk_none;
809 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
810 of modifiers such as const or volatile to apply to the pointer
811 type, represented as identifiers. */
814 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
816 cp_declarator *declarator;
818 declarator = make_declarator (cdk_pointer);
819 declarator->declarator = target;
820 declarator->u.pointer.qualifiers = cv_qualifiers;
821 declarator->u.pointer.class_type = NULL_TREE;
826 /* Like make_pointer_declarator -- but for references. */
829 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
831 cp_declarator *declarator;
833 declarator = make_declarator (cdk_reference);
834 declarator->declarator = target;
835 declarator->u.pointer.qualifiers = cv_qualifiers;
836 declarator->u.pointer.class_type = NULL_TREE;
841 /* Like make_pointer_declarator -- but for a pointer to a non-static
842 member of CLASS_TYPE. */
845 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
846 cp_declarator *pointee)
848 cp_declarator *declarator;
850 declarator = make_declarator (cdk_ptrmem);
851 declarator->declarator = pointee;
852 declarator->u.pointer.qualifiers = cv_qualifiers;
853 declarator->u.pointer.class_type = class_type;
858 /* Make a declarator for the function given by TARGET, with the
859 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
860 "const"-qualified member function. The EXCEPTION_SPECIFICATION
861 indicates what exceptions can be thrown. */
864 make_call_declarator (cp_declarator *target,
865 cp_parameter_declarator *parms,
866 cp_cv_quals cv_qualifiers,
867 tree exception_specification)
869 cp_declarator *declarator;
871 declarator = make_declarator (cdk_function);
872 declarator->declarator = target;
873 declarator->u.function.parameters = parms;
874 declarator->u.function.qualifiers = cv_qualifiers;
875 declarator->u.function.exception_specification = exception_specification;
880 /* Make a declarator for an array of BOUNDS elements, each of which is
881 defined by ELEMENT. */
884 make_array_declarator (cp_declarator *element, tree bounds)
886 cp_declarator *declarator;
888 declarator = make_declarator (cdk_array);
889 declarator->declarator = element;
890 declarator->u.array.bounds = bounds;
895 cp_parameter_declarator *no_parameters;
897 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
898 DECLARATOR and DEFAULT_ARGUMENT. */
900 cp_parameter_declarator *
901 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
902 cp_declarator *declarator,
903 tree default_argument)
905 cp_parameter_declarator *parameter;
907 parameter = ((cp_parameter_declarator *)
908 alloc_declarator (sizeof (cp_parameter_declarator)));
909 parameter->next = NULL;
911 parameter->decl_specifiers = *decl_specifiers;
913 clear_decl_specs (¶meter->decl_specifiers);
914 parameter->declarator = declarator;
915 parameter->default_argument = default_argument;
916 parameter->ellipsis_p = false;
926 A cp_parser parses the token stream as specified by the C++
927 grammar. Its job is purely parsing, not semantic analysis. For
928 example, the parser breaks the token stream into declarators,
929 expressions, statements, and other similar syntactic constructs.
930 It does not check that the types of the expressions on either side
931 of an assignment-statement are compatible, or that a function is
932 not declared with a parameter of type `void'.
934 The parser invokes routines elsewhere in the compiler to perform
935 semantic analysis and to build up the abstract syntax tree for the
938 The parser (and the template instantiation code, which is, in a
939 way, a close relative of parsing) are the only parts of the
940 compiler that should be calling push_scope and pop_scope, or
941 related functions. The parser (and template instantiation code)
942 keeps track of what scope is presently active; everything else
943 should simply honor that. (The code that generates static
944 initializers may also need to set the scope, in order to check
945 access control correctly when emitting the initializers.)
950 The parser is of the standard recursive-descent variety. Upcoming
951 tokens in the token stream are examined in order to determine which
952 production to use when parsing a non-terminal. Some C++ constructs
953 require arbitrary look ahead to disambiguate. For example, it is
954 impossible, in the general case, to tell whether a statement is an
955 expression or declaration without scanning the entire statement.
956 Therefore, the parser is capable of "parsing tentatively." When the
957 parser is not sure what construct comes next, it enters this mode.
958 Then, while we attempt to parse the construct, the parser queues up
959 error messages, rather than issuing them immediately, and saves the
960 tokens it consumes. If the construct is parsed successfully, the
961 parser "commits", i.e., it issues any queued error messages and
962 the tokens that were being preserved are permanently discarded.
963 If, however, the construct is not parsed successfully, the parser
964 rolls back its state completely so that it can resume parsing using
965 a different alternative.
970 The performance of the parser could probably be improved substantially.
971 We could often eliminate the need to parse tentatively by looking ahead
972 a little bit. In some places, this approach might not entirely eliminate
973 the need to parse tentatively, but it might still speed up the average
976 /* Flags that are passed to some parsing functions. These values can
977 be bitwise-ored together. */
979 typedef enum cp_parser_flags
982 CP_PARSER_FLAGS_NONE = 0x0,
983 /* The construct is optional. If it is not present, then no error
985 CP_PARSER_FLAGS_OPTIONAL = 0x1,
986 /* When parsing a type-specifier, do not allow user-defined types. */
987 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
990 /* The different kinds of declarators we want to parse. */
992 typedef enum cp_parser_declarator_kind
994 /* We want an abstract declarator. */
995 CP_PARSER_DECLARATOR_ABSTRACT,
996 /* We want a named declarator. */
997 CP_PARSER_DECLARATOR_NAMED,
998 /* We don't mind, but the name must be an unqualified-id. */
999 CP_PARSER_DECLARATOR_EITHER
1000 } cp_parser_declarator_kind;
1002 /* The precedence values used to parse binary expressions. The minimum value
1003 of PREC must be 1, because zero is reserved to quickly discriminate
1004 binary operators from other tokens. */
1009 PREC_LOGICAL_OR_EXPRESSION,
1010 PREC_LOGICAL_AND_EXPRESSION,
1011 PREC_INCLUSIVE_OR_EXPRESSION,
1012 PREC_EXCLUSIVE_OR_EXPRESSION,
1013 PREC_AND_EXPRESSION,
1014 PREC_EQUALITY_EXPRESSION,
1015 PREC_RELATIONAL_EXPRESSION,
1016 PREC_SHIFT_EXPRESSION,
1017 PREC_ADDITIVE_EXPRESSION,
1018 PREC_MULTIPLICATIVE_EXPRESSION,
1020 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1023 /* A mapping from a token type to a corresponding tree node type, with a
1024 precedence value. */
1026 typedef struct cp_parser_binary_operations_map_node
1028 /* The token type. */
1029 enum cpp_ttype token_type;
1030 /* The corresponding tree code. */
1031 enum tree_code tree_type;
1032 /* The precedence of this operator. */
1033 enum cp_parser_prec prec;
1034 } cp_parser_binary_operations_map_node;
1036 /* The status of a tentative parse. */
1038 typedef enum cp_parser_status_kind
1040 /* No errors have occurred. */
1041 CP_PARSER_STATUS_KIND_NO_ERROR,
1042 /* An error has occurred. */
1043 CP_PARSER_STATUS_KIND_ERROR,
1044 /* We are committed to this tentative parse, whether or not an error
1046 CP_PARSER_STATUS_KIND_COMMITTED
1047 } cp_parser_status_kind;
1049 typedef struct cp_parser_expression_stack_entry
1052 enum tree_code tree_type;
1054 } cp_parser_expression_stack_entry;
1056 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1057 entries because precedence levels on the stack are monotonically
1059 typedef struct cp_parser_expression_stack_entry
1060 cp_parser_expression_stack[NUM_PREC_VALUES];
1062 /* Context that is saved and restored when parsing tentatively. */
1063 typedef struct cp_parser_context GTY (())
1065 /* If this is a tentative parsing context, the status of the
1067 enum cp_parser_status_kind status;
1068 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1069 that are looked up in this context must be looked up both in the
1070 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1071 the context of the containing expression. */
1074 /* The next parsing context in the stack. */
1075 struct cp_parser_context *next;
1076 } cp_parser_context;
1080 /* Constructors and destructors. */
1082 static cp_parser_context *cp_parser_context_new
1083 (cp_parser_context *);
1085 /* Class variables. */
1087 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1089 /* The operator-precedence table used by cp_parser_binary_expression.
1090 Transformed into an associative array (binops_by_token) by
1093 static const cp_parser_binary_operations_map_node binops[] = {
1094 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1095 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1097 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1098 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1099 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1101 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1102 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1104 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1105 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1107 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1108 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1109 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1110 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1111 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1112 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1114 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1115 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1117 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1119 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1121 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1123 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1125 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1128 /* The same as binops, but initialized by cp_parser_new so that
1129 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1131 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1133 /* Constructors and destructors. */
1135 /* Construct a new context. The context below this one on the stack
1136 is given by NEXT. */
1138 static cp_parser_context *
1139 cp_parser_context_new (cp_parser_context* next)
1141 cp_parser_context *context;
1143 /* Allocate the storage. */
1144 if (cp_parser_context_free_list != NULL)
1146 /* Pull the first entry from the free list. */
1147 context = cp_parser_context_free_list;
1148 cp_parser_context_free_list = context->next;
1149 memset (context, 0, sizeof (*context));
1152 context = GGC_CNEW (cp_parser_context);
1154 /* No errors have occurred yet in this context. */
1155 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1156 /* If this is not the bottomost context, copy information that we
1157 need from the previous context. */
1160 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1161 expression, then we are parsing one in this context, too. */
1162 context->object_type = next->object_type;
1163 /* Thread the stack. */
1164 context->next = next;
1170 /* The cp_parser structure represents the C++ parser. */
1172 typedef struct cp_parser GTY(())
1174 /* The lexer from which we are obtaining tokens. */
1177 /* The scope in which names should be looked up. If NULL_TREE, then
1178 we look up names in the scope that is currently open in the
1179 source program. If non-NULL, this is either a TYPE or
1180 NAMESPACE_DECL for the scope in which we should look.
1182 This value is not cleared automatically after a name is looked
1183 up, so we must be careful to clear it before starting a new look
1184 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1185 will look up `Z' in the scope of `X', rather than the current
1186 scope.) Unfortunately, it is difficult to tell when name lookup
1187 is complete, because we sometimes peek at a token, look it up,
1188 and then decide not to consume it. */
1191 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1192 last lookup took place. OBJECT_SCOPE is used if an expression
1193 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1194 respectively. QUALIFYING_SCOPE is used for an expression of the
1195 form "X::Y"; it refers to X. */
1197 tree qualifying_scope;
1199 /* A stack of parsing contexts. All but the bottom entry on the
1200 stack will be tentative contexts.
1202 We parse tentatively in order to determine which construct is in
1203 use in some situations. For example, in order to determine
1204 whether a statement is an expression-statement or a
1205 declaration-statement we parse it tentatively as a
1206 declaration-statement. If that fails, we then reparse the same
1207 token stream as an expression-statement. */
1208 cp_parser_context *context;
1210 /* True if we are parsing GNU C++. If this flag is not set, then
1211 GNU extensions are not recognized. */
1212 bool allow_gnu_extensions_p;
1214 /* TRUE if the `>' token should be interpreted as the greater-than
1215 operator. FALSE if it is the end of a template-id or
1216 template-parameter-list. */
1217 bool greater_than_is_operator_p;
1219 /* TRUE if default arguments are allowed within a parameter list
1220 that starts at this point. FALSE if only a gnu extension makes
1221 them permissible. */
1222 bool default_arg_ok_p;
1224 /* TRUE if we are parsing an integral constant-expression. See
1225 [expr.const] for a precise definition. */
1226 bool integral_constant_expression_p;
1228 /* TRUE if we are parsing an integral constant-expression -- but a
1229 non-constant expression should be permitted as well. This flag
1230 is used when parsing an array bound so that GNU variable-length
1231 arrays are tolerated. */
1232 bool allow_non_integral_constant_expression_p;
1234 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1235 been seen that makes the expression non-constant. */
1236 bool non_integral_constant_expression_p;
1238 /* TRUE if local variable names and `this' are forbidden in the
1240 bool local_variables_forbidden_p;
1242 /* TRUE if the declaration we are parsing is part of a
1243 linkage-specification of the form `extern string-literal
1245 bool in_unbraced_linkage_specification_p;
1247 /* TRUE if we are presently parsing a declarator, after the
1248 direct-declarator. */
1249 bool in_declarator_p;
1251 /* TRUE if we are presently parsing a template-argument-list. */
1252 bool in_template_argument_list_p;
1254 /* TRUE if we are presently parsing the body of an
1255 iteration-statement. */
1256 bool in_iteration_statement_p;
1258 /* TRUE if we are presently parsing the body of a switch
1260 bool in_switch_statement_p;
1262 /* TRUE if we are parsing a type-id in an expression context. In
1263 such a situation, both "type (expr)" and "type (type)" are valid
1265 bool in_type_id_in_expr_p;
1267 /* TRUE if we are currently in a header file where declarations are
1268 implicitly extern "C". */
1269 bool implicit_extern_c;
1271 /* TRUE if strings in expressions should be translated to the execution
1273 bool translate_strings_p;
1275 /* If non-NULL, then we are parsing a construct where new type
1276 definitions are not permitted. The string stored here will be
1277 issued as an error message if a type is defined. */
1278 const char *type_definition_forbidden_message;
1280 /* A list of lists. The outer list is a stack, used for member
1281 functions of local classes. At each level there are two sub-list,
1282 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1283 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1284 TREE_VALUE's. The functions are chained in reverse declaration
1287 The TREE_PURPOSE sublist contains those functions with default
1288 arguments that need post processing, and the TREE_VALUE sublist
1289 contains those functions with definitions that need post
1292 These lists can only be processed once the outermost class being
1293 defined is complete. */
1294 tree unparsed_functions_queues;
1296 /* The number of classes whose definitions are currently in
1298 unsigned num_classes_being_defined;
1300 /* The number of template parameter lists that apply directly to the
1301 current declaration. */
1302 unsigned num_template_parameter_lists;
1305 /* The type of a function that parses some kind of expression. */
1306 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1310 /* Constructors and destructors. */
1312 static cp_parser *cp_parser_new
1315 /* Routines to parse various constructs.
1317 Those that return `tree' will return the error_mark_node (rather
1318 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1319 Sometimes, they will return an ordinary node if error-recovery was
1320 attempted, even though a parse error occurred. So, to check
1321 whether or not a parse error occurred, you should always use
1322 cp_parser_error_occurred. If the construct is optional (indicated
1323 either by an `_opt' in the name of the function that does the
1324 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1325 the construct is not present. */
1327 /* Lexical conventions [gram.lex] */
1329 static tree cp_parser_identifier
1331 static tree cp_parser_string_literal
1332 (cp_parser *, bool, bool);
1334 /* Basic concepts [gram.basic] */
1336 static bool cp_parser_translation_unit
1339 /* Expressions [gram.expr] */
1341 static tree cp_parser_primary_expression
1342 (cp_parser *, cp_id_kind *, tree *);
1343 static tree cp_parser_id_expression
1344 (cp_parser *, bool, bool, bool *, bool);
1345 static tree cp_parser_unqualified_id
1346 (cp_parser *, bool, bool, bool);
1347 static tree cp_parser_nested_name_specifier_opt
1348 (cp_parser *, bool, bool, bool, bool);
1349 static tree cp_parser_nested_name_specifier
1350 (cp_parser *, bool, bool, bool, bool);
1351 static tree cp_parser_class_or_namespace_name
1352 (cp_parser *, bool, bool, bool, bool, bool);
1353 static tree cp_parser_postfix_expression
1354 (cp_parser *, bool);
1355 static tree cp_parser_postfix_open_square_expression
1356 (cp_parser *, tree, bool);
1357 static tree cp_parser_postfix_dot_deref_expression
1358 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1359 static tree cp_parser_parenthesized_expression_list
1360 (cp_parser *, bool, bool *);
1361 static void cp_parser_pseudo_destructor_name
1362 (cp_parser *, tree *, tree *);
1363 static tree cp_parser_unary_expression
1364 (cp_parser *, bool);
1365 static enum tree_code cp_parser_unary_operator
1367 static tree cp_parser_new_expression
1369 static tree cp_parser_new_placement
1371 static tree cp_parser_new_type_id
1372 (cp_parser *, tree *);
1373 static cp_declarator *cp_parser_new_declarator_opt
1375 static cp_declarator *cp_parser_direct_new_declarator
1377 static tree cp_parser_new_initializer
1379 static tree cp_parser_delete_expression
1381 static tree cp_parser_cast_expression
1382 (cp_parser *, bool);
1383 static tree cp_parser_binary_expression
1385 static tree cp_parser_question_colon_clause
1386 (cp_parser *, tree);
1387 static tree cp_parser_assignment_expression
1389 static enum tree_code cp_parser_assignment_operator_opt
1391 static tree cp_parser_expression
1393 static tree cp_parser_constant_expression
1394 (cp_parser *, bool, bool *);
1395 static tree cp_parser_builtin_offsetof
1398 /* Statements [gram.stmt.stmt] */
1400 static void cp_parser_statement
1401 (cp_parser *, tree);
1402 static tree cp_parser_labeled_statement
1403 (cp_parser *, tree);
1404 static tree cp_parser_expression_statement
1405 (cp_parser *, tree);
1406 static tree cp_parser_compound_statement
1407 (cp_parser *, tree, bool);
1408 static void cp_parser_statement_seq_opt
1409 (cp_parser *, tree);
1410 static tree cp_parser_selection_statement
1412 static tree cp_parser_condition
1414 static tree cp_parser_iteration_statement
1416 static void cp_parser_for_init_statement
1418 static tree cp_parser_jump_statement
1420 static void cp_parser_declaration_statement
1423 static tree cp_parser_implicitly_scoped_statement
1425 static void cp_parser_already_scoped_statement
1428 /* Declarations [gram.dcl.dcl] */
1430 static void cp_parser_declaration_seq_opt
1432 static void cp_parser_declaration
1434 static void cp_parser_block_declaration
1435 (cp_parser *, bool);
1436 static void cp_parser_simple_declaration
1437 (cp_parser *, bool);
1438 static void cp_parser_decl_specifier_seq
1439 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1440 static tree cp_parser_storage_class_specifier_opt
1442 static tree cp_parser_function_specifier_opt
1443 (cp_parser *, cp_decl_specifier_seq *);
1444 static tree cp_parser_type_specifier
1445 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1447 static tree cp_parser_simple_type_specifier
1448 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1449 static tree cp_parser_type_name
1451 static tree cp_parser_elaborated_type_specifier
1452 (cp_parser *, bool, bool);
1453 static tree cp_parser_enum_specifier
1455 static void cp_parser_enumerator_list
1456 (cp_parser *, tree);
1457 static void cp_parser_enumerator_definition
1458 (cp_parser *, tree);
1459 static tree cp_parser_namespace_name
1461 static void cp_parser_namespace_definition
1463 static void cp_parser_namespace_body
1465 static tree cp_parser_qualified_namespace_specifier
1467 static void cp_parser_namespace_alias_definition
1469 static void cp_parser_using_declaration
1471 static void cp_parser_using_directive
1473 static void cp_parser_asm_definition
1475 static void cp_parser_linkage_specification
1478 /* Declarators [gram.dcl.decl] */
1480 static tree cp_parser_init_declarator
1481 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1482 static cp_declarator *cp_parser_declarator
1483 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1484 static cp_declarator *cp_parser_direct_declarator
1485 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1486 static enum tree_code cp_parser_ptr_operator
1487 (cp_parser *, tree *, cp_cv_quals *);
1488 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1490 static tree cp_parser_declarator_id
1492 static tree cp_parser_type_id
1494 static void cp_parser_type_specifier_seq
1495 (cp_parser *, cp_decl_specifier_seq *);
1496 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1498 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1499 (cp_parser *, bool *);
1500 static cp_parameter_declarator *cp_parser_parameter_declaration
1501 (cp_parser *, bool, bool *);
1502 static void cp_parser_function_body
1504 static tree cp_parser_initializer
1505 (cp_parser *, bool *, bool *);
1506 static tree cp_parser_initializer_clause
1507 (cp_parser *, bool *);
1508 static tree cp_parser_initializer_list
1509 (cp_parser *, bool *);
1511 static bool cp_parser_ctor_initializer_opt_and_function_body
1514 /* Classes [gram.class] */
1516 static tree cp_parser_class_name
1517 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1518 static tree cp_parser_class_specifier
1520 static tree cp_parser_class_head
1521 (cp_parser *, bool *, tree *);
1522 static enum tag_types cp_parser_class_key
1524 static void cp_parser_member_specification_opt
1526 static void cp_parser_member_declaration
1528 static tree cp_parser_pure_specifier
1530 static tree cp_parser_constant_initializer
1533 /* Derived classes [gram.class.derived] */
1535 static tree cp_parser_base_clause
1537 static tree cp_parser_base_specifier
1540 /* Special member functions [gram.special] */
1542 static tree cp_parser_conversion_function_id
1544 static tree cp_parser_conversion_type_id
1546 static cp_declarator *cp_parser_conversion_declarator_opt
1548 static bool cp_parser_ctor_initializer_opt
1550 static void cp_parser_mem_initializer_list
1552 static tree cp_parser_mem_initializer
1554 static tree cp_parser_mem_initializer_id
1557 /* Overloading [gram.over] */
1559 static tree cp_parser_operator_function_id
1561 static tree cp_parser_operator
1564 /* Templates [gram.temp] */
1566 static void cp_parser_template_declaration
1567 (cp_parser *, bool);
1568 static tree cp_parser_template_parameter_list
1570 static tree cp_parser_template_parameter
1571 (cp_parser *, bool *);
1572 static tree cp_parser_type_parameter
1574 static tree cp_parser_template_id
1575 (cp_parser *, bool, bool, bool);
1576 static tree cp_parser_template_name
1577 (cp_parser *, bool, bool, bool, bool *);
1578 static tree cp_parser_template_argument_list
1580 static tree cp_parser_template_argument
1582 static void cp_parser_explicit_instantiation
1584 static void cp_parser_explicit_specialization
1587 /* Exception handling [gram.exception] */
1589 static tree cp_parser_try_block
1591 static bool cp_parser_function_try_block
1593 static void cp_parser_handler_seq
1595 static void cp_parser_handler
1597 static tree cp_parser_exception_declaration
1599 static tree cp_parser_throw_expression
1601 static tree cp_parser_exception_specification_opt
1603 static tree cp_parser_type_id_list
1606 /* GNU Extensions */
1608 static tree cp_parser_asm_specification_opt
1610 static tree cp_parser_asm_operand_list
1612 static tree cp_parser_asm_clobber_list
1614 static tree cp_parser_attributes_opt
1616 static tree cp_parser_attribute_list
1618 static bool cp_parser_extension_opt
1619 (cp_parser *, int *);
1620 static void cp_parser_label_declaration
1623 /* Utility Routines */
1625 static tree cp_parser_lookup_name
1626 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1627 static tree cp_parser_lookup_name_simple
1628 (cp_parser *, tree);
1629 static tree cp_parser_maybe_treat_template_as_class
1631 static bool cp_parser_check_declarator_template_parameters
1632 (cp_parser *, cp_declarator *);
1633 static bool cp_parser_check_template_parameters
1634 (cp_parser *, unsigned);
1635 static tree cp_parser_simple_cast_expression
1637 static tree cp_parser_global_scope_opt
1638 (cp_parser *, bool);
1639 static bool cp_parser_constructor_declarator_p
1640 (cp_parser *, bool);
1641 static tree cp_parser_function_definition_from_specifiers_and_declarator
1642 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1643 static tree cp_parser_function_definition_after_declarator
1644 (cp_parser *, bool);
1645 static void cp_parser_template_declaration_after_export
1646 (cp_parser *, bool);
1647 static tree cp_parser_single_declaration
1648 (cp_parser *, bool, bool *);
1649 static tree cp_parser_functional_cast
1650 (cp_parser *, tree);
1651 static tree cp_parser_save_member_function_body
1652 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1653 static tree cp_parser_enclosed_template_argument_list
1655 static void cp_parser_save_default_args
1656 (cp_parser *, tree);
1657 static void cp_parser_late_parsing_for_member
1658 (cp_parser *, tree);
1659 static void cp_parser_late_parsing_default_args
1660 (cp_parser *, tree);
1661 static tree cp_parser_sizeof_operand
1662 (cp_parser *, enum rid);
1663 static bool cp_parser_declares_only_class_p
1665 static void cp_parser_set_storage_class
1666 (cp_decl_specifier_seq *, cp_storage_class);
1667 static void cp_parser_set_decl_spec_type
1668 (cp_decl_specifier_seq *, tree, bool);
1669 static bool cp_parser_friend_p
1670 (const cp_decl_specifier_seq *);
1671 static cp_token *cp_parser_require
1672 (cp_parser *, enum cpp_ttype, const char *);
1673 static cp_token *cp_parser_require_keyword
1674 (cp_parser *, enum rid, const char *);
1675 static bool cp_parser_token_starts_function_definition_p
1677 static bool cp_parser_next_token_starts_class_definition_p
1679 static bool cp_parser_next_token_ends_template_argument_p
1681 static bool cp_parser_nth_token_starts_template_argument_list_p
1682 (cp_parser *, size_t);
1683 static enum tag_types cp_parser_token_is_class_key
1685 static void cp_parser_check_class_key
1686 (enum tag_types, tree type);
1687 static void cp_parser_check_access_in_redeclaration
1689 static bool cp_parser_optional_template_keyword
1691 static void cp_parser_pre_parsed_nested_name_specifier
1693 static void cp_parser_cache_group
1694 (cp_parser *, enum cpp_ttype, unsigned);
1695 static void cp_parser_parse_tentatively
1697 static void cp_parser_commit_to_tentative_parse
1699 static void cp_parser_abort_tentative_parse
1701 static bool cp_parser_parse_definitely
1703 static inline bool cp_parser_parsing_tentatively
1705 static bool cp_parser_uncommitted_to_tentative_parse_p
1707 static void cp_parser_error
1708 (cp_parser *, const char *);
1709 static void cp_parser_name_lookup_error
1710 (cp_parser *, tree, tree, const char *);
1711 static bool cp_parser_simulate_error
1713 static void cp_parser_check_type_definition
1715 static void cp_parser_check_for_definition_in_return_type
1716 (cp_declarator *, tree);
1717 static void cp_parser_check_for_invalid_template_id
1718 (cp_parser *, tree);
1719 static bool cp_parser_non_integral_constant_expression
1720 (cp_parser *, const char *);
1721 static void cp_parser_diagnose_invalid_type_name
1722 (cp_parser *, tree, tree);
1723 static bool cp_parser_parse_and_diagnose_invalid_type_name
1725 static int cp_parser_skip_to_closing_parenthesis
1726 (cp_parser *, bool, bool, bool);
1727 static void cp_parser_skip_to_end_of_statement
1729 static void cp_parser_consume_semicolon_at_end_of_statement
1731 static void cp_parser_skip_to_end_of_block_or_statement
1733 static void cp_parser_skip_to_closing_brace
1735 static void cp_parser_skip_until_found
1736 (cp_parser *, enum cpp_ttype, const char *);
1737 static bool cp_parser_error_occurred
1739 static bool cp_parser_allow_gnu_extensions_p
1741 static bool cp_parser_is_string_literal
1743 static bool cp_parser_is_keyword
1744 (cp_token *, enum rid);
1745 static tree cp_parser_make_typename_type
1746 (cp_parser *, tree, tree);
1748 /* Returns nonzero if we are parsing tentatively. */
1751 cp_parser_parsing_tentatively (cp_parser* parser)
1753 return parser->context->next != NULL;
1756 /* Returns nonzero if TOKEN is a string literal. */
1759 cp_parser_is_string_literal (cp_token* token)
1761 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1764 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1767 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1769 return token->keyword == keyword;
1772 /* If not parsing tentatively, issue a diagnostic of the form
1773 FILE:LINE: MESSAGE before TOKEN
1774 where TOKEN is the next token in the input stream. MESSAGE
1775 (specified by the caller) is usually of the form "expected
1779 cp_parser_error (cp_parser* parser, const char* message)
1781 if (!cp_parser_simulate_error (parser))
1783 cp_token *token = cp_lexer_peek_token (parser->lexer);
1784 /* This diagnostic makes more sense if it is tagged to the line
1785 of the token we just peeked at. */
1786 cp_lexer_set_source_position_from_token (token);
1787 c_parse_error (message,
1788 /* Because c_parser_error does not understand
1789 CPP_KEYWORD, keywords are treated like
1791 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1796 /* Issue an error about name-lookup failing. NAME is the
1797 IDENTIFIER_NODE DECL is the result of
1798 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1799 the thing that we hoped to find. */
1802 cp_parser_name_lookup_error (cp_parser* parser,
1805 const char* desired)
1807 /* If name lookup completely failed, tell the user that NAME was not
1809 if (decl == error_mark_node)
1811 if (parser->scope && parser->scope != global_namespace)
1812 error ("%<%D::%D%> has not been declared",
1813 parser->scope, name);
1814 else if (parser->scope == global_namespace)
1815 error ("%<::%D%> has not been declared", name);
1816 else if (parser->object_scope
1817 && !CLASS_TYPE_P (parser->object_scope))
1818 error ("request for member %qD in non-class type %qT",
1819 name, parser->object_scope);
1820 else if (parser->object_scope)
1821 error ("%<%T::%D%> has not been declared",
1822 parser->object_scope, name);
1824 error ("%qD has not been declared", name);
1826 else if (parser->scope && parser->scope != global_namespace)
1827 error ("%<%D::%D%> %s", parser->scope, name, desired);
1828 else if (parser->scope == global_namespace)
1829 error ("%<::%D%> %s", name, desired);
1831 error ("%qD %s", name, desired);
1834 /* If we are parsing tentatively, remember that an error has occurred
1835 during this tentative parse. Returns true if the error was
1836 simulated; false if a message should be issued by the caller. */
1839 cp_parser_simulate_error (cp_parser* parser)
1841 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1843 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1849 /* This function is called when a type is defined. If type
1850 definitions are forbidden at this point, an error message is
1854 cp_parser_check_type_definition (cp_parser* parser)
1856 /* If types are forbidden here, issue a message. */
1857 if (parser->type_definition_forbidden_message)
1858 /* Use `%s' to print the string in case there are any escape
1859 characters in the message. */
1860 error ("%s", parser->type_definition_forbidden_message);
1863 /* This function is called when the DECLARATOR is processed. The TYPE
1864 was a type defined in the decl-specifiers. If it is invalid to
1865 define a type in the decl-specifiers for DECLARATOR, an error is
1869 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1872 /* [dcl.fct] forbids type definitions in return types.
1873 Unfortunately, it's not easy to know whether or not we are
1874 processing a return type until after the fact. */
1876 && (declarator->kind == cdk_pointer
1877 || declarator->kind == cdk_reference
1878 || declarator->kind == cdk_ptrmem))
1879 declarator = declarator->declarator;
1881 && declarator->kind == cdk_function)
1883 error ("new types may not be defined in a return type");
1884 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1889 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1890 "<" in any valid C++ program. If the next token is indeed "<",
1891 issue a message warning the user about what appears to be an
1892 invalid attempt to form a template-id. */
1895 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1898 cp_token_position start = 0;
1900 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1903 error ("%qT is not a template", type);
1904 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1905 error ("%qE is not a template", type);
1907 error ("invalid template-id");
1908 /* Remember the location of the invalid "<". */
1909 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1910 start = cp_lexer_token_position (parser->lexer, true);
1911 /* Consume the "<". */
1912 cp_lexer_consume_token (parser->lexer);
1913 /* Parse the template arguments. */
1914 cp_parser_enclosed_template_argument_list (parser);
1915 /* Permanently remove the invalid template arguments so that
1916 this error message is not issued again. */
1918 cp_lexer_purge_tokens_after (parser->lexer, start);
1922 /* If parsing an integral constant-expression, issue an error message
1923 about the fact that THING appeared and return true. Otherwise,
1924 return false, marking the current expression as non-constant. */
1927 cp_parser_non_integral_constant_expression (cp_parser *parser,
1930 if (parser->integral_constant_expression_p)
1932 if (!parser->allow_non_integral_constant_expression_p)
1934 error ("%s cannot appear in a constant-expression", thing);
1937 parser->non_integral_constant_expression_p = true;
1942 /* Emit a diagnostic for an invalid type name. SCOPE is the
1943 qualifying scope (or NULL, if none) for ID. */
1946 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1948 tree decl, old_scope;
1949 /* Try to lookup the identifier. */
1950 old_scope = parser->scope;
1951 parser->scope = scope;
1952 decl = cp_parser_lookup_name_simple (parser, id);
1953 parser->scope = old_scope;
1954 /* If the lookup found a template-name, it means that the user forgot
1955 to specify an argument list. Emit an useful error message. */
1956 if (TREE_CODE (decl) == TEMPLATE_DECL)
1957 error ("invalid use of template-name %qE without an argument list",
1959 else if (!parser->scope)
1961 /* Issue an error message. */
1962 error ("%qE does not name a type", id);
1963 /* If we're in a template class, it's possible that the user was
1964 referring to a type from a base class. For example:
1966 template <typename T> struct A { typedef T X; };
1967 template <typename T> struct B : public A<T> { X x; };
1969 The user should have said "typename A<T>::X". */
1970 if (processing_template_decl && current_class_type)
1974 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1978 tree base_type = BINFO_TYPE (b);
1979 if (CLASS_TYPE_P (base_type)
1980 && dependent_type_p (base_type))
1983 /* Go from a particular instantiation of the
1984 template (which will have an empty TYPE_FIELDs),
1985 to the main version. */
1986 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1987 for (field = TYPE_FIELDS (base_type);
1989 field = TREE_CHAIN (field))
1990 if (TREE_CODE (field) == TYPE_DECL
1991 && DECL_NAME (field) == id)
1993 inform ("(perhaps %<typename %T::%E%> was intended)",
1994 BINFO_TYPE (b), id);
2003 /* Here we diagnose qualified-ids where the scope is actually correct,
2004 but the identifier does not resolve to a valid type name. */
2007 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2008 error ("%qE in namespace %qE does not name a type",
2010 else if (TYPE_P (parser->scope))
2011 error ("%qE in class %qT does not name a type", id, parser->scope);
2017 /* Check for a common situation where a type-name should be present,
2018 but is not, and issue a sensible error message. Returns true if an
2019 invalid type-name was detected.
2021 The situation handled by this function are variable declarations of the
2022 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2023 Usually, `ID' should name a type, but if we got here it means that it
2024 does not. We try to emit the best possible error message depending on
2025 how exactly the id-expression looks like.
2029 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2033 cp_parser_parse_tentatively (parser);
2034 id = cp_parser_id_expression (parser,
2035 /*template_keyword_p=*/false,
2036 /*check_dependency_p=*/true,
2037 /*template_p=*/NULL,
2038 /*declarator_p=*/true);
2039 /* After the id-expression, there should be a plain identifier,
2040 otherwise this is not a simple variable declaration. Also, if
2041 the scope is dependent, we cannot do much. */
2042 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2043 || (parser->scope && TYPE_P (parser->scope)
2044 && dependent_type_p (parser->scope)))
2046 cp_parser_abort_tentative_parse (parser);
2049 if (!cp_parser_parse_definitely (parser)
2050 || TREE_CODE (id) != IDENTIFIER_NODE)
2053 /* Emit a diagnostic for the invalid type. */
2054 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2055 /* Skip to the end of the declaration; there's no point in
2056 trying to process it. */
2057 cp_parser_skip_to_end_of_block_or_statement (parser);
2061 /* Consume tokens up to, and including, the next non-nested closing `)'.
2062 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2063 are doing error recovery. Returns -1 if OR_COMMA is true and we
2064 found an unnested comma. */
2067 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2072 unsigned paren_depth = 0;
2073 unsigned brace_depth = 0;
2076 if (recovering && !or_comma
2077 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2084 /* If we've run out of tokens, then there is no closing `)'. */
2085 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2091 token = cp_lexer_peek_token (parser->lexer);
2093 /* This matches the processing in skip_to_end_of_statement. */
2094 if (token->type == CPP_SEMICOLON && !brace_depth)
2099 if (token->type == CPP_OPEN_BRACE)
2101 if (token->type == CPP_CLOSE_BRACE)
2109 if (recovering && or_comma && token->type == CPP_COMMA
2110 && !brace_depth && !paren_depth)
2118 /* If it is an `(', we have entered another level of nesting. */
2119 if (token->type == CPP_OPEN_PAREN)
2121 /* If it is a `)', then we might be done. */
2122 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2125 cp_lexer_consume_token (parser->lexer);
2133 /* Consume the token. */
2134 cp_lexer_consume_token (parser->lexer);
2140 /* Consume tokens until we reach the end of the current statement.
2141 Normally, that will be just before consuming a `;'. However, if a
2142 non-nested `}' comes first, then we stop before consuming that. */
2145 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2147 unsigned nesting_depth = 0;
2153 /* Peek at the next token. */
2154 token = cp_lexer_peek_token (parser->lexer);
2155 /* If we've run out of tokens, stop. */
2156 if (token->type == CPP_EOF)
2158 /* If the next token is a `;', we have reached the end of the
2160 if (token->type == CPP_SEMICOLON && !nesting_depth)
2162 /* If the next token is a non-nested `}', then we have reached
2163 the end of the current block. */
2164 if (token->type == CPP_CLOSE_BRACE)
2166 /* If this is a non-nested `}', stop before consuming it.
2167 That way, when confronted with something like:
2171 we stop before consuming the closing `}', even though we
2172 have not yet reached a `;'. */
2173 if (nesting_depth == 0)
2175 /* If it is the closing `}' for a block that we have
2176 scanned, stop -- but only after consuming the token.
2182 we will stop after the body of the erroneously declared
2183 function, but before consuming the following `typedef'
2185 if (--nesting_depth == 0)
2187 cp_lexer_consume_token (parser->lexer);
2191 /* If it the next token is a `{', then we are entering a new
2192 block. Consume the entire block. */
2193 else if (token->type == CPP_OPEN_BRACE)
2195 /* Consume the token. */
2196 cp_lexer_consume_token (parser->lexer);
2200 /* This function is called at the end of a statement or declaration.
2201 If the next token is a semicolon, it is consumed; otherwise, error
2202 recovery is attempted. */
2205 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2207 /* Look for the trailing `;'. */
2208 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2210 /* If there is additional (erroneous) input, skip to the end of
2212 cp_parser_skip_to_end_of_statement (parser);
2213 /* If the next token is now a `;', consume it. */
2214 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2215 cp_lexer_consume_token (parser->lexer);
2219 /* Skip tokens until we have consumed an entire block, or until we
2220 have consumed a non-nested `;'. */
2223 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2225 unsigned nesting_depth = 0;
2231 /* Peek at the next token. */
2232 token = cp_lexer_peek_token (parser->lexer);
2233 /* If we've run out of tokens, stop. */
2234 if (token->type == CPP_EOF)
2236 /* If the next token is a `;', we have reached the end of the
2238 if (token->type == CPP_SEMICOLON && !nesting_depth)
2240 /* Consume the `;'. */
2241 cp_lexer_consume_token (parser->lexer);
2244 /* Consume the token. */
2245 token = cp_lexer_consume_token (parser->lexer);
2246 /* If the next token is a non-nested `}', then we have reached
2247 the end of the current block. */
2248 if (token->type == CPP_CLOSE_BRACE
2249 && (nesting_depth == 0 || --nesting_depth == 0))
2251 /* If it the next token is a `{', then we are entering a new
2252 block. Consume the entire block. */
2253 if (token->type == CPP_OPEN_BRACE)
2258 /* Skip tokens until a non-nested closing curly brace is the next
2262 cp_parser_skip_to_closing_brace (cp_parser *parser)
2264 unsigned nesting_depth = 0;
2270 /* Peek at the next token. */
2271 token = cp_lexer_peek_token (parser->lexer);
2272 /* If we've run out of tokens, stop. */
2273 if (token->type == CPP_EOF)
2275 /* If the next token is a non-nested `}', then we have reached
2276 the end of the current block. */
2277 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2279 /* If it the next token is a `{', then we are entering a new
2280 block. Consume the entire block. */
2281 else if (token->type == CPP_OPEN_BRACE)
2283 /* Consume the token. */
2284 cp_lexer_consume_token (parser->lexer);
2288 /* This is a simple wrapper around make_typename_type. When the id is
2289 an unresolved identifier node, we can provide a superior diagnostic
2290 using cp_parser_diagnose_invalid_type_name. */
2293 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2296 if (TREE_CODE (id) == IDENTIFIER_NODE)
2298 result = make_typename_type (scope, id, typename_type,
2300 if (result == error_mark_node)
2301 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2304 return make_typename_type (scope, id, typename_type, tf_error);
2308 /* Create a new C++ parser. */
2311 cp_parser_new (void)
2317 /* cp_lexer_new_main is called before calling ggc_alloc because
2318 cp_lexer_new_main might load a PCH file. */
2319 lexer = cp_lexer_new_main ();
2321 /* Initialize the binops_by_token so that we can get the tree
2322 directly from the token. */
2323 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2324 binops_by_token[binops[i].token_type] = binops[i];
2326 parser = GGC_CNEW (cp_parser);
2327 parser->lexer = lexer;
2328 parser->context = cp_parser_context_new (NULL);
2330 /* For now, we always accept GNU extensions. */
2331 parser->allow_gnu_extensions_p = 1;
2333 /* The `>' token is a greater-than operator, not the end of a
2335 parser->greater_than_is_operator_p = true;
2337 parser->default_arg_ok_p = true;
2339 /* We are not parsing a constant-expression. */
2340 parser->integral_constant_expression_p = false;
2341 parser->allow_non_integral_constant_expression_p = false;
2342 parser->non_integral_constant_expression_p = false;
2344 /* Local variable names are not forbidden. */
2345 parser->local_variables_forbidden_p = false;
2347 /* We are not processing an `extern "C"' declaration. */
2348 parser->in_unbraced_linkage_specification_p = false;
2350 /* We are not processing a declarator. */
2351 parser->in_declarator_p = false;
2353 /* We are not processing a template-argument-list. */
2354 parser->in_template_argument_list_p = false;
2356 /* We are not in an iteration statement. */
2357 parser->in_iteration_statement_p = false;
2359 /* We are not in a switch statement. */
2360 parser->in_switch_statement_p = false;
2362 /* We are not parsing a type-id inside an expression. */
2363 parser->in_type_id_in_expr_p = false;
2365 /* Declarations aren't implicitly extern "C". */
2366 parser->implicit_extern_c = false;
2368 /* String literals should be translated to the execution character set. */
2369 parser->translate_strings_p = true;
2371 /* The unparsed function queue is empty. */
2372 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2374 /* There are no classes being defined. */
2375 parser->num_classes_being_defined = 0;
2377 /* No template parameters apply. */
2378 parser->num_template_parameter_lists = 0;
2383 /* Create a cp_lexer structure which will emit the tokens in CACHE
2384 and push it onto the parser's lexer stack. This is used for delayed
2385 parsing of in-class method bodies and default arguments, and should
2386 not be confused with tentative parsing. */
2388 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2390 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2391 lexer->next = parser->lexer;
2392 parser->lexer = lexer;
2394 /* Move the current source position to that of the first token in the
2396 cp_lexer_set_source_position_from_token (lexer->next_token);
2399 /* Pop the top lexer off the parser stack. This is never used for the
2400 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2402 cp_parser_pop_lexer (cp_parser *parser)
2404 cp_lexer *lexer = parser->lexer;
2405 parser->lexer = lexer->next;
2406 cp_lexer_destroy (lexer);
2408 /* Put the current source position back where it was before this
2409 lexer was pushed. */
2410 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2413 /* Lexical conventions [gram.lex] */
2415 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2419 cp_parser_identifier (cp_parser* parser)
2423 /* Look for the identifier. */
2424 token = cp_parser_require (parser, CPP_NAME, "identifier");
2425 /* Return the value. */
2426 return token ? token->value : error_mark_node;
2429 /* Parse a sequence of adjacent string constants. Returns a
2430 TREE_STRING representing the combined, nul-terminated string
2431 constant. If TRANSLATE is true, translate the string to the
2432 execution character set. If WIDE_OK is true, a wide string is
2435 C++98 [lex.string] says that if a narrow string literal token is
2436 adjacent to a wide string literal token, the behavior is undefined.
2437 However, C99 6.4.5p4 says that this results in a wide string literal.
2438 We follow C99 here, for consistency with the C front end.
2440 This code is largely lifted from lex_string() in c-lex.c.
2442 FUTURE: ObjC++ will need to handle @-strings here. */
2444 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2449 struct obstack str_ob;
2450 cpp_string str, istr, *strs;
2453 tok = cp_lexer_peek_token (parser->lexer);
2454 if (!cp_parser_is_string_literal (tok))
2456 cp_parser_error (parser, "expected string-literal");
2457 return error_mark_node;
2460 /* Try to avoid the overhead of creating and destroying an obstack
2461 for the common case of just one string. */
2462 if (!cp_parser_is_string_literal
2463 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2465 cp_lexer_consume_token (parser->lexer);
2467 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2468 str.len = TREE_STRING_LENGTH (tok->value);
2470 if (tok->type == CPP_WSTRING)
2477 gcc_obstack_init (&str_ob);
2482 cp_lexer_consume_token (parser->lexer);
2484 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2485 str.len = TREE_STRING_LENGTH (tok->value);
2486 if (tok->type == CPP_WSTRING)
2489 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2491 tok = cp_lexer_peek_token (parser->lexer);
2493 while (cp_parser_is_string_literal (tok));
2495 strs = (cpp_string *) obstack_finish (&str_ob);
2498 if (wide && !wide_ok)
2500 cp_parser_error (parser, "a wide string is invalid in this context");
2504 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2505 (parse_in, strs, count, &istr, wide))
2507 value = build_string (istr.len, (char *)istr.text);
2508 free ((void *)istr.text);
2510 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2511 value = fix_string_type (value);
2514 /* cpp_interpret_string has issued an error. */
2515 value = error_mark_node;
2518 obstack_free (&str_ob, 0);
2524 /* Basic concepts [gram.basic] */
2526 /* Parse a translation-unit.
2529 declaration-seq [opt]
2531 Returns TRUE if all went well. */
2534 cp_parser_translation_unit (cp_parser* parser)
2536 /* The address of the first non-permanent object on the declarator
2538 static void *declarator_obstack_base;
2542 /* Create the declarator obstack, if necessary. */
2543 if (!cp_error_declarator)
2545 gcc_obstack_init (&declarator_obstack);
2546 /* Create the error declarator. */
2547 cp_error_declarator = make_declarator (cdk_error);
2548 /* Create the empty parameter list. */
2549 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2550 /* Remember where the base of the declarator obstack lies. */
2551 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2556 cp_parser_declaration_seq_opt (parser);
2558 /* If there are no tokens left then all went well. */
2559 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2561 /* Get rid of the token array; we don't need it any more. */
2562 cp_lexer_destroy (parser->lexer);
2563 parser->lexer = NULL;
2565 /* This file might have been a context that's implicitly extern
2566 "C". If so, pop the lang context. (Only relevant for PCH.) */
2567 if (parser->implicit_extern_c)
2569 pop_lang_context ();
2570 parser->implicit_extern_c = false;
2574 finish_translation_unit ();
2581 cp_parser_error (parser, "expected declaration");
2587 /* Make sure the declarator obstack was fully cleaned up. */
2588 gcc_assert (obstack_next_free (&declarator_obstack)
2589 == declarator_obstack_base);
2591 /* All went well. */
2595 /* Expressions [gram.expr] */
2597 /* Parse a primary-expression.
2608 ( compound-statement )
2609 __builtin_va_arg ( assignment-expression , type-id )
2614 Returns a representation of the expression.
2616 *IDK indicates what kind of id-expression (if any) was present.
2618 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2619 used as the operand of a pointer-to-member. In that case,
2620 *QUALIFYING_CLASS gives the class that is used as the qualifying
2621 class in the pointer-to-member. */
2624 cp_parser_primary_expression (cp_parser *parser,
2626 tree *qualifying_class)
2630 /* Assume the primary expression is not an id-expression. */
2631 *idk = CP_ID_KIND_NONE;
2632 /* And that it cannot be used as pointer-to-member. */
2633 *qualifying_class = NULL_TREE;
2635 /* Peek at the next token. */
2636 token = cp_lexer_peek_token (parser->lexer);
2637 switch (token->type)
2648 token = cp_lexer_consume_token (parser->lexer);
2649 return token->value;
2653 /* ??? Should wide strings be allowed when parser->translate_strings_p
2654 is false (i.e. in attributes)? If not, we can kill the third
2655 argument to cp_parser_string_literal. */
2656 return cp_parser_string_literal (parser,
2657 parser->translate_strings_p,
2660 case CPP_OPEN_PAREN:
2663 bool saved_greater_than_is_operator_p;
2665 /* Consume the `('. */
2666 cp_lexer_consume_token (parser->lexer);
2667 /* Within a parenthesized expression, a `>' token is always
2668 the greater-than operator. */
2669 saved_greater_than_is_operator_p
2670 = parser->greater_than_is_operator_p;
2671 parser->greater_than_is_operator_p = true;
2672 /* If we see `( { ' then we are looking at the beginning of
2673 a GNU statement-expression. */
2674 if (cp_parser_allow_gnu_extensions_p (parser)
2675 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2677 /* Statement-expressions are not allowed by the standard. */
2679 pedwarn ("ISO C++ forbids braced-groups within expressions");
2681 /* And they're not allowed outside of a function-body; you
2682 cannot, for example, write:
2684 int i = ({ int j = 3; j + 1; });
2686 at class or namespace scope. */
2687 if (!at_function_scope_p ())
2688 error ("statement-expressions are allowed only inside functions");
2689 /* Start the statement-expression. */
2690 expr = begin_stmt_expr ();
2691 /* Parse the compound-statement. */
2692 cp_parser_compound_statement (parser, expr, false);
2694 expr = finish_stmt_expr (expr, false);
2698 /* Parse the parenthesized expression. */
2699 expr = cp_parser_expression (parser);
2700 /* Let the front end know that this expression was
2701 enclosed in parentheses. This matters in case, for
2702 example, the expression is of the form `A::B', since
2703 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2705 finish_parenthesized_expr (expr);
2707 /* The `>' token might be the end of a template-id or
2708 template-parameter-list now. */
2709 parser->greater_than_is_operator_p
2710 = saved_greater_than_is_operator_p;
2711 /* Consume the `)'. */
2712 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2713 cp_parser_skip_to_end_of_statement (parser);
2719 switch (token->keyword)
2721 /* These two are the boolean literals. */
2723 cp_lexer_consume_token (parser->lexer);
2724 return boolean_true_node;
2726 cp_lexer_consume_token (parser->lexer);
2727 return boolean_false_node;
2729 /* The `__null' literal. */
2731 cp_lexer_consume_token (parser->lexer);
2734 /* Recognize the `this' keyword. */
2736 cp_lexer_consume_token (parser->lexer);
2737 if (parser->local_variables_forbidden_p)
2739 error ("%<this%> may not be used in this context");
2740 return error_mark_node;
2742 /* Pointers cannot appear in constant-expressions. */
2743 if (cp_parser_non_integral_constant_expression (parser,
2745 return error_mark_node;
2746 return finish_this_expr ();
2748 /* The `operator' keyword can be the beginning of an
2753 case RID_FUNCTION_NAME:
2754 case RID_PRETTY_FUNCTION_NAME:
2755 case RID_C99_FUNCTION_NAME:
2756 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2757 __func__ are the names of variables -- but they are
2758 treated specially. Therefore, they are handled here,
2759 rather than relying on the generic id-expression logic
2760 below. Grammatically, these names are id-expressions.
2762 Consume the token. */
2763 token = cp_lexer_consume_token (parser->lexer);
2764 /* Look up the name. */
2765 return finish_fname (token->value);
2772 /* The `__builtin_va_arg' construct is used to handle
2773 `va_arg'. Consume the `__builtin_va_arg' token. */
2774 cp_lexer_consume_token (parser->lexer);
2775 /* Look for the opening `('. */
2776 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2777 /* Now, parse the assignment-expression. */
2778 expression = cp_parser_assignment_expression (parser);
2779 /* Look for the `,'. */
2780 cp_parser_require (parser, CPP_COMMA, "`,'");
2781 /* Parse the type-id. */
2782 type = cp_parser_type_id (parser);
2783 /* Look for the closing `)'. */
2784 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2785 /* Using `va_arg' in a constant-expression is not
2787 if (cp_parser_non_integral_constant_expression (parser,
2789 return error_mark_node;
2790 return build_x_va_arg (expression, type);
2794 return cp_parser_builtin_offsetof (parser);
2797 cp_parser_error (parser, "expected primary-expression");
2798 return error_mark_node;
2801 /* An id-expression can start with either an identifier, a
2802 `::' as the beginning of a qualified-id, or the "operator"
2806 case CPP_TEMPLATE_ID:
2807 case CPP_NESTED_NAME_SPECIFIER:
2811 const char *error_msg;
2814 /* Parse the id-expression. */
2816 = cp_parser_id_expression (parser,
2817 /*template_keyword_p=*/false,
2818 /*check_dependency_p=*/true,
2819 /*template_p=*/NULL,
2820 /*declarator_p=*/false);
2821 if (id_expression == error_mark_node)
2822 return error_mark_node;
2823 /* If we have a template-id, then no further lookup is
2824 required. If the template-id was for a template-class, we
2825 will sometimes have a TYPE_DECL at this point. */
2826 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2827 || TREE_CODE (id_expression) == TYPE_DECL)
2828 decl = id_expression;
2829 /* Look up the name. */
2834 decl = cp_parser_lookup_name (parser, id_expression,
2836 /*is_template=*/false,
2837 /*is_namespace=*/false,
2838 /*check_dependency=*/true,
2840 /* If the lookup was ambiguous, an error will already have
2843 return error_mark_node;
2844 /* If name lookup gives us a SCOPE_REF, then the
2845 qualifying scope was dependent. Just propagate the
2847 if (TREE_CODE (decl) == SCOPE_REF)
2849 if (TYPE_P (TREE_OPERAND (decl, 0)))
2850 *qualifying_class = TREE_OPERAND (decl, 0);
2853 /* Check to see if DECL is a local variable in a context
2854 where that is forbidden. */
2855 if (parser->local_variables_forbidden_p
2856 && local_variable_p (decl))
2858 /* It might be that we only found DECL because we are
2859 trying to be generous with pre-ISO scoping rules.
2860 For example, consider:
2864 for (int i = 0; i < 10; ++i) {}
2865 extern void f(int j = i);
2868 Here, name look up will originally find the out
2869 of scope `i'. We need to issue a warning message,
2870 but then use the global `i'. */
2871 decl = check_for_out_of_scope_variable (decl);
2872 if (local_variable_p (decl))
2874 error ("local variable %qD may not appear in this context",
2876 return error_mark_node;
2881 decl = finish_id_expression (id_expression, decl, parser->scope,
2882 idk, qualifying_class,
2883 parser->integral_constant_expression_p,
2884 parser->allow_non_integral_constant_expression_p,
2885 &parser->non_integral_constant_expression_p,
2888 cp_parser_error (parser, error_msg);
2892 /* Anything else is an error. */
2894 cp_parser_error (parser, "expected primary-expression");
2895 return error_mark_node;
2899 /* Parse an id-expression.
2906 :: [opt] nested-name-specifier template [opt] unqualified-id
2908 :: operator-function-id
2911 Return a representation of the unqualified portion of the
2912 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2913 a `::' or nested-name-specifier.
2915 Often, if the id-expression was a qualified-id, the caller will
2916 want to make a SCOPE_REF to represent the qualified-id. This
2917 function does not do this in order to avoid wastefully creating
2918 SCOPE_REFs when they are not required.
2920 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2923 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2924 uninstantiated templates.
2926 If *TEMPLATE_P is non-NULL, it is set to true iff the
2927 `template' keyword is used to explicitly indicate that the entity
2928 named is a template.
2930 If DECLARATOR_P is true, the id-expression is appearing as part of
2931 a declarator, rather than as part of an expression. */
2934 cp_parser_id_expression (cp_parser *parser,
2935 bool template_keyword_p,
2936 bool check_dependency_p,
2940 bool global_scope_p;
2941 bool nested_name_specifier_p;
2943 /* Assume the `template' keyword was not used. */
2945 *template_p = false;
2947 /* Look for the optional `::' operator. */
2949 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2951 /* Look for the optional nested-name-specifier. */
2952 nested_name_specifier_p
2953 = (cp_parser_nested_name_specifier_opt (parser,
2954 /*typename_keyword_p=*/false,
2959 /* If there is a nested-name-specifier, then we are looking at
2960 the first qualified-id production. */
2961 if (nested_name_specifier_p)
2964 tree saved_object_scope;
2965 tree saved_qualifying_scope;
2966 tree unqualified_id;
2969 /* See if the next token is the `template' keyword. */
2971 template_p = &is_template;
2972 *template_p = cp_parser_optional_template_keyword (parser);
2973 /* Name lookup we do during the processing of the
2974 unqualified-id might obliterate SCOPE. */
2975 saved_scope = parser->scope;
2976 saved_object_scope = parser->object_scope;
2977 saved_qualifying_scope = parser->qualifying_scope;
2978 /* Process the final unqualified-id. */
2979 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2982 /* Restore the SAVED_SCOPE for our caller. */
2983 parser->scope = saved_scope;
2984 parser->object_scope = saved_object_scope;
2985 parser->qualifying_scope = saved_qualifying_scope;
2987 return unqualified_id;
2989 /* Otherwise, if we are in global scope, then we are looking at one
2990 of the other qualified-id productions. */
2991 else if (global_scope_p)
2996 /* Peek at the next token. */
2997 token = cp_lexer_peek_token (parser->lexer);
2999 /* If it's an identifier, and the next token is not a "<", then
3000 we can avoid the template-id case. This is an optimization
3001 for this common case. */
3002 if (token->type == CPP_NAME
3003 && !cp_parser_nth_token_starts_template_argument_list_p
3005 return cp_parser_identifier (parser);
3007 cp_parser_parse_tentatively (parser);
3008 /* Try a template-id. */
3009 id = cp_parser_template_id (parser,
3010 /*template_keyword_p=*/false,
3011 /*check_dependency_p=*/true,
3013 /* If that worked, we're done. */
3014 if (cp_parser_parse_definitely (parser))
3017 /* Peek at the next token. (Changes in the token buffer may
3018 have invalidated the pointer obtained above.) */
3019 token = cp_lexer_peek_token (parser->lexer);
3021 switch (token->type)
3024 return cp_parser_identifier (parser);
3027 if (token->keyword == RID_OPERATOR)
3028 return cp_parser_operator_function_id (parser);
3032 cp_parser_error (parser, "expected id-expression");
3033 return error_mark_node;
3037 return cp_parser_unqualified_id (parser, template_keyword_p,
3038 /*check_dependency_p=*/true,
3042 /* Parse an unqualified-id.
3046 operator-function-id
3047 conversion-function-id
3051 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3052 keyword, in a construct like `A::template ...'.
3054 Returns a representation of unqualified-id. For the `identifier'
3055 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3056 production a BIT_NOT_EXPR is returned; the operand of the
3057 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3058 other productions, see the documentation accompanying the
3059 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3060 names are looked up in uninstantiated templates. If DECLARATOR_P
3061 is true, the unqualified-id is appearing as part of a declarator,
3062 rather than as part of an expression. */
3065 cp_parser_unqualified_id (cp_parser* parser,
3066 bool template_keyword_p,
3067 bool check_dependency_p,
3072 /* Peek at the next token. */
3073 token = cp_lexer_peek_token (parser->lexer);
3075 switch (token->type)
3081 /* We don't know yet whether or not this will be a
3083 cp_parser_parse_tentatively (parser);
3084 /* Try a template-id. */
3085 id = cp_parser_template_id (parser, template_keyword_p,
3088 /* If it worked, we're done. */
3089 if (cp_parser_parse_definitely (parser))
3091 /* Otherwise, it's an ordinary identifier. */
3092 return cp_parser_identifier (parser);
3095 case CPP_TEMPLATE_ID:
3096 return cp_parser_template_id (parser, template_keyword_p,
3103 tree qualifying_scope;
3107 /* Consume the `~' token. */
3108 cp_lexer_consume_token (parser->lexer);
3109 /* Parse the class-name. The standard, as written, seems to
3112 template <typename T> struct S { ~S (); };
3113 template <typename T> S<T>::~S() {}
3115 is invalid, since `~' must be followed by a class-name, but
3116 `S<T>' is dependent, and so not known to be a class.
3117 That's not right; we need to look in uninstantiated
3118 templates. A further complication arises from:
3120 template <typename T> void f(T t) {
3124 Here, it is not possible to look up `T' in the scope of `T'
3125 itself. We must look in both the current scope, and the
3126 scope of the containing complete expression.
3128 Yet another issue is:
3137 The standard does not seem to say that the `S' in `~S'
3138 should refer to the type `S' and not the data member
3141 /* DR 244 says that we look up the name after the "~" in the
3142 same scope as we looked up the qualifying name. That idea
3143 isn't fully worked out; it's more complicated than that. */
3144 scope = parser->scope;
3145 object_scope = parser->object_scope;
3146 qualifying_scope = parser->qualifying_scope;
3148 /* If the name is of the form "X::~X" it's OK. */
3149 if (scope && TYPE_P (scope)
3150 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3151 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3153 && (cp_lexer_peek_token (parser->lexer)->value
3154 == TYPE_IDENTIFIER (scope)))
3156 cp_lexer_consume_token (parser->lexer);
3157 return build_nt (BIT_NOT_EXPR, scope);
3160 /* If there was an explicit qualification (S::~T), first look
3161 in the scope given by the qualification (i.e., S). */
3164 cp_parser_parse_tentatively (parser);
3165 type_decl = cp_parser_class_name (parser,
3166 /*typename_keyword_p=*/false,
3167 /*template_keyword_p=*/false,
3169 /*check_dependency=*/false,
3170 /*class_head_p=*/false,
3172 if (cp_parser_parse_definitely (parser))
3173 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3175 /* In "N::S::~S", look in "N" as well. */
3176 if (scope && qualifying_scope)
3178 cp_parser_parse_tentatively (parser);
3179 parser->scope = qualifying_scope;
3180 parser->object_scope = NULL_TREE;
3181 parser->qualifying_scope = NULL_TREE;
3183 = cp_parser_class_name (parser,
3184 /*typename_keyword_p=*/false,
3185 /*template_keyword_p=*/false,
3187 /*check_dependency=*/false,
3188 /*class_head_p=*/false,
3190 if (cp_parser_parse_definitely (parser))
3191 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3193 /* In "p->S::~T", look in the scope given by "*p" as well. */
3194 else if (object_scope)
3196 cp_parser_parse_tentatively (parser);
3197 parser->scope = object_scope;
3198 parser->object_scope = NULL_TREE;
3199 parser->qualifying_scope = NULL_TREE;
3201 = cp_parser_class_name (parser,
3202 /*typename_keyword_p=*/false,
3203 /*template_keyword_p=*/false,
3205 /*check_dependency=*/false,
3206 /*class_head_p=*/false,
3208 if (cp_parser_parse_definitely (parser))
3209 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3211 /* Look in the surrounding context. */
3212 parser->scope = NULL_TREE;
3213 parser->object_scope = NULL_TREE;
3214 parser->qualifying_scope = NULL_TREE;
3216 = cp_parser_class_name (parser,
3217 /*typename_keyword_p=*/false,
3218 /*template_keyword_p=*/false,
3220 /*check_dependency=*/false,
3221 /*class_head_p=*/false,
3223 /* If an error occurred, assume that the name of the
3224 destructor is the same as the name of the qualifying
3225 class. That allows us to keep parsing after running
3226 into ill-formed destructor names. */
3227 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3228 return build_nt (BIT_NOT_EXPR, scope);
3229 else if (type_decl == error_mark_node)
3230 return error_mark_node;
3234 A typedef-name that names a class shall not be used as the
3235 identifier in the declarator for a destructor declaration. */
3237 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3238 && !DECL_SELF_REFERENCE_P (type_decl))
3239 error ("typedef-name %qD used as destructor declarator",
3242 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3246 if (token->keyword == RID_OPERATOR)
3250 /* This could be a template-id, so we try that first. */
3251 cp_parser_parse_tentatively (parser);
3252 /* Try a template-id. */
3253 id = cp_parser_template_id (parser, template_keyword_p,
3254 /*check_dependency_p=*/true,
3256 /* If that worked, we're done. */
3257 if (cp_parser_parse_definitely (parser))
3259 /* We still don't know whether we're looking at an
3260 operator-function-id or a conversion-function-id. */
3261 cp_parser_parse_tentatively (parser);
3262 /* Try an operator-function-id. */
3263 id = cp_parser_operator_function_id (parser);
3264 /* If that didn't work, try a conversion-function-id. */
3265 if (!cp_parser_parse_definitely (parser))
3266 id = cp_parser_conversion_function_id (parser);
3273 cp_parser_error (parser, "expected unqualified-id");
3274 return error_mark_node;
3278 /* Parse an (optional) nested-name-specifier.
3280 nested-name-specifier:
3281 class-or-namespace-name :: nested-name-specifier [opt]
3282 class-or-namespace-name :: template nested-name-specifier [opt]
3284 PARSER->SCOPE should be set appropriately before this function is
3285 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3286 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3289 Sets PARSER->SCOPE to the class (TYPE) or namespace
3290 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3291 it unchanged if there is no nested-name-specifier. Returns the new
3292 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3294 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3295 part of a declaration and/or decl-specifier. */
3298 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3299 bool typename_keyword_p,
3300 bool check_dependency_p,
3302 bool is_declaration)
3304 bool success = false;
3305 tree access_check = NULL_TREE;
3306 cp_token_position start = 0;
3309 /* If the next token corresponds to a nested name specifier, there
3310 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3311 false, it may have been true before, in which case something
3312 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3313 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3314 CHECK_DEPENDENCY_P is false, we have to fall through into the
3316 if (check_dependency_p
3317 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3319 cp_parser_pre_parsed_nested_name_specifier (parser);
3320 return parser->scope;
3323 /* Remember where the nested-name-specifier starts. */
3324 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3325 start = cp_lexer_token_position (parser->lexer, false);
3327 push_deferring_access_checks (dk_deferred);
3333 tree saved_qualifying_scope;
3334 bool template_keyword_p;
3336 /* Spot cases that cannot be the beginning of a
3337 nested-name-specifier. */
3338 token = cp_lexer_peek_token (parser->lexer);
3340 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3341 the already parsed nested-name-specifier. */
3342 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3344 /* Grab the nested-name-specifier and continue the loop. */
3345 cp_parser_pre_parsed_nested_name_specifier (parser);
3350 /* Spot cases that cannot be the beginning of a
3351 nested-name-specifier. On the second and subsequent times
3352 through the loop, we look for the `template' keyword. */
3353 if (success && token->keyword == RID_TEMPLATE)
3355 /* A template-id can start a nested-name-specifier. */
3356 else if (token->type == CPP_TEMPLATE_ID)
3360 /* If the next token is not an identifier, then it is
3361 definitely not a class-or-namespace-name. */
3362 if (token->type != CPP_NAME)
3364 /* If the following token is neither a `<' (to begin a
3365 template-id), nor a `::', then we are not looking at a
3366 nested-name-specifier. */
3367 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3368 if (token->type != CPP_SCOPE
3369 && !cp_parser_nth_token_starts_template_argument_list_p
3374 /* The nested-name-specifier is optional, so we parse
3376 cp_parser_parse_tentatively (parser);
3378 /* Look for the optional `template' keyword, if this isn't the
3379 first time through the loop. */
3381 template_keyword_p = cp_parser_optional_template_keyword (parser);
3383 template_keyword_p = false;
3385 /* Save the old scope since the name lookup we are about to do
3386 might destroy it. */
3387 old_scope = parser->scope;
3388 saved_qualifying_scope = parser->qualifying_scope;
3389 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3390 look up names in "X<T>::I" in order to determine that "Y" is
3391 a template. So, if we have a typename at this point, we make
3392 an effort to look through it. */
3394 && !typename_keyword_p
3396 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3397 parser->scope = resolve_typename_type (parser->scope,
3398 /*only_current_p=*/false);
3399 /* Parse the qualifying entity. */
3401 = cp_parser_class_or_namespace_name (parser,
3407 /* Look for the `::' token. */
3408 cp_parser_require (parser, CPP_SCOPE, "`::'");
3410 /* If we found what we wanted, we keep going; otherwise, we're
3412 if (!cp_parser_parse_definitely (parser))
3414 bool error_p = false;
3416 /* Restore the OLD_SCOPE since it was valid before the
3417 failed attempt at finding the last
3418 class-or-namespace-name. */
3419 parser->scope = old_scope;
3420 parser->qualifying_scope = saved_qualifying_scope;
3421 /* If the next token is an identifier, and the one after
3422 that is a `::', then any valid interpretation would have
3423 found a class-or-namespace-name. */
3424 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3425 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3427 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3430 token = cp_lexer_consume_token (parser->lexer);
3435 decl = cp_parser_lookup_name_simple (parser, token->value);
3436 if (TREE_CODE (decl) == TEMPLATE_DECL)
3437 error ("%qD used without template parameters", decl);
3439 cp_parser_name_lookup_error
3440 (parser, token->value, decl,
3441 "is not a class or namespace");
3442 parser->scope = NULL_TREE;
3444 /* Treat this as a successful nested-name-specifier
3449 If the name found is not a class-name (clause
3450 _class_) or namespace-name (_namespace.def_), the
3451 program is ill-formed. */
3454 cp_lexer_consume_token (parser->lexer);
3459 /* We've found one valid nested-name-specifier. */
3461 /* Make sure we look in the right scope the next time through
3463 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3464 ? TREE_TYPE (new_scope)
3466 /* If it is a class scope, try to complete it; we are about to
3467 be looking up names inside the class. */
3468 if (TYPE_P (parser->scope)
3469 /* Since checking types for dependency can be expensive,
3470 avoid doing it if the type is already complete. */
3471 && !COMPLETE_TYPE_P (parser->scope)
3472 /* Do not try to complete dependent types. */
3473 && !dependent_type_p (parser->scope))
3474 complete_type (parser->scope);
3477 /* Retrieve any deferred checks. Do not pop this access checks yet
3478 so the memory will not be reclaimed during token replacing below. */
3479 access_check = get_deferred_access_checks ();
3481 /* If parsing tentatively, replace the sequence of tokens that makes
3482 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3483 token. That way, should we re-parse the token stream, we will
3484 not have to repeat the effort required to do the parse, nor will
3485 we issue duplicate error messages. */
3486 if (success && start)
3488 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3490 /* Reset the contents of the START token. */
3491 token->type = CPP_NESTED_NAME_SPECIFIER;
3492 token->value = build_tree_list (access_check, parser->scope);
3493 TREE_TYPE (token->value) = parser->qualifying_scope;
3494 token->keyword = RID_MAX;
3496 /* Purge all subsequent tokens. */
3497 cp_lexer_purge_tokens_after (parser->lexer, start);
3500 pop_deferring_access_checks ();
3501 return success ? parser->scope : NULL_TREE;
3504 /* Parse a nested-name-specifier. See
3505 cp_parser_nested_name_specifier_opt for details. This function
3506 behaves identically, except that it will an issue an error if no
3507 nested-name-specifier is present, and it will return
3508 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3512 cp_parser_nested_name_specifier (cp_parser *parser,
3513 bool typename_keyword_p,
3514 bool check_dependency_p,
3516 bool is_declaration)
3520 /* Look for the nested-name-specifier. */
3521 scope = cp_parser_nested_name_specifier_opt (parser,
3526 /* If it was not present, issue an error message. */
3529 cp_parser_error (parser, "expected nested-name-specifier");
3530 parser->scope = NULL_TREE;
3531 return error_mark_node;
3537 /* Parse a class-or-namespace-name.
3539 class-or-namespace-name:
3543 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3544 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3545 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3546 TYPE_P is TRUE iff the next name should be taken as a class-name,
3547 even the same name is declared to be another entity in the same
3550 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3551 specified by the class-or-namespace-name. If neither is found the
3552 ERROR_MARK_NODE is returned. */
3555 cp_parser_class_or_namespace_name (cp_parser *parser,
3556 bool typename_keyword_p,
3557 bool template_keyword_p,
3558 bool check_dependency_p,
3560 bool is_declaration)
3563 tree saved_qualifying_scope;
3564 tree saved_object_scope;
3568 /* Before we try to parse the class-name, we must save away the
3569 current PARSER->SCOPE since cp_parser_class_name will destroy
3571 saved_scope = parser->scope;
3572 saved_qualifying_scope = parser->qualifying_scope;
3573 saved_object_scope = parser->object_scope;
3574 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3575 there is no need to look for a namespace-name. */
3576 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3578 cp_parser_parse_tentatively (parser);
3579 scope = cp_parser_class_name (parser,
3582 type_p ? class_type : none_type,
3584 /*class_head_p=*/false,
3586 /* If that didn't work, try for a namespace-name. */
3587 if (!only_class_p && !cp_parser_parse_definitely (parser))
3589 /* Restore the saved scope. */
3590 parser->scope = saved_scope;
3591 parser->qualifying_scope = saved_qualifying_scope;
3592 parser->object_scope = saved_object_scope;
3593 /* If we are not looking at an identifier followed by the scope
3594 resolution operator, then this is not part of a
3595 nested-name-specifier. (Note that this function is only used
3596 to parse the components of a nested-name-specifier.) */
3597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3598 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3599 return error_mark_node;
3600 scope = cp_parser_namespace_name (parser);
3606 /* Parse a postfix-expression.
3610 postfix-expression [ expression ]
3611 postfix-expression ( expression-list [opt] )
3612 simple-type-specifier ( expression-list [opt] )
3613 typename :: [opt] nested-name-specifier identifier
3614 ( expression-list [opt] )
3615 typename :: [opt] nested-name-specifier template [opt] template-id
3616 ( expression-list [opt] )
3617 postfix-expression . template [opt] id-expression
3618 postfix-expression -> template [opt] id-expression
3619 postfix-expression . pseudo-destructor-name
3620 postfix-expression -> pseudo-destructor-name
3621 postfix-expression ++
3622 postfix-expression --
3623 dynamic_cast < type-id > ( expression )
3624 static_cast < type-id > ( expression )
3625 reinterpret_cast < type-id > ( expression )
3626 const_cast < type-id > ( expression )
3627 typeid ( expression )
3633 ( type-id ) { initializer-list , [opt] }
3635 This extension is a GNU version of the C99 compound-literal
3636 construct. (The C99 grammar uses `type-name' instead of `type-id',
3637 but they are essentially the same concept.)
3639 If ADDRESS_P is true, the postfix expression is the operand of the
3642 Returns a representation of the expression. */
3645 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3649 cp_id_kind idk = CP_ID_KIND_NONE;
3650 tree postfix_expression = NULL_TREE;
3651 /* Non-NULL only if the current postfix-expression can be used to
3652 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3653 class used to qualify the member. */
3654 tree qualifying_class = NULL_TREE;
3656 /* Peek at the next token. */
3657 token = cp_lexer_peek_token (parser->lexer);
3658 /* Some of the productions are determined by keywords. */
3659 keyword = token->keyword;
3669 const char *saved_message;
3671 /* All of these can be handled in the same way from the point
3672 of view of parsing. Begin by consuming the token
3673 identifying the cast. */
3674 cp_lexer_consume_token (parser->lexer);
3676 /* New types cannot be defined in the cast. */
3677 saved_message = parser->type_definition_forbidden_message;
3678 parser->type_definition_forbidden_message
3679 = "types may not be defined in casts";
3681 /* Look for the opening `<'. */
3682 cp_parser_require (parser, CPP_LESS, "`<'");
3683 /* Parse the type to which we are casting. */
3684 type = cp_parser_type_id (parser);
3685 /* Look for the closing `>'. */
3686 cp_parser_require (parser, CPP_GREATER, "`>'");
3687 /* Restore the old message. */
3688 parser->type_definition_forbidden_message = saved_message;
3690 /* And the expression which is being cast. */
3691 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3692 expression = cp_parser_expression (parser);
3693 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3695 /* Only type conversions to integral or enumeration types
3696 can be used in constant-expressions. */
3697 if (parser->integral_constant_expression_p
3698 && !dependent_type_p (type)
3699 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3700 && (cp_parser_non_integral_constant_expression
3702 "a cast to a type other than an integral or "
3703 "enumeration type")))
3704 return error_mark_node;
3710 = build_dynamic_cast (type, expression);
3714 = build_static_cast (type, expression);
3718 = build_reinterpret_cast (type, expression);
3722 = build_const_cast (type, expression);
3733 const char *saved_message;
3734 bool saved_in_type_id_in_expr_p;
3736 /* Consume the `typeid' token. */
3737 cp_lexer_consume_token (parser->lexer);
3738 /* Look for the `(' token. */
3739 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3740 /* Types cannot be defined in a `typeid' expression. */
3741 saved_message = parser->type_definition_forbidden_message;
3742 parser->type_definition_forbidden_message
3743 = "types may not be defined in a `typeid\' expression";
3744 /* We can't be sure yet whether we're looking at a type-id or an
3746 cp_parser_parse_tentatively (parser);
3747 /* Try a type-id first. */
3748 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3749 parser->in_type_id_in_expr_p = true;
3750 type = cp_parser_type_id (parser);
3751 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3752 /* Look for the `)' token. Otherwise, we can't be sure that
3753 we're not looking at an expression: consider `typeid (int
3754 (3))', for example. */
3755 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3756 /* If all went well, simply lookup the type-id. */
3757 if (cp_parser_parse_definitely (parser))
3758 postfix_expression = get_typeid (type);
3759 /* Otherwise, fall back to the expression variant. */
3764 /* Look for an expression. */
3765 expression = cp_parser_expression (parser);
3766 /* Compute its typeid. */
3767 postfix_expression = build_typeid (expression);
3768 /* Look for the `)' token. */
3769 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3771 /* `typeid' may not appear in an integral constant expression. */
3772 if (cp_parser_non_integral_constant_expression(parser,
3773 "`typeid' operator"))
3774 return error_mark_node;
3775 /* Restore the saved message. */
3776 parser->type_definition_forbidden_message = saved_message;
3782 bool template_p = false;
3786 /* Consume the `typename' token. */
3787 cp_lexer_consume_token (parser->lexer);
3788 /* Look for the optional `::' operator. */
3789 cp_parser_global_scope_opt (parser,
3790 /*current_scope_valid_p=*/false);
3791 /* Look for the nested-name-specifier. */
3792 cp_parser_nested_name_specifier (parser,
3793 /*typename_keyword_p=*/true,
3794 /*check_dependency_p=*/true,
3796 /*is_declaration=*/true);
3797 /* Look for the optional `template' keyword. */
3798 template_p = cp_parser_optional_template_keyword (parser);
3799 /* We don't know whether we're looking at a template-id or an
3801 cp_parser_parse_tentatively (parser);
3802 /* Try a template-id. */
3803 id = cp_parser_template_id (parser, template_p,
3804 /*check_dependency_p=*/true,
3805 /*is_declaration=*/true);
3806 /* If that didn't work, try an identifier. */
3807 if (!cp_parser_parse_definitely (parser))
3808 id = cp_parser_identifier (parser);
3809 /* If we look up a template-id in a non-dependent qualifying
3810 scope, there's no need to create a dependent type. */
3811 if (TREE_CODE (id) == TYPE_DECL
3812 && !dependent_type_p (parser->scope))
3813 type = TREE_TYPE (id);
3814 /* Create a TYPENAME_TYPE to represent the type to which the
3815 functional cast is being performed. */
3817 type = make_typename_type (parser->scope, id,
3821 postfix_expression = cp_parser_functional_cast (parser, type);
3829 /* If the next thing is a simple-type-specifier, we may be
3830 looking at a functional cast. We could also be looking at
3831 an id-expression. So, we try the functional cast, and if
3832 that doesn't work we fall back to the primary-expression. */
3833 cp_parser_parse_tentatively (parser);
3834 /* Look for the simple-type-specifier. */
3835 type = cp_parser_simple_type_specifier (parser,
3836 /*decl_specs=*/NULL,
3837 CP_PARSER_FLAGS_NONE);
3838 /* Parse the cast itself. */
3839 if (!cp_parser_error_occurred (parser))
3841 = cp_parser_functional_cast (parser, type);
3842 /* If that worked, we're done. */
3843 if (cp_parser_parse_definitely (parser))
3846 /* If the functional-cast didn't work out, try a
3847 compound-literal. */
3848 if (cp_parser_allow_gnu_extensions_p (parser)
3849 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3851 tree initializer_list = NULL_TREE;
3852 bool saved_in_type_id_in_expr_p;
3854 cp_parser_parse_tentatively (parser);
3855 /* Consume the `('. */
3856 cp_lexer_consume_token (parser->lexer);
3857 /* Parse the type. */
3858 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3859 parser->in_type_id_in_expr_p = true;
3860 type = cp_parser_type_id (parser);
3861 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3862 /* Look for the `)'. */
3863 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3864 /* Look for the `{'. */
3865 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3866 /* If things aren't going well, there's no need to
3868 if (!cp_parser_error_occurred (parser))
3870 bool non_constant_p;
3871 /* Parse the initializer-list. */
3873 = cp_parser_initializer_list (parser, &non_constant_p);
3874 /* Allow a trailing `,'. */
3875 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3876 cp_lexer_consume_token (parser->lexer);
3877 /* Look for the final `}'. */
3878 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3880 /* If that worked, we're definitely looking at a
3881 compound-literal expression. */
3882 if (cp_parser_parse_definitely (parser))
3884 /* Warn the user that a compound literal is not
3885 allowed in standard C++. */
3887 pedwarn ("ISO C++ forbids compound-literals");
3888 /* Form the representation of the compound-literal. */
3890 = finish_compound_literal (type, initializer_list);
3895 /* It must be a primary-expression. */
3896 postfix_expression = cp_parser_primary_expression (parser,
3903 /* If we were avoiding committing to the processing of a
3904 qualified-id until we knew whether or not we had a
3905 pointer-to-member, we now know. */
3906 if (qualifying_class)
3910 /* Peek at the next token. */
3911 token = cp_lexer_peek_token (parser->lexer);
3912 done = (token->type != CPP_OPEN_SQUARE
3913 && token->type != CPP_OPEN_PAREN
3914 && token->type != CPP_DOT
3915 && token->type != CPP_DEREF
3916 && token->type != CPP_PLUS_PLUS
3917 && token->type != CPP_MINUS_MINUS);
3919 postfix_expression = finish_qualified_id_expr (qualifying_class,
3924 return postfix_expression;
3927 /* Keep looping until the postfix-expression is complete. */
3930 if (idk == CP_ID_KIND_UNQUALIFIED
3931 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3932 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3933 /* It is not a Koenig lookup function call. */
3935 = unqualified_name_lookup_error (postfix_expression);
3937 /* Peek at the next token. */
3938 token = cp_lexer_peek_token (parser->lexer);
3940 switch (token->type)
3942 case CPP_OPEN_SQUARE:
3944 = cp_parser_postfix_open_square_expression (parser,
3947 idk = CP_ID_KIND_NONE;
3950 case CPP_OPEN_PAREN:
3951 /* postfix-expression ( expression-list [opt] ) */
3954 tree args = (cp_parser_parenthesized_expression_list
3955 (parser, false, /*non_constant_p=*/NULL));
3957 if (args == error_mark_node)
3959 postfix_expression = error_mark_node;
3963 /* Function calls are not permitted in
3964 constant-expressions. */
3965 if (cp_parser_non_integral_constant_expression (parser,
3968 postfix_expression = error_mark_node;
3973 if (idk == CP_ID_KIND_UNQUALIFIED)
3975 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3981 = perform_koenig_lookup (postfix_expression, args);
3985 = unqualified_fn_lookup_error (postfix_expression);
3987 /* We do not perform argument-dependent lookup if
3988 normal lookup finds a non-function, in accordance
3989 with the expected resolution of DR 218. */
3990 else if (args && is_overloaded_fn (postfix_expression))
3992 tree fn = get_first_fn (postfix_expression);
3994 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3995 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3997 /* Only do argument dependent lookup if regular
3998 lookup does not find a set of member functions.
3999 [basic.lookup.koenig]/2a */
4000 if (!DECL_FUNCTION_MEMBER_P (fn))
4004 = perform_koenig_lookup (postfix_expression, args);
4009 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4011 tree instance = TREE_OPERAND (postfix_expression, 0);
4012 tree fn = TREE_OPERAND (postfix_expression, 1);
4014 if (processing_template_decl
4015 && (type_dependent_expression_p (instance)
4016 || (!BASELINK_P (fn)
4017 && TREE_CODE (fn) != FIELD_DECL)
4018 || type_dependent_expression_p (fn)
4019 || any_type_dependent_arguments_p (args)))
4022 = build_min_nt (CALL_EXPR, postfix_expression,
4027 if (BASELINK_P (fn))
4029 = (build_new_method_call
4030 (instance, fn, args, NULL_TREE,
4031 (idk == CP_ID_KIND_QUALIFIED
4032 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4035 = finish_call_expr (postfix_expression, args,
4036 /*disallow_virtual=*/false,
4037 /*koenig_p=*/false);
4039 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4040 || TREE_CODE (postfix_expression) == MEMBER_REF
4041 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4042 postfix_expression = (build_offset_ref_call_from_tree
4043 (postfix_expression, args));
4044 else if (idk == CP_ID_KIND_QUALIFIED)
4045 /* A call to a static class member, or a namespace-scope
4048 = finish_call_expr (postfix_expression, args,
4049 /*disallow_virtual=*/true,
4052 /* All other function calls. */
4054 = finish_call_expr (postfix_expression, args,
4055 /*disallow_virtual=*/false,
4058 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4059 idk = CP_ID_KIND_NONE;
4065 /* postfix-expression . template [opt] id-expression
4066 postfix-expression . pseudo-destructor-name
4067 postfix-expression -> template [opt] id-expression
4068 postfix-expression -> pseudo-destructor-name */
4070 /* Consume the `.' or `->' operator. */
4071 cp_lexer_consume_token (parser->lexer);
4074 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4080 /* postfix-expression ++ */
4081 /* Consume the `++' token. */
4082 cp_lexer_consume_token (parser->lexer);
4083 /* Generate a representation for the complete expression. */
4085 = finish_increment_expr (postfix_expression,
4086 POSTINCREMENT_EXPR);
4087 /* Increments may not appear in constant-expressions. */
4088 if (cp_parser_non_integral_constant_expression (parser,
4090 postfix_expression = error_mark_node;
4091 idk = CP_ID_KIND_NONE;
4094 case CPP_MINUS_MINUS:
4095 /* postfix-expression -- */
4096 /* Consume the `--' token. */
4097 cp_lexer_consume_token (parser->lexer);
4098 /* Generate a representation for the complete expression. */
4100 = finish_increment_expr (postfix_expression,
4101 POSTDECREMENT_EXPR);
4102 /* Decrements may not appear in constant-expressions. */
4103 if (cp_parser_non_integral_constant_expression (parser,
4105 postfix_expression = error_mark_node;
4106 idk = CP_ID_KIND_NONE;
4110 return postfix_expression;
4114 /* We should never get here. */
4116 return error_mark_node;
4119 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4120 by cp_parser_builtin_offsetof. We're looking for
4122 postfix-expression [ expression ]
4124 FOR_OFFSETOF is set if we're being called in that context, which
4125 changes how we deal with integer constant expressions. */
4128 cp_parser_postfix_open_square_expression (cp_parser *parser,
4129 tree postfix_expression,
4134 /* Consume the `[' token. */
4135 cp_lexer_consume_token (parser->lexer);
4137 /* Parse the index expression. */
4138 /* ??? For offsetof, there is a question of what to allow here. If
4139 offsetof is not being used in an integral constant expression context,
4140 then we *could* get the right answer by computing the value at runtime.
4141 If we are in an integral constant expression context, then we might
4142 could accept any constant expression; hard to say without analysis.
4143 Rather than open the barn door too wide right away, allow only integer
4144 constant expressions here. */
4146 index = cp_parser_constant_expression (parser, false, NULL);
4148 index = cp_parser_expression (parser);
4150 /* Look for the closing `]'. */
4151 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4153 /* Build the ARRAY_REF. */
4154 postfix_expression = grok_array_decl (postfix_expression, index);
4156 /* When not doing offsetof, array references are not permitted in
4157 constant-expressions. */
4159 && (cp_parser_non_integral_constant_expression
4160 (parser, "an array reference")))
4161 postfix_expression = error_mark_node;
4163 return postfix_expression;
4166 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4167 by cp_parser_builtin_offsetof. We're looking for
4169 postfix-expression . template [opt] id-expression
4170 postfix-expression . pseudo-destructor-name
4171 postfix-expression -> template [opt] id-expression
4172 postfix-expression -> pseudo-destructor-name
4174 FOR_OFFSETOF is set if we're being called in that context. That sorta
4175 limits what of the above we'll actually accept, but nevermind.
4176 TOKEN_TYPE is the "." or "->" token, which will already have been
4177 removed from the stream. */
4180 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4181 enum cpp_ttype token_type,
4182 tree postfix_expression,
4183 bool for_offsetof, cp_id_kind *idk)
4188 bool pseudo_destructor_p;
4189 tree scope = NULL_TREE;
4191 /* If this is a `->' operator, dereference the pointer. */
4192 if (token_type == CPP_DEREF)
4193 postfix_expression = build_x_arrow (postfix_expression);
4194 /* Check to see whether or not the expression is type-dependent. */
4195 dependent_p = type_dependent_expression_p (postfix_expression);
4196 /* The identifier following the `->' or `.' is not qualified. */
4197 parser->scope = NULL_TREE;
4198 parser->qualifying_scope = NULL_TREE;
4199 parser->object_scope = NULL_TREE;
4200 *idk = CP_ID_KIND_NONE;
4201 /* Enter the scope corresponding to the type of the object
4202 given by the POSTFIX_EXPRESSION. */
4203 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4205 scope = TREE_TYPE (postfix_expression);
4206 /* According to the standard, no expression should ever have
4207 reference type. Unfortunately, we do not currently match
4208 the standard in this respect in that our internal representation
4209 of an expression may have reference type even when the standard
4210 says it does not. Therefore, we have to manually obtain the
4211 underlying type here. */
4212 scope = non_reference (scope);
4213 /* The type of the POSTFIX_EXPRESSION must be complete. */
4214 scope = complete_type_or_else (scope, NULL_TREE);
4215 /* Let the name lookup machinery know that we are processing a
4216 class member access expression. */
4217 parser->context->object_type = scope;
4218 /* If something went wrong, we want to be able to discern that case,
4219 as opposed to the case where there was no SCOPE due to the type
4220 of expression being dependent. */
4222 scope = error_mark_node;
4223 /* If the SCOPE was erroneous, make the various semantic analysis
4224 functions exit quickly -- and without issuing additional error
4226 if (scope == error_mark_node)
4227 postfix_expression = error_mark_node;
4230 /* Assume this expression is not a pseudo-destructor access. */
4231 pseudo_destructor_p = false;
4233 /* If the SCOPE is a scalar type, then, if this is a valid program,
4234 we must be looking at a pseudo-destructor-name. */
4235 if (scope && SCALAR_TYPE_P (scope))
4240 cp_parser_parse_tentatively (parser);
4241 /* Parse the pseudo-destructor-name. */
4243 cp_parser_pseudo_destructor_name (parser, &s, &type);
4244 if (cp_parser_parse_definitely (parser))
4246 pseudo_destructor_p = true;
4248 = finish_pseudo_destructor_expr (postfix_expression,
4249 s, TREE_TYPE (type));
4253 if (!pseudo_destructor_p)
4255 /* If the SCOPE is not a scalar type, we are looking at an
4256 ordinary class member access expression, rather than a
4257 pseudo-destructor-name. */
4258 template_p = cp_parser_optional_template_keyword (parser);
4259 /* Parse the id-expression. */
4260 name = cp_parser_id_expression (parser, template_p,
4261 /*check_dependency_p=*/true,
4262 /*template_p=*/NULL,
4263 /*declarator_p=*/false);
4264 /* In general, build a SCOPE_REF if the member name is qualified.
4265 However, if the name was not dependent and has already been
4266 resolved; there is no need to build the SCOPE_REF. For example;
4268 struct X { void f(); };
4269 template <typename T> void f(T* t) { t->X::f(); }
4271 Even though "t" is dependent, "X::f" is not and has been resolved
4272 to a BASELINK; there is no need to include scope information. */
4274 /* But we do need to remember that there was an explicit scope for
4275 virtual function calls. */
4277 *idk = CP_ID_KIND_QUALIFIED;
4279 /* If the name is a template-id that names a type, we will get a
4280 TYPE_DECL here. That is invalid code. */
4281 if (TREE_CODE (name) == TYPE_DECL)
4283 error ("invalid use of %qD", name);
4284 postfix_expression = error_mark_node;
4288 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4290 name = build_nt (SCOPE_REF, parser->scope, name);
4291 parser->scope = NULL_TREE;
4292 parser->qualifying_scope = NULL_TREE;
4293 parser->object_scope = NULL_TREE;
4295 if (scope && name && BASELINK_P (name))
4296 adjust_result_of_qualified_name_lookup
4297 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4299 = finish_class_member_access_expr (postfix_expression, name);
4303 /* We no longer need to look up names in the scope of the object on
4304 the left-hand side of the `.' or `->' operator. */
4305 parser->context->object_type = NULL_TREE;
4307 /* Outside of offsetof, these operators may not appear in
4308 constant-expressions. */
4310 && (cp_parser_non_integral_constant_expression
4311 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4312 postfix_expression = error_mark_node;
4314 return postfix_expression;
4317 /* Parse a parenthesized expression-list.
4320 assignment-expression
4321 expression-list, assignment-expression
4326 identifier, expression-list
4328 Returns a TREE_LIST. The TREE_VALUE of each node is a
4329 representation of an assignment-expression. Note that a TREE_LIST
4330 is returned even if there is only a single expression in the list.
4331 error_mark_node is returned if the ( and or ) are
4332 missing. NULL_TREE is returned on no expressions. The parentheses
4333 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4334 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4335 indicates whether or not all of the expressions in the list were
4339 cp_parser_parenthesized_expression_list (cp_parser* parser,
4340 bool is_attribute_list,
4341 bool *non_constant_p)
4343 tree expression_list = NULL_TREE;
4344 bool fold_expr_p = is_attribute_list;
4345 tree identifier = NULL_TREE;
4347 /* Assume all the expressions will be constant. */
4349 *non_constant_p = false;
4351 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4352 return error_mark_node;
4354 /* Consume expressions until there are no more. */
4355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4360 /* At the beginning of attribute lists, check to see if the
4361 next token is an identifier. */
4362 if (is_attribute_list
4363 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4367 /* Consume the identifier. */
4368 token = cp_lexer_consume_token (parser->lexer);
4369 /* Save the identifier. */
4370 identifier = token->value;
4374 /* Parse the next assignment-expression. */
4377 bool expr_non_constant_p;
4378 expr = (cp_parser_constant_expression
4379 (parser, /*allow_non_constant_p=*/true,
4380 &expr_non_constant_p));
4381 if (expr_non_constant_p)
4382 *non_constant_p = true;
4385 expr = cp_parser_assignment_expression (parser);
4388 expr = fold_non_dependent_expr (expr);
4390 /* Add it to the list. We add error_mark_node
4391 expressions to the list, so that we can still tell if
4392 the correct form for a parenthesized expression-list
4393 is found. That gives better errors. */
4394 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4396 if (expr == error_mark_node)
4400 /* After the first item, attribute lists look the same as
4401 expression lists. */
4402 is_attribute_list = false;
4405 /* If the next token isn't a `,', then we are done. */
4406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4409 /* Otherwise, consume the `,' and keep going. */
4410 cp_lexer_consume_token (parser->lexer);
4413 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4418 /* We try and resync to an unnested comma, as that will give the
4419 user better diagnostics. */
4420 ending = cp_parser_skip_to_closing_parenthesis (parser,
4421 /*recovering=*/true,
4423 /*consume_paren=*/true);
4427 return error_mark_node;
4430 /* We built up the list in reverse order so we must reverse it now. */
4431 expression_list = nreverse (expression_list);
4433 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4435 return expression_list;
4438 /* Parse a pseudo-destructor-name.
4440 pseudo-destructor-name:
4441 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4442 :: [opt] nested-name-specifier template template-id :: ~ type-name
4443 :: [opt] nested-name-specifier [opt] ~ type-name
4445 If either of the first two productions is used, sets *SCOPE to the
4446 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4447 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4448 or ERROR_MARK_NODE if the parse fails. */
4451 cp_parser_pseudo_destructor_name (cp_parser* parser,
4455 bool nested_name_specifier_p;
4457 /* Assume that things will not work out. */
4458 *type = error_mark_node;
4460 /* Look for the optional `::' operator. */
4461 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4462 /* Look for the optional nested-name-specifier. */
4463 nested_name_specifier_p
4464 = (cp_parser_nested_name_specifier_opt (parser,
4465 /*typename_keyword_p=*/false,
4466 /*check_dependency_p=*/true,
4468 /*is_declaration=*/true)
4470 /* Now, if we saw a nested-name-specifier, we might be doing the
4471 second production. */
4472 if (nested_name_specifier_p
4473 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4475 /* Consume the `template' keyword. */
4476 cp_lexer_consume_token (parser->lexer);
4477 /* Parse the template-id. */
4478 cp_parser_template_id (parser,
4479 /*template_keyword_p=*/true,
4480 /*check_dependency_p=*/false,
4481 /*is_declaration=*/true);
4482 /* Look for the `::' token. */
4483 cp_parser_require (parser, CPP_SCOPE, "`::'");
4485 /* If the next token is not a `~', then there might be some
4486 additional qualification. */
4487 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4489 /* Look for the type-name. */
4490 *scope = TREE_TYPE (cp_parser_type_name (parser));
4492 if (*scope == error_mark_node)
4495 /* If we don't have ::~, then something has gone wrong. Since
4496 the only caller of this function is looking for something
4497 after `.' or `->' after a scalar type, most likely the
4498 program is trying to get a member of a non-aggregate
4500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4501 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4503 cp_parser_error (parser, "request for member of non-aggregate type");
4507 /* Look for the `::' token. */
4508 cp_parser_require (parser, CPP_SCOPE, "`::'");
4513 /* Look for the `~'. */
4514 cp_parser_require (parser, CPP_COMPL, "`~'");
4515 /* Look for the type-name again. We are not responsible for
4516 checking that it matches the first type-name. */
4517 *type = cp_parser_type_name (parser);
4520 /* Parse a unary-expression.
4526 unary-operator cast-expression
4527 sizeof unary-expression
4535 __extension__ cast-expression
4536 __alignof__ unary-expression
4537 __alignof__ ( type-id )
4538 __real__ cast-expression
4539 __imag__ cast-expression
4542 ADDRESS_P is true iff the unary-expression is appearing as the
4543 operand of the `&' operator.
4545 Returns a representation of the expression. */
4548 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4551 enum tree_code unary_operator;
4553 /* Peek at the next token. */
4554 token = cp_lexer_peek_token (parser->lexer);
4555 /* Some keywords give away the kind of expression. */
4556 if (token->type == CPP_KEYWORD)
4558 enum rid keyword = token->keyword;
4568 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4569 /* Consume the token. */
4570 cp_lexer_consume_token (parser->lexer);
4571 /* Parse the operand. */
4572 operand = cp_parser_sizeof_operand (parser, keyword);
4574 if (TYPE_P (operand))
4575 return cxx_sizeof_or_alignof_type (operand, op, true);
4577 return cxx_sizeof_or_alignof_expr (operand, op);
4581 return cp_parser_new_expression (parser);
4584 return cp_parser_delete_expression (parser);
4588 /* The saved value of the PEDANTIC flag. */
4592 /* Save away the PEDANTIC flag. */
4593 cp_parser_extension_opt (parser, &saved_pedantic);
4594 /* Parse the cast-expression. */
4595 expr = cp_parser_simple_cast_expression (parser);
4596 /* Restore the PEDANTIC flag. */
4597 pedantic = saved_pedantic;
4607 /* Consume the `__real__' or `__imag__' token. */
4608 cp_lexer_consume_token (parser->lexer);
4609 /* Parse the cast-expression. */
4610 expression = cp_parser_simple_cast_expression (parser);
4611 /* Create the complete representation. */
4612 return build_x_unary_op ((keyword == RID_REALPART
4613 ? REALPART_EXPR : IMAGPART_EXPR),
4623 /* Look for the `:: new' and `:: delete', which also signal the
4624 beginning of a new-expression, or delete-expression,
4625 respectively. If the next token is `::', then it might be one of
4627 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4631 /* See if the token after the `::' is one of the keywords in
4632 which we're interested. */
4633 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4634 /* If it's `new', we have a new-expression. */
4635 if (keyword == RID_NEW)
4636 return cp_parser_new_expression (parser);
4637 /* Similarly, for `delete'. */
4638 else if (keyword == RID_DELETE)
4639 return cp_parser_delete_expression (parser);
4642 /* Look for a unary operator. */
4643 unary_operator = cp_parser_unary_operator (token);
4644 /* The `++' and `--' operators can be handled similarly, even though
4645 they are not technically unary-operators in the grammar. */
4646 if (unary_operator == ERROR_MARK)
4648 if (token->type == CPP_PLUS_PLUS)
4649 unary_operator = PREINCREMENT_EXPR;
4650 else if (token->type == CPP_MINUS_MINUS)
4651 unary_operator = PREDECREMENT_EXPR;
4652 /* Handle the GNU address-of-label extension. */
4653 else if (cp_parser_allow_gnu_extensions_p (parser)
4654 && token->type == CPP_AND_AND)
4658 /* Consume the '&&' token. */
4659 cp_lexer_consume_token (parser->lexer);
4660 /* Look for the identifier. */
4661 identifier = cp_parser_identifier (parser);
4662 /* Create an expression representing the address. */
4663 return finish_label_address_expr (identifier);
4666 if (unary_operator != ERROR_MARK)
4668 tree cast_expression;
4669 tree expression = error_mark_node;
4670 const char *non_constant_p = NULL;
4672 /* Consume the operator token. */
4673 token = cp_lexer_consume_token (parser->lexer);
4674 /* Parse the cast-expression. */
4676 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4677 /* Now, build an appropriate representation. */
4678 switch (unary_operator)
4681 non_constant_p = "`*'";
4682 expression = build_x_indirect_ref (cast_expression, "unary *");
4686 non_constant_p = "`&'";
4689 expression = build_x_unary_op (unary_operator, cast_expression);
4692 case PREINCREMENT_EXPR:
4693 case PREDECREMENT_EXPR:
4694 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4699 case TRUTH_NOT_EXPR:
4700 expression = finish_unary_op_expr (unary_operator, cast_expression);
4708 && cp_parser_non_integral_constant_expression (parser,
4710 expression = error_mark_node;
4715 return cp_parser_postfix_expression (parser, address_p);
4718 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4719 unary-operator, the corresponding tree code is returned. */
4721 static enum tree_code
4722 cp_parser_unary_operator (cp_token* token)
4724 switch (token->type)
4727 return INDIRECT_REF;
4733 return CONVERT_EXPR;
4739 return TRUTH_NOT_EXPR;
4742 return BIT_NOT_EXPR;
4749 /* Parse a new-expression.
4752 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4753 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4755 Returns a representation of the expression. */
4758 cp_parser_new_expression (cp_parser* parser)
4760 bool global_scope_p;
4766 /* Look for the optional `::' operator. */
4768 = (cp_parser_global_scope_opt (parser,
4769 /*current_scope_valid_p=*/false)
4771 /* Look for the `new' operator. */
4772 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4773 /* There's no easy way to tell a new-placement from the
4774 `( type-id )' construct. */
4775 cp_parser_parse_tentatively (parser);
4776 /* Look for a new-placement. */
4777 placement = cp_parser_new_placement (parser);
4778 /* If that didn't work out, there's no new-placement. */
4779 if (!cp_parser_parse_definitely (parser))
4780 placement = NULL_TREE;
4782 /* If the next token is a `(', then we have a parenthesized
4784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4786 /* Consume the `('. */
4787 cp_lexer_consume_token (parser->lexer);
4788 /* Parse the type-id. */
4789 type = cp_parser_type_id (parser);
4790 /* Look for the closing `)'. */
4791 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4792 /* There should not be a direct-new-declarator in this production,
4793 but GCC used to allowed this, so we check and emit a sensible error
4794 message for this case. */
4795 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4797 error ("array bound forbidden after parenthesized type-id");
4798 inform ("try removing the parentheses around the type-id");
4799 cp_parser_direct_new_declarator (parser);
4803 /* Otherwise, there must be a new-type-id. */
4805 type = cp_parser_new_type_id (parser, &nelts);
4807 /* If the next token is a `(', then we have a new-initializer. */
4808 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4809 initializer = cp_parser_new_initializer (parser);
4811 initializer = NULL_TREE;
4813 /* A new-expression may not appear in an integral constant
4815 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4816 return error_mark_node;
4818 /* Create a representation of the new-expression. */
4819 return build_new (placement, type, nelts, initializer, global_scope_p);
4822 /* Parse a new-placement.
4827 Returns the same representation as for an expression-list. */
4830 cp_parser_new_placement (cp_parser* parser)
4832 tree expression_list;
4834 /* Parse the expression-list. */
4835 expression_list = (cp_parser_parenthesized_expression_list
4836 (parser, false, /*non_constant_p=*/NULL));
4838 return expression_list;
4841 /* Parse a new-type-id.
4844 type-specifier-seq new-declarator [opt]
4846 Returns the TYPE allocated. If the new-type-id indicates an array
4847 type, *NELTS is set to the number of elements in the last array
4848 bound; the TYPE will not include the last array bound. */
4851 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4853 cp_decl_specifier_seq type_specifier_seq;
4854 cp_declarator *new_declarator;
4855 cp_declarator *declarator;
4856 cp_declarator *outer_declarator;
4857 const char *saved_message;
4860 /* The type-specifier sequence must not contain type definitions.
4861 (It cannot contain declarations of new types either, but if they
4862 are not definitions we will catch that because they are not
4864 saved_message = parser->type_definition_forbidden_message;
4865 parser->type_definition_forbidden_message
4866 = "types may not be defined in a new-type-id";
4867 /* Parse the type-specifier-seq. */
4868 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4869 /* Restore the old message. */
4870 parser->type_definition_forbidden_message = saved_message;
4871 /* Parse the new-declarator. */
4872 new_declarator = cp_parser_new_declarator_opt (parser);
4874 /* Determine the number of elements in the last array dimension, if
4877 /* Skip down to the last array dimension. */
4878 declarator = new_declarator;
4879 outer_declarator = NULL;
4880 while (declarator && (declarator->kind == cdk_pointer
4881 || declarator->kind == cdk_ptrmem))
4883 outer_declarator = declarator;
4884 declarator = declarator->declarator;
4887 && declarator->kind == cdk_array
4888 && declarator->declarator
4889 && declarator->declarator->kind == cdk_array)
4891 outer_declarator = declarator;
4892 declarator = declarator->declarator;
4895 if (declarator && declarator->kind == cdk_array)
4897 *nelts = declarator->u.array.bounds;
4898 if (*nelts == error_mark_node)
4899 *nelts = integer_one_node;
4901 if (outer_declarator)
4902 outer_declarator->declarator = declarator->declarator;
4904 new_declarator = NULL;
4907 type = groktypename (&type_specifier_seq, new_declarator);
4908 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4910 *nelts = array_type_nelts_top (type);
4911 type = TREE_TYPE (type);
4916 /* Parse an (optional) new-declarator.
4919 ptr-operator new-declarator [opt]
4920 direct-new-declarator
4922 Returns the declarator. */
4924 static cp_declarator *
4925 cp_parser_new_declarator_opt (cp_parser* parser)
4927 enum tree_code code;
4929 cp_cv_quals cv_quals;
4931 /* We don't know if there's a ptr-operator next, or not. */
4932 cp_parser_parse_tentatively (parser);
4933 /* Look for a ptr-operator. */
4934 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4935 /* If that worked, look for more new-declarators. */
4936 if (cp_parser_parse_definitely (parser))
4938 cp_declarator *declarator;
4940 /* Parse another optional declarator. */
4941 declarator = cp_parser_new_declarator_opt (parser);
4943 /* Create the representation of the declarator. */
4945 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4946 else if (code == INDIRECT_REF)
4947 declarator = make_pointer_declarator (cv_quals, declarator);
4949 declarator = make_reference_declarator (cv_quals, declarator);
4954 /* If the next token is a `[', there is a direct-new-declarator. */
4955 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4956 return cp_parser_direct_new_declarator (parser);
4961 /* Parse a direct-new-declarator.
4963 direct-new-declarator:
4965 direct-new-declarator [constant-expression]
4969 static cp_declarator *
4970 cp_parser_direct_new_declarator (cp_parser* parser)
4972 cp_declarator *declarator = NULL;
4978 /* Look for the opening `['. */
4979 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4980 /* The first expression is not required to be constant. */
4983 expression = cp_parser_expression (parser);
4984 /* The standard requires that the expression have integral
4985 type. DR 74 adds enumeration types. We believe that the
4986 real intent is that these expressions be handled like the
4987 expression in a `switch' condition, which also allows
4988 classes with a single conversion to integral or
4989 enumeration type. */
4990 if (!processing_template_decl)
4993 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4998 error ("expression in new-declarator must have integral "
4999 "or enumeration type");
5000 expression = error_mark_node;
5004 /* But all the other expressions must be. */
5007 = cp_parser_constant_expression (parser,
5008 /*allow_non_constant=*/false,
5010 /* Look for the closing `]'. */
5011 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5013 /* Add this bound to the declarator. */
5014 declarator = make_array_declarator (declarator, expression);
5016 /* If the next token is not a `[', then there are no more
5018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5025 /* Parse a new-initializer.
5028 ( expression-list [opt] )
5030 Returns a representation of the expression-list. If there is no
5031 expression-list, VOID_ZERO_NODE is returned. */
5034 cp_parser_new_initializer (cp_parser* parser)
5036 tree expression_list;
5038 expression_list = (cp_parser_parenthesized_expression_list
5039 (parser, false, /*non_constant_p=*/NULL));
5040 if (!expression_list)
5041 expression_list = void_zero_node;
5043 return expression_list;
5046 /* Parse a delete-expression.
5049 :: [opt] delete cast-expression
5050 :: [opt] delete [ ] cast-expression
5052 Returns a representation of the expression. */
5055 cp_parser_delete_expression (cp_parser* parser)
5057 bool global_scope_p;
5061 /* Look for the optional `::' operator. */
5063 = (cp_parser_global_scope_opt (parser,
5064 /*current_scope_valid_p=*/false)
5066 /* Look for the `delete' keyword. */
5067 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5068 /* See if the array syntax is in use. */
5069 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5071 /* Consume the `[' token. */
5072 cp_lexer_consume_token (parser->lexer);
5073 /* Look for the `]' token. */
5074 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5075 /* Remember that this is the `[]' construct. */
5081 /* Parse the cast-expression. */
5082 expression = cp_parser_simple_cast_expression (parser);
5084 /* A delete-expression may not appear in an integral constant
5086 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5087 return error_mark_node;
5089 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5092 /* Parse a cast-expression.
5096 ( type-id ) cast-expression
5098 Returns a representation of the expression. */
5101 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5103 /* If it's a `(', then we might be looking at a cast. */
5104 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5106 tree type = NULL_TREE;
5107 tree expr = NULL_TREE;
5108 bool compound_literal_p;
5109 const char *saved_message;
5111 /* There's no way to know yet whether or not this is a cast.
5112 For example, `(int (3))' is a unary-expression, while `(int)
5113 3' is a cast. So, we resort to parsing tentatively. */
5114 cp_parser_parse_tentatively (parser);
5115 /* Types may not be defined in a cast. */
5116 saved_message = parser->type_definition_forbidden_message;
5117 parser->type_definition_forbidden_message
5118 = "types may not be defined in casts";
5119 /* Consume the `('. */
5120 cp_lexer_consume_token (parser->lexer);
5121 /* A very tricky bit is that `(struct S) { 3 }' is a
5122 compound-literal (which we permit in C++ as an extension).
5123 But, that construct is not a cast-expression -- it is a
5124 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5125 is legal; if the compound-literal were a cast-expression,
5126 you'd need an extra set of parentheses.) But, if we parse
5127 the type-id, and it happens to be a class-specifier, then we
5128 will commit to the parse at that point, because we cannot
5129 undo the action that is done when creating a new class. So,
5130 then we cannot back up and do a postfix-expression.
5132 Therefore, we scan ahead to the closing `)', and check to see
5133 if the token after the `)' is a `{'. If so, we are not
5134 looking at a cast-expression.
5136 Save tokens so that we can put them back. */
5137 cp_lexer_save_tokens (parser->lexer);
5138 /* Skip tokens until the next token is a closing parenthesis.
5139 If we find the closing `)', and the next token is a `{', then
5140 we are looking at a compound-literal. */
5142 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5143 /*consume_paren=*/true)
5144 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5145 /* Roll back the tokens we skipped. */
5146 cp_lexer_rollback_tokens (parser->lexer);
5147 /* If we were looking at a compound-literal, simulate an error
5148 so that the call to cp_parser_parse_definitely below will
5150 if (compound_literal_p)
5151 cp_parser_simulate_error (parser);
5154 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5155 parser->in_type_id_in_expr_p = true;
5156 /* Look for the type-id. */
5157 type = cp_parser_type_id (parser);
5158 /* Look for the closing `)'. */
5159 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5160 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5163 /* Restore the saved message. */
5164 parser->type_definition_forbidden_message = saved_message;
5166 /* If ok so far, parse the dependent expression. We cannot be
5167 sure it is a cast. Consider `(T ())'. It is a parenthesized
5168 ctor of T, but looks like a cast to function returning T
5169 without a dependent expression. */
5170 if (!cp_parser_error_occurred (parser))
5171 expr = cp_parser_simple_cast_expression (parser);
5173 if (cp_parser_parse_definitely (parser))
5175 /* Warn about old-style casts, if so requested. */
5176 if (warn_old_style_cast
5177 && !in_system_header
5178 && !VOID_TYPE_P (type)
5179 && current_lang_name != lang_name_c)
5180 warning ("use of old-style cast");
5182 /* Only type conversions to integral or enumeration types
5183 can be used in constant-expressions. */
5184 if (parser->integral_constant_expression_p
5185 && !dependent_type_p (type)
5186 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5187 && (cp_parser_non_integral_constant_expression
5189 "a cast to a type other than an integral or "
5190 "enumeration type")))
5191 return error_mark_node;
5193 /* Perform the cast. */
5194 expr = build_c_cast (type, expr);
5199 /* If we get here, then it's not a cast, so it must be a
5200 unary-expression. */
5201 return cp_parser_unary_expression (parser, address_p);
5204 /* Parse a binary expression of the general form:
5208 pm-expression .* cast-expression
5209 pm-expression ->* cast-expression
5211 multiplicative-expression:
5213 multiplicative-expression * pm-expression
5214 multiplicative-expression / pm-expression
5215 multiplicative-expression % pm-expression
5217 additive-expression:
5218 multiplicative-expression
5219 additive-expression + multiplicative-expression
5220 additive-expression - multiplicative-expression
5224 shift-expression << additive-expression
5225 shift-expression >> additive-expression
5227 relational-expression:
5229 relational-expression < shift-expression
5230 relational-expression > shift-expression
5231 relational-expression <= shift-expression
5232 relational-expression >= shift-expression
5236 relational-expression:
5237 relational-expression <? shift-expression
5238 relational-expression >? shift-expression
5240 equality-expression:
5241 relational-expression
5242 equality-expression == relational-expression
5243 equality-expression != relational-expression
5247 and-expression & equality-expression
5249 exclusive-or-expression:
5251 exclusive-or-expression ^ and-expression
5253 inclusive-or-expression:
5254 exclusive-or-expression
5255 inclusive-or-expression | exclusive-or-expression
5257 logical-and-expression:
5258 inclusive-or-expression
5259 logical-and-expression && inclusive-or-expression
5261 logical-or-expression:
5262 logical-and-expression
5263 logical-or-expression || logical-and-expression
5265 All these are implemented with a single function like:
5268 simple-cast-expression
5269 binary-expression <token> binary-expression
5271 The binops_by_token map is used to get the tree codes for each <token> type.
5272 binary-expressions are associated according to a precedence table. */
5274 #define TOKEN_PRECEDENCE(token) \
5275 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5276 ? PREC_NOT_OPERATOR \
5277 : binops_by_token[token->type].prec)
5280 cp_parser_binary_expression (cp_parser* parser)
5282 cp_parser_expression_stack stack;
5283 cp_parser_expression_stack_entry *sp = &stack[0];
5286 enum tree_code tree_type;
5287 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5290 /* Parse the first expression. */
5291 lhs = cp_parser_simple_cast_expression (parser);
5295 /* Get an operator token. */
5296 token = cp_lexer_peek_token (parser->lexer);
5297 new_prec = TOKEN_PRECEDENCE (token);
5299 /* Popping an entry off the stack means we completed a subexpression:
5300 - either we found a token which is not an operator (`>' where it is not
5301 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5302 will happen repeatedly;
5303 - or, we found an operator which has lower priority. This is the case
5304 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5306 if (new_prec <= prec)
5315 tree_type = binops_by_token[token->type].tree_type;
5317 /* We used the operator token. */
5318 cp_lexer_consume_token (parser->lexer);
5320 /* Extract another operand. It may be the RHS of this expression
5321 or the LHS of a new, higher priority expression. */
5322 rhs = cp_parser_simple_cast_expression (parser);
5324 /* Get another operator token. Look up its precedence to avoid
5325 building a useless (immediately popped) stack entry for common
5326 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5327 token = cp_lexer_peek_token (parser->lexer);
5328 lookahead_prec = TOKEN_PRECEDENCE (token);
5329 if (lookahead_prec > new_prec)
5331 /* ... and prepare to parse the RHS of the new, higher priority
5332 expression. Since precedence levels on the stack are
5333 monotonically increasing, we do not have to care about
5336 sp->tree_type = tree_type;
5341 new_prec = lookahead_prec;
5345 /* If the stack is not empty, we have parsed into LHS the right side
5346 (`4' in the example above) of an expression we had suspended.
5347 We can use the information on the stack to recover the LHS (`3')
5348 from the stack together with the tree code (`MULT_EXPR'), and
5349 the precedence of the higher level subexpression
5350 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5351 which will be used to actually build the additive expression. */
5354 tree_type = sp->tree_type;
5359 overloaded_p = false;
5360 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5362 /* If the binary operator required the use of an overloaded operator,
5363 then this expression cannot be an integral constant-expression.
5364 An overloaded operator can be used even if both operands are
5365 otherwise permissible in an integral constant-expression if at
5366 least one of the operands is of enumeration type. */
5369 && (cp_parser_non_integral_constant_expression
5370 (parser, "calls to overloaded operators")))
5371 return error_mark_node;
5378 /* Parse the `? expression : assignment-expression' part of a
5379 conditional-expression. The LOGICAL_OR_EXPR is the
5380 logical-or-expression that started the conditional-expression.
5381 Returns a representation of the entire conditional-expression.
5383 This routine is used by cp_parser_assignment_expression.
5385 ? expression : assignment-expression
5389 ? : assignment-expression */
5392 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5395 tree assignment_expr;
5397 /* Consume the `?' token. */
5398 cp_lexer_consume_token (parser->lexer);
5399 if (cp_parser_allow_gnu_extensions_p (parser)
5400 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5401 /* Implicit true clause. */
5404 /* Parse the expression. */
5405 expr = cp_parser_expression (parser);
5407 /* The next token should be a `:'. */
5408 cp_parser_require (parser, CPP_COLON, "`:'");
5409 /* Parse the assignment-expression. */
5410 assignment_expr = cp_parser_assignment_expression (parser);
5412 /* Build the conditional-expression. */
5413 return build_x_conditional_expr (logical_or_expr,
5418 /* Parse an assignment-expression.
5420 assignment-expression:
5421 conditional-expression
5422 logical-or-expression assignment-operator assignment_expression
5425 Returns a representation for the expression. */
5428 cp_parser_assignment_expression (cp_parser* parser)
5432 /* If the next token is the `throw' keyword, then we're looking at
5433 a throw-expression. */
5434 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5435 expr = cp_parser_throw_expression (parser);
5436 /* Otherwise, it must be that we are looking at a
5437 logical-or-expression. */
5440 /* Parse the binary expressions (logical-or-expression). */
5441 expr = cp_parser_binary_expression (parser);
5442 /* If the next token is a `?' then we're actually looking at a
5443 conditional-expression. */
5444 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5445 return cp_parser_question_colon_clause (parser, expr);
5448 enum tree_code assignment_operator;
5450 /* If it's an assignment-operator, we're using the second
5453 = cp_parser_assignment_operator_opt (parser);
5454 if (assignment_operator != ERROR_MARK)
5458 /* Parse the right-hand side of the assignment. */
5459 rhs = cp_parser_assignment_expression (parser);
5460 /* An assignment may not appear in a
5461 constant-expression. */
5462 if (cp_parser_non_integral_constant_expression (parser,
5464 return error_mark_node;
5465 /* Build the assignment expression. */
5466 expr = build_x_modify_expr (expr,
5467 assignment_operator,
5476 /* Parse an (optional) assignment-operator.
5478 assignment-operator: one of
5479 = *= /= %= += -= >>= <<= &= ^= |=
5483 assignment-operator: one of
5486 If the next token is an assignment operator, the corresponding tree
5487 code is returned, and the token is consumed. For example, for
5488 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5489 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5490 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5491 operator, ERROR_MARK is returned. */
5493 static enum tree_code
5494 cp_parser_assignment_operator_opt (cp_parser* parser)
5499 /* Peek at the next toen. */
5500 token = cp_lexer_peek_token (parser->lexer);
5502 switch (token->type)
5513 op = TRUNC_DIV_EXPR;
5517 op = TRUNC_MOD_EXPR;
5557 /* Nothing else is an assignment operator. */
5561 /* If it was an assignment operator, consume it. */
5562 if (op != ERROR_MARK)
5563 cp_lexer_consume_token (parser->lexer);
5568 /* Parse an expression.
5571 assignment-expression
5572 expression , assignment-expression
5574 Returns a representation of the expression. */
5577 cp_parser_expression (cp_parser* parser)
5579 tree expression = NULL_TREE;
5583 tree assignment_expression;
5585 /* Parse the next assignment-expression. */
5586 assignment_expression
5587 = cp_parser_assignment_expression (parser);
5588 /* If this is the first assignment-expression, we can just
5591 expression = assignment_expression;
5593 expression = build_x_compound_expr (expression,
5594 assignment_expression);
5595 /* If the next token is not a comma, then we are done with the
5597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5599 /* Consume the `,'. */
5600 cp_lexer_consume_token (parser->lexer);
5601 /* A comma operator cannot appear in a constant-expression. */
5602 if (cp_parser_non_integral_constant_expression (parser,
5603 "a comma operator"))
5604 expression = error_mark_node;
5610 /* Parse a constant-expression.
5612 constant-expression:
5613 conditional-expression
5615 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5616 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5617 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5618 is false, NON_CONSTANT_P should be NULL. */
5621 cp_parser_constant_expression (cp_parser* parser,
5622 bool allow_non_constant_p,
5623 bool *non_constant_p)
5625 bool saved_integral_constant_expression_p;
5626 bool saved_allow_non_integral_constant_expression_p;
5627 bool saved_non_integral_constant_expression_p;
5630 /* It might seem that we could simply parse the
5631 conditional-expression, and then check to see if it were
5632 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5633 one that the compiler can figure out is constant, possibly after
5634 doing some simplifications or optimizations. The standard has a
5635 precise definition of constant-expression, and we must honor
5636 that, even though it is somewhat more restrictive.
5642 is not a legal declaration, because `(2, 3)' is not a
5643 constant-expression. The `,' operator is forbidden in a
5644 constant-expression. However, GCC's constant-folding machinery
5645 will fold this operation to an INTEGER_CST for `3'. */
5647 /* Save the old settings. */
5648 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5649 saved_allow_non_integral_constant_expression_p
5650 = parser->allow_non_integral_constant_expression_p;
5651 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5652 /* We are now parsing a constant-expression. */
5653 parser->integral_constant_expression_p = true;
5654 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5655 parser->non_integral_constant_expression_p = false;
5656 /* Although the grammar says "conditional-expression", we parse an
5657 "assignment-expression", which also permits "throw-expression"
5658 and the use of assignment operators. In the case that
5659 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5660 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5661 actually essential that we look for an assignment-expression.
5662 For example, cp_parser_initializer_clauses uses this function to
5663 determine whether a particular assignment-expression is in fact
5665 expression = cp_parser_assignment_expression (parser);
5666 /* Restore the old settings. */
5667 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5668 parser->allow_non_integral_constant_expression_p
5669 = saved_allow_non_integral_constant_expression_p;
5670 if (allow_non_constant_p)
5671 *non_constant_p = parser->non_integral_constant_expression_p;
5672 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5677 /* Parse __builtin_offsetof.
5679 offsetof-expression:
5680 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5682 offsetof-member-designator:
5684 | offsetof-member-designator "." id-expression
5685 | offsetof-member-designator "[" expression "]"
5689 cp_parser_builtin_offsetof (cp_parser *parser)
5691 int save_ice_p, save_non_ice_p;
5695 /* We're about to accept non-integral-constant things, but will
5696 definitely yield an integral constant expression. Save and
5697 restore these values around our local parsing. */
5698 save_ice_p = parser->integral_constant_expression_p;
5699 save_non_ice_p = parser->non_integral_constant_expression_p;
5701 /* Consume the "__builtin_offsetof" token. */
5702 cp_lexer_consume_token (parser->lexer);
5703 /* Consume the opening `('. */
5704 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5705 /* Parse the type-id. */
5706 type = cp_parser_type_id (parser);
5707 /* Look for the `,'. */
5708 cp_parser_require (parser, CPP_COMMA, "`,'");
5710 /* Build the (type *)null that begins the traditional offsetof macro. */
5711 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5713 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5714 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5718 cp_token *token = cp_lexer_peek_token (parser->lexer);
5719 switch (token->type)
5721 case CPP_OPEN_SQUARE:
5722 /* offsetof-member-designator "[" expression "]" */
5723 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5727 /* offsetof-member-designator "." identifier */
5728 cp_lexer_consume_token (parser->lexer);
5729 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5733 case CPP_CLOSE_PAREN:
5734 /* Consume the ")" token. */
5735 cp_lexer_consume_token (parser->lexer);
5739 /* Error. We know the following require will fail, but
5740 that gives the proper error message. */
5741 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5742 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5743 expr = error_mark_node;
5749 /* If we're processing a template, we can't finish the semantics yet.
5750 Otherwise we can fold the entire expression now. */
5751 if (processing_template_decl)
5752 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5754 expr = fold_offsetof (expr);
5757 parser->integral_constant_expression_p = save_ice_p;
5758 parser->non_integral_constant_expression_p = save_non_ice_p;
5763 /* Statements [gram.stmt.stmt] */
5765 /* Parse a statement.
5769 expression-statement
5774 declaration-statement
5778 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5782 location_t statement_location;
5784 /* There is no statement yet. */
5785 statement = NULL_TREE;
5786 /* Peek at the next token. */
5787 token = cp_lexer_peek_token (parser->lexer);
5788 /* Remember the location of the first token in the statement. */
5789 statement_location = token->location;
5790 /* If this is a keyword, then that will often determine what kind of
5791 statement we have. */
5792 if (token->type == CPP_KEYWORD)
5794 enum rid keyword = token->keyword;
5800 statement = cp_parser_labeled_statement (parser,
5806 statement = cp_parser_selection_statement (parser);
5812 statement = cp_parser_iteration_statement (parser);
5819 statement = cp_parser_jump_statement (parser);
5823 statement = cp_parser_try_block (parser);
5827 /* It might be a keyword like `int' that can start a
5828 declaration-statement. */
5832 else if (token->type == CPP_NAME)
5834 /* If the next token is a `:', then we are looking at a
5835 labeled-statement. */
5836 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5837 if (token->type == CPP_COLON)
5838 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5840 /* Anything that starts with a `{' must be a compound-statement. */
5841 else if (token->type == CPP_OPEN_BRACE)
5842 statement = cp_parser_compound_statement (parser, NULL, false);
5843 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5844 a statement all its own. */
5845 else if (token->type == CPP_PRAGMA)
5847 cp_lexer_handle_pragma (parser->lexer);
5851 /* Everything else must be a declaration-statement or an
5852 expression-statement. Try for the declaration-statement
5853 first, unless we are looking at a `;', in which case we know that
5854 we have an expression-statement. */
5857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5859 cp_parser_parse_tentatively (parser);
5860 /* Try to parse the declaration-statement. */
5861 cp_parser_declaration_statement (parser);
5862 /* If that worked, we're done. */
5863 if (cp_parser_parse_definitely (parser))
5866 /* Look for an expression-statement instead. */
5867 statement = cp_parser_expression_statement (parser, in_statement_expr);
5870 /* Set the line number for the statement. */
5871 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5872 SET_EXPR_LOCATION (statement, statement_location);
5875 /* Parse a labeled-statement.
5878 identifier : statement
5879 case constant-expression : statement
5885 case constant-expression ... constant-expression : statement
5887 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5888 For an ordinary label, returns a LABEL_EXPR. */
5891 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5894 tree statement = error_mark_node;
5896 /* The next token should be an identifier. */
5897 token = cp_lexer_peek_token (parser->lexer);
5898 if (token->type != CPP_NAME
5899 && token->type != CPP_KEYWORD)
5901 cp_parser_error (parser, "expected labeled-statement");
5902 return error_mark_node;
5905 switch (token->keyword)
5912 /* Consume the `case' token. */
5913 cp_lexer_consume_token (parser->lexer);
5914 /* Parse the constant-expression. */
5915 expr = cp_parser_constant_expression (parser,
5916 /*allow_non_constant_p=*/false,
5919 ellipsis = cp_lexer_peek_token (parser->lexer);
5920 if (ellipsis->type == CPP_ELLIPSIS)
5922 /* Consume the `...' token. */
5923 cp_lexer_consume_token (parser->lexer);
5925 cp_parser_constant_expression (parser,
5926 /*allow_non_constant_p=*/false,
5928 /* We don't need to emit warnings here, as the common code
5929 will do this for us. */
5932 expr_hi = NULL_TREE;
5934 if (!parser->in_switch_statement_p)
5935 error ("case label %qE not within a switch statement", expr);
5937 statement = finish_case_label (expr, expr_hi);
5942 /* Consume the `default' token. */
5943 cp_lexer_consume_token (parser->lexer);
5944 if (!parser->in_switch_statement_p)
5945 error ("case label not within a switch statement");
5947 statement = finish_case_label (NULL_TREE, NULL_TREE);
5951 /* Anything else must be an ordinary label. */
5952 statement = finish_label_stmt (cp_parser_identifier (parser));
5956 /* Require the `:' token. */
5957 cp_parser_require (parser, CPP_COLON, "`:'");
5958 /* Parse the labeled statement. */
5959 cp_parser_statement (parser, in_statement_expr);
5961 /* Return the label, in the case of a `case' or `default' label. */
5965 /* Parse an expression-statement.
5967 expression-statement:
5970 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5971 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5972 indicates whether this expression-statement is part of an
5973 expression statement. */
5976 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
5978 tree statement = NULL_TREE;
5980 /* If the next token is a ';', then there is no expression
5982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5983 statement = cp_parser_expression (parser);
5985 /* Consume the final `;'. */
5986 cp_parser_consume_semicolon_at_end_of_statement (parser);
5988 if (in_statement_expr
5989 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5991 /* This is the final expression statement of a statement
5993 statement = finish_stmt_expr_expr (statement, in_statement_expr);
5996 statement = finish_expr_stmt (statement);
6003 /* Parse a compound-statement.
6006 { statement-seq [opt] }
6008 Returns a tree representing the statement. */
6011 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6016 /* Consume the `{'. */
6017 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6018 return error_mark_node;
6019 /* Begin the compound-statement. */
6020 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6021 /* Parse an (optional) statement-seq. */
6022 cp_parser_statement_seq_opt (parser, in_statement_expr);
6023 /* Finish the compound-statement. */
6024 finish_compound_stmt (compound_stmt);
6025 /* Consume the `}'. */
6026 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6028 return compound_stmt;
6031 /* Parse an (optional) statement-seq.
6035 statement-seq [opt] statement */
6038 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6040 /* Scan statements until there aren't any more. */
6043 /* If we're looking at a `}', then we've run out of statements. */
6044 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6045 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6048 /* Parse the statement. */
6049 cp_parser_statement (parser, in_statement_expr);
6053 /* Parse a selection-statement.
6055 selection-statement:
6056 if ( condition ) statement
6057 if ( condition ) statement else statement
6058 switch ( condition ) statement
6060 Returns the new IF_STMT or SWITCH_STMT. */
6063 cp_parser_selection_statement (cp_parser* parser)
6068 /* Peek at the next token. */
6069 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6071 /* See what kind of keyword it is. */
6072 keyword = token->keyword;
6081 /* Look for the `('. */
6082 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6084 cp_parser_skip_to_end_of_statement (parser);
6085 return error_mark_node;
6088 /* Begin the selection-statement. */
6089 if (keyword == RID_IF)
6090 statement = begin_if_stmt ();
6092 statement = begin_switch_stmt ();
6094 /* Parse the condition. */
6095 condition = cp_parser_condition (parser);
6096 /* Look for the `)'. */
6097 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6098 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6099 /*consume_paren=*/true);
6101 if (keyword == RID_IF)
6103 /* Add the condition. */
6104 finish_if_stmt_cond (condition, statement);
6106 /* Parse the then-clause. */
6107 cp_parser_implicitly_scoped_statement (parser);
6108 finish_then_clause (statement);
6110 /* If the next token is `else', parse the else-clause. */
6111 if (cp_lexer_next_token_is_keyword (parser->lexer,
6114 /* Consume the `else' keyword. */
6115 cp_lexer_consume_token (parser->lexer);
6116 begin_else_clause (statement);
6117 /* Parse the else-clause. */
6118 cp_parser_implicitly_scoped_statement (parser);
6119 finish_else_clause (statement);
6122 /* Now we're all done with the if-statement. */
6123 finish_if_stmt (statement);
6127 bool in_switch_statement_p;
6129 /* Add the condition. */
6130 finish_switch_cond (condition, statement);
6132 /* Parse the body of the switch-statement. */
6133 in_switch_statement_p = parser->in_switch_statement_p;
6134 parser->in_switch_statement_p = true;
6135 cp_parser_implicitly_scoped_statement (parser);
6136 parser->in_switch_statement_p = in_switch_statement_p;
6138 /* Now we're all done with the switch-statement. */
6139 finish_switch_stmt (statement);
6147 cp_parser_error (parser, "expected selection-statement");
6148 return error_mark_node;
6152 /* Parse a condition.
6156 type-specifier-seq declarator = assignment-expression
6161 type-specifier-seq declarator asm-specification [opt]
6162 attributes [opt] = assignment-expression
6164 Returns the expression that should be tested. */
6167 cp_parser_condition (cp_parser* parser)
6169 cp_decl_specifier_seq type_specifiers;
6170 const char *saved_message;
6172 /* Try the declaration first. */
6173 cp_parser_parse_tentatively (parser);
6174 /* New types are not allowed in the type-specifier-seq for a
6176 saved_message = parser->type_definition_forbidden_message;
6177 parser->type_definition_forbidden_message
6178 = "types may not be defined in conditions";
6179 /* Parse the type-specifier-seq. */
6180 cp_parser_type_specifier_seq (parser, &type_specifiers);
6181 /* Restore the saved message. */
6182 parser->type_definition_forbidden_message = saved_message;
6183 /* If all is well, we might be looking at a declaration. */
6184 if (!cp_parser_error_occurred (parser))
6187 tree asm_specification;
6189 cp_declarator *declarator;
6190 tree initializer = NULL_TREE;
6192 /* Parse the declarator. */
6193 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6194 /*ctor_dtor_or_conv_p=*/NULL,
6195 /*parenthesized_p=*/NULL,
6196 /*member_p=*/false);
6197 /* Parse the attributes. */
6198 attributes = cp_parser_attributes_opt (parser);
6199 /* Parse the asm-specification. */
6200 asm_specification = cp_parser_asm_specification_opt (parser);
6201 /* If the next token is not an `=', then we might still be
6202 looking at an expression. For example:
6206 looks like a decl-specifier-seq and a declarator -- but then
6207 there is no `=', so this is an expression. */
6208 cp_parser_require (parser, CPP_EQ, "`='");
6209 /* If we did see an `=', then we are looking at a declaration
6211 if (cp_parser_parse_definitely (parser))
6215 /* Create the declaration. */
6216 decl = start_decl (declarator, &type_specifiers,
6217 /*initialized_p=*/true,
6218 attributes, /*prefix_attributes=*/NULL_TREE,
6220 /* Parse the assignment-expression. */
6221 initializer = cp_parser_assignment_expression (parser);
6223 /* Process the initializer. */
6224 cp_finish_decl (decl,
6227 LOOKUP_ONLYCONVERTING);
6230 pop_scope (DECL_CONTEXT (decl));
6232 return convert_from_reference (decl);
6235 /* If we didn't even get past the declarator successfully, we are
6236 definitely not looking at a declaration. */
6238 cp_parser_abort_tentative_parse (parser);
6240 /* Otherwise, we are looking at an expression. */
6241 return cp_parser_expression (parser);
6244 /* Parse an iteration-statement.
6246 iteration-statement:
6247 while ( condition ) statement
6248 do statement while ( expression ) ;
6249 for ( for-init-statement condition [opt] ; expression [opt] )
6252 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6255 cp_parser_iteration_statement (cp_parser* parser)
6260 bool in_iteration_statement_p;
6263 /* Peek at the next token. */
6264 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6266 return error_mark_node;
6268 /* Remember whether or not we are already within an iteration
6270 in_iteration_statement_p = parser->in_iteration_statement_p;
6272 /* See what kind of keyword it is. */
6273 keyword = token->keyword;
6280 /* Begin the while-statement. */
6281 statement = begin_while_stmt ();
6282 /* Look for the `('. */
6283 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6284 /* Parse the condition. */
6285 condition = cp_parser_condition (parser);
6286 finish_while_stmt_cond (condition, statement);
6287 /* Look for the `)'. */
6288 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6289 /* Parse the dependent statement. */
6290 parser->in_iteration_statement_p = true;
6291 cp_parser_already_scoped_statement (parser);
6292 parser->in_iteration_statement_p = in_iteration_statement_p;
6293 /* We're done with the while-statement. */
6294 finish_while_stmt (statement);
6302 /* Begin the do-statement. */
6303 statement = begin_do_stmt ();
6304 /* Parse the body of the do-statement. */
6305 parser->in_iteration_statement_p = true;
6306 cp_parser_implicitly_scoped_statement (parser);
6307 parser->in_iteration_statement_p = in_iteration_statement_p;
6308 finish_do_body (statement);
6309 /* Look for the `while' keyword. */
6310 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6311 /* Look for the `('. */
6312 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6313 /* Parse the expression. */
6314 expression = cp_parser_expression (parser);
6315 /* We're done with the do-statement. */
6316 finish_do_stmt (expression, statement);
6317 /* Look for the `)'. */
6318 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6319 /* Look for the `;'. */
6320 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6326 tree condition = NULL_TREE;
6327 tree expression = NULL_TREE;
6329 /* Begin the for-statement. */
6330 statement = begin_for_stmt ();
6331 /* Look for the `('. */
6332 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6333 /* Parse the initialization. */
6334 cp_parser_for_init_statement (parser);
6335 finish_for_init_stmt (statement);
6337 /* If there's a condition, process it. */
6338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6339 condition = cp_parser_condition (parser);
6340 finish_for_cond (condition, statement);
6341 /* Look for the `;'. */
6342 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6344 /* If there's an expression, process it. */
6345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6346 expression = cp_parser_expression (parser);
6347 finish_for_expr (expression, statement);
6348 /* Look for the `)'. */
6349 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6351 /* Parse the body of the for-statement. */
6352 parser->in_iteration_statement_p = true;
6353 cp_parser_already_scoped_statement (parser);
6354 parser->in_iteration_statement_p = in_iteration_statement_p;
6356 /* We're done with the for-statement. */
6357 finish_for_stmt (statement);
6362 cp_parser_error (parser, "expected iteration-statement");
6363 statement = error_mark_node;
6370 /* Parse a for-init-statement.
6373 expression-statement
6374 simple-declaration */
6377 cp_parser_for_init_statement (cp_parser* parser)
6379 /* If the next token is a `;', then we have an empty
6380 expression-statement. Grammatically, this is also a
6381 simple-declaration, but an invalid one, because it does not
6382 declare anything. Therefore, if we did not handle this case
6383 specially, we would issue an error message about an invalid
6385 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6387 /* We're going to speculatively look for a declaration, falling back
6388 to an expression, if necessary. */
6389 cp_parser_parse_tentatively (parser);
6390 /* Parse the declaration. */
6391 cp_parser_simple_declaration (parser,
6392 /*function_definition_allowed_p=*/false);
6393 /* If the tentative parse failed, then we shall need to look for an
6394 expression-statement. */
6395 if (cp_parser_parse_definitely (parser))
6399 cp_parser_expression_statement (parser, false);
6402 /* Parse a jump-statement.
6407 return expression [opt] ;
6415 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6418 cp_parser_jump_statement (cp_parser* parser)
6420 tree statement = error_mark_node;
6424 /* Peek at the next token. */
6425 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6427 return error_mark_node;
6429 /* See what kind of keyword it is. */
6430 keyword = token->keyword;
6434 if (!parser->in_switch_statement_p
6435 && !parser->in_iteration_statement_p)
6437 error ("break statement not within loop or switch");
6438 statement = error_mark_node;
6441 statement = finish_break_stmt ();
6442 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6446 if (!parser->in_iteration_statement_p)
6448 error ("continue statement not within a loop");
6449 statement = error_mark_node;
6452 statement = finish_continue_stmt ();
6453 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6460 /* If the next token is a `;', then there is no
6462 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6463 expr = cp_parser_expression (parser);
6466 /* Build the return-statement. */
6467 statement = finish_return_stmt (expr);
6468 /* Look for the final `;'. */
6469 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6474 /* Create the goto-statement. */
6475 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6477 /* Issue a warning about this use of a GNU extension. */
6479 pedwarn ("ISO C++ forbids computed gotos");
6480 /* Consume the '*' token. */
6481 cp_lexer_consume_token (parser->lexer);
6482 /* Parse the dependent expression. */
6483 finish_goto_stmt (cp_parser_expression (parser));
6486 finish_goto_stmt (cp_parser_identifier (parser));
6487 /* Look for the final `;'. */
6488 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6492 cp_parser_error (parser, "expected jump-statement");
6499 /* Parse a declaration-statement.
6501 declaration-statement:
6502 block-declaration */
6505 cp_parser_declaration_statement (cp_parser* parser)
6509 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6510 p = obstack_alloc (&declarator_obstack, 0);
6512 /* Parse the block-declaration. */
6513 cp_parser_block_declaration (parser, /*statement_p=*/true);
6515 /* Free any declarators allocated. */
6516 obstack_free (&declarator_obstack, p);
6518 /* Finish off the statement. */
6522 /* Some dependent statements (like `if (cond) statement'), are
6523 implicitly in their own scope. In other words, if the statement is
6524 a single statement (as opposed to a compound-statement), it is
6525 none-the-less treated as if it were enclosed in braces. Any
6526 declarations appearing in the dependent statement are out of scope
6527 after control passes that point. This function parses a statement,
6528 but ensures that is in its own scope, even if it is not a
6531 Returns the new statement. */
6534 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6538 /* If the token is not a `{', then we must take special action. */
6539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6541 /* Create a compound-statement. */
6542 statement = begin_compound_stmt (0);
6543 /* Parse the dependent-statement. */
6544 cp_parser_statement (parser, false);
6545 /* Finish the dummy compound-statement. */
6546 finish_compound_stmt (statement);
6548 /* Otherwise, we simply parse the statement directly. */
6550 statement = cp_parser_compound_statement (parser, NULL, false);
6552 /* Return the statement. */
6556 /* For some dependent statements (like `while (cond) statement'), we
6557 have already created a scope. Therefore, even if the dependent
6558 statement is a compound-statement, we do not want to create another
6562 cp_parser_already_scoped_statement (cp_parser* parser)
6564 /* If the token is a `{', then we must take special action. */
6565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6566 cp_parser_statement (parser, false);
6569 /* Avoid calling cp_parser_compound_statement, so that we
6570 don't create a new scope. Do everything else by hand. */
6571 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6572 cp_parser_statement_seq_opt (parser, false);
6573 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6577 /* Declarations [gram.dcl.dcl] */
6579 /* Parse an optional declaration-sequence.
6583 declaration-seq declaration */
6586 cp_parser_declaration_seq_opt (cp_parser* parser)
6592 token = cp_lexer_peek_token (parser->lexer);
6594 if (token->type == CPP_CLOSE_BRACE
6595 || token->type == CPP_EOF)
6598 if (token->type == CPP_SEMICOLON)
6600 /* A declaration consisting of a single semicolon is
6601 invalid. Allow it unless we're being pedantic. */
6602 cp_lexer_consume_token (parser->lexer);
6603 if (pedantic && !in_system_header)
6604 pedwarn ("extra %<;%>");
6608 /* If we're entering or exiting a region that's implicitly
6609 extern "C", modify the lang context appropriately. */
6610 if (!parser->implicit_extern_c && token->implicit_extern_c)
6612 push_lang_context (lang_name_c);
6613 parser->implicit_extern_c = true;
6615 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6617 pop_lang_context ();
6618 parser->implicit_extern_c = false;
6621 if (token->type == CPP_PRAGMA)
6623 /* A top-level declaration can consist solely of a #pragma.
6624 A nested declaration cannot, so this is done here and not
6625 in cp_parser_declaration. (A #pragma at block scope is
6626 handled in cp_parser_statement.) */
6627 cp_lexer_handle_pragma (parser->lexer);
6631 /* Parse the declaration itself. */
6632 cp_parser_declaration (parser);
6636 /* Parse a declaration.
6641 template-declaration
6642 explicit-instantiation
6643 explicit-specialization
6644 linkage-specification
6645 namespace-definition
6650 __extension__ declaration */
6653 cp_parser_declaration (cp_parser* parser)
6660 /* Check for the `__extension__' keyword. */
6661 if (cp_parser_extension_opt (parser, &saved_pedantic))
6663 /* Parse the qualified declaration. */
6664 cp_parser_declaration (parser);
6665 /* Restore the PEDANTIC flag. */
6666 pedantic = saved_pedantic;
6671 /* Try to figure out what kind of declaration is present. */
6672 token1 = *cp_lexer_peek_token (parser->lexer);
6674 if (token1.type != CPP_EOF)
6675 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6677 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6678 p = obstack_alloc (&declarator_obstack, 0);
6680 /* If the next token is `extern' and the following token is a string
6681 literal, then we have a linkage specification. */
6682 if (token1.keyword == RID_EXTERN
6683 && cp_parser_is_string_literal (&token2))
6684 cp_parser_linkage_specification (parser);
6685 /* If the next token is `template', then we have either a template
6686 declaration, an explicit instantiation, or an explicit
6688 else if (token1.keyword == RID_TEMPLATE)
6690 /* `template <>' indicates a template specialization. */
6691 if (token2.type == CPP_LESS
6692 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6693 cp_parser_explicit_specialization (parser);
6694 /* `template <' indicates a template declaration. */
6695 else if (token2.type == CPP_LESS)
6696 cp_parser_template_declaration (parser, /*member_p=*/false);
6697 /* Anything else must be an explicit instantiation. */
6699 cp_parser_explicit_instantiation (parser);
6701 /* If the next token is `export', then we have a template
6703 else if (token1.keyword == RID_EXPORT)
6704 cp_parser_template_declaration (parser, /*member_p=*/false);
6705 /* If the next token is `extern', 'static' or 'inline' and the one
6706 after that is `template', we have a GNU extended explicit
6707 instantiation directive. */
6708 else if (cp_parser_allow_gnu_extensions_p (parser)
6709 && (token1.keyword == RID_EXTERN
6710 || token1.keyword == RID_STATIC
6711 || token1.keyword == RID_INLINE)
6712 && token2.keyword == RID_TEMPLATE)
6713 cp_parser_explicit_instantiation (parser);
6714 /* If the next token is `namespace', check for a named or unnamed
6715 namespace definition. */
6716 else if (token1.keyword == RID_NAMESPACE
6717 && (/* A named namespace definition. */
6718 (token2.type == CPP_NAME
6719 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6721 /* An unnamed namespace definition. */
6722 || token2.type == CPP_OPEN_BRACE))
6723 cp_parser_namespace_definition (parser);
6724 /* We must have either a block declaration or a function
6727 /* Try to parse a block-declaration, or a function-definition. */
6728 cp_parser_block_declaration (parser, /*statement_p=*/false);
6730 /* Free any declarators allocated. */
6731 obstack_free (&declarator_obstack, p);
6734 /* Parse a block-declaration.
6739 namespace-alias-definition
6746 __extension__ block-declaration
6749 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6750 part of a declaration-statement. */
6753 cp_parser_block_declaration (cp_parser *parser,
6759 /* Check for the `__extension__' keyword. */
6760 if (cp_parser_extension_opt (parser, &saved_pedantic))
6762 /* Parse the qualified declaration. */
6763 cp_parser_block_declaration (parser, statement_p);
6764 /* Restore the PEDANTIC flag. */
6765 pedantic = saved_pedantic;
6770 /* Peek at the next token to figure out which kind of declaration is
6772 token1 = cp_lexer_peek_token (parser->lexer);
6774 /* If the next keyword is `asm', we have an asm-definition. */
6775 if (token1->keyword == RID_ASM)
6778 cp_parser_commit_to_tentative_parse (parser);
6779 cp_parser_asm_definition (parser);
6781 /* If the next keyword is `namespace', we have a
6782 namespace-alias-definition. */
6783 else if (token1->keyword == RID_NAMESPACE)
6784 cp_parser_namespace_alias_definition (parser);
6785 /* If the next keyword is `using', we have either a
6786 using-declaration or a using-directive. */
6787 else if (token1->keyword == RID_USING)
6792 cp_parser_commit_to_tentative_parse (parser);
6793 /* If the token after `using' is `namespace', then we have a
6795 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6796 if (token2->keyword == RID_NAMESPACE)
6797 cp_parser_using_directive (parser);
6798 /* Otherwise, it's a using-declaration. */
6800 cp_parser_using_declaration (parser);
6802 /* If the next keyword is `__label__' we have a label declaration. */
6803 else if (token1->keyword == RID_LABEL)
6806 cp_parser_commit_to_tentative_parse (parser);
6807 cp_parser_label_declaration (parser);
6809 /* Anything else must be a simple-declaration. */
6811 cp_parser_simple_declaration (parser, !statement_p);
6814 /* Parse a simple-declaration.
6817 decl-specifier-seq [opt] init-declarator-list [opt] ;
6819 init-declarator-list:
6821 init-declarator-list , init-declarator
6823 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6824 function-definition as a simple-declaration. */
6827 cp_parser_simple_declaration (cp_parser* parser,
6828 bool function_definition_allowed_p)
6830 cp_decl_specifier_seq decl_specifiers;
6831 int declares_class_or_enum;
6832 bool saw_declarator;
6834 /* Defer access checks until we know what is being declared; the
6835 checks for names appearing in the decl-specifier-seq should be
6836 done as if we were in the scope of the thing being declared. */
6837 push_deferring_access_checks (dk_deferred);
6839 /* Parse the decl-specifier-seq. We have to keep track of whether
6840 or not the decl-specifier-seq declares a named class or
6841 enumeration type, since that is the only case in which the
6842 init-declarator-list is allowed to be empty.
6846 In a simple-declaration, the optional init-declarator-list can be
6847 omitted only when declaring a class or enumeration, that is when
6848 the decl-specifier-seq contains either a class-specifier, an
6849 elaborated-type-specifier, or an enum-specifier. */
6850 cp_parser_decl_specifier_seq (parser,
6851 CP_PARSER_FLAGS_OPTIONAL,
6853 &declares_class_or_enum);
6854 /* We no longer need to defer access checks. */
6855 stop_deferring_access_checks ();
6857 /* In a block scope, a valid declaration must always have a
6858 decl-specifier-seq. By not trying to parse declarators, we can
6859 resolve the declaration/expression ambiguity more quickly. */
6860 if (!function_definition_allowed_p
6861 && !decl_specifiers.any_specifiers_p)
6863 cp_parser_error (parser, "expected declaration");
6867 /* If the next two tokens are both identifiers, the code is
6868 erroneous. The usual cause of this situation is code like:
6872 where "T" should name a type -- but does not. */
6873 if (!decl_specifiers.type
6874 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6876 /* If parsing tentatively, we should commit; we really are
6877 looking at a declaration. */
6878 cp_parser_commit_to_tentative_parse (parser);
6883 /* If we have seen at least one decl-specifier, and the next token
6884 is not a parenthesis, then we must be looking at a declaration.
6885 (After "int (" we might be looking at a functional cast.) */
6886 if (decl_specifiers.any_specifiers_p
6887 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6888 cp_parser_commit_to_tentative_parse (parser);
6890 /* Keep going until we hit the `;' at the end of the simple
6892 saw_declarator = false;
6893 while (cp_lexer_next_token_is_not (parser->lexer,
6897 bool function_definition_p;
6900 saw_declarator = true;
6901 /* Parse the init-declarator. */
6902 decl = cp_parser_init_declarator (parser, &decl_specifiers,
6903 function_definition_allowed_p,
6905 declares_class_or_enum,
6906 &function_definition_p);
6907 /* If an error occurred while parsing tentatively, exit quickly.
6908 (That usually happens when in the body of a function; each
6909 statement is treated as a declaration-statement until proven
6911 if (cp_parser_error_occurred (parser))
6913 /* Handle function definitions specially. */
6914 if (function_definition_p)
6916 /* If the next token is a `,', then we are probably
6917 processing something like:
6921 which is erroneous. */
6922 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6923 error ("mixing declarations and function-definitions is forbidden");
6924 /* Otherwise, we're done with the list of declarators. */
6927 pop_deferring_access_checks ();
6931 /* The next token should be either a `,' or a `;'. */
6932 token = cp_lexer_peek_token (parser->lexer);
6933 /* If it's a `,', there are more declarators to come. */
6934 if (token->type == CPP_COMMA)
6935 cp_lexer_consume_token (parser->lexer);
6936 /* If it's a `;', we are done. */
6937 else if (token->type == CPP_SEMICOLON)
6939 /* Anything else is an error. */
6942 /* If we have already issued an error message we don't need
6943 to issue another one. */
6944 if (decl != error_mark_node
6945 || cp_parser_uncommitted_to_tentative_parse_p (parser))
6946 cp_parser_error (parser, "expected %<,%> or %<;%>");
6947 /* Skip tokens until we reach the end of the statement. */
6948 cp_parser_skip_to_end_of_statement (parser);
6949 /* If the next token is now a `;', consume it. */
6950 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6951 cp_lexer_consume_token (parser->lexer);
6954 /* After the first time around, a function-definition is not
6955 allowed -- even if it was OK at first. For example:
6960 function_definition_allowed_p = false;
6963 /* Issue an error message if no declarators are present, and the
6964 decl-specifier-seq does not itself declare a class or
6966 if (!saw_declarator)
6968 if (cp_parser_declares_only_class_p (parser))
6969 shadow_tag (&decl_specifiers);
6970 /* Perform any deferred access checks. */
6971 perform_deferred_access_checks ();
6974 /* Consume the `;'. */
6975 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6978 pop_deferring_access_checks ();
6981 /* Parse a decl-specifier-seq.
6984 decl-specifier-seq [opt] decl-specifier
6987 storage-class-specifier
6998 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7000 The parser flags FLAGS is used to control type-specifier parsing.
7002 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7005 1: one of the decl-specifiers is an elaborated-type-specifier
7006 (i.e., a type declaration)
7007 2: one of the decl-specifiers is an enum-specifier or a
7008 class-specifier (i.e., a type definition)
7013 cp_parser_decl_specifier_seq (cp_parser* parser,
7014 cp_parser_flags flags,
7015 cp_decl_specifier_seq *decl_specs,
7016 int* declares_class_or_enum)
7018 bool constructor_possible_p = !parser->in_declarator_p;
7020 /* Clear DECL_SPECS. */
7021 clear_decl_specs (decl_specs);
7023 /* Assume no class or enumeration type is declared. */
7024 *declares_class_or_enum = 0;
7026 /* Keep reading specifiers until there are no more to read. */
7030 bool found_decl_spec;
7033 /* Peek at the next token. */
7034 token = cp_lexer_peek_token (parser->lexer);
7035 /* Handle attributes. */
7036 if (token->keyword == RID_ATTRIBUTE)
7038 /* Parse the attributes. */
7039 decl_specs->attributes
7040 = chainon (decl_specs->attributes,
7041 cp_parser_attributes_opt (parser));
7044 /* Assume we will find a decl-specifier keyword. */
7045 found_decl_spec = true;
7046 /* If the next token is an appropriate keyword, we can simply
7047 add it to the list. */
7048 switch (token->keyword)
7053 if (decl_specs->specs[(int) ds_friend]++)
7054 error ("duplicate %<friend%>");
7055 /* Consume the token. */
7056 cp_lexer_consume_token (parser->lexer);
7059 /* function-specifier:
7066 cp_parser_function_specifier_opt (parser, decl_specs);
7072 ++decl_specs->specs[(int) ds_typedef];
7073 /* Consume the token. */
7074 cp_lexer_consume_token (parser->lexer);
7075 /* A constructor declarator cannot appear in a typedef. */
7076 constructor_possible_p = false;
7077 /* The "typedef" keyword can only occur in a declaration; we
7078 may as well commit at this point. */
7079 cp_parser_commit_to_tentative_parse (parser);
7082 /* storage-class-specifier:
7092 /* Consume the token. */
7093 cp_lexer_consume_token (parser->lexer);
7094 cp_parser_set_storage_class (decl_specs, sc_auto);
7097 /* Consume the token. */
7098 cp_lexer_consume_token (parser->lexer);
7099 cp_parser_set_storage_class (decl_specs, sc_register);
7102 /* Consume the token. */
7103 cp_lexer_consume_token (parser->lexer);
7104 if (decl_specs->specs[(int) ds_thread])
7106 error ("%<__thread%> before %<static%>");
7107 decl_specs->specs[(int) ds_thread] = 0;
7109 cp_parser_set_storage_class (decl_specs, sc_static);
7112 /* Consume the token. */
7113 cp_lexer_consume_token (parser->lexer);
7114 if (decl_specs->specs[(int) ds_thread])
7116 error ("%<__thread%> before %<extern%>");
7117 decl_specs->specs[(int) ds_thread] = 0;
7119 cp_parser_set_storage_class (decl_specs, sc_extern);
7122 /* Consume the token. */
7123 cp_lexer_consume_token (parser->lexer);
7124 cp_parser_set_storage_class (decl_specs, sc_mutable);
7127 /* Consume the token. */
7128 cp_lexer_consume_token (parser->lexer);
7129 ++decl_specs->specs[(int) ds_thread];
7133 /* We did not yet find a decl-specifier yet. */
7134 found_decl_spec = false;
7138 /* Constructors are a special case. The `S' in `S()' is not a
7139 decl-specifier; it is the beginning of the declarator. */
7142 && constructor_possible_p
7143 && (cp_parser_constructor_declarator_p
7144 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7146 /* If we don't have a DECL_SPEC yet, then we must be looking at
7147 a type-specifier. */
7148 if (!found_decl_spec && !constructor_p)
7150 int decl_spec_declares_class_or_enum;
7151 bool is_cv_qualifier;
7155 = cp_parser_type_specifier (parser, flags,
7157 /*is_declaration=*/true,
7158 &decl_spec_declares_class_or_enum,
7161 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7163 /* If this type-specifier referenced a user-defined type
7164 (a typedef, class-name, etc.), then we can't allow any
7165 more such type-specifiers henceforth.
7169 The longest sequence of decl-specifiers that could
7170 possibly be a type name is taken as the
7171 decl-specifier-seq of a declaration. The sequence shall
7172 be self-consistent as described below.
7176 As a general rule, at most one type-specifier is allowed
7177 in the complete decl-specifier-seq of a declaration. The
7178 only exceptions are the following:
7180 -- const or volatile can be combined with any other
7183 -- signed or unsigned can be combined with char, long,
7191 void g (const int Pc);
7193 Here, Pc is *not* part of the decl-specifier seq; it's
7194 the declarator. Therefore, once we see a type-specifier
7195 (other than a cv-qualifier), we forbid any additional
7196 user-defined types. We *do* still allow things like `int
7197 int' to be considered a decl-specifier-seq, and issue the
7198 error message later. */
7199 if (type_spec && !is_cv_qualifier)
7200 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7201 /* A constructor declarator cannot follow a type-specifier. */
7204 constructor_possible_p = false;
7205 found_decl_spec = true;
7209 /* If we still do not have a DECL_SPEC, then there are no more
7211 if (!found_decl_spec)
7214 decl_specs->any_specifiers_p = true;
7215 /* After we see one decl-specifier, further decl-specifiers are
7217 flags |= CP_PARSER_FLAGS_OPTIONAL;
7220 /* Don't allow a friend specifier with a class definition. */
7221 if (decl_specs->specs[(int) ds_friend] != 0
7222 && (*declares_class_or_enum & 2))
7223 error ("class definition may not be declared a friend");
7226 /* Parse an (optional) storage-class-specifier.
7228 storage-class-specifier:
7237 storage-class-specifier:
7240 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7243 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7245 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7253 /* Consume the token. */
7254 return cp_lexer_consume_token (parser->lexer)->value;
7261 /* Parse an (optional) function-specifier.
7268 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7269 Updates DECL_SPECS, if it is non-NULL. */
7272 cp_parser_function_specifier_opt (cp_parser* parser,
7273 cp_decl_specifier_seq *decl_specs)
7275 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7279 ++decl_specs->specs[(int) ds_inline];
7284 ++decl_specs->specs[(int) ds_virtual];
7289 ++decl_specs->specs[(int) ds_explicit];
7296 /* Consume the token. */
7297 return cp_lexer_consume_token (parser->lexer)->value;
7300 /* Parse a linkage-specification.
7302 linkage-specification:
7303 extern string-literal { declaration-seq [opt] }
7304 extern string-literal declaration */
7307 cp_parser_linkage_specification (cp_parser* parser)
7311 /* Look for the `extern' keyword. */
7312 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7314 /* Look for the string-literal. */
7315 linkage = cp_parser_string_literal (parser, false, false);
7317 /* Transform the literal into an identifier. If the literal is a
7318 wide-character string, or contains embedded NULs, then we can't
7319 handle it as the user wants. */
7320 if (strlen (TREE_STRING_POINTER (linkage))
7321 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7323 cp_parser_error (parser, "invalid linkage-specification");
7324 /* Assume C++ linkage. */
7325 linkage = lang_name_cplusplus;
7328 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7330 /* We're now using the new linkage. */
7331 push_lang_context (linkage);
7333 /* If the next token is a `{', then we're using the first
7335 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7337 /* Consume the `{' token. */
7338 cp_lexer_consume_token (parser->lexer);
7339 /* Parse the declarations. */
7340 cp_parser_declaration_seq_opt (parser);
7341 /* Look for the closing `}'. */
7342 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7344 /* Otherwise, there's just one declaration. */
7347 bool saved_in_unbraced_linkage_specification_p;
7349 saved_in_unbraced_linkage_specification_p
7350 = parser->in_unbraced_linkage_specification_p;
7351 parser->in_unbraced_linkage_specification_p = true;
7352 have_extern_spec = true;
7353 cp_parser_declaration (parser);
7354 have_extern_spec = false;
7355 parser->in_unbraced_linkage_specification_p
7356 = saved_in_unbraced_linkage_specification_p;
7359 /* We're done with the linkage-specification. */
7360 pop_lang_context ();
7363 /* Special member functions [gram.special] */
7365 /* Parse a conversion-function-id.
7367 conversion-function-id:
7368 operator conversion-type-id
7370 Returns an IDENTIFIER_NODE representing the operator. */
7373 cp_parser_conversion_function_id (cp_parser* parser)
7377 tree saved_qualifying_scope;
7378 tree saved_object_scope;
7381 /* Look for the `operator' token. */
7382 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7383 return error_mark_node;
7384 /* When we parse the conversion-type-id, the current scope will be
7385 reset. However, we need that information in able to look up the
7386 conversion function later, so we save it here. */
7387 saved_scope = parser->scope;
7388 saved_qualifying_scope = parser->qualifying_scope;
7389 saved_object_scope = parser->object_scope;
7390 /* We must enter the scope of the class so that the names of
7391 entities declared within the class are available in the
7392 conversion-type-id. For example, consider:
7399 S::operator I() { ... }
7401 In order to see that `I' is a type-name in the definition, we
7402 must be in the scope of `S'. */
7404 pop_p = push_scope (saved_scope);
7405 /* Parse the conversion-type-id. */
7406 type = cp_parser_conversion_type_id (parser);
7407 /* Leave the scope of the class, if any. */
7409 pop_scope (saved_scope);
7410 /* Restore the saved scope. */
7411 parser->scope = saved_scope;
7412 parser->qualifying_scope = saved_qualifying_scope;
7413 parser->object_scope = saved_object_scope;
7414 /* If the TYPE is invalid, indicate failure. */
7415 if (type == error_mark_node)
7416 return error_mark_node;
7417 return mangle_conv_op_name_for_type (type);
7420 /* Parse a conversion-type-id:
7423 type-specifier-seq conversion-declarator [opt]
7425 Returns the TYPE specified. */
7428 cp_parser_conversion_type_id (cp_parser* parser)
7431 cp_decl_specifier_seq type_specifiers;
7432 cp_declarator *declarator;
7433 tree type_specified;
7435 /* Parse the attributes. */
7436 attributes = cp_parser_attributes_opt (parser);
7437 /* Parse the type-specifiers. */
7438 cp_parser_type_specifier_seq (parser, &type_specifiers);
7439 /* If that didn't work, stop. */
7440 if (type_specifiers.type == error_mark_node)
7441 return error_mark_node;
7442 /* Parse the conversion-declarator. */
7443 declarator = cp_parser_conversion_declarator_opt (parser);
7445 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7446 /*initialized=*/0, &attributes);
7448 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7449 return type_specified;
7452 /* Parse an (optional) conversion-declarator.
7454 conversion-declarator:
7455 ptr-operator conversion-declarator [opt]
7459 static cp_declarator *
7460 cp_parser_conversion_declarator_opt (cp_parser* parser)
7462 enum tree_code code;
7464 cp_cv_quals cv_quals;
7466 /* We don't know if there's a ptr-operator next, or not. */
7467 cp_parser_parse_tentatively (parser);
7468 /* Try the ptr-operator. */
7469 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7470 /* If it worked, look for more conversion-declarators. */
7471 if (cp_parser_parse_definitely (parser))
7473 cp_declarator *declarator;
7475 /* Parse another optional declarator. */
7476 declarator = cp_parser_conversion_declarator_opt (parser);
7478 /* Create the representation of the declarator. */
7480 declarator = make_ptrmem_declarator (cv_quals, class_type,
7482 else if (code == INDIRECT_REF)
7483 declarator = make_pointer_declarator (cv_quals, declarator);
7485 declarator = make_reference_declarator (cv_quals, declarator);
7493 /* Parse an (optional) ctor-initializer.
7496 : mem-initializer-list
7498 Returns TRUE iff the ctor-initializer was actually present. */
7501 cp_parser_ctor_initializer_opt (cp_parser* parser)
7503 /* If the next token is not a `:', then there is no
7504 ctor-initializer. */
7505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7507 /* Do default initialization of any bases and members. */
7508 if (DECL_CONSTRUCTOR_P (current_function_decl))
7509 finish_mem_initializers (NULL_TREE);
7514 /* Consume the `:' token. */
7515 cp_lexer_consume_token (parser->lexer);
7516 /* And the mem-initializer-list. */
7517 cp_parser_mem_initializer_list (parser);
7522 /* Parse a mem-initializer-list.
7524 mem-initializer-list:
7526 mem-initializer , mem-initializer-list */
7529 cp_parser_mem_initializer_list (cp_parser* parser)
7531 tree mem_initializer_list = NULL_TREE;
7533 /* Let the semantic analysis code know that we are starting the
7534 mem-initializer-list. */
7535 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7536 error ("only constructors take base initializers");
7538 /* Loop through the list. */
7541 tree mem_initializer;
7543 /* Parse the mem-initializer. */
7544 mem_initializer = cp_parser_mem_initializer (parser);
7545 /* Add it to the list, unless it was erroneous. */
7546 if (mem_initializer)
7548 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7549 mem_initializer_list = mem_initializer;
7551 /* If the next token is not a `,', we're done. */
7552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7554 /* Consume the `,' token. */
7555 cp_lexer_consume_token (parser->lexer);
7558 /* Perform semantic analysis. */
7559 if (DECL_CONSTRUCTOR_P (current_function_decl))
7560 finish_mem_initializers (mem_initializer_list);
7563 /* Parse a mem-initializer.
7566 mem-initializer-id ( expression-list [opt] )
7571 ( expression-list [opt] )
7573 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7574 class) or FIELD_DECL (for a non-static data member) to initialize;
7575 the TREE_VALUE is the expression-list. */
7578 cp_parser_mem_initializer (cp_parser* parser)
7580 tree mem_initializer_id;
7581 tree expression_list;
7584 /* Find out what is being initialized. */
7585 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7587 pedwarn ("anachronistic old-style base class initializer");
7588 mem_initializer_id = NULL_TREE;
7591 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7592 member = expand_member_init (mem_initializer_id);
7593 if (member && !DECL_P (member))
7594 in_base_initializer = 1;
7597 = cp_parser_parenthesized_expression_list (parser, false,
7598 /*non_constant_p=*/NULL);
7599 if (!expression_list)
7600 expression_list = void_type_node;
7602 in_base_initializer = 0;
7604 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7607 /* Parse a mem-initializer-id.
7610 :: [opt] nested-name-specifier [opt] class-name
7613 Returns a TYPE indicating the class to be initializer for the first
7614 production. Returns an IDENTIFIER_NODE indicating the data member
7615 to be initialized for the second production. */
7618 cp_parser_mem_initializer_id (cp_parser* parser)
7620 bool global_scope_p;
7621 bool nested_name_specifier_p;
7622 bool template_p = false;
7625 /* `typename' is not allowed in this context ([temp.res]). */
7626 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7628 error ("keyword %<typename%> not allowed in this context (a qualified "
7629 "member initializer is implicitly a type)");
7630 cp_lexer_consume_token (parser->lexer);
7632 /* Look for the optional `::' operator. */
7634 = (cp_parser_global_scope_opt (parser,
7635 /*current_scope_valid_p=*/false)
7637 /* Look for the optional nested-name-specifier. The simplest way to
7642 The keyword `typename' is not permitted in a base-specifier or
7643 mem-initializer; in these contexts a qualified name that
7644 depends on a template-parameter is implicitly assumed to be a
7647 is to assume that we have seen the `typename' keyword at this
7649 nested_name_specifier_p
7650 = (cp_parser_nested_name_specifier_opt (parser,
7651 /*typename_keyword_p=*/true,
7652 /*check_dependency_p=*/true,
7654 /*is_declaration=*/true)
7656 if (nested_name_specifier_p)
7657 template_p = cp_parser_optional_template_keyword (parser);
7658 /* If there is a `::' operator or a nested-name-specifier, then we
7659 are definitely looking for a class-name. */
7660 if (global_scope_p || nested_name_specifier_p)
7661 return cp_parser_class_name (parser,
7662 /*typename_keyword_p=*/true,
7663 /*template_keyword_p=*/template_p,
7665 /*check_dependency_p=*/true,
7666 /*class_head_p=*/false,
7667 /*is_declaration=*/true);
7668 /* Otherwise, we could also be looking for an ordinary identifier. */
7669 cp_parser_parse_tentatively (parser);
7670 /* Try a class-name. */
7671 id = cp_parser_class_name (parser,
7672 /*typename_keyword_p=*/true,
7673 /*template_keyword_p=*/false,
7675 /*check_dependency_p=*/true,
7676 /*class_head_p=*/false,
7677 /*is_declaration=*/true);
7678 /* If we found one, we're done. */
7679 if (cp_parser_parse_definitely (parser))
7681 /* Otherwise, look for an ordinary identifier. */
7682 return cp_parser_identifier (parser);
7685 /* Overloading [gram.over] */
7687 /* Parse an operator-function-id.
7689 operator-function-id:
7692 Returns an IDENTIFIER_NODE for the operator which is a
7693 human-readable spelling of the identifier, e.g., `operator +'. */
7696 cp_parser_operator_function_id (cp_parser* parser)
7698 /* Look for the `operator' keyword. */
7699 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7700 return error_mark_node;
7701 /* And then the name of the operator itself. */
7702 return cp_parser_operator (parser);
7705 /* Parse an operator.
7708 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7709 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7710 || ++ -- , ->* -> () []
7717 Returns an IDENTIFIER_NODE for the operator which is a
7718 human-readable spelling of the identifier, e.g., `operator +'. */
7721 cp_parser_operator (cp_parser* parser)
7723 tree id = NULL_TREE;
7726 /* Peek at the next token. */
7727 token = cp_lexer_peek_token (parser->lexer);
7728 /* Figure out which operator we have. */
7729 switch (token->type)
7735 /* The keyword should be either `new' or `delete'. */
7736 if (token->keyword == RID_NEW)
7738 else if (token->keyword == RID_DELETE)
7743 /* Consume the `new' or `delete' token. */
7744 cp_lexer_consume_token (parser->lexer);
7746 /* Peek at the next token. */
7747 token = cp_lexer_peek_token (parser->lexer);
7748 /* If it's a `[' token then this is the array variant of the
7750 if (token->type == CPP_OPEN_SQUARE)
7752 /* Consume the `[' token. */
7753 cp_lexer_consume_token (parser->lexer);
7754 /* Look for the `]' token. */
7755 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7756 id = ansi_opname (op == NEW_EXPR
7757 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7759 /* Otherwise, we have the non-array variant. */
7761 id = ansi_opname (op);
7767 id = ansi_opname (PLUS_EXPR);
7771 id = ansi_opname (MINUS_EXPR);
7775 id = ansi_opname (MULT_EXPR);
7779 id = ansi_opname (TRUNC_DIV_EXPR);
7783 id = ansi_opname (TRUNC_MOD_EXPR);
7787 id = ansi_opname (BIT_XOR_EXPR);
7791 id = ansi_opname (BIT_AND_EXPR);
7795 id = ansi_opname (BIT_IOR_EXPR);
7799 id = ansi_opname (BIT_NOT_EXPR);
7803 id = ansi_opname (TRUTH_NOT_EXPR);
7807 id = ansi_assopname (NOP_EXPR);
7811 id = ansi_opname (LT_EXPR);
7815 id = ansi_opname (GT_EXPR);
7819 id = ansi_assopname (PLUS_EXPR);
7823 id = ansi_assopname (MINUS_EXPR);
7827 id = ansi_assopname (MULT_EXPR);
7831 id = ansi_assopname (TRUNC_DIV_EXPR);
7835 id = ansi_assopname (TRUNC_MOD_EXPR);
7839 id = ansi_assopname (BIT_XOR_EXPR);
7843 id = ansi_assopname (BIT_AND_EXPR);
7847 id = ansi_assopname (BIT_IOR_EXPR);
7851 id = ansi_opname (LSHIFT_EXPR);
7855 id = ansi_opname (RSHIFT_EXPR);
7859 id = ansi_assopname (LSHIFT_EXPR);
7863 id = ansi_assopname (RSHIFT_EXPR);
7867 id = ansi_opname (EQ_EXPR);
7871 id = ansi_opname (NE_EXPR);
7875 id = ansi_opname (LE_EXPR);
7878 case CPP_GREATER_EQ:
7879 id = ansi_opname (GE_EXPR);
7883 id = ansi_opname (TRUTH_ANDIF_EXPR);
7887 id = ansi_opname (TRUTH_ORIF_EXPR);
7891 id = ansi_opname (POSTINCREMENT_EXPR);
7894 case CPP_MINUS_MINUS:
7895 id = ansi_opname (PREDECREMENT_EXPR);
7899 id = ansi_opname (COMPOUND_EXPR);
7902 case CPP_DEREF_STAR:
7903 id = ansi_opname (MEMBER_REF);
7907 id = ansi_opname (COMPONENT_REF);
7910 case CPP_OPEN_PAREN:
7911 /* Consume the `('. */
7912 cp_lexer_consume_token (parser->lexer);
7913 /* Look for the matching `)'. */
7914 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7915 return ansi_opname (CALL_EXPR);
7917 case CPP_OPEN_SQUARE:
7918 /* Consume the `['. */
7919 cp_lexer_consume_token (parser->lexer);
7920 /* Look for the matching `]'. */
7921 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7922 return ansi_opname (ARRAY_REF);
7926 id = ansi_opname (MIN_EXPR);
7930 id = ansi_opname (MAX_EXPR);
7934 id = ansi_assopname (MIN_EXPR);
7938 id = ansi_assopname (MAX_EXPR);
7942 /* Anything else is an error. */
7946 /* If we have selected an identifier, we need to consume the
7949 cp_lexer_consume_token (parser->lexer);
7950 /* Otherwise, no valid operator name was present. */
7953 cp_parser_error (parser, "expected operator");
7954 id = error_mark_node;
7960 /* Parse a template-declaration.
7962 template-declaration:
7963 export [opt] template < template-parameter-list > declaration
7965 If MEMBER_P is TRUE, this template-declaration occurs within a
7968 The grammar rule given by the standard isn't correct. What
7971 template-declaration:
7972 export [opt] template-parameter-list-seq
7973 decl-specifier-seq [opt] init-declarator [opt] ;
7974 export [opt] template-parameter-list-seq
7977 template-parameter-list-seq:
7978 template-parameter-list-seq [opt]
7979 template < template-parameter-list > */
7982 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7984 /* Check for `export'. */
7985 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7987 /* Consume the `export' token. */
7988 cp_lexer_consume_token (parser->lexer);
7989 /* Warn that we do not support `export'. */
7990 warning ("keyword %<export%> not implemented, and will be ignored");
7993 cp_parser_template_declaration_after_export (parser, member_p);
7996 /* Parse a template-parameter-list.
7998 template-parameter-list:
8000 template-parameter-list , template-parameter
8002 Returns a TREE_LIST. Each node represents a template parameter.
8003 The nodes are connected via their TREE_CHAINs. */
8006 cp_parser_template_parameter_list (cp_parser* parser)
8008 tree parameter_list = NULL_TREE;
8016 /* Parse the template-parameter. */
8017 parameter = cp_parser_template_parameter (parser, &is_non_type);
8018 /* Add it to the list. */
8019 parameter_list = process_template_parm (parameter_list,
8022 /* Peek at the next token. */
8023 token = cp_lexer_peek_token (parser->lexer);
8024 /* If it's not a `,', we're done. */
8025 if (token->type != CPP_COMMA)
8027 /* Otherwise, consume the `,' token. */
8028 cp_lexer_consume_token (parser->lexer);
8031 return parameter_list;
8034 /* Parse a template-parameter.
8038 parameter-declaration
8040 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8041 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8042 true iff this parameter is a non-type parameter. */
8045 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8048 cp_parameter_declarator *parameter_declarator;
8050 /* Assume it is a type parameter or a template parameter. */
8051 *is_non_type = false;
8052 /* Peek at the next token. */
8053 token = cp_lexer_peek_token (parser->lexer);
8054 /* If it is `class' or `template', we have a type-parameter. */
8055 if (token->keyword == RID_TEMPLATE)
8056 return cp_parser_type_parameter (parser);
8057 /* If it is `class' or `typename' we do not know yet whether it is a
8058 type parameter or a non-type parameter. Consider:
8060 template <typename T, typename T::X X> ...
8064 template <class C, class D*> ...
8066 Here, the first parameter is a type parameter, and the second is
8067 a non-type parameter. We can tell by looking at the token after
8068 the identifier -- if it is a `,', `=', or `>' then we have a type
8070 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8072 /* Peek at the token after `class' or `typename'. */
8073 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8074 /* If it's an identifier, skip it. */
8075 if (token->type == CPP_NAME)
8076 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8077 /* Now, see if the token looks like the end of a template
8079 if (token->type == CPP_COMMA
8080 || token->type == CPP_EQ
8081 || token->type == CPP_GREATER)
8082 return cp_parser_type_parameter (parser);
8085 /* Otherwise, it is a non-type parameter.
8089 When parsing a default template-argument for a non-type
8090 template-parameter, the first non-nested `>' is taken as the end
8091 of the template parameter-list rather than a greater-than
8093 *is_non_type = true;
8094 parameter_declarator
8095 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8096 /*parenthesized_p=*/NULL);
8097 return (build_tree_list
8098 (parameter_declarator->default_argument,
8099 grokdeclarator (parameter_declarator->declarator,
8100 ¶meter_declarator->decl_specifiers,
8101 PARM, /*initialized=*/0,
8102 /*attrlist=*/NULL)));
8105 /* Parse a type-parameter.
8108 class identifier [opt]
8109 class identifier [opt] = type-id
8110 typename identifier [opt]
8111 typename identifier [opt] = type-id
8112 template < template-parameter-list > class identifier [opt]
8113 template < template-parameter-list > class identifier [opt]
8116 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8117 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8118 the declaration of the parameter. */
8121 cp_parser_type_parameter (cp_parser* parser)
8126 /* Look for a keyword to tell us what kind of parameter this is. */
8127 token = cp_parser_require (parser, CPP_KEYWORD,
8128 "`class', `typename', or `template'");
8130 return error_mark_node;
8132 switch (token->keyword)
8138 tree default_argument;
8140 /* If the next token is an identifier, then it names the
8142 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8143 identifier = cp_parser_identifier (parser);
8145 identifier = NULL_TREE;
8147 /* Create the parameter. */
8148 parameter = finish_template_type_parm (class_type_node, identifier);
8150 /* If the next token is an `=', we have a default argument. */
8151 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8153 /* Consume the `=' token. */
8154 cp_lexer_consume_token (parser->lexer);
8155 /* Parse the default-argument. */
8156 default_argument = cp_parser_type_id (parser);
8159 default_argument = NULL_TREE;
8161 /* Create the combined representation of the parameter and the
8162 default argument. */
8163 parameter = build_tree_list (default_argument, parameter);
8169 tree parameter_list;
8171 tree default_argument;
8173 /* Look for the `<'. */
8174 cp_parser_require (parser, CPP_LESS, "`<'");
8175 /* Parse the template-parameter-list. */
8176 begin_template_parm_list ();
8178 = cp_parser_template_parameter_list (parser);
8179 parameter_list = end_template_parm_list (parameter_list);
8180 /* Look for the `>'. */
8181 cp_parser_require (parser, CPP_GREATER, "`>'");
8182 /* Look for the `class' keyword. */
8183 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8184 /* If the next token is an `=', then there is a
8185 default-argument. If the next token is a `>', we are at
8186 the end of the parameter-list. If the next token is a `,',
8187 then we are at the end of this parameter. */
8188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8189 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8190 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8192 identifier = cp_parser_identifier (parser);
8193 /* Treat invalid names as if the parameter were nameless. */
8194 if (identifier == error_mark_node)
8195 identifier = NULL_TREE;
8198 identifier = NULL_TREE;
8200 /* Create the template parameter. */
8201 parameter = finish_template_template_parm (class_type_node,
8204 /* If the next token is an `=', then there is a
8205 default-argument. */
8206 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8210 /* Consume the `='. */
8211 cp_lexer_consume_token (parser->lexer);
8212 /* Parse the id-expression. */
8214 = cp_parser_id_expression (parser,
8215 /*template_keyword_p=*/false,
8216 /*check_dependency_p=*/true,
8217 /*template_p=*/&is_template,
8218 /*declarator_p=*/false);
8219 if (TREE_CODE (default_argument) == TYPE_DECL)
8220 /* If the id-expression was a template-id that refers to
8221 a template-class, we already have the declaration here,
8222 so no further lookup is needed. */
8225 /* Look up the name. */
8227 = cp_parser_lookup_name (parser, default_argument,
8229 /*is_template=*/is_template,
8230 /*is_namespace=*/false,
8231 /*check_dependency=*/true,
8232 /*ambiguous_p=*/NULL);
8233 /* See if the default argument is valid. */
8235 = check_template_template_default_arg (default_argument);
8238 default_argument = NULL_TREE;
8240 /* Create the combined representation of the parameter and the
8241 default argument. */
8242 parameter = build_tree_list (default_argument, parameter);
8254 /* Parse a template-id.
8257 template-name < template-argument-list [opt] >
8259 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8260 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8261 returned. Otherwise, if the template-name names a function, or set
8262 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8263 names a class, returns a TYPE_DECL for the specialization.
8265 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8266 uninstantiated templates. */
8269 cp_parser_template_id (cp_parser *parser,
8270 bool template_keyword_p,
8271 bool check_dependency_p,
8272 bool is_declaration)
8277 cp_token_position start_of_id = 0;
8278 tree access_check = NULL_TREE;
8279 cp_token *next_token, *next_token_2;
8282 /* If the next token corresponds to a template-id, there is no need
8284 next_token = cp_lexer_peek_token (parser->lexer);
8285 if (next_token->type == CPP_TEMPLATE_ID)
8290 /* Get the stored value. */
8291 value = cp_lexer_consume_token (parser->lexer)->value;
8292 /* Perform any access checks that were deferred. */
8293 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8294 perform_or_defer_access_check (TREE_PURPOSE (check),
8295 TREE_VALUE (check));
8296 /* Return the stored value. */
8297 return TREE_VALUE (value);
8300 /* Avoid performing name lookup if there is no possibility of
8301 finding a template-id. */
8302 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8303 || (next_token->type == CPP_NAME
8304 && !cp_parser_nth_token_starts_template_argument_list_p
8307 cp_parser_error (parser, "expected template-id");
8308 return error_mark_node;
8311 /* Remember where the template-id starts. */
8312 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8313 start_of_id = cp_lexer_token_position (parser->lexer, false);
8315 push_deferring_access_checks (dk_deferred);
8317 /* Parse the template-name. */
8318 is_identifier = false;
8319 template = cp_parser_template_name (parser, template_keyword_p,
8323 if (template == error_mark_node || is_identifier)
8325 pop_deferring_access_checks ();
8329 /* If we find the sequence `[:' after a template-name, it's probably
8330 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8331 parse correctly the argument list. */
8332 next_token = cp_lexer_peek_token (parser->lexer);
8333 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8334 if (next_token->type == CPP_OPEN_SQUARE
8335 && next_token->flags & DIGRAPH
8336 && next_token_2->type == CPP_COLON
8337 && !(next_token_2->flags & PREV_WHITE))
8339 cp_parser_parse_tentatively (parser);
8340 /* Change `:' into `::'. */
8341 next_token_2->type = CPP_SCOPE;
8342 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8344 cp_lexer_consume_token (parser->lexer);
8345 /* Parse the arguments. */
8346 arguments = cp_parser_enclosed_template_argument_list (parser);
8347 if (!cp_parser_parse_definitely (parser))
8349 /* If we couldn't parse an argument list, then we revert our changes
8350 and return simply an error. Maybe this is not a template-id
8352 next_token_2->type = CPP_COLON;
8353 cp_parser_error (parser, "expected %<<%>");
8354 pop_deferring_access_checks ();
8355 return error_mark_node;
8357 /* Otherwise, emit an error about the invalid digraph, but continue
8358 parsing because we got our argument list. */
8359 pedwarn ("%<<::%> cannot begin a template-argument list");
8360 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8361 "between %<<%> and %<::%>");
8362 if (!flag_permissive)
8367 inform ("(if you use -fpermissive G++ will accept your code)");
8374 /* Look for the `<' that starts the template-argument-list. */
8375 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8377 pop_deferring_access_checks ();
8378 return error_mark_node;
8380 /* Parse the arguments. */
8381 arguments = cp_parser_enclosed_template_argument_list (parser);
8384 /* Build a representation of the specialization. */
8385 if (TREE_CODE (template) == IDENTIFIER_NODE)
8386 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8387 else if (DECL_CLASS_TEMPLATE_P (template)
8388 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8390 = finish_template_type (template, arguments,
8391 cp_lexer_next_token_is (parser->lexer,
8395 /* If it's not a class-template or a template-template, it should be
8396 a function-template. */
8397 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8398 || TREE_CODE (template) == OVERLOAD
8399 || BASELINK_P (template)));
8401 template_id = lookup_template_function (template, arguments);
8404 /* Retrieve any deferred checks. Do not pop this access checks yet
8405 so the memory will not be reclaimed during token replacing below. */
8406 access_check = get_deferred_access_checks ();
8408 /* If parsing tentatively, replace the sequence of tokens that makes
8409 up the template-id with a CPP_TEMPLATE_ID token. That way,
8410 should we re-parse the token stream, we will not have to repeat
8411 the effort required to do the parse, nor will we issue duplicate
8412 error messages about problems during instantiation of the
8413 template. Do so only if parsing succeeded, otherwise we may
8414 silently accept template arguments with syntax errors. */
8415 if (start_of_id && !cp_parser_error_occurred (parser))
8417 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8419 /* Reset the contents of the START_OF_ID token. */
8420 token->type = CPP_TEMPLATE_ID;
8421 token->value = build_tree_list (access_check, template_id);
8422 token->keyword = RID_MAX;
8424 /* Purge all subsequent tokens. */
8425 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8428 pop_deferring_access_checks ();
8432 /* Parse a template-name.
8437 The standard should actually say:
8441 operator-function-id
8443 A defect report has been filed about this issue.
8445 A conversion-function-id cannot be a template name because they cannot
8446 be part of a template-id. In fact, looking at this code:
8450 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8451 It is impossible to call a templated conversion-function-id with an
8452 explicit argument list, since the only allowed template parameter is
8453 the type to which it is converting.
8455 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8456 `template' keyword, in a construction like:
8460 In that case `f' is taken to be a template-name, even though there
8461 is no way of knowing for sure.
8463 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8464 name refers to a set of overloaded functions, at least one of which
8465 is a template, or an IDENTIFIER_NODE with the name of the template,
8466 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8467 names are looked up inside uninstantiated templates. */
8470 cp_parser_template_name (cp_parser* parser,
8471 bool template_keyword_p,
8472 bool check_dependency_p,
8473 bool is_declaration,
8474 bool *is_identifier)
8480 /* If the next token is `operator', then we have either an
8481 operator-function-id or a conversion-function-id. */
8482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8484 /* We don't know whether we're looking at an
8485 operator-function-id or a conversion-function-id. */
8486 cp_parser_parse_tentatively (parser);
8487 /* Try an operator-function-id. */
8488 identifier = cp_parser_operator_function_id (parser);
8489 /* If that didn't work, try a conversion-function-id. */
8490 if (!cp_parser_parse_definitely (parser))
8492 cp_parser_error (parser, "expected template-name");
8493 return error_mark_node;
8496 /* Look for the identifier. */
8498 identifier = cp_parser_identifier (parser);
8500 /* If we didn't find an identifier, we don't have a template-id. */
8501 if (identifier == error_mark_node)
8502 return error_mark_node;
8504 /* If the name immediately followed the `template' keyword, then it
8505 is a template-name. However, if the next token is not `<', then
8506 we do not treat it as a template-name, since it is not being used
8507 as part of a template-id. This enables us to handle constructs
8510 template <typename T> struct S { S(); };
8511 template <typename T> S<T>::S();
8513 correctly. We would treat `S' as a template -- if it were `S<T>'
8514 -- but we do not if there is no `<'. */
8516 if (processing_template_decl
8517 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8519 /* In a declaration, in a dependent context, we pretend that the
8520 "template" keyword was present in order to improve error
8521 recovery. For example, given:
8523 template <typename T> void f(T::X<int>);
8525 we want to treat "X<int>" as a template-id. */
8527 && !template_keyword_p
8528 && parser->scope && TYPE_P (parser->scope)
8529 && check_dependency_p
8530 && dependent_type_p (parser->scope)
8531 /* Do not do this for dtors (or ctors), since they never
8532 need the template keyword before their name. */
8533 && !constructor_name_p (identifier, parser->scope))
8535 cp_token_position start = 0;
8537 /* Explain what went wrong. */
8538 error ("non-template %qD used as template", identifier);
8539 inform ("use %<%T::template %D%> to indicate that it is a template",
8540 parser->scope, identifier);
8541 /* If parsing tentatively, find the location of the "<" token. */
8542 if (cp_parser_simulate_error (parser))
8543 start = cp_lexer_token_position (parser->lexer, true);
8544 /* Parse the template arguments so that we can issue error
8545 messages about them. */
8546 cp_lexer_consume_token (parser->lexer);
8547 cp_parser_enclosed_template_argument_list (parser);
8548 /* Skip tokens until we find a good place from which to
8549 continue parsing. */
8550 cp_parser_skip_to_closing_parenthesis (parser,
8551 /*recovering=*/true,
8553 /*consume_paren=*/false);
8554 /* If parsing tentatively, permanently remove the
8555 template argument list. That will prevent duplicate
8556 error messages from being issued about the missing
8557 "template" keyword. */
8559 cp_lexer_purge_tokens_after (parser->lexer, start);
8561 *is_identifier = true;
8565 /* If the "template" keyword is present, then there is generally
8566 no point in doing name-lookup, so we just return IDENTIFIER.
8567 But, if the qualifying scope is non-dependent then we can
8568 (and must) do name-lookup normally. */
8569 if (template_keyword_p
8571 || (TYPE_P (parser->scope)
8572 && dependent_type_p (parser->scope))))
8576 /* Look up the name. */
8577 decl = cp_parser_lookup_name (parser, identifier,
8579 /*is_template=*/false,
8580 /*is_namespace=*/false,
8582 /*ambiguous_p=*/NULL);
8583 decl = maybe_get_template_decl_from_type_decl (decl);
8585 /* If DECL is a template, then the name was a template-name. */
8586 if (TREE_CODE (decl) == TEMPLATE_DECL)
8590 /* The standard does not explicitly indicate whether a name that
8591 names a set of overloaded declarations, some of which are
8592 templates, is a template-name. However, such a name should
8593 be a template-name; otherwise, there is no way to form a
8594 template-id for the overloaded templates. */
8595 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8596 if (TREE_CODE (fns) == OVERLOAD)
8600 for (fn = fns; fn; fn = OVL_NEXT (fn))
8601 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8606 /* Otherwise, the name does not name a template. */
8607 cp_parser_error (parser, "expected template-name");
8608 return error_mark_node;
8612 /* If DECL is dependent, and refers to a function, then just return
8613 its name; we will look it up again during template instantiation. */
8614 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8616 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8617 if (TYPE_P (scope) && dependent_type_p (scope))
8624 /* Parse a template-argument-list.
8626 template-argument-list:
8628 template-argument-list , template-argument
8630 Returns a TREE_VEC containing the arguments. */
8633 cp_parser_template_argument_list (cp_parser* parser)
8635 tree fixed_args[10];
8636 unsigned n_args = 0;
8637 unsigned alloced = 10;
8638 tree *arg_ary = fixed_args;
8640 bool saved_in_template_argument_list_p;
8642 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8643 parser->in_template_argument_list_p = true;
8649 /* Consume the comma. */
8650 cp_lexer_consume_token (parser->lexer);
8652 /* Parse the template-argument. */
8653 argument = cp_parser_template_argument (parser);
8654 if (n_args == alloced)
8658 if (arg_ary == fixed_args)
8660 arg_ary = xmalloc (sizeof (tree) * alloced);
8661 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8664 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8666 arg_ary[n_args++] = argument;
8668 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8670 vec = make_tree_vec (n_args);
8673 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8675 if (arg_ary != fixed_args)
8677 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8681 /* Parse a template-argument.
8684 assignment-expression
8688 The representation is that of an assignment-expression, type-id, or
8689 id-expression -- except that the qualified id-expression is
8690 evaluated, so that the value returned is either a DECL or an
8693 Although the standard says "assignment-expression", it forbids
8694 throw-expressions or assignments in the template argument.
8695 Therefore, we use "conditional-expression" instead. */
8698 cp_parser_template_argument (cp_parser* parser)
8703 bool maybe_type_id = false;
8706 tree qualifying_class;
8708 /* There's really no way to know what we're looking at, so we just
8709 try each alternative in order.
8713 In a template-argument, an ambiguity between a type-id and an
8714 expression is resolved to a type-id, regardless of the form of
8715 the corresponding template-parameter.
8717 Therefore, we try a type-id first. */
8718 cp_parser_parse_tentatively (parser);
8719 argument = cp_parser_type_id (parser);
8720 /* If there was no error parsing the type-id but the next token is a '>>',
8721 we probably found a typo for '> >'. But there are type-id which are
8722 also valid expressions. For instance:
8724 struct X { int operator >> (int); };
8725 template <int V> struct Foo {};
8728 Here 'X()' is a valid type-id of a function type, but the user just
8729 wanted to write the expression "X() >> 5". Thus, we remember that we
8730 found a valid type-id, but we still try to parse the argument as an
8731 expression to see what happens. */
8732 if (!cp_parser_error_occurred (parser)
8733 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8735 maybe_type_id = true;
8736 cp_parser_abort_tentative_parse (parser);
8740 /* If the next token isn't a `,' or a `>', then this argument wasn't
8741 really finished. This means that the argument is not a valid
8743 if (!cp_parser_next_token_ends_template_argument_p (parser))
8744 cp_parser_error (parser, "expected template-argument");
8745 /* If that worked, we're done. */
8746 if (cp_parser_parse_definitely (parser))
8749 /* We're still not sure what the argument will be. */
8750 cp_parser_parse_tentatively (parser);
8751 /* Try a template. */
8752 argument = cp_parser_id_expression (parser,
8753 /*template_keyword_p=*/false,
8754 /*check_dependency_p=*/true,
8756 /*declarator_p=*/false);
8757 /* If the next token isn't a `,' or a `>', then this argument wasn't
8759 if (!cp_parser_next_token_ends_template_argument_p (parser))
8760 cp_parser_error (parser, "expected template-argument");
8761 if (!cp_parser_error_occurred (parser))
8763 /* Figure out what is being referred to. If the id-expression
8764 was for a class template specialization, then we will have a
8765 TYPE_DECL at this point. There is no need to do name lookup
8766 at this point in that case. */
8767 if (TREE_CODE (argument) != TYPE_DECL)
8768 argument = cp_parser_lookup_name (parser, argument,
8770 /*is_template=*/template_p,
8771 /*is_namespace=*/false,
8772 /*check_dependency=*/true,
8773 /*ambiguous_p=*/NULL);
8774 if (TREE_CODE (argument) != TEMPLATE_DECL
8775 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8776 cp_parser_error (parser, "expected template-name");
8778 if (cp_parser_parse_definitely (parser))
8780 /* It must be a non-type argument. There permitted cases are given
8781 in [temp.arg.nontype]:
8783 -- an integral constant-expression of integral or enumeration
8786 -- the name of a non-type template-parameter; or
8788 -- the name of an object or function with external linkage...
8790 -- the address of an object or function with external linkage...
8792 -- a pointer to member... */
8793 /* Look for a non-type template parameter. */
8794 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8796 cp_parser_parse_tentatively (parser);
8797 argument = cp_parser_primary_expression (parser,
8800 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8801 || !cp_parser_next_token_ends_template_argument_p (parser))
8802 cp_parser_simulate_error (parser);
8803 if (cp_parser_parse_definitely (parser))
8807 /* If the next token is "&", the argument must be the address of an
8808 object or function with external linkage. */
8809 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8811 cp_lexer_consume_token (parser->lexer);
8812 /* See if we might have an id-expression. */
8813 token = cp_lexer_peek_token (parser->lexer);
8814 if (token->type == CPP_NAME
8815 || token->keyword == RID_OPERATOR
8816 || token->type == CPP_SCOPE
8817 || token->type == CPP_TEMPLATE_ID
8818 || token->type == CPP_NESTED_NAME_SPECIFIER)
8820 cp_parser_parse_tentatively (parser);
8821 argument = cp_parser_primary_expression (parser,
8824 if (cp_parser_error_occurred (parser)
8825 || !cp_parser_next_token_ends_template_argument_p (parser))
8826 cp_parser_abort_tentative_parse (parser);
8829 if (TREE_CODE (argument) == INDIRECT_REF)
8831 gcc_assert (REFERENCE_REF_P (argument));
8832 argument = TREE_OPERAND (argument, 0);
8835 if (qualifying_class)
8836 argument = finish_qualified_id_expr (qualifying_class,
8840 if (TREE_CODE (argument) == VAR_DECL)
8842 /* A variable without external linkage might still be a
8843 valid constant-expression, so no error is issued here
8844 if the external-linkage check fails. */
8845 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8846 cp_parser_simulate_error (parser);
8848 else if (is_overloaded_fn (argument))
8849 /* All overloaded functions are allowed; if the external
8850 linkage test does not pass, an error will be issued
8854 && (TREE_CODE (argument) == OFFSET_REF
8855 || TREE_CODE (argument) == SCOPE_REF))
8856 /* A pointer-to-member. */
8858 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
8861 cp_parser_simulate_error (parser);
8863 if (cp_parser_parse_definitely (parser))
8866 argument = build_x_unary_op (ADDR_EXPR, argument);
8871 /* If the argument started with "&", there are no other valid
8872 alternatives at this point. */
8875 cp_parser_error (parser, "invalid non-type template argument");
8876 return error_mark_node;
8879 /* If the argument wasn't successfully parsed as a type-id followed
8880 by '>>', the argument can only be a constant expression now.
8881 Otherwise, we try parsing the constant-expression tentatively,
8882 because the argument could really be a type-id. */
8884 cp_parser_parse_tentatively (parser);
8885 argument = cp_parser_constant_expression (parser,
8886 /*allow_non_constant_p=*/false,
8887 /*non_constant_p=*/NULL);
8888 argument = fold_non_dependent_expr (argument);
8891 if (!cp_parser_next_token_ends_template_argument_p (parser))
8892 cp_parser_error (parser, "expected template-argument");
8893 if (cp_parser_parse_definitely (parser))
8895 /* We did our best to parse the argument as a non type-id, but that
8896 was the only alternative that matched (albeit with a '>' after
8897 it). We can assume it's just a typo from the user, and a
8898 diagnostic will then be issued. */
8899 return cp_parser_type_id (parser);
8902 /* Parse an explicit-instantiation.
8904 explicit-instantiation:
8905 template declaration
8907 Although the standard says `declaration', what it really means is:
8909 explicit-instantiation:
8910 template decl-specifier-seq [opt] declarator [opt] ;
8912 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8913 supposed to be allowed. A defect report has been filed about this
8918 explicit-instantiation:
8919 storage-class-specifier template
8920 decl-specifier-seq [opt] declarator [opt] ;
8921 function-specifier template
8922 decl-specifier-seq [opt] declarator [opt] ; */
8925 cp_parser_explicit_instantiation (cp_parser* parser)
8927 int declares_class_or_enum;
8928 cp_decl_specifier_seq decl_specifiers;
8929 tree extension_specifier = NULL_TREE;
8931 /* Look for an (optional) storage-class-specifier or
8932 function-specifier. */
8933 if (cp_parser_allow_gnu_extensions_p (parser))
8936 = cp_parser_storage_class_specifier_opt (parser);
8937 if (!extension_specifier)
8939 = cp_parser_function_specifier_opt (parser,
8940 /*decl_specs=*/NULL);
8943 /* Look for the `template' keyword. */
8944 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8945 /* Let the front end know that we are processing an explicit
8947 begin_explicit_instantiation ();
8948 /* [temp.explicit] says that we are supposed to ignore access
8949 control while processing explicit instantiation directives. */
8950 push_deferring_access_checks (dk_no_check);
8951 /* Parse a decl-specifier-seq. */
8952 cp_parser_decl_specifier_seq (parser,
8953 CP_PARSER_FLAGS_OPTIONAL,
8955 &declares_class_or_enum);
8956 /* If there was exactly one decl-specifier, and it declared a class,
8957 and there's no declarator, then we have an explicit type
8959 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8963 type = check_tag_decl (&decl_specifiers);
8964 /* Turn access control back on for names used during
8965 template instantiation. */
8966 pop_deferring_access_checks ();
8968 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8972 cp_declarator *declarator;
8975 /* Parse the declarator. */
8977 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8978 /*ctor_dtor_or_conv_p=*/NULL,
8979 /*parenthesized_p=*/NULL,
8980 /*member_p=*/false);
8981 if (declares_class_or_enum & 2)
8982 cp_parser_check_for_definition_in_return_type (declarator,
8983 decl_specifiers.type);
8984 if (declarator != cp_error_declarator)
8986 decl = grokdeclarator (declarator, &decl_specifiers,
8988 /* Turn access control back on for names used during
8989 template instantiation. */
8990 pop_deferring_access_checks ();
8991 /* Do the explicit instantiation. */
8992 do_decl_instantiation (decl, extension_specifier);
8996 pop_deferring_access_checks ();
8997 /* Skip the body of the explicit instantiation. */
8998 cp_parser_skip_to_end_of_statement (parser);
9001 /* We're done with the instantiation. */
9002 end_explicit_instantiation ();
9004 cp_parser_consume_semicolon_at_end_of_statement (parser);
9007 /* Parse an explicit-specialization.
9009 explicit-specialization:
9010 template < > declaration
9012 Although the standard says `declaration', what it really means is:
9014 explicit-specialization:
9015 template <> decl-specifier [opt] init-declarator [opt] ;
9016 template <> function-definition
9017 template <> explicit-specialization
9018 template <> template-declaration */
9021 cp_parser_explicit_specialization (cp_parser* parser)
9023 /* Look for the `template' keyword. */
9024 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9025 /* Look for the `<'. */
9026 cp_parser_require (parser, CPP_LESS, "`<'");
9027 /* Look for the `>'. */
9028 cp_parser_require (parser, CPP_GREATER, "`>'");
9029 /* We have processed another parameter list. */
9030 ++parser->num_template_parameter_lists;
9031 /* Let the front end know that we are beginning a specialization. */
9032 begin_specialization ();
9034 /* If the next keyword is `template', we need to figure out whether
9035 or not we're looking a template-declaration. */
9036 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9038 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9039 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9040 cp_parser_template_declaration_after_export (parser,
9041 /*member_p=*/false);
9043 cp_parser_explicit_specialization (parser);
9046 /* Parse the dependent declaration. */
9047 cp_parser_single_declaration (parser,
9051 /* We're done with the specialization. */
9052 end_specialization ();
9053 /* We're done with this parameter list. */
9054 --parser->num_template_parameter_lists;
9057 /* Parse a type-specifier.
9060 simple-type-specifier
9063 elaborated-type-specifier
9071 Returns a representation of the type-specifier. For a
9072 class-specifier, enum-specifier, or elaborated-type-specifier, a
9073 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9075 The parser flags FLAGS is used to control type-specifier parsing.
9077 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9078 in a decl-specifier-seq.
9080 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9081 class-specifier, enum-specifier, or elaborated-type-specifier, then
9082 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9083 if a type is declared; 2 if it is defined. Otherwise, it is set to
9086 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9087 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9091 cp_parser_type_specifier (cp_parser* parser,
9092 cp_parser_flags flags,
9093 cp_decl_specifier_seq *decl_specs,
9094 bool is_declaration,
9095 int* declares_class_or_enum,
9096 bool* is_cv_qualifier)
9098 tree type_spec = NULL_TREE;
9101 cp_decl_spec ds = ds_last;
9103 /* Assume this type-specifier does not declare a new type. */
9104 if (declares_class_or_enum)
9105 *declares_class_or_enum = 0;
9106 /* And that it does not specify a cv-qualifier. */
9107 if (is_cv_qualifier)
9108 *is_cv_qualifier = false;
9109 /* Peek at the next token. */
9110 token = cp_lexer_peek_token (parser->lexer);
9112 /* If we're looking at a keyword, we can use that to guide the
9113 production we choose. */
9114 keyword = token->keyword;
9118 /* 'enum' [identifier] '{' introduces an enum-specifier;
9119 'enum' <anything else> introduces an elaborated-type-specifier. */
9120 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9121 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9122 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9125 if (parser->num_template_parameter_lists)
9127 error ("template declaration of %qs", "enum");
9128 cp_parser_skip_to_end_of_block_or_statement (parser);
9129 type_spec = error_mark_node;
9132 type_spec = cp_parser_enum_specifier (parser);
9134 if (declares_class_or_enum)
9135 *declares_class_or_enum = 2;
9137 cp_parser_set_decl_spec_type (decl_specs,
9139 /*user_defined_p=*/true);
9143 goto elaborated_type_specifier;
9145 /* Any of these indicate either a class-specifier, or an
9146 elaborated-type-specifier. */
9150 /* Parse tentatively so that we can back up if we don't find a
9152 cp_parser_parse_tentatively (parser);
9153 /* Look for the class-specifier. */
9154 type_spec = cp_parser_class_specifier (parser);
9155 /* If that worked, we're done. */
9156 if (cp_parser_parse_definitely (parser))
9158 if (declares_class_or_enum)
9159 *declares_class_or_enum = 2;
9161 cp_parser_set_decl_spec_type (decl_specs,
9163 /*user_defined_p=*/true);
9168 elaborated_type_specifier:
9169 /* We're declaring (not defining) a class or enum. */
9170 if (declares_class_or_enum)
9171 *declares_class_or_enum = 1;
9175 /* Look for an elaborated-type-specifier. */
9177 = (cp_parser_elaborated_type_specifier
9179 decl_specs && decl_specs->specs[(int) ds_friend],
9182 cp_parser_set_decl_spec_type (decl_specs,
9184 /*user_defined_p=*/true);
9189 if (is_cv_qualifier)
9190 *is_cv_qualifier = true;
9195 if (is_cv_qualifier)
9196 *is_cv_qualifier = true;
9201 if (is_cv_qualifier)
9202 *is_cv_qualifier = true;
9206 /* The `__complex__' keyword is a GNU extension. */
9214 /* Handle simple keywords. */
9219 ++decl_specs->specs[(int)ds];
9220 decl_specs->any_specifiers_p = true;
9222 return cp_lexer_consume_token (parser->lexer)->value;
9225 /* If we do not already have a type-specifier, assume we are looking
9226 at a simple-type-specifier. */
9227 type_spec = cp_parser_simple_type_specifier (parser,
9231 /* If we didn't find a type-specifier, and a type-specifier was not
9232 optional in this context, issue an error message. */
9233 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9235 cp_parser_error (parser, "expected type specifier");
9236 return error_mark_node;
9242 /* Parse a simple-type-specifier.
9244 simple-type-specifier:
9245 :: [opt] nested-name-specifier [opt] type-name
9246 :: [opt] nested-name-specifier template template-id
9261 simple-type-specifier:
9262 __typeof__ unary-expression
9263 __typeof__ ( type-id )
9265 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9266 appropriately updated. */
9269 cp_parser_simple_type_specifier (cp_parser* parser,
9270 cp_decl_specifier_seq *decl_specs,
9271 cp_parser_flags flags)
9273 tree type = NULL_TREE;
9276 /* Peek at the next token. */
9277 token = cp_lexer_peek_token (parser->lexer);
9279 /* If we're looking at a keyword, things are easy. */
9280 switch (token->keyword)
9284 decl_specs->explicit_char_p = true;
9285 type = char_type_node;
9288 type = wchar_type_node;
9291 type = boolean_type_node;
9295 ++decl_specs->specs[(int) ds_short];
9296 type = short_integer_type_node;
9300 decl_specs->explicit_int_p = true;
9301 type = integer_type_node;
9305 ++decl_specs->specs[(int) ds_long];
9306 type = long_integer_type_node;
9310 ++decl_specs->specs[(int) ds_signed];
9311 type = integer_type_node;
9315 ++decl_specs->specs[(int) ds_unsigned];
9316 type = unsigned_type_node;
9319 type = float_type_node;
9322 type = double_type_node;
9325 type = void_type_node;
9329 /* Consume the `typeof' token. */
9330 cp_lexer_consume_token (parser->lexer);
9331 /* Parse the operand to `typeof'. */
9332 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9333 /* If it is not already a TYPE, take its type. */
9335 type = finish_typeof (type);
9338 cp_parser_set_decl_spec_type (decl_specs, type,
9339 /*user_defined_p=*/true);
9347 /* If the type-specifier was for a built-in type, we're done. */
9352 /* Record the type. */
9354 && (token->keyword != RID_SIGNED
9355 && token->keyword != RID_UNSIGNED
9356 && token->keyword != RID_SHORT
9357 && token->keyword != RID_LONG))
9358 cp_parser_set_decl_spec_type (decl_specs,
9360 /*user_defined=*/false);
9362 decl_specs->any_specifiers_p = true;
9364 /* Consume the token. */
9365 id = cp_lexer_consume_token (parser->lexer)->value;
9367 /* There is no valid C++ program where a non-template type is
9368 followed by a "<". That usually indicates that the user thought
9369 that the type was a template. */
9370 cp_parser_check_for_invalid_template_id (parser, type);
9372 return TYPE_NAME (type);
9375 /* The type-specifier must be a user-defined type. */
9376 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9381 /* Don't gobble tokens or issue error messages if this is an
9382 optional type-specifier. */
9383 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9384 cp_parser_parse_tentatively (parser);
9386 /* Look for the optional `::' operator. */
9388 = (cp_parser_global_scope_opt (parser,
9389 /*current_scope_valid_p=*/false)
9391 /* Look for the nested-name specifier. */
9393 = (cp_parser_nested_name_specifier_opt (parser,
9394 /*typename_keyword_p=*/false,
9395 /*check_dependency_p=*/true,
9397 /*is_declaration=*/false)
9399 /* If we have seen a nested-name-specifier, and the next token
9400 is `template', then we are using the template-id production. */
9402 && cp_parser_optional_template_keyword (parser))
9404 /* Look for the template-id. */
9405 type = cp_parser_template_id (parser,
9406 /*template_keyword_p=*/true,
9407 /*check_dependency_p=*/true,
9408 /*is_declaration=*/false);
9409 /* If the template-id did not name a type, we are out of
9411 if (TREE_CODE (type) != TYPE_DECL)
9413 cp_parser_error (parser, "expected template-id for type");
9417 /* Otherwise, look for a type-name. */
9419 type = cp_parser_type_name (parser);
9420 /* Keep track of all name-lookups performed in class scopes. */
9424 && TREE_CODE (type) == TYPE_DECL
9425 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9426 maybe_note_name_used_in_class (DECL_NAME (type), type);
9427 /* If it didn't work out, we don't have a TYPE. */
9428 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9429 && !cp_parser_parse_definitely (parser))
9431 if (type && decl_specs)
9432 cp_parser_set_decl_spec_type (decl_specs, type,
9433 /*user_defined=*/true);
9436 /* If we didn't get a type-name, issue an error message. */
9437 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9439 cp_parser_error (parser, "expected type-name");
9440 return error_mark_node;
9443 /* There is no valid C++ program where a non-template type is
9444 followed by a "<". That usually indicates that the user thought
9445 that the type was a template. */
9446 if (type && type != error_mark_node)
9447 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9452 /* Parse a type-name.
9465 Returns a TYPE_DECL for the the type. */
9468 cp_parser_type_name (cp_parser* parser)
9473 /* We can't know yet whether it is a class-name or not. */
9474 cp_parser_parse_tentatively (parser);
9475 /* Try a class-name. */
9476 type_decl = cp_parser_class_name (parser,
9477 /*typename_keyword_p=*/false,
9478 /*template_keyword_p=*/false,
9480 /*check_dependency_p=*/true,
9481 /*class_head_p=*/false,
9482 /*is_declaration=*/false);
9483 /* If it's not a class-name, keep looking. */
9484 if (!cp_parser_parse_definitely (parser))
9486 /* It must be a typedef-name or an enum-name. */
9487 identifier = cp_parser_identifier (parser);
9488 if (identifier == error_mark_node)
9489 return error_mark_node;
9491 /* Look up the type-name. */
9492 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9493 /* Issue an error if we did not find a type-name. */
9494 if (TREE_CODE (type_decl) != TYPE_DECL)
9496 if (!cp_parser_simulate_error (parser))
9497 cp_parser_name_lookup_error (parser, identifier, type_decl,
9499 type_decl = error_mark_node;
9501 /* Remember that the name was used in the definition of the
9502 current class so that we can check later to see if the
9503 meaning would have been different after the class was
9504 entirely defined. */
9505 else if (type_decl != error_mark_node
9507 maybe_note_name_used_in_class (identifier, type_decl);
9514 /* Parse an elaborated-type-specifier. Note that the grammar given
9515 here incorporates the resolution to DR68.
9517 elaborated-type-specifier:
9518 class-key :: [opt] nested-name-specifier [opt] identifier
9519 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9520 enum :: [opt] nested-name-specifier [opt] identifier
9521 typename :: [opt] nested-name-specifier identifier
9522 typename :: [opt] nested-name-specifier template [opt]
9527 elaborated-type-specifier:
9528 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9529 class-key attributes :: [opt] nested-name-specifier [opt]
9530 template [opt] template-id
9531 enum attributes :: [opt] nested-name-specifier [opt] identifier
9533 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9534 declared `friend'. If IS_DECLARATION is TRUE, then this
9535 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9536 something is being declared.
9538 Returns the TYPE specified. */
9541 cp_parser_elaborated_type_specifier (cp_parser* parser,
9543 bool is_declaration)
9545 enum tag_types tag_type;
9547 tree type = NULL_TREE;
9548 tree attributes = NULL_TREE;
9550 /* See if we're looking at the `enum' keyword. */
9551 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9553 /* Consume the `enum' token. */
9554 cp_lexer_consume_token (parser->lexer);
9555 /* Remember that it's an enumeration type. */
9556 tag_type = enum_type;
9557 /* Parse the attributes. */
9558 attributes = cp_parser_attributes_opt (parser);
9560 /* Or, it might be `typename'. */
9561 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9564 /* Consume the `typename' token. */
9565 cp_lexer_consume_token (parser->lexer);
9566 /* Remember that it's a `typename' type. */
9567 tag_type = typename_type;
9568 /* The `typename' keyword is only allowed in templates. */
9569 if (!processing_template_decl)
9570 pedwarn ("using %<typename%> outside of template");
9572 /* Otherwise it must be a class-key. */
9575 tag_type = cp_parser_class_key (parser);
9576 if (tag_type == none_type)
9577 return error_mark_node;
9578 /* Parse the attributes. */
9579 attributes = cp_parser_attributes_opt (parser);
9582 /* Look for the `::' operator. */
9583 cp_parser_global_scope_opt (parser,
9584 /*current_scope_valid_p=*/false);
9585 /* Look for the nested-name-specifier. */
9586 if (tag_type == typename_type)
9588 if (cp_parser_nested_name_specifier (parser,
9589 /*typename_keyword_p=*/true,
9590 /*check_dependency_p=*/true,
9594 return error_mark_node;
9597 /* Even though `typename' is not present, the proposed resolution
9598 to Core Issue 180 says that in `class A<T>::B', `B' should be
9599 considered a type-name, even if `A<T>' is dependent. */
9600 cp_parser_nested_name_specifier_opt (parser,
9601 /*typename_keyword_p=*/true,
9602 /*check_dependency_p=*/true,
9605 /* For everything but enumeration types, consider a template-id. */
9606 if (tag_type != enum_type)
9608 bool template_p = false;
9611 /* Allow the `template' keyword. */
9612 template_p = cp_parser_optional_template_keyword (parser);
9613 /* If we didn't see `template', we don't know if there's a
9614 template-id or not. */
9616 cp_parser_parse_tentatively (parser);
9617 /* Parse the template-id. */
9618 decl = cp_parser_template_id (parser, template_p,
9619 /*check_dependency_p=*/true,
9621 /* If we didn't find a template-id, look for an ordinary
9623 if (!template_p && !cp_parser_parse_definitely (parser))
9625 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9626 in effect, then we must assume that, upon instantiation, the
9627 template will correspond to a class. */
9628 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9629 && tag_type == typename_type)
9630 type = make_typename_type (parser->scope, decl,
9634 type = TREE_TYPE (decl);
9637 /* For an enumeration type, consider only a plain identifier. */
9640 identifier = cp_parser_identifier (parser);
9642 if (identifier == error_mark_node)
9644 parser->scope = NULL_TREE;
9645 return error_mark_node;
9648 /* For a `typename', we needn't call xref_tag. */
9649 if (tag_type == typename_type
9650 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9651 return cp_parser_make_typename_type (parser, parser->scope,
9653 /* Look up a qualified name in the usual way. */
9658 decl = cp_parser_lookup_name (parser, identifier,
9660 /*is_template=*/false,
9661 /*is_namespace=*/false,
9662 /*check_dependency=*/true,
9663 /*ambiguous_p=*/NULL);
9665 /* If we are parsing friend declaration, DECL may be a
9666 TEMPLATE_DECL tree node here. However, we need to check
9667 whether this TEMPLATE_DECL results in valid code. Consider
9668 the following example:
9671 template <class T> class C {};
9674 template <class T> friend class N::C; // #1, valid code
9676 template <class T> class Y {
9677 friend class N::C; // #2, invalid code
9680 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9681 name lookup of `N::C'. We see that friend declaration must
9682 be template for the code to be valid. Note that
9683 processing_template_decl does not work here since it is
9684 always 1 for the above two cases. */
9686 decl = (cp_parser_maybe_treat_template_as_class
9687 (decl, /*tag_name_p=*/is_friend
9688 && parser->num_template_parameter_lists));
9690 if (TREE_CODE (decl) != TYPE_DECL)
9692 cp_parser_diagnose_invalid_type_name (parser,
9695 return error_mark_node;
9698 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9699 check_elaborated_type_specifier
9701 (parser->num_template_parameter_lists
9702 || DECL_SELF_REFERENCE_P (decl)));
9704 type = TREE_TYPE (decl);
9708 /* An elaborated-type-specifier sometimes introduces a new type and
9709 sometimes names an existing type. Normally, the rule is that it
9710 introduces a new type only if there is not an existing type of
9711 the same name already in scope. For example, given:
9714 void f() { struct S s; }
9716 the `struct S' in the body of `f' is the same `struct S' as in
9717 the global scope; the existing definition is used. However, if
9718 there were no global declaration, this would introduce a new
9719 local class named `S'.
9721 An exception to this rule applies to the following code:
9723 namespace N { struct S; }
9725 Here, the elaborated-type-specifier names a new type
9726 unconditionally; even if there is already an `S' in the
9727 containing scope this declaration names a new type.
9728 This exception only applies if the elaborated-type-specifier
9729 forms the complete declaration:
9733 A declaration consisting solely of `class-key identifier ;' is
9734 either a redeclaration of the name in the current scope or a
9735 forward declaration of the identifier as a class name. It
9736 introduces the name into the current scope.
9738 We are in this situation precisely when the next token is a `;'.
9740 An exception to the exception is that a `friend' declaration does
9741 *not* name a new type; i.e., given:
9743 struct S { friend struct T; };
9745 `T' is not a new type in the scope of `S'.
9747 Also, `new struct S' or `sizeof (struct S)' never results in the
9748 definition of a new type; a new type can only be declared in a
9749 declaration context. */
9753 /* Friends have special name lookup rules. */
9754 ts = ts_within_enclosing_non_class;
9755 else if (is_declaration
9756 && cp_lexer_next_token_is (parser->lexer,
9758 /* This is a `class-key identifier ;' */
9763 /* Warn about attributes. They are ignored. */
9765 warning ("type attributes are honored only at type definition");
9767 type = xref_tag (tag_type, identifier, ts,
9768 parser->num_template_parameter_lists);
9771 if (tag_type != enum_type)
9772 cp_parser_check_class_key (tag_type, type);
9774 /* A "<" cannot follow an elaborated type specifier. If that
9775 happens, the user was probably trying to form a template-id. */
9776 cp_parser_check_for_invalid_template_id (parser, type);
9781 /* Parse an enum-specifier.
9784 enum identifier [opt] { enumerator-list [opt] }
9787 enum identifier [opt] { enumerator-list [opt] } attributes
9789 Returns an ENUM_TYPE representing the enumeration. */
9792 cp_parser_enum_specifier (cp_parser* parser)
9797 /* Caller guarantees that the current token is 'enum', an identifier
9798 possibly follows, and the token after that is an opening brace.
9799 If we don't have an identifier, fabricate an anonymous name for
9800 the enumeration being defined. */
9801 cp_lexer_consume_token (parser->lexer);
9803 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9804 identifier = cp_parser_identifier (parser);
9806 identifier = make_anon_name ();
9808 /* Issue an error message if type-definitions are forbidden here. */
9809 cp_parser_check_type_definition (parser);
9811 /* Create the new type. We do this before consuming the opening brace
9812 so the enum will be recorded as being on the line of its tag (or the
9813 'enum' keyword, if there is no tag). */
9814 type = start_enum (identifier);
9816 /* Consume the opening brace. */
9817 cp_lexer_consume_token (parser->lexer);
9819 /* If the next token is not '}', then there are some enumerators. */
9820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9821 cp_parser_enumerator_list (parser, type);
9823 /* Consume the final '}'. */
9824 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9826 /* Look for trailing attributes to apply to this enumeration, and
9827 apply them if appropriate. */
9828 if (cp_parser_allow_gnu_extensions_p (parser))
9830 tree trailing_attr = cp_parser_attributes_opt (parser);
9831 cplus_decl_attributes (&type,
9833 (int) ATTR_FLAG_TYPE_IN_PLACE);
9836 /* Finish up the enumeration. */
9842 /* Parse an enumerator-list. The enumerators all have the indicated
9846 enumerator-definition
9847 enumerator-list , enumerator-definition */
9850 cp_parser_enumerator_list (cp_parser* parser, tree type)
9854 /* Parse an enumerator-definition. */
9855 cp_parser_enumerator_definition (parser, type);
9857 /* If the next token is not a ',', we've reached the end of
9859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9861 /* Otherwise, consume the `,' and keep going. */
9862 cp_lexer_consume_token (parser->lexer);
9863 /* If the next token is a `}', there is a trailing comma. */
9864 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9866 if (pedantic && !in_system_header)
9867 pedwarn ("comma at end of enumerator list");
9873 /* Parse an enumerator-definition. The enumerator has the indicated
9876 enumerator-definition:
9878 enumerator = constant-expression
9884 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9889 /* Look for the identifier. */
9890 identifier = cp_parser_identifier (parser);
9891 if (identifier == error_mark_node)
9894 /* If the next token is an '=', then there is an explicit value. */
9895 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9897 /* Consume the `=' token. */
9898 cp_lexer_consume_token (parser->lexer);
9899 /* Parse the value. */
9900 value = cp_parser_constant_expression (parser,
9901 /*allow_non_constant_p=*/false,
9907 /* Create the enumerator. */
9908 build_enumerator (identifier, value, type);
9911 /* Parse a namespace-name.
9914 original-namespace-name
9917 Returns the NAMESPACE_DECL for the namespace. */
9920 cp_parser_namespace_name (cp_parser* parser)
9923 tree namespace_decl;
9925 /* Get the name of the namespace. */
9926 identifier = cp_parser_identifier (parser);
9927 if (identifier == error_mark_node)
9928 return error_mark_node;
9930 /* Look up the identifier in the currently active scope. Look only
9931 for namespaces, due to:
9935 When looking up a namespace-name in a using-directive or alias
9936 definition, only namespace names are considered.
9942 During the lookup of a name preceding the :: scope resolution
9943 operator, object, function, and enumerator names are ignored.
9945 (Note that cp_parser_class_or_namespace_name only calls this
9946 function if the token after the name is the scope resolution
9948 namespace_decl = cp_parser_lookup_name (parser, identifier,
9950 /*is_template=*/false,
9951 /*is_namespace=*/true,
9952 /*check_dependency=*/true,
9953 /*ambiguous_p=*/NULL);
9954 /* If it's not a namespace, issue an error. */
9955 if (namespace_decl == error_mark_node
9956 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9958 cp_parser_error (parser, "expected namespace-name");
9959 namespace_decl = error_mark_node;
9962 return namespace_decl;
9965 /* Parse a namespace-definition.
9967 namespace-definition:
9968 named-namespace-definition
9969 unnamed-namespace-definition
9971 named-namespace-definition:
9972 original-namespace-definition
9973 extension-namespace-definition
9975 original-namespace-definition:
9976 namespace identifier { namespace-body }
9978 extension-namespace-definition:
9979 namespace original-namespace-name { namespace-body }
9981 unnamed-namespace-definition:
9982 namespace { namespace-body } */
9985 cp_parser_namespace_definition (cp_parser* parser)
9989 /* Look for the `namespace' keyword. */
9990 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9992 /* Get the name of the namespace. We do not attempt to distinguish
9993 between an original-namespace-definition and an
9994 extension-namespace-definition at this point. The semantic
9995 analysis routines are responsible for that. */
9996 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9997 identifier = cp_parser_identifier (parser);
9999 identifier = NULL_TREE;
10001 /* Look for the `{' to start the namespace. */
10002 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10003 /* Start the namespace. */
10004 push_namespace (identifier);
10005 /* Parse the body of the namespace. */
10006 cp_parser_namespace_body (parser);
10007 /* Finish the namespace. */
10009 /* Look for the final `}'. */
10010 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10013 /* Parse a namespace-body.
10016 declaration-seq [opt] */
10019 cp_parser_namespace_body (cp_parser* parser)
10021 cp_parser_declaration_seq_opt (parser);
10024 /* Parse a namespace-alias-definition.
10026 namespace-alias-definition:
10027 namespace identifier = qualified-namespace-specifier ; */
10030 cp_parser_namespace_alias_definition (cp_parser* parser)
10033 tree namespace_specifier;
10035 /* Look for the `namespace' keyword. */
10036 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10037 /* Look for the identifier. */
10038 identifier = cp_parser_identifier (parser);
10039 if (identifier == error_mark_node)
10041 /* Look for the `=' token. */
10042 cp_parser_require (parser, CPP_EQ, "`='");
10043 /* Look for the qualified-namespace-specifier. */
10044 namespace_specifier
10045 = cp_parser_qualified_namespace_specifier (parser);
10046 /* Look for the `;' token. */
10047 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10049 /* Register the alias in the symbol table. */
10050 do_namespace_alias (identifier, namespace_specifier);
10053 /* Parse a qualified-namespace-specifier.
10055 qualified-namespace-specifier:
10056 :: [opt] nested-name-specifier [opt] namespace-name
10058 Returns a NAMESPACE_DECL corresponding to the specified
10062 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10064 /* Look for the optional `::'. */
10065 cp_parser_global_scope_opt (parser,
10066 /*current_scope_valid_p=*/false);
10068 /* Look for the optional nested-name-specifier. */
10069 cp_parser_nested_name_specifier_opt (parser,
10070 /*typename_keyword_p=*/false,
10071 /*check_dependency_p=*/true,
10073 /*is_declaration=*/true);
10075 return cp_parser_namespace_name (parser);
10078 /* Parse a using-declaration.
10081 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10082 using :: unqualified-id ; */
10085 cp_parser_using_declaration (cp_parser* parser)
10088 bool typename_p = false;
10089 bool global_scope_p;
10094 /* Look for the `using' keyword. */
10095 cp_parser_require_keyword (parser, RID_USING, "`using'");
10097 /* Peek at the next token. */
10098 token = cp_lexer_peek_token (parser->lexer);
10099 /* See if it's `typename'. */
10100 if (token->keyword == RID_TYPENAME)
10102 /* Remember that we've seen it. */
10104 /* Consume the `typename' token. */
10105 cp_lexer_consume_token (parser->lexer);
10108 /* Look for the optional global scope qualification. */
10110 = (cp_parser_global_scope_opt (parser,
10111 /*current_scope_valid_p=*/false)
10114 /* If we saw `typename', or didn't see `::', then there must be a
10115 nested-name-specifier present. */
10116 if (typename_p || !global_scope_p)
10117 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10118 /*check_dependency_p=*/true,
10120 /*is_declaration=*/true);
10121 /* Otherwise, we could be in either of the two productions. In that
10122 case, treat the nested-name-specifier as optional. */
10124 qscope = cp_parser_nested_name_specifier_opt (parser,
10125 /*typename_keyword_p=*/false,
10126 /*check_dependency_p=*/true,
10128 /*is_declaration=*/true);
10130 qscope = global_namespace;
10132 /* Parse the unqualified-id. */
10133 identifier = cp_parser_unqualified_id (parser,
10134 /*template_keyword_p=*/false,
10135 /*check_dependency_p=*/true,
10136 /*declarator_p=*/true);
10138 /* The function we call to handle a using-declaration is different
10139 depending on what scope we are in. */
10140 if (identifier == error_mark_node)
10142 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10143 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10144 /* [namespace.udecl]
10146 A using declaration shall not name a template-id. */
10147 error ("a template-id may not appear in a using-declaration");
10150 if (at_class_scope_p ())
10152 /* Create the USING_DECL. */
10153 decl = do_class_using_decl (build_nt (SCOPE_REF,
10156 /* Add it to the list of members in this class. */
10157 finish_member_declaration (decl);
10161 decl = cp_parser_lookup_name_simple (parser, identifier);
10162 if (decl == error_mark_node)
10163 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10164 else if (!at_namespace_scope_p ())
10165 do_local_using_decl (decl, qscope, identifier);
10167 do_toplevel_using_decl (decl, qscope, identifier);
10171 /* Look for the final `;'. */
10172 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10175 /* Parse a using-directive.
10178 using namespace :: [opt] nested-name-specifier [opt]
10179 namespace-name ; */
10182 cp_parser_using_directive (cp_parser* parser)
10184 tree namespace_decl;
10187 /* Look for the `using' keyword. */
10188 cp_parser_require_keyword (parser, RID_USING, "`using'");
10189 /* And the `namespace' keyword. */
10190 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10191 /* Look for the optional `::' operator. */
10192 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10193 /* And the optional nested-name-specifier. */
10194 cp_parser_nested_name_specifier_opt (parser,
10195 /*typename_keyword_p=*/false,
10196 /*check_dependency_p=*/true,
10198 /*is_declaration=*/true);
10199 /* Get the namespace being used. */
10200 namespace_decl = cp_parser_namespace_name (parser);
10201 /* And any specified attributes. */
10202 attribs = cp_parser_attributes_opt (parser);
10203 /* Update the symbol table. */
10204 parse_using_directive (namespace_decl, attribs);
10205 /* Look for the final `;'. */
10206 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10209 /* Parse an asm-definition.
10212 asm ( string-literal ) ;
10217 asm volatile [opt] ( string-literal ) ;
10218 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10219 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10220 : asm-operand-list [opt] ) ;
10221 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10222 : asm-operand-list [opt]
10223 : asm-operand-list [opt] ) ; */
10226 cp_parser_asm_definition (cp_parser* parser)
10229 tree outputs = NULL_TREE;
10230 tree inputs = NULL_TREE;
10231 tree clobbers = NULL_TREE;
10233 bool volatile_p = false;
10234 bool extended_p = false;
10236 /* Look for the `asm' keyword. */
10237 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10238 /* See if the next token is `volatile'. */
10239 if (cp_parser_allow_gnu_extensions_p (parser)
10240 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10242 /* Remember that we saw the `volatile' keyword. */
10244 /* Consume the token. */
10245 cp_lexer_consume_token (parser->lexer);
10247 /* Look for the opening `('. */
10248 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10250 /* Look for the string. */
10251 string = cp_parser_string_literal (parser, false, false);
10252 if (string == error_mark_node)
10254 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10255 /*consume_paren=*/true);
10259 /* If we're allowing GNU extensions, check for the extended assembly
10260 syntax. Unfortunately, the `:' tokens need not be separated by
10261 a space in C, and so, for compatibility, we tolerate that here
10262 too. Doing that means that we have to treat the `::' operator as
10264 if (cp_parser_allow_gnu_extensions_p (parser)
10265 && at_function_scope_p ()
10266 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10267 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10269 bool inputs_p = false;
10270 bool clobbers_p = false;
10272 /* The extended syntax was used. */
10275 /* Look for outputs. */
10276 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10278 /* Consume the `:'. */
10279 cp_lexer_consume_token (parser->lexer);
10280 /* Parse the output-operands. */
10281 if (cp_lexer_next_token_is_not (parser->lexer,
10283 && cp_lexer_next_token_is_not (parser->lexer,
10285 && cp_lexer_next_token_is_not (parser->lexer,
10287 outputs = cp_parser_asm_operand_list (parser);
10289 /* If the next token is `::', there are no outputs, and the
10290 next token is the beginning of the inputs. */
10291 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10292 /* The inputs are coming next. */
10295 /* Look for inputs. */
10297 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10299 /* Consume the `:' or `::'. */
10300 cp_lexer_consume_token (parser->lexer);
10301 /* Parse the output-operands. */
10302 if (cp_lexer_next_token_is_not (parser->lexer,
10304 && cp_lexer_next_token_is_not (parser->lexer,
10306 inputs = cp_parser_asm_operand_list (parser);
10308 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10309 /* The clobbers are coming next. */
10312 /* Look for clobbers. */
10314 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10316 /* Consume the `:' or `::'. */
10317 cp_lexer_consume_token (parser->lexer);
10318 /* Parse the clobbers. */
10319 if (cp_lexer_next_token_is_not (parser->lexer,
10321 clobbers = cp_parser_asm_clobber_list (parser);
10324 /* Look for the closing `)'. */
10325 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10326 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10327 /*consume_paren=*/true);
10328 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10330 /* Create the ASM_EXPR. */
10331 if (at_function_scope_p ())
10333 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10335 /* If the extended syntax was not used, mark the ASM_EXPR. */
10338 tree temp = asm_stmt;
10339 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10340 temp = TREE_OPERAND (temp, 0);
10342 ASM_INPUT_P (temp) = 1;
10346 assemble_asm (string);
10349 /* Declarators [gram.dcl.decl] */
10351 /* Parse an init-declarator.
10354 declarator initializer [opt]
10359 declarator asm-specification [opt] attributes [opt] initializer [opt]
10361 function-definition:
10362 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10364 decl-specifier-seq [opt] declarator function-try-block
10368 function-definition:
10369 __extension__ function-definition
10371 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10372 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10373 then this declarator appears in a class scope. The new DECL created
10374 by this declarator is returned.
10376 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10377 for a function-definition here as well. If the declarator is a
10378 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10379 be TRUE upon return. By that point, the function-definition will
10380 have been completely parsed.
10382 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10386 cp_parser_init_declarator (cp_parser* parser,
10387 cp_decl_specifier_seq *decl_specifiers,
10388 bool function_definition_allowed_p,
10390 int declares_class_or_enum,
10391 bool* function_definition_p)
10394 cp_declarator *declarator;
10395 tree prefix_attributes;
10397 tree asm_specification;
10399 tree decl = NULL_TREE;
10401 bool is_initialized;
10402 bool is_parenthesized_init;
10403 bool is_non_constant_init;
10404 int ctor_dtor_or_conv_p;
10406 bool pop_p = false;
10408 /* Gather the attributes that were provided with the
10409 decl-specifiers. */
10410 prefix_attributes = decl_specifiers->attributes;
10412 /* Assume that this is not the declarator for a function
10414 if (function_definition_p)
10415 *function_definition_p = false;
10417 /* Defer access checks while parsing the declarator; we cannot know
10418 what names are accessible until we know what is being
10420 resume_deferring_access_checks ();
10422 /* Parse the declarator. */
10424 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10425 &ctor_dtor_or_conv_p,
10426 /*parenthesized_p=*/NULL,
10427 /*member_p=*/false);
10428 /* Gather up the deferred checks. */
10429 stop_deferring_access_checks ();
10431 /* If the DECLARATOR was erroneous, there's no need to go
10433 if (declarator == cp_error_declarator)
10434 return error_mark_node;
10436 if (declares_class_or_enum & 2)
10437 cp_parser_check_for_definition_in_return_type (declarator,
10438 decl_specifiers->type);
10440 /* Figure out what scope the entity declared by the DECLARATOR is
10441 located in. `grokdeclarator' sometimes changes the scope, so
10442 we compute it now. */
10443 scope = get_scope_of_declarator (declarator);
10445 /* If we're allowing GNU extensions, look for an asm-specification
10447 if (cp_parser_allow_gnu_extensions_p (parser))
10449 /* Look for an asm-specification. */
10450 asm_specification = cp_parser_asm_specification_opt (parser);
10451 /* And attributes. */
10452 attributes = cp_parser_attributes_opt (parser);
10456 asm_specification = NULL_TREE;
10457 attributes = NULL_TREE;
10460 /* Peek at the next token. */
10461 token = cp_lexer_peek_token (parser->lexer);
10462 /* Check to see if the token indicates the start of a
10463 function-definition. */
10464 if (cp_parser_token_starts_function_definition_p (token))
10466 if (!function_definition_allowed_p)
10468 /* If a function-definition should not appear here, issue an
10470 cp_parser_error (parser,
10471 "a function-definition is not allowed here");
10472 return error_mark_node;
10476 /* Neither attributes nor an asm-specification are allowed
10477 on a function-definition. */
10478 if (asm_specification)
10479 error ("an asm-specification is not allowed on a function-definition");
10481 error ("attributes are not allowed on a function-definition");
10482 /* This is a function-definition. */
10483 *function_definition_p = true;
10485 /* Parse the function definition. */
10487 decl = cp_parser_save_member_function_body (parser,
10490 prefix_attributes);
10493 = (cp_parser_function_definition_from_specifiers_and_declarator
10494 (parser, decl_specifiers, prefix_attributes, declarator));
10502 Only in function declarations for constructors, destructors, and
10503 type conversions can the decl-specifier-seq be omitted.
10505 We explicitly postpone this check past the point where we handle
10506 function-definitions because we tolerate function-definitions
10507 that are missing their return types in some modes. */
10508 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10510 cp_parser_error (parser,
10511 "expected constructor, destructor, or type conversion");
10512 return error_mark_node;
10515 /* An `=' or an `(' indicates an initializer. */
10516 is_initialized = (token->type == CPP_EQ
10517 || token->type == CPP_OPEN_PAREN);
10518 /* If the init-declarator isn't initialized and isn't followed by a
10519 `,' or `;', it's not a valid init-declarator. */
10520 if (!is_initialized
10521 && token->type != CPP_COMMA
10522 && token->type != CPP_SEMICOLON)
10524 cp_parser_error (parser, "expected initializer");
10525 return error_mark_node;
10528 /* Because start_decl has side-effects, we should only call it if we
10529 know we're going ahead. By this point, we know that we cannot
10530 possibly be looking at any other construct. */
10531 cp_parser_commit_to_tentative_parse (parser);
10533 /* If the decl specifiers were bad, issue an error now that we're
10534 sure this was intended to be a declarator. Then continue
10535 declaring the variable(s), as int, to try to cut down on further
10537 if (decl_specifiers->any_specifiers_p
10538 && decl_specifiers->type == error_mark_node)
10540 cp_parser_error (parser, "invalid type in declaration");
10541 decl_specifiers->type = integer_type_node;
10544 /* Check to see whether or not this declaration is a friend. */
10545 friend_p = cp_parser_friend_p (decl_specifiers);
10547 /* Check that the number of template-parameter-lists is OK. */
10548 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10549 return error_mark_node;
10551 /* Enter the newly declared entry in the symbol table. If we're
10552 processing a declaration in a class-specifier, we wait until
10553 after processing the initializer. */
10556 if (parser->in_unbraced_linkage_specification_p)
10558 decl_specifiers->storage_class = sc_extern;
10559 have_extern_spec = false;
10561 decl = start_decl (declarator, decl_specifiers,
10562 is_initialized, attributes, prefix_attributes,
10566 /* Enter the SCOPE. That way unqualified names appearing in the
10567 initializer will be looked up in SCOPE. */
10568 pop_p = push_scope (scope);
10570 /* Perform deferred access control checks, now that we know in which
10571 SCOPE the declared entity resides. */
10572 if (!member_p && decl)
10574 tree saved_current_function_decl = NULL_TREE;
10576 /* If the entity being declared is a function, pretend that we
10577 are in its scope. If it is a `friend', it may have access to
10578 things that would not otherwise be accessible. */
10579 if (TREE_CODE (decl) == FUNCTION_DECL)
10581 saved_current_function_decl = current_function_decl;
10582 current_function_decl = decl;
10585 /* Perform the access control checks for the declarator and the
10586 the decl-specifiers. */
10587 perform_deferred_access_checks ();
10589 /* Restore the saved value. */
10590 if (TREE_CODE (decl) == FUNCTION_DECL)
10591 current_function_decl = saved_current_function_decl;
10594 /* Parse the initializer. */
10595 if (is_initialized)
10596 initializer = cp_parser_initializer (parser,
10597 &is_parenthesized_init,
10598 &is_non_constant_init);
10601 initializer = NULL_TREE;
10602 is_parenthesized_init = false;
10603 is_non_constant_init = true;
10606 /* The old parser allows attributes to appear after a parenthesized
10607 initializer. Mark Mitchell proposed removing this functionality
10608 on the GCC mailing lists on 2002-08-13. This parser accepts the
10609 attributes -- but ignores them. */
10610 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10611 if (cp_parser_attributes_opt (parser))
10612 warning ("attributes after parenthesized initializer ignored");
10614 /* For an in-class declaration, use `grokfield' to create the
10623 decl = grokfield (declarator, decl_specifiers,
10624 initializer, /*asmspec=*/NULL_TREE,
10625 /*attributes=*/NULL_TREE);
10626 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10627 cp_parser_save_default_args (parser, decl);
10630 /* Finish processing the declaration. But, skip friend
10632 if (!friend_p && decl && decl != error_mark_node)
10634 cp_finish_decl (decl,
10637 /* If the initializer is in parentheses, then this is
10638 a direct-initialization, which means that an
10639 `explicit' constructor is OK. Otherwise, an
10640 `explicit' constructor cannot be used. */
10641 ((is_parenthesized_init || !is_initialized)
10642 ? 0 : LOOKUP_ONLYCONVERTING));
10644 pop_scope (DECL_CONTEXT (decl));
10647 /* Remember whether or not variables were initialized by
10648 constant-expressions. */
10649 if (decl && TREE_CODE (decl) == VAR_DECL
10650 && is_initialized && !is_non_constant_init)
10651 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10656 /* Parse a declarator.
10660 ptr-operator declarator
10662 abstract-declarator:
10663 ptr-operator abstract-declarator [opt]
10664 direct-abstract-declarator
10669 attributes [opt] direct-declarator
10670 attributes [opt] ptr-operator declarator
10672 abstract-declarator:
10673 attributes [opt] ptr-operator abstract-declarator [opt]
10674 attributes [opt] direct-abstract-declarator
10676 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10677 detect constructor, destructor or conversion operators. It is set
10678 to -1 if the declarator is a name, and +1 if it is a
10679 function. Otherwise it is set to zero. Usually you just want to
10680 test for >0, but internally the negative value is used.
10682 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10683 a decl-specifier-seq unless it declares a constructor, destructor,
10684 or conversion. It might seem that we could check this condition in
10685 semantic analysis, rather than parsing, but that makes it difficult
10686 to handle something like `f()'. We want to notice that there are
10687 no decl-specifiers, and therefore realize that this is an
10688 expression, not a declaration.)
10690 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10691 the declarator is a direct-declarator of the form "(...)".
10693 MEMBER_P is true iff this declarator is a member-declarator. */
10695 static cp_declarator *
10696 cp_parser_declarator (cp_parser* parser,
10697 cp_parser_declarator_kind dcl_kind,
10698 int* ctor_dtor_or_conv_p,
10699 bool* parenthesized_p,
10703 cp_declarator *declarator;
10704 enum tree_code code;
10705 cp_cv_quals cv_quals;
10707 tree attributes = NULL_TREE;
10709 /* Assume this is not a constructor, destructor, or type-conversion
10711 if (ctor_dtor_or_conv_p)
10712 *ctor_dtor_or_conv_p = 0;
10714 if (cp_parser_allow_gnu_extensions_p (parser))
10715 attributes = cp_parser_attributes_opt (parser);
10717 /* Peek at the next token. */
10718 token = cp_lexer_peek_token (parser->lexer);
10720 /* Check for the ptr-operator production. */
10721 cp_parser_parse_tentatively (parser);
10722 /* Parse the ptr-operator. */
10723 code = cp_parser_ptr_operator (parser,
10726 /* If that worked, then we have a ptr-operator. */
10727 if (cp_parser_parse_definitely (parser))
10729 /* If a ptr-operator was found, then this declarator was not
10731 if (parenthesized_p)
10732 *parenthesized_p = true;
10733 /* The dependent declarator is optional if we are parsing an
10734 abstract-declarator. */
10735 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10736 cp_parser_parse_tentatively (parser);
10738 /* Parse the dependent declarator. */
10739 declarator = cp_parser_declarator (parser, dcl_kind,
10740 /*ctor_dtor_or_conv_p=*/NULL,
10741 /*parenthesized_p=*/NULL,
10742 /*member_p=*/false);
10744 /* If we are parsing an abstract-declarator, we must handle the
10745 case where the dependent declarator is absent. */
10746 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10747 && !cp_parser_parse_definitely (parser))
10750 /* Build the representation of the ptr-operator. */
10752 declarator = make_ptrmem_declarator (cv_quals,
10755 else if (code == INDIRECT_REF)
10756 declarator = make_pointer_declarator (cv_quals, declarator);
10758 declarator = make_reference_declarator (cv_quals, declarator);
10760 /* Everything else is a direct-declarator. */
10763 if (parenthesized_p)
10764 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10766 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10767 ctor_dtor_or_conv_p,
10771 if (attributes && declarator != cp_error_declarator)
10772 declarator->attributes = attributes;
10777 /* Parse a direct-declarator or direct-abstract-declarator.
10781 direct-declarator ( parameter-declaration-clause )
10782 cv-qualifier-seq [opt]
10783 exception-specification [opt]
10784 direct-declarator [ constant-expression [opt] ]
10787 direct-abstract-declarator:
10788 direct-abstract-declarator [opt]
10789 ( parameter-declaration-clause )
10790 cv-qualifier-seq [opt]
10791 exception-specification [opt]
10792 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10793 ( abstract-declarator )
10795 Returns a representation of the declarator. DCL_KIND is
10796 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10797 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10798 we are parsing a direct-declarator. It is
10799 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10800 of ambiguity we prefer an abstract declarator, as per
10801 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10802 cp_parser_declarator. */
10804 static cp_declarator *
10805 cp_parser_direct_declarator (cp_parser* parser,
10806 cp_parser_declarator_kind dcl_kind,
10807 int* ctor_dtor_or_conv_p,
10811 cp_declarator *declarator = NULL;
10812 tree scope = NULL_TREE;
10813 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10814 bool saved_in_declarator_p = parser->in_declarator_p;
10816 bool pop_p = false;
10820 /* Peek at the next token. */
10821 token = cp_lexer_peek_token (parser->lexer);
10822 if (token->type == CPP_OPEN_PAREN)
10824 /* This is either a parameter-declaration-clause, or a
10825 parenthesized declarator. When we know we are parsing a
10826 named declarator, it must be a parenthesized declarator
10827 if FIRST is true. For instance, `(int)' is a
10828 parameter-declaration-clause, with an omitted
10829 direct-abstract-declarator. But `((*))', is a
10830 parenthesized abstract declarator. Finally, when T is a
10831 template parameter `(T)' is a
10832 parameter-declaration-clause, and not a parenthesized
10835 We first try and parse a parameter-declaration-clause,
10836 and then try a nested declarator (if FIRST is true).
10838 It is not an error for it not to be a
10839 parameter-declaration-clause, even when FIRST is
10845 The first is the declaration of a function while the
10846 second is a the definition of a variable, including its
10849 Having seen only the parenthesis, we cannot know which of
10850 these two alternatives should be selected. Even more
10851 complex are examples like:
10856 The former is a function-declaration; the latter is a
10857 variable initialization.
10859 Thus again, we try a parameter-declaration-clause, and if
10860 that fails, we back out and return. */
10862 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10864 cp_parameter_declarator *params;
10865 unsigned saved_num_template_parameter_lists;
10867 /* In a member-declarator, the only valid interpretation
10868 of a parenthesis is the start of a
10869 parameter-declaration-clause. (It is invalid to
10870 initialize a static data member with a parenthesized
10871 initializer; only the "=" form of initialization is
10874 cp_parser_parse_tentatively (parser);
10876 /* Consume the `('. */
10877 cp_lexer_consume_token (parser->lexer);
10880 /* If this is going to be an abstract declarator, we're
10881 in a declarator and we can't have default args. */
10882 parser->default_arg_ok_p = false;
10883 parser->in_declarator_p = true;
10886 /* Inside the function parameter list, surrounding
10887 template-parameter-lists do not apply. */
10888 saved_num_template_parameter_lists
10889 = parser->num_template_parameter_lists;
10890 parser->num_template_parameter_lists = 0;
10892 /* Parse the parameter-declaration-clause. */
10893 params = cp_parser_parameter_declaration_clause (parser);
10895 parser->num_template_parameter_lists
10896 = saved_num_template_parameter_lists;
10898 /* If all went well, parse the cv-qualifier-seq and the
10899 exception-specification. */
10900 if (member_p || cp_parser_parse_definitely (parser))
10902 cp_cv_quals cv_quals;
10903 tree exception_specification;
10905 if (ctor_dtor_or_conv_p)
10906 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10908 /* Consume the `)'. */
10909 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10911 /* Parse the cv-qualifier-seq. */
10912 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10913 /* And the exception-specification. */
10914 exception_specification
10915 = cp_parser_exception_specification_opt (parser);
10917 /* Create the function-declarator. */
10918 declarator = make_call_declarator (declarator,
10921 exception_specification);
10922 /* Any subsequent parameter lists are to do with
10923 return type, so are not those of the declared
10925 parser->default_arg_ok_p = false;
10927 /* Repeat the main loop. */
10932 /* If this is the first, we can try a parenthesized
10936 bool saved_in_type_id_in_expr_p;
10938 parser->default_arg_ok_p = saved_default_arg_ok_p;
10939 parser->in_declarator_p = saved_in_declarator_p;
10941 /* Consume the `('. */
10942 cp_lexer_consume_token (parser->lexer);
10943 /* Parse the nested declarator. */
10944 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10945 parser->in_type_id_in_expr_p = true;
10947 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10948 /*parenthesized_p=*/NULL,
10950 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10952 /* Expect a `)'. */
10953 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10954 declarator = cp_error_declarator;
10955 if (declarator == cp_error_declarator)
10958 goto handle_declarator;
10960 /* Otherwise, we must be done. */
10964 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10965 && token->type == CPP_OPEN_SQUARE)
10967 /* Parse an array-declarator. */
10970 if (ctor_dtor_or_conv_p)
10971 *ctor_dtor_or_conv_p = 0;
10974 parser->default_arg_ok_p = false;
10975 parser->in_declarator_p = true;
10976 /* Consume the `['. */
10977 cp_lexer_consume_token (parser->lexer);
10978 /* Peek at the next token. */
10979 token = cp_lexer_peek_token (parser->lexer);
10980 /* If the next token is `]', then there is no
10981 constant-expression. */
10982 if (token->type != CPP_CLOSE_SQUARE)
10984 bool non_constant_p;
10987 = cp_parser_constant_expression (parser,
10988 /*allow_non_constant=*/true,
10990 if (!non_constant_p)
10991 bounds = fold_non_dependent_expr (bounds);
10992 else if (!at_function_scope_p ())
10994 error ("array bound is not an integer constant");
10995 bounds = error_mark_node;
10999 bounds = NULL_TREE;
11000 /* Look for the closing `]'. */
11001 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11003 declarator = cp_error_declarator;
11007 declarator = make_array_declarator (declarator, bounds);
11009 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11013 /* Parse a declarator-id */
11014 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11015 cp_parser_parse_tentatively (parser);
11016 id = cp_parser_declarator_id (parser);
11017 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11019 if (!cp_parser_parse_definitely (parser))
11020 id = error_mark_node;
11021 else if (TREE_CODE (id) != IDENTIFIER_NODE)
11023 cp_parser_error (parser, "expected unqualified-id");
11024 id = error_mark_node;
11028 if (id == error_mark_node)
11030 declarator = cp_error_declarator;
11034 if (TREE_CODE (id) == SCOPE_REF && at_namespace_scope_p ())
11036 tree scope = TREE_OPERAND (id, 0);
11038 /* In the declaration of a member of a template class
11039 outside of the class itself, the SCOPE will sometimes
11040 be a TYPENAME_TYPE. For example, given:
11042 template <typename T>
11043 int S<T>::R::i = 3;
11045 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11046 this context, we must resolve S<T>::R to an ordinary
11047 type, rather than a typename type.
11049 The reason we normally avoid resolving TYPENAME_TYPEs
11050 is that a specialization of `S' might render
11051 `S<T>::R' not a type. However, if `S' is
11052 specialized, then this `i' will not be used, so there
11053 is no harm in resolving the types here. */
11054 if (TREE_CODE (scope) == TYPENAME_TYPE)
11058 /* Resolve the TYPENAME_TYPE. */
11059 type = resolve_typename_type (scope,
11060 /*only_current_p=*/false);
11061 /* If that failed, the declarator is invalid. */
11062 if (type == error_mark_node)
11063 error ("%<%T::%D%> is not a type",
11064 TYPE_CONTEXT (scope),
11065 TYPE_IDENTIFIER (scope));
11066 /* Build a new DECLARATOR. */
11067 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11071 declarator = make_id_declarator (id);
11075 tree unqualified_name;
11077 if (TREE_CODE (id) == SCOPE_REF
11078 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11080 class_type = TREE_OPERAND (id, 0);
11081 unqualified_name = TREE_OPERAND (id, 1);
11085 class_type = current_class_type;
11086 unqualified_name = id;
11091 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11092 declarator->u.id.sfk = sfk_destructor;
11093 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11094 declarator->u.id.sfk = sfk_conversion;
11095 else if (constructor_name_p (unqualified_name,
11097 || (TREE_CODE (unqualified_name) == TYPE_DECL
11098 && same_type_p (TREE_TYPE (unqualified_name),
11100 declarator->u.id.sfk = sfk_constructor;
11102 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11103 *ctor_dtor_or_conv_p = -1;
11104 if (TREE_CODE (id) == SCOPE_REF
11105 && TREE_CODE (unqualified_name) == TYPE_DECL
11106 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11108 error ("invalid use of constructor as a template");
11109 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11110 "the constructor in a qualified name",
11112 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11113 class_type, class_type);
11118 handle_declarator:;
11119 scope = get_scope_of_declarator (declarator);
11121 /* Any names that appear after the declarator-id for a
11122 member are looked up in the containing scope. */
11123 pop_p = push_scope (scope);
11124 parser->in_declarator_p = true;
11125 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11126 || (declarator && declarator->kind == cdk_id))
11127 /* Default args are only allowed on function
11129 parser->default_arg_ok_p = saved_default_arg_ok_p;
11131 parser->default_arg_ok_p = false;
11140 /* For an abstract declarator, we might wind up with nothing at this
11141 point. That's an error; the declarator is not optional. */
11143 cp_parser_error (parser, "expected declarator");
11145 /* If we entered a scope, we must exit it now. */
11149 parser->default_arg_ok_p = saved_default_arg_ok_p;
11150 parser->in_declarator_p = saved_in_declarator_p;
11155 /* Parse a ptr-operator.
11158 * cv-qualifier-seq [opt]
11160 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11165 & cv-qualifier-seq [opt]
11167 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11168 Returns ADDR_EXPR if a reference was used. In the case of a
11169 pointer-to-member, *TYPE is filled in with the TYPE containing the
11170 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11171 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11172 ERROR_MARK if an error occurred. */
11174 static enum tree_code
11175 cp_parser_ptr_operator (cp_parser* parser,
11177 cp_cv_quals *cv_quals)
11179 enum tree_code code = ERROR_MARK;
11182 /* Assume that it's not a pointer-to-member. */
11184 /* And that there are no cv-qualifiers. */
11185 *cv_quals = TYPE_UNQUALIFIED;
11187 /* Peek at the next token. */
11188 token = cp_lexer_peek_token (parser->lexer);
11189 /* If it's a `*' or `&' we have a pointer or reference. */
11190 if (token->type == CPP_MULT || token->type == CPP_AND)
11192 /* Remember which ptr-operator we were processing. */
11193 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11195 /* Consume the `*' or `&'. */
11196 cp_lexer_consume_token (parser->lexer);
11198 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11199 `&', if we are allowing GNU extensions. (The only qualifier
11200 that can legally appear after `&' is `restrict', but that is
11201 enforced during semantic analysis. */
11202 if (code == INDIRECT_REF
11203 || cp_parser_allow_gnu_extensions_p (parser))
11204 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11208 /* Try the pointer-to-member case. */
11209 cp_parser_parse_tentatively (parser);
11210 /* Look for the optional `::' operator. */
11211 cp_parser_global_scope_opt (parser,
11212 /*current_scope_valid_p=*/false);
11213 /* Look for the nested-name specifier. */
11214 cp_parser_nested_name_specifier (parser,
11215 /*typename_keyword_p=*/false,
11216 /*check_dependency_p=*/true,
11218 /*is_declaration=*/false);
11219 /* If we found it, and the next token is a `*', then we are
11220 indeed looking at a pointer-to-member operator. */
11221 if (!cp_parser_error_occurred (parser)
11222 && cp_parser_require (parser, CPP_MULT, "`*'"))
11224 /* The type of which the member is a member is given by the
11226 *type = parser->scope;
11227 /* The next name will not be qualified. */
11228 parser->scope = NULL_TREE;
11229 parser->qualifying_scope = NULL_TREE;
11230 parser->object_scope = NULL_TREE;
11231 /* Indicate that the `*' operator was used. */
11232 code = INDIRECT_REF;
11233 /* Look for the optional cv-qualifier-seq. */
11234 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11236 /* If that didn't work we don't have a ptr-operator. */
11237 if (!cp_parser_parse_definitely (parser))
11238 cp_parser_error (parser, "expected ptr-operator");
11244 /* Parse an (optional) cv-qualifier-seq.
11247 cv-qualifier cv-qualifier-seq [opt]
11258 Returns a bitmask representing the cv-qualifiers. */
11261 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11263 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11268 cp_cv_quals cv_qualifier;
11270 /* Peek at the next token. */
11271 token = cp_lexer_peek_token (parser->lexer);
11272 /* See if it's a cv-qualifier. */
11273 switch (token->keyword)
11276 cv_qualifier = TYPE_QUAL_CONST;
11280 cv_qualifier = TYPE_QUAL_VOLATILE;
11284 cv_qualifier = TYPE_QUAL_RESTRICT;
11288 cv_qualifier = TYPE_UNQUALIFIED;
11295 if (cv_quals & cv_qualifier)
11297 error ("duplicate cv-qualifier");
11298 cp_lexer_purge_token (parser->lexer);
11302 cp_lexer_consume_token (parser->lexer);
11303 cv_quals |= cv_qualifier;
11310 /* Parse a declarator-id.
11314 :: [opt] nested-name-specifier [opt] type-name
11316 In the `id-expression' case, the value returned is as for
11317 cp_parser_id_expression if the id-expression was an unqualified-id.
11318 If the id-expression was a qualified-id, then a SCOPE_REF is
11319 returned. The first operand is the scope (either a NAMESPACE_DECL
11320 or TREE_TYPE), but the second is still just a representation of an
11324 cp_parser_declarator_id (cp_parser* parser)
11326 tree id_expression;
11328 /* The expression must be an id-expression. Assume that qualified
11329 names are the names of types so that:
11332 int S<T>::R::i = 3;
11334 will work; we must treat `S<T>::R' as the name of a type.
11335 Similarly, assume that qualified names are templates, where
11339 int S<T>::R<T>::i = 3;
11342 id_expression = cp_parser_id_expression (parser,
11343 /*template_keyword_p=*/false,
11344 /*check_dependency_p=*/false,
11345 /*template_p=*/NULL,
11346 /*declarator_p=*/true);
11347 /* If the name was qualified, create a SCOPE_REF to represent
11351 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11352 parser->scope = NULL_TREE;
11355 return id_expression;
11358 /* Parse a type-id.
11361 type-specifier-seq abstract-declarator [opt]
11363 Returns the TYPE specified. */
11366 cp_parser_type_id (cp_parser* parser)
11368 cp_decl_specifier_seq type_specifier_seq;
11369 cp_declarator *abstract_declarator;
11371 /* Parse the type-specifier-seq. */
11372 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11373 if (type_specifier_seq.type == error_mark_node)
11374 return error_mark_node;
11376 /* There might or might not be an abstract declarator. */
11377 cp_parser_parse_tentatively (parser);
11378 /* Look for the declarator. */
11379 abstract_declarator
11380 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11381 /*parenthesized_p=*/NULL,
11382 /*member_p=*/false);
11383 /* Check to see if there really was a declarator. */
11384 if (!cp_parser_parse_definitely (parser))
11385 abstract_declarator = NULL;
11387 return groktypename (&type_specifier_seq, abstract_declarator);
11390 /* Parse a type-specifier-seq.
11392 type-specifier-seq:
11393 type-specifier type-specifier-seq [opt]
11397 type-specifier-seq:
11398 attributes type-specifier-seq [opt]
11400 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11403 cp_parser_type_specifier_seq (cp_parser* parser,
11404 cp_decl_specifier_seq *type_specifier_seq)
11406 bool seen_type_specifier = false;
11408 /* Clear the TYPE_SPECIFIER_SEQ. */
11409 clear_decl_specs (type_specifier_seq);
11411 /* Parse the type-specifiers and attributes. */
11414 tree type_specifier;
11416 /* Check for attributes first. */
11417 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11419 type_specifier_seq->attributes =
11420 chainon (type_specifier_seq->attributes,
11421 cp_parser_attributes_opt (parser));
11425 /* Look for the type-specifier. */
11426 type_specifier = cp_parser_type_specifier (parser,
11427 CP_PARSER_FLAGS_OPTIONAL,
11428 type_specifier_seq,
11429 /*is_declaration=*/false,
11432 /* If the first type-specifier could not be found, this is not a
11433 type-specifier-seq at all. */
11434 if (!seen_type_specifier && !type_specifier)
11436 cp_parser_error (parser, "expected type-specifier");
11437 type_specifier_seq->type = error_mark_node;
11440 /* If subsequent type-specifiers could not be found, the
11441 type-specifier-seq is complete. */
11442 else if (seen_type_specifier && !type_specifier)
11445 seen_type_specifier = true;
11451 /* Parse a parameter-declaration-clause.
11453 parameter-declaration-clause:
11454 parameter-declaration-list [opt] ... [opt]
11455 parameter-declaration-list , ...
11457 Returns a representation for the parameter declarations. A return
11458 value of NULL indicates a parameter-declaration-clause consisting
11459 only of an ellipsis. */
11461 static cp_parameter_declarator *
11462 cp_parser_parameter_declaration_clause (cp_parser* parser)
11464 cp_parameter_declarator *parameters;
11469 /* Peek at the next token. */
11470 token = cp_lexer_peek_token (parser->lexer);
11471 /* Check for trivial parameter-declaration-clauses. */
11472 if (token->type == CPP_ELLIPSIS)
11474 /* Consume the `...' token. */
11475 cp_lexer_consume_token (parser->lexer);
11478 else if (token->type == CPP_CLOSE_PAREN)
11479 /* There are no parameters. */
11481 #ifndef NO_IMPLICIT_EXTERN_C
11482 if (in_system_header && current_class_type == NULL
11483 && current_lang_name == lang_name_c)
11487 return no_parameters;
11489 /* Check for `(void)', too, which is a special case. */
11490 else if (token->keyword == RID_VOID
11491 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11492 == CPP_CLOSE_PAREN))
11494 /* Consume the `void' token. */
11495 cp_lexer_consume_token (parser->lexer);
11496 /* There are no parameters. */
11497 return no_parameters;
11500 /* Parse the parameter-declaration-list. */
11501 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11502 /* If a parse error occurred while parsing the
11503 parameter-declaration-list, then the entire
11504 parameter-declaration-clause is erroneous. */
11508 /* Peek at the next token. */
11509 token = cp_lexer_peek_token (parser->lexer);
11510 /* If it's a `,', the clause should terminate with an ellipsis. */
11511 if (token->type == CPP_COMMA)
11513 /* Consume the `,'. */
11514 cp_lexer_consume_token (parser->lexer);
11515 /* Expect an ellipsis. */
11517 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11519 /* It might also be `...' if the optional trailing `,' was
11521 else if (token->type == CPP_ELLIPSIS)
11523 /* Consume the `...' token. */
11524 cp_lexer_consume_token (parser->lexer);
11525 /* And remember that we saw it. */
11529 ellipsis_p = false;
11531 /* Finish the parameter list. */
11532 if (parameters && ellipsis_p)
11533 parameters->ellipsis_p = true;
11538 /* Parse a parameter-declaration-list.
11540 parameter-declaration-list:
11541 parameter-declaration
11542 parameter-declaration-list , parameter-declaration
11544 Returns a representation of the parameter-declaration-list, as for
11545 cp_parser_parameter_declaration_clause. However, the
11546 `void_list_node' is never appended to the list. Upon return,
11547 *IS_ERROR will be true iff an error occurred. */
11549 static cp_parameter_declarator *
11550 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11552 cp_parameter_declarator *parameters = NULL;
11553 cp_parameter_declarator **tail = ¶meters;
11555 /* Assume all will go well. */
11558 /* Look for more parameters. */
11561 cp_parameter_declarator *parameter;
11562 bool parenthesized_p;
11563 /* Parse the parameter. */
11565 = cp_parser_parameter_declaration (parser,
11566 /*template_parm_p=*/false,
11569 /* If a parse error occurred parsing the parameter declaration,
11570 then the entire parameter-declaration-list is erroneous. */
11577 /* Add the new parameter to the list. */
11579 tail = ¶meter->next;
11581 /* Peek at the next token. */
11582 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11583 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11584 /* The parameter-declaration-list is complete. */
11586 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11590 /* Peek at the next token. */
11591 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11592 /* If it's an ellipsis, then the list is complete. */
11593 if (token->type == CPP_ELLIPSIS)
11595 /* Otherwise, there must be more parameters. Consume the
11597 cp_lexer_consume_token (parser->lexer);
11598 /* When parsing something like:
11600 int i(float f, double d)
11602 we can tell after seeing the declaration for "f" that we
11603 are not looking at an initialization of a variable "i",
11604 but rather at the declaration of a function "i".
11606 Due to the fact that the parsing of template arguments
11607 (as specified to a template-id) requires backtracking we
11608 cannot use this technique when inside a template argument
11610 if (!parser->in_template_argument_list_p
11611 && !parser->in_type_id_in_expr_p
11612 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11613 /* However, a parameter-declaration of the form
11614 "foat(f)" (which is a valid declaration of a
11615 parameter "f") can also be interpreted as an
11616 expression (the conversion of "f" to "float"). */
11617 && !parenthesized_p)
11618 cp_parser_commit_to_tentative_parse (parser);
11622 cp_parser_error (parser, "expected %<,%> or %<...%>");
11623 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11624 cp_parser_skip_to_closing_parenthesis (parser,
11625 /*recovering=*/true,
11626 /*or_comma=*/false,
11627 /*consume_paren=*/false);
11635 /* Parse a parameter declaration.
11637 parameter-declaration:
11638 decl-specifier-seq declarator
11639 decl-specifier-seq declarator = assignment-expression
11640 decl-specifier-seq abstract-declarator [opt]
11641 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11643 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11644 declares a template parameter. (In that case, a non-nested `>'
11645 token encountered during the parsing of the assignment-expression
11646 is not interpreted as a greater-than operator.)
11648 Returns a representation of the parameter, or NULL if an error
11649 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11650 true iff the declarator is of the form "(p)". */
11652 static cp_parameter_declarator *
11653 cp_parser_parameter_declaration (cp_parser *parser,
11654 bool template_parm_p,
11655 bool *parenthesized_p)
11657 int declares_class_or_enum;
11658 bool greater_than_is_operator_p;
11659 cp_decl_specifier_seq decl_specifiers;
11660 cp_declarator *declarator;
11661 tree default_argument;
11663 const char *saved_message;
11665 /* In a template parameter, `>' is not an operator.
11669 When parsing a default template-argument for a non-type
11670 template-parameter, the first non-nested `>' is taken as the end
11671 of the template parameter-list rather than a greater-than
11673 greater_than_is_operator_p = !template_parm_p;
11675 /* Type definitions may not appear in parameter types. */
11676 saved_message = parser->type_definition_forbidden_message;
11677 parser->type_definition_forbidden_message
11678 = "types may not be defined in parameter types";
11680 /* Parse the declaration-specifiers. */
11681 cp_parser_decl_specifier_seq (parser,
11682 CP_PARSER_FLAGS_NONE,
11684 &declares_class_or_enum);
11685 /* If an error occurred, there's no reason to attempt to parse the
11686 rest of the declaration. */
11687 if (cp_parser_error_occurred (parser))
11689 parser->type_definition_forbidden_message = saved_message;
11693 /* Peek at the next token. */
11694 token = cp_lexer_peek_token (parser->lexer);
11695 /* If the next token is a `)', `,', `=', `>', or `...', then there
11696 is no declarator. */
11697 if (token->type == CPP_CLOSE_PAREN
11698 || token->type == CPP_COMMA
11699 || token->type == CPP_EQ
11700 || token->type == CPP_ELLIPSIS
11701 || token->type == CPP_GREATER)
11704 if (parenthesized_p)
11705 *parenthesized_p = false;
11707 /* Otherwise, there should be a declarator. */
11710 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11711 parser->default_arg_ok_p = false;
11713 /* After seeing a decl-specifier-seq, if the next token is not a
11714 "(", there is no possibility that the code is a valid
11715 expression. Therefore, if parsing tentatively, we commit at
11717 if (!parser->in_template_argument_list_p
11718 /* In an expression context, having seen:
11722 we cannot be sure whether we are looking at a
11723 function-type (taking a "char" as a parameter) or a cast
11724 of some object of type "char" to "int". */
11725 && !parser->in_type_id_in_expr_p
11726 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11727 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11728 cp_parser_commit_to_tentative_parse (parser);
11729 /* Parse the declarator. */
11730 declarator = cp_parser_declarator (parser,
11731 CP_PARSER_DECLARATOR_EITHER,
11732 /*ctor_dtor_or_conv_p=*/NULL,
11734 /*member_p=*/false);
11735 parser->default_arg_ok_p = saved_default_arg_ok_p;
11736 /* After the declarator, allow more attributes. */
11737 decl_specifiers.attributes
11738 = chainon (decl_specifiers.attributes,
11739 cp_parser_attributes_opt (parser));
11742 /* The restriction on defining new types applies only to the type
11743 of the parameter, not to the default argument. */
11744 parser->type_definition_forbidden_message = saved_message;
11746 /* If the next token is `=', then process a default argument. */
11747 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11749 bool saved_greater_than_is_operator_p;
11750 /* Consume the `='. */
11751 cp_lexer_consume_token (parser->lexer);
11753 /* If we are defining a class, then the tokens that make up the
11754 default argument must be saved and processed later. */
11755 if (!template_parm_p && at_class_scope_p ()
11756 && TYPE_BEING_DEFINED (current_class_type))
11758 unsigned depth = 0;
11759 cp_token *first_token;
11762 /* Add tokens until we have processed the entire default
11763 argument. We add the range [first_token, token). */
11764 first_token = cp_lexer_peek_token (parser->lexer);
11769 /* Peek at the next token. */
11770 token = cp_lexer_peek_token (parser->lexer);
11771 /* What we do depends on what token we have. */
11772 switch (token->type)
11774 /* In valid code, a default argument must be
11775 immediately followed by a `,' `)', or `...'. */
11777 case CPP_CLOSE_PAREN:
11779 /* If we run into a non-nested `;', `}', or `]',
11780 then the code is invalid -- but the default
11781 argument is certainly over. */
11782 case CPP_SEMICOLON:
11783 case CPP_CLOSE_BRACE:
11784 case CPP_CLOSE_SQUARE:
11787 /* Update DEPTH, if necessary. */
11788 else if (token->type == CPP_CLOSE_PAREN
11789 || token->type == CPP_CLOSE_BRACE
11790 || token->type == CPP_CLOSE_SQUARE)
11794 case CPP_OPEN_PAREN:
11795 case CPP_OPEN_SQUARE:
11796 case CPP_OPEN_BRACE:
11801 /* If we see a non-nested `>', and `>' is not an
11802 operator, then it marks the end of the default
11804 if (!depth && !greater_than_is_operator_p)
11808 /* If we run out of tokens, issue an error message. */
11810 error ("file ends in default argument");
11816 /* In these cases, we should look for template-ids.
11817 For example, if the default argument is
11818 `X<int, double>()', we need to do name lookup to
11819 figure out whether or not `X' is a template; if
11820 so, the `,' does not end the default argument.
11822 That is not yet done. */
11829 /* If we've reached the end, stop. */
11833 /* Add the token to the token block. */
11834 token = cp_lexer_consume_token (parser->lexer);
11837 /* Create a DEFAULT_ARG to represented the unparsed default
11839 default_argument = make_node (DEFAULT_ARG);
11840 DEFARG_TOKENS (default_argument)
11841 = cp_token_cache_new (first_token, token);
11843 /* Outside of a class definition, we can just parse the
11844 assignment-expression. */
11847 bool saved_local_variables_forbidden_p;
11849 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11851 saved_greater_than_is_operator_p
11852 = parser->greater_than_is_operator_p;
11853 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11854 /* Local variable names (and the `this' keyword) may not
11855 appear in a default argument. */
11856 saved_local_variables_forbidden_p
11857 = parser->local_variables_forbidden_p;
11858 parser->local_variables_forbidden_p = true;
11859 /* Parse the assignment-expression. */
11860 default_argument = cp_parser_assignment_expression (parser);
11861 /* Restore saved state. */
11862 parser->greater_than_is_operator_p
11863 = saved_greater_than_is_operator_p;
11864 parser->local_variables_forbidden_p
11865 = saved_local_variables_forbidden_p;
11867 if (!parser->default_arg_ok_p)
11869 if (!flag_pedantic_errors)
11870 warning ("deprecated use of default argument for parameter of non-function");
11873 error ("default arguments are only permitted for function parameters");
11874 default_argument = NULL_TREE;
11879 default_argument = NULL_TREE;
11881 return make_parameter_declarator (&decl_specifiers,
11886 /* Parse a function-body.
11889 compound_statement */
11892 cp_parser_function_body (cp_parser *parser)
11894 cp_parser_compound_statement (parser, NULL, false);
11897 /* Parse a ctor-initializer-opt followed by a function-body. Return
11898 true if a ctor-initializer was present. */
11901 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11904 bool ctor_initializer_p;
11906 /* Begin the function body. */
11907 body = begin_function_body ();
11908 /* Parse the optional ctor-initializer. */
11909 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11910 /* Parse the function-body. */
11911 cp_parser_function_body (parser);
11912 /* Finish the function body. */
11913 finish_function_body (body);
11915 return ctor_initializer_p;
11918 /* Parse an initializer.
11921 = initializer-clause
11922 ( expression-list )
11924 Returns a expression representing the initializer. If no
11925 initializer is present, NULL_TREE is returned.
11927 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11928 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11929 set to FALSE if there is no initializer present. If there is an
11930 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11931 is set to true; otherwise it is set to false. */
11934 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11935 bool* non_constant_p)
11940 /* Peek at the next token. */
11941 token = cp_lexer_peek_token (parser->lexer);
11943 /* Let our caller know whether or not this initializer was
11945 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11946 /* Assume that the initializer is constant. */
11947 *non_constant_p = false;
11949 if (token->type == CPP_EQ)
11951 /* Consume the `='. */
11952 cp_lexer_consume_token (parser->lexer);
11953 /* Parse the initializer-clause. */
11954 init = cp_parser_initializer_clause (parser, non_constant_p);
11956 else if (token->type == CPP_OPEN_PAREN)
11957 init = cp_parser_parenthesized_expression_list (parser, false,
11961 /* Anything else is an error. */
11962 cp_parser_error (parser, "expected initializer");
11963 init = error_mark_node;
11969 /* Parse an initializer-clause.
11971 initializer-clause:
11972 assignment-expression
11973 { initializer-list , [opt] }
11976 Returns an expression representing the initializer.
11978 If the `assignment-expression' production is used the value
11979 returned is simply a representation for the expression.
11981 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11982 the elements of the initializer-list (or NULL_TREE, if the last
11983 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11984 NULL_TREE. There is no way to detect whether or not the optional
11985 trailing `,' was provided. NON_CONSTANT_P is as for
11986 cp_parser_initializer. */
11989 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11993 /* If it is not a `{', then we are looking at an
11994 assignment-expression. */
11995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11998 = cp_parser_constant_expression (parser,
11999 /*allow_non_constant_p=*/true,
12001 if (!*non_constant_p)
12002 initializer = fold_non_dependent_expr (initializer);
12006 /* Consume the `{' token. */
12007 cp_lexer_consume_token (parser->lexer);
12008 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12009 initializer = make_node (CONSTRUCTOR);
12010 /* If it's not a `}', then there is a non-trivial initializer. */
12011 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12013 /* Parse the initializer list. */
12014 CONSTRUCTOR_ELTS (initializer)
12015 = cp_parser_initializer_list (parser, non_constant_p);
12016 /* A trailing `,' token is allowed. */
12017 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12018 cp_lexer_consume_token (parser->lexer);
12020 /* Now, there should be a trailing `}'. */
12021 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12024 return initializer;
12027 /* Parse an initializer-list.
12031 initializer-list , initializer-clause
12036 identifier : initializer-clause
12037 initializer-list, identifier : initializer-clause
12039 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12040 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12041 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12042 as for cp_parser_initializer. */
12045 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12047 tree initializers = NULL_TREE;
12049 /* Assume all of the expressions are constant. */
12050 *non_constant_p = false;
12052 /* Parse the rest of the list. */
12058 bool clause_non_constant_p;
12060 /* If the next token is an identifier and the following one is a
12061 colon, we are looking at the GNU designated-initializer
12063 if (cp_parser_allow_gnu_extensions_p (parser)
12064 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12065 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12067 /* Consume the identifier. */
12068 identifier = cp_lexer_consume_token (parser->lexer)->value;
12069 /* Consume the `:'. */
12070 cp_lexer_consume_token (parser->lexer);
12073 identifier = NULL_TREE;
12075 /* Parse the initializer. */
12076 initializer = cp_parser_initializer_clause (parser,
12077 &clause_non_constant_p);
12078 /* If any clause is non-constant, so is the entire initializer. */
12079 if (clause_non_constant_p)
12080 *non_constant_p = true;
12081 /* Add it to the list. */
12082 initializers = tree_cons (identifier, initializer, initializers);
12084 /* If the next token is not a comma, we have reached the end of
12086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12089 /* Peek at the next token. */
12090 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12091 /* If the next token is a `}', then we're still done. An
12092 initializer-clause can have a trailing `,' after the
12093 initializer-list and before the closing `}'. */
12094 if (token->type == CPP_CLOSE_BRACE)
12097 /* Consume the `,' token. */
12098 cp_lexer_consume_token (parser->lexer);
12101 /* The initializers were built up in reverse order, so we need to
12102 reverse them now. */
12103 return nreverse (initializers);
12106 /* Classes [gram.class] */
12108 /* Parse a class-name.
12114 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12115 to indicate that names looked up in dependent types should be
12116 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12117 keyword has been used to indicate that the name that appears next
12118 is a template. TAG_TYPE indicates the explicit tag given before
12119 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12120 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12121 is the class being defined in a class-head.
12123 Returns the TYPE_DECL representing the class. */
12126 cp_parser_class_name (cp_parser *parser,
12127 bool typename_keyword_p,
12128 bool template_keyword_p,
12129 enum tag_types tag_type,
12130 bool check_dependency_p,
12132 bool is_declaration)
12139 /* All class-names start with an identifier. */
12140 token = cp_lexer_peek_token (parser->lexer);
12141 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12143 cp_parser_error (parser, "expected class-name");
12144 return error_mark_node;
12147 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12148 to a template-id, so we save it here. */
12149 scope = parser->scope;
12150 if (scope == error_mark_node)
12151 return error_mark_node;
12153 /* Any name names a type if we're following the `typename' keyword
12154 in a qualified name where the enclosing scope is type-dependent. */
12155 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12156 && dependent_type_p (scope));
12157 /* Handle the common case (an identifier, but not a template-id)
12159 if (token->type == CPP_NAME
12160 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12164 /* Look for the identifier. */
12165 identifier = cp_parser_identifier (parser);
12166 /* If the next token isn't an identifier, we are certainly not
12167 looking at a class-name. */
12168 if (identifier == error_mark_node)
12169 decl = error_mark_node;
12170 /* If we know this is a type-name, there's no need to look it
12172 else if (typename_p)
12176 /* If the next token is a `::', then the name must be a type
12179 [basic.lookup.qual]
12181 During the lookup for a name preceding the :: scope
12182 resolution operator, object, function, and enumerator
12183 names are ignored. */
12184 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12185 tag_type = typename_type;
12186 /* Look up the name. */
12187 decl = cp_parser_lookup_name (parser, identifier,
12189 /*is_template=*/false,
12190 /*is_namespace=*/false,
12191 check_dependency_p,
12192 /*ambiguous_p=*/NULL);
12197 /* Try a template-id. */
12198 decl = cp_parser_template_id (parser, template_keyword_p,
12199 check_dependency_p,
12201 if (decl == error_mark_node)
12202 return error_mark_node;
12205 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12207 /* If this is a typename, create a TYPENAME_TYPE. */
12208 if (typename_p && decl != error_mark_node)
12210 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12211 if (decl != error_mark_node)
12212 decl = TYPE_NAME (decl);
12215 /* Check to see that it is really the name of a class. */
12216 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12217 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12218 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12219 /* Situations like this:
12221 template <typename T> struct A {
12222 typename T::template X<int>::I i;
12225 are problematic. Is `T::template X<int>' a class-name? The
12226 standard does not seem to be definitive, but there is no other
12227 valid interpretation of the following `::'. Therefore, those
12228 names are considered class-names. */
12229 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12230 else if (decl == error_mark_node
12231 || TREE_CODE (decl) != TYPE_DECL
12232 || TREE_TYPE (decl) == error_mark_node
12233 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12235 cp_parser_error (parser, "expected class-name");
12236 return error_mark_node;
12242 /* Parse a class-specifier.
12245 class-head { member-specification [opt] }
12247 Returns the TREE_TYPE representing the class. */
12250 cp_parser_class_specifier (cp_parser* parser)
12254 tree attributes = NULL_TREE;
12255 int has_trailing_semicolon;
12256 bool nested_name_specifier_p;
12257 unsigned saved_num_template_parameter_lists;
12258 tree old_scope = NULL_TREE;
12259 tree scope = NULL_TREE;
12261 push_deferring_access_checks (dk_no_deferred);
12263 /* Parse the class-head. */
12264 type = cp_parser_class_head (parser,
12265 &nested_name_specifier_p,
12267 /* If the class-head was a semantic disaster, skip the entire body
12271 cp_parser_skip_to_end_of_block_or_statement (parser);
12272 pop_deferring_access_checks ();
12273 return error_mark_node;
12276 /* Look for the `{'. */
12277 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12279 pop_deferring_access_checks ();
12280 return error_mark_node;
12283 /* Issue an error message if type-definitions are forbidden here. */
12284 cp_parser_check_type_definition (parser);
12285 /* Remember that we are defining one more class. */
12286 ++parser->num_classes_being_defined;
12287 /* Inside the class, surrounding template-parameter-lists do not
12289 saved_num_template_parameter_lists
12290 = parser->num_template_parameter_lists;
12291 parser->num_template_parameter_lists = 0;
12293 /* Start the class. */
12294 if (nested_name_specifier_p)
12296 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12297 old_scope = push_inner_scope (scope);
12299 type = begin_class_definition (type);
12301 if (type == error_mark_node)
12302 /* If the type is erroneous, skip the entire body of the class. */
12303 cp_parser_skip_to_closing_brace (parser);
12305 /* Parse the member-specification. */
12306 cp_parser_member_specification_opt (parser);
12308 /* Look for the trailing `}'. */
12309 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12310 /* We get better error messages by noticing a common problem: a
12311 missing trailing `;'. */
12312 token = cp_lexer_peek_token (parser->lexer);
12313 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12314 /* Look for trailing attributes to apply to this class. */
12315 if (cp_parser_allow_gnu_extensions_p (parser))
12317 tree sub_attr = cp_parser_attributes_opt (parser);
12318 attributes = chainon (attributes, sub_attr);
12320 if (type != error_mark_node)
12321 type = finish_struct (type, attributes);
12322 if (nested_name_specifier_p)
12323 pop_inner_scope (old_scope, scope);
12324 /* If this class is not itself within the scope of another class,
12325 then we need to parse the bodies of all of the queued function
12326 definitions. Note that the queued functions defined in a class
12327 are not always processed immediately following the
12328 class-specifier for that class. Consider:
12331 struct B { void f() { sizeof (A); } };
12334 If `f' were processed before the processing of `A' were
12335 completed, there would be no way to compute the size of `A'.
12336 Note that the nesting we are interested in here is lexical --
12337 not the semantic nesting given by TYPE_CONTEXT. In particular,
12340 struct A { struct B; };
12341 struct A::B { void f() { } };
12343 there is no need to delay the parsing of `A::B::f'. */
12344 if (--parser->num_classes_being_defined == 0)
12351 /* In a first pass, parse default arguments to the functions.
12352 Then, in a second pass, parse the bodies of the functions.
12353 This two-phased approach handles cases like:
12361 class_type = NULL_TREE;
12363 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12364 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12365 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12366 TREE_PURPOSE (parser->unparsed_functions_queues)
12367 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12369 fn = TREE_VALUE (queue_entry);
12370 /* If there are default arguments that have not yet been processed,
12371 take care of them now. */
12372 if (class_type != TREE_PURPOSE (queue_entry))
12375 pop_scope (class_type);
12376 class_type = TREE_PURPOSE (queue_entry);
12377 pop_p = push_scope (class_type);
12379 /* Make sure that any template parameters are in scope. */
12380 maybe_begin_member_template_processing (fn);
12381 /* Parse the default argument expressions. */
12382 cp_parser_late_parsing_default_args (parser, fn);
12383 /* Remove any template parameters from the symbol table. */
12384 maybe_end_member_template_processing ();
12387 pop_scope (class_type);
12388 /* Now parse the body of the functions. */
12389 for (TREE_VALUE (parser->unparsed_functions_queues)
12390 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12391 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12392 TREE_VALUE (parser->unparsed_functions_queues)
12393 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12395 /* Figure out which function we need to process. */
12396 fn = TREE_VALUE (queue_entry);
12398 /* A hack to prevent garbage collection. */
12401 /* Parse the function. */
12402 cp_parser_late_parsing_for_member (parser, fn);
12407 /* Put back any saved access checks. */
12408 pop_deferring_access_checks ();
12410 /* Restore the count of active template-parameter-lists. */
12411 parser->num_template_parameter_lists
12412 = saved_num_template_parameter_lists;
12417 /* Parse a class-head.
12420 class-key identifier [opt] base-clause [opt]
12421 class-key nested-name-specifier identifier base-clause [opt]
12422 class-key nested-name-specifier [opt] template-id
12426 class-key attributes identifier [opt] base-clause [opt]
12427 class-key attributes nested-name-specifier identifier base-clause [opt]
12428 class-key attributes nested-name-specifier [opt] template-id
12431 Returns the TYPE of the indicated class. Sets
12432 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12433 involving a nested-name-specifier was used, and FALSE otherwise.
12435 Returns error_mark_node if this is not a class-head.
12437 Returns NULL_TREE if the class-head is syntactically valid, but
12438 semantically invalid in a way that means we should skip the entire
12439 body of the class. */
12442 cp_parser_class_head (cp_parser* parser,
12443 bool* nested_name_specifier_p,
12444 tree *attributes_p)
12446 tree nested_name_specifier;
12447 enum tag_types class_key;
12448 tree id = NULL_TREE;
12449 tree type = NULL_TREE;
12451 bool template_id_p = false;
12452 bool qualified_p = false;
12453 bool invalid_nested_name_p = false;
12454 bool invalid_explicit_specialization_p = false;
12455 bool pop_p = false;
12456 unsigned num_templates;
12459 /* Assume no nested-name-specifier will be present. */
12460 *nested_name_specifier_p = false;
12461 /* Assume no template parameter lists will be used in defining the
12465 /* Look for the class-key. */
12466 class_key = cp_parser_class_key (parser);
12467 if (class_key == none_type)
12468 return error_mark_node;
12470 /* Parse the attributes. */
12471 attributes = cp_parser_attributes_opt (parser);
12473 /* If the next token is `::', that is invalid -- but sometimes
12474 people do try to write:
12478 Handle this gracefully by accepting the extra qualifier, and then
12479 issuing an error about it later if this really is a
12480 class-head. If it turns out just to be an elaborated type
12481 specifier, remain silent. */
12482 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12483 qualified_p = true;
12485 push_deferring_access_checks (dk_no_check);
12487 /* Determine the name of the class. Begin by looking for an
12488 optional nested-name-specifier. */
12489 nested_name_specifier
12490 = cp_parser_nested_name_specifier_opt (parser,
12491 /*typename_keyword_p=*/false,
12492 /*check_dependency_p=*/false,
12494 /*is_declaration=*/false);
12495 /* If there was a nested-name-specifier, then there *must* be an
12497 if (nested_name_specifier)
12499 /* Although the grammar says `identifier', it really means
12500 `class-name' or `template-name'. You are only allowed to
12501 define a class that has already been declared with this
12504 The proposed resolution for Core Issue 180 says that whever
12505 you see `class T::X' you should treat `X' as a type-name.
12507 It is OK to define an inaccessible class; for example:
12509 class A { class B; };
12512 We do not know if we will see a class-name, or a
12513 template-name. We look for a class-name first, in case the
12514 class-name is a template-id; if we looked for the
12515 template-name first we would stop after the template-name. */
12516 cp_parser_parse_tentatively (parser);
12517 type = cp_parser_class_name (parser,
12518 /*typename_keyword_p=*/false,
12519 /*template_keyword_p=*/false,
12521 /*check_dependency_p=*/false,
12522 /*class_head_p=*/true,
12523 /*is_declaration=*/false);
12524 /* If that didn't work, ignore the nested-name-specifier. */
12525 if (!cp_parser_parse_definitely (parser))
12527 invalid_nested_name_p = true;
12528 id = cp_parser_identifier (parser);
12529 if (id == error_mark_node)
12532 /* If we could not find a corresponding TYPE, treat this
12533 declaration like an unqualified declaration. */
12534 if (type == error_mark_node)
12535 nested_name_specifier = NULL_TREE;
12536 /* Otherwise, count the number of templates used in TYPE and its
12537 containing scopes. */
12542 for (scope = TREE_TYPE (type);
12543 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12544 scope = (TYPE_P (scope)
12545 ? TYPE_CONTEXT (scope)
12546 : DECL_CONTEXT (scope)))
12548 && CLASS_TYPE_P (scope)
12549 && CLASSTYPE_TEMPLATE_INFO (scope)
12550 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12551 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12555 /* Otherwise, the identifier is optional. */
12558 /* We don't know whether what comes next is a template-id,
12559 an identifier, or nothing at all. */
12560 cp_parser_parse_tentatively (parser);
12561 /* Check for a template-id. */
12562 id = cp_parser_template_id (parser,
12563 /*template_keyword_p=*/false,
12564 /*check_dependency_p=*/true,
12565 /*is_declaration=*/true);
12566 /* If that didn't work, it could still be an identifier. */
12567 if (!cp_parser_parse_definitely (parser))
12569 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12570 id = cp_parser_identifier (parser);
12576 template_id_p = true;
12581 pop_deferring_access_checks ();
12584 cp_parser_check_for_invalid_template_id (parser, id);
12586 /* If it's not a `:' or a `{' then we can't really be looking at a
12587 class-head, since a class-head only appears as part of a
12588 class-specifier. We have to detect this situation before calling
12589 xref_tag, since that has irreversible side-effects. */
12590 if (!cp_parser_next_token_starts_class_definition_p (parser))
12592 cp_parser_error (parser, "expected %<{%> or %<:%>");
12593 return error_mark_node;
12596 /* At this point, we're going ahead with the class-specifier, even
12597 if some other problem occurs. */
12598 cp_parser_commit_to_tentative_parse (parser);
12599 /* Issue the error about the overly-qualified name now. */
12601 cp_parser_error (parser,
12602 "global qualification of class name is invalid");
12603 else if (invalid_nested_name_p)
12604 cp_parser_error (parser,
12605 "qualified name does not name a class");
12606 else if (nested_name_specifier)
12610 /* Reject typedef-names in class heads. */
12611 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12613 error ("invalid class name in declaration of %qD", type);
12618 /* Figure out in what scope the declaration is being placed. */
12619 scope = current_scope ();
12620 /* If that scope does not contain the scope in which the
12621 class was originally declared, the program is invalid. */
12622 if (scope && !is_ancestor (scope, nested_name_specifier))
12624 error ("declaration of %qD in %qD which does not enclose %qD",
12625 type, scope, nested_name_specifier);
12631 A declarator-id shall not be qualified exception of the
12632 definition of a ... nested class outside of its class
12633 ... [or] a the definition or explicit instantiation of a
12634 class member of a namespace outside of its namespace. */
12635 if (scope == nested_name_specifier)
12637 pedwarn ("extra qualification ignored");
12638 nested_name_specifier = NULL_TREE;
12642 /* An explicit-specialization must be preceded by "template <>". If
12643 it is not, try to recover gracefully. */
12644 if (at_namespace_scope_p ()
12645 && parser->num_template_parameter_lists == 0
12648 error ("an explicit specialization must be preceded by %<template <>%>");
12649 invalid_explicit_specialization_p = true;
12650 /* Take the same action that would have been taken by
12651 cp_parser_explicit_specialization. */
12652 ++parser->num_template_parameter_lists;
12653 begin_specialization ();
12655 /* There must be no "return" statements between this point and the
12656 end of this function; set "type "to the correct return value and
12657 use "goto done;" to return. */
12658 /* Make sure that the right number of template parameters were
12660 if (!cp_parser_check_template_parameters (parser, num_templates))
12662 /* If something went wrong, there is no point in even trying to
12663 process the class-definition. */
12668 /* Look up the type. */
12671 type = TREE_TYPE (id);
12672 maybe_process_partial_specialization (type);
12674 else if (!nested_name_specifier)
12676 /* If the class was unnamed, create a dummy name. */
12678 id = make_anon_name ();
12679 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12680 parser->num_template_parameter_lists);
12685 bool pop_p = false;
12689 template <typename T> struct S { struct T };
12690 template <typename T> struct S<T>::T { };
12692 we will get a TYPENAME_TYPE when processing the definition of
12693 `S::T'. We need to resolve it to the actual type before we
12694 try to define it. */
12695 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12697 class_type = resolve_typename_type (TREE_TYPE (type),
12698 /*only_current_p=*/false);
12699 if (class_type != error_mark_node)
12700 type = TYPE_NAME (class_type);
12703 cp_parser_error (parser, "could not resolve typename type");
12704 type = error_mark_node;
12708 maybe_process_partial_specialization (TREE_TYPE (type));
12709 class_type = current_class_type;
12710 /* Enter the scope indicated by the nested-name-specifier. */
12711 if (nested_name_specifier)
12712 pop_p = push_scope (nested_name_specifier);
12713 /* Get the canonical version of this type. */
12714 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12715 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12716 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12718 type = push_template_decl (type);
12719 if (type == error_mark_node)
12726 type = TREE_TYPE (type);
12727 if (nested_name_specifier)
12729 *nested_name_specifier_p = true;
12731 pop_scope (nested_name_specifier);
12734 /* Indicate whether this class was declared as a `class' or as a
12736 if (TREE_CODE (type) == RECORD_TYPE)
12737 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12738 cp_parser_check_class_key (class_key, type);
12740 /* Enter the scope containing the class; the names of base classes
12741 should be looked up in that context. For example, given:
12743 struct A { struct B {}; struct C; };
12744 struct A::C : B {};
12747 if (nested_name_specifier)
12748 pop_p = push_scope (nested_name_specifier);
12752 /* Get the list of base-classes, if there is one. */
12753 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12754 bases = cp_parser_base_clause (parser);
12756 /* Process the base classes. */
12757 xref_basetypes (type, bases);
12759 /* Leave the scope given by the nested-name-specifier. We will
12760 enter the class scope itself while processing the members. */
12762 pop_scope (nested_name_specifier);
12765 if (invalid_explicit_specialization_p)
12767 end_specialization ();
12768 --parser->num_template_parameter_lists;
12770 *attributes_p = attributes;
12774 /* Parse a class-key.
12781 Returns the kind of class-key specified, or none_type to indicate
12784 static enum tag_types
12785 cp_parser_class_key (cp_parser* parser)
12788 enum tag_types tag_type;
12790 /* Look for the class-key. */
12791 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12795 /* Check to see if the TOKEN is a class-key. */
12796 tag_type = cp_parser_token_is_class_key (token);
12798 cp_parser_error (parser, "expected class-key");
12802 /* Parse an (optional) member-specification.
12804 member-specification:
12805 member-declaration member-specification [opt]
12806 access-specifier : member-specification [opt] */
12809 cp_parser_member_specification_opt (cp_parser* parser)
12816 /* Peek at the next token. */
12817 token = cp_lexer_peek_token (parser->lexer);
12818 /* If it's a `}', or EOF then we've seen all the members. */
12819 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12822 /* See if this token is a keyword. */
12823 keyword = token->keyword;
12827 case RID_PROTECTED:
12829 /* Consume the access-specifier. */
12830 cp_lexer_consume_token (parser->lexer);
12831 /* Remember which access-specifier is active. */
12832 current_access_specifier = token->value;
12833 /* Look for the `:'. */
12834 cp_parser_require (parser, CPP_COLON, "`:'");
12838 /* Accept #pragmas at class scope. */
12839 if (token->type == CPP_PRAGMA)
12841 cp_lexer_handle_pragma (parser->lexer);
12845 /* Otherwise, the next construction must be a
12846 member-declaration. */
12847 cp_parser_member_declaration (parser);
12852 /* Parse a member-declaration.
12854 member-declaration:
12855 decl-specifier-seq [opt] member-declarator-list [opt] ;
12856 function-definition ; [opt]
12857 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12859 template-declaration
12861 member-declarator-list:
12863 member-declarator-list , member-declarator
12866 declarator pure-specifier [opt]
12867 declarator constant-initializer [opt]
12868 identifier [opt] : constant-expression
12872 member-declaration:
12873 __extension__ member-declaration
12876 declarator attributes [opt] pure-specifier [opt]
12877 declarator attributes [opt] constant-initializer [opt]
12878 identifier [opt] attributes [opt] : constant-expression */
12881 cp_parser_member_declaration (cp_parser* parser)
12883 cp_decl_specifier_seq decl_specifiers;
12884 tree prefix_attributes;
12886 int declares_class_or_enum;
12889 int saved_pedantic;
12891 /* Check for the `__extension__' keyword. */
12892 if (cp_parser_extension_opt (parser, &saved_pedantic))
12895 cp_parser_member_declaration (parser);
12896 /* Restore the old value of the PEDANTIC flag. */
12897 pedantic = saved_pedantic;
12902 /* Check for a template-declaration. */
12903 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12905 /* Parse the template-declaration. */
12906 cp_parser_template_declaration (parser, /*member_p=*/true);
12911 /* Check for a using-declaration. */
12912 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12914 /* Parse the using-declaration. */
12915 cp_parser_using_declaration (parser);
12920 /* Parse the decl-specifier-seq. */
12921 cp_parser_decl_specifier_seq (parser,
12922 CP_PARSER_FLAGS_OPTIONAL,
12924 &declares_class_or_enum);
12925 prefix_attributes = decl_specifiers.attributes;
12926 decl_specifiers.attributes = NULL_TREE;
12927 /* Check for an invalid type-name. */
12928 if (!decl_specifiers.type
12929 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12931 /* If there is no declarator, then the decl-specifier-seq should
12933 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12935 /* If there was no decl-specifier-seq, and the next token is a
12936 `;', then we have something like:
12942 Each member-declaration shall declare at least one member
12943 name of the class. */
12944 if (!decl_specifiers.any_specifiers_p)
12946 cp_token *token = cp_lexer_peek_token (parser->lexer);
12947 if (pedantic && !token->in_system_header)
12948 pedwarn ("%Hextra %<;%>", &token->location);
12954 /* See if this declaration is a friend. */
12955 friend_p = cp_parser_friend_p (&decl_specifiers);
12956 /* If there were decl-specifiers, check to see if there was
12957 a class-declaration. */
12958 type = check_tag_decl (&decl_specifiers);
12959 /* Nested classes have already been added to the class, but
12960 a `friend' needs to be explicitly registered. */
12963 /* If the `friend' keyword was present, the friend must
12964 be introduced with a class-key. */
12965 if (!declares_class_or_enum)
12966 error ("a class-key must be used when declaring a friend");
12969 template <typename T> struct A {
12970 friend struct A<T>::B;
12973 A<T>::B will be represented by a TYPENAME_TYPE, and
12974 therefore not recognized by check_tag_decl. */
12976 && decl_specifiers.type
12977 && TYPE_P (decl_specifiers.type))
12978 type = decl_specifiers.type;
12979 if (!type || !TYPE_P (type))
12980 error ("friend declaration does not name a class or "
12983 make_friend_class (current_class_type, type,
12984 /*complain=*/true);
12986 /* If there is no TYPE, an error message will already have
12988 else if (!type || type == error_mark_node)
12990 /* An anonymous aggregate has to be handled specially; such
12991 a declaration really declares a data member (with a
12992 particular type), as opposed to a nested class. */
12993 else if (ANON_AGGR_TYPE_P (type))
12995 /* Remove constructors and such from TYPE, now that we
12996 know it is an anonymous aggregate. */
12997 fixup_anonymous_aggr (type);
12998 /* And make the corresponding data member. */
12999 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13000 /* Add it to the class. */
13001 finish_member_declaration (decl);
13004 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13009 /* See if these declarations will be friends. */
13010 friend_p = cp_parser_friend_p (&decl_specifiers);
13012 /* Keep going until we hit the `;' at the end of the
13014 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13016 tree attributes = NULL_TREE;
13017 tree first_attribute;
13019 /* Peek at the next token. */
13020 token = cp_lexer_peek_token (parser->lexer);
13022 /* Check for a bitfield declaration. */
13023 if (token->type == CPP_COLON
13024 || (token->type == CPP_NAME
13025 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13031 /* Get the name of the bitfield. Note that we cannot just
13032 check TOKEN here because it may have been invalidated by
13033 the call to cp_lexer_peek_nth_token above. */
13034 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13035 identifier = cp_parser_identifier (parser);
13037 identifier = NULL_TREE;
13039 /* Consume the `:' token. */
13040 cp_lexer_consume_token (parser->lexer);
13041 /* Get the width of the bitfield. */
13043 = cp_parser_constant_expression (parser,
13044 /*allow_non_constant=*/false,
13047 /* Look for attributes that apply to the bitfield. */
13048 attributes = cp_parser_attributes_opt (parser);
13049 /* Remember which attributes are prefix attributes and
13051 first_attribute = attributes;
13052 /* Combine the attributes. */
13053 attributes = chainon (prefix_attributes, attributes);
13055 /* Create the bitfield declaration. */
13056 decl = grokbitfield (identifier
13057 ? make_id_declarator (identifier)
13061 /* Apply the attributes. */
13062 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13066 cp_declarator *declarator;
13068 tree asm_specification;
13069 int ctor_dtor_or_conv_p;
13071 /* Parse the declarator. */
13073 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13074 &ctor_dtor_or_conv_p,
13075 /*parenthesized_p=*/NULL,
13076 /*member_p=*/true);
13078 /* If something went wrong parsing the declarator, make sure
13079 that we at least consume some tokens. */
13080 if (declarator == cp_error_declarator)
13082 /* Skip to the end of the statement. */
13083 cp_parser_skip_to_end_of_statement (parser);
13084 /* If the next token is not a semicolon, that is
13085 probably because we just skipped over the body of
13086 a function. So, we consume a semicolon if
13087 present, but do not issue an error message if it
13089 if (cp_lexer_next_token_is (parser->lexer,
13091 cp_lexer_consume_token (parser->lexer);
13095 if (declares_class_or_enum & 2)
13096 cp_parser_check_for_definition_in_return_type
13097 (declarator, decl_specifiers.type);
13099 /* Look for an asm-specification. */
13100 asm_specification = cp_parser_asm_specification_opt (parser);
13101 /* Look for attributes that apply to the declaration. */
13102 attributes = cp_parser_attributes_opt (parser);
13103 /* Remember which attributes are prefix attributes and
13105 first_attribute = attributes;
13106 /* Combine the attributes. */
13107 attributes = chainon (prefix_attributes, attributes);
13109 /* If it's an `=', then we have a constant-initializer or a
13110 pure-specifier. It is not correct to parse the
13111 initializer before registering the member declaration
13112 since the member declaration should be in scope while
13113 its initializer is processed. However, the rest of the
13114 front end does not yet provide an interface that allows
13115 us to handle this correctly. */
13116 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13120 A pure-specifier shall be used only in the declaration of
13121 a virtual function.
13123 A member-declarator can contain a constant-initializer
13124 only if it declares a static member of integral or
13127 Therefore, if the DECLARATOR is for a function, we look
13128 for a pure-specifier; otherwise, we look for a
13129 constant-initializer. When we call `grokfield', it will
13130 perform more stringent semantics checks. */
13131 if (declarator->kind == cdk_function)
13132 initializer = cp_parser_pure_specifier (parser);
13134 /* Parse the initializer. */
13135 initializer = cp_parser_constant_initializer (parser);
13137 /* Otherwise, there is no initializer. */
13139 initializer = NULL_TREE;
13141 /* See if we are probably looking at a function
13142 definition. We are certainly not looking at a
13143 member-declarator. Calling `grokfield' has
13144 side-effects, so we must not do it unless we are sure
13145 that we are looking at a member-declarator. */
13146 if (cp_parser_token_starts_function_definition_p
13147 (cp_lexer_peek_token (parser->lexer)))
13149 /* The grammar does not allow a pure-specifier to be
13150 used when a member function is defined. (It is
13151 possible that this fact is an oversight in the
13152 standard, since a pure function may be defined
13153 outside of the class-specifier. */
13155 error ("pure-specifier on function-definition");
13156 decl = cp_parser_save_member_function_body (parser,
13160 /* If the member was not a friend, declare it here. */
13162 finish_member_declaration (decl);
13163 /* Peek at the next token. */
13164 token = cp_lexer_peek_token (parser->lexer);
13165 /* If the next token is a semicolon, consume it. */
13166 if (token->type == CPP_SEMICOLON)
13167 cp_lexer_consume_token (parser->lexer);
13172 /* Create the declaration. */
13173 decl = grokfield (declarator, &decl_specifiers,
13174 initializer, asm_specification,
13176 /* Any initialization must have been from a
13177 constant-expression. */
13178 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13179 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13183 /* Reset PREFIX_ATTRIBUTES. */
13184 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13185 attributes = TREE_CHAIN (attributes);
13187 TREE_CHAIN (attributes) = NULL_TREE;
13189 /* If there is any qualification still in effect, clear it
13190 now; we will be starting fresh with the next declarator. */
13191 parser->scope = NULL_TREE;
13192 parser->qualifying_scope = NULL_TREE;
13193 parser->object_scope = NULL_TREE;
13194 /* If it's a `,', then there are more declarators. */
13195 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13196 cp_lexer_consume_token (parser->lexer);
13197 /* If the next token isn't a `;', then we have a parse error. */
13198 else if (cp_lexer_next_token_is_not (parser->lexer,
13201 cp_parser_error (parser, "expected %<;%>");
13202 /* Skip tokens until we find a `;'. */
13203 cp_parser_skip_to_end_of_statement (parser);
13210 /* Add DECL to the list of members. */
13212 finish_member_declaration (decl);
13214 if (TREE_CODE (decl) == FUNCTION_DECL)
13215 cp_parser_save_default_args (parser, decl);
13220 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13223 /* Parse a pure-specifier.
13228 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13229 Otherwise, ERROR_MARK_NODE is returned. */
13232 cp_parser_pure_specifier (cp_parser* parser)
13236 /* Look for the `=' token. */
13237 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13238 return error_mark_node;
13239 /* Look for the `0' token. */
13240 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13241 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13242 to get information from the lexer about how the number was
13243 spelled in order to fix this problem. */
13244 if (!token || !integer_zerop (token->value))
13245 return error_mark_node;
13247 return integer_zero_node;
13250 /* Parse a constant-initializer.
13252 constant-initializer:
13253 = constant-expression
13255 Returns a representation of the constant-expression. */
13258 cp_parser_constant_initializer (cp_parser* parser)
13260 /* Look for the `=' token. */
13261 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13262 return error_mark_node;
13264 /* It is invalid to write:
13266 struct S { static const int i = { 7 }; };
13269 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13271 cp_parser_error (parser,
13272 "a brace-enclosed initializer is not allowed here");
13273 /* Consume the opening brace. */
13274 cp_lexer_consume_token (parser->lexer);
13275 /* Skip the initializer. */
13276 cp_parser_skip_to_closing_brace (parser);
13277 /* Look for the trailing `}'. */
13278 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13280 return error_mark_node;
13283 return cp_parser_constant_expression (parser,
13284 /*allow_non_constant=*/false,
13288 /* Derived classes [gram.class.derived] */
13290 /* Parse a base-clause.
13293 : base-specifier-list
13295 base-specifier-list:
13297 base-specifier-list , base-specifier
13299 Returns a TREE_LIST representing the base-classes, in the order in
13300 which they were declared. The representation of each node is as
13301 described by cp_parser_base_specifier.
13303 In the case that no bases are specified, this function will return
13304 NULL_TREE, not ERROR_MARK_NODE. */
13307 cp_parser_base_clause (cp_parser* parser)
13309 tree bases = NULL_TREE;
13311 /* Look for the `:' that begins the list. */
13312 cp_parser_require (parser, CPP_COLON, "`:'");
13314 /* Scan the base-specifier-list. */
13320 /* Look for the base-specifier. */
13321 base = cp_parser_base_specifier (parser);
13322 /* Add BASE to the front of the list. */
13323 if (base != error_mark_node)
13325 TREE_CHAIN (base) = bases;
13328 /* Peek at the next token. */
13329 token = cp_lexer_peek_token (parser->lexer);
13330 /* If it's not a comma, then the list is complete. */
13331 if (token->type != CPP_COMMA)
13333 /* Consume the `,'. */
13334 cp_lexer_consume_token (parser->lexer);
13337 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13338 base class had a qualified name. However, the next name that
13339 appears is certainly not qualified. */
13340 parser->scope = NULL_TREE;
13341 parser->qualifying_scope = NULL_TREE;
13342 parser->object_scope = NULL_TREE;
13344 return nreverse (bases);
13347 /* Parse a base-specifier.
13350 :: [opt] nested-name-specifier [opt] class-name
13351 virtual access-specifier [opt] :: [opt] nested-name-specifier
13353 access-specifier virtual [opt] :: [opt] nested-name-specifier
13356 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13357 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13358 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13359 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13362 cp_parser_base_specifier (cp_parser* parser)
13366 bool virtual_p = false;
13367 bool duplicate_virtual_error_issued_p = false;
13368 bool duplicate_access_error_issued_p = false;
13369 bool class_scope_p, template_p;
13370 tree access = access_default_node;
13373 /* Process the optional `virtual' and `access-specifier'. */
13376 /* Peek at the next token. */
13377 token = cp_lexer_peek_token (parser->lexer);
13378 /* Process `virtual'. */
13379 switch (token->keyword)
13382 /* If `virtual' appears more than once, issue an error. */
13383 if (virtual_p && !duplicate_virtual_error_issued_p)
13385 cp_parser_error (parser,
13386 "%<virtual%> specified more than once in base-specified");
13387 duplicate_virtual_error_issued_p = true;
13392 /* Consume the `virtual' token. */
13393 cp_lexer_consume_token (parser->lexer);
13398 case RID_PROTECTED:
13400 /* If more than one access specifier appears, issue an
13402 if (access != access_default_node
13403 && !duplicate_access_error_issued_p)
13405 cp_parser_error (parser,
13406 "more than one access specifier in base-specified");
13407 duplicate_access_error_issued_p = true;
13410 access = ridpointers[(int) token->keyword];
13412 /* Consume the access-specifier. */
13413 cp_lexer_consume_token (parser->lexer);
13422 /* It is not uncommon to see programs mechanically, erroneously, use
13423 the 'typename' keyword to denote (dependent) qualified types
13424 as base classes. */
13425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13427 if (!processing_template_decl)
13428 error ("keyword %<typename%> not allowed outside of templates");
13430 error ("keyword %<typename%> not allowed in this context "
13431 "(the base class is implicitly a type)");
13432 cp_lexer_consume_token (parser->lexer);
13435 /* Look for the optional `::' operator. */
13436 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13437 /* Look for the nested-name-specifier. The simplest way to
13442 The keyword `typename' is not permitted in a base-specifier or
13443 mem-initializer; in these contexts a qualified name that
13444 depends on a template-parameter is implicitly assumed to be a
13447 is to pretend that we have seen the `typename' keyword at this
13449 cp_parser_nested_name_specifier_opt (parser,
13450 /*typename_keyword_p=*/true,
13451 /*check_dependency_p=*/true,
13453 /*is_declaration=*/true);
13454 /* If the base class is given by a qualified name, assume that names
13455 we see are type names or templates, as appropriate. */
13456 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13457 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13459 /* Finally, look for the class-name. */
13460 type = cp_parser_class_name (parser,
13464 /*check_dependency_p=*/true,
13465 /*class_head_p=*/false,
13466 /*is_declaration=*/true);
13468 if (type == error_mark_node)
13469 return error_mark_node;
13471 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13474 /* Exception handling [gram.exception] */
13476 /* Parse an (optional) exception-specification.
13478 exception-specification:
13479 throw ( type-id-list [opt] )
13481 Returns a TREE_LIST representing the exception-specification. The
13482 TREE_VALUE of each node is a type. */
13485 cp_parser_exception_specification_opt (cp_parser* parser)
13490 /* Peek at the next token. */
13491 token = cp_lexer_peek_token (parser->lexer);
13492 /* If it's not `throw', then there's no exception-specification. */
13493 if (!cp_parser_is_keyword (token, RID_THROW))
13496 /* Consume the `throw'. */
13497 cp_lexer_consume_token (parser->lexer);
13499 /* Look for the `('. */
13500 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13502 /* Peek at the next token. */
13503 token = cp_lexer_peek_token (parser->lexer);
13504 /* If it's not a `)', then there is a type-id-list. */
13505 if (token->type != CPP_CLOSE_PAREN)
13507 const char *saved_message;
13509 /* Types may not be defined in an exception-specification. */
13510 saved_message = parser->type_definition_forbidden_message;
13511 parser->type_definition_forbidden_message
13512 = "types may not be defined in an exception-specification";
13513 /* Parse the type-id-list. */
13514 type_id_list = cp_parser_type_id_list (parser);
13515 /* Restore the saved message. */
13516 parser->type_definition_forbidden_message = saved_message;
13519 type_id_list = empty_except_spec;
13521 /* Look for the `)'. */
13522 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13524 return type_id_list;
13527 /* Parse an (optional) type-id-list.
13531 type-id-list , type-id
13533 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13534 in the order that the types were presented. */
13537 cp_parser_type_id_list (cp_parser* parser)
13539 tree types = NULL_TREE;
13546 /* Get the next type-id. */
13547 type = cp_parser_type_id (parser);
13548 /* Add it to the list. */
13549 types = add_exception_specifier (types, type, /*complain=*/1);
13550 /* Peek at the next token. */
13551 token = cp_lexer_peek_token (parser->lexer);
13552 /* If it is not a `,', we are done. */
13553 if (token->type != CPP_COMMA)
13555 /* Consume the `,'. */
13556 cp_lexer_consume_token (parser->lexer);
13559 return nreverse (types);
13562 /* Parse a try-block.
13565 try compound-statement handler-seq */
13568 cp_parser_try_block (cp_parser* parser)
13572 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13573 try_block = begin_try_block ();
13574 cp_parser_compound_statement (parser, NULL, true);
13575 finish_try_block (try_block);
13576 cp_parser_handler_seq (parser);
13577 finish_handler_sequence (try_block);
13582 /* Parse a function-try-block.
13584 function-try-block:
13585 try ctor-initializer [opt] function-body handler-seq */
13588 cp_parser_function_try_block (cp_parser* parser)
13591 bool ctor_initializer_p;
13593 /* Look for the `try' keyword. */
13594 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13596 /* Let the rest of the front-end know where we are. */
13597 try_block = begin_function_try_block ();
13598 /* Parse the function-body. */
13600 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13601 /* We're done with the `try' part. */
13602 finish_function_try_block (try_block);
13603 /* Parse the handlers. */
13604 cp_parser_handler_seq (parser);
13605 /* We're done with the handlers. */
13606 finish_function_handler_sequence (try_block);
13608 return ctor_initializer_p;
13611 /* Parse a handler-seq.
13614 handler handler-seq [opt] */
13617 cp_parser_handler_seq (cp_parser* parser)
13623 /* Parse the handler. */
13624 cp_parser_handler (parser);
13625 /* Peek at the next token. */
13626 token = cp_lexer_peek_token (parser->lexer);
13627 /* If it's not `catch' then there are no more handlers. */
13628 if (!cp_parser_is_keyword (token, RID_CATCH))
13633 /* Parse a handler.
13636 catch ( exception-declaration ) compound-statement */
13639 cp_parser_handler (cp_parser* parser)
13644 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13645 handler = begin_handler ();
13646 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13647 declaration = cp_parser_exception_declaration (parser);
13648 finish_handler_parms (declaration, handler);
13649 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13650 cp_parser_compound_statement (parser, NULL, false);
13651 finish_handler (handler);
13654 /* Parse an exception-declaration.
13656 exception-declaration:
13657 type-specifier-seq declarator
13658 type-specifier-seq abstract-declarator
13662 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13663 ellipsis variant is used. */
13666 cp_parser_exception_declaration (cp_parser* parser)
13669 cp_decl_specifier_seq type_specifiers;
13670 cp_declarator *declarator;
13671 const char *saved_message;
13673 /* If it's an ellipsis, it's easy to handle. */
13674 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13676 /* Consume the `...' token. */
13677 cp_lexer_consume_token (parser->lexer);
13681 /* Types may not be defined in exception-declarations. */
13682 saved_message = parser->type_definition_forbidden_message;
13683 parser->type_definition_forbidden_message
13684 = "types may not be defined in exception-declarations";
13686 /* Parse the type-specifier-seq. */
13687 cp_parser_type_specifier_seq (parser, &type_specifiers);
13688 /* If it's a `)', then there is no declarator. */
13689 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13692 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13693 /*ctor_dtor_or_conv_p=*/NULL,
13694 /*parenthesized_p=*/NULL,
13695 /*member_p=*/false);
13697 /* Restore the saved message. */
13698 parser->type_definition_forbidden_message = saved_message;
13700 if (type_specifiers.any_specifiers_p)
13702 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13703 if (decl == NULL_TREE)
13704 error ("invalid catch parameter");
13712 /* Parse a throw-expression.
13715 throw assignment-expression [opt]
13717 Returns a THROW_EXPR representing the throw-expression. */
13720 cp_parser_throw_expression (cp_parser* parser)
13725 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13726 token = cp_lexer_peek_token (parser->lexer);
13727 /* Figure out whether or not there is an assignment-expression
13728 following the "throw" keyword. */
13729 if (token->type == CPP_COMMA
13730 || token->type == CPP_SEMICOLON
13731 || token->type == CPP_CLOSE_PAREN
13732 || token->type == CPP_CLOSE_SQUARE
13733 || token->type == CPP_CLOSE_BRACE
13734 || token->type == CPP_COLON)
13735 expression = NULL_TREE;
13737 expression = cp_parser_assignment_expression (parser);
13739 return build_throw (expression);
13742 /* GNU Extensions */
13744 /* Parse an (optional) asm-specification.
13747 asm ( string-literal )
13749 If the asm-specification is present, returns a STRING_CST
13750 corresponding to the string-literal. Otherwise, returns
13754 cp_parser_asm_specification_opt (cp_parser* parser)
13757 tree asm_specification;
13759 /* Peek at the next token. */
13760 token = cp_lexer_peek_token (parser->lexer);
13761 /* If the next token isn't the `asm' keyword, then there's no
13762 asm-specification. */
13763 if (!cp_parser_is_keyword (token, RID_ASM))
13766 /* Consume the `asm' token. */
13767 cp_lexer_consume_token (parser->lexer);
13768 /* Look for the `('. */
13769 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13771 /* Look for the string-literal. */
13772 asm_specification = cp_parser_string_literal (parser, false, false);
13774 /* Look for the `)'. */
13775 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13777 return asm_specification;
13780 /* Parse an asm-operand-list.
13784 asm-operand-list , asm-operand
13787 string-literal ( expression )
13788 [ string-literal ] string-literal ( expression )
13790 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13791 each node is the expression. The TREE_PURPOSE is itself a
13792 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13793 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13794 is a STRING_CST for the string literal before the parenthesis. */
13797 cp_parser_asm_operand_list (cp_parser* parser)
13799 tree asm_operands = NULL_TREE;
13803 tree string_literal;
13807 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13809 /* Consume the `[' token. */
13810 cp_lexer_consume_token (parser->lexer);
13811 /* Read the operand name. */
13812 name = cp_parser_identifier (parser);
13813 if (name != error_mark_node)
13814 name = build_string (IDENTIFIER_LENGTH (name),
13815 IDENTIFIER_POINTER (name));
13816 /* Look for the closing `]'. */
13817 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13821 /* Look for the string-literal. */
13822 string_literal = cp_parser_string_literal (parser, false, false);
13824 /* Look for the `('. */
13825 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13826 /* Parse the expression. */
13827 expression = cp_parser_expression (parser);
13828 /* Look for the `)'. */
13829 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13831 /* Add this operand to the list. */
13832 asm_operands = tree_cons (build_tree_list (name, string_literal),
13835 /* If the next token is not a `,', there are no more
13837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13839 /* Consume the `,'. */
13840 cp_lexer_consume_token (parser->lexer);
13843 return nreverse (asm_operands);
13846 /* Parse an asm-clobber-list.
13850 asm-clobber-list , string-literal
13852 Returns a TREE_LIST, indicating the clobbers in the order that they
13853 appeared. The TREE_VALUE of each node is a STRING_CST. */
13856 cp_parser_asm_clobber_list (cp_parser* parser)
13858 tree clobbers = NULL_TREE;
13862 tree string_literal;
13864 /* Look for the string literal. */
13865 string_literal = cp_parser_string_literal (parser, false, false);
13866 /* Add it to the list. */
13867 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13868 /* If the next token is not a `,', then the list is
13870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13872 /* Consume the `,' token. */
13873 cp_lexer_consume_token (parser->lexer);
13879 /* Parse an (optional) series of attributes.
13882 attributes attribute
13885 __attribute__ (( attribute-list [opt] ))
13887 The return value is as for cp_parser_attribute_list. */
13890 cp_parser_attributes_opt (cp_parser* parser)
13892 tree attributes = NULL_TREE;
13897 tree attribute_list;
13899 /* Peek at the next token. */
13900 token = cp_lexer_peek_token (parser->lexer);
13901 /* If it's not `__attribute__', then we're done. */
13902 if (token->keyword != RID_ATTRIBUTE)
13905 /* Consume the `__attribute__' keyword. */
13906 cp_lexer_consume_token (parser->lexer);
13907 /* Look for the two `(' tokens. */
13908 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13909 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13911 /* Peek at the next token. */
13912 token = cp_lexer_peek_token (parser->lexer);
13913 if (token->type != CPP_CLOSE_PAREN)
13914 /* Parse the attribute-list. */
13915 attribute_list = cp_parser_attribute_list (parser);
13917 /* If the next token is a `)', then there is no attribute
13919 attribute_list = NULL;
13921 /* Look for the two `)' tokens. */
13922 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13923 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13925 /* Add these new attributes to the list. */
13926 attributes = chainon (attributes, attribute_list);
13932 /* Parse an attribute-list.
13936 attribute-list , attribute
13940 identifier ( identifier )
13941 identifier ( identifier , expression-list )
13942 identifier ( expression-list )
13944 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13945 TREE_PURPOSE of each node is the identifier indicating which
13946 attribute is in use. The TREE_VALUE represents the arguments, if
13950 cp_parser_attribute_list (cp_parser* parser)
13952 tree attribute_list = NULL_TREE;
13953 bool save_translate_strings_p = parser->translate_strings_p;
13955 parser->translate_strings_p = false;
13962 /* Look for the identifier. We also allow keywords here; for
13963 example `__attribute__ ((const))' is legal. */
13964 token = cp_lexer_peek_token (parser->lexer);
13965 if (token->type != CPP_NAME
13966 && token->type != CPP_KEYWORD)
13967 return error_mark_node;
13968 /* Consume the token. */
13969 token = cp_lexer_consume_token (parser->lexer);
13971 /* Save away the identifier that indicates which attribute this is. */
13972 identifier = token->value;
13973 attribute = build_tree_list (identifier, NULL_TREE);
13975 /* Peek at the next token. */
13976 token = cp_lexer_peek_token (parser->lexer);
13977 /* If it's an `(', then parse the attribute arguments. */
13978 if (token->type == CPP_OPEN_PAREN)
13982 arguments = (cp_parser_parenthesized_expression_list
13983 (parser, true, /*non_constant_p=*/NULL));
13984 /* Save the identifier and arguments away. */
13985 TREE_VALUE (attribute) = arguments;
13988 /* Add this attribute to the list. */
13989 TREE_CHAIN (attribute) = attribute_list;
13990 attribute_list = attribute;
13992 /* Now, look for more attributes. */
13993 token = cp_lexer_peek_token (parser->lexer);
13994 /* If the next token isn't a `,', we're done. */
13995 if (token->type != CPP_COMMA)
13998 /* Consume the comma and keep going. */
13999 cp_lexer_consume_token (parser->lexer);
14001 parser->translate_strings_p = save_translate_strings_p;
14003 /* We built up the list in reverse order. */
14004 return nreverse (attribute_list);
14007 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14008 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14009 current value of the PEDANTIC flag, regardless of whether or not
14010 the `__extension__' keyword is present. The caller is responsible
14011 for restoring the value of the PEDANTIC flag. */
14014 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14016 /* Save the old value of the PEDANTIC flag. */
14017 *saved_pedantic = pedantic;
14019 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14021 /* Consume the `__extension__' token. */
14022 cp_lexer_consume_token (parser->lexer);
14023 /* We're not being pedantic while the `__extension__' keyword is
14033 /* Parse a label declaration.
14036 __label__ label-declarator-seq ;
14038 label-declarator-seq:
14039 identifier , label-declarator-seq
14043 cp_parser_label_declaration (cp_parser* parser)
14045 /* Look for the `__label__' keyword. */
14046 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14052 /* Look for an identifier. */
14053 identifier = cp_parser_identifier (parser);
14054 /* Declare it as a lobel. */
14055 finish_label_decl (identifier);
14056 /* If the next token is a `;', stop. */
14057 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14059 /* Look for the `,' separating the label declarations. */
14060 cp_parser_require (parser, CPP_COMMA, "`,'");
14063 /* Look for the final `;'. */
14064 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14067 /* Support Functions */
14069 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14070 NAME should have one of the representations used for an
14071 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14072 is returned. If PARSER->SCOPE is a dependent type, then a
14073 SCOPE_REF is returned.
14075 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14076 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14077 was formed. Abstractly, such entities should not be passed to this
14078 function, because they do not need to be looked up, but it is
14079 simpler to check for this special case here, rather than at the
14082 In cases not explicitly covered above, this function returns a
14083 DECL, OVERLOAD, or baselink representing the result of the lookup.
14084 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14087 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14088 (e.g., "struct") that was used. In that case bindings that do not
14089 refer to types are ignored.
14091 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14094 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14097 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14100 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14101 results in an ambiguity, and false otherwise. */
14104 cp_parser_lookup_name (cp_parser *parser, tree name,
14105 enum tag_types tag_type,
14106 bool is_template, bool is_namespace,
14107 bool check_dependency,
14111 tree object_type = parser->context->object_type;
14113 /* Assume that the lookup will be unambiguous. */
14115 *ambiguous_p = false;
14117 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14118 no longer valid. Note that if we are parsing tentatively, and
14119 the parse fails, OBJECT_TYPE will be automatically restored. */
14120 parser->context->object_type = NULL_TREE;
14122 if (name == error_mark_node)
14123 return error_mark_node;
14125 /* A template-id has already been resolved; there is no lookup to
14127 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14129 if (BASELINK_P (name))
14131 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14132 == TEMPLATE_ID_EXPR);
14136 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14137 it should already have been checked to make sure that the name
14138 used matches the type being destroyed. */
14139 if (TREE_CODE (name) == BIT_NOT_EXPR)
14143 /* Figure out to which type this destructor applies. */
14145 type = parser->scope;
14146 else if (object_type)
14147 type = object_type;
14149 type = current_class_type;
14150 /* If that's not a class type, there is no destructor. */
14151 if (!type || !CLASS_TYPE_P (type))
14152 return error_mark_node;
14153 if (!CLASSTYPE_DESTRUCTORS (type))
14154 return error_mark_node;
14155 /* If it was a class type, return the destructor. */
14156 return CLASSTYPE_DESTRUCTORS (type);
14159 /* By this point, the NAME should be an ordinary identifier. If
14160 the id-expression was a qualified name, the qualifying scope is
14161 stored in PARSER->SCOPE at this point. */
14162 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14164 /* Perform the lookup. */
14169 if (parser->scope == error_mark_node)
14170 return error_mark_node;
14172 /* If the SCOPE is dependent, the lookup must be deferred until
14173 the template is instantiated -- unless we are explicitly
14174 looking up names in uninstantiated templates. Even then, we
14175 cannot look up the name if the scope is not a class type; it
14176 might, for example, be a template type parameter. */
14177 dependent_p = (TYPE_P (parser->scope)
14178 && !(parser->in_declarator_p
14179 && currently_open_class (parser->scope))
14180 && dependent_type_p (parser->scope));
14181 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14188 /* The resolution to Core Issue 180 says that `struct
14189 A::B' should be considered a type-name, even if `A'
14191 type = make_typename_type (parser->scope, name, tag_type,
14193 if (tag_type == enum_type)
14194 TYPENAME_IS_ENUM_P (type) = 1;
14195 else if (tag_type != typename_type)
14196 TYPENAME_IS_CLASS_P (type) = 1;
14197 decl = TYPE_NAME (type);
14199 else if (is_template)
14200 decl = make_unbound_class_template (parser->scope,
14204 decl = build_nt (SCOPE_REF, parser->scope, name);
14208 bool pop_p = false;
14210 /* If PARSER->SCOPE is a dependent type, then it must be a
14211 class type, and we must not be checking dependencies;
14212 otherwise, we would have processed this lookup above. So
14213 that PARSER->SCOPE is not considered a dependent base by
14214 lookup_member, we must enter the scope here. */
14216 pop_p = push_scope (parser->scope);
14217 /* If the PARSER->SCOPE is a a template specialization, it
14218 may be instantiated during name lookup. In that case,
14219 errors may be issued. Even if we rollback the current
14220 tentative parse, those errors are valid. */
14221 decl = lookup_qualified_name (parser->scope, name,
14222 tag_type != none_type,
14223 /*complain=*/true);
14225 pop_scope (parser->scope);
14227 parser->qualifying_scope = parser->scope;
14228 parser->object_scope = NULL_TREE;
14230 else if (object_type)
14232 tree object_decl = NULL_TREE;
14233 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14234 OBJECT_TYPE is not a class. */
14235 if (CLASS_TYPE_P (object_type))
14236 /* If the OBJECT_TYPE is a template specialization, it may
14237 be instantiated during name lookup. In that case, errors
14238 may be issued. Even if we rollback the current tentative
14239 parse, those errors are valid. */
14240 object_decl = lookup_member (object_type,
14243 tag_type != none_type);
14244 /* Look it up in the enclosing context, too. */
14245 decl = lookup_name_real (name, tag_type != none_type,
14247 /*block_p=*/true, is_namespace,
14249 parser->object_scope = object_type;
14250 parser->qualifying_scope = NULL_TREE;
14252 decl = object_decl;
14256 decl = lookup_name_real (name, tag_type != none_type,
14258 /*block_p=*/true, is_namespace,
14260 parser->qualifying_scope = NULL_TREE;
14261 parser->object_scope = NULL_TREE;
14264 /* If the lookup failed, let our caller know. */
14266 || decl == error_mark_node
14267 || (TREE_CODE (decl) == FUNCTION_DECL
14268 && DECL_ANTICIPATED (decl)))
14269 return error_mark_node;
14271 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14272 if (TREE_CODE (decl) == TREE_LIST)
14275 *ambiguous_p = true;
14276 /* The error message we have to print is too complicated for
14277 cp_parser_error, so we incorporate its actions directly. */
14278 if (!cp_parser_simulate_error (parser))
14280 error ("reference to %qD is ambiguous", name);
14281 print_candidates (decl);
14283 return error_mark_node;
14286 gcc_assert (DECL_P (decl)
14287 || TREE_CODE (decl) == OVERLOAD
14288 || TREE_CODE (decl) == SCOPE_REF
14289 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14290 || BASELINK_P (decl));
14292 /* If we have resolved the name of a member declaration, check to
14293 see if the declaration is accessible. When the name resolves to
14294 set of overloaded functions, accessibility is checked when
14295 overload resolution is done.
14297 During an explicit instantiation, access is not checked at all,
14298 as per [temp.explicit]. */
14300 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14305 /* Like cp_parser_lookup_name, but for use in the typical case where
14306 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14307 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14310 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14312 return cp_parser_lookup_name (parser, name,
14314 /*is_template=*/false,
14315 /*is_namespace=*/false,
14316 /*check_dependency=*/true,
14317 /*ambiguous_p=*/NULL);
14320 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14321 the current context, return the TYPE_DECL. If TAG_NAME_P is
14322 true, the DECL indicates the class being defined in a class-head,
14323 or declared in an elaborated-type-specifier.
14325 Otherwise, return DECL. */
14328 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14330 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14331 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14334 template <typename T> struct B;
14337 template <typename T> struct A::B {};
14339 Similarly, in a elaborated-type-specifier:
14341 namespace N { struct X{}; }
14344 template <typename T> friend struct N::X;
14347 However, if the DECL refers to a class type, and we are in
14348 the scope of the class, then the name lookup automatically
14349 finds the TYPE_DECL created by build_self_reference rather
14350 than a TEMPLATE_DECL. For example, in:
14352 template <class T> struct S {
14356 there is no need to handle such case. */
14358 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14359 return DECL_TEMPLATE_RESULT (decl);
14364 /* If too many, or too few, template-parameter lists apply to the
14365 declarator, issue an error message. Returns TRUE if all went well,
14366 and FALSE otherwise. */
14369 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14370 cp_declarator *declarator)
14372 unsigned num_templates;
14374 /* We haven't seen any classes that involve template parameters yet. */
14377 switch (declarator->kind)
14380 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14385 scope = TREE_OPERAND (declarator->u.id.name, 0);
14386 member = TREE_OPERAND (declarator->u.id.name, 1);
14388 while (scope && CLASS_TYPE_P (scope))
14390 /* You're supposed to have one `template <...>'
14391 for every template class, but you don't need one
14392 for a full specialization. For example:
14394 template <class T> struct S{};
14395 template <> struct S<int> { void f(); };
14396 void S<int>::f () {}
14398 is correct; there shouldn't be a `template <>' for
14399 the definition of `S<int>::f'. */
14400 if (CLASSTYPE_TEMPLATE_INFO (scope)
14401 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14402 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14403 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14406 scope = TYPE_CONTEXT (scope);
14410 /* If the DECLARATOR has the form `X<y>' then it uses one
14411 additional level of template parameters. */
14412 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14415 return cp_parser_check_template_parameters (parser,
14421 case cdk_reference:
14423 return (cp_parser_check_declarator_template_parameters
14424 (parser, declarator->declarator));
14430 gcc_unreachable ();
14435 /* NUM_TEMPLATES were used in the current declaration. If that is
14436 invalid, return FALSE and issue an error messages. Otherwise,
14440 cp_parser_check_template_parameters (cp_parser* parser,
14441 unsigned num_templates)
14443 /* If there are more template classes than parameter lists, we have
14446 template <class T> void S<T>::R<T>::f (); */
14447 if (parser->num_template_parameter_lists < num_templates)
14449 error ("too few template-parameter-lists");
14452 /* If there are the same number of template classes and parameter
14453 lists, that's OK. */
14454 if (parser->num_template_parameter_lists == num_templates)
14456 /* If there are more, but only one more, then we are referring to a
14457 member template. That's OK too. */
14458 if (parser->num_template_parameter_lists == num_templates + 1)
14460 /* Otherwise, there are too many template parameter lists. We have
14463 template <class T> template <class U> void S::f(); */
14464 error ("too many template-parameter-lists");
14468 /* Parse an optional `::' token indicating that the following name is
14469 from the global namespace. If so, PARSER->SCOPE is set to the
14470 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14471 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14472 Returns the new value of PARSER->SCOPE, if the `::' token is
14473 present, and NULL_TREE otherwise. */
14476 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14480 /* Peek at the next token. */
14481 token = cp_lexer_peek_token (parser->lexer);
14482 /* If we're looking at a `::' token then we're starting from the
14483 global namespace, not our current location. */
14484 if (token->type == CPP_SCOPE)
14486 /* Consume the `::' token. */
14487 cp_lexer_consume_token (parser->lexer);
14488 /* Set the SCOPE so that we know where to start the lookup. */
14489 parser->scope = global_namespace;
14490 parser->qualifying_scope = global_namespace;
14491 parser->object_scope = NULL_TREE;
14493 return parser->scope;
14495 else if (!current_scope_valid_p)
14497 parser->scope = NULL_TREE;
14498 parser->qualifying_scope = NULL_TREE;
14499 parser->object_scope = NULL_TREE;
14505 /* Returns TRUE if the upcoming token sequence is the start of a
14506 constructor declarator. If FRIEND_P is true, the declarator is
14507 preceded by the `friend' specifier. */
14510 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14512 bool constructor_p;
14513 tree type_decl = NULL_TREE;
14514 bool nested_name_p;
14515 cp_token *next_token;
14517 /* The common case is that this is not a constructor declarator, so
14518 try to avoid doing lots of work if at all possible. It's not
14519 valid declare a constructor at function scope. */
14520 if (at_function_scope_p ())
14522 /* And only certain tokens can begin a constructor declarator. */
14523 next_token = cp_lexer_peek_token (parser->lexer);
14524 if (next_token->type != CPP_NAME
14525 && next_token->type != CPP_SCOPE
14526 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14527 && next_token->type != CPP_TEMPLATE_ID)
14530 /* Parse tentatively; we are going to roll back all of the tokens
14532 cp_parser_parse_tentatively (parser);
14533 /* Assume that we are looking at a constructor declarator. */
14534 constructor_p = true;
14536 /* Look for the optional `::' operator. */
14537 cp_parser_global_scope_opt (parser,
14538 /*current_scope_valid_p=*/false);
14539 /* Look for the nested-name-specifier. */
14541 = (cp_parser_nested_name_specifier_opt (parser,
14542 /*typename_keyword_p=*/false,
14543 /*check_dependency_p=*/false,
14545 /*is_declaration=*/false)
14547 /* Outside of a class-specifier, there must be a
14548 nested-name-specifier. */
14549 if (!nested_name_p &&
14550 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14552 constructor_p = false;
14553 /* If we still think that this might be a constructor-declarator,
14554 look for a class-name. */
14559 template <typename T> struct S { S(); };
14560 template <typename T> S<T>::S ();
14562 we must recognize that the nested `S' names a class.
14565 template <typename T> S<T>::S<T> ();
14567 we must recognize that the nested `S' names a template. */
14568 type_decl = cp_parser_class_name (parser,
14569 /*typename_keyword_p=*/false,
14570 /*template_keyword_p=*/false,
14572 /*check_dependency_p=*/false,
14573 /*class_head_p=*/false,
14574 /*is_declaration=*/false);
14575 /* If there was no class-name, then this is not a constructor. */
14576 constructor_p = !cp_parser_error_occurred (parser);
14579 /* If we're still considering a constructor, we have to see a `(',
14580 to begin the parameter-declaration-clause, followed by either a
14581 `)', an `...', or a decl-specifier. We need to check for a
14582 type-specifier to avoid being fooled into thinking that:
14586 is a constructor. (It is actually a function named `f' that
14587 takes one parameter (of type `int') and returns a value of type
14590 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14593 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14594 /* A parameter declaration begins with a decl-specifier,
14595 which is either the "attribute" keyword, a storage class
14596 specifier, or (usually) a type-specifier. */
14597 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14598 && !cp_parser_storage_class_specifier_opt (parser))
14601 bool pop_p = false;
14602 unsigned saved_num_template_parameter_lists;
14604 /* Names appearing in the type-specifier should be looked up
14605 in the scope of the class. */
14606 if (current_class_type)
14610 type = TREE_TYPE (type_decl);
14611 if (TREE_CODE (type) == TYPENAME_TYPE)
14613 type = resolve_typename_type (type,
14614 /*only_current_p=*/false);
14615 if (type == error_mark_node)
14617 cp_parser_abort_tentative_parse (parser);
14621 pop_p = push_scope (type);
14624 /* Inside the constructor parameter list, surrounding
14625 template-parameter-lists do not apply. */
14626 saved_num_template_parameter_lists
14627 = parser->num_template_parameter_lists;
14628 parser->num_template_parameter_lists = 0;
14630 /* Look for the type-specifier. */
14631 cp_parser_type_specifier (parser,
14632 CP_PARSER_FLAGS_NONE,
14633 /*decl_specs=*/NULL,
14634 /*is_declarator=*/true,
14635 /*declares_class_or_enum=*/NULL,
14636 /*is_cv_qualifier=*/NULL);
14638 parser->num_template_parameter_lists
14639 = saved_num_template_parameter_lists;
14641 /* Leave the scope of the class. */
14645 constructor_p = !cp_parser_error_occurred (parser);
14649 constructor_p = false;
14650 /* We did not really want to consume any tokens. */
14651 cp_parser_abort_tentative_parse (parser);
14653 return constructor_p;
14656 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14657 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14658 they must be performed once we are in the scope of the function.
14660 Returns the function defined. */
14663 cp_parser_function_definition_from_specifiers_and_declarator
14664 (cp_parser* parser,
14665 cp_decl_specifier_seq *decl_specifiers,
14667 const cp_declarator *declarator)
14672 /* Begin the function-definition. */
14673 success_p = start_function (decl_specifiers, declarator, attributes);
14675 /* The things we're about to see are not directly qualified by any
14676 template headers we've seen thus far. */
14677 reset_specialization ();
14679 /* If there were names looked up in the decl-specifier-seq that we
14680 did not check, check them now. We must wait until we are in the
14681 scope of the function to perform the checks, since the function
14682 might be a friend. */
14683 perform_deferred_access_checks ();
14687 /* Skip the entire function. */
14688 error ("invalid function declaration");
14689 cp_parser_skip_to_end_of_block_or_statement (parser);
14690 fn = error_mark_node;
14693 fn = cp_parser_function_definition_after_declarator (parser,
14694 /*inline_p=*/false);
14699 /* Parse the part of a function-definition that follows the
14700 declarator. INLINE_P is TRUE iff this function is an inline
14701 function defined with a class-specifier.
14703 Returns the function defined. */
14706 cp_parser_function_definition_after_declarator (cp_parser* parser,
14710 bool ctor_initializer_p = false;
14711 bool saved_in_unbraced_linkage_specification_p;
14712 unsigned saved_num_template_parameter_lists;
14714 /* If the next token is `return', then the code may be trying to
14715 make use of the "named return value" extension that G++ used to
14717 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14719 /* Consume the `return' keyword. */
14720 cp_lexer_consume_token (parser->lexer);
14721 /* Look for the identifier that indicates what value is to be
14723 cp_parser_identifier (parser);
14724 /* Issue an error message. */
14725 error ("named return values are no longer supported");
14726 /* Skip tokens until we reach the start of the function body. */
14727 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14728 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14729 cp_lexer_consume_token (parser->lexer);
14731 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14732 anything declared inside `f'. */
14733 saved_in_unbraced_linkage_specification_p
14734 = parser->in_unbraced_linkage_specification_p;
14735 parser->in_unbraced_linkage_specification_p = false;
14736 /* Inside the function, surrounding template-parameter-lists do not
14738 saved_num_template_parameter_lists
14739 = parser->num_template_parameter_lists;
14740 parser->num_template_parameter_lists = 0;
14741 /* If the next token is `try', then we are looking at a
14742 function-try-block. */
14743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14744 ctor_initializer_p = cp_parser_function_try_block (parser);
14745 /* A function-try-block includes the function-body, so we only do
14746 this next part if we're not processing a function-try-block. */
14749 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14751 /* Finish the function. */
14752 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14753 (inline_p ? 2 : 0));
14754 /* Generate code for it, if necessary. */
14755 expand_or_defer_fn (fn);
14756 /* Restore the saved values. */
14757 parser->in_unbraced_linkage_specification_p
14758 = saved_in_unbraced_linkage_specification_p;
14759 parser->num_template_parameter_lists
14760 = saved_num_template_parameter_lists;
14765 /* Parse a template-declaration, assuming that the `export' (and
14766 `extern') keywords, if present, has already been scanned. MEMBER_P
14767 is as for cp_parser_template_declaration. */
14770 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14772 tree decl = NULL_TREE;
14773 tree parameter_list;
14774 bool friend_p = false;
14776 /* Look for the `template' keyword. */
14777 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14781 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14784 /* If the next token is `>', then we have an invalid
14785 specialization. Rather than complain about an invalid template
14786 parameter, issue an error message here. */
14787 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14789 cp_parser_error (parser, "invalid explicit specialization");
14790 begin_specialization ();
14791 parameter_list = NULL_TREE;
14795 /* Parse the template parameters. */
14796 begin_template_parm_list ();
14797 parameter_list = cp_parser_template_parameter_list (parser);
14798 parameter_list = end_template_parm_list (parameter_list);
14801 /* Look for the `>'. */
14802 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14803 /* We just processed one more parameter list. */
14804 ++parser->num_template_parameter_lists;
14805 /* If the next token is `template', there are more template
14807 if (cp_lexer_next_token_is_keyword (parser->lexer,
14809 cp_parser_template_declaration_after_export (parser, member_p);
14812 /* There are no access checks when parsing a template, as we do not
14813 know if a specialization will be a friend. */
14814 push_deferring_access_checks (dk_no_check);
14816 decl = cp_parser_single_declaration (parser,
14820 pop_deferring_access_checks ();
14822 /* If this is a member template declaration, let the front
14824 if (member_p && !friend_p && decl)
14826 if (TREE_CODE (decl) == TYPE_DECL)
14827 cp_parser_check_access_in_redeclaration (decl);
14829 decl = finish_member_template_decl (decl);
14831 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14832 make_friend_class (current_class_type, TREE_TYPE (decl),
14833 /*complain=*/true);
14835 /* We are done with the current parameter list. */
14836 --parser->num_template_parameter_lists;
14839 finish_template_decl (parameter_list);
14841 /* Register member declarations. */
14842 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14843 finish_member_declaration (decl);
14845 /* If DECL is a function template, we must return to parse it later.
14846 (Even though there is no definition, there might be default
14847 arguments that need handling.) */
14848 if (member_p && decl
14849 && (TREE_CODE (decl) == FUNCTION_DECL
14850 || DECL_FUNCTION_TEMPLATE_P (decl)))
14851 TREE_VALUE (parser->unparsed_functions_queues)
14852 = tree_cons (NULL_TREE, decl,
14853 TREE_VALUE (parser->unparsed_functions_queues));
14856 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14857 `function-definition' sequence. MEMBER_P is true, this declaration
14858 appears in a class scope.
14860 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14861 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14864 cp_parser_single_declaration (cp_parser* parser,
14868 int declares_class_or_enum;
14869 tree decl = NULL_TREE;
14870 cp_decl_specifier_seq decl_specifiers;
14871 bool function_definition_p = false;
14873 /* This function is only used when processing a template
14875 gcc_assert (innermost_scope_kind () == sk_template_parms
14876 || innermost_scope_kind () == sk_template_spec);
14878 /* Defer access checks until we know what is being declared. */
14879 push_deferring_access_checks (dk_deferred);
14881 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14883 cp_parser_decl_specifier_seq (parser,
14884 CP_PARSER_FLAGS_OPTIONAL,
14886 &declares_class_or_enum);
14888 *friend_p = cp_parser_friend_p (&decl_specifiers);
14890 /* There are no template typedefs. */
14891 if (decl_specifiers.specs[(int) ds_typedef])
14893 error ("template declaration of %qs", "typedef");
14894 decl = error_mark_node;
14897 /* Gather up the access checks that occurred the
14898 decl-specifier-seq. */
14899 stop_deferring_access_checks ();
14901 /* Check for the declaration of a template class. */
14902 if (declares_class_or_enum)
14904 if (cp_parser_declares_only_class_p (parser))
14906 decl = shadow_tag (&decl_specifiers);
14911 friend template <typename T> struct A<T>::B;
14914 A<T>::B will be represented by a TYPENAME_TYPE, and
14915 therefore not recognized by shadow_tag. */
14916 if (friend_p && *friend_p
14918 && decl_specifiers.type
14919 && TYPE_P (decl_specifiers.type))
14920 decl = decl_specifiers.type;
14922 if (decl && decl != error_mark_node)
14923 decl = TYPE_NAME (decl);
14925 decl = error_mark_node;
14928 /* If it's not a template class, try for a template function. If
14929 the next token is a `;', then this declaration does not declare
14930 anything. But, if there were errors in the decl-specifiers, then
14931 the error might well have come from an attempted class-specifier.
14932 In that case, there's no need to warn about a missing declarator. */
14934 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14935 || decl_specifiers.type != error_mark_node))
14936 decl = cp_parser_init_declarator (parser,
14938 /*function_definition_allowed_p=*/true,
14940 declares_class_or_enum,
14941 &function_definition_p);
14943 pop_deferring_access_checks ();
14945 /* Clear any current qualification; whatever comes next is the start
14946 of something new. */
14947 parser->scope = NULL_TREE;
14948 parser->qualifying_scope = NULL_TREE;
14949 parser->object_scope = NULL_TREE;
14950 /* Look for a trailing `;' after the declaration. */
14951 if (!function_definition_p
14952 && (decl == error_mark_node
14953 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
14954 cp_parser_skip_to_end_of_block_or_statement (parser);
14959 /* Parse a cast-expression that is not the operand of a unary "&". */
14962 cp_parser_simple_cast_expression (cp_parser *parser)
14964 return cp_parser_cast_expression (parser, /*address_p=*/false);
14967 /* Parse a functional cast to TYPE. Returns an expression
14968 representing the cast. */
14971 cp_parser_functional_cast (cp_parser* parser, tree type)
14973 tree expression_list;
14977 = cp_parser_parenthesized_expression_list (parser, false,
14978 /*non_constant_p=*/NULL);
14980 cast = build_functional_cast (type, expression_list);
14981 /* [expr.const]/1: In an integral constant expression "only type
14982 conversions to integral or enumeration type can be used". */
14983 if (cast != error_mark_node && !type_dependent_expression_p (type)
14984 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14986 if (cp_parser_non_integral_constant_expression
14987 (parser, "a call to a constructor"))
14988 return error_mark_node;
14993 /* Save the tokens that make up the body of a member function defined
14994 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14995 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14996 specifiers applied to the declaration. Returns the FUNCTION_DECL
14997 for the member function. */
15000 cp_parser_save_member_function_body (cp_parser* parser,
15001 cp_decl_specifier_seq *decl_specifiers,
15002 cp_declarator *declarator,
15009 /* Create the function-declaration. */
15010 fn = start_method (decl_specifiers, declarator, attributes);
15011 /* If something went badly wrong, bail out now. */
15012 if (fn == error_mark_node)
15014 /* If there's a function-body, skip it. */
15015 if (cp_parser_token_starts_function_definition_p
15016 (cp_lexer_peek_token (parser->lexer)))
15017 cp_parser_skip_to_end_of_block_or_statement (parser);
15018 return error_mark_node;
15021 /* Remember it, if there default args to post process. */
15022 cp_parser_save_default_args (parser, fn);
15024 /* Save away the tokens that make up the body of the
15026 first = parser->lexer->next_token;
15027 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15028 /* Handle function try blocks. */
15029 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15030 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15031 last = parser->lexer->next_token;
15033 /* Save away the inline definition; we will process it when the
15034 class is complete. */
15035 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15036 DECL_PENDING_INLINE_P (fn) = 1;
15038 /* We need to know that this was defined in the class, so that
15039 friend templates are handled correctly. */
15040 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15042 /* We're done with the inline definition. */
15043 finish_method (fn);
15045 /* Add FN to the queue of functions to be parsed later. */
15046 TREE_VALUE (parser->unparsed_functions_queues)
15047 = tree_cons (NULL_TREE, fn,
15048 TREE_VALUE (parser->unparsed_functions_queues));
15053 /* Parse a template-argument-list, as well as the trailing ">" (but
15054 not the opening ">"). See cp_parser_template_argument_list for the
15058 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15062 tree saved_qualifying_scope;
15063 tree saved_object_scope;
15064 bool saved_greater_than_is_operator_p;
15068 When parsing a template-id, the first non-nested `>' is taken as
15069 the end of the template-argument-list rather than a greater-than
15071 saved_greater_than_is_operator_p
15072 = parser->greater_than_is_operator_p;
15073 parser->greater_than_is_operator_p = false;
15074 /* Parsing the argument list may modify SCOPE, so we save it
15076 saved_scope = parser->scope;
15077 saved_qualifying_scope = parser->qualifying_scope;
15078 saved_object_scope = parser->object_scope;
15079 /* Parse the template-argument-list itself. */
15080 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15081 arguments = NULL_TREE;
15083 arguments = cp_parser_template_argument_list (parser);
15084 /* Look for the `>' that ends the template-argument-list. If we find
15085 a '>>' instead, it's probably just a typo. */
15086 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15088 if (!saved_greater_than_is_operator_p)
15090 /* If we're in a nested template argument list, the '>>' has
15091 to be a typo for '> >'. We emit the error message, but we
15092 continue parsing and we push a '>' as next token, so that
15093 the argument list will be parsed correctly. Note that the
15094 global source location is still on the token before the
15095 '>>', so we need to say explicitly where we want it. */
15096 cp_token *token = cp_lexer_peek_token (parser->lexer);
15097 error ("%H%<>>%> should be %<> >%> "
15098 "within a nested template argument list",
15101 /* ??? Proper recovery should terminate two levels of
15102 template argument list here. */
15103 token->type = CPP_GREATER;
15107 /* If this is not a nested template argument list, the '>>'
15108 is a typo for '>'. Emit an error message and continue.
15109 Same deal about the token location, but here we can get it
15110 right by consuming the '>>' before issuing the diagnostic. */
15111 cp_lexer_consume_token (parser->lexer);
15112 error ("spurious %<>>%>, use %<>%> to terminate "
15113 "a template argument list");
15116 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15117 error ("missing %<>%> to terminate the template argument list");
15119 /* It's what we want, a '>'; consume it. */
15120 cp_lexer_consume_token (parser->lexer);
15121 /* The `>' token might be a greater-than operator again now. */
15122 parser->greater_than_is_operator_p
15123 = saved_greater_than_is_operator_p;
15124 /* Restore the SAVED_SCOPE. */
15125 parser->scope = saved_scope;
15126 parser->qualifying_scope = saved_qualifying_scope;
15127 parser->object_scope = saved_object_scope;
15132 /* MEMBER_FUNCTION is a member function, or a friend. If default
15133 arguments, or the body of the function have not yet been parsed,
15137 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15139 /* If this member is a template, get the underlying
15141 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15142 member_function = DECL_TEMPLATE_RESULT (member_function);
15144 /* There should not be any class definitions in progress at this
15145 point; the bodies of members are only parsed outside of all class
15147 gcc_assert (parser->num_classes_being_defined == 0);
15148 /* While we're parsing the member functions we might encounter more
15149 classes. We want to handle them right away, but we don't want
15150 them getting mixed up with functions that are currently in the
15152 parser->unparsed_functions_queues
15153 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15155 /* Make sure that any template parameters are in scope. */
15156 maybe_begin_member_template_processing (member_function);
15158 /* If the body of the function has not yet been parsed, parse it
15160 if (DECL_PENDING_INLINE_P (member_function))
15162 tree function_scope;
15163 cp_token_cache *tokens;
15165 /* The function is no longer pending; we are processing it. */
15166 tokens = DECL_PENDING_INLINE_INFO (member_function);
15167 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15168 DECL_PENDING_INLINE_P (member_function) = 0;
15169 /* If this was an inline function in a local class, enter the scope
15170 of the containing function. */
15171 function_scope = decl_function_context (member_function);
15172 if (function_scope)
15173 push_function_context_to (function_scope);
15175 /* Push the body of the function onto the lexer stack. */
15176 cp_parser_push_lexer_for_tokens (parser, tokens);
15178 /* Let the front end know that we going to be defining this
15180 start_preparsed_function (member_function, NULL_TREE,
15181 SF_PRE_PARSED | SF_INCLASS_INLINE);
15183 /* Now, parse the body of the function. */
15184 cp_parser_function_definition_after_declarator (parser,
15185 /*inline_p=*/true);
15187 /* Leave the scope of the containing function. */
15188 if (function_scope)
15189 pop_function_context_from (function_scope);
15190 cp_parser_pop_lexer (parser);
15193 /* Remove any template parameters from the symbol table. */
15194 maybe_end_member_template_processing ();
15196 /* Restore the queue. */
15197 parser->unparsed_functions_queues
15198 = TREE_CHAIN (parser->unparsed_functions_queues);
15201 /* If DECL contains any default args, remember it on the unparsed
15202 functions queue. */
15205 cp_parser_save_default_args (cp_parser* parser, tree decl)
15209 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15211 probe = TREE_CHAIN (probe))
15212 if (TREE_PURPOSE (probe))
15214 TREE_PURPOSE (parser->unparsed_functions_queues)
15215 = tree_cons (current_class_type, decl,
15216 TREE_PURPOSE (parser->unparsed_functions_queues));
15222 /* FN is a FUNCTION_DECL which may contains a parameter with an
15223 unparsed DEFAULT_ARG. Parse the default args now. This function
15224 assumes that the current scope is the scope in which the default
15225 argument should be processed. */
15228 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15230 bool saved_local_variables_forbidden_p;
15233 /* While we're parsing the default args, we might (due to the
15234 statement expression extension) encounter more classes. We want
15235 to handle them right away, but we don't want them getting mixed
15236 up with default args that are currently in the queue. */
15237 parser->unparsed_functions_queues
15238 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15240 /* Local variable names (and the `this' keyword) may not appear
15241 in a default argument. */
15242 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15243 parser->local_variables_forbidden_p = true;
15245 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15247 parm = TREE_CHAIN (parm))
15249 cp_token_cache *tokens;
15251 if (!TREE_PURPOSE (parm)
15252 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15255 /* Push the saved tokens for the default argument onto the parser's
15257 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15258 cp_parser_push_lexer_for_tokens (parser, tokens);
15260 /* Parse the assignment-expression. */
15261 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15263 /* If the token stream has not been completely used up, then
15264 there was extra junk after the end of the default
15266 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15267 cp_parser_error (parser, "expected %<,%>");
15269 /* Revert to the main lexer. */
15270 cp_parser_pop_lexer (parser);
15273 /* Restore the state of local_variables_forbidden_p. */
15274 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15276 /* Restore the queue. */
15277 parser->unparsed_functions_queues
15278 = TREE_CHAIN (parser->unparsed_functions_queues);
15281 /* Parse the operand of `sizeof' (or a similar operator). Returns
15282 either a TYPE or an expression, depending on the form of the
15283 input. The KEYWORD indicates which kind of expression we have
15287 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15289 static const char *format;
15290 tree expr = NULL_TREE;
15291 const char *saved_message;
15292 bool saved_integral_constant_expression_p;
15294 /* Initialize FORMAT the first time we get here. */
15296 format = "types may not be defined in '%s' expressions";
15298 /* Types cannot be defined in a `sizeof' expression. Save away the
15300 saved_message = parser->type_definition_forbidden_message;
15301 /* And create the new one. */
15302 parser->type_definition_forbidden_message
15303 = xmalloc (strlen (format)
15304 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15306 sprintf ((char *) parser->type_definition_forbidden_message,
15307 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15309 /* The restrictions on constant-expressions do not apply inside
15310 sizeof expressions. */
15311 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15312 parser->integral_constant_expression_p = false;
15314 /* Do not actually evaluate the expression. */
15316 /* If it's a `(', then we might be looking at the type-id
15318 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15321 bool saved_in_type_id_in_expr_p;
15323 /* We can't be sure yet whether we're looking at a type-id or an
15325 cp_parser_parse_tentatively (parser);
15326 /* Consume the `('. */
15327 cp_lexer_consume_token (parser->lexer);
15328 /* Parse the type-id. */
15329 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15330 parser->in_type_id_in_expr_p = true;
15331 type = cp_parser_type_id (parser);
15332 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15333 /* Now, look for the trailing `)'. */
15334 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15335 /* If all went well, then we're done. */
15336 if (cp_parser_parse_definitely (parser))
15338 cp_decl_specifier_seq decl_specs;
15340 /* Build a trivial decl-specifier-seq. */
15341 clear_decl_specs (&decl_specs);
15342 decl_specs.type = type;
15344 /* Call grokdeclarator to figure out what type this is. */
15345 expr = grokdeclarator (NULL,
15349 /*attrlist=*/NULL);
15353 /* If the type-id production did not work out, then we must be
15354 looking at the unary-expression production. */
15356 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15357 /* Go back to evaluating expressions. */
15360 /* Free the message we created. */
15361 free ((char *) parser->type_definition_forbidden_message);
15362 /* And restore the old one. */
15363 parser->type_definition_forbidden_message = saved_message;
15364 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15369 /* If the current declaration has no declarator, return true. */
15372 cp_parser_declares_only_class_p (cp_parser *parser)
15374 /* If the next token is a `;' or a `,' then there is no
15376 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15377 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15380 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15383 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15384 cp_storage_class storage_class)
15386 if (decl_specs->storage_class != sc_none)
15387 decl_specs->multiple_storage_classes_p = true;
15389 decl_specs->storage_class = storage_class;
15392 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15393 is true, the type is a user-defined type; otherwise it is a
15394 built-in type specified by a keyword. */
15397 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15399 bool user_defined_p)
15401 decl_specs->any_specifiers_p = true;
15403 /* If the user tries to redeclare bool or wchar_t (with, for
15404 example, in "typedef int wchar_t;") we remember that this is what
15405 happened. In system headers, we ignore these declarations so
15406 that G++ can work with system headers that are not C++-safe. */
15407 if (decl_specs->specs[(int) ds_typedef]
15409 && (type_spec == boolean_type_node
15410 || type_spec == wchar_type_node)
15411 && (decl_specs->type
15412 || decl_specs->specs[(int) ds_long]
15413 || decl_specs->specs[(int) ds_short]
15414 || decl_specs->specs[(int) ds_unsigned]
15415 || decl_specs->specs[(int) ds_signed]))
15417 decl_specs->redefined_builtin_type = type_spec;
15418 if (!decl_specs->type)
15420 decl_specs->type = type_spec;
15421 decl_specs->user_defined_type_p = false;
15424 else if (decl_specs->type)
15425 decl_specs->multiple_types_p = true;
15428 decl_specs->type = type_spec;
15429 decl_specs->user_defined_type_p = user_defined_p;
15430 decl_specs->redefined_builtin_type = NULL_TREE;
15434 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15435 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15438 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15440 return decl_specifiers->specs[(int) ds_friend] != 0;
15443 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15444 issue an error message indicating that TOKEN_DESC was expected.
15446 Returns the token consumed, if the token had the appropriate type.
15447 Otherwise, returns NULL. */
15450 cp_parser_require (cp_parser* parser,
15451 enum cpp_ttype type,
15452 const char* token_desc)
15454 if (cp_lexer_next_token_is (parser->lexer, type))
15455 return cp_lexer_consume_token (parser->lexer);
15458 /* Output the MESSAGE -- unless we're parsing tentatively. */
15459 if (!cp_parser_simulate_error (parser))
15461 char *message = concat ("expected ", token_desc, NULL);
15462 cp_parser_error (parser, message);
15469 /* Like cp_parser_require, except that tokens will be skipped until
15470 the desired token is found. An error message is still produced if
15471 the next token is not as expected. */
15474 cp_parser_skip_until_found (cp_parser* parser,
15475 enum cpp_ttype type,
15476 const char* token_desc)
15479 unsigned nesting_depth = 0;
15481 if (cp_parser_require (parser, type, token_desc))
15484 /* Skip tokens until the desired token is found. */
15487 /* Peek at the next token. */
15488 token = cp_lexer_peek_token (parser->lexer);
15489 /* If we've reached the token we want, consume it and
15491 if (token->type == type && !nesting_depth)
15493 cp_lexer_consume_token (parser->lexer);
15496 /* If we've run out of tokens, stop. */
15497 if (token->type == CPP_EOF)
15499 if (token->type == CPP_OPEN_BRACE
15500 || token->type == CPP_OPEN_PAREN
15501 || token->type == CPP_OPEN_SQUARE)
15503 else if (token->type == CPP_CLOSE_BRACE
15504 || token->type == CPP_CLOSE_PAREN
15505 || token->type == CPP_CLOSE_SQUARE)
15507 if (nesting_depth-- == 0)
15510 /* Consume this token. */
15511 cp_lexer_consume_token (parser->lexer);
15515 /* If the next token is the indicated keyword, consume it. Otherwise,
15516 issue an error message indicating that TOKEN_DESC was expected.
15518 Returns the token consumed, if the token had the appropriate type.
15519 Otherwise, returns NULL. */
15522 cp_parser_require_keyword (cp_parser* parser,
15524 const char* token_desc)
15526 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15528 if (token && token->keyword != keyword)
15530 dyn_string_t error_msg;
15532 /* Format the error message. */
15533 error_msg = dyn_string_new (0);
15534 dyn_string_append_cstr (error_msg, "expected ");
15535 dyn_string_append_cstr (error_msg, token_desc);
15536 cp_parser_error (parser, error_msg->s);
15537 dyn_string_delete (error_msg);
15544 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15545 function-definition. */
15548 cp_parser_token_starts_function_definition_p (cp_token* token)
15550 return (/* An ordinary function-body begins with an `{'. */
15551 token->type == CPP_OPEN_BRACE
15552 /* A ctor-initializer begins with a `:'. */
15553 || token->type == CPP_COLON
15554 /* A function-try-block begins with `try'. */
15555 || token->keyword == RID_TRY
15556 /* The named return value extension begins with `return'. */
15557 || token->keyword == RID_RETURN);
15560 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15564 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15568 token = cp_lexer_peek_token (parser->lexer);
15569 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15572 /* Returns TRUE iff the next token is the "," or ">" ending a
15573 template-argument. */
15576 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15580 token = cp_lexer_peek_token (parser->lexer);
15581 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15584 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15585 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15588 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15593 token = cp_lexer_peek_nth_token (parser->lexer, n);
15594 if (token->type == CPP_LESS)
15596 /* Check for the sequence `<::' in the original code. It would be lexed as
15597 `[:', where `[' is a digraph, and there is no whitespace before
15599 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15602 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15603 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15609 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15610 or none_type otherwise. */
15612 static enum tag_types
15613 cp_parser_token_is_class_key (cp_token* token)
15615 switch (token->keyword)
15620 return record_type;
15629 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15632 cp_parser_check_class_key (enum tag_types class_key, tree type)
15634 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15635 pedwarn ("%qs tag used in naming %q#T",
15636 class_key == union_type ? "union"
15637 : class_key == record_type ? "struct" : "class",
15641 /* Issue an error message if DECL is redeclared with different
15642 access than its original declaration [class.access.spec/3].
15643 This applies to nested classes and nested class templates.
15647 cp_parser_check_access_in_redeclaration (tree decl)
15649 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15652 if ((TREE_PRIVATE (decl)
15653 != (current_access_specifier == access_private_node))
15654 || (TREE_PROTECTED (decl)
15655 != (current_access_specifier == access_protected_node)))
15656 error ("%qD redeclared with different access", decl);
15659 /* Look for the `template' keyword, as a syntactic disambiguator.
15660 Return TRUE iff it is present, in which case it will be
15664 cp_parser_optional_template_keyword (cp_parser *parser)
15666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15668 /* The `template' keyword can only be used within templates;
15669 outside templates the parser can always figure out what is a
15670 template and what is not. */
15671 if (!processing_template_decl)
15673 error ("%<template%> (as a disambiguator) is only allowed "
15674 "within templates");
15675 /* If this part of the token stream is rescanned, the same
15676 error message would be generated. So, we purge the token
15677 from the stream. */
15678 cp_lexer_purge_token (parser->lexer);
15683 /* Consume the `template' keyword. */
15684 cp_lexer_consume_token (parser->lexer);
15692 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15693 set PARSER->SCOPE, and perform other related actions. */
15696 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15701 /* Get the stored value. */
15702 value = cp_lexer_consume_token (parser->lexer)->value;
15703 /* Perform any access checks that were deferred. */
15704 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15705 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15706 /* Set the scope from the stored value. */
15707 parser->scope = TREE_VALUE (value);
15708 parser->qualifying_scope = TREE_TYPE (value);
15709 parser->object_scope = NULL_TREE;
15712 /* Consume tokens up through a non-nested END token. */
15715 cp_parser_cache_group (cp_parser *parser,
15716 enum cpp_ttype end,
15723 /* Abort a parenthesized expression if we encounter a brace. */
15724 if ((end == CPP_CLOSE_PAREN || depth == 0)
15725 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15727 /* If we've reached the end of the file, stop. */
15728 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15730 /* Consume the next token. */
15731 token = cp_lexer_consume_token (parser->lexer);
15732 /* See if it starts a new group. */
15733 if (token->type == CPP_OPEN_BRACE)
15735 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15739 else if (token->type == CPP_OPEN_PAREN)
15740 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15741 else if (token->type == end)
15746 /* Begin parsing tentatively. We always save tokens while parsing
15747 tentatively so that if the tentative parsing fails we can restore the
15751 cp_parser_parse_tentatively (cp_parser* parser)
15753 /* Enter a new parsing context. */
15754 parser->context = cp_parser_context_new (parser->context);
15755 /* Begin saving tokens. */
15756 cp_lexer_save_tokens (parser->lexer);
15757 /* In order to avoid repetitive access control error messages,
15758 access checks are queued up until we are no longer parsing
15760 push_deferring_access_checks (dk_deferred);
15763 /* Commit to the currently active tentative parse. */
15766 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15768 cp_parser_context *context;
15771 /* Mark all of the levels as committed. */
15772 lexer = parser->lexer;
15773 for (context = parser->context; context->next; context = context->next)
15775 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15777 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15778 while (!cp_lexer_saving_tokens (lexer))
15779 lexer = lexer->next;
15780 cp_lexer_commit_tokens (lexer);
15784 /* Abort the currently active tentative parse. All consumed tokens
15785 will be rolled back, and no diagnostics will be issued. */
15788 cp_parser_abort_tentative_parse (cp_parser* parser)
15790 cp_parser_simulate_error (parser);
15791 /* Now, pretend that we want to see if the construct was
15792 successfully parsed. */
15793 cp_parser_parse_definitely (parser);
15796 /* Stop parsing tentatively. If a parse error has occurred, restore the
15797 token stream. Otherwise, commit to the tokens we have consumed.
15798 Returns true if no error occurred; false otherwise. */
15801 cp_parser_parse_definitely (cp_parser* parser)
15803 bool error_occurred;
15804 cp_parser_context *context;
15806 /* Remember whether or not an error occurred, since we are about to
15807 destroy that information. */
15808 error_occurred = cp_parser_error_occurred (parser);
15809 /* Remove the topmost context from the stack. */
15810 context = parser->context;
15811 parser->context = context->next;
15812 /* If no parse errors occurred, commit to the tentative parse. */
15813 if (!error_occurred)
15815 /* Commit to the tokens read tentatively, unless that was
15817 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15818 cp_lexer_commit_tokens (parser->lexer);
15820 pop_to_parent_deferring_access_checks ();
15822 /* Otherwise, if errors occurred, roll back our state so that things
15823 are just as they were before we began the tentative parse. */
15826 cp_lexer_rollback_tokens (parser->lexer);
15827 pop_deferring_access_checks ();
15829 /* Add the context to the front of the free list. */
15830 context->next = cp_parser_context_free_list;
15831 cp_parser_context_free_list = context;
15833 return !error_occurred;
15836 /* Returns true if we are parsing tentatively and are not committed to
15837 this tentative parse. */
15840 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
15842 return (cp_parser_parsing_tentatively (parser)
15843 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
15846 /* Returns nonzero iff an error has occurred during the most recent
15847 tentative parse. */
15850 cp_parser_error_occurred (cp_parser* parser)
15852 return (cp_parser_parsing_tentatively (parser)
15853 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15856 /* Returns nonzero if GNU extensions are allowed. */
15859 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15861 return parser->allow_gnu_extensions_p;
15867 static GTY (()) cp_parser *the_parser;
15869 /* External interface. */
15871 /* Parse one entire translation unit. */
15874 c_parse_file (void)
15876 bool error_occurred;
15877 static bool already_called = false;
15879 if (already_called)
15881 sorry ("inter-module optimizations not implemented for C++");
15884 already_called = true;
15886 the_parser = cp_parser_new ();
15887 push_deferring_access_checks (flag_access_control
15888 ? dk_no_deferred : dk_no_check);
15889 error_occurred = cp_parser_translation_unit (the_parser);
15893 /* This variable must be provided by every front end. */
15897 #include "gt-cp-parser.h"