2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
50 typedef struct cp_token GTY (())
52 /* The kind of token. */
53 ENUM_BITFIELD (cpp_ttype) type : 8;
54 /* If this token is a keyword, this value indicates which keyword.
55 Otherwise, this value is RID_MAX. */
56 ENUM_BITFIELD (rid) keyword : 8;
59 /* Identifier for the pragma. */
60 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61 /* True if this token is from a system header. */
62 BOOL_BITFIELD in_system_header : 1;
63 /* True if this token is from a context where it is implicitly extern "C" */
64 BOOL_BITFIELD implicit_extern_c : 1;
65 /* True for a CPP_NAME token that is not a keyword (i.e., for which
66 KEYWORD is RID_MAX) iff this name was looked up and found to be
67 ambiguous. An error has already been reported. */
68 BOOL_BITFIELD ambiguous_p : 1;
69 /* The value associated with this token, if any. */
71 /* The location at which this token was found. */
75 /* We use a stack of token pointer for saving token sets. */
76 typedef struct cp_token *cp_token_position;
77 DEF_VEC_P (cp_token_position);
78 DEF_VEC_ALLOC_P (cp_token_position,heap);
80 static const cp_token eof_token =
82 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
83 #if USE_MAPPED_LOCATION
90 /* The cp_lexer structure represents the C++ lexer. It is responsible
91 for managing the token stream from the preprocessor and supplying
92 it to the parser. Tokens are never added to the cp_lexer after
95 typedef struct cp_lexer GTY (())
97 /* The memory allocated for the buffer. NULL if this lexer does not
98 own the token buffer. */
99 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
100 /* If the lexer owns the buffer, this is the number of tokens in the
102 size_t buffer_length;
104 /* A pointer just past the last available token. The tokens
105 in this lexer are [buffer, last_token). */
106 cp_token_position GTY ((skip)) last_token;
108 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
109 no more available tokens. */
110 cp_token_position GTY ((skip)) next_token;
112 /* A stack indicating positions at which cp_lexer_save_tokens was
113 called. The top entry is the most recent position at which we
114 began saving tokens. If the stack is non-empty, we are saving
116 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
118 /* The next lexer in a linked list of lexers. */
119 struct cp_lexer *next;
121 /* True if we should output debugging information. */
124 /* True if we're in the context of parsing a pragma, and should not
125 increment past the end-of-line marker. */
129 /* cp_token_cache is a range of tokens. There is no need to represent
130 allocate heap memory for it, since tokens are never removed from the
131 lexer's array. There is also no need for the GC to walk through
132 a cp_token_cache, since everything in here is referenced through
135 typedef struct cp_token_cache GTY(())
137 /* The beginning of the token range. */
138 cp_token * GTY((skip)) first;
140 /* Points immediately after the last token in the range. */
141 cp_token * GTY ((skip)) last;
146 static cp_lexer *cp_lexer_new_main
148 static cp_lexer *cp_lexer_new_from_tokens
149 (cp_token_cache *tokens);
150 static void cp_lexer_destroy
152 static int cp_lexer_saving_tokens
154 static cp_token_position cp_lexer_token_position
156 static cp_token *cp_lexer_token_at
157 (cp_lexer *, cp_token_position);
158 static void cp_lexer_get_preprocessor_token
159 (cp_lexer *, cp_token *);
160 static inline cp_token *cp_lexer_peek_token
162 static cp_token *cp_lexer_peek_nth_token
163 (cp_lexer *, size_t);
164 static inline bool cp_lexer_next_token_is
165 (cp_lexer *, enum cpp_ttype);
166 static bool cp_lexer_next_token_is_not
167 (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_keyword
169 (cp_lexer *, enum rid);
170 static cp_token *cp_lexer_consume_token
172 static void cp_lexer_purge_token
174 static void cp_lexer_purge_tokens_after
175 (cp_lexer *, cp_token_position);
176 static void cp_lexer_save_tokens
178 static void cp_lexer_commit_tokens
180 static void cp_lexer_rollback_tokens
182 #ifdef ENABLE_CHECKING
183 static void cp_lexer_print_token
184 (FILE *, cp_token *);
185 static inline bool cp_lexer_debugging_p
187 static void cp_lexer_start_debugging
188 (cp_lexer *) ATTRIBUTE_UNUSED;
189 static void cp_lexer_stop_debugging
190 (cp_lexer *) ATTRIBUTE_UNUSED;
192 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
193 about passing NULL to functions that require non-NULL arguments
194 (fputs, fprintf). It will never be used, so all we need is a value
195 of the right type that's guaranteed not to be NULL. */
196 #define cp_lexer_debug_stream stdout
197 #define cp_lexer_print_token(str, tok) (void) 0
198 #define cp_lexer_debugging_p(lexer) 0
199 #endif /* ENABLE_CHECKING */
201 static cp_token_cache *cp_token_cache_new
202 (cp_token *, cp_token *);
204 static void cp_parser_initial_pragma
207 /* Manifest constants. */
208 #define CP_LEXER_BUFFER_SIZE 10000
209 #define CP_SAVED_TOKEN_STACK 5
211 /* A token type for keywords, as opposed to ordinary identifiers. */
212 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
214 /* A token type for template-ids. If a template-id is processed while
215 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
216 the value of the CPP_TEMPLATE_ID is whatever was returned by
217 cp_parser_template_id. */
218 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
220 /* A token type for nested-name-specifiers. If a
221 nested-name-specifier is processed while parsing tentatively, it is
222 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
223 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
224 cp_parser_nested_name_specifier_opt. */
225 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
227 /* A token type for tokens that are not tokens at all; these are used
228 to represent slots in the array where there used to be a token
229 that has now been deleted. */
230 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
232 /* The number of token types, including C++-specific ones. */
233 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
237 #ifdef ENABLE_CHECKING
238 /* The stream to which debugging output should be written. */
239 static FILE *cp_lexer_debug_stream;
240 #endif /* ENABLE_CHECKING */
242 /* Create a new main C++ lexer, the lexer that gets tokens from the
246 cp_lexer_new_main (void)
248 cp_token first_token;
255 /* It's possible that parsing the first pragma will load a PCH file,
256 which is a GC collection point. So we have to do that before
257 allocating any memory. */
258 cp_parser_initial_pragma (&first_token);
260 /* Tell c_lex_with_flags not to merge string constants. */
261 c_lex_return_raw_strings = true;
263 c_common_no_more_pch ();
265 /* Allocate the memory. */
266 lexer = GGC_CNEW (cp_lexer);
268 #ifdef ENABLE_CHECKING
269 /* Initially we are not debugging. */
270 lexer->debugging_p = false;
271 #endif /* ENABLE_CHECKING */
272 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
273 CP_SAVED_TOKEN_STACK);
275 /* Create the buffer. */
276 alloc = CP_LEXER_BUFFER_SIZE;
277 buffer = GGC_NEWVEC (cp_token, alloc);
279 /* Put the first token in the buffer. */
284 /* Get the remaining tokens from the preprocessor. */
285 while (pos->type != CPP_EOF)
292 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
293 pos = buffer + space;
295 cp_lexer_get_preprocessor_token (lexer, pos);
297 lexer->buffer = buffer;
298 lexer->buffer_length = alloc - space;
299 lexer->last_token = pos;
300 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
302 /* Subsequent preprocessor diagnostics should use compiler
303 diagnostic functions to get the compiler source location. */
304 cpp_get_options (parse_in)->client_diagnostic = true;
305 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
307 gcc_assert (lexer->next_token->type != CPP_PURGED);
311 /* Create a new lexer whose token stream is primed with the tokens in
312 CACHE. When these tokens are exhausted, no new tokens will be read. */
315 cp_lexer_new_from_tokens (cp_token_cache *cache)
317 cp_token *first = cache->first;
318 cp_token *last = cache->last;
319 cp_lexer *lexer = GGC_CNEW (cp_lexer);
321 /* We do not own the buffer. */
322 lexer->buffer = NULL;
323 lexer->buffer_length = 0;
324 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
325 lexer->last_token = last;
327 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
328 CP_SAVED_TOKEN_STACK);
330 #ifdef ENABLE_CHECKING
331 /* Initially we are not debugging. */
332 lexer->debugging_p = false;
335 gcc_assert (lexer->next_token->type != CPP_PURGED);
339 /* Frees all resources associated with LEXER. */
342 cp_lexer_destroy (cp_lexer *lexer)
345 ggc_free (lexer->buffer);
346 VEC_free (cp_token_position, heap, lexer->saved_tokens);
350 /* Returns nonzero if debugging information should be output. */
352 #ifdef ENABLE_CHECKING
355 cp_lexer_debugging_p (cp_lexer *lexer)
357 return lexer->debugging_p;
360 #endif /* ENABLE_CHECKING */
362 static inline cp_token_position
363 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
365 gcc_assert (!previous_p || lexer->next_token != &eof_token);
367 return lexer->next_token - previous_p;
370 static inline cp_token *
371 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
376 /* nonzero if we are presently saving tokens. */
379 cp_lexer_saving_tokens (const cp_lexer* lexer)
381 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384 /* Store the next token from the preprocessor in *TOKEN. Return true
388 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391 static int is_extern_c = 0;
393 /* Get a new token from the preprocessor. */
395 = c_lex_with_flags (&token->value, &token->location, &token->flags);
396 token->keyword = RID_MAX;
397 token->pragma_kind = PRAGMA_NONE;
398 token->in_system_header = in_system_header;
400 /* On some systems, some header files are surrounded by an
401 implicit extern "C" block. Set a flag in the token if it
402 comes from such a header. */
403 is_extern_c += pending_lang_change;
404 pending_lang_change = 0;
405 token->implicit_extern_c = is_extern_c > 0;
407 /* Check to see if this token is a keyword. */
408 if (token->type == CPP_NAME)
410 if (C_IS_RESERVED_WORD (token->value))
412 /* Mark this token as a keyword. */
413 token->type = CPP_KEYWORD;
414 /* Record which keyword. */
415 token->keyword = C_RID_CODE (token->value);
416 /* Update the value. Some keywords are mapped to particular
417 entities, rather than simply having the value of the
418 corresponding IDENTIFIER_NODE. For example, `__const' is
419 mapped to `const'. */
420 token->value = ridpointers[token->keyword];
424 token->ambiguous_p = false;
425 token->keyword = RID_MAX;
428 /* Handle Objective-C++ keywords. */
429 else if (token->type == CPP_AT_NAME)
431 token->type = CPP_KEYWORD;
432 switch (C_RID_CODE (token->value))
434 /* Map 'class' to '@class', 'private' to '@private', etc. */
435 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439 case RID_THROW: token->keyword = RID_AT_THROW; break;
440 case RID_TRY: token->keyword = RID_AT_TRY; break;
441 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442 default: token->keyword = C_RID_CODE (token->value);
445 else if (token->type == CPP_PRAGMA)
447 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
448 token->pragma_kind = TREE_INT_CST_LOW (token->value);
453 /* Update the globals input_location and in_system_header from TOKEN. */
455 cp_lexer_set_source_position_from_token (cp_token *token)
457 if (token->type != CPP_EOF)
459 input_location = token->location;
460 in_system_header = token->in_system_header;
464 /* Return a pointer to the next token in the token stream, but do not
467 static inline cp_token *
468 cp_lexer_peek_token (cp_lexer *lexer)
470 if (cp_lexer_debugging_p (lexer))
472 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
473 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
474 putc ('\n', cp_lexer_debug_stream);
476 return lexer->next_token;
479 /* Return true if the next token has the indicated TYPE. */
482 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
484 return cp_lexer_peek_token (lexer)->type == type;
487 /* Return true if the next token does not have the indicated TYPE. */
490 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
492 return !cp_lexer_next_token_is (lexer, type);
495 /* Return true if the next token is the indicated KEYWORD. */
498 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
500 return cp_lexer_peek_token (lexer)->keyword == keyword;
503 /* Return a pointer to the Nth token in the token stream. If N is 1,
504 then this is precisely equivalent to cp_lexer_peek_token (except
505 that it is not inline). One would like to disallow that case, but
506 there is one case (cp_parser_nth_token_starts_template_id) where
507 the caller passes a variable for N and it might be 1. */
510 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
514 /* N is 1-based, not zero-based. */
517 if (cp_lexer_debugging_p (lexer))
518 fprintf (cp_lexer_debug_stream,
519 "cp_lexer: peeking ahead %ld at token: ", (long)n);
522 token = lexer->next_token;
523 gcc_assert (!n || token != &eof_token);
527 if (token == lexer->last_token)
529 token = (cp_token *)&eof_token;
533 if (token->type != CPP_PURGED)
537 if (cp_lexer_debugging_p (lexer))
539 cp_lexer_print_token (cp_lexer_debug_stream, token);
540 putc ('\n', cp_lexer_debug_stream);
546 /* Return the next token, and advance the lexer's next_token pointer
547 to point to the next non-purged token. */
550 cp_lexer_consume_token (cp_lexer* lexer)
552 cp_token *token = lexer->next_token;
554 gcc_assert (token != &eof_token);
555 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
560 if (lexer->next_token == lexer->last_token)
562 lexer->next_token = (cp_token *)&eof_token;
567 while (lexer->next_token->type == CPP_PURGED);
569 cp_lexer_set_source_position_from_token (token);
571 /* Provide debugging output. */
572 if (cp_lexer_debugging_p (lexer))
574 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
575 cp_lexer_print_token (cp_lexer_debug_stream, token);
576 putc ('\n', cp_lexer_debug_stream);
582 /* Permanently remove the next token from the token stream, and
583 advance the next_token pointer to refer to the next non-purged
587 cp_lexer_purge_token (cp_lexer *lexer)
589 cp_token *tok = lexer->next_token;
591 gcc_assert (tok != &eof_token);
592 tok->type = CPP_PURGED;
593 tok->location = UNKNOWN_LOCATION;
594 tok->value = NULL_TREE;
595 tok->keyword = RID_MAX;
600 if (tok == lexer->last_token)
602 tok = (cp_token *)&eof_token;
606 while (tok->type == CPP_PURGED);
607 lexer->next_token = tok;
610 /* Permanently remove all tokens after TOK, up to, but not
611 including, the token that will be returned next by
612 cp_lexer_peek_token. */
615 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
617 cp_token *peek = lexer->next_token;
619 if (peek == &eof_token)
620 peek = lexer->last_token;
622 gcc_assert (tok < peek);
624 for ( tok += 1; tok != peek; tok += 1)
626 tok->type = CPP_PURGED;
627 tok->location = UNKNOWN_LOCATION;
628 tok->value = NULL_TREE;
629 tok->keyword = RID_MAX;
633 /* Begin saving tokens. All tokens consumed after this point will be
637 cp_lexer_save_tokens (cp_lexer* lexer)
639 /* Provide debugging output. */
640 if (cp_lexer_debugging_p (lexer))
641 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
643 VEC_safe_push (cp_token_position, heap,
644 lexer->saved_tokens, lexer->next_token);
647 /* Commit to the portion of the token stream most recently saved. */
650 cp_lexer_commit_tokens (cp_lexer* lexer)
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer))
654 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
656 VEC_pop (cp_token_position, lexer->saved_tokens);
659 /* Return all tokens saved since the last call to cp_lexer_save_tokens
660 to the token stream. Stop saving tokens. */
663 cp_lexer_rollback_tokens (cp_lexer* lexer)
665 /* Provide debugging output. */
666 if (cp_lexer_debugging_p (lexer))
667 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
669 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
672 /* Print a representation of the TOKEN on the STREAM. */
674 #ifdef ENABLE_CHECKING
677 cp_lexer_print_token (FILE * stream, cp_token *token)
679 /* We don't use cpp_type2name here because the parser defines
680 a few tokens of its own. */
681 static const char *const token_names[] = {
682 /* cpplib-defined token types */
688 /* C++ parser token types - see "Manifest constants", above. */
691 "NESTED_NAME_SPECIFIER",
695 /* If we have a name for the token, print it out. Otherwise, we
696 simply give the numeric code. */
697 gcc_assert (token->type < ARRAY_SIZE(token_names));
698 fputs (token_names[token->type], stream);
700 /* For some tokens, print the associated data. */
704 /* Some keywords have a value that is not an IDENTIFIER_NODE.
705 For example, `struct' is mapped to an INTEGER_CST. */
706 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
708 /* else fall through */
710 fputs (IDENTIFIER_POINTER (token->value), stream);
715 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
723 /* Start emitting debugging information. */
726 cp_lexer_start_debugging (cp_lexer* lexer)
728 lexer->debugging_p = true;
731 /* Stop emitting debugging information. */
734 cp_lexer_stop_debugging (cp_lexer* lexer)
736 lexer->debugging_p = false;
739 #endif /* ENABLE_CHECKING */
741 /* Create a new cp_token_cache, representing a range of tokens. */
743 static cp_token_cache *
744 cp_token_cache_new (cp_token *first, cp_token *last)
746 cp_token_cache *cache = GGC_NEW (cp_token_cache);
747 cache->first = first;
753 /* Decl-specifiers. */
755 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
758 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
760 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
765 /* Nothing other than the parser should be creating declarators;
766 declarators are a semi-syntactic representation of C++ entities.
767 Other parts of the front end that need to create entities (like
768 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
770 static cp_declarator *make_call_declarator
771 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
772 static cp_declarator *make_array_declarator
773 (cp_declarator *, tree);
774 static cp_declarator *make_pointer_declarator
775 (cp_cv_quals, cp_declarator *);
776 static cp_declarator *make_reference_declarator
777 (cp_cv_quals, cp_declarator *);
778 static cp_parameter_declarator *make_parameter_declarator
779 (cp_decl_specifier_seq *, cp_declarator *, tree);
780 static cp_declarator *make_ptrmem_declarator
781 (cp_cv_quals, tree, cp_declarator *);
783 cp_declarator *cp_error_declarator;
785 /* The obstack on which declarators and related data structures are
787 static struct obstack declarator_obstack;
789 /* Alloc BYTES from the declarator memory pool. */
792 alloc_declarator (size_t bytes)
794 return obstack_alloc (&declarator_obstack, bytes);
797 /* Allocate a declarator of the indicated KIND. Clear fields that are
798 common to all declarators. */
800 static cp_declarator *
801 make_declarator (cp_declarator_kind kind)
803 cp_declarator *declarator;
805 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
806 declarator->kind = kind;
807 declarator->attributes = NULL_TREE;
808 declarator->declarator = NULL;
813 /* Make a declarator for a generalized identifier. If
814 QUALIFYING_SCOPE is non-NULL, the identifier is
815 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
816 UNQUALIFIED_NAME. SFK indicates the kind of special function this
819 static cp_declarator *
820 make_id_declarator (tree qualifying_scope, tree unqualified_name,
821 special_function_kind sfk)
823 cp_declarator *declarator;
825 /* It is valid to write:
827 class C { void f(); };
831 The standard is not clear about whether `typedef const C D' is
832 legal; as of 2002-09-15 the committee is considering that
833 question. EDG 3.0 allows that syntax. Therefore, we do as
835 if (qualifying_scope && TYPE_P (qualifying_scope))
836 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
838 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
839 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
840 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
842 declarator = make_declarator (cdk_id);
843 declarator->u.id.qualifying_scope = qualifying_scope;
844 declarator->u.id.unqualified_name = unqualified_name;
845 declarator->u.id.sfk = sfk;
850 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
851 of modifiers such as const or volatile to apply to the pointer
852 type, represented as identifiers. */
855 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
857 cp_declarator *declarator;
859 declarator = make_declarator (cdk_pointer);
860 declarator->declarator = target;
861 declarator->u.pointer.qualifiers = cv_qualifiers;
862 declarator->u.pointer.class_type = NULL_TREE;
867 /* Like make_pointer_declarator -- but for references. */
870 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
872 cp_declarator *declarator;
874 declarator = make_declarator (cdk_reference);
875 declarator->declarator = target;
876 declarator->u.pointer.qualifiers = cv_qualifiers;
877 declarator->u.pointer.class_type = NULL_TREE;
882 /* Like make_pointer_declarator -- but for a pointer to a non-static
883 member of CLASS_TYPE. */
886 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
887 cp_declarator *pointee)
889 cp_declarator *declarator;
891 declarator = make_declarator (cdk_ptrmem);
892 declarator->declarator = pointee;
893 declarator->u.pointer.qualifiers = cv_qualifiers;
894 declarator->u.pointer.class_type = class_type;
899 /* Make a declarator for the function given by TARGET, with the
900 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
901 "const"-qualified member function. The EXCEPTION_SPECIFICATION
902 indicates what exceptions can be thrown. */
905 make_call_declarator (cp_declarator *target,
906 cp_parameter_declarator *parms,
907 cp_cv_quals cv_qualifiers,
908 tree exception_specification)
910 cp_declarator *declarator;
912 declarator = make_declarator (cdk_function);
913 declarator->declarator = target;
914 declarator->u.function.parameters = parms;
915 declarator->u.function.qualifiers = cv_qualifiers;
916 declarator->u.function.exception_specification = exception_specification;
921 /* Make a declarator for an array of BOUNDS elements, each of which is
922 defined by ELEMENT. */
925 make_array_declarator (cp_declarator *element, tree bounds)
927 cp_declarator *declarator;
929 declarator = make_declarator (cdk_array);
930 declarator->declarator = element;
931 declarator->u.array.bounds = bounds;
936 cp_parameter_declarator *no_parameters;
938 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
939 DECLARATOR and DEFAULT_ARGUMENT. */
941 cp_parameter_declarator *
942 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
943 cp_declarator *declarator,
944 tree default_argument)
946 cp_parameter_declarator *parameter;
948 parameter = ((cp_parameter_declarator *)
949 alloc_declarator (sizeof (cp_parameter_declarator)));
950 parameter->next = NULL;
952 parameter->decl_specifiers = *decl_specifiers;
954 clear_decl_specs (¶meter->decl_specifiers);
955 parameter->declarator = declarator;
956 parameter->default_argument = default_argument;
957 parameter->ellipsis_p = false;
967 A cp_parser parses the token stream as specified by the C++
968 grammar. Its job is purely parsing, not semantic analysis. For
969 example, the parser breaks the token stream into declarators,
970 expressions, statements, and other similar syntactic constructs.
971 It does not check that the types of the expressions on either side
972 of an assignment-statement are compatible, or that a function is
973 not declared with a parameter of type `void'.
975 The parser invokes routines elsewhere in the compiler to perform
976 semantic analysis and to build up the abstract syntax tree for the
979 The parser (and the template instantiation code, which is, in a
980 way, a close relative of parsing) are the only parts of the
981 compiler that should be calling push_scope and pop_scope, or
982 related functions. The parser (and template instantiation code)
983 keeps track of what scope is presently active; everything else
984 should simply honor that. (The code that generates static
985 initializers may also need to set the scope, in order to check
986 access control correctly when emitting the initializers.)
991 The parser is of the standard recursive-descent variety. Upcoming
992 tokens in the token stream are examined in order to determine which
993 production to use when parsing a non-terminal. Some C++ constructs
994 require arbitrary look ahead to disambiguate. For example, it is
995 impossible, in the general case, to tell whether a statement is an
996 expression or declaration without scanning the entire statement.
997 Therefore, the parser is capable of "parsing tentatively." When the
998 parser is not sure what construct comes next, it enters this mode.
999 Then, while we attempt to parse the construct, the parser queues up
1000 error messages, rather than issuing them immediately, and saves the
1001 tokens it consumes. If the construct is parsed successfully, the
1002 parser "commits", i.e., it issues any queued error messages and
1003 the tokens that were being preserved are permanently discarded.
1004 If, however, the construct is not parsed successfully, the parser
1005 rolls back its state completely so that it can resume parsing using
1006 a different alternative.
1011 The performance of the parser could probably be improved substantially.
1012 We could often eliminate the need to parse tentatively by looking ahead
1013 a little bit. In some places, this approach might not entirely eliminate
1014 the need to parse tentatively, but it might still speed up the average
1017 /* Flags that are passed to some parsing functions. These values can
1018 be bitwise-ored together. */
1020 typedef enum cp_parser_flags
1023 CP_PARSER_FLAGS_NONE = 0x0,
1024 /* The construct is optional. If it is not present, then no error
1025 should be issued. */
1026 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1027 /* When parsing a type-specifier, do not allow user-defined types. */
1028 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1031 /* The different kinds of declarators we want to parse. */
1033 typedef enum cp_parser_declarator_kind
1035 /* We want an abstract declarator. */
1036 CP_PARSER_DECLARATOR_ABSTRACT,
1037 /* We want a named declarator. */
1038 CP_PARSER_DECLARATOR_NAMED,
1039 /* We don't mind, but the name must be an unqualified-id. */
1040 CP_PARSER_DECLARATOR_EITHER
1041 } cp_parser_declarator_kind;
1043 /* The precedence values used to parse binary expressions. The minimum value
1044 of PREC must be 1, because zero is reserved to quickly discriminate
1045 binary operators from other tokens. */
1050 PREC_LOGICAL_OR_EXPRESSION,
1051 PREC_LOGICAL_AND_EXPRESSION,
1052 PREC_INCLUSIVE_OR_EXPRESSION,
1053 PREC_EXCLUSIVE_OR_EXPRESSION,
1054 PREC_AND_EXPRESSION,
1055 PREC_EQUALITY_EXPRESSION,
1056 PREC_RELATIONAL_EXPRESSION,
1057 PREC_SHIFT_EXPRESSION,
1058 PREC_ADDITIVE_EXPRESSION,
1059 PREC_MULTIPLICATIVE_EXPRESSION,
1061 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1064 /* A mapping from a token type to a corresponding tree node type, with a
1065 precedence value. */
1067 typedef struct cp_parser_binary_operations_map_node
1069 /* The token type. */
1070 enum cpp_ttype token_type;
1071 /* The corresponding tree code. */
1072 enum tree_code tree_type;
1073 /* The precedence of this operator. */
1074 enum cp_parser_prec prec;
1075 } cp_parser_binary_operations_map_node;
1077 /* The status of a tentative parse. */
1079 typedef enum cp_parser_status_kind
1081 /* No errors have occurred. */
1082 CP_PARSER_STATUS_KIND_NO_ERROR,
1083 /* An error has occurred. */
1084 CP_PARSER_STATUS_KIND_ERROR,
1085 /* We are committed to this tentative parse, whether or not an error
1087 CP_PARSER_STATUS_KIND_COMMITTED
1088 } cp_parser_status_kind;
1090 typedef struct cp_parser_expression_stack_entry
1093 enum tree_code tree_type;
1095 } cp_parser_expression_stack_entry;
1097 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1098 entries because precedence levels on the stack are monotonically
1100 typedef struct cp_parser_expression_stack_entry
1101 cp_parser_expression_stack[NUM_PREC_VALUES];
1103 /* Context that is saved and restored when parsing tentatively. */
1104 typedef struct cp_parser_context GTY (())
1106 /* If this is a tentative parsing context, the status of the
1108 enum cp_parser_status_kind status;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1115 /* The next parsing context in the stack. */
1116 struct cp_parser_context *next;
1117 } cp_parser_context;
1121 /* Constructors and destructors. */
1123 static cp_parser_context *cp_parser_context_new
1124 (cp_parser_context *);
1126 /* Class variables. */
1128 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1130 /* The operator-precedence table used by cp_parser_binary_expression.
1131 Transformed into an associative array (binops_by_token) by
1134 static const cp_parser_binary_operations_map_node binops[] = {
1135 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1136 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1138 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1142 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1145 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1148 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1153 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1155 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1156 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1158 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1160 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1162 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1164 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1166 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1169 /* The same as binops, but initialized by cp_parser_new so that
1170 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1172 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1174 /* Constructors and destructors. */
1176 /* Construct a new context. The context below this one on the stack
1177 is given by NEXT. */
1179 static cp_parser_context *
1180 cp_parser_context_new (cp_parser_context* next)
1182 cp_parser_context *context;
1184 /* Allocate the storage. */
1185 if (cp_parser_context_free_list != NULL)
1187 /* Pull the first entry from the free list. */
1188 context = cp_parser_context_free_list;
1189 cp_parser_context_free_list = context->next;
1190 memset (context, 0, sizeof (*context));
1193 context = GGC_CNEW (cp_parser_context);
1195 /* No errors have occurred yet in this context. */
1196 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1197 /* If this is not the bottomost context, copy information that we
1198 need from the previous context. */
1201 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1202 expression, then we are parsing one in this context, too. */
1203 context->object_type = next->object_type;
1204 /* Thread the stack. */
1205 context->next = next;
1211 /* The cp_parser structure represents the C++ parser. */
1213 typedef struct cp_parser GTY(())
1215 /* The lexer from which we are obtaining tokens. */
1218 /* The scope in which names should be looked up. If NULL_TREE, then
1219 we look up names in the scope that is currently open in the
1220 source program. If non-NULL, this is either a TYPE or
1221 NAMESPACE_DECL for the scope in which we should look. It can
1222 also be ERROR_MARK, when we've parsed a bogus scope.
1224 This value is not cleared automatically after a name is looked
1225 up, so we must be careful to clear it before starting a new look
1226 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1227 will look up `Z' in the scope of `X', rather than the current
1228 scope.) Unfortunately, it is difficult to tell when name lookup
1229 is complete, because we sometimes peek at a token, look it up,
1230 and then decide not to consume it. */
1233 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1234 last lookup took place. OBJECT_SCOPE is used if an expression
1235 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1236 respectively. QUALIFYING_SCOPE is used for an expression of the
1237 form "X::Y"; it refers to X. */
1239 tree qualifying_scope;
1241 /* A stack of parsing contexts. All but the bottom entry on the
1242 stack will be tentative contexts.
1244 We parse tentatively in order to determine which construct is in
1245 use in some situations. For example, in order to determine
1246 whether a statement is an expression-statement or a
1247 declaration-statement we parse it tentatively as a
1248 declaration-statement. If that fails, we then reparse the same
1249 token stream as an expression-statement. */
1250 cp_parser_context *context;
1252 /* True if we are parsing GNU C++. If this flag is not set, then
1253 GNU extensions are not recognized. */
1254 bool allow_gnu_extensions_p;
1256 /* TRUE if the `>' token should be interpreted as the greater-than
1257 operator. FALSE if it is the end of a template-id or
1258 template-parameter-list. */
1259 bool greater_than_is_operator_p;
1261 /* TRUE if default arguments are allowed within a parameter list
1262 that starts at this point. FALSE if only a gnu extension makes
1263 them permissible. */
1264 bool default_arg_ok_p;
1266 /* TRUE if we are parsing an integral constant-expression. See
1267 [expr.const] for a precise definition. */
1268 bool integral_constant_expression_p;
1270 /* TRUE if we are parsing an integral constant-expression -- but a
1271 non-constant expression should be permitted as well. This flag
1272 is used when parsing an array bound so that GNU variable-length
1273 arrays are tolerated. */
1274 bool allow_non_integral_constant_expression_p;
1276 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1277 been seen that makes the expression non-constant. */
1278 bool non_integral_constant_expression_p;
1280 /* TRUE if local variable names and `this' are forbidden in the
1282 bool local_variables_forbidden_p;
1284 /* TRUE if the declaration we are parsing is part of a
1285 linkage-specification of the form `extern string-literal
1287 bool in_unbraced_linkage_specification_p;
1289 /* TRUE if we are presently parsing a declarator, after the
1290 direct-declarator. */
1291 bool in_declarator_p;
1293 /* TRUE if we are presently parsing a template-argument-list. */
1294 bool in_template_argument_list_p;
1296 /* TRUE if we are presently parsing the body of an
1297 iteration-statement. */
1298 bool in_iteration_statement_p;
1300 /* TRUE if we are presently parsing the body of a switch
1302 bool in_switch_statement_p;
1304 /* TRUE if we are parsing a type-id in an expression context. In
1305 such a situation, both "type (expr)" and "type (type)" are valid
1307 bool in_type_id_in_expr_p;
1309 /* TRUE if we are currently in a header file where declarations are
1310 implicitly extern "C". */
1311 bool implicit_extern_c;
1313 /* TRUE if strings in expressions should be translated to the execution
1315 bool translate_strings_p;
1317 /* If non-NULL, then we are parsing a construct where new type
1318 definitions are not permitted. The string stored here will be
1319 issued as an error message if a type is defined. */
1320 const char *type_definition_forbidden_message;
1322 /* A list of lists. The outer list is a stack, used for member
1323 functions of local classes. At each level there are two sub-list,
1324 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1325 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1326 TREE_VALUE's. The functions are chained in reverse declaration
1329 The TREE_PURPOSE sublist contains those functions with default
1330 arguments that need post processing, and the TREE_VALUE sublist
1331 contains those functions with definitions that need post
1334 These lists can only be processed once the outermost class being
1335 defined is complete. */
1336 tree unparsed_functions_queues;
1338 /* The number of classes whose definitions are currently in
1340 unsigned num_classes_being_defined;
1342 /* The number of template parameter lists that apply directly to the
1343 current declaration. */
1344 unsigned num_template_parameter_lists;
1349 /* Constructors and destructors. */
1351 static cp_parser *cp_parser_new
1354 /* Routines to parse various constructs.
1356 Those that return `tree' will return the error_mark_node (rather
1357 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1358 Sometimes, they will return an ordinary node if error-recovery was
1359 attempted, even though a parse error occurred. So, to check
1360 whether or not a parse error occurred, you should always use
1361 cp_parser_error_occurred. If the construct is optional (indicated
1362 either by an `_opt' in the name of the function that does the
1363 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1364 the construct is not present. */
1366 /* Lexical conventions [gram.lex] */
1368 static tree cp_parser_identifier
1370 static tree cp_parser_string_literal
1371 (cp_parser *, bool, bool);
1373 /* Basic concepts [gram.basic] */
1375 static bool cp_parser_translation_unit
1378 /* Expressions [gram.expr] */
1380 static tree cp_parser_primary_expression
1381 (cp_parser *, bool, bool, bool, cp_id_kind *);
1382 static tree cp_parser_id_expression
1383 (cp_parser *, bool, bool, bool *, bool);
1384 static tree cp_parser_unqualified_id
1385 (cp_parser *, bool, bool, bool);
1386 static tree cp_parser_nested_name_specifier_opt
1387 (cp_parser *, bool, bool, bool, bool);
1388 static tree cp_parser_nested_name_specifier
1389 (cp_parser *, bool, bool, bool, bool);
1390 static tree cp_parser_class_or_namespace_name
1391 (cp_parser *, bool, bool, bool, bool, bool);
1392 static tree cp_parser_postfix_expression
1393 (cp_parser *, bool, bool);
1394 static tree cp_parser_postfix_open_square_expression
1395 (cp_parser *, tree, bool);
1396 static tree cp_parser_postfix_dot_deref_expression
1397 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1398 static tree cp_parser_parenthesized_expression_list
1399 (cp_parser *, bool, bool, bool *);
1400 static void cp_parser_pseudo_destructor_name
1401 (cp_parser *, tree *, tree *);
1402 static tree cp_parser_unary_expression
1403 (cp_parser *, bool, bool);
1404 static enum tree_code cp_parser_unary_operator
1406 static tree cp_parser_new_expression
1408 static tree cp_parser_new_placement
1410 static tree cp_parser_new_type_id
1411 (cp_parser *, tree *);
1412 static cp_declarator *cp_parser_new_declarator_opt
1414 static cp_declarator *cp_parser_direct_new_declarator
1416 static tree cp_parser_new_initializer
1418 static tree cp_parser_delete_expression
1420 static tree cp_parser_cast_expression
1421 (cp_parser *, bool, bool);
1422 static tree cp_parser_binary_expression
1423 (cp_parser *, bool);
1424 static tree cp_parser_question_colon_clause
1425 (cp_parser *, tree);
1426 static tree cp_parser_assignment_expression
1427 (cp_parser *, bool);
1428 static enum tree_code cp_parser_assignment_operator_opt
1430 static tree cp_parser_expression
1431 (cp_parser *, bool);
1432 static tree cp_parser_constant_expression
1433 (cp_parser *, bool, bool *);
1434 static tree cp_parser_builtin_offsetof
1437 /* Statements [gram.stmt.stmt] */
1439 static void cp_parser_statement
1440 (cp_parser *, tree, bool);
1441 static tree cp_parser_labeled_statement
1442 (cp_parser *, tree, bool);
1443 static tree cp_parser_expression_statement
1444 (cp_parser *, tree);
1445 static tree cp_parser_compound_statement
1446 (cp_parser *, tree, bool);
1447 static void cp_parser_statement_seq_opt
1448 (cp_parser *, tree);
1449 static tree cp_parser_selection_statement
1451 static tree cp_parser_condition
1453 static tree cp_parser_iteration_statement
1455 static void cp_parser_for_init_statement
1457 static tree cp_parser_jump_statement
1459 static void cp_parser_declaration_statement
1462 static tree cp_parser_implicitly_scoped_statement
1464 static void cp_parser_already_scoped_statement
1467 /* Declarations [gram.dcl.dcl] */
1469 static void cp_parser_declaration_seq_opt
1471 static void cp_parser_declaration
1473 static void cp_parser_block_declaration
1474 (cp_parser *, bool);
1475 static void cp_parser_simple_declaration
1476 (cp_parser *, bool);
1477 static void cp_parser_decl_specifier_seq
1478 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1479 static tree cp_parser_storage_class_specifier_opt
1481 static tree cp_parser_function_specifier_opt
1482 (cp_parser *, cp_decl_specifier_seq *);
1483 static tree cp_parser_type_specifier
1484 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1486 static tree cp_parser_simple_type_specifier
1487 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1488 static tree cp_parser_type_name
1490 static tree cp_parser_elaborated_type_specifier
1491 (cp_parser *, bool, bool);
1492 static tree cp_parser_enum_specifier
1494 static void cp_parser_enumerator_list
1495 (cp_parser *, tree);
1496 static void cp_parser_enumerator_definition
1497 (cp_parser *, tree);
1498 static tree cp_parser_namespace_name
1500 static void cp_parser_namespace_definition
1502 static void cp_parser_namespace_body
1504 static tree cp_parser_qualified_namespace_specifier
1506 static void cp_parser_namespace_alias_definition
1508 static void cp_parser_using_declaration
1510 static void cp_parser_using_directive
1512 static void cp_parser_asm_definition
1514 static void cp_parser_linkage_specification
1517 /* Declarators [gram.dcl.decl] */
1519 static tree cp_parser_init_declarator
1520 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1521 static cp_declarator *cp_parser_declarator
1522 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1523 static cp_declarator *cp_parser_direct_declarator
1524 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1525 static enum tree_code cp_parser_ptr_operator
1526 (cp_parser *, tree *, cp_cv_quals *);
1527 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1529 static tree cp_parser_declarator_id
1531 static tree cp_parser_type_id
1533 static void cp_parser_type_specifier_seq
1534 (cp_parser *, bool, cp_decl_specifier_seq *);
1535 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1537 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1538 (cp_parser *, bool *);
1539 static cp_parameter_declarator *cp_parser_parameter_declaration
1540 (cp_parser *, bool, bool *);
1541 static void cp_parser_function_body
1543 static tree cp_parser_initializer
1544 (cp_parser *, bool *, bool *);
1545 static tree cp_parser_initializer_clause
1546 (cp_parser *, bool *);
1547 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1548 (cp_parser *, bool *);
1550 static bool cp_parser_ctor_initializer_opt_and_function_body
1553 /* Classes [gram.class] */
1555 static tree cp_parser_class_name
1556 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1557 static tree cp_parser_class_specifier
1559 static tree cp_parser_class_head
1560 (cp_parser *, bool *, tree *);
1561 static enum tag_types cp_parser_class_key
1563 static void cp_parser_member_specification_opt
1565 static void cp_parser_member_declaration
1567 static tree cp_parser_pure_specifier
1569 static tree cp_parser_constant_initializer
1572 /* Derived classes [gram.class.derived] */
1574 static tree cp_parser_base_clause
1576 static tree cp_parser_base_specifier
1579 /* Special member functions [gram.special] */
1581 static tree cp_parser_conversion_function_id
1583 static tree cp_parser_conversion_type_id
1585 static cp_declarator *cp_parser_conversion_declarator_opt
1587 static bool cp_parser_ctor_initializer_opt
1589 static void cp_parser_mem_initializer_list
1591 static tree cp_parser_mem_initializer
1593 static tree cp_parser_mem_initializer_id
1596 /* Overloading [gram.over] */
1598 static tree cp_parser_operator_function_id
1600 static tree cp_parser_operator
1603 /* Templates [gram.temp] */
1605 static void cp_parser_template_declaration
1606 (cp_parser *, bool);
1607 static tree cp_parser_template_parameter_list
1609 static tree cp_parser_template_parameter
1610 (cp_parser *, bool *);
1611 static tree cp_parser_type_parameter
1613 static tree cp_parser_template_id
1614 (cp_parser *, bool, bool, bool);
1615 static tree cp_parser_template_name
1616 (cp_parser *, bool, bool, bool, bool *);
1617 static tree cp_parser_template_argument_list
1619 static tree cp_parser_template_argument
1621 static void cp_parser_explicit_instantiation
1623 static void cp_parser_explicit_specialization
1626 /* Exception handling [gram.exception] */
1628 static tree cp_parser_try_block
1630 static bool cp_parser_function_try_block
1632 static void cp_parser_handler_seq
1634 static void cp_parser_handler
1636 static tree cp_parser_exception_declaration
1638 static tree cp_parser_throw_expression
1640 static tree cp_parser_exception_specification_opt
1642 static tree cp_parser_type_id_list
1645 /* GNU Extensions */
1647 static tree cp_parser_asm_specification_opt
1649 static tree cp_parser_asm_operand_list
1651 static tree cp_parser_asm_clobber_list
1653 static tree cp_parser_attributes_opt
1655 static tree cp_parser_attribute_list
1657 static bool cp_parser_extension_opt
1658 (cp_parser *, int *);
1659 static void cp_parser_label_declaration
1662 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1663 static bool cp_parser_pragma
1664 (cp_parser *, enum pragma_context);
1666 /* Objective-C++ Productions */
1668 static tree cp_parser_objc_message_receiver
1670 static tree cp_parser_objc_message_args
1672 static tree cp_parser_objc_message_expression
1674 static tree cp_parser_objc_encode_expression
1676 static tree cp_parser_objc_defs_expression
1678 static tree cp_parser_objc_protocol_expression
1680 static tree cp_parser_objc_selector_expression
1682 static tree cp_parser_objc_expression
1684 static bool cp_parser_objc_selector_p
1686 static tree cp_parser_objc_selector
1688 static tree cp_parser_objc_protocol_refs_opt
1690 static void cp_parser_objc_declaration
1692 static tree cp_parser_objc_statement
1695 /* Utility Routines */
1697 static tree cp_parser_lookup_name
1698 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1699 static tree cp_parser_lookup_name_simple
1700 (cp_parser *, tree);
1701 static tree cp_parser_maybe_treat_template_as_class
1703 static bool cp_parser_check_declarator_template_parameters
1704 (cp_parser *, cp_declarator *);
1705 static bool cp_parser_check_template_parameters
1706 (cp_parser *, unsigned);
1707 static tree cp_parser_simple_cast_expression
1709 static tree cp_parser_global_scope_opt
1710 (cp_parser *, bool);
1711 static bool cp_parser_constructor_declarator_p
1712 (cp_parser *, bool);
1713 static tree cp_parser_function_definition_from_specifiers_and_declarator
1714 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1715 static tree cp_parser_function_definition_after_declarator
1716 (cp_parser *, bool);
1717 static void cp_parser_template_declaration_after_export
1718 (cp_parser *, bool);
1719 static tree cp_parser_single_declaration
1720 (cp_parser *, bool, bool *);
1721 static tree cp_parser_functional_cast
1722 (cp_parser *, tree);
1723 static tree cp_parser_save_member_function_body
1724 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1725 static tree cp_parser_enclosed_template_argument_list
1727 static void cp_parser_save_default_args
1728 (cp_parser *, tree);
1729 static void cp_parser_late_parsing_for_member
1730 (cp_parser *, tree);
1731 static void cp_parser_late_parsing_default_args
1732 (cp_parser *, tree);
1733 static tree cp_parser_sizeof_operand
1734 (cp_parser *, enum rid);
1735 static bool cp_parser_declares_only_class_p
1737 static void cp_parser_set_storage_class
1738 (cp_decl_specifier_seq *, cp_storage_class);
1739 static void cp_parser_set_decl_spec_type
1740 (cp_decl_specifier_seq *, tree, bool);
1741 static bool cp_parser_friend_p
1742 (const cp_decl_specifier_seq *);
1743 static cp_token *cp_parser_require
1744 (cp_parser *, enum cpp_ttype, const char *);
1745 static cp_token *cp_parser_require_keyword
1746 (cp_parser *, enum rid, const char *);
1747 static bool cp_parser_token_starts_function_definition_p
1749 static bool cp_parser_next_token_starts_class_definition_p
1751 static bool cp_parser_next_token_ends_template_argument_p
1753 static bool cp_parser_nth_token_starts_template_argument_list_p
1754 (cp_parser *, size_t);
1755 static enum tag_types cp_parser_token_is_class_key
1757 static void cp_parser_check_class_key
1758 (enum tag_types, tree type);
1759 static void cp_parser_check_access_in_redeclaration
1761 static bool cp_parser_optional_template_keyword
1763 static void cp_parser_pre_parsed_nested_name_specifier
1765 static void cp_parser_cache_group
1766 (cp_parser *, enum cpp_ttype, unsigned);
1767 static void cp_parser_parse_tentatively
1769 static void cp_parser_commit_to_tentative_parse
1771 static void cp_parser_abort_tentative_parse
1773 static bool cp_parser_parse_definitely
1775 static inline bool cp_parser_parsing_tentatively
1777 static bool cp_parser_uncommitted_to_tentative_parse_p
1779 static void cp_parser_error
1780 (cp_parser *, const char *);
1781 static void cp_parser_name_lookup_error
1782 (cp_parser *, tree, tree, const char *);
1783 static bool cp_parser_simulate_error
1785 static void cp_parser_check_type_definition
1787 static void cp_parser_check_for_definition_in_return_type
1788 (cp_declarator *, tree);
1789 static void cp_parser_check_for_invalid_template_id
1790 (cp_parser *, tree);
1791 static bool cp_parser_non_integral_constant_expression
1792 (cp_parser *, const char *);
1793 static void cp_parser_diagnose_invalid_type_name
1794 (cp_parser *, tree, tree);
1795 static bool cp_parser_parse_and_diagnose_invalid_type_name
1797 static int cp_parser_skip_to_closing_parenthesis
1798 (cp_parser *, bool, bool, bool);
1799 static void cp_parser_skip_to_end_of_statement
1801 static void cp_parser_consume_semicolon_at_end_of_statement
1803 static void cp_parser_skip_to_end_of_block_or_statement
1805 static void cp_parser_skip_to_closing_brace
1807 static void cp_parser_skip_until_found
1808 (cp_parser *, enum cpp_ttype, const char *);
1809 static void cp_parser_skip_to_pragma_eol
1810 (cp_parser*, cp_token *);
1811 static bool cp_parser_error_occurred
1813 static bool cp_parser_allow_gnu_extensions_p
1815 static bool cp_parser_is_string_literal
1817 static bool cp_parser_is_keyword
1818 (cp_token *, enum rid);
1819 static tree cp_parser_make_typename_type
1820 (cp_parser *, tree, tree);
1822 /* Returns nonzero if we are parsing tentatively. */
1825 cp_parser_parsing_tentatively (cp_parser* parser)
1827 return parser->context->next != NULL;
1830 /* Returns nonzero if TOKEN is a string literal. */
1833 cp_parser_is_string_literal (cp_token* token)
1835 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1838 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1841 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1843 return token->keyword == keyword;
1846 /* A minimum or maximum operator has been seen. As these are
1847 deprecated, issue a warning. */
1850 cp_parser_warn_min_max (void)
1852 if (warn_deprecated && !in_system_header)
1853 warning (OPT_Wdeprecated, "minimum/maximum operators are deprecated");
1856 /* If not parsing tentatively, issue a diagnostic of the form
1857 FILE:LINE: MESSAGE before TOKEN
1858 where TOKEN is the next token in the input stream. MESSAGE
1859 (specified by the caller) is usually of the form "expected
1863 cp_parser_error (cp_parser* parser, const char* message)
1865 if (!cp_parser_simulate_error (parser))
1867 cp_token *token = cp_lexer_peek_token (parser->lexer);
1868 /* This diagnostic makes more sense if it is tagged to the line
1869 of the token we just peeked at. */
1870 cp_lexer_set_source_position_from_token (token);
1872 if (token->type == CPP_PRAGMA)
1874 error ("%<#pragma%> is not allowed here");
1875 cp_parser_skip_to_pragma_eol (parser, token);
1879 c_parse_error (message,
1880 /* Because c_parser_error does not understand
1881 CPP_KEYWORD, keywords are treated like
1883 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1888 /* Issue an error about name-lookup failing. NAME is the
1889 IDENTIFIER_NODE DECL is the result of
1890 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1891 the thing that we hoped to find. */
1894 cp_parser_name_lookup_error (cp_parser* parser,
1897 const char* desired)
1899 /* If name lookup completely failed, tell the user that NAME was not
1901 if (decl == error_mark_node)
1903 if (parser->scope && parser->scope != global_namespace)
1904 error ("%<%D::%D%> has not been declared",
1905 parser->scope, name);
1906 else if (parser->scope == global_namespace)
1907 error ("%<::%D%> has not been declared", name);
1908 else if (parser->object_scope
1909 && !CLASS_TYPE_P (parser->object_scope))
1910 error ("request for member %qD in non-class type %qT",
1911 name, parser->object_scope);
1912 else if (parser->object_scope)
1913 error ("%<%T::%D%> has not been declared",
1914 parser->object_scope, name);
1916 error ("%qD has not been declared", name);
1918 else if (parser->scope && parser->scope != global_namespace)
1919 error ("%<%D::%D%> %s", parser->scope, name, desired);
1920 else if (parser->scope == global_namespace)
1921 error ("%<::%D%> %s", name, desired);
1923 error ("%qD %s", name, desired);
1926 /* If we are parsing tentatively, remember that an error has occurred
1927 during this tentative parse. Returns true if the error was
1928 simulated; false if a message should be issued by the caller. */
1931 cp_parser_simulate_error (cp_parser* parser)
1933 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1935 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1941 /* This function is called when a type is defined. If type
1942 definitions are forbidden at this point, an error message is
1946 cp_parser_check_type_definition (cp_parser* parser)
1948 /* If types are forbidden here, issue a message. */
1949 if (parser->type_definition_forbidden_message)
1950 /* Use `%s' to print the string in case there are any escape
1951 characters in the message. */
1952 error ("%s", parser->type_definition_forbidden_message);
1955 /* This function is called when the DECLARATOR is processed. The TYPE
1956 was a type defined in the decl-specifiers. If it is invalid to
1957 define a type in the decl-specifiers for DECLARATOR, an error is
1961 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1964 /* [dcl.fct] forbids type definitions in return types.
1965 Unfortunately, it's not easy to know whether or not we are
1966 processing a return type until after the fact. */
1968 && (declarator->kind == cdk_pointer
1969 || declarator->kind == cdk_reference
1970 || declarator->kind == cdk_ptrmem))
1971 declarator = declarator->declarator;
1973 && declarator->kind == cdk_function)
1975 error ("new types may not be defined in a return type");
1976 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1981 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1982 "<" in any valid C++ program. If the next token is indeed "<",
1983 issue a message warning the user about what appears to be an
1984 invalid attempt to form a template-id. */
1987 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1990 cp_token_position start = 0;
1992 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1995 error ("%qT is not a template", type);
1996 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1997 error ("%qE is not a template", type);
1999 error ("invalid template-id");
2000 /* Remember the location of the invalid "<". */
2001 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2002 start = cp_lexer_token_position (parser->lexer, true);
2003 /* Consume the "<". */
2004 cp_lexer_consume_token (parser->lexer);
2005 /* Parse the template arguments. */
2006 cp_parser_enclosed_template_argument_list (parser);
2007 /* Permanently remove the invalid template arguments so that
2008 this error message is not issued again. */
2010 cp_lexer_purge_tokens_after (parser->lexer, start);
2014 /* If parsing an integral constant-expression, issue an error message
2015 about the fact that THING appeared and return true. Otherwise,
2016 return false. In either case, set
2017 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2020 cp_parser_non_integral_constant_expression (cp_parser *parser,
2023 parser->non_integral_constant_expression_p = true;
2024 if (parser->integral_constant_expression_p)
2026 if (!parser->allow_non_integral_constant_expression_p)
2028 error ("%s cannot appear in a constant-expression", thing);
2035 /* Emit a diagnostic for an invalid type name. SCOPE is the
2036 qualifying scope (or NULL, if none) for ID. This function commits
2037 to the current active tentative parse, if any. (Otherwise, the
2038 problematic construct might be encountered again later, resulting
2039 in duplicate error messages.) */
2042 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2044 tree decl, old_scope;
2045 /* Try to lookup the identifier. */
2046 old_scope = parser->scope;
2047 parser->scope = scope;
2048 decl = cp_parser_lookup_name_simple (parser, id);
2049 parser->scope = old_scope;
2050 /* If the lookup found a template-name, it means that the user forgot
2051 to specify an argument list. Emit a useful error message. */
2052 if (TREE_CODE (decl) == TEMPLATE_DECL)
2053 error ("invalid use of template-name %qE without an argument list",
2055 else if (!parser->scope)
2057 /* Issue an error message. */
2058 error ("%qE does not name a type", id);
2059 /* If we're in a template class, it's possible that the user was
2060 referring to a type from a base class. For example:
2062 template <typename T> struct A { typedef T X; };
2063 template <typename T> struct B : public A<T> { X x; };
2065 The user should have said "typename A<T>::X". */
2066 if (processing_template_decl && current_class_type
2067 && TYPE_BINFO (current_class_type))
2071 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2075 tree base_type = BINFO_TYPE (b);
2076 if (CLASS_TYPE_P (base_type)
2077 && dependent_type_p (base_type))
2080 /* Go from a particular instantiation of the
2081 template (which will have an empty TYPE_FIELDs),
2082 to the main version. */
2083 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2084 for (field = TYPE_FIELDS (base_type);
2086 field = TREE_CHAIN (field))
2087 if (TREE_CODE (field) == TYPE_DECL
2088 && DECL_NAME (field) == id)
2090 inform ("(perhaps %<typename %T::%E%> was intended)",
2091 BINFO_TYPE (b), id);
2100 /* Here we diagnose qualified-ids where the scope is actually correct,
2101 but the identifier does not resolve to a valid type name. */
2102 else if (parser->scope != error_mark_node)
2104 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2105 error ("%qE in namespace %qE does not name a type",
2107 else if (TYPE_P (parser->scope))
2108 error ("%qE in class %qT does not name a type", id, parser->scope);
2112 cp_parser_commit_to_tentative_parse (parser);
2115 /* Check for a common situation where a type-name should be present,
2116 but is not, and issue a sensible error message. Returns true if an
2117 invalid type-name was detected.
2119 The situation handled by this function are variable declarations of the
2120 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2121 Usually, `ID' should name a type, but if we got here it means that it
2122 does not. We try to emit the best possible error message depending on
2123 how exactly the id-expression looks like.
2127 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2131 cp_parser_parse_tentatively (parser);
2132 id = cp_parser_id_expression (parser,
2133 /*template_keyword_p=*/false,
2134 /*check_dependency_p=*/true,
2135 /*template_p=*/NULL,
2136 /*declarator_p=*/true);
2137 /* After the id-expression, there should be a plain identifier,
2138 otherwise this is not a simple variable declaration. Also, if
2139 the scope is dependent, we cannot do much. */
2140 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2141 || (parser->scope && TYPE_P (parser->scope)
2142 && dependent_type_p (parser->scope)))
2144 cp_parser_abort_tentative_parse (parser);
2147 if (!cp_parser_parse_definitely (parser)
2148 || TREE_CODE (id) != IDENTIFIER_NODE)
2151 /* Emit a diagnostic for the invalid type. */
2152 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2153 /* Skip to the end of the declaration; there's no point in
2154 trying to process it. */
2155 cp_parser_skip_to_end_of_block_or_statement (parser);
2159 /* Consume tokens up to, and including, the next non-nested closing `)'.
2160 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2161 are doing error recovery. Returns -1 if OR_COMMA is true and we
2162 found an unnested comma. */
2165 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2170 unsigned paren_depth = 0;
2171 unsigned brace_depth = 0;
2173 if (recovering && !or_comma
2174 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2179 cp_token * token = cp_lexer_peek_token (parser->lexer);
2181 switch (token->type)
2184 case CPP_PRAGMA_EOL:
2185 /* If we've run out of tokens, then there is no closing `)'. */
2189 /* This matches the processing in skip_to_end_of_statement. */
2194 case CPP_OPEN_BRACE:
2197 case CPP_CLOSE_BRACE:
2203 if (recovering && or_comma && !brace_depth && !paren_depth)
2207 case CPP_OPEN_PAREN:
2212 case CPP_CLOSE_PAREN:
2213 if (!brace_depth && !paren_depth--)
2216 cp_lexer_consume_token (parser->lexer);
2225 /* Consume the token. */
2226 cp_lexer_consume_token (parser->lexer);
2230 /* Consume tokens until we reach the end of the current statement.
2231 Normally, that will be just before consuming a `;'. However, if a
2232 non-nested `}' comes first, then we stop before consuming that. */
2235 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2237 unsigned nesting_depth = 0;
2241 cp_token *token = cp_lexer_peek_token (parser->lexer);
2243 switch (token->type)
2246 case CPP_PRAGMA_EOL:
2247 /* If we've run out of tokens, stop. */
2251 /* If the next token is a `;', we have reached the end of the
2257 case CPP_CLOSE_BRACE:
2258 /* If this is a non-nested '}', stop before consuming it.
2259 That way, when confronted with something like:
2263 we stop before consuming the closing '}', even though we
2264 have not yet reached a `;'. */
2265 if (nesting_depth == 0)
2268 /* If it is the closing '}' for a block that we have
2269 scanned, stop -- but only after consuming the token.
2275 we will stop after the body of the erroneously declared
2276 function, but before consuming the following `typedef'
2278 if (--nesting_depth == 0)
2280 cp_lexer_consume_token (parser->lexer);
2284 case CPP_OPEN_BRACE:
2292 /* Consume the token. */
2293 cp_lexer_consume_token (parser->lexer);
2297 /* This function is called at the end of a statement or declaration.
2298 If the next token is a semicolon, it is consumed; otherwise, error
2299 recovery is attempted. */
2302 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2304 /* Look for the trailing `;'. */
2305 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2307 /* If there is additional (erroneous) input, skip to the end of
2309 cp_parser_skip_to_end_of_statement (parser);
2310 /* If the next token is now a `;', consume it. */
2311 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2312 cp_lexer_consume_token (parser->lexer);
2316 /* Skip tokens until we have consumed an entire block, or until we
2317 have consumed a non-nested `;'. */
2320 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2322 int nesting_depth = 0;
2324 while (nesting_depth >= 0)
2326 cp_token *token = cp_lexer_peek_token (parser->lexer);
2328 switch (token->type)
2331 case CPP_PRAGMA_EOL:
2332 /* If we've run out of tokens, stop. */
2336 /* Stop if this is an unnested ';'. */
2341 case CPP_CLOSE_BRACE:
2342 /* Stop if this is an unnested '}', or closes the outermost
2349 case CPP_OPEN_BRACE:
2358 /* Consume the token. */
2359 cp_lexer_consume_token (parser->lexer);
2363 /* Skip tokens until a non-nested closing curly brace is the next
2367 cp_parser_skip_to_closing_brace (cp_parser *parser)
2369 unsigned nesting_depth = 0;
2373 cp_token *token = cp_lexer_peek_token (parser->lexer);
2375 switch (token->type)
2378 case CPP_PRAGMA_EOL:
2379 /* If we've run out of tokens, stop. */
2382 case CPP_CLOSE_BRACE:
2383 /* If the next token is a non-nested `}', then we have reached
2384 the end of the current block. */
2385 if (nesting_depth-- == 0)
2389 case CPP_OPEN_BRACE:
2390 /* If it the next token is a `{', then we are entering a new
2391 block. Consume the entire block. */
2399 /* Consume the token. */
2400 cp_lexer_consume_token (parser->lexer);
2404 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2405 parameter is the PRAGMA token, allowing us to purge the entire pragma
2409 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2413 parser->lexer->in_pragma = false;
2416 token = cp_lexer_consume_token (parser->lexer);
2417 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2419 /* Ensure that the pragma is not parsed again. */
2420 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2423 /* This is a simple wrapper around make_typename_type. When the id is
2424 an unresolved identifier node, we can provide a superior diagnostic
2425 using cp_parser_diagnose_invalid_type_name. */
2428 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2431 if (TREE_CODE (id) == IDENTIFIER_NODE)
2433 result = make_typename_type (scope, id, typename_type,
2434 /*complain=*/tf_none);
2435 if (result == error_mark_node)
2436 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2439 return make_typename_type (scope, id, typename_type, tf_error);
2443 /* Create a new C++ parser. */
2446 cp_parser_new (void)
2452 /* cp_lexer_new_main is called before calling ggc_alloc because
2453 cp_lexer_new_main might load a PCH file. */
2454 lexer = cp_lexer_new_main ();
2456 /* Initialize the binops_by_token so that we can get the tree
2457 directly from the token. */
2458 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2459 binops_by_token[binops[i].token_type] = binops[i];
2461 parser = GGC_CNEW (cp_parser);
2462 parser->lexer = lexer;
2463 parser->context = cp_parser_context_new (NULL);
2465 /* For now, we always accept GNU extensions. */
2466 parser->allow_gnu_extensions_p = 1;
2468 /* The `>' token is a greater-than operator, not the end of a
2470 parser->greater_than_is_operator_p = true;
2472 parser->default_arg_ok_p = true;
2474 /* We are not parsing a constant-expression. */
2475 parser->integral_constant_expression_p = false;
2476 parser->allow_non_integral_constant_expression_p = false;
2477 parser->non_integral_constant_expression_p = false;
2479 /* Local variable names are not forbidden. */
2480 parser->local_variables_forbidden_p = false;
2482 /* We are not processing an `extern "C"' declaration. */
2483 parser->in_unbraced_linkage_specification_p = false;
2485 /* We are not processing a declarator. */
2486 parser->in_declarator_p = false;
2488 /* We are not processing a template-argument-list. */
2489 parser->in_template_argument_list_p = false;
2491 /* We are not in an iteration statement. */
2492 parser->in_iteration_statement_p = false;
2494 /* We are not in a switch statement. */
2495 parser->in_switch_statement_p = false;
2497 /* We are not parsing a type-id inside an expression. */
2498 parser->in_type_id_in_expr_p = false;
2500 /* Declarations aren't implicitly extern "C". */
2501 parser->implicit_extern_c = false;
2503 /* String literals should be translated to the execution character set. */
2504 parser->translate_strings_p = true;
2506 /* The unparsed function queue is empty. */
2507 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2509 /* There are no classes being defined. */
2510 parser->num_classes_being_defined = 0;
2512 /* No template parameters apply. */
2513 parser->num_template_parameter_lists = 0;
2518 /* Create a cp_lexer structure which will emit the tokens in CACHE
2519 and push it onto the parser's lexer stack. This is used for delayed
2520 parsing of in-class method bodies and default arguments, and should
2521 not be confused with tentative parsing. */
2523 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2525 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2526 lexer->next = parser->lexer;
2527 parser->lexer = lexer;
2529 /* Move the current source position to that of the first token in the
2531 cp_lexer_set_source_position_from_token (lexer->next_token);
2534 /* Pop the top lexer off the parser stack. This is never used for the
2535 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2537 cp_parser_pop_lexer (cp_parser *parser)
2539 cp_lexer *lexer = parser->lexer;
2540 parser->lexer = lexer->next;
2541 cp_lexer_destroy (lexer);
2543 /* Put the current source position back where it was before this
2544 lexer was pushed. */
2545 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2548 /* Lexical conventions [gram.lex] */
2550 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2554 cp_parser_identifier (cp_parser* parser)
2558 /* Look for the identifier. */
2559 token = cp_parser_require (parser, CPP_NAME, "identifier");
2560 /* Return the value. */
2561 return token ? token->value : error_mark_node;
2564 /* Parse a sequence of adjacent string constants. Returns a
2565 TREE_STRING representing the combined, nul-terminated string
2566 constant. If TRANSLATE is true, translate the string to the
2567 execution character set. If WIDE_OK is true, a wide string is
2570 C++98 [lex.string] says that if a narrow string literal token is
2571 adjacent to a wide string literal token, the behavior is undefined.
2572 However, C99 6.4.5p4 says that this results in a wide string literal.
2573 We follow C99 here, for consistency with the C front end.
2575 This code is largely lifted from lex_string() in c-lex.c.
2577 FUTURE: ObjC++ will need to handle @-strings here. */
2579 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2584 struct obstack str_ob;
2585 cpp_string str, istr, *strs;
2588 tok = cp_lexer_peek_token (parser->lexer);
2589 if (!cp_parser_is_string_literal (tok))
2591 cp_parser_error (parser, "expected string-literal");
2592 return error_mark_node;
2595 /* Try to avoid the overhead of creating and destroying an obstack
2596 for the common case of just one string. */
2597 if (!cp_parser_is_string_literal
2598 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2600 cp_lexer_consume_token (parser->lexer);
2602 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2603 str.len = TREE_STRING_LENGTH (tok->value);
2605 if (tok->type == CPP_WSTRING)
2612 gcc_obstack_init (&str_ob);
2617 cp_lexer_consume_token (parser->lexer);
2619 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2620 str.len = TREE_STRING_LENGTH (tok->value);
2621 if (tok->type == CPP_WSTRING)
2624 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2626 tok = cp_lexer_peek_token (parser->lexer);
2628 while (cp_parser_is_string_literal (tok));
2630 strs = (cpp_string *) obstack_finish (&str_ob);
2633 if (wide && !wide_ok)
2635 cp_parser_error (parser, "a wide string is invalid in this context");
2639 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2640 (parse_in, strs, count, &istr, wide))
2642 value = build_string (istr.len, (char *)istr.text);
2643 free ((void *)istr.text);
2645 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2646 value = fix_string_type (value);
2649 /* cpp_interpret_string has issued an error. */
2650 value = error_mark_node;
2653 obstack_free (&str_ob, 0);
2659 /* Basic concepts [gram.basic] */
2661 /* Parse a translation-unit.
2664 declaration-seq [opt]
2666 Returns TRUE if all went well. */
2669 cp_parser_translation_unit (cp_parser* parser)
2671 /* The address of the first non-permanent object on the declarator
2673 static void *declarator_obstack_base;
2677 /* Create the declarator obstack, if necessary. */
2678 if (!cp_error_declarator)
2680 gcc_obstack_init (&declarator_obstack);
2681 /* Create the error declarator. */
2682 cp_error_declarator = make_declarator (cdk_error);
2683 /* Create the empty parameter list. */
2684 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2685 /* Remember where the base of the declarator obstack lies. */
2686 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2689 cp_parser_declaration_seq_opt (parser);
2691 /* If there are no tokens left then all went well. */
2692 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2694 /* Get rid of the token array; we don't need it any more. */
2695 cp_lexer_destroy (parser->lexer);
2696 parser->lexer = NULL;
2698 /* This file might have been a context that's implicitly extern
2699 "C". If so, pop the lang context. (Only relevant for PCH.) */
2700 if (parser->implicit_extern_c)
2702 pop_lang_context ();
2703 parser->implicit_extern_c = false;
2707 finish_translation_unit ();
2713 cp_parser_error (parser, "expected declaration");
2717 /* Make sure the declarator obstack was fully cleaned up. */
2718 gcc_assert (obstack_next_free (&declarator_obstack)
2719 == declarator_obstack_base);
2721 /* All went well. */
2725 /* Expressions [gram.expr] */
2727 /* Parse a primary-expression.
2738 ( compound-statement )
2739 __builtin_va_arg ( assignment-expression , type-id )
2740 __builtin_offsetof ( type-id , offsetof-expression )
2742 Objective-C++ Extension:
2750 ADDRESS_P is true iff this expression was immediately preceded by
2751 "&" and therefore might denote a pointer-to-member. CAST_P is true
2752 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2753 true iff this expression is a template argument.
2755 Returns a representation of the expression. Upon return, *IDK
2756 indicates what kind of id-expression (if any) was present. */
2759 cp_parser_primary_expression (cp_parser *parser,
2762 bool template_arg_p,
2767 /* Assume the primary expression is not an id-expression. */
2768 *idk = CP_ID_KIND_NONE;
2770 /* Peek at the next token. */
2771 token = cp_lexer_peek_token (parser->lexer);
2772 switch (token->type)
2783 token = cp_lexer_consume_token (parser->lexer);
2784 /* Floating-point literals are only allowed in an integral
2785 constant expression if they are cast to an integral or
2786 enumeration type. */
2787 if (TREE_CODE (token->value) == REAL_CST
2788 && parser->integral_constant_expression_p
2791 /* CAST_P will be set even in invalid code like "int(2.7 +
2792 ...)". Therefore, we have to check that the next token
2793 is sure to end the cast. */
2796 cp_token *next_token;
2798 next_token = cp_lexer_peek_token (parser->lexer);
2799 if (/* The comma at the end of an
2800 enumerator-definition. */
2801 next_token->type != CPP_COMMA
2802 /* The curly brace at the end of an enum-specifier. */
2803 && next_token->type != CPP_CLOSE_BRACE
2804 /* The end of a statement. */
2805 && next_token->type != CPP_SEMICOLON
2806 /* The end of the cast-expression. */
2807 && next_token->type != CPP_CLOSE_PAREN
2808 /* The end of an array bound. */
2809 && next_token->type != CPP_CLOSE_SQUARE
2810 /* The closing ">" in a template-argument-list. */
2811 && (next_token->type != CPP_GREATER
2812 || parser->greater_than_is_operator_p))
2816 /* If we are within a cast, then the constraint that the
2817 cast is to an integral or enumeration type will be
2818 checked at that point. If we are not within a cast, then
2819 this code is invalid. */
2821 cp_parser_non_integral_constant_expression
2822 (parser, "floating-point literal");
2824 return token->value;
2828 /* ??? Should wide strings be allowed when parser->translate_strings_p
2829 is false (i.e. in attributes)? If not, we can kill the third
2830 argument to cp_parser_string_literal. */
2831 return cp_parser_string_literal (parser,
2832 parser->translate_strings_p,
2835 case CPP_OPEN_PAREN:
2838 bool saved_greater_than_is_operator_p;
2840 /* Consume the `('. */
2841 cp_lexer_consume_token (parser->lexer);
2842 /* Within a parenthesized expression, a `>' token is always
2843 the greater-than operator. */
2844 saved_greater_than_is_operator_p
2845 = parser->greater_than_is_operator_p;
2846 parser->greater_than_is_operator_p = true;
2847 /* If we see `( { ' then we are looking at the beginning of
2848 a GNU statement-expression. */
2849 if (cp_parser_allow_gnu_extensions_p (parser)
2850 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2852 /* Statement-expressions are not allowed by the standard. */
2854 pedwarn ("ISO C++ forbids braced-groups within expressions");
2856 /* And they're not allowed outside of a function-body; you
2857 cannot, for example, write:
2859 int i = ({ int j = 3; j + 1; });
2861 at class or namespace scope. */
2862 if (!at_function_scope_p ())
2863 error ("statement-expressions are allowed only inside functions");
2864 /* Start the statement-expression. */
2865 expr = begin_stmt_expr ();
2866 /* Parse the compound-statement. */
2867 cp_parser_compound_statement (parser, expr, false);
2869 expr = finish_stmt_expr (expr, false);
2873 /* Parse the parenthesized expression. */
2874 expr = cp_parser_expression (parser, cast_p);
2875 /* Let the front end know that this expression was
2876 enclosed in parentheses. This matters in case, for
2877 example, the expression is of the form `A::B', since
2878 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2880 finish_parenthesized_expr (expr);
2882 /* The `>' token might be the end of a template-id or
2883 template-parameter-list now. */
2884 parser->greater_than_is_operator_p
2885 = saved_greater_than_is_operator_p;
2886 /* Consume the `)'. */
2887 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2888 cp_parser_skip_to_end_of_statement (parser);
2894 switch (token->keyword)
2896 /* These two are the boolean literals. */
2898 cp_lexer_consume_token (parser->lexer);
2899 return boolean_true_node;
2901 cp_lexer_consume_token (parser->lexer);
2902 return boolean_false_node;
2904 /* The `__null' literal. */
2906 cp_lexer_consume_token (parser->lexer);
2909 /* Recognize the `this' keyword. */
2911 cp_lexer_consume_token (parser->lexer);
2912 if (parser->local_variables_forbidden_p)
2914 error ("%<this%> may not be used in this context");
2915 return error_mark_node;
2917 /* Pointers cannot appear in constant-expressions. */
2918 if (cp_parser_non_integral_constant_expression (parser,
2920 return error_mark_node;
2921 return finish_this_expr ();
2923 /* The `operator' keyword can be the beginning of an
2928 case RID_FUNCTION_NAME:
2929 case RID_PRETTY_FUNCTION_NAME:
2930 case RID_C99_FUNCTION_NAME:
2931 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2932 __func__ are the names of variables -- but they are
2933 treated specially. Therefore, they are handled here,
2934 rather than relying on the generic id-expression logic
2935 below. Grammatically, these names are id-expressions.
2937 Consume the token. */
2938 token = cp_lexer_consume_token (parser->lexer);
2939 /* Look up the name. */
2940 return finish_fname (token->value);
2947 /* The `__builtin_va_arg' construct is used to handle
2948 `va_arg'. Consume the `__builtin_va_arg' token. */
2949 cp_lexer_consume_token (parser->lexer);
2950 /* Look for the opening `('. */
2951 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2952 /* Now, parse the assignment-expression. */
2953 expression = cp_parser_assignment_expression (parser,
2955 /* Look for the `,'. */
2956 cp_parser_require (parser, CPP_COMMA, "`,'");
2957 /* Parse the type-id. */
2958 type = cp_parser_type_id (parser);
2959 /* Look for the closing `)'. */
2960 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2961 /* Using `va_arg' in a constant-expression is not
2963 if (cp_parser_non_integral_constant_expression (parser,
2965 return error_mark_node;
2966 return build_x_va_arg (expression, type);
2970 return cp_parser_builtin_offsetof (parser);
2972 /* Objective-C++ expressions. */
2974 case RID_AT_PROTOCOL:
2975 case RID_AT_SELECTOR:
2976 return cp_parser_objc_expression (parser);
2979 cp_parser_error (parser, "expected primary-expression");
2980 return error_mark_node;
2983 /* An id-expression can start with either an identifier, a
2984 `::' as the beginning of a qualified-id, or the "operator"
2988 case CPP_TEMPLATE_ID:
2989 case CPP_NESTED_NAME_SPECIFIER:
2993 const char *error_msg;
2998 /* Parse the id-expression. */
3000 = cp_parser_id_expression (parser,
3001 /*template_keyword_p=*/false,
3002 /*check_dependency_p=*/true,
3004 /*declarator_p=*/false);
3005 if (id_expression == error_mark_node)
3006 return error_mark_node;
3007 token = cp_lexer_peek_token (parser->lexer);
3008 done = (token->type != CPP_OPEN_SQUARE
3009 && token->type != CPP_OPEN_PAREN
3010 && token->type != CPP_DOT
3011 && token->type != CPP_DEREF
3012 && token->type != CPP_PLUS_PLUS
3013 && token->type != CPP_MINUS_MINUS);
3014 /* If we have a template-id, then no further lookup is
3015 required. If the template-id was for a template-class, we
3016 will sometimes have a TYPE_DECL at this point. */
3017 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3018 || TREE_CODE (id_expression) == TYPE_DECL)
3019 decl = id_expression;
3020 /* Look up the name. */
3023 tree ambiguous_decls;
3025 decl = cp_parser_lookup_name (parser, id_expression,
3028 /*is_namespace=*/false,
3029 /*check_dependency=*/true,
3031 /* If the lookup was ambiguous, an error will already have
3033 if (ambiguous_decls)
3034 return error_mark_node;
3036 /* In Objective-C++, an instance variable (ivar) may be preferred
3037 to whatever cp_parser_lookup_name() found. */
3038 decl = objc_lookup_ivar (decl, id_expression);
3040 /* If name lookup gives us a SCOPE_REF, then the
3041 qualifying scope was dependent. */
3042 if (TREE_CODE (decl) == SCOPE_REF)
3044 /* Check to see if DECL is a local variable in a context
3045 where that is forbidden. */
3046 if (parser->local_variables_forbidden_p
3047 && local_variable_p (decl))
3049 /* It might be that we only found DECL because we are
3050 trying to be generous with pre-ISO scoping rules.
3051 For example, consider:
3055 for (int i = 0; i < 10; ++i) {}
3056 extern void f(int j = i);
3059 Here, name look up will originally find the out
3060 of scope `i'. We need to issue a warning message,
3061 but then use the global `i'. */
3062 decl = check_for_out_of_scope_variable (decl);
3063 if (local_variable_p (decl))
3065 error ("local variable %qD may not appear in this context",
3067 return error_mark_node;
3072 decl = (finish_id_expression
3073 (id_expression, decl, parser->scope,
3075 parser->integral_constant_expression_p,
3076 parser->allow_non_integral_constant_expression_p,
3077 &parser->non_integral_constant_expression_p,
3078 template_p, done, address_p,
3082 cp_parser_error (parser, error_msg);
3086 /* Anything else is an error. */
3088 /* ...unless we have an Objective-C++ message or string literal, that is. */
3089 if (c_dialect_objc ()
3090 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3091 return cp_parser_objc_expression (parser);
3093 cp_parser_error (parser, "expected primary-expression");
3094 return error_mark_node;
3098 /* Parse an id-expression.
3105 :: [opt] nested-name-specifier template [opt] unqualified-id
3107 :: operator-function-id
3110 Return a representation of the unqualified portion of the
3111 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3112 a `::' or nested-name-specifier.
3114 Often, if the id-expression was a qualified-id, the caller will
3115 want to make a SCOPE_REF to represent the qualified-id. This
3116 function does not do this in order to avoid wastefully creating
3117 SCOPE_REFs when they are not required.
3119 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3122 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3123 uninstantiated templates.
3125 If *TEMPLATE_P is non-NULL, it is set to true iff the
3126 `template' keyword is used to explicitly indicate that the entity
3127 named is a template.
3129 If DECLARATOR_P is true, the id-expression is appearing as part of
3130 a declarator, rather than as part of an expression. */
3133 cp_parser_id_expression (cp_parser *parser,
3134 bool template_keyword_p,
3135 bool check_dependency_p,
3139 bool global_scope_p;
3140 bool nested_name_specifier_p;
3142 /* Assume the `template' keyword was not used. */
3144 *template_p = template_keyword_p;
3146 /* Look for the optional `::' operator. */
3148 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3150 /* Look for the optional nested-name-specifier. */
3151 nested_name_specifier_p
3152 = (cp_parser_nested_name_specifier_opt (parser,
3153 /*typename_keyword_p=*/false,
3158 /* If there is a nested-name-specifier, then we are looking at
3159 the first qualified-id production. */
3160 if (nested_name_specifier_p)
3163 tree saved_object_scope;
3164 tree saved_qualifying_scope;
3165 tree unqualified_id;
3168 /* See if the next token is the `template' keyword. */
3170 template_p = &is_template;
3171 *template_p = cp_parser_optional_template_keyword (parser);
3172 /* Name lookup we do during the processing of the
3173 unqualified-id might obliterate SCOPE. */
3174 saved_scope = parser->scope;
3175 saved_object_scope = parser->object_scope;
3176 saved_qualifying_scope = parser->qualifying_scope;
3177 /* Process the final unqualified-id. */
3178 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3181 /* Restore the SAVED_SCOPE for our caller. */
3182 parser->scope = saved_scope;
3183 parser->object_scope = saved_object_scope;
3184 parser->qualifying_scope = saved_qualifying_scope;
3186 return unqualified_id;
3188 /* Otherwise, if we are in global scope, then we are looking at one
3189 of the other qualified-id productions. */
3190 else if (global_scope_p)
3195 /* Peek at the next token. */
3196 token = cp_lexer_peek_token (parser->lexer);
3198 /* If it's an identifier, and the next token is not a "<", then
3199 we can avoid the template-id case. This is an optimization
3200 for this common case. */
3201 if (token->type == CPP_NAME
3202 && !cp_parser_nth_token_starts_template_argument_list_p
3204 return cp_parser_identifier (parser);
3206 cp_parser_parse_tentatively (parser);
3207 /* Try a template-id. */
3208 id = cp_parser_template_id (parser,
3209 /*template_keyword_p=*/false,
3210 /*check_dependency_p=*/true,
3212 /* If that worked, we're done. */
3213 if (cp_parser_parse_definitely (parser))
3216 /* Peek at the next token. (Changes in the token buffer may
3217 have invalidated the pointer obtained above.) */
3218 token = cp_lexer_peek_token (parser->lexer);
3220 switch (token->type)
3223 return cp_parser_identifier (parser);
3226 if (token->keyword == RID_OPERATOR)
3227 return cp_parser_operator_function_id (parser);
3231 cp_parser_error (parser, "expected id-expression");
3232 return error_mark_node;
3236 return cp_parser_unqualified_id (parser, template_keyword_p,
3237 /*check_dependency_p=*/true,
3241 /* Parse an unqualified-id.
3245 operator-function-id
3246 conversion-function-id
3250 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3251 keyword, in a construct like `A::template ...'.
3253 Returns a representation of unqualified-id. For the `identifier'
3254 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3255 production a BIT_NOT_EXPR is returned; the operand of the
3256 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3257 other productions, see the documentation accompanying the
3258 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3259 names are looked up in uninstantiated templates. If DECLARATOR_P
3260 is true, the unqualified-id is appearing as part of a declarator,
3261 rather than as part of an expression. */
3264 cp_parser_unqualified_id (cp_parser* parser,
3265 bool template_keyword_p,
3266 bool check_dependency_p,
3271 /* Peek at the next token. */
3272 token = cp_lexer_peek_token (parser->lexer);
3274 switch (token->type)
3280 /* We don't know yet whether or not this will be a
3282 cp_parser_parse_tentatively (parser);
3283 /* Try a template-id. */
3284 id = cp_parser_template_id (parser, template_keyword_p,
3287 /* If it worked, we're done. */
3288 if (cp_parser_parse_definitely (parser))
3290 /* Otherwise, it's an ordinary identifier. */
3291 return cp_parser_identifier (parser);
3294 case CPP_TEMPLATE_ID:
3295 return cp_parser_template_id (parser, template_keyword_p,
3302 tree qualifying_scope;
3307 /* Consume the `~' token. */
3308 cp_lexer_consume_token (parser->lexer);
3309 /* Parse the class-name. The standard, as written, seems to
3312 template <typename T> struct S { ~S (); };
3313 template <typename T> S<T>::~S() {}
3315 is invalid, since `~' must be followed by a class-name, but
3316 `S<T>' is dependent, and so not known to be a class.
3317 That's not right; we need to look in uninstantiated
3318 templates. A further complication arises from:
3320 template <typename T> void f(T t) {
3324 Here, it is not possible to look up `T' in the scope of `T'
3325 itself. We must look in both the current scope, and the
3326 scope of the containing complete expression.
3328 Yet another issue is:
3337 The standard does not seem to say that the `S' in `~S'
3338 should refer to the type `S' and not the data member
3341 /* DR 244 says that we look up the name after the "~" in the
3342 same scope as we looked up the qualifying name. That idea
3343 isn't fully worked out; it's more complicated than that. */
3344 scope = parser->scope;
3345 object_scope = parser->object_scope;
3346 qualifying_scope = parser->qualifying_scope;
3348 /* If the name is of the form "X::~X" it's OK. */
3349 if (scope && TYPE_P (scope)
3350 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3351 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3353 && (cp_lexer_peek_token (parser->lexer)->value
3354 == TYPE_IDENTIFIER (scope)))
3356 cp_lexer_consume_token (parser->lexer);
3357 return build_nt (BIT_NOT_EXPR, scope);
3360 /* If there was an explicit qualification (S::~T), first look
3361 in the scope given by the qualification (i.e., S). */
3363 type_decl = NULL_TREE;
3366 cp_parser_parse_tentatively (parser);
3367 type_decl = cp_parser_class_name (parser,
3368 /*typename_keyword_p=*/false,
3369 /*template_keyword_p=*/false,
3371 /*check_dependency=*/false,
3372 /*class_head_p=*/false,
3374 if (cp_parser_parse_definitely (parser))
3377 /* In "N::S::~S", look in "N" as well. */
3378 if (!done && scope && qualifying_scope)
3380 cp_parser_parse_tentatively (parser);
3381 parser->scope = qualifying_scope;
3382 parser->object_scope = NULL_TREE;
3383 parser->qualifying_scope = NULL_TREE;
3385 = cp_parser_class_name (parser,
3386 /*typename_keyword_p=*/false,
3387 /*template_keyword_p=*/false,
3389 /*check_dependency=*/false,
3390 /*class_head_p=*/false,
3392 if (cp_parser_parse_definitely (parser))
3395 /* In "p->S::~T", look in the scope given by "*p" as well. */
3396 else if (!done && object_scope)
3398 cp_parser_parse_tentatively (parser);
3399 parser->scope = object_scope;
3400 parser->object_scope = NULL_TREE;
3401 parser->qualifying_scope = NULL_TREE;
3403 = cp_parser_class_name (parser,
3404 /*typename_keyword_p=*/false,
3405 /*template_keyword_p=*/false,
3407 /*check_dependency=*/false,
3408 /*class_head_p=*/false,
3410 if (cp_parser_parse_definitely (parser))
3413 /* Look in the surrounding context. */
3416 parser->scope = NULL_TREE;
3417 parser->object_scope = NULL_TREE;
3418 parser->qualifying_scope = NULL_TREE;
3420 = cp_parser_class_name (parser,
3421 /*typename_keyword_p=*/false,
3422 /*template_keyword_p=*/false,
3424 /*check_dependency=*/false,
3425 /*class_head_p=*/false,
3428 /* If an error occurred, assume that the name of the
3429 destructor is the same as the name of the qualifying
3430 class. That allows us to keep parsing after running
3431 into ill-formed destructor names. */
3432 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3433 return build_nt (BIT_NOT_EXPR, scope);
3434 else if (type_decl == error_mark_node)
3435 return error_mark_node;
3437 /* Check that destructor name and scope match. */
3438 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3440 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3441 error ("declaration of %<~%T%> as member of %qT",
3443 return error_mark_node;
3448 A typedef-name that names a class shall not be used as the
3449 identifier in the declarator for a destructor declaration. */
3451 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3452 && !DECL_SELF_REFERENCE_P (type_decl)
3453 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3454 error ("typedef-name %qD used as destructor declarator",
3457 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3461 if (token->keyword == RID_OPERATOR)
3465 /* This could be a template-id, so we try that first. */
3466 cp_parser_parse_tentatively (parser);
3467 /* Try a template-id. */
3468 id = cp_parser_template_id (parser, template_keyword_p,
3469 /*check_dependency_p=*/true,
3471 /* If that worked, we're done. */
3472 if (cp_parser_parse_definitely (parser))
3474 /* We still don't know whether we're looking at an
3475 operator-function-id or a conversion-function-id. */
3476 cp_parser_parse_tentatively (parser);
3477 /* Try an operator-function-id. */
3478 id = cp_parser_operator_function_id (parser);
3479 /* If that didn't work, try a conversion-function-id. */
3480 if (!cp_parser_parse_definitely (parser))
3481 id = cp_parser_conversion_function_id (parser);
3488 cp_parser_error (parser, "expected unqualified-id");
3489 return error_mark_node;
3493 /* Parse an (optional) nested-name-specifier.
3495 nested-name-specifier:
3496 class-or-namespace-name :: nested-name-specifier [opt]
3497 class-or-namespace-name :: template nested-name-specifier [opt]
3499 PARSER->SCOPE should be set appropriately before this function is
3500 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3501 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3504 Sets PARSER->SCOPE to the class (TYPE) or namespace
3505 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3506 it unchanged if there is no nested-name-specifier. Returns the new
3507 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3509 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3510 part of a declaration and/or decl-specifier. */
3513 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3514 bool typename_keyword_p,
3515 bool check_dependency_p,
3517 bool is_declaration)
3519 bool success = false;
3520 cp_token_position start = 0;
3523 /* If the next token corresponds to a nested name specifier, there
3524 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3525 false, it may have been true before, in which case something
3526 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3527 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3528 CHECK_DEPENDENCY_P is false, we have to fall through into the
3530 if (check_dependency_p
3531 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3533 cp_parser_pre_parsed_nested_name_specifier (parser);
3534 return parser->scope;
3537 /* Remember where the nested-name-specifier starts. */
3538 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3540 start = cp_lexer_token_position (parser->lexer, false);
3541 push_deferring_access_checks (dk_deferred);
3548 tree saved_qualifying_scope;
3549 bool template_keyword_p;
3551 /* Spot cases that cannot be the beginning of a
3552 nested-name-specifier. */
3553 token = cp_lexer_peek_token (parser->lexer);
3555 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3556 the already parsed nested-name-specifier. */
3557 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3559 /* Grab the nested-name-specifier and continue the loop. */
3560 cp_parser_pre_parsed_nested_name_specifier (parser);
3565 /* Spot cases that cannot be the beginning of a
3566 nested-name-specifier. On the second and subsequent times
3567 through the loop, we look for the `template' keyword. */
3568 if (success && token->keyword == RID_TEMPLATE)
3570 /* A template-id can start a nested-name-specifier. */
3571 else if (token->type == CPP_TEMPLATE_ID)
3575 /* If the next token is not an identifier, then it is
3576 definitely not a class-or-namespace-name. */
3577 if (token->type != CPP_NAME)
3579 /* If the following token is neither a `<' (to begin a
3580 template-id), nor a `::', then we are not looking at a
3581 nested-name-specifier. */
3582 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3583 if (token->type != CPP_SCOPE
3584 && !cp_parser_nth_token_starts_template_argument_list_p
3589 /* The nested-name-specifier is optional, so we parse
3591 cp_parser_parse_tentatively (parser);
3593 /* Look for the optional `template' keyword, if this isn't the
3594 first time through the loop. */
3596 template_keyword_p = cp_parser_optional_template_keyword (parser);
3598 template_keyword_p = false;
3600 /* Save the old scope since the name lookup we are about to do
3601 might destroy it. */
3602 old_scope = parser->scope;
3603 saved_qualifying_scope = parser->qualifying_scope;
3604 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3605 look up names in "X<T>::I" in order to determine that "Y" is
3606 a template. So, if we have a typename at this point, we make
3607 an effort to look through it. */
3609 && !typename_keyword_p
3611 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3612 parser->scope = resolve_typename_type (parser->scope,
3613 /*only_current_p=*/false);
3614 /* Parse the qualifying entity. */
3616 = cp_parser_class_or_namespace_name (parser,
3622 /* Look for the `::' token. */
3623 cp_parser_require (parser, CPP_SCOPE, "`::'");
3625 /* If we found what we wanted, we keep going; otherwise, we're
3627 if (!cp_parser_parse_definitely (parser))
3629 bool error_p = false;
3631 /* Restore the OLD_SCOPE since it was valid before the
3632 failed attempt at finding the last
3633 class-or-namespace-name. */
3634 parser->scope = old_scope;
3635 parser->qualifying_scope = saved_qualifying_scope;
3636 /* If the next token is an identifier, and the one after
3637 that is a `::', then any valid interpretation would have
3638 found a class-or-namespace-name. */
3639 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3640 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3642 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3645 token = cp_lexer_consume_token (parser->lexer);
3648 if (!token->ambiguous_p)
3651 tree ambiguous_decls;
3653 decl = cp_parser_lookup_name (parser, token->value,
3655 /*is_template=*/false,
3656 /*is_namespace=*/false,
3657 /*check_dependency=*/true,
3659 if (TREE_CODE (decl) == TEMPLATE_DECL)
3660 error ("%qD used without template parameters", decl);
3661 else if (ambiguous_decls)
3663 error ("reference to %qD is ambiguous",
3665 print_candidates (ambiguous_decls);
3666 decl = error_mark_node;
3669 cp_parser_name_lookup_error
3670 (parser, token->value, decl,
3671 "is not a class or namespace");
3673 parser->scope = error_mark_node;
3675 /* Treat this as a successful nested-name-specifier
3680 If the name found is not a class-name (clause
3681 _class_) or namespace-name (_namespace.def_), the
3682 program is ill-formed. */
3685 cp_lexer_consume_token (parser->lexer);
3689 /* We've found one valid nested-name-specifier. */
3691 /* Name lookup always gives us a DECL. */
3692 if (TREE_CODE (new_scope) == TYPE_DECL)
3693 new_scope = TREE_TYPE (new_scope);
3694 /* Uses of "template" must be followed by actual templates. */
3695 if (template_keyword_p
3696 && !(CLASS_TYPE_P (new_scope)
3697 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3698 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3699 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3700 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3701 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3702 == TEMPLATE_ID_EXPR)))
3703 pedwarn (TYPE_P (new_scope)
3704 ? "%qT is not a template"
3705 : "%qD is not a template",
3707 /* If it is a class scope, try to complete it; we are about to
3708 be looking up names inside the class. */
3709 if (TYPE_P (new_scope)
3710 /* Since checking types for dependency can be expensive,
3711 avoid doing it if the type is already complete. */
3712 && !COMPLETE_TYPE_P (new_scope)
3713 /* Do not try to complete dependent types. */
3714 && !dependent_type_p (new_scope))
3715 new_scope = complete_type (new_scope);
3716 /* Make sure we look in the right scope the next time through
3718 parser->scope = new_scope;
3721 /* If parsing tentatively, replace the sequence of tokens that makes
3722 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3723 token. That way, should we re-parse the token stream, we will
3724 not have to repeat the effort required to do the parse, nor will
3725 we issue duplicate error messages. */
3726 if (success && start)
3731 token = cp_lexer_token_at (parser->lexer, start);
3732 /* Reset the contents of the START token. */
3733 token->type = CPP_NESTED_NAME_SPECIFIER;
3734 /* Retrieve any deferred checks. Do not pop this access checks yet
3735 so the memory will not be reclaimed during token replacing below. */
3736 access_checks = get_deferred_access_checks ();
3737 token->value = build_tree_list (copy_list (access_checks),
3739 TREE_TYPE (token->value) = parser->qualifying_scope;
3740 token->keyword = RID_MAX;
3742 /* Purge all subsequent tokens. */
3743 cp_lexer_purge_tokens_after (parser->lexer, start);
3747 pop_to_parent_deferring_access_checks ();
3749 return success ? parser->scope : NULL_TREE;
3752 /* Parse a nested-name-specifier. See
3753 cp_parser_nested_name_specifier_opt for details. This function
3754 behaves identically, except that it will an issue an error if no
3755 nested-name-specifier is present. */
3758 cp_parser_nested_name_specifier (cp_parser *parser,
3759 bool typename_keyword_p,
3760 bool check_dependency_p,
3762 bool is_declaration)
3766 /* Look for the nested-name-specifier. */
3767 scope = cp_parser_nested_name_specifier_opt (parser,
3772 /* If it was not present, issue an error message. */
3775 cp_parser_error (parser, "expected nested-name-specifier");
3776 parser->scope = NULL_TREE;
3782 /* Parse a class-or-namespace-name.
3784 class-or-namespace-name:
3788 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3789 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3790 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3791 TYPE_P is TRUE iff the next name should be taken as a class-name,
3792 even the same name is declared to be another entity in the same
3795 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3796 specified by the class-or-namespace-name. If neither is found the
3797 ERROR_MARK_NODE is returned. */
3800 cp_parser_class_or_namespace_name (cp_parser *parser,
3801 bool typename_keyword_p,
3802 bool template_keyword_p,
3803 bool check_dependency_p,
3805 bool is_declaration)
3808 tree saved_qualifying_scope;
3809 tree saved_object_scope;
3813 /* Before we try to parse the class-name, we must save away the
3814 current PARSER->SCOPE since cp_parser_class_name will destroy
3816 saved_scope = parser->scope;
3817 saved_qualifying_scope = parser->qualifying_scope;
3818 saved_object_scope = parser->object_scope;
3819 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3820 there is no need to look for a namespace-name. */
3821 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3823 cp_parser_parse_tentatively (parser);
3824 scope = cp_parser_class_name (parser,
3827 type_p ? class_type : none_type,
3829 /*class_head_p=*/false,
3831 /* If that didn't work, try for a namespace-name. */
3832 if (!only_class_p && !cp_parser_parse_definitely (parser))
3834 /* Restore the saved scope. */
3835 parser->scope = saved_scope;
3836 parser->qualifying_scope = saved_qualifying_scope;
3837 parser->object_scope = saved_object_scope;
3838 /* If we are not looking at an identifier followed by the scope
3839 resolution operator, then this is not part of a
3840 nested-name-specifier. (Note that this function is only used
3841 to parse the components of a nested-name-specifier.) */
3842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3843 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3844 return error_mark_node;
3845 scope = cp_parser_namespace_name (parser);
3851 /* Parse a postfix-expression.
3855 postfix-expression [ expression ]
3856 postfix-expression ( expression-list [opt] )
3857 simple-type-specifier ( expression-list [opt] )
3858 typename :: [opt] nested-name-specifier identifier
3859 ( expression-list [opt] )
3860 typename :: [opt] nested-name-specifier template [opt] template-id
3861 ( expression-list [opt] )
3862 postfix-expression . template [opt] id-expression
3863 postfix-expression -> template [opt] id-expression
3864 postfix-expression . pseudo-destructor-name
3865 postfix-expression -> pseudo-destructor-name
3866 postfix-expression ++
3867 postfix-expression --
3868 dynamic_cast < type-id > ( expression )
3869 static_cast < type-id > ( expression )
3870 reinterpret_cast < type-id > ( expression )
3871 const_cast < type-id > ( expression )
3872 typeid ( expression )
3878 ( type-id ) { initializer-list , [opt] }
3880 This extension is a GNU version of the C99 compound-literal
3881 construct. (The C99 grammar uses `type-name' instead of `type-id',
3882 but they are essentially the same concept.)
3884 If ADDRESS_P is true, the postfix expression is the operand of the
3885 `&' operator. CAST_P is true if this expression is the target of a
3888 Returns a representation of the expression. */
3891 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3895 cp_id_kind idk = CP_ID_KIND_NONE;
3896 tree postfix_expression = NULL_TREE;
3898 /* Peek at the next token. */
3899 token = cp_lexer_peek_token (parser->lexer);
3900 /* Some of the productions are determined by keywords. */
3901 keyword = token->keyword;
3911 const char *saved_message;
3913 /* All of these can be handled in the same way from the point
3914 of view of parsing. Begin by consuming the token
3915 identifying the cast. */
3916 cp_lexer_consume_token (parser->lexer);
3918 /* New types cannot be defined in the cast. */
3919 saved_message = parser->type_definition_forbidden_message;
3920 parser->type_definition_forbidden_message
3921 = "types may not be defined in casts";
3923 /* Look for the opening `<'. */
3924 cp_parser_require (parser, CPP_LESS, "`<'");
3925 /* Parse the type to which we are casting. */
3926 type = cp_parser_type_id (parser);
3927 /* Look for the closing `>'. */
3928 cp_parser_require (parser, CPP_GREATER, "`>'");
3929 /* Restore the old message. */
3930 parser->type_definition_forbidden_message = saved_message;
3932 /* And the expression which is being cast. */
3933 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3934 expression = cp_parser_expression (parser, /*cast_p=*/true);
3935 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3937 /* Only type conversions to integral or enumeration types
3938 can be used in constant-expressions. */
3939 if (parser->integral_constant_expression_p
3940 && !dependent_type_p (type)
3941 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3942 && (cp_parser_non_integral_constant_expression
3944 "a cast to a type other than an integral or "
3945 "enumeration type")))
3946 return error_mark_node;
3952 = build_dynamic_cast (type, expression);
3956 = build_static_cast (type, expression);
3960 = build_reinterpret_cast (type, expression);
3964 = build_const_cast (type, expression);
3975 const char *saved_message;
3976 bool saved_in_type_id_in_expr_p;
3978 /* Consume the `typeid' token. */
3979 cp_lexer_consume_token (parser->lexer);
3980 /* Look for the `(' token. */
3981 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3982 /* Types cannot be defined in a `typeid' expression. */
3983 saved_message = parser->type_definition_forbidden_message;
3984 parser->type_definition_forbidden_message
3985 = "types may not be defined in a `typeid\' expression";
3986 /* We can't be sure yet whether we're looking at a type-id or an
3988 cp_parser_parse_tentatively (parser);
3989 /* Try a type-id first. */
3990 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3991 parser->in_type_id_in_expr_p = true;
3992 type = cp_parser_type_id (parser);
3993 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3994 /* Look for the `)' token. Otherwise, we can't be sure that
3995 we're not looking at an expression: consider `typeid (int
3996 (3))', for example. */
3997 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3998 /* If all went well, simply lookup the type-id. */
3999 if (cp_parser_parse_definitely (parser))
4000 postfix_expression = get_typeid (type);
4001 /* Otherwise, fall back to the expression variant. */
4006 /* Look for an expression. */
4007 expression = cp_parser_expression (parser, /*cast_p=*/false);
4008 /* Compute its typeid. */
4009 postfix_expression = build_typeid (expression);
4010 /* Look for the `)' token. */
4011 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4013 /* `typeid' may not appear in an integral constant expression. */
4014 if (cp_parser_non_integral_constant_expression(parser,
4015 "`typeid' operator"))
4016 return error_mark_node;
4017 /* Restore the saved message. */
4018 parser->type_definition_forbidden_message = saved_message;
4025 /* The syntax permitted here is the same permitted for an
4026 elaborated-type-specifier. */
4027 type = cp_parser_elaborated_type_specifier (parser,
4028 /*is_friend=*/false,
4029 /*is_declaration=*/false);
4030 postfix_expression = cp_parser_functional_cast (parser, type);
4038 /* If the next thing is a simple-type-specifier, we may be
4039 looking at a functional cast. We could also be looking at
4040 an id-expression. So, we try the functional cast, and if
4041 that doesn't work we fall back to the primary-expression. */
4042 cp_parser_parse_tentatively (parser);
4043 /* Look for the simple-type-specifier. */
4044 type = cp_parser_simple_type_specifier (parser,
4045 /*decl_specs=*/NULL,
4046 CP_PARSER_FLAGS_NONE);
4047 /* Parse the cast itself. */
4048 if (!cp_parser_error_occurred (parser))
4050 = cp_parser_functional_cast (parser, type);
4051 /* If that worked, we're done. */
4052 if (cp_parser_parse_definitely (parser))
4055 /* If the functional-cast didn't work out, try a
4056 compound-literal. */
4057 if (cp_parser_allow_gnu_extensions_p (parser)
4058 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4060 VEC(constructor_elt,gc) *initializer_list = NULL;
4061 bool saved_in_type_id_in_expr_p;
4063 cp_parser_parse_tentatively (parser);
4064 /* Consume the `('. */
4065 cp_lexer_consume_token (parser->lexer);
4066 /* Parse the type. */
4067 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4068 parser->in_type_id_in_expr_p = true;
4069 type = cp_parser_type_id (parser);
4070 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4071 /* Look for the `)'. */
4072 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4073 /* Look for the `{'. */
4074 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4075 /* If things aren't going well, there's no need to
4077 if (!cp_parser_error_occurred (parser))
4079 bool non_constant_p;
4080 /* Parse the initializer-list. */
4082 = cp_parser_initializer_list (parser, &non_constant_p);
4083 /* Allow a trailing `,'. */
4084 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4085 cp_lexer_consume_token (parser->lexer);
4086 /* Look for the final `}'. */
4087 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4089 /* If that worked, we're definitely looking at a
4090 compound-literal expression. */
4091 if (cp_parser_parse_definitely (parser))
4093 /* Warn the user that a compound literal is not
4094 allowed in standard C++. */
4096 pedwarn ("ISO C++ forbids compound-literals");
4097 /* Form the representation of the compound-literal. */
4099 = finish_compound_literal (type, initializer_list);
4104 /* It must be a primary-expression. */
4106 = cp_parser_primary_expression (parser, address_p, cast_p,
4107 /*template_arg_p=*/false,
4113 /* Keep looping until the postfix-expression is complete. */
4116 if (idk == CP_ID_KIND_UNQUALIFIED
4117 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4118 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4119 /* It is not a Koenig lookup function call. */
4121 = unqualified_name_lookup_error (postfix_expression);
4123 /* Peek at the next token. */
4124 token = cp_lexer_peek_token (parser->lexer);
4126 switch (token->type)
4128 case CPP_OPEN_SQUARE:
4130 = cp_parser_postfix_open_square_expression (parser,
4133 idk = CP_ID_KIND_NONE;
4136 case CPP_OPEN_PAREN:
4137 /* postfix-expression ( expression-list [opt] ) */
4140 bool is_builtin_constant_p;
4141 bool saved_integral_constant_expression_p = false;
4142 bool saved_non_integral_constant_expression_p = false;
4145 is_builtin_constant_p
4146 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4147 if (is_builtin_constant_p)
4149 /* The whole point of __builtin_constant_p is to allow
4150 non-constant expressions to appear as arguments. */
4151 saved_integral_constant_expression_p
4152 = parser->integral_constant_expression_p;
4153 saved_non_integral_constant_expression_p
4154 = parser->non_integral_constant_expression_p;
4155 parser->integral_constant_expression_p = false;
4157 args = (cp_parser_parenthesized_expression_list
4158 (parser, /*is_attribute_list=*/false,
4160 /*non_constant_p=*/NULL));
4161 if (is_builtin_constant_p)
4163 parser->integral_constant_expression_p
4164 = saved_integral_constant_expression_p;
4165 parser->non_integral_constant_expression_p
4166 = saved_non_integral_constant_expression_p;
4169 if (args == error_mark_node)
4171 postfix_expression = error_mark_node;
4175 /* Function calls are not permitted in
4176 constant-expressions. */
4177 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4178 && cp_parser_non_integral_constant_expression (parser,
4181 postfix_expression = error_mark_node;
4186 if (idk == CP_ID_KIND_UNQUALIFIED)
4188 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4194 = perform_koenig_lookup (postfix_expression, args);
4198 = unqualified_fn_lookup_error (postfix_expression);
4200 /* We do not perform argument-dependent lookup if
4201 normal lookup finds a non-function, in accordance
4202 with the expected resolution of DR 218. */
4203 else if (args && is_overloaded_fn (postfix_expression))
4205 tree fn = get_first_fn (postfix_expression);
4207 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4208 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4210 /* Only do argument dependent lookup if regular
4211 lookup does not find a set of member functions.
4212 [basic.lookup.koenig]/2a */
4213 if (!DECL_FUNCTION_MEMBER_P (fn))
4217 = perform_koenig_lookup (postfix_expression, args);
4222 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4224 tree instance = TREE_OPERAND (postfix_expression, 0);
4225 tree fn = TREE_OPERAND (postfix_expression, 1);
4227 if (processing_template_decl
4228 && (type_dependent_expression_p (instance)
4229 || (!BASELINK_P (fn)
4230 && TREE_CODE (fn) != FIELD_DECL)
4231 || type_dependent_expression_p (fn)
4232 || any_type_dependent_arguments_p (args)))
4235 = build_min_nt (CALL_EXPR, postfix_expression,
4240 if (BASELINK_P (fn))
4242 = (build_new_method_call
4243 (instance, fn, args, NULL_TREE,
4244 (idk == CP_ID_KIND_QUALIFIED
4245 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4248 = finish_call_expr (postfix_expression, args,
4249 /*disallow_virtual=*/false,
4250 /*koenig_p=*/false);
4252 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4253 || TREE_CODE (postfix_expression) == MEMBER_REF
4254 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4255 postfix_expression = (build_offset_ref_call_from_tree
4256 (postfix_expression, args));
4257 else if (idk == CP_ID_KIND_QUALIFIED)
4258 /* A call to a static class member, or a namespace-scope
4261 = finish_call_expr (postfix_expression, args,
4262 /*disallow_virtual=*/true,
4265 /* All other function calls. */
4267 = finish_call_expr (postfix_expression, args,
4268 /*disallow_virtual=*/false,
4271 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4272 idk = CP_ID_KIND_NONE;
4278 /* postfix-expression . template [opt] id-expression
4279 postfix-expression . pseudo-destructor-name
4280 postfix-expression -> template [opt] id-expression
4281 postfix-expression -> pseudo-destructor-name */
4283 /* Consume the `.' or `->' operator. */
4284 cp_lexer_consume_token (parser->lexer);
4287 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4293 /* postfix-expression ++ */
4294 /* Consume the `++' token. */
4295 cp_lexer_consume_token (parser->lexer);
4296 /* Generate a representation for the complete expression. */
4298 = finish_increment_expr (postfix_expression,
4299 POSTINCREMENT_EXPR);
4300 /* Increments may not appear in constant-expressions. */
4301 if (cp_parser_non_integral_constant_expression (parser,
4303 postfix_expression = error_mark_node;
4304 idk = CP_ID_KIND_NONE;
4307 case CPP_MINUS_MINUS:
4308 /* postfix-expression -- */
4309 /* Consume the `--' token. */
4310 cp_lexer_consume_token (parser->lexer);
4311 /* Generate a representation for the complete expression. */
4313 = finish_increment_expr (postfix_expression,
4314 POSTDECREMENT_EXPR);
4315 /* Decrements may not appear in constant-expressions. */
4316 if (cp_parser_non_integral_constant_expression (parser,
4318 postfix_expression = error_mark_node;
4319 idk = CP_ID_KIND_NONE;
4323 return postfix_expression;
4327 /* We should never get here. */
4329 return error_mark_node;
4332 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4333 by cp_parser_builtin_offsetof. We're looking for
4335 postfix-expression [ expression ]
4337 FOR_OFFSETOF is set if we're being called in that context, which
4338 changes how we deal with integer constant expressions. */
4341 cp_parser_postfix_open_square_expression (cp_parser *parser,
4342 tree postfix_expression,
4347 /* Consume the `[' token. */
4348 cp_lexer_consume_token (parser->lexer);
4350 /* Parse the index expression. */
4351 /* ??? For offsetof, there is a question of what to allow here. If
4352 offsetof is not being used in an integral constant expression context,
4353 then we *could* get the right answer by computing the value at runtime.
4354 If we are in an integral constant expression context, then we might
4355 could accept any constant expression; hard to say without analysis.
4356 Rather than open the barn door too wide right away, allow only integer
4357 constant expressions here. */
4359 index = cp_parser_constant_expression (parser, false, NULL);
4361 index = cp_parser_expression (parser, /*cast_p=*/false);
4363 /* Look for the closing `]'. */
4364 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4366 /* Build the ARRAY_REF. */
4367 postfix_expression = grok_array_decl (postfix_expression, index);
4369 /* When not doing offsetof, array references are not permitted in
4370 constant-expressions. */
4372 && (cp_parser_non_integral_constant_expression
4373 (parser, "an array reference")))
4374 postfix_expression = error_mark_node;
4376 return postfix_expression;
4379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4380 by cp_parser_builtin_offsetof. We're looking for
4382 postfix-expression . template [opt] id-expression
4383 postfix-expression . pseudo-destructor-name
4384 postfix-expression -> template [opt] id-expression
4385 postfix-expression -> pseudo-destructor-name
4387 FOR_OFFSETOF is set if we're being called in that context. That sorta
4388 limits what of the above we'll actually accept, but nevermind.
4389 TOKEN_TYPE is the "." or "->" token, which will already have been
4390 removed from the stream. */
4393 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4394 enum cpp_ttype token_type,
4395 tree postfix_expression,
4396 bool for_offsetof, cp_id_kind *idk)
4400 bool pseudo_destructor_p;
4401 tree scope = NULL_TREE;
4403 /* If this is a `->' operator, dereference the pointer. */
4404 if (token_type == CPP_DEREF)
4405 postfix_expression = build_x_arrow (postfix_expression);
4406 /* Check to see whether or not the expression is type-dependent. */
4407 dependent_p = type_dependent_expression_p (postfix_expression);
4408 /* The identifier following the `->' or `.' is not qualified. */
4409 parser->scope = NULL_TREE;
4410 parser->qualifying_scope = NULL_TREE;
4411 parser->object_scope = NULL_TREE;
4412 *idk = CP_ID_KIND_NONE;
4413 /* Enter the scope corresponding to the type of the object
4414 given by the POSTFIX_EXPRESSION. */
4415 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4417 scope = TREE_TYPE (postfix_expression);
4418 /* According to the standard, no expression should ever have
4419 reference type. Unfortunately, we do not currently match
4420 the standard in this respect in that our internal representation
4421 of an expression may have reference type even when the standard
4422 says it does not. Therefore, we have to manually obtain the
4423 underlying type here. */
4424 scope = non_reference (scope);
4425 /* The type of the POSTFIX_EXPRESSION must be complete. */
4426 if (scope == unknown_type_node)
4428 error ("%qE does not have class type", postfix_expression);
4432 scope = complete_type_or_else (scope, NULL_TREE);
4433 /* Let the name lookup machinery know that we are processing a
4434 class member access expression. */
4435 parser->context->object_type = scope;
4436 /* If something went wrong, we want to be able to discern that case,
4437 as opposed to the case where there was no SCOPE due to the type
4438 of expression being dependent. */
4440 scope = error_mark_node;
4441 /* If the SCOPE was erroneous, make the various semantic analysis
4442 functions exit quickly -- and without issuing additional error
4444 if (scope == error_mark_node)
4445 postfix_expression = error_mark_node;
4448 /* Assume this expression is not a pseudo-destructor access. */
4449 pseudo_destructor_p = false;
4451 /* If the SCOPE is a scalar type, then, if this is a valid program,
4452 we must be looking at a pseudo-destructor-name. */
4453 if (scope && SCALAR_TYPE_P (scope))
4458 cp_parser_parse_tentatively (parser);
4459 /* Parse the pseudo-destructor-name. */
4461 cp_parser_pseudo_destructor_name (parser, &s, &type);
4462 if (cp_parser_parse_definitely (parser))
4464 pseudo_destructor_p = true;
4466 = finish_pseudo_destructor_expr (postfix_expression,
4467 s, TREE_TYPE (type));
4471 if (!pseudo_destructor_p)
4473 /* If the SCOPE is not a scalar type, we are looking at an
4474 ordinary class member access expression, rather than a
4475 pseudo-destructor-name. */
4477 /* Parse the id-expression. */
4478 name = (cp_parser_id_expression
4480 cp_parser_optional_template_keyword (parser),
4481 /*check_dependency_p=*/true,
4483 /*declarator_p=*/false));
4484 /* In general, build a SCOPE_REF if the member name is qualified.
4485 However, if the name was not dependent and has already been
4486 resolved; there is no need to build the SCOPE_REF. For example;
4488 struct X { void f(); };
4489 template <typename T> void f(T* t) { t->X::f(); }
4491 Even though "t" is dependent, "X::f" is not and has been resolved
4492 to a BASELINK; there is no need to include scope information. */
4494 /* But we do need to remember that there was an explicit scope for
4495 virtual function calls. */
4497 *idk = CP_ID_KIND_QUALIFIED;
4499 /* If the name is a template-id that names a type, we will get a
4500 TYPE_DECL here. That is invalid code. */
4501 if (TREE_CODE (name) == TYPE_DECL)
4503 error ("invalid use of %qD", name);
4504 postfix_expression = error_mark_node;
4508 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4510 name = build_qualified_name (/*type=*/NULL_TREE,
4514 parser->scope = NULL_TREE;
4515 parser->qualifying_scope = NULL_TREE;
4516 parser->object_scope = NULL_TREE;
4518 if (scope && name && BASELINK_P (name))
4519 adjust_result_of_qualified_name_lookup
4520 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4522 = finish_class_member_access_expr (postfix_expression, name,
4527 /* We no longer need to look up names in the scope of the object on
4528 the left-hand side of the `.' or `->' operator. */
4529 parser->context->object_type = NULL_TREE;
4531 /* Outside of offsetof, these operators may not appear in
4532 constant-expressions. */
4534 && (cp_parser_non_integral_constant_expression
4535 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4536 postfix_expression = error_mark_node;
4538 return postfix_expression;
4541 /* Parse a parenthesized expression-list.
4544 assignment-expression
4545 expression-list, assignment-expression
4550 identifier, expression-list
4552 CAST_P is true if this expression is the target of a cast.
4554 Returns a TREE_LIST. The TREE_VALUE of each node is a
4555 representation of an assignment-expression. Note that a TREE_LIST
4556 is returned even if there is only a single expression in the list.
4557 error_mark_node is returned if the ( and or ) are
4558 missing. NULL_TREE is returned on no expressions. The parentheses
4559 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4560 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4561 indicates whether or not all of the expressions in the list were
4565 cp_parser_parenthesized_expression_list (cp_parser* parser,
4566 bool is_attribute_list,
4568 bool *non_constant_p)
4570 tree expression_list = NULL_TREE;
4571 bool fold_expr_p = is_attribute_list;
4572 tree identifier = NULL_TREE;
4574 /* Assume all the expressions will be constant. */
4576 *non_constant_p = false;
4578 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4579 return error_mark_node;
4581 /* Consume expressions until there are no more. */
4582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4587 /* At the beginning of attribute lists, check to see if the
4588 next token is an identifier. */
4589 if (is_attribute_list
4590 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4594 /* Consume the identifier. */
4595 token = cp_lexer_consume_token (parser->lexer);
4596 /* Save the identifier. */
4597 identifier = token->value;
4601 /* Parse the next assignment-expression. */
4604 bool expr_non_constant_p;
4605 expr = (cp_parser_constant_expression
4606 (parser, /*allow_non_constant_p=*/true,
4607 &expr_non_constant_p));
4608 if (expr_non_constant_p)
4609 *non_constant_p = true;
4612 expr = cp_parser_assignment_expression (parser, cast_p);
4615 expr = fold_non_dependent_expr (expr);
4617 /* Add it to the list. We add error_mark_node
4618 expressions to the list, so that we can still tell if
4619 the correct form for a parenthesized expression-list
4620 is found. That gives better errors. */
4621 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4623 if (expr == error_mark_node)
4627 /* After the first item, attribute lists look the same as
4628 expression lists. */
4629 is_attribute_list = false;
4632 /* If the next token isn't a `,', then we are done. */
4633 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4636 /* Otherwise, consume the `,' and keep going. */
4637 cp_lexer_consume_token (parser->lexer);
4640 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4645 /* We try and resync to an unnested comma, as that will give the
4646 user better diagnostics. */
4647 ending = cp_parser_skip_to_closing_parenthesis (parser,
4648 /*recovering=*/true,
4650 /*consume_paren=*/true);
4654 return error_mark_node;
4657 /* We built up the list in reverse order so we must reverse it now. */
4658 expression_list = nreverse (expression_list);
4660 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4662 return expression_list;
4665 /* Parse a pseudo-destructor-name.
4667 pseudo-destructor-name:
4668 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4669 :: [opt] nested-name-specifier template template-id :: ~ type-name
4670 :: [opt] nested-name-specifier [opt] ~ type-name
4672 If either of the first two productions is used, sets *SCOPE to the
4673 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4674 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4675 or ERROR_MARK_NODE if the parse fails. */
4678 cp_parser_pseudo_destructor_name (cp_parser* parser,
4682 bool nested_name_specifier_p;
4684 /* Assume that things will not work out. */
4685 *type = error_mark_node;
4687 /* Look for the optional `::' operator. */
4688 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4689 /* Look for the optional nested-name-specifier. */
4690 nested_name_specifier_p
4691 = (cp_parser_nested_name_specifier_opt (parser,
4692 /*typename_keyword_p=*/false,
4693 /*check_dependency_p=*/true,
4695 /*is_declaration=*/true)
4697 /* Now, if we saw a nested-name-specifier, we might be doing the
4698 second production. */
4699 if (nested_name_specifier_p
4700 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4702 /* Consume the `template' keyword. */
4703 cp_lexer_consume_token (parser->lexer);
4704 /* Parse the template-id. */
4705 cp_parser_template_id (parser,
4706 /*template_keyword_p=*/true,
4707 /*check_dependency_p=*/false,
4708 /*is_declaration=*/true);
4709 /* Look for the `::' token. */
4710 cp_parser_require (parser, CPP_SCOPE, "`::'");
4712 /* If the next token is not a `~', then there might be some
4713 additional qualification. */
4714 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4716 /* Look for the type-name. */
4717 *scope = TREE_TYPE (cp_parser_type_name (parser));
4719 if (*scope == error_mark_node)
4722 /* If we don't have ::~, then something has gone wrong. Since
4723 the only caller of this function is looking for something
4724 after `.' or `->' after a scalar type, most likely the
4725 program is trying to get a member of a non-aggregate
4727 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4728 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4730 cp_parser_error (parser, "request for member of non-aggregate type");
4734 /* Look for the `::' token. */
4735 cp_parser_require (parser, CPP_SCOPE, "`::'");
4740 /* Look for the `~'. */
4741 cp_parser_require (parser, CPP_COMPL, "`~'");
4742 /* Look for the type-name again. We are not responsible for
4743 checking that it matches the first type-name. */
4744 *type = cp_parser_type_name (parser);
4747 /* Parse a unary-expression.
4753 unary-operator cast-expression
4754 sizeof unary-expression
4762 __extension__ cast-expression
4763 __alignof__ unary-expression
4764 __alignof__ ( type-id )
4765 __real__ cast-expression
4766 __imag__ cast-expression
4769 ADDRESS_P is true iff the unary-expression is appearing as the
4770 operand of the `&' operator. CAST_P is true if this expression is
4771 the target of a cast.
4773 Returns a representation of the expression. */
4776 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4779 enum tree_code unary_operator;
4781 /* Peek at the next token. */
4782 token = cp_lexer_peek_token (parser->lexer);
4783 /* Some keywords give away the kind of expression. */
4784 if (token->type == CPP_KEYWORD)
4786 enum rid keyword = token->keyword;
4796 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4797 /* Consume the token. */
4798 cp_lexer_consume_token (parser->lexer);
4799 /* Parse the operand. */
4800 operand = cp_parser_sizeof_operand (parser, keyword);
4802 if (TYPE_P (operand))
4803 return cxx_sizeof_or_alignof_type (operand, op, true);
4805 return cxx_sizeof_or_alignof_expr (operand, op);
4809 return cp_parser_new_expression (parser);
4812 return cp_parser_delete_expression (parser);
4816 /* The saved value of the PEDANTIC flag. */
4820 /* Save away the PEDANTIC flag. */
4821 cp_parser_extension_opt (parser, &saved_pedantic);
4822 /* Parse the cast-expression. */
4823 expr = cp_parser_simple_cast_expression (parser);
4824 /* Restore the PEDANTIC flag. */
4825 pedantic = saved_pedantic;
4835 /* Consume the `__real__' or `__imag__' token. */
4836 cp_lexer_consume_token (parser->lexer);
4837 /* Parse the cast-expression. */
4838 expression = cp_parser_simple_cast_expression (parser);
4839 /* Create the complete representation. */
4840 return build_x_unary_op ((keyword == RID_REALPART
4841 ? REALPART_EXPR : IMAGPART_EXPR),
4851 /* Look for the `:: new' and `:: delete', which also signal the
4852 beginning of a new-expression, or delete-expression,
4853 respectively. If the next token is `::', then it might be one of
4855 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4859 /* See if the token after the `::' is one of the keywords in
4860 which we're interested. */
4861 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4862 /* If it's `new', we have a new-expression. */
4863 if (keyword == RID_NEW)
4864 return cp_parser_new_expression (parser);
4865 /* Similarly, for `delete'. */
4866 else if (keyword == RID_DELETE)
4867 return cp_parser_delete_expression (parser);
4870 /* Look for a unary operator. */
4871 unary_operator = cp_parser_unary_operator (token);
4872 /* The `++' and `--' operators can be handled similarly, even though
4873 they are not technically unary-operators in the grammar. */
4874 if (unary_operator == ERROR_MARK)
4876 if (token->type == CPP_PLUS_PLUS)
4877 unary_operator = PREINCREMENT_EXPR;
4878 else if (token->type == CPP_MINUS_MINUS)
4879 unary_operator = PREDECREMENT_EXPR;
4880 /* Handle the GNU address-of-label extension. */
4881 else if (cp_parser_allow_gnu_extensions_p (parser)
4882 && token->type == CPP_AND_AND)
4886 /* Consume the '&&' token. */
4887 cp_lexer_consume_token (parser->lexer);
4888 /* Look for the identifier. */
4889 identifier = cp_parser_identifier (parser);
4890 /* Create an expression representing the address. */
4891 return finish_label_address_expr (identifier);
4894 if (unary_operator != ERROR_MARK)
4896 tree cast_expression;
4897 tree expression = error_mark_node;
4898 const char *non_constant_p = NULL;
4900 /* Consume the operator token. */
4901 token = cp_lexer_consume_token (parser->lexer);
4902 /* Parse the cast-expression. */
4904 = cp_parser_cast_expression (parser,
4905 unary_operator == ADDR_EXPR,
4907 /* Now, build an appropriate representation. */
4908 switch (unary_operator)
4911 non_constant_p = "`*'";
4912 expression = build_x_indirect_ref (cast_expression, "unary *");
4916 non_constant_p = "`&'";
4919 expression = build_x_unary_op (unary_operator, cast_expression);
4922 case PREINCREMENT_EXPR:
4923 case PREDECREMENT_EXPR:
4924 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4927 case UNARY_PLUS_EXPR:
4929 case TRUTH_NOT_EXPR:
4930 expression = finish_unary_op_expr (unary_operator, cast_expression);
4938 && cp_parser_non_integral_constant_expression (parser,
4940 expression = error_mark_node;
4945 return cp_parser_postfix_expression (parser, address_p, cast_p);
4948 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4949 unary-operator, the corresponding tree code is returned. */
4951 static enum tree_code
4952 cp_parser_unary_operator (cp_token* token)
4954 switch (token->type)
4957 return INDIRECT_REF;
4963 return UNARY_PLUS_EXPR;
4969 return TRUTH_NOT_EXPR;
4972 return BIT_NOT_EXPR;
4979 /* Parse a new-expression.
4982 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4983 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4985 Returns a representation of the expression. */
4988 cp_parser_new_expression (cp_parser* parser)
4990 bool global_scope_p;
4996 /* Look for the optional `::' operator. */
4998 = (cp_parser_global_scope_opt (parser,
4999 /*current_scope_valid_p=*/false)
5001 /* Look for the `new' operator. */
5002 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5003 /* There's no easy way to tell a new-placement from the
5004 `( type-id )' construct. */
5005 cp_parser_parse_tentatively (parser);
5006 /* Look for a new-placement. */
5007 placement = cp_parser_new_placement (parser);
5008 /* If that didn't work out, there's no new-placement. */
5009 if (!cp_parser_parse_definitely (parser))
5010 placement = NULL_TREE;
5012 /* If the next token is a `(', then we have a parenthesized
5014 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5016 /* Consume the `('. */
5017 cp_lexer_consume_token (parser->lexer);
5018 /* Parse the type-id. */
5019 type = cp_parser_type_id (parser);
5020 /* Look for the closing `)'. */
5021 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5022 /* There should not be a direct-new-declarator in this production,
5023 but GCC used to allowed this, so we check and emit a sensible error
5024 message for this case. */
5025 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5027 error ("array bound forbidden after parenthesized type-id");
5028 inform ("try removing the parentheses around the type-id");
5029 cp_parser_direct_new_declarator (parser);
5033 /* Otherwise, there must be a new-type-id. */
5035 type = cp_parser_new_type_id (parser, &nelts);
5037 /* If the next token is a `(', then we have a new-initializer. */
5038 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5039 initializer = cp_parser_new_initializer (parser);
5041 initializer = NULL_TREE;
5043 /* A new-expression may not appear in an integral constant
5045 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5046 return error_mark_node;
5048 /* Create a representation of the new-expression. */
5049 return build_new (placement, type, nelts, initializer, global_scope_p);
5052 /* Parse a new-placement.
5057 Returns the same representation as for an expression-list. */
5060 cp_parser_new_placement (cp_parser* parser)
5062 tree expression_list;
5064 /* Parse the expression-list. */
5065 expression_list = (cp_parser_parenthesized_expression_list
5066 (parser, false, /*cast_p=*/false,
5067 /*non_constant_p=*/NULL));
5069 return expression_list;
5072 /* Parse a new-type-id.
5075 type-specifier-seq new-declarator [opt]
5077 Returns the TYPE allocated. If the new-type-id indicates an array
5078 type, *NELTS is set to the number of elements in the last array
5079 bound; the TYPE will not include the last array bound. */
5082 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5084 cp_decl_specifier_seq type_specifier_seq;
5085 cp_declarator *new_declarator;
5086 cp_declarator *declarator;
5087 cp_declarator *outer_declarator;
5088 const char *saved_message;
5091 /* The type-specifier sequence must not contain type definitions.
5092 (It cannot contain declarations of new types either, but if they
5093 are not definitions we will catch that because they are not
5095 saved_message = parser->type_definition_forbidden_message;
5096 parser->type_definition_forbidden_message
5097 = "types may not be defined in a new-type-id";
5098 /* Parse the type-specifier-seq. */
5099 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5100 &type_specifier_seq);
5101 /* Restore the old message. */
5102 parser->type_definition_forbidden_message = saved_message;
5103 /* Parse the new-declarator. */
5104 new_declarator = cp_parser_new_declarator_opt (parser);
5106 /* Determine the number of elements in the last array dimension, if
5109 /* Skip down to the last array dimension. */
5110 declarator = new_declarator;
5111 outer_declarator = NULL;
5112 while (declarator && (declarator->kind == cdk_pointer
5113 || declarator->kind == cdk_ptrmem))
5115 outer_declarator = declarator;
5116 declarator = declarator->declarator;
5119 && declarator->kind == cdk_array
5120 && declarator->declarator
5121 && declarator->declarator->kind == cdk_array)
5123 outer_declarator = declarator;
5124 declarator = declarator->declarator;
5127 if (declarator && declarator->kind == cdk_array)
5129 *nelts = declarator->u.array.bounds;
5130 if (*nelts == error_mark_node)
5131 *nelts = integer_one_node;
5133 if (outer_declarator)
5134 outer_declarator->declarator = declarator->declarator;
5136 new_declarator = NULL;
5139 type = groktypename (&type_specifier_seq, new_declarator);
5140 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5142 *nelts = array_type_nelts_top (type);
5143 type = TREE_TYPE (type);
5148 /* Parse an (optional) new-declarator.
5151 ptr-operator new-declarator [opt]
5152 direct-new-declarator
5154 Returns the declarator. */
5156 static cp_declarator *
5157 cp_parser_new_declarator_opt (cp_parser* parser)
5159 enum tree_code code;
5161 cp_cv_quals cv_quals;
5163 /* We don't know if there's a ptr-operator next, or not. */
5164 cp_parser_parse_tentatively (parser);
5165 /* Look for a ptr-operator. */
5166 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5167 /* If that worked, look for more new-declarators. */
5168 if (cp_parser_parse_definitely (parser))
5170 cp_declarator *declarator;
5172 /* Parse another optional declarator. */
5173 declarator = cp_parser_new_declarator_opt (parser);
5175 /* Create the representation of the declarator. */
5177 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5178 else if (code == INDIRECT_REF)
5179 declarator = make_pointer_declarator (cv_quals, declarator);
5181 declarator = make_reference_declarator (cv_quals, declarator);
5186 /* If the next token is a `[', there is a direct-new-declarator. */
5187 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5188 return cp_parser_direct_new_declarator (parser);
5193 /* Parse a direct-new-declarator.
5195 direct-new-declarator:
5197 direct-new-declarator [constant-expression]
5201 static cp_declarator *
5202 cp_parser_direct_new_declarator (cp_parser* parser)
5204 cp_declarator *declarator = NULL;
5210 /* Look for the opening `['. */
5211 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5212 /* The first expression is not required to be constant. */
5215 expression = cp_parser_expression (parser, /*cast_p=*/false);
5216 /* The standard requires that the expression have integral
5217 type. DR 74 adds enumeration types. We believe that the
5218 real intent is that these expressions be handled like the
5219 expression in a `switch' condition, which also allows
5220 classes with a single conversion to integral or
5221 enumeration type. */
5222 if (!processing_template_decl)
5225 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5230 error ("expression in new-declarator must have integral "
5231 "or enumeration type");
5232 expression = error_mark_node;
5236 /* But all the other expressions must be. */
5239 = cp_parser_constant_expression (parser,
5240 /*allow_non_constant=*/false,
5242 /* Look for the closing `]'. */
5243 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5245 /* Add this bound to the declarator. */
5246 declarator = make_array_declarator (declarator, expression);
5248 /* If the next token is not a `[', then there are no more
5250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5257 /* Parse a new-initializer.
5260 ( expression-list [opt] )
5262 Returns a representation of the expression-list. If there is no
5263 expression-list, VOID_ZERO_NODE is returned. */
5266 cp_parser_new_initializer (cp_parser* parser)
5268 tree expression_list;
5270 expression_list = (cp_parser_parenthesized_expression_list
5271 (parser, false, /*cast_p=*/false,
5272 /*non_constant_p=*/NULL));
5273 if (!expression_list)
5274 expression_list = void_zero_node;
5276 return expression_list;
5279 /* Parse a delete-expression.
5282 :: [opt] delete cast-expression
5283 :: [opt] delete [ ] cast-expression
5285 Returns a representation of the expression. */
5288 cp_parser_delete_expression (cp_parser* parser)
5290 bool global_scope_p;
5294 /* Look for the optional `::' operator. */
5296 = (cp_parser_global_scope_opt (parser,
5297 /*current_scope_valid_p=*/false)
5299 /* Look for the `delete' keyword. */
5300 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5301 /* See if the array syntax is in use. */
5302 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5304 /* Consume the `[' token. */
5305 cp_lexer_consume_token (parser->lexer);
5306 /* Look for the `]' token. */
5307 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5308 /* Remember that this is the `[]' construct. */
5314 /* Parse the cast-expression. */
5315 expression = cp_parser_simple_cast_expression (parser);
5317 /* A delete-expression may not appear in an integral constant
5319 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5320 return error_mark_node;
5322 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5325 /* Parse a cast-expression.
5329 ( type-id ) cast-expression
5331 ADDRESS_P is true iff the unary-expression is appearing as the
5332 operand of the `&' operator. CAST_P is true if this expression is
5333 the target of a cast.
5335 Returns a representation of the expression. */
5338 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5340 /* If it's a `(', then we might be looking at a cast. */
5341 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5343 tree type = NULL_TREE;
5344 tree expr = NULL_TREE;
5345 bool compound_literal_p;
5346 const char *saved_message;
5348 /* There's no way to know yet whether or not this is a cast.
5349 For example, `(int (3))' is a unary-expression, while `(int)
5350 3' is a cast. So, we resort to parsing tentatively. */
5351 cp_parser_parse_tentatively (parser);
5352 /* Types may not be defined in a cast. */
5353 saved_message = parser->type_definition_forbidden_message;
5354 parser->type_definition_forbidden_message
5355 = "types may not be defined in casts";
5356 /* Consume the `('. */
5357 cp_lexer_consume_token (parser->lexer);
5358 /* A very tricky bit is that `(struct S) { 3 }' is a
5359 compound-literal (which we permit in C++ as an extension).
5360 But, that construct is not a cast-expression -- it is a
5361 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5362 is legal; if the compound-literal were a cast-expression,
5363 you'd need an extra set of parentheses.) But, if we parse
5364 the type-id, and it happens to be a class-specifier, then we
5365 will commit to the parse at that point, because we cannot
5366 undo the action that is done when creating a new class. So,
5367 then we cannot back up and do a postfix-expression.
5369 Therefore, we scan ahead to the closing `)', and check to see
5370 if the token after the `)' is a `{'. If so, we are not
5371 looking at a cast-expression.
5373 Save tokens so that we can put them back. */
5374 cp_lexer_save_tokens (parser->lexer);
5375 /* Skip tokens until the next token is a closing parenthesis.
5376 If we find the closing `)', and the next token is a `{', then
5377 we are looking at a compound-literal. */
5379 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5380 /*consume_paren=*/true)
5381 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5382 /* Roll back the tokens we skipped. */
5383 cp_lexer_rollback_tokens (parser->lexer);
5384 /* If we were looking at a compound-literal, simulate an error
5385 so that the call to cp_parser_parse_definitely below will
5387 if (compound_literal_p)
5388 cp_parser_simulate_error (parser);
5391 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5392 parser->in_type_id_in_expr_p = true;
5393 /* Look for the type-id. */
5394 type = cp_parser_type_id (parser);
5395 /* Look for the closing `)'. */
5396 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5397 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5400 /* Restore the saved message. */
5401 parser->type_definition_forbidden_message = saved_message;
5403 /* If ok so far, parse the dependent expression. We cannot be
5404 sure it is a cast. Consider `(T ())'. It is a parenthesized
5405 ctor of T, but looks like a cast to function returning T
5406 without a dependent expression. */
5407 if (!cp_parser_error_occurred (parser))
5408 expr = cp_parser_cast_expression (parser,
5409 /*address_p=*/false,
5412 if (cp_parser_parse_definitely (parser))
5414 /* Warn about old-style casts, if so requested. */
5415 if (warn_old_style_cast
5416 && !in_system_header
5417 && !VOID_TYPE_P (type)
5418 && current_lang_name != lang_name_c)
5419 warning (OPT_Wold_style_cast, "use of old-style cast");
5421 /* Only type conversions to integral or enumeration types
5422 can be used in constant-expressions. */
5423 if (parser->integral_constant_expression_p
5424 && !dependent_type_p (type)
5425 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5426 && (cp_parser_non_integral_constant_expression
5428 "a cast to a type other than an integral or "
5429 "enumeration type")))
5430 return error_mark_node;
5432 /* Perform the cast. */
5433 expr = build_c_cast (type, expr);
5438 /* If we get here, then it's not a cast, so it must be a
5439 unary-expression. */
5440 return cp_parser_unary_expression (parser, address_p, cast_p);
5443 /* Parse a binary expression of the general form:
5447 pm-expression .* cast-expression
5448 pm-expression ->* cast-expression
5450 multiplicative-expression:
5452 multiplicative-expression * pm-expression
5453 multiplicative-expression / pm-expression
5454 multiplicative-expression % pm-expression
5456 additive-expression:
5457 multiplicative-expression
5458 additive-expression + multiplicative-expression
5459 additive-expression - multiplicative-expression
5463 shift-expression << additive-expression
5464 shift-expression >> additive-expression
5466 relational-expression:
5468 relational-expression < shift-expression
5469 relational-expression > shift-expression
5470 relational-expression <= shift-expression
5471 relational-expression >= shift-expression
5475 relational-expression:
5476 relational-expression <? shift-expression
5477 relational-expression >? shift-expression
5479 equality-expression:
5480 relational-expression
5481 equality-expression == relational-expression
5482 equality-expression != relational-expression
5486 and-expression & equality-expression
5488 exclusive-or-expression:
5490 exclusive-or-expression ^ and-expression
5492 inclusive-or-expression:
5493 exclusive-or-expression
5494 inclusive-or-expression | exclusive-or-expression
5496 logical-and-expression:
5497 inclusive-or-expression
5498 logical-and-expression && inclusive-or-expression
5500 logical-or-expression:
5501 logical-and-expression
5502 logical-or-expression || logical-and-expression
5504 All these are implemented with a single function like:
5507 simple-cast-expression
5508 binary-expression <token> binary-expression
5510 CAST_P is true if this expression is the target of a cast.
5512 The binops_by_token map is used to get the tree codes for each <token> type.
5513 binary-expressions are associated according to a precedence table. */
5515 #define TOKEN_PRECEDENCE(token) \
5516 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5517 ? PREC_NOT_OPERATOR \
5518 : binops_by_token[token->type].prec)
5521 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5523 cp_parser_expression_stack stack;
5524 cp_parser_expression_stack_entry *sp = &stack[0];
5527 enum tree_code tree_type;
5528 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5531 /* Parse the first expression. */
5532 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5536 /* Get an operator token. */
5537 token = cp_lexer_peek_token (parser->lexer);
5538 if (token->type == CPP_MIN || token->type == CPP_MAX)
5539 cp_parser_warn_min_max ();
5541 new_prec = TOKEN_PRECEDENCE (token);
5543 /* Popping an entry off the stack means we completed a subexpression:
5544 - either we found a token which is not an operator (`>' where it is not
5545 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5546 will happen repeatedly;
5547 - or, we found an operator which has lower priority. This is the case
5548 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5550 if (new_prec <= prec)
5559 tree_type = binops_by_token[token->type].tree_type;
5561 /* We used the operator token. */
5562 cp_lexer_consume_token (parser->lexer);
5564 /* Extract another operand. It may be the RHS of this expression
5565 or the LHS of a new, higher priority expression. */
5566 rhs = cp_parser_simple_cast_expression (parser);
5568 /* Get another operator token. Look up its precedence to avoid
5569 building a useless (immediately popped) stack entry for common
5570 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5571 token = cp_lexer_peek_token (parser->lexer);
5572 lookahead_prec = TOKEN_PRECEDENCE (token);
5573 if (lookahead_prec > new_prec)
5575 /* ... and prepare to parse the RHS of the new, higher priority
5576 expression. Since precedence levels on the stack are
5577 monotonically increasing, we do not have to care about
5580 sp->tree_type = tree_type;
5585 new_prec = lookahead_prec;
5589 /* If the stack is not empty, we have parsed into LHS the right side
5590 (`4' in the example above) of an expression we had suspended.
5591 We can use the information on the stack to recover the LHS (`3')
5592 from the stack together with the tree code (`MULT_EXPR'), and
5593 the precedence of the higher level subexpression
5594 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5595 which will be used to actually build the additive expression. */
5598 tree_type = sp->tree_type;
5603 overloaded_p = false;
5604 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5606 /* If the binary operator required the use of an overloaded operator,
5607 then this expression cannot be an integral constant-expression.
5608 An overloaded operator can be used even if both operands are
5609 otherwise permissible in an integral constant-expression if at
5610 least one of the operands is of enumeration type. */
5613 && (cp_parser_non_integral_constant_expression
5614 (parser, "calls to overloaded operators")))
5615 return error_mark_node;
5622 /* Parse the `? expression : assignment-expression' part of a
5623 conditional-expression. The LOGICAL_OR_EXPR is the
5624 logical-or-expression that started the conditional-expression.
5625 Returns a representation of the entire conditional-expression.
5627 This routine is used by cp_parser_assignment_expression.
5629 ? expression : assignment-expression
5633 ? : assignment-expression */
5636 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5639 tree assignment_expr;
5641 /* Consume the `?' token. */
5642 cp_lexer_consume_token (parser->lexer);
5643 if (cp_parser_allow_gnu_extensions_p (parser)
5644 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5645 /* Implicit true clause. */
5648 /* Parse the expression. */
5649 expr = cp_parser_expression (parser, /*cast_p=*/false);
5651 /* The next token should be a `:'. */
5652 cp_parser_require (parser, CPP_COLON, "`:'");
5653 /* Parse the assignment-expression. */
5654 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5656 /* Build the conditional-expression. */
5657 return build_x_conditional_expr (logical_or_expr,
5662 /* Parse an assignment-expression.
5664 assignment-expression:
5665 conditional-expression
5666 logical-or-expression assignment-operator assignment_expression
5669 CAST_P is true if this expression is the target of a cast.
5671 Returns a representation for the expression. */
5674 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5678 /* If the next token is the `throw' keyword, then we're looking at
5679 a throw-expression. */
5680 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5681 expr = cp_parser_throw_expression (parser);
5682 /* Otherwise, it must be that we are looking at a
5683 logical-or-expression. */
5686 /* Parse the binary expressions (logical-or-expression). */
5687 expr = cp_parser_binary_expression (parser, cast_p);
5688 /* If the next token is a `?' then we're actually looking at a
5689 conditional-expression. */
5690 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5691 return cp_parser_question_colon_clause (parser, expr);
5694 enum tree_code assignment_operator;
5696 /* If it's an assignment-operator, we're using the second
5699 = cp_parser_assignment_operator_opt (parser);
5700 if (assignment_operator != ERROR_MARK)
5704 /* Parse the right-hand side of the assignment. */
5705 rhs = cp_parser_assignment_expression (parser, cast_p);
5706 /* An assignment may not appear in a
5707 constant-expression. */
5708 if (cp_parser_non_integral_constant_expression (parser,
5710 return error_mark_node;
5711 /* Build the assignment expression. */
5712 expr = build_x_modify_expr (expr,
5713 assignment_operator,
5722 /* Parse an (optional) assignment-operator.
5724 assignment-operator: one of
5725 = *= /= %= += -= >>= <<= &= ^= |=
5729 assignment-operator: one of
5732 If the next token is an assignment operator, the corresponding tree
5733 code is returned, and the token is consumed. For example, for
5734 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5735 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5736 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5737 operator, ERROR_MARK is returned. */
5739 static enum tree_code
5740 cp_parser_assignment_operator_opt (cp_parser* parser)
5745 /* Peek at the next toen. */
5746 token = cp_lexer_peek_token (parser->lexer);
5748 switch (token->type)
5759 op = TRUNC_DIV_EXPR;
5763 op = TRUNC_MOD_EXPR;
5796 cp_parser_warn_min_max ();
5801 cp_parser_warn_min_max ();
5805 /* Nothing else is an assignment operator. */
5809 /* If it was an assignment operator, consume it. */
5810 if (op != ERROR_MARK)
5811 cp_lexer_consume_token (parser->lexer);
5816 /* Parse an expression.
5819 assignment-expression
5820 expression , assignment-expression
5822 CAST_P is true if this expression is the target of a cast.
5824 Returns a representation of the expression. */
5827 cp_parser_expression (cp_parser* parser, bool cast_p)
5829 tree expression = NULL_TREE;
5833 tree assignment_expression;
5835 /* Parse the next assignment-expression. */
5836 assignment_expression
5837 = cp_parser_assignment_expression (parser, cast_p);
5838 /* If this is the first assignment-expression, we can just
5841 expression = assignment_expression;
5843 expression = build_x_compound_expr (expression,
5844 assignment_expression);
5845 /* If the next token is not a comma, then we are done with the
5847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5849 /* Consume the `,'. */
5850 cp_lexer_consume_token (parser->lexer);
5851 /* A comma operator cannot appear in a constant-expression. */
5852 if (cp_parser_non_integral_constant_expression (parser,
5853 "a comma operator"))
5854 expression = error_mark_node;
5860 /* Parse a constant-expression.
5862 constant-expression:
5863 conditional-expression
5865 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5866 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5867 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5868 is false, NON_CONSTANT_P should be NULL. */
5871 cp_parser_constant_expression (cp_parser* parser,
5872 bool allow_non_constant_p,
5873 bool *non_constant_p)
5875 bool saved_integral_constant_expression_p;
5876 bool saved_allow_non_integral_constant_expression_p;
5877 bool saved_non_integral_constant_expression_p;
5880 /* It might seem that we could simply parse the
5881 conditional-expression, and then check to see if it were
5882 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5883 one that the compiler can figure out is constant, possibly after
5884 doing some simplifications or optimizations. The standard has a
5885 precise definition of constant-expression, and we must honor
5886 that, even though it is somewhat more restrictive.
5892 is not a legal declaration, because `(2, 3)' is not a
5893 constant-expression. The `,' operator is forbidden in a
5894 constant-expression. However, GCC's constant-folding machinery
5895 will fold this operation to an INTEGER_CST for `3'. */
5897 /* Save the old settings. */
5898 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5899 saved_allow_non_integral_constant_expression_p
5900 = parser->allow_non_integral_constant_expression_p;
5901 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5902 /* We are now parsing a constant-expression. */
5903 parser->integral_constant_expression_p = true;
5904 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5905 parser->non_integral_constant_expression_p = false;
5906 /* Although the grammar says "conditional-expression", we parse an
5907 "assignment-expression", which also permits "throw-expression"
5908 and the use of assignment operators. In the case that
5909 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5910 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5911 actually essential that we look for an assignment-expression.
5912 For example, cp_parser_initializer_clauses uses this function to
5913 determine whether a particular assignment-expression is in fact
5915 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5916 /* Restore the old settings. */
5917 parser->integral_constant_expression_p
5918 = saved_integral_constant_expression_p;
5919 parser->allow_non_integral_constant_expression_p
5920 = saved_allow_non_integral_constant_expression_p;
5921 if (allow_non_constant_p)
5922 *non_constant_p = parser->non_integral_constant_expression_p;
5923 else if (parser->non_integral_constant_expression_p)
5924 expression = error_mark_node;
5925 parser->non_integral_constant_expression_p
5926 = saved_non_integral_constant_expression_p;
5931 /* Parse __builtin_offsetof.
5933 offsetof-expression:
5934 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5936 offsetof-member-designator:
5938 | offsetof-member-designator "." id-expression
5939 | offsetof-member-designator "[" expression "]"
5943 cp_parser_builtin_offsetof (cp_parser *parser)
5945 int save_ice_p, save_non_ice_p;
5949 /* We're about to accept non-integral-constant things, but will
5950 definitely yield an integral constant expression. Save and
5951 restore these values around our local parsing. */
5952 save_ice_p = parser->integral_constant_expression_p;
5953 save_non_ice_p = parser->non_integral_constant_expression_p;
5955 /* Consume the "__builtin_offsetof" token. */
5956 cp_lexer_consume_token (parser->lexer);
5957 /* Consume the opening `('. */
5958 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5959 /* Parse the type-id. */
5960 type = cp_parser_type_id (parser);
5961 /* Look for the `,'. */
5962 cp_parser_require (parser, CPP_COMMA, "`,'");
5964 /* Build the (type *)null that begins the traditional offsetof macro. */
5965 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5967 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5968 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5972 cp_token *token = cp_lexer_peek_token (parser->lexer);
5973 switch (token->type)
5975 case CPP_OPEN_SQUARE:
5976 /* offsetof-member-designator "[" expression "]" */
5977 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5981 /* offsetof-member-designator "." identifier */
5982 cp_lexer_consume_token (parser->lexer);
5983 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5987 case CPP_CLOSE_PAREN:
5988 /* Consume the ")" token. */
5989 cp_lexer_consume_token (parser->lexer);
5993 /* Error. We know the following require will fail, but
5994 that gives the proper error message. */
5995 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5996 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5997 expr = error_mark_node;
6003 /* If we're processing a template, we can't finish the semantics yet.
6004 Otherwise we can fold the entire expression now. */
6005 if (processing_template_decl)
6006 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6008 expr = fold_offsetof (expr);
6011 parser->integral_constant_expression_p = save_ice_p;
6012 parser->non_integral_constant_expression_p = save_non_ice_p;
6017 /* Statements [gram.stmt.stmt] */
6019 /* Parse a statement.
6023 expression-statement
6028 declaration-statement
6031 IN_COMPOUND is true when the statement is nested inside a
6032 cp_parser_compound_statement; this matters for certain pragmas. */
6035 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6040 location_t statement_location;
6043 /* There is no statement yet. */
6044 statement = NULL_TREE;
6045 /* Peek at the next token. */
6046 token = cp_lexer_peek_token (parser->lexer);
6047 /* Remember the location of the first token in the statement. */
6048 statement_location = token->location;
6049 /* If this is a keyword, then that will often determine what kind of
6050 statement we have. */
6051 if (token->type == CPP_KEYWORD)
6053 enum rid keyword = token->keyword;
6059 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6065 statement = cp_parser_selection_statement (parser);
6071 statement = cp_parser_iteration_statement (parser);
6078 statement = cp_parser_jump_statement (parser);
6081 /* Objective-C++ exception-handling constructs. */
6084 case RID_AT_FINALLY:
6085 case RID_AT_SYNCHRONIZED:
6087 statement = cp_parser_objc_statement (parser);
6091 statement = cp_parser_try_block (parser);
6095 /* It might be a keyword like `int' that can start a
6096 declaration-statement. */
6100 else if (token->type == CPP_NAME)
6102 /* If the next token is a `:', then we are looking at a
6103 labeled-statement. */
6104 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6105 if (token->type == CPP_COLON)
6106 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6109 /* Anything that starts with a `{' must be a compound-statement. */
6110 else if (token->type == CPP_OPEN_BRACE)
6111 statement = cp_parser_compound_statement (parser, NULL, false);
6112 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6113 a statement all its own. */
6114 else if (token->type == CPP_PRAGMA)
6116 /* Only certain OpenMP pragmas are attached to statements, and thus
6117 are considered statements themselves. All others are not. In
6118 the context of a compound, accept the pragma as a "statement" and
6119 return so that we can check for a close brace. Otherwise we
6120 require a real statement and must go back and read one. */
6122 cp_parser_pragma (parser, pragma_compound);
6123 else if (!cp_parser_pragma (parser, pragma_stmt))
6127 else if (token->type == CPP_EOF)
6129 cp_parser_error (parser, "expected statement");
6133 /* Everything else must be a declaration-statement or an
6134 expression-statement. Try for the declaration-statement
6135 first, unless we are looking at a `;', in which case we know that
6136 we have an expression-statement. */
6139 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6141 cp_parser_parse_tentatively (parser);
6142 /* Try to parse the declaration-statement. */
6143 cp_parser_declaration_statement (parser);
6144 /* If that worked, we're done. */
6145 if (cp_parser_parse_definitely (parser))
6148 /* Look for an expression-statement instead. */
6149 statement = cp_parser_expression_statement (parser, in_statement_expr);
6152 /* Set the line number for the statement. */
6153 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6154 SET_EXPR_LOCATION (statement, statement_location);
6157 /* Parse a labeled-statement.
6160 identifier : statement
6161 case constant-expression : statement
6167 case constant-expression ... constant-expression : statement
6169 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6170 For an ordinary label, returns a LABEL_EXPR.
6172 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6173 inside a compound. */
6176 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6180 tree statement = error_mark_node;
6182 /* The next token should be an identifier. */
6183 token = cp_lexer_peek_token (parser->lexer);
6184 if (token->type != CPP_NAME
6185 && token->type != CPP_KEYWORD)
6187 cp_parser_error (parser, "expected labeled-statement");
6188 return error_mark_node;
6191 switch (token->keyword)
6198 /* Consume the `case' token. */
6199 cp_lexer_consume_token (parser->lexer);
6200 /* Parse the constant-expression. */
6201 expr = cp_parser_constant_expression (parser,
6202 /*allow_non_constant_p=*/false,
6205 ellipsis = cp_lexer_peek_token (parser->lexer);
6206 if (ellipsis->type == CPP_ELLIPSIS)
6208 /* Consume the `...' token. */
6209 cp_lexer_consume_token (parser->lexer);
6211 cp_parser_constant_expression (parser,
6212 /*allow_non_constant_p=*/false,
6214 /* We don't need to emit warnings here, as the common code
6215 will do this for us. */
6218 expr_hi = NULL_TREE;
6220 if (parser->in_switch_statement_p)
6221 statement = finish_case_label (expr, expr_hi);
6223 error ("case label %qE not within a switch statement", expr);
6228 /* Consume the `default' token. */
6229 cp_lexer_consume_token (parser->lexer);
6231 if (parser->in_switch_statement_p)
6232 statement = finish_case_label (NULL_TREE, NULL_TREE);
6234 error ("case label not within a switch statement");
6238 /* Anything else must be an ordinary label. */
6239 statement = finish_label_stmt (cp_parser_identifier (parser));
6243 /* Require the `:' token. */
6244 cp_parser_require (parser, CPP_COLON, "`:'");
6245 /* Parse the labeled statement. */
6246 cp_parser_statement (parser, in_statement_expr, in_compound);
6248 /* Return the label, in the case of a `case' or `default' label. */
6252 /* Parse an expression-statement.
6254 expression-statement:
6257 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6258 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6259 indicates whether this expression-statement is part of an
6260 expression statement. */
6263 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6265 tree statement = NULL_TREE;
6267 /* If the next token is a ';', then there is no expression
6269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6270 statement = cp_parser_expression (parser, /*cast_p=*/false);
6272 /* Consume the final `;'. */
6273 cp_parser_consume_semicolon_at_end_of_statement (parser);
6275 if (in_statement_expr
6276 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6277 /* This is the final expression statement of a statement
6279 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6281 statement = finish_expr_stmt (statement);
6288 /* Parse a compound-statement.
6291 { statement-seq [opt] }
6293 Returns a tree representing the statement. */
6296 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6301 /* Consume the `{'. */
6302 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6303 return error_mark_node;
6304 /* Begin the compound-statement. */
6305 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6306 /* Parse an (optional) statement-seq. */
6307 cp_parser_statement_seq_opt (parser, in_statement_expr);
6308 /* Finish the compound-statement. */
6309 finish_compound_stmt (compound_stmt);
6310 /* Consume the `}'. */
6311 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6313 return compound_stmt;
6316 /* Parse an (optional) statement-seq.
6320 statement-seq [opt] statement */
6323 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6325 /* Scan statements until there aren't any more. */
6328 cp_token *token = cp_lexer_peek_token (parser->lexer);
6330 /* If we're looking at a `}', then we've run out of statements. */
6331 if (token->type == CPP_CLOSE_BRACE
6332 || token->type == CPP_EOF
6333 || token->type == CPP_PRAGMA_EOL)
6336 /* Parse the statement. */
6337 cp_parser_statement (parser, in_statement_expr, true);
6341 /* Parse a selection-statement.
6343 selection-statement:
6344 if ( condition ) statement
6345 if ( condition ) statement else statement
6346 switch ( condition ) statement
6348 Returns the new IF_STMT or SWITCH_STMT. */
6351 cp_parser_selection_statement (cp_parser* parser)
6356 /* Peek at the next token. */
6357 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6359 /* See what kind of keyword it is. */
6360 keyword = token->keyword;
6369 /* Look for the `('. */
6370 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6372 cp_parser_skip_to_end_of_statement (parser);
6373 return error_mark_node;
6376 /* Begin the selection-statement. */
6377 if (keyword == RID_IF)
6378 statement = begin_if_stmt ();
6380 statement = begin_switch_stmt ();
6382 /* Parse the condition. */
6383 condition = cp_parser_condition (parser);
6384 /* Look for the `)'. */
6385 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6386 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6387 /*consume_paren=*/true);
6389 if (keyword == RID_IF)
6391 /* Add the condition. */
6392 finish_if_stmt_cond (condition, statement);
6394 /* Parse the then-clause. */
6395 cp_parser_implicitly_scoped_statement (parser);
6396 finish_then_clause (statement);
6398 /* If the next token is `else', parse the else-clause. */
6399 if (cp_lexer_next_token_is_keyword (parser->lexer,
6402 /* Consume the `else' keyword. */
6403 cp_lexer_consume_token (parser->lexer);
6404 begin_else_clause (statement);
6405 /* Parse the else-clause. */
6406 cp_parser_implicitly_scoped_statement (parser);
6407 finish_else_clause (statement);
6410 /* Now we're all done with the if-statement. */
6411 finish_if_stmt (statement);
6415 bool in_switch_statement_p;
6417 /* Add the condition. */
6418 finish_switch_cond (condition, statement);
6420 /* Parse the body of the switch-statement. */
6421 in_switch_statement_p = parser->in_switch_statement_p;
6422 parser->in_switch_statement_p = true;
6423 cp_parser_implicitly_scoped_statement (parser);
6424 parser->in_switch_statement_p = in_switch_statement_p;
6426 /* Now we're all done with the switch-statement. */
6427 finish_switch_stmt (statement);
6435 cp_parser_error (parser, "expected selection-statement");
6436 return error_mark_node;
6440 /* Parse a condition.
6444 type-specifier-seq declarator = assignment-expression
6449 type-specifier-seq declarator asm-specification [opt]
6450 attributes [opt] = assignment-expression
6452 Returns the expression that should be tested. */
6455 cp_parser_condition (cp_parser* parser)
6457 cp_decl_specifier_seq type_specifiers;
6458 const char *saved_message;
6460 /* Try the declaration first. */
6461 cp_parser_parse_tentatively (parser);
6462 /* New types are not allowed in the type-specifier-seq for a
6464 saved_message = parser->type_definition_forbidden_message;
6465 parser->type_definition_forbidden_message
6466 = "types may not be defined in conditions";
6467 /* Parse the type-specifier-seq. */
6468 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6470 /* Restore the saved message. */
6471 parser->type_definition_forbidden_message = saved_message;
6472 /* If all is well, we might be looking at a declaration. */
6473 if (!cp_parser_error_occurred (parser))
6476 tree asm_specification;
6478 cp_declarator *declarator;
6479 tree initializer = NULL_TREE;
6481 /* Parse the declarator. */
6482 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6483 /*ctor_dtor_or_conv_p=*/NULL,
6484 /*parenthesized_p=*/NULL,
6485 /*member_p=*/false);
6486 /* Parse the attributes. */
6487 attributes = cp_parser_attributes_opt (parser);
6488 /* Parse the asm-specification. */
6489 asm_specification = cp_parser_asm_specification_opt (parser);
6490 /* If the next token is not an `=', then we might still be
6491 looking at an expression. For example:
6495 looks like a decl-specifier-seq and a declarator -- but then
6496 there is no `=', so this is an expression. */
6497 cp_parser_require (parser, CPP_EQ, "`='");
6498 /* If we did see an `=', then we are looking at a declaration
6500 if (cp_parser_parse_definitely (parser))
6503 bool non_constant_p;
6505 /* Create the declaration. */
6506 decl = start_decl (declarator, &type_specifiers,
6507 /*initialized_p=*/true,
6508 attributes, /*prefix_attributes=*/NULL_TREE,
6510 /* Parse the assignment-expression. */
6512 = cp_parser_constant_expression (parser,
6513 /*allow_non_constant_p=*/true,
6515 if (!non_constant_p)
6516 initializer = fold_non_dependent_expr (initializer);
6518 /* Process the initializer. */
6519 cp_finish_decl (decl,
6520 initializer, !non_constant_p,
6522 LOOKUP_ONLYCONVERTING);
6525 pop_scope (pushed_scope);
6527 return convert_from_reference (decl);
6530 /* If we didn't even get past the declarator successfully, we are
6531 definitely not looking at a declaration. */
6533 cp_parser_abort_tentative_parse (parser);
6535 /* Otherwise, we are looking at an expression. */
6536 return cp_parser_expression (parser, /*cast_p=*/false);
6539 /* Parse an iteration-statement.
6541 iteration-statement:
6542 while ( condition ) statement
6543 do statement while ( expression ) ;
6544 for ( for-init-statement condition [opt] ; expression [opt] )
6547 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6550 cp_parser_iteration_statement (cp_parser* parser)
6555 bool in_iteration_statement_p;
6558 /* Peek at the next token. */
6559 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6561 return error_mark_node;
6563 /* Remember whether or not we are already within an iteration
6565 in_iteration_statement_p = parser->in_iteration_statement_p;
6567 /* See what kind of keyword it is. */
6568 keyword = token->keyword;
6575 /* Begin the while-statement. */
6576 statement = begin_while_stmt ();
6577 /* Look for the `('. */
6578 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6579 /* Parse the condition. */
6580 condition = cp_parser_condition (parser);
6581 finish_while_stmt_cond (condition, statement);
6582 /* Look for the `)'. */
6583 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6584 /* Parse the dependent statement. */
6585 parser->in_iteration_statement_p = true;
6586 cp_parser_already_scoped_statement (parser);
6587 parser->in_iteration_statement_p = in_iteration_statement_p;
6588 /* We're done with the while-statement. */
6589 finish_while_stmt (statement);
6597 /* Begin the do-statement. */
6598 statement = begin_do_stmt ();
6599 /* Parse the body of the do-statement. */
6600 parser->in_iteration_statement_p = true;
6601 cp_parser_implicitly_scoped_statement (parser);
6602 parser->in_iteration_statement_p = in_iteration_statement_p;
6603 finish_do_body (statement);
6604 /* Look for the `while' keyword. */
6605 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6606 /* Look for the `('. */
6607 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6608 /* Parse the expression. */
6609 expression = cp_parser_expression (parser, /*cast_p=*/false);
6610 /* We're done with the do-statement. */
6611 finish_do_stmt (expression, statement);
6612 /* Look for the `)'. */
6613 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6614 /* Look for the `;'. */
6615 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6621 tree condition = NULL_TREE;
6622 tree expression = NULL_TREE;
6624 /* Begin the for-statement. */
6625 statement = begin_for_stmt ();
6626 /* Look for the `('. */
6627 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6628 /* Parse the initialization. */
6629 cp_parser_for_init_statement (parser);
6630 finish_for_init_stmt (statement);
6632 /* If there's a condition, process it. */
6633 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6634 condition = cp_parser_condition (parser);
6635 finish_for_cond (condition, statement);
6636 /* Look for the `;'. */
6637 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6639 /* If there's an expression, process it. */
6640 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6641 expression = cp_parser_expression (parser, /*cast_p=*/false);
6642 finish_for_expr (expression, statement);
6643 /* Look for the `)'. */
6644 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6646 /* Parse the body of the for-statement. */
6647 parser->in_iteration_statement_p = true;
6648 cp_parser_already_scoped_statement (parser);
6649 parser->in_iteration_statement_p = in_iteration_statement_p;
6651 /* We're done with the for-statement. */
6652 finish_for_stmt (statement);
6657 cp_parser_error (parser, "expected iteration-statement");
6658 statement = error_mark_node;
6665 /* Parse a for-init-statement.
6668 expression-statement
6669 simple-declaration */
6672 cp_parser_for_init_statement (cp_parser* parser)
6674 /* If the next token is a `;', then we have an empty
6675 expression-statement. Grammatically, this is also a
6676 simple-declaration, but an invalid one, because it does not
6677 declare anything. Therefore, if we did not handle this case
6678 specially, we would issue an error message about an invalid
6680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6682 /* We're going to speculatively look for a declaration, falling back
6683 to an expression, if necessary. */
6684 cp_parser_parse_tentatively (parser);
6685 /* Parse the declaration. */
6686 cp_parser_simple_declaration (parser,
6687 /*function_definition_allowed_p=*/false);
6688 /* If the tentative parse failed, then we shall need to look for an
6689 expression-statement. */
6690 if (cp_parser_parse_definitely (parser))
6694 cp_parser_expression_statement (parser, false);
6697 /* Parse a jump-statement.
6702 return expression [opt] ;
6710 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6713 cp_parser_jump_statement (cp_parser* parser)
6715 tree statement = error_mark_node;
6719 /* Peek at the next token. */
6720 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6722 return error_mark_node;
6724 /* See what kind of keyword it is. */
6725 keyword = token->keyword;
6729 if (!parser->in_switch_statement_p
6730 && !parser->in_iteration_statement_p)
6732 error ("break statement not within loop or switch");
6733 statement = error_mark_node;
6736 statement = finish_break_stmt ();
6737 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6741 if (!parser->in_iteration_statement_p)
6743 error ("continue statement not within a loop");
6744 statement = error_mark_node;
6747 statement = finish_continue_stmt ();
6748 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6755 /* If the next token is a `;', then there is no
6757 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6758 expr = cp_parser_expression (parser, /*cast_p=*/false);
6761 /* Build the return-statement. */
6762 statement = finish_return_stmt (expr);
6763 /* Look for the final `;'. */
6764 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6769 /* Create the goto-statement. */
6770 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6772 /* Issue a warning about this use of a GNU extension. */
6774 pedwarn ("ISO C++ forbids computed gotos");
6775 /* Consume the '*' token. */
6776 cp_lexer_consume_token (parser->lexer);
6777 /* Parse the dependent expression. */
6778 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6781 finish_goto_stmt (cp_parser_identifier (parser));
6782 /* Look for the final `;'. */
6783 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6787 cp_parser_error (parser, "expected jump-statement");
6794 /* Parse a declaration-statement.
6796 declaration-statement:
6797 block-declaration */
6800 cp_parser_declaration_statement (cp_parser* parser)
6804 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6805 p = obstack_alloc (&declarator_obstack, 0);
6807 /* Parse the block-declaration. */
6808 cp_parser_block_declaration (parser, /*statement_p=*/true);
6810 /* Free any declarators allocated. */
6811 obstack_free (&declarator_obstack, p);
6813 /* Finish off the statement. */
6817 /* Some dependent statements (like `if (cond) statement'), are
6818 implicitly in their own scope. In other words, if the statement is
6819 a single statement (as opposed to a compound-statement), it is
6820 none-the-less treated as if it were enclosed in braces. Any
6821 declarations appearing in the dependent statement are out of scope
6822 after control passes that point. This function parses a statement,
6823 but ensures that is in its own scope, even if it is not a
6826 Returns the new statement. */
6829 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6833 /* Mark if () ; with a special NOP_EXPR. */
6834 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6836 cp_lexer_consume_token (parser->lexer);
6837 statement = add_stmt (build_empty_stmt ());
6839 /* if a compound is opened, we simply parse the statement directly. */
6840 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6841 statement = cp_parser_compound_statement (parser, NULL, false);
6842 /* If the token is not a `{', then we must take special action. */
6845 /* Create a compound-statement. */
6846 statement = begin_compound_stmt (0);
6847 /* Parse the dependent-statement. */
6848 cp_parser_statement (parser, NULL_TREE, false);
6849 /* Finish the dummy compound-statement. */
6850 finish_compound_stmt (statement);
6853 /* Return the statement. */
6857 /* For some dependent statements (like `while (cond) statement'), we
6858 have already created a scope. Therefore, even if the dependent
6859 statement is a compound-statement, we do not want to create another
6863 cp_parser_already_scoped_statement (cp_parser* parser)
6865 /* If the token is a `{', then we must take special action. */
6866 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6867 cp_parser_statement (parser, NULL_TREE, false);
6870 /* Avoid calling cp_parser_compound_statement, so that we
6871 don't create a new scope. Do everything else by hand. */
6872 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6873 cp_parser_statement_seq_opt (parser, NULL_TREE);
6874 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6878 /* Declarations [gram.dcl.dcl] */
6880 /* Parse an optional declaration-sequence.
6884 declaration-seq declaration */
6887 cp_parser_declaration_seq_opt (cp_parser* parser)
6893 token = cp_lexer_peek_token (parser->lexer);
6895 if (token->type == CPP_CLOSE_BRACE
6896 || token->type == CPP_EOF
6897 || token->type == CPP_PRAGMA_EOL)
6900 if (token->type == CPP_SEMICOLON)
6902 /* A declaration consisting of a single semicolon is
6903 invalid. Allow it unless we're being pedantic. */
6904 cp_lexer_consume_token (parser->lexer);
6905 if (pedantic && !in_system_header)
6906 pedwarn ("extra %<;%>");
6910 /* If we're entering or exiting a region that's implicitly
6911 extern "C", modify the lang context appropriately. */
6912 if (!parser->implicit_extern_c && token->implicit_extern_c)
6914 push_lang_context (lang_name_c);
6915 parser->implicit_extern_c = true;
6917 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6919 pop_lang_context ();
6920 parser->implicit_extern_c = false;
6923 if (token->type == CPP_PRAGMA)
6925 /* A top-level declaration can consist solely of a #pragma.
6926 A nested declaration cannot, so this is done here and not
6927 in cp_parser_declaration. (A #pragma at block scope is
6928 handled in cp_parser_statement.) */
6929 cp_parser_pragma (parser, pragma_external);
6933 /* Parse the declaration itself. */
6934 cp_parser_declaration (parser);
6938 /* Parse a declaration.
6943 template-declaration
6944 explicit-instantiation
6945 explicit-specialization
6946 linkage-specification
6947 namespace-definition
6952 __extension__ declaration */
6955 cp_parser_declaration (cp_parser* parser)
6962 /* Check for the `__extension__' keyword. */
6963 if (cp_parser_extension_opt (parser, &saved_pedantic))
6965 /* Parse the qualified declaration. */
6966 cp_parser_declaration (parser);
6967 /* Restore the PEDANTIC flag. */
6968 pedantic = saved_pedantic;
6973 /* Try to figure out what kind of declaration is present. */
6974 token1 = *cp_lexer_peek_token (parser->lexer);
6976 if (token1.type != CPP_EOF)
6977 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6980 token2.type = CPP_EOF;
6981 token2.keyword = RID_MAX;
6984 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6985 p = obstack_alloc (&declarator_obstack, 0);
6987 /* If the next token is `extern' and the following token is a string
6988 literal, then we have a linkage specification. */
6989 if (token1.keyword == RID_EXTERN
6990 && cp_parser_is_string_literal (&token2))
6991 cp_parser_linkage_specification (parser);
6992 /* If the next token is `template', then we have either a template
6993 declaration, an explicit instantiation, or an explicit
6995 else if (token1.keyword == RID_TEMPLATE)
6997 /* `template <>' indicates a template specialization. */
6998 if (token2.type == CPP_LESS
6999 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7000 cp_parser_explicit_specialization (parser);
7001 /* `template <' indicates a template declaration. */
7002 else if (token2.type == CPP_LESS)
7003 cp_parser_template_declaration (parser, /*member_p=*/false);
7004 /* Anything else must be an explicit instantiation. */
7006 cp_parser_explicit_instantiation (parser);
7008 /* If the next token is `export', then we have a template
7010 else if (token1.keyword == RID_EXPORT)
7011 cp_parser_template_declaration (parser, /*member_p=*/false);
7012 /* If the next token is `extern', 'static' or 'inline' and the one
7013 after that is `template', we have a GNU extended explicit
7014 instantiation directive. */
7015 else if (cp_parser_allow_gnu_extensions_p (parser)
7016 && (token1.keyword == RID_EXTERN
7017 || token1.keyword == RID_STATIC
7018 || token1.keyword == RID_INLINE)
7019 && token2.keyword == RID_TEMPLATE)
7020 cp_parser_explicit_instantiation (parser);
7021 /* If the next token is `namespace', check for a named or unnamed
7022 namespace definition. */
7023 else if (token1.keyword == RID_NAMESPACE
7024 && (/* A named namespace definition. */
7025 (token2.type == CPP_NAME
7026 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7028 /* An unnamed namespace definition. */
7029 || token2.type == CPP_OPEN_BRACE))
7030 cp_parser_namespace_definition (parser);
7031 /* Objective-C++ declaration/definition. */
7032 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7033 cp_parser_objc_declaration (parser);
7034 /* We must have either a block declaration or a function
7037 /* Try to parse a block-declaration, or a function-definition. */
7038 cp_parser_block_declaration (parser, /*statement_p=*/false);
7040 /* Free any declarators allocated. */
7041 obstack_free (&declarator_obstack, p);
7044 /* Parse a block-declaration.
7049 namespace-alias-definition
7056 __extension__ block-declaration
7059 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7060 part of a declaration-statement. */
7063 cp_parser_block_declaration (cp_parser *parser,
7069 /* Check for the `__extension__' keyword. */
7070 if (cp_parser_extension_opt (parser, &saved_pedantic))
7072 /* Parse the qualified declaration. */
7073 cp_parser_block_declaration (parser, statement_p);
7074 /* Restore the PEDANTIC flag. */
7075 pedantic = saved_pedantic;
7080 /* Peek at the next token to figure out which kind of declaration is
7082 token1 = cp_lexer_peek_token (parser->lexer);
7084 /* If the next keyword is `asm', we have an asm-definition. */
7085 if (token1->keyword == RID_ASM)
7088 cp_parser_commit_to_tentative_parse (parser);
7089 cp_parser_asm_definition (parser);
7091 /* If the next keyword is `namespace', we have a
7092 namespace-alias-definition. */
7093 else if (token1->keyword == RID_NAMESPACE)
7094 cp_parser_namespace_alias_definition (parser);
7095 /* If the next keyword is `using', we have either a
7096 using-declaration or a using-directive. */
7097 else if (token1->keyword == RID_USING)
7102 cp_parser_commit_to_tentative_parse (parser);
7103 /* If the token after `using' is `namespace', then we have a
7105 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7106 if (token2->keyword == RID_NAMESPACE)
7107 cp_parser_using_directive (parser);
7108 /* Otherwise, it's a using-declaration. */
7110 cp_parser_using_declaration (parser);
7112 /* If the next keyword is `__label__' we have a label declaration. */
7113 else if (token1->keyword == RID_LABEL)
7116 cp_parser_commit_to_tentative_parse (parser);
7117 cp_parser_label_declaration (parser);
7119 /* Anything else must be a simple-declaration. */
7121 cp_parser_simple_declaration (parser, !statement_p);
7124 /* Parse a simple-declaration.
7127 decl-specifier-seq [opt] init-declarator-list [opt] ;
7129 init-declarator-list:
7131 init-declarator-list , init-declarator
7133 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7134 function-definition as a simple-declaration. */
7137 cp_parser_simple_declaration (cp_parser* parser,
7138 bool function_definition_allowed_p)
7140 cp_decl_specifier_seq decl_specifiers;
7141 int declares_class_or_enum;
7142 bool saw_declarator;
7144 /* Defer access checks until we know what is being declared; the
7145 checks for names appearing in the decl-specifier-seq should be
7146 done as if we were in the scope of the thing being declared. */
7147 push_deferring_access_checks (dk_deferred);
7149 /* Parse the decl-specifier-seq. We have to keep track of whether
7150 or not the decl-specifier-seq declares a named class or
7151 enumeration type, since that is the only case in which the
7152 init-declarator-list is allowed to be empty.
7156 In a simple-declaration, the optional init-declarator-list can be
7157 omitted only when declaring a class or enumeration, that is when
7158 the decl-specifier-seq contains either a class-specifier, an
7159 elaborated-type-specifier, or an enum-specifier. */
7160 cp_parser_decl_specifier_seq (parser,
7161 CP_PARSER_FLAGS_OPTIONAL,
7163 &declares_class_or_enum);
7164 /* We no longer need to defer access checks. */
7165 stop_deferring_access_checks ();
7167 /* In a block scope, a valid declaration must always have a
7168 decl-specifier-seq. By not trying to parse declarators, we can
7169 resolve the declaration/expression ambiguity more quickly. */
7170 if (!function_definition_allowed_p
7171 && !decl_specifiers.any_specifiers_p)
7173 cp_parser_error (parser, "expected declaration");
7177 /* If the next two tokens are both identifiers, the code is
7178 erroneous. The usual cause of this situation is code like:
7182 where "T" should name a type -- but does not. */
7183 if (!decl_specifiers.type
7184 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7186 /* If parsing tentatively, we should commit; we really are
7187 looking at a declaration. */
7188 cp_parser_commit_to_tentative_parse (parser);
7193 /* If we have seen at least one decl-specifier, and the next token
7194 is not a parenthesis, then we must be looking at a declaration.
7195 (After "int (" we might be looking at a functional cast.) */
7196 if (decl_specifiers.any_specifiers_p
7197 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7198 cp_parser_commit_to_tentative_parse (parser);
7200 /* Keep going until we hit the `;' at the end of the simple
7202 saw_declarator = false;
7203 while (cp_lexer_next_token_is_not (parser->lexer,
7207 bool function_definition_p;
7212 /* If we are processing next declarator, coma is expected */
7213 token = cp_lexer_peek_token (parser->lexer);
7214 gcc_assert (token->type == CPP_COMMA);
7215 cp_lexer_consume_token (parser->lexer);
7218 saw_declarator = true;
7220 /* Parse the init-declarator. */
7221 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7222 function_definition_allowed_p,
7224 declares_class_or_enum,
7225 &function_definition_p);
7226 /* If an error occurred while parsing tentatively, exit quickly.
7227 (That usually happens when in the body of a function; each
7228 statement is treated as a declaration-statement until proven
7230 if (cp_parser_error_occurred (parser))
7232 /* Handle function definitions specially. */
7233 if (function_definition_p)
7235 /* If the next token is a `,', then we are probably
7236 processing something like:
7240 which is erroneous. */
7241 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7242 error ("mixing declarations and function-definitions is forbidden");
7243 /* Otherwise, we're done with the list of declarators. */
7246 pop_deferring_access_checks ();
7250 /* The next token should be either a `,' or a `;'. */
7251 token = cp_lexer_peek_token (parser->lexer);
7252 /* If it's a `,', there are more declarators to come. */
7253 if (token->type == CPP_COMMA)
7254 /* will be consumed next time around */;
7255 /* If it's a `;', we are done. */
7256 else if (token->type == CPP_SEMICOLON)
7258 /* Anything else is an error. */
7261 /* If we have already issued an error message we don't need
7262 to issue another one. */
7263 if (decl != error_mark_node
7264 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7265 cp_parser_error (parser, "expected %<,%> or %<;%>");
7266 /* Skip tokens until we reach the end of the statement. */
7267 cp_parser_skip_to_end_of_statement (parser);
7268 /* If the next token is now a `;', consume it. */
7269 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7270 cp_lexer_consume_token (parser->lexer);
7273 /* After the first time around, a function-definition is not
7274 allowed -- even if it was OK at first. For example:
7279 function_definition_allowed_p = false;
7282 /* Issue an error message if no declarators are present, and the
7283 decl-specifier-seq does not itself declare a class or
7285 if (!saw_declarator)
7287 if (cp_parser_declares_only_class_p (parser))
7288 shadow_tag (&decl_specifiers);
7289 /* Perform any deferred access checks. */
7290 perform_deferred_access_checks ();
7293 /* Consume the `;'. */
7294 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7297 pop_deferring_access_checks ();
7300 /* Parse a decl-specifier-seq.
7303 decl-specifier-seq [opt] decl-specifier
7306 storage-class-specifier
7317 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7319 The parser flags FLAGS is used to control type-specifier parsing.
7321 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7324 1: one of the decl-specifiers is an elaborated-type-specifier
7325 (i.e., a type declaration)
7326 2: one of the decl-specifiers is an enum-specifier or a
7327 class-specifier (i.e., a type definition)
7332 cp_parser_decl_specifier_seq (cp_parser* parser,
7333 cp_parser_flags flags,
7334 cp_decl_specifier_seq *decl_specs,
7335 int* declares_class_or_enum)
7337 bool constructor_possible_p = !parser->in_declarator_p;
7340 /* Clear DECL_SPECS. */
7341 clear_decl_specs (decl_specs);
7343 /* Assume no class or enumeration type is declared. */
7344 *declares_class_or_enum = 0;
7346 /* Keep reading specifiers until there are no more to read. */
7350 bool found_decl_spec;
7353 /* Peek at the next token. */
7354 token = cp_lexer_peek_token (parser->lexer);
7355 /* Handle attributes. */
7356 if (token->keyword == RID_ATTRIBUTE)
7358 /* Parse the attributes. */
7359 decl_specs->attributes
7360 = chainon (decl_specs->attributes,
7361 cp_parser_attributes_opt (parser));
7364 /* Assume we will find a decl-specifier keyword. */
7365 found_decl_spec = true;
7366 /* If the next token is an appropriate keyword, we can simply
7367 add it to the list. */
7368 switch (token->keyword)
7373 ++decl_specs->specs[(int) ds_friend];
7374 /* Consume the token. */
7375 cp_lexer_consume_token (parser->lexer);
7378 /* function-specifier:
7385 cp_parser_function_specifier_opt (parser, decl_specs);
7391 ++decl_specs->specs[(int) ds_typedef];
7392 /* Consume the token. */
7393 cp_lexer_consume_token (parser->lexer);
7394 /* A constructor declarator cannot appear in a typedef. */
7395 constructor_possible_p = false;
7396 /* The "typedef" keyword can only occur in a declaration; we
7397 may as well commit at this point. */
7398 cp_parser_commit_to_tentative_parse (parser);
7401 /* storage-class-specifier:
7411 /* Consume the token. */
7412 cp_lexer_consume_token (parser->lexer);
7413 cp_parser_set_storage_class (decl_specs, sc_auto);
7416 /* Consume the token. */
7417 cp_lexer_consume_token (parser->lexer);
7418 cp_parser_set_storage_class (decl_specs, sc_register);
7421 /* Consume the token. */
7422 cp_lexer_consume_token (parser->lexer);
7423 if (decl_specs->specs[(int) ds_thread])
7425 error ("%<__thread%> before %<static%>");
7426 decl_specs->specs[(int) ds_thread] = 0;
7428 cp_parser_set_storage_class (decl_specs, sc_static);
7431 /* Consume the token. */
7432 cp_lexer_consume_token (parser->lexer);
7433 if (decl_specs->specs[(int) ds_thread])
7435 error ("%<__thread%> before %<extern%>");
7436 decl_specs->specs[(int) ds_thread] = 0;
7438 cp_parser_set_storage_class (decl_specs, sc_extern);
7441 /* Consume the token. */
7442 cp_lexer_consume_token (parser->lexer);
7443 cp_parser_set_storage_class (decl_specs, sc_mutable);
7446 /* Consume the token. */
7447 cp_lexer_consume_token (parser->lexer);
7448 ++decl_specs->specs[(int) ds_thread];
7452 /* We did not yet find a decl-specifier yet. */
7453 found_decl_spec = false;
7457 /* Constructors are a special case. The `S' in `S()' is not a
7458 decl-specifier; it is the beginning of the declarator. */
7461 && constructor_possible_p
7462 && (cp_parser_constructor_declarator_p
7463 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7465 /* If we don't have a DECL_SPEC yet, then we must be looking at
7466 a type-specifier. */
7467 if (!found_decl_spec && !constructor_p)
7469 int decl_spec_declares_class_or_enum;
7470 bool is_cv_qualifier;
7474 = cp_parser_type_specifier (parser, flags,
7476 /*is_declaration=*/true,
7477 &decl_spec_declares_class_or_enum,
7480 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7482 /* If this type-specifier referenced a user-defined type
7483 (a typedef, class-name, etc.), then we can't allow any
7484 more such type-specifiers henceforth.
7488 The longest sequence of decl-specifiers that could
7489 possibly be a type name is taken as the
7490 decl-specifier-seq of a declaration. The sequence shall
7491 be self-consistent as described below.
7495 As a general rule, at most one type-specifier is allowed
7496 in the complete decl-specifier-seq of a declaration. The
7497 only exceptions are the following:
7499 -- const or volatile can be combined with any other
7502 -- signed or unsigned can be combined with char, long,
7510 void g (const int Pc);
7512 Here, Pc is *not* part of the decl-specifier seq; it's
7513 the declarator. Therefore, once we see a type-specifier
7514 (other than a cv-qualifier), we forbid any additional
7515 user-defined types. We *do* still allow things like `int
7516 int' to be considered a decl-specifier-seq, and issue the
7517 error message later. */
7518 if (type_spec && !is_cv_qualifier)
7519 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7520 /* A constructor declarator cannot follow a type-specifier. */
7523 constructor_possible_p = false;
7524 found_decl_spec = true;
7528 /* If we still do not have a DECL_SPEC, then there are no more
7530 if (!found_decl_spec)
7533 decl_specs->any_specifiers_p = true;
7534 /* After we see one decl-specifier, further decl-specifiers are
7536 flags |= CP_PARSER_FLAGS_OPTIONAL;
7539 /* Check for repeated decl-specifiers. */
7540 for (ds = ds_first; ds != ds_last; ++ds)
7542 unsigned count = decl_specs->specs[(int)ds];
7545 /* The "long" specifier is a special case because of "long long". */
7549 error ("%<long long long%> is too long for GCC");
7550 else if (pedantic && !in_system_header && warn_long_long)
7551 pedwarn ("ISO C++ does not support %<long long%>");
7555 static const char *const decl_spec_names[] = {
7571 error ("duplicate %qs", decl_spec_names[(int)ds]);
7575 /* Don't allow a friend specifier with a class definition. */
7576 if (decl_specs->specs[(int) ds_friend] != 0
7577 && (*declares_class_or_enum & 2))
7578 error ("class definition may not be declared a friend");
7581 /* Parse an (optional) storage-class-specifier.
7583 storage-class-specifier:
7592 storage-class-specifier:
7595 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7598 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7600 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7608 /* Consume the token. */
7609 return cp_lexer_consume_token (parser->lexer)->value;
7616 /* Parse an (optional) function-specifier.
7623 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7624 Updates DECL_SPECS, if it is non-NULL. */
7627 cp_parser_function_specifier_opt (cp_parser* parser,
7628 cp_decl_specifier_seq *decl_specs)
7630 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7634 ++decl_specs->specs[(int) ds_inline];
7639 ++decl_specs->specs[(int) ds_virtual];
7644 ++decl_specs->specs[(int) ds_explicit];
7651 /* Consume the token. */
7652 return cp_lexer_consume_token (parser->lexer)->value;
7655 /* Parse a linkage-specification.
7657 linkage-specification:
7658 extern string-literal { declaration-seq [opt] }
7659 extern string-literal declaration */
7662 cp_parser_linkage_specification (cp_parser* parser)
7666 /* Look for the `extern' keyword. */
7667 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7669 /* Look for the string-literal. */
7670 linkage = cp_parser_string_literal (parser, false, false);
7672 /* Transform the literal into an identifier. If the literal is a
7673 wide-character string, or contains embedded NULs, then we can't
7674 handle it as the user wants. */
7675 if (strlen (TREE_STRING_POINTER (linkage))
7676 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7678 cp_parser_error (parser, "invalid linkage-specification");
7679 /* Assume C++ linkage. */
7680 linkage = lang_name_cplusplus;
7683 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7685 /* We're now using the new linkage. */
7686 push_lang_context (linkage);
7688 /* If the next token is a `{', then we're using the first
7690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7692 /* Consume the `{' token. */
7693 cp_lexer_consume_token (parser->lexer);
7694 /* Parse the declarations. */
7695 cp_parser_declaration_seq_opt (parser);
7696 /* Look for the closing `}'. */
7697 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7699 /* Otherwise, there's just one declaration. */
7702 bool saved_in_unbraced_linkage_specification_p;
7704 saved_in_unbraced_linkage_specification_p
7705 = parser->in_unbraced_linkage_specification_p;
7706 parser->in_unbraced_linkage_specification_p = true;
7707 have_extern_spec = true;
7708 cp_parser_declaration (parser);
7709 have_extern_spec = false;
7710 parser->in_unbraced_linkage_specification_p
7711 = saved_in_unbraced_linkage_specification_p;
7714 /* We're done with the linkage-specification. */
7715 pop_lang_context ();
7718 /* Special member functions [gram.special] */
7720 /* Parse a conversion-function-id.
7722 conversion-function-id:
7723 operator conversion-type-id
7725 Returns an IDENTIFIER_NODE representing the operator. */
7728 cp_parser_conversion_function_id (cp_parser* parser)
7732 tree saved_qualifying_scope;
7733 tree saved_object_scope;
7734 tree pushed_scope = NULL_TREE;
7736 /* Look for the `operator' token. */
7737 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7738 return error_mark_node;
7739 /* When we parse the conversion-type-id, the current scope will be
7740 reset. However, we need that information in able to look up the
7741 conversion function later, so we save it here. */
7742 saved_scope = parser->scope;
7743 saved_qualifying_scope = parser->qualifying_scope;
7744 saved_object_scope = parser->object_scope;
7745 /* We must enter the scope of the class so that the names of
7746 entities declared within the class are available in the
7747 conversion-type-id. For example, consider:
7754 S::operator I() { ... }
7756 In order to see that `I' is a type-name in the definition, we
7757 must be in the scope of `S'. */
7759 pushed_scope = push_scope (saved_scope);
7760 /* Parse the conversion-type-id. */
7761 type = cp_parser_conversion_type_id (parser);
7762 /* Leave the scope of the class, if any. */
7764 pop_scope (pushed_scope);
7765 /* Restore the saved scope. */
7766 parser->scope = saved_scope;
7767 parser->qualifying_scope = saved_qualifying_scope;
7768 parser->object_scope = saved_object_scope;
7769 /* If the TYPE is invalid, indicate failure. */
7770 if (type == error_mark_node)
7771 return error_mark_node;
7772 return mangle_conv_op_name_for_type (type);
7775 /* Parse a conversion-type-id:
7778 type-specifier-seq conversion-declarator [opt]
7780 Returns the TYPE specified. */
7783 cp_parser_conversion_type_id (cp_parser* parser)
7786 cp_decl_specifier_seq type_specifiers;
7787 cp_declarator *declarator;
7788 tree type_specified;
7790 /* Parse the attributes. */
7791 attributes = cp_parser_attributes_opt (parser);
7792 /* Parse the type-specifiers. */
7793 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7795 /* If that didn't work, stop. */
7796 if (type_specifiers.type == error_mark_node)
7797 return error_mark_node;
7798 /* Parse the conversion-declarator. */
7799 declarator = cp_parser_conversion_declarator_opt (parser);
7801 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7802 /*initialized=*/0, &attributes);
7804 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7805 return type_specified;
7808 /* Parse an (optional) conversion-declarator.
7810 conversion-declarator:
7811 ptr-operator conversion-declarator [opt]
7815 static cp_declarator *
7816 cp_parser_conversion_declarator_opt (cp_parser* parser)
7818 enum tree_code code;
7820 cp_cv_quals cv_quals;
7822 /* We don't know if there's a ptr-operator next, or not. */
7823 cp_parser_parse_tentatively (parser);
7824 /* Try the ptr-operator. */
7825 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7826 /* If it worked, look for more conversion-declarators. */
7827 if (cp_parser_parse_definitely (parser))
7829 cp_declarator *declarator;
7831 /* Parse another optional declarator. */
7832 declarator = cp_parser_conversion_declarator_opt (parser);
7834 /* Create the representation of the declarator. */
7836 declarator = make_ptrmem_declarator (cv_quals, class_type,
7838 else if (code == INDIRECT_REF)
7839 declarator = make_pointer_declarator (cv_quals, declarator);
7841 declarator = make_reference_declarator (cv_quals, declarator);
7849 /* Parse an (optional) ctor-initializer.
7852 : mem-initializer-list
7854 Returns TRUE iff the ctor-initializer was actually present. */
7857 cp_parser_ctor_initializer_opt (cp_parser* parser)
7859 /* If the next token is not a `:', then there is no
7860 ctor-initializer. */
7861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7863 /* Do default initialization of any bases and members. */
7864 if (DECL_CONSTRUCTOR_P (current_function_decl))
7865 finish_mem_initializers (NULL_TREE);
7870 /* Consume the `:' token. */
7871 cp_lexer_consume_token (parser->lexer);
7872 /* And the mem-initializer-list. */
7873 cp_parser_mem_initializer_list (parser);
7878 /* Parse a mem-initializer-list.
7880 mem-initializer-list:
7882 mem-initializer , mem-initializer-list */
7885 cp_parser_mem_initializer_list (cp_parser* parser)
7887 tree mem_initializer_list = NULL_TREE;
7889 /* Let the semantic analysis code know that we are starting the
7890 mem-initializer-list. */
7891 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7892 error ("only constructors take base initializers");
7894 /* Loop through the list. */
7897 tree mem_initializer;
7899 /* Parse the mem-initializer. */
7900 mem_initializer = cp_parser_mem_initializer (parser);
7901 /* Add it to the list, unless it was erroneous. */
7902 if (mem_initializer != error_mark_node)
7904 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7905 mem_initializer_list = mem_initializer;
7907 /* If the next token is not a `,', we're done. */
7908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7910 /* Consume the `,' token. */
7911 cp_lexer_consume_token (parser->lexer);
7914 /* Perform semantic analysis. */
7915 if (DECL_CONSTRUCTOR_P (current_function_decl))
7916 finish_mem_initializers (mem_initializer_list);
7919 /* Parse a mem-initializer.
7922 mem-initializer-id ( expression-list [opt] )
7927 ( expression-list [opt] )
7929 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7930 class) or FIELD_DECL (for a non-static data member) to initialize;
7931 the TREE_VALUE is the expression-list. An empty initialization
7932 list is represented by void_list_node. */
7935 cp_parser_mem_initializer (cp_parser* parser)
7937 tree mem_initializer_id;
7938 tree expression_list;
7941 /* Find out what is being initialized. */
7942 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7944 pedwarn ("anachronistic old-style base class initializer");
7945 mem_initializer_id = NULL_TREE;
7948 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7949 member = expand_member_init (mem_initializer_id);
7950 if (member && !DECL_P (member))
7951 in_base_initializer = 1;
7954 = cp_parser_parenthesized_expression_list (parser, false,
7956 /*non_constant_p=*/NULL);
7957 if (expression_list == error_mark_node)
7958 return error_mark_node;
7959 if (!expression_list)
7960 expression_list = void_type_node;
7962 in_base_initializer = 0;
7964 return member ? build_tree_list (member, expression_list) : error_mark_node;
7967 /* Parse a mem-initializer-id.
7970 :: [opt] nested-name-specifier [opt] class-name
7973 Returns a TYPE indicating the class to be initializer for the first
7974 production. Returns an IDENTIFIER_NODE indicating the data member
7975 to be initialized for the second production. */
7978 cp_parser_mem_initializer_id (cp_parser* parser)
7980 bool global_scope_p;
7981 bool nested_name_specifier_p;
7982 bool template_p = false;
7985 /* `typename' is not allowed in this context ([temp.res]). */
7986 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7988 error ("keyword %<typename%> not allowed in this context (a qualified "
7989 "member initializer is implicitly a type)");
7990 cp_lexer_consume_token (parser->lexer);
7992 /* Look for the optional `::' operator. */
7994 = (cp_parser_global_scope_opt (parser,
7995 /*current_scope_valid_p=*/false)
7997 /* Look for the optional nested-name-specifier. The simplest way to
8002 The keyword `typename' is not permitted in a base-specifier or
8003 mem-initializer; in these contexts a qualified name that
8004 depends on a template-parameter is implicitly assumed to be a
8007 is to assume that we have seen the `typename' keyword at this
8009 nested_name_specifier_p
8010 = (cp_parser_nested_name_specifier_opt (parser,
8011 /*typename_keyword_p=*/true,
8012 /*check_dependency_p=*/true,
8014 /*is_declaration=*/true)
8016 if (nested_name_specifier_p)
8017 template_p = cp_parser_optional_template_keyword (parser);
8018 /* If there is a `::' operator or a nested-name-specifier, then we
8019 are definitely looking for a class-name. */
8020 if (global_scope_p || nested_name_specifier_p)
8021 return cp_parser_class_name (parser,
8022 /*typename_keyword_p=*/true,
8023 /*template_keyword_p=*/template_p,
8025 /*check_dependency_p=*/true,
8026 /*class_head_p=*/false,
8027 /*is_declaration=*/true);
8028 /* Otherwise, we could also be looking for an ordinary identifier. */
8029 cp_parser_parse_tentatively (parser);
8030 /* Try a class-name. */
8031 id = cp_parser_class_name (parser,
8032 /*typename_keyword_p=*/true,
8033 /*template_keyword_p=*/false,
8035 /*check_dependency_p=*/true,
8036 /*class_head_p=*/false,
8037 /*is_declaration=*/true);
8038 /* If we found one, we're done. */
8039 if (cp_parser_parse_definitely (parser))
8041 /* Otherwise, look for an ordinary identifier. */
8042 return cp_parser_identifier (parser);
8045 /* Overloading [gram.over] */
8047 /* Parse an operator-function-id.
8049 operator-function-id:
8052 Returns an IDENTIFIER_NODE for the operator which is a
8053 human-readable spelling of the identifier, e.g., `operator +'. */
8056 cp_parser_operator_function_id (cp_parser* parser)
8058 /* Look for the `operator' keyword. */
8059 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8060 return error_mark_node;
8061 /* And then the name of the operator itself. */
8062 return cp_parser_operator (parser);
8065 /* Parse an operator.
8068 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8069 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8070 || ++ -- , ->* -> () []
8077 Returns an IDENTIFIER_NODE for the operator which is a
8078 human-readable spelling of the identifier, e.g., `operator +'. */
8081 cp_parser_operator (cp_parser* parser)
8083 tree id = NULL_TREE;
8086 /* Peek at the next token. */
8087 token = cp_lexer_peek_token (parser->lexer);
8088 /* Figure out which operator we have. */
8089 switch (token->type)
8095 /* The keyword should be either `new' or `delete'. */
8096 if (token->keyword == RID_NEW)
8098 else if (token->keyword == RID_DELETE)
8103 /* Consume the `new' or `delete' token. */
8104 cp_lexer_consume_token (parser->lexer);
8106 /* Peek at the next token. */
8107 token = cp_lexer_peek_token (parser->lexer);
8108 /* If it's a `[' token then this is the array variant of the
8110 if (token->type == CPP_OPEN_SQUARE)
8112 /* Consume the `[' token. */
8113 cp_lexer_consume_token (parser->lexer);
8114 /* Look for the `]' token. */
8115 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8116 id = ansi_opname (op == NEW_EXPR
8117 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8119 /* Otherwise, we have the non-array variant. */
8121 id = ansi_opname (op);
8127 id = ansi_opname (PLUS_EXPR);
8131 id = ansi_opname (MINUS_EXPR);
8135 id = ansi_opname (MULT_EXPR);
8139 id = ansi_opname (TRUNC_DIV_EXPR);
8143 id = ansi_opname (TRUNC_MOD_EXPR);
8147 id = ansi_opname (BIT_XOR_EXPR);
8151 id = ansi_opname (BIT_AND_EXPR);
8155 id = ansi_opname (BIT_IOR_EXPR);
8159 id = ansi_opname (BIT_NOT_EXPR);
8163 id = ansi_opname (TRUTH_NOT_EXPR);
8167 id = ansi_assopname (NOP_EXPR);
8171 id = ansi_opname (LT_EXPR);
8175 id = ansi_opname (GT_EXPR);
8179 id = ansi_assopname (PLUS_EXPR);
8183 id = ansi_assopname (MINUS_EXPR);
8187 id = ansi_assopname (MULT_EXPR);
8191 id = ansi_assopname (TRUNC_DIV_EXPR);
8195 id = ansi_assopname (TRUNC_MOD_EXPR);
8199 id = ansi_assopname (BIT_XOR_EXPR);
8203 id = ansi_assopname (BIT_AND_EXPR);
8207 id = ansi_assopname (BIT_IOR_EXPR);
8211 id = ansi_opname (LSHIFT_EXPR);
8215 id = ansi_opname (RSHIFT_EXPR);
8219 id = ansi_assopname (LSHIFT_EXPR);
8223 id = ansi_assopname (RSHIFT_EXPR);
8227 id = ansi_opname (EQ_EXPR);
8231 id = ansi_opname (NE_EXPR);
8235 id = ansi_opname (LE_EXPR);
8238 case CPP_GREATER_EQ:
8239 id = ansi_opname (GE_EXPR);
8243 id = ansi_opname (TRUTH_ANDIF_EXPR);
8247 id = ansi_opname (TRUTH_ORIF_EXPR);
8251 id = ansi_opname (POSTINCREMENT_EXPR);
8254 case CPP_MINUS_MINUS:
8255 id = ansi_opname (PREDECREMENT_EXPR);
8259 id = ansi_opname (COMPOUND_EXPR);
8262 case CPP_DEREF_STAR:
8263 id = ansi_opname (MEMBER_REF);
8267 id = ansi_opname (COMPONENT_REF);
8270 case CPP_OPEN_PAREN:
8271 /* Consume the `('. */
8272 cp_lexer_consume_token (parser->lexer);
8273 /* Look for the matching `)'. */
8274 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8275 return ansi_opname (CALL_EXPR);
8277 case CPP_OPEN_SQUARE:
8278 /* Consume the `['. */
8279 cp_lexer_consume_token (parser->lexer);
8280 /* Look for the matching `]'. */
8281 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8282 return ansi_opname (ARRAY_REF);
8286 id = ansi_opname (MIN_EXPR);
8287 cp_parser_warn_min_max ();
8291 id = ansi_opname (MAX_EXPR);
8292 cp_parser_warn_min_max ();
8296 id = ansi_assopname (MIN_EXPR);
8297 cp_parser_warn_min_max ();
8301 id = ansi_assopname (MAX_EXPR);
8302 cp_parser_warn_min_max ();
8306 /* Anything else is an error. */
8310 /* If we have selected an identifier, we need to consume the
8313 cp_lexer_consume_token (parser->lexer);
8314 /* Otherwise, no valid operator name was present. */
8317 cp_parser_error (parser, "expected operator");
8318 id = error_mark_node;
8324 /* Parse a template-declaration.
8326 template-declaration:
8327 export [opt] template < template-parameter-list > declaration
8329 If MEMBER_P is TRUE, this template-declaration occurs within a
8332 The grammar rule given by the standard isn't correct. What
8335 template-declaration:
8336 export [opt] template-parameter-list-seq
8337 decl-specifier-seq [opt] init-declarator [opt] ;
8338 export [opt] template-parameter-list-seq
8341 template-parameter-list-seq:
8342 template-parameter-list-seq [opt]
8343 template < template-parameter-list > */
8346 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8348 /* Check for `export'. */
8349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8351 /* Consume the `export' token. */
8352 cp_lexer_consume_token (parser->lexer);
8353 /* Warn that we do not support `export'. */
8354 warning (0, "keyword %<export%> not implemented, and will be ignored");
8357 cp_parser_template_declaration_after_export (parser, member_p);
8360 /* Parse a template-parameter-list.
8362 template-parameter-list:
8364 template-parameter-list , template-parameter
8366 Returns a TREE_LIST. Each node represents a template parameter.
8367 The nodes are connected via their TREE_CHAINs. */
8370 cp_parser_template_parameter_list (cp_parser* parser)
8372 tree parameter_list = NULL_TREE;
8374 begin_template_parm_list ();
8381 /* Parse the template-parameter. */
8382 parameter = cp_parser_template_parameter (parser, &is_non_type);
8383 /* Add it to the list. */
8384 if (parameter != error_mark_node)
8385 parameter_list = process_template_parm (parameter_list,
8388 /* Peek at the next token. */
8389 token = cp_lexer_peek_token (parser->lexer);
8390 /* If it's not a `,', we're done. */
8391 if (token->type != CPP_COMMA)
8393 /* Otherwise, consume the `,' token. */
8394 cp_lexer_consume_token (parser->lexer);
8397 return end_template_parm_list (parameter_list);
8400 /* Parse a template-parameter.
8404 parameter-declaration
8406 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8407 the parameter. The TREE_PURPOSE is the default value, if any.
8408 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8409 iff this parameter is a non-type parameter. */
8412 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8415 cp_parameter_declarator *parameter_declarator;
8418 /* Assume it is a type parameter or a template parameter. */
8419 *is_non_type = false;
8420 /* Peek at the next token. */
8421 token = cp_lexer_peek_token (parser->lexer);
8422 /* If it is `class' or `template', we have a type-parameter. */
8423 if (token->keyword == RID_TEMPLATE)
8424 return cp_parser_type_parameter (parser);
8425 /* If it is `class' or `typename' we do not know yet whether it is a
8426 type parameter or a non-type parameter. Consider:
8428 template <typename T, typename T::X X> ...
8432 template <class C, class D*> ...
8434 Here, the first parameter is a type parameter, and the second is
8435 a non-type parameter. We can tell by looking at the token after
8436 the identifier -- if it is a `,', `=', or `>' then we have a type
8438 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8440 /* Peek at the token after `class' or `typename'. */
8441 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8442 /* If it's an identifier, skip it. */
8443 if (token->type == CPP_NAME)
8444 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8445 /* Now, see if the token looks like the end of a template
8447 if (token->type == CPP_COMMA
8448 || token->type == CPP_EQ
8449 || token->type == CPP_GREATER)
8450 return cp_parser_type_parameter (parser);
8453 /* Otherwise, it is a non-type parameter.
8457 When parsing a default template-argument for a non-type
8458 template-parameter, the first non-nested `>' is taken as the end
8459 of the template parameter-list rather than a greater-than
8461 *is_non_type = true;
8462 parameter_declarator
8463 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8464 /*parenthesized_p=*/NULL);
8465 parm = grokdeclarator (parameter_declarator->declarator,
8466 ¶meter_declarator->decl_specifiers,
8467 PARM, /*initialized=*/0,
8469 if (parm == error_mark_node)
8470 return error_mark_node;
8471 return build_tree_list (parameter_declarator->default_argument, parm);
8474 /* Parse a type-parameter.
8477 class identifier [opt]
8478 class identifier [opt] = type-id
8479 typename identifier [opt]
8480 typename identifier [opt] = type-id
8481 template < template-parameter-list > class identifier [opt]
8482 template < template-parameter-list > class identifier [opt]
8485 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8486 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8487 the declaration of the parameter. */
8490 cp_parser_type_parameter (cp_parser* parser)
8495 /* Look for a keyword to tell us what kind of parameter this is. */
8496 token = cp_parser_require (parser, CPP_KEYWORD,
8497 "`class', `typename', or `template'");
8499 return error_mark_node;
8501 switch (token->keyword)
8507 tree default_argument;
8509 /* If the next token is an identifier, then it names the
8511 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8512 identifier = cp_parser_identifier (parser);
8514 identifier = NULL_TREE;
8516 /* Create the parameter. */
8517 parameter = finish_template_type_parm (class_type_node, identifier);
8519 /* If the next token is an `=', we have a default argument. */
8520 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8522 /* Consume the `=' token. */
8523 cp_lexer_consume_token (parser->lexer);
8524 /* Parse the default-argument. */
8525 default_argument = cp_parser_type_id (parser);
8528 default_argument = NULL_TREE;
8530 /* Create the combined representation of the parameter and the
8531 default argument. */
8532 parameter = build_tree_list (default_argument, parameter);
8538 tree parameter_list;
8540 tree default_argument;
8542 /* Look for the `<'. */
8543 cp_parser_require (parser, CPP_LESS, "`<'");
8544 /* Parse the template-parameter-list. */
8545 parameter_list = cp_parser_template_parameter_list (parser);
8546 /* Look for the `>'. */
8547 cp_parser_require (parser, CPP_GREATER, "`>'");
8548 /* Look for the `class' keyword. */
8549 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8550 /* If the next token is an `=', then there is a
8551 default-argument. If the next token is a `>', we are at
8552 the end of the parameter-list. If the next token is a `,',
8553 then we are at the end of this parameter. */
8554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8555 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8556 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8558 identifier = cp_parser_identifier (parser);
8559 /* Treat invalid names as if the parameter were nameless. */
8560 if (identifier == error_mark_node)
8561 identifier = NULL_TREE;
8564 identifier = NULL_TREE;
8566 /* Create the template parameter. */
8567 parameter = finish_template_template_parm (class_type_node,
8570 /* If the next token is an `=', then there is a
8571 default-argument. */
8572 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8576 /* Consume the `='. */
8577 cp_lexer_consume_token (parser->lexer);
8578 /* Parse the id-expression. */
8580 = cp_parser_id_expression (parser,
8581 /*template_keyword_p=*/false,
8582 /*check_dependency_p=*/true,
8583 /*template_p=*/&is_template,
8584 /*declarator_p=*/false);
8585 if (TREE_CODE (default_argument) == TYPE_DECL)
8586 /* If the id-expression was a template-id that refers to
8587 a template-class, we already have the declaration here,
8588 so no further lookup is needed. */
8591 /* Look up the name. */
8593 = cp_parser_lookup_name (parser, default_argument,
8595 /*is_template=*/is_template,
8596 /*is_namespace=*/false,
8597 /*check_dependency=*/true,
8598 /*ambiguous_decls=*/NULL);
8599 /* See if the default argument is valid. */
8601 = check_template_template_default_arg (default_argument);
8604 default_argument = NULL_TREE;
8606 /* Create the combined representation of the parameter and the
8607 default argument. */
8608 parameter = build_tree_list (default_argument, parameter);
8620 /* Parse a template-id.
8623 template-name < template-argument-list [opt] >
8625 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8626 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8627 returned. Otherwise, if the template-name names a function, or set
8628 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8629 names a class, returns a TYPE_DECL for the specialization.
8631 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8632 uninstantiated templates. */
8635 cp_parser_template_id (cp_parser *parser,
8636 bool template_keyword_p,
8637 bool check_dependency_p,
8638 bool is_declaration)
8643 cp_token_position start_of_id = 0;
8644 tree access_check = NULL_TREE;
8645 cp_token *next_token, *next_token_2;
8648 /* If the next token corresponds to a template-id, there is no need
8650 next_token = cp_lexer_peek_token (parser->lexer);
8651 if (next_token->type == CPP_TEMPLATE_ID)
8656 /* Get the stored value. */
8657 value = cp_lexer_consume_token (parser->lexer)->value;
8658 /* Perform any access checks that were deferred. */
8659 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8660 perform_or_defer_access_check (TREE_PURPOSE (check),
8661 TREE_VALUE (check));
8662 /* Return the stored value. */
8663 return TREE_VALUE (value);
8666 /* Avoid performing name lookup if there is no possibility of
8667 finding a template-id. */
8668 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8669 || (next_token->type == CPP_NAME
8670 && !cp_parser_nth_token_starts_template_argument_list_p
8673 cp_parser_error (parser, "expected template-id");
8674 return error_mark_node;
8677 /* Remember where the template-id starts. */
8678 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8679 start_of_id = cp_lexer_token_position (parser->lexer, false);
8681 push_deferring_access_checks (dk_deferred);
8683 /* Parse the template-name. */
8684 is_identifier = false;
8685 template = cp_parser_template_name (parser, template_keyword_p,
8689 if (template == error_mark_node || is_identifier)
8691 pop_deferring_access_checks ();
8695 /* If we find the sequence `[:' after a template-name, it's probably
8696 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8697 parse correctly the argument list. */
8698 next_token = cp_lexer_peek_token (parser->lexer);
8699 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8700 if (next_token->type == CPP_OPEN_SQUARE
8701 && next_token->flags & DIGRAPH
8702 && next_token_2->type == CPP_COLON
8703 && !(next_token_2->flags & PREV_WHITE))
8705 cp_parser_parse_tentatively (parser);
8706 /* Change `:' into `::'. */
8707 next_token_2->type = CPP_SCOPE;
8708 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8710 cp_lexer_consume_token (parser->lexer);
8711 /* Parse the arguments. */
8712 arguments = cp_parser_enclosed_template_argument_list (parser);
8713 if (!cp_parser_parse_definitely (parser))
8715 /* If we couldn't parse an argument list, then we revert our changes
8716 and return simply an error. Maybe this is not a template-id
8718 next_token_2->type = CPP_COLON;
8719 cp_parser_error (parser, "expected %<<%>");
8720 pop_deferring_access_checks ();
8721 return error_mark_node;
8723 /* Otherwise, emit an error about the invalid digraph, but continue
8724 parsing because we got our argument list. */
8725 pedwarn ("%<<::%> cannot begin a template-argument list");
8726 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8727 "between %<<%> and %<::%>");
8728 if (!flag_permissive)
8733 inform ("(if you use -fpermissive G++ will accept your code)");
8740 /* Look for the `<' that starts the template-argument-list. */
8741 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8743 pop_deferring_access_checks ();
8744 return error_mark_node;
8746 /* Parse the arguments. */
8747 arguments = cp_parser_enclosed_template_argument_list (parser);
8750 /* Build a representation of the specialization. */
8751 if (TREE_CODE (template) == IDENTIFIER_NODE)
8752 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8753 else if (DECL_CLASS_TEMPLATE_P (template)
8754 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8756 = finish_template_type (template, arguments,
8757 cp_lexer_next_token_is (parser->lexer,
8761 /* If it's not a class-template or a template-template, it should be
8762 a function-template. */
8763 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8764 || TREE_CODE (template) == OVERLOAD
8765 || BASELINK_P (template)));
8767 template_id = lookup_template_function (template, arguments);
8770 /* Retrieve any deferred checks. Do not pop this access checks yet
8771 so the memory will not be reclaimed during token replacing below. */
8772 access_check = get_deferred_access_checks ();
8774 /* If parsing tentatively, replace the sequence of tokens that makes
8775 up the template-id with a CPP_TEMPLATE_ID token. That way,
8776 should we re-parse the token stream, we will not have to repeat
8777 the effort required to do the parse, nor will we issue duplicate
8778 error messages about problems during instantiation of the
8782 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8784 /* Reset the contents of the START_OF_ID token. */
8785 token->type = CPP_TEMPLATE_ID;
8786 token->value = build_tree_list (access_check, template_id);
8787 token->keyword = RID_MAX;
8789 /* Purge all subsequent tokens. */
8790 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8792 /* ??? Can we actually assume that, if template_id ==
8793 error_mark_node, we will have issued a diagnostic to the
8794 user, as opposed to simply marking the tentative parse as
8796 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8797 error ("parse error in template argument list");
8800 pop_deferring_access_checks ();
8804 /* Parse a template-name.
8809 The standard should actually say:
8813 operator-function-id
8815 A defect report has been filed about this issue.
8817 A conversion-function-id cannot be a template name because they cannot
8818 be part of a template-id. In fact, looking at this code:
8822 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8823 It is impossible to call a templated conversion-function-id with an
8824 explicit argument list, since the only allowed template parameter is
8825 the type to which it is converting.
8827 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8828 `template' keyword, in a construction like:
8832 In that case `f' is taken to be a template-name, even though there
8833 is no way of knowing for sure.
8835 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8836 name refers to a set of overloaded functions, at least one of which
8837 is a template, or an IDENTIFIER_NODE with the name of the template,
8838 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8839 names are looked up inside uninstantiated templates. */
8842 cp_parser_template_name (cp_parser* parser,
8843 bool template_keyword_p,
8844 bool check_dependency_p,
8845 bool is_declaration,
8846 bool *is_identifier)
8852 /* If the next token is `operator', then we have either an
8853 operator-function-id or a conversion-function-id. */
8854 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8856 /* We don't know whether we're looking at an
8857 operator-function-id or a conversion-function-id. */
8858 cp_parser_parse_tentatively (parser);
8859 /* Try an operator-function-id. */
8860 identifier = cp_parser_operator_function_id (parser);
8861 /* If that didn't work, try a conversion-function-id. */
8862 if (!cp_parser_parse_definitely (parser))
8864 cp_parser_error (parser, "expected template-name");
8865 return error_mark_node;
8868 /* Look for the identifier. */
8870 identifier = cp_parser_identifier (parser);
8872 /* If we didn't find an identifier, we don't have a template-id. */
8873 if (identifier == error_mark_node)
8874 return error_mark_node;
8876 /* If the name immediately followed the `template' keyword, then it
8877 is a template-name. However, if the next token is not `<', then
8878 we do not treat it as a template-name, since it is not being used
8879 as part of a template-id. This enables us to handle constructs
8882 template <typename T> struct S { S(); };
8883 template <typename T> S<T>::S();
8885 correctly. We would treat `S' as a template -- if it were `S<T>'
8886 -- but we do not if there is no `<'. */
8888 if (processing_template_decl
8889 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8891 /* In a declaration, in a dependent context, we pretend that the
8892 "template" keyword was present in order to improve error
8893 recovery. For example, given:
8895 template <typename T> void f(T::X<int>);
8897 we want to treat "X<int>" as a template-id. */
8899 && !template_keyword_p
8900 && parser->scope && TYPE_P (parser->scope)
8901 && check_dependency_p
8902 && dependent_type_p (parser->scope)
8903 /* Do not do this for dtors (or ctors), since they never
8904 need the template keyword before their name. */
8905 && !constructor_name_p (identifier, parser->scope))
8907 cp_token_position start = 0;
8909 /* Explain what went wrong. */
8910 error ("non-template %qD used as template", identifier);
8911 inform ("use %<%T::template %D%> to indicate that it is a template",
8912 parser->scope, identifier);
8913 /* If parsing tentatively, find the location of the "<" token. */
8914 if (cp_parser_simulate_error (parser))
8915 start = cp_lexer_token_position (parser->lexer, true);
8916 /* Parse the template arguments so that we can issue error
8917 messages about them. */
8918 cp_lexer_consume_token (parser->lexer);
8919 cp_parser_enclosed_template_argument_list (parser);
8920 /* Skip tokens until we find a good place from which to
8921 continue parsing. */
8922 cp_parser_skip_to_closing_parenthesis (parser,
8923 /*recovering=*/true,
8925 /*consume_paren=*/false);
8926 /* If parsing tentatively, permanently remove the
8927 template argument list. That will prevent duplicate
8928 error messages from being issued about the missing
8929 "template" keyword. */
8931 cp_lexer_purge_tokens_after (parser->lexer, start);
8933 *is_identifier = true;
8937 /* If the "template" keyword is present, then there is generally
8938 no point in doing name-lookup, so we just return IDENTIFIER.
8939 But, if the qualifying scope is non-dependent then we can
8940 (and must) do name-lookup normally. */
8941 if (template_keyword_p
8943 || (TYPE_P (parser->scope)
8944 && dependent_type_p (parser->scope))))
8948 /* Look up the name. */
8949 decl = cp_parser_lookup_name (parser, identifier,
8951 /*is_template=*/false,
8952 /*is_namespace=*/false,
8954 /*ambiguous_decls=*/NULL);
8955 decl = maybe_get_template_decl_from_type_decl (decl);
8957 /* If DECL is a template, then the name was a template-name. */
8958 if (TREE_CODE (decl) == TEMPLATE_DECL)
8962 tree fn = NULL_TREE;
8964 /* The standard does not explicitly indicate whether a name that
8965 names a set of overloaded declarations, some of which are
8966 templates, is a template-name. However, such a name should
8967 be a template-name; otherwise, there is no way to form a
8968 template-id for the overloaded templates. */
8969 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8970 if (TREE_CODE (fns) == OVERLOAD)
8971 for (fn = fns; fn; fn = OVL_NEXT (fn))
8972 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8977 /* The name does not name a template. */
8978 cp_parser_error (parser, "expected template-name");
8979 return error_mark_node;
8983 /* If DECL is dependent, and refers to a function, then just return
8984 its name; we will look it up again during template instantiation. */
8985 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8987 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8988 if (TYPE_P (scope) && dependent_type_p (scope))
8995 /* Parse a template-argument-list.
8997 template-argument-list:
8999 template-argument-list , template-argument
9001 Returns a TREE_VEC containing the arguments. */
9004 cp_parser_template_argument_list (cp_parser* parser)
9006 tree fixed_args[10];
9007 unsigned n_args = 0;
9008 unsigned alloced = 10;
9009 tree *arg_ary = fixed_args;
9011 bool saved_in_template_argument_list_p;
9013 bool saved_non_ice_p;
9015 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9016 parser->in_template_argument_list_p = true;
9017 /* Even if the template-id appears in an integral
9018 constant-expression, the contents of the argument list do
9020 saved_ice_p = parser->integral_constant_expression_p;
9021 parser->integral_constant_expression_p = false;
9022 saved_non_ice_p = parser->non_integral_constant_expression_p;
9023 parser->non_integral_constant_expression_p = false;
9024 /* Parse the arguments. */
9030 /* Consume the comma. */
9031 cp_lexer_consume_token (parser->lexer);
9033 /* Parse the template-argument. */
9034 argument = cp_parser_template_argument (parser);
9035 if (n_args == alloced)
9039 if (arg_ary == fixed_args)
9041 arg_ary = XNEWVEC (tree, alloced);
9042 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9045 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9047 arg_ary[n_args++] = argument;
9049 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9051 vec = make_tree_vec (n_args);
9054 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9056 if (arg_ary != fixed_args)
9058 parser->non_integral_constant_expression_p = saved_non_ice_p;
9059 parser->integral_constant_expression_p = saved_ice_p;
9060 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9064 /* Parse a template-argument.
9067 assignment-expression
9071 The representation is that of an assignment-expression, type-id, or
9072 id-expression -- except that the qualified id-expression is
9073 evaluated, so that the value returned is either a DECL or an
9076 Although the standard says "assignment-expression", it forbids
9077 throw-expressions or assignments in the template argument.
9078 Therefore, we use "conditional-expression" instead. */
9081 cp_parser_template_argument (cp_parser* parser)
9086 bool maybe_type_id = false;
9090 /* There's really no way to know what we're looking at, so we just
9091 try each alternative in order.
9095 In a template-argument, an ambiguity between a type-id and an
9096 expression is resolved to a type-id, regardless of the form of
9097 the corresponding template-parameter.
9099 Therefore, we try a type-id first. */
9100 cp_parser_parse_tentatively (parser);
9101 argument = cp_parser_type_id (parser);
9102 /* If there was no error parsing the type-id but the next token is a '>>',
9103 we probably found a typo for '> >'. But there are type-id which are
9104 also valid expressions. For instance:
9106 struct X { int operator >> (int); };
9107 template <int V> struct Foo {};
9110 Here 'X()' is a valid type-id of a function type, but the user just
9111 wanted to write the expression "X() >> 5". Thus, we remember that we
9112 found a valid type-id, but we still try to parse the argument as an
9113 expression to see what happens. */
9114 if (!cp_parser_error_occurred (parser)
9115 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9117 maybe_type_id = true;
9118 cp_parser_abort_tentative_parse (parser);
9122 /* If the next token isn't a `,' or a `>', then this argument wasn't
9123 really finished. This means that the argument is not a valid
9125 if (!cp_parser_next_token_ends_template_argument_p (parser))
9126 cp_parser_error (parser, "expected template-argument");
9127 /* If that worked, we're done. */
9128 if (cp_parser_parse_definitely (parser))
9131 /* We're still not sure what the argument will be. */
9132 cp_parser_parse_tentatively (parser);
9133 /* Try a template. */
9134 argument = cp_parser_id_expression (parser,
9135 /*template_keyword_p=*/false,
9136 /*check_dependency_p=*/true,
9138 /*declarator_p=*/false);
9139 /* If the next token isn't a `,' or a `>', then this argument wasn't
9141 if (!cp_parser_next_token_ends_template_argument_p (parser))
9142 cp_parser_error (parser, "expected template-argument");
9143 if (!cp_parser_error_occurred (parser))
9145 /* Figure out what is being referred to. If the id-expression
9146 was for a class template specialization, then we will have a
9147 TYPE_DECL at this point. There is no need to do name lookup
9148 at this point in that case. */
9149 if (TREE_CODE (argument) != TYPE_DECL)
9150 argument = cp_parser_lookup_name (parser, argument,
9152 /*is_template=*/template_p,
9153 /*is_namespace=*/false,
9154 /*check_dependency=*/true,
9155 /*ambiguous_decls=*/NULL);
9156 if (TREE_CODE (argument) != TEMPLATE_DECL
9157 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9158 cp_parser_error (parser, "expected template-name");
9160 if (cp_parser_parse_definitely (parser))
9162 /* It must be a non-type argument. There permitted cases are given
9163 in [temp.arg.nontype]:
9165 -- an integral constant-expression of integral or enumeration
9168 -- the name of a non-type template-parameter; or
9170 -- the name of an object or function with external linkage...
9172 -- the address of an object or function with external linkage...
9174 -- a pointer to member... */
9175 /* Look for a non-type template parameter. */
9176 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9178 cp_parser_parse_tentatively (parser);
9179 argument = cp_parser_primary_expression (parser,
9182 /*template_arg_p=*/true,
9184 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9185 || !cp_parser_next_token_ends_template_argument_p (parser))
9186 cp_parser_simulate_error (parser);
9187 if (cp_parser_parse_definitely (parser))
9191 /* If the next token is "&", the argument must be the address of an
9192 object or function with external linkage. */
9193 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9195 cp_lexer_consume_token (parser->lexer);
9196 /* See if we might have an id-expression. */
9197 token = cp_lexer_peek_token (parser->lexer);
9198 if (token->type == CPP_NAME
9199 || token->keyword == RID_OPERATOR
9200 || token->type == CPP_SCOPE
9201 || token->type == CPP_TEMPLATE_ID
9202 || token->type == CPP_NESTED_NAME_SPECIFIER)
9204 cp_parser_parse_tentatively (parser);
9205 argument = cp_parser_primary_expression (parser,
9208 /*template_arg_p=*/true,
9210 if (cp_parser_error_occurred (parser)
9211 || !cp_parser_next_token_ends_template_argument_p (parser))
9212 cp_parser_abort_tentative_parse (parser);
9215 if (TREE_CODE (argument) == INDIRECT_REF)
9217 gcc_assert (REFERENCE_REF_P (argument));
9218 argument = TREE_OPERAND (argument, 0);
9221 if (TREE_CODE (argument) == BASELINK)
9222 /* We don't need the information about what class was used
9223 to name the overloaded functions. */
9224 argument = BASELINK_FUNCTIONS (argument);
9226 if (TREE_CODE (argument) == VAR_DECL)
9228 /* A variable without external linkage might still be a
9229 valid constant-expression, so no error is issued here
9230 if the external-linkage check fails. */
9231 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9232 cp_parser_simulate_error (parser);
9234 else if (is_overloaded_fn (argument))
9235 /* All overloaded functions are allowed; if the external
9236 linkage test does not pass, an error will be issued
9240 && (TREE_CODE (argument) == OFFSET_REF
9241 || TREE_CODE (argument) == SCOPE_REF))
9242 /* A pointer-to-member. */
9244 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9247 cp_parser_simulate_error (parser);
9249 if (cp_parser_parse_definitely (parser))
9252 argument = build_x_unary_op (ADDR_EXPR, argument);
9257 /* If the argument started with "&", there are no other valid
9258 alternatives at this point. */
9261 cp_parser_error (parser, "invalid non-type template argument");
9262 return error_mark_node;
9265 /* If the argument wasn't successfully parsed as a type-id followed
9266 by '>>', the argument can only be a constant expression now.
9267 Otherwise, we try parsing the constant-expression tentatively,
9268 because the argument could really be a type-id. */
9270 cp_parser_parse_tentatively (parser);
9271 argument = cp_parser_constant_expression (parser,
9272 /*allow_non_constant_p=*/false,
9273 /*non_constant_p=*/NULL);
9274 argument = fold_non_dependent_expr (argument);
9277 if (!cp_parser_next_token_ends_template_argument_p (parser))
9278 cp_parser_error (parser, "expected template-argument");
9279 if (cp_parser_parse_definitely (parser))
9281 /* We did our best to parse the argument as a non type-id, but that
9282 was the only alternative that matched (albeit with a '>' after
9283 it). We can assume it's just a typo from the user, and a
9284 diagnostic will then be issued. */
9285 return cp_parser_type_id (parser);
9288 /* Parse an explicit-instantiation.
9290 explicit-instantiation:
9291 template declaration
9293 Although the standard says `declaration', what it really means is:
9295 explicit-instantiation:
9296 template decl-specifier-seq [opt] declarator [opt] ;
9298 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9299 supposed to be allowed. A defect report has been filed about this
9304 explicit-instantiation:
9305 storage-class-specifier template
9306 decl-specifier-seq [opt] declarator [opt] ;
9307 function-specifier template
9308 decl-specifier-seq [opt] declarator [opt] ; */
9311 cp_parser_explicit_instantiation (cp_parser* parser)
9313 int declares_class_or_enum;
9314 cp_decl_specifier_seq decl_specifiers;
9315 tree extension_specifier = NULL_TREE;
9317 /* Look for an (optional) storage-class-specifier or
9318 function-specifier. */
9319 if (cp_parser_allow_gnu_extensions_p (parser))
9322 = cp_parser_storage_class_specifier_opt (parser);
9323 if (!extension_specifier)
9325 = cp_parser_function_specifier_opt (parser,
9326 /*decl_specs=*/NULL);
9329 /* Look for the `template' keyword. */
9330 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9331 /* Let the front end know that we are processing an explicit
9333 begin_explicit_instantiation ();
9334 /* [temp.explicit] says that we are supposed to ignore access
9335 control while processing explicit instantiation directives. */
9336 push_deferring_access_checks (dk_no_check);
9337 /* Parse a decl-specifier-seq. */
9338 cp_parser_decl_specifier_seq (parser,
9339 CP_PARSER_FLAGS_OPTIONAL,
9341 &declares_class_or_enum);
9342 /* If there was exactly one decl-specifier, and it declared a class,
9343 and there's no declarator, then we have an explicit type
9345 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9349 type = check_tag_decl (&decl_specifiers);
9350 /* Turn access control back on for names used during
9351 template instantiation. */
9352 pop_deferring_access_checks ();
9354 do_type_instantiation (type, extension_specifier,
9355 /*complain=*/tf_error);
9359 cp_declarator *declarator;
9362 /* Parse the declarator. */
9364 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9365 /*ctor_dtor_or_conv_p=*/NULL,
9366 /*parenthesized_p=*/NULL,
9367 /*member_p=*/false);
9368 if (declares_class_or_enum & 2)
9369 cp_parser_check_for_definition_in_return_type (declarator,
9370 decl_specifiers.type);
9371 if (declarator != cp_error_declarator)
9373 decl = grokdeclarator (declarator, &decl_specifiers,
9375 /* Turn access control back on for names used during
9376 template instantiation. */
9377 pop_deferring_access_checks ();
9378 /* Do the explicit instantiation. */
9379 do_decl_instantiation (decl, extension_specifier);
9383 pop_deferring_access_checks ();
9384 /* Skip the body of the explicit instantiation. */
9385 cp_parser_skip_to_end_of_statement (parser);
9388 /* We're done with the instantiation. */
9389 end_explicit_instantiation ();
9391 cp_parser_consume_semicolon_at_end_of_statement (parser);
9394 /* Parse an explicit-specialization.
9396 explicit-specialization:
9397 template < > declaration
9399 Although the standard says `declaration', what it really means is:
9401 explicit-specialization:
9402 template <> decl-specifier [opt] init-declarator [opt] ;
9403 template <> function-definition
9404 template <> explicit-specialization
9405 template <> template-declaration */
9408 cp_parser_explicit_specialization (cp_parser* parser)
9411 /* Look for the `template' keyword. */
9412 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9413 /* Look for the `<'. */
9414 cp_parser_require (parser, CPP_LESS, "`<'");
9415 /* Look for the `>'. */
9416 cp_parser_require (parser, CPP_GREATER, "`>'");
9417 /* We have processed another parameter list. */
9418 ++parser->num_template_parameter_lists;
9421 A template ... explicit specialization ... shall not have C
9423 if (current_lang_name == lang_name_c)
9425 error ("template specialization with C linkage");
9426 /* Give it C++ linkage to avoid confusing other parts of the
9428 push_lang_context (lang_name_cplusplus);
9429 need_lang_pop = true;
9432 need_lang_pop = false;
9433 /* Let the front end know that we are beginning a specialization. */
9434 begin_specialization ();
9435 /* If the next keyword is `template', we need to figure out whether
9436 or not we're looking a template-declaration. */
9437 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9439 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9440 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9441 cp_parser_template_declaration_after_export (parser,
9442 /*member_p=*/false);
9444 cp_parser_explicit_specialization (parser);
9447 /* Parse the dependent declaration. */
9448 cp_parser_single_declaration (parser,
9451 /* We're done with the specialization. */
9452 end_specialization ();
9453 /* For the erroneous case of a template with C linkage, we pushed an
9454 implicit C++ linkage scope; exit that scope now. */
9456 pop_lang_context ();
9457 /* We're done with this parameter list. */
9458 --parser->num_template_parameter_lists;
9461 /* Parse a type-specifier.
9464 simple-type-specifier
9467 elaborated-type-specifier
9475 Returns a representation of the type-specifier. For a
9476 class-specifier, enum-specifier, or elaborated-type-specifier, a
9477 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9479 The parser flags FLAGS is used to control type-specifier parsing.
9481 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9482 in a decl-specifier-seq.
9484 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9485 class-specifier, enum-specifier, or elaborated-type-specifier, then
9486 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9487 if a type is declared; 2 if it is defined. Otherwise, it is set to
9490 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9491 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9495 cp_parser_type_specifier (cp_parser* parser,
9496 cp_parser_flags flags,
9497 cp_decl_specifier_seq *decl_specs,
9498 bool is_declaration,
9499 int* declares_class_or_enum,
9500 bool* is_cv_qualifier)
9502 tree type_spec = NULL_TREE;
9505 cp_decl_spec ds = ds_last;
9507 /* Assume this type-specifier does not declare a new type. */
9508 if (declares_class_or_enum)
9509 *declares_class_or_enum = 0;
9510 /* And that it does not specify a cv-qualifier. */
9511 if (is_cv_qualifier)
9512 *is_cv_qualifier = false;
9513 /* Peek at the next token. */
9514 token = cp_lexer_peek_token (parser->lexer);
9516 /* If we're looking at a keyword, we can use that to guide the
9517 production we choose. */
9518 keyword = token->keyword;
9522 /* 'enum' [identifier] '{' introduces an enum-specifier;
9523 'enum' <anything else> introduces an elaborated-type-specifier. */
9524 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9525 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9526 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9529 if (parser->num_template_parameter_lists)
9531 error ("template declaration of %qs", "enum");
9532 cp_parser_skip_to_end_of_block_or_statement (parser);
9533 type_spec = error_mark_node;
9536 type_spec = cp_parser_enum_specifier (parser);
9538 if (declares_class_or_enum)
9539 *declares_class_or_enum = 2;
9541 cp_parser_set_decl_spec_type (decl_specs,
9543 /*user_defined_p=*/true);
9547 goto elaborated_type_specifier;
9549 /* Any of these indicate either a class-specifier, or an
9550 elaborated-type-specifier. */
9554 /* Parse tentatively so that we can back up if we don't find a
9556 cp_parser_parse_tentatively (parser);
9557 /* Look for the class-specifier. */
9558 type_spec = cp_parser_class_specifier (parser);
9559 /* If that worked, we're done. */
9560 if (cp_parser_parse_definitely (parser))
9562 if (declares_class_or_enum)
9563 *declares_class_or_enum = 2;
9565 cp_parser_set_decl_spec_type (decl_specs,
9567 /*user_defined_p=*/true);
9572 elaborated_type_specifier:
9573 /* We're declaring (not defining) a class or enum. */
9574 if (declares_class_or_enum)
9575 *declares_class_or_enum = 1;
9579 /* Look for an elaborated-type-specifier. */
9581 = (cp_parser_elaborated_type_specifier
9583 decl_specs && decl_specs->specs[(int) ds_friend],
9586 cp_parser_set_decl_spec_type (decl_specs,
9588 /*user_defined_p=*/true);
9593 if (is_cv_qualifier)
9594 *is_cv_qualifier = true;
9599 if (is_cv_qualifier)
9600 *is_cv_qualifier = true;
9605 if (is_cv_qualifier)
9606 *is_cv_qualifier = true;
9610 /* The `__complex__' keyword is a GNU extension. */
9618 /* Handle simple keywords. */
9623 ++decl_specs->specs[(int)ds];
9624 decl_specs->any_specifiers_p = true;
9626 return cp_lexer_consume_token (parser->lexer)->value;
9629 /* If we do not already have a type-specifier, assume we are looking
9630 at a simple-type-specifier. */
9631 type_spec = cp_parser_simple_type_specifier (parser,
9635 /* If we didn't find a type-specifier, and a type-specifier was not
9636 optional in this context, issue an error message. */
9637 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9639 cp_parser_error (parser, "expected type specifier");
9640 return error_mark_node;
9646 /* Parse a simple-type-specifier.
9648 simple-type-specifier:
9649 :: [opt] nested-name-specifier [opt] type-name
9650 :: [opt] nested-name-specifier template template-id
9665 simple-type-specifier:
9666 __typeof__ unary-expression
9667 __typeof__ ( type-id )
9669 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9670 appropriately updated. */
9673 cp_parser_simple_type_specifier (cp_parser* parser,
9674 cp_decl_specifier_seq *decl_specs,
9675 cp_parser_flags flags)
9677 tree type = NULL_TREE;
9680 /* Peek at the next token. */
9681 token = cp_lexer_peek_token (parser->lexer);
9683 /* If we're looking at a keyword, things are easy. */
9684 switch (token->keyword)
9688 decl_specs->explicit_char_p = true;
9689 type = char_type_node;
9692 type = wchar_type_node;
9695 type = boolean_type_node;
9699 ++decl_specs->specs[(int) ds_short];
9700 type = short_integer_type_node;
9704 decl_specs->explicit_int_p = true;
9705 type = integer_type_node;
9709 ++decl_specs->specs[(int) ds_long];
9710 type = long_integer_type_node;
9714 ++decl_specs->specs[(int) ds_signed];
9715 type = integer_type_node;
9719 ++decl_specs->specs[(int) ds_unsigned];
9720 type = unsigned_type_node;
9723 type = float_type_node;
9726 type = double_type_node;
9729 type = void_type_node;
9733 /* Consume the `typeof' token. */
9734 cp_lexer_consume_token (parser->lexer);
9735 /* Parse the operand to `typeof'. */
9736 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9737 /* If it is not already a TYPE, take its type. */
9739 type = finish_typeof (type);
9742 cp_parser_set_decl_spec_type (decl_specs, type,
9743 /*user_defined_p=*/true);
9751 /* If the type-specifier was for a built-in type, we're done. */
9756 /* Record the type. */
9758 && (token->keyword != RID_SIGNED
9759 && token->keyword != RID_UNSIGNED
9760 && token->keyword != RID_SHORT
9761 && token->keyword != RID_LONG))
9762 cp_parser_set_decl_spec_type (decl_specs,
9764 /*user_defined=*/false);
9766 decl_specs->any_specifiers_p = true;
9768 /* Consume the token. */
9769 id = cp_lexer_consume_token (parser->lexer)->value;
9771 /* There is no valid C++ program where a non-template type is
9772 followed by a "<". That usually indicates that the user thought
9773 that the type was a template. */
9774 cp_parser_check_for_invalid_template_id (parser, type);
9776 return TYPE_NAME (type);
9779 /* The type-specifier must be a user-defined type. */
9780 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9785 /* Don't gobble tokens or issue error messages if this is an
9786 optional type-specifier. */
9787 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9788 cp_parser_parse_tentatively (parser);
9790 /* Look for the optional `::' operator. */
9792 = (cp_parser_global_scope_opt (parser,
9793 /*current_scope_valid_p=*/false)
9795 /* Look for the nested-name specifier. */
9797 = (cp_parser_nested_name_specifier_opt (parser,
9798 /*typename_keyword_p=*/false,
9799 /*check_dependency_p=*/true,
9801 /*is_declaration=*/false)
9803 /* If we have seen a nested-name-specifier, and the next token
9804 is `template', then we are using the template-id production. */
9806 && cp_parser_optional_template_keyword (parser))
9808 /* Look for the template-id. */
9809 type = cp_parser_template_id (parser,
9810 /*template_keyword_p=*/true,
9811 /*check_dependency_p=*/true,
9812 /*is_declaration=*/false);
9813 /* If the template-id did not name a type, we are out of
9815 if (TREE_CODE (type) != TYPE_DECL)
9817 cp_parser_error (parser, "expected template-id for type");
9821 /* Otherwise, look for a type-name. */
9823 type = cp_parser_type_name (parser);
9824 /* Keep track of all name-lookups performed in class scopes. */
9828 && TREE_CODE (type) == TYPE_DECL
9829 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9830 maybe_note_name_used_in_class (DECL_NAME (type), type);
9831 /* If it didn't work out, we don't have a TYPE. */
9832 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9833 && !cp_parser_parse_definitely (parser))
9835 if (type && decl_specs)
9836 cp_parser_set_decl_spec_type (decl_specs, type,
9837 /*user_defined=*/true);
9840 /* If we didn't get a type-name, issue an error message. */
9841 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9843 cp_parser_error (parser, "expected type-name");
9844 return error_mark_node;
9847 /* There is no valid C++ program where a non-template type is
9848 followed by a "<". That usually indicates that the user thought
9849 that the type was a template. */
9850 if (type && type != error_mark_node)
9852 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9853 If it is, then the '<'...'>' enclose protocol names rather than
9854 template arguments, and so everything is fine. */
9855 if (c_dialect_objc ()
9856 && (objc_is_id (type) || objc_is_class_name (type)))
9858 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9859 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9861 /* Clobber the "unqualified" type previously entered into
9862 DECL_SPECS with the new, improved protocol-qualified version. */
9864 decl_specs->type = qual_type;
9869 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9875 /* Parse a type-name.
9888 Returns a TYPE_DECL for the type. */
9891 cp_parser_type_name (cp_parser* parser)
9896 /* We can't know yet whether it is a class-name or not. */
9897 cp_parser_parse_tentatively (parser);
9898 /* Try a class-name. */
9899 type_decl = cp_parser_class_name (parser,
9900 /*typename_keyword_p=*/false,
9901 /*template_keyword_p=*/false,
9903 /*check_dependency_p=*/true,
9904 /*class_head_p=*/false,
9905 /*is_declaration=*/false);
9906 /* If it's not a class-name, keep looking. */
9907 if (!cp_parser_parse_definitely (parser))
9909 /* It must be a typedef-name or an enum-name. */
9910 identifier = cp_parser_identifier (parser);
9911 if (identifier == error_mark_node)
9912 return error_mark_node;
9914 /* Look up the type-name. */
9915 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9917 if (TREE_CODE (type_decl) != TYPE_DECL
9918 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9920 /* See if this is an Objective-C type. */
9921 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9922 tree type = objc_get_protocol_qualified_type (identifier, protos);
9924 type_decl = TYPE_NAME (type);
9927 /* Issue an error if we did not find a type-name. */
9928 if (TREE_CODE (type_decl) != TYPE_DECL)
9930 if (!cp_parser_simulate_error (parser))
9931 cp_parser_name_lookup_error (parser, identifier, type_decl,
9933 type_decl = error_mark_node;
9935 /* Remember that the name was used in the definition of the
9936 current class so that we can check later to see if the
9937 meaning would have been different after the class was
9938 entirely defined. */
9939 else if (type_decl != error_mark_node
9941 maybe_note_name_used_in_class (identifier, type_decl);
9948 /* Parse an elaborated-type-specifier. Note that the grammar given
9949 here incorporates the resolution to DR68.
9951 elaborated-type-specifier:
9952 class-key :: [opt] nested-name-specifier [opt] identifier
9953 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9954 enum :: [opt] nested-name-specifier [opt] identifier
9955 typename :: [opt] nested-name-specifier identifier
9956 typename :: [opt] nested-name-specifier template [opt]
9961 elaborated-type-specifier:
9962 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9963 class-key attributes :: [opt] nested-name-specifier [opt]
9964 template [opt] template-id
9965 enum attributes :: [opt] nested-name-specifier [opt] identifier
9967 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9968 declared `friend'. If IS_DECLARATION is TRUE, then this
9969 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9970 something is being declared.
9972 Returns the TYPE specified. */
9975 cp_parser_elaborated_type_specifier (cp_parser* parser,
9977 bool is_declaration)
9979 enum tag_types tag_type;
9981 tree type = NULL_TREE;
9982 tree attributes = NULL_TREE;
9984 /* See if we're looking at the `enum' keyword. */
9985 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9987 /* Consume the `enum' token. */
9988 cp_lexer_consume_token (parser->lexer);
9989 /* Remember that it's an enumeration type. */
9990 tag_type = enum_type;
9991 /* Parse the attributes. */
9992 attributes = cp_parser_attributes_opt (parser);
9994 /* Or, it might be `typename'. */
9995 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9998 /* Consume the `typename' token. */
9999 cp_lexer_consume_token (parser->lexer);
10000 /* Remember that it's a `typename' type. */
10001 tag_type = typename_type;
10002 /* The `typename' keyword is only allowed in templates. */
10003 if (!processing_template_decl)
10004 pedwarn ("using %<typename%> outside of template");
10006 /* Otherwise it must be a class-key. */
10009 tag_type = cp_parser_class_key (parser);
10010 if (tag_type == none_type)
10011 return error_mark_node;
10012 /* Parse the attributes. */
10013 attributes = cp_parser_attributes_opt (parser);
10016 /* Look for the `::' operator. */
10017 cp_parser_global_scope_opt (parser,
10018 /*current_scope_valid_p=*/false);
10019 /* Look for the nested-name-specifier. */
10020 if (tag_type == typename_type)
10022 if (!cp_parser_nested_name_specifier (parser,
10023 /*typename_keyword_p=*/true,
10024 /*check_dependency_p=*/true,
10027 return error_mark_node;
10030 /* Even though `typename' is not present, the proposed resolution
10031 to Core Issue 180 says that in `class A<T>::B', `B' should be
10032 considered a type-name, even if `A<T>' is dependent. */
10033 cp_parser_nested_name_specifier_opt (parser,
10034 /*typename_keyword_p=*/true,
10035 /*check_dependency_p=*/true,
10038 /* For everything but enumeration types, consider a template-id. */
10039 if (tag_type != enum_type)
10041 bool template_p = false;
10044 /* Allow the `template' keyword. */
10045 template_p = cp_parser_optional_template_keyword (parser);
10046 /* If we didn't see `template', we don't know if there's a
10047 template-id or not. */
10049 cp_parser_parse_tentatively (parser);
10050 /* Parse the template-id. */
10051 decl = cp_parser_template_id (parser, template_p,
10052 /*check_dependency_p=*/true,
10054 /* If we didn't find a template-id, look for an ordinary
10056 if (!template_p && !cp_parser_parse_definitely (parser))
10058 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10059 in effect, then we must assume that, upon instantiation, the
10060 template will correspond to a class. */
10061 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10062 && tag_type == typename_type)
10063 type = make_typename_type (parser->scope, decl,
10065 /*complain=*/tf_error);
10067 type = TREE_TYPE (decl);
10070 /* For an enumeration type, consider only a plain identifier. */
10073 identifier = cp_parser_identifier (parser);
10075 if (identifier == error_mark_node)
10077 parser->scope = NULL_TREE;
10078 return error_mark_node;
10081 /* For a `typename', we needn't call xref_tag. */
10082 if (tag_type == typename_type
10083 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10084 return cp_parser_make_typename_type (parser, parser->scope,
10086 /* Look up a qualified name in the usual way. */
10091 decl = cp_parser_lookup_name (parser, identifier,
10093 /*is_template=*/false,
10094 /*is_namespace=*/false,
10095 /*check_dependency=*/true,
10096 /*ambiguous_decls=*/NULL);
10098 /* If we are parsing friend declaration, DECL may be a
10099 TEMPLATE_DECL tree node here. However, we need to check
10100 whether this TEMPLATE_DECL results in valid code. Consider
10101 the following example:
10104 template <class T> class C {};
10107 template <class T> friend class N::C; // #1, valid code
10109 template <class T> class Y {
10110 friend class N::C; // #2, invalid code
10113 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10114 name lookup of `N::C'. We see that friend declaration must
10115 be template for the code to be valid. Note that
10116 processing_template_decl does not work here since it is
10117 always 1 for the above two cases. */
10119 decl = (cp_parser_maybe_treat_template_as_class
10120 (decl, /*tag_name_p=*/is_friend
10121 && parser->num_template_parameter_lists));
10123 if (TREE_CODE (decl) != TYPE_DECL)
10125 cp_parser_diagnose_invalid_type_name (parser,
10128 return error_mark_node;
10131 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10132 check_elaborated_type_specifier
10134 (parser->num_template_parameter_lists
10135 || DECL_SELF_REFERENCE_P (decl)));
10137 type = TREE_TYPE (decl);
10141 /* An elaborated-type-specifier sometimes introduces a new type and
10142 sometimes names an existing type. Normally, the rule is that it
10143 introduces a new type only if there is not an existing type of
10144 the same name already in scope. For example, given:
10147 void f() { struct S s; }
10149 the `struct S' in the body of `f' is the same `struct S' as in
10150 the global scope; the existing definition is used. However, if
10151 there were no global declaration, this would introduce a new
10152 local class named `S'.
10154 An exception to this rule applies to the following code:
10156 namespace N { struct S; }
10158 Here, the elaborated-type-specifier names a new type
10159 unconditionally; even if there is already an `S' in the
10160 containing scope this declaration names a new type.
10161 This exception only applies if the elaborated-type-specifier
10162 forms the complete declaration:
10166 A declaration consisting solely of `class-key identifier ;' is
10167 either a redeclaration of the name in the current scope or a
10168 forward declaration of the identifier as a class name. It
10169 introduces the name into the current scope.
10171 We are in this situation precisely when the next token is a `;'.
10173 An exception to the exception is that a `friend' declaration does
10174 *not* name a new type; i.e., given:
10176 struct S { friend struct T; };
10178 `T' is not a new type in the scope of `S'.
10180 Also, `new struct S' or `sizeof (struct S)' never results in the
10181 definition of a new type; a new type can only be declared in a
10182 declaration context. */
10188 /* Friends have special name lookup rules. */
10189 ts = ts_within_enclosing_non_class;
10190 else if (is_declaration
10191 && cp_lexer_next_token_is (parser->lexer,
10193 /* This is a `class-key identifier ;' */
10198 /* Warn about attributes. They are ignored. */
10200 warning (OPT_Wattributes,
10201 "type attributes are honored only at type definition");
10204 (parser->num_template_parameter_lists
10205 && (cp_parser_next_token_starts_class_definition_p (parser)
10206 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10207 /* An unqualified name was used to reference this type, so
10208 there were no qualifying templates. */
10209 if (!cp_parser_check_template_parameters (parser,
10210 /*num_templates=*/0))
10211 return error_mark_node;
10212 type = xref_tag (tag_type, identifier, ts, template_p);
10215 if (tag_type != enum_type)
10216 cp_parser_check_class_key (tag_type, type);
10218 /* A "<" cannot follow an elaborated type specifier. If that
10219 happens, the user was probably trying to form a template-id. */
10220 cp_parser_check_for_invalid_template_id (parser, type);
10225 /* Parse an enum-specifier.
10228 enum identifier [opt] { enumerator-list [opt] }
10231 enum identifier [opt] { enumerator-list [opt] } attributes
10233 Returns an ENUM_TYPE representing the enumeration. */
10236 cp_parser_enum_specifier (cp_parser* parser)
10241 /* Caller guarantees that the current token is 'enum', an identifier
10242 possibly follows, and the token after that is an opening brace.
10243 If we don't have an identifier, fabricate an anonymous name for
10244 the enumeration being defined. */
10245 cp_lexer_consume_token (parser->lexer);
10247 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10248 identifier = cp_parser_identifier (parser);
10250 identifier = make_anon_name ();
10252 /* Issue an error message if type-definitions are forbidden here. */
10253 cp_parser_check_type_definition (parser);
10255 /* Create the new type. We do this before consuming the opening brace
10256 so the enum will be recorded as being on the line of its tag (or the
10257 'enum' keyword, if there is no tag). */
10258 type = start_enum (identifier);
10260 /* Consume the opening brace. */
10261 cp_lexer_consume_token (parser->lexer);
10263 /* If the next token is not '}', then there are some enumerators. */
10264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10265 cp_parser_enumerator_list (parser, type);
10267 /* Consume the final '}'. */
10268 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10270 /* Look for trailing attributes to apply to this enumeration, and
10271 apply them if appropriate. */
10272 if (cp_parser_allow_gnu_extensions_p (parser))
10274 tree trailing_attr = cp_parser_attributes_opt (parser);
10275 cplus_decl_attributes (&type,
10277 (int) ATTR_FLAG_TYPE_IN_PLACE);
10280 /* Finish up the enumeration. */
10281 finish_enum (type);
10286 /* Parse an enumerator-list. The enumerators all have the indicated
10290 enumerator-definition
10291 enumerator-list , enumerator-definition */
10294 cp_parser_enumerator_list (cp_parser* parser, tree type)
10298 /* Parse an enumerator-definition. */
10299 cp_parser_enumerator_definition (parser, type);
10301 /* If the next token is not a ',', we've reached the end of
10303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10305 /* Otherwise, consume the `,' and keep going. */
10306 cp_lexer_consume_token (parser->lexer);
10307 /* If the next token is a `}', there is a trailing comma. */
10308 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10310 if (pedantic && !in_system_header)
10311 pedwarn ("comma at end of enumerator list");
10317 /* Parse an enumerator-definition. The enumerator has the indicated
10320 enumerator-definition:
10322 enumerator = constant-expression
10328 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10333 /* Look for the identifier. */
10334 identifier = cp_parser_identifier (parser);
10335 if (identifier == error_mark_node)
10338 /* If the next token is an '=', then there is an explicit value. */
10339 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10341 /* Consume the `=' token. */
10342 cp_lexer_consume_token (parser->lexer);
10343 /* Parse the value. */
10344 value = cp_parser_constant_expression (parser,
10345 /*allow_non_constant_p=*/false,
10351 /* Create the enumerator. */
10352 build_enumerator (identifier, value, type);
10355 /* Parse a namespace-name.
10358 original-namespace-name
10361 Returns the NAMESPACE_DECL for the namespace. */
10364 cp_parser_namespace_name (cp_parser* parser)
10367 tree namespace_decl;
10369 /* Get the name of the namespace. */
10370 identifier = cp_parser_identifier (parser);
10371 if (identifier == error_mark_node)
10372 return error_mark_node;
10374 /* Look up the identifier in the currently active scope. Look only
10375 for namespaces, due to:
10377 [basic.lookup.udir]
10379 When looking up a namespace-name in a using-directive or alias
10380 definition, only namespace names are considered.
10384 [basic.lookup.qual]
10386 During the lookup of a name preceding the :: scope resolution
10387 operator, object, function, and enumerator names are ignored.
10389 (Note that cp_parser_class_or_namespace_name only calls this
10390 function if the token after the name is the scope resolution
10392 namespace_decl = cp_parser_lookup_name (parser, identifier,
10394 /*is_template=*/false,
10395 /*is_namespace=*/true,
10396 /*check_dependency=*/true,
10397 /*ambiguous_decls=*/NULL);
10398 /* If it's not a namespace, issue an error. */
10399 if (namespace_decl == error_mark_node
10400 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10402 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10403 error ("%qD is not a namespace-name", identifier);
10404 cp_parser_error (parser, "expected namespace-name");
10405 namespace_decl = error_mark_node;
10408 return namespace_decl;
10411 /* Parse a namespace-definition.
10413 namespace-definition:
10414 named-namespace-definition
10415 unnamed-namespace-definition
10417 named-namespace-definition:
10418 original-namespace-definition
10419 extension-namespace-definition
10421 original-namespace-definition:
10422 namespace identifier { namespace-body }
10424 extension-namespace-definition:
10425 namespace original-namespace-name { namespace-body }
10427 unnamed-namespace-definition:
10428 namespace { namespace-body } */
10431 cp_parser_namespace_definition (cp_parser* parser)
10435 /* Look for the `namespace' keyword. */
10436 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10438 /* Get the name of the namespace. We do not attempt to distinguish
10439 between an original-namespace-definition and an
10440 extension-namespace-definition at this point. The semantic
10441 analysis routines are responsible for that. */
10442 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10443 identifier = cp_parser_identifier (parser);
10445 identifier = NULL_TREE;
10447 /* Look for the `{' to start the namespace. */
10448 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10449 /* Start the namespace. */
10450 push_namespace (identifier);
10451 /* Parse the body of the namespace. */
10452 cp_parser_namespace_body (parser);
10453 /* Finish the namespace. */
10455 /* Look for the final `}'. */
10456 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10459 /* Parse a namespace-body.
10462 declaration-seq [opt] */
10465 cp_parser_namespace_body (cp_parser* parser)
10467 cp_parser_declaration_seq_opt (parser);
10470 /* Parse a namespace-alias-definition.
10472 namespace-alias-definition:
10473 namespace identifier = qualified-namespace-specifier ; */
10476 cp_parser_namespace_alias_definition (cp_parser* parser)
10479 tree namespace_specifier;
10481 /* Look for the `namespace' keyword. */
10482 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10483 /* Look for the identifier. */
10484 identifier = cp_parser_identifier (parser);
10485 if (identifier == error_mark_node)
10487 /* Look for the `=' token. */
10488 cp_parser_require (parser, CPP_EQ, "`='");
10489 /* Look for the qualified-namespace-specifier. */
10490 namespace_specifier
10491 = cp_parser_qualified_namespace_specifier (parser);
10492 /* Look for the `;' token. */
10493 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10495 /* Register the alias in the symbol table. */
10496 do_namespace_alias (identifier, namespace_specifier);
10499 /* Parse a qualified-namespace-specifier.
10501 qualified-namespace-specifier:
10502 :: [opt] nested-name-specifier [opt] namespace-name
10504 Returns a NAMESPACE_DECL corresponding to the specified
10508 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10510 /* Look for the optional `::'. */
10511 cp_parser_global_scope_opt (parser,
10512 /*current_scope_valid_p=*/false);
10514 /* Look for the optional nested-name-specifier. */
10515 cp_parser_nested_name_specifier_opt (parser,
10516 /*typename_keyword_p=*/false,
10517 /*check_dependency_p=*/true,
10519 /*is_declaration=*/true);
10521 return cp_parser_namespace_name (parser);
10524 /* Parse a using-declaration.
10527 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10528 using :: unqualified-id ; */
10531 cp_parser_using_declaration (cp_parser* parser)
10534 bool typename_p = false;
10535 bool global_scope_p;
10540 /* Look for the `using' keyword. */
10541 cp_parser_require_keyword (parser, RID_USING, "`using'");
10543 /* Peek at the next token. */
10544 token = cp_lexer_peek_token (parser->lexer);
10545 /* See if it's `typename'. */
10546 if (token->keyword == RID_TYPENAME)
10548 /* Remember that we've seen it. */
10550 /* Consume the `typename' token. */
10551 cp_lexer_consume_token (parser->lexer);
10554 /* Look for the optional global scope qualification. */
10556 = (cp_parser_global_scope_opt (parser,
10557 /*current_scope_valid_p=*/false)
10560 /* If we saw `typename', or didn't see `::', then there must be a
10561 nested-name-specifier present. */
10562 if (typename_p || !global_scope_p)
10563 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10564 /*check_dependency_p=*/true,
10566 /*is_declaration=*/true);
10567 /* Otherwise, we could be in either of the two productions. In that
10568 case, treat the nested-name-specifier as optional. */
10570 qscope = cp_parser_nested_name_specifier_opt (parser,
10571 /*typename_keyword_p=*/false,
10572 /*check_dependency_p=*/true,
10574 /*is_declaration=*/true);
10576 qscope = global_namespace;
10578 /* Parse the unqualified-id. */
10579 identifier = cp_parser_unqualified_id (parser,
10580 /*template_keyword_p=*/false,
10581 /*check_dependency_p=*/true,
10582 /*declarator_p=*/true);
10584 /* The function we call to handle a using-declaration is different
10585 depending on what scope we are in. */
10586 if (qscope == error_mark_node || identifier == error_mark_node)
10588 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10589 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10590 /* [namespace.udecl]
10592 A using declaration shall not name a template-id. */
10593 error ("a template-id may not appear in a using-declaration");
10596 if (at_class_scope_p ())
10598 /* Create the USING_DECL. */
10599 decl = do_class_using_decl (parser->scope, identifier);
10600 /* Add it to the list of members in this class. */
10601 finish_member_declaration (decl);
10605 decl = cp_parser_lookup_name_simple (parser, identifier);
10606 if (decl == error_mark_node)
10607 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10608 else if (!at_namespace_scope_p ())
10609 do_local_using_decl (decl, qscope, identifier);
10611 do_toplevel_using_decl (decl, qscope, identifier);
10615 /* Look for the final `;'. */
10616 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10619 /* Parse a using-directive.
10622 using namespace :: [opt] nested-name-specifier [opt]
10623 namespace-name ; */
10626 cp_parser_using_directive (cp_parser* parser)
10628 tree namespace_decl;
10631 /* Look for the `using' keyword. */
10632 cp_parser_require_keyword (parser, RID_USING, "`using'");
10633 /* And the `namespace' keyword. */
10634 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10635 /* Look for the optional `::' operator. */
10636 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10637 /* And the optional nested-name-specifier. */
10638 cp_parser_nested_name_specifier_opt (parser,
10639 /*typename_keyword_p=*/false,
10640 /*check_dependency_p=*/true,
10642 /*is_declaration=*/true);
10643 /* Get the namespace being used. */
10644 namespace_decl = cp_parser_namespace_name (parser);
10645 /* And any specified attributes. */
10646 attribs = cp_parser_attributes_opt (parser);
10647 /* Update the symbol table. */
10648 parse_using_directive (namespace_decl, attribs);
10649 /* Look for the final `;'. */
10650 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10653 /* Parse an asm-definition.
10656 asm ( string-literal ) ;
10661 asm volatile [opt] ( string-literal ) ;
10662 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10663 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10664 : asm-operand-list [opt] ) ;
10665 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10666 : asm-operand-list [opt]
10667 : asm-operand-list [opt] ) ; */
10670 cp_parser_asm_definition (cp_parser* parser)
10673 tree outputs = NULL_TREE;
10674 tree inputs = NULL_TREE;
10675 tree clobbers = NULL_TREE;
10677 bool volatile_p = false;
10678 bool extended_p = false;
10680 /* Look for the `asm' keyword. */
10681 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10682 /* See if the next token is `volatile'. */
10683 if (cp_parser_allow_gnu_extensions_p (parser)
10684 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10686 /* Remember that we saw the `volatile' keyword. */
10688 /* Consume the token. */
10689 cp_lexer_consume_token (parser->lexer);
10691 /* Look for the opening `('. */
10692 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10694 /* Look for the string. */
10695 string = cp_parser_string_literal (parser, false, false);
10696 if (string == error_mark_node)
10698 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10699 /*consume_paren=*/true);
10703 /* If we're allowing GNU extensions, check for the extended assembly
10704 syntax. Unfortunately, the `:' tokens need not be separated by
10705 a space in C, and so, for compatibility, we tolerate that here
10706 too. Doing that means that we have to treat the `::' operator as
10708 if (cp_parser_allow_gnu_extensions_p (parser)
10709 && at_function_scope_p ()
10710 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10711 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10713 bool inputs_p = false;
10714 bool clobbers_p = false;
10716 /* The extended syntax was used. */
10719 /* Look for outputs. */
10720 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10722 /* Consume the `:'. */
10723 cp_lexer_consume_token (parser->lexer);
10724 /* Parse the output-operands. */
10725 if (cp_lexer_next_token_is_not (parser->lexer,
10727 && cp_lexer_next_token_is_not (parser->lexer,
10729 && cp_lexer_next_token_is_not (parser->lexer,
10731 outputs = cp_parser_asm_operand_list (parser);
10733 /* If the next token is `::', there are no outputs, and the
10734 next token is the beginning of the inputs. */
10735 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10736 /* The inputs are coming next. */
10739 /* Look for inputs. */
10741 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10743 /* Consume the `:' or `::'. */
10744 cp_lexer_consume_token (parser->lexer);
10745 /* Parse the output-operands. */
10746 if (cp_lexer_next_token_is_not (parser->lexer,
10748 && cp_lexer_next_token_is_not (parser->lexer,
10750 inputs = cp_parser_asm_operand_list (parser);
10752 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10753 /* The clobbers are coming next. */
10756 /* Look for clobbers. */
10758 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10760 /* Consume the `:' or `::'. */
10761 cp_lexer_consume_token (parser->lexer);
10762 /* Parse the clobbers. */
10763 if (cp_lexer_next_token_is_not (parser->lexer,
10765 clobbers = cp_parser_asm_clobber_list (parser);
10768 /* Look for the closing `)'. */
10769 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10770 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10771 /*consume_paren=*/true);
10772 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10774 /* Create the ASM_EXPR. */
10775 if (at_function_scope_p ())
10777 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10779 /* If the extended syntax was not used, mark the ASM_EXPR. */
10782 tree temp = asm_stmt;
10783 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10784 temp = TREE_OPERAND (temp, 0);
10786 ASM_INPUT_P (temp) = 1;
10790 cgraph_add_asm_node (string);
10793 /* Declarators [gram.dcl.decl] */
10795 /* Parse an init-declarator.
10798 declarator initializer [opt]
10803 declarator asm-specification [opt] attributes [opt] initializer [opt]
10805 function-definition:
10806 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10808 decl-specifier-seq [opt] declarator function-try-block
10812 function-definition:
10813 __extension__ function-definition
10815 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10816 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10817 then this declarator appears in a class scope. The new DECL created
10818 by this declarator is returned.
10820 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10821 for a function-definition here as well. If the declarator is a
10822 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10823 be TRUE upon return. By that point, the function-definition will
10824 have been completely parsed.
10826 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10830 cp_parser_init_declarator (cp_parser* parser,
10831 cp_decl_specifier_seq *decl_specifiers,
10832 bool function_definition_allowed_p,
10834 int declares_class_or_enum,
10835 bool* function_definition_p)
10838 cp_declarator *declarator;
10839 tree prefix_attributes;
10841 tree asm_specification;
10843 tree decl = NULL_TREE;
10845 bool is_initialized;
10846 bool is_parenthesized_init;
10847 bool is_non_constant_init;
10848 int ctor_dtor_or_conv_p;
10850 tree pushed_scope = NULL;
10852 /* Gather the attributes that were provided with the
10853 decl-specifiers. */
10854 prefix_attributes = decl_specifiers->attributes;
10856 /* Assume that this is not the declarator for a function
10858 if (function_definition_p)
10859 *function_definition_p = false;
10861 /* Defer access checks while parsing the declarator; we cannot know
10862 what names are accessible until we know what is being
10864 resume_deferring_access_checks ();
10866 /* Parse the declarator. */
10868 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10869 &ctor_dtor_or_conv_p,
10870 /*parenthesized_p=*/NULL,
10871 /*member_p=*/false);
10872 /* Gather up the deferred checks. */
10873 stop_deferring_access_checks ();
10875 /* If the DECLARATOR was erroneous, there's no need to go
10877 if (declarator == cp_error_declarator)
10878 return error_mark_node;
10880 if (declares_class_or_enum & 2)
10881 cp_parser_check_for_definition_in_return_type (declarator,
10882 decl_specifiers->type);
10884 /* Figure out what scope the entity declared by the DECLARATOR is
10885 located in. `grokdeclarator' sometimes changes the scope, so
10886 we compute it now. */
10887 scope = get_scope_of_declarator (declarator);
10889 /* If we're allowing GNU extensions, look for an asm-specification
10891 if (cp_parser_allow_gnu_extensions_p (parser))
10893 /* Look for an asm-specification. */
10894 asm_specification = cp_parser_asm_specification_opt (parser);
10895 /* And attributes. */
10896 attributes = cp_parser_attributes_opt (parser);
10900 asm_specification = NULL_TREE;
10901 attributes = NULL_TREE;
10904 /* Peek at the next token. */
10905 token = cp_lexer_peek_token (parser->lexer);
10906 /* Check to see if the token indicates the start of a
10907 function-definition. */
10908 if (cp_parser_token_starts_function_definition_p (token))
10910 if (!function_definition_allowed_p)
10912 /* If a function-definition should not appear here, issue an
10914 cp_parser_error (parser,
10915 "a function-definition is not allowed here");
10916 return error_mark_node;
10920 /* Neither attributes nor an asm-specification are allowed
10921 on a function-definition. */
10922 if (asm_specification)
10923 error ("an asm-specification is not allowed on a function-definition");
10925 error ("attributes are not allowed on a function-definition");
10926 /* This is a function-definition. */
10927 *function_definition_p = true;
10929 /* Parse the function definition. */
10931 decl = cp_parser_save_member_function_body (parser,
10934 prefix_attributes);
10937 = (cp_parser_function_definition_from_specifiers_and_declarator
10938 (parser, decl_specifiers, prefix_attributes, declarator));
10946 Only in function declarations for constructors, destructors, and
10947 type conversions can the decl-specifier-seq be omitted.
10949 We explicitly postpone this check past the point where we handle
10950 function-definitions because we tolerate function-definitions
10951 that are missing their return types in some modes. */
10952 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10954 cp_parser_error (parser,
10955 "expected constructor, destructor, or type conversion");
10956 return error_mark_node;
10959 /* An `=' or an `(' indicates an initializer. */
10960 is_initialized = (token->type == CPP_EQ
10961 || token->type == CPP_OPEN_PAREN);
10962 /* If the init-declarator isn't initialized and isn't followed by a
10963 `,' or `;', it's not a valid init-declarator. */
10964 if (!is_initialized
10965 && token->type != CPP_COMMA
10966 && token->type != CPP_SEMICOLON)
10968 cp_parser_error (parser, "expected initializer");
10969 return error_mark_node;
10972 /* Because start_decl has side-effects, we should only call it if we
10973 know we're going ahead. By this point, we know that we cannot
10974 possibly be looking at any other construct. */
10975 cp_parser_commit_to_tentative_parse (parser);
10977 /* If the decl specifiers were bad, issue an error now that we're
10978 sure this was intended to be a declarator. Then continue
10979 declaring the variable(s), as int, to try to cut down on further
10981 if (decl_specifiers->any_specifiers_p
10982 && decl_specifiers->type == error_mark_node)
10984 cp_parser_error (parser, "invalid type in declaration");
10985 decl_specifiers->type = integer_type_node;
10988 /* Check to see whether or not this declaration is a friend. */
10989 friend_p = cp_parser_friend_p (decl_specifiers);
10991 /* Check that the number of template-parameter-lists is OK. */
10992 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10993 return error_mark_node;
10995 /* Enter the newly declared entry in the symbol table. If we're
10996 processing a declaration in a class-specifier, we wait until
10997 after processing the initializer. */
11000 if (parser->in_unbraced_linkage_specification_p)
11002 decl_specifiers->storage_class = sc_extern;
11003 have_extern_spec = false;
11005 decl = start_decl (declarator, decl_specifiers,
11006 is_initialized, attributes, prefix_attributes,
11010 /* Enter the SCOPE. That way unqualified names appearing in the
11011 initializer will be looked up in SCOPE. */
11012 pushed_scope = push_scope (scope);
11014 /* Perform deferred access control checks, now that we know in which
11015 SCOPE the declared entity resides. */
11016 if (!member_p && decl)
11018 tree saved_current_function_decl = NULL_TREE;
11020 /* If the entity being declared is a function, pretend that we
11021 are in its scope. If it is a `friend', it may have access to
11022 things that would not otherwise be accessible. */
11023 if (TREE_CODE (decl) == FUNCTION_DECL)
11025 saved_current_function_decl = current_function_decl;
11026 current_function_decl = decl;
11029 /* Perform the access control checks for the declarator and the
11030 the decl-specifiers. */
11031 perform_deferred_access_checks ();
11033 /* Restore the saved value. */
11034 if (TREE_CODE (decl) == FUNCTION_DECL)
11035 current_function_decl = saved_current_function_decl;
11038 /* Parse the initializer. */
11039 if (is_initialized)
11040 initializer = cp_parser_initializer (parser,
11041 &is_parenthesized_init,
11042 &is_non_constant_init);
11045 initializer = NULL_TREE;
11046 is_parenthesized_init = false;
11047 is_non_constant_init = true;
11050 /* The old parser allows attributes to appear after a parenthesized
11051 initializer. Mark Mitchell proposed removing this functionality
11052 on the GCC mailing lists on 2002-08-13. This parser accepts the
11053 attributes -- but ignores them. */
11054 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11055 if (cp_parser_attributes_opt (parser))
11056 warning (OPT_Wattributes,
11057 "attributes after parenthesized initializer ignored");
11059 /* For an in-class declaration, use `grokfield' to create the
11065 pop_scope (pushed_scope);
11066 pushed_scope = false;
11068 decl = grokfield (declarator, decl_specifiers,
11069 initializer, !is_non_constant_init,
11070 /*asmspec=*/NULL_TREE,
11071 prefix_attributes);
11072 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11073 cp_parser_save_default_args (parser, decl);
11076 /* Finish processing the declaration. But, skip friend
11078 if (!friend_p && decl && decl != error_mark_node)
11080 cp_finish_decl (decl,
11081 initializer, !is_non_constant_init,
11083 /* If the initializer is in parentheses, then this is
11084 a direct-initialization, which means that an
11085 `explicit' constructor is OK. Otherwise, an
11086 `explicit' constructor cannot be used. */
11087 ((is_parenthesized_init || !is_initialized)
11088 ? 0 : LOOKUP_ONLYCONVERTING));
11090 if (!friend_p && pushed_scope)
11091 pop_scope (pushed_scope);
11096 /* Parse a declarator.
11100 ptr-operator declarator
11102 abstract-declarator:
11103 ptr-operator abstract-declarator [opt]
11104 direct-abstract-declarator
11109 attributes [opt] direct-declarator
11110 attributes [opt] ptr-operator declarator
11112 abstract-declarator:
11113 attributes [opt] ptr-operator abstract-declarator [opt]
11114 attributes [opt] direct-abstract-declarator
11116 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11117 detect constructor, destructor or conversion operators. It is set
11118 to -1 if the declarator is a name, and +1 if it is a
11119 function. Otherwise it is set to zero. Usually you just want to
11120 test for >0, but internally the negative value is used.
11122 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11123 a decl-specifier-seq unless it declares a constructor, destructor,
11124 or conversion. It might seem that we could check this condition in
11125 semantic analysis, rather than parsing, but that makes it difficult
11126 to handle something like `f()'. We want to notice that there are
11127 no decl-specifiers, and therefore realize that this is an
11128 expression, not a declaration.)
11130 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11131 the declarator is a direct-declarator of the form "(...)".
11133 MEMBER_P is true iff this declarator is a member-declarator. */
11135 static cp_declarator *
11136 cp_parser_declarator (cp_parser* parser,
11137 cp_parser_declarator_kind dcl_kind,
11138 int* ctor_dtor_or_conv_p,
11139 bool* parenthesized_p,
11143 cp_declarator *declarator;
11144 enum tree_code code;
11145 cp_cv_quals cv_quals;
11147 tree attributes = NULL_TREE;
11149 /* Assume this is not a constructor, destructor, or type-conversion
11151 if (ctor_dtor_or_conv_p)
11152 *ctor_dtor_or_conv_p = 0;
11154 if (cp_parser_allow_gnu_extensions_p (parser))
11155 attributes = cp_parser_attributes_opt (parser);
11157 /* Peek at the next token. */
11158 token = cp_lexer_peek_token (parser->lexer);
11160 /* Check for the ptr-operator production. */
11161 cp_parser_parse_tentatively (parser);
11162 /* Parse the ptr-operator. */
11163 code = cp_parser_ptr_operator (parser,
11166 /* If that worked, then we have a ptr-operator. */
11167 if (cp_parser_parse_definitely (parser))
11169 /* If a ptr-operator was found, then this declarator was not
11171 if (parenthesized_p)
11172 *parenthesized_p = true;
11173 /* The dependent declarator is optional if we are parsing an
11174 abstract-declarator. */
11175 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11176 cp_parser_parse_tentatively (parser);
11178 /* Parse the dependent declarator. */
11179 declarator = cp_parser_declarator (parser, dcl_kind,
11180 /*ctor_dtor_or_conv_p=*/NULL,
11181 /*parenthesized_p=*/NULL,
11182 /*member_p=*/false);
11184 /* If we are parsing an abstract-declarator, we must handle the
11185 case where the dependent declarator is absent. */
11186 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11187 && !cp_parser_parse_definitely (parser))
11190 /* Build the representation of the ptr-operator. */
11192 declarator = make_ptrmem_declarator (cv_quals,
11195 else if (code == INDIRECT_REF)
11196 declarator = make_pointer_declarator (cv_quals, declarator);
11198 declarator = make_reference_declarator (cv_quals, declarator);
11200 /* Everything else is a direct-declarator. */
11203 if (parenthesized_p)
11204 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11206 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11207 ctor_dtor_or_conv_p,
11211 if (attributes && declarator != cp_error_declarator)
11212 declarator->attributes = attributes;
11217 /* Parse a direct-declarator or direct-abstract-declarator.
11221 direct-declarator ( parameter-declaration-clause )
11222 cv-qualifier-seq [opt]
11223 exception-specification [opt]
11224 direct-declarator [ constant-expression [opt] ]
11227 direct-abstract-declarator:
11228 direct-abstract-declarator [opt]
11229 ( parameter-declaration-clause )
11230 cv-qualifier-seq [opt]
11231 exception-specification [opt]
11232 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11233 ( abstract-declarator )
11235 Returns a representation of the declarator. DCL_KIND is
11236 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11237 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11238 we are parsing a direct-declarator. It is
11239 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11240 of ambiguity we prefer an abstract declarator, as per
11241 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11242 cp_parser_declarator. */
11244 static cp_declarator *
11245 cp_parser_direct_declarator (cp_parser* parser,
11246 cp_parser_declarator_kind dcl_kind,
11247 int* ctor_dtor_or_conv_p,
11251 cp_declarator *declarator = NULL;
11252 tree scope = NULL_TREE;
11253 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11254 bool saved_in_declarator_p = parser->in_declarator_p;
11256 tree pushed_scope = NULL_TREE;
11260 /* Peek at the next token. */
11261 token = cp_lexer_peek_token (parser->lexer);
11262 if (token->type == CPP_OPEN_PAREN)
11264 /* This is either a parameter-declaration-clause, or a
11265 parenthesized declarator. When we know we are parsing a
11266 named declarator, it must be a parenthesized declarator
11267 if FIRST is true. For instance, `(int)' is a
11268 parameter-declaration-clause, with an omitted
11269 direct-abstract-declarator. But `((*))', is a
11270 parenthesized abstract declarator. Finally, when T is a
11271 template parameter `(T)' is a
11272 parameter-declaration-clause, and not a parenthesized
11275 We first try and parse a parameter-declaration-clause,
11276 and then try a nested declarator (if FIRST is true).
11278 It is not an error for it not to be a
11279 parameter-declaration-clause, even when FIRST is
11285 The first is the declaration of a function while the
11286 second is a the definition of a variable, including its
11289 Having seen only the parenthesis, we cannot know which of
11290 these two alternatives should be selected. Even more
11291 complex are examples like:
11296 The former is a function-declaration; the latter is a
11297 variable initialization.
11299 Thus again, we try a parameter-declaration-clause, and if
11300 that fails, we back out and return. */
11302 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11304 cp_parameter_declarator *params;
11305 unsigned saved_num_template_parameter_lists;
11307 /* In a member-declarator, the only valid interpretation
11308 of a parenthesis is the start of a
11309 parameter-declaration-clause. (It is invalid to
11310 initialize a static data member with a parenthesized
11311 initializer; only the "=" form of initialization is
11314 cp_parser_parse_tentatively (parser);
11316 /* Consume the `('. */
11317 cp_lexer_consume_token (parser->lexer);
11320 /* If this is going to be an abstract declarator, we're
11321 in a declarator and we can't have default args. */
11322 parser->default_arg_ok_p = false;
11323 parser->in_declarator_p = true;
11326 /* Inside the function parameter list, surrounding
11327 template-parameter-lists do not apply. */
11328 saved_num_template_parameter_lists
11329 = parser->num_template_parameter_lists;
11330 parser->num_template_parameter_lists = 0;
11332 /* Parse the parameter-declaration-clause. */
11333 params = cp_parser_parameter_declaration_clause (parser);
11335 parser->num_template_parameter_lists
11336 = saved_num_template_parameter_lists;
11338 /* If all went well, parse the cv-qualifier-seq and the
11339 exception-specification. */
11340 if (member_p || cp_parser_parse_definitely (parser))
11342 cp_cv_quals cv_quals;
11343 tree exception_specification;
11345 if (ctor_dtor_or_conv_p)
11346 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11348 /* Consume the `)'. */
11349 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11351 /* Parse the cv-qualifier-seq. */
11352 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11353 /* And the exception-specification. */
11354 exception_specification
11355 = cp_parser_exception_specification_opt (parser);
11357 /* Create the function-declarator. */
11358 declarator = make_call_declarator (declarator,
11361 exception_specification);
11362 /* Any subsequent parameter lists are to do with
11363 return type, so are not those of the declared
11365 parser->default_arg_ok_p = false;
11367 /* Repeat the main loop. */
11372 /* If this is the first, we can try a parenthesized
11376 bool saved_in_type_id_in_expr_p;
11378 parser->default_arg_ok_p = saved_default_arg_ok_p;
11379 parser->in_declarator_p = saved_in_declarator_p;
11381 /* Consume the `('. */
11382 cp_lexer_consume_token (parser->lexer);
11383 /* Parse the nested declarator. */
11384 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11385 parser->in_type_id_in_expr_p = true;
11387 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11388 /*parenthesized_p=*/NULL,
11390 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11392 /* Expect a `)'. */
11393 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11394 declarator = cp_error_declarator;
11395 if (declarator == cp_error_declarator)
11398 goto handle_declarator;
11400 /* Otherwise, we must be done. */
11404 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11405 && token->type == CPP_OPEN_SQUARE)
11407 /* Parse an array-declarator. */
11410 if (ctor_dtor_or_conv_p)
11411 *ctor_dtor_or_conv_p = 0;
11414 parser->default_arg_ok_p = false;
11415 parser->in_declarator_p = true;
11416 /* Consume the `['. */
11417 cp_lexer_consume_token (parser->lexer);
11418 /* Peek at the next token. */
11419 token = cp_lexer_peek_token (parser->lexer);
11420 /* If the next token is `]', then there is no
11421 constant-expression. */
11422 if (token->type != CPP_CLOSE_SQUARE)
11424 bool non_constant_p;
11427 = cp_parser_constant_expression (parser,
11428 /*allow_non_constant=*/true,
11430 if (!non_constant_p)
11431 bounds = fold_non_dependent_expr (bounds);
11432 /* Normally, the array bound must be an integral constant
11433 expression. However, as an extension, we allow VLAs
11434 in function scopes. */
11435 else if (!at_function_scope_p ())
11437 error ("array bound is not an integer constant");
11438 bounds = error_mark_node;
11442 bounds = NULL_TREE;
11443 /* Look for the closing `]'. */
11444 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11446 declarator = cp_error_declarator;
11450 declarator = make_array_declarator (declarator, bounds);
11452 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11454 tree qualifying_scope;
11455 tree unqualified_name;
11456 special_function_kind sfk;
11458 /* Parse a declarator-id */
11459 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11460 cp_parser_parse_tentatively (parser);
11461 unqualified_name = cp_parser_declarator_id (parser);
11462 qualifying_scope = parser->scope;
11463 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11465 if (!cp_parser_parse_definitely (parser))
11466 unqualified_name = error_mark_node;
11467 else if (qualifying_scope
11468 || (TREE_CODE (unqualified_name)
11469 != IDENTIFIER_NODE))
11471 cp_parser_error (parser, "expected unqualified-id");
11472 unqualified_name = error_mark_node;
11476 if (unqualified_name == error_mark_node)
11478 declarator = cp_error_declarator;
11482 if (qualifying_scope && at_namespace_scope_p ()
11483 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11485 /* In the declaration of a member of a template class
11486 outside of the class itself, the SCOPE will sometimes
11487 be a TYPENAME_TYPE. For example, given:
11489 template <typename T>
11490 int S<T>::R::i = 3;
11492 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11493 this context, we must resolve S<T>::R to an ordinary
11494 type, rather than a typename type.
11496 The reason we normally avoid resolving TYPENAME_TYPEs
11497 is that a specialization of `S' might render
11498 `S<T>::R' not a type. However, if `S' is
11499 specialized, then this `i' will not be used, so there
11500 is no harm in resolving the types here. */
11503 /* Resolve the TYPENAME_TYPE. */
11504 type = resolve_typename_type (qualifying_scope,
11505 /*only_current_p=*/false);
11506 /* If that failed, the declarator is invalid. */
11507 if (type == error_mark_node)
11508 error ("%<%T::%D%> is not a type",
11509 TYPE_CONTEXT (qualifying_scope),
11510 TYPE_IDENTIFIER (qualifying_scope));
11511 qualifying_scope = type;
11515 if (unqualified_name)
11519 if (qualifying_scope
11520 && CLASS_TYPE_P (qualifying_scope))
11521 class_type = qualifying_scope;
11523 class_type = current_class_type;
11525 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11527 tree name_type = TREE_TYPE (unqualified_name);
11528 if (class_type && same_type_p (name_type, class_type))
11530 if (qualifying_scope
11531 && CLASSTYPE_USE_TEMPLATE (name_type))
11533 error ("invalid use of constructor as a template");
11534 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11535 "name the constructor in a qualified name",
11537 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11538 class_type, name_type);
11539 declarator = cp_error_declarator;
11543 unqualified_name = constructor_name (class_type);
11547 /* We do not attempt to print the declarator
11548 here because we do not have enough
11549 information about its original syntactic
11551 cp_parser_error (parser, "invalid declarator");
11552 declarator = cp_error_declarator;
11559 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11560 sfk = sfk_destructor;
11561 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11562 sfk = sfk_conversion;
11563 else if (/* There's no way to declare a constructor
11564 for an anonymous type, even if the type
11565 got a name for linkage purposes. */
11566 !TYPE_WAS_ANONYMOUS (class_type)
11567 && constructor_name_p (unqualified_name,
11570 unqualified_name = constructor_name (class_type);
11571 sfk = sfk_constructor;
11574 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11575 *ctor_dtor_or_conv_p = -1;
11578 declarator = make_id_declarator (qualifying_scope,
11581 declarator->id_loc = token->location;
11583 handle_declarator:;
11584 scope = get_scope_of_declarator (declarator);
11586 /* Any names that appear after the declarator-id for a
11587 member are looked up in the containing scope. */
11588 pushed_scope = push_scope (scope);
11589 parser->in_declarator_p = true;
11590 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11591 || (declarator && declarator->kind == cdk_id))
11592 /* Default args are only allowed on function
11594 parser->default_arg_ok_p = saved_default_arg_ok_p;
11596 parser->default_arg_ok_p = false;
11605 /* For an abstract declarator, we might wind up with nothing at this
11606 point. That's an error; the declarator is not optional. */
11608 cp_parser_error (parser, "expected declarator");
11610 /* If we entered a scope, we must exit it now. */
11612 pop_scope (pushed_scope);
11614 parser->default_arg_ok_p = saved_default_arg_ok_p;
11615 parser->in_declarator_p = saved_in_declarator_p;
11620 /* Parse a ptr-operator.
11623 * cv-qualifier-seq [opt]
11625 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11630 & cv-qualifier-seq [opt]
11632 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11633 Returns ADDR_EXPR if a reference was used. In the case of a
11634 pointer-to-member, *TYPE is filled in with the TYPE containing the
11635 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11636 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11637 ERROR_MARK if an error occurred. */
11639 static enum tree_code
11640 cp_parser_ptr_operator (cp_parser* parser,
11642 cp_cv_quals *cv_quals)
11644 enum tree_code code = ERROR_MARK;
11647 /* Assume that it's not a pointer-to-member. */
11649 /* And that there are no cv-qualifiers. */
11650 *cv_quals = TYPE_UNQUALIFIED;
11652 /* Peek at the next token. */
11653 token = cp_lexer_peek_token (parser->lexer);
11654 /* If it's a `*' or `&' we have a pointer or reference. */
11655 if (token->type == CPP_MULT || token->type == CPP_AND)
11657 /* Remember which ptr-operator we were processing. */
11658 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11660 /* Consume the `*' or `&'. */
11661 cp_lexer_consume_token (parser->lexer);
11663 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11664 `&', if we are allowing GNU extensions. (The only qualifier
11665 that can legally appear after `&' is `restrict', but that is
11666 enforced during semantic analysis. */
11667 if (code == INDIRECT_REF
11668 || cp_parser_allow_gnu_extensions_p (parser))
11669 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11673 /* Try the pointer-to-member case. */
11674 cp_parser_parse_tentatively (parser);
11675 /* Look for the optional `::' operator. */
11676 cp_parser_global_scope_opt (parser,
11677 /*current_scope_valid_p=*/false);
11678 /* Look for the nested-name specifier. */
11679 cp_parser_nested_name_specifier (parser,
11680 /*typename_keyword_p=*/false,
11681 /*check_dependency_p=*/true,
11683 /*is_declaration=*/false);
11684 /* If we found it, and the next token is a `*', then we are
11685 indeed looking at a pointer-to-member operator. */
11686 if (!cp_parser_error_occurred (parser)
11687 && cp_parser_require (parser, CPP_MULT, "`*'"))
11689 /* The type of which the member is a member is given by the
11691 *type = parser->scope;
11692 /* The next name will not be qualified. */
11693 parser->scope = NULL_TREE;
11694 parser->qualifying_scope = NULL_TREE;
11695 parser->object_scope = NULL_TREE;
11696 /* Indicate that the `*' operator was used. */
11697 code = INDIRECT_REF;
11698 /* Look for the optional cv-qualifier-seq. */
11699 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11701 /* If that didn't work we don't have a ptr-operator. */
11702 if (!cp_parser_parse_definitely (parser))
11703 cp_parser_error (parser, "expected ptr-operator");
11709 /* Parse an (optional) cv-qualifier-seq.
11712 cv-qualifier cv-qualifier-seq [opt]
11723 Returns a bitmask representing the cv-qualifiers. */
11726 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11728 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11733 cp_cv_quals cv_qualifier;
11735 /* Peek at the next token. */
11736 token = cp_lexer_peek_token (parser->lexer);
11737 /* See if it's a cv-qualifier. */
11738 switch (token->keyword)
11741 cv_qualifier = TYPE_QUAL_CONST;
11745 cv_qualifier = TYPE_QUAL_VOLATILE;
11749 cv_qualifier = TYPE_QUAL_RESTRICT;
11753 cv_qualifier = TYPE_UNQUALIFIED;
11760 if (cv_quals & cv_qualifier)
11762 error ("duplicate cv-qualifier");
11763 cp_lexer_purge_token (parser->lexer);
11767 cp_lexer_consume_token (parser->lexer);
11768 cv_quals |= cv_qualifier;
11775 /* Parse a declarator-id.
11779 :: [opt] nested-name-specifier [opt] type-name
11781 In the `id-expression' case, the value returned is as for
11782 cp_parser_id_expression if the id-expression was an unqualified-id.
11783 If the id-expression was a qualified-id, then a SCOPE_REF is
11784 returned. The first operand is the scope (either a NAMESPACE_DECL
11785 or TREE_TYPE), but the second is still just a representation of an
11789 cp_parser_declarator_id (cp_parser* parser)
11792 /* The expression must be an id-expression. Assume that qualified
11793 names are the names of types so that:
11796 int S<T>::R::i = 3;
11798 will work; we must treat `S<T>::R' as the name of a type.
11799 Similarly, assume that qualified names are templates, where
11803 int S<T>::R<T>::i = 3;
11806 id = cp_parser_id_expression (parser,
11807 /*template_keyword_p=*/false,
11808 /*check_dependency_p=*/false,
11809 /*template_p=*/NULL,
11810 /*declarator_p=*/true);
11811 if (BASELINK_P (id))
11812 id = BASELINK_FUNCTIONS (id);
11816 /* Parse a type-id.
11819 type-specifier-seq abstract-declarator [opt]
11821 Returns the TYPE specified. */
11824 cp_parser_type_id (cp_parser* parser)
11826 cp_decl_specifier_seq type_specifier_seq;
11827 cp_declarator *abstract_declarator;
11829 /* Parse the type-specifier-seq. */
11830 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11831 &type_specifier_seq);
11832 if (type_specifier_seq.type == error_mark_node)
11833 return error_mark_node;
11835 /* There might or might not be an abstract declarator. */
11836 cp_parser_parse_tentatively (parser);
11837 /* Look for the declarator. */
11838 abstract_declarator
11839 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11840 /*parenthesized_p=*/NULL,
11841 /*member_p=*/false);
11842 /* Check to see if there really was a declarator. */
11843 if (!cp_parser_parse_definitely (parser))
11844 abstract_declarator = NULL;
11846 return groktypename (&type_specifier_seq, abstract_declarator);
11849 /* Parse a type-specifier-seq.
11851 type-specifier-seq:
11852 type-specifier type-specifier-seq [opt]
11856 type-specifier-seq:
11857 attributes type-specifier-seq [opt]
11859 If IS_CONDITION is true, we are at the start of a "condition",
11860 e.g., we've just seen "if (".
11862 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11865 cp_parser_type_specifier_seq (cp_parser* parser,
11867 cp_decl_specifier_seq *type_specifier_seq)
11869 bool seen_type_specifier = false;
11870 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11872 /* Clear the TYPE_SPECIFIER_SEQ. */
11873 clear_decl_specs (type_specifier_seq);
11875 /* Parse the type-specifiers and attributes. */
11878 tree type_specifier;
11879 bool is_cv_qualifier;
11881 /* Check for attributes first. */
11882 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11884 type_specifier_seq->attributes =
11885 chainon (type_specifier_seq->attributes,
11886 cp_parser_attributes_opt (parser));
11890 /* Look for the type-specifier. */
11891 type_specifier = cp_parser_type_specifier (parser,
11893 type_specifier_seq,
11894 /*is_declaration=*/false,
11897 if (!type_specifier)
11899 /* If the first type-specifier could not be found, this is not a
11900 type-specifier-seq at all. */
11901 if (!seen_type_specifier)
11903 cp_parser_error (parser, "expected type-specifier");
11904 type_specifier_seq->type = error_mark_node;
11907 /* If subsequent type-specifiers could not be found, the
11908 type-specifier-seq is complete. */
11912 seen_type_specifier = true;
11913 /* The standard says that a condition can be:
11915 type-specifier-seq declarator = assignment-expression
11922 we should treat the "S" as a declarator, not as a
11923 type-specifier. The standard doesn't say that explicitly for
11924 type-specifier-seq, but it does say that for
11925 decl-specifier-seq in an ordinary declaration. Perhaps it
11926 would be clearer just to allow a decl-specifier-seq here, and
11927 then add a semantic restriction that if any decl-specifiers
11928 that are not type-specifiers appear, the program is invalid. */
11929 if (is_condition && !is_cv_qualifier)
11930 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11934 /* Parse a parameter-declaration-clause.
11936 parameter-declaration-clause:
11937 parameter-declaration-list [opt] ... [opt]
11938 parameter-declaration-list , ...
11940 Returns a representation for the parameter declarations. A return
11941 value of NULL indicates a parameter-declaration-clause consisting
11942 only of an ellipsis. */
11944 static cp_parameter_declarator *
11945 cp_parser_parameter_declaration_clause (cp_parser* parser)
11947 cp_parameter_declarator *parameters;
11952 /* Peek at the next token. */
11953 token = cp_lexer_peek_token (parser->lexer);
11954 /* Check for trivial parameter-declaration-clauses. */
11955 if (token->type == CPP_ELLIPSIS)
11957 /* Consume the `...' token. */
11958 cp_lexer_consume_token (parser->lexer);
11961 else if (token->type == CPP_CLOSE_PAREN)
11962 /* There are no parameters. */
11964 #ifndef NO_IMPLICIT_EXTERN_C
11965 if (in_system_header && current_class_type == NULL
11966 && current_lang_name == lang_name_c)
11970 return no_parameters;
11972 /* Check for `(void)', too, which is a special case. */
11973 else if (token->keyword == RID_VOID
11974 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11975 == CPP_CLOSE_PAREN))
11977 /* Consume the `void' token. */
11978 cp_lexer_consume_token (parser->lexer);
11979 /* There are no parameters. */
11980 return no_parameters;
11983 /* Parse the parameter-declaration-list. */
11984 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11985 /* If a parse error occurred while parsing the
11986 parameter-declaration-list, then the entire
11987 parameter-declaration-clause is erroneous. */
11991 /* Peek at the next token. */
11992 token = cp_lexer_peek_token (parser->lexer);
11993 /* If it's a `,', the clause should terminate with an ellipsis. */
11994 if (token->type == CPP_COMMA)
11996 /* Consume the `,'. */
11997 cp_lexer_consume_token (parser->lexer);
11998 /* Expect an ellipsis. */
12000 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12002 /* It might also be `...' if the optional trailing `,' was
12004 else if (token->type == CPP_ELLIPSIS)
12006 /* Consume the `...' token. */
12007 cp_lexer_consume_token (parser->lexer);
12008 /* And remember that we saw it. */
12012 ellipsis_p = false;
12014 /* Finish the parameter list. */
12015 if (parameters && ellipsis_p)
12016 parameters->ellipsis_p = true;
12021 /* Parse a parameter-declaration-list.
12023 parameter-declaration-list:
12024 parameter-declaration
12025 parameter-declaration-list , parameter-declaration
12027 Returns a representation of the parameter-declaration-list, as for
12028 cp_parser_parameter_declaration_clause. However, the
12029 `void_list_node' is never appended to the list. Upon return,
12030 *IS_ERROR will be true iff an error occurred. */
12032 static cp_parameter_declarator *
12033 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12035 cp_parameter_declarator *parameters = NULL;
12036 cp_parameter_declarator **tail = ¶meters;
12038 /* Assume all will go well. */
12041 /* Look for more parameters. */
12044 cp_parameter_declarator *parameter;
12045 bool parenthesized_p;
12046 /* Parse the parameter. */
12048 = cp_parser_parameter_declaration (parser,
12049 /*template_parm_p=*/false,
12052 /* If a parse error occurred parsing the parameter declaration,
12053 then the entire parameter-declaration-list is erroneous. */
12060 /* Add the new parameter to the list. */
12062 tail = ¶meter->next;
12064 /* Peek at the next token. */
12065 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12066 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12067 /* These are for Objective-C++ */
12068 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12069 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12070 /* The parameter-declaration-list is complete. */
12072 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12076 /* Peek at the next token. */
12077 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12078 /* If it's an ellipsis, then the list is complete. */
12079 if (token->type == CPP_ELLIPSIS)
12081 /* Otherwise, there must be more parameters. Consume the
12083 cp_lexer_consume_token (parser->lexer);
12084 /* When parsing something like:
12086 int i(float f, double d)
12088 we can tell after seeing the declaration for "f" that we
12089 are not looking at an initialization of a variable "i",
12090 but rather at the declaration of a function "i".
12092 Due to the fact that the parsing of template arguments
12093 (as specified to a template-id) requires backtracking we
12094 cannot use this technique when inside a template argument
12096 if (!parser->in_template_argument_list_p
12097 && !parser->in_type_id_in_expr_p
12098 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12099 /* However, a parameter-declaration of the form
12100 "foat(f)" (which is a valid declaration of a
12101 parameter "f") can also be interpreted as an
12102 expression (the conversion of "f" to "float"). */
12103 && !parenthesized_p)
12104 cp_parser_commit_to_tentative_parse (parser);
12108 cp_parser_error (parser, "expected %<,%> or %<...%>");
12109 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12110 cp_parser_skip_to_closing_parenthesis (parser,
12111 /*recovering=*/true,
12112 /*or_comma=*/false,
12113 /*consume_paren=*/false);
12121 /* Parse a parameter declaration.
12123 parameter-declaration:
12124 decl-specifier-seq declarator
12125 decl-specifier-seq declarator = assignment-expression
12126 decl-specifier-seq abstract-declarator [opt]
12127 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12129 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12130 declares a template parameter. (In that case, a non-nested `>'
12131 token encountered during the parsing of the assignment-expression
12132 is not interpreted as a greater-than operator.)
12134 Returns a representation of the parameter, or NULL if an error
12135 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12136 true iff the declarator is of the form "(p)". */
12138 static cp_parameter_declarator *
12139 cp_parser_parameter_declaration (cp_parser *parser,
12140 bool template_parm_p,
12141 bool *parenthesized_p)
12143 int declares_class_or_enum;
12144 bool greater_than_is_operator_p;
12145 cp_decl_specifier_seq decl_specifiers;
12146 cp_declarator *declarator;
12147 tree default_argument;
12149 const char *saved_message;
12151 /* In a template parameter, `>' is not an operator.
12155 When parsing a default template-argument for a non-type
12156 template-parameter, the first non-nested `>' is taken as the end
12157 of the template parameter-list rather than a greater-than
12159 greater_than_is_operator_p = !template_parm_p;
12161 /* Type definitions may not appear in parameter types. */
12162 saved_message = parser->type_definition_forbidden_message;
12163 parser->type_definition_forbidden_message
12164 = "types may not be defined in parameter types";
12166 /* Parse the declaration-specifiers. */
12167 cp_parser_decl_specifier_seq (parser,
12168 CP_PARSER_FLAGS_NONE,
12170 &declares_class_or_enum);
12171 /* If an error occurred, there's no reason to attempt to parse the
12172 rest of the declaration. */
12173 if (cp_parser_error_occurred (parser))
12175 parser->type_definition_forbidden_message = saved_message;
12179 /* Peek at the next token. */
12180 token = cp_lexer_peek_token (parser->lexer);
12181 /* If the next token is a `)', `,', `=', `>', or `...', then there
12182 is no declarator. */
12183 if (token->type == CPP_CLOSE_PAREN
12184 || token->type == CPP_COMMA
12185 || token->type == CPP_EQ
12186 || token->type == CPP_ELLIPSIS
12187 || token->type == CPP_GREATER)
12190 if (parenthesized_p)
12191 *parenthesized_p = false;
12193 /* Otherwise, there should be a declarator. */
12196 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12197 parser->default_arg_ok_p = false;
12199 /* After seeing a decl-specifier-seq, if the next token is not a
12200 "(", there is no possibility that the code is a valid
12201 expression. Therefore, if parsing tentatively, we commit at
12203 if (!parser->in_template_argument_list_p
12204 /* In an expression context, having seen:
12208 we cannot be sure whether we are looking at a
12209 function-type (taking a "char" as a parameter) or a cast
12210 of some object of type "char" to "int". */
12211 && !parser->in_type_id_in_expr_p
12212 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12213 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12214 cp_parser_commit_to_tentative_parse (parser);
12215 /* Parse the declarator. */
12216 declarator = cp_parser_declarator (parser,
12217 CP_PARSER_DECLARATOR_EITHER,
12218 /*ctor_dtor_or_conv_p=*/NULL,
12220 /*member_p=*/false);
12221 parser->default_arg_ok_p = saved_default_arg_ok_p;
12222 /* After the declarator, allow more attributes. */
12223 decl_specifiers.attributes
12224 = chainon (decl_specifiers.attributes,
12225 cp_parser_attributes_opt (parser));
12228 /* The restriction on defining new types applies only to the type
12229 of the parameter, not to the default argument. */
12230 parser->type_definition_forbidden_message = saved_message;
12232 /* If the next token is `=', then process a default argument. */
12233 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12235 bool saved_greater_than_is_operator_p;
12236 /* Consume the `='. */
12237 cp_lexer_consume_token (parser->lexer);
12239 /* If we are defining a class, then the tokens that make up the
12240 default argument must be saved and processed later. */
12241 if (!template_parm_p && at_class_scope_p ()
12242 && TYPE_BEING_DEFINED (current_class_type))
12244 unsigned depth = 0;
12245 cp_token *first_token;
12248 /* Add tokens until we have processed the entire default
12249 argument. We add the range [first_token, token). */
12250 first_token = cp_lexer_peek_token (parser->lexer);
12255 /* Peek at the next token. */
12256 token = cp_lexer_peek_token (parser->lexer);
12257 /* What we do depends on what token we have. */
12258 switch (token->type)
12260 /* In valid code, a default argument must be
12261 immediately followed by a `,' `)', or `...'. */
12263 case CPP_CLOSE_PAREN:
12265 /* If we run into a non-nested `;', `}', or `]',
12266 then the code is invalid -- but the default
12267 argument is certainly over. */
12268 case CPP_SEMICOLON:
12269 case CPP_CLOSE_BRACE:
12270 case CPP_CLOSE_SQUARE:
12273 /* Update DEPTH, if necessary. */
12274 else if (token->type == CPP_CLOSE_PAREN
12275 || token->type == CPP_CLOSE_BRACE
12276 || token->type == CPP_CLOSE_SQUARE)
12280 case CPP_OPEN_PAREN:
12281 case CPP_OPEN_SQUARE:
12282 case CPP_OPEN_BRACE:
12287 /* If we see a non-nested `>', and `>' is not an
12288 operator, then it marks the end of the default
12290 if (!depth && !greater_than_is_operator_p)
12294 /* If we run out of tokens, issue an error message. */
12296 case CPP_PRAGMA_EOL:
12297 error ("file ends in default argument");
12303 /* In these cases, we should look for template-ids.
12304 For example, if the default argument is
12305 `X<int, double>()', we need to do name lookup to
12306 figure out whether or not `X' is a template; if
12307 so, the `,' does not end the default argument.
12309 That is not yet done. */
12316 /* If we've reached the end, stop. */
12320 /* Add the token to the token block. */
12321 token = cp_lexer_consume_token (parser->lexer);
12324 /* Create a DEFAULT_ARG to represented the unparsed default
12326 default_argument = make_node (DEFAULT_ARG);
12327 DEFARG_TOKENS (default_argument)
12328 = cp_token_cache_new (first_token, token);
12329 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12331 /* Outside of a class definition, we can just parse the
12332 assignment-expression. */
12335 bool saved_local_variables_forbidden_p;
12337 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12339 saved_greater_than_is_operator_p
12340 = parser->greater_than_is_operator_p;
12341 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12342 /* Local variable names (and the `this' keyword) may not
12343 appear in a default argument. */
12344 saved_local_variables_forbidden_p
12345 = parser->local_variables_forbidden_p;
12346 parser->local_variables_forbidden_p = true;
12347 /* Parse the assignment-expression. */
12349 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12350 /* Restore saved state. */
12351 parser->greater_than_is_operator_p
12352 = saved_greater_than_is_operator_p;
12353 parser->local_variables_forbidden_p
12354 = saved_local_variables_forbidden_p;
12356 if (!parser->default_arg_ok_p)
12358 if (!flag_pedantic_errors)
12359 warning (0, "deprecated use of default argument for parameter of non-function");
12362 error ("default arguments are only permitted for function parameters");
12363 default_argument = NULL_TREE;
12368 default_argument = NULL_TREE;
12370 return make_parameter_declarator (&decl_specifiers,
12375 /* Parse a function-body.
12378 compound_statement */
12381 cp_parser_function_body (cp_parser *parser)
12383 cp_parser_compound_statement (parser, NULL, false);
12386 /* Parse a ctor-initializer-opt followed by a function-body. Return
12387 true if a ctor-initializer was present. */
12390 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12393 bool ctor_initializer_p;
12395 /* Begin the function body. */
12396 body = begin_function_body ();
12397 /* Parse the optional ctor-initializer. */
12398 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12399 /* Parse the function-body. */
12400 cp_parser_function_body (parser);
12401 /* Finish the function body. */
12402 finish_function_body (body);
12404 return ctor_initializer_p;
12407 /* Parse an initializer.
12410 = initializer-clause
12411 ( expression-list )
12413 Returns an expression representing the initializer. If no
12414 initializer is present, NULL_TREE is returned.
12416 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12417 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12418 set to FALSE if there is no initializer present. If there is an
12419 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12420 is set to true; otherwise it is set to false. */
12423 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12424 bool* non_constant_p)
12429 /* Peek at the next token. */
12430 token = cp_lexer_peek_token (parser->lexer);
12432 /* Let our caller know whether or not this initializer was
12434 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12435 /* Assume that the initializer is constant. */
12436 *non_constant_p = false;
12438 if (token->type == CPP_EQ)
12440 /* Consume the `='. */
12441 cp_lexer_consume_token (parser->lexer);
12442 /* Parse the initializer-clause. */
12443 init = cp_parser_initializer_clause (parser, non_constant_p);
12445 else if (token->type == CPP_OPEN_PAREN)
12446 init = cp_parser_parenthesized_expression_list (parser, false,
12451 /* Anything else is an error. */
12452 cp_parser_error (parser, "expected initializer");
12453 init = error_mark_node;
12459 /* Parse an initializer-clause.
12461 initializer-clause:
12462 assignment-expression
12463 { initializer-list , [opt] }
12466 Returns an expression representing the initializer.
12468 If the `assignment-expression' production is used the value
12469 returned is simply a representation for the expression.
12471 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12472 the elements of the initializer-list (or NULL, if the last
12473 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12474 NULL_TREE. There is no way to detect whether or not the optional
12475 trailing `,' was provided. NON_CONSTANT_P is as for
12476 cp_parser_initializer. */
12479 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12483 /* Assume the expression is constant. */
12484 *non_constant_p = false;
12486 /* If it is not a `{', then we are looking at an
12487 assignment-expression. */
12488 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12491 = cp_parser_constant_expression (parser,
12492 /*allow_non_constant_p=*/true,
12494 if (!*non_constant_p)
12495 initializer = fold_non_dependent_expr (initializer);
12499 /* Consume the `{' token. */
12500 cp_lexer_consume_token (parser->lexer);
12501 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12502 initializer = make_node (CONSTRUCTOR);
12503 /* If it's not a `}', then there is a non-trivial initializer. */
12504 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12506 /* Parse the initializer list. */
12507 CONSTRUCTOR_ELTS (initializer)
12508 = cp_parser_initializer_list (parser, non_constant_p);
12509 /* A trailing `,' token is allowed. */
12510 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12511 cp_lexer_consume_token (parser->lexer);
12513 /* Now, there should be a trailing `}'. */
12514 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12517 return initializer;
12520 /* Parse an initializer-list.
12524 initializer-list , initializer-clause
12529 identifier : initializer-clause
12530 initializer-list, identifier : initializer-clause
12532 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12533 for the initializer. If the INDEX of the elt is non-NULL, it is the
12534 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12535 as for cp_parser_initializer. */
12537 static VEC(constructor_elt,gc) *
12538 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12540 VEC(constructor_elt,gc) *v = NULL;
12542 /* Assume all of the expressions are constant. */
12543 *non_constant_p = false;
12545 /* Parse the rest of the list. */
12551 bool clause_non_constant_p;
12553 /* If the next token is an identifier and the following one is a
12554 colon, we are looking at the GNU designated-initializer
12556 if (cp_parser_allow_gnu_extensions_p (parser)
12557 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12558 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12560 /* Consume the identifier. */
12561 identifier = cp_lexer_consume_token (parser->lexer)->value;
12562 /* Consume the `:'. */
12563 cp_lexer_consume_token (parser->lexer);
12566 identifier = NULL_TREE;
12568 /* Parse the initializer. */
12569 initializer = cp_parser_initializer_clause (parser,
12570 &clause_non_constant_p);
12571 /* If any clause is non-constant, so is the entire initializer. */
12572 if (clause_non_constant_p)
12573 *non_constant_p = true;
12575 /* Add it to the vector. */
12576 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12578 /* If the next token is not a comma, we have reached the end of
12580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12583 /* Peek at the next token. */
12584 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12585 /* If the next token is a `}', then we're still done. An
12586 initializer-clause can have a trailing `,' after the
12587 initializer-list and before the closing `}'. */
12588 if (token->type == CPP_CLOSE_BRACE)
12591 /* Consume the `,' token. */
12592 cp_lexer_consume_token (parser->lexer);
12598 /* Classes [gram.class] */
12600 /* Parse a class-name.
12606 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12607 to indicate that names looked up in dependent types should be
12608 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12609 keyword has been used to indicate that the name that appears next
12610 is a template. TAG_TYPE indicates the explicit tag given before
12611 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12612 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12613 is the class being defined in a class-head.
12615 Returns the TYPE_DECL representing the class. */
12618 cp_parser_class_name (cp_parser *parser,
12619 bool typename_keyword_p,
12620 bool template_keyword_p,
12621 enum tag_types tag_type,
12622 bool check_dependency_p,
12624 bool is_declaration)
12631 /* All class-names start with an identifier. */
12632 token = cp_lexer_peek_token (parser->lexer);
12633 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12635 cp_parser_error (parser, "expected class-name");
12636 return error_mark_node;
12639 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12640 to a template-id, so we save it here. */
12641 scope = parser->scope;
12642 if (scope == error_mark_node)
12643 return error_mark_node;
12645 /* Any name names a type if we're following the `typename' keyword
12646 in a qualified name where the enclosing scope is type-dependent. */
12647 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12648 && dependent_type_p (scope));
12649 /* Handle the common case (an identifier, but not a template-id)
12651 if (token->type == CPP_NAME
12652 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12654 cp_token *identifier_token;
12658 /* Look for the identifier. */
12659 identifier_token = cp_lexer_peek_token (parser->lexer);
12660 ambiguous_p = identifier_token->ambiguous_p;
12661 identifier = cp_parser_identifier (parser);
12662 /* If the next token isn't an identifier, we are certainly not
12663 looking at a class-name. */
12664 if (identifier == error_mark_node)
12665 decl = error_mark_node;
12666 /* If we know this is a type-name, there's no need to look it
12668 else if (typename_p)
12672 tree ambiguous_decls;
12673 /* If we already know that this lookup is ambiguous, then
12674 we've already issued an error message; there's no reason
12678 cp_parser_simulate_error (parser);
12679 return error_mark_node;
12681 /* If the next token is a `::', then the name must be a type
12684 [basic.lookup.qual]
12686 During the lookup for a name preceding the :: scope
12687 resolution operator, object, function, and enumerator
12688 names are ignored. */
12689 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12690 tag_type = typename_type;
12691 /* Look up the name. */
12692 decl = cp_parser_lookup_name (parser, identifier,
12694 /*is_template=*/false,
12695 /*is_namespace=*/false,
12696 check_dependency_p,
12698 if (ambiguous_decls)
12700 error ("reference to %qD is ambiguous", identifier);
12701 print_candidates (ambiguous_decls);
12702 if (cp_parser_parsing_tentatively (parser))
12704 identifier_token->ambiguous_p = true;
12705 cp_parser_simulate_error (parser);
12707 return error_mark_node;
12713 /* Try a template-id. */
12714 decl = cp_parser_template_id (parser, template_keyword_p,
12715 check_dependency_p,
12717 if (decl == error_mark_node)
12718 return error_mark_node;
12721 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12723 /* If this is a typename, create a TYPENAME_TYPE. */
12724 if (typename_p && decl != error_mark_node)
12726 decl = make_typename_type (scope, decl, typename_type,
12727 /*complain=*/tf_error);
12728 if (decl != error_mark_node)
12729 decl = TYPE_NAME (decl);
12732 /* Check to see that it is really the name of a class. */
12733 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12734 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12735 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12736 /* Situations like this:
12738 template <typename T> struct A {
12739 typename T::template X<int>::I i;
12742 are problematic. Is `T::template X<int>' a class-name? The
12743 standard does not seem to be definitive, but there is no other
12744 valid interpretation of the following `::'. Therefore, those
12745 names are considered class-names. */
12746 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12747 else if (decl == error_mark_node
12748 || TREE_CODE (decl) != TYPE_DECL
12749 || TREE_TYPE (decl) == error_mark_node
12750 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12752 cp_parser_error (parser, "expected class-name");
12753 return error_mark_node;
12759 /* Parse a class-specifier.
12762 class-head { member-specification [opt] }
12764 Returns the TREE_TYPE representing the class. */
12767 cp_parser_class_specifier (cp_parser* parser)
12771 tree attributes = NULL_TREE;
12772 int has_trailing_semicolon;
12773 bool nested_name_specifier_p;
12774 unsigned saved_num_template_parameter_lists;
12775 tree old_scope = NULL_TREE;
12776 tree scope = NULL_TREE;
12778 push_deferring_access_checks (dk_no_deferred);
12780 /* Parse the class-head. */
12781 type = cp_parser_class_head (parser,
12782 &nested_name_specifier_p,
12784 /* If the class-head was a semantic disaster, skip the entire body
12788 cp_parser_skip_to_end_of_block_or_statement (parser);
12789 pop_deferring_access_checks ();
12790 return error_mark_node;
12793 /* Look for the `{'. */
12794 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12796 pop_deferring_access_checks ();
12797 return error_mark_node;
12800 /* Issue an error message if type-definitions are forbidden here. */
12801 cp_parser_check_type_definition (parser);
12802 /* Remember that we are defining one more class. */
12803 ++parser->num_classes_being_defined;
12804 /* Inside the class, surrounding template-parameter-lists do not
12806 saved_num_template_parameter_lists
12807 = parser->num_template_parameter_lists;
12808 parser->num_template_parameter_lists = 0;
12810 /* Start the class. */
12811 if (nested_name_specifier_p)
12813 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12814 old_scope = push_inner_scope (scope);
12816 type = begin_class_definition (type);
12818 if (type == error_mark_node)
12819 /* If the type is erroneous, skip the entire body of the class. */
12820 cp_parser_skip_to_closing_brace (parser);
12822 /* Parse the member-specification. */
12823 cp_parser_member_specification_opt (parser);
12825 /* Look for the trailing `}'. */
12826 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12827 /* We get better error messages by noticing a common problem: a
12828 missing trailing `;'. */
12829 token = cp_lexer_peek_token (parser->lexer);
12830 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12831 /* Look for trailing attributes to apply to this class. */
12832 if (cp_parser_allow_gnu_extensions_p (parser))
12834 tree sub_attr = cp_parser_attributes_opt (parser);
12835 attributes = chainon (attributes, sub_attr);
12837 if (type != error_mark_node)
12838 type = finish_struct (type, attributes);
12839 if (nested_name_specifier_p)
12840 pop_inner_scope (old_scope, scope);
12841 /* If this class is not itself within the scope of another class,
12842 then we need to parse the bodies of all of the queued function
12843 definitions. Note that the queued functions defined in a class
12844 are not always processed immediately following the
12845 class-specifier for that class. Consider:
12848 struct B { void f() { sizeof (A); } };
12851 If `f' were processed before the processing of `A' were
12852 completed, there would be no way to compute the size of `A'.
12853 Note that the nesting we are interested in here is lexical --
12854 not the semantic nesting given by TYPE_CONTEXT. In particular,
12857 struct A { struct B; };
12858 struct A::B { void f() { } };
12860 there is no need to delay the parsing of `A::B::f'. */
12861 if (--parser->num_classes_being_defined == 0)
12865 tree class_type = NULL_TREE;
12866 tree pushed_scope = NULL_TREE;
12868 /* In a first pass, parse default arguments to the functions.
12869 Then, in a second pass, parse the bodies of the functions.
12870 This two-phased approach handles cases like:
12878 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12879 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12880 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12881 TREE_PURPOSE (parser->unparsed_functions_queues)
12882 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12884 fn = TREE_VALUE (queue_entry);
12885 /* If there are default arguments that have not yet been processed,
12886 take care of them now. */
12887 if (class_type != TREE_PURPOSE (queue_entry))
12890 pop_scope (pushed_scope);
12891 class_type = TREE_PURPOSE (queue_entry);
12892 pushed_scope = push_scope (class_type);
12894 /* Make sure that any template parameters are in scope. */
12895 maybe_begin_member_template_processing (fn);
12896 /* Parse the default argument expressions. */
12897 cp_parser_late_parsing_default_args (parser, fn);
12898 /* Remove any template parameters from the symbol table. */
12899 maybe_end_member_template_processing ();
12902 pop_scope (pushed_scope);
12903 /* Now parse the body of the functions. */
12904 for (TREE_VALUE (parser->unparsed_functions_queues)
12905 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12906 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12907 TREE_VALUE (parser->unparsed_functions_queues)
12908 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12910 /* Figure out which function we need to process. */
12911 fn = TREE_VALUE (queue_entry);
12912 /* Parse the function. */
12913 cp_parser_late_parsing_for_member (parser, fn);
12917 /* Put back any saved access checks. */
12918 pop_deferring_access_checks ();
12920 /* Restore the count of active template-parameter-lists. */
12921 parser->num_template_parameter_lists
12922 = saved_num_template_parameter_lists;
12927 /* Parse a class-head.
12930 class-key identifier [opt] base-clause [opt]
12931 class-key nested-name-specifier identifier base-clause [opt]
12932 class-key nested-name-specifier [opt] template-id
12936 class-key attributes identifier [opt] base-clause [opt]
12937 class-key attributes nested-name-specifier identifier base-clause [opt]
12938 class-key attributes nested-name-specifier [opt] template-id
12941 Returns the TYPE of the indicated class. Sets
12942 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12943 involving a nested-name-specifier was used, and FALSE otherwise.
12945 Returns error_mark_node if this is not a class-head.
12947 Returns NULL_TREE if the class-head is syntactically valid, but
12948 semantically invalid in a way that means we should skip the entire
12949 body of the class. */
12952 cp_parser_class_head (cp_parser* parser,
12953 bool* nested_name_specifier_p,
12954 tree *attributes_p)
12956 tree nested_name_specifier;
12957 enum tag_types class_key;
12958 tree id = NULL_TREE;
12959 tree type = NULL_TREE;
12961 bool template_id_p = false;
12962 bool qualified_p = false;
12963 bool invalid_nested_name_p = false;
12964 bool invalid_explicit_specialization_p = false;
12965 tree pushed_scope = NULL_TREE;
12966 unsigned num_templates;
12969 /* Assume no nested-name-specifier will be present. */
12970 *nested_name_specifier_p = false;
12971 /* Assume no template parameter lists will be used in defining the
12975 /* Look for the class-key. */
12976 class_key = cp_parser_class_key (parser);
12977 if (class_key == none_type)
12978 return error_mark_node;
12980 /* Parse the attributes. */
12981 attributes = cp_parser_attributes_opt (parser);
12983 /* If the next token is `::', that is invalid -- but sometimes
12984 people do try to write:
12988 Handle this gracefully by accepting the extra qualifier, and then
12989 issuing an error about it later if this really is a
12990 class-head. If it turns out just to be an elaborated type
12991 specifier, remain silent. */
12992 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12993 qualified_p = true;
12995 push_deferring_access_checks (dk_no_check);
12997 /* Determine the name of the class. Begin by looking for an
12998 optional nested-name-specifier. */
12999 nested_name_specifier
13000 = cp_parser_nested_name_specifier_opt (parser,
13001 /*typename_keyword_p=*/false,
13002 /*check_dependency_p=*/false,
13004 /*is_declaration=*/false);
13005 /* If there was a nested-name-specifier, then there *must* be an
13007 if (nested_name_specifier)
13009 /* Although the grammar says `identifier', it really means
13010 `class-name' or `template-name'. You are only allowed to
13011 define a class that has already been declared with this
13014 The proposed resolution for Core Issue 180 says that whever
13015 you see `class T::X' you should treat `X' as a type-name.
13017 It is OK to define an inaccessible class; for example:
13019 class A { class B; };
13022 We do not know if we will see a class-name, or a
13023 template-name. We look for a class-name first, in case the
13024 class-name is a template-id; if we looked for the
13025 template-name first we would stop after the template-name. */
13026 cp_parser_parse_tentatively (parser);
13027 type = cp_parser_class_name (parser,
13028 /*typename_keyword_p=*/false,
13029 /*template_keyword_p=*/false,
13031 /*check_dependency_p=*/false,
13032 /*class_head_p=*/true,
13033 /*is_declaration=*/false);
13034 /* If that didn't work, ignore the nested-name-specifier. */
13035 if (!cp_parser_parse_definitely (parser))
13037 invalid_nested_name_p = true;
13038 id = cp_parser_identifier (parser);
13039 if (id == error_mark_node)
13042 /* If we could not find a corresponding TYPE, treat this
13043 declaration like an unqualified declaration. */
13044 if (type == error_mark_node)
13045 nested_name_specifier = NULL_TREE;
13046 /* Otherwise, count the number of templates used in TYPE and its
13047 containing scopes. */
13052 for (scope = TREE_TYPE (type);
13053 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13054 scope = (TYPE_P (scope)
13055 ? TYPE_CONTEXT (scope)
13056 : DECL_CONTEXT (scope)))
13058 && CLASS_TYPE_P (scope)
13059 && CLASSTYPE_TEMPLATE_INFO (scope)
13060 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13061 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13065 /* Otherwise, the identifier is optional. */
13068 /* We don't know whether what comes next is a template-id,
13069 an identifier, or nothing at all. */
13070 cp_parser_parse_tentatively (parser);
13071 /* Check for a template-id. */
13072 id = cp_parser_template_id (parser,
13073 /*template_keyword_p=*/false,
13074 /*check_dependency_p=*/true,
13075 /*is_declaration=*/true);
13076 /* If that didn't work, it could still be an identifier. */
13077 if (!cp_parser_parse_definitely (parser))
13079 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13080 id = cp_parser_identifier (parser);
13086 template_id_p = true;
13091 pop_deferring_access_checks ();
13094 cp_parser_check_for_invalid_template_id (parser, id);
13096 /* If it's not a `:' or a `{' then we can't really be looking at a
13097 class-head, since a class-head only appears as part of a
13098 class-specifier. We have to detect this situation before calling
13099 xref_tag, since that has irreversible side-effects. */
13100 if (!cp_parser_next_token_starts_class_definition_p (parser))
13102 cp_parser_error (parser, "expected %<{%> or %<:%>");
13103 return error_mark_node;
13106 /* At this point, we're going ahead with the class-specifier, even
13107 if some other problem occurs. */
13108 cp_parser_commit_to_tentative_parse (parser);
13109 /* Issue the error about the overly-qualified name now. */
13111 cp_parser_error (parser,
13112 "global qualification of class name is invalid");
13113 else if (invalid_nested_name_p)
13114 cp_parser_error (parser,
13115 "qualified name does not name a class");
13116 else if (nested_name_specifier)
13120 /* Reject typedef-names in class heads. */
13121 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13123 error ("invalid class name in declaration of %qD", type);
13128 /* Figure out in what scope the declaration is being placed. */
13129 scope = current_scope ();
13130 /* If that scope does not contain the scope in which the
13131 class was originally declared, the program is invalid. */
13132 if (scope && !is_ancestor (scope, nested_name_specifier))
13134 error ("declaration of %qD in %qD which does not enclose %qD",
13135 type, scope, nested_name_specifier);
13141 A declarator-id shall not be qualified exception of the
13142 definition of a ... nested class outside of its class
13143 ... [or] a the definition or explicit instantiation of a
13144 class member of a namespace outside of its namespace. */
13145 if (scope == nested_name_specifier)
13147 pedwarn ("extra qualification ignored");
13148 nested_name_specifier = NULL_TREE;
13152 /* An explicit-specialization must be preceded by "template <>". If
13153 it is not, try to recover gracefully. */
13154 if (at_namespace_scope_p ()
13155 && parser->num_template_parameter_lists == 0
13158 error ("an explicit specialization must be preceded by %<template <>%>");
13159 invalid_explicit_specialization_p = true;
13160 /* Take the same action that would have been taken by
13161 cp_parser_explicit_specialization. */
13162 ++parser->num_template_parameter_lists;
13163 begin_specialization ();
13165 /* There must be no "return" statements between this point and the
13166 end of this function; set "type "to the correct return value and
13167 use "goto done;" to return. */
13168 /* Make sure that the right number of template parameters were
13170 if (!cp_parser_check_template_parameters (parser, num_templates))
13172 /* If something went wrong, there is no point in even trying to
13173 process the class-definition. */
13178 /* Look up the type. */
13181 type = TREE_TYPE (id);
13182 maybe_process_partial_specialization (type);
13183 if (nested_name_specifier)
13184 pushed_scope = push_scope (nested_name_specifier);
13186 else if (nested_name_specifier)
13192 template <typename T> struct S { struct T };
13193 template <typename T> struct S<T>::T { };
13195 we will get a TYPENAME_TYPE when processing the definition of
13196 `S::T'. We need to resolve it to the actual type before we
13197 try to define it. */
13198 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13200 class_type = resolve_typename_type (TREE_TYPE (type),
13201 /*only_current_p=*/false);
13202 if (class_type != error_mark_node)
13203 type = TYPE_NAME (class_type);
13206 cp_parser_error (parser, "could not resolve typename type");
13207 type = error_mark_node;
13211 maybe_process_partial_specialization (TREE_TYPE (type));
13212 class_type = current_class_type;
13213 /* Enter the scope indicated by the nested-name-specifier. */
13214 pushed_scope = push_scope (nested_name_specifier);
13215 /* Get the canonical version of this type. */
13216 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13217 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13218 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13220 type = push_template_decl (type);
13221 if (type == error_mark_node)
13228 type = TREE_TYPE (type);
13229 *nested_name_specifier_p = true;
13231 else /* The name is not a nested name. */
13233 /* If the class was unnamed, create a dummy name. */
13235 id = make_anon_name ();
13236 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13237 parser->num_template_parameter_lists);
13240 /* Indicate whether this class was declared as a `class' or as a
13242 if (TREE_CODE (type) == RECORD_TYPE)
13243 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13244 cp_parser_check_class_key (class_key, type);
13246 /* If this type was already complete, and we see another definition,
13247 that's an error. */
13248 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13250 error ("redefinition of %q#T", type);
13251 error ("previous definition of %q+#T", type);
13256 /* We will have entered the scope containing the class; the names of
13257 base classes should be looked up in that context. For example:
13259 struct A { struct B {}; struct C; };
13260 struct A::C : B {};
13265 /* Get the list of base-classes, if there is one. */
13266 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13267 bases = cp_parser_base_clause (parser);
13269 /* Process the base classes. */
13270 xref_basetypes (type, bases);
13273 /* Leave the scope given by the nested-name-specifier. We will
13274 enter the class scope itself while processing the members. */
13276 pop_scope (pushed_scope);
13278 if (invalid_explicit_specialization_p)
13280 end_specialization ();
13281 --parser->num_template_parameter_lists;
13283 *attributes_p = attributes;
13287 /* Parse a class-key.
13294 Returns the kind of class-key specified, or none_type to indicate
13297 static enum tag_types
13298 cp_parser_class_key (cp_parser* parser)
13301 enum tag_types tag_type;
13303 /* Look for the class-key. */
13304 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13308 /* Check to see if the TOKEN is a class-key. */
13309 tag_type = cp_parser_token_is_class_key (token);
13311 cp_parser_error (parser, "expected class-key");
13315 /* Parse an (optional) member-specification.
13317 member-specification:
13318 member-declaration member-specification [opt]
13319 access-specifier : member-specification [opt] */
13322 cp_parser_member_specification_opt (cp_parser* parser)
13329 /* Peek at the next token. */
13330 token = cp_lexer_peek_token (parser->lexer);
13331 /* If it's a `}', or EOF then we've seen all the members. */
13332 if (token->type == CPP_CLOSE_BRACE
13333 || token->type == CPP_EOF
13334 || token->type == CPP_PRAGMA_EOL)
13337 /* See if this token is a keyword. */
13338 keyword = token->keyword;
13342 case RID_PROTECTED:
13344 /* Consume the access-specifier. */
13345 cp_lexer_consume_token (parser->lexer);
13346 /* Remember which access-specifier is active. */
13347 current_access_specifier = token->value;
13348 /* Look for the `:'. */
13349 cp_parser_require (parser, CPP_COLON, "`:'");
13353 /* Accept #pragmas at class scope. */
13354 if (token->type == CPP_PRAGMA)
13356 cp_parser_pragma (parser, pragma_external);
13360 /* Otherwise, the next construction must be a
13361 member-declaration. */
13362 cp_parser_member_declaration (parser);
13367 /* Parse a member-declaration.
13369 member-declaration:
13370 decl-specifier-seq [opt] member-declarator-list [opt] ;
13371 function-definition ; [opt]
13372 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13374 template-declaration
13376 member-declarator-list:
13378 member-declarator-list , member-declarator
13381 declarator pure-specifier [opt]
13382 declarator constant-initializer [opt]
13383 identifier [opt] : constant-expression
13387 member-declaration:
13388 __extension__ member-declaration
13391 declarator attributes [opt] pure-specifier [opt]
13392 declarator attributes [opt] constant-initializer [opt]
13393 identifier [opt] attributes [opt] : constant-expression */
13396 cp_parser_member_declaration (cp_parser* parser)
13398 cp_decl_specifier_seq decl_specifiers;
13399 tree prefix_attributes;
13401 int declares_class_or_enum;
13404 int saved_pedantic;
13406 /* Check for the `__extension__' keyword. */
13407 if (cp_parser_extension_opt (parser, &saved_pedantic))
13410 cp_parser_member_declaration (parser);
13411 /* Restore the old value of the PEDANTIC flag. */
13412 pedantic = saved_pedantic;
13417 /* Check for a template-declaration. */
13418 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13420 /* An explicit specialization here is an error condition, and we
13421 expect the specialization handler to detect and report this. */
13422 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13423 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13424 cp_parser_explicit_specialization (parser);
13426 cp_parser_template_declaration (parser, /*member_p=*/true);
13431 /* Check for a using-declaration. */
13432 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13434 /* Parse the using-declaration. */
13435 cp_parser_using_declaration (parser);
13440 /* Check for @defs. */
13441 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13444 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13445 ivar = ivar_chains;
13449 ivar = TREE_CHAIN (member);
13450 TREE_CHAIN (member) = NULL_TREE;
13451 finish_member_declaration (member);
13456 /* Parse the decl-specifier-seq. */
13457 cp_parser_decl_specifier_seq (parser,
13458 CP_PARSER_FLAGS_OPTIONAL,
13460 &declares_class_or_enum);
13461 prefix_attributes = decl_specifiers.attributes;
13462 decl_specifiers.attributes = NULL_TREE;
13463 /* Check for an invalid type-name. */
13464 if (!decl_specifiers.type
13465 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13467 /* If there is no declarator, then the decl-specifier-seq should
13469 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13471 /* If there was no decl-specifier-seq, and the next token is a
13472 `;', then we have something like:
13478 Each member-declaration shall declare at least one member
13479 name of the class. */
13480 if (!decl_specifiers.any_specifiers_p)
13482 cp_token *token = cp_lexer_peek_token (parser->lexer);
13483 if (pedantic && !token->in_system_header)
13484 pedwarn ("%Hextra %<;%>", &token->location);
13490 /* See if this declaration is a friend. */
13491 friend_p = cp_parser_friend_p (&decl_specifiers);
13492 /* If there were decl-specifiers, check to see if there was
13493 a class-declaration. */
13494 type = check_tag_decl (&decl_specifiers);
13495 /* Nested classes have already been added to the class, but
13496 a `friend' needs to be explicitly registered. */
13499 /* If the `friend' keyword was present, the friend must
13500 be introduced with a class-key. */
13501 if (!declares_class_or_enum)
13502 error ("a class-key must be used when declaring a friend");
13505 template <typename T> struct A {
13506 friend struct A<T>::B;
13509 A<T>::B will be represented by a TYPENAME_TYPE, and
13510 therefore not recognized by check_tag_decl. */
13512 && decl_specifiers.type
13513 && TYPE_P (decl_specifiers.type))
13514 type = decl_specifiers.type;
13515 if (!type || !TYPE_P (type))
13516 error ("friend declaration does not name a class or "
13519 make_friend_class (current_class_type, type,
13520 /*complain=*/true);
13522 /* If there is no TYPE, an error message will already have
13524 else if (!type || type == error_mark_node)
13526 /* An anonymous aggregate has to be handled specially; such
13527 a declaration really declares a data member (with a
13528 particular type), as opposed to a nested class. */
13529 else if (ANON_AGGR_TYPE_P (type))
13531 /* Remove constructors and such from TYPE, now that we
13532 know it is an anonymous aggregate. */
13533 fixup_anonymous_aggr (type);
13534 /* And make the corresponding data member. */
13535 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13536 /* Add it to the class. */
13537 finish_member_declaration (decl);
13540 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13545 /* See if these declarations will be friends. */
13546 friend_p = cp_parser_friend_p (&decl_specifiers);
13548 /* Keep going until we hit the `;' at the end of the
13550 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13552 tree attributes = NULL_TREE;
13553 tree first_attribute;
13555 /* Peek at the next token. */
13556 token = cp_lexer_peek_token (parser->lexer);
13558 /* Check for a bitfield declaration. */
13559 if (token->type == CPP_COLON
13560 || (token->type == CPP_NAME
13561 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13567 /* Get the name of the bitfield. Note that we cannot just
13568 check TOKEN here because it may have been invalidated by
13569 the call to cp_lexer_peek_nth_token above. */
13570 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13571 identifier = cp_parser_identifier (parser);
13573 identifier = NULL_TREE;
13575 /* Consume the `:' token. */
13576 cp_lexer_consume_token (parser->lexer);
13577 /* Get the width of the bitfield. */
13579 = cp_parser_constant_expression (parser,
13580 /*allow_non_constant=*/false,
13583 /* Look for attributes that apply to the bitfield. */
13584 attributes = cp_parser_attributes_opt (parser);
13585 /* Remember which attributes are prefix attributes and
13587 first_attribute = attributes;
13588 /* Combine the attributes. */
13589 attributes = chainon (prefix_attributes, attributes);
13591 /* Create the bitfield declaration. */
13592 decl = grokbitfield (identifier
13593 ? make_id_declarator (NULL_TREE,
13599 /* Apply the attributes. */
13600 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13604 cp_declarator *declarator;
13606 tree asm_specification;
13607 int ctor_dtor_or_conv_p;
13609 /* Parse the declarator. */
13611 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13612 &ctor_dtor_or_conv_p,
13613 /*parenthesized_p=*/NULL,
13614 /*member_p=*/true);
13616 /* If something went wrong parsing the declarator, make sure
13617 that we at least consume some tokens. */
13618 if (declarator == cp_error_declarator)
13620 /* Skip to the end of the statement. */
13621 cp_parser_skip_to_end_of_statement (parser);
13622 /* If the next token is not a semicolon, that is
13623 probably because we just skipped over the body of
13624 a function. So, we consume a semicolon if
13625 present, but do not issue an error message if it
13627 if (cp_lexer_next_token_is (parser->lexer,
13629 cp_lexer_consume_token (parser->lexer);
13633 if (declares_class_or_enum & 2)
13634 cp_parser_check_for_definition_in_return_type
13635 (declarator, decl_specifiers.type);
13637 /* Look for an asm-specification. */
13638 asm_specification = cp_parser_asm_specification_opt (parser);
13639 /* Look for attributes that apply to the declaration. */
13640 attributes = cp_parser_attributes_opt (parser);
13641 /* Remember which attributes are prefix attributes and
13643 first_attribute = attributes;
13644 /* Combine the attributes. */
13645 attributes = chainon (prefix_attributes, attributes);
13647 /* If it's an `=', then we have a constant-initializer or a
13648 pure-specifier. It is not correct to parse the
13649 initializer before registering the member declaration
13650 since the member declaration should be in scope while
13651 its initializer is processed. However, the rest of the
13652 front end does not yet provide an interface that allows
13653 us to handle this correctly. */
13654 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13658 A pure-specifier shall be used only in the declaration of
13659 a virtual function.
13661 A member-declarator can contain a constant-initializer
13662 only if it declares a static member of integral or
13665 Therefore, if the DECLARATOR is for a function, we look
13666 for a pure-specifier; otherwise, we look for a
13667 constant-initializer. When we call `grokfield', it will
13668 perform more stringent semantics checks. */
13669 if (declarator->kind == cdk_function)
13670 initializer = cp_parser_pure_specifier (parser);
13672 /* Parse the initializer. */
13673 initializer = cp_parser_constant_initializer (parser);
13675 /* Otherwise, there is no initializer. */
13677 initializer = NULL_TREE;
13679 /* See if we are probably looking at a function
13680 definition. We are certainly not looking at a
13681 member-declarator. Calling `grokfield' has
13682 side-effects, so we must not do it unless we are sure
13683 that we are looking at a member-declarator. */
13684 if (cp_parser_token_starts_function_definition_p
13685 (cp_lexer_peek_token (parser->lexer)))
13687 /* The grammar does not allow a pure-specifier to be
13688 used when a member function is defined. (It is
13689 possible that this fact is an oversight in the
13690 standard, since a pure function may be defined
13691 outside of the class-specifier. */
13693 error ("pure-specifier on function-definition");
13694 decl = cp_parser_save_member_function_body (parser,
13698 /* If the member was not a friend, declare it here. */
13700 finish_member_declaration (decl);
13701 /* Peek at the next token. */
13702 token = cp_lexer_peek_token (parser->lexer);
13703 /* If the next token is a semicolon, consume it. */
13704 if (token->type == CPP_SEMICOLON)
13705 cp_lexer_consume_token (parser->lexer);
13709 /* Create the declaration. */
13710 decl = grokfield (declarator, &decl_specifiers,
13711 initializer, /*init_const_expr_p=*/true,
13716 /* Reset PREFIX_ATTRIBUTES. */
13717 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13718 attributes = TREE_CHAIN (attributes);
13720 TREE_CHAIN (attributes) = NULL_TREE;
13722 /* If there is any qualification still in effect, clear it
13723 now; we will be starting fresh with the next declarator. */
13724 parser->scope = NULL_TREE;
13725 parser->qualifying_scope = NULL_TREE;
13726 parser->object_scope = NULL_TREE;
13727 /* If it's a `,', then there are more declarators. */
13728 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13729 cp_lexer_consume_token (parser->lexer);
13730 /* If the next token isn't a `;', then we have a parse error. */
13731 else if (cp_lexer_next_token_is_not (parser->lexer,
13734 cp_parser_error (parser, "expected %<;%>");
13735 /* Skip tokens until we find a `;'. */
13736 cp_parser_skip_to_end_of_statement (parser);
13743 /* Add DECL to the list of members. */
13745 finish_member_declaration (decl);
13747 if (TREE_CODE (decl) == FUNCTION_DECL)
13748 cp_parser_save_default_args (parser, decl);
13753 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13756 /* Parse a pure-specifier.
13761 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13762 Otherwise, ERROR_MARK_NODE is returned. */
13765 cp_parser_pure_specifier (cp_parser* parser)
13769 /* Look for the `=' token. */
13770 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13771 return error_mark_node;
13772 /* Look for the `0' token. */
13773 token = cp_lexer_consume_token (parser->lexer);
13774 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13775 if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13776 return integer_zero_node;
13778 cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13779 cp_parser_skip_to_end_of_statement (parser);
13780 return error_mark_node;
13783 /* Parse a constant-initializer.
13785 constant-initializer:
13786 = constant-expression
13788 Returns a representation of the constant-expression. */
13791 cp_parser_constant_initializer (cp_parser* parser)
13793 /* Look for the `=' token. */
13794 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13795 return error_mark_node;
13797 /* It is invalid to write:
13799 struct S { static const int i = { 7 }; };
13802 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13804 cp_parser_error (parser,
13805 "a brace-enclosed initializer is not allowed here");
13806 /* Consume the opening brace. */
13807 cp_lexer_consume_token (parser->lexer);
13808 /* Skip the initializer. */
13809 cp_parser_skip_to_closing_brace (parser);
13810 /* Look for the trailing `}'. */
13811 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13813 return error_mark_node;
13816 return cp_parser_constant_expression (parser,
13817 /*allow_non_constant=*/false,
13821 /* Derived classes [gram.class.derived] */
13823 /* Parse a base-clause.
13826 : base-specifier-list
13828 base-specifier-list:
13830 base-specifier-list , base-specifier
13832 Returns a TREE_LIST representing the base-classes, in the order in
13833 which they were declared. The representation of each node is as
13834 described by cp_parser_base_specifier.
13836 In the case that no bases are specified, this function will return
13837 NULL_TREE, not ERROR_MARK_NODE. */
13840 cp_parser_base_clause (cp_parser* parser)
13842 tree bases = NULL_TREE;
13844 /* Look for the `:' that begins the list. */
13845 cp_parser_require (parser, CPP_COLON, "`:'");
13847 /* Scan the base-specifier-list. */
13853 /* Look for the base-specifier. */
13854 base = cp_parser_base_specifier (parser);
13855 /* Add BASE to the front of the list. */
13856 if (base != error_mark_node)
13858 TREE_CHAIN (base) = bases;
13861 /* Peek at the next token. */
13862 token = cp_lexer_peek_token (parser->lexer);
13863 /* If it's not a comma, then the list is complete. */
13864 if (token->type != CPP_COMMA)
13866 /* Consume the `,'. */
13867 cp_lexer_consume_token (parser->lexer);
13870 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13871 base class had a qualified name. However, the next name that
13872 appears is certainly not qualified. */
13873 parser->scope = NULL_TREE;
13874 parser->qualifying_scope = NULL_TREE;
13875 parser->object_scope = NULL_TREE;
13877 return nreverse (bases);
13880 /* Parse a base-specifier.
13883 :: [opt] nested-name-specifier [opt] class-name
13884 virtual access-specifier [opt] :: [opt] nested-name-specifier
13886 access-specifier virtual [opt] :: [opt] nested-name-specifier
13889 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13890 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13891 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13892 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13895 cp_parser_base_specifier (cp_parser* parser)
13899 bool virtual_p = false;
13900 bool duplicate_virtual_error_issued_p = false;
13901 bool duplicate_access_error_issued_p = false;
13902 bool class_scope_p, template_p;
13903 tree access = access_default_node;
13906 /* Process the optional `virtual' and `access-specifier'. */
13909 /* Peek at the next token. */
13910 token = cp_lexer_peek_token (parser->lexer);
13911 /* Process `virtual'. */
13912 switch (token->keyword)
13915 /* If `virtual' appears more than once, issue an error. */
13916 if (virtual_p && !duplicate_virtual_error_issued_p)
13918 cp_parser_error (parser,
13919 "%<virtual%> specified more than once in base-specified");
13920 duplicate_virtual_error_issued_p = true;
13925 /* Consume the `virtual' token. */
13926 cp_lexer_consume_token (parser->lexer);
13931 case RID_PROTECTED:
13933 /* If more than one access specifier appears, issue an
13935 if (access != access_default_node
13936 && !duplicate_access_error_issued_p)
13938 cp_parser_error (parser,
13939 "more than one access specifier in base-specified");
13940 duplicate_access_error_issued_p = true;
13943 access = ridpointers[(int) token->keyword];
13945 /* Consume the access-specifier. */
13946 cp_lexer_consume_token (parser->lexer);
13955 /* It is not uncommon to see programs mechanically, erroneously, use
13956 the 'typename' keyword to denote (dependent) qualified types
13957 as base classes. */
13958 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13960 if (!processing_template_decl)
13961 error ("keyword %<typename%> not allowed outside of templates");
13963 error ("keyword %<typename%> not allowed in this context "
13964 "(the base class is implicitly a type)");
13965 cp_lexer_consume_token (parser->lexer);
13968 /* Look for the optional `::' operator. */
13969 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13970 /* Look for the nested-name-specifier. The simplest way to
13975 The keyword `typename' is not permitted in a base-specifier or
13976 mem-initializer; in these contexts a qualified name that
13977 depends on a template-parameter is implicitly assumed to be a
13980 is to pretend that we have seen the `typename' keyword at this
13982 cp_parser_nested_name_specifier_opt (parser,
13983 /*typename_keyword_p=*/true,
13984 /*check_dependency_p=*/true,
13986 /*is_declaration=*/true);
13987 /* If the base class is given by a qualified name, assume that names
13988 we see are type names or templates, as appropriate. */
13989 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13990 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13992 /* Finally, look for the class-name. */
13993 type = cp_parser_class_name (parser,
13997 /*check_dependency_p=*/true,
13998 /*class_head_p=*/false,
13999 /*is_declaration=*/true);
14001 if (type == error_mark_node)
14002 return error_mark_node;
14004 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14007 /* Exception handling [gram.exception] */
14009 /* Parse an (optional) exception-specification.
14011 exception-specification:
14012 throw ( type-id-list [opt] )
14014 Returns a TREE_LIST representing the exception-specification. The
14015 TREE_VALUE of each node is a type. */
14018 cp_parser_exception_specification_opt (cp_parser* parser)
14023 /* Peek at the next token. */
14024 token = cp_lexer_peek_token (parser->lexer);
14025 /* If it's not `throw', then there's no exception-specification. */
14026 if (!cp_parser_is_keyword (token, RID_THROW))
14029 /* Consume the `throw'. */
14030 cp_lexer_consume_token (parser->lexer);
14032 /* Look for the `('. */
14033 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14035 /* Peek at the next token. */
14036 token = cp_lexer_peek_token (parser->lexer);
14037 /* If it's not a `)', then there is a type-id-list. */
14038 if (token->type != CPP_CLOSE_PAREN)
14040 const char *saved_message;
14042 /* Types may not be defined in an exception-specification. */
14043 saved_message = parser->type_definition_forbidden_message;
14044 parser->type_definition_forbidden_message
14045 = "types may not be defined in an exception-specification";
14046 /* Parse the type-id-list. */
14047 type_id_list = cp_parser_type_id_list (parser);
14048 /* Restore the saved message. */
14049 parser->type_definition_forbidden_message = saved_message;
14052 type_id_list = empty_except_spec;
14054 /* Look for the `)'. */
14055 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14057 return type_id_list;
14060 /* Parse an (optional) type-id-list.
14064 type-id-list , type-id
14066 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14067 in the order that the types were presented. */
14070 cp_parser_type_id_list (cp_parser* parser)
14072 tree types = NULL_TREE;
14079 /* Get the next type-id. */
14080 type = cp_parser_type_id (parser);
14081 /* Add it to the list. */
14082 types = add_exception_specifier (types, type, /*complain=*/1);
14083 /* Peek at the next token. */
14084 token = cp_lexer_peek_token (parser->lexer);
14085 /* If it is not a `,', we are done. */
14086 if (token->type != CPP_COMMA)
14088 /* Consume the `,'. */
14089 cp_lexer_consume_token (parser->lexer);
14092 return nreverse (types);
14095 /* Parse a try-block.
14098 try compound-statement handler-seq */
14101 cp_parser_try_block (cp_parser* parser)
14105 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14106 try_block = begin_try_block ();
14107 cp_parser_compound_statement (parser, NULL, true);
14108 finish_try_block (try_block);
14109 cp_parser_handler_seq (parser);
14110 finish_handler_sequence (try_block);
14115 /* Parse a function-try-block.
14117 function-try-block:
14118 try ctor-initializer [opt] function-body handler-seq */
14121 cp_parser_function_try_block (cp_parser* parser)
14124 bool ctor_initializer_p;
14126 /* Look for the `try' keyword. */
14127 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14129 /* Let the rest of the front-end know where we are. */
14130 try_block = begin_function_try_block ();
14131 /* Parse the function-body. */
14133 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14134 /* We're done with the `try' part. */
14135 finish_function_try_block (try_block);
14136 /* Parse the handlers. */
14137 cp_parser_handler_seq (parser);
14138 /* We're done with the handlers. */
14139 finish_function_handler_sequence (try_block);
14141 return ctor_initializer_p;
14144 /* Parse a handler-seq.
14147 handler handler-seq [opt] */
14150 cp_parser_handler_seq (cp_parser* parser)
14156 /* Parse the handler. */
14157 cp_parser_handler (parser);
14158 /* Peek at the next token. */
14159 token = cp_lexer_peek_token (parser->lexer);
14160 /* If it's not `catch' then there are no more handlers. */
14161 if (!cp_parser_is_keyword (token, RID_CATCH))
14166 /* Parse a handler.
14169 catch ( exception-declaration ) compound-statement */
14172 cp_parser_handler (cp_parser* parser)
14177 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14178 handler = begin_handler ();
14179 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14180 declaration = cp_parser_exception_declaration (parser);
14181 finish_handler_parms (declaration, handler);
14182 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14183 cp_parser_compound_statement (parser, NULL, false);
14184 finish_handler (handler);
14187 /* Parse an exception-declaration.
14189 exception-declaration:
14190 type-specifier-seq declarator
14191 type-specifier-seq abstract-declarator
14195 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14196 ellipsis variant is used. */
14199 cp_parser_exception_declaration (cp_parser* parser)
14202 cp_decl_specifier_seq type_specifiers;
14203 cp_declarator *declarator;
14204 const char *saved_message;
14206 /* If it's an ellipsis, it's easy to handle. */
14207 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14209 /* Consume the `...' token. */
14210 cp_lexer_consume_token (parser->lexer);
14214 /* Types may not be defined in exception-declarations. */
14215 saved_message = parser->type_definition_forbidden_message;
14216 parser->type_definition_forbidden_message
14217 = "types may not be defined in exception-declarations";
14219 /* Parse the type-specifier-seq. */
14220 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14222 /* If it's a `)', then there is no declarator. */
14223 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14226 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14227 /*ctor_dtor_or_conv_p=*/NULL,
14228 /*parenthesized_p=*/NULL,
14229 /*member_p=*/false);
14231 /* Restore the saved message. */
14232 parser->type_definition_forbidden_message = saved_message;
14234 if (type_specifiers.any_specifiers_p)
14236 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14237 if (decl == NULL_TREE)
14238 error ("invalid catch parameter");
14246 /* Parse a throw-expression.
14249 throw assignment-expression [opt]
14251 Returns a THROW_EXPR representing the throw-expression. */
14254 cp_parser_throw_expression (cp_parser* parser)
14259 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14260 token = cp_lexer_peek_token (parser->lexer);
14261 /* Figure out whether or not there is an assignment-expression
14262 following the "throw" keyword. */
14263 if (token->type == CPP_COMMA
14264 || token->type == CPP_SEMICOLON
14265 || token->type == CPP_CLOSE_PAREN
14266 || token->type == CPP_CLOSE_SQUARE
14267 || token->type == CPP_CLOSE_BRACE
14268 || token->type == CPP_COLON)
14269 expression = NULL_TREE;
14271 expression = cp_parser_assignment_expression (parser,
14274 return build_throw (expression);
14277 /* GNU Extensions */
14279 /* Parse an (optional) asm-specification.
14282 asm ( string-literal )
14284 If the asm-specification is present, returns a STRING_CST
14285 corresponding to the string-literal. Otherwise, returns
14289 cp_parser_asm_specification_opt (cp_parser* parser)
14292 tree asm_specification;
14294 /* Peek at the next token. */
14295 token = cp_lexer_peek_token (parser->lexer);
14296 /* If the next token isn't the `asm' keyword, then there's no
14297 asm-specification. */
14298 if (!cp_parser_is_keyword (token, RID_ASM))
14301 /* Consume the `asm' token. */
14302 cp_lexer_consume_token (parser->lexer);
14303 /* Look for the `('. */
14304 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14306 /* Look for the string-literal. */
14307 asm_specification = cp_parser_string_literal (parser, false, false);
14309 /* Look for the `)'. */
14310 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14312 return asm_specification;
14315 /* Parse an asm-operand-list.
14319 asm-operand-list , asm-operand
14322 string-literal ( expression )
14323 [ string-literal ] string-literal ( expression )
14325 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14326 each node is the expression. The TREE_PURPOSE is itself a
14327 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14328 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14329 is a STRING_CST for the string literal before the parenthesis. */
14332 cp_parser_asm_operand_list (cp_parser* parser)
14334 tree asm_operands = NULL_TREE;
14338 tree string_literal;
14342 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14344 /* Consume the `[' token. */
14345 cp_lexer_consume_token (parser->lexer);
14346 /* Read the operand name. */
14347 name = cp_parser_identifier (parser);
14348 if (name != error_mark_node)
14349 name = build_string (IDENTIFIER_LENGTH (name),
14350 IDENTIFIER_POINTER (name));
14351 /* Look for the closing `]'. */
14352 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14356 /* Look for the string-literal. */
14357 string_literal = cp_parser_string_literal (parser, false, false);
14359 /* Look for the `('. */
14360 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14361 /* Parse the expression. */
14362 expression = cp_parser_expression (parser, /*cast_p=*/false);
14363 /* Look for the `)'. */
14364 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14366 /* Add this operand to the list. */
14367 asm_operands = tree_cons (build_tree_list (name, string_literal),
14370 /* If the next token is not a `,', there are no more
14372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14374 /* Consume the `,'. */
14375 cp_lexer_consume_token (parser->lexer);
14378 return nreverse (asm_operands);
14381 /* Parse an asm-clobber-list.
14385 asm-clobber-list , string-literal
14387 Returns a TREE_LIST, indicating the clobbers in the order that they
14388 appeared. The TREE_VALUE of each node is a STRING_CST. */
14391 cp_parser_asm_clobber_list (cp_parser* parser)
14393 tree clobbers = NULL_TREE;
14397 tree string_literal;
14399 /* Look for the string literal. */
14400 string_literal = cp_parser_string_literal (parser, false, false);
14401 /* Add it to the list. */
14402 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14403 /* If the next token is not a `,', then the list is
14405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14407 /* Consume the `,' token. */
14408 cp_lexer_consume_token (parser->lexer);
14414 /* Parse an (optional) series of attributes.
14417 attributes attribute
14420 __attribute__ (( attribute-list [opt] ))
14422 The return value is as for cp_parser_attribute_list. */
14425 cp_parser_attributes_opt (cp_parser* parser)
14427 tree attributes = NULL_TREE;
14432 tree attribute_list;
14434 /* Peek at the next token. */
14435 token = cp_lexer_peek_token (parser->lexer);
14436 /* If it's not `__attribute__', then we're done. */
14437 if (token->keyword != RID_ATTRIBUTE)
14440 /* Consume the `__attribute__' keyword. */
14441 cp_lexer_consume_token (parser->lexer);
14442 /* Look for the two `(' tokens. */
14443 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14444 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14446 /* Peek at the next token. */
14447 token = cp_lexer_peek_token (parser->lexer);
14448 if (token->type != CPP_CLOSE_PAREN)
14449 /* Parse the attribute-list. */
14450 attribute_list = cp_parser_attribute_list (parser);
14452 /* If the next token is a `)', then there is no attribute
14454 attribute_list = NULL;
14456 /* Look for the two `)' tokens. */
14457 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14458 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14460 /* Add these new attributes to the list. */
14461 attributes = chainon (attributes, attribute_list);
14467 /* Parse an attribute-list.
14471 attribute-list , attribute
14475 identifier ( identifier )
14476 identifier ( identifier , expression-list )
14477 identifier ( expression-list )
14479 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14480 to an attribute. The TREE_PURPOSE of each node is the identifier
14481 indicating which attribute is in use. The TREE_VALUE represents
14482 the arguments, if any. */
14485 cp_parser_attribute_list (cp_parser* parser)
14487 tree attribute_list = NULL_TREE;
14488 bool save_translate_strings_p = parser->translate_strings_p;
14490 parser->translate_strings_p = false;
14497 /* Look for the identifier. We also allow keywords here; for
14498 example `__attribute__ ((const))' is legal. */
14499 token = cp_lexer_peek_token (parser->lexer);
14500 if (token->type == CPP_NAME
14501 || token->type == CPP_KEYWORD)
14503 /* Consume the token. */
14504 token = cp_lexer_consume_token (parser->lexer);
14506 /* Save away the identifier that indicates which attribute
14508 identifier = token->value;
14509 attribute = build_tree_list (identifier, NULL_TREE);
14511 /* Peek at the next token. */
14512 token = cp_lexer_peek_token (parser->lexer);
14513 /* If it's an `(', then parse the attribute arguments. */
14514 if (token->type == CPP_OPEN_PAREN)
14518 arguments = (cp_parser_parenthesized_expression_list
14519 (parser, true, /*cast_p=*/false,
14520 /*non_constant_p=*/NULL));
14521 /* Save the identifier and arguments away. */
14522 TREE_VALUE (attribute) = arguments;
14525 /* Add this attribute to the list. */
14526 TREE_CHAIN (attribute) = attribute_list;
14527 attribute_list = attribute;
14529 token = cp_lexer_peek_token (parser->lexer);
14531 /* Now, look for more attributes. If the next token isn't a
14532 `,', we're done. */
14533 if (token->type != CPP_COMMA)
14536 /* Consume the comma and keep going. */
14537 cp_lexer_consume_token (parser->lexer);
14539 parser->translate_strings_p = save_translate_strings_p;
14541 /* We built up the list in reverse order. */
14542 return nreverse (attribute_list);
14545 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14546 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14547 current value of the PEDANTIC flag, regardless of whether or not
14548 the `__extension__' keyword is present. The caller is responsible
14549 for restoring the value of the PEDANTIC flag. */
14552 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14554 /* Save the old value of the PEDANTIC flag. */
14555 *saved_pedantic = pedantic;
14557 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14559 /* Consume the `__extension__' token. */
14560 cp_lexer_consume_token (parser->lexer);
14561 /* We're not being pedantic while the `__extension__' keyword is
14571 /* Parse a label declaration.
14574 __label__ label-declarator-seq ;
14576 label-declarator-seq:
14577 identifier , label-declarator-seq
14581 cp_parser_label_declaration (cp_parser* parser)
14583 /* Look for the `__label__' keyword. */
14584 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14590 /* Look for an identifier. */
14591 identifier = cp_parser_identifier (parser);
14592 /* If we failed, stop. */
14593 if (identifier == error_mark_node)
14595 /* Declare it as a label. */
14596 finish_label_decl (identifier);
14597 /* If the next token is a `;', stop. */
14598 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14600 /* Look for the `,' separating the label declarations. */
14601 cp_parser_require (parser, CPP_COMMA, "`,'");
14604 /* Look for the final `;'. */
14605 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14608 /* Support Functions */
14610 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14611 NAME should have one of the representations used for an
14612 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14613 is returned. If PARSER->SCOPE is a dependent type, then a
14614 SCOPE_REF is returned.
14616 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14617 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14618 was formed. Abstractly, such entities should not be passed to this
14619 function, because they do not need to be looked up, but it is
14620 simpler to check for this special case here, rather than at the
14623 In cases not explicitly covered above, this function returns a
14624 DECL, OVERLOAD, or baselink representing the result of the lookup.
14625 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14628 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14629 (e.g., "struct") that was used. In that case bindings that do not
14630 refer to types are ignored.
14632 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14635 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14638 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14641 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14642 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14643 NULL_TREE otherwise. */
14646 cp_parser_lookup_name (cp_parser *parser, tree name,
14647 enum tag_types tag_type,
14650 bool check_dependency,
14651 tree *ambiguous_decls)
14655 tree object_type = parser->context->object_type;
14657 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14658 flags |= LOOKUP_COMPLAIN;
14660 /* Assume that the lookup will be unambiguous. */
14661 if (ambiguous_decls)
14662 *ambiguous_decls = NULL_TREE;
14664 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14665 no longer valid. Note that if we are parsing tentatively, and
14666 the parse fails, OBJECT_TYPE will be automatically restored. */
14667 parser->context->object_type = NULL_TREE;
14669 if (name == error_mark_node)
14670 return error_mark_node;
14672 /* A template-id has already been resolved; there is no lookup to
14674 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14676 if (BASELINK_P (name))
14678 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14679 == TEMPLATE_ID_EXPR);
14683 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14684 it should already have been checked to make sure that the name
14685 used matches the type being destroyed. */
14686 if (TREE_CODE (name) == BIT_NOT_EXPR)
14690 /* Figure out to which type this destructor applies. */
14692 type = parser->scope;
14693 else if (object_type)
14694 type = object_type;
14696 type = current_class_type;
14697 /* If that's not a class type, there is no destructor. */
14698 if (!type || !CLASS_TYPE_P (type))
14699 return error_mark_node;
14700 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14701 lazily_declare_fn (sfk_destructor, type);
14702 if (!CLASSTYPE_DESTRUCTORS (type))
14703 return error_mark_node;
14704 /* If it was a class type, return the destructor. */
14705 return CLASSTYPE_DESTRUCTORS (type);
14708 /* By this point, the NAME should be an ordinary identifier. If
14709 the id-expression was a qualified name, the qualifying scope is
14710 stored in PARSER->SCOPE at this point. */
14711 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14713 /* Perform the lookup. */
14718 if (parser->scope == error_mark_node)
14719 return error_mark_node;
14721 /* If the SCOPE is dependent, the lookup must be deferred until
14722 the template is instantiated -- unless we are explicitly
14723 looking up names in uninstantiated templates. Even then, we
14724 cannot look up the name if the scope is not a class type; it
14725 might, for example, be a template type parameter. */
14726 dependent_p = (TYPE_P (parser->scope)
14727 && !(parser->in_declarator_p
14728 && currently_open_class (parser->scope))
14729 && dependent_type_p (parser->scope));
14730 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14737 /* The resolution to Core Issue 180 says that `struct
14738 A::B' should be considered a type-name, even if `A'
14740 type = make_typename_type (parser->scope, name, tag_type,
14741 /*complain=*/tf_error);
14742 decl = TYPE_NAME (type);
14744 else if (is_template
14745 && (cp_parser_next_token_ends_template_argument_p (parser)
14746 || cp_lexer_next_token_is (parser->lexer,
14748 decl = make_unbound_class_template (parser->scope,
14750 /*complain=*/tf_error);
14752 decl = build_qualified_name (/*type=*/NULL_TREE,
14753 parser->scope, name,
14758 tree pushed_scope = NULL_TREE;
14760 /* If PARSER->SCOPE is a dependent type, then it must be a
14761 class type, and we must not be checking dependencies;
14762 otherwise, we would have processed this lookup above. So
14763 that PARSER->SCOPE is not considered a dependent base by
14764 lookup_member, we must enter the scope here. */
14766 pushed_scope = push_scope (parser->scope);
14767 /* If the PARSER->SCOPE is a template specialization, it
14768 may be instantiated during name lookup. In that case,
14769 errors may be issued. Even if we rollback the current
14770 tentative parse, those errors are valid. */
14771 decl = lookup_qualified_name (parser->scope, name,
14772 tag_type != none_type,
14773 /*complain=*/true);
14775 pop_scope (pushed_scope);
14777 parser->qualifying_scope = parser->scope;
14778 parser->object_scope = NULL_TREE;
14780 else if (object_type)
14782 tree object_decl = NULL_TREE;
14783 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14784 OBJECT_TYPE is not a class. */
14785 if (CLASS_TYPE_P (object_type))
14786 /* If the OBJECT_TYPE is a template specialization, it may
14787 be instantiated during name lookup. In that case, errors
14788 may be issued. Even if we rollback the current tentative
14789 parse, those errors are valid. */
14790 object_decl = lookup_member (object_type,
14793 tag_type != none_type);
14794 /* Look it up in the enclosing context, too. */
14795 decl = lookup_name_real (name, tag_type != none_type,
14797 /*block_p=*/true, is_namespace, flags);
14798 parser->object_scope = object_type;
14799 parser->qualifying_scope = NULL_TREE;
14801 decl = object_decl;
14805 decl = lookup_name_real (name, tag_type != none_type,
14807 /*block_p=*/true, is_namespace, flags);
14808 parser->qualifying_scope = NULL_TREE;
14809 parser->object_scope = NULL_TREE;
14812 /* If the lookup failed, let our caller know. */
14813 if (!decl || decl == error_mark_node)
14814 return error_mark_node;
14816 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14817 if (TREE_CODE (decl) == TREE_LIST)
14819 if (ambiguous_decls)
14820 *ambiguous_decls = decl;
14821 /* The error message we have to print is too complicated for
14822 cp_parser_error, so we incorporate its actions directly. */
14823 if (!cp_parser_simulate_error (parser))
14825 error ("reference to %qD is ambiguous", name);
14826 print_candidates (decl);
14828 return error_mark_node;
14831 gcc_assert (DECL_P (decl)
14832 || TREE_CODE (decl) == OVERLOAD
14833 || TREE_CODE (decl) == SCOPE_REF
14834 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14835 || BASELINK_P (decl));
14837 /* If we have resolved the name of a member declaration, check to
14838 see if the declaration is accessible. When the name resolves to
14839 set of overloaded functions, accessibility is checked when
14840 overload resolution is done.
14842 During an explicit instantiation, access is not checked at all,
14843 as per [temp.explicit]. */
14845 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14850 /* Like cp_parser_lookup_name, but for use in the typical case where
14851 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14852 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14855 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14857 return cp_parser_lookup_name (parser, name,
14859 /*is_template=*/false,
14860 /*is_namespace=*/false,
14861 /*check_dependency=*/true,
14862 /*ambiguous_decls=*/NULL);
14865 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14866 the current context, return the TYPE_DECL. If TAG_NAME_P is
14867 true, the DECL indicates the class being defined in a class-head,
14868 or declared in an elaborated-type-specifier.
14870 Otherwise, return DECL. */
14873 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14875 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14876 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14879 template <typename T> struct B;
14882 template <typename T> struct A::B {};
14884 Similarly, in an elaborated-type-specifier:
14886 namespace N { struct X{}; }
14889 template <typename T> friend struct N::X;
14892 However, if the DECL refers to a class type, and we are in
14893 the scope of the class, then the name lookup automatically
14894 finds the TYPE_DECL created by build_self_reference rather
14895 than a TEMPLATE_DECL. For example, in:
14897 template <class T> struct S {
14901 there is no need to handle such case. */
14903 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14904 return DECL_TEMPLATE_RESULT (decl);
14909 /* If too many, or too few, template-parameter lists apply to the
14910 declarator, issue an error message. Returns TRUE if all went well,
14911 and FALSE otherwise. */
14914 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14915 cp_declarator *declarator)
14917 unsigned num_templates;
14919 /* We haven't seen any classes that involve template parameters yet. */
14922 switch (declarator->kind)
14925 if (declarator->u.id.qualifying_scope)
14930 scope = declarator->u.id.qualifying_scope;
14931 member = declarator->u.id.unqualified_name;
14933 while (scope && CLASS_TYPE_P (scope))
14935 /* You're supposed to have one `template <...>'
14936 for every template class, but you don't need one
14937 for a full specialization. For example:
14939 template <class T> struct S{};
14940 template <> struct S<int> { void f(); };
14941 void S<int>::f () {}
14943 is correct; there shouldn't be a `template <>' for
14944 the definition of `S<int>::f'. */
14945 if (CLASSTYPE_TEMPLATE_INFO (scope)
14946 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14947 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14948 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14951 scope = TYPE_CONTEXT (scope);
14954 else if (TREE_CODE (declarator->u.id.unqualified_name)
14955 == TEMPLATE_ID_EXPR)
14956 /* If the DECLARATOR has the form `X<y>' then it uses one
14957 additional level of template parameters. */
14960 return cp_parser_check_template_parameters (parser,
14966 case cdk_reference:
14968 return (cp_parser_check_declarator_template_parameters
14969 (parser, declarator->declarator));
14975 gcc_unreachable ();
14980 /* NUM_TEMPLATES were used in the current declaration. If that is
14981 invalid, return FALSE and issue an error messages. Otherwise,
14985 cp_parser_check_template_parameters (cp_parser* parser,
14986 unsigned num_templates)
14988 /* If there are more template classes than parameter lists, we have
14991 template <class T> void S<T>::R<T>::f (); */
14992 if (parser->num_template_parameter_lists < num_templates)
14994 error ("too few template-parameter-lists");
14997 /* If there are the same number of template classes and parameter
14998 lists, that's OK. */
14999 if (parser->num_template_parameter_lists == num_templates)
15001 /* If there are more, but only one more, then we are referring to a
15002 member template. That's OK too. */
15003 if (parser->num_template_parameter_lists == num_templates + 1)
15005 /* Otherwise, there are too many template parameter lists. We have
15008 template <class T> template <class U> void S::f(); */
15009 error ("too many template-parameter-lists");
15013 /* Parse an optional `::' token indicating that the following name is
15014 from the global namespace. If so, PARSER->SCOPE is set to the
15015 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15016 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15017 Returns the new value of PARSER->SCOPE, if the `::' token is
15018 present, and NULL_TREE otherwise. */
15021 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15025 /* Peek at the next token. */
15026 token = cp_lexer_peek_token (parser->lexer);
15027 /* If we're looking at a `::' token then we're starting from the
15028 global namespace, not our current location. */
15029 if (token->type == CPP_SCOPE)
15031 /* Consume the `::' token. */
15032 cp_lexer_consume_token (parser->lexer);
15033 /* Set the SCOPE so that we know where to start the lookup. */
15034 parser->scope = global_namespace;
15035 parser->qualifying_scope = global_namespace;
15036 parser->object_scope = NULL_TREE;
15038 return parser->scope;
15040 else if (!current_scope_valid_p)
15042 parser->scope = NULL_TREE;
15043 parser->qualifying_scope = NULL_TREE;
15044 parser->object_scope = NULL_TREE;
15050 /* Returns TRUE if the upcoming token sequence is the start of a
15051 constructor declarator. If FRIEND_P is true, the declarator is
15052 preceded by the `friend' specifier. */
15055 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15057 bool constructor_p;
15058 tree type_decl = NULL_TREE;
15059 bool nested_name_p;
15060 cp_token *next_token;
15062 /* The common case is that this is not a constructor declarator, so
15063 try to avoid doing lots of work if at all possible. It's not
15064 valid declare a constructor at function scope. */
15065 if (at_function_scope_p ())
15067 /* And only certain tokens can begin a constructor declarator. */
15068 next_token = cp_lexer_peek_token (parser->lexer);
15069 if (next_token->type != CPP_NAME
15070 && next_token->type != CPP_SCOPE
15071 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15072 && next_token->type != CPP_TEMPLATE_ID)
15075 /* Parse tentatively; we are going to roll back all of the tokens
15077 cp_parser_parse_tentatively (parser);
15078 /* Assume that we are looking at a constructor declarator. */
15079 constructor_p = true;
15081 /* Look for the optional `::' operator. */
15082 cp_parser_global_scope_opt (parser,
15083 /*current_scope_valid_p=*/false);
15084 /* Look for the nested-name-specifier. */
15086 = (cp_parser_nested_name_specifier_opt (parser,
15087 /*typename_keyword_p=*/false,
15088 /*check_dependency_p=*/false,
15090 /*is_declaration=*/false)
15092 /* Outside of a class-specifier, there must be a
15093 nested-name-specifier. */
15094 if (!nested_name_p &&
15095 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15097 constructor_p = false;
15098 /* If we still think that this might be a constructor-declarator,
15099 look for a class-name. */
15104 template <typename T> struct S { S(); };
15105 template <typename T> S<T>::S ();
15107 we must recognize that the nested `S' names a class.
15110 template <typename T> S<T>::S<T> ();
15112 we must recognize that the nested `S' names a template. */
15113 type_decl = cp_parser_class_name (parser,
15114 /*typename_keyword_p=*/false,
15115 /*template_keyword_p=*/false,
15117 /*check_dependency_p=*/false,
15118 /*class_head_p=*/false,
15119 /*is_declaration=*/false);
15120 /* If there was no class-name, then this is not a constructor. */
15121 constructor_p = !cp_parser_error_occurred (parser);
15124 /* If we're still considering a constructor, we have to see a `(',
15125 to begin the parameter-declaration-clause, followed by either a
15126 `)', an `...', or a decl-specifier. We need to check for a
15127 type-specifier to avoid being fooled into thinking that:
15131 is a constructor. (It is actually a function named `f' that
15132 takes one parameter (of type `int') and returns a value of type
15135 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15137 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15138 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15139 /* A parameter declaration begins with a decl-specifier,
15140 which is either the "attribute" keyword, a storage class
15141 specifier, or (usually) a type-specifier. */
15142 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15143 && !cp_parser_storage_class_specifier_opt (parser))
15146 tree pushed_scope = NULL_TREE;
15147 unsigned saved_num_template_parameter_lists;
15149 /* Names appearing in the type-specifier should be looked up
15150 in the scope of the class. */
15151 if (current_class_type)
15155 type = TREE_TYPE (type_decl);
15156 if (TREE_CODE (type) == TYPENAME_TYPE)
15158 type = resolve_typename_type (type,
15159 /*only_current_p=*/false);
15160 if (type == error_mark_node)
15162 cp_parser_abort_tentative_parse (parser);
15166 pushed_scope = push_scope (type);
15169 /* Inside the constructor parameter list, surrounding
15170 template-parameter-lists do not apply. */
15171 saved_num_template_parameter_lists
15172 = parser->num_template_parameter_lists;
15173 parser->num_template_parameter_lists = 0;
15175 /* Look for the type-specifier. */
15176 cp_parser_type_specifier (parser,
15177 CP_PARSER_FLAGS_NONE,
15178 /*decl_specs=*/NULL,
15179 /*is_declarator=*/true,
15180 /*declares_class_or_enum=*/NULL,
15181 /*is_cv_qualifier=*/NULL);
15183 parser->num_template_parameter_lists
15184 = saved_num_template_parameter_lists;
15186 /* Leave the scope of the class. */
15188 pop_scope (pushed_scope);
15190 constructor_p = !cp_parser_error_occurred (parser);
15194 constructor_p = false;
15195 /* We did not really want to consume any tokens. */
15196 cp_parser_abort_tentative_parse (parser);
15198 return constructor_p;
15201 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15202 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15203 they must be performed once we are in the scope of the function.
15205 Returns the function defined. */
15208 cp_parser_function_definition_from_specifiers_and_declarator
15209 (cp_parser* parser,
15210 cp_decl_specifier_seq *decl_specifiers,
15212 const cp_declarator *declarator)
15217 /* Begin the function-definition. */
15218 success_p = start_function (decl_specifiers, declarator, attributes);
15220 /* The things we're about to see are not directly qualified by any
15221 template headers we've seen thus far. */
15222 reset_specialization ();
15224 /* If there were names looked up in the decl-specifier-seq that we
15225 did not check, check them now. We must wait until we are in the
15226 scope of the function to perform the checks, since the function
15227 might be a friend. */
15228 perform_deferred_access_checks ();
15232 /* Skip the entire function. */
15233 error ("invalid function declaration");
15234 cp_parser_skip_to_end_of_block_or_statement (parser);
15235 fn = error_mark_node;
15238 fn = cp_parser_function_definition_after_declarator (parser,
15239 /*inline_p=*/false);
15244 /* Parse the part of a function-definition that follows the
15245 declarator. INLINE_P is TRUE iff this function is an inline
15246 function defined with a class-specifier.
15248 Returns the function defined. */
15251 cp_parser_function_definition_after_declarator (cp_parser* parser,
15255 bool ctor_initializer_p = false;
15256 bool saved_in_unbraced_linkage_specification_p;
15257 unsigned saved_num_template_parameter_lists;
15259 /* If the next token is `return', then the code may be trying to
15260 make use of the "named return value" extension that G++ used to
15262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15264 /* Consume the `return' keyword. */
15265 cp_lexer_consume_token (parser->lexer);
15266 /* Look for the identifier that indicates what value is to be
15268 cp_parser_identifier (parser);
15269 /* Issue an error message. */
15270 error ("named return values are no longer supported");
15271 /* Skip tokens until we reach the start of the function body. */
15274 cp_token *token = cp_lexer_peek_token (parser->lexer);
15275 if (token->type == CPP_OPEN_BRACE
15276 || token->type == CPP_EOF
15277 || token->type == CPP_PRAGMA_EOL)
15279 cp_lexer_consume_token (parser->lexer);
15282 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15283 anything declared inside `f'. */
15284 saved_in_unbraced_linkage_specification_p
15285 = parser->in_unbraced_linkage_specification_p;
15286 parser->in_unbraced_linkage_specification_p = false;
15287 /* Inside the function, surrounding template-parameter-lists do not
15289 saved_num_template_parameter_lists
15290 = parser->num_template_parameter_lists;
15291 parser->num_template_parameter_lists = 0;
15292 /* If the next token is `try', then we are looking at a
15293 function-try-block. */
15294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15295 ctor_initializer_p = cp_parser_function_try_block (parser);
15296 /* A function-try-block includes the function-body, so we only do
15297 this next part if we're not processing a function-try-block. */
15300 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15302 /* Finish the function. */
15303 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15304 (inline_p ? 2 : 0));
15305 /* Generate code for it, if necessary. */
15306 expand_or_defer_fn (fn);
15307 /* Restore the saved values. */
15308 parser->in_unbraced_linkage_specification_p
15309 = saved_in_unbraced_linkage_specification_p;
15310 parser->num_template_parameter_lists
15311 = saved_num_template_parameter_lists;
15316 /* Parse a template-declaration, assuming that the `export' (and
15317 `extern') keywords, if present, has already been scanned. MEMBER_P
15318 is as for cp_parser_template_declaration. */
15321 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15323 tree decl = NULL_TREE;
15324 tree parameter_list;
15325 bool friend_p = false;
15326 bool need_lang_pop;
15328 /* Look for the `template' keyword. */
15329 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15333 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15337 A template ... shall not have C linkage. */
15338 if (current_lang_name == lang_name_c)
15340 error ("template with C linkage");
15341 /* Give it C++ linkage to avoid confusing other parts of the
15343 push_lang_context (lang_name_cplusplus);
15344 need_lang_pop = true;
15347 need_lang_pop = false;
15348 /* If the next token is `>', then we have an invalid
15349 specialization. Rather than complain about an invalid template
15350 parameter, issue an error message here. */
15351 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15353 cp_parser_error (parser, "invalid explicit specialization");
15354 begin_specialization ();
15355 parameter_list = NULL_TREE;
15358 /* Parse the template parameters. */
15359 parameter_list = cp_parser_template_parameter_list (parser);
15361 /* Look for the `>'. */
15362 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15363 /* We just processed one more parameter list. */
15364 ++parser->num_template_parameter_lists;
15365 /* If the next token is `template', there are more template
15367 if (cp_lexer_next_token_is_keyword (parser->lexer,
15369 cp_parser_template_declaration_after_export (parser, member_p);
15372 /* There are no access checks when parsing a template, as we do not
15373 know if a specialization will be a friend. */
15374 push_deferring_access_checks (dk_no_check);
15376 decl = cp_parser_single_declaration (parser,
15380 pop_deferring_access_checks ();
15382 /* If this is a member template declaration, let the front
15384 if (member_p && !friend_p && decl)
15386 if (TREE_CODE (decl) == TYPE_DECL)
15387 cp_parser_check_access_in_redeclaration (decl);
15389 decl = finish_member_template_decl (decl);
15391 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15392 make_friend_class (current_class_type, TREE_TYPE (decl),
15393 /*complain=*/true);
15395 /* We are done with the current parameter list. */
15396 --parser->num_template_parameter_lists;
15399 finish_template_decl (parameter_list);
15401 /* Register member declarations. */
15402 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15403 finish_member_declaration (decl);
15404 /* For the erroneous case of a template with C linkage, we pushed an
15405 implicit C++ linkage scope; exit that scope now. */
15407 pop_lang_context ();
15408 /* If DECL is a function template, we must return to parse it later.
15409 (Even though there is no definition, there might be default
15410 arguments that need handling.) */
15411 if (member_p && decl
15412 && (TREE_CODE (decl) == FUNCTION_DECL
15413 || DECL_FUNCTION_TEMPLATE_P (decl)))
15414 TREE_VALUE (parser->unparsed_functions_queues)
15415 = tree_cons (NULL_TREE, decl,
15416 TREE_VALUE (parser->unparsed_functions_queues));
15419 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15420 `function-definition' sequence. MEMBER_P is true, this declaration
15421 appears in a class scope.
15423 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15424 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15427 cp_parser_single_declaration (cp_parser* parser,
15431 int declares_class_or_enum;
15432 tree decl = NULL_TREE;
15433 cp_decl_specifier_seq decl_specifiers;
15434 bool function_definition_p = false;
15436 /* This function is only used when processing a template
15438 gcc_assert (innermost_scope_kind () == sk_template_parms
15439 || innermost_scope_kind () == sk_template_spec);
15441 /* Defer access checks until we know what is being declared. */
15442 push_deferring_access_checks (dk_deferred);
15444 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15446 cp_parser_decl_specifier_seq (parser,
15447 CP_PARSER_FLAGS_OPTIONAL,
15449 &declares_class_or_enum);
15451 *friend_p = cp_parser_friend_p (&decl_specifiers);
15453 /* There are no template typedefs. */
15454 if (decl_specifiers.specs[(int) ds_typedef])
15456 error ("template declaration of %qs", "typedef");
15457 decl = error_mark_node;
15460 /* Gather up the access checks that occurred the
15461 decl-specifier-seq. */
15462 stop_deferring_access_checks ();
15464 /* Check for the declaration of a template class. */
15465 if (declares_class_or_enum)
15467 if (cp_parser_declares_only_class_p (parser))
15469 decl = shadow_tag (&decl_specifiers);
15474 friend template <typename T> struct A<T>::B;
15477 A<T>::B will be represented by a TYPENAME_TYPE, and
15478 therefore not recognized by shadow_tag. */
15479 if (friend_p && *friend_p
15481 && decl_specifiers.type
15482 && TYPE_P (decl_specifiers.type))
15483 decl = decl_specifiers.type;
15485 if (decl && decl != error_mark_node)
15486 decl = TYPE_NAME (decl);
15488 decl = error_mark_node;
15491 /* If it's not a template class, try for a template function. If
15492 the next token is a `;', then this declaration does not declare
15493 anything. But, if there were errors in the decl-specifiers, then
15494 the error might well have come from an attempted class-specifier.
15495 In that case, there's no need to warn about a missing declarator. */
15497 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15498 || decl_specifiers.type != error_mark_node))
15499 decl = cp_parser_init_declarator (parser,
15501 /*function_definition_allowed_p=*/true,
15503 declares_class_or_enum,
15504 &function_definition_p);
15506 pop_deferring_access_checks ();
15508 /* Clear any current qualification; whatever comes next is the start
15509 of something new. */
15510 parser->scope = NULL_TREE;
15511 parser->qualifying_scope = NULL_TREE;
15512 parser->object_scope = NULL_TREE;
15513 /* Look for a trailing `;' after the declaration. */
15514 if (!function_definition_p
15515 && (decl == error_mark_node
15516 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15517 cp_parser_skip_to_end_of_block_or_statement (parser);
15522 /* Parse a cast-expression that is not the operand of a unary "&". */
15525 cp_parser_simple_cast_expression (cp_parser *parser)
15527 return cp_parser_cast_expression (parser, /*address_p=*/false,
15531 /* Parse a functional cast to TYPE. Returns an expression
15532 representing the cast. */
15535 cp_parser_functional_cast (cp_parser* parser, tree type)
15537 tree expression_list;
15541 = cp_parser_parenthesized_expression_list (parser, false,
15543 /*non_constant_p=*/NULL);
15545 cast = build_functional_cast (type, expression_list);
15546 /* [expr.const]/1: In an integral constant expression "only type
15547 conversions to integral or enumeration type can be used". */
15548 if (TREE_CODE (type) == TYPE_DECL)
15549 type = TREE_TYPE (type);
15550 if (cast != error_mark_node && !dependent_type_p (type)
15551 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15553 if (cp_parser_non_integral_constant_expression
15554 (parser, "a call to a constructor"))
15555 return error_mark_node;
15560 /* Save the tokens that make up the body of a member function defined
15561 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15562 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15563 specifiers applied to the declaration. Returns the FUNCTION_DECL
15564 for the member function. */
15567 cp_parser_save_member_function_body (cp_parser* parser,
15568 cp_decl_specifier_seq *decl_specifiers,
15569 cp_declarator *declarator,
15576 /* Create the function-declaration. */
15577 fn = start_method (decl_specifiers, declarator, attributes);
15578 /* If something went badly wrong, bail out now. */
15579 if (fn == error_mark_node)
15581 /* If there's a function-body, skip it. */
15582 if (cp_parser_token_starts_function_definition_p
15583 (cp_lexer_peek_token (parser->lexer)))
15584 cp_parser_skip_to_end_of_block_or_statement (parser);
15585 return error_mark_node;
15588 /* Remember it, if there default args to post process. */
15589 cp_parser_save_default_args (parser, fn);
15591 /* Save away the tokens that make up the body of the
15593 first = parser->lexer->next_token;
15594 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15595 /* Handle function try blocks. */
15596 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15597 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15598 last = parser->lexer->next_token;
15600 /* Save away the inline definition; we will process it when the
15601 class is complete. */
15602 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15603 DECL_PENDING_INLINE_P (fn) = 1;
15605 /* We need to know that this was defined in the class, so that
15606 friend templates are handled correctly. */
15607 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15609 /* We're done with the inline definition. */
15610 finish_method (fn);
15612 /* Add FN to the queue of functions to be parsed later. */
15613 TREE_VALUE (parser->unparsed_functions_queues)
15614 = tree_cons (NULL_TREE, fn,
15615 TREE_VALUE (parser->unparsed_functions_queues));
15620 /* Parse a template-argument-list, as well as the trailing ">" (but
15621 not the opening ">"). See cp_parser_template_argument_list for the
15625 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15629 tree saved_qualifying_scope;
15630 tree saved_object_scope;
15631 bool saved_greater_than_is_operator_p;
15632 bool saved_skip_evaluation;
15636 When parsing a template-id, the first non-nested `>' is taken as
15637 the end of the template-argument-list rather than a greater-than
15639 saved_greater_than_is_operator_p
15640 = parser->greater_than_is_operator_p;
15641 parser->greater_than_is_operator_p = false;
15642 /* Parsing the argument list may modify SCOPE, so we save it
15644 saved_scope = parser->scope;
15645 saved_qualifying_scope = parser->qualifying_scope;
15646 saved_object_scope = parser->object_scope;
15647 /* We need to evaluate the template arguments, even though this
15648 template-id may be nested within a "sizeof". */
15649 saved_skip_evaluation = skip_evaluation;
15650 skip_evaluation = false;
15651 /* Parse the template-argument-list itself. */
15652 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15653 arguments = NULL_TREE;
15655 arguments = cp_parser_template_argument_list (parser);
15656 /* Look for the `>' that ends the template-argument-list. If we find
15657 a '>>' instead, it's probably just a typo. */
15658 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15660 if (!saved_greater_than_is_operator_p)
15662 /* If we're in a nested template argument list, the '>>' has
15663 to be a typo for '> >'. We emit the error message, but we
15664 continue parsing and we push a '>' as next token, so that
15665 the argument list will be parsed correctly. Note that the
15666 global source location is still on the token before the
15667 '>>', so we need to say explicitly where we want it. */
15668 cp_token *token = cp_lexer_peek_token (parser->lexer);
15669 error ("%H%<>>%> should be %<> >%> "
15670 "within a nested template argument list",
15673 /* ??? Proper recovery should terminate two levels of
15674 template argument list here. */
15675 token->type = CPP_GREATER;
15679 /* If this is not a nested template argument list, the '>>'
15680 is a typo for '>'. Emit an error message and continue.
15681 Same deal about the token location, but here we can get it
15682 right by consuming the '>>' before issuing the diagnostic. */
15683 cp_lexer_consume_token (parser->lexer);
15684 error ("spurious %<>>%>, use %<>%> to terminate "
15685 "a template argument list");
15689 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15690 /* The `>' token might be a greater-than operator again now. */
15691 parser->greater_than_is_operator_p
15692 = saved_greater_than_is_operator_p;
15693 /* Restore the SAVED_SCOPE. */
15694 parser->scope = saved_scope;
15695 parser->qualifying_scope = saved_qualifying_scope;
15696 parser->object_scope = saved_object_scope;
15697 skip_evaluation = saved_skip_evaluation;
15702 /* MEMBER_FUNCTION is a member function, or a friend. If default
15703 arguments, or the body of the function have not yet been parsed,
15707 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15709 /* If this member is a template, get the underlying
15711 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15712 member_function = DECL_TEMPLATE_RESULT (member_function);
15714 /* There should not be any class definitions in progress at this
15715 point; the bodies of members are only parsed outside of all class
15717 gcc_assert (parser->num_classes_being_defined == 0);
15718 /* While we're parsing the member functions we might encounter more
15719 classes. We want to handle them right away, but we don't want
15720 them getting mixed up with functions that are currently in the
15722 parser->unparsed_functions_queues
15723 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15725 /* Make sure that any template parameters are in scope. */
15726 maybe_begin_member_template_processing (member_function);
15728 /* If the body of the function has not yet been parsed, parse it
15730 if (DECL_PENDING_INLINE_P (member_function))
15732 tree function_scope;
15733 cp_token_cache *tokens;
15735 /* The function is no longer pending; we are processing it. */
15736 tokens = DECL_PENDING_INLINE_INFO (member_function);
15737 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15738 DECL_PENDING_INLINE_P (member_function) = 0;
15740 /* If this is a local class, enter the scope of the containing
15742 function_scope = current_function_decl;
15743 if (function_scope)
15744 push_function_context_to (function_scope);
15747 /* Push the body of the function onto the lexer stack. */
15748 cp_parser_push_lexer_for_tokens (parser, tokens);
15750 /* Let the front end know that we going to be defining this
15752 start_preparsed_function (member_function, NULL_TREE,
15753 SF_PRE_PARSED | SF_INCLASS_INLINE);
15755 /* Don't do access checking if it is a templated function. */
15756 if (processing_template_decl)
15757 push_deferring_access_checks (dk_no_check);
15759 /* Now, parse the body of the function. */
15760 cp_parser_function_definition_after_declarator (parser,
15761 /*inline_p=*/true);
15763 if (processing_template_decl)
15764 pop_deferring_access_checks ();
15766 /* Leave the scope of the containing function. */
15767 if (function_scope)
15768 pop_function_context_from (function_scope);
15769 cp_parser_pop_lexer (parser);
15772 /* Remove any template parameters from the symbol table. */
15773 maybe_end_member_template_processing ();
15775 /* Restore the queue. */
15776 parser->unparsed_functions_queues
15777 = TREE_CHAIN (parser->unparsed_functions_queues);
15780 /* If DECL contains any default args, remember it on the unparsed
15781 functions queue. */
15784 cp_parser_save_default_args (cp_parser* parser, tree decl)
15788 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15790 probe = TREE_CHAIN (probe))
15791 if (TREE_PURPOSE (probe))
15793 TREE_PURPOSE (parser->unparsed_functions_queues)
15794 = tree_cons (current_class_type, decl,
15795 TREE_PURPOSE (parser->unparsed_functions_queues));
15800 /* FN is a FUNCTION_DECL which may contains a parameter with an
15801 unparsed DEFAULT_ARG. Parse the default args now. This function
15802 assumes that the current scope is the scope in which the default
15803 argument should be processed. */
15806 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15808 bool saved_local_variables_forbidden_p;
15811 /* While we're parsing the default args, we might (due to the
15812 statement expression extension) encounter more classes. We want
15813 to handle them right away, but we don't want them getting mixed
15814 up with default args that are currently in the queue. */
15815 parser->unparsed_functions_queues
15816 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15818 /* Local variable names (and the `this' keyword) may not appear
15819 in a default argument. */
15820 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15821 parser->local_variables_forbidden_p = true;
15823 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15825 parm = TREE_CHAIN (parm))
15827 cp_token_cache *tokens;
15828 tree default_arg = TREE_PURPOSE (parm);
15830 VEC(tree,gc) *insts;
15837 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15838 /* This can happen for a friend declaration for a function
15839 already declared with default arguments. */
15842 /* Push the saved tokens for the default argument onto the parser's
15844 tokens = DEFARG_TOKENS (default_arg);
15845 cp_parser_push_lexer_for_tokens (parser, tokens);
15847 /* Parse the assignment-expression. */
15848 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15850 if (!processing_template_decl)
15851 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15853 TREE_PURPOSE (parm) = parsed_arg;
15855 /* Update any instantiations we've already created. */
15856 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15857 VEC_iterate (tree, insts, ix, copy); ix++)
15858 TREE_PURPOSE (copy) = parsed_arg;
15860 /* If the token stream has not been completely used up, then
15861 there was extra junk after the end of the default
15863 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15864 cp_parser_error (parser, "expected %<,%>");
15866 /* Revert to the main lexer. */
15867 cp_parser_pop_lexer (parser);
15870 /* Make sure no default arg is missing. */
15871 check_default_args (fn);
15873 /* Restore the state of local_variables_forbidden_p. */
15874 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15876 /* Restore the queue. */
15877 parser->unparsed_functions_queues
15878 = TREE_CHAIN (parser->unparsed_functions_queues);
15881 /* Parse the operand of `sizeof' (or a similar operator). Returns
15882 either a TYPE or an expression, depending on the form of the
15883 input. The KEYWORD indicates which kind of expression we have
15887 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15889 static const char *format;
15890 tree expr = NULL_TREE;
15891 const char *saved_message;
15892 bool saved_integral_constant_expression_p;
15893 bool saved_non_integral_constant_expression_p;
15895 /* Initialize FORMAT the first time we get here. */
15897 format = "types may not be defined in '%s' expressions";
15899 /* Types cannot be defined in a `sizeof' expression. Save away the
15901 saved_message = parser->type_definition_forbidden_message;
15902 /* And create the new one. */
15903 parser->type_definition_forbidden_message
15904 = XNEWVEC (const char, strlen (format)
15905 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15907 sprintf ((char *) parser->type_definition_forbidden_message,
15908 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15910 /* The restrictions on constant-expressions do not apply inside
15911 sizeof expressions. */
15912 saved_integral_constant_expression_p
15913 = parser->integral_constant_expression_p;
15914 saved_non_integral_constant_expression_p
15915 = parser->non_integral_constant_expression_p;
15916 parser->integral_constant_expression_p = false;
15918 /* Do not actually evaluate the expression. */
15920 /* If it's a `(', then we might be looking at the type-id
15922 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15925 bool saved_in_type_id_in_expr_p;
15927 /* We can't be sure yet whether we're looking at a type-id or an
15929 cp_parser_parse_tentatively (parser);
15930 /* Consume the `('. */
15931 cp_lexer_consume_token (parser->lexer);
15932 /* Parse the type-id. */
15933 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15934 parser->in_type_id_in_expr_p = true;
15935 type = cp_parser_type_id (parser);
15936 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15937 /* Now, look for the trailing `)'. */
15938 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15939 /* If all went well, then we're done. */
15940 if (cp_parser_parse_definitely (parser))
15942 cp_decl_specifier_seq decl_specs;
15944 /* Build a trivial decl-specifier-seq. */
15945 clear_decl_specs (&decl_specs);
15946 decl_specs.type = type;
15948 /* Call grokdeclarator to figure out what type this is. */
15949 expr = grokdeclarator (NULL,
15953 /*attrlist=*/NULL);
15957 /* If the type-id production did not work out, then we must be
15958 looking at the unary-expression production. */
15960 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15962 /* Go back to evaluating expressions. */
15965 /* Free the message we created. */
15966 free ((char *) parser->type_definition_forbidden_message);
15967 /* And restore the old one. */
15968 parser->type_definition_forbidden_message = saved_message;
15969 parser->integral_constant_expression_p
15970 = saved_integral_constant_expression_p;
15971 parser->non_integral_constant_expression_p
15972 = saved_non_integral_constant_expression_p;
15977 /* If the current declaration has no declarator, return true. */
15980 cp_parser_declares_only_class_p (cp_parser *parser)
15982 /* If the next token is a `;' or a `,' then there is no
15984 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15985 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15988 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15991 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15992 cp_storage_class storage_class)
15994 if (decl_specs->storage_class != sc_none)
15995 decl_specs->multiple_storage_classes_p = true;
15997 decl_specs->storage_class = storage_class;
16000 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16001 is true, the type is a user-defined type; otherwise it is a
16002 built-in type specified by a keyword. */
16005 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16007 bool user_defined_p)
16009 decl_specs->any_specifiers_p = true;
16011 /* If the user tries to redeclare bool or wchar_t (with, for
16012 example, in "typedef int wchar_t;") we remember that this is what
16013 happened. In system headers, we ignore these declarations so
16014 that G++ can work with system headers that are not C++-safe. */
16015 if (decl_specs->specs[(int) ds_typedef]
16017 && (type_spec == boolean_type_node
16018 || type_spec == wchar_type_node)
16019 && (decl_specs->type
16020 || decl_specs->specs[(int) ds_long]
16021 || decl_specs->specs[(int) ds_short]
16022 || decl_specs->specs[(int) ds_unsigned]
16023 || decl_specs->specs[(int) ds_signed]))
16025 decl_specs->redefined_builtin_type = type_spec;
16026 if (!decl_specs->type)
16028 decl_specs->type = type_spec;
16029 decl_specs->user_defined_type_p = false;
16032 else if (decl_specs->type)
16033 decl_specs->multiple_types_p = true;
16036 decl_specs->type = type_spec;
16037 decl_specs->user_defined_type_p = user_defined_p;
16038 decl_specs->redefined_builtin_type = NULL_TREE;
16042 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16043 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16046 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16048 return decl_specifiers->specs[(int) ds_friend] != 0;
16051 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16052 issue an error message indicating that TOKEN_DESC was expected.
16054 Returns the token consumed, if the token had the appropriate type.
16055 Otherwise, returns NULL. */
16058 cp_parser_require (cp_parser* parser,
16059 enum cpp_ttype type,
16060 const char* token_desc)
16062 if (cp_lexer_next_token_is (parser->lexer, type))
16063 return cp_lexer_consume_token (parser->lexer);
16066 /* Output the MESSAGE -- unless we're parsing tentatively. */
16067 if (!cp_parser_simulate_error (parser))
16069 char *message = concat ("expected ", token_desc, NULL);
16070 cp_parser_error (parser, message);
16077 /* Like cp_parser_require, except that tokens will be skipped until
16078 the desired token is found. An error message is still produced if
16079 the next token is not as expected. */
16082 cp_parser_skip_until_found (cp_parser* parser,
16083 enum cpp_ttype type,
16084 const char* token_desc)
16087 unsigned nesting_depth = 0;
16089 if (cp_parser_require (parser, type, token_desc))
16092 /* Skip tokens until the desired token is found. */
16095 /* Peek at the next token. */
16096 token = cp_lexer_peek_token (parser->lexer);
16098 /* If we've reached the token we want, consume it and stop. */
16099 if (token->type == type && !nesting_depth)
16101 cp_lexer_consume_token (parser->lexer);
16105 switch (token->type)
16108 case CPP_PRAGMA_EOL:
16109 /* If we've run out of tokens, stop. */
16112 case CPP_OPEN_BRACE:
16113 case CPP_OPEN_PAREN:
16114 case CPP_OPEN_SQUARE:
16118 case CPP_CLOSE_BRACE:
16119 case CPP_CLOSE_PAREN:
16120 case CPP_CLOSE_SQUARE:
16121 if (nesting_depth-- == 0)
16129 /* Consume this token. */
16130 cp_lexer_consume_token (parser->lexer);
16134 /* If the next token is the indicated keyword, consume it. Otherwise,
16135 issue an error message indicating that TOKEN_DESC was expected.
16137 Returns the token consumed, if the token had the appropriate type.
16138 Otherwise, returns NULL. */
16141 cp_parser_require_keyword (cp_parser* parser,
16143 const char* token_desc)
16145 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16147 if (token && token->keyword != keyword)
16149 dyn_string_t error_msg;
16151 /* Format the error message. */
16152 error_msg = dyn_string_new (0);
16153 dyn_string_append_cstr (error_msg, "expected ");
16154 dyn_string_append_cstr (error_msg, token_desc);
16155 cp_parser_error (parser, error_msg->s);
16156 dyn_string_delete (error_msg);
16163 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16164 function-definition. */
16167 cp_parser_token_starts_function_definition_p (cp_token* token)
16169 return (/* An ordinary function-body begins with an `{'. */
16170 token->type == CPP_OPEN_BRACE
16171 /* A ctor-initializer begins with a `:'. */
16172 || token->type == CPP_COLON
16173 /* A function-try-block begins with `try'. */
16174 || token->keyword == RID_TRY
16175 /* The named return value extension begins with `return'. */
16176 || token->keyword == RID_RETURN);
16179 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16183 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16187 token = cp_lexer_peek_token (parser->lexer);
16188 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16191 /* Returns TRUE iff the next token is the "," or ">" ending a
16192 template-argument. */
16195 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16199 token = cp_lexer_peek_token (parser->lexer);
16200 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16203 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16204 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16207 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16212 token = cp_lexer_peek_nth_token (parser->lexer, n);
16213 if (token->type == CPP_LESS)
16215 /* Check for the sequence `<::' in the original code. It would be lexed as
16216 `[:', where `[' is a digraph, and there is no whitespace before
16218 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16221 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16222 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16228 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16229 or none_type otherwise. */
16231 static enum tag_types
16232 cp_parser_token_is_class_key (cp_token* token)
16234 switch (token->keyword)
16239 return record_type;
16248 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16251 cp_parser_check_class_key (enum tag_types class_key, tree type)
16253 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16254 pedwarn ("%qs tag used in naming %q#T",
16255 class_key == union_type ? "union"
16256 : class_key == record_type ? "struct" : "class",
16260 /* Issue an error message if DECL is redeclared with different
16261 access than its original declaration [class.access.spec/3].
16262 This applies to nested classes and nested class templates.
16266 cp_parser_check_access_in_redeclaration (tree decl)
16268 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16271 if ((TREE_PRIVATE (decl)
16272 != (current_access_specifier == access_private_node))
16273 || (TREE_PROTECTED (decl)
16274 != (current_access_specifier == access_protected_node)))
16275 error ("%qD redeclared with different access", decl);
16278 /* Look for the `template' keyword, as a syntactic disambiguator.
16279 Return TRUE iff it is present, in which case it will be
16283 cp_parser_optional_template_keyword (cp_parser *parser)
16285 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16287 /* The `template' keyword can only be used within templates;
16288 outside templates the parser can always figure out what is a
16289 template and what is not. */
16290 if (!processing_template_decl)
16292 error ("%<template%> (as a disambiguator) is only allowed "
16293 "within templates");
16294 /* If this part of the token stream is rescanned, the same
16295 error message would be generated. So, we purge the token
16296 from the stream. */
16297 cp_lexer_purge_token (parser->lexer);
16302 /* Consume the `template' keyword. */
16303 cp_lexer_consume_token (parser->lexer);
16311 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16312 set PARSER->SCOPE, and perform other related actions. */
16315 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16320 /* Get the stored value. */
16321 value = cp_lexer_consume_token (parser->lexer)->value;
16322 /* Perform any access checks that were deferred. */
16323 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16324 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16325 /* Set the scope from the stored value. */
16326 parser->scope = TREE_VALUE (value);
16327 parser->qualifying_scope = TREE_TYPE (value);
16328 parser->object_scope = NULL_TREE;
16331 /* Consume tokens up through a non-nested END token. */
16334 cp_parser_cache_group (cp_parser *parser,
16335 enum cpp_ttype end,
16342 /* Abort a parenthesized expression if we encounter a brace. */
16343 if ((end == CPP_CLOSE_PAREN || depth == 0)
16344 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16346 /* If we've reached the end of the file, stop. */
16347 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16348 || (end != CPP_PRAGMA_EOL
16349 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16351 /* Consume the next token. */
16352 token = cp_lexer_consume_token (parser->lexer);
16353 /* See if it starts a new group. */
16354 if (token->type == CPP_OPEN_BRACE)
16356 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16360 else if (token->type == CPP_OPEN_PAREN)
16361 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16362 else if (token->type == CPP_PRAGMA)
16363 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16364 else if (token->type == end)
16369 /* Begin parsing tentatively. We always save tokens while parsing
16370 tentatively so that if the tentative parsing fails we can restore the
16374 cp_parser_parse_tentatively (cp_parser* parser)
16376 /* Enter a new parsing context. */
16377 parser->context = cp_parser_context_new (parser->context);
16378 /* Begin saving tokens. */
16379 cp_lexer_save_tokens (parser->lexer);
16380 /* In order to avoid repetitive access control error messages,
16381 access checks are queued up until we are no longer parsing
16383 push_deferring_access_checks (dk_deferred);
16386 /* Commit to the currently active tentative parse. */
16389 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16391 cp_parser_context *context;
16394 /* Mark all of the levels as committed. */
16395 lexer = parser->lexer;
16396 for (context = parser->context; context->next; context = context->next)
16398 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16400 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16401 while (!cp_lexer_saving_tokens (lexer))
16402 lexer = lexer->next;
16403 cp_lexer_commit_tokens (lexer);
16407 /* Abort the currently active tentative parse. All consumed tokens
16408 will be rolled back, and no diagnostics will be issued. */
16411 cp_parser_abort_tentative_parse (cp_parser* parser)
16413 cp_parser_simulate_error (parser);
16414 /* Now, pretend that we want to see if the construct was
16415 successfully parsed. */
16416 cp_parser_parse_definitely (parser);
16419 /* Stop parsing tentatively. If a parse error has occurred, restore the
16420 token stream. Otherwise, commit to the tokens we have consumed.
16421 Returns true if no error occurred; false otherwise. */
16424 cp_parser_parse_definitely (cp_parser* parser)
16426 bool error_occurred;
16427 cp_parser_context *context;
16429 /* Remember whether or not an error occurred, since we are about to
16430 destroy that information. */
16431 error_occurred = cp_parser_error_occurred (parser);
16432 /* Remove the topmost context from the stack. */
16433 context = parser->context;
16434 parser->context = context->next;
16435 /* If no parse errors occurred, commit to the tentative parse. */
16436 if (!error_occurred)
16438 /* Commit to the tokens read tentatively, unless that was
16440 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16441 cp_lexer_commit_tokens (parser->lexer);
16443 pop_to_parent_deferring_access_checks ();
16445 /* Otherwise, if errors occurred, roll back our state so that things
16446 are just as they were before we began the tentative parse. */
16449 cp_lexer_rollback_tokens (parser->lexer);
16450 pop_deferring_access_checks ();
16452 /* Add the context to the front of the free list. */
16453 context->next = cp_parser_context_free_list;
16454 cp_parser_context_free_list = context;
16456 return !error_occurred;
16459 /* Returns true if we are parsing tentatively and are not committed to
16460 this tentative parse. */
16463 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16465 return (cp_parser_parsing_tentatively (parser)
16466 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16469 /* Returns nonzero iff an error has occurred during the most recent
16470 tentative parse. */
16473 cp_parser_error_occurred (cp_parser* parser)
16475 return (cp_parser_parsing_tentatively (parser)
16476 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16479 /* Returns nonzero if GNU extensions are allowed. */
16482 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16484 return parser->allow_gnu_extensions_p;
16487 /* Objective-C++ Productions */
16490 /* Parse an Objective-C expression, which feeds into a primary-expression
16494 objc-message-expression
16495 objc-string-literal
16496 objc-encode-expression
16497 objc-protocol-expression
16498 objc-selector-expression
16500 Returns a tree representation of the expression. */
16503 cp_parser_objc_expression (cp_parser* parser)
16505 /* Try to figure out what kind of declaration is present. */
16506 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16510 case CPP_OPEN_SQUARE:
16511 return cp_parser_objc_message_expression (parser);
16513 case CPP_OBJC_STRING:
16514 kwd = cp_lexer_consume_token (parser->lexer);
16515 return objc_build_string_object (kwd->value);
16518 switch (kwd->keyword)
16520 case RID_AT_ENCODE:
16521 return cp_parser_objc_encode_expression (parser);
16523 case RID_AT_PROTOCOL:
16524 return cp_parser_objc_protocol_expression (parser);
16526 case RID_AT_SELECTOR:
16527 return cp_parser_objc_selector_expression (parser);
16533 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16534 cp_parser_skip_to_end_of_block_or_statement (parser);
16537 return error_mark_node;
16540 /* Parse an Objective-C message expression.
16542 objc-message-expression:
16543 [ objc-message-receiver objc-message-args ]
16545 Returns a representation of an Objective-C message. */
16548 cp_parser_objc_message_expression (cp_parser* parser)
16550 tree receiver, messageargs;
16552 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16553 receiver = cp_parser_objc_message_receiver (parser);
16554 messageargs = cp_parser_objc_message_args (parser);
16555 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16557 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16560 /* Parse an objc-message-receiver.
16562 objc-message-receiver:
16564 simple-type-specifier
16566 Returns a representation of the type or expression. */
16569 cp_parser_objc_message_receiver (cp_parser* parser)
16573 /* An Objective-C message receiver may be either (1) a type
16574 or (2) an expression. */
16575 cp_parser_parse_tentatively (parser);
16576 rcv = cp_parser_expression (parser, false);
16578 if (cp_parser_parse_definitely (parser))
16581 rcv = cp_parser_simple_type_specifier (parser,
16582 /*decl_specs=*/NULL,
16583 CP_PARSER_FLAGS_NONE);
16585 return objc_get_class_reference (rcv);
16588 /* Parse the arguments and selectors comprising an Objective-C message.
16593 objc-selector-args , objc-comma-args
16595 objc-selector-args:
16596 objc-selector [opt] : assignment-expression
16597 objc-selector-args objc-selector [opt] : assignment-expression
16600 assignment-expression
16601 objc-comma-args , assignment-expression
16603 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16604 selector arguments and TREE_VALUE containing a list of comma
16608 cp_parser_objc_message_args (cp_parser* parser)
16610 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16611 bool maybe_unary_selector_p = true;
16612 cp_token *token = cp_lexer_peek_token (parser->lexer);
16614 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16616 tree selector = NULL_TREE, arg;
16618 if (token->type != CPP_COLON)
16619 selector = cp_parser_objc_selector (parser);
16621 /* Detect if we have a unary selector. */
16622 if (maybe_unary_selector_p
16623 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16624 return build_tree_list (selector, NULL_TREE);
16626 maybe_unary_selector_p = false;
16627 cp_parser_require (parser, CPP_COLON, "`:'");
16628 arg = cp_parser_assignment_expression (parser, false);
16631 = chainon (sel_args,
16632 build_tree_list (selector, arg));
16634 token = cp_lexer_peek_token (parser->lexer);
16637 /* Handle non-selector arguments, if any. */
16638 while (token->type == CPP_COMMA)
16642 cp_lexer_consume_token (parser->lexer);
16643 arg = cp_parser_assignment_expression (parser, false);
16646 = chainon (addl_args,
16647 build_tree_list (NULL_TREE, arg));
16649 token = cp_lexer_peek_token (parser->lexer);
16652 return build_tree_list (sel_args, addl_args);
16655 /* Parse an Objective-C encode expression.
16657 objc-encode-expression:
16658 @encode objc-typename
16660 Returns an encoded representation of the type argument. */
16663 cp_parser_objc_encode_expression (cp_parser* parser)
16667 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16668 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16669 type = complete_type (cp_parser_type_id (parser));
16670 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16674 error ("%<@encode%> must specify a type as an argument");
16675 return error_mark_node;
16678 return objc_build_encode_expr (type);
16681 /* Parse an Objective-C @defs expression. */
16684 cp_parser_objc_defs_expression (cp_parser *parser)
16688 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16689 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16690 name = cp_parser_identifier (parser);
16691 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16693 return objc_get_class_ivars (name);
16696 /* Parse an Objective-C protocol expression.
16698 objc-protocol-expression:
16699 @protocol ( identifier )
16701 Returns a representation of the protocol expression. */
16704 cp_parser_objc_protocol_expression (cp_parser* parser)
16708 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16709 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16710 proto = cp_parser_identifier (parser);
16711 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16713 return objc_build_protocol_expr (proto);
16716 /* Parse an Objective-C selector expression.
16718 objc-selector-expression:
16719 @selector ( objc-method-signature )
16721 objc-method-signature:
16727 objc-selector-seq objc-selector :
16729 Returns a representation of the method selector. */
16732 cp_parser_objc_selector_expression (cp_parser* parser)
16734 tree sel_seq = NULL_TREE;
16735 bool maybe_unary_selector_p = true;
16738 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16739 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16740 token = cp_lexer_peek_token (parser->lexer);
16742 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16743 || token->type == CPP_SCOPE)
16745 tree selector = NULL_TREE;
16747 if (token->type != CPP_COLON
16748 || token->type == CPP_SCOPE)
16749 selector = cp_parser_objc_selector (parser);
16751 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16752 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16754 /* Detect if we have a unary selector. */
16755 if (maybe_unary_selector_p)
16757 sel_seq = selector;
16758 goto finish_selector;
16762 cp_parser_error (parser, "expected %<:%>");
16765 maybe_unary_selector_p = false;
16766 token = cp_lexer_consume_token (parser->lexer);
16768 if (token->type == CPP_SCOPE)
16771 = chainon (sel_seq,
16772 build_tree_list (selector, NULL_TREE));
16774 = chainon (sel_seq,
16775 build_tree_list (NULL_TREE, NULL_TREE));
16779 = chainon (sel_seq,
16780 build_tree_list (selector, NULL_TREE));
16782 token = cp_lexer_peek_token (parser->lexer);
16786 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16788 return objc_build_selector_expr (sel_seq);
16791 /* Parse a list of identifiers.
16793 objc-identifier-list:
16795 objc-identifier-list , identifier
16797 Returns a TREE_LIST of identifier nodes. */
16800 cp_parser_objc_identifier_list (cp_parser* parser)
16802 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16803 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16805 while (sep->type == CPP_COMMA)
16807 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16808 list = chainon (list,
16809 build_tree_list (NULL_TREE,
16810 cp_parser_identifier (parser)));
16811 sep = cp_lexer_peek_token (parser->lexer);
16817 /* Parse an Objective-C alias declaration.
16819 objc-alias-declaration:
16820 @compatibility_alias identifier identifier ;
16822 This function registers the alias mapping with the Objective-C front-end.
16823 It returns nothing. */
16826 cp_parser_objc_alias_declaration (cp_parser* parser)
16830 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16831 alias = cp_parser_identifier (parser);
16832 orig = cp_parser_identifier (parser);
16833 objc_declare_alias (alias, orig);
16834 cp_parser_consume_semicolon_at_end_of_statement (parser);
16837 /* Parse an Objective-C class forward-declaration.
16839 objc-class-declaration:
16840 @class objc-identifier-list ;
16842 The function registers the forward declarations with the Objective-C
16843 front-end. It returns nothing. */
16846 cp_parser_objc_class_declaration (cp_parser* parser)
16848 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16849 objc_declare_class (cp_parser_objc_identifier_list (parser));
16850 cp_parser_consume_semicolon_at_end_of_statement (parser);
16853 /* Parse a list of Objective-C protocol references.
16855 objc-protocol-refs-opt:
16856 objc-protocol-refs [opt]
16858 objc-protocol-refs:
16859 < objc-identifier-list >
16861 Returns a TREE_LIST of identifiers, if any. */
16864 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16866 tree protorefs = NULL_TREE;
16868 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16870 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16871 protorefs = cp_parser_objc_identifier_list (parser);
16872 cp_parser_require (parser, CPP_GREATER, "`>'");
16878 /* Parse a Objective-C visibility specification. */
16881 cp_parser_objc_visibility_spec (cp_parser* parser)
16883 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16885 switch (vis->keyword)
16887 case RID_AT_PRIVATE:
16888 objc_set_visibility (2);
16890 case RID_AT_PROTECTED:
16891 objc_set_visibility (0);
16893 case RID_AT_PUBLIC:
16894 objc_set_visibility (1);
16900 /* Eat '@private'/'@protected'/'@public'. */
16901 cp_lexer_consume_token (parser->lexer);
16904 /* Parse an Objective-C method type. */
16907 cp_parser_objc_method_type (cp_parser* parser)
16909 objc_set_method_type
16910 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16915 /* Parse an Objective-C protocol qualifier. */
16918 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16920 tree quals = NULL_TREE, node;
16921 cp_token *token = cp_lexer_peek_token (parser->lexer);
16923 node = token->value;
16925 while (node && TREE_CODE (node) == IDENTIFIER_NODE
16926 && (node == ridpointers [(int) RID_IN]
16927 || node == ridpointers [(int) RID_OUT]
16928 || node == ridpointers [(int) RID_INOUT]
16929 || node == ridpointers [(int) RID_BYCOPY]
16930 || node == ridpointers [(int) RID_BYREF]
16931 || node == ridpointers [(int) RID_ONEWAY]))
16933 quals = tree_cons (NULL_TREE, node, quals);
16934 cp_lexer_consume_token (parser->lexer);
16935 token = cp_lexer_peek_token (parser->lexer);
16936 node = token->value;
16942 /* Parse an Objective-C typename. */
16945 cp_parser_objc_typename (cp_parser* parser)
16947 tree typename = NULL_TREE;
16949 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16951 tree proto_quals, cp_type = NULL_TREE;
16953 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
16954 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16956 /* An ObjC type name may consist of just protocol qualifiers, in which
16957 case the type shall default to 'id'. */
16958 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16959 cp_type = cp_parser_type_id (parser);
16961 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16962 typename = build_tree_list (proto_quals, cp_type);
16968 /* Check to see if TYPE refers to an Objective-C selector name. */
16971 cp_parser_objc_selector_p (enum cpp_ttype type)
16973 return (type == CPP_NAME || type == CPP_KEYWORD
16974 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16975 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16976 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16977 || type == CPP_XOR || type == CPP_XOR_EQ);
16980 /* Parse an Objective-C selector. */
16983 cp_parser_objc_selector (cp_parser* parser)
16985 cp_token *token = cp_lexer_consume_token (parser->lexer);
16987 if (!cp_parser_objc_selector_p (token->type))
16989 error ("invalid Objective-C++ selector name");
16990 return error_mark_node;
16993 /* C++ operator names are allowed to appear in ObjC selectors. */
16994 switch (token->type)
16996 case CPP_AND_AND: return get_identifier ("and");
16997 case CPP_AND_EQ: return get_identifier ("and_eq");
16998 case CPP_AND: return get_identifier ("bitand");
16999 case CPP_OR: return get_identifier ("bitor");
17000 case CPP_COMPL: return get_identifier ("compl");
17001 case CPP_NOT: return get_identifier ("not");
17002 case CPP_NOT_EQ: return get_identifier ("not_eq");
17003 case CPP_OR_OR: return get_identifier ("or");
17004 case CPP_OR_EQ: return get_identifier ("or_eq");
17005 case CPP_XOR: return get_identifier ("xor");
17006 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17007 default: return token->value;
17011 /* Parse an Objective-C params list. */
17014 cp_parser_objc_method_keyword_params (cp_parser* parser)
17016 tree params = NULL_TREE;
17017 bool maybe_unary_selector_p = true;
17018 cp_token *token = cp_lexer_peek_token (parser->lexer);
17020 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17022 tree selector = NULL_TREE, typename, identifier;
17024 if (token->type != CPP_COLON)
17025 selector = cp_parser_objc_selector (parser);
17027 /* Detect if we have a unary selector. */
17028 if (maybe_unary_selector_p
17029 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17032 maybe_unary_selector_p = false;
17033 cp_parser_require (parser, CPP_COLON, "`:'");
17034 typename = cp_parser_objc_typename (parser);
17035 identifier = cp_parser_identifier (parser);
17039 objc_build_keyword_decl (selector,
17043 token = cp_lexer_peek_token (parser->lexer);
17049 /* Parse the non-keyword Objective-C params. */
17052 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17054 tree params = make_node (TREE_LIST);
17055 cp_token *token = cp_lexer_peek_token (parser->lexer);
17056 *ellipsisp = false; /* Initially, assume no ellipsis. */
17058 while (token->type == CPP_COMMA)
17060 cp_parameter_declarator *parmdecl;
17063 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17064 token = cp_lexer_peek_token (parser->lexer);
17066 if (token->type == CPP_ELLIPSIS)
17068 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17073 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17074 parm = grokdeclarator (parmdecl->declarator,
17075 &parmdecl->decl_specifiers,
17076 PARM, /*initialized=*/0,
17077 /*attrlist=*/NULL);
17079 chainon (params, build_tree_list (NULL_TREE, parm));
17080 token = cp_lexer_peek_token (parser->lexer);
17086 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17089 cp_parser_objc_interstitial_code (cp_parser* parser)
17091 cp_token *token = cp_lexer_peek_token (parser->lexer);
17093 /* If the next token is `extern' and the following token is a string
17094 literal, then we have a linkage specification. */
17095 if (token->keyword == RID_EXTERN
17096 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17097 cp_parser_linkage_specification (parser);
17098 /* Handle #pragma, if any. */
17099 else if (token->type == CPP_PRAGMA)
17100 cp_parser_pragma (parser, pragma_external);
17101 /* Allow stray semicolons. */
17102 else if (token->type == CPP_SEMICOLON)
17103 cp_lexer_consume_token (parser->lexer);
17104 /* Finally, try to parse a block-declaration, or a function-definition. */
17106 cp_parser_block_declaration (parser, /*statement_p=*/false);
17109 /* Parse a method signature. */
17112 cp_parser_objc_method_signature (cp_parser* parser)
17114 tree rettype, kwdparms, optparms;
17115 bool ellipsis = false;
17117 cp_parser_objc_method_type (parser);
17118 rettype = cp_parser_objc_typename (parser);
17119 kwdparms = cp_parser_objc_method_keyword_params (parser);
17120 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17122 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17125 /* Pars an Objective-C method prototype list. */
17128 cp_parser_objc_method_prototype_list (cp_parser* parser)
17130 cp_token *token = cp_lexer_peek_token (parser->lexer);
17132 while (token->keyword != RID_AT_END)
17134 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17136 objc_add_method_declaration
17137 (cp_parser_objc_method_signature (parser));
17138 cp_parser_consume_semicolon_at_end_of_statement (parser);
17141 /* Allow for interspersed non-ObjC++ code. */
17142 cp_parser_objc_interstitial_code (parser);
17144 token = cp_lexer_peek_token (parser->lexer);
17147 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17148 objc_finish_interface ();
17151 /* Parse an Objective-C method definition list. */
17154 cp_parser_objc_method_definition_list (cp_parser* parser)
17156 cp_token *token = cp_lexer_peek_token (parser->lexer);
17158 while (token->keyword != RID_AT_END)
17162 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17164 push_deferring_access_checks (dk_deferred);
17165 objc_start_method_definition
17166 (cp_parser_objc_method_signature (parser));
17168 /* For historical reasons, we accept an optional semicolon. */
17169 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17170 cp_lexer_consume_token (parser->lexer);
17172 perform_deferred_access_checks ();
17173 stop_deferring_access_checks ();
17174 meth = cp_parser_function_definition_after_declarator (parser,
17176 pop_deferring_access_checks ();
17177 objc_finish_method_definition (meth);
17180 /* Allow for interspersed non-ObjC++ code. */
17181 cp_parser_objc_interstitial_code (parser);
17183 token = cp_lexer_peek_token (parser->lexer);
17186 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17187 objc_finish_implementation ();
17190 /* Parse Objective-C ivars. */
17193 cp_parser_objc_class_ivars (cp_parser* parser)
17195 cp_token *token = cp_lexer_peek_token (parser->lexer);
17197 if (token->type != CPP_OPEN_BRACE)
17198 return; /* No ivars specified. */
17200 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17201 token = cp_lexer_peek_token (parser->lexer);
17203 while (token->type != CPP_CLOSE_BRACE)
17205 cp_decl_specifier_seq declspecs;
17206 int decl_class_or_enum_p;
17207 tree prefix_attributes;
17209 cp_parser_objc_visibility_spec (parser);
17211 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17214 cp_parser_decl_specifier_seq (parser,
17215 CP_PARSER_FLAGS_OPTIONAL,
17217 &decl_class_or_enum_p);
17218 prefix_attributes = declspecs.attributes;
17219 declspecs.attributes = NULL_TREE;
17221 /* Keep going until we hit the `;' at the end of the
17223 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17225 tree width = NULL_TREE, attributes, first_attribute, decl;
17226 cp_declarator *declarator = NULL;
17227 int ctor_dtor_or_conv_p;
17229 /* Check for a (possibly unnamed) bitfield declaration. */
17230 token = cp_lexer_peek_token (parser->lexer);
17231 if (token->type == CPP_COLON)
17234 if (token->type == CPP_NAME
17235 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17238 /* Get the name of the bitfield. */
17239 declarator = make_id_declarator (NULL_TREE,
17240 cp_parser_identifier (parser),
17244 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17245 /* Get the width of the bitfield. */
17247 = cp_parser_constant_expression (parser,
17248 /*allow_non_constant=*/false,
17253 /* Parse the declarator. */
17255 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17256 &ctor_dtor_or_conv_p,
17257 /*parenthesized_p=*/NULL,
17258 /*member_p=*/false);
17261 /* Look for attributes that apply to the ivar. */
17262 attributes = cp_parser_attributes_opt (parser);
17263 /* Remember which attributes are prefix attributes and
17265 first_attribute = attributes;
17266 /* Combine the attributes. */
17267 attributes = chainon (prefix_attributes, attributes);
17271 /* Create the bitfield declaration. */
17272 decl = grokbitfield (declarator, &declspecs, width);
17273 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17276 decl = grokfield (declarator, &declspecs,
17277 NULL_TREE, /*init_const_expr_p=*/false,
17278 NULL_TREE, attributes);
17280 /* Add the instance variable. */
17281 objc_add_instance_variable (decl);
17283 /* Reset PREFIX_ATTRIBUTES. */
17284 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17285 attributes = TREE_CHAIN (attributes);
17287 TREE_CHAIN (attributes) = NULL_TREE;
17289 token = cp_lexer_peek_token (parser->lexer);
17291 if (token->type == CPP_COMMA)
17293 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17299 cp_parser_consume_semicolon_at_end_of_statement (parser);
17300 token = cp_lexer_peek_token (parser->lexer);
17303 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17304 /* For historical reasons, we accept an optional semicolon. */
17305 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17306 cp_lexer_consume_token (parser->lexer);
17309 /* Parse an Objective-C protocol declaration. */
17312 cp_parser_objc_protocol_declaration (cp_parser* parser)
17314 tree proto, protorefs;
17317 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17318 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17320 error ("identifier expected after %<@protocol%>");
17324 /* See if we have a forward declaration or a definition. */
17325 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17327 /* Try a forward declaration first. */
17328 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17330 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17332 cp_parser_consume_semicolon_at_end_of_statement (parser);
17335 /* Ok, we got a full-fledged definition (or at least should). */
17338 proto = cp_parser_identifier (parser);
17339 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17340 objc_start_protocol (proto, protorefs);
17341 cp_parser_objc_method_prototype_list (parser);
17345 /* Parse an Objective-C superclass or category. */
17348 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17351 cp_token *next = cp_lexer_peek_token (parser->lexer);
17353 *super = *categ = NULL_TREE;
17354 if (next->type == CPP_COLON)
17356 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17357 *super = cp_parser_identifier (parser);
17359 else if (next->type == CPP_OPEN_PAREN)
17361 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17362 *categ = cp_parser_identifier (parser);
17363 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17367 /* Parse an Objective-C class interface. */
17370 cp_parser_objc_class_interface (cp_parser* parser)
17372 tree name, super, categ, protos;
17374 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17375 name = cp_parser_identifier (parser);
17376 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17377 protos = cp_parser_objc_protocol_refs_opt (parser);
17379 /* We have either a class or a category on our hands. */
17381 objc_start_category_interface (name, categ, protos);
17384 objc_start_class_interface (name, super, protos);
17385 /* Handle instance variable declarations, if any. */
17386 cp_parser_objc_class_ivars (parser);
17387 objc_continue_interface ();
17390 cp_parser_objc_method_prototype_list (parser);
17393 /* Parse an Objective-C class implementation. */
17396 cp_parser_objc_class_implementation (cp_parser* parser)
17398 tree name, super, categ;
17400 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17401 name = cp_parser_identifier (parser);
17402 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17404 /* We have either a class or a category on our hands. */
17406 objc_start_category_implementation (name, categ);
17409 objc_start_class_implementation (name, super);
17410 /* Handle instance variable declarations, if any. */
17411 cp_parser_objc_class_ivars (parser);
17412 objc_continue_implementation ();
17415 cp_parser_objc_method_definition_list (parser);
17418 /* Consume the @end token and finish off the implementation. */
17421 cp_parser_objc_end_implementation (cp_parser* parser)
17423 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17424 objc_finish_implementation ();
17427 /* Parse an Objective-C declaration. */
17430 cp_parser_objc_declaration (cp_parser* parser)
17432 /* Try to figure out what kind of declaration is present. */
17433 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17435 switch (kwd->keyword)
17438 cp_parser_objc_alias_declaration (parser);
17441 cp_parser_objc_class_declaration (parser);
17443 case RID_AT_PROTOCOL:
17444 cp_parser_objc_protocol_declaration (parser);
17446 case RID_AT_INTERFACE:
17447 cp_parser_objc_class_interface (parser);
17449 case RID_AT_IMPLEMENTATION:
17450 cp_parser_objc_class_implementation (parser);
17453 cp_parser_objc_end_implementation (parser);
17456 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17457 cp_parser_skip_to_end_of_block_or_statement (parser);
17461 /* Parse an Objective-C try-catch-finally statement.
17463 objc-try-catch-finally-stmt:
17464 @try compound-statement objc-catch-clause-seq [opt]
17465 objc-finally-clause [opt]
17467 objc-catch-clause-seq:
17468 objc-catch-clause objc-catch-clause-seq [opt]
17471 @catch ( exception-declaration ) compound-statement
17473 objc-finally-clause
17474 @finally compound-statement
17476 Returns NULL_TREE. */
17479 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17480 location_t location;
17483 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17484 location = cp_lexer_peek_token (parser->lexer)->location;
17485 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17486 node, lest it get absorbed into the surrounding block. */
17487 stmt = push_stmt_list ();
17488 cp_parser_compound_statement (parser, NULL, false);
17489 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17491 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17493 cp_parameter_declarator *parmdecl;
17496 cp_lexer_consume_token (parser->lexer);
17497 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17498 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17499 parm = grokdeclarator (parmdecl->declarator,
17500 &parmdecl->decl_specifiers,
17501 PARM, /*initialized=*/0,
17502 /*attrlist=*/NULL);
17503 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17504 objc_begin_catch_clause (parm);
17505 cp_parser_compound_statement (parser, NULL, false);
17506 objc_finish_catch_clause ();
17509 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17511 cp_lexer_consume_token (parser->lexer);
17512 location = cp_lexer_peek_token (parser->lexer)->location;
17513 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17514 node, lest it get absorbed into the surrounding block. */
17515 stmt = push_stmt_list ();
17516 cp_parser_compound_statement (parser, NULL, false);
17517 objc_build_finally_clause (location, pop_stmt_list (stmt));
17520 return objc_finish_try_stmt ();
17523 /* Parse an Objective-C synchronized statement.
17525 objc-synchronized-stmt:
17526 @synchronized ( expression ) compound-statement
17528 Returns NULL_TREE. */
17531 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17532 location_t location;
17535 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17537 location = cp_lexer_peek_token (parser->lexer)->location;
17538 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17539 lock = cp_parser_expression (parser, false);
17540 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17542 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17543 node, lest it get absorbed into the surrounding block. */
17544 stmt = push_stmt_list ();
17545 cp_parser_compound_statement (parser, NULL, false);
17547 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17550 /* Parse an Objective-C throw statement.
17553 @throw assignment-expression [opt] ;
17555 Returns a constructed '@throw' statement. */
17558 cp_parser_objc_throw_statement (cp_parser *parser) {
17559 tree expr = NULL_TREE;
17561 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17564 expr = cp_parser_assignment_expression (parser, false);
17566 cp_parser_consume_semicolon_at_end_of_statement (parser);
17568 return objc_build_throw_stmt (expr);
17571 /* Parse an Objective-C statement. */
17574 cp_parser_objc_statement (cp_parser * parser) {
17575 /* Try to figure out what kind of declaration is present. */
17576 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17578 switch (kwd->keyword)
17581 return cp_parser_objc_try_catch_finally_statement (parser);
17582 case RID_AT_SYNCHRONIZED:
17583 return cp_parser_objc_synchronized_statement (parser);
17585 return cp_parser_objc_throw_statement (parser);
17587 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17588 cp_parser_skip_to_end_of_block_or_statement (parser);
17591 return error_mark_node;
17595 static GTY (()) cp_parser *the_parser;
17598 /* Special handling for the first token or line in the file. The first
17599 thing in the file might be #pragma GCC pch_preprocess, which loads a
17600 PCH file, which is a GC collection point. So we need to handle this
17601 first pragma without benefit of an existing lexer structure.
17603 Always returns one token to the caller in *FIRST_TOKEN. This is
17604 either the true first token of the file, or the first token after
17605 the initial pragma. */
17608 cp_parser_initial_pragma (cp_token *first_token)
17612 cp_lexer_get_preprocessor_token (NULL, first_token);
17613 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
17616 cp_lexer_get_preprocessor_token (NULL, first_token);
17617 if (first_token->type == CPP_STRING)
17619 name = first_token->value;
17621 cp_lexer_get_preprocessor_token (NULL, first_token);
17622 if (first_token->type != CPP_PRAGMA_EOL)
17623 error ("junk at end of %<#pragma GCC pch_preprocess%>");
17626 error ("expected string literal");
17628 /* Skip to the end of the pragma. */
17629 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
17630 cp_lexer_get_preprocessor_token (NULL, first_token);
17632 /* Read one more token to return to our caller. */
17633 cp_lexer_get_preprocessor_token (NULL, first_token);
17635 /* Now actually load the PCH file. */
17637 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
17640 /* Normal parsing of a pragma token. Here we can (and must) use the
17644 cp_parser_pragma (cp_parser *parser, enum pragma_context context ATTRIBUTE_UNUSED)
17646 cp_token *pragma_tok;
17649 pragma_tok = cp_lexer_consume_token (parser->lexer);
17650 gcc_assert (pragma_tok->type == CPP_PRAGMA);
17651 parser->lexer->in_pragma = true;
17653 id = pragma_tok->pragma_kind;
17656 case PRAGMA_GCC_PCH_PREPROCESS:
17657 error ("%<#pragma GCC pch_preprocess%> must be first");
17661 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
17662 c_invoke_pragma_handler (id);
17666 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
17670 /* The interface the pragma parsers have to the lexer. */
17673 pragma_lex (tree *value)
17676 enum cpp_ttype ret;
17678 tok = cp_lexer_peek_token (the_parser->lexer);
17681 *value = tok->value;
17683 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
17685 else if (ret == CPP_STRING)
17686 *value = cp_parser_string_literal (the_parser, false, false);
17689 cp_lexer_consume_token (the_parser->lexer);
17690 if (ret == CPP_KEYWORD)
17698 /* External interface. */
17700 /* Parse one entire translation unit. */
17703 c_parse_file (void)
17705 bool error_occurred;
17706 static bool already_called = false;
17708 if (already_called)
17710 sorry ("inter-module optimizations not implemented for C++");
17713 already_called = true;
17715 the_parser = cp_parser_new ();
17716 push_deferring_access_checks (flag_access_control
17717 ? dk_no_deferred : dk_no_check);
17718 error_occurred = cp_parser_translation_unit (the_parser);
17722 /* This variable must be provided by every front end. */
17726 #include "gt-cp-parser.h"