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, bool, 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, bool, 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_committed_to_tentative_parse
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 *, int);
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 ("`%D' 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_parsing_tentatively (parser)
1842 && !cp_parser_committed_to_tentative_parse (parser))
1844 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1850 /* This function is called when a type is defined. If type
1851 definitions are forbidden at this point, an error message is
1855 cp_parser_check_type_definition (cp_parser* parser)
1857 /* If types are forbidden here, issue a message. */
1858 if (parser->type_definition_forbidden_message)
1859 /* Use `%s' to print the string in case there are any escape
1860 characters in the message. */
1861 error ("%s", parser->type_definition_forbidden_message);
1864 /* This function is called when a declaration is parsed. If
1865 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1866 indicates that a type was defined in the decl-specifiers for DECL,
1867 then an error is issued. */
1870 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1871 int declares_class_or_enum)
1873 /* [dcl.fct] forbids type definitions in return types.
1874 Unfortunately, it's not easy to know whether or not we are
1875 processing a return type until after the fact. */
1877 && (declarator->kind == cdk_pointer
1878 || declarator->kind == cdk_reference
1879 || declarator->kind == cdk_ptrmem))
1880 declarator = declarator->declarator;
1882 && declarator->kind == cdk_function
1883 && declares_class_or_enum & 2)
1884 error ("new types may not be defined in a return type");
1887 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1888 "<" in any valid C++ program. If the next token is indeed "<",
1889 issue a message warning the user about what appears to be an
1890 invalid attempt to form a template-id. */
1893 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1896 cp_token_position start = 0;
1898 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1901 error ("%qT is not a template", type);
1902 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1903 error ("%qE is not a template", type);
1905 error ("invalid template-id");
1906 /* Remember the location of the invalid "<". */
1907 if (cp_parser_parsing_tentatively (parser)
1908 && !cp_parser_committed_to_tentative_parse (parser))
1909 start = cp_lexer_token_position (parser->lexer, true);
1910 /* Consume the "<". */
1911 cp_lexer_consume_token (parser->lexer);
1912 /* Parse the template arguments. */
1913 cp_parser_enclosed_template_argument_list (parser);
1914 /* Permanently remove the invalid template arguments so that
1915 this error message is not issued again. */
1917 cp_lexer_purge_tokens_after (parser->lexer, start);
1921 /* If parsing an integral constant-expression, issue an error message
1922 about the fact that THING appeared and return true. Otherwise,
1923 return false, marking the current expression as non-constant. */
1926 cp_parser_non_integral_constant_expression (cp_parser *parser,
1929 if (parser->integral_constant_expression_p)
1931 if (!parser->allow_non_integral_constant_expression_p)
1933 error ("%s cannot appear in a constant-expression", thing);
1936 parser->non_integral_constant_expression_p = true;
1941 /* Emit a diagnostic for an invalid type name. Consider also if it is
1942 qualified or not and the result of a lookup, to provide a better
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 && cp_parser_parsing_tentatively (parser)
2077 && !cp_parser_committed_to_tentative_parse (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, /*complain=*/0);
2299 if (result == error_mark_node)
2300 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2303 return make_typename_type (scope, id, tf_error);
2307 /* Create a new C++ parser. */
2310 cp_parser_new (void)
2316 /* cp_lexer_new_main is called before calling ggc_alloc because
2317 cp_lexer_new_main might load a PCH file. */
2318 lexer = cp_lexer_new_main ();
2320 /* Initialize the binops_by_token so that we can get the tree
2321 directly from the token. */
2322 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2323 binops_by_token[binops[i].token_type] = binops[i];
2325 parser = GGC_CNEW (cp_parser);
2326 parser->lexer = lexer;
2327 parser->context = cp_parser_context_new (NULL);
2329 /* For now, we always accept GNU extensions. */
2330 parser->allow_gnu_extensions_p = 1;
2332 /* The `>' token is a greater-than operator, not the end of a
2334 parser->greater_than_is_operator_p = true;
2336 parser->default_arg_ok_p = true;
2338 /* We are not parsing a constant-expression. */
2339 parser->integral_constant_expression_p = false;
2340 parser->allow_non_integral_constant_expression_p = false;
2341 parser->non_integral_constant_expression_p = false;
2343 /* Local variable names are not forbidden. */
2344 parser->local_variables_forbidden_p = false;
2346 /* We are not processing an `extern "C"' declaration. */
2347 parser->in_unbraced_linkage_specification_p = false;
2349 /* We are not processing a declarator. */
2350 parser->in_declarator_p = false;
2352 /* We are not processing a template-argument-list. */
2353 parser->in_template_argument_list_p = false;
2355 /* We are not in an iteration statement. */
2356 parser->in_iteration_statement_p = false;
2358 /* We are not in a switch statement. */
2359 parser->in_switch_statement_p = false;
2361 /* We are not parsing a type-id inside an expression. */
2362 parser->in_type_id_in_expr_p = false;
2364 /* Declarations aren't implicitly extern "C". */
2365 parser->implicit_extern_c = false;
2367 /* String literals should be translated to the execution character set. */
2368 parser->translate_strings_p = true;
2370 /* The unparsed function queue is empty. */
2371 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2373 /* There are no classes being defined. */
2374 parser->num_classes_being_defined = 0;
2376 /* No template parameters apply. */
2377 parser->num_template_parameter_lists = 0;
2382 /* Create a cp_lexer structure which will emit the tokens in CACHE
2383 and push it onto the parser's lexer stack. This is used for delayed
2384 parsing of in-class method bodies and default arguments, and should
2385 not be confused with tentative parsing. */
2387 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2389 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2390 lexer->next = parser->lexer;
2391 parser->lexer = lexer;
2393 /* Move the current source position to that of the first token in the
2395 cp_lexer_set_source_position_from_token (lexer->next_token);
2398 /* Pop the top lexer off the parser stack. This is never used for the
2399 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2401 cp_parser_pop_lexer (cp_parser *parser)
2403 cp_lexer *lexer = parser->lexer;
2404 parser->lexer = lexer->next;
2405 cp_lexer_destroy (lexer);
2407 /* Put the current source position back where it was before this
2408 lexer was pushed. */
2409 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2412 /* Lexical conventions [gram.lex] */
2414 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2418 cp_parser_identifier (cp_parser* parser)
2422 /* Look for the identifier. */
2423 token = cp_parser_require (parser, CPP_NAME, "identifier");
2424 /* Return the value. */
2425 return token ? token->value : error_mark_node;
2428 /* Parse a sequence of adjacent string constants. Returns a
2429 TREE_STRING representing the combined, nul-terminated string
2430 constant. If TRANSLATE is true, translate the string to the
2431 execution character set. If WIDE_OK is true, a wide string is
2434 C++98 [lex.string] says that if a narrow string literal token is
2435 adjacent to a wide string literal token, the behavior is undefined.
2436 However, C99 6.4.5p4 says that this results in a wide string literal.
2437 We follow C99 here, for consistency with the C front end.
2439 This code is largely lifted from lex_string() in c-lex.c.
2441 FUTURE: ObjC++ will need to handle @-strings here. */
2443 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2448 struct obstack str_ob;
2449 cpp_string str, istr, *strs;
2452 tok = cp_lexer_peek_token (parser->lexer);
2453 if (!cp_parser_is_string_literal (tok))
2455 cp_parser_error (parser, "expected string-literal");
2456 return error_mark_node;
2459 /* Try to avoid the overhead of creating and destroying an obstack
2460 for the common case of just one string. */
2461 if (!cp_parser_is_string_literal
2462 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2464 cp_lexer_consume_token (parser->lexer);
2466 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2467 str.len = TREE_STRING_LENGTH (tok->value);
2469 if (tok->type == CPP_WSTRING)
2476 gcc_obstack_init (&str_ob);
2481 cp_lexer_consume_token (parser->lexer);
2483 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2484 str.len = TREE_STRING_LENGTH (tok->value);
2485 if (tok->type == CPP_WSTRING)
2488 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2490 tok = cp_lexer_peek_token (parser->lexer);
2492 while (cp_parser_is_string_literal (tok));
2494 strs = (cpp_string *) obstack_finish (&str_ob);
2497 if (wide && !wide_ok)
2499 cp_parser_error (parser, "a wide string is invalid in this context");
2503 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2504 (parse_in, strs, count, &istr, wide))
2506 value = build_string (istr.len, (char *)istr.text);
2507 free ((void *)istr.text);
2509 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2510 value = fix_string_type (value);
2513 /* cpp_interpret_string has issued an error. */
2514 value = error_mark_node;
2517 obstack_free (&str_ob, 0);
2523 /* Basic concepts [gram.basic] */
2525 /* Parse a translation-unit.
2528 declaration-seq [opt]
2530 Returns TRUE if all went well. */
2533 cp_parser_translation_unit (cp_parser* parser)
2535 /* The address of the first non-permanent object on the declarator
2537 static void *declarator_obstack_base;
2541 /* Create the declarator obstack, if necessary. */
2542 if (!cp_error_declarator)
2544 gcc_obstack_init (&declarator_obstack);
2545 /* Create the error declarator. */
2546 cp_error_declarator = make_declarator (cdk_error);
2547 /* Create the empty parameter list. */
2548 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2549 /* Remember where the base of the declarator obstack lies. */
2550 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2555 cp_parser_declaration_seq_opt (parser);
2557 /* If there are no tokens left then all went well. */
2558 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2560 /* Get rid of the token array; we don't need it any more. */
2561 cp_lexer_destroy (parser->lexer);
2562 parser->lexer = NULL;
2564 /* This file might have been a context that's implicitly extern
2565 "C". If so, pop the lang context. (Only relevant for PCH.) */
2566 if (parser->implicit_extern_c)
2568 pop_lang_context ();
2569 parser->implicit_extern_c = false;
2573 finish_translation_unit ();
2580 cp_parser_error (parser, "expected declaration");
2586 /* Make sure the declarator obstack was fully cleaned up. */
2587 gcc_assert (obstack_next_free (&declarator_obstack)
2588 == declarator_obstack_base);
2590 /* All went well. */
2594 /* Expressions [gram.expr] */
2596 /* Parse a primary-expression.
2607 ( compound-statement )
2608 __builtin_va_arg ( assignment-expression , type-id )
2613 Returns a representation of the expression.
2615 *IDK indicates what kind of id-expression (if any) was present.
2617 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2618 used as the operand of a pointer-to-member. In that case,
2619 *QUALIFYING_CLASS gives the class that is used as the qualifying
2620 class in the pointer-to-member. */
2623 cp_parser_primary_expression (cp_parser *parser,
2625 tree *qualifying_class)
2629 /* Assume the primary expression is not an id-expression. */
2630 *idk = CP_ID_KIND_NONE;
2631 /* And that it cannot be used as pointer-to-member. */
2632 *qualifying_class = NULL_TREE;
2634 /* Peek at the next token. */
2635 token = cp_lexer_peek_token (parser->lexer);
2636 switch (token->type)
2647 token = cp_lexer_consume_token (parser->lexer);
2648 return token->value;
2652 /* ??? Should wide strings be allowed when parser->translate_strings_p
2653 is false (i.e. in attributes)? If not, we can kill the third
2654 argument to cp_parser_string_literal. */
2655 return cp_parser_string_literal (parser,
2656 parser->translate_strings_p,
2659 case CPP_OPEN_PAREN:
2662 bool saved_greater_than_is_operator_p;
2664 /* Consume the `('. */
2665 cp_lexer_consume_token (parser->lexer);
2666 /* Within a parenthesized expression, a `>' token is always
2667 the greater-than operator. */
2668 saved_greater_than_is_operator_p
2669 = parser->greater_than_is_operator_p;
2670 parser->greater_than_is_operator_p = true;
2671 /* If we see `( { ' then we are looking at the beginning of
2672 a GNU statement-expression. */
2673 if (cp_parser_allow_gnu_extensions_p (parser)
2674 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2676 /* Statement-expressions are not allowed by the standard. */
2678 pedwarn ("ISO C++ forbids braced-groups within expressions");
2680 /* And they're not allowed outside of a function-body; you
2681 cannot, for example, write:
2683 int i = ({ int j = 3; j + 1; });
2685 at class or namespace scope. */
2686 if (!at_function_scope_p ())
2687 error ("statement-expressions are allowed only inside functions");
2688 /* Start the statement-expression. */
2689 expr = begin_stmt_expr ();
2690 /* Parse the compound-statement. */
2691 cp_parser_compound_statement (parser, expr, false);
2693 expr = finish_stmt_expr (expr, false);
2697 /* Parse the parenthesized expression. */
2698 expr = cp_parser_expression (parser);
2699 /* Let the front end know that this expression was
2700 enclosed in parentheses. This matters in case, for
2701 example, the expression is of the form `A::B', since
2702 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2704 finish_parenthesized_expr (expr);
2706 /* The `>' token might be the end of a template-id or
2707 template-parameter-list now. */
2708 parser->greater_than_is_operator_p
2709 = saved_greater_than_is_operator_p;
2710 /* Consume the `)'. */
2711 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2712 cp_parser_skip_to_end_of_statement (parser);
2718 switch (token->keyword)
2720 /* These two are the boolean literals. */
2722 cp_lexer_consume_token (parser->lexer);
2723 return boolean_true_node;
2725 cp_lexer_consume_token (parser->lexer);
2726 return boolean_false_node;
2728 /* The `__null' literal. */
2730 cp_lexer_consume_token (parser->lexer);
2733 /* Recognize the `this' keyword. */
2735 cp_lexer_consume_token (parser->lexer);
2736 if (parser->local_variables_forbidden_p)
2738 error ("%<this%> may not be used in this context");
2739 return error_mark_node;
2741 /* Pointers cannot appear in constant-expressions. */
2742 if (cp_parser_non_integral_constant_expression (parser,
2744 return error_mark_node;
2745 return finish_this_expr ();
2747 /* The `operator' keyword can be the beginning of an
2752 case RID_FUNCTION_NAME:
2753 case RID_PRETTY_FUNCTION_NAME:
2754 case RID_C99_FUNCTION_NAME:
2755 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2756 __func__ are the names of variables -- but they are
2757 treated specially. Therefore, they are handled here,
2758 rather than relying on the generic id-expression logic
2759 below. Grammatically, these names are id-expressions.
2761 Consume the token. */
2762 token = cp_lexer_consume_token (parser->lexer);
2763 /* Look up the name. */
2764 return finish_fname (token->value);
2771 /* The `__builtin_va_arg' construct is used to handle
2772 `va_arg'. Consume the `__builtin_va_arg' token. */
2773 cp_lexer_consume_token (parser->lexer);
2774 /* Look for the opening `('. */
2775 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2776 /* Now, parse the assignment-expression. */
2777 expression = cp_parser_assignment_expression (parser);
2778 /* Look for the `,'. */
2779 cp_parser_require (parser, CPP_COMMA, "`,'");
2780 /* Parse the type-id. */
2781 type = cp_parser_type_id (parser);
2782 /* Look for the closing `)'. */
2783 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2784 /* Using `va_arg' in a constant-expression is not
2786 if (cp_parser_non_integral_constant_expression (parser,
2788 return error_mark_node;
2789 return build_x_va_arg (expression, type);
2793 return cp_parser_builtin_offsetof (parser);
2796 cp_parser_error (parser, "expected primary-expression");
2797 return error_mark_node;
2800 /* An id-expression can start with either an identifier, a
2801 `::' as the beginning of a qualified-id, or the "operator"
2805 case CPP_TEMPLATE_ID:
2806 case CPP_NESTED_NAME_SPECIFIER:
2810 const char *error_msg;
2813 /* Parse the id-expression. */
2815 = cp_parser_id_expression (parser,
2816 /*template_keyword_p=*/false,
2817 /*check_dependency_p=*/true,
2818 /*template_p=*/NULL,
2819 /*declarator_p=*/false);
2820 if (id_expression == error_mark_node)
2821 return error_mark_node;
2822 /* If we have a template-id, then no further lookup is
2823 required. If the template-id was for a template-class, we
2824 will sometimes have a TYPE_DECL at this point. */
2825 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2826 || TREE_CODE (id_expression) == TYPE_DECL)
2827 decl = id_expression;
2828 /* Look up the name. */
2833 decl = cp_parser_lookup_name (parser, id_expression,
2835 /*is_template=*/false,
2836 /*is_namespace=*/false,
2837 /*check_dependency=*/true,
2839 /* If the lookup was ambiguous, an error will already have
2842 return error_mark_node;
2843 /* If name lookup gives us a SCOPE_REF, then the
2844 qualifying scope was dependent. Just propagate the
2846 if (TREE_CODE (decl) == SCOPE_REF)
2848 if (TYPE_P (TREE_OPERAND (decl, 0)))
2849 *qualifying_class = TREE_OPERAND (decl, 0);
2852 /* Check to see if DECL is a local variable in a context
2853 where that is forbidden. */
2854 if (parser->local_variables_forbidden_p
2855 && local_variable_p (decl))
2857 /* It might be that we only found DECL because we are
2858 trying to be generous with pre-ISO scoping rules.
2859 For example, consider:
2863 for (int i = 0; i < 10; ++i) {}
2864 extern void f(int j = i);
2867 Here, name look up will originally find the out
2868 of scope `i'. We need to issue a warning message,
2869 but then use the global `i'. */
2870 decl = check_for_out_of_scope_variable (decl);
2871 if (local_variable_p (decl))
2873 error ("local variable %qD may not appear in this context",
2875 return error_mark_node;
2880 decl = finish_id_expression (id_expression, decl, parser->scope,
2881 idk, qualifying_class,
2882 parser->integral_constant_expression_p,
2883 parser->allow_non_integral_constant_expression_p,
2884 &parser->non_integral_constant_expression_p,
2887 cp_parser_error (parser, error_msg);
2891 /* Anything else is an error. */
2893 cp_parser_error (parser, "expected primary-expression");
2894 return error_mark_node;
2898 /* Parse an id-expression.
2905 :: [opt] nested-name-specifier template [opt] unqualified-id
2907 :: operator-function-id
2910 Return a representation of the unqualified portion of the
2911 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2912 a `::' or nested-name-specifier.
2914 Often, if the id-expression was a qualified-id, the caller will
2915 want to make a SCOPE_REF to represent the qualified-id. This
2916 function does not do this in order to avoid wastefully creating
2917 SCOPE_REFs when they are not required.
2919 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2922 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2923 uninstantiated templates.
2925 If *TEMPLATE_P is non-NULL, it is set to true iff the
2926 `template' keyword is used to explicitly indicate that the entity
2927 named is a template.
2929 If DECLARATOR_P is true, the id-expression is appearing as part of
2930 a declarator, rather than as part of an expression. */
2933 cp_parser_id_expression (cp_parser *parser,
2934 bool template_keyword_p,
2935 bool check_dependency_p,
2939 bool global_scope_p;
2940 bool nested_name_specifier_p;
2942 /* Assume the `template' keyword was not used. */
2944 *template_p = false;
2946 /* Look for the optional `::' operator. */
2948 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2950 /* Look for the optional nested-name-specifier. */
2951 nested_name_specifier_p
2952 = (cp_parser_nested_name_specifier_opt (parser,
2953 /*typename_keyword_p=*/false,
2958 /* If there is a nested-name-specifier, then we are looking at
2959 the first qualified-id production. */
2960 if (nested_name_specifier_p)
2963 tree saved_object_scope;
2964 tree saved_qualifying_scope;
2965 tree unqualified_id;
2968 /* See if the next token is the `template' keyword. */
2970 template_p = &is_template;
2971 *template_p = cp_parser_optional_template_keyword (parser);
2972 /* Name lookup we do during the processing of the
2973 unqualified-id might obliterate SCOPE. */
2974 saved_scope = parser->scope;
2975 saved_object_scope = parser->object_scope;
2976 saved_qualifying_scope = parser->qualifying_scope;
2977 /* Process the final unqualified-id. */
2978 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2981 /* Restore the SAVED_SCOPE for our caller. */
2982 parser->scope = saved_scope;
2983 parser->object_scope = saved_object_scope;
2984 parser->qualifying_scope = saved_qualifying_scope;
2986 return unqualified_id;
2988 /* Otherwise, if we are in global scope, then we are looking at one
2989 of the other qualified-id productions. */
2990 else if (global_scope_p)
2995 /* Peek at the next token. */
2996 token = cp_lexer_peek_token (parser->lexer);
2998 /* If it's an identifier, and the next token is not a "<", then
2999 we can avoid the template-id case. This is an optimization
3000 for this common case. */
3001 if (token->type == CPP_NAME
3002 && !cp_parser_nth_token_starts_template_argument_list_p
3004 return cp_parser_identifier (parser);
3006 cp_parser_parse_tentatively (parser);
3007 /* Try a template-id. */
3008 id = cp_parser_template_id (parser,
3009 /*template_keyword_p=*/false,
3010 /*check_dependency_p=*/true,
3012 /* If that worked, we're done. */
3013 if (cp_parser_parse_definitely (parser))
3016 /* Peek at the next token. (Changes in the token buffer may
3017 have invalidated the pointer obtained above.) */
3018 token = cp_lexer_peek_token (parser->lexer);
3020 switch (token->type)
3023 return cp_parser_identifier (parser);
3026 if (token->keyword == RID_OPERATOR)
3027 return cp_parser_operator_function_id (parser);
3031 cp_parser_error (parser, "expected id-expression");
3032 return error_mark_node;
3036 return cp_parser_unqualified_id (parser, template_keyword_p,
3037 /*check_dependency_p=*/true,
3041 /* Parse an unqualified-id.
3045 operator-function-id
3046 conversion-function-id
3050 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3051 keyword, in a construct like `A::template ...'.
3053 Returns a representation of unqualified-id. For the `identifier'
3054 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3055 production a BIT_NOT_EXPR is returned; the operand of the
3056 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3057 other productions, see the documentation accompanying the
3058 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3059 names are looked up in uninstantiated templates. If DECLARATOR_P
3060 is true, the unqualified-id is appearing as part of a declarator,
3061 rather than as part of an expression. */
3064 cp_parser_unqualified_id (cp_parser* parser,
3065 bool template_keyword_p,
3066 bool check_dependency_p,
3071 /* Peek at the next token. */
3072 token = cp_lexer_peek_token (parser->lexer);
3074 switch (token->type)
3080 /* We don't know yet whether or not this will be a
3082 cp_parser_parse_tentatively (parser);
3083 /* Try a template-id. */
3084 id = cp_parser_template_id (parser, template_keyword_p,
3087 /* If it worked, we're done. */
3088 if (cp_parser_parse_definitely (parser))
3090 /* Otherwise, it's an ordinary identifier. */
3091 return cp_parser_identifier (parser);
3094 case CPP_TEMPLATE_ID:
3095 return cp_parser_template_id (parser, template_keyword_p,
3102 tree qualifying_scope;
3106 /* Consume the `~' token. */
3107 cp_lexer_consume_token (parser->lexer);
3108 /* Parse the class-name. The standard, as written, seems to
3111 template <typename T> struct S { ~S (); };
3112 template <typename T> S<T>::~S() {}
3114 is invalid, since `~' must be followed by a class-name, but
3115 `S<T>' is dependent, and so not known to be a class.
3116 That's not right; we need to look in uninstantiated
3117 templates. A further complication arises from:
3119 template <typename T> void f(T t) {
3123 Here, it is not possible to look up `T' in the scope of `T'
3124 itself. We must look in both the current scope, and the
3125 scope of the containing complete expression.
3127 Yet another issue is:
3136 The standard does not seem to say that the `S' in `~S'
3137 should refer to the type `S' and not the data member
3140 /* DR 244 says that we look up the name after the "~" in the
3141 same scope as we looked up the qualifying name. That idea
3142 isn't fully worked out; it's more complicated than that. */
3143 scope = parser->scope;
3144 object_scope = parser->object_scope;
3145 qualifying_scope = parser->qualifying_scope;
3147 /* If the name is of the form "X::~X" it's OK. */
3148 if (scope && TYPE_P (scope)
3149 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3150 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3152 && (cp_lexer_peek_token (parser->lexer)->value
3153 == TYPE_IDENTIFIER (scope)))
3155 cp_lexer_consume_token (parser->lexer);
3156 return build_nt (BIT_NOT_EXPR, scope);
3159 /* If there was an explicit qualification (S::~T), first look
3160 in the scope given by the qualification (i.e., S). */
3163 cp_parser_parse_tentatively (parser);
3164 type_decl = cp_parser_class_name (parser,
3165 /*typename_keyword_p=*/false,
3166 /*template_keyword_p=*/false,
3168 /*check_dependency=*/false,
3169 /*class_head_p=*/false,
3171 if (cp_parser_parse_definitely (parser))
3172 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3174 /* In "N::S::~S", look in "N" as well. */
3175 if (scope && qualifying_scope)
3177 cp_parser_parse_tentatively (parser);
3178 parser->scope = qualifying_scope;
3179 parser->object_scope = NULL_TREE;
3180 parser->qualifying_scope = NULL_TREE;
3182 = cp_parser_class_name (parser,
3183 /*typename_keyword_p=*/false,
3184 /*template_keyword_p=*/false,
3186 /*check_dependency=*/false,
3187 /*class_head_p=*/false,
3189 if (cp_parser_parse_definitely (parser))
3190 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3192 /* In "p->S::~T", look in the scope given by "*p" as well. */
3193 else if (object_scope)
3195 cp_parser_parse_tentatively (parser);
3196 parser->scope = object_scope;
3197 parser->object_scope = NULL_TREE;
3198 parser->qualifying_scope = NULL_TREE;
3200 = cp_parser_class_name (parser,
3201 /*typename_keyword_p=*/false,
3202 /*template_keyword_p=*/false,
3204 /*check_dependency=*/false,
3205 /*class_head_p=*/false,
3207 if (cp_parser_parse_definitely (parser))
3208 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3210 /* Look in the surrounding context. */
3211 parser->scope = NULL_TREE;
3212 parser->object_scope = NULL_TREE;
3213 parser->qualifying_scope = NULL_TREE;
3215 = cp_parser_class_name (parser,
3216 /*typename_keyword_p=*/false,
3217 /*template_keyword_p=*/false,
3219 /*check_dependency=*/false,
3220 /*class_head_p=*/false,
3222 /* If an error occurred, assume that the name of the
3223 destructor is the same as the name of the qualifying
3224 class. That allows us to keep parsing after running
3225 into ill-formed destructor names. */
3226 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3227 return build_nt (BIT_NOT_EXPR, scope);
3228 else if (type_decl == error_mark_node)
3229 return error_mark_node;
3233 A typedef-name that names a class shall not be used as the
3234 identifier in the declarator for a destructor declaration. */
3236 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3237 && !DECL_SELF_REFERENCE_P (type_decl))
3238 error ("typedef-name %qD used as destructor declarator",
3241 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3245 if (token->keyword == RID_OPERATOR)
3249 /* This could be a template-id, so we try that first. */
3250 cp_parser_parse_tentatively (parser);
3251 /* Try a template-id. */
3252 id = cp_parser_template_id (parser, template_keyword_p,
3253 /*check_dependency_p=*/true,
3255 /* If that worked, we're done. */
3256 if (cp_parser_parse_definitely (parser))
3258 /* We still don't know whether we're looking at an
3259 operator-function-id or a conversion-function-id. */
3260 cp_parser_parse_tentatively (parser);
3261 /* Try an operator-function-id. */
3262 id = cp_parser_operator_function_id (parser);
3263 /* If that didn't work, try a conversion-function-id. */
3264 if (!cp_parser_parse_definitely (parser))
3265 id = cp_parser_conversion_function_id (parser);
3272 cp_parser_error (parser, "expected unqualified-id");
3273 return error_mark_node;
3277 /* Parse an (optional) nested-name-specifier.
3279 nested-name-specifier:
3280 class-or-namespace-name :: nested-name-specifier [opt]
3281 class-or-namespace-name :: template nested-name-specifier [opt]
3283 PARSER->SCOPE should be set appropriately before this function is
3284 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3285 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3288 Sets PARSER->SCOPE to the class (TYPE) or namespace
3289 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3290 it unchanged if there is no nested-name-specifier. Returns the new
3291 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3293 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3294 part of a declaration and/or decl-specifier. */
3297 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3298 bool typename_keyword_p,
3299 bool check_dependency_p,
3301 bool is_declaration)
3303 bool success = false;
3304 tree access_check = NULL_TREE;
3305 cp_token_position start = 0;
3308 /* If the next token corresponds to a nested name specifier, there
3309 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3310 false, it may have been true before, in which case something
3311 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3312 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3313 CHECK_DEPENDENCY_P is false, we have to fall through into the
3315 if (check_dependency_p
3316 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3318 cp_parser_pre_parsed_nested_name_specifier (parser);
3319 return parser->scope;
3322 /* Remember where the nested-name-specifier starts. */
3323 if (cp_parser_parsing_tentatively (parser)
3324 && !cp_parser_committed_to_tentative_parse (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,
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,
3820 postfix_expression = cp_parser_functional_cast (parser, type);
3828 /* If the next thing is a simple-type-specifier, we may be
3829 looking at a functional cast. We could also be looking at
3830 an id-expression. So, we try the functional cast, and if
3831 that doesn't work we fall back to the primary-expression. */
3832 cp_parser_parse_tentatively (parser);
3833 /* Look for the simple-type-specifier. */
3834 type = cp_parser_simple_type_specifier (parser,
3835 /*decl_specs=*/NULL,
3836 CP_PARSER_FLAGS_NONE);
3837 /* Parse the cast itself. */
3838 if (!cp_parser_error_occurred (parser))
3840 = cp_parser_functional_cast (parser, type);
3841 /* If that worked, we're done. */
3842 if (cp_parser_parse_definitely (parser))
3845 /* If the functional-cast didn't work out, try a
3846 compound-literal. */
3847 if (cp_parser_allow_gnu_extensions_p (parser)
3848 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3850 tree initializer_list = NULL_TREE;
3851 bool saved_in_type_id_in_expr_p;
3853 cp_parser_parse_tentatively (parser);
3854 /* Consume the `('. */
3855 cp_lexer_consume_token (parser->lexer);
3856 /* Parse the type. */
3857 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3858 parser->in_type_id_in_expr_p = true;
3859 type = cp_parser_type_id (parser);
3860 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3861 /* Look for the `)'. */
3862 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3863 /* Look for the `{'. */
3864 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3865 /* If things aren't going well, there's no need to
3867 if (!cp_parser_error_occurred (parser))
3869 bool non_constant_p;
3870 /* Parse the initializer-list. */
3872 = cp_parser_initializer_list (parser, &non_constant_p);
3873 /* Allow a trailing `,'. */
3874 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3875 cp_lexer_consume_token (parser->lexer);
3876 /* Look for the final `}'. */
3877 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3879 /* If that worked, we're definitely looking at a
3880 compound-literal expression. */
3881 if (cp_parser_parse_definitely (parser))
3883 /* Warn the user that a compound literal is not
3884 allowed in standard C++. */
3886 pedwarn ("ISO C++ forbids compound-literals");
3887 /* Form the representation of the compound-literal. */
3889 = finish_compound_literal (type, initializer_list);
3894 /* It must be a primary-expression. */
3895 postfix_expression = cp_parser_primary_expression (parser,
3902 /* If we were avoiding committing to the processing of a
3903 qualified-id until we knew whether or not we had a
3904 pointer-to-member, we now know. */
3905 if (qualifying_class)
3909 /* Peek at the next token. */
3910 token = cp_lexer_peek_token (parser->lexer);
3911 done = (token->type != CPP_OPEN_SQUARE
3912 && token->type != CPP_OPEN_PAREN
3913 && token->type != CPP_DOT
3914 && token->type != CPP_DEREF
3915 && token->type != CPP_PLUS_PLUS
3916 && token->type != CPP_MINUS_MINUS);
3918 postfix_expression = finish_qualified_id_expr (qualifying_class,
3923 return postfix_expression;
3926 /* Keep looping until the postfix-expression is complete. */
3929 if (idk == CP_ID_KIND_UNQUALIFIED
3930 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3931 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3932 /* It is not a Koenig lookup function call. */
3934 = unqualified_name_lookup_error (postfix_expression);
3936 /* Peek at the next token. */
3937 token = cp_lexer_peek_token (parser->lexer);
3939 switch (token->type)
3941 case CPP_OPEN_SQUARE:
3943 = cp_parser_postfix_open_square_expression (parser,
3946 idk = CP_ID_KIND_NONE;
3949 case CPP_OPEN_PAREN:
3950 /* postfix-expression ( expression-list [opt] ) */
3953 tree args = (cp_parser_parenthesized_expression_list
3954 (parser, false, /*non_constant_p=*/NULL));
3956 if (args == error_mark_node)
3958 postfix_expression = error_mark_node;
3962 /* Function calls are not permitted in
3963 constant-expressions. */
3964 if (cp_parser_non_integral_constant_expression (parser,
3967 postfix_expression = error_mark_node;
3972 if (idk == CP_ID_KIND_UNQUALIFIED)
3974 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3980 = perform_koenig_lookup (postfix_expression, args);
3984 = unqualified_fn_lookup_error (postfix_expression);
3986 /* We do not perform argument-dependent lookup if
3987 normal lookup finds a non-function, in accordance
3988 with the expected resolution of DR 218. */
3989 else if (args && is_overloaded_fn (postfix_expression))
3991 tree fn = get_first_fn (postfix_expression);
3993 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3994 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3996 /* Only do argument dependent lookup if regular
3997 lookup does not find a set of member functions.
3998 [basic.lookup.koenig]/2a */
3999 if (!DECL_FUNCTION_MEMBER_P (fn))
4003 = perform_koenig_lookup (postfix_expression, args);
4008 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4010 tree instance = TREE_OPERAND (postfix_expression, 0);
4011 tree fn = TREE_OPERAND (postfix_expression, 1);
4013 if (processing_template_decl
4014 && (type_dependent_expression_p (instance)
4015 || (!BASELINK_P (fn)
4016 && TREE_CODE (fn) != FIELD_DECL)
4017 || type_dependent_expression_p (fn)
4018 || any_type_dependent_arguments_p (args)))
4021 = build_min_nt (CALL_EXPR, postfix_expression,
4026 if (BASELINK_P (fn))
4028 = (build_new_method_call
4029 (instance, fn, args, NULL_TREE,
4030 (idk == CP_ID_KIND_QUALIFIED
4031 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4034 = finish_call_expr (postfix_expression, args,
4035 /*disallow_virtual=*/false,
4036 /*koenig_p=*/false);
4038 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4039 || TREE_CODE (postfix_expression) == MEMBER_REF
4040 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4041 postfix_expression = (build_offset_ref_call_from_tree
4042 (postfix_expression, args));
4043 else if (idk == CP_ID_KIND_QUALIFIED)
4044 /* A call to a static class member, or a namespace-scope
4047 = finish_call_expr (postfix_expression, args,
4048 /*disallow_virtual=*/true,
4051 /* All other function calls. */
4053 = finish_call_expr (postfix_expression, args,
4054 /*disallow_virtual=*/false,
4057 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4058 idk = CP_ID_KIND_NONE;
4064 /* postfix-expression . template [opt] id-expression
4065 postfix-expression . pseudo-destructor-name
4066 postfix-expression -> template [opt] id-expression
4067 postfix-expression -> pseudo-destructor-name */
4069 /* Consume the `.' or `->' operator. */
4070 cp_lexer_consume_token (parser->lexer);
4073 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4079 /* postfix-expression ++ */
4080 /* Consume the `++' token. */
4081 cp_lexer_consume_token (parser->lexer);
4082 /* Generate a representation for the complete expression. */
4084 = finish_increment_expr (postfix_expression,
4085 POSTINCREMENT_EXPR);
4086 /* Increments may not appear in constant-expressions. */
4087 if (cp_parser_non_integral_constant_expression (parser,
4089 postfix_expression = error_mark_node;
4090 idk = CP_ID_KIND_NONE;
4093 case CPP_MINUS_MINUS:
4094 /* postfix-expression -- */
4095 /* Consume the `--' token. */
4096 cp_lexer_consume_token (parser->lexer);
4097 /* Generate a representation for the complete expression. */
4099 = finish_increment_expr (postfix_expression,
4100 POSTDECREMENT_EXPR);
4101 /* Decrements may not appear in constant-expressions. */
4102 if (cp_parser_non_integral_constant_expression (parser,
4104 postfix_expression = error_mark_node;
4105 idk = CP_ID_KIND_NONE;
4109 return postfix_expression;
4113 /* We should never get here. */
4115 return error_mark_node;
4118 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4119 by cp_parser_builtin_offsetof. We're looking for
4121 postfix-expression [ expression ]
4123 FOR_OFFSETOF is set if we're being called in that context, which
4124 changes how we deal with integer constant expressions. */
4127 cp_parser_postfix_open_square_expression (cp_parser *parser,
4128 tree postfix_expression,
4133 /* Consume the `[' token. */
4134 cp_lexer_consume_token (parser->lexer);
4136 /* Parse the index expression. */
4137 /* ??? For offsetof, there is a question of what to allow here. If
4138 offsetof is not being used in an integral constant expression context,
4139 then we *could* get the right answer by computing the value at runtime.
4140 If we are in an integral constant expression context, then we might
4141 could accept any constant expression; hard to say without analysis.
4142 Rather than open the barn door too wide right away, allow only integer
4143 constant expressions here. */
4145 index = cp_parser_constant_expression (parser, false, NULL);
4147 index = cp_parser_expression (parser);
4149 /* Look for the closing `]'. */
4150 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4152 /* Build the ARRAY_REF. */
4153 postfix_expression = grok_array_decl (postfix_expression, index);
4155 /* When not doing offsetof, array references are not permitted in
4156 constant-expressions. */
4158 && (cp_parser_non_integral_constant_expression
4159 (parser, "an array reference")))
4160 postfix_expression = error_mark_node;
4162 return postfix_expression;
4165 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4166 by cp_parser_builtin_offsetof. We're looking for
4168 postfix-expression . template [opt] id-expression
4169 postfix-expression . pseudo-destructor-name
4170 postfix-expression -> template [opt] id-expression
4171 postfix-expression -> pseudo-destructor-name
4173 FOR_OFFSETOF is set if we're being called in that context. That sorta
4174 limits what of the above we'll actually accept, but nevermind.
4175 TOKEN_TYPE is the "." or "->" token, which will already have been
4176 removed from the stream. */
4179 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4180 enum cpp_ttype token_type,
4181 tree postfix_expression,
4182 bool for_offsetof, cp_id_kind *idk)
4187 bool pseudo_destructor_p;
4188 tree scope = NULL_TREE;
4190 /* If this is a `->' operator, dereference the pointer. */
4191 if (token_type == CPP_DEREF)
4192 postfix_expression = build_x_arrow (postfix_expression);
4193 /* Check to see whether or not the expression is type-dependent. */
4194 dependent_p = type_dependent_expression_p (postfix_expression);
4195 /* The identifier following the `->' or `.' is not qualified. */
4196 parser->scope = NULL_TREE;
4197 parser->qualifying_scope = NULL_TREE;
4198 parser->object_scope = NULL_TREE;
4199 *idk = CP_ID_KIND_NONE;
4200 /* Enter the scope corresponding to the type of the object
4201 given by the POSTFIX_EXPRESSION. */
4202 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4204 scope = TREE_TYPE (postfix_expression);
4205 /* According to the standard, no expression should ever have
4206 reference type. Unfortunately, we do not currently match
4207 the standard in this respect in that our internal representation
4208 of an expression may have reference type even when the standard
4209 says it does not. Therefore, we have to manually obtain the
4210 underlying type here. */
4211 scope = non_reference (scope);
4212 /* The type of the POSTFIX_EXPRESSION must be complete. */
4213 scope = complete_type_or_else (scope, NULL_TREE);
4214 /* Let the name lookup machinery know that we are processing a
4215 class member access expression. */
4216 parser->context->object_type = scope;
4217 /* If something went wrong, we want to be able to discern that case,
4218 as opposed to the case where there was no SCOPE due to the type
4219 of expression being dependent. */
4221 scope = error_mark_node;
4222 /* If the SCOPE was erroneous, make the various semantic analysis
4223 functions exit quickly -- and without issuing additional error
4225 if (scope == error_mark_node)
4226 postfix_expression = error_mark_node;
4229 /* Assume this expression is not a pseudo-destructor access. */
4230 pseudo_destructor_p = false;
4232 /* If the SCOPE is a scalar type, then, if this is a valid program,
4233 we must be looking at a pseudo-destructor-name. */
4234 if (scope && SCALAR_TYPE_P (scope))
4239 cp_parser_parse_tentatively (parser);
4240 /* Parse the pseudo-destructor-name. */
4242 cp_parser_pseudo_destructor_name (parser, &s, &type);
4243 if (cp_parser_parse_definitely (parser))
4245 pseudo_destructor_p = true;
4247 = finish_pseudo_destructor_expr (postfix_expression,
4248 s, TREE_TYPE (type));
4252 if (!pseudo_destructor_p)
4254 /* If the SCOPE is not a scalar type, we are looking at an
4255 ordinary class member access expression, rather than a
4256 pseudo-destructor-name. */
4257 template_p = cp_parser_optional_template_keyword (parser);
4258 /* Parse the id-expression. */
4259 name = cp_parser_id_expression (parser, template_p,
4260 /*check_dependency_p=*/true,
4261 /*template_p=*/NULL,
4262 /*declarator_p=*/false);
4263 /* In general, build a SCOPE_REF if the member name is qualified.
4264 However, if the name was not dependent and has already been
4265 resolved; there is no need to build the SCOPE_REF. For example;
4267 struct X { void f(); };
4268 template <typename T> void f(T* t) { t->X::f(); }
4270 Even though "t" is dependent, "X::f" is not and has been resolved
4271 to a BASELINK; there is no need to include scope information. */
4273 /* But we do need to remember that there was an explicit scope for
4274 virtual function calls. */
4276 *idk = CP_ID_KIND_QUALIFIED;
4278 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4280 name = build_nt (SCOPE_REF, parser->scope, name);
4281 parser->scope = NULL_TREE;
4282 parser->qualifying_scope = NULL_TREE;
4283 parser->object_scope = NULL_TREE;
4285 if (scope && name && BASELINK_P (name))
4286 adjust_result_of_qualified_name_lookup
4287 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4289 = finish_class_member_access_expr (postfix_expression, name);
4292 /* We no longer need to look up names in the scope of the object on
4293 the left-hand side of the `.' or `->' operator. */
4294 parser->context->object_type = NULL_TREE;
4296 /* Outside of offsetof, these operators may not appear in
4297 constant-expressions. */
4299 && (cp_parser_non_integral_constant_expression
4300 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4301 postfix_expression = error_mark_node;
4303 return postfix_expression;
4306 /* Parse a parenthesized expression-list.
4309 assignment-expression
4310 expression-list, assignment-expression
4315 identifier, expression-list
4317 Returns a TREE_LIST. The TREE_VALUE of each node is a
4318 representation of an assignment-expression. Note that a TREE_LIST
4319 is returned even if there is only a single expression in the list.
4320 error_mark_node is returned if the ( and or ) are
4321 missing. NULL_TREE is returned on no expressions. The parentheses
4322 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4323 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4324 indicates whether or not all of the expressions in the list were
4328 cp_parser_parenthesized_expression_list (cp_parser* parser,
4329 bool is_attribute_list,
4330 bool *non_constant_p)
4332 tree expression_list = NULL_TREE;
4333 bool fold_expr_p = is_attribute_list;
4334 tree identifier = NULL_TREE;
4336 /* Assume all the expressions will be constant. */
4338 *non_constant_p = false;
4340 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4341 return error_mark_node;
4343 /* Consume expressions until there are no more. */
4344 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4349 /* At the beginning of attribute lists, check to see if the
4350 next token is an identifier. */
4351 if (is_attribute_list
4352 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4356 /* Consume the identifier. */
4357 token = cp_lexer_consume_token (parser->lexer);
4358 /* Save the identifier. */
4359 identifier = token->value;
4363 /* Parse the next assignment-expression. */
4366 bool expr_non_constant_p;
4367 expr = (cp_parser_constant_expression
4368 (parser, /*allow_non_constant_p=*/true,
4369 &expr_non_constant_p));
4370 if (expr_non_constant_p)
4371 *non_constant_p = true;
4374 expr = cp_parser_assignment_expression (parser);
4377 expr = fold_non_dependent_expr (expr);
4379 /* Add it to the list. We add error_mark_node
4380 expressions to the list, so that we can still tell if
4381 the correct form for a parenthesized expression-list
4382 is found. That gives better errors. */
4383 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4385 if (expr == error_mark_node)
4389 /* After the first item, attribute lists look the same as
4390 expression lists. */
4391 is_attribute_list = false;
4394 /* If the next token isn't a `,', then we are done. */
4395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4398 /* Otherwise, consume the `,' and keep going. */
4399 cp_lexer_consume_token (parser->lexer);
4402 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4407 /* We try and resync to an unnested comma, as that will give the
4408 user better diagnostics. */
4409 ending = cp_parser_skip_to_closing_parenthesis (parser,
4410 /*recovering=*/true,
4412 /*consume_paren=*/true);
4416 return error_mark_node;
4419 /* We built up the list in reverse order so we must reverse it now. */
4420 expression_list = nreverse (expression_list);
4422 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4424 return expression_list;
4427 /* Parse a pseudo-destructor-name.
4429 pseudo-destructor-name:
4430 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4431 :: [opt] nested-name-specifier template template-id :: ~ type-name
4432 :: [opt] nested-name-specifier [opt] ~ type-name
4434 If either of the first two productions is used, sets *SCOPE to the
4435 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4436 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4437 or ERROR_MARK_NODE if the parse fails. */
4440 cp_parser_pseudo_destructor_name (cp_parser* parser,
4444 bool nested_name_specifier_p;
4446 /* Assume that things will not work out. */
4447 *type = error_mark_node;
4449 /* Look for the optional `::' operator. */
4450 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4451 /* Look for the optional nested-name-specifier. */
4452 nested_name_specifier_p
4453 = (cp_parser_nested_name_specifier_opt (parser,
4454 /*typename_keyword_p=*/false,
4455 /*check_dependency_p=*/true,
4457 /*is_declaration=*/true)
4459 /* Now, if we saw a nested-name-specifier, we might be doing the
4460 second production. */
4461 if (nested_name_specifier_p
4462 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4464 /* Consume the `template' keyword. */
4465 cp_lexer_consume_token (parser->lexer);
4466 /* Parse the template-id. */
4467 cp_parser_template_id (parser,
4468 /*template_keyword_p=*/true,
4469 /*check_dependency_p=*/false,
4470 /*is_declaration=*/true);
4471 /* Look for the `::' token. */
4472 cp_parser_require (parser, CPP_SCOPE, "`::'");
4474 /* If the next token is not a `~', then there might be some
4475 additional qualification. */
4476 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4478 /* Look for the type-name. */
4479 *scope = TREE_TYPE (cp_parser_type_name (parser));
4481 if (*scope == error_mark_node)
4484 /* If we don't have ::~, then something has gone wrong. Since
4485 the only caller of this function is looking for something
4486 after `.' or `->' after a scalar type, most likely the
4487 program is trying to get a member of a non-aggregate
4489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4490 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4492 cp_parser_error (parser, "request for member of non-aggregate type");
4496 /* Look for the `::' token. */
4497 cp_parser_require (parser, CPP_SCOPE, "`::'");
4502 /* Look for the `~'. */
4503 cp_parser_require (parser, CPP_COMPL, "`~'");
4504 /* Look for the type-name again. We are not responsible for
4505 checking that it matches the first type-name. */
4506 *type = cp_parser_type_name (parser);
4509 /* Parse a unary-expression.
4515 unary-operator cast-expression
4516 sizeof unary-expression
4524 __extension__ cast-expression
4525 __alignof__ unary-expression
4526 __alignof__ ( type-id )
4527 __real__ cast-expression
4528 __imag__ cast-expression
4531 ADDRESS_P is true iff the unary-expression is appearing as the
4532 operand of the `&' operator.
4534 Returns a representation of the expression. */
4537 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4540 enum tree_code unary_operator;
4542 /* Peek at the next token. */
4543 token = cp_lexer_peek_token (parser->lexer);
4544 /* Some keywords give away the kind of expression. */
4545 if (token->type == CPP_KEYWORD)
4547 enum rid keyword = token->keyword;
4557 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4558 /* Consume the token. */
4559 cp_lexer_consume_token (parser->lexer);
4560 /* Parse the operand. */
4561 operand = cp_parser_sizeof_operand (parser, keyword);
4563 if (TYPE_P (operand))
4564 return cxx_sizeof_or_alignof_type (operand, op, true);
4566 return cxx_sizeof_or_alignof_expr (operand, op);
4570 return cp_parser_new_expression (parser);
4573 return cp_parser_delete_expression (parser);
4577 /* The saved value of the PEDANTIC flag. */
4581 /* Save away the PEDANTIC flag. */
4582 cp_parser_extension_opt (parser, &saved_pedantic);
4583 /* Parse the cast-expression. */
4584 expr = cp_parser_simple_cast_expression (parser);
4585 /* Restore the PEDANTIC flag. */
4586 pedantic = saved_pedantic;
4596 /* Consume the `__real__' or `__imag__' token. */
4597 cp_lexer_consume_token (parser->lexer);
4598 /* Parse the cast-expression. */
4599 expression = cp_parser_simple_cast_expression (parser);
4600 /* Create the complete representation. */
4601 return build_x_unary_op ((keyword == RID_REALPART
4602 ? REALPART_EXPR : IMAGPART_EXPR),
4612 /* Look for the `:: new' and `:: delete', which also signal the
4613 beginning of a new-expression, or delete-expression,
4614 respectively. If the next token is `::', then it might be one of
4616 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4620 /* See if the token after the `::' is one of the keywords in
4621 which we're interested. */
4622 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4623 /* If it's `new', we have a new-expression. */
4624 if (keyword == RID_NEW)
4625 return cp_parser_new_expression (parser);
4626 /* Similarly, for `delete'. */
4627 else if (keyword == RID_DELETE)
4628 return cp_parser_delete_expression (parser);
4631 /* Look for a unary operator. */
4632 unary_operator = cp_parser_unary_operator (token);
4633 /* The `++' and `--' operators can be handled similarly, even though
4634 they are not technically unary-operators in the grammar. */
4635 if (unary_operator == ERROR_MARK)
4637 if (token->type == CPP_PLUS_PLUS)
4638 unary_operator = PREINCREMENT_EXPR;
4639 else if (token->type == CPP_MINUS_MINUS)
4640 unary_operator = PREDECREMENT_EXPR;
4641 /* Handle the GNU address-of-label extension. */
4642 else if (cp_parser_allow_gnu_extensions_p (parser)
4643 && token->type == CPP_AND_AND)
4647 /* Consume the '&&' token. */
4648 cp_lexer_consume_token (parser->lexer);
4649 /* Look for the identifier. */
4650 identifier = cp_parser_identifier (parser);
4651 /* Create an expression representing the address. */
4652 return finish_label_address_expr (identifier);
4655 if (unary_operator != ERROR_MARK)
4657 tree cast_expression;
4658 tree expression = error_mark_node;
4659 const char *non_constant_p = NULL;
4661 /* Consume the operator token. */
4662 token = cp_lexer_consume_token (parser->lexer);
4663 /* Parse the cast-expression. */
4665 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4666 /* Now, build an appropriate representation. */
4667 switch (unary_operator)
4670 non_constant_p = "`*'";
4671 expression = build_x_indirect_ref (cast_expression, "unary *");
4675 non_constant_p = "`&'";
4678 expression = build_x_unary_op (unary_operator, cast_expression);
4681 case PREINCREMENT_EXPR:
4682 case PREDECREMENT_EXPR:
4683 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4688 case TRUTH_NOT_EXPR:
4689 expression = finish_unary_op_expr (unary_operator, cast_expression);
4697 && cp_parser_non_integral_constant_expression (parser,
4699 expression = error_mark_node;
4704 return cp_parser_postfix_expression (parser, address_p);
4707 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4708 unary-operator, the corresponding tree code is returned. */
4710 static enum tree_code
4711 cp_parser_unary_operator (cp_token* token)
4713 switch (token->type)
4716 return INDIRECT_REF;
4722 return CONVERT_EXPR;
4728 return TRUTH_NOT_EXPR;
4731 return BIT_NOT_EXPR;
4738 /* Parse a new-expression.
4741 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4742 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4744 Returns a representation of the expression. */
4747 cp_parser_new_expression (cp_parser* parser)
4749 bool global_scope_p;
4755 /* Look for the optional `::' operator. */
4757 = (cp_parser_global_scope_opt (parser,
4758 /*current_scope_valid_p=*/false)
4760 /* Look for the `new' operator. */
4761 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4762 /* There's no easy way to tell a new-placement from the
4763 `( type-id )' construct. */
4764 cp_parser_parse_tentatively (parser);
4765 /* Look for a new-placement. */
4766 placement = cp_parser_new_placement (parser);
4767 /* If that didn't work out, there's no new-placement. */
4768 if (!cp_parser_parse_definitely (parser))
4769 placement = NULL_TREE;
4771 /* If the next token is a `(', then we have a parenthesized
4773 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4775 /* Consume the `('. */
4776 cp_lexer_consume_token (parser->lexer);
4777 /* Parse the type-id. */
4778 type = cp_parser_type_id (parser);
4779 /* Look for the closing `)'. */
4780 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4781 /* There should not be a direct-new-declarator in this production,
4782 but GCC used to allowed this, so we check and emit a sensible error
4783 message for this case. */
4784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4786 error ("array bound forbidden after parenthesized type-id");
4787 inform ("try removing the parentheses around the type-id");
4788 cp_parser_direct_new_declarator (parser);
4792 /* Otherwise, there must be a new-type-id. */
4794 type = cp_parser_new_type_id (parser, &nelts);
4796 /* If the next token is a `(', then we have a new-initializer. */
4797 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4798 initializer = cp_parser_new_initializer (parser);
4800 initializer = NULL_TREE;
4802 /* A new-expression may not appear in an integral constant
4804 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4805 return error_mark_node;
4807 /* Create a representation of the new-expression. */
4808 return build_new (placement, type, nelts, initializer, global_scope_p);
4811 /* Parse a new-placement.
4816 Returns the same representation as for an expression-list. */
4819 cp_parser_new_placement (cp_parser* parser)
4821 tree expression_list;
4823 /* Parse the expression-list. */
4824 expression_list = (cp_parser_parenthesized_expression_list
4825 (parser, false, /*non_constant_p=*/NULL));
4827 return expression_list;
4830 /* Parse a new-type-id.
4833 type-specifier-seq new-declarator [opt]
4835 Returns the TYPE allocated. If the new-type-id indicates an array
4836 type, *NELTS is set to the number of elements in the last array
4837 bound; the TYPE will not include the last array bound. */
4840 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4842 cp_decl_specifier_seq type_specifier_seq;
4843 cp_declarator *new_declarator;
4844 cp_declarator *declarator;
4845 cp_declarator *outer_declarator;
4846 const char *saved_message;
4849 /* The type-specifier sequence must not contain type definitions.
4850 (It cannot contain declarations of new types either, but if they
4851 are not definitions we will catch that because they are not
4853 saved_message = parser->type_definition_forbidden_message;
4854 parser->type_definition_forbidden_message
4855 = "types may not be defined in a new-type-id";
4856 /* Parse the type-specifier-seq. */
4857 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4858 /* Restore the old message. */
4859 parser->type_definition_forbidden_message = saved_message;
4860 /* Parse the new-declarator. */
4861 new_declarator = cp_parser_new_declarator_opt (parser);
4863 /* Determine the number of elements in the last array dimension, if
4866 /* Skip down to the last array dimension. */
4867 declarator = new_declarator;
4868 outer_declarator = NULL;
4869 while (declarator && (declarator->kind == cdk_pointer
4870 || declarator->kind == cdk_ptrmem))
4872 outer_declarator = declarator;
4873 declarator = declarator->declarator;
4876 && declarator->kind == cdk_array
4877 && declarator->declarator
4878 && declarator->declarator->kind == cdk_array)
4880 outer_declarator = declarator;
4881 declarator = declarator->declarator;
4884 if (declarator && declarator->kind == cdk_array)
4886 *nelts = declarator->u.array.bounds;
4887 if (*nelts == error_mark_node)
4888 *nelts = integer_one_node;
4889 else if (!processing_template_decl)
4891 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4893 pedwarn ("size in array new must have integral type");
4894 *nelts = save_expr (cp_convert (sizetype, *nelts));
4895 if (*nelts == integer_zero_node)
4896 warning ("zero size array reserves no space");
4898 if (outer_declarator)
4899 outer_declarator->declarator = declarator->declarator;
4901 new_declarator = NULL;
4904 type = groktypename (&type_specifier_seq, new_declarator);
4905 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4907 *nelts = array_type_nelts_top (type);
4908 type = TREE_TYPE (type);
4913 /* Parse an (optional) new-declarator.
4916 ptr-operator new-declarator [opt]
4917 direct-new-declarator
4919 Returns the declarator. */
4921 static cp_declarator *
4922 cp_parser_new_declarator_opt (cp_parser* parser)
4924 enum tree_code code;
4926 cp_cv_quals cv_quals;
4928 /* We don't know if there's a ptr-operator next, or not. */
4929 cp_parser_parse_tentatively (parser);
4930 /* Look for a ptr-operator. */
4931 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4932 /* If that worked, look for more new-declarators. */
4933 if (cp_parser_parse_definitely (parser))
4935 cp_declarator *declarator;
4937 /* Parse another optional declarator. */
4938 declarator = cp_parser_new_declarator_opt (parser);
4940 /* Create the representation of the declarator. */
4942 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4943 else if (code == INDIRECT_REF)
4944 declarator = make_pointer_declarator (cv_quals, declarator);
4946 declarator = make_reference_declarator (cv_quals, declarator);
4951 /* If the next token is a `[', there is a direct-new-declarator. */
4952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4953 return cp_parser_direct_new_declarator (parser);
4958 /* Parse a direct-new-declarator.
4960 direct-new-declarator:
4962 direct-new-declarator [constant-expression]
4966 static cp_declarator *
4967 cp_parser_direct_new_declarator (cp_parser* parser)
4969 cp_declarator *declarator = NULL;
4975 /* Look for the opening `['. */
4976 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4977 /* The first expression is not required to be constant. */
4980 expression = cp_parser_expression (parser);
4981 /* The standard requires that the expression have integral
4982 type. DR 74 adds enumeration types. We believe that the
4983 real intent is that these expressions be handled like the
4984 expression in a `switch' condition, which also allows
4985 classes with a single conversion to integral or
4986 enumeration type. */
4987 if (!processing_template_decl)
4990 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4995 error ("expression in new-declarator must have integral "
4996 "or enumeration type");
4997 expression = error_mark_node;
5001 /* But all the other expressions must be. */
5004 = cp_parser_constant_expression (parser,
5005 /*allow_non_constant=*/false,
5007 /* Look for the closing `]'. */
5008 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5010 /* Add this bound to the declarator. */
5011 declarator = make_array_declarator (declarator, expression);
5013 /* If the next token is not a `[', then there are no more
5015 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5022 /* Parse a new-initializer.
5025 ( expression-list [opt] )
5027 Returns a representation of the expression-list. If there is no
5028 expression-list, VOID_ZERO_NODE is returned. */
5031 cp_parser_new_initializer (cp_parser* parser)
5033 tree expression_list;
5035 expression_list = (cp_parser_parenthesized_expression_list
5036 (parser, false, /*non_constant_p=*/NULL));
5037 if (!expression_list)
5038 expression_list = void_zero_node;
5040 return expression_list;
5043 /* Parse a delete-expression.
5046 :: [opt] delete cast-expression
5047 :: [opt] delete [ ] cast-expression
5049 Returns a representation of the expression. */
5052 cp_parser_delete_expression (cp_parser* parser)
5054 bool global_scope_p;
5058 /* Look for the optional `::' operator. */
5060 = (cp_parser_global_scope_opt (parser,
5061 /*current_scope_valid_p=*/false)
5063 /* Look for the `delete' keyword. */
5064 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5065 /* See if the array syntax is in use. */
5066 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5068 /* Consume the `[' token. */
5069 cp_lexer_consume_token (parser->lexer);
5070 /* Look for the `]' token. */
5071 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5072 /* Remember that this is the `[]' construct. */
5078 /* Parse the cast-expression. */
5079 expression = cp_parser_simple_cast_expression (parser);
5081 /* A delete-expression may not appear in an integral constant
5083 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5084 return error_mark_node;
5086 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5089 /* Parse a cast-expression.
5093 ( type-id ) cast-expression
5095 Returns a representation of the expression. */
5098 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5100 /* If it's a `(', then we might be looking at a cast. */
5101 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5103 tree type = NULL_TREE;
5104 tree expr = NULL_TREE;
5105 bool compound_literal_p;
5106 const char *saved_message;
5108 /* There's no way to know yet whether or not this is a cast.
5109 For example, `(int (3))' is a unary-expression, while `(int)
5110 3' is a cast. So, we resort to parsing tentatively. */
5111 cp_parser_parse_tentatively (parser);
5112 /* Types may not be defined in a cast. */
5113 saved_message = parser->type_definition_forbidden_message;
5114 parser->type_definition_forbidden_message
5115 = "types may not be defined in casts";
5116 /* Consume the `('. */
5117 cp_lexer_consume_token (parser->lexer);
5118 /* A very tricky bit is that `(struct S) { 3 }' is a
5119 compound-literal (which we permit in C++ as an extension).
5120 But, that construct is not a cast-expression -- it is a
5121 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5122 is legal; if the compound-literal were a cast-expression,
5123 you'd need an extra set of parentheses.) But, if we parse
5124 the type-id, and it happens to be a class-specifier, then we
5125 will commit to the parse at that point, because we cannot
5126 undo the action that is done when creating a new class. So,
5127 then we cannot back up and do a postfix-expression.
5129 Therefore, we scan ahead to the closing `)', and check to see
5130 if the token after the `)' is a `{'. If so, we are not
5131 looking at a cast-expression.
5133 Save tokens so that we can put them back. */
5134 cp_lexer_save_tokens (parser->lexer);
5135 /* Skip tokens until the next token is a closing parenthesis.
5136 If we find the closing `)', and the next token is a `{', then
5137 we are looking at a compound-literal. */
5139 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5140 /*consume_paren=*/true)
5141 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5142 /* Roll back the tokens we skipped. */
5143 cp_lexer_rollback_tokens (parser->lexer);
5144 /* If we were looking at a compound-literal, simulate an error
5145 so that the call to cp_parser_parse_definitely below will
5147 if (compound_literal_p)
5148 cp_parser_simulate_error (parser);
5151 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5152 parser->in_type_id_in_expr_p = true;
5153 /* Look for the type-id. */
5154 type = cp_parser_type_id (parser);
5155 /* Look for the closing `)'. */
5156 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5157 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5160 /* Restore the saved message. */
5161 parser->type_definition_forbidden_message = saved_message;
5163 /* If ok so far, parse the dependent expression. We cannot be
5164 sure it is a cast. Consider `(T ())'. It is a parenthesized
5165 ctor of T, but looks like a cast to function returning T
5166 without a dependent expression. */
5167 if (!cp_parser_error_occurred (parser))
5168 expr = cp_parser_simple_cast_expression (parser);
5170 if (cp_parser_parse_definitely (parser))
5172 /* Warn about old-style casts, if so requested. */
5173 if (warn_old_style_cast
5174 && !in_system_header
5175 && !VOID_TYPE_P (type)
5176 && current_lang_name != lang_name_c)
5177 warning ("use of old-style cast");
5179 /* Only type conversions to integral or enumeration types
5180 can be used in constant-expressions. */
5181 if (parser->integral_constant_expression_p
5182 && !dependent_type_p (type)
5183 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5184 && (cp_parser_non_integral_constant_expression
5186 "a cast to a type other than an integral or "
5187 "enumeration type")))
5188 return error_mark_node;
5190 /* Perform the cast. */
5191 expr = build_c_cast (type, expr);
5196 /* If we get here, then it's not a cast, so it must be a
5197 unary-expression. */
5198 return cp_parser_unary_expression (parser, address_p);
5201 /* Parse a binary expression of the general form:
5205 pm-expression .* cast-expression
5206 pm-expression ->* cast-expression
5208 multiplicative-expression:
5210 multiplicative-expression * pm-expression
5211 multiplicative-expression / pm-expression
5212 multiplicative-expression % pm-expression
5214 additive-expression:
5215 multiplicative-expression
5216 additive-expression + multiplicative-expression
5217 additive-expression - multiplicative-expression
5221 shift-expression << additive-expression
5222 shift-expression >> additive-expression
5224 relational-expression:
5226 relational-expression < shift-expression
5227 relational-expression > shift-expression
5228 relational-expression <= shift-expression
5229 relational-expression >= shift-expression
5233 relational-expression:
5234 relational-expression <? shift-expression
5235 relational-expression >? shift-expression
5237 equality-expression:
5238 relational-expression
5239 equality-expression == relational-expression
5240 equality-expression != relational-expression
5244 and-expression & equality-expression
5246 exclusive-or-expression:
5248 exclusive-or-expression ^ and-expression
5250 inclusive-or-expression:
5251 exclusive-or-expression
5252 inclusive-or-expression | exclusive-or-expression
5254 logical-and-expression:
5255 inclusive-or-expression
5256 logical-and-expression && inclusive-or-expression
5258 logical-or-expression:
5259 logical-and-expression
5260 logical-or-expression || logical-and-expression
5262 All these are implemented with a single function like:
5265 simple-cast-expression
5266 binary-expression <token> binary-expression
5268 The binops_by_token map is used to get the tree codes for each <token> type.
5269 binary-expressions are associated according to a precedence table. */
5271 #define TOKEN_PRECEDENCE(token) \
5272 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5273 ? PREC_NOT_OPERATOR \
5274 : binops_by_token[token->type].prec)
5277 cp_parser_binary_expression (cp_parser* parser)
5279 cp_parser_expression_stack stack;
5280 cp_parser_expression_stack_entry *sp = &stack[0];
5283 enum tree_code tree_type;
5284 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5287 /* Parse the first expression. */
5288 lhs = cp_parser_simple_cast_expression (parser);
5292 /* Get an operator token. */
5293 token = cp_lexer_peek_token (parser->lexer);
5294 new_prec = TOKEN_PRECEDENCE (token);
5296 /* Popping an entry off the stack means we completed a subexpression:
5297 - either we found a token which is not an operator (`>' where it is not
5298 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5299 will happen repeatedly;
5300 - or, we found an operator which has lower priority. This is the case
5301 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5303 if (new_prec <= prec)
5312 tree_type = binops_by_token[token->type].tree_type;
5314 /* We used the operator token. */
5315 cp_lexer_consume_token (parser->lexer);
5317 /* Extract another operand. It may be the RHS of this expression
5318 or the LHS of a new, higher priority expression. */
5319 rhs = cp_parser_simple_cast_expression (parser);
5321 /* Get another operator token. Look up its precedence to avoid
5322 building a useless (immediately popped) stack entry for common
5323 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5324 token = cp_lexer_peek_token (parser->lexer);
5325 lookahead_prec = TOKEN_PRECEDENCE (token);
5326 if (lookahead_prec > new_prec)
5328 /* ... and prepare to parse the RHS of the new, higher priority
5329 expression. Since precedence levels on the stack are
5330 monotonically increasing, we do not have to care about
5333 sp->tree_type = tree_type;
5338 new_prec = lookahead_prec;
5342 /* If the stack is not empty, we have parsed into LHS the right side
5343 (`4' in the example above) of an expression we had suspended.
5344 We can use the information on the stack to recover the LHS (`3')
5345 from the stack together with the tree code (`MULT_EXPR'), and
5346 the precedence of the higher level subexpression
5347 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5348 which will be used to actually build the additive expression. */
5351 tree_type = sp->tree_type;
5356 overloaded_p = false;
5357 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5359 /* If the binary operator required the use of an overloaded operator,
5360 then this expression cannot be an integral constant-expression.
5361 An overloaded operator can be used even if both operands are
5362 otherwise permissible in an integral constant-expression if at
5363 least one of the operands is of enumeration type. */
5366 && (cp_parser_non_integral_constant_expression
5367 (parser, "calls to overloaded operators")))
5368 return error_mark_node;
5375 /* Parse the `? expression : assignment-expression' part of a
5376 conditional-expression. The LOGICAL_OR_EXPR is the
5377 logical-or-expression that started the conditional-expression.
5378 Returns a representation of the entire conditional-expression.
5380 This routine is used by cp_parser_assignment_expression.
5382 ? expression : assignment-expression
5386 ? : assignment-expression */
5389 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5392 tree assignment_expr;
5394 /* Consume the `?' token. */
5395 cp_lexer_consume_token (parser->lexer);
5396 if (cp_parser_allow_gnu_extensions_p (parser)
5397 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5398 /* Implicit true clause. */
5401 /* Parse the expression. */
5402 expr = cp_parser_expression (parser);
5404 /* The next token should be a `:'. */
5405 cp_parser_require (parser, CPP_COLON, "`:'");
5406 /* Parse the assignment-expression. */
5407 assignment_expr = cp_parser_assignment_expression (parser);
5409 /* Build the conditional-expression. */
5410 return build_x_conditional_expr (logical_or_expr,
5415 /* Parse an assignment-expression.
5417 assignment-expression:
5418 conditional-expression
5419 logical-or-expression assignment-operator assignment_expression
5422 Returns a representation for the expression. */
5425 cp_parser_assignment_expression (cp_parser* parser)
5429 /* If the next token is the `throw' keyword, then we're looking at
5430 a throw-expression. */
5431 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5432 expr = cp_parser_throw_expression (parser);
5433 /* Otherwise, it must be that we are looking at a
5434 logical-or-expression. */
5437 /* Parse the binary expressions (logical-or-expression). */
5438 expr = cp_parser_binary_expression (parser);
5439 /* If the next token is a `?' then we're actually looking at a
5440 conditional-expression. */
5441 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5442 return cp_parser_question_colon_clause (parser, expr);
5445 enum tree_code assignment_operator;
5447 /* If it's an assignment-operator, we're using the second
5450 = cp_parser_assignment_operator_opt (parser);
5451 if (assignment_operator != ERROR_MARK)
5455 /* Parse the right-hand side of the assignment. */
5456 rhs = cp_parser_assignment_expression (parser);
5457 /* An assignment may not appear in a
5458 constant-expression. */
5459 if (cp_parser_non_integral_constant_expression (parser,
5461 return error_mark_node;
5462 /* Build the assignment expression. */
5463 expr = build_x_modify_expr (expr,
5464 assignment_operator,
5473 /* Parse an (optional) assignment-operator.
5475 assignment-operator: one of
5476 = *= /= %= += -= >>= <<= &= ^= |=
5480 assignment-operator: one of
5483 If the next token is an assignment operator, the corresponding tree
5484 code is returned, and the token is consumed. For example, for
5485 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5486 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5487 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5488 operator, ERROR_MARK is returned. */
5490 static enum tree_code
5491 cp_parser_assignment_operator_opt (cp_parser* parser)
5496 /* Peek at the next toen. */
5497 token = cp_lexer_peek_token (parser->lexer);
5499 switch (token->type)
5510 op = TRUNC_DIV_EXPR;
5514 op = TRUNC_MOD_EXPR;
5554 /* Nothing else is an assignment operator. */
5558 /* If it was an assignment operator, consume it. */
5559 if (op != ERROR_MARK)
5560 cp_lexer_consume_token (parser->lexer);
5565 /* Parse an expression.
5568 assignment-expression
5569 expression , assignment-expression
5571 Returns a representation of the expression. */
5574 cp_parser_expression (cp_parser* parser)
5576 tree expression = NULL_TREE;
5580 tree assignment_expression;
5582 /* Parse the next assignment-expression. */
5583 assignment_expression
5584 = cp_parser_assignment_expression (parser);
5585 /* If this is the first assignment-expression, we can just
5588 expression = assignment_expression;
5590 expression = build_x_compound_expr (expression,
5591 assignment_expression);
5592 /* If the next token is not a comma, then we are done with the
5594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5596 /* Consume the `,'. */
5597 cp_lexer_consume_token (parser->lexer);
5598 /* A comma operator cannot appear in a constant-expression. */
5599 if (cp_parser_non_integral_constant_expression (parser,
5600 "a comma operator"))
5601 expression = error_mark_node;
5607 /* Parse a constant-expression.
5609 constant-expression:
5610 conditional-expression
5612 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5613 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5614 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5615 is false, NON_CONSTANT_P should be NULL. */
5618 cp_parser_constant_expression (cp_parser* parser,
5619 bool allow_non_constant_p,
5620 bool *non_constant_p)
5622 bool saved_integral_constant_expression_p;
5623 bool saved_allow_non_integral_constant_expression_p;
5624 bool saved_non_integral_constant_expression_p;
5627 /* It might seem that we could simply parse the
5628 conditional-expression, and then check to see if it were
5629 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5630 one that the compiler can figure out is constant, possibly after
5631 doing some simplifications or optimizations. The standard has a
5632 precise definition of constant-expression, and we must honor
5633 that, even though it is somewhat more restrictive.
5639 is not a legal declaration, because `(2, 3)' is not a
5640 constant-expression. The `,' operator is forbidden in a
5641 constant-expression. However, GCC's constant-folding machinery
5642 will fold this operation to an INTEGER_CST for `3'. */
5644 /* Save the old settings. */
5645 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5646 saved_allow_non_integral_constant_expression_p
5647 = parser->allow_non_integral_constant_expression_p;
5648 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5649 /* We are now parsing a constant-expression. */
5650 parser->integral_constant_expression_p = true;
5651 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5652 parser->non_integral_constant_expression_p = false;
5653 /* Although the grammar says "conditional-expression", we parse an
5654 "assignment-expression", which also permits "throw-expression"
5655 and the use of assignment operators. In the case that
5656 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5657 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5658 actually essential that we look for an assignment-expression.
5659 For example, cp_parser_initializer_clauses uses this function to
5660 determine whether a particular assignment-expression is in fact
5662 expression = cp_parser_assignment_expression (parser);
5663 /* Restore the old settings. */
5664 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5665 parser->allow_non_integral_constant_expression_p
5666 = saved_allow_non_integral_constant_expression_p;
5667 if (allow_non_constant_p)
5668 *non_constant_p = parser->non_integral_constant_expression_p;
5669 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5674 /* Parse __builtin_offsetof.
5676 offsetof-expression:
5677 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5679 offsetof-member-designator:
5681 | offsetof-member-designator "." id-expression
5682 | offsetof-member-designator "[" expression "]"
5686 cp_parser_builtin_offsetof (cp_parser *parser)
5688 int save_ice_p, save_non_ice_p;
5692 /* We're about to accept non-integral-constant things, but will
5693 definitely yield an integral constant expression. Save and
5694 restore these values around our local parsing. */
5695 save_ice_p = parser->integral_constant_expression_p;
5696 save_non_ice_p = parser->non_integral_constant_expression_p;
5698 /* Consume the "__builtin_offsetof" token. */
5699 cp_lexer_consume_token (parser->lexer);
5700 /* Consume the opening `('. */
5701 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5702 /* Parse the type-id. */
5703 type = cp_parser_type_id (parser);
5704 /* Look for the `,'. */
5705 cp_parser_require (parser, CPP_COMMA, "`,'");
5707 /* Build the (type *)null that begins the traditional offsetof macro. */
5708 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5710 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5711 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5715 cp_token *token = cp_lexer_peek_token (parser->lexer);
5716 switch (token->type)
5718 case CPP_OPEN_SQUARE:
5719 /* offsetof-member-designator "[" expression "]" */
5720 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5724 /* offsetof-member-designator "." identifier */
5725 cp_lexer_consume_token (parser->lexer);
5726 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5730 case CPP_CLOSE_PAREN:
5731 /* Consume the ")" token. */
5732 cp_lexer_consume_token (parser->lexer);
5736 /* Error. We know the following require will fail, but
5737 that gives the proper error message. */
5738 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5739 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5740 expr = error_mark_node;
5746 /* If we're processing a template, we can't finish the semantics yet.
5747 Otherwise we can fold the entire expression now. */
5748 if (processing_template_decl)
5749 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5751 expr = fold_offsetof (expr);
5754 parser->integral_constant_expression_p = save_ice_p;
5755 parser->non_integral_constant_expression_p = save_non_ice_p;
5760 /* Statements [gram.stmt.stmt] */
5762 /* Parse a statement.
5766 expression-statement
5771 declaration-statement
5775 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5779 location_t statement_location;
5781 /* There is no statement yet. */
5782 statement = NULL_TREE;
5783 /* Peek at the next token. */
5784 token = cp_lexer_peek_token (parser->lexer);
5785 /* Remember the location of the first token in the statement. */
5786 statement_location = token->location;
5787 /* If this is a keyword, then that will often determine what kind of
5788 statement we have. */
5789 if (token->type == CPP_KEYWORD)
5791 enum rid keyword = token->keyword;
5797 statement = cp_parser_labeled_statement (parser,
5803 statement = cp_parser_selection_statement (parser);
5809 statement = cp_parser_iteration_statement (parser);
5816 statement = cp_parser_jump_statement (parser);
5820 statement = cp_parser_try_block (parser);
5824 /* It might be a keyword like `int' that can start a
5825 declaration-statement. */
5829 else if (token->type == CPP_NAME)
5831 /* If the next token is a `:', then we are looking at a
5832 labeled-statement. */
5833 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5834 if (token->type == CPP_COLON)
5835 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5837 /* Anything that starts with a `{' must be a compound-statement. */
5838 else if (token->type == CPP_OPEN_BRACE)
5839 statement = cp_parser_compound_statement (parser, NULL, false);
5840 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5841 a statement all its own. */
5842 else if (token->type == CPP_PRAGMA)
5844 cp_lexer_handle_pragma (parser->lexer);
5848 /* Everything else must be a declaration-statement or an
5849 expression-statement. Try for the declaration-statement
5850 first, unless we are looking at a `;', in which case we know that
5851 we have an expression-statement. */
5854 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5856 cp_parser_parse_tentatively (parser);
5857 /* Try to parse the declaration-statement. */
5858 cp_parser_declaration_statement (parser);
5859 /* If that worked, we're done. */
5860 if (cp_parser_parse_definitely (parser))
5863 /* Look for an expression-statement instead. */
5864 statement = cp_parser_expression_statement (parser, in_statement_expr);
5867 /* Set the line number for the statement. */
5868 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5869 SET_EXPR_LOCATION (statement, statement_location);
5872 /* Parse a labeled-statement.
5875 identifier : statement
5876 case constant-expression : statement
5882 case constant-expression ... constant-expression : statement
5884 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5885 For an ordinary label, returns a LABEL_EXPR. */
5888 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5891 tree statement = error_mark_node;
5893 /* The next token should be an identifier. */
5894 token = cp_lexer_peek_token (parser->lexer);
5895 if (token->type != CPP_NAME
5896 && token->type != CPP_KEYWORD)
5898 cp_parser_error (parser, "expected labeled-statement");
5899 return error_mark_node;
5902 switch (token->keyword)
5909 /* Consume the `case' token. */
5910 cp_lexer_consume_token (parser->lexer);
5911 /* Parse the constant-expression. */
5912 expr = cp_parser_constant_expression (parser,
5913 /*allow_non_constant_p=*/false,
5916 ellipsis = cp_lexer_peek_token (parser->lexer);
5917 if (ellipsis->type == CPP_ELLIPSIS)
5919 /* Consume the `...' token. */
5920 cp_lexer_consume_token (parser->lexer);
5922 cp_parser_constant_expression (parser,
5923 /*allow_non_constant_p=*/false,
5925 /* We don't need to emit warnings here, as the common code
5926 will do this for us. */
5929 expr_hi = NULL_TREE;
5931 if (!parser->in_switch_statement_p)
5932 error ("case label %qE not within a switch statement", expr);
5934 statement = finish_case_label (expr, expr_hi);
5939 /* Consume the `default' token. */
5940 cp_lexer_consume_token (parser->lexer);
5941 if (!parser->in_switch_statement_p)
5942 error ("case label not within a switch statement");
5944 statement = finish_case_label (NULL_TREE, NULL_TREE);
5948 /* Anything else must be an ordinary label. */
5949 statement = finish_label_stmt (cp_parser_identifier (parser));
5953 /* Require the `:' token. */
5954 cp_parser_require (parser, CPP_COLON, "`:'");
5955 /* Parse the labeled statement. */
5956 cp_parser_statement (parser, in_statement_expr);
5958 /* Return the label, in the case of a `case' or `default' label. */
5962 /* Parse an expression-statement.
5964 expression-statement:
5967 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5968 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5969 indicates whether this expression-statement is part of an
5970 expression statement. */
5973 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
5975 tree statement = NULL_TREE;
5977 /* If the next token is a ';', then there is no expression
5979 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5980 statement = cp_parser_expression (parser);
5982 /* Consume the final `;'. */
5983 cp_parser_consume_semicolon_at_end_of_statement (parser);
5985 if (in_statement_expr
5986 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5988 /* This is the final expression statement of a statement
5990 statement = finish_stmt_expr_expr (statement, in_statement_expr);
5993 statement = finish_expr_stmt (statement);
6000 /* Parse a compound-statement.
6003 { statement-seq [opt] }
6005 Returns a tree representing the statement. */
6008 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6013 /* Consume the `{'. */
6014 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6015 return error_mark_node;
6016 /* Begin the compound-statement. */
6017 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6018 /* Parse an (optional) statement-seq. */
6019 cp_parser_statement_seq_opt (parser, in_statement_expr);
6020 /* Finish the compound-statement. */
6021 finish_compound_stmt (compound_stmt);
6022 /* Consume the `}'. */
6023 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6025 return compound_stmt;
6028 /* Parse an (optional) statement-seq.
6032 statement-seq [opt] statement */
6035 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6037 /* Scan statements until there aren't any more. */
6040 /* If we're looking at a `}', then we've run out of statements. */
6041 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6042 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6045 /* Parse the statement. */
6046 cp_parser_statement (parser, in_statement_expr);
6050 /* Parse a selection-statement.
6052 selection-statement:
6053 if ( condition ) statement
6054 if ( condition ) statement else statement
6055 switch ( condition ) statement
6057 Returns the new IF_STMT or SWITCH_STMT. */
6060 cp_parser_selection_statement (cp_parser* parser)
6065 /* Peek at the next token. */
6066 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6068 /* See what kind of keyword it is. */
6069 keyword = token->keyword;
6078 /* Look for the `('. */
6079 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6081 cp_parser_skip_to_end_of_statement (parser);
6082 return error_mark_node;
6085 /* Begin the selection-statement. */
6086 if (keyword == RID_IF)
6087 statement = begin_if_stmt ();
6089 statement = begin_switch_stmt ();
6091 /* Parse the condition. */
6092 condition = cp_parser_condition (parser);
6093 /* Look for the `)'. */
6094 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6095 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6096 /*consume_paren=*/true);
6098 if (keyword == RID_IF)
6100 /* Add the condition. */
6101 finish_if_stmt_cond (condition, statement);
6103 /* Parse the then-clause. */
6104 cp_parser_implicitly_scoped_statement (parser);
6105 finish_then_clause (statement);
6107 /* If the next token is `else', parse the else-clause. */
6108 if (cp_lexer_next_token_is_keyword (parser->lexer,
6111 /* Consume the `else' keyword. */
6112 cp_lexer_consume_token (parser->lexer);
6113 begin_else_clause (statement);
6114 /* Parse the else-clause. */
6115 cp_parser_implicitly_scoped_statement (parser);
6116 finish_else_clause (statement);
6119 /* Now we're all done with the if-statement. */
6120 finish_if_stmt (statement);
6124 bool in_switch_statement_p;
6126 /* Add the condition. */
6127 finish_switch_cond (condition, statement);
6129 /* Parse the body of the switch-statement. */
6130 in_switch_statement_p = parser->in_switch_statement_p;
6131 parser->in_switch_statement_p = true;
6132 cp_parser_implicitly_scoped_statement (parser);
6133 parser->in_switch_statement_p = in_switch_statement_p;
6135 /* Now we're all done with the switch-statement. */
6136 finish_switch_stmt (statement);
6144 cp_parser_error (parser, "expected selection-statement");
6145 return error_mark_node;
6149 /* Parse a condition.
6153 type-specifier-seq declarator = assignment-expression
6158 type-specifier-seq declarator asm-specification [opt]
6159 attributes [opt] = assignment-expression
6161 Returns the expression that should be tested. */
6164 cp_parser_condition (cp_parser* parser)
6166 cp_decl_specifier_seq type_specifiers;
6167 const char *saved_message;
6169 /* Try the declaration first. */
6170 cp_parser_parse_tentatively (parser);
6171 /* New types are not allowed in the type-specifier-seq for a
6173 saved_message = parser->type_definition_forbidden_message;
6174 parser->type_definition_forbidden_message
6175 = "types may not be defined in conditions";
6176 /* Parse the type-specifier-seq. */
6177 cp_parser_type_specifier_seq (parser, &type_specifiers);
6178 /* Restore the saved message. */
6179 parser->type_definition_forbidden_message = saved_message;
6180 /* If all is well, we might be looking at a declaration. */
6181 if (!cp_parser_error_occurred (parser))
6184 tree asm_specification;
6186 cp_declarator *declarator;
6187 tree initializer = NULL_TREE;
6189 /* Parse the declarator. */
6190 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6191 /*ctor_dtor_or_conv_p=*/NULL,
6192 /*parenthesized_p=*/NULL,
6193 /*member_p=*/false);
6194 /* Parse the attributes. */
6195 attributes = cp_parser_attributes_opt (parser);
6196 /* Parse the asm-specification. */
6197 asm_specification = cp_parser_asm_specification_opt (parser);
6198 /* If the next token is not an `=', then we might still be
6199 looking at an expression. For example:
6203 looks like a decl-specifier-seq and a declarator -- but then
6204 there is no `=', so this is an expression. */
6205 cp_parser_require (parser, CPP_EQ, "`='");
6206 /* If we did see an `=', then we are looking at a declaration
6208 if (cp_parser_parse_definitely (parser))
6212 /* Create the declaration. */
6213 decl = start_decl (declarator, &type_specifiers,
6214 /*initialized_p=*/true,
6215 attributes, /*prefix_attributes=*/NULL_TREE,
6217 /* Parse the assignment-expression. */
6218 initializer = cp_parser_assignment_expression (parser);
6220 /* Process the initializer. */
6221 cp_finish_decl (decl,
6224 LOOKUP_ONLYCONVERTING);
6227 pop_scope (DECL_CONTEXT (decl));
6229 return convert_from_reference (decl);
6232 /* If we didn't even get past the declarator successfully, we are
6233 definitely not looking at a declaration. */
6235 cp_parser_abort_tentative_parse (parser);
6237 /* Otherwise, we are looking at an expression. */
6238 return cp_parser_expression (parser);
6241 /* Parse an iteration-statement.
6243 iteration-statement:
6244 while ( condition ) statement
6245 do statement while ( expression ) ;
6246 for ( for-init-statement condition [opt] ; expression [opt] )
6249 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6252 cp_parser_iteration_statement (cp_parser* parser)
6257 bool in_iteration_statement_p;
6260 /* Peek at the next token. */
6261 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6263 return error_mark_node;
6265 /* Remember whether or not we are already within an iteration
6267 in_iteration_statement_p = parser->in_iteration_statement_p;
6269 /* See what kind of keyword it is. */
6270 keyword = token->keyword;
6277 /* Begin the while-statement. */
6278 statement = begin_while_stmt ();
6279 /* Look for the `('. */
6280 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6281 /* Parse the condition. */
6282 condition = cp_parser_condition (parser);
6283 finish_while_stmt_cond (condition, statement);
6284 /* Look for the `)'. */
6285 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6286 /* Parse the dependent statement. */
6287 parser->in_iteration_statement_p = true;
6288 cp_parser_already_scoped_statement (parser);
6289 parser->in_iteration_statement_p = in_iteration_statement_p;
6290 /* We're done with the while-statement. */
6291 finish_while_stmt (statement);
6299 /* Begin the do-statement. */
6300 statement = begin_do_stmt ();
6301 /* Parse the body of the do-statement. */
6302 parser->in_iteration_statement_p = true;
6303 cp_parser_implicitly_scoped_statement (parser);
6304 parser->in_iteration_statement_p = in_iteration_statement_p;
6305 finish_do_body (statement);
6306 /* Look for the `while' keyword. */
6307 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6308 /* Look for the `('. */
6309 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6310 /* Parse the expression. */
6311 expression = cp_parser_expression (parser);
6312 /* We're done with the do-statement. */
6313 finish_do_stmt (expression, statement);
6314 /* Look for the `)'. */
6315 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6316 /* Look for the `;'. */
6317 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6323 tree condition = NULL_TREE;
6324 tree expression = NULL_TREE;
6326 /* Begin the for-statement. */
6327 statement = begin_for_stmt ();
6328 /* Look for the `('. */
6329 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6330 /* Parse the initialization. */
6331 cp_parser_for_init_statement (parser);
6332 finish_for_init_stmt (statement);
6334 /* If there's a condition, process it. */
6335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6336 condition = cp_parser_condition (parser);
6337 finish_for_cond (condition, statement);
6338 /* Look for the `;'. */
6339 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6341 /* If there's an expression, process it. */
6342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6343 expression = cp_parser_expression (parser);
6344 finish_for_expr (expression, statement);
6345 /* Look for the `)'. */
6346 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6348 /* Parse the body of the for-statement. */
6349 parser->in_iteration_statement_p = true;
6350 cp_parser_already_scoped_statement (parser);
6351 parser->in_iteration_statement_p = in_iteration_statement_p;
6353 /* We're done with the for-statement. */
6354 finish_for_stmt (statement);
6359 cp_parser_error (parser, "expected iteration-statement");
6360 statement = error_mark_node;
6367 /* Parse a for-init-statement.
6370 expression-statement
6371 simple-declaration */
6374 cp_parser_for_init_statement (cp_parser* parser)
6376 /* If the next token is a `;', then we have an empty
6377 expression-statement. Grammatically, this is also a
6378 simple-declaration, but an invalid one, because it does not
6379 declare anything. Therefore, if we did not handle this case
6380 specially, we would issue an error message about an invalid
6382 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6384 /* We're going to speculatively look for a declaration, falling back
6385 to an expression, if necessary. */
6386 cp_parser_parse_tentatively (parser);
6387 /* Parse the declaration. */
6388 cp_parser_simple_declaration (parser,
6389 /*function_definition_allowed_p=*/false);
6390 /* If the tentative parse failed, then we shall need to look for an
6391 expression-statement. */
6392 if (cp_parser_parse_definitely (parser))
6396 cp_parser_expression_statement (parser, false);
6399 /* Parse a jump-statement.
6404 return expression [opt] ;
6412 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6415 cp_parser_jump_statement (cp_parser* parser)
6417 tree statement = error_mark_node;
6421 /* Peek at the next token. */
6422 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6424 return error_mark_node;
6426 /* See what kind of keyword it is. */
6427 keyword = token->keyword;
6431 if (!parser->in_switch_statement_p
6432 && !parser->in_iteration_statement_p)
6434 error ("break statement not within loop or switch");
6435 statement = error_mark_node;
6438 statement = finish_break_stmt ();
6439 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6443 if (!parser->in_iteration_statement_p)
6445 error ("continue statement not within a loop");
6446 statement = error_mark_node;
6449 statement = finish_continue_stmt ();
6450 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6457 /* If the next token is a `;', then there is no
6459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6460 expr = cp_parser_expression (parser);
6463 /* Build the return-statement. */
6464 statement = finish_return_stmt (expr);
6465 /* Look for the final `;'. */
6466 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6471 /* Create the goto-statement. */
6472 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6474 /* Issue a warning about this use of a GNU extension. */
6476 pedwarn ("ISO C++ forbids computed gotos");
6477 /* Consume the '*' token. */
6478 cp_lexer_consume_token (parser->lexer);
6479 /* Parse the dependent expression. */
6480 finish_goto_stmt (cp_parser_expression (parser));
6483 finish_goto_stmt (cp_parser_identifier (parser));
6484 /* Look for the final `;'. */
6485 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6489 cp_parser_error (parser, "expected jump-statement");
6496 /* Parse a declaration-statement.
6498 declaration-statement:
6499 block-declaration */
6502 cp_parser_declaration_statement (cp_parser* parser)
6506 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6507 p = obstack_alloc (&declarator_obstack, 0);
6509 /* Parse the block-declaration. */
6510 cp_parser_block_declaration (parser, /*statement_p=*/true);
6512 /* Free any declarators allocated. */
6513 obstack_free (&declarator_obstack, p);
6515 /* Finish off the statement. */
6519 /* Some dependent statements (like `if (cond) statement'), are
6520 implicitly in their own scope. In other words, if the statement is
6521 a single statement (as opposed to a compound-statement), it is
6522 none-the-less treated as if it were enclosed in braces. Any
6523 declarations appearing in the dependent statement are out of scope
6524 after control passes that point. This function parses a statement,
6525 but ensures that is in its own scope, even if it is not a
6528 Returns the new statement. */
6531 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6535 /* If the token is not a `{', then we must take special action. */
6536 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6538 /* Create a compound-statement. */
6539 statement = begin_compound_stmt (0);
6540 /* Parse the dependent-statement. */
6541 cp_parser_statement (parser, false);
6542 /* Finish the dummy compound-statement. */
6543 finish_compound_stmt (statement);
6545 /* Otherwise, we simply parse the statement directly. */
6547 statement = cp_parser_compound_statement (parser, NULL, false);
6549 /* Return the statement. */
6553 /* For some dependent statements (like `while (cond) statement'), we
6554 have already created a scope. Therefore, even if the dependent
6555 statement is a compound-statement, we do not want to create another
6559 cp_parser_already_scoped_statement (cp_parser* parser)
6561 /* If the token is a `{', then we must take special action. */
6562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6563 cp_parser_statement (parser, false);
6566 /* Avoid calling cp_parser_compound_statement, so that we
6567 don't create a new scope. Do everything else by hand. */
6568 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6569 cp_parser_statement_seq_opt (parser, false);
6570 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6574 /* Declarations [gram.dcl.dcl] */
6576 /* Parse an optional declaration-sequence.
6580 declaration-seq declaration */
6583 cp_parser_declaration_seq_opt (cp_parser* parser)
6589 token = cp_lexer_peek_token (parser->lexer);
6591 if (token->type == CPP_CLOSE_BRACE
6592 || token->type == CPP_EOF)
6595 if (token->type == CPP_SEMICOLON)
6597 /* A declaration consisting of a single semicolon is
6598 invalid. Allow it unless we're being pedantic. */
6599 cp_lexer_consume_token (parser->lexer);
6600 if (pedantic && !in_system_header)
6601 pedwarn ("extra %<;%>");
6605 /* If we're entering or exiting a region that's implicitly
6606 extern "C", modify the lang context appropriately. */
6607 if (!parser->implicit_extern_c && token->implicit_extern_c)
6609 push_lang_context (lang_name_c);
6610 parser->implicit_extern_c = true;
6612 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6614 pop_lang_context ();
6615 parser->implicit_extern_c = false;
6618 if (token->type == CPP_PRAGMA)
6620 /* A top-level declaration can consist solely of a #pragma.
6621 A nested declaration cannot, so this is done here and not
6622 in cp_parser_declaration. (A #pragma at block scope is
6623 handled in cp_parser_statement.) */
6624 cp_lexer_handle_pragma (parser->lexer);
6628 /* Parse the declaration itself. */
6629 cp_parser_declaration (parser);
6633 /* Parse a declaration.
6638 template-declaration
6639 explicit-instantiation
6640 explicit-specialization
6641 linkage-specification
6642 namespace-definition
6647 __extension__ declaration */
6650 cp_parser_declaration (cp_parser* parser)
6657 /* Check for the `__extension__' keyword. */
6658 if (cp_parser_extension_opt (parser, &saved_pedantic))
6660 /* Parse the qualified declaration. */
6661 cp_parser_declaration (parser);
6662 /* Restore the PEDANTIC flag. */
6663 pedantic = saved_pedantic;
6668 /* Try to figure out what kind of declaration is present. */
6669 token1 = *cp_lexer_peek_token (parser->lexer);
6671 if (token1.type != CPP_EOF)
6672 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6674 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6675 p = obstack_alloc (&declarator_obstack, 0);
6677 /* If the next token is `extern' and the following token is a string
6678 literal, then we have a linkage specification. */
6679 if (token1.keyword == RID_EXTERN
6680 && cp_parser_is_string_literal (&token2))
6681 cp_parser_linkage_specification (parser);
6682 /* If the next token is `template', then we have either a template
6683 declaration, an explicit instantiation, or an explicit
6685 else if (token1.keyword == RID_TEMPLATE)
6687 /* `template <>' indicates a template specialization. */
6688 if (token2.type == CPP_LESS
6689 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6690 cp_parser_explicit_specialization (parser);
6691 /* `template <' indicates a template declaration. */
6692 else if (token2.type == CPP_LESS)
6693 cp_parser_template_declaration (parser, /*member_p=*/false);
6694 /* Anything else must be an explicit instantiation. */
6696 cp_parser_explicit_instantiation (parser);
6698 /* If the next token is `export', then we have a template
6700 else if (token1.keyword == RID_EXPORT)
6701 cp_parser_template_declaration (parser, /*member_p=*/false);
6702 /* If the next token is `extern', 'static' or 'inline' and the one
6703 after that is `template', we have a GNU extended explicit
6704 instantiation directive. */
6705 else if (cp_parser_allow_gnu_extensions_p (parser)
6706 && (token1.keyword == RID_EXTERN
6707 || token1.keyword == RID_STATIC
6708 || token1.keyword == RID_INLINE)
6709 && token2.keyword == RID_TEMPLATE)
6710 cp_parser_explicit_instantiation (parser);
6711 /* If the next token is `namespace', check for a named or unnamed
6712 namespace definition. */
6713 else if (token1.keyword == RID_NAMESPACE
6714 && (/* A named namespace definition. */
6715 (token2.type == CPP_NAME
6716 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6718 /* An unnamed namespace definition. */
6719 || token2.type == CPP_OPEN_BRACE))
6720 cp_parser_namespace_definition (parser);
6721 /* We must have either a block declaration or a function
6724 /* Try to parse a block-declaration, or a function-definition. */
6725 cp_parser_block_declaration (parser, /*statement_p=*/false);
6727 /* Free any declarators allocated. */
6728 obstack_free (&declarator_obstack, p);
6731 /* Parse a block-declaration.
6736 namespace-alias-definition
6743 __extension__ block-declaration
6746 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6747 part of a declaration-statement. */
6750 cp_parser_block_declaration (cp_parser *parser,
6756 /* Check for the `__extension__' keyword. */
6757 if (cp_parser_extension_opt (parser, &saved_pedantic))
6759 /* Parse the qualified declaration. */
6760 cp_parser_block_declaration (parser, statement_p);
6761 /* Restore the PEDANTIC flag. */
6762 pedantic = saved_pedantic;
6767 /* Peek at the next token to figure out which kind of declaration is
6769 token1 = cp_lexer_peek_token (parser->lexer);
6771 /* If the next keyword is `asm', we have an asm-definition. */
6772 if (token1->keyword == RID_ASM)
6775 cp_parser_commit_to_tentative_parse (parser);
6776 cp_parser_asm_definition (parser);
6778 /* If the next keyword is `namespace', we have a
6779 namespace-alias-definition. */
6780 else if (token1->keyword == RID_NAMESPACE)
6781 cp_parser_namespace_alias_definition (parser);
6782 /* If the next keyword is `using', we have either a
6783 using-declaration or a using-directive. */
6784 else if (token1->keyword == RID_USING)
6789 cp_parser_commit_to_tentative_parse (parser);
6790 /* If the token after `using' is `namespace', then we have a
6792 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6793 if (token2->keyword == RID_NAMESPACE)
6794 cp_parser_using_directive (parser);
6795 /* Otherwise, it's a using-declaration. */
6797 cp_parser_using_declaration (parser);
6799 /* If the next keyword is `__label__' we have a label declaration. */
6800 else if (token1->keyword == RID_LABEL)
6803 cp_parser_commit_to_tentative_parse (parser);
6804 cp_parser_label_declaration (parser);
6806 /* Anything else must be a simple-declaration. */
6808 cp_parser_simple_declaration (parser, !statement_p);
6811 /* Parse a simple-declaration.
6814 decl-specifier-seq [opt] init-declarator-list [opt] ;
6816 init-declarator-list:
6818 init-declarator-list , init-declarator
6820 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6821 function-definition as a simple-declaration. */
6824 cp_parser_simple_declaration (cp_parser* parser,
6825 bool function_definition_allowed_p)
6827 cp_decl_specifier_seq decl_specifiers;
6828 int declares_class_or_enum;
6829 bool saw_declarator;
6831 /* Defer access checks until we know what is being declared; the
6832 checks for names appearing in the decl-specifier-seq should be
6833 done as if we were in the scope of the thing being declared. */
6834 push_deferring_access_checks (dk_deferred);
6836 /* Parse the decl-specifier-seq. We have to keep track of whether
6837 or not the decl-specifier-seq declares a named class or
6838 enumeration type, since that is the only case in which the
6839 init-declarator-list is allowed to be empty.
6843 In a simple-declaration, the optional init-declarator-list can be
6844 omitted only when declaring a class or enumeration, that is when
6845 the decl-specifier-seq contains either a class-specifier, an
6846 elaborated-type-specifier, or an enum-specifier. */
6847 cp_parser_decl_specifier_seq (parser,
6848 CP_PARSER_FLAGS_OPTIONAL,
6850 &declares_class_or_enum);
6851 /* We no longer need to defer access checks. */
6852 stop_deferring_access_checks ();
6854 /* In a block scope, a valid declaration must always have a
6855 decl-specifier-seq. By not trying to parse declarators, we can
6856 resolve the declaration/expression ambiguity more quickly. */
6857 if (!function_definition_allowed_p
6858 && !decl_specifiers.any_specifiers_p)
6860 cp_parser_error (parser, "expected declaration");
6864 /* If the next two tokens are both identifiers, the code is
6865 erroneous. The usual cause of this situation is code like:
6869 where "T" should name a type -- but does not. */
6870 if (!decl_specifiers.type
6871 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6873 /* If parsing tentatively, we should commit; we really are
6874 looking at a declaration. */
6875 cp_parser_commit_to_tentative_parse (parser);
6880 /* If we have seen at least one decl-specifier, and the next token
6881 is not a parenthesis, then we must be looking at a declaration.
6882 (After "int (" we might be looking at a functional cast.) */
6883 if (decl_specifiers.any_specifiers_p
6884 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6885 cp_parser_commit_to_tentative_parse (parser);
6887 /* Keep going until we hit the `;' at the end of the simple
6889 saw_declarator = false;
6890 while (cp_lexer_next_token_is_not (parser->lexer,
6894 bool function_definition_p;
6897 saw_declarator = true;
6898 /* Parse the init-declarator. */
6899 decl = cp_parser_init_declarator (parser, &decl_specifiers,
6900 function_definition_allowed_p,
6902 declares_class_or_enum,
6903 &function_definition_p);
6904 /* If an error occurred while parsing tentatively, exit quickly.
6905 (That usually happens when in the body of a function; each
6906 statement is treated as a declaration-statement until proven
6908 if (cp_parser_error_occurred (parser))
6910 /* Handle function definitions specially. */
6911 if (function_definition_p)
6913 /* If the next token is a `,', then we are probably
6914 processing something like:
6918 which is erroneous. */
6919 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6920 error ("mixing declarations and function-definitions is forbidden");
6921 /* Otherwise, we're done with the list of declarators. */
6924 pop_deferring_access_checks ();
6928 /* The next token should be either a `,' or a `;'. */
6929 token = cp_lexer_peek_token (parser->lexer);
6930 /* If it's a `,', there are more declarators to come. */
6931 if (token->type == CPP_COMMA)
6932 cp_lexer_consume_token (parser->lexer);
6933 /* If it's a `;', we are done. */
6934 else if (token->type == CPP_SEMICOLON)
6936 /* Anything else is an error. */
6939 /* If we have already issued an error message we don't need
6940 to issue another one. */
6941 if (decl != error_mark_node
6942 || (cp_parser_parsing_tentatively (parser)
6943 && !cp_parser_committed_to_tentative_parse (parser)))
6944 cp_parser_error (parser, "expected %<,%> or %<;%>");
6945 /* Skip tokens until we reach the end of the statement. */
6946 cp_parser_skip_to_end_of_statement (parser);
6947 /* If the next token is now a `;', consume it. */
6948 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6949 cp_lexer_consume_token (parser->lexer);
6952 /* After the first time around, a function-definition is not
6953 allowed -- even if it was OK at first. For example:
6958 function_definition_allowed_p = false;
6961 /* Issue an error message if no declarators are present, and the
6962 decl-specifier-seq does not itself declare a class or
6964 if (!saw_declarator)
6966 if (cp_parser_declares_only_class_p (parser))
6967 shadow_tag (&decl_specifiers);
6968 /* Perform any deferred access checks. */
6969 perform_deferred_access_checks ();
6972 /* Consume the `;'. */
6973 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6976 pop_deferring_access_checks ();
6979 /* Parse a decl-specifier-seq.
6982 decl-specifier-seq [opt] decl-specifier
6985 storage-class-specifier
6996 Set *DECL_SPECS to a representation of the decl-specifier-seq.
6998 The parser flags FLAGS is used to control type-specifier parsing.
7000 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7003 1: one of the decl-specifiers is an elaborated-type-specifier
7004 (i.e., a type declaration)
7005 2: one of the decl-specifiers is an enum-specifier or a
7006 class-specifier (i.e., a type definition)
7011 cp_parser_decl_specifier_seq (cp_parser* parser,
7012 cp_parser_flags flags,
7013 cp_decl_specifier_seq *decl_specs,
7014 int* declares_class_or_enum)
7016 bool constructor_possible_p = !parser->in_declarator_p;
7018 /* Clear DECL_SPECS. */
7019 clear_decl_specs (decl_specs);
7021 /* Assume no class or enumeration type is declared. */
7022 *declares_class_or_enum = 0;
7024 /* Keep reading specifiers until there are no more to read. */
7028 bool found_decl_spec;
7031 /* Peek at the next token. */
7032 token = cp_lexer_peek_token (parser->lexer);
7033 /* Handle attributes. */
7034 if (token->keyword == RID_ATTRIBUTE)
7036 /* Parse the attributes. */
7037 decl_specs->attributes
7038 = chainon (decl_specs->attributes,
7039 cp_parser_attributes_opt (parser));
7042 /* Assume we will find a decl-specifier keyword. */
7043 found_decl_spec = true;
7044 /* If the next token is an appropriate keyword, we can simply
7045 add it to the list. */
7046 switch (token->keyword)
7051 if (decl_specs->specs[(int) ds_friend]++)
7052 error ("duplicate %<friend%>");
7053 /* Consume the token. */
7054 cp_lexer_consume_token (parser->lexer);
7057 /* function-specifier:
7064 cp_parser_function_specifier_opt (parser, decl_specs);
7070 ++decl_specs->specs[(int) ds_typedef];
7071 /* Consume the token. */
7072 cp_lexer_consume_token (parser->lexer);
7073 /* A constructor declarator cannot appear in a typedef. */
7074 constructor_possible_p = false;
7075 /* The "typedef" keyword can only occur in a declaration; we
7076 may as well commit at this point. */
7077 cp_parser_commit_to_tentative_parse (parser);
7080 /* storage-class-specifier:
7090 /* Consume the token. */
7091 cp_lexer_consume_token (parser->lexer);
7092 cp_parser_set_storage_class (decl_specs, sc_auto);
7095 /* Consume the token. */
7096 cp_lexer_consume_token (parser->lexer);
7097 cp_parser_set_storage_class (decl_specs, sc_register);
7100 /* Consume the token. */
7101 cp_lexer_consume_token (parser->lexer);
7102 if (decl_specs->specs[(int) ds_thread])
7104 error ("%<__thread%> before %<static%>");
7105 decl_specs->specs[(int) ds_thread] = 0;
7107 cp_parser_set_storage_class (decl_specs, sc_static);
7110 /* Consume the token. */
7111 cp_lexer_consume_token (parser->lexer);
7112 if (decl_specs->specs[(int) ds_thread])
7114 error ("%<__thread%> before %<extern%>");
7115 decl_specs->specs[(int) ds_thread] = 0;
7117 cp_parser_set_storage_class (decl_specs, sc_extern);
7120 /* Consume the token. */
7121 cp_lexer_consume_token (parser->lexer);
7122 cp_parser_set_storage_class (decl_specs, sc_mutable);
7125 /* Consume the token. */
7126 cp_lexer_consume_token (parser->lexer);
7127 ++decl_specs->specs[(int) ds_thread];
7131 /* We did not yet find a decl-specifier yet. */
7132 found_decl_spec = false;
7136 /* Constructors are a special case. The `S' in `S()' is not a
7137 decl-specifier; it is the beginning of the declarator. */
7140 && constructor_possible_p
7141 && (cp_parser_constructor_declarator_p
7142 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7144 /* If we don't have a DECL_SPEC yet, then we must be looking at
7145 a type-specifier. */
7146 if (!found_decl_spec && !constructor_p)
7148 int decl_spec_declares_class_or_enum;
7149 bool is_cv_qualifier;
7153 = cp_parser_type_specifier (parser, flags,
7155 /*is_declaration=*/true,
7156 &decl_spec_declares_class_or_enum,
7159 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7161 /* If this type-specifier referenced a user-defined type
7162 (a typedef, class-name, etc.), then we can't allow any
7163 more such type-specifiers henceforth.
7167 The longest sequence of decl-specifiers that could
7168 possibly be a type name is taken as the
7169 decl-specifier-seq of a declaration. The sequence shall
7170 be self-consistent as described below.
7174 As a general rule, at most one type-specifier is allowed
7175 in the complete decl-specifier-seq of a declaration. The
7176 only exceptions are the following:
7178 -- const or volatile can be combined with any other
7181 -- signed or unsigned can be combined with char, long,
7189 void g (const int Pc);
7191 Here, Pc is *not* part of the decl-specifier seq; it's
7192 the declarator. Therefore, once we see a type-specifier
7193 (other than a cv-qualifier), we forbid any additional
7194 user-defined types. We *do* still allow things like `int
7195 int' to be considered a decl-specifier-seq, and issue the
7196 error message later. */
7197 if (type_spec && !is_cv_qualifier)
7198 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7199 /* A constructor declarator cannot follow a type-specifier. */
7202 constructor_possible_p = false;
7203 found_decl_spec = true;
7207 /* If we still do not have a DECL_SPEC, then there are no more
7209 if (!found_decl_spec)
7212 decl_specs->any_specifiers_p = true;
7213 /* After we see one decl-specifier, further decl-specifiers are
7215 flags |= CP_PARSER_FLAGS_OPTIONAL;
7218 /* Don't allow a friend specifier with a class definition. */
7219 if (decl_specs->specs[(int) ds_friend] != 0
7220 && (*declares_class_or_enum & 2))
7221 error ("class definition may not be declared a friend");
7224 /* Parse an (optional) storage-class-specifier.
7226 storage-class-specifier:
7235 storage-class-specifier:
7238 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7241 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7243 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7251 /* Consume the token. */
7252 return cp_lexer_consume_token (parser->lexer)->value;
7259 /* Parse an (optional) function-specifier.
7266 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7267 Updates DECL_SPECS, if it is non-NULL. */
7270 cp_parser_function_specifier_opt (cp_parser* parser,
7271 cp_decl_specifier_seq *decl_specs)
7273 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7277 ++decl_specs->specs[(int) ds_inline];
7282 ++decl_specs->specs[(int) ds_virtual];
7287 ++decl_specs->specs[(int) ds_explicit];
7294 /* Consume the token. */
7295 return cp_lexer_consume_token (parser->lexer)->value;
7298 /* Parse a linkage-specification.
7300 linkage-specification:
7301 extern string-literal { declaration-seq [opt] }
7302 extern string-literal declaration */
7305 cp_parser_linkage_specification (cp_parser* parser)
7309 /* Look for the `extern' keyword. */
7310 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7312 /* Look for the string-literal. */
7313 linkage = cp_parser_string_literal (parser, false, false);
7315 /* Transform the literal into an identifier. If the literal is a
7316 wide-character string, or contains embedded NULs, then we can't
7317 handle it as the user wants. */
7318 if (strlen (TREE_STRING_POINTER (linkage))
7319 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7321 cp_parser_error (parser, "invalid linkage-specification");
7322 /* Assume C++ linkage. */
7323 linkage = lang_name_cplusplus;
7326 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7328 /* We're now using the new linkage. */
7329 push_lang_context (linkage);
7331 /* If the next token is a `{', then we're using the first
7333 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7335 /* Consume the `{' token. */
7336 cp_lexer_consume_token (parser->lexer);
7337 /* Parse the declarations. */
7338 cp_parser_declaration_seq_opt (parser);
7339 /* Look for the closing `}'. */
7340 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7342 /* Otherwise, there's just one declaration. */
7345 bool saved_in_unbraced_linkage_specification_p;
7347 saved_in_unbraced_linkage_specification_p
7348 = parser->in_unbraced_linkage_specification_p;
7349 parser->in_unbraced_linkage_specification_p = true;
7350 have_extern_spec = true;
7351 cp_parser_declaration (parser);
7352 have_extern_spec = false;
7353 parser->in_unbraced_linkage_specification_p
7354 = saved_in_unbraced_linkage_specification_p;
7357 /* We're done with the linkage-specification. */
7358 pop_lang_context ();
7361 /* Special member functions [gram.special] */
7363 /* Parse a conversion-function-id.
7365 conversion-function-id:
7366 operator conversion-type-id
7368 Returns an IDENTIFIER_NODE representing the operator. */
7371 cp_parser_conversion_function_id (cp_parser* parser)
7375 tree saved_qualifying_scope;
7376 tree saved_object_scope;
7379 /* Look for the `operator' token. */
7380 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7381 return error_mark_node;
7382 /* When we parse the conversion-type-id, the current scope will be
7383 reset. However, we need that information in able to look up the
7384 conversion function later, so we save it here. */
7385 saved_scope = parser->scope;
7386 saved_qualifying_scope = parser->qualifying_scope;
7387 saved_object_scope = parser->object_scope;
7388 /* We must enter the scope of the class so that the names of
7389 entities declared within the class are available in the
7390 conversion-type-id. For example, consider:
7397 S::operator I() { ... }
7399 In order to see that `I' is a type-name in the definition, we
7400 must be in the scope of `S'. */
7402 pop_p = push_scope (saved_scope);
7403 /* Parse the conversion-type-id. */
7404 type = cp_parser_conversion_type_id (parser);
7405 /* Leave the scope of the class, if any. */
7407 pop_scope (saved_scope);
7408 /* Restore the saved scope. */
7409 parser->scope = saved_scope;
7410 parser->qualifying_scope = saved_qualifying_scope;
7411 parser->object_scope = saved_object_scope;
7412 /* If the TYPE is invalid, indicate failure. */
7413 if (type == error_mark_node)
7414 return error_mark_node;
7415 return mangle_conv_op_name_for_type (type);
7418 /* Parse a conversion-type-id:
7421 type-specifier-seq conversion-declarator [opt]
7423 Returns the TYPE specified. */
7426 cp_parser_conversion_type_id (cp_parser* parser)
7429 cp_decl_specifier_seq type_specifiers;
7430 cp_declarator *declarator;
7431 tree type_specified;
7433 /* Parse the attributes. */
7434 attributes = cp_parser_attributes_opt (parser);
7435 /* Parse the type-specifiers. */
7436 cp_parser_type_specifier_seq (parser, &type_specifiers);
7437 /* If that didn't work, stop. */
7438 if (type_specifiers.type == error_mark_node)
7439 return error_mark_node;
7440 /* Parse the conversion-declarator. */
7441 declarator = cp_parser_conversion_declarator_opt (parser);
7443 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7444 /*initialized=*/0, &attributes);
7446 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7447 return type_specified;
7450 /* Parse an (optional) conversion-declarator.
7452 conversion-declarator:
7453 ptr-operator conversion-declarator [opt]
7457 static cp_declarator *
7458 cp_parser_conversion_declarator_opt (cp_parser* parser)
7460 enum tree_code code;
7462 cp_cv_quals cv_quals;
7464 /* We don't know if there's a ptr-operator next, or not. */
7465 cp_parser_parse_tentatively (parser);
7466 /* Try the ptr-operator. */
7467 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7468 /* If it worked, look for more conversion-declarators. */
7469 if (cp_parser_parse_definitely (parser))
7471 cp_declarator *declarator;
7473 /* Parse another optional declarator. */
7474 declarator = cp_parser_conversion_declarator_opt (parser);
7476 /* Create the representation of the declarator. */
7478 declarator = make_ptrmem_declarator (cv_quals, class_type,
7480 else if (code == INDIRECT_REF)
7481 declarator = make_pointer_declarator (cv_quals, declarator);
7483 declarator = make_reference_declarator (cv_quals, declarator);
7491 /* Parse an (optional) ctor-initializer.
7494 : mem-initializer-list
7496 Returns TRUE iff the ctor-initializer was actually present. */
7499 cp_parser_ctor_initializer_opt (cp_parser* parser)
7501 /* If the next token is not a `:', then there is no
7502 ctor-initializer. */
7503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7505 /* Do default initialization of any bases and members. */
7506 if (DECL_CONSTRUCTOR_P (current_function_decl))
7507 finish_mem_initializers (NULL_TREE);
7512 /* Consume the `:' token. */
7513 cp_lexer_consume_token (parser->lexer);
7514 /* And the mem-initializer-list. */
7515 cp_parser_mem_initializer_list (parser);
7520 /* Parse a mem-initializer-list.
7522 mem-initializer-list:
7524 mem-initializer , mem-initializer-list */
7527 cp_parser_mem_initializer_list (cp_parser* parser)
7529 tree mem_initializer_list = NULL_TREE;
7531 /* Let the semantic analysis code know that we are starting the
7532 mem-initializer-list. */
7533 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7534 error ("only constructors take base initializers");
7536 /* Loop through the list. */
7539 tree mem_initializer;
7541 /* Parse the mem-initializer. */
7542 mem_initializer = cp_parser_mem_initializer (parser);
7543 /* Add it to the list, unless it was erroneous. */
7544 if (mem_initializer)
7546 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7547 mem_initializer_list = mem_initializer;
7549 /* If the next token is not a `,', we're done. */
7550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7552 /* Consume the `,' token. */
7553 cp_lexer_consume_token (parser->lexer);
7556 /* Perform semantic analysis. */
7557 if (DECL_CONSTRUCTOR_P (current_function_decl))
7558 finish_mem_initializers (mem_initializer_list);
7561 /* Parse a mem-initializer.
7564 mem-initializer-id ( expression-list [opt] )
7569 ( expression-list [opt] )
7571 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7572 class) or FIELD_DECL (for a non-static data member) to initialize;
7573 the TREE_VALUE is the expression-list. */
7576 cp_parser_mem_initializer (cp_parser* parser)
7578 tree mem_initializer_id;
7579 tree expression_list;
7582 /* Find out what is being initialized. */
7583 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7585 pedwarn ("anachronistic old-style base class initializer");
7586 mem_initializer_id = NULL_TREE;
7589 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7590 member = expand_member_init (mem_initializer_id);
7591 if (member && !DECL_P (member))
7592 in_base_initializer = 1;
7595 = cp_parser_parenthesized_expression_list (parser, false,
7596 /*non_constant_p=*/NULL);
7597 if (!expression_list)
7598 expression_list = void_type_node;
7600 in_base_initializer = 0;
7602 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7605 /* Parse a mem-initializer-id.
7608 :: [opt] nested-name-specifier [opt] class-name
7611 Returns a TYPE indicating the class to be initializer for the first
7612 production. Returns an IDENTIFIER_NODE indicating the data member
7613 to be initialized for the second production. */
7616 cp_parser_mem_initializer_id (cp_parser* parser)
7618 bool global_scope_p;
7619 bool nested_name_specifier_p;
7620 bool template_p = false;
7623 /* `typename' is not allowed in this context ([temp.res]). */
7624 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7626 error ("keyword %<typename%> not allowed in this context (a qualified "
7627 "member initializer is implicitly a type)");
7628 cp_lexer_consume_token (parser->lexer);
7630 /* Look for the optional `::' operator. */
7632 = (cp_parser_global_scope_opt (parser,
7633 /*current_scope_valid_p=*/false)
7635 /* Look for the optional nested-name-specifier. The simplest way to
7640 The keyword `typename' is not permitted in a base-specifier or
7641 mem-initializer; in these contexts a qualified name that
7642 depends on a template-parameter is implicitly assumed to be a
7645 is to assume that we have seen the `typename' keyword at this
7647 nested_name_specifier_p
7648 = (cp_parser_nested_name_specifier_opt (parser,
7649 /*typename_keyword_p=*/true,
7650 /*check_dependency_p=*/true,
7652 /*is_declaration=*/true)
7654 if (nested_name_specifier_p)
7655 template_p = cp_parser_optional_template_keyword (parser);
7656 /* If there is a `::' operator or a nested-name-specifier, then we
7657 are definitely looking for a class-name. */
7658 if (global_scope_p || nested_name_specifier_p)
7659 return cp_parser_class_name (parser,
7660 /*typename_keyword_p=*/true,
7661 /*template_keyword_p=*/template_p,
7663 /*check_dependency_p=*/true,
7664 /*class_head_p=*/false,
7665 /*is_declaration=*/true);
7666 /* Otherwise, we could also be looking for an ordinary identifier. */
7667 cp_parser_parse_tentatively (parser);
7668 /* Try a class-name. */
7669 id = cp_parser_class_name (parser,
7670 /*typename_keyword_p=*/true,
7671 /*template_keyword_p=*/false,
7673 /*check_dependency_p=*/true,
7674 /*class_head_p=*/false,
7675 /*is_declaration=*/true);
7676 /* If we found one, we're done. */
7677 if (cp_parser_parse_definitely (parser))
7679 /* Otherwise, look for an ordinary identifier. */
7680 return cp_parser_identifier (parser);
7683 /* Overloading [gram.over] */
7685 /* Parse an operator-function-id.
7687 operator-function-id:
7690 Returns an IDENTIFIER_NODE for the operator which is a
7691 human-readable spelling of the identifier, e.g., `operator +'. */
7694 cp_parser_operator_function_id (cp_parser* parser)
7696 /* Look for the `operator' keyword. */
7697 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7698 return error_mark_node;
7699 /* And then the name of the operator itself. */
7700 return cp_parser_operator (parser);
7703 /* Parse an operator.
7706 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7707 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7708 || ++ -- , ->* -> () []
7715 Returns an IDENTIFIER_NODE for the operator which is a
7716 human-readable spelling of the identifier, e.g., `operator +'. */
7719 cp_parser_operator (cp_parser* parser)
7721 tree id = NULL_TREE;
7724 /* Peek at the next token. */
7725 token = cp_lexer_peek_token (parser->lexer);
7726 /* Figure out which operator we have. */
7727 switch (token->type)
7733 /* The keyword should be either `new' or `delete'. */
7734 if (token->keyword == RID_NEW)
7736 else if (token->keyword == RID_DELETE)
7741 /* Consume the `new' or `delete' token. */
7742 cp_lexer_consume_token (parser->lexer);
7744 /* Peek at the next token. */
7745 token = cp_lexer_peek_token (parser->lexer);
7746 /* If it's a `[' token then this is the array variant of the
7748 if (token->type == CPP_OPEN_SQUARE)
7750 /* Consume the `[' token. */
7751 cp_lexer_consume_token (parser->lexer);
7752 /* Look for the `]' token. */
7753 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7754 id = ansi_opname (op == NEW_EXPR
7755 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7757 /* Otherwise, we have the non-array variant. */
7759 id = ansi_opname (op);
7765 id = ansi_opname (PLUS_EXPR);
7769 id = ansi_opname (MINUS_EXPR);
7773 id = ansi_opname (MULT_EXPR);
7777 id = ansi_opname (TRUNC_DIV_EXPR);
7781 id = ansi_opname (TRUNC_MOD_EXPR);
7785 id = ansi_opname (BIT_XOR_EXPR);
7789 id = ansi_opname (BIT_AND_EXPR);
7793 id = ansi_opname (BIT_IOR_EXPR);
7797 id = ansi_opname (BIT_NOT_EXPR);
7801 id = ansi_opname (TRUTH_NOT_EXPR);
7805 id = ansi_assopname (NOP_EXPR);
7809 id = ansi_opname (LT_EXPR);
7813 id = ansi_opname (GT_EXPR);
7817 id = ansi_assopname (PLUS_EXPR);
7821 id = ansi_assopname (MINUS_EXPR);
7825 id = ansi_assopname (MULT_EXPR);
7829 id = ansi_assopname (TRUNC_DIV_EXPR);
7833 id = ansi_assopname (TRUNC_MOD_EXPR);
7837 id = ansi_assopname (BIT_XOR_EXPR);
7841 id = ansi_assopname (BIT_AND_EXPR);
7845 id = ansi_assopname (BIT_IOR_EXPR);
7849 id = ansi_opname (LSHIFT_EXPR);
7853 id = ansi_opname (RSHIFT_EXPR);
7857 id = ansi_assopname (LSHIFT_EXPR);
7861 id = ansi_assopname (RSHIFT_EXPR);
7865 id = ansi_opname (EQ_EXPR);
7869 id = ansi_opname (NE_EXPR);
7873 id = ansi_opname (LE_EXPR);
7876 case CPP_GREATER_EQ:
7877 id = ansi_opname (GE_EXPR);
7881 id = ansi_opname (TRUTH_ANDIF_EXPR);
7885 id = ansi_opname (TRUTH_ORIF_EXPR);
7889 id = ansi_opname (POSTINCREMENT_EXPR);
7892 case CPP_MINUS_MINUS:
7893 id = ansi_opname (PREDECREMENT_EXPR);
7897 id = ansi_opname (COMPOUND_EXPR);
7900 case CPP_DEREF_STAR:
7901 id = ansi_opname (MEMBER_REF);
7905 id = ansi_opname (COMPONENT_REF);
7908 case CPP_OPEN_PAREN:
7909 /* Consume the `('. */
7910 cp_lexer_consume_token (parser->lexer);
7911 /* Look for the matching `)'. */
7912 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7913 return ansi_opname (CALL_EXPR);
7915 case CPP_OPEN_SQUARE:
7916 /* Consume the `['. */
7917 cp_lexer_consume_token (parser->lexer);
7918 /* Look for the matching `]'. */
7919 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7920 return ansi_opname (ARRAY_REF);
7924 id = ansi_opname (MIN_EXPR);
7928 id = ansi_opname (MAX_EXPR);
7932 id = ansi_assopname (MIN_EXPR);
7936 id = ansi_assopname (MAX_EXPR);
7940 /* Anything else is an error. */
7944 /* If we have selected an identifier, we need to consume the
7947 cp_lexer_consume_token (parser->lexer);
7948 /* Otherwise, no valid operator name was present. */
7951 cp_parser_error (parser, "expected operator");
7952 id = error_mark_node;
7958 /* Parse a template-declaration.
7960 template-declaration:
7961 export [opt] template < template-parameter-list > declaration
7963 If MEMBER_P is TRUE, this template-declaration occurs within a
7966 The grammar rule given by the standard isn't correct. What
7969 template-declaration:
7970 export [opt] template-parameter-list-seq
7971 decl-specifier-seq [opt] init-declarator [opt] ;
7972 export [opt] template-parameter-list-seq
7975 template-parameter-list-seq:
7976 template-parameter-list-seq [opt]
7977 template < template-parameter-list > */
7980 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7982 /* Check for `export'. */
7983 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7985 /* Consume the `export' token. */
7986 cp_lexer_consume_token (parser->lexer);
7987 /* Warn that we do not support `export'. */
7988 warning ("keyword %<export%> not implemented, and will be ignored");
7991 cp_parser_template_declaration_after_export (parser, member_p);
7994 /* Parse a template-parameter-list.
7996 template-parameter-list:
7998 template-parameter-list , template-parameter
8000 Returns a TREE_LIST. Each node represents a template parameter.
8001 The nodes are connected via their TREE_CHAINs. */
8004 cp_parser_template_parameter_list (cp_parser* parser)
8006 tree parameter_list = NULL_TREE;
8014 /* Parse the template-parameter. */
8015 parameter = cp_parser_template_parameter (parser, &is_non_type);
8016 /* Add it to the list. */
8017 parameter_list = process_template_parm (parameter_list,
8020 /* Peek at the next token. */
8021 token = cp_lexer_peek_token (parser->lexer);
8022 /* If it's not a `,', we're done. */
8023 if (token->type != CPP_COMMA)
8025 /* Otherwise, consume the `,' token. */
8026 cp_lexer_consume_token (parser->lexer);
8029 return parameter_list;
8032 /* Parse a template-parameter.
8036 parameter-declaration
8038 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8039 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8040 true iff this parameter is a non-type parameter. */
8043 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8046 cp_parameter_declarator *parameter_declarator;
8048 /* Assume it is a type parameter or a template parameter. */
8049 *is_non_type = false;
8050 /* Peek at the next token. */
8051 token = cp_lexer_peek_token (parser->lexer);
8052 /* If it is `class' or `template', we have a type-parameter. */
8053 if (token->keyword == RID_TEMPLATE)
8054 return cp_parser_type_parameter (parser);
8055 /* If it is `class' or `typename' we do not know yet whether it is a
8056 type parameter or a non-type parameter. Consider:
8058 template <typename T, typename T::X X> ...
8062 template <class C, class D*> ...
8064 Here, the first parameter is a type parameter, and the second is
8065 a non-type parameter. We can tell by looking at the token after
8066 the identifier -- if it is a `,', `=', or `>' then we have a type
8068 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8070 /* Peek at the token after `class' or `typename'. */
8071 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8072 /* If it's an identifier, skip it. */
8073 if (token->type == CPP_NAME)
8074 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8075 /* Now, see if the token looks like the end of a template
8077 if (token->type == CPP_COMMA
8078 || token->type == CPP_EQ
8079 || token->type == CPP_GREATER)
8080 return cp_parser_type_parameter (parser);
8083 /* Otherwise, it is a non-type parameter.
8087 When parsing a default template-argument for a non-type
8088 template-parameter, the first non-nested `>' is taken as the end
8089 of the template parameter-list rather than a greater-than
8091 *is_non_type = true;
8092 parameter_declarator
8093 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8094 /*parenthesized_p=*/NULL);
8095 return (build_tree_list
8096 (parameter_declarator->default_argument,
8097 grokdeclarator (parameter_declarator->declarator,
8098 ¶meter_declarator->decl_specifiers,
8099 PARM, /*initialized=*/0,
8100 /*attrlist=*/NULL)));
8103 /* Parse a type-parameter.
8106 class identifier [opt]
8107 class identifier [opt] = type-id
8108 typename identifier [opt]
8109 typename identifier [opt] = type-id
8110 template < template-parameter-list > class identifier [opt]
8111 template < template-parameter-list > class identifier [opt]
8114 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8115 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8116 the declaration of the parameter. */
8119 cp_parser_type_parameter (cp_parser* parser)
8124 /* Look for a keyword to tell us what kind of parameter this is. */
8125 token = cp_parser_require (parser, CPP_KEYWORD,
8126 "`class', `typename', or `template'");
8128 return error_mark_node;
8130 switch (token->keyword)
8136 tree default_argument;
8138 /* If the next token is an identifier, then it names the
8140 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8141 identifier = cp_parser_identifier (parser);
8143 identifier = NULL_TREE;
8145 /* Create the parameter. */
8146 parameter = finish_template_type_parm (class_type_node, identifier);
8148 /* If the next token is an `=', we have a default argument. */
8149 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8151 /* Consume the `=' token. */
8152 cp_lexer_consume_token (parser->lexer);
8153 /* Parse the default-argument. */
8154 default_argument = cp_parser_type_id (parser);
8157 default_argument = NULL_TREE;
8159 /* Create the combined representation of the parameter and the
8160 default argument. */
8161 parameter = build_tree_list (default_argument, parameter);
8167 tree parameter_list;
8169 tree default_argument;
8171 /* Look for the `<'. */
8172 cp_parser_require (parser, CPP_LESS, "`<'");
8173 /* Parse the template-parameter-list. */
8174 begin_template_parm_list ();
8176 = cp_parser_template_parameter_list (parser);
8177 parameter_list = end_template_parm_list (parameter_list);
8178 /* Look for the `>'. */
8179 cp_parser_require (parser, CPP_GREATER, "`>'");
8180 /* Look for the `class' keyword. */
8181 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8182 /* If the next token is an `=', then there is a
8183 default-argument. If the next token is a `>', we are at
8184 the end of the parameter-list. If the next token is a `,',
8185 then we are at the end of this parameter. */
8186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8187 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8188 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8189 identifier = cp_parser_identifier (parser);
8191 identifier = NULL_TREE;
8192 /* Create the template parameter. */
8193 parameter = finish_template_template_parm (class_type_node,
8196 /* If the next token is an `=', then there is a
8197 default-argument. */
8198 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8202 /* Consume the `='. */
8203 cp_lexer_consume_token (parser->lexer);
8204 /* Parse the id-expression. */
8206 = cp_parser_id_expression (parser,
8207 /*template_keyword_p=*/false,
8208 /*check_dependency_p=*/true,
8209 /*template_p=*/&is_template,
8210 /*declarator_p=*/false);
8211 if (TREE_CODE (default_argument) == TYPE_DECL)
8212 /* If the id-expression was a template-id that refers to
8213 a template-class, we already have the declaration here,
8214 so no further lookup is needed. */
8217 /* Look up the name. */
8219 = cp_parser_lookup_name (parser, default_argument,
8221 /*is_template=*/is_template,
8222 /*is_namespace=*/false,
8223 /*check_dependency=*/true,
8224 /*ambiguous_p=*/NULL);
8225 /* See if the default argument is valid. */
8227 = check_template_template_default_arg (default_argument);
8230 default_argument = NULL_TREE;
8232 /* Create the combined representation of the parameter and the
8233 default argument. */
8234 parameter = build_tree_list (default_argument, parameter);
8239 /* Anything else is an error. */
8240 cp_parser_error (parser,
8241 "expected %<class%>, %<typename%>, or %<template%>");
8242 parameter = error_mark_node;
8248 /* Parse a template-id.
8251 template-name < template-argument-list [opt] >
8253 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8254 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8255 returned. Otherwise, if the template-name names a function, or set
8256 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8257 names a class, returns a TYPE_DECL for the specialization.
8259 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8260 uninstantiated templates. */
8263 cp_parser_template_id (cp_parser *parser,
8264 bool template_keyword_p,
8265 bool check_dependency_p,
8266 bool is_declaration)
8271 cp_token_position start_of_id = 0;
8272 tree access_check = NULL_TREE;
8273 cp_token *next_token, *next_token_2;
8276 /* If the next token corresponds to a template-id, there is no need
8278 next_token = cp_lexer_peek_token (parser->lexer);
8279 if (next_token->type == CPP_TEMPLATE_ID)
8284 /* Get the stored value. */
8285 value = cp_lexer_consume_token (parser->lexer)->value;
8286 /* Perform any access checks that were deferred. */
8287 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8288 perform_or_defer_access_check (TREE_PURPOSE (check),
8289 TREE_VALUE (check));
8290 /* Return the stored value. */
8291 return TREE_VALUE (value);
8294 /* Avoid performing name lookup if there is no possibility of
8295 finding a template-id. */
8296 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8297 || (next_token->type == CPP_NAME
8298 && !cp_parser_nth_token_starts_template_argument_list_p
8301 cp_parser_error (parser, "expected template-id");
8302 return error_mark_node;
8305 /* Remember where the template-id starts. */
8306 if (cp_parser_parsing_tentatively (parser)
8307 && !cp_parser_committed_to_tentative_parse (parser))
8308 start_of_id = cp_lexer_token_position (parser->lexer, false);
8310 push_deferring_access_checks (dk_deferred);
8312 /* Parse the template-name. */
8313 is_identifier = false;
8314 template = cp_parser_template_name (parser, template_keyword_p,
8318 if (template == error_mark_node || is_identifier)
8320 pop_deferring_access_checks ();
8324 /* If we find the sequence `[:' after a template-name, it's probably
8325 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8326 parse correctly the argument list. */
8327 next_token = cp_lexer_peek_token (parser->lexer);
8328 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8329 if (next_token->type == CPP_OPEN_SQUARE
8330 && next_token->flags & DIGRAPH
8331 && next_token_2->type == CPP_COLON
8332 && !(next_token_2->flags & PREV_WHITE))
8334 cp_parser_parse_tentatively (parser);
8335 /* Change `:' into `::'. */
8336 next_token_2->type = CPP_SCOPE;
8337 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8339 cp_lexer_consume_token (parser->lexer);
8340 /* Parse the arguments. */
8341 arguments = cp_parser_enclosed_template_argument_list (parser);
8342 if (!cp_parser_parse_definitely (parser))
8344 /* If we couldn't parse an argument list, then we revert our changes
8345 and return simply an error. Maybe this is not a template-id
8347 next_token_2->type = CPP_COLON;
8348 cp_parser_error (parser, "expected %<<%>");
8349 pop_deferring_access_checks ();
8350 return error_mark_node;
8352 /* Otherwise, emit an error about the invalid digraph, but continue
8353 parsing because we got our argument list. */
8354 pedwarn ("%<<::%> cannot begin a template-argument list");
8355 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8356 "between %<<%> and %<::%>");
8357 if (!flag_permissive)
8362 inform ("(if you use -fpermissive G++ will accept your code)");
8369 /* Look for the `<' that starts the template-argument-list. */
8370 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8372 pop_deferring_access_checks ();
8373 return error_mark_node;
8375 /* Parse the arguments. */
8376 arguments = cp_parser_enclosed_template_argument_list (parser);
8379 /* Build a representation of the specialization. */
8380 if (TREE_CODE (template) == IDENTIFIER_NODE)
8381 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8382 else if (DECL_CLASS_TEMPLATE_P (template)
8383 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8385 = finish_template_type (template, arguments,
8386 cp_lexer_next_token_is (parser->lexer,
8390 /* If it's not a class-template or a template-template, it should be
8391 a function-template. */
8392 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8393 || TREE_CODE (template) == OVERLOAD
8394 || BASELINK_P (template)));
8396 template_id = lookup_template_function (template, arguments);
8399 /* Retrieve any deferred checks. Do not pop this access checks yet
8400 so the memory will not be reclaimed during token replacing below. */
8401 access_check = get_deferred_access_checks ();
8403 /* If parsing tentatively, replace the sequence of tokens that makes
8404 up the template-id with a CPP_TEMPLATE_ID token. That way,
8405 should we re-parse the token stream, we will not have to repeat
8406 the effort required to do the parse, nor will we issue duplicate
8407 error messages about problems during instantiation of the
8411 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8413 /* Reset the contents of the START_OF_ID token. */
8414 token->type = CPP_TEMPLATE_ID;
8415 token->value = build_tree_list (access_check, template_id);
8416 token->keyword = RID_MAX;
8418 /* Purge all subsequent tokens. */
8419 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8422 pop_deferring_access_checks ();
8426 /* Parse a template-name.
8431 The standard should actually say:
8435 operator-function-id
8437 A defect report has been filed about this issue.
8439 A conversion-function-id cannot be a template name because they cannot
8440 be part of a template-id. In fact, looking at this code:
8444 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8445 It is impossible to call a templated conversion-function-id with an
8446 explicit argument list, since the only allowed template parameter is
8447 the type to which it is converting.
8449 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8450 `template' keyword, in a construction like:
8454 In that case `f' is taken to be a template-name, even though there
8455 is no way of knowing for sure.
8457 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8458 name refers to a set of overloaded functions, at least one of which
8459 is a template, or an IDENTIFIER_NODE with the name of the template,
8460 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8461 names are looked up inside uninstantiated templates. */
8464 cp_parser_template_name (cp_parser* parser,
8465 bool template_keyword_p,
8466 bool check_dependency_p,
8467 bool is_declaration,
8468 bool *is_identifier)
8474 /* If the next token is `operator', then we have either an
8475 operator-function-id or a conversion-function-id. */
8476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8478 /* We don't know whether we're looking at an
8479 operator-function-id or a conversion-function-id. */
8480 cp_parser_parse_tentatively (parser);
8481 /* Try an operator-function-id. */
8482 identifier = cp_parser_operator_function_id (parser);
8483 /* If that didn't work, try a conversion-function-id. */
8484 if (!cp_parser_parse_definitely (parser))
8486 cp_parser_error (parser, "expected template-name");
8487 return error_mark_node;
8490 /* Look for the identifier. */
8492 identifier = cp_parser_identifier (parser);
8494 /* If we didn't find an identifier, we don't have a template-id. */
8495 if (identifier == error_mark_node)
8496 return error_mark_node;
8498 /* If the name immediately followed the `template' keyword, then it
8499 is a template-name. However, if the next token is not `<', then
8500 we do not treat it as a template-name, since it is not being used
8501 as part of a template-id. This enables us to handle constructs
8504 template <typename T> struct S { S(); };
8505 template <typename T> S<T>::S();
8507 correctly. We would treat `S' as a template -- if it were `S<T>'
8508 -- but we do not if there is no `<'. */
8510 if (processing_template_decl
8511 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8513 /* In a declaration, in a dependent context, we pretend that the
8514 "template" keyword was present in order to improve error
8515 recovery. For example, given:
8517 template <typename T> void f(T::X<int>);
8519 we want to treat "X<int>" as a template-id. */
8521 && !template_keyword_p
8522 && parser->scope && TYPE_P (parser->scope)
8523 && check_dependency_p
8524 && dependent_type_p (parser->scope)
8525 /* Do not do this for dtors (or ctors), since they never
8526 need the template keyword before their name. */
8527 && !constructor_name_p (identifier, parser->scope))
8529 cp_token_position start = 0;
8531 /* Explain what went wrong. */
8532 error ("non-template %qD used as template", identifier);
8533 inform ("use %<%T::template %D%> to indicate that it is a template",
8534 parser->scope, identifier);
8535 /* If parsing tentatively, find the location of the "<"
8537 if (cp_parser_parsing_tentatively (parser)
8538 && !cp_parser_committed_to_tentative_parse (parser))
8540 cp_parser_simulate_error (parser);
8541 start = cp_lexer_token_position (parser->lexer, true);
8543 /* Parse the template arguments so that we can issue error
8544 messages about them. */
8545 cp_lexer_consume_token (parser->lexer);
8546 cp_parser_enclosed_template_argument_list (parser);
8547 /* Skip tokens until we find a good place from which to
8548 continue parsing. */
8549 cp_parser_skip_to_closing_parenthesis (parser,
8550 /*recovering=*/true,
8552 /*consume_paren=*/false);
8553 /* If parsing tentatively, permanently remove the
8554 template argument list. That will prevent duplicate
8555 error messages from being issued about the missing
8556 "template" keyword. */
8558 cp_lexer_purge_tokens_after (parser->lexer, start);
8560 *is_identifier = true;
8564 /* If the "template" keyword is present, then there is generally
8565 no point in doing name-lookup, so we just return IDENTIFIER.
8566 But, if the qualifying scope is non-dependent then we can
8567 (and must) do name-lookup normally. */
8568 if (template_keyword_p
8570 || (TYPE_P (parser->scope)
8571 && dependent_type_p (parser->scope))))
8575 /* Look up the name. */
8576 decl = cp_parser_lookup_name (parser, identifier,
8578 /*is_template=*/false,
8579 /*is_namespace=*/false,
8581 /*ambiguous_p=*/NULL);
8582 decl = maybe_get_template_decl_from_type_decl (decl);
8584 /* If DECL is a template, then the name was a template-name. */
8585 if (TREE_CODE (decl) == TEMPLATE_DECL)
8589 /* The standard does not explicitly indicate whether a name that
8590 names a set of overloaded declarations, some of which are
8591 templates, is a template-name. However, such a name should
8592 be a template-name; otherwise, there is no way to form a
8593 template-id for the overloaded templates. */
8594 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8595 if (TREE_CODE (fns) == OVERLOAD)
8599 for (fn = fns; fn; fn = OVL_NEXT (fn))
8600 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8605 /* Otherwise, the name does not name a template. */
8606 cp_parser_error (parser, "expected template-name");
8607 return error_mark_node;
8611 /* If DECL is dependent, and refers to a function, then just return
8612 its name; we will look it up again during template instantiation. */
8613 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8615 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8616 if (TYPE_P (scope) && dependent_type_p (scope))
8623 /* Parse a template-argument-list.
8625 template-argument-list:
8627 template-argument-list , template-argument
8629 Returns a TREE_VEC containing the arguments. */
8632 cp_parser_template_argument_list (cp_parser* parser)
8634 tree fixed_args[10];
8635 unsigned n_args = 0;
8636 unsigned alloced = 10;
8637 tree *arg_ary = fixed_args;
8639 bool saved_in_template_argument_list_p;
8641 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8642 parser->in_template_argument_list_p = true;
8648 /* Consume the comma. */
8649 cp_lexer_consume_token (parser->lexer);
8651 /* Parse the template-argument. */
8652 argument = cp_parser_template_argument (parser);
8653 if (n_args == alloced)
8657 if (arg_ary == fixed_args)
8659 arg_ary = xmalloc (sizeof (tree) * alloced);
8660 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8663 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8665 arg_ary[n_args++] = argument;
8667 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8669 vec = make_tree_vec (n_args);
8672 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8674 if (arg_ary != fixed_args)
8676 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8680 /* Parse a template-argument.
8683 assignment-expression
8687 The representation is that of an assignment-expression, type-id, or
8688 id-expression -- except that the qualified id-expression is
8689 evaluated, so that the value returned is either a DECL or an
8692 Although the standard says "assignment-expression", it forbids
8693 throw-expressions or assignments in the template argument.
8694 Therefore, we use "conditional-expression" instead. */
8697 cp_parser_template_argument (cp_parser* parser)
8702 bool maybe_type_id = false;
8705 tree qualifying_class;
8707 /* There's really no way to know what we're looking at, so we just
8708 try each alternative in order.
8712 In a template-argument, an ambiguity between a type-id and an
8713 expression is resolved to a type-id, regardless of the form of
8714 the corresponding template-parameter.
8716 Therefore, we try a type-id first. */
8717 cp_parser_parse_tentatively (parser);
8718 argument = cp_parser_type_id (parser);
8719 /* If there was no error parsing the type-id but the next token is a '>>',
8720 we probably found a typo for '> >'. But there are type-id which are
8721 also valid expressions. For instance:
8723 struct X { int operator >> (int); };
8724 template <int V> struct Foo {};
8727 Here 'X()' is a valid type-id of a function type, but the user just
8728 wanted to write the expression "X() >> 5". Thus, we remember that we
8729 found a valid type-id, but we still try to parse the argument as an
8730 expression to see what happens. */
8731 if (!cp_parser_error_occurred (parser)
8732 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8734 maybe_type_id = true;
8735 cp_parser_abort_tentative_parse (parser);
8739 /* If the next token isn't a `,' or a `>', then this argument wasn't
8740 really finished. This means that the argument is not a valid
8742 if (!cp_parser_next_token_ends_template_argument_p (parser))
8743 cp_parser_error (parser, "expected template-argument");
8744 /* If that worked, we're done. */
8745 if (cp_parser_parse_definitely (parser))
8748 /* We're still not sure what the argument will be. */
8749 cp_parser_parse_tentatively (parser);
8750 /* Try a template. */
8751 argument = cp_parser_id_expression (parser,
8752 /*template_keyword_p=*/false,
8753 /*check_dependency_p=*/true,
8755 /*declarator_p=*/false);
8756 /* If the next token isn't a `,' or a `>', then this argument wasn't
8758 if (!cp_parser_next_token_ends_template_argument_p (parser))
8759 cp_parser_error (parser, "expected template-argument");
8760 if (!cp_parser_error_occurred (parser))
8762 /* Figure out what is being referred to. If the id-expression
8763 was for a class template specialization, then we will have a
8764 TYPE_DECL at this point. There is no need to do name lookup
8765 at this point in that case. */
8766 if (TREE_CODE (argument) != TYPE_DECL)
8767 argument = cp_parser_lookup_name (parser, argument,
8769 /*is_template=*/template_p,
8770 /*is_namespace=*/false,
8771 /*check_dependency=*/true,
8772 /*ambiguous_p=*/NULL);
8773 if (TREE_CODE (argument) != TEMPLATE_DECL
8774 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8775 cp_parser_error (parser, "expected template-name");
8777 if (cp_parser_parse_definitely (parser))
8779 /* It must be a non-type argument. There permitted cases are given
8780 in [temp.arg.nontype]:
8782 -- an integral constant-expression of integral or enumeration
8785 -- the name of a non-type template-parameter; or
8787 -- the name of an object or function with external linkage...
8789 -- the address of an object or function with external linkage...
8791 -- a pointer to member... */
8792 /* Look for a non-type template parameter. */
8793 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8795 cp_parser_parse_tentatively (parser);
8796 argument = cp_parser_primary_expression (parser,
8799 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8800 || !cp_parser_next_token_ends_template_argument_p (parser))
8801 cp_parser_simulate_error (parser);
8802 if (cp_parser_parse_definitely (parser))
8805 /* If the next token is "&", the argument must be the address of an
8806 object or function with external linkage. */
8807 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8809 cp_lexer_consume_token (parser->lexer);
8810 /* See if we might have an id-expression. */
8811 token = cp_lexer_peek_token (parser->lexer);
8812 if (token->type == CPP_NAME
8813 || token->keyword == RID_OPERATOR
8814 || token->type == CPP_SCOPE
8815 || token->type == CPP_TEMPLATE_ID
8816 || token->type == CPP_NESTED_NAME_SPECIFIER)
8818 cp_parser_parse_tentatively (parser);
8819 argument = cp_parser_primary_expression (parser,
8822 if (cp_parser_error_occurred (parser)
8823 || !cp_parser_next_token_ends_template_argument_p (parser))
8824 cp_parser_abort_tentative_parse (parser);
8827 if (qualifying_class)
8828 argument = finish_qualified_id_expr (qualifying_class,
8832 if (TREE_CODE (argument) == VAR_DECL)
8834 /* A variable without external linkage might still be a
8835 valid constant-expression, so no error is issued here
8836 if the external-linkage check fails. */
8837 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8838 cp_parser_simulate_error (parser);
8840 else if (is_overloaded_fn (argument))
8841 /* All overloaded functions are allowed; if the external
8842 linkage test does not pass, an error will be issued
8846 && (TREE_CODE (argument) == OFFSET_REF
8847 || TREE_CODE (argument) == SCOPE_REF))
8848 /* A pointer-to-member. */
8851 cp_parser_simulate_error (parser);
8853 if (cp_parser_parse_definitely (parser))
8856 argument = build_x_unary_op (ADDR_EXPR, argument);
8861 /* If the argument started with "&", there are no other valid
8862 alternatives at this point. */
8865 cp_parser_error (parser, "invalid non-type template argument");
8866 return error_mark_node;
8868 /* If the argument wasn't successfully parsed as a type-id followed
8869 by '>>', the argument can only be a constant expression now.
8870 Otherwise, we try parsing the constant-expression tentatively,
8871 because the argument could really be a type-id. */
8873 cp_parser_parse_tentatively (parser);
8874 argument = cp_parser_constant_expression (parser,
8875 /*allow_non_constant_p=*/false,
8876 /*non_constant_p=*/NULL);
8877 argument = fold_non_dependent_expr (argument);
8880 if (!cp_parser_next_token_ends_template_argument_p (parser))
8881 cp_parser_error (parser, "expected template-argument");
8882 if (cp_parser_parse_definitely (parser))
8884 /* We did our best to parse the argument as a non type-id, but that
8885 was the only alternative that matched (albeit with a '>' after
8886 it). We can assume it's just a typo from the user, and a
8887 diagnostic will then be issued. */
8888 return cp_parser_type_id (parser);
8891 /* Parse an explicit-instantiation.
8893 explicit-instantiation:
8894 template declaration
8896 Although the standard says `declaration', what it really means is:
8898 explicit-instantiation:
8899 template decl-specifier-seq [opt] declarator [opt] ;
8901 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8902 supposed to be allowed. A defect report has been filed about this
8907 explicit-instantiation:
8908 storage-class-specifier template
8909 decl-specifier-seq [opt] declarator [opt] ;
8910 function-specifier template
8911 decl-specifier-seq [opt] declarator [opt] ; */
8914 cp_parser_explicit_instantiation (cp_parser* parser)
8916 int declares_class_or_enum;
8917 cp_decl_specifier_seq decl_specifiers;
8918 tree extension_specifier = NULL_TREE;
8920 /* Look for an (optional) storage-class-specifier or
8921 function-specifier. */
8922 if (cp_parser_allow_gnu_extensions_p (parser))
8925 = cp_parser_storage_class_specifier_opt (parser);
8926 if (!extension_specifier)
8928 = cp_parser_function_specifier_opt (parser,
8929 /*decl_specs=*/NULL);
8932 /* Look for the `template' keyword. */
8933 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8934 /* Let the front end know that we are processing an explicit
8936 begin_explicit_instantiation ();
8937 /* [temp.explicit] says that we are supposed to ignore access
8938 control while processing explicit instantiation directives. */
8939 push_deferring_access_checks (dk_no_check);
8940 /* Parse a decl-specifier-seq. */
8941 cp_parser_decl_specifier_seq (parser,
8942 CP_PARSER_FLAGS_OPTIONAL,
8944 &declares_class_or_enum);
8945 /* If there was exactly one decl-specifier, and it declared a class,
8946 and there's no declarator, then we have an explicit type
8948 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8952 type = check_tag_decl (&decl_specifiers);
8953 /* Turn access control back on for names used during
8954 template instantiation. */
8955 pop_deferring_access_checks ();
8957 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8961 cp_declarator *declarator;
8964 /* Parse the declarator. */
8966 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8967 /*ctor_dtor_or_conv_p=*/NULL,
8968 /*parenthesized_p=*/NULL,
8969 /*member_p=*/false);
8970 cp_parser_check_for_definition_in_return_type (declarator,
8971 declares_class_or_enum);
8972 if (declarator != cp_error_declarator)
8974 decl = grokdeclarator (declarator, &decl_specifiers,
8976 /* Turn access control back on for names used during
8977 template instantiation. */
8978 pop_deferring_access_checks ();
8979 /* Do the explicit instantiation. */
8980 do_decl_instantiation (decl, extension_specifier);
8984 pop_deferring_access_checks ();
8985 /* Skip the body of the explicit instantiation. */
8986 cp_parser_skip_to_end_of_statement (parser);
8989 /* We're done with the instantiation. */
8990 end_explicit_instantiation ();
8992 cp_parser_consume_semicolon_at_end_of_statement (parser);
8995 /* Parse an explicit-specialization.
8997 explicit-specialization:
8998 template < > declaration
9000 Although the standard says `declaration', what it really means is:
9002 explicit-specialization:
9003 template <> decl-specifier [opt] init-declarator [opt] ;
9004 template <> function-definition
9005 template <> explicit-specialization
9006 template <> template-declaration */
9009 cp_parser_explicit_specialization (cp_parser* parser)
9011 /* Look for the `template' keyword. */
9012 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9013 /* Look for the `<'. */
9014 cp_parser_require (parser, CPP_LESS, "`<'");
9015 /* Look for the `>'. */
9016 cp_parser_require (parser, CPP_GREATER, "`>'");
9017 /* We have processed another parameter list. */
9018 ++parser->num_template_parameter_lists;
9019 /* Let the front end know that we are beginning a specialization. */
9020 begin_specialization ();
9022 /* If the next keyword is `template', we need to figure out whether
9023 or not we're looking a template-declaration. */
9024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9026 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9027 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9028 cp_parser_template_declaration_after_export (parser,
9029 /*member_p=*/false);
9031 cp_parser_explicit_specialization (parser);
9034 /* Parse the dependent declaration. */
9035 cp_parser_single_declaration (parser,
9039 /* We're done with the specialization. */
9040 end_specialization ();
9041 /* We're done with this parameter list. */
9042 --parser->num_template_parameter_lists;
9045 /* Parse a type-specifier.
9048 simple-type-specifier
9051 elaborated-type-specifier
9059 Returns a representation of the type-specifier. For a
9060 class-specifier, enum-specifier, or elaborated-type-specifier, a
9061 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9063 The parser flags FLAGS is used to control type-specifier parsing.
9065 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9066 in a decl-specifier-seq.
9068 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9069 class-specifier, enum-specifier, or elaborated-type-specifier, then
9070 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9071 if a type is declared; 2 if it is defined. Otherwise, it is set to
9074 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9075 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9079 cp_parser_type_specifier (cp_parser* parser,
9080 cp_parser_flags flags,
9081 cp_decl_specifier_seq *decl_specs,
9082 bool is_declaration,
9083 int* declares_class_or_enum,
9084 bool* is_cv_qualifier)
9086 tree type_spec = NULL_TREE;
9089 cp_decl_spec ds = ds_last;
9091 /* Assume this type-specifier does not declare a new type. */
9092 if (declares_class_or_enum)
9093 *declares_class_or_enum = 0;
9094 /* And that it does not specify a cv-qualifier. */
9095 if (is_cv_qualifier)
9096 *is_cv_qualifier = false;
9097 /* Peek at the next token. */
9098 token = cp_lexer_peek_token (parser->lexer);
9100 /* If we're looking at a keyword, we can use that to guide the
9101 production we choose. */
9102 keyword = token->keyword;
9106 /* 'enum' [identifier] '{' introduces an enum-specifier;
9107 'enum' <anything else> introduces an elaborated-type-specifier. */
9108 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9109 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9110 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9113 type_spec = cp_parser_enum_specifier (parser);
9114 if (declares_class_or_enum)
9115 *declares_class_or_enum = 2;
9117 cp_parser_set_decl_spec_type (decl_specs,
9119 /*user_defined_p=*/true);
9123 goto elaborated_type_specifier;
9125 /* Any of these indicate either a class-specifier, or an
9126 elaborated-type-specifier. */
9130 /* Parse tentatively so that we can back up if we don't find a
9132 cp_parser_parse_tentatively (parser);
9133 /* Look for the class-specifier. */
9134 type_spec = cp_parser_class_specifier (parser);
9135 /* If that worked, we're done. */
9136 if (cp_parser_parse_definitely (parser))
9138 if (declares_class_or_enum)
9139 *declares_class_or_enum = 2;
9141 cp_parser_set_decl_spec_type (decl_specs,
9143 /*user_defined_p=*/true);
9148 elaborated_type_specifier:
9149 /* We're declaring (not defining) a class or enum. */
9150 if (declares_class_or_enum)
9151 *declares_class_or_enum = 1;
9155 /* Look for an elaborated-type-specifier. */
9157 = (cp_parser_elaborated_type_specifier
9159 decl_specs && decl_specs->specs[(int) ds_friend],
9162 cp_parser_set_decl_spec_type (decl_specs,
9164 /*user_defined_p=*/true);
9169 if (is_cv_qualifier)
9170 *is_cv_qualifier = true;
9175 if (is_cv_qualifier)
9176 *is_cv_qualifier = true;
9181 if (is_cv_qualifier)
9182 *is_cv_qualifier = true;
9186 /* The `__complex__' keyword is a GNU extension. */
9194 /* Handle simple keywords. */
9199 ++decl_specs->specs[(int)ds];
9200 decl_specs->any_specifiers_p = true;
9202 return cp_lexer_consume_token (parser->lexer)->value;
9205 /* If we do not already have a type-specifier, assume we are looking
9206 at a simple-type-specifier. */
9207 type_spec = cp_parser_simple_type_specifier (parser,
9211 /* If we didn't find a type-specifier, and a type-specifier was not
9212 optional in this context, issue an error message. */
9213 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9215 cp_parser_error (parser, "expected type specifier");
9216 return error_mark_node;
9222 /* Parse a simple-type-specifier.
9224 simple-type-specifier:
9225 :: [opt] nested-name-specifier [opt] type-name
9226 :: [opt] nested-name-specifier template template-id
9241 simple-type-specifier:
9242 __typeof__ unary-expression
9243 __typeof__ ( type-id )
9245 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9246 appropriately updated. */
9249 cp_parser_simple_type_specifier (cp_parser* parser,
9250 cp_decl_specifier_seq *decl_specs,
9251 cp_parser_flags flags)
9253 tree type = NULL_TREE;
9256 /* Peek at the next token. */
9257 token = cp_lexer_peek_token (parser->lexer);
9259 /* If we're looking at a keyword, things are easy. */
9260 switch (token->keyword)
9264 decl_specs->explicit_char_p = true;
9265 type = char_type_node;
9268 type = wchar_type_node;
9271 type = boolean_type_node;
9275 ++decl_specs->specs[(int) ds_short];
9276 type = short_integer_type_node;
9280 decl_specs->explicit_int_p = true;
9281 type = integer_type_node;
9285 ++decl_specs->specs[(int) ds_long];
9286 type = long_integer_type_node;
9290 ++decl_specs->specs[(int) ds_signed];
9291 type = integer_type_node;
9295 ++decl_specs->specs[(int) ds_unsigned];
9296 type = unsigned_type_node;
9299 type = float_type_node;
9302 type = double_type_node;
9305 type = void_type_node;
9309 /* Consume the `typeof' token. */
9310 cp_lexer_consume_token (parser->lexer);
9311 /* Parse the operand to `typeof'. */
9312 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9313 /* If it is not already a TYPE, take its type. */
9315 type = finish_typeof (type);
9318 cp_parser_set_decl_spec_type (decl_specs, type,
9319 /*user_defined_p=*/true);
9327 /* If the type-specifier was for a built-in type, we're done. */
9332 /* Record the type. */
9334 && (token->keyword != RID_SIGNED
9335 && token->keyword != RID_UNSIGNED
9336 && token->keyword != RID_SHORT
9337 && token->keyword != RID_LONG))
9338 cp_parser_set_decl_spec_type (decl_specs,
9340 /*user_defined=*/false);
9342 decl_specs->any_specifiers_p = true;
9344 /* Consume the token. */
9345 id = cp_lexer_consume_token (parser->lexer)->value;
9347 /* There is no valid C++ program where a non-template type is
9348 followed by a "<". That usually indicates that the user thought
9349 that the type was a template. */
9350 cp_parser_check_for_invalid_template_id (parser, type);
9352 return TYPE_NAME (type);
9355 /* The type-specifier must be a user-defined type. */
9356 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9361 /* Don't gobble tokens or issue error messages if this is an
9362 optional type-specifier. */
9363 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9364 cp_parser_parse_tentatively (parser);
9366 /* Look for the optional `::' operator. */
9368 = (cp_parser_global_scope_opt (parser,
9369 /*current_scope_valid_p=*/false)
9371 /* Look for the nested-name specifier. */
9373 = (cp_parser_nested_name_specifier_opt (parser,
9374 /*typename_keyword_p=*/false,
9375 /*check_dependency_p=*/true,
9377 /*is_declaration=*/false)
9379 /* If we have seen a nested-name-specifier, and the next token
9380 is `template', then we are using the template-id production. */
9382 && cp_parser_optional_template_keyword (parser))
9384 /* Look for the template-id. */
9385 type = cp_parser_template_id (parser,
9386 /*template_keyword_p=*/true,
9387 /*check_dependency_p=*/true,
9388 /*is_declaration=*/false);
9389 /* If the template-id did not name a type, we are out of
9391 if (TREE_CODE (type) != TYPE_DECL)
9393 cp_parser_error (parser, "expected template-id for type");
9397 /* Otherwise, look for a type-name. */
9399 type = cp_parser_type_name (parser);
9400 /* Keep track of all name-lookups performed in class scopes. */
9404 && TREE_CODE (type) == TYPE_DECL
9405 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9406 maybe_note_name_used_in_class (DECL_NAME (type), type);
9407 /* If it didn't work out, we don't have a TYPE. */
9408 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9409 && !cp_parser_parse_definitely (parser))
9411 if (type && decl_specs)
9412 cp_parser_set_decl_spec_type (decl_specs, type,
9413 /*user_defined=*/true);
9416 /* If we didn't get a type-name, issue an error message. */
9417 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9419 cp_parser_error (parser, "expected type-name");
9420 return error_mark_node;
9423 /* There is no valid C++ program where a non-template type is
9424 followed by a "<". That usually indicates that the user thought
9425 that the type was a template. */
9426 if (type && type != error_mark_node)
9427 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9432 /* Parse a type-name.
9445 Returns a TYPE_DECL for the the type. */
9448 cp_parser_type_name (cp_parser* parser)
9453 /* We can't know yet whether it is a class-name or not. */
9454 cp_parser_parse_tentatively (parser);
9455 /* Try a class-name. */
9456 type_decl = cp_parser_class_name (parser,
9457 /*typename_keyword_p=*/false,
9458 /*template_keyword_p=*/false,
9460 /*check_dependency_p=*/true,
9461 /*class_head_p=*/false,
9462 /*is_declaration=*/false);
9463 /* If it's not a class-name, keep looking. */
9464 if (!cp_parser_parse_definitely (parser))
9466 /* It must be a typedef-name or an enum-name. */
9467 identifier = cp_parser_identifier (parser);
9468 if (identifier == error_mark_node)
9469 return error_mark_node;
9471 /* Look up the type-name. */
9472 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9473 /* Issue an error if we did not find a type-name. */
9474 if (TREE_CODE (type_decl) != TYPE_DECL)
9476 if (!cp_parser_simulate_error (parser))
9477 cp_parser_name_lookup_error (parser, identifier, type_decl,
9479 type_decl = error_mark_node;
9481 /* Remember that the name was used in the definition of the
9482 current class so that we can check later to see if the
9483 meaning would have been different after the class was
9484 entirely defined. */
9485 else if (type_decl != error_mark_node
9487 maybe_note_name_used_in_class (identifier, type_decl);
9494 /* Parse an elaborated-type-specifier. Note that the grammar given
9495 here incorporates the resolution to DR68.
9497 elaborated-type-specifier:
9498 class-key :: [opt] nested-name-specifier [opt] identifier
9499 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9500 enum :: [opt] nested-name-specifier [opt] identifier
9501 typename :: [opt] nested-name-specifier identifier
9502 typename :: [opt] nested-name-specifier template [opt]
9507 elaborated-type-specifier:
9508 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9509 class-key attributes :: [opt] nested-name-specifier [opt]
9510 template [opt] template-id
9511 enum attributes :: [opt] nested-name-specifier [opt] identifier
9513 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9514 declared `friend'. If IS_DECLARATION is TRUE, then this
9515 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9516 something is being declared.
9518 Returns the TYPE specified. */
9521 cp_parser_elaborated_type_specifier (cp_parser* parser,
9523 bool is_declaration)
9525 enum tag_types tag_type;
9527 tree type = NULL_TREE;
9528 tree attributes = NULL_TREE;
9530 /* See if we're looking at the `enum' keyword. */
9531 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9533 /* Consume the `enum' token. */
9534 cp_lexer_consume_token (parser->lexer);
9535 /* Remember that it's an enumeration type. */
9536 tag_type = enum_type;
9537 /* Parse the attributes. */
9538 attributes = cp_parser_attributes_opt (parser);
9540 /* Or, it might be `typename'. */
9541 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9544 /* Consume the `typename' token. */
9545 cp_lexer_consume_token (parser->lexer);
9546 /* Remember that it's a `typename' type. */
9547 tag_type = typename_type;
9548 /* The `typename' keyword is only allowed in templates. */
9549 if (!processing_template_decl)
9550 pedwarn ("using %<typename%> outside of template");
9552 /* Otherwise it must be a class-key. */
9555 tag_type = cp_parser_class_key (parser);
9556 if (tag_type == none_type)
9557 return error_mark_node;
9558 /* Parse the attributes. */
9559 attributes = cp_parser_attributes_opt (parser);
9562 /* Look for the `::' operator. */
9563 cp_parser_global_scope_opt (parser,
9564 /*current_scope_valid_p=*/false);
9565 /* Look for the nested-name-specifier. */
9566 if (tag_type == typename_type)
9568 if (cp_parser_nested_name_specifier (parser,
9569 /*typename_keyword_p=*/true,
9570 /*check_dependency_p=*/true,
9574 return error_mark_node;
9577 /* Even though `typename' is not present, the proposed resolution
9578 to Core Issue 180 says that in `class A<T>::B', `B' should be
9579 considered a type-name, even if `A<T>' is dependent. */
9580 cp_parser_nested_name_specifier_opt (parser,
9581 /*typename_keyword_p=*/true,
9582 /*check_dependency_p=*/true,
9585 /* For everything but enumeration types, consider a template-id. */
9586 if (tag_type != enum_type)
9588 bool template_p = false;
9591 /* Allow the `template' keyword. */
9592 template_p = cp_parser_optional_template_keyword (parser);
9593 /* If we didn't see `template', we don't know if there's a
9594 template-id or not. */
9596 cp_parser_parse_tentatively (parser);
9597 /* Parse the template-id. */
9598 decl = cp_parser_template_id (parser, template_p,
9599 /*check_dependency_p=*/true,
9601 /* If we didn't find a template-id, look for an ordinary
9603 if (!template_p && !cp_parser_parse_definitely (parser))
9605 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9606 in effect, then we must assume that, upon instantiation, the
9607 template will correspond to a class. */
9608 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9609 && tag_type == typename_type)
9610 type = make_typename_type (parser->scope, decl,
9613 type = TREE_TYPE (decl);
9616 /* For an enumeration type, consider only a plain identifier. */
9619 identifier = cp_parser_identifier (parser);
9621 if (identifier == error_mark_node)
9623 parser->scope = NULL_TREE;
9624 return error_mark_node;
9627 /* For a `typename', we needn't call xref_tag. */
9628 if (tag_type == typename_type)
9629 return cp_parser_make_typename_type (parser, parser->scope,
9631 /* Look up a qualified name in the usual way. */
9636 /* In an elaborated-type-specifier, names are assumed to name
9637 types, so we set IS_TYPE to TRUE when calling
9638 cp_parser_lookup_name. */
9639 decl = cp_parser_lookup_name (parser, identifier,
9641 /*is_template=*/false,
9642 /*is_namespace=*/false,
9643 /*check_dependency=*/true,
9644 /*ambiguous_p=*/NULL);
9646 /* If we are parsing friend declaration, DECL may be a
9647 TEMPLATE_DECL tree node here. However, we need to check
9648 whether this TEMPLATE_DECL results in valid code. Consider
9649 the following example:
9652 template <class T> class C {};
9655 template <class T> friend class N::C; // #1, valid code
9657 template <class T> class Y {
9658 friend class N::C; // #2, invalid code
9661 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9662 name lookup of `N::C'. We see that friend declaration must
9663 be template for the code to be valid. Note that
9664 processing_template_decl does not work here since it is
9665 always 1 for the above two cases. */
9667 decl = (cp_parser_maybe_treat_template_as_class
9668 (decl, /*tag_name_p=*/is_friend
9669 && parser->num_template_parameter_lists));
9671 if (TREE_CODE (decl) != TYPE_DECL)
9673 error ("expected type-name");
9674 return error_mark_node;
9677 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9678 check_elaborated_type_specifier
9680 (parser->num_template_parameter_lists
9681 || DECL_SELF_REFERENCE_P (decl)));
9683 type = TREE_TYPE (decl);
9687 /* An elaborated-type-specifier sometimes introduces a new type and
9688 sometimes names an existing type. Normally, the rule is that it
9689 introduces a new type only if there is not an existing type of
9690 the same name already in scope. For example, given:
9693 void f() { struct S s; }
9695 the `struct S' in the body of `f' is the same `struct S' as in
9696 the global scope; the existing definition is used. However, if
9697 there were no global declaration, this would introduce a new
9698 local class named `S'.
9700 An exception to this rule applies to the following code:
9702 namespace N { struct S; }
9704 Here, the elaborated-type-specifier names a new type
9705 unconditionally; even if there is already an `S' in the
9706 containing scope this declaration names a new type.
9707 This exception only applies if the elaborated-type-specifier
9708 forms the complete declaration:
9712 A declaration consisting solely of `class-key identifier ;' is
9713 either a redeclaration of the name in the current scope or a
9714 forward declaration of the identifier as a class name. It
9715 introduces the name into the current scope.
9717 We are in this situation precisely when the next token is a `;'.
9719 An exception to the exception is that a `friend' declaration does
9720 *not* name a new type; i.e., given:
9722 struct S { friend struct T; };
9724 `T' is not a new type in the scope of `S'.
9726 Also, `new struct S' or `sizeof (struct S)' never results in the
9727 definition of a new type; a new type can only be declared in a
9728 declaration context. */
9730 /* Warn about attributes. They are ignored. */
9732 warning ("type attributes are honored only at type definition");
9734 type = xref_tag (tag_type, identifier,
9737 || cp_lexer_next_token_is_not (parser->lexer,
9739 parser->num_template_parameter_lists);
9742 if (tag_type != enum_type)
9743 cp_parser_check_class_key (tag_type, type);
9745 /* A "<" cannot follow an elaborated type specifier. If that
9746 happens, the user was probably trying to form a template-id. */
9747 cp_parser_check_for_invalid_template_id (parser, type);
9752 /* Parse an enum-specifier.
9755 enum identifier [opt] { enumerator-list [opt] }
9758 enum identifier [opt] { enumerator-list [opt] } attributes
9760 Returns an ENUM_TYPE representing the enumeration. */
9763 cp_parser_enum_specifier (cp_parser* parser)
9768 /* Caller guarantees that the current token is 'enum', an identifier
9769 possibly follows, and the token after that is an opening brace.
9770 If we don't have an identifier, fabricate an anonymous name for
9771 the enumeration being defined. */
9772 cp_lexer_consume_token (parser->lexer);
9774 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9775 identifier = cp_parser_identifier (parser);
9777 identifier = make_anon_name ();
9779 /* Issue an error message if type-definitions are forbidden here. */
9780 cp_parser_check_type_definition (parser);
9782 /* Create the new type. We do this before consuming the opening brace
9783 so the enum will be recorded as being on the line of its tag (or the
9784 'enum' keyword, if there is no tag). */
9785 type = start_enum (identifier);
9787 /* Consume the opening brace. */
9788 cp_lexer_consume_token (parser->lexer);
9790 /* If the next token is not '}', then there are some enumerators. */
9791 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9792 cp_parser_enumerator_list (parser, type);
9794 /* Consume the final '}'. */
9795 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9797 /* Look for trailing attributes to apply to this enumeration, and
9798 apply them if appropriate. */
9799 if (cp_parser_allow_gnu_extensions_p (parser))
9801 tree trailing_attr = cp_parser_attributes_opt (parser);
9802 cplus_decl_attributes (&type,
9804 (int) ATTR_FLAG_TYPE_IN_PLACE);
9807 /* Finish up the enumeration. */
9813 /* Parse an enumerator-list. The enumerators all have the indicated
9817 enumerator-definition
9818 enumerator-list , enumerator-definition */
9821 cp_parser_enumerator_list (cp_parser* parser, tree type)
9825 /* Parse an enumerator-definition. */
9826 cp_parser_enumerator_definition (parser, type);
9828 /* If the next token is not a ',', we've reached the end of
9830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9832 /* Otherwise, consume the `,' and keep going. */
9833 cp_lexer_consume_token (parser->lexer);
9834 /* If the next token is a `}', there is a trailing comma. */
9835 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9837 if (pedantic && !in_system_header)
9838 pedwarn ("comma at end of enumerator list");
9844 /* Parse an enumerator-definition. The enumerator has the indicated
9847 enumerator-definition:
9849 enumerator = constant-expression
9855 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9860 /* Look for the identifier. */
9861 identifier = cp_parser_identifier (parser);
9862 if (identifier == error_mark_node)
9865 /* If the next token is an '=', then there is an explicit value. */
9866 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9868 /* Consume the `=' token. */
9869 cp_lexer_consume_token (parser->lexer);
9870 /* Parse the value. */
9871 value = cp_parser_constant_expression (parser,
9872 /*allow_non_constant_p=*/false,
9878 /* Create the enumerator. */
9879 build_enumerator (identifier, value, type);
9882 /* Parse a namespace-name.
9885 original-namespace-name
9888 Returns the NAMESPACE_DECL for the namespace. */
9891 cp_parser_namespace_name (cp_parser* parser)
9894 tree namespace_decl;
9896 /* Get the name of the namespace. */
9897 identifier = cp_parser_identifier (parser);
9898 if (identifier == error_mark_node)
9899 return error_mark_node;
9901 /* Look up the identifier in the currently active scope. Look only
9902 for namespaces, due to:
9906 When looking up a namespace-name in a using-directive or alias
9907 definition, only namespace names are considered.
9913 During the lookup of a name preceding the :: scope resolution
9914 operator, object, function, and enumerator names are ignored.
9916 (Note that cp_parser_class_or_namespace_name only calls this
9917 function if the token after the name is the scope resolution
9919 namespace_decl = cp_parser_lookup_name (parser, identifier,
9921 /*is_template=*/false,
9922 /*is_namespace=*/true,
9923 /*check_dependency=*/true,
9924 /*ambiguous_p=*/NULL);
9925 /* If it's not a namespace, issue an error. */
9926 if (namespace_decl == error_mark_node
9927 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9929 cp_parser_error (parser, "expected namespace-name");
9930 namespace_decl = error_mark_node;
9933 return namespace_decl;
9936 /* Parse a namespace-definition.
9938 namespace-definition:
9939 named-namespace-definition
9940 unnamed-namespace-definition
9942 named-namespace-definition:
9943 original-namespace-definition
9944 extension-namespace-definition
9946 original-namespace-definition:
9947 namespace identifier { namespace-body }
9949 extension-namespace-definition:
9950 namespace original-namespace-name { namespace-body }
9952 unnamed-namespace-definition:
9953 namespace { namespace-body } */
9956 cp_parser_namespace_definition (cp_parser* parser)
9960 /* Look for the `namespace' keyword. */
9961 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9963 /* Get the name of the namespace. We do not attempt to distinguish
9964 between an original-namespace-definition and an
9965 extension-namespace-definition at this point. The semantic
9966 analysis routines are responsible for that. */
9967 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9968 identifier = cp_parser_identifier (parser);
9970 identifier = NULL_TREE;
9972 /* Look for the `{' to start the namespace. */
9973 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9974 /* Start the namespace. */
9975 push_namespace (identifier);
9976 /* Parse the body of the namespace. */
9977 cp_parser_namespace_body (parser);
9978 /* Finish the namespace. */
9980 /* Look for the final `}'. */
9981 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9984 /* Parse a namespace-body.
9987 declaration-seq [opt] */
9990 cp_parser_namespace_body (cp_parser* parser)
9992 cp_parser_declaration_seq_opt (parser);
9995 /* Parse a namespace-alias-definition.
9997 namespace-alias-definition:
9998 namespace identifier = qualified-namespace-specifier ; */
10001 cp_parser_namespace_alias_definition (cp_parser* parser)
10004 tree namespace_specifier;
10006 /* Look for the `namespace' keyword. */
10007 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10008 /* Look for the identifier. */
10009 identifier = cp_parser_identifier (parser);
10010 if (identifier == error_mark_node)
10012 /* Look for the `=' token. */
10013 cp_parser_require (parser, CPP_EQ, "`='");
10014 /* Look for the qualified-namespace-specifier. */
10015 namespace_specifier
10016 = cp_parser_qualified_namespace_specifier (parser);
10017 /* Look for the `;' token. */
10018 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10020 /* Register the alias in the symbol table. */
10021 do_namespace_alias (identifier, namespace_specifier);
10024 /* Parse a qualified-namespace-specifier.
10026 qualified-namespace-specifier:
10027 :: [opt] nested-name-specifier [opt] namespace-name
10029 Returns a NAMESPACE_DECL corresponding to the specified
10033 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10035 /* Look for the optional `::'. */
10036 cp_parser_global_scope_opt (parser,
10037 /*current_scope_valid_p=*/false);
10039 /* Look for the optional nested-name-specifier. */
10040 cp_parser_nested_name_specifier_opt (parser,
10041 /*typename_keyword_p=*/false,
10042 /*check_dependency_p=*/true,
10044 /*is_declaration=*/true);
10046 return cp_parser_namespace_name (parser);
10049 /* Parse a using-declaration.
10052 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10053 using :: unqualified-id ; */
10056 cp_parser_using_declaration (cp_parser* parser)
10059 bool typename_p = false;
10060 bool global_scope_p;
10065 /* Look for the `using' keyword. */
10066 cp_parser_require_keyword (parser, RID_USING, "`using'");
10068 /* Peek at the next token. */
10069 token = cp_lexer_peek_token (parser->lexer);
10070 /* See if it's `typename'. */
10071 if (token->keyword == RID_TYPENAME)
10073 /* Remember that we've seen it. */
10075 /* Consume the `typename' token. */
10076 cp_lexer_consume_token (parser->lexer);
10079 /* Look for the optional global scope qualification. */
10081 = (cp_parser_global_scope_opt (parser,
10082 /*current_scope_valid_p=*/false)
10085 /* If we saw `typename', or didn't see `::', then there must be a
10086 nested-name-specifier present. */
10087 if (typename_p || !global_scope_p)
10088 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10089 /*check_dependency_p=*/true,
10091 /*is_declaration=*/true);
10092 /* Otherwise, we could be in either of the two productions. In that
10093 case, treat the nested-name-specifier as optional. */
10095 qscope = cp_parser_nested_name_specifier_opt (parser,
10096 /*typename_keyword_p=*/false,
10097 /*check_dependency_p=*/true,
10099 /*is_declaration=*/true);
10101 qscope = global_namespace;
10103 /* Parse the unqualified-id. */
10104 identifier = cp_parser_unqualified_id (parser,
10105 /*template_keyword_p=*/false,
10106 /*check_dependency_p=*/true,
10107 /*declarator_p=*/true);
10109 /* The function we call to handle a using-declaration is different
10110 depending on what scope we are in. */
10111 if (identifier == error_mark_node)
10113 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10114 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10115 /* [namespace.udecl]
10117 A using declaration shall not name a template-id. */
10118 error ("a template-id may not appear in a using-declaration");
10121 if (at_class_scope_p ())
10123 /* Create the USING_DECL. */
10124 decl = do_class_using_decl (build_nt (SCOPE_REF,
10127 /* Add it to the list of members in this class. */
10128 finish_member_declaration (decl);
10132 decl = cp_parser_lookup_name_simple (parser, identifier);
10133 if (decl == error_mark_node)
10134 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10135 else if (!at_namespace_scope_p ())
10136 do_local_using_decl (decl, qscope, identifier);
10138 do_toplevel_using_decl (decl, qscope, identifier);
10142 /* Look for the final `;'. */
10143 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10146 /* Parse a using-directive.
10149 using namespace :: [opt] nested-name-specifier [opt]
10150 namespace-name ; */
10153 cp_parser_using_directive (cp_parser* parser)
10155 tree namespace_decl;
10158 /* Look for the `using' keyword. */
10159 cp_parser_require_keyword (parser, RID_USING, "`using'");
10160 /* And the `namespace' keyword. */
10161 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10162 /* Look for the optional `::' operator. */
10163 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10164 /* And the optional nested-name-specifier. */
10165 cp_parser_nested_name_specifier_opt (parser,
10166 /*typename_keyword_p=*/false,
10167 /*check_dependency_p=*/true,
10169 /*is_declaration=*/true);
10170 /* Get the namespace being used. */
10171 namespace_decl = cp_parser_namespace_name (parser);
10172 /* And any specified attributes. */
10173 attribs = cp_parser_attributes_opt (parser);
10174 /* Update the symbol table. */
10175 parse_using_directive (namespace_decl, attribs);
10176 /* Look for the final `;'. */
10177 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10180 /* Parse an asm-definition.
10183 asm ( string-literal ) ;
10188 asm volatile [opt] ( string-literal ) ;
10189 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10190 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10191 : asm-operand-list [opt] ) ;
10192 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10193 : asm-operand-list [opt]
10194 : asm-operand-list [opt] ) ; */
10197 cp_parser_asm_definition (cp_parser* parser)
10200 tree outputs = NULL_TREE;
10201 tree inputs = NULL_TREE;
10202 tree clobbers = NULL_TREE;
10204 bool volatile_p = false;
10205 bool extended_p = false;
10207 /* Look for the `asm' keyword. */
10208 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10209 /* See if the next token is `volatile'. */
10210 if (cp_parser_allow_gnu_extensions_p (parser)
10211 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10213 /* Remember that we saw the `volatile' keyword. */
10215 /* Consume the token. */
10216 cp_lexer_consume_token (parser->lexer);
10218 /* Look for the opening `('. */
10219 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10221 /* Look for the string. */
10222 string = cp_parser_string_literal (parser, false, false);
10223 if (string == error_mark_node)
10225 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10226 /*consume_paren=*/true);
10230 /* If we're allowing GNU extensions, check for the extended assembly
10231 syntax. Unfortunately, the `:' tokens need not be separated by
10232 a space in C, and so, for compatibility, we tolerate that here
10233 too. Doing that means that we have to treat the `::' operator as
10235 if (cp_parser_allow_gnu_extensions_p (parser)
10236 && at_function_scope_p ()
10237 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10238 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10240 bool inputs_p = false;
10241 bool clobbers_p = false;
10243 /* The extended syntax was used. */
10246 /* Look for outputs. */
10247 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10249 /* Consume the `:'. */
10250 cp_lexer_consume_token (parser->lexer);
10251 /* Parse the output-operands. */
10252 if (cp_lexer_next_token_is_not (parser->lexer,
10254 && cp_lexer_next_token_is_not (parser->lexer,
10256 && cp_lexer_next_token_is_not (parser->lexer,
10258 outputs = cp_parser_asm_operand_list (parser);
10260 /* If the next token is `::', there are no outputs, and the
10261 next token is the beginning of the inputs. */
10262 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10263 /* The inputs are coming next. */
10266 /* Look for inputs. */
10268 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10270 /* Consume the `:' or `::'. */
10271 cp_lexer_consume_token (parser->lexer);
10272 /* Parse the output-operands. */
10273 if (cp_lexer_next_token_is_not (parser->lexer,
10275 && cp_lexer_next_token_is_not (parser->lexer,
10277 inputs = cp_parser_asm_operand_list (parser);
10279 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10280 /* The clobbers are coming next. */
10283 /* Look for clobbers. */
10285 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10287 /* Consume the `:' or `::'. */
10288 cp_lexer_consume_token (parser->lexer);
10289 /* Parse the clobbers. */
10290 if (cp_lexer_next_token_is_not (parser->lexer,
10292 clobbers = cp_parser_asm_clobber_list (parser);
10295 /* Look for the closing `)'. */
10296 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10297 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10298 /*consume_paren=*/true);
10299 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10301 /* Create the ASM_EXPR. */
10302 if (at_function_scope_p ())
10304 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10306 /* If the extended syntax was not used, mark the ASM_EXPR. */
10309 tree temp = asm_stmt;
10310 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10311 temp = TREE_OPERAND (temp, 0);
10313 ASM_INPUT_P (temp) = 1;
10317 assemble_asm (string);
10320 /* Declarators [gram.dcl.decl] */
10322 /* Parse an init-declarator.
10325 declarator initializer [opt]
10330 declarator asm-specification [opt] attributes [opt] initializer [opt]
10332 function-definition:
10333 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10335 decl-specifier-seq [opt] declarator function-try-block
10339 function-definition:
10340 __extension__ function-definition
10342 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10343 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10344 then this declarator appears in a class scope. The new DECL created
10345 by this declarator is returned.
10347 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10348 for a function-definition here as well. If the declarator is a
10349 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10350 be TRUE upon return. By that point, the function-definition will
10351 have been completely parsed.
10353 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10357 cp_parser_init_declarator (cp_parser* parser,
10358 cp_decl_specifier_seq *decl_specifiers,
10359 bool function_definition_allowed_p,
10361 int declares_class_or_enum,
10362 bool* function_definition_p)
10365 cp_declarator *declarator;
10366 tree prefix_attributes;
10368 tree asm_specification;
10370 tree decl = NULL_TREE;
10372 bool is_initialized;
10373 bool is_parenthesized_init;
10374 bool is_non_constant_init;
10375 int ctor_dtor_or_conv_p;
10377 bool pop_p = false;
10379 /* Gather the attributes that were provided with the
10380 decl-specifiers. */
10381 prefix_attributes = decl_specifiers->attributes;
10383 /* Assume that this is not the declarator for a function
10385 if (function_definition_p)
10386 *function_definition_p = false;
10388 /* Defer access checks while parsing the declarator; we cannot know
10389 what names are accessible until we know what is being
10391 resume_deferring_access_checks ();
10393 /* Parse the declarator. */
10395 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10396 &ctor_dtor_or_conv_p,
10397 /*parenthesized_p=*/NULL,
10398 /*member_p=*/false);
10399 /* Gather up the deferred checks. */
10400 stop_deferring_access_checks ();
10402 /* If the DECLARATOR was erroneous, there's no need to go
10404 if (declarator == cp_error_declarator)
10405 return error_mark_node;
10407 cp_parser_check_for_definition_in_return_type (declarator,
10408 declares_class_or_enum);
10410 /* Figure out what scope the entity declared by the DECLARATOR is
10411 located in. `grokdeclarator' sometimes changes the scope, so
10412 we compute it now. */
10413 scope = get_scope_of_declarator (declarator);
10415 /* If we're allowing GNU extensions, look for an asm-specification
10417 if (cp_parser_allow_gnu_extensions_p (parser))
10419 /* Look for an asm-specification. */
10420 asm_specification = cp_parser_asm_specification_opt (parser);
10421 /* And attributes. */
10422 attributes = cp_parser_attributes_opt (parser);
10426 asm_specification = NULL_TREE;
10427 attributes = NULL_TREE;
10430 /* Peek at the next token. */
10431 token = cp_lexer_peek_token (parser->lexer);
10432 /* Check to see if the token indicates the start of a
10433 function-definition. */
10434 if (cp_parser_token_starts_function_definition_p (token))
10436 if (!function_definition_allowed_p)
10438 /* If a function-definition should not appear here, issue an
10440 cp_parser_error (parser,
10441 "a function-definition is not allowed here");
10442 return error_mark_node;
10446 /* Neither attributes nor an asm-specification are allowed
10447 on a function-definition. */
10448 if (asm_specification)
10449 error ("an asm-specification is not allowed on a function-definition");
10451 error ("attributes are not allowed on a function-definition");
10452 /* This is a function-definition. */
10453 *function_definition_p = true;
10455 /* Parse the function definition. */
10457 decl = cp_parser_save_member_function_body (parser,
10460 prefix_attributes);
10463 = (cp_parser_function_definition_from_specifiers_and_declarator
10464 (parser, decl_specifiers, prefix_attributes, declarator));
10472 Only in function declarations for constructors, destructors, and
10473 type conversions can the decl-specifier-seq be omitted.
10475 We explicitly postpone this check past the point where we handle
10476 function-definitions because we tolerate function-definitions
10477 that are missing their return types in some modes. */
10478 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10480 cp_parser_error (parser,
10481 "expected constructor, destructor, or type conversion");
10482 return error_mark_node;
10485 /* An `=' or an `(' indicates an initializer. */
10486 is_initialized = (token->type == CPP_EQ
10487 || token->type == CPP_OPEN_PAREN);
10488 /* If the init-declarator isn't initialized and isn't followed by a
10489 `,' or `;', it's not a valid init-declarator. */
10490 if (!is_initialized
10491 && token->type != CPP_COMMA
10492 && token->type != CPP_SEMICOLON)
10494 cp_parser_error (parser, "expected initializer");
10495 return error_mark_node;
10498 /* Because start_decl has side-effects, we should only call it if we
10499 know we're going ahead. By this point, we know that we cannot
10500 possibly be looking at any other construct. */
10501 cp_parser_commit_to_tentative_parse (parser);
10503 /* If the decl specifiers were bad, issue an error now that we're
10504 sure this was intended to be a declarator. Then continue
10505 declaring the variable(s), as int, to try to cut down on further
10507 if (decl_specifiers->any_specifiers_p
10508 && decl_specifiers->type == error_mark_node)
10510 cp_parser_error (parser, "invalid type in declaration");
10511 decl_specifiers->type = integer_type_node;
10514 /* Check to see whether or not this declaration is a friend. */
10515 friend_p = cp_parser_friend_p (decl_specifiers);
10517 /* Check that the number of template-parameter-lists is OK. */
10518 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10519 return error_mark_node;
10521 /* Enter the newly declared entry in the symbol table. If we're
10522 processing a declaration in a class-specifier, we wait until
10523 after processing the initializer. */
10526 if (parser->in_unbraced_linkage_specification_p)
10528 decl_specifiers->storage_class = sc_extern;
10529 have_extern_spec = false;
10531 decl = start_decl (declarator, decl_specifiers,
10532 is_initialized, attributes, prefix_attributes,
10536 /* Enter the SCOPE. That way unqualified names appearing in the
10537 initializer will be looked up in SCOPE. */
10538 pop_p = push_scope (scope);
10540 /* Perform deferred access control checks, now that we know in which
10541 SCOPE the declared entity resides. */
10542 if (!member_p && decl)
10544 tree saved_current_function_decl = NULL_TREE;
10546 /* If the entity being declared is a function, pretend that we
10547 are in its scope. If it is a `friend', it may have access to
10548 things that would not otherwise be accessible. */
10549 if (TREE_CODE (decl) == FUNCTION_DECL)
10551 saved_current_function_decl = current_function_decl;
10552 current_function_decl = decl;
10555 /* Perform the access control checks for the declarator and the
10556 the decl-specifiers. */
10557 perform_deferred_access_checks ();
10559 /* Restore the saved value. */
10560 if (TREE_CODE (decl) == FUNCTION_DECL)
10561 current_function_decl = saved_current_function_decl;
10564 /* Parse the initializer. */
10565 if (is_initialized)
10566 initializer = cp_parser_initializer (parser,
10567 &is_parenthesized_init,
10568 &is_non_constant_init);
10571 initializer = NULL_TREE;
10572 is_parenthesized_init = false;
10573 is_non_constant_init = true;
10576 /* The old parser allows attributes to appear after a parenthesized
10577 initializer. Mark Mitchell proposed removing this functionality
10578 on the GCC mailing lists on 2002-08-13. This parser accepts the
10579 attributes -- but ignores them. */
10580 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10581 if (cp_parser_attributes_opt (parser))
10582 warning ("attributes after parenthesized initializer ignored");
10584 /* For an in-class declaration, use `grokfield' to create the
10590 decl = grokfield (declarator, decl_specifiers,
10591 initializer, /*asmspec=*/NULL_TREE,
10592 /*attributes=*/NULL_TREE);
10593 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10594 cp_parser_save_default_args (parser, decl);
10597 /* Finish processing the declaration. But, skip friend
10599 if (!friend_p && decl && decl != error_mark_node)
10601 cp_finish_decl (decl,
10604 /* If the initializer is in parentheses, then this is
10605 a direct-initialization, which means that an
10606 `explicit' constructor is OK. Otherwise, an
10607 `explicit' constructor cannot be used. */
10608 ((is_parenthesized_init || !is_initialized)
10609 ? 0 : LOOKUP_ONLYCONVERTING));
10611 pop_scope (DECL_CONTEXT (decl));
10614 /* Remember whether or not variables were initialized by
10615 constant-expressions. */
10616 if (decl && TREE_CODE (decl) == VAR_DECL
10617 && is_initialized && !is_non_constant_init)
10618 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10623 /* Parse a declarator.
10627 ptr-operator declarator
10629 abstract-declarator:
10630 ptr-operator abstract-declarator [opt]
10631 direct-abstract-declarator
10636 attributes [opt] direct-declarator
10637 attributes [opt] ptr-operator declarator
10639 abstract-declarator:
10640 attributes [opt] ptr-operator abstract-declarator [opt]
10641 attributes [opt] direct-abstract-declarator
10643 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10644 detect constructor, destructor or conversion operators. It is set
10645 to -1 if the declarator is a name, and +1 if it is a
10646 function. Otherwise it is set to zero. Usually you just want to
10647 test for >0, but internally the negative value is used.
10649 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10650 a decl-specifier-seq unless it declares a constructor, destructor,
10651 or conversion. It might seem that we could check this condition in
10652 semantic analysis, rather than parsing, but that makes it difficult
10653 to handle something like `f()'. We want to notice that there are
10654 no decl-specifiers, and therefore realize that this is an
10655 expression, not a declaration.)
10657 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10658 the declarator is a direct-declarator of the form "(...)".
10660 MEMBER_P is true iff this declarator is a member-declarator. */
10662 static cp_declarator *
10663 cp_parser_declarator (cp_parser* parser,
10664 cp_parser_declarator_kind dcl_kind,
10665 int* ctor_dtor_or_conv_p,
10666 bool* parenthesized_p,
10670 cp_declarator *declarator;
10671 enum tree_code code;
10672 cp_cv_quals cv_quals;
10674 tree attributes = NULL_TREE;
10676 /* Assume this is not a constructor, destructor, or type-conversion
10678 if (ctor_dtor_or_conv_p)
10679 *ctor_dtor_or_conv_p = 0;
10681 if (cp_parser_allow_gnu_extensions_p (parser))
10682 attributes = cp_parser_attributes_opt (parser);
10684 /* Peek at the next token. */
10685 token = cp_lexer_peek_token (parser->lexer);
10687 /* Check for the ptr-operator production. */
10688 cp_parser_parse_tentatively (parser);
10689 /* Parse the ptr-operator. */
10690 code = cp_parser_ptr_operator (parser,
10693 /* If that worked, then we have a ptr-operator. */
10694 if (cp_parser_parse_definitely (parser))
10696 /* If a ptr-operator was found, then this declarator was not
10698 if (parenthesized_p)
10699 *parenthesized_p = true;
10700 /* The dependent declarator is optional if we are parsing an
10701 abstract-declarator. */
10702 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10703 cp_parser_parse_tentatively (parser);
10705 /* Parse the dependent declarator. */
10706 declarator = cp_parser_declarator (parser, dcl_kind,
10707 /*ctor_dtor_or_conv_p=*/NULL,
10708 /*parenthesized_p=*/NULL,
10709 /*member_p=*/false);
10711 /* If we are parsing an abstract-declarator, we must handle the
10712 case where the dependent declarator is absent. */
10713 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10714 && !cp_parser_parse_definitely (parser))
10717 /* Build the representation of the ptr-operator. */
10719 declarator = make_ptrmem_declarator (cv_quals,
10722 else if (code == INDIRECT_REF)
10723 declarator = make_pointer_declarator (cv_quals, declarator);
10725 declarator = make_reference_declarator (cv_quals, declarator);
10727 /* Everything else is a direct-declarator. */
10730 if (parenthesized_p)
10731 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10733 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10734 ctor_dtor_or_conv_p,
10738 if (attributes && declarator != cp_error_declarator)
10739 declarator->attributes = attributes;
10744 /* Parse a direct-declarator or direct-abstract-declarator.
10748 direct-declarator ( parameter-declaration-clause )
10749 cv-qualifier-seq [opt]
10750 exception-specification [opt]
10751 direct-declarator [ constant-expression [opt] ]
10754 direct-abstract-declarator:
10755 direct-abstract-declarator [opt]
10756 ( parameter-declaration-clause )
10757 cv-qualifier-seq [opt]
10758 exception-specification [opt]
10759 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10760 ( abstract-declarator )
10762 Returns a representation of the declarator. DCL_KIND is
10763 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10764 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10765 we are parsing a direct-declarator. It is
10766 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10767 of ambiguity we prefer an abstract declarator, as per
10768 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10769 cp_parser_declarator. */
10771 static cp_declarator *
10772 cp_parser_direct_declarator (cp_parser* parser,
10773 cp_parser_declarator_kind dcl_kind,
10774 int* ctor_dtor_or_conv_p,
10778 cp_declarator *declarator = NULL;
10779 tree scope = NULL_TREE;
10780 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10781 bool saved_in_declarator_p = parser->in_declarator_p;
10783 bool pop_p = false;
10787 /* Peek at the next token. */
10788 token = cp_lexer_peek_token (parser->lexer);
10789 if (token->type == CPP_OPEN_PAREN)
10791 /* This is either a parameter-declaration-clause, or a
10792 parenthesized declarator. When we know we are parsing a
10793 named declarator, it must be a parenthesized declarator
10794 if FIRST is true. For instance, `(int)' is a
10795 parameter-declaration-clause, with an omitted
10796 direct-abstract-declarator. But `((*))', is a
10797 parenthesized abstract declarator. Finally, when T is a
10798 template parameter `(T)' is a
10799 parameter-declaration-clause, and not a parenthesized
10802 We first try and parse a parameter-declaration-clause,
10803 and then try a nested declarator (if FIRST is true).
10805 It is not an error for it not to be a
10806 parameter-declaration-clause, even when FIRST is
10812 The first is the declaration of a function while the
10813 second is a the definition of a variable, including its
10816 Having seen only the parenthesis, we cannot know which of
10817 these two alternatives should be selected. Even more
10818 complex are examples like:
10823 The former is a function-declaration; the latter is a
10824 variable initialization.
10826 Thus again, we try a parameter-declaration-clause, and if
10827 that fails, we back out and return. */
10829 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10831 cp_parameter_declarator *params;
10832 unsigned saved_num_template_parameter_lists;
10834 /* In a member-declarator, the only valid interpretation
10835 of a parenthesis is the start of a
10836 parameter-declaration-clause. (It is invalid to
10837 initialize a static data member with a parenthesized
10838 initializer; only the "=" form of initialization is
10841 cp_parser_parse_tentatively (parser);
10843 /* Consume the `('. */
10844 cp_lexer_consume_token (parser->lexer);
10847 /* If this is going to be an abstract declarator, we're
10848 in a declarator and we can't have default args. */
10849 parser->default_arg_ok_p = false;
10850 parser->in_declarator_p = true;
10853 /* Inside the function parameter list, surrounding
10854 template-parameter-lists do not apply. */
10855 saved_num_template_parameter_lists
10856 = parser->num_template_parameter_lists;
10857 parser->num_template_parameter_lists = 0;
10859 /* Parse the parameter-declaration-clause. */
10860 params = cp_parser_parameter_declaration_clause (parser);
10862 parser->num_template_parameter_lists
10863 = saved_num_template_parameter_lists;
10865 /* If all went well, parse the cv-qualifier-seq and the
10866 exception-specification. */
10867 if (member_p || cp_parser_parse_definitely (parser))
10869 cp_cv_quals cv_quals;
10870 tree exception_specification;
10872 if (ctor_dtor_or_conv_p)
10873 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10875 /* Consume the `)'. */
10876 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10878 /* Parse the cv-qualifier-seq. */
10879 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10880 /* And the exception-specification. */
10881 exception_specification
10882 = cp_parser_exception_specification_opt (parser);
10884 /* Create the function-declarator. */
10885 declarator = make_call_declarator (declarator,
10888 exception_specification);
10889 /* Any subsequent parameter lists are to do with
10890 return type, so are not those of the declared
10892 parser->default_arg_ok_p = false;
10894 /* Repeat the main loop. */
10899 /* If this is the first, we can try a parenthesized
10903 bool saved_in_type_id_in_expr_p;
10905 parser->default_arg_ok_p = saved_default_arg_ok_p;
10906 parser->in_declarator_p = saved_in_declarator_p;
10908 /* Consume the `('. */
10909 cp_lexer_consume_token (parser->lexer);
10910 /* Parse the nested declarator. */
10911 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10912 parser->in_type_id_in_expr_p = true;
10914 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10915 /*parenthesized_p=*/NULL,
10917 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10919 /* Expect a `)'. */
10920 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10921 declarator = cp_error_declarator;
10922 if (declarator == cp_error_declarator)
10925 goto handle_declarator;
10927 /* Otherwise, we must be done. */
10931 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10932 && token->type == CPP_OPEN_SQUARE)
10934 /* Parse an array-declarator. */
10937 if (ctor_dtor_or_conv_p)
10938 *ctor_dtor_or_conv_p = 0;
10941 parser->default_arg_ok_p = false;
10942 parser->in_declarator_p = true;
10943 /* Consume the `['. */
10944 cp_lexer_consume_token (parser->lexer);
10945 /* Peek at the next token. */
10946 token = cp_lexer_peek_token (parser->lexer);
10947 /* If the next token is `]', then there is no
10948 constant-expression. */
10949 if (token->type != CPP_CLOSE_SQUARE)
10951 bool non_constant_p;
10954 = cp_parser_constant_expression (parser,
10955 /*allow_non_constant=*/true,
10957 if (!non_constant_p)
10958 bounds = fold_non_dependent_expr (bounds);
10961 bounds = NULL_TREE;
10962 /* Look for the closing `]'. */
10963 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10965 declarator = cp_error_declarator;
10969 declarator = make_array_declarator (declarator, bounds);
10971 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10975 /* Parse a declarator-id */
10976 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10977 cp_parser_parse_tentatively (parser);
10978 id = cp_parser_declarator_id (parser);
10979 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10981 if (!cp_parser_parse_definitely (parser))
10982 id = error_mark_node;
10983 else if (TREE_CODE (id) != IDENTIFIER_NODE)
10985 cp_parser_error (parser, "expected unqualified-id");
10986 id = error_mark_node;
10990 if (id == error_mark_node)
10992 declarator = cp_error_declarator;
10996 if (TREE_CODE (id) == SCOPE_REF && at_namespace_scope_p ())
10998 tree scope = TREE_OPERAND (id, 0);
11000 /* In the declaration of a member of a template class
11001 outside of the class itself, the SCOPE will sometimes
11002 be a TYPENAME_TYPE. For example, given:
11004 template <typename T>
11005 int S<T>::R::i = 3;
11007 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11008 this context, we must resolve S<T>::R to an ordinary
11009 type, rather than a typename type.
11011 The reason we normally avoid resolving TYPENAME_TYPEs
11012 is that a specialization of `S' might render
11013 `S<T>::R' not a type. However, if `S' is
11014 specialized, then this `i' will not be used, so there
11015 is no harm in resolving the types here. */
11016 if (TREE_CODE (scope) == TYPENAME_TYPE)
11020 /* Resolve the TYPENAME_TYPE. */
11021 type = resolve_typename_type (scope,
11022 /*only_current_p=*/false);
11023 /* If that failed, the declarator is invalid. */
11024 if (type == error_mark_node)
11025 error ("%<%T::%D%> is not a type",
11026 TYPE_CONTEXT (scope),
11027 TYPE_IDENTIFIER (scope));
11028 /* Build a new DECLARATOR. */
11029 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11033 declarator = make_id_declarator (id);
11037 tree unqualified_name;
11039 if (TREE_CODE (id) == SCOPE_REF
11040 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11042 class_type = TREE_OPERAND (id, 0);
11043 unqualified_name = TREE_OPERAND (id, 1);
11047 class_type = current_class_type;
11048 unqualified_name = id;
11053 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11054 declarator->u.id.sfk = sfk_destructor;
11055 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11056 declarator->u.id.sfk = sfk_conversion;
11057 else if (constructor_name_p (unqualified_name,
11059 || (TREE_CODE (unqualified_name) == TYPE_DECL
11060 && same_type_p (TREE_TYPE (unqualified_name),
11062 declarator->u.id.sfk = sfk_constructor;
11064 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11065 *ctor_dtor_or_conv_p = -1;
11066 if (TREE_CODE (id) == SCOPE_REF
11067 && TREE_CODE (unqualified_name) == TYPE_DECL
11068 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11070 error ("invalid use of constructor as a template");
11071 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11072 "the constructor in a qualified name",
11074 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11075 class_type, class_type);
11080 handle_declarator:;
11081 scope = get_scope_of_declarator (declarator);
11083 /* Any names that appear after the declarator-id for a
11084 member are looked up in the containing scope. */
11085 pop_p = push_scope (scope);
11086 parser->in_declarator_p = true;
11087 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11088 || (declarator && declarator->kind == cdk_id))
11089 /* Default args are only allowed on function
11091 parser->default_arg_ok_p = saved_default_arg_ok_p;
11093 parser->default_arg_ok_p = false;
11102 /* For an abstract declarator, we might wind up with nothing at this
11103 point. That's an error; the declarator is not optional. */
11105 cp_parser_error (parser, "expected declarator");
11107 /* If we entered a scope, we must exit it now. */
11111 parser->default_arg_ok_p = saved_default_arg_ok_p;
11112 parser->in_declarator_p = saved_in_declarator_p;
11117 /* Parse a ptr-operator.
11120 * cv-qualifier-seq [opt]
11122 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11127 & cv-qualifier-seq [opt]
11129 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11130 Returns ADDR_EXPR if a reference was used. In the case of a
11131 pointer-to-member, *TYPE is filled in with the TYPE containing the
11132 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11133 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11134 ERROR_MARK if an error occurred. */
11136 static enum tree_code
11137 cp_parser_ptr_operator (cp_parser* parser,
11139 cp_cv_quals *cv_quals)
11141 enum tree_code code = ERROR_MARK;
11144 /* Assume that it's not a pointer-to-member. */
11146 /* And that there are no cv-qualifiers. */
11147 *cv_quals = TYPE_UNQUALIFIED;
11149 /* Peek at the next token. */
11150 token = cp_lexer_peek_token (parser->lexer);
11151 /* If it's a `*' or `&' we have a pointer or reference. */
11152 if (token->type == CPP_MULT || token->type == CPP_AND)
11154 /* Remember which ptr-operator we were processing. */
11155 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11157 /* Consume the `*' or `&'. */
11158 cp_lexer_consume_token (parser->lexer);
11160 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11161 `&', if we are allowing GNU extensions. (The only qualifier
11162 that can legally appear after `&' is `restrict', but that is
11163 enforced during semantic analysis. */
11164 if (code == INDIRECT_REF
11165 || cp_parser_allow_gnu_extensions_p (parser))
11166 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11170 /* Try the pointer-to-member case. */
11171 cp_parser_parse_tentatively (parser);
11172 /* Look for the optional `::' operator. */
11173 cp_parser_global_scope_opt (parser,
11174 /*current_scope_valid_p=*/false);
11175 /* Look for the nested-name specifier. */
11176 cp_parser_nested_name_specifier (parser,
11177 /*typename_keyword_p=*/false,
11178 /*check_dependency_p=*/true,
11180 /*is_declaration=*/false);
11181 /* If we found it, and the next token is a `*', then we are
11182 indeed looking at a pointer-to-member operator. */
11183 if (!cp_parser_error_occurred (parser)
11184 && cp_parser_require (parser, CPP_MULT, "`*'"))
11186 /* The type of which the member is a member is given by the
11188 *type = parser->scope;
11189 /* The next name will not be qualified. */
11190 parser->scope = NULL_TREE;
11191 parser->qualifying_scope = NULL_TREE;
11192 parser->object_scope = NULL_TREE;
11193 /* Indicate that the `*' operator was used. */
11194 code = INDIRECT_REF;
11195 /* Look for the optional cv-qualifier-seq. */
11196 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11198 /* If that didn't work we don't have a ptr-operator. */
11199 if (!cp_parser_parse_definitely (parser))
11200 cp_parser_error (parser, "expected ptr-operator");
11206 /* Parse an (optional) cv-qualifier-seq.
11209 cv-qualifier cv-qualifier-seq [opt]
11220 Returns a bitmask representing the cv-qualifiers. */
11223 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11225 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11230 cp_cv_quals cv_qualifier;
11232 /* Peek at the next token. */
11233 token = cp_lexer_peek_token (parser->lexer);
11234 /* See if it's a cv-qualifier. */
11235 switch (token->keyword)
11238 cv_qualifier = TYPE_QUAL_CONST;
11242 cv_qualifier = TYPE_QUAL_VOLATILE;
11246 cv_qualifier = TYPE_QUAL_RESTRICT;
11250 cv_qualifier = TYPE_UNQUALIFIED;
11257 if (cv_quals & cv_qualifier)
11259 error ("duplicate cv-qualifier");
11260 cp_lexer_purge_token (parser->lexer);
11264 cp_lexer_consume_token (parser->lexer);
11265 cv_quals |= cv_qualifier;
11272 /* Parse a declarator-id.
11276 :: [opt] nested-name-specifier [opt] type-name
11278 In the `id-expression' case, the value returned is as for
11279 cp_parser_id_expression if the id-expression was an unqualified-id.
11280 If the id-expression was a qualified-id, then a SCOPE_REF is
11281 returned. The first operand is the scope (either a NAMESPACE_DECL
11282 or TREE_TYPE), but the second is still just a representation of an
11286 cp_parser_declarator_id (cp_parser* parser)
11288 tree id_expression;
11290 /* The expression must be an id-expression. Assume that qualified
11291 names are the names of types so that:
11294 int S<T>::R::i = 3;
11296 will work; we must treat `S<T>::R' as the name of a type.
11297 Similarly, assume that qualified names are templates, where
11301 int S<T>::R<T>::i = 3;
11304 id_expression = cp_parser_id_expression (parser,
11305 /*template_keyword_p=*/false,
11306 /*check_dependency_p=*/false,
11307 /*template_p=*/NULL,
11308 /*declarator_p=*/true);
11309 /* If the name was qualified, create a SCOPE_REF to represent
11313 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11314 parser->scope = NULL_TREE;
11317 return id_expression;
11320 /* Parse a type-id.
11323 type-specifier-seq abstract-declarator [opt]
11325 Returns the TYPE specified. */
11328 cp_parser_type_id (cp_parser* parser)
11330 cp_decl_specifier_seq type_specifier_seq;
11331 cp_declarator *abstract_declarator;
11333 /* Parse the type-specifier-seq. */
11334 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11335 if (type_specifier_seq.type == error_mark_node)
11336 return error_mark_node;
11338 /* There might or might not be an abstract declarator. */
11339 cp_parser_parse_tentatively (parser);
11340 /* Look for the declarator. */
11341 abstract_declarator
11342 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11343 /*parenthesized_p=*/NULL,
11344 /*member_p=*/false);
11345 /* Check to see if there really was a declarator. */
11346 if (!cp_parser_parse_definitely (parser))
11347 abstract_declarator = NULL;
11349 return groktypename (&type_specifier_seq, abstract_declarator);
11352 /* Parse a type-specifier-seq.
11354 type-specifier-seq:
11355 type-specifier type-specifier-seq [opt]
11359 type-specifier-seq:
11360 attributes type-specifier-seq [opt]
11362 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11365 cp_parser_type_specifier_seq (cp_parser* parser,
11366 cp_decl_specifier_seq *type_specifier_seq)
11368 bool seen_type_specifier = false;
11370 /* Clear the TYPE_SPECIFIER_SEQ. */
11371 clear_decl_specs (type_specifier_seq);
11373 /* Parse the type-specifiers and attributes. */
11376 tree type_specifier;
11378 /* Check for attributes first. */
11379 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11381 type_specifier_seq->attributes =
11382 chainon (type_specifier_seq->attributes,
11383 cp_parser_attributes_opt (parser));
11387 /* Look for the type-specifier. */
11388 type_specifier = cp_parser_type_specifier (parser,
11389 CP_PARSER_FLAGS_OPTIONAL,
11390 type_specifier_seq,
11391 /*is_declaration=*/false,
11394 /* If the first type-specifier could not be found, this is not a
11395 type-specifier-seq at all. */
11396 if (!seen_type_specifier && !type_specifier)
11398 cp_parser_error (parser, "expected type-specifier");
11399 type_specifier_seq->type = error_mark_node;
11402 /* If subsequent type-specifiers could not be found, the
11403 type-specifier-seq is complete. */
11404 else if (seen_type_specifier && !type_specifier)
11407 seen_type_specifier = true;
11413 /* Parse a parameter-declaration-clause.
11415 parameter-declaration-clause:
11416 parameter-declaration-list [opt] ... [opt]
11417 parameter-declaration-list , ...
11419 Returns a representation for the parameter declarations. A return
11420 value of NULL indicates a parameter-declaration-clause consisting
11421 only of an ellipsis. */
11423 static cp_parameter_declarator *
11424 cp_parser_parameter_declaration_clause (cp_parser* parser)
11426 cp_parameter_declarator *parameters;
11431 /* Peek at the next token. */
11432 token = cp_lexer_peek_token (parser->lexer);
11433 /* Check for trivial parameter-declaration-clauses. */
11434 if (token->type == CPP_ELLIPSIS)
11436 /* Consume the `...' token. */
11437 cp_lexer_consume_token (parser->lexer);
11440 else if (token->type == CPP_CLOSE_PAREN)
11441 /* There are no parameters. */
11443 #ifndef NO_IMPLICIT_EXTERN_C
11444 if (in_system_header && current_class_type == NULL
11445 && current_lang_name == lang_name_c)
11449 return no_parameters;
11451 /* Check for `(void)', too, which is a special case. */
11452 else if (token->keyword == RID_VOID
11453 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11454 == CPP_CLOSE_PAREN))
11456 /* Consume the `void' token. */
11457 cp_lexer_consume_token (parser->lexer);
11458 /* There are no parameters. */
11459 return no_parameters;
11462 /* Parse the parameter-declaration-list. */
11463 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11464 /* If a parse error occurred while parsing the
11465 parameter-declaration-list, then the entire
11466 parameter-declaration-clause is erroneous. */
11470 /* Peek at the next token. */
11471 token = cp_lexer_peek_token (parser->lexer);
11472 /* If it's a `,', the clause should terminate with an ellipsis. */
11473 if (token->type == CPP_COMMA)
11475 /* Consume the `,'. */
11476 cp_lexer_consume_token (parser->lexer);
11477 /* Expect an ellipsis. */
11479 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11481 /* It might also be `...' if the optional trailing `,' was
11483 else if (token->type == CPP_ELLIPSIS)
11485 /* Consume the `...' token. */
11486 cp_lexer_consume_token (parser->lexer);
11487 /* And remember that we saw it. */
11491 ellipsis_p = false;
11493 /* Finish the parameter list. */
11494 if (parameters && ellipsis_p)
11495 parameters->ellipsis_p = true;
11500 /* Parse a parameter-declaration-list.
11502 parameter-declaration-list:
11503 parameter-declaration
11504 parameter-declaration-list , parameter-declaration
11506 Returns a representation of the parameter-declaration-list, as for
11507 cp_parser_parameter_declaration_clause. However, the
11508 `void_list_node' is never appended to the list. Upon return,
11509 *IS_ERROR will be true iff an error occurred. */
11511 static cp_parameter_declarator *
11512 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11514 cp_parameter_declarator *parameters = NULL;
11515 cp_parameter_declarator **tail = ¶meters;
11517 /* Assume all will go well. */
11520 /* Look for more parameters. */
11523 cp_parameter_declarator *parameter;
11524 bool parenthesized_p;
11525 /* Parse the parameter. */
11527 = cp_parser_parameter_declaration (parser,
11528 /*template_parm_p=*/false,
11531 /* If a parse error occurred parsing the parameter declaration,
11532 then the entire parameter-declaration-list is erroneous. */
11539 /* Add the new parameter to the list. */
11541 tail = ¶meter->next;
11543 /* Peek at the next token. */
11544 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11545 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11546 /* The parameter-declaration-list is complete. */
11548 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11552 /* Peek at the next token. */
11553 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11554 /* If it's an ellipsis, then the list is complete. */
11555 if (token->type == CPP_ELLIPSIS)
11557 /* Otherwise, there must be more parameters. Consume the
11559 cp_lexer_consume_token (parser->lexer);
11560 /* When parsing something like:
11562 int i(float f, double d)
11564 we can tell after seeing the declaration for "f" that we
11565 are not looking at an initialization of a variable "i",
11566 but rather at the declaration of a function "i".
11568 Due to the fact that the parsing of template arguments
11569 (as specified to a template-id) requires backtracking we
11570 cannot use this technique when inside a template argument
11572 if (!parser->in_template_argument_list_p
11573 && !parser->in_type_id_in_expr_p
11574 && cp_parser_parsing_tentatively (parser)
11575 && !cp_parser_committed_to_tentative_parse (parser)
11576 /* However, a parameter-declaration of the form
11577 "foat(f)" (which is a valid declaration of a
11578 parameter "f") can also be interpreted as an
11579 expression (the conversion of "f" to "float"). */
11580 && !parenthesized_p)
11581 cp_parser_commit_to_tentative_parse (parser);
11585 cp_parser_error (parser, "expected %<,%> or %<...%>");
11586 if (!cp_parser_parsing_tentatively (parser)
11587 || cp_parser_committed_to_tentative_parse (parser))
11588 cp_parser_skip_to_closing_parenthesis (parser,
11589 /*recovering=*/true,
11590 /*or_comma=*/false,
11591 /*consume_paren=*/false);
11599 /* Parse a parameter declaration.
11601 parameter-declaration:
11602 decl-specifier-seq declarator
11603 decl-specifier-seq declarator = assignment-expression
11604 decl-specifier-seq abstract-declarator [opt]
11605 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11607 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11608 declares a template parameter. (In that case, a non-nested `>'
11609 token encountered during the parsing of the assignment-expression
11610 is not interpreted as a greater-than operator.)
11612 Returns a representation of the parameter, or NULL if an error
11613 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11614 true iff the declarator is of the form "(p)". */
11616 static cp_parameter_declarator *
11617 cp_parser_parameter_declaration (cp_parser *parser,
11618 bool template_parm_p,
11619 bool *parenthesized_p)
11621 int declares_class_or_enum;
11622 bool greater_than_is_operator_p;
11623 cp_decl_specifier_seq decl_specifiers;
11624 cp_declarator *declarator;
11625 tree default_argument;
11627 const char *saved_message;
11629 /* In a template parameter, `>' is not an operator.
11633 When parsing a default template-argument for a non-type
11634 template-parameter, the first non-nested `>' is taken as the end
11635 of the template parameter-list rather than a greater-than
11637 greater_than_is_operator_p = !template_parm_p;
11639 /* Type definitions may not appear in parameter types. */
11640 saved_message = parser->type_definition_forbidden_message;
11641 parser->type_definition_forbidden_message
11642 = "types may not be defined in parameter types";
11644 /* Parse the declaration-specifiers. */
11645 cp_parser_decl_specifier_seq (parser,
11646 CP_PARSER_FLAGS_NONE,
11648 &declares_class_or_enum);
11649 /* If an error occurred, there's no reason to attempt to parse the
11650 rest of the declaration. */
11651 if (cp_parser_error_occurred (parser))
11653 parser->type_definition_forbidden_message = saved_message;
11657 /* Peek at the next token. */
11658 token = cp_lexer_peek_token (parser->lexer);
11659 /* If the next token is a `)', `,', `=', `>', or `...', then there
11660 is no declarator. */
11661 if (token->type == CPP_CLOSE_PAREN
11662 || token->type == CPP_COMMA
11663 || token->type == CPP_EQ
11664 || token->type == CPP_ELLIPSIS
11665 || token->type == CPP_GREATER)
11668 if (parenthesized_p)
11669 *parenthesized_p = false;
11671 /* Otherwise, there should be a declarator. */
11674 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11675 parser->default_arg_ok_p = false;
11677 /* After seeing a decl-specifier-seq, if the next token is not a
11678 "(", there is no possibility that the code is a valid
11679 expression. Therefore, if parsing tentatively, we commit at
11681 if (!parser->in_template_argument_list_p
11682 /* In an expression context, having seen:
11686 we cannot be sure whether we are looking at a
11687 function-type (taking a "char" as a parameter) or a cast
11688 of some object of type "char" to "int". */
11689 && !parser->in_type_id_in_expr_p
11690 && cp_parser_parsing_tentatively (parser)
11691 && !cp_parser_committed_to_tentative_parse (parser)
11692 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11693 cp_parser_commit_to_tentative_parse (parser);
11694 /* Parse the declarator. */
11695 declarator = cp_parser_declarator (parser,
11696 CP_PARSER_DECLARATOR_EITHER,
11697 /*ctor_dtor_or_conv_p=*/NULL,
11699 /*member_p=*/false);
11700 parser->default_arg_ok_p = saved_default_arg_ok_p;
11701 /* After the declarator, allow more attributes. */
11702 decl_specifiers.attributes
11703 = chainon (decl_specifiers.attributes,
11704 cp_parser_attributes_opt (parser));
11707 /* The restriction on defining new types applies only to the type
11708 of the parameter, not to the default argument. */
11709 parser->type_definition_forbidden_message = saved_message;
11711 /* If the next token is `=', then process a default argument. */
11712 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11714 bool saved_greater_than_is_operator_p;
11715 /* Consume the `='. */
11716 cp_lexer_consume_token (parser->lexer);
11718 /* If we are defining a class, then the tokens that make up the
11719 default argument must be saved and processed later. */
11720 if (!template_parm_p && at_class_scope_p ()
11721 && TYPE_BEING_DEFINED (current_class_type))
11723 unsigned depth = 0;
11724 cp_token *first_token;
11727 /* Add tokens until we have processed the entire default
11728 argument. We add the range [first_token, token). */
11729 first_token = cp_lexer_peek_token (parser->lexer);
11734 /* Peek at the next token. */
11735 token = cp_lexer_peek_token (parser->lexer);
11736 /* What we do depends on what token we have. */
11737 switch (token->type)
11739 /* In valid code, a default argument must be
11740 immediately followed by a `,' `)', or `...'. */
11742 case CPP_CLOSE_PAREN:
11744 /* If we run into a non-nested `;', `}', or `]',
11745 then the code is invalid -- but the default
11746 argument is certainly over. */
11747 case CPP_SEMICOLON:
11748 case CPP_CLOSE_BRACE:
11749 case CPP_CLOSE_SQUARE:
11752 /* Update DEPTH, if necessary. */
11753 else if (token->type == CPP_CLOSE_PAREN
11754 || token->type == CPP_CLOSE_BRACE
11755 || token->type == CPP_CLOSE_SQUARE)
11759 case CPP_OPEN_PAREN:
11760 case CPP_OPEN_SQUARE:
11761 case CPP_OPEN_BRACE:
11766 /* If we see a non-nested `>', and `>' is not an
11767 operator, then it marks the end of the default
11769 if (!depth && !greater_than_is_operator_p)
11773 /* If we run out of tokens, issue an error message. */
11775 error ("file ends in default argument");
11781 /* In these cases, we should look for template-ids.
11782 For example, if the default argument is
11783 `X<int, double>()', we need to do name lookup to
11784 figure out whether or not `X' is a template; if
11785 so, the `,' does not end the default argument.
11787 That is not yet done. */
11794 /* If we've reached the end, stop. */
11798 /* Add the token to the token block. */
11799 token = cp_lexer_consume_token (parser->lexer);
11802 /* Create a DEFAULT_ARG to represented the unparsed default
11804 default_argument = make_node (DEFAULT_ARG);
11805 DEFARG_TOKENS (default_argument)
11806 = cp_token_cache_new (first_token, token);
11808 /* Outside of a class definition, we can just parse the
11809 assignment-expression. */
11812 bool saved_local_variables_forbidden_p;
11814 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11816 saved_greater_than_is_operator_p
11817 = parser->greater_than_is_operator_p;
11818 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11819 /* Local variable names (and the `this' keyword) may not
11820 appear in a default argument. */
11821 saved_local_variables_forbidden_p
11822 = parser->local_variables_forbidden_p;
11823 parser->local_variables_forbidden_p = true;
11824 /* Parse the assignment-expression. */
11825 default_argument = cp_parser_assignment_expression (parser);
11826 /* Restore saved state. */
11827 parser->greater_than_is_operator_p
11828 = saved_greater_than_is_operator_p;
11829 parser->local_variables_forbidden_p
11830 = saved_local_variables_forbidden_p;
11832 if (!parser->default_arg_ok_p)
11834 if (!flag_pedantic_errors)
11835 warning ("deprecated use of default argument for parameter of non-function");
11838 error ("default arguments are only permitted for function parameters");
11839 default_argument = NULL_TREE;
11844 default_argument = NULL_TREE;
11846 return make_parameter_declarator (&decl_specifiers,
11851 /* Parse a function-body.
11854 compound_statement */
11857 cp_parser_function_body (cp_parser *parser)
11859 cp_parser_compound_statement (parser, NULL, false);
11862 /* Parse a ctor-initializer-opt followed by a function-body. Return
11863 true if a ctor-initializer was present. */
11866 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11869 bool ctor_initializer_p;
11871 /* Begin the function body. */
11872 body = begin_function_body ();
11873 /* Parse the optional ctor-initializer. */
11874 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11875 /* Parse the function-body. */
11876 cp_parser_function_body (parser);
11877 /* Finish the function body. */
11878 finish_function_body (body);
11880 return ctor_initializer_p;
11883 /* Parse an initializer.
11886 = initializer-clause
11887 ( expression-list )
11889 Returns a expression representing the initializer. If no
11890 initializer is present, NULL_TREE is returned.
11892 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11893 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11894 set to FALSE if there is no initializer present. If there is an
11895 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11896 is set to true; otherwise it is set to false. */
11899 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11900 bool* non_constant_p)
11905 /* Peek at the next token. */
11906 token = cp_lexer_peek_token (parser->lexer);
11908 /* Let our caller know whether or not this initializer was
11910 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11911 /* Assume that the initializer is constant. */
11912 *non_constant_p = false;
11914 if (token->type == CPP_EQ)
11916 /* Consume the `='. */
11917 cp_lexer_consume_token (parser->lexer);
11918 /* Parse the initializer-clause. */
11919 init = cp_parser_initializer_clause (parser, non_constant_p);
11921 else if (token->type == CPP_OPEN_PAREN)
11922 init = cp_parser_parenthesized_expression_list (parser, false,
11926 /* Anything else is an error. */
11927 cp_parser_error (parser, "expected initializer");
11928 init = error_mark_node;
11934 /* Parse an initializer-clause.
11936 initializer-clause:
11937 assignment-expression
11938 { initializer-list , [opt] }
11941 Returns an expression representing the initializer.
11943 If the `assignment-expression' production is used the value
11944 returned is simply a representation for the expression.
11946 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11947 the elements of the initializer-list (or NULL_TREE, if the last
11948 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11949 NULL_TREE. There is no way to detect whether or not the optional
11950 trailing `,' was provided. NON_CONSTANT_P is as for
11951 cp_parser_initializer. */
11954 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11958 /* If it is not a `{', then we are looking at an
11959 assignment-expression. */
11960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11963 = cp_parser_constant_expression (parser,
11964 /*allow_non_constant_p=*/true,
11966 if (!*non_constant_p)
11967 initializer = fold_non_dependent_expr (initializer);
11971 /* Consume the `{' token. */
11972 cp_lexer_consume_token (parser->lexer);
11973 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11974 initializer = make_node (CONSTRUCTOR);
11975 /* If it's not a `}', then there is a non-trivial initializer. */
11976 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11978 /* Parse the initializer list. */
11979 CONSTRUCTOR_ELTS (initializer)
11980 = cp_parser_initializer_list (parser, non_constant_p);
11981 /* A trailing `,' token is allowed. */
11982 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11983 cp_lexer_consume_token (parser->lexer);
11985 /* Now, there should be a trailing `}'. */
11986 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11989 return initializer;
11992 /* Parse an initializer-list.
11996 initializer-list , initializer-clause
12001 identifier : initializer-clause
12002 initializer-list, identifier : initializer-clause
12004 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12005 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12006 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12007 as for cp_parser_initializer. */
12010 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12012 tree initializers = NULL_TREE;
12014 /* Assume all of the expressions are constant. */
12015 *non_constant_p = false;
12017 /* Parse the rest of the list. */
12023 bool clause_non_constant_p;
12025 /* If the next token is an identifier and the following one is a
12026 colon, we are looking at the GNU designated-initializer
12028 if (cp_parser_allow_gnu_extensions_p (parser)
12029 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12030 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12032 /* Consume the identifier. */
12033 identifier = cp_lexer_consume_token (parser->lexer)->value;
12034 /* Consume the `:'. */
12035 cp_lexer_consume_token (parser->lexer);
12038 identifier = NULL_TREE;
12040 /* Parse the initializer. */
12041 initializer = cp_parser_initializer_clause (parser,
12042 &clause_non_constant_p);
12043 /* If any clause is non-constant, so is the entire initializer. */
12044 if (clause_non_constant_p)
12045 *non_constant_p = true;
12046 /* Add it to the list. */
12047 initializers = tree_cons (identifier, initializer, initializers);
12049 /* If the next token is not a comma, we have reached the end of
12051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12054 /* Peek at the next token. */
12055 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12056 /* If the next token is a `}', then we're still done. An
12057 initializer-clause can have a trailing `,' after the
12058 initializer-list and before the closing `}'. */
12059 if (token->type == CPP_CLOSE_BRACE)
12062 /* Consume the `,' token. */
12063 cp_lexer_consume_token (parser->lexer);
12066 /* The initializers were built up in reverse order, so we need to
12067 reverse them now. */
12068 return nreverse (initializers);
12071 /* Classes [gram.class] */
12073 /* Parse a class-name.
12079 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12080 to indicate that names looked up in dependent types should be
12081 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12082 keyword has been used to indicate that the name that appears next
12083 is a template. TYPE_P is true iff the next name should be treated
12084 as class-name, even if it is declared to be some other kind of name
12085 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12086 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
12087 being defined in a class-head.
12089 Returns the TYPE_DECL representing the class. */
12092 cp_parser_class_name (cp_parser *parser,
12093 bool typename_keyword_p,
12094 bool template_keyword_p,
12096 bool check_dependency_p,
12098 bool is_declaration)
12105 /* All class-names start with an identifier. */
12106 token = cp_lexer_peek_token (parser->lexer);
12107 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12109 cp_parser_error (parser, "expected class-name");
12110 return error_mark_node;
12113 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12114 to a template-id, so we save it here. */
12115 scope = parser->scope;
12116 if (scope == error_mark_node)
12117 return error_mark_node;
12119 /* Any name names a type if we're following the `typename' keyword
12120 in a qualified name where the enclosing scope is type-dependent. */
12121 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12122 && dependent_type_p (scope));
12123 /* Handle the common case (an identifier, but not a template-id)
12125 if (token->type == CPP_NAME
12126 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12130 /* Look for the identifier. */
12131 identifier = cp_parser_identifier (parser);
12132 /* If the next token isn't an identifier, we are certainly not
12133 looking at a class-name. */
12134 if (identifier == error_mark_node)
12135 decl = error_mark_node;
12136 /* If we know this is a type-name, there's no need to look it
12138 else if (typename_p)
12142 /* If the next token is a `::', then the name must be a type
12145 [basic.lookup.qual]
12147 During the lookup for a name preceding the :: scope
12148 resolution operator, object, function, and enumerator
12149 names are ignored. */
12150 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12152 /* Look up the name. */
12153 decl = cp_parser_lookup_name (parser, identifier,
12155 /*is_template=*/false,
12156 /*is_namespace=*/false,
12157 check_dependency_p,
12158 /*ambiguous_p=*/NULL);
12163 /* Try a template-id. */
12164 decl = cp_parser_template_id (parser, template_keyword_p,
12165 check_dependency_p,
12167 if (decl == error_mark_node)
12168 return error_mark_node;
12171 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12173 /* If this is a typename, create a TYPENAME_TYPE. */
12174 if (typename_p && decl != error_mark_node)
12176 decl = make_typename_type (scope, decl, /*complain=*/1);
12177 if (decl != error_mark_node)
12178 decl = TYPE_NAME (decl);
12181 /* Check to see that it is really the name of a class. */
12182 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12183 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12184 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12185 /* Situations like this:
12187 template <typename T> struct A {
12188 typename T::template X<int>::I i;
12191 are problematic. Is `T::template X<int>' a class-name? The
12192 standard does not seem to be definitive, but there is no other
12193 valid interpretation of the following `::'. Therefore, those
12194 names are considered class-names. */
12195 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12196 else if (decl == error_mark_node
12197 || TREE_CODE (decl) != TYPE_DECL
12198 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12200 cp_parser_error (parser, "expected class-name");
12201 return error_mark_node;
12207 /* Parse a class-specifier.
12210 class-head { member-specification [opt] }
12212 Returns the TREE_TYPE representing the class. */
12215 cp_parser_class_specifier (cp_parser* parser)
12219 tree attributes = NULL_TREE;
12220 int has_trailing_semicolon;
12221 bool nested_name_specifier_p;
12222 unsigned saved_num_template_parameter_lists;
12223 bool pop_p = false;
12224 tree scope = NULL_TREE;
12226 push_deferring_access_checks (dk_no_deferred);
12228 /* Parse the class-head. */
12229 type = cp_parser_class_head (parser,
12230 &nested_name_specifier_p,
12232 /* If the class-head was a semantic disaster, skip the entire body
12236 cp_parser_skip_to_end_of_block_or_statement (parser);
12237 pop_deferring_access_checks ();
12238 return error_mark_node;
12241 /* Look for the `{'. */
12242 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12244 pop_deferring_access_checks ();
12245 return error_mark_node;
12248 /* Issue an error message if type-definitions are forbidden here. */
12249 cp_parser_check_type_definition (parser);
12250 /* Remember that we are defining one more class. */
12251 ++parser->num_classes_being_defined;
12252 /* Inside the class, surrounding template-parameter-lists do not
12254 saved_num_template_parameter_lists
12255 = parser->num_template_parameter_lists;
12256 parser->num_template_parameter_lists = 0;
12258 /* Start the class. */
12259 if (nested_name_specifier_p)
12261 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12262 pop_p = push_scope (scope);
12264 type = begin_class_definition (type);
12266 if (type == error_mark_node)
12267 /* If the type is erroneous, skip the entire body of the class. */
12268 cp_parser_skip_to_closing_brace (parser);
12270 /* Parse the member-specification. */
12271 cp_parser_member_specification_opt (parser);
12273 /* Look for the trailing `}'. */
12274 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12275 /* We get better error messages by noticing a common problem: a
12276 missing trailing `;'. */
12277 token = cp_lexer_peek_token (parser->lexer);
12278 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12279 /* Look for trailing attributes to apply to this class. */
12280 if (cp_parser_allow_gnu_extensions_p (parser))
12282 tree sub_attr = cp_parser_attributes_opt (parser);
12283 attributes = chainon (attributes, sub_attr);
12285 if (type != error_mark_node)
12286 type = finish_struct (type, attributes);
12289 /* If this class is not itself within the scope of another class,
12290 then we need to parse the bodies of all of the queued function
12291 definitions. Note that the queued functions defined in a class
12292 are not always processed immediately following the
12293 class-specifier for that class. Consider:
12296 struct B { void f() { sizeof (A); } };
12299 If `f' were processed before the processing of `A' were
12300 completed, there would be no way to compute the size of `A'.
12301 Note that the nesting we are interested in here is lexical --
12302 not the semantic nesting given by TYPE_CONTEXT. In particular,
12305 struct A { struct B; };
12306 struct A::B { void f() { } };
12308 there is no need to delay the parsing of `A::B::f'. */
12309 if (--parser->num_classes_being_defined == 0)
12316 /* In a first pass, parse default arguments to the functions.
12317 Then, in a second pass, parse the bodies of the functions.
12318 This two-phased approach handles cases like:
12326 class_type = NULL_TREE;
12328 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12329 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12330 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12331 TREE_PURPOSE (parser->unparsed_functions_queues)
12332 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12334 fn = TREE_VALUE (queue_entry);
12335 /* If there are default arguments that have not yet been processed,
12336 take care of them now. */
12337 if (class_type != TREE_PURPOSE (queue_entry))
12340 pop_scope (class_type);
12341 class_type = TREE_PURPOSE (queue_entry);
12342 pop_p = push_scope (class_type);
12344 /* Make sure that any template parameters are in scope. */
12345 maybe_begin_member_template_processing (fn);
12346 /* Parse the default argument expressions. */
12347 cp_parser_late_parsing_default_args (parser, fn);
12348 /* Remove any template parameters from the symbol table. */
12349 maybe_end_member_template_processing ();
12352 pop_scope (class_type);
12353 /* Now parse the body of the functions. */
12354 for (TREE_VALUE (parser->unparsed_functions_queues)
12355 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12356 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12357 TREE_VALUE (parser->unparsed_functions_queues)
12358 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12360 /* Figure out which function we need to process. */
12361 fn = TREE_VALUE (queue_entry);
12363 /* A hack to prevent garbage collection. */
12366 /* Parse the function. */
12367 cp_parser_late_parsing_for_member (parser, fn);
12372 /* Put back any saved access checks. */
12373 pop_deferring_access_checks ();
12375 /* Restore the count of active template-parameter-lists. */
12376 parser->num_template_parameter_lists
12377 = saved_num_template_parameter_lists;
12382 /* Parse a class-head.
12385 class-key identifier [opt] base-clause [opt]
12386 class-key nested-name-specifier identifier base-clause [opt]
12387 class-key nested-name-specifier [opt] template-id
12391 class-key attributes identifier [opt] base-clause [opt]
12392 class-key attributes nested-name-specifier identifier base-clause [opt]
12393 class-key attributes nested-name-specifier [opt] template-id
12396 Returns the TYPE of the indicated class. Sets
12397 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12398 involving a nested-name-specifier was used, and FALSE otherwise.
12400 Returns NULL_TREE if the class-head is syntactically valid, but
12401 semantically invalid in a way that means we should skip the entire
12402 body of the class. */
12405 cp_parser_class_head (cp_parser* parser,
12406 bool* nested_name_specifier_p,
12407 tree *attributes_p)
12409 tree nested_name_specifier;
12410 enum tag_types class_key;
12411 tree id = NULL_TREE;
12412 tree type = NULL_TREE;
12414 bool template_id_p = false;
12415 bool qualified_p = false;
12416 bool invalid_nested_name_p = false;
12417 bool invalid_explicit_specialization_p = false;
12418 bool pop_p = false;
12419 unsigned num_templates;
12422 /* Assume no nested-name-specifier will be present. */
12423 *nested_name_specifier_p = false;
12424 /* Assume no template parameter lists will be used in defining the
12428 /* Look for the class-key. */
12429 class_key = cp_parser_class_key (parser);
12430 if (class_key == none_type)
12431 return error_mark_node;
12433 /* Parse the attributes. */
12434 attributes = cp_parser_attributes_opt (parser);
12436 /* If the next token is `::', that is invalid -- but sometimes
12437 people do try to write:
12441 Handle this gracefully by accepting the extra qualifier, and then
12442 issuing an error about it later if this really is a
12443 class-head. If it turns out just to be an elaborated type
12444 specifier, remain silent. */
12445 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12446 qualified_p = true;
12448 push_deferring_access_checks (dk_no_check);
12450 /* Determine the name of the class. Begin by looking for an
12451 optional nested-name-specifier. */
12452 nested_name_specifier
12453 = cp_parser_nested_name_specifier_opt (parser,
12454 /*typename_keyword_p=*/false,
12455 /*check_dependency_p=*/false,
12457 /*is_declaration=*/false);
12458 /* If there was a nested-name-specifier, then there *must* be an
12460 if (nested_name_specifier)
12462 /* Although the grammar says `identifier', it really means
12463 `class-name' or `template-name'. You are only allowed to
12464 define a class that has already been declared with this
12467 The proposed resolution for Core Issue 180 says that whever
12468 you see `class T::X' you should treat `X' as a type-name.
12470 It is OK to define an inaccessible class; for example:
12472 class A { class B; };
12475 We do not know if we will see a class-name, or a
12476 template-name. We look for a class-name first, in case the
12477 class-name is a template-id; if we looked for the
12478 template-name first we would stop after the template-name. */
12479 cp_parser_parse_tentatively (parser);
12480 type = cp_parser_class_name (parser,
12481 /*typename_keyword_p=*/false,
12482 /*template_keyword_p=*/false,
12484 /*check_dependency_p=*/false,
12485 /*class_head_p=*/true,
12486 /*is_declaration=*/false);
12487 /* If that didn't work, ignore the nested-name-specifier. */
12488 if (!cp_parser_parse_definitely (parser))
12490 invalid_nested_name_p = true;
12491 id = cp_parser_identifier (parser);
12492 if (id == error_mark_node)
12495 /* If we could not find a corresponding TYPE, treat this
12496 declaration like an unqualified declaration. */
12497 if (type == error_mark_node)
12498 nested_name_specifier = NULL_TREE;
12499 /* Otherwise, count the number of templates used in TYPE and its
12500 containing scopes. */
12505 for (scope = TREE_TYPE (type);
12506 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12507 scope = (TYPE_P (scope)
12508 ? TYPE_CONTEXT (scope)
12509 : DECL_CONTEXT (scope)))
12511 && CLASS_TYPE_P (scope)
12512 && CLASSTYPE_TEMPLATE_INFO (scope)
12513 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12514 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12518 /* Otherwise, the identifier is optional. */
12521 /* We don't know whether what comes next is a template-id,
12522 an identifier, or nothing at all. */
12523 cp_parser_parse_tentatively (parser);
12524 /* Check for a template-id. */
12525 id = cp_parser_template_id (parser,
12526 /*template_keyword_p=*/false,
12527 /*check_dependency_p=*/true,
12528 /*is_declaration=*/true);
12529 /* If that didn't work, it could still be an identifier. */
12530 if (!cp_parser_parse_definitely (parser))
12532 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12533 id = cp_parser_identifier (parser);
12539 template_id_p = true;
12544 pop_deferring_access_checks ();
12547 cp_parser_check_for_invalid_template_id (parser, id);
12549 /* If it's not a `:' or a `{' then we can't really be looking at a
12550 class-head, since a class-head only appears as part of a
12551 class-specifier. We have to detect this situation before calling
12552 xref_tag, since that has irreversible side-effects. */
12553 if (!cp_parser_next_token_starts_class_definition_p (parser))
12555 cp_parser_error (parser, "expected %<{%> or %<:%>");
12556 return error_mark_node;
12559 /* At this point, we're going ahead with the class-specifier, even
12560 if some other problem occurs. */
12561 cp_parser_commit_to_tentative_parse (parser);
12562 /* Issue the error about the overly-qualified name now. */
12564 cp_parser_error (parser,
12565 "global qualification of class name is invalid");
12566 else if (invalid_nested_name_p)
12567 cp_parser_error (parser,
12568 "qualified name does not name a class");
12569 else if (nested_name_specifier)
12572 /* Figure out in what scope the declaration is being placed. */
12573 scope = current_scope ();
12574 /* If that scope does not contain the scope in which the
12575 class was originally declared, the program is invalid. */
12576 if (scope && !is_ancestor (scope, nested_name_specifier))
12578 error ("declaration of %qD in %qD which does not enclose %qD",
12579 type, scope, nested_name_specifier);
12585 A declarator-id shall not be qualified exception of the
12586 definition of a ... nested class outside of its class
12587 ... [or] a the definition or explicit instantiation of a
12588 class member of a namespace outside of its namespace. */
12589 if (scope == nested_name_specifier)
12591 pedwarn ("extra qualification ignored");
12592 nested_name_specifier = NULL_TREE;
12596 /* An explicit-specialization must be preceded by "template <>". If
12597 it is not, try to recover gracefully. */
12598 if (at_namespace_scope_p ()
12599 && parser->num_template_parameter_lists == 0
12602 error ("an explicit specialization must be preceded by %<template <>%>");
12603 invalid_explicit_specialization_p = true;
12604 /* Take the same action that would have been taken by
12605 cp_parser_explicit_specialization. */
12606 ++parser->num_template_parameter_lists;
12607 begin_specialization ();
12609 /* There must be no "return" statements between this point and the
12610 end of this function; set "type "to the correct return value and
12611 use "goto done;" to return. */
12612 /* Make sure that the right number of template parameters were
12614 if (!cp_parser_check_template_parameters (parser, num_templates))
12616 /* If something went wrong, there is no point in even trying to
12617 process the class-definition. */
12622 /* Look up the type. */
12625 type = TREE_TYPE (id);
12626 maybe_process_partial_specialization (type);
12628 else if (!nested_name_specifier)
12630 /* If the class was unnamed, create a dummy name. */
12632 id = make_anon_name ();
12633 type = xref_tag (class_key, id, /*globalize=*/false,
12634 parser->num_template_parameter_lists);
12639 bool pop_p = false;
12643 template <typename T> struct S { struct T };
12644 template <typename T> struct S<T>::T { };
12646 we will get a TYPENAME_TYPE when processing the definition of
12647 `S::T'. We need to resolve it to the actual type before we
12648 try to define it. */
12649 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12651 class_type = resolve_typename_type (TREE_TYPE (type),
12652 /*only_current_p=*/false);
12653 if (class_type != error_mark_node)
12654 type = TYPE_NAME (class_type);
12657 cp_parser_error (parser, "could not resolve typename type");
12658 type = error_mark_node;
12662 maybe_process_partial_specialization (TREE_TYPE (type));
12663 class_type = current_class_type;
12664 /* Enter the scope indicated by the nested-name-specifier. */
12665 if (nested_name_specifier)
12666 pop_p = push_scope (nested_name_specifier);
12667 /* Get the canonical version of this type. */
12668 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12669 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12670 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12671 type = push_template_decl (type);
12672 type = TREE_TYPE (type);
12673 if (nested_name_specifier)
12675 *nested_name_specifier_p = true;
12677 pop_scope (nested_name_specifier);
12680 /* Indicate whether this class was declared as a `class' or as a
12682 if (TREE_CODE (type) == RECORD_TYPE)
12683 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12684 cp_parser_check_class_key (class_key, type);
12686 /* Enter the scope containing the class; the names of base classes
12687 should be looked up in that context. For example, given:
12689 struct A { struct B {}; struct C; };
12690 struct A::C : B {};
12693 if (nested_name_specifier)
12694 pop_p = push_scope (nested_name_specifier);
12698 /* Get the list of base-classes, if there is one. */
12699 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12700 bases = cp_parser_base_clause (parser);
12702 /* Process the base classes. */
12703 xref_basetypes (type, bases);
12705 /* Leave the scope given by the nested-name-specifier. We will
12706 enter the class scope itself while processing the members. */
12708 pop_scope (nested_name_specifier);
12711 if (invalid_explicit_specialization_p)
12713 end_specialization ();
12714 --parser->num_template_parameter_lists;
12716 *attributes_p = attributes;
12720 /* Parse a class-key.
12727 Returns the kind of class-key specified, or none_type to indicate
12730 static enum tag_types
12731 cp_parser_class_key (cp_parser* parser)
12734 enum tag_types tag_type;
12736 /* Look for the class-key. */
12737 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12741 /* Check to see if the TOKEN is a class-key. */
12742 tag_type = cp_parser_token_is_class_key (token);
12744 cp_parser_error (parser, "expected class-key");
12748 /* Parse an (optional) member-specification.
12750 member-specification:
12751 member-declaration member-specification [opt]
12752 access-specifier : member-specification [opt] */
12755 cp_parser_member_specification_opt (cp_parser* parser)
12762 /* Peek at the next token. */
12763 token = cp_lexer_peek_token (parser->lexer);
12764 /* If it's a `}', or EOF then we've seen all the members. */
12765 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12768 /* See if this token is a keyword. */
12769 keyword = token->keyword;
12773 case RID_PROTECTED:
12775 /* Consume the access-specifier. */
12776 cp_lexer_consume_token (parser->lexer);
12777 /* Remember which access-specifier is active. */
12778 current_access_specifier = token->value;
12779 /* Look for the `:'. */
12780 cp_parser_require (parser, CPP_COLON, "`:'");
12784 /* Accept #pragmas at class scope. */
12785 if (token->type == CPP_PRAGMA)
12787 cp_lexer_handle_pragma (parser->lexer);
12791 /* Otherwise, the next construction must be a
12792 member-declaration. */
12793 cp_parser_member_declaration (parser);
12798 /* Parse a member-declaration.
12800 member-declaration:
12801 decl-specifier-seq [opt] member-declarator-list [opt] ;
12802 function-definition ; [opt]
12803 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12805 template-declaration
12807 member-declarator-list:
12809 member-declarator-list , member-declarator
12812 declarator pure-specifier [opt]
12813 declarator constant-initializer [opt]
12814 identifier [opt] : constant-expression
12818 member-declaration:
12819 __extension__ member-declaration
12822 declarator attributes [opt] pure-specifier [opt]
12823 declarator attributes [opt] constant-initializer [opt]
12824 identifier [opt] attributes [opt] : constant-expression */
12827 cp_parser_member_declaration (cp_parser* parser)
12829 cp_decl_specifier_seq decl_specifiers;
12830 tree prefix_attributes;
12832 int declares_class_or_enum;
12835 int saved_pedantic;
12837 /* Check for the `__extension__' keyword. */
12838 if (cp_parser_extension_opt (parser, &saved_pedantic))
12841 cp_parser_member_declaration (parser);
12842 /* Restore the old value of the PEDANTIC flag. */
12843 pedantic = saved_pedantic;
12848 /* Check for a template-declaration. */
12849 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12851 /* Parse the template-declaration. */
12852 cp_parser_template_declaration (parser, /*member_p=*/true);
12857 /* Check for a using-declaration. */
12858 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12860 /* Parse the using-declaration. */
12861 cp_parser_using_declaration (parser);
12866 /* Parse the decl-specifier-seq. */
12867 cp_parser_decl_specifier_seq (parser,
12868 CP_PARSER_FLAGS_OPTIONAL,
12870 &declares_class_or_enum);
12871 prefix_attributes = decl_specifiers.attributes;
12872 decl_specifiers.attributes = NULL_TREE;
12873 /* Check for an invalid type-name. */
12874 if (!decl_specifiers.type
12875 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12877 /* If there is no declarator, then the decl-specifier-seq should
12879 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12881 /* If there was no decl-specifier-seq, and the next token is a
12882 `;', then we have something like:
12888 Each member-declaration shall declare at least one member
12889 name of the class. */
12890 if (!decl_specifiers.any_specifiers_p)
12892 cp_token *token = cp_lexer_peek_token (parser->lexer);
12893 if (pedantic && !token->in_system_header)
12894 pedwarn ("%Hextra %<;%>", &token->location);
12900 /* See if this declaration is a friend. */
12901 friend_p = cp_parser_friend_p (&decl_specifiers);
12902 /* If there were decl-specifiers, check to see if there was
12903 a class-declaration. */
12904 type = check_tag_decl (&decl_specifiers);
12905 /* Nested classes have already been added to the class, but
12906 a `friend' needs to be explicitly registered. */
12909 /* If the `friend' keyword was present, the friend must
12910 be introduced with a class-key. */
12911 if (!declares_class_or_enum)
12912 error ("a class-key must be used when declaring a friend");
12915 template <typename T> struct A {
12916 friend struct A<T>::B;
12919 A<T>::B will be represented by a TYPENAME_TYPE, and
12920 therefore not recognized by check_tag_decl. */
12922 && decl_specifiers.type
12923 && TYPE_P (decl_specifiers.type))
12924 type = decl_specifiers.type;
12925 if (!type || !TYPE_P (type))
12926 error ("friend declaration does not name a class or "
12929 make_friend_class (current_class_type, type,
12930 /*complain=*/true);
12932 /* If there is no TYPE, an error message will already have
12934 else if (!type || type == error_mark_node)
12936 /* An anonymous aggregate has to be handled specially; such
12937 a declaration really declares a data member (with a
12938 particular type), as opposed to a nested class. */
12939 else if (ANON_AGGR_TYPE_P (type))
12941 /* Remove constructors and such from TYPE, now that we
12942 know it is an anonymous aggregate. */
12943 fixup_anonymous_aggr (type);
12944 /* And make the corresponding data member. */
12945 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12946 /* Add it to the class. */
12947 finish_member_declaration (decl);
12950 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12955 /* See if these declarations will be friends. */
12956 friend_p = cp_parser_friend_p (&decl_specifiers);
12958 /* Keep going until we hit the `;' at the end of the
12960 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12962 tree attributes = NULL_TREE;
12963 tree first_attribute;
12965 /* Peek at the next token. */
12966 token = cp_lexer_peek_token (parser->lexer);
12968 /* Check for a bitfield declaration. */
12969 if (token->type == CPP_COLON
12970 || (token->type == CPP_NAME
12971 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12977 /* Get the name of the bitfield. Note that we cannot just
12978 check TOKEN here because it may have been invalidated by
12979 the call to cp_lexer_peek_nth_token above. */
12980 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12981 identifier = cp_parser_identifier (parser);
12983 identifier = NULL_TREE;
12985 /* Consume the `:' token. */
12986 cp_lexer_consume_token (parser->lexer);
12987 /* Get the width of the bitfield. */
12989 = cp_parser_constant_expression (parser,
12990 /*allow_non_constant=*/false,
12993 /* Look for attributes that apply to the bitfield. */
12994 attributes = cp_parser_attributes_opt (parser);
12995 /* Remember which attributes are prefix attributes and
12997 first_attribute = attributes;
12998 /* Combine the attributes. */
12999 attributes = chainon (prefix_attributes, attributes);
13001 /* Create the bitfield declaration. */
13002 decl = grokbitfield (identifier
13003 ? make_id_declarator (identifier)
13007 /* Apply the attributes. */
13008 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13012 cp_declarator *declarator;
13014 tree asm_specification;
13015 int ctor_dtor_or_conv_p;
13017 /* Parse the declarator. */
13019 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13020 &ctor_dtor_or_conv_p,
13021 /*parenthesized_p=*/NULL,
13022 /*member_p=*/true);
13024 /* If something went wrong parsing the declarator, make sure
13025 that we at least consume some tokens. */
13026 if (declarator == cp_error_declarator)
13028 /* Skip to the end of the statement. */
13029 cp_parser_skip_to_end_of_statement (parser);
13030 /* If the next token is not a semicolon, that is
13031 probably because we just skipped over the body of
13032 a function. So, we consume a semicolon if
13033 present, but do not issue an error message if it
13035 if (cp_lexer_next_token_is (parser->lexer,
13037 cp_lexer_consume_token (parser->lexer);
13041 cp_parser_check_for_definition_in_return_type
13042 (declarator, declares_class_or_enum);
13044 /* Look for an asm-specification. */
13045 asm_specification = cp_parser_asm_specification_opt (parser);
13046 /* Look for attributes that apply to the declaration. */
13047 attributes = cp_parser_attributes_opt (parser);
13048 /* Remember which attributes are prefix attributes and
13050 first_attribute = attributes;
13051 /* Combine the attributes. */
13052 attributes = chainon (prefix_attributes, attributes);
13054 /* If it's an `=', then we have a constant-initializer or a
13055 pure-specifier. It is not correct to parse the
13056 initializer before registering the member declaration
13057 since the member declaration should be in scope while
13058 its initializer is processed. However, the rest of the
13059 front end does not yet provide an interface that allows
13060 us to handle this correctly. */
13061 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13065 A pure-specifier shall be used only in the declaration of
13066 a virtual function.
13068 A member-declarator can contain a constant-initializer
13069 only if it declares a static member of integral or
13072 Therefore, if the DECLARATOR is for a function, we look
13073 for a pure-specifier; otherwise, we look for a
13074 constant-initializer. When we call `grokfield', it will
13075 perform more stringent semantics checks. */
13076 if (declarator->kind == cdk_function)
13077 initializer = cp_parser_pure_specifier (parser);
13079 /* Parse the initializer. */
13080 initializer = cp_parser_constant_initializer (parser);
13082 /* Otherwise, there is no initializer. */
13084 initializer = NULL_TREE;
13086 /* See if we are probably looking at a function
13087 definition. We are certainly not looking at at a
13088 member-declarator. Calling `grokfield' has
13089 side-effects, so we must not do it unless we are sure
13090 that we are looking at a member-declarator. */
13091 if (cp_parser_token_starts_function_definition_p
13092 (cp_lexer_peek_token (parser->lexer)))
13094 /* The grammar does not allow a pure-specifier to be
13095 used when a member function is defined. (It is
13096 possible that this fact is an oversight in the
13097 standard, since a pure function may be defined
13098 outside of the class-specifier. */
13100 error ("pure-specifier on function-definition");
13101 decl = cp_parser_save_member_function_body (parser,
13105 /* If the member was not a friend, declare it here. */
13107 finish_member_declaration (decl);
13108 /* Peek at the next token. */
13109 token = cp_lexer_peek_token (parser->lexer);
13110 /* If the next token is a semicolon, consume it. */
13111 if (token->type == CPP_SEMICOLON)
13112 cp_lexer_consume_token (parser->lexer);
13117 /* Create the declaration. */
13118 decl = grokfield (declarator, &decl_specifiers,
13119 initializer, asm_specification,
13121 /* Any initialization must have been from a
13122 constant-expression. */
13123 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13124 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13128 /* Reset PREFIX_ATTRIBUTES. */
13129 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13130 attributes = TREE_CHAIN (attributes);
13132 TREE_CHAIN (attributes) = NULL_TREE;
13134 /* If there is any qualification still in effect, clear it
13135 now; we will be starting fresh with the next declarator. */
13136 parser->scope = NULL_TREE;
13137 parser->qualifying_scope = NULL_TREE;
13138 parser->object_scope = NULL_TREE;
13139 /* If it's a `,', then there are more declarators. */
13140 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13141 cp_lexer_consume_token (parser->lexer);
13142 /* If the next token isn't a `;', then we have a parse error. */
13143 else if (cp_lexer_next_token_is_not (parser->lexer,
13146 cp_parser_error (parser, "expected %<;%>");
13147 /* Skip tokens until we find a `;'. */
13148 cp_parser_skip_to_end_of_statement (parser);
13155 /* Add DECL to the list of members. */
13157 finish_member_declaration (decl);
13159 if (TREE_CODE (decl) == FUNCTION_DECL)
13160 cp_parser_save_default_args (parser, decl);
13165 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13168 /* Parse a pure-specifier.
13173 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13174 Otherwise, ERROR_MARK_NODE is returned. */
13177 cp_parser_pure_specifier (cp_parser* parser)
13181 /* Look for the `=' token. */
13182 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13183 return error_mark_node;
13184 /* Look for the `0' token. */
13185 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13186 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13187 to get information from the lexer about how the number was
13188 spelled in order to fix this problem. */
13189 if (!token || !integer_zerop (token->value))
13190 return error_mark_node;
13192 return integer_zero_node;
13195 /* Parse a constant-initializer.
13197 constant-initializer:
13198 = constant-expression
13200 Returns a representation of the constant-expression. */
13203 cp_parser_constant_initializer (cp_parser* parser)
13205 /* Look for the `=' token. */
13206 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13207 return error_mark_node;
13209 /* It is invalid to write:
13211 struct S { static const int i = { 7 }; };
13214 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13216 cp_parser_error (parser,
13217 "a brace-enclosed initializer is not allowed here");
13218 /* Consume the opening brace. */
13219 cp_lexer_consume_token (parser->lexer);
13220 /* Skip the initializer. */
13221 cp_parser_skip_to_closing_brace (parser);
13222 /* Look for the trailing `}'. */
13223 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13225 return error_mark_node;
13228 return cp_parser_constant_expression (parser,
13229 /*allow_non_constant=*/false,
13233 /* Derived classes [gram.class.derived] */
13235 /* Parse a base-clause.
13238 : base-specifier-list
13240 base-specifier-list:
13242 base-specifier-list , base-specifier
13244 Returns a TREE_LIST representing the base-classes, in the order in
13245 which they were declared. The representation of each node is as
13246 described by cp_parser_base_specifier.
13248 In the case that no bases are specified, this function will return
13249 NULL_TREE, not ERROR_MARK_NODE. */
13252 cp_parser_base_clause (cp_parser* parser)
13254 tree bases = NULL_TREE;
13256 /* Look for the `:' that begins the list. */
13257 cp_parser_require (parser, CPP_COLON, "`:'");
13259 /* Scan the base-specifier-list. */
13265 /* Look for the base-specifier. */
13266 base = cp_parser_base_specifier (parser);
13267 /* Add BASE to the front of the list. */
13268 if (base != error_mark_node)
13270 TREE_CHAIN (base) = bases;
13273 /* Peek at the next token. */
13274 token = cp_lexer_peek_token (parser->lexer);
13275 /* If it's not a comma, then the list is complete. */
13276 if (token->type != CPP_COMMA)
13278 /* Consume the `,'. */
13279 cp_lexer_consume_token (parser->lexer);
13282 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13283 base class had a qualified name. However, the next name that
13284 appears is certainly not qualified. */
13285 parser->scope = NULL_TREE;
13286 parser->qualifying_scope = NULL_TREE;
13287 parser->object_scope = NULL_TREE;
13289 return nreverse (bases);
13292 /* Parse a base-specifier.
13295 :: [opt] nested-name-specifier [opt] class-name
13296 virtual access-specifier [opt] :: [opt] nested-name-specifier
13298 access-specifier virtual [opt] :: [opt] nested-name-specifier
13301 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13302 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13303 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13304 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13307 cp_parser_base_specifier (cp_parser* parser)
13311 bool virtual_p = false;
13312 bool duplicate_virtual_error_issued_p = false;
13313 bool duplicate_access_error_issued_p = false;
13314 bool class_scope_p, template_p;
13315 tree access = access_default_node;
13318 /* Process the optional `virtual' and `access-specifier'. */
13321 /* Peek at the next token. */
13322 token = cp_lexer_peek_token (parser->lexer);
13323 /* Process `virtual'. */
13324 switch (token->keyword)
13327 /* If `virtual' appears more than once, issue an error. */
13328 if (virtual_p && !duplicate_virtual_error_issued_p)
13330 cp_parser_error (parser,
13331 "%<virtual%> specified more than once in base-specified");
13332 duplicate_virtual_error_issued_p = true;
13337 /* Consume the `virtual' token. */
13338 cp_lexer_consume_token (parser->lexer);
13343 case RID_PROTECTED:
13345 /* If more than one access specifier appears, issue an
13347 if (access != access_default_node
13348 && !duplicate_access_error_issued_p)
13350 cp_parser_error (parser,
13351 "more than one access specifier in base-specified");
13352 duplicate_access_error_issued_p = true;
13355 access = ridpointers[(int) token->keyword];
13357 /* Consume the access-specifier. */
13358 cp_lexer_consume_token (parser->lexer);
13367 /* It is not uncommon to see programs mechanically, erroneously, use
13368 the 'typename' keyword to denote (dependent) qualified types
13369 as base classes. */
13370 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13372 if (!processing_template_decl)
13373 error ("keyword %<typename%> not allowed outside of templates");
13375 error ("keyword %<typename%> not allowed in this context "
13376 "(the base class is implicitly a type)");
13377 cp_lexer_consume_token (parser->lexer);
13380 /* Look for the optional `::' operator. */
13381 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13382 /* Look for the nested-name-specifier. The simplest way to
13387 The keyword `typename' is not permitted in a base-specifier or
13388 mem-initializer; in these contexts a qualified name that
13389 depends on a template-parameter is implicitly assumed to be a
13392 is to pretend that we have seen the `typename' keyword at this
13394 cp_parser_nested_name_specifier_opt (parser,
13395 /*typename_keyword_p=*/true,
13396 /*check_dependency_p=*/true,
13398 /*is_declaration=*/true);
13399 /* If the base class is given by a qualified name, assume that names
13400 we see are type names or templates, as appropriate. */
13401 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13402 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13404 /* Finally, look for the class-name. */
13405 type = cp_parser_class_name (parser,
13409 /*check_dependency_p=*/true,
13410 /*class_head_p=*/false,
13411 /*is_declaration=*/true);
13413 if (type == error_mark_node)
13414 return error_mark_node;
13416 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13419 /* Exception handling [gram.exception] */
13421 /* Parse an (optional) exception-specification.
13423 exception-specification:
13424 throw ( type-id-list [opt] )
13426 Returns a TREE_LIST representing the exception-specification. The
13427 TREE_VALUE of each node is a type. */
13430 cp_parser_exception_specification_opt (cp_parser* parser)
13435 /* Peek at the next token. */
13436 token = cp_lexer_peek_token (parser->lexer);
13437 /* If it's not `throw', then there's no exception-specification. */
13438 if (!cp_parser_is_keyword (token, RID_THROW))
13441 /* Consume the `throw'. */
13442 cp_lexer_consume_token (parser->lexer);
13444 /* Look for the `('. */
13445 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13447 /* Peek at the next token. */
13448 token = cp_lexer_peek_token (parser->lexer);
13449 /* If it's not a `)', then there is a type-id-list. */
13450 if (token->type != CPP_CLOSE_PAREN)
13452 const char *saved_message;
13454 /* Types may not be defined in an exception-specification. */
13455 saved_message = parser->type_definition_forbidden_message;
13456 parser->type_definition_forbidden_message
13457 = "types may not be defined in an exception-specification";
13458 /* Parse the type-id-list. */
13459 type_id_list = cp_parser_type_id_list (parser);
13460 /* Restore the saved message. */
13461 parser->type_definition_forbidden_message = saved_message;
13464 type_id_list = empty_except_spec;
13466 /* Look for the `)'. */
13467 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13469 return type_id_list;
13472 /* Parse an (optional) type-id-list.
13476 type-id-list , type-id
13478 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13479 in the order that the types were presented. */
13482 cp_parser_type_id_list (cp_parser* parser)
13484 tree types = NULL_TREE;
13491 /* Get the next type-id. */
13492 type = cp_parser_type_id (parser);
13493 /* Add it to the list. */
13494 types = add_exception_specifier (types, type, /*complain=*/1);
13495 /* Peek at the next token. */
13496 token = cp_lexer_peek_token (parser->lexer);
13497 /* If it is not a `,', we are done. */
13498 if (token->type != CPP_COMMA)
13500 /* Consume the `,'. */
13501 cp_lexer_consume_token (parser->lexer);
13504 return nreverse (types);
13507 /* Parse a try-block.
13510 try compound-statement handler-seq */
13513 cp_parser_try_block (cp_parser* parser)
13517 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13518 try_block = begin_try_block ();
13519 cp_parser_compound_statement (parser, NULL, true);
13520 finish_try_block (try_block);
13521 cp_parser_handler_seq (parser);
13522 finish_handler_sequence (try_block);
13527 /* Parse a function-try-block.
13529 function-try-block:
13530 try ctor-initializer [opt] function-body handler-seq */
13533 cp_parser_function_try_block (cp_parser* parser)
13536 bool ctor_initializer_p;
13538 /* Look for the `try' keyword. */
13539 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13541 /* Let the rest of the front-end know where we are. */
13542 try_block = begin_function_try_block ();
13543 /* Parse the function-body. */
13545 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13546 /* We're done with the `try' part. */
13547 finish_function_try_block (try_block);
13548 /* Parse the handlers. */
13549 cp_parser_handler_seq (parser);
13550 /* We're done with the handlers. */
13551 finish_function_handler_sequence (try_block);
13553 return ctor_initializer_p;
13556 /* Parse a handler-seq.
13559 handler handler-seq [opt] */
13562 cp_parser_handler_seq (cp_parser* parser)
13568 /* Parse the handler. */
13569 cp_parser_handler (parser);
13570 /* Peek at the next token. */
13571 token = cp_lexer_peek_token (parser->lexer);
13572 /* If it's not `catch' then there are no more handlers. */
13573 if (!cp_parser_is_keyword (token, RID_CATCH))
13578 /* Parse a handler.
13581 catch ( exception-declaration ) compound-statement */
13584 cp_parser_handler (cp_parser* parser)
13589 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13590 handler = begin_handler ();
13591 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13592 declaration = cp_parser_exception_declaration (parser);
13593 finish_handler_parms (declaration, handler);
13594 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13595 cp_parser_compound_statement (parser, NULL, false);
13596 finish_handler (handler);
13599 /* Parse an exception-declaration.
13601 exception-declaration:
13602 type-specifier-seq declarator
13603 type-specifier-seq abstract-declarator
13607 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13608 ellipsis variant is used. */
13611 cp_parser_exception_declaration (cp_parser* parser)
13614 cp_decl_specifier_seq type_specifiers;
13615 cp_declarator *declarator;
13616 const char *saved_message;
13618 /* If it's an ellipsis, it's easy to handle. */
13619 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13621 /* Consume the `...' token. */
13622 cp_lexer_consume_token (parser->lexer);
13626 /* Types may not be defined in exception-declarations. */
13627 saved_message = parser->type_definition_forbidden_message;
13628 parser->type_definition_forbidden_message
13629 = "types may not be defined in exception-declarations";
13631 /* Parse the type-specifier-seq. */
13632 cp_parser_type_specifier_seq (parser, &type_specifiers);
13633 /* If it's a `)', then there is no declarator. */
13634 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13637 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13638 /*ctor_dtor_or_conv_p=*/NULL,
13639 /*parenthesized_p=*/NULL,
13640 /*member_p=*/false);
13642 /* Restore the saved message. */
13643 parser->type_definition_forbidden_message = saved_message;
13645 if (type_specifiers.any_specifiers_p)
13647 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13648 if (decl == NULL_TREE)
13649 error ("invalid catch parameter");
13657 /* Parse a throw-expression.
13660 throw assignment-expression [opt]
13662 Returns a THROW_EXPR representing the throw-expression. */
13665 cp_parser_throw_expression (cp_parser* parser)
13670 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13671 token = cp_lexer_peek_token (parser->lexer);
13672 /* Figure out whether or not there is an assignment-expression
13673 following the "throw" keyword. */
13674 if (token->type == CPP_COMMA
13675 || token->type == CPP_SEMICOLON
13676 || token->type == CPP_CLOSE_PAREN
13677 || token->type == CPP_CLOSE_SQUARE
13678 || token->type == CPP_CLOSE_BRACE
13679 || token->type == CPP_COLON)
13680 expression = NULL_TREE;
13682 expression = cp_parser_assignment_expression (parser);
13684 return build_throw (expression);
13687 /* GNU Extensions */
13689 /* Parse an (optional) asm-specification.
13692 asm ( string-literal )
13694 If the asm-specification is present, returns a STRING_CST
13695 corresponding to the string-literal. Otherwise, returns
13699 cp_parser_asm_specification_opt (cp_parser* parser)
13702 tree asm_specification;
13704 /* Peek at the next token. */
13705 token = cp_lexer_peek_token (parser->lexer);
13706 /* If the next token isn't the `asm' keyword, then there's no
13707 asm-specification. */
13708 if (!cp_parser_is_keyword (token, RID_ASM))
13711 /* Consume the `asm' token. */
13712 cp_lexer_consume_token (parser->lexer);
13713 /* Look for the `('. */
13714 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13716 /* Look for the string-literal. */
13717 asm_specification = cp_parser_string_literal (parser, false, false);
13719 /* Look for the `)'. */
13720 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13722 return asm_specification;
13725 /* Parse an asm-operand-list.
13729 asm-operand-list , asm-operand
13732 string-literal ( expression )
13733 [ string-literal ] string-literal ( expression )
13735 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13736 each node is the expression. The TREE_PURPOSE is itself a
13737 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13738 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13739 is a STRING_CST for the string literal before the parenthesis. */
13742 cp_parser_asm_operand_list (cp_parser* parser)
13744 tree asm_operands = NULL_TREE;
13748 tree string_literal;
13752 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13754 /* Consume the `[' token. */
13755 cp_lexer_consume_token (parser->lexer);
13756 /* Read the operand name. */
13757 name = cp_parser_identifier (parser);
13758 if (name != error_mark_node)
13759 name = build_string (IDENTIFIER_LENGTH (name),
13760 IDENTIFIER_POINTER (name));
13761 /* Look for the closing `]'. */
13762 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13766 /* Look for the string-literal. */
13767 string_literal = cp_parser_string_literal (parser, false, false);
13769 /* Look for the `('. */
13770 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13771 /* Parse the expression. */
13772 expression = cp_parser_expression (parser);
13773 /* Look for the `)'. */
13774 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13776 /* Add this operand to the list. */
13777 asm_operands = tree_cons (build_tree_list (name, string_literal),
13780 /* If the next token is not a `,', there are no more
13782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13784 /* Consume the `,'. */
13785 cp_lexer_consume_token (parser->lexer);
13788 return nreverse (asm_operands);
13791 /* Parse an asm-clobber-list.
13795 asm-clobber-list , string-literal
13797 Returns a TREE_LIST, indicating the clobbers in the order that they
13798 appeared. The TREE_VALUE of each node is a STRING_CST. */
13801 cp_parser_asm_clobber_list (cp_parser* parser)
13803 tree clobbers = NULL_TREE;
13807 tree string_literal;
13809 /* Look for the string literal. */
13810 string_literal = cp_parser_string_literal (parser, false, false);
13811 /* Add it to the list. */
13812 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13813 /* If the next token is not a `,', then the list is
13815 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13817 /* Consume the `,' token. */
13818 cp_lexer_consume_token (parser->lexer);
13824 /* Parse an (optional) series of attributes.
13827 attributes attribute
13830 __attribute__ (( attribute-list [opt] ))
13832 The return value is as for cp_parser_attribute_list. */
13835 cp_parser_attributes_opt (cp_parser* parser)
13837 tree attributes = NULL_TREE;
13842 tree attribute_list;
13844 /* Peek at the next token. */
13845 token = cp_lexer_peek_token (parser->lexer);
13846 /* If it's not `__attribute__', then we're done. */
13847 if (token->keyword != RID_ATTRIBUTE)
13850 /* Consume the `__attribute__' keyword. */
13851 cp_lexer_consume_token (parser->lexer);
13852 /* Look for the two `(' tokens. */
13853 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13854 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13856 /* Peek at the next token. */
13857 token = cp_lexer_peek_token (parser->lexer);
13858 if (token->type != CPP_CLOSE_PAREN)
13859 /* Parse the attribute-list. */
13860 attribute_list = cp_parser_attribute_list (parser);
13862 /* If the next token is a `)', then there is no attribute
13864 attribute_list = NULL;
13866 /* Look for the two `)' tokens. */
13867 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13868 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13870 /* Add these new attributes to the list. */
13871 attributes = chainon (attributes, attribute_list);
13877 /* Parse an attribute-list.
13881 attribute-list , attribute
13885 identifier ( identifier )
13886 identifier ( identifier , expression-list )
13887 identifier ( expression-list )
13889 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13890 TREE_PURPOSE of each node is the identifier indicating which
13891 attribute is in use. The TREE_VALUE represents the arguments, if
13895 cp_parser_attribute_list (cp_parser* parser)
13897 tree attribute_list = NULL_TREE;
13898 bool save_translate_strings_p = parser->translate_strings_p;
13900 parser->translate_strings_p = false;
13907 /* Look for the identifier. We also allow keywords here; for
13908 example `__attribute__ ((const))' is legal. */
13909 token = cp_lexer_peek_token (parser->lexer);
13910 if (token->type != CPP_NAME
13911 && token->type != CPP_KEYWORD)
13912 return error_mark_node;
13913 /* Consume the token. */
13914 token = cp_lexer_consume_token (parser->lexer);
13916 /* Save away the identifier that indicates which attribute this is. */
13917 identifier = token->value;
13918 attribute = build_tree_list (identifier, NULL_TREE);
13920 /* Peek at the next token. */
13921 token = cp_lexer_peek_token (parser->lexer);
13922 /* If it's an `(', then parse the attribute arguments. */
13923 if (token->type == CPP_OPEN_PAREN)
13927 arguments = (cp_parser_parenthesized_expression_list
13928 (parser, true, /*non_constant_p=*/NULL));
13929 /* Save the identifier and arguments away. */
13930 TREE_VALUE (attribute) = arguments;
13933 /* Add this attribute to the list. */
13934 TREE_CHAIN (attribute) = attribute_list;
13935 attribute_list = attribute;
13937 /* Now, look for more attributes. */
13938 token = cp_lexer_peek_token (parser->lexer);
13939 /* If the next token isn't a `,', we're done. */
13940 if (token->type != CPP_COMMA)
13943 /* Consume the comma and keep going. */
13944 cp_lexer_consume_token (parser->lexer);
13946 parser->translate_strings_p = save_translate_strings_p;
13948 /* We built up the list in reverse order. */
13949 return nreverse (attribute_list);
13952 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13953 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13954 current value of the PEDANTIC flag, regardless of whether or not
13955 the `__extension__' keyword is present. The caller is responsible
13956 for restoring the value of the PEDANTIC flag. */
13959 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13961 /* Save the old value of the PEDANTIC flag. */
13962 *saved_pedantic = pedantic;
13964 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13966 /* Consume the `__extension__' token. */
13967 cp_lexer_consume_token (parser->lexer);
13968 /* We're not being pedantic while the `__extension__' keyword is
13978 /* Parse a label declaration.
13981 __label__ label-declarator-seq ;
13983 label-declarator-seq:
13984 identifier , label-declarator-seq
13988 cp_parser_label_declaration (cp_parser* parser)
13990 /* Look for the `__label__' keyword. */
13991 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13997 /* Look for an identifier. */
13998 identifier = cp_parser_identifier (parser);
13999 /* Declare it as a lobel. */
14000 finish_label_decl (identifier);
14001 /* If the next token is a `;', stop. */
14002 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14004 /* Look for the `,' separating the label declarations. */
14005 cp_parser_require (parser, CPP_COMMA, "`,'");
14008 /* Look for the final `;'. */
14009 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14012 /* Support Functions */
14014 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14015 NAME should have one of the representations used for an
14016 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14017 is returned. If PARSER->SCOPE is a dependent type, then a
14018 SCOPE_REF is returned.
14020 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14021 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14022 was formed. Abstractly, such entities should not be passed to this
14023 function, because they do not need to be looked up, but it is
14024 simpler to check for this special case here, rather than at the
14027 In cases not explicitly covered above, this function returns a
14028 DECL, OVERLOAD, or baselink representing the result of the lookup.
14029 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14032 If IS_TYPE is TRUE, bindings that do not refer to types are
14035 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14038 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14041 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14044 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14045 results in an ambiguity, and false otherwise. */
14048 cp_parser_lookup_name (cp_parser *parser, tree name,
14049 bool is_type, bool is_template, bool is_namespace,
14050 bool check_dependency,
14054 tree object_type = parser->context->object_type;
14056 /* Assume that the lookup will be unambiguous. */
14058 *ambiguous_p = false;
14060 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14061 no longer valid. Note that if we are parsing tentatively, and
14062 the parse fails, OBJECT_TYPE will be automatically restored. */
14063 parser->context->object_type = NULL_TREE;
14065 if (name == error_mark_node)
14066 return error_mark_node;
14068 /* A template-id has already been resolved; there is no lookup to
14070 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14072 if (BASELINK_P (name))
14074 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14075 == TEMPLATE_ID_EXPR);
14079 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14080 it should already have been checked to make sure that the name
14081 used matches the type being destroyed. */
14082 if (TREE_CODE (name) == BIT_NOT_EXPR)
14086 /* Figure out to which type this destructor applies. */
14088 type = parser->scope;
14089 else if (object_type)
14090 type = object_type;
14092 type = current_class_type;
14093 /* If that's not a class type, there is no destructor. */
14094 if (!type || !CLASS_TYPE_P (type))
14095 return error_mark_node;
14096 if (!CLASSTYPE_DESTRUCTORS (type))
14097 return error_mark_node;
14098 /* If it was a class type, return the destructor. */
14099 return CLASSTYPE_DESTRUCTORS (type);
14102 /* By this point, the NAME should be an ordinary identifier. If
14103 the id-expression was a qualified name, the qualifying scope is
14104 stored in PARSER->SCOPE at this point. */
14105 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14107 /* Perform the lookup. */
14112 if (parser->scope == error_mark_node)
14113 return error_mark_node;
14115 /* If the SCOPE is dependent, the lookup must be deferred until
14116 the template is instantiated -- unless we are explicitly
14117 looking up names in uninstantiated templates. Even then, we
14118 cannot look up the name if the scope is not a class type; it
14119 might, for example, be a template type parameter. */
14120 dependent_p = (TYPE_P (parser->scope)
14121 && !(parser->in_declarator_p
14122 && currently_open_class (parser->scope))
14123 && dependent_type_p (parser->scope));
14124 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14128 /* The resolution to Core Issue 180 says that `struct A::B'
14129 should be considered a type-name, even if `A' is
14131 decl = TYPE_NAME (make_typename_type (parser->scope,
14134 else if (is_template)
14135 decl = make_unbound_class_template (parser->scope,
14139 decl = build_nt (SCOPE_REF, parser->scope, name);
14143 bool pop_p = false;
14145 /* If PARSER->SCOPE is a dependent type, then it must be a
14146 class type, and we must not be checking dependencies;
14147 otherwise, we would have processed this lookup above. So
14148 that PARSER->SCOPE is not considered a dependent base by
14149 lookup_member, we must enter the scope here. */
14151 pop_p = push_scope (parser->scope);
14152 /* If the PARSER->SCOPE is a a template specialization, it
14153 may be instantiated during name lookup. In that case,
14154 errors may be issued. Even if we rollback the current
14155 tentative parse, those errors are valid. */
14156 decl = lookup_qualified_name (parser->scope, name, is_type,
14157 /*complain=*/true);
14159 pop_scope (parser->scope);
14161 parser->qualifying_scope = parser->scope;
14162 parser->object_scope = NULL_TREE;
14164 else if (object_type)
14166 tree object_decl = NULL_TREE;
14167 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14168 OBJECT_TYPE is not a class. */
14169 if (CLASS_TYPE_P (object_type))
14170 /* If the OBJECT_TYPE is a template specialization, it may
14171 be instantiated during name lookup. In that case, errors
14172 may be issued. Even if we rollback the current tentative
14173 parse, those errors are valid. */
14174 object_decl = lookup_member (object_type,
14176 /*protect=*/0, is_type);
14177 /* Look it up in the enclosing context, too. */
14178 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14179 /*block_p=*/true, is_namespace,
14181 parser->object_scope = object_type;
14182 parser->qualifying_scope = NULL_TREE;
14184 decl = object_decl;
14188 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14189 /*block_p=*/true, is_namespace,
14191 parser->qualifying_scope = NULL_TREE;
14192 parser->object_scope = NULL_TREE;
14195 /* If the lookup failed, let our caller know. */
14197 || decl == error_mark_node
14198 || (TREE_CODE (decl) == FUNCTION_DECL
14199 && DECL_ANTICIPATED (decl)))
14200 return error_mark_node;
14202 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14203 if (TREE_CODE (decl) == TREE_LIST)
14206 *ambiguous_p = true;
14207 /* The error message we have to print is too complicated for
14208 cp_parser_error, so we incorporate its actions directly. */
14209 if (!cp_parser_simulate_error (parser))
14211 error ("reference to %qD is ambiguous", name);
14212 print_candidates (decl);
14214 return error_mark_node;
14217 gcc_assert (DECL_P (decl)
14218 || TREE_CODE (decl) == OVERLOAD
14219 || TREE_CODE (decl) == SCOPE_REF
14220 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14221 || BASELINK_P (decl));
14223 /* If we have resolved the name of a member declaration, check to
14224 see if the declaration is accessible. When the name resolves to
14225 set of overloaded functions, accessibility is checked when
14226 overload resolution is done.
14228 During an explicit instantiation, access is not checked at all,
14229 as per [temp.explicit]. */
14231 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14236 /* Like cp_parser_lookup_name, but for use in the typical case where
14237 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14238 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14241 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14243 return cp_parser_lookup_name (parser, name,
14245 /*is_template=*/false,
14246 /*is_namespace=*/false,
14247 /*check_dependency=*/true,
14248 /*ambiguous_p=*/NULL);
14251 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14252 the current context, return the TYPE_DECL. If TAG_NAME_P is
14253 true, the DECL indicates the class being defined in a class-head,
14254 or declared in an elaborated-type-specifier.
14256 Otherwise, return DECL. */
14259 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14261 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14262 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14265 template <typename T> struct B;
14268 template <typename T> struct A::B {};
14270 Similarly, in a elaborated-type-specifier:
14272 namespace N { struct X{}; }
14275 template <typename T> friend struct N::X;
14278 However, if the DECL refers to a class type, and we are in
14279 the scope of the class, then the name lookup automatically
14280 finds the TYPE_DECL created by build_self_reference rather
14281 than a TEMPLATE_DECL. For example, in:
14283 template <class T> struct S {
14287 there is no need to handle such case. */
14289 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14290 return DECL_TEMPLATE_RESULT (decl);
14295 /* If too many, or too few, template-parameter lists apply to the
14296 declarator, issue an error message. Returns TRUE if all went well,
14297 and FALSE otherwise. */
14300 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14301 cp_declarator *declarator)
14303 unsigned num_templates;
14305 /* We haven't seen any classes that involve template parameters yet. */
14308 switch (declarator->kind)
14311 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14316 scope = TREE_OPERAND (declarator->u.id.name, 0);
14317 member = TREE_OPERAND (declarator->u.id.name, 1);
14319 while (scope && CLASS_TYPE_P (scope))
14321 /* You're supposed to have one `template <...>'
14322 for every template class, but you don't need one
14323 for a full specialization. For example:
14325 template <class T> struct S{};
14326 template <> struct S<int> { void f(); };
14327 void S<int>::f () {}
14329 is correct; there shouldn't be a `template <>' for
14330 the definition of `S<int>::f'. */
14331 if (CLASSTYPE_TEMPLATE_INFO (scope)
14332 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14333 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14334 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14337 scope = TYPE_CONTEXT (scope);
14341 /* If the DECLARATOR has the form `X<y>' then it uses one
14342 additional level of template parameters. */
14343 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14346 return cp_parser_check_template_parameters (parser,
14352 case cdk_reference:
14354 return (cp_parser_check_declarator_template_parameters
14355 (parser, declarator->declarator));
14361 gcc_unreachable ();
14366 /* NUM_TEMPLATES were used in the current declaration. If that is
14367 invalid, return FALSE and issue an error messages. Otherwise,
14371 cp_parser_check_template_parameters (cp_parser* parser,
14372 unsigned num_templates)
14374 /* If there are more template classes than parameter lists, we have
14377 template <class T> void S<T>::R<T>::f (); */
14378 if (parser->num_template_parameter_lists < num_templates)
14380 error ("too few template-parameter-lists");
14383 /* If there are the same number of template classes and parameter
14384 lists, that's OK. */
14385 if (parser->num_template_parameter_lists == num_templates)
14387 /* If there are more, but only one more, then we are referring to a
14388 member template. That's OK too. */
14389 if (parser->num_template_parameter_lists == num_templates + 1)
14391 /* Otherwise, there are too many template parameter lists. We have
14394 template <class T> template <class U> void S::f(); */
14395 error ("too many template-parameter-lists");
14399 /* Parse an optional `::' token indicating that the following name is
14400 from the global namespace. If so, PARSER->SCOPE is set to the
14401 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14402 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14403 Returns the new value of PARSER->SCOPE, if the `::' token is
14404 present, and NULL_TREE otherwise. */
14407 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14411 /* Peek at the next token. */
14412 token = cp_lexer_peek_token (parser->lexer);
14413 /* If we're looking at a `::' token then we're starting from the
14414 global namespace, not our current location. */
14415 if (token->type == CPP_SCOPE)
14417 /* Consume the `::' token. */
14418 cp_lexer_consume_token (parser->lexer);
14419 /* Set the SCOPE so that we know where to start the lookup. */
14420 parser->scope = global_namespace;
14421 parser->qualifying_scope = global_namespace;
14422 parser->object_scope = NULL_TREE;
14424 return parser->scope;
14426 else if (!current_scope_valid_p)
14428 parser->scope = NULL_TREE;
14429 parser->qualifying_scope = NULL_TREE;
14430 parser->object_scope = NULL_TREE;
14436 /* Returns TRUE if the upcoming token sequence is the start of a
14437 constructor declarator. If FRIEND_P is true, the declarator is
14438 preceded by the `friend' specifier. */
14441 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14443 bool constructor_p;
14444 tree type_decl = NULL_TREE;
14445 bool nested_name_p;
14446 cp_token *next_token;
14448 /* The common case is that this is not a constructor declarator, so
14449 try to avoid doing lots of work if at all possible. It's not
14450 valid declare a constructor at function scope. */
14451 if (at_function_scope_p ())
14453 /* And only certain tokens can begin a constructor declarator. */
14454 next_token = cp_lexer_peek_token (parser->lexer);
14455 if (next_token->type != CPP_NAME
14456 && next_token->type != CPP_SCOPE
14457 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14458 && next_token->type != CPP_TEMPLATE_ID)
14461 /* Parse tentatively; we are going to roll back all of the tokens
14463 cp_parser_parse_tentatively (parser);
14464 /* Assume that we are looking at a constructor declarator. */
14465 constructor_p = true;
14467 /* Look for the optional `::' operator. */
14468 cp_parser_global_scope_opt (parser,
14469 /*current_scope_valid_p=*/false);
14470 /* Look for the nested-name-specifier. */
14472 = (cp_parser_nested_name_specifier_opt (parser,
14473 /*typename_keyword_p=*/false,
14474 /*check_dependency_p=*/false,
14476 /*is_declaration=*/false)
14478 /* Outside of a class-specifier, there must be a
14479 nested-name-specifier. */
14480 if (!nested_name_p &&
14481 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14483 constructor_p = false;
14484 /* If we still think that this might be a constructor-declarator,
14485 look for a class-name. */
14490 template <typename T> struct S { S(); };
14491 template <typename T> S<T>::S ();
14493 we must recognize that the nested `S' names a class.
14496 template <typename T> S<T>::S<T> ();
14498 we must recognize that the nested `S' names a template. */
14499 type_decl = cp_parser_class_name (parser,
14500 /*typename_keyword_p=*/false,
14501 /*template_keyword_p=*/false,
14503 /*check_dependency_p=*/false,
14504 /*class_head_p=*/false,
14505 /*is_declaration=*/false);
14506 /* If there was no class-name, then this is not a constructor. */
14507 constructor_p = !cp_parser_error_occurred (parser);
14510 /* If we're still considering a constructor, we have to see a `(',
14511 to begin the parameter-declaration-clause, followed by either a
14512 `)', an `...', or a decl-specifier. We need to check for a
14513 type-specifier to avoid being fooled into thinking that:
14517 is a constructor. (It is actually a function named `f' that
14518 takes one parameter (of type `int') and returns a value of type
14521 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14524 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14525 /* A parameter declaration begins with a decl-specifier,
14526 which is either the "attribute" keyword, a storage class
14527 specifier, or (usually) a type-specifier. */
14528 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14529 && !cp_parser_storage_class_specifier_opt (parser))
14532 bool pop_p = false;
14533 unsigned saved_num_template_parameter_lists;
14535 /* Names appearing in the type-specifier should be looked up
14536 in the scope of the class. */
14537 if (current_class_type)
14541 type = TREE_TYPE (type_decl);
14542 if (TREE_CODE (type) == TYPENAME_TYPE)
14544 type = resolve_typename_type (type,
14545 /*only_current_p=*/false);
14546 if (type == error_mark_node)
14548 cp_parser_abort_tentative_parse (parser);
14552 pop_p = push_scope (type);
14555 /* Inside the constructor parameter list, surrounding
14556 template-parameter-lists do not apply. */
14557 saved_num_template_parameter_lists
14558 = parser->num_template_parameter_lists;
14559 parser->num_template_parameter_lists = 0;
14561 /* Look for the type-specifier. */
14562 cp_parser_type_specifier (parser,
14563 CP_PARSER_FLAGS_NONE,
14564 /*decl_specs=*/NULL,
14565 /*is_declarator=*/true,
14566 /*declares_class_or_enum=*/NULL,
14567 /*is_cv_qualifier=*/NULL);
14569 parser->num_template_parameter_lists
14570 = saved_num_template_parameter_lists;
14572 /* Leave the scope of the class. */
14576 constructor_p = !cp_parser_error_occurred (parser);
14580 constructor_p = false;
14581 /* We did not really want to consume any tokens. */
14582 cp_parser_abort_tentative_parse (parser);
14584 return constructor_p;
14587 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14588 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14589 they must be performed once we are in the scope of the function.
14591 Returns the function defined. */
14594 cp_parser_function_definition_from_specifiers_and_declarator
14595 (cp_parser* parser,
14596 cp_decl_specifier_seq *decl_specifiers,
14598 const cp_declarator *declarator)
14603 /* Begin the function-definition. */
14604 success_p = start_function (decl_specifiers, declarator, attributes);
14606 /* The things we're about to see are not directly qualified by any
14607 template headers we've seen thus far. */
14608 reset_specialization ();
14610 /* If there were names looked up in the decl-specifier-seq that we
14611 did not check, check them now. We must wait until we are in the
14612 scope of the function to perform the checks, since the function
14613 might be a friend. */
14614 perform_deferred_access_checks ();
14618 /* Skip the entire function. */
14619 error ("invalid function declaration");
14620 cp_parser_skip_to_end_of_block_or_statement (parser);
14621 fn = error_mark_node;
14624 fn = cp_parser_function_definition_after_declarator (parser,
14625 /*inline_p=*/false);
14630 /* Parse the part of a function-definition that follows the
14631 declarator. INLINE_P is TRUE iff this function is an inline
14632 function defined with a class-specifier.
14634 Returns the function defined. */
14637 cp_parser_function_definition_after_declarator (cp_parser* parser,
14641 bool ctor_initializer_p = false;
14642 bool saved_in_unbraced_linkage_specification_p;
14643 unsigned saved_num_template_parameter_lists;
14645 /* If the next token is `return', then the code may be trying to
14646 make use of the "named return value" extension that G++ used to
14648 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14650 /* Consume the `return' keyword. */
14651 cp_lexer_consume_token (parser->lexer);
14652 /* Look for the identifier that indicates what value is to be
14654 cp_parser_identifier (parser);
14655 /* Issue an error message. */
14656 error ("named return values are no longer supported");
14657 /* Skip tokens until we reach the start of the function body. */
14658 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14659 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14660 cp_lexer_consume_token (parser->lexer);
14662 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14663 anything declared inside `f'. */
14664 saved_in_unbraced_linkage_specification_p
14665 = parser->in_unbraced_linkage_specification_p;
14666 parser->in_unbraced_linkage_specification_p = false;
14667 /* Inside the function, surrounding template-parameter-lists do not
14669 saved_num_template_parameter_lists
14670 = parser->num_template_parameter_lists;
14671 parser->num_template_parameter_lists = 0;
14672 /* If the next token is `try', then we are looking at a
14673 function-try-block. */
14674 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14675 ctor_initializer_p = cp_parser_function_try_block (parser);
14676 /* A function-try-block includes the function-body, so we only do
14677 this next part if we're not processing a function-try-block. */
14680 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14682 /* Finish the function. */
14683 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14684 (inline_p ? 2 : 0));
14685 /* Generate code for it, if necessary. */
14686 expand_or_defer_fn (fn);
14687 /* Restore the saved values. */
14688 parser->in_unbraced_linkage_specification_p
14689 = saved_in_unbraced_linkage_specification_p;
14690 parser->num_template_parameter_lists
14691 = saved_num_template_parameter_lists;
14696 /* Parse a template-declaration, assuming that the `export' (and
14697 `extern') keywords, if present, has already been scanned. MEMBER_P
14698 is as for cp_parser_template_declaration. */
14701 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14703 tree decl = NULL_TREE;
14704 tree parameter_list;
14705 bool friend_p = false;
14707 /* Look for the `template' keyword. */
14708 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14712 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14715 /* If the next token is `>', then we have an invalid
14716 specialization. Rather than complain about an invalid template
14717 parameter, issue an error message here. */
14718 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14720 cp_parser_error (parser, "invalid explicit specialization");
14721 begin_specialization ();
14722 parameter_list = NULL_TREE;
14726 /* Parse the template parameters. */
14727 begin_template_parm_list ();
14728 parameter_list = cp_parser_template_parameter_list (parser);
14729 parameter_list = end_template_parm_list (parameter_list);
14732 /* Look for the `>'. */
14733 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14734 /* We just processed one more parameter list. */
14735 ++parser->num_template_parameter_lists;
14736 /* If the next token is `template', there are more template
14738 if (cp_lexer_next_token_is_keyword (parser->lexer,
14740 cp_parser_template_declaration_after_export (parser, member_p);
14743 /* There are no access checks when parsing a template, as we do not
14744 know if a specialization will be a friend. */
14745 push_deferring_access_checks (dk_no_check);
14747 decl = cp_parser_single_declaration (parser,
14751 pop_deferring_access_checks ();
14753 /* If this is a member template declaration, let the front
14755 if (member_p && !friend_p && decl)
14757 if (TREE_CODE (decl) == TYPE_DECL)
14758 cp_parser_check_access_in_redeclaration (decl);
14760 decl = finish_member_template_decl (decl);
14762 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14763 make_friend_class (current_class_type, TREE_TYPE (decl),
14764 /*complain=*/true);
14766 /* We are done with the current parameter list. */
14767 --parser->num_template_parameter_lists;
14770 finish_template_decl (parameter_list);
14772 /* Register member declarations. */
14773 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14774 finish_member_declaration (decl);
14776 /* If DECL is a function template, we must return to parse it later.
14777 (Even though there is no definition, there might be default
14778 arguments that need handling.) */
14779 if (member_p && decl
14780 && (TREE_CODE (decl) == FUNCTION_DECL
14781 || DECL_FUNCTION_TEMPLATE_P (decl)))
14782 TREE_VALUE (parser->unparsed_functions_queues)
14783 = tree_cons (NULL_TREE, decl,
14784 TREE_VALUE (parser->unparsed_functions_queues));
14787 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14788 `function-definition' sequence. MEMBER_P is true, this declaration
14789 appears in a class scope.
14791 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14792 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14795 cp_parser_single_declaration (cp_parser* parser,
14799 int declares_class_or_enum;
14800 tree decl = NULL_TREE;
14801 cp_decl_specifier_seq decl_specifiers;
14802 bool function_definition_p = false;
14804 /* Defer access checks until we know what is being declared. */
14805 push_deferring_access_checks (dk_deferred);
14807 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14809 cp_parser_decl_specifier_seq (parser,
14810 CP_PARSER_FLAGS_OPTIONAL,
14812 &declares_class_or_enum);
14814 *friend_p = cp_parser_friend_p (&decl_specifiers);
14815 /* Gather up the access checks that occurred the
14816 decl-specifier-seq. */
14817 stop_deferring_access_checks ();
14819 /* Check for the declaration of a template class. */
14820 if (declares_class_or_enum)
14822 if (cp_parser_declares_only_class_p (parser))
14824 decl = shadow_tag (&decl_specifiers);
14829 friend template <typename T> struct A<T>::B;
14832 A<T>::B will be represented by a TYPENAME_TYPE, and
14833 therefore not recognized by shadow_tag. */
14834 if (friend_p && *friend_p
14836 && decl_specifiers.type
14837 && TYPE_P (decl_specifiers.type))
14838 decl = decl_specifiers.type;
14840 if (decl && decl != error_mark_node)
14841 decl = TYPE_NAME (decl);
14843 decl = error_mark_node;
14848 /* If it's not a template class, try for a template function. If
14849 the next token is a `;', then this declaration does not declare
14850 anything. But, if there were errors in the decl-specifiers, then
14851 the error might well have come from an attempted class-specifier.
14852 In that case, there's no need to warn about a missing declarator. */
14854 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14855 || decl_specifiers.type != error_mark_node))
14856 decl = cp_parser_init_declarator (parser,
14858 /*function_definition_allowed_p=*/true,
14860 declares_class_or_enum,
14861 &function_definition_p);
14863 pop_deferring_access_checks ();
14865 /* Clear any current qualification; whatever comes next is the start
14866 of something new. */
14867 parser->scope = NULL_TREE;
14868 parser->qualifying_scope = NULL_TREE;
14869 parser->object_scope = NULL_TREE;
14870 /* Look for a trailing `;' after the declaration. */
14871 if (!function_definition_p
14872 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14873 cp_parser_skip_to_end_of_block_or_statement (parser);
14878 /* Parse a cast-expression that is not the operand of a unary "&". */
14881 cp_parser_simple_cast_expression (cp_parser *parser)
14883 return cp_parser_cast_expression (parser, /*address_p=*/false);
14886 /* Parse a functional cast to TYPE. Returns an expression
14887 representing the cast. */
14890 cp_parser_functional_cast (cp_parser* parser, tree type)
14892 tree expression_list;
14896 = cp_parser_parenthesized_expression_list (parser, false,
14897 /*non_constant_p=*/NULL);
14899 cast = build_functional_cast (type, expression_list);
14900 /* [expr.const]/1: In an integral constant expression "only type
14901 conversions to integral or enumeration type can be used". */
14902 if (cast != error_mark_node && !type_dependent_expression_p (type)
14903 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14905 if (cp_parser_non_integral_constant_expression
14906 (parser, "a call to a constructor"))
14907 return error_mark_node;
14912 /* Save the tokens that make up the body of a member function defined
14913 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14914 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14915 specifiers applied to the declaration. Returns the FUNCTION_DECL
14916 for the member function. */
14919 cp_parser_save_member_function_body (cp_parser* parser,
14920 cp_decl_specifier_seq *decl_specifiers,
14921 cp_declarator *declarator,
14928 /* Create the function-declaration. */
14929 fn = start_method (decl_specifiers, declarator, attributes);
14930 /* If something went badly wrong, bail out now. */
14931 if (fn == error_mark_node)
14933 /* If there's a function-body, skip it. */
14934 if (cp_parser_token_starts_function_definition_p
14935 (cp_lexer_peek_token (parser->lexer)))
14936 cp_parser_skip_to_end_of_block_or_statement (parser);
14937 return error_mark_node;
14940 /* Remember it, if there default args to post process. */
14941 cp_parser_save_default_args (parser, fn);
14943 /* Save away the tokens that make up the body of the
14945 first = parser->lexer->next_token;
14946 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14947 /* Handle function try blocks. */
14948 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14949 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14950 last = parser->lexer->next_token;
14952 /* Save away the inline definition; we will process it when the
14953 class is complete. */
14954 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
14955 DECL_PENDING_INLINE_P (fn) = 1;
14957 /* We need to know that this was defined in the class, so that
14958 friend templates are handled correctly. */
14959 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14961 /* We're done with the inline definition. */
14962 finish_method (fn);
14964 /* Add FN to the queue of functions to be parsed later. */
14965 TREE_VALUE (parser->unparsed_functions_queues)
14966 = tree_cons (NULL_TREE, fn,
14967 TREE_VALUE (parser->unparsed_functions_queues));
14972 /* Parse a template-argument-list, as well as the trailing ">" (but
14973 not the opening ">"). See cp_parser_template_argument_list for the
14977 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14981 tree saved_qualifying_scope;
14982 tree saved_object_scope;
14983 bool saved_greater_than_is_operator_p;
14987 When parsing a template-id, the first non-nested `>' is taken as
14988 the end of the template-argument-list rather than a greater-than
14990 saved_greater_than_is_operator_p
14991 = parser->greater_than_is_operator_p;
14992 parser->greater_than_is_operator_p = false;
14993 /* Parsing the argument list may modify SCOPE, so we save it
14995 saved_scope = parser->scope;
14996 saved_qualifying_scope = parser->qualifying_scope;
14997 saved_object_scope = parser->object_scope;
14998 /* Parse the template-argument-list itself. */
14999 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15000 arguments = NULL_TREE;
15002 arguments = cp_parser_template_argument_list (parser);
15003 /* Look for the `>' that ends the template-argument-list. If we find
15004 a '>>' instead, it's probably just a typo. */
15005 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15007 if (!saved_greater_than_is_operator_p)
15009 /* If we're in a nested template argument list, the '>>' has
15010 to be a typo for '> >'. We emit the error message, but we
15011 continue parsing and we push a '>' as next token, so that
15012 the argument list will be parsed correctly. Note that the
15013 global source location is still on the token before the
15014 '>>', so we need to say explicitly where we want it. */
15015 cp_token *token = cp_lexer_peek_token (parser->lexer);
15016 error ("%H%<>>%> should be %<> >%> "
15017 "within a nested template argument list",
15020 /* ??? Proper recovery should terminate two levels of
15021 template argument list here. */
15022 token->type = CPP_GREATER;
15026 /* If this is not a nested template argument list, the '>>'
15027 is a typo for '>'. Emit an error message and continue.
15028 Same deal about the token location, but here we can get it
15029 right by consuming the '>>' before issuing the diagnostic. */
15030 cp_lexer_consume_token (parser->lexer);
15031 error ("spurious %<>>%>, use %<>%> to terminate "
15032 "a template argument list");
15035 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15036 error ("missing %<>%> to terminate the template argument list");
15038 /* It's what we want, a '>'; consume it. */
15039 cp_lexer_consume_token (parser->lexer);
15040 /* The `>' token might be a greater-than operator again now. */
15041 parser->greater_than_is_operator_p
15042 = saved_greater_than_is_operator_p;
15043 /* Restore the SAVED_SCOPE. */
15044 parser->scope = saved_scope;
15045 parser->qualifying_scope = saved_qualifying_scope;
15046 parser->object_scope = saved_object_scope;
15051 /* MEMBER_FUNCTION is a member function, or a friend. If default
15052 arguments, or the body of the function have not yet been parsed,
15056 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15058 /* If this member is a template, get the underlying
15060 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15061 member_function = DECL_TEMPLATE_RESULT (member_function);
15063 /* There should not be any class definitions in progress at this
15064 point; the bodies of members are only parsed outside of all class
15066 gcc_assert (parser->num_classes_being_defined == 0);
15067 /* While we're parsing the member functions we might encounter more
15068 classes. We want to handle them right away, but we don't want
15069 them getting mixed up with functions that are currently in the
15071 parser->unparsed_functions_queues
15072 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15074 /* Make sure that any template parameters are in scope. */
15075 maybe_begin_member_template_processing (member_function);
15077 /* If the body of the function has not yet been parsed, parse it
15079 if (DECL_PENDING_INLINE_P (member_function))
15081 tree function_scope;
15082 cp_token_cache *tokens;
15084 /* The function is no longer pending; we are processing it. */
15085 tokens = DECL_PENDING_INLINE_INFO (member_function);
15086 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15087 DECL_PENDING_INLINE_P (member_function) = 0;
15088 /* If this was an inline function in a local class, enter the scope
15089 of the containing function. */
15090 function_scope = decl_function_context (member_function);
15091 if (function_scope)
15092 push_function_context_to (function_scope);
15094 /* Push the body of the function onto the lexer stack. */
15095 cp_parser_push_lexer_for_tokens (parser, tokens);
15097 /* Let the front end know that we going to be defining this
15099 start_preparsed_function (member_function, NULL_TREE,
15100 SF_PRE_PARSED | SF_INCLASS_INLINE);
15102 /* Now, parse the body of the function. */
15103 cp_parser_function_definition_after_declarator (parser,
15104 /*inline_p=*/true);
15106 /* Leave the scope of the containing function. */
15107 if (function_scope)
15108 pop_function_context_from (function_scope);
15109 cp_parser_pop_lexer (parser);
15112 /* Remove any template parameters from the symbol table. */
15113 maybe_end_member_template_processing ();
15115 /* Restore the queue. */
15116 parser->unparsed_functions_queues
15117 = TREE_CHAIN (parser->unparsed_functions_queues);
15120 /* If DECL contains any default args, remember it on the unparsed
15121 functions queue. */
15124 cp_parser_save_default_args (cp_parser* parser, tree decl)
15128 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15130 probe = TREE_CHAIN (probe))
15131 if (TREE_PURPOSE (probe))
15133 TREE_PURPOSE (parser->unparsed_functions_queues)
15134 = tree_cons (current_class_type, decl,
15135 TREE_PURPOSE (parser->unparsed_functions_queues));
15141 /* FN is a FUNCTION_DECL which may contains a parameter with an
15142 unparsed DEFAULT_ARG. Parse the default args now. This function
15143 assumes that the current scope is the scope in which the default
15144 argument should be processed. */
15147 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15149 bool saved_local_variables_forbidden_p;
15152 /* While we're parsing the default args, we might (due to the
15153 statement expression extension) encounter more classes. We want
15154 to handle them right away, but we don't want them getting mixed
15155 up with default args that are currently in the queue. */
15156 parser->unparsed_functions_queues
15157 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15159 /* Local variable names (and the `this' keyword) may not appear
15160 in a default argument. */
15161 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15162 parser->local_variables_forbidden_p = true;
15164 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15166 parm = TREE_CHAIN (parm))
15168 cp_token_cache *tokens;
15170 if (!TREE_PURPOSE (parm)
15171 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15174 /* Push the saved tokens for the default argument onto the parser's
15176 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15177 cp_parser_push_lexer_for_tokens (parser, tokens);
15179 /* Parse the assignment-expression. */
15180 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15182 /* If the token stream has not been completely used up, then
15183 there was extra junk after the end of the default
15185 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15186 cp_parser_error (parser, "expected %<,%>");
15188 /* Revert to the main lexer. */
15189 cp_parser_pop_lexer (parser);
15192 /* Restore the state of local_variables_forbidden_p. */
15193 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15195 /* Restore the queue. */
15196 parser->unparsed_functions_queues
15197 = TREE_CHAIN (parser->unparsed_functions_queues);
15200 /* Parse the operand of `sizeof' (or a similar operator). Returns
15201 either a TYPE or an expression, depending on the form of the
15202 input. The KEYWORD indicates which kind of expression we have
15206 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15208 static const char *format;
15209 tree expr = NULL_TREE;
15210 const char *saved_message;
15211 bool saved_integral_constant_expression_p;
15213 /* Initialize FORMAT the first time we get here. */
15215 format = "types may not be defined in `%s' expressions";
15217 /* Types cannot be defined in a `sizeof' expression. Save away the
15219 saved_message = parser->type_definition_forbidden_message;
15220 /* And create the new one. */
15221 parser->type_definition_forbidden_message
15222 = xmalloc (strlen (format)
15223 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15225 sprintf ((char *) parser->type_definition_forbidden_message,
15226 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15228 /* The restrictions on constant-expressions do not apply inside
15229 sizeof expressions. */
15230 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15231 parser->integral_constant_expression_p = false;
15233 /* Do not actually evaluate the expression. */
15235 /* If it's a `(', then we might be looking at the type-id
15237 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15240 bool saved_in_type_id_in_expr_p;
15242 /* We can't be sure yet whether we're looking at a type-id or an
15244 cp_parser_parse_tentatively (parser);
15245 /* Consume the `('. */
15246 cp_lexer_consume_token (parser->lexer);
15247 /* Parse the type-id. */
15248 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15249 parser->in_type_id_in_expr_p = true;
15250 type = cp_parser_type_id (parser);
15251 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15252 /* Now, look for the trailing `)'. */
15253 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15254 /* If all went well, then we're done. */
15255 if (cp_parser_parse_definitely (parser))
15257 cp_decl_specifier_seq decl_specs;
15259 /* Build a trivial decl-specifier-seq. */
15260 clear_decl_specs (&decl_specs);
15261 decl_specs.type = type;
15263 /* Call grokdeclarator to figure out what type this is. */
15264 expr = grokdeclarator (NULL,
15268 /*attrlist=*/NULL);
15272 /* If the type-id production did not work out, then we must be
15273 looking at the unary-expression production. */
15275 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15276 /* Go back to evaluating expressions. */
15279 /* Free the message we created. */
15280 free ((char *) parser->type_definition_forbidden_message);
15281 /* And restore the old one. */
15282 parser->type_definition_forbidden_message = saved_message;
15283 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15288 /* If the current declaration has no declarator, return true. */
15291 cp_parser_declares_only_class_p (cp_parser *parser)
15293 /* If the next token is a `;' or a `,' then there is no
15295 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15296 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15299 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15302 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15303 cp_storage_class storage_class)
15305 if (decl_specs->storage_class != sc_none)
15306 decl_specs->multiple_storage_classes_p = true;
15308 decl_specs->storage_class = storage_class;
15311 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15312 is true, the type is a user-defined type; otherwise it is a
15313 built-in type specified by a keyword. */
15316 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15318 bool user_defined_p)
15320 decl_specs->any_specifiers_p = true;
15322 /* If the user tries to redeclare a built-in type (with, for example,
15323 in "typedef int wchar_t;") we remember that this is what
15324 happened. In system headers, we ignore these declarations so
15325 that G++ can work with system headers that are not C++-safe. */
15326 if (decl_specs->specs[(int) ds_typedef]
15328 && (decl_specs->type
15329 || decl_specs->specs[(int) ds_long]
15330 || decl_specs->specs[(int) ds_short]
15331 || decl_specs->specs[(int) ds_unsigned]
15332 || decl_specs->specs[(int) ds_signed]))
15334 decl_specs->redefined_builtin_type = type_spec;
15335 if (!decl_specs->type)
15337 decl_specs->type = type_spec;
15338 decl_specs->user_defined_type_p = false;
15341 else if (decl_specs->type)
15342 decl_specs->multiple_types_p = true;
15345 decl_specs->type = type_spec;
15346 decl_specs->user_defined_type_p = user_defined_p;
15347 decl_specs->redefined_builtin_type = NULL_TREE;
15351 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15352 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15355 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15357 return decl_specifiers->specs[(int) ds_friend] != 0;
15360 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15361 issue an error message indicating that TOKEN_DESC was expected.
15363 Returns the token consumed, if the token had the appropriate type.
15364 Otherwise, returns NULL. */
15367 cp_parser_require (cp_parser* parser,
15368 enum cpp_ttype type,
15369 const char* token_desc)
15371 if (cp_lexer_next_token_is (parser->lexer, type))
15372 return cp_lexer_consume_token (parser->lexer);
15375 /* Output the MESSAGE -- unless we're parsing tentatively. */
15376 if (!cp_parser_simulate_error (parser))
15378 char *message = concat ("expected ", token_desc, NULL);
15379 cp_parser_error (parser, message);
15386 /* Like cp_parser_require, except that tokens will be skipped until
15387 the desired token is found. An error message is still produced if
15388 the next token is not as expected. */
15391 cp_parser_skip_until_found (cp_parser* parser,
15392 enum cpp_ttype type,
15393 const char* token_desc)
15396 unsigned nesting_depth = 0;
15398 if (cp_parser_require (parser, type, token_desc))
15401 /* Skip tokens until the desired token is found. */
15404 /* Peek at the next token. */
15405 token = cp_lexer_peek_token (parser->lexer);
15406 /* If we've reached the token we want, consume it and
15408 if (token->type == type && !nesting_depth)
15410 cp_lexer_consume_token (parser->lexer);
15413 /* If we've run out of tokens, stop. */
15414 if (token->type == CPP_EOF)
15416 if (token->type == CPP_OPEN_BRACE
15417 || token->type == CPP_OPEN_PAREN
15418 || token->type == CPP_OPEN_SQUARE)
15420 else if (token->type == CPP_CLOSE_BRACE
15421 || token->type == CPP_CLOSE_PAREN
15422 || token->type == CPP_CLOSE_SQUARE)
15424 if (nesting_depth-- == 0)
15427 /* Consume this token. */
15428 cp_lexer_consume_token (parser->lexer);
15432 /* If the next token is the indicated keyword, consume it. Otherwise,
15433 issue an error message indicating that TOKEN_DESC was expected.
15435 Returns the token consumed, if the token had the appropriate type.
15436 Otherwise, returns NULL. */
15439 cp_parser_require_keyword (cp_parser* parser,
15441 const char* token_desc)
15443 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15445 if (token && token->keyword != keyword)
15447 dyn_string_t error_msg;
15449 /* Format the error message. */
15450 error_msg = dyn_string_new (0);
15451 dyn_string_append_cstr (error_msg, "expected ");
15452 dyn_string_append_cstr (error_msg, token_desc);
15453 cp_parser_error (parser, error_msg->s);
15454 dyn_string_delete (error_msg);
15461 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15462 function-definition. */
15465 cp_parser_token_starts_function_definition_p (cp_token* token)
15467 return (/* An ordinary function-body begins with an `{'. */
15468 token->type == CPP_OPEN_BRACE
15469 /* A ctor-initializer begins with a `:'. */
15470 || token->type == CPP_COLON
15471 /* A function-try-block begins with `try'. */
15472 || token->keyword == RID_TRY
15473 /* The named return value extension begins with `return'. */
15474 || token->keyword == RID_RETURN);
15477 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15481 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15485 token = cp_lexer_peek_token (parser->lexer);
15486 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15489 /* Returns TRUE iff the next token is the "," or ">" ending a
15490 template-argument. */
15493 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15497 token = cp_lexer_peek_token (parser->lexer);
15498 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15501 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15502 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15505 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15510 token = cp_lexer_peek_nth_token (parser->lexer, n);
15511 if (token->type == CPP_LESS)
15513 /* Check for the sequence `<::' in the original code. It would be lexed as
15514 `[:', where `[' is a digraph, and there is no whitespace before
15516 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15519 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15520 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15526 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15527 or none_type otherwise. */
15529 static enum tag_types
15530 cp_parser_token_is_class_key (cp_token* token)
15532 switch (token->keyword)
15537 return record_type;
15546 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15549 cp_parser_check_class_key (enum tag_types class_key, tree type)
15551 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15552 pedwarn ("%qs tag used in naming %q#T",
15553 class_key == union_type ? "union"
15554 : class_key == record_type ? "struct" : "class",
15558 /* Issue an error message if DECL is redeclared with different
15559 access than its original declaration [class.access.spec/3].
15560 This applies to nested classes and nested class templates.
15564 cp_parser_check_access_in_redeclaration (tree decl)
15566 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15569 if ((TREE_PRIVATE (decl)
15570 != (current_access_specifier == access_private_node))
15571 || (TREE_PROTECTED (decl)
15572 != (current_access_specifier == access_protected_node)))
15573 error ("%qD redeclared with different access", decl);
15576 /* Look for the `template' keyword, as a syntactic disambiguator.
15577 Return TRUE iff it is present, in which case it will be
15581 cp_parser_optional_template_keyword (cp_parser *parser)
15583 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15585 /* The `template' keyword can only be used within templates;
15586 outside templates the parser can always figure out what is a
15587 template and what is not. */
15588 if (!processing_template_decl)
15590 error ("%<template%> (as a disambiguator) is only allowed "
15591 "within templates");
15592 /* If this part of the token stream is rescanned, the same
15593 error message would be generated. So, we purge the token
15594 from the stream. */
15595 cp_lexer_purge_token (parser->lexer);
15600 /* Consume the `template' keyword. */
15601 cp_lexer_consume_token (parser->lexer);
15609 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15610 set PARSER->SCOPE, and perform other related actions. */
15613 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15618 /* Get the stored value. */
15619 value = cp_lexer_consume_token (parser->lexer)->value;
15620 /* Perform any access checks that were deferred. */
15621 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15622 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15623 /* Set the scope from the stored value. */
15624 parser->scope = TREE_VALUE (value);
15625 parser->qualifying_scope = TREE_TYPE (value);
15626 parser->object_scope = NULL_TREE;
15629 /* Consume tokens up through a non-nested END token. */
15632 cp_parser_cache_group (cp_parser *parser,
15633 enum cpp_ttype end,
15640 /* Abort a parenthesized expression if we encounter a brace. */
15641 if ((end == CPP_CLOSE_PAREN || depth == 0)
15642 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15644 /* If we've reached the end of the file, stop. */
15645 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15647 /* Consume the next token. */
15648 token = cp_lexer_consume_token (parser->lexer);
15649 /* See if it starts a new group. */
15650 if (token->type == CPP_OPEN_BRACE)
15652 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15656 else if (token->type == CPP_OPEN_PAREN)
15657 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15658 else if (token->type == end)
15663 /* Begin parsing tentatively. We always save tokens while parsing
15664 tentatively so that if the tentative parsing fails we can restore the
15668 cp_parser_parse_tentatively (cp_parser* parser)
15670 /* Enter a new parsing context. */
15671 parser->context = cp_parser_context_new (parser->context);
15672 /* Begin saving tokens. */
15673 cp_lexer_save_tokens (parser->lexer);
15674 /* In order to avoid repetitive access control error messages,
15675 access checks are queued up until we are no longer parsing
15677 push_deferring_access_checks (dk_deferred);
15680 /* Commit to the currently active tentative parse. */
15683 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15685 cp_parser_context *context;
15688 /* Mark all of the levels as committed. */
15689 lexer = parser->lexer;
15690 for (context = parser->context; context->next; context = context->next)
15692 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15694 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15695 while (!cp_lexer_saving_tokens (lexer))
15696 lexer = lexer->next;
15697 cp_lexer_commit_tokens (lexer);
15701 /* Abort the currently active tentative parse. All consumed tokens
15702 will be rolled back, and no diagnostics will be issued. */
15705 cp_parser_abort_tentative_parse (cp_parser* parser)
15707 cp_parser_simulate_error (parser);
15708 /* Now, pretend that we want to see if the construct was
15709 successfully parsed. */
15710 cp_parser_parse_definitely (parser);
15713 /* Stop parsing tentatively. If a parse error has occurred, restore the
15714 token stream. Otherwise, commit to the tokens we have consumed.
15715 Returns true if no error occurred; false otherwise. */
15718 cp_parser_parse_definitely (cp_parser* parser)
15720 bool error_occurred;
15721 cp_parser_context *context;
15723 /* Remember whether or not an error occurred, since we are about to
15724 destroy that information. */
15725 error_occurred = cp_parser_error_occurred (parser);
15726 /* Remove the topmost context from the stack. */
15727 context = parser->context;
15728 parser->context = context->next;
15729 /* If no parse errors occurred, commit to the tentative parse. */
15730 if (!error_occurred)
15732 /* Commit to the tokens read tentatively, unless that was
15734 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15735 cp_lexer_commit_tokens (parser->lexer);
15737 pop_to_parent_deferring_access_checks ();
15739 /* Otherwise, if errors occurred, roll back our state so that things
15740 are just as they were before we began the tentative parse. */
15743 cp_lexer_rollback_tokens (parser->lexer);
15744 pop_deferring_access_checks ();
15746 /* Add the context to the front of the free list. */
15747 context->next = cp_parser_context_free_list;
15748 cp_parser_context_free_list = context;
15750 return !error_occurred;
15753 /* Returns true if we are parsing tentatively -- but have decided that
15754 we will stick with this tentative parse, even if errors occur. */
15757 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15759 return (cp_parser_parsing_tentatively (parser)
15760 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15763 /* Returns nonzero iff an error has occurred during the most recent
15764 tentative parse. */
15767 cp_parser_error_occurred (cp_parser* parser)
15769 return (cp_parser_parsing_tentatively (parser)
15770 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15773 /* Returns nonzero if GNU extensions are allowed. */
15776 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15778 return parser->allow_gnu_extensions_p;
15784 static GTY (()) cp_parser *the_parser;
15786 /* External interface. */
15788 /* Parse one entire translation unit. */
15791 c_parse_file (void)
15793 bool error_occurred;
15794 static bool already_called = false;
15796 if (already_called)
15798 sorry ("inter-module optimizations not implemented for C++");
15801 already_called = true;
15803 the_parser = cp_parser_new ();
15804 push_deferring_access_checks (flag_access_control
15805 ? dk_no_deferred : dk_no_check);
15806 error_occurred = cp_parser_translation_unit (the_parser);
15810 /* This variable must be provided by every front end. */
15814 #include "gt-cp-parser.h"